Home | History | Annotate | Line # | Download | only in dev
kauai.c revision 1.6
      1 /*	$NetBSD: kauai.c,v 1.6 2003/10/08 11:12:36 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 Tsubai Masanari.  All rights reserved.
      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. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: kauai.c,v 1.6 2003/10/08 11:12:36 bouyer Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 #include <sys/malloc.h>
     36 
     37 #include <uvm/uvm_extern.h>
     38 
     39 #include <machine/bus.h>
     40 
     41 #include <dev/ata/atareg.h>
     42 #include <dev/ata/atavar.h>
     43 #include <dev/ic/wdcvar.h>
     44 
     45 #include <dev/ofw/openfirm.h>
     46 
     47 #include <dev/pci/pcivar.h>
     48 #include <dev/pci/pcireg.h>
     49 #include <dev/pci/pcidevs.h>
     50 
     51 #include <macppc/dev/dbdma.h>
     52 
     53 #define WDC_REG_NPORTS		8
     54 #define WDC_AUXREG_OFFSET	0x16
     55 
     56 #define PIO_CONFIG_REG (0x200 >> 4)	/* PIO and DMA access timing */
     57 #define DMA_CONFIG_REG (0x210 >> 4)	/* UDMA access timing */
     58 
     59 struct kauai_softc {
     60 	struct wdc_softc sc_wdcdev;
     61 	struct channel_softc *wdc_chanptr;
     62 	struct channel_softc wdc_channel;
     63 	struct channel_queue wdc_queue;
     64 	dbdma_regmap_t *sc_dmareg;
     65 	dbdma_command_t	*sc_dmacmd;
     66 	u_int sc_piotiming_r[2];
     67 	u_int sc_piotiming_w[2];
     68 	u_int sc_dmatiming_r[2];
     69 	u_int sc_dmatiming_w[2];
     70 	void (*sc_calc_timing)(struct kauai_softc *, int);
     71 };
     72 
     73 int kauai_match __P((struct device *, struct cfdata *, void *));
     74 void kauai_attach __P((struct device *, struct device *, void *));
     75 int kauai_dma_init __P((void *, int, int, void *, size_t, int));
     76 void kauai_dma_start __P((void *, int, int));
     77 int kauai_dma_finish __P((void *, int, int, int));
     78 void kauai_set_modes __P((struct channel_softc *));
     79 static void calc_timing_kauai __P((struct kauai_softc *, int));
     80 static int getnodebypci(pci_chipset_tag_t, pcitag_t);
     81 
     82 CFATTACH_DECL(kauai, sizeof(struct kauai_softc),
     83     kauai_match, kauai_attach, NULL, wdcactivate);
     84 
     85 int
     86 kauai_match(parent, match, aux)
     87 	struct device *parent;
     88 	struct cfdata *match;
     89 	void *aux;
     90 {
     91 	struct pci_attach_args *pa = aux;
     92 
     93 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
     94 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KAUAI ||
     95 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_UNINORTH_ATA))
     96 		return 5;
     97 
     98 	return 0;
     99 }
    100 
    101 void
    102 kauai_attach(parent, self, aux)
    103 	struct device *parent, *self;
    104 	void *aux;
    105 {
    106 	struct kauai_softc *sc = (void *)self;
    107 	struct pci_attach_args *pa = aux;
    108 	struct channel_softc *chp = &sc->wdc_channel;
    109 	pci_intr_handle_t ih;
    110 	paddr_t regbase, dmabase;
    111 	int node, reg[5];
    112 
    113 #ifdef DIAGNOSTIC
    114 	if ((vaddr_t)sc->sc_dmacmd & 0x0f) {
    115 		printf(": bad dbdma alignment\n");
    116 		return;
    117 	}
    118 #endif
    119 
    120 	node = getnodebypci(pa->pa_pc, pa->pa_tag);
    121 	if (node == 0) {
    122 		printf(": cannot find gmac node\n");
    123 		return;
    124 	}
    125 
    126 	if (OF_getprop(node, "assigned-addresses", reg, sizeof reg) < 12) {
    127 		printf(": cannot get address property\n");
    128 		return;
    129 	}
    130 	regbase = reg[2] + 0x2000;
    131 	dmabase = reg[2] + 0x1000;
    132 
    133 	/*
    134 	 * XXX PCI_INTERRUPT_REG seems to be wired to 0.
    135 	 * XXX So use fixed intrpin and intrline values.
    136 	 */
    137 	if (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_INTERRUPT_REG) == 0) {
    138 		pa->pa_intrpin = 1;
    139 		pa->pa_intrline = 39;
    140 	}
    141 
    142 	if (pci_intr_map(pa, &ih)) {
    143 		printf(": unable to map interrupt\n");
    144 		return;
    145 	}
    146 	printf(": interrupting at %s\n", pci_intr_string(pa->pa_pc, ih));
    147 
    148 	chp->cmd_iot = chp->ctl_iot = macppc_make_bus_space_tag(regbase, 4);
    149 
    150 	if (bus_space_map(chp->cmd_iot, 0, WDC_REG_NPORTS, 0, &chp->cmd_ioh) ||
    151 	    bus_space_subregion(chp->cmd_iot, chp->cmd_ioh,
    152 			WDC_AUXREG_OFFSET, 1, &chp->ctl_ioh)) {
    153 		printf("%s: couldn't map registers\n", self->dv_xname);
    154 		return;
    155 	}
    156 
    157 	if (pci_intr_establish(pa->pa_pc, ih, IPL_BIO, wdcintr, chp) == NULL) {
    158 		printf("%s: unable to establish interrupt\n", self->dv_xname);
    159 		return;
    160 	}
    161 
    162 
    163 	sc->sc_wdcdev.PIO_cap = 4;
    164 	sc->sc_wdcdev.DMA_cap = 2;
    165 	sc->sc_wdcdev.UDMA_cap = 5;
    166 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_MODE;
    167 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
    168 	sc->wdc_chanptr = chp;
    169 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    170 	sc->sc_wdcdev.nchannels = 1;
    171 	sc->sc_wdcdev.dma_arg = sc;
    172 	sc->sc_wdcdev.dma_init = kauai_dma_init;
    173 	sc->sc_wdcdev.dma_start = kauai_dma_start;
    174 	sc->sc_wdcdev.dma_finish = kauai_dma_finish;
    175 	sc->sc_wdcdev.set_modes = kauai_set_modes;
    176 	sc->sc_calc_timing = calc_timing_kauai;
    177 	sc->sc_dmareg = (void *)dmabase;
    178 
    179 	chp->channel = 0;
    180 	chp->wdc = &sc->sc_wdcdev;
    181 	chp->ch_queue = &sc->wdc_queue;
    182 
    183 	wdcattach(chp);
    184 }
    185 
    186 void
    187 kauai_set_modes(chp)
    188 	struct channel_softc *chp;
    189 {
    190 	struct kauai_softc *sc = (void *)chp->wdc;
    191 	struct ata_drive_datas *drvp0 = &chp->ch_drive[0];
    192 	struct ata_drive_datas *drvp1 = &chp->ch_drive[1];
    193 	struct ata_drive_datas *drvp;
    194 	int drive;
    195 
    196 	if ((drvp0->drive_flags & DRIVE) && (drvp1->drive_flags & DRIVE)) {
    197 		drvp0->PIO_mode = drvp1->PIO_mode =
    198 		    min(drvp0->PIO_mode, drvp1->PIO_mode);
    199 	}
    200 
    201 	for (drive = 0; drive < 2; drive++) {
    202 		drvp = &chp->ch_drive[drive];
    203 		if (drvp->drive_flags & DRIVE) {
    204 			(*sc->sc_calc_timing)(sc, drive);
    205 			bus_space_write_4(chp->cmd_iot, chp->cmd_ioh,
    206 			    PIO_CONFIG_REG, sc->sc_piotiming_r[drive]);
    207 			bus_space_write_4(chp->cmd_iot, chp->cmd_ioh,
    208 			    DMA_CONFIG_REG, sc->sc_dmatiming_r[drive]);
    209 		}
    210 	}
    211 }
    212 
    213 /*
    214  * IDE transfer timings
    215  */
    216 static const u_int pio_timing_kauai[] = {	/* 0xff000fff */
    217 	0x08000a92,	/* Mode 0 */
    218 	0x0800060f,	/*      1 */
    219 	0x0800038b,	/*      2 */
    220 	0x05000249,	/*      3 */
    221 	0x04000148	/*      4 */
    222 };
    223 static const u_int dma_timing_kauai[] = {	/* 0x00fff000 */
    224 	0x00618000,	/* Mode 0 */
    225 	0x00209000,	/*      1 */
    226 	0x00148000	/*      2 */
    227 };
    228 static const u_int udma_timing_kauai[] = {	/* 0x0000ffff */
    229 	0x000070c0,	/* Mode 0 */
    230 	0x00005d80,	/*      1 */
    231 	0x00004a60,	/*      2 */
    232 	0x00003a50,	/*      3 */
    233 	0x00002a30,	/*      4 */
    234 	0x00002921	/*      5 */
    235 };
    236 
    237 /*
    238  * Timing calculation for Kauai.
    239  */
    240 void
    241 calc_timing_kauai(sc, drive)
    242 	struct kauai_softc *sc;
    243 	int drive;
    244 {
    245 	struct channel_softc *chp = &sc->wdc_channel;
    246 	struct ata_drive_datas *drvp = &chp->ch_drive[drive];
    247 	int piomode = drvp->PIO_mode;
    248 	int dmamode = drvp->DMA_mode;
    249 	int udmamode = drvp->UDMA_mode;
    250 	u_int pioconf, dmaconf;
    251 
    252 	pioconf = pio_timing_kauai[piomode];
    253 
    254 	dmaconf = 0;
    255 	if (drvp->drive_flags & DRIVE_DMA)
    256 		dmaconf |= dma_timing_kauai[dmamode];
    257 	if (drvp->drive_flags & DRIVE_UDMA)
    258 		dmaconf |= udma_timing_kauai[udmamode];
    259 
    260 	if (drvp->drive_flags & DRIVE_UDMA)
    261 		dmaconf |= 1;
    262 
    263 	sc->sc_piotiming_r[drive] = sc->sc_piotiming_w[drive] = pioconf;
    264 	sc->sc_dmatiming_r[drive] = sc->sc_dmatiming_w[drive] = dmaconf;
    265 }
    266 
    267 int
    268 kauai_dma_init(v, channel, drive, databuf, datalen, flags)
    269 	void *v;
    270 	void *databuf;
    271 	size_t datalen;
    272 	int flags;
    273 {
    274 	struct kauai_softc *sc = v;
    275 	dbdma_command_t *cmdp = sc->sc_dmacmd;
    276 	struct channel_softc *chp = &sc->wdc_channel;
    277 	vaddr_t va = (vaddr_t)databuf;
    278 	int read = flags & WDC_DMA_READ;
    279 	int cmd = read ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
    280 	u_int offset;
    281 
    282 	bus_space_write_4(chp->cmd_iot, chp->cmd_ioh, DMA_CONFIG_REG,
    283 	    read ? sc->sc_dmatiming_r[drive] : sc->sc_dmatiming_w[drive]);
    284 	bus_space_read_4(chp->cmd_iot, chp->cmd_ioh, DMA_CONFIG_REG);
    285 
    286 	offset = va & PGOFSET;
    287 
    288 	/* if va is not page-aligned, setup the first page */
    289 	if (offset != 0) {
    290 		int rest = PAGE_SIZE - offset;	/* the rest of the page */
    291 
    292 		if (datalen > rest) {		/* if continues to next page */
    293 			DBDMA_BUILD(cmdp, cmd, 0, rest, vtophys(va),
    294 				DBDMA_INT_NEVER, DBDMA_WAIT_NEVER,
    295 				DBDMA_BRANCH_NEVER);
    296 			datalen -= rest;
    297 			va += rest;
    298 			cmdp++;
    299 		}
    300 	}
    301 
    302 	/* now va is page-aligned */
    303 	while (datalen > PAGE_SIZE) {
    304 		DBDMA_BUILD(cmdp, cmd, 0, PAGE_SIZE, vtophys(va),
    305 			DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    306 		datalen -= PAGE_SIZE;
    307 		va += PAGE_SIZE;
    308 		cmdp++;
    309 	}
    310 
    311 	/* the last page (datalen <= PAGE_SIZE here) */
    312 	cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
    313 	DBDMA_BUILD(cmdp, cmd, 0, datalen, vtophys(va),
    314 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    315 	cmdp++;
    316 
    317 	DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
    318 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    319 
    320 	return 0;
    321 }
    322 
    323 void
    324 kauai_dma_start(v, channel, drive)
    325 	void *v;
    326 	int channel, drive;
    327 {
    328 	struct kauai_softc *sc = v;
    329 
    330 	dbdma_start(sc->sc_dmareg, sc->sc_dmacmd);
    331 }
    332 
    333 int
    334 kauai_dma_finish(v, channel, drive, read)
    335 	void *v;
    336 	int channel, drive;
    337 	int read;
    338 {
    339 	struct kauai_softc *sc = v;
    340 
    341 	dbdma_stop(sc->sc_dmareg);
    342 	return 0;
    343 }
    344 
    345 /*
    346  * Find OF-device corresponding to the PCI device.
    347  */
    348 int
    349 getnodebypci(pc, tag)
    350 	pci_chipset_tag_t pc;
    351 	pcitag_t tag;
    352 {
    353 	int bus, dev, func;
    354 	u_int reg[5];
    355 	int p, q;
    356 	int l, b, d, f;
    357 
    358 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    359 
    360 	for (q = OF_peer(0); q; q = p) {
    361 		l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
    362 		if (l > 4) {
    363 			b = (reg[0] >> 16) & 0xff;
    364 			d = (reg[0] >> 11) & 0x1f;
    365 			f = (reg[0] >> 8) & 0x07;
    366 
    367 			if (b == bus && d == dev && f == func)
    368 				return q;
    369 		}
    370 		if ((p = OF_child(q)))
    371 			continue;
    372 		while (q) {
    373 			if ((p = OF_peer(q)))
    374 				break;
    375 			q = OF_parent(q);
    376 		}
    377 	}
    378 	return 0;
    379 }
    380