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