blob: acf3f11e38cc1294555c6b7e2193865e7f455f54 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/ioport.h>
28#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/string.h>
30#include <linux/delay.h>
31#include <linux/errno.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010035#include <linux/platform_device.h>
Finn Thainefcce832005-08-20 15:53:22 +100036#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/bootinfo.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/pgtable.h>
41#include <asm/io.h>
42#include <asm/dma.h>
43#include <asm/jazz.h>
44#include <asm/jazzdma.h>
45
46static char jazz_sonic_string[] = "jazzsonic";
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define SONIC_MEM_SIZE 0x100
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "sonic.h"
51
52/*
53 * Macros to access SONIC registers
54 */
Finn Thainefcce832005-08-20 15:53:22 +100055#define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define SONIC_WRITE(reg,val) \
58do { \
Finn Thainefcce832005-08-20 15:53:22 +100059 *((volatile unsigned int *)dev->base_addr+(reg)) = (val); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070060} while (0)
61
62
Finn Thainefcce832005-08-20 15:53:22 +100063/* use 0 for production, 1 for verification, >1 for debug */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifdef SONIC_DEBUG
65static unsigned int sonic_debug = SONIC_DEBUG;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040066#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static unsigned int sonic_debug = 1;
68#endif
69
70/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * We cannot use station (ethernet) address prefixes to detect the
72 * sonic controller since these are board manufacturer depended.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040073 * So we check for known Silicon Revision IDs instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
75static unsigned short known_revisions[] =
76{
77 0x04, /* Mips Magnum 4000 */
78 0xffff /* end of list */
79};
80
Finn Thainf4d86752007-05-02 12:55:56 +100081static int jazzsonic_open(struct net_device* dev)
82{
Kulikov Vasiliy62cd69a2010-07-10 01:00:35 +000083 int retval;
84
Michael Opdenackerd8865d52013-09-13 06:16:53 +020085 retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
Kulikov Vasiliy62cd69a2010-07-10 01:00:35 +000086 if (retval) {
87 printk(KERN_ERR "%s: unable to get IRQ %d.\n",
88 dev->name, dev->irq);
89 return retval;
Finn Thainf4d86752007-05-02 12:55:56 +100090 }
Kulikov Vasiliy62cd69a2010-07-10 01:00:35 +000091
92 retval = sonic_open(dev);
93 if (retval)
94 free_irq(dev->irq, dev);
95 return retval;
Finn Thainf4d86752007-05-02 12:55:56 +100096}
97
98static int jazzsonic_close(struct net_device* dev)
99{
100 int err;
101 err = sonic_close(dev);
102 free_irq(dev->irq, dev);
103 return err;
104}
105
Alexander Beregalov502a3262009-04-15 12:52:45 +0000106static const struct net_device_ops sonic_netdev_ops = {
107 .ndo_open = jazzsonic_open,
108 .ndo_stop = jazzsonic_close,
109 .ndo_start_xmit = sonic_send_packet,
110 .ndo_get_stats = sonic_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000111 .ndo_set_rx_mode = sonic_multicast_list,
Alexander Beregalov502a3262009-04-15 12:52:45 +0000112 .ndo_tx_timeout = sonic_tx_timeout,
113 .ndo_change_mtu = eth_change_mtu,
114 .ndo_validate_addr = eth_validate_addr,
115 .ndo_set_mac_address = eth_mac_addr,
116};
117
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500118static int sonic_probe1(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 static unsigned version_printed;
121 unsigned int silicon_revision;
122 unsigned int val;
Finn Thainefcce832005-08-20 15:53:22 +1000123 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int err = -ENODEV;
125 int i;
126
Finn Thainefcce832005-08-20 15:53:22 +1000127 if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -EBUSY;
Finn Thainefcce832005-08-20 15:53:22 +1000129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /*
131 * get the Silicon Revision ID. If this is one of the known
132 * one assume that we found a SONIC ethernet controller at
133 * the expected location.
134 */
135 silicon_revision = SONIC_READ(SONIC_SR);
136 if (sonic_debug > 1)
137 printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
138
139 i = 0;
Joe Perches8e95a202009-12-03 07:58:21 +0000140 while (known_revisions[i] != 0xffff &&
141 known_revisions[i] != silicon_revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 i++;
143
144 if (known_revisions[i] == 0xffff) {
145 printk("SONIC ethernet controller not found (0x%4x)\n",
146 silicon_revision);
147 goto out;
148 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (sonic_debug && version_printed++ == 0)
151 printk(version);
152
Kay Sieversc2313552009-03-24 16:38:22 -0700153 printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ",
154 dev_name(lp->device), dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /*
157 * Put the sonic into software reset, then
158 * retrieve and print the ethernet address.
159 */
160 SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
161 SONIC_WRITE(SONIC_CEP,0);
162 for (i=0; i<3; i++) {
163 val = SONIC_READ(SONIC_CAP0-i);
164 dev->dev_addr[i*2] = val;
165 dev->dev_addr[i*2+1] = val >> 8;
166 }
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 err = -ENOMEM;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* Initialize the device structure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Finn Thainefcce832005-08-20 15:53:22 +1000172 lp->dma_bitmode = SONIC_BITMODE32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Finn Thainefcce832005-08-20 15:53:22 +1000174 /* Allocate the entire chunk of memory for the descriptors.
175 Note that this cannot cross a 64K boundary. */
Joe Perchesd0320f72013-03-14 13:07:21 +0000176 lp->descriptors = dma_alloc_coherent(lp->device,
177 SIZEOF_SONIC_DESC *
178 SONIC_BUS_SCALE(lp->dma_bitmode),
179 &lp->descriptors_laddr,
180 GFP_KERNEL);
181 if (lp->descriptors == NULL)
Finn Thainefcce832005-08-20 15:53:22 +1000182 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Finn Thainefcce832005-08-20 15:53:22 +1000184 /* Now set up the pointers to point to the appropriate places */
185 lp->cda = lp->descriptors;
186 lp->tda = lp->cda + (SIZEOF_SONIC_CDA
187 * SONIC_BUS_SCALE(lp->dma_bitmode));
188 lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
189 * SONIC_BUS_SCALE(lp->dma_bitmode));
190 lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
191 * SONIC_BUS_SCALE(lp->dma_bitmode));
192
193 lp->cda_laddr = lp->descriptors_laddr;
194 lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
195 * SONIC_BUS_SCALE(lp->dma_bitmode));
196 lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
197 * SONIC_BUS_SCALE(lp->dma_bitmode));
198 lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
199 * SONIC_BUS_SCALE(lp->dma_bitmode));
200
Alexander Beregalov502a3262009-04-15 12:52:45 +0000201 dev->netdev_ops = &sonic_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 dev->watchdog_timeo = TX_TIMEOUT;
203
204 /*
205 * clear tally counter
206 */
207 SONIC_WRITE(SONIC_CRCT,0xffff);
208 SONIC_WRITE(SONIC_FAET,0xffff);
209 SONIC_WRITE(SONIC_MPT,0xffff);
210
211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212out:
Julia Lawallc9145a22009-08-08 21:43:42 +0000213 release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return err;
215}
216
217/*
218 * Probe for a SONIC ethernet controller on a Mips Jazz board.
219 * Actually probing is superfluous but we're paranoid.
220 */
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500221static int jazz_sonic_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct net_device *dev;
224 struct sonic_local *lp;
Thomas Bogendoerfered9f0e02007-09-08 21:46:49 +0200225 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Thomas Bogendoerfered9f0e02007-09-08 21:46:49 +0200228 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
229 if (!res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -ENODEV;
231
Finn Thainefcce832005-08-20 15:53:22 +1000232 dev = alloc_etherdev(sizeof(struct sonic_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!dev)
234 return -ENOMEM;
235
Finn Thainefcce832005-08-20 15:53:22 +1000236 lp = netdev_priv(dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000237 lp->device = &pdev->dev;
238 SET_NETDEV_DEV(dev, &pdev->dev);
Finn Thain4564cba2009-07-21 12:21:49 -0700239 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Finn Thainefcce832005-08-20 15:53:22 +1000241 netdev_boot_setup_check(dev);
242
Thomas Bogendoerfered9f0e02007-09-08 21:46:49 +0200243 dev->base_addr = res->start;
244 dev->irq = platform_get_irq(pdev, 0);
245 err = sonic_probe1(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (err)
247 goto out;
248 err = register_netdev(dev);
249 if (err)
250 goto out1;
251
Johannes Berge1749612008-10-27 15:59:26 -0700252 printk("%s: MAC %pM IRQ %d\n", dev->name, dev->dev_addr, dev->irq);
Finn Thainefcce832005-08-20 15:53:22 +1000253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255
256out1:
Julia Lawallc9145a22009-08-08 21:43:42 +0000257 release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258out:
259 free_netdev(dev);
260
261 return err;
262}
263
Finn Thainefcce832005-08-20 15:53:22 +1000264MODULE_DESCRIPTION("Jazz SONIC ethernet driver");
265module_param(sonic_debug, int, 0);
266MODULE_PARM_DESC(sonic_debug, "jazzsonic debug level (1-4)");
Kay Sievers72abb462008-04-18 13:50:44 -0700267MODULE_ALIAS("platform:jazzsonic");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#include "sonic.c"
270
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500271static int jazz_sonic_device_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Russell King3ae5eae2005-11-09 22:32:44 +0000273 struct net_device *dev = platform_get_drvdata(pdev);
Finn Thainefcce832005-08-20 15:53:22 +1000274 struct sonic_local* lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Finn Thaind74472f2007-05-01 22:33:02 +0200276 unregister_netdev(dev);
Finn Thainefcce832005-08-20 15:53:22 +1000277 dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
278 lp->descriptors, lp->descriptors_laddr);
Julia Lawallc9145a22009-08-08 21:43:42 +0000279 release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
Finn Thaind74472f2007-05-01 22:33:02 +0200280 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 return 0;
283}
284
Russell King3ae5eae2005-11-09 22:32:44 +0000285static struct platform_driver jazz_sonic_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 .probe = jazz_sonic_probe,
Bill Pemberton6980cbe2012-12-03 09:23:17 -0500287 .remove = jazz_sonic_device_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000288 .driver = {
289 .name = jazz_sonic_string,
290 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291};
Finn Thainefcce832005-08-20 15:53:22 +1000292
Axel Lindb62f682011-11-27 16:44:17 +0000293module_platform_driver(jazz_sonic_driver);