Linux 4.14.13
[linux-stable.git] / drivers / net / ethernet / natsemi / jazzsonic.c
blobd5b28884e21eb10f742935df7d10ce3d4793dcab
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * jazzsonic.c
5 * (C) 2005 Finn Thain
7 * Converted to DMA API, and (from the mac68k project) introduced
8 * dhd's support for 16-bit cards.
10 * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
12 * This driver is based on work from Andreas Busse, but most of
13 * the code is rewritten.
15 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
17 * A driver for the onboard Sonic ethernet controller on Mips Jazz
18 * systems (Acer Pica-61, Mips Magnum 4000, Olivetti M700 and
19 * perhaps others, too)
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/fcntl.h>
26 #include <linux/gfp.h>
27 #include <linux/interrupt.h>
28 #include <linux/ioport.h>
29 #include <linux/in.h>
30 #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>
36 #include <linux/platform_device.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/slab.h>
40 #include <asm/bootinfo.h>
41 #include <asm/pgtable.h>
42 #include <asm/io.h>
43 #include <asm/dma.h>
44 #include <asm/jazz.h>
45 #include <asm/jazzdma.h>
47 static char jazz_sonic_string[] = "jazzsonic";
49 #define SONIC_MEM_SIZE 0x100
51 #include "sonic.h"
54 * Macros to access SONIC registers
56 #define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg))
58 #define SONIC_WRITE(reg,val) \
59 do { \
60 *((volatile unsigned int *)dev->base_addr+(reg)) = (val); \
61 } while (0)
64 /* use 0 for production, 1 for verification, >1 for debug */
65 #ifdef SONIC_DEBUG
66 static unsigned int sonic_debug = SONIC_DEBUG;
67 #else
68 static unsigned int sonic_debug = 1;
69 #endif
72 * We cannot use station (ethernet) address prefixes to detect the
73 * sonic controller since these are board manufacturer depended.
74 * So we check for known Silicon Revision IDs instead.
76 static unsigned short known_revisions[] =
78 0x04, /* Mips Magnum 4000 */
79 0xffff /* end of list */
82 static int jazzsonic_open(struct net_device* dev)
84 int retval;
86 retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
87 if (retval) {
88 printk(KERN_ERR "%s: unable to get IRQ %d.\n",
89 dev->name, dev->irq);
90 return retval;
93 retval = sonic_open(dev);
94 if (retval)
95 free_irq(dev->irq, dev);
96 return retval;
99 static int jazzsonic_close(struct net_device* dev)
101 int err;
102 err = sonic_close(dev);
103 free_irq(dev->irq, dev);
104 return err;
107 static 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,
112 .ndo_set_rx_mode = sonic_multicast_list,
113 .ndo_tx_timeout = sonic_tx_timeout,
114 .ndo_validate_addr = eth_validate_addr,
115 .ndo_set_mac_address = eth_mac_addr,
118 static int sonic_probe1(struct net_device *dev)
120 static unsigned version_printed;
121 unsigned int silicon_revision;
122 unsigned int val;
123 struct sonic_local *lp = netdev_priv(dev);
124 int err = -ENODEV;
125 int i;
127 if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
128 return -EBUSY;
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.
135 silicon_revision = SONIC_READ(SONIC_SR);
136 if (sonic_debug > 1)
137 printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
139 i = 0;
140 while (known_revisions[i] != 0xffff &&
141 known_revisions[i] != silicon_revision)
142 i++;
144 if (known_revisions[i] == 0xffff) {
145 printk("SONIC ethernet controller not found (0x%4x)\n",
146 silicon_revision);
147 goto out;
150 if (sonic_debug && version_printed++ == 0)
151 printk(version);
153 printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ",
154 dev_name(lp->device), dev->base_addr);
157 * Put the sonic into software reset, then
158 * retrieve and print the ethernet address.
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;
168 err = -ENOMEM;
170 /* Initialize the device structure. */
172 lp->dma_bitmode = SONIC_BITMODE32;
174 /* Allocate the entire chunk of memory for the descriptors.
175 Note that this cannot cross a 64K boundary. */
176 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)
182 goto out;
184 /* 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));
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));
201 dev->netdev_ops = &sonic_netdev_ops;
202 dev->watchdog_timeo = TX_TIMEOUT;
205 * clear tally counter
207 SONIC_WRITE(SONIC_CRCT,0xffff);
208 SONIC_WRITE(SONIC_FAET,0xffff);
209 SONIC_WRITE(SONIC_MPT,0xffff);
211 return 0;
212 out:
213 release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
214 return err;
218 * Probe for a SONIC ethernet controller on a Mips Jazz board.
219 * Actually probing is superfluous but we're paranoid.
221 static int jazz_sonic_probe(struct platform_device *pdev)
223 struct net_device *dev;
224 struct sonic_local *lp;
225 struct resource *res;
226 int err = 0;
228 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
229 if (!res)
230 return -ENODEV;
232 dev = alloc_etherdev(sizeof(struct sonic_local));
233 if (!dev)
234 return -ENOMEM;
236 lp = netdev_priv(dev);
237 lp->device = &pdev->dev;
238 SET_NETDEV_DEV(dev, &pdev->dev);
239 platform_set_drvdata(pdev, dev);
241 netdev_boot_setup_check(dev);
243 dev->base_addr = res->start;
244 dev->irq = platform_get_irq(pdev, 0);
245 err = sonic_probe1(dev);
246 if (err)
247 goto out;
248 err = register_netdev(dev);
249 if (err)
250 goto out1;
252 printk("%s: MAC %pM IRQ %d\n", dev->name, dev->dev_addr, dev->irq);
254 return 0;
256 out1:
257 release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
258 out:
259 free_netdev(dev);
261 return err;
264 MODULE_DESCRIPTION("Jazz SONIC ethernet driver");
265 module_param(sonic_debug, int, 0);
266 MODULE_PARM_DESC(sonic_debug, "jazzsonic debug level (1-4)");
267 MODULE_ALIAS("platform:jazzsonic");
269 #include "sonic.c"
271 static int jazz_sonic_device_remove(struct platform_device *pdev)
273 struct net_device *dev = platform_get_drvdata(pdev);
274 struct sonic_local* lp = netdev_priv(dev);
276 unregister_netdev(dev);
277 dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
278 lp->descriptors, lp->descriptors_laddr);
279 release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
280 free_netdev(dev);
282 return 0;
285 static struct platform_driver jazz_sonic_driver = {
286 .probe = jazz_sonic_probe,
287 .remove = jazz_sonic_device_remove,
288 .driver = {
289 .name = jazz_sonic_string,
293 module_platform_driver(jazz_sonic_driver);