blob: 346a4e025c34100315b1ca2785d631d3bc5d3408 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#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
Finn Thainefcce832005-08-20 15:53:22 +100064static char mac_sonic_string[] = "macsonic";
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include "sonic.h"
67
Finn Thainefcce832005-08-20 15:53:22 +100068/* 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))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Finn Thainefcce832005-08-20 15:53:22 +100076/* use 0 for production, 1 for verification, >1 for debug */
77#ifdef SONIC_DEBUG
78static unsigned int sonic_debug = SONIC_DEBUG;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040079#else
Finn Thainefcce832005-08-20 15:53:22 +100080static unsigned int sonic_debug = 1;
81#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int sonic_version_printed;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* For onboard SONIC */
86#define ONBOARD_SONIC_REGISTERS 0x50F0A000
87#define ONBOARD_SONIC_PROM_BASE 0x50f08000
88
89enum macsonic_type {
90 MACSONIC_DUODOCK,
91 MACSONIC_APPLE,
92 MACSONIC_APPLE16,
93 MACSONIC_DAYNA,
94 MACSONIC_DAYNALINK
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/*
118 * For reversing the PROM address
119 */
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static inline void bit_reverse_addr(unsigned char addr[6])
122{
123 int i;
124
125 for(i = 0; i < 6; i++)
Akinobu Mitabc63eb92006-12-19 13:09:08 -0800126 addr[i] = bitrev8(addr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Finn Thainf4d86752007-05-02 12:55:56 +1000129static irqreturn_t macsonic_interrupt(int irq, void *dev_id)
130{
131 irqreturn_t result;
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
140static int macsonic_open(struct net_device* dev)
141{
Kulikov Vasiliy546e3ab2010-07-10 01:39:06 +0000142 int retval;
143
Geert Uytterhoevene71ef312011-07-13 22:33:13 +0200144 retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
Kulikov Vasiliy546e3ab2010-07-10 01:39:06 +0000145 if (retval) {
146 printk(KERN_ERR "%s: unable to get IRQ %d.\n",
147 dev->name, dev->irq);
148 goto err;
Finn Thainf4d86752007-05-02 12:55:56 +1000149 }
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 */
Kulikov Vasiliy546e3ab2010-07-10 01:39:06 +0000154 if (dev->irq == IRQ_AUTO_3) {
Geert Uytterhoevene71ef312011-07-13 22:33:13 +0200155 retval = request_irq(IRQ_NUBUS_9, macsonic_interrupt, 0,
156 "sonic", dev);
Kulikov Vasiliy546e3ab2010-07-10 01:39:06 +0000157 if (retval) {
158 printk(KERN_ERR "%s: unable to get IRQ %d.\n",
159 dev->name, IRQ_NUBUS_9);
160 goto err_irq;
Finn Thainf4d86752007-05-02 12:55:56 +1000161 }
Kulikov Vasiliy546e3ab2010-07-10 01:39:06 +0000162 }
163 retval = sonic_open(dev);
164 if (retval)
165 goto err_irq_nubus;
166 return 0;
167
168err_irq_nubus:
169 if (dev->irq == IRQ_AUTO_3)
170 free_irq(IRQ_NUBUS_9, dev);
171err_irq:
172 free_irq(dev->irq, dev);
173err:
174 return retval;
Finn Thainf4d86752007-05-02 12:55:56 +1000175}
176
177static 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
Alexander Beregalovc6e6d852009-04-11 07:41:28 +0000187static 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,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000191 .ndo_set_rx_mode = sonic_multicast_list,
Alexander Beregalovc6e6d852009-04-11 07:41:28 +0000192 .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
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500199static int macsonic_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Finn Thainefcce832005-08-20 15:53:22 +1000201 struct sonic_local* lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* Allocate the entire chunk of memory for the descriptors.
204 Note that this cannot cross a 64K boundary. */
Joe Perchesd0320f72013-03-14 13:07:21 +0000205 lp->descriptors = dma_alloc_coherent(lp->device,
206 SIZEOF_SONIC_DESC *
207 SONIC_BUS_SCALE(lp->dma_bitmode),
208 &lp->descriptors_laddr,
209 GFP_KERNEL);
210 if (lp->descriptors == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* Now set up the pointers to point to the appropriate places */
Finn Thainefcce832005-08-20 15:53:22 +1000214 lp->cda = lp->descriptors;
215 lp->tda = lp->cda + (SIZEOF_SONIC_CDA
216 * SONIC_BUS_SCALE(lp->dma_bitmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
Finn Thainefcce832005-08-20 15:53:22 +1000218 * SONIC_BUS_SCALE(lp->dma_bitmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
Finn Thainefcce832005-08-20 15:53:22 +1000220 * SONIC_BUS_SCALE(lp->dma_bitmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Finn Thainefcce832005-08-20 15:53:22 +1000222 lp->cda_laddr = lp->descriptors_laddr;
223 lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
224 * SONIC_BUS_SCALE(lp->dma_bitmode));
225 lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
226 * SONIC_BUS_SCALE(lp->dma_bitmode));
227 lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
228 * SONIC_BUS_SCALE(lp->dma_bitmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Alexander Beregalovc6e6d852009-04-11 07:41:28 +0000230 dev->netdev_ops = &macsonic_netdev_ops;
Finn Thainefcce832005-08-20 15:53:22 +1000231 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /*
234 * clear tally counter
235 */
Finn Thainefcce832005-08-20 15:53:22 +1000236 SONIC_WRITE(SONIC_CRCT, 0xffff);
237 SONIC_WRITE(SONIC_FAET, 0xffff);
238 SONIC_WRITE(SONIC_MPT, 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 return 0;
241}
242
Finn Thaindcaa6a92009-11-03 03:42:40 +0000243#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
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500248static void mac_onboard_sonic_ethernet_addr(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Finn Thainefcce832005-08-20 15:53:22 +1000250 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 const int prom_addr = ONBOARD_SONIC_PROM_BASE;
Finn Thaindcaa6a92009-11-03 03:42:40 +0000252 unsigned short val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Finn Thaindcaa6a92009-11-03 03:42:40 +0000254 /*
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Finn Thaindcaa6a92009-11-03 03:42:40 +0000262 for (i = 0; i < 6; i++)
263 dev->dev_addr[i] = SONIC_READ_PROM(i);
264 if (!INVALID_MAC(dev->dev_addr))
265 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /*
Finn Thaindcaa6a92009-11-03 03:42:40 +0000268 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
Finn Thaindcaa6a92009-11-03 03:42:40 +0000272 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
289 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
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");
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000309 eth_hw_addr_random(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500312static int mac_onboard_sonic_probe(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Finn Thainefcce832005-08-20 15:53:22 +1000314 struct sonic_local* lp = netdev_priv(dev);
315 int sr;
316 int commslot = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!MACH_IS_MAC)
319 return -ENODEV;
320
Finn Thainefcce832005-08-20 15:53:22 +1000321 printk(KERN_INFO "Checking for internal Macintosh ethernet (SONIC).. ");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /* 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 ||
Finn Thainefcce832005-08-20 15:53:22 +1000329 macintosh_config->ident == MAC_MODEL_P575 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 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 }
Finn Thainefcce832005-08-20 15:53:22 +1000342 commslot = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400345 printk("yes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Finn Thainefcce832005-08-20 15:53:22 +1000347 /* Danger! My arms are flailing wildly! You *must* set lp->reg_offset
348 * and dev->base_addr before using SONIC_READ() or SONIC_WRITE() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 dev->base_addr = ONBOARD_SONIC_REGISTERS;
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",
Kay Sieversc2313552009-03-24 16:38:22 -0700360 dev_name(lp->device), dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /* The PowerBook's SONIC is 16 bit always. */
363 if (macintosh_config->ident == MAC_MODEL_PB520) {
Finn Thainefcce832005-08-20 15:53:22 +1000364 lp->reg_offset = 0;
365 lp->dma_bitmode = SONIC_BITMODE16;
366 sr = SONIC_READ(SONIC_SR);
367 } else if (commslot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* Some of the comm-slot cards are 16 bit. But some
Finn Thainefcce832005-08-20 15:53:22 +1000369 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;
374 lp->dma_bitmode = SONIC_BITMODE16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Finn Thainefcce832005-08-20 15:53:22 +1000376 sr = SONIC_READ(SONIC_SR);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400377 if (sr == 0x0004 || sr == 0x0006 || sr == 0x0100 || sr == 0x0101)
Finn Thainefcce832005-08-20 15:53:22 +1000378 /* 83932 is 0x0004 or 0x0006, 83934 is 0x0100 or 0x0101 */
379 lp->dma_bitmode = SONIC_BITMODE32;
380 else {
381 lp->dma_bitmode = SONIC_BITMODE16;
382 lp->reg_offset = 0;
383 sr = SONIC_READ(SONIC_SR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Finn Thainefcce832005-08-20 15:53:22 +1000385 } else {
386 /* All onboard cards are at offset 2 with 32 bit DMA. */
387 lp->reg_offset = 2;
388 lp->dma_bitmode = SONIC_BITMODE32;
389 sr = SONIC_READ(SONIC_SR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Finn Thainefcce832005-08-20 15:53:22 +1000391 printk(KERN_INFO
392 "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
Kay Sieversc2313552009-03-24 16:38:22 -0700393 dev_name(lp->device), sr, lp->dma_bitmode?32:16, lp->reg_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Finn Thainefcce832005-08-20 15:53:22 +1000395#if 0 /* This is sometimes useful to find out how MacOS configured the card. */
Kay Sieversc2313552009-03-24 16:38:22 -0700396 printk(KERN_INFO "%s: DCR: 0x%04x, DCR2: 0x%04x\n", dev_name(lp->device),
Finn Thainefcce832005-08-20 15:53:22 +1000397 SONIC_READ(SONIC_DCR) & 0xffff, SONIC_READ(SONIC_DCR2) & 0xffff);
398#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* Software reset, then initialize control registers. */
Finn Thainefcce832005-08-20 15:53:22 +1000401 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
402
403 SONIC_WRITE(SONIC_DCR, SONIC_DCR_EXBUS | SONIC_DCR_BMS |
404 SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
405 (lp->dma_bitmode ? SONIC_DCR_DW : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* This *must* be written back to in order to restore the
Finn Thainefcce832005-08-20 15:53:22 +1000408 * extended programmable output bits, as it may not have been
409 * initialised since the hardware reset. */
410 SONIC_WRITE(SONIC_DCR2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* Clear *and* disable interrupts to be on the safe side */
Finn Thainefcce832005-08-20 15:53:22 +1000413 SONIC_WRITE(SONIC_IMR, 0);
414 SONIC_WRITE(SONIC_ISR, 0x7fff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* Now look for the MAC address. */
Finn Thaindcaa6a92009-11-03 03:42:40 +0000417 mac_onboard_sonic_ethernet_addr(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* Shared init code */
420 return macsonic_init(dev);
421}
422
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500423static int mac_nubus_sonic_ethernet_addr(struct net_device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000424 unsigned long prom_addr, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 int i;
427 for(i = 0; i < 6; i++)
428 dev->dev_addr[i] = SONIC_READ_PROM(i);
Finn Thainefcce832005-08-20 15:53:22 +1000429
430 /* Some of the addresses are bit-reversed */
431 if (id != MACSONIC_DAYNA)
432 bit_reverse_addr(dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 return 0;
435}
436
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500437static int macsonic_ident(struct nubus_dev *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400439 if (ndev->dr_hw == NUBUS_DRHW_ASANTE_LC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 ndev->dr_sw == NUBUS_DRSW_SONIC_LC)
441 return MACSONIC_DAYNALINK;
442 if (ndev->dr_hw == NUBUS_DRHW_SONIC &&
443 ndev->dr_sw == NUBUS_DRSW_APPLE) {
444 /* There has to be a better way to do this... */
445 if (strstr(ndev->board->name, "DuoDock"))
446 return MACSONIC_DUODOCK;
447 else
448 return MACSONIC_APPLE;
449 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400450
Finn Thainefcce832005-08-20 15:53:22 +1000451 if (ndev->dr_hw == NUBUS_DRHW_SMC9194 &&
452 ndev->dr_sw == NUBUS_DRSW_DAYNA)
453 return MACSONIC_DAYNA;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400454
Finn Thainf8779582007-05-01 22:32:53 +0200455 if (ndev->dr_hw == NUBUS_DRHW_APPLE_SONIC_LC &&
Finn Thainefcce832005-08-20 15:53:22 +1000456 ndev->dr_sw == 0) { /* huh? */
457 return MACSONIC_APPLE16;
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return -1;
460}
461
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500462static int mac_nubus_sonic_probe(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 static int slots;
465 struct nubus_dev* ndev = NULL;
Finn Thainefcce832005-08-20 15:53:22 +1000466 struct sonic_local* lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 unsigned long base_addr, prom_addr;
468 u16 sonic_dcr;
Finn Thainefcce832005-08-20 15:53:22 +1000469 int id = -1;
470 int reg_offset, dma_bitmode;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* Find the first SONIC that hasn't been initialized already */
473 while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK,
474 NUBUS_TYPE_ETHERNET, ndev)) != NULL)
475 {
476 /* Have we seen it already? */
477 if (slots & (1<<ndev->board->slot))
478 continue;
479 slots |= 1<<ndev->board->slot;
480
481 /* Is it one of ours? */
482 if ((id = macsonic_ident(ndev)) != -1)
483 break;
484 }
485
486 if (ndev == NULL)
487 return -ENODEV;
488
489 switch (id) {
490 case MACSONIC_DUODOCK:
491 base_addr = ndev->board->slot_addr + DUODOCK_SONIC_REGISTERS;
492 prom_addr = ndev->board->slot_addr + DUODOCK_SONIC_PROM_BASE;
Finn Thainefcce832005-08-20 15:53:22 +1000493 sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT0 | SONIC_DCR_RFT1 |
494 SONIC_DCR_TFT0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 reg_offset = 2;
Finn Thainefcce832005-08-20 15:53:22 +1000496 dma_bitmode = SONIC_BITMODE32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498 case MACSONIC_APPLE:
499 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
500 prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
501 sonic_dcr = SONIC_DCR_BMS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0;
502 reg_offset = 0;
Finn Thainefcce832005-08-20 15:53:22 +1000503 dma_bitmode = SONIC_BITMODE32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505 case MACSONIC_APPLE16:
506 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
507 prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
Finn Thainefcce832005-08-20 15:53:22 +1000508 sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400509 SONIC_DCR_PO1 | SONIC_DCR_BMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 reg_offset = 0;
Finn Thainefcce832005-08-20 15:53:22 +1000511 dma_bitmode = SONIC_BITMODE16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
513 case MACSONIC_DAYNALINK:
514 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
515 prom_addr = ndev->board->slot_addr + DAYNALINK_PROM_BASE;
Finn Thainefcce832005-08-20 15:53:22 +1000516 sonic_dcr = SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400517 SONIC_DCR_PO1 | SONIC_DCR_BMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 reg_offset = 0;
Finn Thainefcce832005-08-20 15:53:22 +1000519 dma_bitmode = SONIC_BITMODE16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 break;
521 case MACSONIC_DAYNA:
522 base_addr = ndev->board->slot_addr + DAYNA_SONIC_REGISTERS;
523 prom_addr = ndev->board->slot_addr + DAYNA_SONIC_MAC_ADDR;
Finn Thainefcce832005-08-20 15:53:22 +1000524 sonic_dcr = SONIC_DCR_BMS |
525 SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_PO1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 reg_offset = 0;
Finn Thainefcce832005-08-20 15:53:22 +1000527 dma_bitmode = SONIC_BITMODE16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
529 default:
530 printk(KERN_ERR "macsonic: WTF, id is %d\n", id);
531 return -ENODEV;
532 }
533
Finn Thainefcce832005-08-20 15:53:22 +1000534 /* Danger! My arms are flailing wildly! You *must* set lp->reg_offset
535 * and dev->base_addr before using SONIC_READ() or SONIC_WRITE() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 dev->base_addr = base_addr;
Finn Thainefcce832005-08-20 15:53:22 +1000537 lp->reg_offset = reg_offset;
538 lp->dma_bitmode = dma_bitmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 dev->irq = SLOT2IRQ(ndev->board->slot);
540
541 if (!sonic_version_printed) {
542 printk(KERN_INFO "%s", version);
543 sonic_version_printed = 1;
544 }
545 printk(KERN_INFO "%s: %s in slot %X\n",
Kay Sieversc2313552009-03-24 16:38:22 -0700546 dev_name(lp->device), ndev->board->name, ndev->board->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 printk(KERN_INFO "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
Kay Sieversc2313552009-03-24 16:38:22 -0700548 dev_name(lp->device), SONIC_READ(SONIC_SR), dma_bitmode?32:16, reg_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Finn Thainefcce832005-08-20 15:53:22 +1000550#if 0 /* This is sometimes useful to find out how MacOS configured the card. */
Kay Sieversc2313552009-03-24 16:38:22 -0700551 printk(KERN_INFO "%s: DCR: 0x%04x, DCR2: 0x%04x\n", dev_name(lp->device),
Finn Thainefcce832005-08-20 15:53:22 +1000552 SONIC_READ(SONIC_DCR) & 0xffff, SONIC_READ(SONIC_DCR2) & 0xffff);
553#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* Software reset, then initialize control registers. */
Finn Thainefcce832005-08-20 15:53:22 +1000556 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
557 SONIC_WRITE(SONIC_DCR, sonic_dcr | (dma_bitmode ? SONIC_DCR_DW : 0));
558 /* This *must* be written back to in order to restore the
559 * extended programmable output bits, since it may not have been
560 * initialised since the hardware reset. */
561 SONIC_WRITE(SONIC_DCR2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /* Clear *and* disable interrupts to be on the safe side */
Finn Thainefcce832005-08-20 15:53:22 +1000564 SONIC_WRITE(SONIC_IMR, 0);
565 SONIC_WRITE(SONIC_ISR, 0x7fff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* Now look for the MAC address. */
568 if (mac_nubus_sonic_ethernet_addr(dev, prom_addr, id) != 0)
569 return -ENODEV;
570
Finn Thainefcce832005-08-20 15:53:22 +1000571 /* Shared init code */
572 return macsonic_init(dev);
573}
574
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500575static int mac_sonic_probe(struct platform_device *pdev)
Finn Thainefcce832005-08-20 15:53:22 +1000576{
577 struct net_device *dev;
578 struct sonic_local *lp;
579 int err;
Finn Thainefcce832005-08-20 15:53:22 +1000580
581 dev = alloc_etherdev(sizeof(struct sonic_local));
582 if (!dev)
583 return -ENOMEM;
584
585 lp = netdev_priv(dev);
Finn Thaind74472f2007-05-01 22:33:02 +0200586 lp->device = &pdev->dev;
587 SET_NETDEV_DEV(dev, &pdev->dev);
Finn Thain4564cba2009-07-21 12:21:49 -0700588 platform_set_drvdata(pdev, dev);
Finn Thainefcce832005-08-20 15:53:22 +1000589
590 /* This will catch fatal stuff like -ENOMEM as well as success */
591 err = mac_onboard_sonic_probe(dev);
592 if (err == 0)
593 goto found;
594 if (err != -ENODEV)
595 goto out;
596 err = mac_nubus_sonic_probe(dev);
597 if (err)
598 goto out;
599found:
600 err = register_netdev(dev);
601 if (err)
602 goto out;
603
Johannes Berge1749612008-10-27 15:59:26 -0700604 printk("%s: MAC %pM IRQ %d\n", dev->name, dev->dev_addr, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Finn Thainefcce832005-08-20 15:53:22 +1000606 return 0;
607
608out:
609 free_netdev(dev);
610
611 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Finn Thainefcce832005-08-20 15:53:22 +1000614MODULE_DESCRIPTION("Macintosh SONIC ethernet driver");
615module_param(sonic_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616MODULE_PARM_DESC(sonic_debug, "macsonic debug level (1-4)");
Finn Thaineeb9c182009-11-04 00:42:02 +1100617MODULE_ALIAS("platform:macsonic");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619#include "sonic.c"
620
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500621static int mac_sonic_device_remove(struct platform_device *pdev)
Finn Thainefcce832005-08-20 15:53:22 +1000622{
Finn Thaind74472f2007-05-01 22:33:02 +0200623 struct net_device *dev = platform_get_drvdata(pdev);
Finn Thainefcce832005-08-20 15:53:22 +1000624 struct sonic_local* lp = netdev_priv(dev);
625
Finn Thaind74472f2007-05-01 22:33:02 +0200626 unregister_netdev(dev);
Finn Thainefcce832005-08-20 15:53:22 +1000627 dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
628 lp->descriptors, lp->descriptors_laddr);
Finn Thaind74472f2007-05-01 22:33:02 +0200629 free_netdev(dev);
Finn Thainefcce832005-08-20 15:53:22 +1000630
631 return 0;
632}
633
Russell King3ae5eae2005-11-09 22:32:44 +0000634static struct platform_driver mac_sonic_driver = {
Finn Thainefcce832005-08-20 15:53:22 +1000635 .probe = mac_sonic_probe,
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500636 .remove = mac_sonic_device_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000637 .driver = {
Finn Thaineeb9c182009-11-04 00:42:02 +1100638 .name = mac_sonic_string,
639 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +0000640 },
Finn Thainefcce832005-08-20 15:53:22 +1000641};
642
Axel Lindb62f682011-11-27 16:44:17 +0000643module_platform_driver(mac_sonic_driver);