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