blob: 5987d172d3d6cabb3624fe8b2b500b0ce7b140e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * macsonic.c
3 *
Finn Thainefcce832005-08-20 15:53:22 +10004 * (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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * (C) 1998 Alan Cox
11 *
12 * Debugging Andreas Ehliar, Michael Schmitz
13 *
14 * Based on code
15 * (C) 1996 by Thomas Bogendoerfer (tsbogend@bigbug.franken.de)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * This driver is based on work from Andreas Busse, but most of
18 * the code is rewritten.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
21 *
22 * A driver for the Mac onboard Sonic ethernet chip.
23 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -040024 * 98/12/21 MSch: judged from tests on Q800, it's basically working,
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * 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.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040029 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * 00/10/31 sammy@oh.verio.com: Updated driver for 2.4 kernels, fixed problems
31 * on centris.
32 */
33
34#include <linux/kernel.h>
Finn Thainefcce832005-08-20 15:53:22 +100035#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/fcntl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/interrupt.h>
40#include <linux/init.h>
41#include <linux/ioport.h>
42#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#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>
Russell Kingd052d1b2005-10-29 19:07:23 +010050#include <linux/platform_device.h>
Finn Thainefcce832005-08-20 15:53:22 +100051#include <linux/dma-mapping.h>
Al Viro427a57a2007-02-09 16:05:22 +000052#include <linux/bitrev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <asm/bootinfo.h>
56#include <asm/system.h>
57#include <asm/pgtable.h>
58#include <asm/io.h>
59#include <asm/hwtest.h>
60#include <asm/dma.h>
61#include <asm/macintosh.h>
62#include <asm/macints.h>
63#include <asm/mac_via.h>
64
Finn Thainefcce832005-08-20 15:53:22 +100065static char mac_sonic_string[] = "macsonic";
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#include "sonic.h"
68
Finn Thainefcce832005-08-20 15:53:22 +100069/* These should basically be bus-size and endian independent (since
70 the SONIC is at least smart enough that it uses the same endianness
71 as the host, unlike certain less enlightened Macintosh NICs) */
72#define SONIC_READ(reg) (nubus_readw(dev->base_addr + (reg * 4) \
73 + lp->reg_offset))
74#define SONIC_WRITE(reg,val) (nubus_writew(val, dev->base_addr + (reg * 4) \
75 + lp->reg_offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Finn Thainefcce832005-08-20 15:53:22 +100077/* use 0 for production, 1 for verification, >1 for debug */
78#ifdef SONIC_DEBUG
79static unsigned int sonic_debug = SONIC_DEBUG;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040080#else
Finn Thainefcce832005-08-20 15:53:22 +100081static unsigned int sonic_debug = 1;
82#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static int sonic_version_printed;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* For onboard SONIC */
87#define ONBOARD_SONIC_REGISTERS 0x50F0A000
88#define ONBOARD_SONIC_PROM_BASE 0x50f08000
89
90enum macsonic_type {
91 MACSONIC_DUODOCK,
92 MACSONIC_APPLE,
93 MACSONIC_APPLE16,
94 MACSONIC_DAYNA,
95 MACSONIC_DAYNALINK
96};
97
98/* For the built-in SONIC in the Duo Dock */
99#define DUODOCK_SONIC_REGISTERS 0xe10000
100#define DUODOCK_SONIC_PROM_BASE 0xe12000
101
102/* For Apple-style NuBus SONIC */
103#define APPLE_SONIC_REGISTERS 0
104#define APPLE_SONIC_PROM_BASE 0x40000
105
106/* Daynalink LC SONIC */
107#define DAYNALINK_PROM_BASE 0x400000
108
109/* For Dayna-style NuBus SONIC (haven't seen one yet) */
110#define DAYNA_SONIC_REGISTERS 0x180000
111/* This is what OpenBSD says. However, this is definitely in NuBus
112 ROM space so we should be able to get it by walking the NuBus
113 resource directories */
114#define DAYNA_SONIC_MAC_ADDR 0xffe004
115
116#define SONIC_READ_PROM(addr) nubus_readb(prom_addr+addr)
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
119 * For reversing the PROM address
120 */
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static inline void bit_reverse_addr(unsigned char addr[6])
123{
124 int i;
125
126 for(i = 0; i < 6; i++)
Akinobu Mitabc63eb92006-12-19 13:09:08 -0800127 addr[i] = bitrev8(addr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Finn Thainf4d86752007-05-02 12:55:56 +1000130static irqreturn_t macsonic_interrupt(int irq, void *dev_id)
131{
132 irqreturn_t result;
133 unsigned long flags;
134
135 local_irq_save(flags);
136 result = sonic_interrupt(irq, dev_id);
137 local_irq_restore(flags);
138 return result;
139}
140
141static int macsonic_open(struct net_device* dev)
142{
Kulikov Vasiliy546e3ab2010-07-10 01:39:06 +0000143 int retval;
144
Geert Uytterhoevene71ef312011-07-13 22:33:13 +0200145 retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
Kulikov Vasiliy546e3ab2010-07-10 01:39:06 +0000146 if (retval) {
147 printk(KERN_ERR "%s: unable to get IRQ %d.\n",
148 dev->name, dev->irq);
149 goto err;
Finn Thainf4d86752007-05-02 12:55:56 +1000150 }
151 /* Under the A/UX interrupt scheme, the onboard SONIC interrupt comes
152 * in at priority level 3. However, we sometimes get the level 2 inter-
153 * rupt as well, which must prevent re-entrance of the sonic handler.
154 */
Kulikov Vasiliy546e3ab2010-07-10 01:39:06 +0000155 if (dev->irq == IRQ_AUTO_3) {
Geert Uytterhoevene71ef312011-07-13 22:33:13 +0200156 retval = request_irq(IRQ_NUBUS_9, macsonic_interrupt, 0,
157 "sonic", dev);
Kulikov Vasiliy546e3ab2010-07-10 01:39:06 +0000158 if (retval) {
159 printk(KERN_ERR "%s: unable to get IRQ %d.\n",
160 dev->name, IRQ_NUBUS_9);
161 goto err_irq;
Finn Thainf4d86752007-05-02 12:55:56 +1000162 }
Kulikov Vasiliy546e3ab2010-07-10 01:39:06 +0000163 }
164 retval = sonic_open(dev);
165 if (retval)
166 goto err_irq_nubus;
167 return 0;
168
169err_irq_nubus:
170 if (dev->irq == IRQ_AUTO_3)
171 free_irq(IRQ_NUBUS_9, dev);
172err_irq:
173 free_irq(dev->irq, dev);
174err:
175 return retval;
Finn Thainf4d86752007-05-02 12:55:56 +1000176}
177
178static int macsonic_close(struct net_device* dev)
179{
180 int err;
181 err = sonic_close(dev);
182 free_irq(dev->irq, dev);
183 if (dev->irq == IRQ_AUTO_3)
184 free_irq(IRQ_NUBUS_9, dev);
185 return err;
186}
187
Alexander Beregalovc6e6d852009-04-11 07:41:28 +0000188static const struct net_device_ops macsonic_netdev_ops = {
189 .ndo_open = macsonic_open,
190 .ndo_stop = macsonic_close,
191 .ndo_start_xmit = sonic_send_packet,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000192 .ndo_set_rx_mode = sonic_multicast_list,
Alexander Beregalovc6e6d852009-04-11 07:41:28 +0000193 .ndo_tx_timeout = sonic_tx_timeout,
194 .ndo_get_stats = sonic_get_stats,
195 .ndo_validate_addr = eth_validate_addr,
196 .ndo_change_mtu = eth_change_mtu,
197 .ndo_set_mac_address = eth_mac_addr,
198};
199
Uwe Kleine-König251774762009-07-11 20:52:48 +0000200static int __devinit macsonic_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Finn Thainefcce832005-08-20 15:53:22 +1000202 struct sonic_local* lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* Allocate the entire chunk of memory for the descriptors.
205 Note that this cannot cross a 64K boundary. */
Finn Thainefcce832005-08-20 15:53:22 +1000206 if ((lp->descriptors = dma_alloc_coherent(lp->device,
207 SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
208 &lp->descriptors_laddr, GFP_KERNEL)) == NULL) {
Kay Sieversc2313552009-03-24 16:38:22 -0700209 printk(KERN_ERR "%s: couldn't alloc DMA memory for descriptors.\n",
210 dev_name(lp->device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -ENOMEM;
Finn Thainefcce832005-08-20 15:53:22 +1000212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* Now set up the pointers to point to the appropriate places */
Finn Thainefcce832005-08-20 15:53:22 +1000215 lp->cda = lp->descriptors;
216 lp->tda = lp->cda + (SIZEOF_SONIC_CDA
217 * SONIC_BUS_SCALE(lp->dma_bitmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
Finn Thainefcce832005-08-20 15:53:22 +1000219 * SONIC_BUS_SCALE(lp->dma_bitmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
Finn Thainefcce832005-08-20 15:53:22 +1000221 * SONIC_BUS_SCALE(lp->dma_bitmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Finn Thainefcce832005-08-20 15:53:22 +1000223 lp->cda_laddr = lp->descriptors_laddr;
224 lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
225 * SONIC_BUS_SCALE(lp->dma_bitmode));
226 lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
227 * SONIC_BUS_SCALE(lp->dma_bitmode));
228 lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
229 * SONIC_BUS_SCALE(lp->dma_bitmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Alexander Beregalovc6e6d852009-04-11 07:41:28 +0000231 dev->netdev_ops = &macsonic_netdev_ops;
Finn Thainefcce832005-08-20 15:53:22 +1000232 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /*
235 * clear tally counter
236 */
Finn Thainefcce832005-08-20 15:53:22 +1000237 SONIC_WRITE(SONIC_CRCT, 0xffff);
238 SONIC_WRITE(SONIC_FAET, 0xffff);
239 SONIC_WRITE(SONIC_MPT, 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 return 0;
242}
243
Finn Thaindcaa6a92009-11-03 03:42:40 +0000244#define INVALID_MAC(mac) (memcmp(mac, "\x08\x00\x07", 3) && \
245 memcmp(mac, "\x00\xA0\x40", 3) && \
246 memcmp(mac, "\x00\x80\x19", 3) && \
247 memcmp(mac, "\x00\x05\x02", 3))
248
249static void __devinit mac_onboard_sonic_ethernet_addr(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Finn Thainefcce832005-08-20 15:53:22 +1000251 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 const int prom_addr = ONBOARD_SONIC_PROM_BASE;
Finn Thaindcaa6a92009-11-03 03:42:40 +0000253 unsigned short val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Finn Thaindcaa6a92009-11-03 03:42:40 +0000255 /*
256 * On NuBus boards we can sometimes look in the ROM resources.
257 * No such luck for comm-slot/onboard.
258 * On the PowerBook 520, the PROM base address is a mystery.
259 */
260 if (hwreg_present((void *)prom_addr)) {
261 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Finn Thaindcaa6a92009-11-03 03:42:40 +0000263 for (i = 0; i < 6; i++)
264 dev->dev_addr[i] = SONIC_READ_PROM(i);
265 if (!INVALID_MAC(dev->dev_addr))
266 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /*
Finn Thaindcaa6a92009-11-03 03:42:40 +0000269 * Most of the time, the address is bit-reversed. The NetBSD
270 * source has a rather long and detailed historical account of
271 * why this is so.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Finn Thaindcaa6a92009-11-03 03:42:40 +0000273 bit_reverse_addr(dev->dev_addr);
274 if (!INVALID_MAC(dev->dev_addr))
275 return;
276
277 /*
278 * If we still have what seems to be a bogus address, we'll
279 * look in the CAM. The top entry should be ours.
280 */
281 printk(KERN_WARNING "macsonic: MAC address in PROM seems "
282 "to be invalid, trying CAM\n");
283 } else {
284 printk(KERN_WARNING "macsonic: cannot read MAC address from "
285 "PROM, trying CAM\n");
286 }
287
288 /* This only works if MacOS has already initialized the card. */
289
290 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
291 SONIC_WRITE(SONIC_CEP, 15);
292
293 val = SONIC_READ(SONIC_CAP2);
294 dev->dev_addr[5] = val >> 8;
295 dev->dev_addr[4] = val & 0xff;
296 val = SONIC_READ(SONIC_CAP1);
297 dev->dev_addr[3] = val >> 8;
298 dev->dev_addr[2] = val & 0xff;
299 val = SONIC_READ(SONIC_CAP0);
300 dev->dev_addr[1] = val >> 8;
301 dev->dev_addr[0] = val & 0xff;
302
303 if (!INVALID_MAC(dev->dev_addr))
304 return;
305
306 /* Still nonsense ... messed up someplace! */
307
308 printk(KERN_WARNING "macsonic: MAC address in CAM entry 15 "
309 "seems invalid, will use a random MAC\n");
310 random_ether_addr(dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Uwe Kleine-König251774762009-07-11 20:52:48 +0000313static int __devinit mac_onboard_sonic_probe(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Finn Thainefcce832005-08-20 15:53:22 +1000315 struct sonic_local* lp = netdev_priv(dev);
316 int sr;
317 int commslot = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (!MACH_IS_MAC)
320 return -ENODEV;
321
Finn Thainefcce832005-08-20 15:53:22 +1000322 printk(KERN_INFO "Checking for internal Macintosh ethernet (SONIC).. ");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* Bogus probing, on the models which may or may not have
325 Ethernet (BTW, the Ethernet *is* always at the same
326 address, and nothing else lives there, at least if Apple's
327 documentation is to be believed) */
328 if (macintosh_config->ident == MAC_MODEL_Q630 ||
329 macintosh_config->ident == MAC_MODEL_P588 ||
Finn Thainefcce832005-08-20 15:53:22 +1000330 macintosh_config->ident == MAC_MODEL_P575 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 macintosh_config->ident == MAC_MODEL_C610) {
332 unsigned long flags;
333 int card_present;
334
335 local_irq_save(flags);
336 card_present = hwreg_present((void*)ONBOARD_SONIC_REGISTERS);
337 local_irq_restore(flags);
338
339 if (!card_present) {
340 printk("none.\n");
341 return -ENODEV;
342 }
Finn Thainefcce832005-08-20 15:53:22 +1000343 commslot = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400346 printk("yes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Finn Thainefcce832005-08-20 15:53:22 +1000348 /* Danger! My arms are flailing wildly! You *must* set lp->reg_offset
349 * and dev->base_addr before using SONIC_READ() or SONIC_WRITE() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 dev->base_addr = ONBOARD_SONIC_REGISTERS;
351 if (via_alt_mapping)
352 dev->irq = IRQ_AUTO_3;
353 else
354 dev->irq = IRQ_NUBUS_9;
355
356 if (!sonic_version_printed) {
357 printk(KERN_INFO "%s", version);
358 sonic_version_printed = 1;
359 }
360 printk(KERN_INFO "%s: onboard / comm-slot SONIC at 0x%08lx\n",
Kay Sieversc2313552009-03-24 16:38:22 -0700361 dev_name(lp->device), dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* The PowerBook's SONIC is 16 bit always. */
364 if (macintosh_config->ident == MAC_MODEL_PB520) {
Finn Thainefcce832005-08-20 15:53:22 +1000365 lp->reg_offset = 0;
366 lp->dma_bitmode = SONIC_BITMODE16;
367 sr = SONIC_READ(SONIC_SR);
368 } else if (commslot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* Some of the comm-slot cards are 16 bit. But some
Finn Thainefcce832005-08-20 15:53:22 +1000370 of them are not. The 32-bit cards use offset 2 and
371 have known revisions, we try reading the revision
372 register at offset 2, if we don't get a known revision
373 we assume 16 bit at offset 0. */
374 lp->reg_offset = 2;
375 lp->dma_bitmode = SONIC_BITMODE16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Finn Thainefcce832005-08-20 15:53:22 +1000377 sr = SONIC_READ(SONIC_SR);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400378 if (sr == 0x0004 || sr == 0x0006 || sr == 0x0100 || sr == 0x0101)
Finn Thainefcce832005-08-20 15:53:22 +1000379 /* 83932 is 0x0004 or 0x0006, 83934 is 0x0100 or 0x0101 */
380 lp->dma_bitmode = SONIC_BITMODE32;
381 else {
382 lp->dma_bitmode = SONIC_BITMODE16;
383 lp->reg_offset = 0;
384 sr = SONIC_READ(SONIC_SR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Finn Thainefcce832005-08-20 15:53:22 +1000386 } else {
387 /* All onboard cards are at offset 2 with 32 bit DMA. */
388 lp->reg_offset = 2;
389 lp->dma_bitmode = SONIC_BITMODE32;
390 sr = SONIC_READ(SONIC_SR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Finn Thainefcce832005-08-20 15:53:22 +1000392 printk(KERN_INFO
393 "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
Kay Sieversc2313552009-03-24 16:38:22 -0700394 dev_name(lp->device), sr, lp->dma_bitmode?32:16, lp->reg_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Finn Thainefcce832005-08-20 15:53:22 +1000396#if 0 /* This is sometimes useful to find out how MacOS configured the card. */
Kay Sieversc2313552009-03-24 16:38:22 -0700397 printk(KERN_INFO "%s: DCR: 0x%04x, DCR2: 0x%04x\n", dev_name(lp->device),
Finn Thainefcce832005-08-20 15:53:22 +1000398 SONIC_READ(SONIC_DCR) & 0xffff, SONIC_READ(SONIC_DCR2) & 0xffff);
399#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* Software reset, then initialize control registers. */
Finn Thainefcce832005-08-20 15:53:22 +1000402 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
403
404 SONIC_WRITE(SONIC_DCR, SONIC_DCR_EXBUS | SONIC_DCR_BMS |
405 SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
406 (lp->dma_bitmode ? SONIC_DCR_DW : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* This *must* be written back to in order to restore the
Finn Thainefcce832005-08-20 15:53:22 +1000409 * extended programmable output bits, as it may not have been
410 * initialised since the hardware reset. */
411 SONIC_WRITE(SONIC_DCR2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /* Clear *and* disable interrupts to be on the safe side */
Finn Thainefcce832005-08-20 15:53:22 +1000414 SONIC_WRITE(SONIC_IMR, 0);
415 SONIC_WRITE(SONIC_ISR, 0x7fff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /* Now look for the MAC address. */
Finn Thaindcaa6a92009-11-03 03:42:40 +0000418 mac_onboard_sonic_ethernet_addr(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* Shared init code */
421 return macsonic_init(dev);
422}
423
Uwe Kleine-König251774762009-07-11 20:52:48 +0000424static int __devinit mac_nubus_sonic_ethernet_addr(struct net_device *dev,
Adrian Bunk44f74c02008-06-10 01:23:47 +0300425 unsigned long prom_addr,
426 int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 int i;
429 for(i = 0; i < 6; i++)
430 dev->dev_addr[i] = SONIC_READ_PROM(i);
Finn Thainefcce832005-08-20 15:53:22 +1000431
432 /* Some of the addresses are bit-reversed */
433 if (id != MACSONIC_DAYNA)
434 bit_reverse_addr(dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 return 0;
437}
438
Uwe Kleine-König251774762009-07-11 20:52:48 +0000439static int __devinit macsonic_ident(struct nubus_dev *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400441 if (ndev->dr_hw == NUBUS_DRHW_ASANTE_LC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 ndev->dr_sw == NUBUS_DRSW_SONIC_LC)
443 return MACSONIC_DAYNALINK;
444 if (ndev->dr_hw == NUBUS_DRHW_SONIC &&
445 ndev->dr_sw == NUBUS_DRSW_APPLE) {
446 /* There has to be a better way to do this... */
447 if (strstr(ndev->board->name, "DuoDock"))
448 return MACSONIC_DUODOCK;
449 else
450 return MACSONIC_APPLE;
451 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400452
Finn Thainefcce832005-08-20 15:53:22 +1000453 if (ndev->dr_hw == NUBUS_DRHW_SMC9194 &&
454 ndev->dr_sw == NUBUS_DRSW_DAYNA)
455 return MACSONIC_DAYNA;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400456
Finn Thainf8779582007-05-01 22:32:53 +0200457 if (ndev->dr_hw == NUBUS_DRHW_APPLE_SONIC_LC &&
Finn Thainefcce832005-08-20 15:53:22 +1000458 ndev->dr_sw == 0) { /* huh? */
459 return MACSONIC_APPLE16;
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return -1;
462}
463
Uwe Kleine-König251774762009-07-11 20:52:48 +0000464static int __devinit mac_nubus_sonic_probe(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 static int slots;
467 struct nubus_dev* ndev = NULL;
Finn Thainefcce832005-08-20 15:53:22 +1000468 struct sonic_local* lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 unsigned long base_addr, prom_addr;
470 u16 sonic_dcr;
Finn Thainefcce832005-08-20 15:53:22 +1000471 int id = -1;
472 int reg_offset, dma_bitmode;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* Find the first SONIC that hasn't been initialized already */
475 while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK,
476 NUBUS_TYPE_ETHERNET, ndev)) != NULL)
477 {
478 /* Have we seen it already? */
479 if (slots & (1<<ndev->board->slot))
480 continue;
481 slots |= 1<<ndev->board->slot;
482
483 /* Is it one of ours? */
484 if ((id = macsonic_ident(ndev)) != -1)
485 break;
486 }
487
488 if (ndev == NULL)
489 return -ENODEV;
490
491 switch (id) {
492 case MACSONIC_DUODOCK:
493 base_addr = ndev->board->slot_addr + DUODOCK_SONIC_REGISTERS;
494 prom_addr = ndev->board->slot_addr + DUODOCK_SONIC_PROM_BASE;
Finn Thainefcce832005-08-20 15:53:22 +1000495 sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT0 | SONIC_DCR_RFT1 |
496 SONIC_DCR_TFT0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 reg_offset = 2;
Finn Thainefcce832005-08-20 15:53:22 +1000498 dma_bitmode = SONIC_BITMODE32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 break;
500 case MACSONIC_APPLE:
501 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
502 prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
503 sonic_dcr = SONIC_DCR_BMS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0;
504 reg_offset = 0;
Finn Thainefcce832005-08-20 15:53:22 +1000505 dma_bitmode = SONIC_BITMODE32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
507 case MACSONIC_APPLE16:
508 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
509 prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
Finn Thainefcce832005-08-20 15:53:22 +1000510 sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400511 SONIC_DCR_PO1 | SONIC_DCR_BMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 reg_offset = 0;
Finn Thainefcce832005-08-20 15:53:22 +1000513 dma_bitmode = SONIC_BITMODE16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
515 case MACSONIC_DAYNALINK:
516 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
517 prom_addr = ndev->board->slot_addr + DAYNALINK_PROM_BASE;
Finn Thainefcce832005-08-20 15:53:22 +1000518 sonic_dcr = SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400519 SONIC_DCR_PO1 | SONIC_DCR_BMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 reg_offset = 0;
Finn Thainefcce832005-08-20 15:53:22 +1000521 dma_bitmode = SONIC_BITMODE16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523 case MACSONIC_DAYNA:
524 base_addr = ndev->board->slot_addr + DAYNA_SONIC_REGISTERS;
525 prom_addr = ndev->board->slot_addr + DAYNA_SONIC_MAC_ADDR;
Finn Thainefcce832005-08-20 15:53:22 +1000526 sonic_dcr = SONIC_DCR_BMS |
527 SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_PO1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 reg_offset = 0;
Finn Thainefcce832005-08-20 15:53:22 +1000529 dma_bitmode = SONIC_BITMODE16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
531 default:
532 printk(KERN_ERR "macsonic: WTF, id is %d\n", id);
533 return -ENODEV;
534 }
535
Finn Thainefcce832005-08-20 15:53:22 +1000536 /* Danger! My arms are flailing wildly! You *must* set lp->reg_offset
537 * and dev->base_addr before using SONIC_READ() or SONIC_WRITE() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 dev->base_addr = base_addr;
Finn Thainefcce832005-08-20 15:53:22 +1000539 lp->reg_offset = reg_offset;
540 lp->dma_bitmode = dma_bitmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 dev->irq = SLOT2IRQ(ndev->board->slot);
542
543 if (!sonic_version_printed) {
544 printk(KERN_INFO "%s", version);
545 sonic_version_printed = 1;
546 }
547 printk(KERN_INFO "%s: %s in slot %X\n",
Kay Sieversc2313552009-03-24 16:38:22 -0700548 dev_name(lp->device), ndev->board->name, ndev->board->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 printk(KERN_INFO "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
Kay Sieversc2313552009-03-24 16:38:22 -0700550 dev_name(lp->device), SONIC_READ(SONIC_SR), dma_bitmode?32:16, reg_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Finn Thainefcce832005-08-20 15:53:22 +1000552#if 0 /* This is sometimes useful to find out how MacOS configured the card. */
Kay Sieversc2313552009-03-24 16:38:22 -0700553 printk(KERN_INFO "%s: DCR: 0x%04x, DCR2: 0x%04x\n", dev_name(lp->device),
Finn Thainefcce832005-08-20 15:53:22 +1000554 SONIC_READ(SONIC_DCR) & 0xffff, SONIC_READ(SONIC_DCR2) & 0xffff);
555#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* Software reset, then initialize control registers. */
Finn Thainefcce832005-08-20 15:53:22 +1000558 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
559 SONIC_WRITE(SONIC_DCR, sonic_dcr | (dma_bitmode ? SONIC_DCR_DW : 0));
560 /* This *must* be written back to in order to restore the
561 * extended programmable output bits, since it may not have been
562 * initialised since the hardware reset. */
563 SONIC_WRITE(SONIC_DCR2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /* Clear *and* disable interrupts to be on the safe side */
Finn Thainefcce832005-08-20 15:53:22 +1000566 SONIC_WRITE(SONIC_IMR, 0);
567 SONIC_WRITE(SONIC_ISR, 0x7fff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* Now look for the MAC address. */
570 if (mac_nubus_sonic_ethernet_addr(dev, prom_addr, id) != 0)
571 return -ENODEV;
572
Finn Thainefcce832005-08-20 15:53:22 +1000573 /* Shared init code */
574 return macsonic_init(dev);
575}
576
Uwe Kleine-König251774762009-07-11 20:52:48 +0000577static int __devinit mac_sonic_probe(struct platform_device *pdev)
Finn Thainefcce832005-08-20 15:53:22 +1000578{
579 struct net_device *dev;
580 struct sonic_local *lp;
581 int err;
Finn Thainefcce832005-08-20 15:53:22 +1000582
583 dev = alloc_etherdev(sizeof(struct sonic_local));
584 if (!dev)
585 return -ENOMEM;
586
587 lp = netdev_priv(dev);
Finn Thaind74472f2007-05-01 22:33:02 +0200588 lp->device = &pdev->dev;
589 SET_NETDEV_DEV(dev, &pdev->dev);
Finn Thain4564cba2009-07-21 12:21:49 -0700590 platform_set_drvdata(pdev, dev);
Finn Thainefcce832005-08-20 15:53:22 +1000591
592 /* This will catch fatal stuff like -ENOMEM as well as success */
593 err = mac_onboard_sonic_probe(dev);
594 if (err == 0)
595 goto found;
596 if (err != -ENODEV)
597 goto out;
598 err = mac_nubus_sonic_probe(dev);
599 if (err)
600 goto out;
601found:
602 err = register_netdev(dev);
603 if (err)
604 goto out;
605
Johannes Berge1749612008-10-27 15:59:26 -0700606 printk("%s: MAC %pM IRQ %d\n", dev->name, dev->dev_addr, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Finn Thainefcce832005-08-20 15:53:22 +1000608 return 0;
609
610out:
611 free_netdev(dev);
612
613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Finn Thainefcce832005-08-20 15:53:22 +1000616MODULE_DESCRIPTION("Macintosh SONIC ethernet driver");
617module_param(sonic_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618MODULE_PARM_DESC(sonic_debug, "macsonic debug level (1-4)");
Finn Thaineeb9c182009-11-04 00:42:02 +1100619MODULE_ALIAS("platform:macsonic");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621#include "sonic.c"
622
Finn Thaind74472f2007-05-01 22:33:02 +0200623static int __devexit mac_sonic_device_remove (struct platform_device *pdev)
Finn Thainefcce832005-08-20 15:53:22 +1000624{
Finn Thaind74472f2007-05-01 22:33:02 +0200625 struct net_device *dev = platform_get_drvdata(pdev);
Finn Thainefcce832005-08-20 15:53:22 +1000626 struct sonic_local* lp = netdev_priv(dev);
627
Finn Thaind74472f2007-05-01 22:33:02 +0200628 unregister_netdev(dev);
Finn Thainefcce832005-08-20 15:53:22 +1000629 dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
630 lp->descriptors, lp->descriptors_laddr);
Finn Thaind74472f2007-05-01 22:33:02 +0200631 free_netdev(dev);
Finn Thainefcce832005-08-20 15:53:22 +1000632
633 return 0;
634}
635
Russell King3ae5eae2005-11-09 22:32:44 +0000636static struct platform_driver mac_sonic_driver = {
Finn Thainefcce832005-08-20 15:53:22 +1000637 .probe = mac_sonic_probe,
638 .remove = __devexit_p(mac_sonic_device_remove),
Russell King3ae5eae2005-11-09 22:32:44 +0000639 .driver = {
Finn Thaineeb9c182009-11-04 00:42:02 +1100640 .name = mac_sonic_string,
641 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +0000642 },
Finn Thainefcce832005-08-20 15:53:22 +1000643};
644
Finn Thainefcce832005-08-20 15:53:22 +1000645static int __init mac_sonic_init_module(void)
646{
Finn Thaineeb9c182009-11-04 00:42:02 +1100647 return platform_driver_register(&mac_sonic_driver);
Finn Thainefcce832005-08-20 15:53:22 +1000648}
649
650static void __exit mac_sonic_cleanup_module(void)
651{
Russell King3ae5eae2005-11-09 22:32:44 +0000652 platform_driver_unregister(&mac_sonic_driver);
Finn Thainefcce832005-08-20 15:53:22 +1000653}
654
655module_init(mac_sonic_init_module);
656module_exit(mac_sonic_cleanup_module);