Home | History | Annotate | Line # | Download | only in pci
svwsata.c revision 1.16
      1  1.16    bouyer /*	$NetBSD: svwsata.c,v 1.16 2012/07/31 15:50:36 bouyer Exp $	*/
      2   1.1    bouyer 
      3   1.1    bouyer /*
      4   1.1    bouyer  * Copyright (c) 2005 Mark Kettenis
      5   1.1    bouyer  *
      6   1.1    bouyer  * Permission to use, copy, modify, and distribute this software for any
      7   1.1    bouyer  * purpose with or without fee is hereby granted, provided that the above
      8   1.1    bouyer  * copyright notice and this permission notice appear in all copies.
      9   1.1    bouyer  *
     10   1.1    bouyer  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11   1.1    bouyer  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12   1.1    bouyer  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13   1.1    bouyer  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14   1.1    bouyer  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15   1.1    bouyer  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16   1.1    bouyer  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17   1.1    bouyer  */
     18   1.1    bouyer 
     19   1.1    bouyer #include <sys/cdefs.h>
     20  1.16    bouyer __KERNEL_RCSID(0, "$NetBSD: svwsata.c,v 1.16 2012/07/31 15:50:36 bouyer Exp $");
     21   1.1    bouyer 
     22   1.1    bouyer #include <sys/param.h>
     23   1.1    bouyer #include <sys/systm.h>
     24   1.1    bouyer 
     25   1.1    bouyer #include <dev/ata/atareg.h>
     26   1.1    bouyer #include <dev/ata/satareg.h>
     27   1.1    bouyer #include <dev/ata/satavar.h>
     28   1.1    bouyer #include <dev/pci/pcivar.h>
     29   1.1    bouyer #include <dev/pci/pcidevs.h>
     30   1.1    bouyer #include <dev/pci/pciidereg.h>
     31   1.1    bouyer #include <dev/pci/pciidevar.h>
     32   1.1    bouyer #include <dev/pci/pciide_svwsata_reg.h>
     33   1.1    bouyer 
     34  1.10      cube static int  svwsata_match(device_t, cfdata_t, void *);
     35  1.10      cube static void svwsata_attach(device_t, device_t, void *);
     36   1.1    bouyer 
     37  1.13    dyoung static void svwsata_chip_map(struct pciide_softc *,
     38  1.13    dyoung     const struct pci_attach_args *);
     39  1.13    dyoung static void svwsata_mapreg_dma(struct pciide_softc *,
     40  1.13    dyoung     const struct pci_attach_args *);
     41   1.1    bouyer static void svwsata_mapchan(struct pciide_channel *);
     42   1.1    bouyer 
     43  1.10      cube CFATTACH_DECL_NEW(svwsata, sizeof(struct pciide_softc),
     44   1.1    bouyer     svwsata_match, svwsata_attach, NULL, NULL);
     45   1.1    bouyer 
     46   1.1    bouyer static const struct pciide_product_desc pciide_svwsata_products[] =  {
     47   1.1    bouyer 	{ PCI_PRODUCT_SERVERWORKS_K2_SATA,
     48   1.1    bouyer 	  0,
     49   1.1    bouyer 	  "ServerWorks K2 SATA Controller",
     50   1.1    bouyer 	  svwsata_chip_map
     51   1.1    bouyer 	},
     52   1.2    bouyer 	{ PCI_PRODUCT_SERVERWORKS_FRODO4_SATA,
     53   1.2    bouyer 	  0,
     54   1.2    bouyer 	  "ServerWorks Frodo4 SATA Controller",
     55   1.2    bouyer 	  svwsata_chip_map
     56   1.2    bouyer 	},
     57   1.2    bouyer 	{ PCI_PRODUCT_SERVERWORKS_FRODO8_SATA,
     58   1.2    bouyer 	  0,
     59   1.2    bouyer 	  "ServerWorks Frodo8 SATA Controller",
     60   1.2    bouyer 	  svwsata_chip_map
     61   1.2    bouyer 	},
     62   1.8   xtraeme 	{ PCI_PRODUCT_SERVERWORKS_HT1000_SATA_1,
     63   1.8   xtraeme 	  0,
     64   1.8   xtraeme 	  "ServerWorks HT-1000 SATA Controller",
     65   1.8   xtraeme 	  svwsata_chip_map
     66   1.8   xtraeme 	},
     67   1.8   xtraeme 	{ PCI_PRODUCT_SERVERWORKS_HT1000_SATA_2,
     68   1.2    bouyer 	  0,
     69   1.2    bouyer 	  "ServerWorks HT-1000 SATA Controller",
     70   1.2    bouyer 	  svwsata_chip_map
     71   1.2    bouyer 	},
     72   1.1    bouyer 	{ 0,
     73   1.1    bouyer 	  0,
     74   1.1    bouyer 	  NULL,
     75   1.1    bouyer 	  NULL,
     76   1.1    bouyer 	}
     77   1.1    bouyer };
     78   1.1    bouyer 
     79   1.1    bouyer static int
     80  1.10      cube svwsata_match(device_t parent, cfdata_t match, void *aux)
     81   1.1    bouyer {
     82   1.1    bouyer 	struct pci_attach_args *pa = aux;
     83   1.1    bouyer 
     84   1.1    bouyer 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SERVERWORKS) {
     85   1.1    bouyer 		if (pciide_lookup_product(pa->pa_id,
     86   1.1    bouyer 		    pciide_svwsata_products))
     87   1.1    bouyer 			return (2);
     88   1.1    bouyer 	}
     89   1.1    bouyer 	return (0);
     90   1.1    bouyer }
     91   1.1    bouyer 
     92   1.1    bouyer static void
     93  1.10      cube svwsata_attach(device_t parent, device_t self, void *aux)
     94   1.1    bouyer {
     95   1.1    bouyer 	struct pci_attach_args *pa = aux;
     96  1.10      cube 	struct pciide_softc *sc = device_private(self);
     97  1.10      cube 
     98  1.10      cube 	sc->sc_wdcdev.sc_atac.atac_dev = self;
     99   1.1    bouyer 
    100   1.1    bouyer 	pciide_common_attach(sc, pa,
    101   1.1    bouyer 	    pciide_lookup_product(pa->pa_id, pciide_svwsata_products));
    102   1.1    bouyer }
    103   1.1    bouyer 
    104   1.1    bouyer static void
    105  1.13    dyoung svwsata_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
    106   1.1    bouyer {
    107   1.1    bouyer 	struct pciide_channel *cp;
    108   1.1    bouyer 	pci_intr_handle_t intrhandle;
    109   1.1    bouyer 	pcireg_t interface;
    110   1.1    bouyer 	const char *intrstr;
    111   1.1    bouyer 	int channel;
    112   1.1    bouyer 
    113   1.1    bouyer 	if (pciide_chipen(sc, pa) == 0)
    114   1.1    bouyer 		return;
    115   1.1    bouyer 
    116   1.2    bouyer 	/* The 4-port version has a dummy second function. */
    117   1.2    bouyer 	if (pci_conf_read(sc->sc_pc, sc->sc_tag,
    118   1.2    bouyer 	    PCI_MAPREG_START + 0x14) == 0) {
    119   1.2    bouyer 		aprint_normal("\n");
    120   1.2    bouyer 		return;
    121   1.2    bouyer 	}
    122   1.2    bouyer 
    123   1.1    bouyer 	if (pci_mapreg_map(pa, PCI_MAPREG_START + 0x14,
    124   1.1    bouyer 			   PCI_MAPREG_TYPE_MEM |
    125   1.1    bouyer 			   PCI_MAPREG_MEM_TYPE_32BIT, 0,
    126   1.1    bouyer 			   &sc->sc_ba5_st, &sc->sc_ba5_sh,
    127  1.12  jakllsch 			   NULL, &sc->sc_ba5_ss) != 0) {
    128   1.1    bouyer 		aprint_error(": unable to map BA5 register space\n");
    129   1.1    bouyer 		return;
    130   1.1    bouyer 	}
    131   1.1    bouyer 
    132  1.10      cube 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    133  1.10      cube 	    "bus-master DMA support present");
    134   1.1    bouyer 	svwsata_mapreg_dma(sc, pa);
    135   1.9    simonb 	aprint_verbose("\n");
    136   1.1    bouyer 
    137   1.7   hannken 	sc->sc_wdcdev.cap = WDC_CAPABILITY_WIDEREGS;
    138   1.7   hannken 
    139   1.1    bouyer 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    140   1.1    bouyer 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    141   1.1    bouyer 	if (sc->sc_dma_ok) {
    142   1.1    bouyer 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    143   1.1    bouyer 		sc->sc_wdcdev.irqack = pciide_irqack;
    144   1.1    bouyer 		sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    145   1.1    bouyer 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    146   1.1    bouyer 	}
    147   1.1    bouyer 
    148   1.1    bouyer 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    149   1.1    bouyer 	sc->sc_wdcdev.sc_atac.atac_nchannels = 4;
    150   1.1    bouyer 	sc->sc_wdcdev.sc_atac.atac_set_modes = sata_setup_channel;
    151   1.1    bouyer 
    152   1.1    bouyer 	/* We can use SControl and SStatus to probe for drives. */
    153   1.5    bouyer 	sc->sc_wdcdev.sc_atac.atac_probe = wdc_sataprobe;
    154  1.16    bouyer 	sc->sc_wdcdev.wdc_maxdrives = 1;
    155   1.1    bouyer 
    156   1.1    bouyer 	wdc_allocate_regs(&sc->sc_wdcdev);
    157   1.1    bouyer 
    158   1.1    bouyer 	/* Map and establish the interrupt handler. */
    159   1.1    bouyer 	if(pci_intr_map(pa, &intrhandle) != 0) {
    160  1.10      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    161  1.10      cube 		    "couldn't map native-PCI interrupt\n");
    162   1.1    bouyer 		return;
    163   1.1    bouyer 	}
    164   1.1    bouyer 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    165   1.1    bouyer 	sc->sc_pci_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_BIO,
    166   1.1    bouyer 	    pciide_pci_intr, sc);
    167   1.1    bouyer 	if (sc->sc_pci_ih != NULL) {
    168  1.10      cube 		aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    169  1.10      cube 		    "using %s for native-PCI interrupt\n",
    170   1.1    bouyer 		    intrstr ? intrstr : "unknown interrupt");
    171   1.1    bouyer 	} else {
    172  1.10      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    173  1.10      cube 		    "couldn't establish native-PCI interrupt");
    174   1.1    bouyer 		if (intrstr != NULL)
    175  1.11     njoly 			aprint_error(" at %s", intrstr);
    176  1.11     njoly 		aprint_error("\n");
    177   1.1    bouyer 		return;
    178   1.1    bouyer 	}
    179   1.1    bouyer 
    180   1.1    bouyer 	interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
    181   1.1    bouyer 	    PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
    182   1.1    bouyer 
    183   1.1    bouyer 
    184   1.1    bouyer 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    185   1.1    bouyer 	     channel++) {
    186   1.1    bouyer 		cp = &sc->pciide_channels[channel];
    187   1.1    bouyer 
    188   1.1    bouyer 		if (pciide_chansetup(sc, channel, interface) == 0)
    189   1.1    bouyer 			continue;
    190   1.1    bouyer 		svwsata_mapchan(cp);
    191   1.1    bouyer 	}
    192   1.1    bouyer }
    193   1.1    bouyer 
    194   1.1    bouyer static void
    195  1.13    dyoung svwsata_mapreg_dma(struct pciide_softc *sc, const struct pci_attach_args *pa)
    196   1.1    bouyer {
    197   1.1    bouyer 	struct pciide_channel *pc;
    198   1.1    bouyer 	int chan, reg;
    199   1.1    bouyer 	bus_size_t size;
    200   1.1    bouyer 
    201   1.1    bouyer 	sc->sc_wdcdev.dma_arg = sc;
    202   1.1    bouyer 	sc->sc_wdcdev.dma_init = pciide_dma_init;
    203   1.1    bouyer 	sc->sc_wdcdev.dma_start = pciide_dma_start;
    204   1.1    bouyer 	sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    205   1.1    bouyer 
    206  1.10      cube 	if (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
    207   1.1    bouyer 	    PCIIDE_OPTIONS_NODMA) {
    208   1.1    bouyer 		aprint_normal(
    209   1.1    bouyer 		    ", but unused (forced off by config file)");
    210   1.1    bouyer 		sc->sc_dma_ok = 0;
    211   1.1    bouyer 		return;
    212   1.1    bouyer 	}
    213   1.1    bouyer 
    214   1.1    bouyer 	/*
    215   1.1    bouyer 	 * Slice off a subregion of BA5 for each of the channel's DMA
    216   1.1    bouyer 	 * registers.
    217   1.1    bouyer 	 */
    218   1.1    bouyer 
    219   1.1    bouyer 	sc->sc_dma_iot = sc->sc_ba5_st;
    220   1.1    bouyer 	for (chan = 0; chan < 4; chan++) {
    221   1.1    bouyer 		pc = &sc->pciide_channels[chan];
    222   1.1    bouyer 		for (reg = 0; reg < IDEDMA_NREGS; reg++) {
    223   1.1    bouyer 			size = 4;
    224   1.1    bouyer 			if (size > (IDEDMA_SCH_OFFSET - reg))
    225   1.1    bouyer 				size = IDEDMA_SCH_OFFSET - reg;
    226   1.1    bouyer 			if (bus_space_subregion(sc->sc_ba5_st,
    227   1.1    bouyer 			    sc->sc_ba5_sh,
    228   1.1    bouyer 			    (chan << 8) + SVWSATA_DMA + reg,
    229   1.1    bouyer 			    size, &pc->dma_iohs[reg]) != 0) {
    230   1.1    bouyer 				sc->sc_dma_ok = 0;
    231   1.1    bouyer 				aprint_normal(", but can't subregion offset "
    232   1.1    bouyer 				    "%lu size %lu",
    233   1.1    bouyer 				    (u_long) (chan << 8) + SVWSATA_DMA + reg,
    234   1.1    bouyer 				    (u_long) size);
    235   1.1    bouyer 				return;
    236   1.1    bouyer 			}
    237   1.1    bouyer 		}
    238   1.1    bouyer 	}
    239   1.1    bouyer 
    240   1.1    bouyer 	/* DMA registers all set up! */
    241   1.1    bouyer 	sc->sc_dmat = pa->pa_dmat;
    242   1.1    bouyer 	sc->sc_dma_ok = 1;
    243   1.1    bouyer }
    244   1.1    bouyer 
    245   1.1    bouyer static void
    246   1.1    bouyer svwsata_mapchan(struct pciide_channel *cp)
    247   1.1    bouyer {
    248   1.1    bouyer 	struct ata_channel *wdc_cp = &cp->ata_channel;
    249   1.1    bouyer 	struct pciide_softc *sc = CHAN_TO_PCIIDE(wdc_cp);
    250   1.1    bouyer 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp);
    251   1.1    bouyer 	int i;
    252   1.1    bouyer 
    253   1.1    bouyer 	cp->compat = 0;
    254   1.1    bouyer 	cp->ih = sc->sc_pci_ih;
    255   1.1    bouyer 
    256   1.1    bouyer 	wdr->cmd_iot = sc->sc_ba5_st;
    257   1.1    bouyer 	if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    258   1.1    bouyer 		(wdc_cp->ch_channel << 8) + SVWSATA_TF0,
    259   1.1    bouyer 		SVWSATA_TF8 - SVWSATA_TF0, &wdr->cmd_baseioh) != 0) {
    260  1.10      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    261  1.10      cube 		    "couldn't map %s cmd regs\n", cp->name);
    262   1.1    bouyer 		goto bad;
    263   1.1    bouyer 	}
    264   1.1    bouyer 
    265   1.1    bouyer 	wdr->ctl_iot = sc->sc_ba5_st;
    266   1.1    bouyer 	if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    267   1.1    bouyer 		(wdc_cp->ch_channel << 8) + SVWSATA_TF8, 4,
    268   1.1    bouyer 		&cp->ctl_baseioh) != 0) {
    269  1.10      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    270  1.10      cube 		    "couldn't map %s ctl regs\n", cp->name);
    271   1.1    bouyer 		goto bad;
    272   1.1    bouyer 	}
    273   1.1    bouyer 	wdr->ctl_ioh = cp->ctl_baseioh;
    274   1.1    bouyer 
    275   1.1    bouyer 	for (i = 0; i < WDC_NREG; i++) {
    276   1.1    bouyer 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
    277   1.1    bouyer 					i << 2, i == 0 ? 4 : 1,
    278   1.1    bouyer 					&wdr->cmd_iohs[i]) != 0) {
    279  1.10      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    280  1.10      cube 			    "couldn't subregion %s channel cmd regs\n",
    281  1.10      cube 			    cp->name);
    282   1.1    bouyer 			goto bad;
    283   1.1    bouyer 		}
    284   1.1    bouyer 	}
    285   1.1    bouyer 	wdc_init_shadow_regs(wdc_cp);
    286   1.1    bouyer 	wdr->data32iot = wdr->cmd_iot;
    287   1.1    bouyer 	wdr->data32ioh = wdr->cmd_iohs[0];
    288   1.1    bouyer 
    289   1.5    bouyer 
    290   1.5    bouyer 	wdr->sata_iot = sc->sc_ba5_st;
    291   1.5    bouyer 	wdr->sata_baseioh = sc->sc_ba5_sh;
    292   1.5    bouyer 	if (bus_space_subregion(wdr->sata_iot, wdr->sata_baseioh,
    293   1.5    bouyer 	    (wdc_cp->ch_channel << 8) + SVWSATA_SSTATUS, 1,
    294   1.5    bouyer 	    &wdr->sata_status) != 0) {
    295  1.10      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    296  1.10      cube 		    "couldn't map channel %d sata_status regs\n",
    297   1.5    bouyer 		    wdc_cp->ch_channel);
    298   1.5    bouyer 		goto bad;
    299   1.5    bouyer 	}
    300   1.5    bouyer 	if (bus_space_subregion(wdr->sata_iot, wdr->sata_baseioh,
    301   1.5    bouyer 	    (wdc_cp->ch_channel << 8) + SVWSATA_SERROR, 1,
    302   1.5    bouyer 	    &wdr->sata_error) != 0) {
    303  1.10      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    304  1.10      cube 		   "couldn't map channel %d sata_error regs\n",
    305   1.5    bouyer 		    wdc_cp->ch_channel);
    306   1.5    bouyer 		goto bad;
    307   1.5    bouyer 	}
    308   1.5    bouyer 	if (bus_space_subregion(wdr->sata_iot, wdr->sata_baseioh,
    309   1.5    bouyer 	    (wdc_cp->ch_channel << 8) + SVWSATA_SCONTROL, 1,
    310   1.5    bouyer 	    &wdr->sata_control) != 0) {
    311  1.10      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    312  1.10      cube 		    "couldn't map channel %d sata_control regs\n",
    313   1.5    bouyer 		    wdc_cp->ch_channel);
    314   1.5    bouyer 		goto bad;
    315   1.5    bouyer 	}
    316   1.5    bouyer 
    317   1.1    bouyer 	wdcattach(wdc_cp);
    318   1.1    bouyer 	return;
    319   1.1    bouyer 
    320   1.1    bouyer  bad:
    321   1.1    bouyer 	cp->ata_channel.ch_flags |= ATACH_DISABLED;
    322   1.1    bouyer }
    323