xref: /netbsd-src/sys/arch/macppc/dev/kauai.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: kauai.c,v 1.18 2005/12/11 12:18:03 christos 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.18 2005/12/11 12:18:03 christos 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 ata_channel *sc_chanptr;
62 	struct ata_channel sc_channel;
63 	struct wdc_regs sc_wdc_regs;
64 	struct ata_queue sc_queue;
65 	dbdma_regmap_t *sc_dmareg;
66 	dbdma_command_t	*sc_dmacmd;
67 	u_int sc_piotiming_r[2];
68 	u_int sc_piotiming_w[2];
69 	u_int sc_dmatiming_r[2];
70 	u_int sc_dmatiming_w[2];
71 	void (*sc_calc_timing)(struct kauai_softc *, int);
72 };
73 
74 int kauai_match __P((struct device *, struct cfdata *, void *));
75 void kauai_attach __P((struct device *, struct device *, void *));
76 int kauai_dma_init __P((void *, int, int, void *, size_t, int));
77 void kauai_dma_start __P((void *, int, int));
78 int kauai_dma_finish __P((void *, int, int, int));
79 void kauai_set_modes __P((struct ata_channel *));
80 static void calc_timing_kauai __P((struct kauai_softc *, int));
81 static int getnodebypci(pci_chipset_tag_t, pcitag_t);
82 
83 CFATTACH_DECL(kauai, sizeof(struct kauai_softc),
84     kauai_match, kauai_attach, NULL, wdcactivate);
85 
86 int
87 kauai_match(parent, match, aux)
88 	struct device *parent;
89 	struct cfdata *match;
90 	void *aux;
91 {
92 	struct pci_attach_args *pa = aux;
93 
94 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
95 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KAUAI ||
96 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_UNINORTH_ATA))
97 		return 5;
98 
99 	return 0;
100 }
101 
102 void
103 kauai_attach(parent, self, aux)
104 	struct device *parent, *self;
105 	void *aux;
106 {
107 	struct kauai_softc *sc = (void *)self;
108 	struct pci_attach_args *pa = aux;
109 	struct ata_channel *chp = &sc->sc_channel;
110 	struct wdc_regs *wdr;
111 	pci_intr_handle_t ih;
112 	paddr_t regbase, dmabase;
113 	int node, reg[5], i;
114 
115 #ifdef DIAGNOSTIC
116 	if ((vaddr_t)sc->sc_dmacmd & 0x0f) {
117 		printf(": bad dbdma alignment\n");
118 		return;
119 	}
120 #endif
121 
122 	node = getnodebypci(pa->pa_pc, pa->pa_tag);
123 	if (node == 0) {
124 		printf(": cannot find gmac node\n");
125 		return;
126 	}
127 
128 	if (OF_getprop(node, "assigned-addresses", reg, sizeof reg) < 12) {
129 		printf(": cannot get address property\n");
130 		return;
131 	}
132 	regbase = reg[2] + 0x2000;
133 	dmabase = reg[2] + 0x1000;
134 
135 	/*
136 	 * XXX PCI_INTERRUPT_REG seems to be wired to 0.
137 	 * XXX So use fixed intrpin and intrline values.
138 	 */
139 	if (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_INTERRUPT_REG) == 0) {
140 		pa->pa_intrpin = 1;
141 		pa->pa_intrline = 39;
142 	}
143 
144 	if (pci_intr_map(pa, &ih)) {
145 		printf(": unable to map interrupt\n");
146 		return;
147 	}
148 	printf(": interrupting at %s\n", pci_intr_string(pa->pa_pc, ih));
149 
150 	sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
151 
152 	wdr->cmd_iot = wdr->ctl_iot = macppc_make_bus_space_tag(regbase, 4);
153 
154 	if (bus_space_map(wdr->cmd_iot, 0, WDC_REG_NPORTS, 0,
155 	    &wdr->cmd_baseioh) ||
156 	    bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
157 			WDC_AUXREG_OFFSET, 1, &wdr->ctl_ioh)) {
158 		printf("%s: couldn't map registers\n", self->dv_xname);
159 		return;
160 	}
161 	for (i = 0; i < WDC_NREG; i++) {
162 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i,
163 		    i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
164 			bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
165 			    WDC_REG_NPORTS);
166 			printf("%s: couldn't subregion registers\n",
167 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
168 			return;
169 		}
170 	}
171 
172 	if (pci_intr_establish(pa->pa_pc, ih, IPL_BIO, wdcintr, chp) == NULL) {
173 		printf("%s: unable to establish interrupt\n", self->dv_xname);
174 		return;
175 	}
176 
177 
178 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
179 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
180 	sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
181 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
182 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
183 	sc->sc_chanptr = chp;
184 	sc->sc_wdcdev.sc_atac.atac_channels = &sc->sc_chanptr;
185 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
186 	sc->sc_wdcdev.dma_arg = sc;
187 	sc->sc_wdcdev.dma_init = kauai_dma_init;
188 	sc->sc_wdcdev.dma_start = kauai_dma_start;
189 	sc->sc_wdcdev.dma_finish = kauai_dma_finish;
190 	sc->sc_wdcdev.sc_atac.atac_set_modes = kauai_set_modes;
191 	sc->sc_calc_timing = calc_timing_kauai;
192 	sc->sc_dmareg = (void *)dmabase;
193 
194 	chp->ch_channel = 0;
195 	chp->ch_atac = &sc->sc_wdcdev.sc_atac;
196 	chp->ch_queue = &sc->sc_queue;
197 	wdc_init_shadow_regs(chp);
198 
199 	wdcattach(chp);
200 }
201 
202 void
203 kauai_set_modes(chp)
204 	struct ata_channel *chp;
205 {
206 	struct kauai_softc *sc = (void *)chp->ch_atac;
207 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
208 	struct ata_drive_datas *drvp0 = &chp->ch_drive[0];
209 	struct ata_drive_datas *drvp1 = &chp->ch_drive[1];
210 	struct ata_drive_datas *drvp;
211 	int drive;
212 
213 	if ((drvp0->drive_flags & DRIVE) && (drvp1->drive_flags & DRIVE)) {
214 		drvp0->PIO_mode = drvp1->PIO_mode =
215 		    min(drvp0->PIO_mode, drvp1->PIO_mode);
216 	}
217 
218 	for (drive = 0; drive < 2; drive++) {
219 		drvp = &chp->ch_drive[drive];
220 		if (drvp->drive_flags & DRIVE) {
221 			(*sc->sc_calc_timing)(sc, drive);
222 			bus_space_write_4(wdr->cmd_iot, wdr->cmd_baseioh,
223 			    PIO_CONFIG_REG, sc->sc_piotiming_r[drive]);
224 			bus_space_write_4(wdr->cmd_iot, wdr->cmd_baseioh,
225 			    DMA_CONFIG_REG, sc->sc_dmatiming_r[drive]);
226 		}
227 	}
228 }
229 
230 /*
231  * IDE transfer timings
232  */
233 static const u_int pio_timing_kauai[] = {	/* 0xff000fff */
234 	0x08000a92,	/* Mode 0 */
235 	0x0800060f,	/*      1 */
236 	0x0800038b,	/*      2 */
237 	0x05000249,	/*      3 */
238 	0x04000148	/*      4 */
239 };
240 static const u_int dma_timing_kauai[] = {	/* 0x00fff000 */
241 	0x00618000,	/* Mode 0 */
242 	0x00209000,	/*      1 */
243 	0x00148000	/*      2 */
244 };
245 static const u_int udma_timing_kauai[] = {	/* 0x0000ffff */
246 	0x000070c0,	/* Mode 0 */
247 	0x00005d80,	/*      1 */
248 	0x00004a60,	/*      2 */
249 	0x00003a50,	/*      3 */
250 	0x00002a30,	/*      4 */
251 	0x00002921	/*      5 */
252 };
253 
254 /*
255  * Timing calculation for Kauai.
256  */
257 void
258 calc_timing_kauai(sc, drive)
259 	struct kauai_softc *sc;
260 	int drive;
261 {
262 	struct ata_channel *chp = &sc->sc_channel;
263 	struct ata_drive_datas *drvp = &chp->ch_drive[drive];
264 	int piomode = drvp->PIO_mode;
265 	int dmamode = drvp->DMA_mode;
266 	int udmamode = drvp->UDMA_mode;
267 	u_int pioconf, dmaconf;
268 
269 	pioconf = pio_timing_kauai[piomode];
270 
271 	dmaconf = 0;
272 	if (drvp->drive_flags & DRIVE_DMA)
273 		dmaconf |= dma_timing_kauai[dmamode];
274 	if (drvp->drive_flags & DRIVE_UDMA)
275 		dmaconf |= udma_timing_kauai[udmamode];
276 
277 	if (drvp->drive_flags & DRIVE_UDMA)
278 		dmaconf |= 1;
279 
280 	sc->sc_piotiming_r[drive] = sc->sc_piotiming_w[drive] = pioconf;
281 	sc->sc_dmatiming_r[drive] = sc->sc_dmatiming_w[drive] = dmaconf;
282 }
283 
284 int
285 kauai_dma_init(v, channel, drive, databuf, datalen, flags)
286 	void *v;
287 	void *databuf;
288 	size_t datalen;
289 	int flags;
290 {
291 	struct kauai_softc *sc = v;
292 	dbdma_command_t *cmdp = sc->sc_dmacmd;
293 	struct ata_channel *chp = &sc->sc_channel;
294 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
295 	vaddr_t va = (vaddr_t)databuf;
296 	int read = flags & WDC_DMA_READ;
297 	int cmd = read ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
298 	u_int offset;
299 
300 	bus_space_write_4(wdr->cmd_iot, wdr->cmd_baseioh, DMA_CONFIG_REG,
301 	    read ? sc->sc_dmatiming_r[drive] : sc->sc_dmatiming_w[drive]);
302 	bus_space_read_4(wdr->cmd_iot, wdr->cmd_baseioh, DMA_CONFIG_REG);
303 
304 	offset = va & PGOFSET;
305 
306 	/* if va is not page-aligned, setup the first page */
307 	if (offset != 0) {
308 		int rest = PAGE_SIZE - offset;	/* the rest of the page */
309 
310 		if (datalen > rest) {		/* if continues to next page */
311 			DBDMA_BUILD(cmdp, cmd, 0, rest, vtophys(va),
312 				DBDMA_INT_NEVER, DBDMA_WAIT_NEVER,
313 				DBDMA_BRANCH_NEVER);
314 			datalen -= rest;
315 			va += rest;
316 			cmdp++;
317 		}
318 	}
319 
320 	/* now va is page-aligned */
321 	while (datalen > PAGE_SIZE) {
322 		DBDMA_BUILD(cmdp, cmd, 0, PAGE_SIZE, vtophys(va),
323 			DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
324 		datalen -= PAGE_SIZE;
325 		va += PAGE_SIZE;
326 		cmdp++;
327 	}
328 
329 	/* the last page (datalen <= PAGE_SIZE here) */
330 	cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
331 	DBDMA_BUILD(cmdp, cmd, 0, datalen, vtophys(va),
332 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
333 	cmdp++;
334 
335 	DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
336 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
337 
338 	return 0;
339 }
340 
341 void
342 kauai_dma_start(v, channel, drive)
343 	void *v;
344 	int channel, drive;
345 {
346 	struct kauai_softc *sc = v;
347 
348 	dbdma_start(sc->sc_dmareg, sc->sc_dmacmd);
349 }
350 
351 int
352 kauai_dma_finish(v, channel, drive, read)
353 	void *v;
354 	int channel, drive;
355 	int read;
356 {
357 	struct kauai_softc *sc = v;
358 
359 	dbdma_stop(sc->sc_dmareg);
360 	return 0;
361 }
362 
363 /*
364  * Find OF-device corresponding to the PCI device.
365  */
366 int
367 getnodebypci(pc, tag)
368 	pci_chipset_tag_t pc;
369 	pcitag_t tag;
370 {
371 	int bus, dev, func;
372 	u_int reg[5];
373 	int p, q;
374 	int l, b, d, f;
375 
376 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
377 
378 	for (q = OF_peer(0); q; q = p) {
379 		l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
380 		if (l > 4) {
381 			b = (reg[0] >> 16) & 0xff;
382 			d = (reg[0] >> 11) & 0x1f;
383 			f = (reg[0] >> 8) & 0x07;
384 
385 			if (b == bus && d == dev && f == func)
386 				return q;
387 		}
388 		if ((p = OF_child(q)))
389 			continue;
390 		while (q) {
391 			if ((p = OF_peer(q)))
392 				break;
393 			q = OF_parent(q);
394 		}
395 	}
396 	return 0;
397 }
398