Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
macsonic.c
Go to the documentation of this file.
1 /*
2  * macsonic.c
3  *
4  * (C) 2005 Finn Thain
5  *
6  * Converted to DMA API, converted to unified driver model, made it work as
7  * a module again, and from the mac68k project, introduced more 32-bit cards
8  * and dhd's support for 16-bit cards.
9  *
10  * (C) 1998 Alan Cox
11  *
12  * Debugging Andreas Ehliar, Michael Schmitz
13  *
14  * Based on code
15  * (C) 1996 by Thomas Bogendoerfer ([email protected])
16  *
17  * This driver is based on work from Andreas Busse, but most of
18  * the code is rewritten.
19  *
20  * (C) 1995 by Andreas Busse ([email protected])
21  *
22  * A driver for the Mac onboard Sonic ethernet chip.
23  *
24  * 98/12/21 MSch: judged from tests on Q800, it's basically working,
25  * but eating up both receive and transmit resources
26  * and duplicating packets. Needs more testing.
27  *
28  * 99/01/03 MSch: upgraded to version 0.92 of the core driver, fixed.
29  *
30  * 00/10/31 [email protected]: Updated driver for 2.4 kernels, fixed problems
31  * on centris.
32  */
33 
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/fcntl.h>
38 #include <linux/gfp.h>
39 #include <linux/interrupt.h>
40 #include <linux/init.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/bootinfo.h>
56 #include <asm/pgtable.h>
57 #include <asm/io.h>
58 #include <asm/hwtest.h>
59 #include <asm/dma.h>
60 #include <asm/macintosh.h>
61 #include <asm/macints.h>
62 #include <asm/mac_via.h>
63 
64 static char mac_sonic_string[] = "macsonic";
65 
66 #include "sonic.h"
67 
68 /* These should basically be bus-size and endian independent (since
69  the SONIC is at least smart enough that it uses the same endianness
70  as the host, unlike certain less enlightened Macintosh NICs) */
71 #define SONIC_READ(reg) (nubus_readw(dev->base_addr + (reg * 4) \
72  + lp->reg_offset))
73 #define SONIC_WRITE(reg,val) (nubus_writew(val, dev->base_addr + (reg * 4) \
74  + lp->reg_offset))
75 
76 /* use 0 for production, 1 for verification, >1 for debug */
77 #ifdef SONIC_DEBUG
78 static unsigned int sonic_debug = SONIC_DEBUG;
79 #else
80 static unsigned int sonic_debug = 1;
81 #endif
82 
83 static int sonic_version_printed;
84 
85 /* For onboard SONIC */
86 #define ONBOARD_SONIC_REGISTERS 0x50F0A000
87 #define ONBOARD_SONIC_PROM_BASE 0x50f08000
88 
95 };
96 
97 /* For the built-in SONIC in the Duo Dock */
98 #define DUODOCK_SONIC_REGISTERS 0xe10000
99 #define DUODOCK_SONIC_PROM_BASE 0xe12000
100 
101 /* For Apple-style NuBus SONIC */
102 #define APPLE_SONIC_REGISTERS 0
103 #define APPLE_SONIC_PROM_BASE 0x40000
104 
105 /* Daynalink LC SONIC */
106 #define DAYNALINK_PROM_BASE 0x400000
107 
108 /* For Dayna-style NuBus SONIC (haven't seen one yet) */
109 #define DAYNA_SONIC_REGISTERS 0x180000
110 /* This is what OpenBSD says. However, this is definitely in NuBus
111  ROM space so we should be able to get it by walking the NuBus
112  resource directories */
113 #define DAYNA_SONIC_MAC_ADDR 0xffe004
114 
115 #define SONIC_READ_PROM(addr) nubus_readb(prom_addr+addr)
116 
117 /*
118  * For reversing the PROM address
119  */
120 
121 static inline void bit_reverse_addr(unsigned char addr[6])
122 {
123  int i;
124 
125  for(i = 0; i < 6; i++)
126  addr[i] = bitrev8(addr[i]);
127 }
128 
129 static irqreturn_t macsonic_interrupt(int irq, void *dev_id)
130 {
132  unsigned long flags;
133 
134  local_irq_save(flags);
135  result = sonic_interrupt(irq, dev_id);
136  local_irq_restore(flags);
137  return result;
138 }
139 
140 static int macsonic_open(struct net_device* dev)
141 {
142  int retval;
143 
144  retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
145  if (retval) {
146  printk(KERN_ERR "%s: unable to get IRQ %d.\n",
147  dev->name, dev->irq);
148  goto err;
149  }
150  /* Under the A/UX interrupt scheme, the onboard SONIC interrupt comes
151  * in at priority level 3. However, we sometimes get the level 2 inter-
152  * rupt as well, which must prevent re-entrance of the sonic handler.
153  */
154  if (dev->irq == IRQ_AUTO_3) {
155  retval = request_irq(IRQ_NUBUS_9, macsonic_interrupt, 0,
156  "sonic", dev);
157  if (retval) {
158  printk(KERN_ERR "%s: unable to get IRQ %d.\n",
159  dev->name, IRQ_NUBUS_9);
160  goto err_irq;
161  }
162  }
163  retval = sonic_open(dev);
164  if (retval)
165  goto err_irq_nubus;
166  return 0;
167 
168 err_irq_nubus:
169  if (dev->irq == IRQ_AUTO_3)
170  free_irq(IRQ_NUBUS_9, dev);
171 err_irq:
172  free_irq(dev->irq, dev);
173 err:
174  return retval;
175 }
176 
177 static int macsonic_close(struct net_device* dev)
178 {
179  int err;
180  err = sonic_close(dev);
181  free_irq(dev->irq, dev);
182  if (dev->irq == IRQ_AUTO_3)
183  free_irq(IRQ_NUBUS_9, dev);
184  return err;
185 }
186 
187 static const struct net_device_ops macsonic_netdev_ops = {
188  .ndo_open = macsonic_open,
189  .ndo_stop = macsonic_close,
190  .ndo_start_xmit = sonic_send_packet,
191  .ndo_set_rx_mode = sonic_multicast_list,
192  .ndo_tx_timeout = sonic_tx_timeout,
193  .ndo_get_stats = sonic_get_stats,
194  .ndo_validate_addr = eth_validate_addr,
195  .ndo_change_mtu = eth_change_mtu,
196  .ndo_set_mac_address = eth_mac_addr,
197 };
198 
199 static int __devinit macsonic_init(struct net_device *dev)
200 {
201  struct sonic_local* lp = netdev_priv(dev);
202 
203  /* Allocate the entire chunk of memory for the descriptors.
204  Note that this cannot cross a 64K boundary. */
205  if ((lp->descriptors = dma_alloc_coherent(lp->device,
207  &lp->descriptors_laddr, GFP_KERNEL)) == NULL) {
208  printk(KERN_ERR "%s: couldn't alloc DMA memory for descriptors.\n",
209  dev_name(lp->device));
210  return -ENOMEM;
211  }
212 
213  /* Now set up the pointers to point to the appropriate places */
214  lp->cda = lp->descriptors;
215  lp->tda = lp->cda + (SIZEOF_SONIC_CDA
217  lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
219  lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
221 
222  lp->cda_laddr = lp->descriptors_laddr;
229 
230  dev->netdev_ops = &macsonic_netdev_ops;
231  dev->watchdog_timeo = TX_TIMEOUT;
232 
233  /*
234  * clear tally counter
235  */
236  SONIC_WRITE(SONIC_CRCT, 0xffff);
237  SONIC_WRITE(SONIC_FAET, 0xffff);
238  SONIC_WRITE(SONIC_MPT, 0xffff);
239 
240  return 0;
241 }
242 
243 #define INVALID_MAC(mac) (memcmp(mac, "\x08\x00\x07", 3) && \
244  memcmp(mac, "\x00\xA0\x40", 3) && \
245  memcmp(mac, "\x00\x80\x19", 3) && \
246  memcmp(mac, "\x00\x05\x02", 3))
247 
248 static void __devinit mac_onboard_sonic_ethernet_addr(struct net_device *dev)
249 {
250  struct sonic_local *lp = netdev_priv(dev);
251  const int prom_addr = ONBOARD_SONIC_PROM_BASE;
252  unsigned short val;
253 
254  /*
255  * On NuBus boards we can sometimes look in the ROM resources.
256  * No such luck for comm-slot/onboard.
257  * On the PowerBook 520, the PROM base address is a mystery.
258  */
259  if (hwreg_present((void *)prom_addr)) {
260  int i;
261 
262  for (i = 0; i < 6; i++)
263  dev->dev_addr[i] = SONIC_READ_PROM(i);
264  if (!INVALID_MAC(dev->dev_addr))
265  return;
266 
267  /*
268  * Most of the time, the address is bit-reversed. The NetBSD
269  * source has a rather long and detailed historical account of
270  * why this is so.
271  */
272  bit_reverse_addr(dev->dev_addr);
273  if (!INVALID_MAC(dev->dev_addr))
274  return;
275 
276  /*
277  * If we still have what seems to be a bogus address, we'll
278  * look in the CAM. The top entry should be ours.
279  */
280  printk(KERN_WARNING "macsonic: MAC address in PROM seems "
281  "to be invalid, trying CAM\n");
282  } else {
283  printk(KERN_WARNING "macsonic: cannot read MAC address from "
284  "PROM, trying CAM\n");
285  }
286 
287  /* This only works if MacOS has already initialized the card. */
288 
290  SONIC_WRITE(SONIC_CEP, 15);
291 
292  val = SONIC_READ(SONIC_CAP2);
293  dev->dev_addr[5] = val >> 8;
294  dev->dev_addr[4] = val & 0xff;
295  val = SONIC_READ(SONIC_CAP1);
296  dev->dev_addr[3] = val >> 8;
297  dev->dev_addr[2] = val & 0xff;
298  val = SONIC_READ(SONIC_CAP0);
299  dev->dev_addr[1] = val >> 8;
300  dev->dev_addr[0] = val & 0xff;
301 
302  if (!INVALID_MAC(dev->dev_addr))
303  return;
304 
305  /* Still nonsense ... messed up someplace! */
306 
307  printk(KERN_WARNING "macsonic: MAC address in CAM entry 15 "
308  "seems invalid, will use a random MAC\n");
309  eth_hw_addr_random(dev);
310 }
311 
312 static int __devinit mac_onboard_sonic_probe(struct net_device *dev)
313 {
314  struct sonic_local* lp = netdev_priv(dev);
315  int sr;
316  int commslot = 0;
317 
318  if (!MACH_IS_MAC)
319  return -ENODEV;
320 
321  printk(KERN_INFO "Checking for internal Macintosh ethernet (SONIC).. ");
322 
323  /* Bogus probing, on the models which may or may not have
324  Ethernet (BTW, the Ethernet *is* always at the same
325  address, and nothing else lives there, at least if Apple's
326  documentation is to be believed) */
327  if (macintosh_config->ident == MAC_MODEL_Q630 ||
328  macintosh_config->ident == MAC_MODEL_P588 ||
329  macintosh_config->ident == MAC_MODEL_P575 ||
330  macintosh_config->ident == MAC_MODEL_C610) {
331  unsigned long flags;
332  int card_present;
333 
334  local_irq_save(flags);
335  card_present = hwreg_present((void*)ONBOARD_SONIC_REGISTERS);
336  local_irq_restore(flags);
337 
338  if (!card_present) {
339  printk("none.\n");
340  return -ENODEV;
341  }
342  commslot = 1;
343  }
344 
345  printk("yes\n");
346 
347  /* Danger! My arms are flailing wildly! You *must* set lp->reg_offset
348  * and dev->base_addr before using SONIC_READ() or SONIC_WRITE() */
350  if (via_alt_mapping)
351  dev->irq = IRQ_AUTO_3;
352  else
353  dev->irq = IRQ_NUBUS_9;
354 
355  if (!sonic_version_printed) {
356  printk(KERN_INFO "%s", version);
357  sonic_version_printed = 1;
358  }
359  printk(KERN_INFO "%s: onboard / comm-slot SONIC at 0x%08lx\n",
360  dev_name(lp->device), dev->base_addr);
361 
362  /* The PowerBook's SONIC is 16 bit always. */
363  if (macintosh_config->ident == MAC_MODEL_PB520) {
364  lp->reg_offset = 0;
366  sr = SONIC_READ(SONIC_SR);
367  } else if (commslot) {
368  /* Some of the comm-slot cards are 16 bit. But some
369  of them are not. The 32-bit cards use offset 2 and
370  have known revisions, we try reading the revision
371  register at offset 2, if we don't get a known revision
372  we assume 16 bit at offset 0. */
373  lp->reg_offset = 2;
375 
376  sr = SONIC_READ(SONIC_SR);
377  if (sr == 0x0004 || sr == 0x0006 || sr == 0x0100 || sr == 0x0101)
378  /* 83932 is 0x0004 or 0x0006, 83934 is 0x0100 or 0x0101 */
380  else {
382  lp->reg_offset = 0;
383  sr = SONIC_READ(SONIC_SR);
384  }
385  } else {
386  /* All onboard cards are at offset 2 with 32 bit DMA. */
387  lp->reg_offset = 2;
389  sr = SONIC_READ(SONIC_SR);
390  }
392  "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
393  dev_name(lp->device), sr, lp->dma_bitmode?32:16, lp->reg_offset);
394 
395 #if 0 /* This is sometimes useful to find out how MacOS configured the card. */
396  printk(KERN_INFO "%s: DCR: 0x%04x, DCR2: 0x%04x\n", dev_name(lp->device),
397  SONIC_READ(SONIC_DCR) & 0xffff, SONIC_READ(SONIC_DCR2) & 0xffff);
398 #endif
399 
400  /* Software reset, then initialize control registers. */
402 
405  (lp->dma_bitmode ? SONIC_DCR_DW : 0));
406 
407  /* This *must* be written back to in order to restore the
408  * extended programmable output bits, as it may not have been
409  * initialised since the hardware reset. */
411 
412  /* Clear *and* disable interrupts to be on the safe side */
414  SONIC_WRITE(SONIC_ISR, 0x7fff);
415 
416  /* Now look for the MAC address. */
417  mac_onboard_sonic_ethernet_addr(dev);
418 
419  /* Shared init code */
420  return macsonic_init(dev);
421 }
422 
423 static int __devinit mac_nubus_sonic_ethernet_addr(struct net_device *dev,
424  unsigned long prom_addr,
425  int id)
426 {
427  int i;
428  for(i = 0; i < 6; i++)
429  dev->dev_addr[i] = SONIC_READ_PROM(i);
430 
431  /* Some of the addresses are bit-reversed */
432  if (id != MACSONIC_DAYNA)
433  bit_reverse_addr(dev->dev_addr);
434 
435  return 0;
436 }
437 
438 static int __devinit macsonic_ident(struct nubus_dev *ndev)
439 {
440  if (ndev->dr_hw == NUBUS_DRHW_ASANTE_LC &&
441  ndev->dr_sw == NUBUS_DRSW_SONIC_LC)
442  return MACSONIC_DAYNALINK;
443  if (ndev->dr_hw == NUBUS_DRHW_SONIC &&
444  ndev->dr_sw == NUBUS_DRSW_APPLE) {
445  /* There has to be a better way to do this... */
446  if (strstr(ndev->board->name, "DuoDock"))
447  return MACSONIC_DUODOCK;
448  else
449  return MACSONIC_APPLE;
450  }
451 
452  if (ndev->dr_hw == NUBUS_DRHW_SMC9194 &&
453  ndev->dr_sw == NUBUS_DRSW_DAYNA)
454  return MACSONIC_DAYNA;
455 
456  if (ndev->dr_hw == NUBUS_DRHW_APPLE_SONIC_LC &&
457  ndev->dr_sw == 0) { /* huh? */
458  return MACSONIC_APPLE16;
459  }
460  return -1;
461 }
462 
463 static int __devinit mac_nubus_sonic_probe(struct net_device *dev)
464 {
465  static int slots;
466  struct nubus_dev* ndev = NULL;
467  struct sonic_local* lp = netdev_priv(dev);
468  unsigned long base_addr, prom_addr;
469  u16 sonic_dcr;
470  int id = -1;
471  int reg_offset, dma_bitmode;
472 
473  /* Find the first SONIC that hasn't been initialized already */
474  while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK,
475  NUBUS_TYPE_ETHERNET, ndev)) != NULL)
476  {
477  /* Have we seen it already? */
478  if (slots & (1<<ndev->board->slot))
479  continue;
480  slots |= 1<<ndev->board->slot;
481 
482  /* Is it one of ours? */
483  if ((id = macsonic_ident(ndev)) != -1)
484  break;
485  }
486 
487  if (ndev == NULL)
488  return -ENODEV;
489 
490  switch (id) {
491  case MACSONIC_DUODOCK:
492  base_addr = ndev->board->slot_addr + DUODOCK_SONIC_REGISTERS;
493  prom_addr = ndev->board->slot_addr + DUODOCK_SONIC_PROM_BASE;
496  reg_offset = 2;
497  dma_bitmode = SONIC_BITMODE32;
498  break;
499  case MACSONIC_APPLE:
500  base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
501  prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
503  reg_offset = 0;
504  dma_bitmode = SONIC_BITMODE32;
505  break;
506  case MACSONIC_APPLE16:
507  base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
508  prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
511  reg_offset = 0;
512  dma_bitmode = SONIC_BITMODE16;
513  break;
514  case MACSONIC_DAYNALINK:
515  base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
516  prom_addr = ndev->board->slot_addr + DAYNALINK_PROM_BASE;
517  sonic_dcr = SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
519  reg_offset = 0;
520  dma_bitmode = SONIC_BITMODE16;
521  break;
522  case MACSONIC_DAYNA:
523  base_addr = ndev->board->slot_addr + DAYNA_SONIC_REGISTERS;
524  prom_addr = ndev->board->slot_addr + DAYNA_SONIC_MAC_ADDR;
525  sonic_dcr = SONIC_DCR_BMS |
527  reg_offset = 0;
528  dma_bitmode = SONIC_BITMODE16;
529  break;
530  default:
531  printk(KERN_ERR "macsonic: WTF, id is %d\n", id);
532  return -ENODEV;
533  }
534 
535  /* Danger! My arms are flailing wildly! You *must* set lp->reg_offset
536  * and dev->base_addr before using SONIC_READ() or SONIC_WRITE() */
537  dev->base_addr = base_addr;
538  lp->reg_offset = reg_offset;
539  lp->dma_bitmode = dma_bitmode;
540  dev->irq = SLOT2IRQ(ndev->board->slot);
541 
542  if (!sonic_version_printed) {
543  printk(KERN_INFO "%s", version);
544  sonic_version_printed = 1;
545  }
546  printk(KERN_INFO "%s: %s in slot %X\n",
547  dev_name(lp->device), ndev->board->name, ndev->board->slot);
548  printk(KERN_INFO "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
549  dev_name(lp->device), SONIC_READ(SONIC_SR), dma_bitmode?32:16, reg_offset);
550 
551 #if 0 /* This is sometimes useful to find out how MacOS configured the card. */
552  printk(KERN_INFO "%s: DCR: 0x%04x, DCR2: 0x%04x\n", dev_name(lp->device),
553  SONIC_READ(SONIC_DCR) & 0xffff, SONIC_READ(SONIC_DCR2) & 0xffff);
554 #endif
555 
556  /* Software reset, then initialize control registers. */
558  SONIC_WRITE(SONIC_DCR, sonic_dcr | (dma_bitmode ? SONIC_DCR_DW : 0));
559  /* This *must* be written back to in order to restore the
560  * extended programmable output bits, since it may not have been
561  * initialised since the hardware reset. */
563 
564  /* Clear *and* disable interrupts to be on the safe side */
566  SONIC_WRITE(SONIC_ISR, 0x7fff);
567 
568  /* Now look for the MAC address. */
569  if (mac_nubus_sonic_ethernet_addr(dev, prom_addr, id) != 0)
570  return -ENODEV;
571 
572  /* Shared init code */
573  return macsonic_init(dev);
574 }
575 
576 static int __devinit mac_sonic_probe(struct platform_device *pdev)
577 {
578  struct net_device *dev;
579  struct sonic_local *lp;
580  int err;
581 
582  dev = alloc_etherdev(sizeof(struct sonic_local));
583  if (!dev)
584  return -ENOMEM;
585 
586  lp = netdev_priv(dev);
587  lp->device = &pdev->dev;
588  SET_NETDEV_DEV(dev, &pdev->dev);
589  platform_set_drvdata(pdev, dev);
590 
591  /* This will catch fatal stuff like -ENOMEM as well as success */
592  err = mac_onboard_sonic_probe(dev);
593  if (err == 0)
594  goto found;
595  if (err != -ENODEV)
596  goto out;
597  err = mac_nubus_sonic_probe(dev);
598  if (err)
599  goto out;
600 found:
601  err = register_netdev(dev);
602  if (err)
603  goto out;
604 
605  printk("%s: MAC %pM IRQ %d\n", dev->name, dev->dev_addr, dev->irq);
606 
607  return 0;
608 
609 out:
610  free_netdev(dev);
611 
612  return err;
613 }
614 
615 MODULE_DESCRIPTION("Macintosh SONIC ethernet driver");
616 module_param(sonic_debug, int, 0);
617 MODULE_PARM_DESC(sonic_debug, "macsonic debug level (1-4)");
618 MODULE_ALIAS("platform:macsonic");
619 
620 #include "sonic.c"
621 
622 static int __devexit mac_sonic_device_remove (struct platform_device *pdev)
623 {
624  struct net_device *dev = platform_get_drvdata(pdev);
625  struct sonic_local* lp = netdev_priv(dev);
626 
627  unregister_netdev(dev);
629  lp->descriptors, lp->descriptors_laddr);
630  free_netdev(dev);
631 
632  return 0;
633 }
634 
635 static struct platform_driver mac_sonic_driver = {
636  .probe = mac_sonic_probe,
637  .remove = __devexit_p(mac_sonic_device_remove),
638  .driver = {
639  .name = mac_sonic_string,
640  .owner = THIS_MODULE,
641  },
642 };
643 
644 module_platform_driver(mac_sonic_driver);