if_mc_obio.c revision 1.3 1 /* $NetBSD: if_mc_obio.c,v 1.3 1997/11/07 13:31:16 briggs Exp $ */
2
3 /*-
4 * Copyright (c) 1997 David Huang <khym (at) bga.com>
5 * All rights reserved.
6 *
7 * Portions of this code are based on code by Denton Gentry <denny1 (at) home.com>
8 * and Yanagisawa Takeshi <yanagisw (at) aa.ap.titech.ac.jp>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31 /*
32 * Bus attachment and DMA routines for the mc driver (Centris/Quadra
33 * 660av and Quadra 840av onboard ethernet, based on the AMD Am79C940
34 * MACE ethernet chip). Also uses the PSC (Peripheral Subsystem
35 * Controller) for DMA to and from the MACE.
36 */
37
38 #include <sys/param.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/systm.h>
43
44 #include <net/if.h>
45 #include <net/if_ether.h>
46
47 #include <vm/vm.h>
48
49 #include <machine/bus.h>
50 #include <machine/psc.h>
51
52 #include <mac68k/dev/obiovar.h>
53 #include <mac68k/dev/if_mcreg.h>
54 #include <mac68k/dev/if_mcvar.h>
55
56 #define MACE_REG_BASE 0x50F1C000
57 #define MACE_PROM_BASE 0x50F08000
58
59 hide int mc_obio_match __P((struct device *, struct cfdata *, void *));
60 hide void mc_obio_attach __P((struct device *, struct device *, void *));
61 hide void mc_obio_init __P((struct mc_softc *sc));
62 hide void mc_obio_put __P((struct mc_softc *sc, u_int len));
63 hide int mc_dmaintr __P((void *arg));
64 hide void mc_reset_rxdma __P((struct mc_softc *sc));
65 hide void mc_reset_rxdma_set __P((struct mc_softc *, int set));
66 hide void mc_reset_txdma __P((struct mc_softc *sc));
67 hide int mc_obio_getaddr __P((struct mc_softc *, u_int8_t *));
68
69 extern int kvtop __P((register caddr_t addr));
70
71 struct cfattach mc_obio_ca = {
72 sizeof(struct mc_softc), mc_obio_match, mc_obio_attach
73 };
74
75 hide int
76 mc_obio_match(parent, cf, aux)
77 struct device *parent;
78 struct cfdata *cf;
79 void *aux;
80 {
81 struct obio_attach_args *oa = aux;
82 bus_space_handle_t bsh;
83 int found = 0;
84
85 if (current_mac_model->class != MACH_CLASSAV)
86 return 0;
87
88 if (bus_space_map(oa->oa_tag, MACE_REG_BASE, MC_REGSIZE, 0, &bsh))
89 return 0;
90
91 /*
92 * Make sure the MACE's I/O space is readable, and if it is,
93 * try to read the CHIPID register. A MACE will always have
94 * 0x?940, where the ? depends on the chip version.
95 */
96 if (bus_probe(oa->oa_tag, bsh, 0, 1)) {
97 if ((bus_space_read_1(
98 oa->oa_tag, bsh, MACE_REG(MACE_CHIPIDL)) == 0x40) &&
99 ((bus_space_read_1(
100 oa->oa_tag, bsh, MACE_REG(MACE_CHIPIDH)) & 0xf) == 9))
101 found = 1;
102 }
103
104 bus_space_unmap(oa->oa_tag, bsh, MC_REGSIZE);
105
106 return found;
107 }
108
109 hide void
110 mc_obio_attach(parent, self, aux)
111 struct device *parent, *self;
112 void *aux;
113 {
114 struct obio_attach_args *oa = (struct obio_attach_args *)aux;
115 struct mc_softc *sc = (void *)self;
116 u_int8_t myaddr[ETHER_ADDR_LEN];
117 int i, noncontig = 0;
118
119 sc->sc_regt = oa->oa_tag;
120 sc->sc_biucc = XMTSP_64;
121 sc->sc_fifocc = XMTFW_16 | RCVFW_64 | XMTFWU | RCVFWU |
122 XMTBRST | RCVBRST;
123 sc->sc_plscc = PORTSEL_AUI;
124
125 if (bus_space_map(sc->sc_regt, MACE_REG_BASE, MC_REGSIZE, 0,
126 &sc->sc_regh)) {
127 printf(": failed to map space for MACE regs.\n");
128 return;
129 }
130
131 if (mc_obio_getaddr(sc, myaddr)) {
132 printf(": failed to get MAC address.\n");
133 return;
134 }
135
136 /* allocate memory for transmit buffer and mark it non-cacheable */
137 sc->sc_txbuf = malloc(NBPG, M_DEVBUF, M_WAITOK);
138 sc->sc_txbuf_phys = kvtop(sc->sc_txbuf);
139 physaccess (sc->sc_txbuf, (caddr_t)sc->sc_txbuf_phys, NBPG,
140 PG_V | PG_RW | PG_CI);
141
142 /*
143 * allocate memory for receive buffer and mark it non-cacheable
144 * XXX This should use the bus_dma interface, since the buffer
145 * needs to be physically contiguous. However, it seems that
146 * at least on my system, malloc() does allocate contiguous
147 * memory. If it's not, suggest reducing the number of buffers
148 * to 2, which will fit in one 4K page.
149 */
150 sc->sc_rxbuf = malloc(MC_NPAGES * NBPG, M_DEVBUF, M_WAITOK);
151 sc->sc_rxbuf_phys = kvtop(sc->sc_rxbuf);
152 for (i = 0; i < MC_NPAGES; i++) {
153 int pa;
154
155 pa = kvtop(sc->sc_rxbuf + NBPG*i);
156 physaccess (sc->sc_rxbuf + NBPG*i, (caddr_t)pa, NBPG,
157 PG_V | PG_RW | PG_CI);
158 if (pa != sc->sc_rxbuf_phys + NBPG*i)
159 noncontig = 1;
160 }
161
162 if (noncontig) {
163 printf("%s: receive DMA buffer not contiguous! "
164 "Try compiling with \"options MC_RXDMABUFS=2\"\n",
165 sc->sc_dev.dv_xname);
166 return;
167 }
168
169 sc->sc_bus_init = mc_obio_init;
170 sc->sc_putpacket = mc_obio_put;
171
172 /* disable receive DMA */
173 psc_reg2(PSC_ENETRD_CTL) = 0x8800;
174 psc_reg2(PSC_ENETRD_CTL) = 0x1000;
175 psc_reg2(PSC_ENETRD_CMD + PSC_SET0) = 0x1100;
176 psc_reg2(PSC_ENETRD_CMD + PSC_SET1) = 0x1100;
177
178 /* disable transmit DMA */
179 psc_reg2(PSC_ENETWR_CTL) = 0x8800;
180 psc_reg2(PSC_ENETWR_CTL) = 0x1000;
181 psc_reg2(PSC_ENETWR_CMD + PSC_SET0) = 0x1100;
182 psc_reg2(PSC_ENETWR_CMD + PSC_SET1) = 0x1100;
183
184 /* install interrupt handlers */
185 add_psc_lev4_intr(PSCINTR_ENET_DMA, mc_dmaintr, sc);
186 add_psc_lev3_intr(mcintr, sc);
187
188 /* enable MACE DMA interrupts */
189 psc_reg1(PSC_LEV4_IER) = 0x80 | (1 << PSCINTR_ENET_DMA);
190
191 /* don't know what this does */
192 psc_reg2(PSC_ENETWR_CTL) = 0x9000;
193 psc_reg2(PSC_ENETRD_CTL) = 0x9000;
194 psc_reg2(PSC_ENETWR_CTL) = 0x0400;
195 psc_reg2(PSC_ENETRD_CTL) = 0x0400;
196
197 /* enable MACE interrupts */
198 psc_reg1(PSC_LEV3_IER) = 0x80 | (1 << PSCINTR_ENET);
199
200 /* mcsetup returns 1 if something fails */
201 if (mcsetup(sc, myaddr)) {
202 /* disable interrupts */
203 psc_reg1(PSC_LEV4_IER) = (1 << PSCINTR_ENET_DMA);
204 psc_reg1(PSC_LEV3_IER) = (1 << PSCINTR_ENET);
205 /* remove interrupt handlers */
206 remove_psc_lev4_intr(PSCINTR_ENET_DMA);
207 remove_psc_lev3_intr();
208
209 bus_space_unmap(sc->sc_regt, sc->sc_regh, MC_REGSIZE);
210 return;
211 }
212 }
213
214 /* Bus-specific initialization */
215 hide void
216 mc_obio_init(sc)
217 struct mc_softc *sc;
218 {
219 mc_reset_rxdma(sc);
220 mc_reset_txdma(sc);
221 }
222
223 hide void
224 mc_obio_put(sc, len)
225 struct mc_softc *sc;
226 u_int len;
227 {
228 psc_reg4(PSC_ENETWR_ADDR + sc->sc_txset) = sc->sc_txbuf_phys;
229 psc_reg4(PSC_ENETWR_LEN + sc->sc_txset) = len;
230 psc_reg2(PSC_ENETWR_CMD + sc->sc_txset) = 0x9800;
231
232 sc->sc_txset ^= 0x10;
233 }
234
235 /*
236 * Interrupt handler for the MACE DMA completion interrupts
237 */
238 int
239 mc_dmaintr(arg)
240 void *arg;
241 {
242 struct mc_softc *sc = arg;
243 u_int16_t status;
244 u_int32_t bufsleft, which;
245 int head;
246
247 /*
248 * Not sure what this does... figure out if this interrupt is
249 * really ours?
250 */
251 while ((which = psc_reg4(0x804)) != psc_reg4(0x804))
252 ;
253 if ((which & 0x60000000) == 0)
254 return 0;
255
256 /* Get the read channel status */
257 status = psc_reg2(PSC_ENETRD_CTL);
258 if (status & 0x2000) {
259 /* I think this is an exceptional condition. Reset the DMA */
260 mc_reset_rxdma(sc);
261 #ifdef MCDEBUG
262 printf("%s: resetting receive DMA channel (status 0x%04x)\n",
263 sc->sc_dev.dv_xname, status);
264 #endif
265 } else if (status & 0x100) {
266 /* We've received some packets from the MACE */
267 int offset;
268
269 /* Clear the interrupt */
270 psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x1100;
271
272 /* See how may receive buffers are left */
273 bufsleft = psc_reg4(PSC_ENETRD_LEN + sc->sc_rxset);
274 head = MC_RXDMABUFS - bufsleft;
275
276 #if 0 /* I don't think this should ever happen */
277 if (head == sc->sc_tail) {
278 #ifdef MCDEBUG
279 printf("%s: head == tail: suspending DMA?\n",
280 sc->sc_dev.dv_xname);
281 #endif
282 psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x9000;
283 }
284 #endif
285
286 /* Loop through, processing each of the packets */
287 for (; sc->sc_tail < head; sc->sc_tail++) {
288 offset = sc->sc_tail * 0x800;
289 sc->sc_rxframe.rx_rcvcnt = sc->sc_rxbuf[offset];
290 sc->sc_rxframe.rx_rcvsts = sc->sc_rxbuf[offset+2];
291 sc->sc_rxframe.rx_rntpc = sc->sc_rxbuf[offset+4];
292 sc->sc_rxframe.rx_rcvcc = sc->sc_rxbuf[offset+6];
293 sc->sc_rxframe.rx_frame = sc->sc_rxbuf + offset + 16;
294
295 mc_rint(sc);
296 }
297
298 /*
299 * If we're out of buffers, reset this register set
300 * and switch to the other one. Otherwise, reactivate
301 * this set.
302 */
303 if (bufsleft == 0) {
304 mc_reset_rxdma_set(sc, sc->sc_rxset);
305 sc->sc_rxset ^= 0x10;
306 } else
307 psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x9800;
308 }
309
310 /* Get the write channel status */
311 status = psc_reg2(PSC_ENETWR_CTL);
312 if (status & 0x2000) {
313 /* I think this is an exceptional condition. Reset the DMA */
314 mc_reset_txdma(sc);
315 #ifdef MCDEBUG
316 printf("%s: resetting transmit DMA channel (status 0x%04x)\n",
317 sc->sc_dev.dv_xname, status);
318 #endif
319 } else if (status & 0x100) {
320 /* Clear the interrupt and switch register sets */
321 psc_reg2(PSC_ENETWR_CMD + sc->sc_txseti) = 0x100;
322 sc->sc_txseti ^= 0x10;
323 }
324
325 return 1;
326 }
327
328
329 hide void
330 mc_reset_rxdma(sc)
331 struct mc_softc *sc;
332 {
333 u_int8_t maccc;
334
335 /* Disable receiver, reset the DMA channels */
336 maccc = NIC_GET(sc, MACE_MACCC);
337 NIC_PUT(sc, MACE_MACCC, maccc & ~ENRCV);
338 psc_reg2(PSC_ENETRD_CTL) = 0x8800;
339 mc_reset_rxdma_set(sc, 0);
340 psc_reg2(PSC_ENETRD_CTL) = 0x400;
341
342 psc_reg2(PSC_ENETRD_CTL) = 0x8800;
343 mc_reset_rxdma_set(sc, 0x10);
344 psc_reg2(PSC_ENETRD_CTL) = 0x400;
345
346 /* Reenable receiver, reenable DMA */
347 NIC_PUT(sc, MACE_MACCC, maccc);
348 sc->sc_rxset = 0;
349
350 psc_reg2(PSC_ENETRD_CMD + PSC_SET0) = 0x9800;
351 psc_reg2(PSC_ENETRD_CMD + PSC_SET1) = 0x9800;
352 }
353
354 hide void
355 mc_reset_rxdma_set(sc, set)
356 struct mc_softc *sc;
357 int set;
358 {
359 /* disable DMA while modifying the registers, then reenable DMA */
360 psc_reg2(PSC_ENETRD_CMD + set) = 0x0100;
361 psc_reg4(PSC_ENETRD_ADDR + set) = sc->sc_rxbuf_phys;
362 psc_reg4(PSC_ENETRD_LEN + set) = MC_RXDMABUFS;
363 psc_reg2(PSC_ENETRD_CMD + set) = 0x9800;
364 sc->sc_tail = 0;
365 }
366
367 hide void
368 mc_reset_txdma(sc)
369 struct mc_softc *sc;
370 {
371 u_int8_t maccc;
372
373 psc_reg2(PSC_ENETWR_CTL) = 0x8800;
374 maccc = NIC_GET(sc, MACE_MACCC);
375 NIC_PUT(sc, MACE_MACCC, maccc & ~ENXMT);
376 sc->sc_txset = sc->sc_txseti = 0;
377 psc_reg2(PSC_ENETWR_CTL) = 0x400;
378 NIC_PUT(sc, MACE_MACCC, maccc);
379 }
380
381 hide int
382 mc_obio_getaddr(sc, lladdr)
383 struct mc_softc *sc;
384 u_int8_t *lladdr;
385 {
386 bus_space_handle_t bsh;
387 u_char csum;
388
389 if (bus_space_map(sc->sc_regt, MACE_PROM_BASE, 8*16, 0, &bsh)) {
390 printf(": failed to map space to read MACE address.\n%s",
391 sc->sc_dev.dv_xname);
392 return (-1);
393 }
394
395 if (!bus_probe(sc->sc_regt, bsh, 0, 1)) {
396 bus_space_unmap(sc->sc_regt, bsh, 8*16);
397 return (-1);
398 }
399
400 csum = mc_get_enaddr(sc->sc_regt, bsh, 1, lladdr);
401 if (csum != 0xff)
402 printf(": ethernet PROM checksum failed (0x%x != 0xff)\n%s",
403 (int)csum, sc->sc_dev.dv_xname);
404
405 bus_space_unmap(sc->sc_regt, bsh, 8*16);
406
407 return (csum == 0xff ? 0 : -1);
408 }
409