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