Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
efcce839 FT |
2 | * 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. | |
1da177e4 LT |
8 | * |
9 | * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de) | |
6aa20a22 | 10 | * |
1da177e4 LT |
11 | * This driver is based on work from Andreas Busse, but most of |
12 | * the code is rewritten. | |
6aa20a22 | 13 | * |
1da177e4 LT |
14 | * (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> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/init.h> | |
27 | #include <linux/ioport.h> | |
28 | #include <linux/in.h> | |
29 | #include <linux/slab.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> | |
d052d1be | 36 | #include <linux/platform_device.h> |
efcce839 | 37 | #include <linux/dma-mapping.h> |
1da177e4 LT |
38 | |
39 | #include <asm/bootinfo.h> | |
40 | #include <asm/system.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> | |
46 | ||
47 | static char jazz_sonic_string[] = "jazzsonic"; | |
48 | static struct platform_device *jazz_sonic_device; | |
49 | ||
50 | #define SONIC_MEM_SIZE 0x100 | |
51 | ||
1da177e4 LT |
52 | #include "sonic.h" |
53 | ||
54 | /* | |
55 | * Macros to access SONIC registers | |
56 | */ | |
efcce839 | 57 | #define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg)) |
1da177e4 LT |
58 | |
59 | #define SONIC_WRITE(reg,val) \ | |
60 | do { \ | |
efcce839 | 61 | *((volatile unsigned int *)dev->base_addr+(reg)) = (val); \ |
1da177e4 LT |
62 | } while (0) |
63 | ||
64 | ||
efcce839 | 65 | /* use 0 for production, 1 for verification, >1 for debug */ |
1da177e4 LT |
66 | #ifdef SONIC_DEBUG |
67 | static unsigned int sonic_debug = SONIC_DEBUG; | |
6aa20a22 | 68 | #else |
1da177e4 LT |
69 | static unsigned int sonic_debug = 1; |
70 | #endif | |
71 | ||
72 | /* | |
73 | * Base address and interrupt of the SONIC controller on JAZZ boards | |
74 | */ | |
75 | static struct { | |
76 | unsigned int port; | |
77 | unsigned int irq; | |
78 | } sonic_portlist[] = { {JAZZ_ETHERNET_BASE, JAZZ_ETHERNET_IRQ}, {0, 0}}; | |
79 | ||
80 | /* | |
81 | * We cannot use station (ethernet) address prefixes to detect the | |
82 | * sonic controller since these are board manufacturer depended. | |
6aa20a22 | 83 | * So we check for known Silicon Revision IDs instead. |
1da177e4 LT |
84 | */ |
85 | static unsigned short known_revisions[] = | |
86 | { | |
87 | 0x04, /* Mips Magnum 4000 */ | |
88 | 0xffff /* end of list */ | |
89 | }; | |
90 | ||
f4d86754 FT |
91 | static int jazzsonic_open(struct net_device* dev) |
92 | { | |
93 | if (request_irq(dev->irq, &sonic_interrupt, IRQF_DISABLED, "sonic", dev)) { | |
94 | printk(KERN_ERR "%s: unable to get IRQ %d.\n", dev->name, dev->irq); | |
95 | return -EAGAIN; | |
96 | } | |
97 | return sonic_open(dev); | |
98 | } | |
99 | ||
100 | static int jazzsonic_close(struct net_device* dev) | |
101 | { | |
102 | int err; | |
103 | err = sonic_close(dev); | |
104 | free_irq(dev->irq, dev); | |
105 | return err; | |
106 | } | |
107 | ||
efcce839 | 108 | static int __init sonic_probe1(struct net_device *dev) |
1da177e4 LT |
109 | { |
110 | static unsigned version_printed; | |
111 | unsigned int silicon_revision; | |
112 | unsigned int val; | |
efcce839 | 113 | struct sonic_local *lp = netdev_priv(dev); |
1da177e4 LT |
114 | int err = -ENODEV; |
115 | int i; | |
116 | ||
efcce839 | 117 | if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string)) |
1da177e4 | 118 | return -EBUSY; |
efcce839 | 119 | |
1da177e4 LT |
120 | /* |
121 | * get the Silicon Revision ID. If this is one of the known | |
122 | * one assume that we found a SONIC ethernet controller at | |
123 | * the expected location. | |
124 | */ | |
125 | silicon_revision = SONIC_READ(SONIC_SR); | |
126 | if (sonic_debug > 1) | |
127 | printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision); | |
128 | ||
129 | i = 0; | |
130 | while (known_revisions[i] != 0xffff | |
131 | && known_revisions[i] != silicon_revision) | |
132 | i++; | |
133 | ||
134 | if (known_revisions[i] == 0xffff) { | |
135 | printk("SONIC ethernet controller not found (0x%4x)\n", | |
136 | silicon_revision); | |
137 | goto out; | |
138 | } | |
6aa20a22 | 139 | |
1da177e4 LT |
140 | if (sonic_debug && version_printed++ == 0) |
141 | printk(version); | |
142 | ||
efcce839 | 143 | printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ", lp->device->bus_id, dev->base_addr); |
1da177e4 LT |
144 | |
145 | /* | |
146 | * Put the sonic into software reset, then | |
147 | * retrieve and print the ethernet address. | |
148 | */ | |
149 | SONIC_WRITE(SONIC_CMD,SONIC_CR_RST); | |
150 | SONIC_WRITE(SONIC_CEP,0); | |
151 | for (i=0; i<3; i++) { | |
152 | val = SONIC_READ(SONIC_CAP0-i); | |
153 | dev->dev_addr[i*2] = val; | |
154 | dev->dev_addr[i*2+1] = val >> 8; | |
155 | } | |
156 | ||
1da177e4 | 157 | err = -ENOMEM; |
6aa20a22 | 158 | |
1da177e4 | 159 | /* Initialize the device structure. */ |
1da177e4 | 160 | |
efcce839 | 161 | lp->dma_bitmode = SONIC_BITMODE32; |
1da177e4 | 162 | |
efcce839 FT |
163 | /* Allocate the entire chunk of memory for the descriptors. |
164 | Note that this cannot cross a 64K boundary. */ | |
165 | if ((lp->descriptors = dma_alloc_coherent(lp->device, | |
166 | SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode), | |
167 | &lp->descriptors_laddr, GFP_KERNEL)) == NULL) { | |
168 | printk(KERN_ERR "%s: couldn't alloc DMA memory for descriptors.\n", lp->device->bus_id); | |
169 | goto out; | |
1da177e4 LT |
170 | } |
171 | ||
efcce839 FT |
172 | /* Now set up the pointers to point to the appropriate places */ |
173 | lp->cda = lp->descriptors; | |
174 | lp->tda = lp->cda + (SIZEOF_SONIC_CDA | |
175 | * SONIC_BUS_SCALE(lp->dma_bitmode)); | |
176 | lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS | |
177 | * SONIC_BUS_SCALE(lp->dma_bitmode)); | |
178 | lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS | |
179 | * SONIC_BUS_SCALE(lp->dma_bitmode)); | |
180 | ||
181 | lp->cda_laddr = lp->descriptors_laddr; | |
182 | lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA | |
183 | * SONIC_BUS_SCALE(lp->dma_bitmode)); | |
184 | lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS | |
185 | * SONIC_BUS_SCALE(lp->dma_bitmode)); | |
186 | lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS | |
187 | * SONIC_BUS_SCALE(lp->dma_bitmode)); | |
188 | ||
f4d86754 FT |
189 | dev->open = jazzsonic_open; |
190 | dev->stop = jazzsonic_close; | |
1da177e4 | 191 | dev->hard_start_xmit = sonic_send_packet; |
efcce839 | 192 | dev->get_stats = sonic_get_stats; |
1da177e4 | 193 | dev->set_multicast_list = &sonic_multicast_list; |
efcce839 | 194 | dev->tx_timeout = sonic_tx_timeout; |
1da177e4 LT |
195 | dev->watchdog_timeo = TX_TIMEOUT; |
196 | ||
197 | /* | |
198 | * clear tally counter | |
199 | */ | |
200 | SONIC_WRITE(SONIC_CRCT,0xffff); | |
201 | SONIC_WRITE(SONIC_FAET,0xffff); | |
202 | SONIC_WRITE(SONIC_MPT,0xffff); | |
203 | ||
204 | return 0; | |
1da177e4 | 205 | out: |
efcce839 | 206 | release_region(dev->base_addr, SONIC_MEM_SIZE); |
1da177e4 LT |
207 | return err; |
208 | } | |
209 | ||
210 | /* | |
211 | * Probe for a SONIC ethernet controller on a Mips Jazz board. | |
212 | * Actually probing is superfluous but we're paranoid. | |
213 | */ | |
3ae5eaec | 214 | static int __init jazz_sonic_probe(struct platform_device *pdev) |
1da177e4 LT |
215 | { |
216 | struct net_device *dev; | |
217 | struct sonic_local *lp; | |
1da177e4 LT |
218 | int err = 0; |
219 | int i; | |
220 | ||
221 | /* | |
222 | * Don't probe if we're not running on a Jazz board. | |
223 | */ | |
224 | if (mips_machgroup != MACH_GROUP_JAZZ) | |
225 | return -ENODEV; | |
226 | ||
efcce839 | 227 | dev = alloc_etherdev(sizeof(struct sonic_local)); |
1da177e4 LT |
228 | if (!dev) |
229 | return -ENOMEM; | |
230 | ||
efcce839 | 231 | lp = netdev_priv(dev); |
3ae5eaec RK |
232 | lp->device = &pdev->dev; |
233 | SET_NETDEV_DEV(dev, &pdev->dev); | |
efcce839 FT |
234 | SET_MODULE_OWNER(dev); |
235 | ||
1da177e4 | 236 | netdev_boot_setup_check(dev); |
1da177e4 | 237 | |
efcce839 FT |
238 | if (dev->base_addr >= KSEG0) { /* Check a single specified location. */ |
239 | err = sonic_probe1(dev); | |
240 | } else if (dev->base_addr != 0) { /* Don't probe at all. */ | |
1da177e4 LT |
241 | err = -ENXIO; |
242 | } else { | |
243 | for (i = 0; sonic_portlist[i].port; i++) { | |
efcce839 FT |
244 | dev->base_addr = sonic_portlist[i].port; |
245 | dev->irq = sonic_portlist[i].irq; | |
246 | if (sonic_probe1(dev) == 0) | |
1da177e4 LT |
247 | break; |
248 | } | |
249 | if (!sonic_portlist[i].port) | |
250 | err = -ENODEV; | |
251 | } | |
252 | if (err) | |
253 | goto out; | |
254 | err = register_netdev(dev); | |
255 | if (err) | |
256 | goto out1; | |
257 | ||
efcce839 FT |
258 | printk("%s: MAC ", dev->name); |
259 | for (i = 0; i < 6; i++) { | |
260 | printk("%2.2x", dev->dev_addr[i]); | |
261 | if (i < 5) | |
262 | printk(":"); | |
263 | } | |
264 | printk(" IRQ %d\n", dev->irq); | |
265 | ||
1da177e4 LT |
266 | return 0; |
267 | ||
268 | out1: | |
1da177e4 LT |
269 | release_region(dev->base_addr, SONIC_MEM_SIZE); |
270 | out: | |
271 | free_netdev(dev); | |
272 | ||
273 | return err; | |
274 | } | |
275 | ||
efcce839 FT |
276 | MODULE_DESCRIPTION("Jazz SONIC ethernet driver"); |
277 | module_param(sonic_debug, int, 0); | |
278 | MODULE_PARM_DESC(sonic_debug, "jazzsonic debug level (1-4)"); | |
1da177e4 | 279 | |
1da177e4 LT |
280 | #include "sonic.c" |
281 | ||
3ae5eaec | 282 | static int __devexit jazz_sonic_device_remove (struct platform_device *pdev) |
1da177e4 | 283 | { |
3ae5eaec | 284 | struct net_device *dev = platform_get_drvdata(pdev); |
efcce839 | 285 | struct sonic_local* lp = netdev_priv(dev); |
1da177e4 | 286 | |
d74472f0 | 287 | unregister_netdev(dev); |
efcce839 FT |
288 | dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode), |
289 | lp->descriptors, lp->descriptors_laddr); | |
1da177e4 | 290 | release_region (dev->base_addr, SONIC_MEM_SIZE); |
d74472f0 | 291 | free_netdev(dev); |
1da177e4 LT |
292 | |
293 | return 0; | |
294 | } | |
295 | ||
3ae5eaec | 296 | static struct platform_driver jazz_sonic_driver = { |
1da177e4 LT |
297 | .probe = jazz_sonic_probe, |
298 | .remove = __devexit_p(jazz_sonic_device_remove), | |
3ae5eaec RK |
299 | .driver = { |
300 | .name = jazz_sonic_string, | |
301 | }, | |
1da177e4 | 302 | }; |
efcce839 | 303 | |
1da177e4 LT |
304 | static int __init jazz_sonic_init_module(void) |
305 | { | |
efcce839 | 306 | int err; |
1da177e4 | 307 | |
3ae5eaec | 308 | if ((err = platform_driver_register(&jazz_sonic_driver))) { |
1da177e4 | 309 | printk(KERN_ERR "Driver registration failed\n"); |
efcce839 | 310 | return err; |
1da177e4 LT |
311 | } |
312 | ||
95cb5d95 | 313 | jazz_sonic_device = platform_device_alloc(jazz_sonic_string, 0); |
23c2a7b5 | 314 | if (!jazz_sonic_device) |
1da177e4 | 315 | goto out_unregister; |
1da177e4 | 316 | |
95cb5d95 RK |
317 | if (platform_device_add(jazz_sonic_device)) { |
318 | platform_device_put(jazz_sonic_device); | |
1da177e4 LT |
319 | jazz_sonic_device = NULL; |
320 | } | |
321 | ||
322 | return 0; | |
323 | ||
324 | out_unregister: | |
ee7ebdf4 | 325 | platform_driver_unregister(&jazz_sonic_driver); |
1da177e4 LT |
326 | |
327 | return -ENOMEM; | |
328 | } | |
329 | ||
330 | static void __exit jazz_sonic_cleanup_module(void) | |
331 | { | |
3ae5eaec | 332 | platform_driver_unregister(&jazz_sonic_driver); |
1da177e4 LT |
333 | |
334 | if (jazz_sonic_device) { | |
335 | platform_device_unregister(jazz_sonic_device); | |
336 | jazz_sonic_device = NULL; | |
337 | } | |
338 | } | |
339 | ||
340 | module_init(jazz_sonic_init_module); | |
341 | module_exit(jazz_sonic_cleanup_module); |