blob: 79257f71c5d95e9daac062f111dd1ac3792885d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Finn Thainefcce832005-08-20 15:53:22 +10002 * jazzsonic.c
3 *
4 * (C) 2005 Finn Thain
5 *
6 * Converted to DMA API, and (from the mac68k project) introduced
7 * dhd's support for 16-bit cards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * This driver is based on work from Andreas Busse, but most of
12 * the code is rewritten.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
15 *
16 * A driver for the onboard Sonic ethernet controller on Mips Jazz
17 * systems (Acer Pica-61, Mips Magnum 4000, Olivetti M700 and
18 * perhaps others, too)
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/fcntl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/interrupt.h>
27#include <linux/init.h>
28#include <linux/ioport.h>
29#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/string.h>
31#include <linux/delay.h>
32#include <linux/errno.h>
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/skbuff.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010036#include <linux/platform_device.h>
Finn Thainefcce832005-08-20 15:53:22 +100037#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/bootinfo.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/pgtable.h>
42#include <asm/io.h>
43#include <asm/dma.h>
44#include <asm/jazz.h>
45#include <asm/jazzdma.h>
46
47static char jazz_sonic_string[] = "jazzsonic";
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define SONIC_MEM_SIZE 0x100
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "sonic.h"
52
53/*
54 * Macros to access SONIC registers
55 */
Finn Thainefcce832005-08-20 15:53:22 +100056#define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define SONIC_WRITE(reg,val) \
59do { \
Finn Thainefcce832005-08-20 15:53:22 +100060 *((volatile unsigned int *)dev->base_addr+(reg)) = (val); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070061} while (0)
62
63
Finn Thainefcce832005-08-20 15:53:22 +100064/* use 0 for production, 1 for verification, >1 for debug */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#ifdef SONIC_DEBUG
66static unsigned int sonic_debug = SONIC_DEBUG;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040067#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static unsigned int sonic_debug = 1;
69#endif
70
71/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * We cannot use station (ethernet) address prefixes to detect the
73 * sonic controller since these are board manufacturer depended.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040074 * So we check for known Silicon Revision IDs instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
76static unsigned short known_revisions[] =
77{
78 0x04, /* Mips Magnum 4000 */
79 0xffff /* end of list */
80};
81
Finn Thainf4d86752007-05-02 12:55:56 +100082static int jazzsonic_open(struct net_device* dev)
83{
Kulikov Vasiliy62cd69a2010-07-10 01:00:35 +000084 int retval;
85
Michael Opdenackerd8865d52013-09-13 06:16:53 +020086 retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
Kulikov Vasiliy62cd69a2010-07-10 01:00:35 +000087 if (retval) {
88 printk(KERN_ERR "%s: unable to get IRQ %d.\n",
89 dev->name, dev->irq);
90 return retval;
Finn Thainf4d86752007-05-02 12:55:56 +100091 }
Kulikov Vasiliy62cd69a2010-07-10 01:00:35 +000092
93 retval = sonic_open(dev);
94 if (retval)
95 free_irq(dev->irq, dev);
96 return retval;
Finn Thainf4d86752007-05-02 12:55:56 +100097}
98
99static int jazzsonic_close(struct net_device* dev)
100{
101 int err;
102 err = sonic_close(dev);
103 free_irq(dev->irq, dev);
104 return err;
105}
106
Alexander Beregalov502a3262009-04-15 12:52:45 +0000107static const struct net_device_ops sonic_netdev_ops = {
108 .ndo_open = jazzsonic_open,
109 .ndo_stop = jazzsonic_close,
110 .ndo_start_xmit = sonic_send_packet,
111 .ndo_get_stats = sonic_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000112 .ndo_set_rx_mode = sonic_multicast_list,
Alexander Beregalov502a3262009-04-15 12:52:45 +0000113 .ndo_tx_timeout = sonic_tx_timeout,
114 .ndo_change_mtu = eth_change_mtu,
115 .ndo_validate_addr = eth_validate_addr,
116 .ndo_set_mac_address = eth_mac_addr,
117};
118
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500119static int sonic_probe1(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 static unsigned version_printed;
122 unsigned int silicon_revision;
123 unsigned int val;
Finn Thainefcce832005-08-20 15:53:22 +1000124 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int err = -ENODEV;
126 int i;
127
Finn Thainefcce832005-08-20 15:53:22 +1000128 if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return -EBUSY;
Finn Thainefcce832005-08-20 15:53:22 +1000130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 /*
132 * get the Silicon Revision ID. If this is one of the known
133 * one assume that we found a SONIC ethernet controller at
134 * the expected location.
135 */
136 silicon_revision = SONIC_READ(SONIC_SR);
137 if (sonic_debug > 1)
138 printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
139
140 i = 0;
Joe Perches8e95a202009-12-03 07:58:21 +0000141 while (known_revisions[i] != 0xffff &&
142 known_revisions[i] != silicon_revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 i++;
144
145 if (known_revisions[i] == 0xffff) {
146 printk("SONIC ethernet controller not found (0x%4x)\n",
147 silicon_revision);
148 goto out;
149 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (sonic_debug && version_printed++ == 0)
152 printk(version);
153
Kay Sieversc2313552009-03-24 16:38:22 -0700154 printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ",
155 dev_name(lp->device), dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /*
158 * Put the sonic into software reset, then
159 * retrieve and print the ethernet address.
160 */
161 SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
162 SONIC_WRITE(SONIC_CEP,0);
163 for (i=0; i<3; i++) {
164 val = SONIC_READ(SONIC_CAP0-i);
165 dev->dev_addr[i*2] = val;
166 dev->dev_addr[i*2+1] = val >> 8;
167 }
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 err = -ENOMEM;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* Initialize the device structure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Finn Thainefcce832005-08-20 15:53:22 +1000173 lp->dma_bitmode = SONIC_BITMODE32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Finn Thainefcce832005-08-20 15:53:22 +1000175 /* Allocate the entire chunk of memory for the descriptors.
176 Note that this cannot cross a 64K boundary. */
Joe Perchesd0320f72013-03-14 13:07:21 +0000177 lp->descriptors = dma_alloc_coherent(lp->device,
178 SIZEOF_SONIC_DESC *
179 SONIC_BUS_SCALE(lp->dma_bitmode),
180 &lp->descriptors_laddr,
181 GFP_KERNEL);
182 if (lp->descriptors == NULL)
Finn Thainefcce832005-08-20 15:53:22 +1000183 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Finn Thainefcce832005-08-20 15:53:22 +1000185 /* Now set up the pointers to point to the appropriate places */
186 lp->cda = lp->descriptors;
187 lp->tda = lp->cda + (SIZEOF_SONIC_CDA
188 * SONIC_BUS_SCALE(lp->dma_bitmode));
189 lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
190 * SONIC_BUS_SCALE(lp->dma_bitmode));
191 lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
192 * SONIC_BUS_SCALE(lp->dma_bitmode));
193
194 lp->cda_laddr = lp->descriptors_laddr;
195 lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
196 * SONIC_BUS_SCALE(lp->dma_bitmode));
197 lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
198 * SONIC_BUS_SCALE(lp->dma_bitmode));
199 lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
200 * SONIC_BUS_SCALE(lp->dma_bitmode));
201
Alexander Beregalov502a3262009-04-15 12:52:45 +0000202 dev->netdev_ops = &sonic_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 dev->watchdog_timeo = TX_TIMEOUT;
204
205 /*
206 * clear tally counter
207 */
208 SONIC_WRITE(SONIC_CRCT,0xffff);
209 SONIC_WRITE(SONIC_FAET,0xffff);
210 SONIC_WRITE(SONIC_MPT,0xffff);
211
212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213out:
Julia Lawallc9145a22009-08-08 21:43:42 +0000214 release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return err;
216}
217
218/*
219 * Probe for a SONIC ethernet controller on a Mips Jazz board.
220 * Actually probing is superfluous but we're paranoid.
221 */
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500222static int jazz_sonic_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct net_device *dev;
225 struct sonic_local *lp;
Thomas Bogendoerfered9f0e02007-09-08 21:46:49 +0200226 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Thomas Bogendoerfered9f0e02007-09-08 21:46:49 +0200229 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
230 if (!res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -ENODEV;
232
Finn Thainefcce832005-08-20 15:53:22 +1000233 dev = alloc_etherdev(sizeof(struct sonic_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (!dev)
235 return -ENOMEM;
236
Finn Thainefcce832005-08-20 15:53:22 +1000237 lp = netdev_priv(dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000238 lp->device = &pdev->dev;
239 SET_NETDEV_DEV(dev, &pdev->dev);
Finn Thain4564cba2009-07-21 12:21:49 -0700240 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Finn Thainefcce832005-08-20 15:53:22 +1000242 netdev_boot_setup_check(dev);
243
Thomas Bogendoerfered9f0e02007-09-08 21:46:49 +0200244 dev->base_addr = res->start;
245 dev->irq = platform_get_irq(pdev, 0);
246 err = sonic_probe1(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (err)
248 goto out;
249 err = register_netdev(dev);
250 if (err)
251 goto out1;
252
Johannes Berge1749612008-10-27 15:59:26 -0700253 printk("%s: MAC %pM IRQ %d\n", dev->name, dev->dev_addr, dev->irq);
Finn Thainefcce832005-08-20 15:53:22 +1000254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 0;
256
257out1:
Julia Lawallc9145a22009-08-08 21:43:42 +0000258 release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259out:
260 free_netdev(dev);
261
262 return err;
263}
264
Finn Thainefcce832005-08-20 15:53:22 +1000265MODULE_DESCRIPTION("Jazz SONIC ethernet driver");
266module_param(sonic_debug, int, 0);
267MODULE_PARM_DESC(sonic_debug, "jazzsonic debug level (1-4)");
Kay Sievers72abb462008-04-18 13:50:44 -0700268MODULE_ALIAS("platform:jazzsonic");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#include "sonic.c"
271
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500272static int jazz_sonic_device_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Russell King3ae5eae2005-11-09 22:32:44 +0000274 struct net_device *dev = platform_get_drvdata(pdev);
Finn Thainefcce832005-08-20 15:53:22 +1000275 struct sonic_local* lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Finn Thaind74472f2007-05-01 22:33:02 +0200277 unregister_netdev(dev);
Finn Thainefcce832005-08-20 15:53:22 +1000278 dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
279 lp->descriptors, lp->descriptors_laddr);
Julia Lawallc9145a22009-08-08 21:43:42 +0000280 release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
Finn Thaind74472f2007-05-01 22:33:02 +0200281 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 return 0;
284}
285
Russell King3ae5eae2005-11-09 22:32:44 +0000286static struct platform_driver jazz_sonic_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 .probe = jazz_sonic_probe,
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500288 .remove = jazz_sonic_device_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000289 .driver = {
290 .name = jazz_sonic_string,
Kay Sievers72abb462008-04-18 13:50:44 -0700291 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +0000292 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
Finn Thainefcce832005-08-20 15:53:22 +1000294
Axel Lindb62f682011-11-27 16:44:17 +0000295module_platform_driver(jazz_sonic_driver);