Home | History | Annotate | Line # | Download | only in pci
ahcisata_pci.c revision 1.6
      1 /*	$NetBSD: ahcisata_pci.c,v 1.6 2008/02/23 14:53:31 mlelstv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: ahcisata_pci.c,v 1.6 2008/02/23 14:53:31 mlelstv Exp $");
     35 
     36 #include <sys/types.h>
     37 #include <sys/malloc.h>
     38 #include <sys/param.h>
     39 #include <sys/kernel.h>
     40 #include <sys/systm.h>
     41 #include <sys/disklabel.h>
     42 #include <sys/pmf.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <dev/pci/pcivar.h>
     47 #include <dev/pci/pcidevs.h>
     48 #include <dev/pci/pciidereg.h>
     49 #include <dev/pci/pciidevar.h>
     50 #include <dev/ic/ahcisatavar.h>
     51 
     52 struct ahci_pci_softc {
     53 	struct ahci_softc ah_sc; /* must come first, struct device */
     54 	pci_chipset_tag_t sc_pc;
     55 	pcitag_t sc_pcitag;
     56 };
     57 
     58 
     59 static int  ahci_pci_match(struct device *, struct cfdata *, void *);
     60 static void ahci_pci_attach(struct device *, struct device *, void *);
     61 static bool ahci_pci_resume(device_t);
     62 
     63 
     64 CFATTACH_DECL(ahcisata_pci, sizeof(struct ahci_pci_softc),
     65     ahci_pci_match, ahci_pci_attach, NULL, NULL);
     66 
     67 static int
     68 ahci_pci_match(struct device *parent, struct cfdata *match,
     69     void *aux)
     70 {
     71 	struct pci_attach_args *pa = aux;
     72 	bus_space_tag_t regt;
     73 	bus_space_handle_t regh;
     74 	bus_size_t size;
     75 	int ret = 0;
     76 
     77 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
     78 	    ((PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_SATA &&
     79 	     PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_SATA_AHCI) ||
     80 	     PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID)) {
     81 		/* check if the chip is in ahci mode */
     82 		if (pci_mapreg_map(pa, AHCI_PCI_ABAR,
     83 		    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
     84 		    &regt, &regh, NULL, &size) != 0)
     85 			return 0;
     86 		if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_SATA
     87 		    && PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_SATA_AHCI)
     88 			ret = 3;
     89 		else if (bus_space_read_4(regt, regh, AHCI_GHC) & AHCI_GHC_AE)
     90 			ret = 3;
     91 		bus_space_unmap(regt, regh, size);
     92 		return ret;
     93 	}
     94 
     95 	return ret;
     96 }
     97 
     98 static void
     99 ahci_pci_attach(struct device *parent, struct device *self, void *aux)
    100 {
    101 	struct pci_attach_args *pa = aux;
    102 	struct ahci_pci_softc *psc = (struct ahci_pci_softc *)self;
    103 	struct ahci_softc *sc = &psc->ah_sc;
    104 	bus_size_t size;
    105 	char devinfo[256];
    106 	const char *intrstr;
    107 	pci_intr_handle_t intrhandle;
    108 	void *ih;
    109 
    110 	if (pci_mapreg_map(pa, AHCI_PCI_ABAR,
    111 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
    112 	    &sc->sc_ahcit, &sc->sc_ahcih, NULL, &size) != 0) {
    113 		aprint_error("%s: can't map ahci registers\n", AHCINAME(sc));
    114 		return;
    115 	}
    116 	psc->sc_pc = pa->pa_pc;
    117 	psc->sc_pcitag = pa->pa_tag;
    118 
    119 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    120 	aprint_naive(": AHCI disk controller\n");
    121 	aprint_normal(": %s\n", devinfo);
    122 
    123 	if (pci_intr_map(pa, &intrhandle) != 0) {
    124 		aprint_error("%s: couldn't map interrupt\n", AHCINAME(sc));
    125 		return;
    126 	}
    127 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    128 	ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_BIO, ahci_intr, sc);
    129 	if (ih == NULL) {
    130 		aprint_error("%s: couldn't establish interrupt", AHCINAME(sc));
    131 		return;
    132 	}
    133 	aprint_normal("%s: interrupting at %s\n", AHCINAME(sc),
    134 	    intrstr ? intrstr : "unknown interrupt");
    135 	sc->sc_dmat = pa->pa_dmat;
    136 
    137 	if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID) {
    138 		aprint_normal("%s: RAID mode\n", AHCINAME(sc));
    139 		sc->sc_atac_capflags = ATAC_CAP_RAID;
    140 	} else {
    141 		aprint_normal("%s: SATA mode\n", AHCINAME(sc));
    142 	}
    143 
    144 	ahci_attach(sc);
    145 
    146 	if (!pmf_device_register(self, NULL, ahci_pci_resume))
    147 		aprint_error_dev(self, "couldn't establish power handler\n");
    148 }
    149 
    150 static bool
    151 ahci_pci_resume(device_t dv)
    152 {
    153 	struct ahci_pci_softc *psc = device_private(dv);
    154 	struct ahci_softc *sc = &psc->ah_sc;
    155 	int s;
    156 
    157 	s = splbio();
    158 	ahci_reset(sc);
    159 	ahci_setup_ports(sc);
    160 	ahci_reprobe_drives(sc);
    161 	ahci_enable_intrs(sc);
    162 	splx(s);
    163 
    164 	return true;
    165 }
    166