if_mc_obio.c revision 1.9 1 /* $NetBSD: if_mc_obio.c,v 1.9 2002/10/02 05:36:39 thorpej 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 "opt_ddb.h"
39
40 #include <sys/param.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/systm.h>
45
46 #include <net/if.h>
47 #include <net/if_ether.h>
48
49 #include <uvm/uvm_extern.h>
50
51 #include <machine/bus.h>
52 #include <machine/psc.h>
53
54 #include <mac68k/obio/obiovar.h>
55 #include <mac68k/dev/if_mcreg.h>
56 #include <mac68k/dev/if_mcvar.h>
57
58 #define MACE_REG_BASE 0x50F1C000
59 #define MACE_PROM_BASE 0x50F08000
60
61 hide int mc_obio_match __P((struct device *, struct cfdata *, void *));
62 hide void mc_obio_attach __P((struct device *, struct device *, void *));
63 hide void mc_obio_init __P((struct mc_softc *sc));
64 hide void mc_obio_put __P((struct mc_softc *sc, u_int len));
65 hide int mc_dmaintr __P((void *arg));
66 hide void mc_reset_rxdma __P((struct mc_softc *sc));
67 hide void mc_reset_rxdma_set __P((struct mc_softc *, int set));
68 hide void mc_reset_txdma __P((struct mc_softc *sc));
69 hide int mc_obio_getaddr __P((struct mc_softc *, u_int8_t *));
70
71 extern int kvtop __P((register caddr_t addr));
72
73 CFATTACH_DECL(mc_obio, sizeof(struct mc_softc),
74 mc_obio_match, mc_obio_attach, NULL, NULL);
75
76 hide int
77 mc_obio_match(parent, cf, aux)
78 struct device *parent;
79 struct cfdata *cf;
80 void *aux;
81 {
82 struct obio_attach_args *oa = aux;
83 bus_space_handle_t bsh;
84 int found = 0;
85
86 if (current_mac_model->class != MACH_CLASSAV)
87 return 0;
88
89 if (bus_space_map(oa->oa_tag, MACE_REG_BASE, MC_REGSIZE, 0, &bsh))
90 return 0;
91
92 /*
93 * Make sure the MACE's I/O space is readable, and if it is,
94 * try to read the CHIPID register. A MACE will always have
95 * 0x?940, where the ? depends on the chip version.
96 */
97 if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 1)) {
98 if ((bus_space_read_1(
99 oa->oa_tag, bsh, MACE_REG(MACE_CHIPIDL)) == 0x40) &&
100 ((bus_space_read_1(
101 oa->oa_tag, bsh, MACE_REG(MACE_CHIPIDH)) & 0xf) == 9))
102 found = 1;
103 }
104
105 bus_space_unmap(oa->oa_tag, bsh, MC_REGSIZE);
106
107 return found;
108 }
109
110 hide void
111 mc_obio_attach(parent, self, aux)
112 struct device *parent, *self;
113 void *aux;
114 {
115 struct obio_attach_args *oa = (struct obio_attach_args *)aux;
116 struct mc_softc *sc = (void *)self;
117 u_int8_t myaddr[ETHER_ADDR_LEN];
118 int i, noncontig = 0;
119
120 sc->sc_regt = oa->oa_tag;
121 sc->sc_biucc = XMTSP_64;
122 sc->sc_fifocc = XMTFW_16 | RCVFW_64 | XMTFWU | RCVFWU |
123 XMTBRST | RCVBRST;
124 sc->sc_plscc = PORTSEL_AUI;
125
126 if (bus_space_map(sc->sc_regt, MACE_REG_BASE, MC_REGSIZE, 0,
127 &sc->sc_regh)) {
128 printf(": failed to map space for MACE regs.\n");
129 return;
130 }
131
132 if (mc_obio_getaddr(sc, myaddr)) {
133 printf(": failed to get MAC address.\n");
134 return;
135 }
136
137 /* allocate memory for transmit buffer and mark it non-cacheable */
138 sc->sc_txbuf = malloc(NBPG, M_DEVBUF, M_WAITOK);
139 sc->sc_txbuf_phys = kvtop(sc->sc_txbuf);
140 physaccess (sc->sc_txbuf, (caddr_t)sc->sc_txbuf_phys, NBPG,
141 PG_V | PG_RW | PG_CI);
142
143 /*
144 * allocate memory for receive buffer and mark it non-cacheable
145 * XXX This should use the bus_dma interface, since the buffer
146 * needs to be physically contiguous. However, it seems that
147 * at least on my system, malloc() does allocate contiguous
148 * memory. If it's not, suggest reducing the number of buffers
149 * to 2, which will fit in one 4K page.
150 */
151 sc->sc_rxbuf = malloc(MC_NPAGES * NBPG, M_DEVBUF, M_WAITOK);
152 sc->sc_rxbuf_phys = kvtop(sc->sc_rxbuf);
153 for (i = 0; i < MC_NPAGES; i++) {
154 int pa;
155
156 pa = kvtop(sc->sc_rxbuf + NBPG*i);
157 physaccess (sc->sc_rxbuf + NBPG*i, (caddr_t)pa, NBPG,
158 PG_V | PG_RW | PG_CI);
159 if (pa != sc->sc_rxbuf_phys + NBPG*i)
160 noncontig = 1;
161 }
162
163 if (noncontig) {
164 printf("%s: receive DMA buffer not contiguous! "
165 "Try compiling with \"options MC_RXDMABUFS=2\"\n",
166 sc->sc_dev.dv_xname);
167 return;
168 }
169
170 sc->sc_bus_init = mc_obio_init;
171 sc->sc_putpacket = mc_obio_put;
172
173 /* disable receive DMA */
174 psc_reg2(PSC_ENETRD_CTL) = 0x8800;
175 psc_reg2(PSC_ENETRD_CTL) = 0x1000;
176 psc_reg2(PSC_ENETRD_CMD + PSC_SET0) = 0x1100;
177 psc_reg2(PSC_ENETRD_CMD + PSC_SET1) = 0x1100;
178
179 /* disable transmit DMA */
180 psc_reg2(PSC_ENETWR_CTL) = 0x8800;
181 psc_reg2(PSC_ENETWR_CTL) = 0x1000;
182 psc_reg2(PSC_ENETWR_CMD + PSC_SET0) = 0x1100;
183 psc_reg2(PSC_ENETWR_CMD + PSC_SET1) = 0x1100;
184
185 /* install interrupt handlers */
186 add_psc_lev4_intr(PSCINTR_ENET_DMA, mc_dmaintr, sc);
187 add_psc_lev3_intr(mcintr, sc);
188
189 /* enable MACE DMA interrupts */
190 psc_reg1(PSC_LEV4_IER) = 0x80 | (1 << PSCINTR_ENET_DMA);
191
192 /* don't know what this does */
193 psc_reg2(PSC_ENETWR_CTL) = 0x9000;
194 psc_reg2(PSC_ENETRD_CTL) = 0x9000;
195 psc_reg2(PSC_ENETWR_CTL) = 0x0400;
196 psc_reg2(PSC_ENETRD_CTL) = 0x0400;
197
198 /* enable MACE interrupts */
199 psc_reg1(PSC_LEV3_IER) = 0x80 | (1 << PSCINTR_ENET);
200
201 /* mcsetup returns 1 if something fails */
202 if (mcsetup(sc, myaddr)) {
203 /* disable interrupts */
204 psc_reg1(PSC_LEV4_IER) = (1 << PSCINTR_ENET_DMA);
205 psc_reg1(PSC_LEV3_IER) = (1 << PSCINTR_ENET);
206 /* remove interrupt handlers */
207 remove_psc_lev4_intr(PSCINTR_ENET_DMA);
208 remove_psc_lev3_intr();
209
210 bus_space_unmap(sc->sc_regt, sc->sc_regh, MC_REGSIZE);
211 return;
212 }
213 }
214
215 /* Bus-specific initialization */
216 hide void
217 mc_obio_init(sc)
218 struct mc_softc *sc;
219 {
220 mc_reset_rxdma(sc);
221 mc_reset_txdma(sc);
222 }
223
224 hide void
225 mc_obio_put(sc, len)
226 struct mc_softc *sc;
227 u_int len;
228 {
229 psc_reg4(PSC_ENETWR_ADDR + sc->sc_txset) = sc->sc_txbuf_phys;
230 psc_reg4(PSC_ENETWR_LEN + sc->sc_txset) = len;
231 psc_reg2(PSC_ENETWR_CMD + sc->sc_txset) = 0x9800;
232
233 sc->sc_txset ^= 0x10;
234 }
235
236 /*
237 * Interrupt handler for the MACE DMA completion interrupts
238 */
239 int
240 mc_dmaintr(arg)
241 void *arg;
242 {
243 struct mc_softc *sc = arg;
244 u_int16_t status;
245 u_int32_t bufsleft, which;
246 int head;
247
248 /*
249 * Not sure what this does... figure out if this interrupt is
250 * really ours?
251 */
252 while ((which = psc_reg4(0x804)) != psc_reg4(0x804))
253 ;
254 if ((which & 0x60000000) == 0)
255 return 0;
256
257 /* Get the read channel status */
258 status = psc_reg2(PSC_ENETRD_CTL);
259 if (status & 0x2000) {
260 /* I think this is an exceptional condition. Reset the DMA */
261 mc_reset_rxdma(sc);
262 #ifdef MCDEBUG
263 printf("%s: resetting receive DMA channel (status 0x%04x)\n",
264 sc->sc_dev.dv_xname, status);
265 #endif
266 } else if (status & 0x100) {
267 /* We've received some packets from the MACE */
268 int offset;
269
270 /* Clear the interrupt */
271 psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x1100;
272
273 /* See how may receive buffers are left */
274 bufsleft = psc_reg4(PSC_ENETRD_LEN + sc->sc_rxset);
275 head = MC_RXDMABUFS - bufsleft;
276
277 #if 0 /* I don't think this should ever happen */
278 if (head == sc->sc_tail) {
279 #ifdef MCDEBUG
280 printf("%s: head == tail: suspending DMA?\n",
281 sc->sc_dev.dv_xname);
282 #endif
283 psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x9000;
284 }
285 #endif
286
287 /* Loop through, processing each of the packets */
288 for (; sc->sc_tail < head; sc->sc_tail++) {
289 offset = sc->sc_tail * 0x800;
290 sc->sc_rxframe.rx_rcvcnt = sc->sc_rxbuf[offset];
291 sc->sc_rxframe.rx_rcvsts = sc->sc_rxbuf[offset+2];
292 sc->sc_rxframe.rx_rntpc = sc->sc_rxbuf[offset+4];
293 sc->sc_rxframe.rx_rcvcc = sc->sc_rxbuf[offset+6];
294 sc->sc_rxframe.rx_frame = sc->sc_rxbuf + offset + 16;
295
296 mc_rint(sc);
297 }
298
299 /*
300 * If we're out of buffers, reset this register set
301 * and switch to the other one. Otherwise, reactivate
302 * this set.
303 */
304 if (bufsleft == 0) {
305 mc_reset_rxdma_set(sc, sc->sc_rxset);
306 sc->sc_rxset ^= 0x10;
307 } else
308 psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x9800;
309 }
310
311 /* Get the write channel status */
312 status = psc_reg2(PSC_ENETWR_CTL);
313 if (status & 0x2000) {
314 /* I think this is an exceptional condition. Reset the DMA */
315 mc_reset_txdma(sc);
316 #ifdef MCDEBUG
317 printf("%s: resetting transmit DMA channel (status 0x%04x)\n",
318 sc->sc_dev.dv_xname, status);
319 #endif
320 } else if (status & 0x100) {
321 /* Clear the interrupt and switch register sets */
322 psc_reg2(PSC_ENETWR_CMD + sc->sc_txseti) = 0x100;
323 sc->sc_txseti ^= 0x10;
324 }
325
326 return 1;
327 }
328
329
330 hide void
331 mc_reset_rxdma(sc)
332 struct mc_softc *sc;
333 {
334 u_int8_t maccc;
335
336 /* Disable receiver, reset the DMA channels */
337 maccc = NIC_GET(sc, MACE_MACCC);
338 NIC_PUT(sc, MACE_MACCC, maccc & ~ENRCV);
339 psc_reg2(PSC_ENETRD_CTL) = 0x8800;
340 mc_reset_rxdma_set(sc, 0);
341 psc_reg2(PSC_ENETRD_CTL) = 0x400;
342
343 psc_reg2(PSC_ENETRD_CTL) = 0x8800;
344 mc_reset_rxdma_set(sc, 0x10);
345 psc_reg2(PSC_ENETRD_CTL) = 0x400;
346
347 /* Reenable receiver, reenable DMA */
348 NIC_PUT(sc, MACE_MACCC, maccc);
349 sc->sc_rxset = 0;
350
351 psc_reg2(PSC_ENETRD_CMD + PSC_SET0) = 0x9800;
352 psc_reg2(PSC_ENETRD_CMD + PSC_SET1) = 0x9800;
353 }
354
355 hide void
356 mc_reset_rxdma_set(sc, set)
357 struct mc_softc *sc;
358 int set;
359 {
360 /* disable DMA while modifying the registers, then reenable DMA */
361 psc_reg2(PSC_ENETRD_CMD + set) = 0x0100;
362 psc_reg4(PSC_ENETRD_ADDR + set) = sc->sc_rxbuf_phys;
363 psc_reg4(PSC_ENETRD_LEN + set) = MC_RXDMABUFS;
364 psc_reg2(PSC_ENETRD_CMD + set) = 0x9800;
365 sc->sc_tail = 0;
366 }
367
368 hide void
369 mc_reset_txdma(sc)
370 struct mc_softc *sc;
371 {
372 u_int8_t maccc;
373
374 psc_reg2(PSC_ENETWR_CTL) = 0x8800;
375 maccc = NIC_GET(sc, MACE_MACCC);
376 NIC_PUT(sc, MACE_MACCC, maccc & ~ENXMT);
377 sc->sc_txset = sc->sc_txseti = 0;
378 psc_reg2(PSC_ENETWR_CTL) = 0x400;
379 NIC_PUT(sc, MACE_MACCC, maccc);
380 }
381
382 hide int
383 mc_obio_getaddr(sc, lladdr)
384 struct mc_softc *sc;
385 u_int8_t *lladdr;
386 {
387 bus_space_handle_t bsh;
388 u_char csum;
389
390 if (bus_space_map(sc->sc_regt, MACE_PROM_BASE, 8*16, 0, &bsh)) {
391 printf(": failed to map space to read MACE address.\n%s",
392 sc->sc_dev.dv_xname);
393 return (-1);
394 }
395
396 if (!mac68k_bus_space_probe(sc->sc_regt, bsh, 0, 1)) {
397 bus_space_unmap(sc->sc_regt, bsh, 8*16);
398 return (-1);
399 }
400
401 csum = mc_get_enaddr(sc->sc_regt, bsh, 1, lladdr);
402 if (csum != 0xff)
403 printf(": ethernet PROM checksum failed (0x%x != 0xff)\n%s",
404 (int)csum, sc->sc_dev.dv_xname);
405
406 bus_space_unmap(sc->sc_regt, bsh, 8*16);
407
408 return (csum == 0xff ? 0 : -1);
409 }
410