Home | History | Annotate | Line # | Download | only in pci
pdcsata.c revision 1.4.2.1
      1  1.4.2.1      yamt /*	$NetBSD: pdcsata.c,v 1.4.2.1 2006/02/01 14:52:09 yamt Exp $	*/
      2      1.1    bouyer 
      3      1.1    bouyer /*
      4      1.1    bouyer  * Copyright (c) 2004, Manuel Bouyer.
      5      1.1    bouyer  *
      6      1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      7      1.1    bouyer  * modification, are permitted provided that the following conditions
      8      1.1    bouyer  * are met:
      9      1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     10      1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     11      1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     13      1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     14      1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     15      1.1    bouyer  *    must display the following acknowledgement:
     16      1.1    bouyer  *	This product includes software developed by Manuel Bouyer.
     17      1.1    bouyer  * 4. The name of the author may not be used to endorse or promote products
     18      1.1    bouyer  *    derived from this software without specific prior written permission.
     19      1.1    bouyer  *
     20      1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21      1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22      1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23      1.3     perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24      1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25      1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26      1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27      1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28      1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29      1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30      1.1    bouyer  */
     31      1.1    bouyer 
     32      1.1    bouyer #include <sys/types.h>
     33      1.1    bouyer #include <sys/malloc.h>
     34      1.1    bouyer #include <sys/param.h>
     35      1.1    bouyer #include <sys/systm.h>
     36      1.1    bouyer 
     37      1.1    bouyer #include <dev/pci/pcivar.h>
     38      1.1    bouyer #include <dev/pci/pcidevs.h>
     39      1.1    bouyer #include <dev/pci/pciidereg.h>
     40      1.1    bouyer #include <dev/pci/pciidevar.h>
     41      1.4  christos #include <dev/ata/atareg.h>
     42      1.4  christos #include <dev/ata/satavar.h>
     43      1.4  christos #include <dev/ata/satareg.h>
     44      1.1    bouyer 
     45      1.1    bouyer #define PDC203xx_NCHANNELS 4
     46      1.4  christos #define PDC40718_NCHANNELS 4
     47  1.4.2.1      yamt #define PDC20575_NCHANNELS 3
     48      1.1    bouyer 
     49      1.1    bouyer #define PDC203xx_BAR_IDEREGS 0x1c /* BAR where the IDE registers are mapped */
     50      1.1    bouyer 
     51      1.1    bouyer static void pdcsata_chip_map(struct pciide_softc *, struct pci_attach_args *);
     52      1.1    bouyer static void pdc203xx_setup_channel(struct ata_channel *);
     53      1.1    bouyer static int  pdc203xx_pci_intr(void *);
     54      1.1    bouyer static void pdc203xx_irqack(struct ata_channel *);
     55      1.1    bouyer static int  pdc203xx_dma_init(void *, int, int, void *, size_t, int);
     56      1.1    bouyer static void pdc203xx_dma_start(void *,int ,int);
     57      1.1    bouyer static int  pdc203xx_dma_finish(void *, int, int, int);
     58      1.1    bouyer 
     59      1.4  christos /* PDC205xx, PDC405xx and PDC407xx. but tested only pdc40718 */
     60      1.4  christos static int  pdc205xx_pci_intr(void *);
     61      1.4  christos static void pdc205xx_do_reset(struct ata_channel *, int);
     62      1.4  christos static void pdc205xx_drv_probe(struct ata_channel *);
     63      1.4  christos 
     64      1.1    bouyer static int  pdcsata_match(struct device *, struct cfdata *, void *);
     65      1.1    bouyer static void pdcsata_attach(struct device *, struct device *, void *);
     66      1.1    bouyer 
     67      1.1    bouyer CFATTACH_DECL(pdcsata, sizeof(struct pciide_softc),
     68      1.1    bouyer     pdcsata_match, pdcsata_attach, NULL, NULL);
     69      1.1    bouyer 
     70      1.1    bouyer static const struct pciide_product_desc pciide_pdcsata_products[] =  {
     71      1.1    bouyer 	{ PCI_PRODUCT_PROMISE_PDC20318,
     72      1.1    bouyer 	  0,
     73      1.1    bouyer 	  "Promise PDC20318 SATA150 controller",
     74      1.1    bouyer 	  pdcsata_chip_map,
     75      1.1    bouyer 	},
     76      1.1    bouyer 	{ PCI_PRODUCT_PROMISE_PDC20319,
     77      1.1    bouyer 	  0,
     78      1.1    bouyer 	  "Promise PDC20319 SATA150 controller",
     79      1.1    bouyer 	  pdcsata_chip_map,
     80      1.1    bouyer 	},
     81      1.1    bouyer 	{ PCI_PRODUCT_PROMISE_PDC20371,
     82      1.1    bouyer 	  0,
     83      1.1    bouyer 	  "Promise PDC20371 SATA150 controller",
     84      1.1    bouyer 	  pdcsata_chip_map,
     85      1.1    bouyer 	},
     86      1.1    bouyer 	{ PCI_PRODUCT_PROMISE_PDC20375,
     87      1.1    bouyer 	  0,
     88      1.1    bouyer 	  "Promise PDC20375 SATA150 controller",
     89      1.1    bouyer 	  pdcsata_chip_map,
     90      1.1    bouyer 	},
     91      1.1    bouyer 	{ PCI_PRODUCT_PROMISE_PDC20376,
     92      1.1    bouyer 	  0,
     93      1.1    bouyer 	  "Promise PDC20376 SATA150 controller",
     94      1.1    bouyer 	  pdcsata_chip_map,
     95      1.1    bouyer 	},
     96      1.1    bouyer 	{ PCI_PRODUCT_PROMISE_PDC20377,
     97      1.1    bouyer 	  0,
     98      1.1    bouyer 	  "Promise PDC20377 SATA150 controller",
     99      1.1    bouyer 	  pdcsata_chip_map,
    100      1.1    bouyer 	},
    101      1.1    bouyer 	{ PCI_PRODUCT_PROMISE_PDC20378,
    102      1.1    bouyer 	  0,
    103      1.1    bouyer 	  "Promise PDC20378 SATA150 controller",
    104      1.1    bouyer 	  pdcsata_chip_map,
    105      1.1    bouyer 	},
    106      1.1    bouyer 	{ PCI_PRODUCT_PROMISE_PDC20379,
    107      1.1    bouyer 	  0,
    108      1.1    bouyer 	  "Promise PDC20379 SATA150 controller",
    109      1.1    bouyer 	  pdcsata_chip_map,
    110      1.1    bouyer 	},
    111      1.4  christos 	{ PCI_PRODUCT_PROMISE_PDC40718,
    112      1.4  christos 	  0,
    113      1.4  christos 	  "Promise PDC40718 SATA300 controller",
    114      1.4  christos 	  pdcsata_chip_map,
    115      1.4  christos 	},
    116      1.4  christos 	{ PCI_PRODUCT_PROMISE_PDC40719,
    117      1.4  christos 	  0,
    118      1.4  christos 	  "Promise PDC40719 SATA300 controller",
    119      1.4  christos 	  pdcsata_chip_map,
    120      1.4  christos 	},
    121  1.4.2.1      yamt 	{ PCI_PRODUCT_PROMISE_PDC20571,
    122  1.4.2.1      yamt 	  0,
    123  1.4.2.1      yamt 	  "Promise PDC20571 SATA150 controller",
    124  1.4.2.1      yamt 	  pdcsata_chip_map,
    125  1.4.2.1      yamt 	},
    126  1.4.2.1      yamt 	{ PCI_PRODUCT_PROMISE_PDC20575,
    127  1.4.2.1      yamt 	  0,
    128  1.4.2.1      yamt 	  "Promise PDC20575 SATA150 controller",
    129  1.4.2.1      yamt 	  pdcsata_chip_map,
    130  1.4.2.1      yamt 	},
    131  1.4.2.1      yamt 	{ PCI_PRODUCT_PROMISE_PDC20579,
    132  1.4.2.1      yamt 	  0,
    133  1.4.2.1      yamt 	  "Promise PDC20579 SATA150 controller",
    134  1.4.2.1      yamt 	  pdcsata_chip_map,
    135  1.4.2.1      yamt 	},
    136      1.1    bouyer 	{ 0,
    137      1.1    bouyer 	  0,
    138      1.1    bouyer 	  NULL,
    139      1.1    bouyer 	  NULL
    140      1.1    bouyer 	}
    141      1.1    bouyer };
    142      1.1    bouyer 
    143      1.1    bouyer static int
    144      1.1    bouyer pdcsata_match(struct device *parent, struct cfdata *match, void *aux)
    145      1.1    bouyer {
    146      1.1    bouyer 	struct pci_attach_args *pa = aux;
    147      1.1    bouyer 
    148      1.1    bouyer 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_PROMISE) {
    149      1.1    bouyer 		if (pciide_lookup_product(pa->pa_id, pciide_pdcsata_products))
    150      1.1    bouyer 			return (2);
    151      1.1    bouyer 	}
    152      1.1    bouyer 	return (0);
    153      1.1    bouyer }
    154      1.1    bouyer 
    155      1.1    bouyer static void
    156      1.1    bouyer pdcsata_attach(struct device *parent, struct device *self, void *aux)
    157      1.1    bouyer {
    158      1.1    bouyer 	struct pci_attach_args *pa = aux;
    159      1.1    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)self;
    160      1.1    bouyer 
    161      1.1    bouyer 	pciide_common_attach(sc, pa,
    162      1.1    bouyer 	    pciide_lookup_product(pa->pa_id, pciide_pdcsata_products));
    163      1.1    bouyer }
    164      1.1    bouyer 
    165      1.1    bouyer static void
    166      1.1    bouyer pdcsata_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    167      1.1    bouyer {
    168      1.1    bouyer 	struct pciide_channel *cp;
    169      1.1    bouyer 	struct ata_channel *wdc_cp;
    170      1.1    bouyer 	struct wdc_regs *wdr;
    171      1.1    bouyer 	int channel, i;
    172      1.1    bouyer 	bus_size_t dmasize;
    173      1.1    bouyer 	pci_intr_handle_t intrhandle;
    174      1.1    bouyer 	const char *intrstr;
    175      1.1    bouyer 
    176      1.1    bouyer 	/*
    177      1.1    bouyer 	 * Promise SATA controllers have 3 or 4 channels,
    178      1.1    bouyer 	 * the usual IDE registers are mapped in I/O space, with offsets.
    179      1.1    bouyer 	 */
    180      1.1    bouyer 	if (pci_intr_map(pa, &intrhandle) != 0) {
    181      1.1    bouyer 		aprint_error("%s: couldn't map interrupt\n",
    182      1.1    bouyer 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    183      1.1    bouyer 		return;
    184      1.1    bouyer 	}
    185      1.1    bouyer 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    186      1.4  christos 
    187      1.4  christos 	switch (sc->sc_pp->ide_product) {
    188      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20318:
    189      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20319:
    190      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20371:
    191      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20375:
    192      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20376:
    193      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20377:
    194      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20378:
    195      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20379:
    196      1.4  christos 	default:
    197      1.4  christos 		sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
    198      1.4  christos 		    intrhandle, IPL_BIO, pdc203xx_pci_intr, sc);
    199      1.4  christos 		break;
    200      1.4  christos 
    201      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC40718:
    202      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC40719:
    203  1.4.2.1      yamt 	case PCI_PRODUCT_PROMISE_PDC20571:
    204  1.4.2.1      yamt 	case PCI_PRODUCT_PROMISE_PDC20575:
    205  1.4.2.1      yamt 	case PCI_PRODUCT_PROMISE_PDC20579:
    206      1.4  christos 		sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
    207      1.4  christos 		    intrhandle, IPL_BIO, pdc205xx_pci_intr, sc);
    208      1.4  christos 		break;
    209      1.4  christos 	}
    210      1.4  christos 
    211      1.1    bouyer 	if (sc->sc_pci_ih == NULL) {
    212      1.1    bouyer 		aprint_error("%s: couldn't establish native-PCI interrupt",
    213      1.1    bouyer 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    214      1.1    bouyer 		if (intrstr != NULL)
    215      1.1    bouyer 		    aprint_normal(" at %s", intrstr);
    216      1.1    bouyer 		aprint_normal("\n");
    217      1.1    bouyer 		return;
    218      1.1    bouyer 	}
    219      1.1    bouyer 	aprint_normal("%s: interrupting at %s\n",
    220      1.1    bouyer 		sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    221      1.1    bouyer 		intrstr ? intrstr : "unknown interrupt");
    222      1.3     perry 
    223      1.1    bouyer 	sc->sc_dma_ok = (pci_mapreg_map(pa, PCIIDE_REG_BUS_MASTER_DMA,
    224      1.1    bouyer 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &sc->sc_dma_iot,
    225      1.1    bouyer 	    &sc->sc_dma_ioh, NULL, &dmasize) == 0);
    226      1.1    bouyer 	if (!sc->sc_dma_ok) {
    227      1.1    bouyer 		aprint_error("%s: couldn't map bus-master DMA registers\n",
    228      1.1    bouyer 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    229      1.1    bouyer 		pci_intr_disestablish(pa->pa_pc, sc->sc_pci_ih);
    230      1.1    bouyer 		return;
    231      1.1    bouyer 	}
    232      1.1    bouyer 
    233      1.1    bouyer 	sc->sc_dmat = pa->pa_dmat;
    234      1.1    bouyer 
    235      1.1    bouyer 	if (pci_mapreg_map(pa, PDC203xx_BAR_IDEREGS,
    236      1.1    bouyer 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &sc->sc_ba5_st,
    237      1.1    bouyer 	    &sc->sc_ba5_sh, NULL, NULL) != 0) {
    238      1.1    bouyer 		aprint_error("%s: couldn't map IDE registers\n",
    239      1.1    bouyer 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    240      1.1    bouyer 		bus_space_unmap(sc->sc_dma_iot, sc->sc_dma_ioh, dmasize);
    241      1.1    bouyer 		pci_intr_disestablish(pa->pa_pc, sc->sc_pci_ih);
    242      1.1    bouyer 		return;
    243      1.1    bouyer 	}
    244      1.1    bouyer 
    245      1.1    bouyer 	aprint_normal("%s: bus-master DMA support present\n",
    246      1.1    bouyer 	    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    247      1.1    bouyer 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16;
    248      1.1    bouyer 	if (sc->sc_dma_ok) {
    249      1.1    bouyer 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    250      1.1    bouyer 	}
    251      1.2    bouyer 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    252      1.2    bouyer 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID)
    253      1.2    bouyer 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_RAID;
    254      1.1    bouyer 	sc->sc_wdcdev.irqack = pdc203xx_irqack;
    255      1.1    bouyer 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    256      1.1    bouyer 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    257      1.1    bouyer 	sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    258      1.1    bouyer 	sc->sc_wdcdev.sc_atac.atac_set_modes = pdc203xx_setup_channel;
    259      1.1    bouyer 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    260      1.4  christos 
    261      1.4  christos 	switch (sc->sc_pp->ide_product) {
    262      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20318:
    263      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20319:
    264      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20371:
    265      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20375:
    266      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20376:
    267      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20377:
    268      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20378:
    269      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC20379:
    270      1.4  christos 	default:
    271      1.4  christos 		bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x6c, 0x00ff0033);
    272      1.4  christos 		sc->sc_wdcdev.sc_atac.atac_nchannels =
    273      1.4  christos 		    (bus_space_read_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x48) & 0x02) ?
    274      1.4  christos 		    PDC203xx_NCHANNELS : 3;
    275      1.4  christos 
    276      1.4  christos 		break;
    277      1.4  christos 
    278      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC40718:
    279      1.4  christos 	case PCI_PRODUCT_PROMISE_PDC40719:
    280  1.4.2.1      yamt 	case PCI_PRODUCT_PROMISE_PDC20571:
    281      1.4  christos 		bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x60, 0x00ff00ff);
    282      1.4  christos 		sc->sc_wdcdev.sc_atac.atac_nchannels = PDC40718_NCHANNELS;
    283      1.4  christos 
    284      1.4  christos 		sc->sc_wdcdev.reset = pdc205xx_do_reset;
    285      1.4  christos 		sc->sc_wdcdev.sc_atac.atac_probe = pdc205xx_drv_probe;
    286      1.4  christos 
    287      1.4  christos 		break;
    288  1.4.2.1      yamt 	case PCI_PRODUCT_PROMISE_PDC20575:
    289  1.4.2.1      yamt 	case PCI_PRODUCT_PROMISE_PDC20579:
    290  1.4.2.1      yamt 		bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x60, 0x00ff00ff);
    291  1.4.2.1      yamt 		sc->sc_wdcdev.sc_atac.atac_nchannels = PDC20575_NCHANNELS;
    292  1.4.2.1      yamt 
    293  1.4.2.1      yamt 		sc->sc_wdcdev.reset = pdc205xx_do_reset;
    294  1.4.2.1      yamt 		sc->sc_wdcdev.sc_atac.atac_probe = pdc205xx_drv_probe;
    295  1.4.2.1      yamt 
    296  1.4.2.1      yamt 		break;
    297      1.4  christos 	}
    298      1.4  christos 
    299      1.1    bouyer 	wdc_allocate_regs(&sc->sc_wdcdev);
    300      1.1    bouyer 
    301      1.1    bouyer 	sc->sc_wdcdev.dma_arg = sc;
    302      1.1    bouyer 	sc->sc_wdcdev.dma_init = pdc203xx_dma_init;
    303      1.1    bouyer 	sc->sc_wdcdev.dma_start = pdc203xx_dma_start;
    304      1.1    bouyer 	sc->sc_wdcdev.dma_finish = pdc203xx_dma_finish;
    305      1.1    bouyer 
    306      1.1    bouyer 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    307      1.1    bouyer 	     channel++) {
    308      1.1    bouyer 		cp = &sc->pciide_channels[channel];
    309      1.1    bouyer 		sc->wdc_chanarray[channel] = &cp->ata_channel;
    310      1.1    bouyer 
    311      1.1    bouyer 		cp->ih = sc->sc_pci_ih;
    312      1.1    bouyer 		cp->name = NULL;
    313      1.1    bouyer 		cp->ata_channel.ch_channel = channel;
    314      1.1    bouyer 		cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    315      1.1    bouyer 		cp->ata_channel.ch_queue =
    316      1.1    bouyer 		    malloc(sizeof(struct ata_queue), M_DEVBUF, M_NOWAIT);
    317  1.4.2.1      yamt 		cp->ata_channel.ch_ndrive = 2;
    318      1.1    bouyer 		if (cp->ata_channel.ch_queue == NULL) {
    319      1.1    bouyer 			aprint_error("%s channel %d: "
    320      1.1    bouyer 			    "can't allocate memory for command queue\n",
    321      1.1    bouyer 			sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, channel);
    322      1.1    bouyer 			goto next_channel;
    323      1.1    bouyer 		}
    324      1.1    bouyer 		wdc_cp = &cp->ata_channel;
    325      1.1    bouyer 		wdr = CHAN_TO_WDC_REGS(wdc_cp);
    326      1.1    bouyer 
    327      1.1    bouyer 		wdr->ctl_iot = sc->sc_ba5_st;
    328      1.1    bouyer 		wdr->cmd_iot = sc->sc_ba5_st;
    329      1.1    bouyer 
    330      1.1    bouyer 		if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    331      1.1    bouyer 		    0x0238 + (channel << 7), 1, &wdr->ctl_ioh) != 0) {
    332      1.1    bouyer 			aprint_error("%s: couldn't map channel %d ctl regs\n",
    333      1.1    bouyer 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    334      1.1    bouyer 			    channel);
    335      1.1    bouyer 			goto next_channel;
    336      1.1    bouyer 		}
    337      1.1    bouyer 		for (i = 0; i < WDC_NREG; i++) {
    338      1.1    bouyer 			if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    339      1.1    bouyer 			    0x0200 + (i << 2) + (channel << 7), i == 0 ? 4 : 1,
    340      1.1    bouyer 			    &wdr->cmd_iohs[i]) != 0) {
    341      1.1    bouyer 				aprint_error("%s: couldn't map channel %d cmd "
    342      1.1    bouyer 				    "regs\n",
    343      1.1    bouyer 				    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    344      1.1    bouyer 				    channel);
    345      1.1    bouyer 				goto next_channel;
    346      1.1    bouyer 			}
    347      1.1    bouyer 		}
    348      1.1    bouyer 		wdc_init_shadow_regs(wdc_cp);
    349      1.1    bouyer 
    350      1.1    bouyer 		/*
    351      1.1    bouyer 		 * subregion de busmaster registers. They're spread all over
    352      1.1    bouyer 		 * the controller's register space :(. They are also 4 bytes
    353      1.1    bouyer 		 * sized, with some specific extentions in the extra bits.
    354      1.1    bouyer 		 * It also seems that the IDEDMA_CTL register isn't available.
    355      1.1    bouyer 		 */
    356      1.1    bouyer 		if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    357      1.1    bouyer 		    0x260 + (channel << 7), 1,
    358      1.1    bouyer 		    &cp->dma_iohs[IDEDMA_CMD]) != 0) {
    359      1.1    bouyer 			aprint_normal("%s channel %d: can't subregion DMA "
    360      1.1    bouyer 			    "registers\n",
    361      1.1    bouyer 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, channel);
    362      1.1    bouyer 			goto next_channel;
    363      1.1    bouyer 		}
    364      1.1    bouyer 		if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    365      1.1    bouyer 		    0x244 + (channel << 7), 4,
    366      1.1    bouyer 		    &cp->dma_iohs[IDEDMA_TBL]) != 0) {
    367      1.1    bouyer 			aprint_normal("%s channel %d: can't subregion DMA "
    368      1.1    bouyer 			    "registers\n",
    369      1.1    bouyer 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, channel);
    370      1.1    bouyer 			goto next_channel;
    371      1.1    bouyer 		}
    372      1.1    bouyer 
    373      1.1    bouyer 		wdcattach(wdc_cp);
    374      1.1    bouyer 		bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    375      1.1    bouyer 		    (bus_space_read_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD],
    376      1.1    bouyer 			0) & ~0x00003f9f) | (channel + 1));
    377      1.1    bouyer 		bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh,
    378      1.1    bouyer 		    (channel + 1) << 2, 0x00000001);
    379      1.1    bouyer next_channel:
    380      1.1    bouyer 	continue;
    381      1.1    bouyer 	}
    382      1.1    bouyer 	return;
    383      1.1    bouyer }
    384      1.1    bouyer 
    385      1.1    bouyer static void
    386      1.1    bouyer pdc203xx_setup_channel(struct ata_channel *chp)
    387      1.1    bouyer {
    388      1.1    bouyer 	struct ata_drive_datas *drvp;
    389      1.1    bouyer 	int drive, s;
    390      1.1    bouyer 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    391      1.1    bouyer 
    392      1.1    bouyer 	pciide_channel_dma_setup(cp);
    393      1.1    bouyer 
    394      1.1    bouyer 	for (drive = 0; drive < 2; drive++) {
    395      1.1    bouyer 		drvp = &chp->ch_drive[drive];
    396      1.1    bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
    397      1.1    bouyer 			continue;
    398      1.1    bouyer 		if (drvp->drive_flags & DRIVE_UDMA) {
    399      1.1    bouyer 			s = splbio();
    400      1.1    bouyer 			drvp->drive_flags &= ~DRIVE_DMA;
    401      1.1    bouyer 			splx(s);
    402      1.1    bouyer 		}
    403      1.1    bouyer 	}
    404      1.1    bouyer }
    405      1.1    bouyer 
    406      1.1    bouyer static int
    407      1.1    bouyer pdc203xx_pci_intr(void *arg)
    408      1.1    bouyer {
    409      1.1    bouyer 	struct pciide_softc *sc = arg;
    410      1.1    bouyer 	struct pciide_channel *cp;
    411      1.1    bouyer 	struct ata_channel *wdc_cp;
    412      1.3     perry 	int i, rv, crv;
    413      1.1    bouyer 	u_int32_t scr;
    414      1.1    bouyer 
    415      1.1    bouyer 	rv = 0;
    416      1.1    bouyer 	scr = bus_space_read_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x00040);
    417      1.1    bouyer 
    418      1.1    bouyer 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    419      1.1    bouyer 		cp = &sc->pciide_channels[i];
    420      1.1    bouyer 		wdc_cp = &cp->ata_channel;
    421      1.1    bouyer 		if (scr & (1 << (i + 1))) {
    422      1.1    bouyer 			crv = wdcintr(wdc_cp);
    423      1.1    bouyer 			if (crv == 0) {
    424      1.1    bouyer 				printf("%s:%d: bogus intr (reg 0x%x)\n",
    425      1.1    bouyer 				    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    426      1.1    bouyer 				    i, scr);
    427      1.1    bouyer 			} else
    428      1.1    bouyer 				rv = 1;
    429      1.1    bouyer 		}
    430      1.1    bouyer 	}
    431      1.1    bouyer 	return rv;
    432      1.1    bouyer }
    433      1.1    bouyer 
    434      1.4  christos static int
    435      1.4  christos pdc205xx_pci_intr(void *arg)
    436      1.4  christos {
    437      1.4  christos 	struct pciide_softc *sc = arg;
    438      1.4  christos 	struct pciide_channel *cp;
    439      1.4  christos 	struct ata_channel *wdc_cp;
    440      1.4  christos 	int i, rv, crv;
    441      1.4  christos 	u_int32_t scr, status;
    442      1.4  christos 
    443      1.4  christos 	rv = 0;
    444      1.4  christos 	scr = bus_space_read_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x40);
    445      1.4  christos 	bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x40, scr & 0x0000ffff);
    446      1.4  christos 
    447      1.4  christos 	status = bus_space_read_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x60);
    448      1.4  christos 	bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x60, status & 0x000000ff);
    449      1.4  christos 
    450      1.4  christos 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    451      1.4  christos 		cp = &sc->pciide_channels[i];
    452      1.4  christos 		wdc_cp = &cp->ata_channel;
    453      1.4  christos 		if (scr & (1 << (i + 1))) {
    454      1.4  christos 			crv = wdcintr(wdc_cp);
    455      1.4  christos 			if (crv == 0) {
    456      1.4  christos 				printf("%s:%d: bogus intr (reg 0x%x)\n",
    457      1.4  christos 				    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    458      1.4  christos 				    i, scr);
    459      1.4  christos 			} else
    460      1.4  christos 				rv = 1;
    461      1.4  christos 		}
    462      1.4  christos 	}
    463      1.4  christos 	return rv;
    464      1.4  christos }
    465      1.4  christos 
    466      1.1    bouyer static void
    467      1.1    bouyer pdc203xx_irqack(struct ata_channel *chp)
    468      1.1    bouyer {
    469      1.1    bouyer 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    470      1.1    bouyer 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    471      1.1    bouyer 
    472      1.1    bouyer 	bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    473      1.1    bouyer 	    (bus_space_read_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD],
    474      1.1    bouyer 		0) & ~0x00003f9f) | (cp->ata_channel.ch_channel + 1));
    475      1.1    bouyer 	bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh,
    476      1.1    bouyer 	    (cp->ata_channel.ch_channel + 1) << 2, 0x00000001);
    477      1.1    bouyer }
    478      1.1    bouyer 
    479      1.1    bouyer static int
    480      1.1    bouyer pdc203xx_dma_init(void *v, int channel, int drive, void *databuf,
    481      1.1    bouyer     size_t datalen, int flags)
    482      1.1    bouyer {
    483      1.1    bouyer 	struct pciide_softc *sc = v;
    484      1.1    bouyer 
    485      1.1    bouyer 	return pciide_dma_dmamap_setup(sc, channel, drive,
    486      1.1    bouyer 	    databuf, datalen, flags);
    487      1.1    bouyer }
    488      1.1    bouyer 
    489      1.1    bouyer static void
    490      1.1    bouyer pdc203xx_dma_start(void *v, int channel, int drive)
    491      1.1    bouyer {
    492      1.1    bouyer 	struct pciide_softc *sc = v;
    493      1.1    bouyer 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    494      1.1    bouyer 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    495      1.1    bouyer 
    496      1.1    bouyer 	/* Write table addr */
    497      1.1    bouyer 	bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_TBL], 0,
    498      1.1    bouyer 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    499      1.1    bouyer 	/* start DMA engine */
    500      1.1    bouyer 	bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    501      1.1    bouyer 	    (bus_space_read_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD],
    502      1.1    bouyer 	    0) & ~0xc0) | ((dma_maps->dma_flags & WDC_DMA_READ) ? 0x80 : 0xc0));
    503      1.1    bouyer }
    504      1.1    bouyer 
    505      1.1    bouyer static int
    506      1.1    bouyer pdc203xx_dma_finish(void *v, int channel, int drive, int force)
    507      1.1    bouyer {
    508      1.1    bouyer 	struct pciide_softc *sc = v;
    509      1.1    bouyer 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    510      1.1    bouyer 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    511      1.1    bouyer 
    512      1.1    bouyer 	/* stop DMA channel */
    513      1.1    bouyer 	bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    514      1.1    bouyer 	    (bus_space_read_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD],
    515      1.1    bouyer 	    0) & ~0x80));
    516      1.1    bouyer 
    517      1.1    bouyer 	/* Unload the map of the data buffer */
    518      1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    519      1.1    bouyer 	    dma_maps->dmamap_xfer->dm_mapsize,
    520      1.1    bouyer 	    (dma_maps->dma_flags & WDC_DMA_READ) ?
    521      1.1    bouyer 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    522      1.1    bouyer 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
    523      1.1    bouyer 
    524      1.1    bouyer 	return 0;
    525      1.1    bouyer }
    526      1.4  christos 
    527      1.4  christos #define	PDC205_REGADDR(base,ch)	((base)+((ch)<<8))
    528      1.4  christos #define	PDC205_SSTATUS(ch)	PDC205_REGADDR(0x400,ch)
    529      1.4  christos #define	PDC205_SERROR(ch)	PDC205_REGADDR(0x404,ch)
    530      1.4  christos #define	PDC205_SCONTROL(ch)	PDC205_REGADDR(0x408,ch)
    531      1.4  christos #define	PDC205_MULTIPLIER(ch)	PDC205_REGADDR(0x4e8,ch)
    532      1.4  christos 
    533      1.4  christos 
    534      1.4  christos #define	SCONTROL_WRITE(sc,channel,scontrol)	\
    535      1.4  christos 	bus_space_write_4((sc)->sc_ba5_st, (sc)->sc_ba5_sh,	\
    536      1.4  christos 	PDC205_SCONTROL(channel), scontrol)
    537      1.4  christos 
    538      1.4  christos #define	SSTATUS_READ(sc,channel)	\
    539      1.4  christos 	bus_space_read_4((sc)->sc_ba5_st, (sc)->sc_ba5_sh,	\
    540      1.4  christos 	PDC205_SSTATUS(channel))
    541      1.4  christos 
    542      1.4  christos 
    543      1.4  christos 
    544      1.4  christos static void
    545      1.4  christos pdc205xx_do_reset(struct ata_channel *chp, int poll)
    546      1.4  christos {
    547      1.4  christos 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    548      1.4  christos 	u_int32_t scontrol;
    549      1.4  christos 
    550      1.4  christos 	wdc_do_reset(chp, poll);
    551      1.4  christos 
    552      1.4  christos 	/* reset SATA */
    553      1.4  christos 	scontrol = SControl_DET_INIT | SControl_SPD_ANY | SControl_IPM_NONE;
    554      1.4  christos 	SCONTROL_WRITE(sc, chp->ch_channel, scontrol);
    555      1.4  christos 	delay(50*1000);
    556      1.4  christos 
    557      1.4  christos 	scontrol &= ~SControl_DET_INIT;
    558      1.4  christos 	SCONTROL_WRITE(sc, chp->ch_channel, scontrol);
    559      1.4  christos 	delay(50*1000);
    560      1.4  christos }
    561      1.4  christos 
    562      1.4  christos 
    563      1.4  christos 
    564      1.4  christos static void
    565      1.4  christos pdc205xx_drv_probe(struct ata_channel *chp)
    566      1.4  christos {
    567      1.4  christos 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    568      1.4  christos 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
    569      1.4  christos 	u_int32_t scontrol, sstatus;
    570      1.4  christos 	u_int16_t scnt, sn, cl, ch;
    571      1.4  christos 	int i, s;
    572      1.4  christos 
    573      1.4  christos 	/* XXX This should be done by other code. */
    574      1.4  christos 	for (i = 0; i < 2; i++) {
    575      1.4  christos 		chp->ch_drive[i].chnl_softc = chp;
    576      1.4  christos 		chp->ch_drive[i].drive = i;
    577      1.4  christos 	}
    578      1.4  christos 
    579      1.4  christos 	SCONTROL_WRITE(sc, chp->ch_channel, 0);
    580      1.4  christos 	delay(50*1000);
    581      1.4  christos 
    582      1.4  christos 	scontrol = SControl_DET_INIT | SControl_SPD_ANY | SControl_IPM_NONE;
    583      1.4  christos 	SCONTROL_WRITE(sc,chp->ch_channel,scontrol);
    584      1.4  christos 	delay(50*1000);
    585      1.4  christos 
    586      1.4  christos 	scontrol &= ~SControl_DET_INIT;
    587      1.4  christos 	SCONTROL_WRITE(sc,chp->ch_channel,scontrol);
    588      1.4  christos 	delay(50*1000);
    589      1.4  christos 
    590      1.4  christos 	sstatus = SSTATUS_READ(sc,chp->ch_channel);
    591      1.4  christos 
    592      1.4  christos 	switch (sstatus & SStatus_DET_mask) {
    593      1.4  christos 	case SStatus_DET_NODEV:
    594      1.4  christos 		/* No Device; be silent.  */
    595      1.4  christos 		break;
    596      1.4  christos 
    597      1.4  christos 	case SStatus_DET_DEV_NE:
    598      1.4  christos 		aprint_error("%s: port %d: device connected, but "
    599      1.4  christos 		    "communication not established\n",
    600      1.4  christos 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, chp->ch_channel);
    601      1.4  christos 		break;
    602      1.4  christos 
    603      1.4  christos 	case SStatus_DET_OFFLINE:
    604      1.4  christos 		aprint_error("%s: port %d: PHY offline\n",
    605      1.4  christos 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, chp->ch_channel);
    606      1.4  christos 		break;
    607      1.4  christos 
    608      1.4  christos 	case SStatus_DET_DEV:
    609      1.4  christos 		bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
    610      1.4  christos 		    WDSD_IBM);
    611      1.4  christos 		delay(10);	/* 400ns delay */
    612      1.4  christos 		scnt = bus_space_read_2(wdr->cmd_iot,
    613      1.4  christos 		    wdr->cmd_iohs[wd_seccnt], 0);
    614      1.4  christos 		sn = bus_space_read_2(wdr->cmd_iot,
    615      1.4  christos 		    wdr->cmd_iohs[wd_sector], 0);
    616      1.4  christos 		cl = bus_space_read_2(wdr->cmd_iot,
    617      1.4  christos 		    wdr->cmd_iohs[wd_cyl_lo], 0);
    618      1.4  christos 		ch = bus_space_read_2(wdr->cmd_iot,
    619      1.4  christos 		    wdr->cmd_iohs[wd_cyl_hi], 0);
    620      1.4  christos #if 0
    621      1.4  christos 		printf("%s: port %d: scnt=0x%x sn=0x%x cl=0x%x ch=0x%x\n",
    622      1.4  christos 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, chp->ch_channel,
    623      1.4  christos 		    scnt, sn, cl, ch);
    624      1.4  christos #endif
    625      1.4  christos 		/*
    626      1.4  christos 		 * scnt and sn are supposed to be 0x1 for ATAPI, but in some
    627      1.4  christos 		 * cases we get wrong values here, so ignore it.
    628      1.4  christos 		 */
    629      1.4  christos 		s = splbio();
    630      1.4  christos 		if (cl == 0x14 && ch == 0xeb)
    631      1.4  christos 			chp->ch_drive[0].drive_flags |= DRIVE_ATAPI;
    632      1.4  christos 		else
    633      1.4  christos 			chp->ch_drive[0].drive_flags |= DRIVE_ATA;
    634      1.4  christos 		splx(s);
    635      1.4  christos #if 0
    636      1.4  christos 		aprint_normal("%s: port %d: device present, speed: %s\n",
    637      1.4  christos 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, chp->ch_channel,
    638      1.4  christos 		    sata_speed(sstatus));
    639      1.4  christos #endif
    640      1.4  christos 		break;
    641      1.4  christos 
    642      1.4  christos 	default:
    643      1.4  christos 		aprint_error("%s: port %d: unknown SStatus: 0x%08x\n",
    644      1.4  christos 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, chp->ch_channel,
    645      1.4  christos 		    sstatus);
    646      1.4  christos 	}
    647      1.4  christos }
    648