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#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/types.h>
38#include <linux/fcntl.h>
39#include <linux/gfp.h>
40#include <linux/interrupt.h>
41#include <linux/ioport.h>
42#include <linux/in.h>
43#include <linux/string.h>
44#include <linux/delay.h>
45#include <linux/nubus.h>
46#include <linux/errno.h>
47#include <linux/netdevice.h>
48#include <linux/etherdevice.h>
49#include <linux/skbuff.h>
50#include <linux/platform_device.h>
51#include <linux/dma-mapping.h>
52#include <linux/bitrev.h>
53#include <linux/slab.h>
54
55#include <asm/pgtable.h>
56#include <asm/io.h>
57#include <asm/hwtest.h>
58#include <asm/dma.h>
59#include <asm/macintosh.h>
60#include <asm/macints.h>
61#include <asm/mac_via.h>
62
63#include "sonic.h"
64
65
66
67
68#define SONIC_READ(reg) (nubus_readw(dev->base_addr + (reg * 4) \
69 + lp->reg_offset))
70#define SONIC_WRITE(reg,val) (nubus_writew(val, dev->base_addr + (reg * 4) \
71 + lp->reg_offset))
72
73
74#define ONBOARD_SONIC_REGISTERS 0x50F0A000
75#define ONBOARD_SONIC_PROM_BASE 0x50f08000
76
77enum macsonic_type {
78 MACSONIC_DUODOCK,
79 MACSONIC_APPLE,
80 MACSONIC_APPLE16,
81 MACSONIC_DAYNA,
82 MACSONIC_DAYNALINK
83};
84
85
86#define DUODOCK_SONIC_REGISTERS 0xe10000
87#define DUODOCK_SONIC_PROM_BASE 0xe12000
88
89
90#define APPLE_SONIC_REGISTERS 0
91#define APPLE_SONIC_PROM_BASE 0x40000
92
93
94#define DAYNALINK_PROM_BASE 0x400000
95
96
97#define DAYNA_SONIC_REGISTERS 0x180000
98
99
100
101#define DAYNA_SONIC_MAC_ADDR 0xffe004
102
103#define SONIC_READ_PROM(addr) nubus_readb(prom_addr+addr)
104
105
106
107
108
109static inline void bit_reverse_addr(unsigned char addr[6])
110{
111 int i;
112
113 for(i = 0; i < 6; i++)
114 addr[i] = bitrev8(addr[i]);
115}
116
117static irqreturn_t macsonic_interrupt(int irq, void *dev_id)
118{
119 irqreturn_t result;
120 unsigned long flags;
121
122 local_irq_save(flags);
123 result = sonic_interrupt(irq, dev_id);
124 local_irq_restore(flags);
125 return result;
126}
127
128static int macsonic_open(struct net_device* dev)
129{
130 int retval;
131
132 retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
133 if (retval) {
134 printk(KERN_ERR "%s: unable to get IRQ %d.\n",
135 dev->name, dev->irq);
136 goto err;
137 }
138
139
140
141
142 if (dev->irq == IRQ_AUTO_3) {
143 retval = request_irq(IRQ_NUBUS_9, macsonic_interrupt, 0,
144 "sonic", dev);
145 if (retval) {
146 printk(KERN_ERR "%s: unable to get IRQ %d.\n",
147 dev->name, IRQ_NUBUS_9);
148 goto err_irq;
149 }
150 }
151 retval = sonic_open(dev);
152 if (retval)
153 goto err_irq_nubus;
154 return 0;
155
156err_irq_nubus:
157 if (dev->irq == IRQ_AUTO_3)
158 free_irq(IRQ_NUBUS_9, dev);
159err_irq:
160 free_irq(dev->irq, dev);
161err:
162 return retval;
163}
164
165static int macsonic_close(struct net_device* dev)
166{
167 int err;
168 err = sonic_close(dev);
169 free_irq(dev->irq, dev);
170 if (dev->irq == IRQ_AUTO_3)
171 free_irq(IRQ_NUBUS_9, dev);
172 return err;
173}
174
175static const struct net_device_ops macsonic_netdev_ops = {
176 .ndo_open = macsonic_open,
177 .ndo_stop = macsonic_close,
178 .ndo_start_xmit = sonic_send_packet,
179 .ndo_set_rx_mode = sonic_multicast_list,
180 .ndo_tx_timeout = sonic_tx_timeout,
181 .ndo_get_stats = sonic_get_stats,
182 .ndo_validate_addr = eth_validate_addr,
183 .ndo_set_mac_address = eth_mac_addr,
184};
185
186static int macsonic_init(struct net_device *dev)
187{
188 struct sonic_local* lp = netdev_priv(dev);
189
190
191
192 lp->descriptors = dma_alloc_coherent(lp->device,
193 SIZEOF_SONIC_DESC *
194 SONIC_BUS_SCALE(lp->dma_bitmode),
195 &lp->descriptors_laddr,
196 GFP_KERNEL);
197 if (lp->descriptors == NULL)
198 return -ENOMEM;
199
200
201 lp->cda = lp->descriptors;
202 lp->tda = lp->cda + (SIZEOF_SONIC_CDA
203 * SONIC_BUS_SCALE(lp->dma_bitmode));
204 lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
205 * SONIC_BUS_SCALE(lp->dma_bitmode));
206 lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
207 * SONIC_BUS_SCALE(lp->dma_bitmode));
208
209 lp->cda_laddr = lp->descriptors_laddr;
210 lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
211 * SONIC_BUS_SCALE(lp->dma_bitmode));
212 lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
213 * SONIC_BUS_SCALE(lp->dma_bitmode));
214 lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
215 * SONIC_BUS_SCALE(lp->dma_bitmode));
216
217 dev->netdev_ops = &macsonic_netdev_ops;
218 dev->watchdog_timeo = TX_TIMEOUT;
219
220
221
222
223 SONIC_WRITE(SONIC_CRCT, 0xffff);
224 SONIC_WRITE(SONIC_FAET, 0xffff);
225 SONIC_WRITE(SONIC_MPT, 0xffff);
226
227 return 0;
228}
229
230#define INVALID_MAC(mac) (memcmp(mac, "\x08\x00\x07", 3) && \
231 memcmp(mac, "\x00\xA0\x40", 3) && \
232 memcmp(mac, "\x00\x80\x19", 3) && \
233 memcmp(mac, "\x00\x05\x02", 3))
234
235static void mac_onboard_sonic_ethernet_addr(struct net_device *dev)
236{
237 struct sonic_local *lp = netdev_priv(dev);
238 const int prom_addr = ONBOARD_SONIC_PROM_BASE;
239 unsigned short val;
240
241
242
243
244
245
246 if (hwreg_present((void *)prom_addr)) {
247 int i;
248
249 for (i = 0; i < 6; i++)
250 dev->dev_addr[i] = SONIC_READ_PROM(i);
251 if (!INVALID_MAC(dev->dev_addr))
252 return;
253
254
255
256
257
258
259 bit_reverse_addr(dev->dev_addr);
260 if (!INVALID_MAC(dev->dev_addr))
261 return;
262
263
264
265
266
267 printk(KERN_WARNING "macsonic: MAC address in PROM seems "
268 "to be invalid, trying CAM\n");
269 } else {
270 printk(KERN_WARNING "macsonic: cannot read MAC address from "
271 "PROM, trying CAM\n");
272 }
273
274
275
276 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
277 SONIC_WRITE(SONIC_CEP, 15);
278
279 val = SONIC_READ(SONIC_CAP2);
280 dev->dev_addr[5] = val >> 8;
281 dev->dev_addr[4] = val & 0xff;
282 val = SONIC_READ(SONIC_CAP1);
283 dev->dev_addr[3] = val >> 8;
284 dev->dev_addr[2] = val & 0xff;
285 val = SONIC_READ(SONIC_CAP0);
286 dev->dev_addr[1] = val >> 8;
287 dev->dev_addr[0] = val & 0xff;
288
289 if (!INVALID_MAC(dev->dev_addr))
290 return;
291
292
293
294 printk(KERN_WARNING "macsonic: MAC address in CAM entry 15 "
295 "seems invalid, will use a random MAC\n");
296 eth_hw_addr_random(dev);
297}
298
299static int mac_onboard_sonic_probe(struct net_device *dev)
300{
301 struct sonic_local* lp = netdev_priv(dev);
302 int sr;
303 bool commslot = macintosh_config->expansion_type == MAC_EXP_PDS_COMM;
304
305
306
307
308
309 if (commslot || macintosh_config->ident == MAC_MODEL_C610) {
310 int card_present;
311
312 card_present = hwreg_present((void*)ONBOARD_SONIC_REGISTERS);
313 if (!card_present) {
314 pr_info("Onboard/comm-slot SONIC not found\n");
315 return -ENODEV;
316 }
317 }
318
319
320
321 dev->base_addr = ONBOARD_SONIC_REGISTERS;
322 if (via_alt_mapping)
323 dev->irq = IRQ_AUTO_3;
324 else
325 dev->irq = IRQ_NUBUS_9;
326
327
328 if (macintosh_config->ident == MAC_MODEL_PB520) {
329 lp->reg_offset = 0;
330 lp->dma_bitmode = SONIC_BITMODE16;
331 } else if (commslot) {
332
333
334
335
336
337 lp->reg_offset = 2;
338 lp->dma_bitmode = SONIC_BITMODE16;
339
340 sr = SONIC_READ(SONIC_SR);
341 if (sr == 0x0004 || sr == 0x0006 || sr == 0x0100 || sr == 0x0101)
342
343 lp->dma_bitmode = SONIC_BITMODE32;
344 else {
345 lp->dma_bitmode = SONIC_BITMODE16;
346 lp->reg_offset = 0;
347 }
348 } else {
349
350 lp->reg_offset = 2;
351 lp->dma_bitmode = SONIC_BITMODE32;
352 }
353
354 pr_info("Onboard/comm-slot SONIC, revision 0x%04x, %d bit DMA, register offset %d\n",
355 SONIC_READ(SONIC_SR), lp->dma_bitmode ? 32 : 16,
356 lp->reg_offset);
357
358
359 pr_debug("%s: DCR=0x%04x, DCR2=0x%04x\n", __func__,
360 SONIC_READ(SONIC_DCR) & 0xffff,
361 SONIC_READ(SONIC_DCR2) & 0xffff);
362
363
364 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
365
366 SONIC_WRITE(SONIC_DCR, SONIC_DCR_EXBUS | SONIC_DCR_BMS |
367 SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
368 (lp->dma_bitmode ? SONIC_DCR_DW : 0));
369
370
371
372
373 SONIC_WRITE(SONIC_DCR2, 0);
374
375
376 SONIC_WRITE(SONIC_IMR, 0);
377 SONIC_WRITE(SONIC_ISR, 0x7fff);
378
379
380 mac_onboard_sonic_ethernet_addr(dev);
381
382 pr_info("SONIC ethernet @%08lx, MAC %pM, IRQ %d\n",
383 dev->base_addr, dev->dev_addr, dev->irq);
384
385
386 return macsonic_init(dev);
387}
388
389static int mac_sonic_nubus_ethernet_addr(struct net_device *dev,
390 unsigned long prom_addr, int id)
391{
392 int i;
393 for(i = 0; i < 6; i++)
394 dev->dev_addr[i] = SONIC_READ_PROM(i);
395
396
397 if (id != MACSONIC_DAYNA)
398 bit_reverse_addr(dev->dev_addr);
399
400 return 0;
401}
402
403static int macsonic_ident(struct nubus_rsrc *fres)
404{
405 if (fres->dr_hw == NUBUS_DRHW_ASANTE_LC &&
406 fres->dr_sw == NUBUS_DRSW_SONIC_LC)
407 return MACSONIC_DAYNALINK;
408 if (fres->dr_hw == NUBUS_DRHW_SONIC &&
409 fres->dr_sw == NUBUS_DRSW_APPLE) {
410
411 if (strstr(fres->board->name, "DuoDock"))
412 return MACSONIC_DUODOCK;
413 else
414 return MACSONIC_APPLE;
415 }
416
417 if (fres->dr_hw == NUBUS_DRHW_SMC9194 &&
418 fres->dr_sw == NUBUS_DRSW_DAYNA)
419 return MACSONIC_DAYNA;
420
421 if (fres->dr_hw == NUBUS_DRHW_APPLE_SONIC_LC &&
422 fres->dr_sw == 0) {
423 return MACSONIC_APPLE16;
424 }
425 return -1;
426}
427
428static int mac_sonic_nubus_probe_board(struct nubus_board *board, int id,
429 struct net_device *dev)
430{
431 struct sonic_local* lp = netdev_priv(dev);
432 unsigned long base_addr, prom_addr;
433 u16 sonic_dcr;
434 int reg_offset, dma_bitmode;
435
436 switch (id) {
437 case MACSONIC_DUODOCK:
438 base_addr = board->slot_addr + DUODOCK_SONIC_REGISTERS;
439 prom_addr = board->slot_addr + DUODOCK_SONIC_PROM_BASE;
440 sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT0 | SONIC_DCR_RFT1 |
441 SONIC_DCR_TFT0;
442 reg_offset = 2;
443 dma_bitmode = SONIC_BITMODE32;
444 break;
445 case MACSONIC_APPLE:
446 base_addr = board->slot_addr + APPLE_SONIC_REGISTERS;
447 prom_addr = board->slot_addr + APPLE_SONIC_PROM_BASE;
448 sonic_dcr = SONIC_DCR_BMS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0;
449 reg_offset = 0;
450 dma_bitmode = SONIC_BITMODE32;
451 break;
452 case MACSONIC_APPLE16:
453 base_addr = board->slot_addr + APPLE_SONIC_REGISTERS;
454 prom_addr = board->slot_addr + APPLE_SONIC_PROM_BASE;
455 sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
456 SONIC_DCR_PO1 | SONIC_DCR_BMS;
457 reg_offset = 0;
458 dma_bitmode = SONIC_BITMODE16;
459 break;
460 case MACSONIC_DAYNALINK:
461 base_addr = board->slot_addr + APPLE_SONIC_REGISTERS;
462 prom_addr = board->slot_addr + DAYNALINK_PROM_BASE;
463 sonic_dcr = SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
464 SONIC_DCR_PO1 | SONIC_DCR_BMS;
465 reg_offset = 0;
466 dma_bitmode = SONIC_BITMODE16;
467 break;
468 case MACSONIC_DAYNA:
469 base_addr = board->slot_addr + DAYNA_SONIC_REGISTERS;
470 prom_addr = board->slot_addr + DAYNA_SONIC_MAC_ADDR;
471 sonic_dcr = SONIC_DCR_BMS |
472 SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_PO1;
473 reg_offset = 0;
474 dma_bitmode = SONIC_BITMODE16;
475 break;
476 default:
477 printk(KERN_ERR "macsonic: WTF, id is %d\n", id);
478 return -ENODEV;
479 }
480
481
482
483 dev->base_addr = base_addr;
484 lp->reg_offset = reg_offset;
485 lp->dma_bitmode = dma_bitmode;
486 dev->irq = SLOT2IRQ(board->slot);
487
488 dev_info(&board->dev, "%s, revision 0x%04x, %d bit DMA, register offset %d\n",
489 board->name, SONIC_READ(SONIC_SR),
490 lp->dma_bitmode ? 32 : 16, lp->reg_offset);
491
492
493 dev_dbg(&board->dev, "%s: DCR=0x%04x, DCR2=0x%04x\n", __func__,
494 SONIC_READ(SONIC_DCR) & 0xffff,
495 SONIC_READ(SONIC_DCR2) & 0xffff);
496
497
498 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
499 SONIC_WRITE(SONIC_DCR, sonic_dcr | (dma_bitmode ? SONIC_DCR_DW : 0));
500
501
502
503 SONIC_WRITE(SONIC_DCR2, 0);
504
505
506 SONIC_WRITE(SONIC_IMR, 0);
507 SONIC_WRITE(SONIC_ISR, 0x7fff);
508
509
510 if (mac_sonic_nubus_ethernet_addr(dev, prom_addr, id) != 0)
511 return -ENODEV;
512
513 dev_info(&board->dev, "SONIC ethernet @%08lx, MAC %pM, IRQ %d\n",
514 dev->base_addr, dev->dev_addr, dev->irq);
515
516
517 return macsonic_init(dev);
518}
519
520static int mac_sonic_platform_probe(struct platform_device *pdev)
521{
522 struct net_device *dev;
523 struct sonic_local *lp;
524 int err;
525
526 dev = alloc_etherdev(sizeof(struct sonic_local));
527 if (!dev)
528 return -ENOMEM;
529
530 lp = netdev_priv(dev);
531 lp->device = &pdev->dev;
532 SET_NETDEV_DEV(dev, &pdev->dev);
533 platform_set_drvdata(pdev, dev);
534
535 err = mac_onboard_sonic_probe(dev);
536 if (err)
537 goto out;
538
539 sonic_msg_init(dev);
540
541 err = register_netdev(dev);
542 if (err)
543 goto out;
544
545 return 0;
546
547out:
548 free_netdev(dev);
549
550 return err;
551}
552
553MODULE_DESCRIPTION("Macintosh SONIC ethernet driver");
554MODULE_ALIAS("platform:macsonic");
555
556#include "sonic.c"
557
558static int mac_sonic_platform_remove(struct platform_device *pdev)
559{
560 struct net_device *dev = platform_get_drvdata(pdev);
561 struct sonic_local* lp = netdev_priv(dev);
562
563 unregister_netdev(dev);
564 dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
565 lp->descriptors, lp->descriptors_laddr);
566 free_netdev(dev);
567
568 return 0;
569}
570
571static struct platform_driver mac_sonic_platform_driver = {
572 .probe = mac_sonic_platform_probe,
573 .remove = mac_sonic_platform_remove,
574 .driver = {
575 .name = "macsonic",
576 },
577};
578
579static int mac_sonic_nubus_probe(struct nubus_board *board)
580{
581 struct net_device *ndev;
582 struct sonic_local *lp;
583 struct nubus_rsrc *fres;
584 int id = -1;
585 int err;
586
587
588
589
590 if (macintosh_config->expansion_type == MAC_EXP_PDS_COMM)
591 return -ENODEV;
592
593 for_each_board_func_rsrc(board, fres) {
594 if (fres->category != NUBUS_CAT_NETWORK ||
595 fres->type != NUBUS_TYPE_ETHERNET)
596 continue;
597
598 id = macsonic_ident(fres);
599 if (id != -1)
600 break;
601 }
602 if (!fres)
603 return -ENODEV;
604
605 ndev = alloc_etherdev(sizeof(struct sonic_local));
606 if (!ndev)
607 return -ENOMEM;
608
609 lp = netdev_priv(ndev);
610 lp->device = &board->dev;
611 SET_NETDEV_DEV(ndev, &board->dev);
612
613 err = mac_sonic_nubus_probe_board(board, id, ndev);
614 if (err)
615 goto out;
616
617 sonic_msg_init(ndev);
618
619 err = register_netdev(ndev);
620 if (err)
621 goto out;
622
623 nubus_set_drvdata(board, ndev);
624
625 return 0;
626
627out:
628 free_netdev(ndev);
629 return err;
630}
631
632static int mac_sonic_nubus_remove(struct nubus_board *board)
633{
634 struct net_device *ndev = nubus_get_drvdata(board);
635 struct sonic_local *lp = netdev_priv(ndev);
636
637 unregister_netdev(ndev);
638 dma_free_coherent(lp->device,
639 SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
640 lp->descriptors, lp->descriptors_laddr);
641 free_netdev(ndev);
642
643 return 0;
644}
645
646static struct nubus_driver mac_sonic_nubus_driver = {
647 .probe = mac_sonic_nubus_probe,
648 .remove = mac_sonic_nubus_remove,
649 .driver = {
650 .name = "macsonic-nubus",
651 .owner = THIS_MODULE,
652 },
653};
654
655static int perr, nerr;
656
657static int __init mac_sonic_init(void)
658{
659 perr = platform_driver_register(&mac_sonic_platform_driver);
660 nerr = nubus_driver_register(&mac_sonic_nubus_driver);
661 return 0;
662}
663module_init(mac_sonic_init);
664
665static void __exit mac_sonic_exit(void)
666{
667 if (!perr)
668 platform_driver_unregister(&mac_sonic_platform_driver);
669 if (!nerr)
670 nubus_driver_unregister(&mac_sonic_nubus_driver);
671}
672module_exit(mac_sonic_exit);
673