4 * (C) 2001 - 2007 Tensilica Inc.
5 * Kevin Chea <kchea@yahoo.com>
6 * Marc Gauthier <marc@linux-xtensa.org>
7 * Chris Zankel <chris@zankel.net>
9 * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
11 * This driver is based on work from Andreas Busse, but most of
12 * the code is rewritten.
14 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
16 * A driver for the onboard Sonic ethernet controller on the XT2000.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/fcntl.h>
23 #include <linux/gfp.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/ioport.h>
28 #include <linux/string.h>
29 #include <linux/delay.h>
30 #include <linux/errno.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/platform_device.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/slab.h>
39 #include <asm/pgtable.h>
42 static char xtsonic_string
[] = "xtsonic";
44 extern unsigned xtboard_nvram_valid(void);
45 extern void xtboard_get_ether_addr(unsigned char *buf
);
50 * According to the documentation for the Sonic ethernet controller,
51 * EOBC should be 760 words (1520 bytes) for 32-bit applications, and,
52 * as such, 2 words less than the buffer size. The value for RBSIZE
53 * defined in sonic.h, however is only 1520.
55 * (Note that in 16-bit configurations, EOBC is 759 words (1518 bytes) and
59 #define SONIC_RBSIZE 1524
62 * The chip provides 256 byte register space.
64 #define SONIC_MEM_SIZE 0x100
67 * Macros to access SONIC registers
69 #define SONIC_READ(reg) \
70 (0xffff & *((volatile unsigned int *)dev->base_addr+reg))
72 #define SONIC_WRITE(reg,val) \
73 *((volatile unsigned int *)dev->base_addr+reg) = val
76 /* Use 0 for production, 1 for verification, and >2 for debug */
78 static unsigned int sonic_debug
= SONIC_DEBUG
;
80 static unsigned int sonic_debug
= 1;
84 * We cannot use station (ethernet) address prefixes to detect the
85 * sonic controller since these are board manufacturer depended.
86 * So we check for known Silicon Revision IDs instead.
88 static unsigned short known_revisions
[] =
90 0x101, /* SONIC 83934 */
91 0xffff /* end of list */
94 static int xtsonic_open(struct net_device
*dev
)
96 if (request_irq(dev
->irq
,sonic_interrupt
,IRQF_DISABLED
,"sonic",dev
)) {
97 printk(KERN_ERR
"%s: unable to get IRQ %d.\n",
101 return sonic_open(dev
);
104 static int xtsonic_close(struct net_device
*dev
)
107 err
= sonic_close(dev
);
108 free_irq(dev
->irq
, dev
);
112 static const struct net_device_ops xtsonic_netdev_ops
= {
113 .ndo_open
= xtsonic_open
,
114 .ndo_stop
= xtsonic_close
,
115 .ndo_start_xmit
= sonic_send_packet
,
116 .ndo_get_stats
= sonic_get_stats
,
117 .ndo_set_multicast_list
= sonic_multicast_list
,
118 .ndo_tx_timeout
= sonic_tx_timeout
,
119 .ndo_validate_addr
= eth_validate_addr
,
120 .ndo_change_mtu
= eth_change_mtu
,
121 .ndo_set_mac_address
= eth_mac_addr
,
124 static int __init
sonic_probe1(struct net_device
*dev
)
126 static unsigned version_printed
= 0;
127 unsigned int silicon_revision
;
128 struct sonic_local
*lp
= netdev_priv(dev
);
129 unsigned int base_addr
= dev
->base_addr
;
133 if (!request_mem_region(base_addr
, 0x100, xtsonic_string
))
137 * get the Silicon Revision ID. If this is one of the known
138 * one assume that we found a SONIC ethernet controller at
139 * the expected location.
141 silicon_revision
= SONIC_READ(SONIC_SR
);
143 printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision
);
146 while ((known_revisions
[i
] != 0xffff) &&
147 (known_revisions
[i
] != silicon_revision
))
150 if (known_revisions
[i
] == 0xffff) {
151 printk("SONIC ethernet controller not found (0x%4x)\n",
156 if (sonic_debug
&& version_printed
++ == 0)
160 * Put the sonic into software reset, then retrieve ethernet address.
161 * Note: we are assuming that the boot-loader has initialized the cam.
163 SONIC_WRITE(SONIC_CMD
,SONIC_CR_RST
);
164 SONIC_WRITE(SONIC_DCR
,
165 SONIC_DCR_WC0
|SONIC_DCR_DW
|SONIC_DCR_LBR
|SONIC_DCR_SBUS
);
166 SONIC_WRITE(SONIC_CEP
,0);
167 SONIC_WRITE(SONIC_IMR
,0);
169 SONIC_WRITE(SONIC_CMD
,SONIC_CR_RST
);
170 SONIC_WRITE(SONIC_CEP
,0);
172 for (i
=0; i
<3; i
++) {
173 unsigned int val
= SONIC_READ(SONIC_CAP0
-i
);
174 dev
->dev_addr
[i
*2] = val
;
175 dev
->dev_addr
[i
*2+1] = val
>> 8;
178 /* Initialize the device structure. */
180 lp
->dma_bitmode
= SONIC_BITMODE32
;
183 * Allocate local private descriptor areas in uncached space.
184 * The entire structure must be located within the same 64kb segment.
185 * A simple way to ensure this is to allocate twice the
186 * size of the structure -- given that the structure is
187 * much less than 64 kB, at least one of the halves of
188 * the allocated area will be contained entirely in 64 kB.
189 * We also allocate extra space for a pointer to allow freeing
190 * this structure later on (in xtsonic_cleanup_module()).
193 dma_alloc_coherent(lp
->device
,
194 SIZEOF_SONIC_DESC
* SONIC_BUS_SCALE(lp
->dma_bitmode
),
195 &lp
->descriptors_laddr
, GFP_KERNEL
);
197 if (lp
->descriptors
== NULL
) {
198 printk(KERN_ERR
"%s: couldn't alloc DMA memory for "
199 " descriptors.\n", dev_name(lp
->device
));
203 lp
->cda
= lp
->descriptors
;
204 lp
->tda
= lp
->cda
+ (SIZEOF_SONIC_CDA
205 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
206 lp
->rda
= lp
->tda
+ (SIZEOF_SONIC_TD
* SONIC_NUM_TDS
207 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
208 lp
->rra
= lp
->rda
+ (SIZEOF_SONIC_RD
* SONIC_NUM_RDS
209 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
211 /* get the virtual dma address */
213 lp
->cda_laddr
= lp
->descriptors_laddr
;
214 lp
->tda_laddr
= lp
->cda_laddr
+ (SIZEOF_SONIC_CDA
215 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
216 lp
->rda_laddr
= lp
->tda_laddr
+ (SIZEOF_SONIC_TD
* SONIC_NUM_TDS
217 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
218 lp
->rra_laddr
= lp
->rda_laddr
+ (SIZEOF_SONIC_RD
* SONIC_NUM_RDS
219 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
221 dev
->netdev_ops
= &xtsonic_netdev_ops
;
222 dev
->watchdog_timeo
= TX_TIMEOUT
;
225 * clear tally counter
227 SONIC_WRITE(SONIC_CRCT
,0xffff);
228 SONIC_WRITE(SONIC_FAET
,0xffff);
229 SONIC_WRITE(SONIC_MPT
,0xffff);
233 release_region(dev
->base_addr
, SONIC_MEM_SIZE
);
239 * Probe for a SONIC ethernet controller on an XT2000 board.
240 * Actually probing is superfluous but we're paranoid.
243 int __devinit
xtsonic_probe(struct platform_device
*pdev
)
245 struct net_device
*dev
;
246 struct sonic_local
*lp
;
247 struct resource
*resmem
, *resirq
;
250 if ((resmem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0)) == NULL
)
253 if ((resirq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0)) == NULL
)
256 if ((dev
= alloc_etherdev(sizeof(struct sonic_local
))) == NULL
)
259 lp
= netdev_priv(dev
);
260 lp
->device
= &pdev
->dev
;
261 SET_NETDEV_DEV(dev
, &pdev
->dev
);
262 netdev_boot_setup_check(dev
);
264 dev
->base_addr
= resmem
->start
;
265 dev
->irq
= resirq
->start
;
267 if ((err
= sonic_probe1(dev
)))
269 if ((err
= register_netdev(dev
)))
272 printk("%s: SONIC ethernet @%08lx, MAC %pM, IRQ %d\n", dev
->name
,
273 dev
->base_addr
, dev
->dev_addr
, dev
->irq
);
278 release_region(dev
->base_addr
, SONIC_MEM_SIZE
);
285 MODULE_DESCRIPTION("Xtensa XT2000 SONIC ethernet driver");
286 module_param(sonic_debug
, int, 0);
287 MODULE_PARM_DESC(sonic_debug
, "xtsonic debug level (1-4)");
291 static int __devexit
xtsonic_device_remove (struct platform_device
*pdev
)
293 struct net_device
*dev
= platform_get_drvdata(pdev
);
294 struct sonic_local
*lp
= netdev_priv(dev
);
296 unregister_netdev(dev
);
297 dma_free_coherent(lp
->device
,
298 SIZEOF_SONIC_DESC
* SONIC_BUS_SCALE(lp
->dma_bitmode
),
299 lp
->descriptors
, lp
->descriptors_laddr
);
300 release_region (dev
->base_addr
, SONIC_MEM_SIZE
);
306 static struct platform_driver xtsonic_driver
= {
307 .probe
= xtsonic_probe
,
308 .remove
= __devexit_p(xtsonic_device_remove
),
310 .name
= xtsonic_string
,
314 static int __init
xtsonic_init(void)
316 return platform_driver_register(&xtsonic_driver
);
319 static void __exit
xtsonic_cleanup(void)
321 platform_driver_unregister(&xtsonic_driver
);
324 module_init(xtsonic_init
);
325 module_exit(xtsonic_cleanup
);