1/* $NetBSD: amdpcib_hpet.c,v 1.7 2011/06/15 08:19:43 jruoho Exp $ */ 2 3/* 4 * Copyright (c) 2006 Nicolas Joly 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include <sys/cdefs.h> 32__KERNEL_RCSID(0, "$NetBSD: amdpcib_hpet.c,v 1.7 2011/06/15 08:19:43 jruoho Exp $"); 33 34#include <sys/systm.h> 35#include <sys/device.h> 36 37#include <sys/time.h> 38#include <sys/timetc.h> 39 40#include <sys/bus.h> 41 42#include <dev/pci/pcivar.h> 43#include <dev/pci/pcireg.h> 44#include <dev/pci/pcidevs.h> 45 46#include <dev/ic/hpetreg.h> 47#include <dev/ic/hpetvar.h> 48 49static int amdpcib_hpet_match(device_t , cfdata_t , void *); 50static void amdpcib_hpet_attach(device_t, device_t, void *); 51static int amdpcib_hpet_detach(device_t, int); 52static pcireg_t amdpcib_hpet_addr(struct pci_attach_args *); 53 54CFATTACH_DECL_NEW(amdpcib_hpet, sizeof(struct hpet_softc), 55 amdpcib_hpet_match, amdpcib_hpet_attach, amdpcib_hpet_detach, NULL); 56 57static int 58amdpcib_hpet_match(device_t parent, cfdata_t match, void *aux) 59{ 60 struct pci_attach_args *pa = aux; 61 bus_space_handle_t bh; 62 bus_space_tag_t bt; 63 pcireg_t addr; 64 65 addr = amdpcib_hpet_addr(pa); 66 67 if (addr == 0) 68 return 0; 69 70 bt = pa->pa_memt; 71 72 if (bus_space_map(bt, addr, HPET_WINDOW_SIZE, 0, &bh) != 0) 73 return 0; 74 75 bus_space_unmap(bt, bh, HPET_WINDOW_SIZE); 76 77 return 1; 78} 79 80static void 81amdpcib_hpet_attach(device_t parent, device_t self, void *aux) 82{ 83 struct hpet_softc *sc = device_private(self); 84 struct pci_attach_args *pa = aux; 85 pcireg_t addr; 86 87 sc->sc_mapped = false; 88 addr = amdpcib_hpet_addr(pa); 89 90 if (addr == 0) { 91 aprint_error(": failed to get address\n"); 92 return; 93 } 94 95 sc->sc_memt = pa->pa_memt; 96 sc->sc_mems = HPET_WINDOW_SIZE; 97 98 if (bus_space_map(sc->sc_memt, addr, sc->sc_mems, 0, &sc->sc_memh)) { 99 aprint_error(": failed to map mem space\n"); 100 return; 101 } 102 103 aprint_naive("\n"); 104 aprint_normal(": high precision event timer (mem 0x%08x-0x%08x)\n", 105 addr, addr + HPET_WINDOW_SIZE); 106 107 sc->sc_mapped = true; 108 hpet_attach_subr(self); 109} 110 111static int 112amdpcib_hpet_detach(device_t self, int flags) 113{ 114 struct hpet_softc *sc = device_private(self); 115 int rv; 116 117 if (sc->sc_mapped != true) 118 return 0; 119 120 rv = hpet_detach(self, flags); 121 122 if (rv != 0) 123 return rv; 124 125 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems); 126 127 return 0; 128} 129 130static pcireg_t 131amdpcib_hpet_addr(struct pci_attach_args *pa) 132{ 133 pcireg_t conf; 134 135 conf = pci_conf_read(pa->pa_pc, pa->pa_tag, 0xa0); 136 137 if ((conf & 1) == 0) 138 return 0; 139 140 return conf & 0xfffffc00; 141} 142