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/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/ioport.h>
27 #include <linux/slab.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>
38 #include <asm/pgtable.h>
41 static char xtsonic_string
[] = "xtsonic";
43 extern unsigned xtboard_nvram_valid(void);
44 extern void xtboard_get_ether_addr(unsigned char *buf
);
49 * According to the documentation for the Sonic ethernet controller,
50 * EOBC should be 760 words (1520 bytes) for 32-bit applications, and,
51 * as such, 2 words less than the buffer size. The value for RBSIZE
52 * defined in sonic.h, however is only 1520.
54 * (Note that in 16-bit configurations, EOBC is 759 words (1518 bytes) and
58 #define SONIC_RBSIZE 1524
61 * The chip provides 256 byte register space.
63 #define SONIC_MEM_SIZE 0x100
66 * Macros to access SONIC registers
68 #define SONIC_READ(reg) \
69 (0xffff & *((volatile unsigned int *)dev->base_addr+reg))
71 #define SONIC_WRITE(reg,val) \
72 *((volatile unsigned int *)dev->base_addr+reg) = val
75 /* Use 0 for production, 1 for verification, and >2 for debug */
77 static unsigned int sonic_debug
= SONIC_DEBUG
;
79 static unsigned int sonic_debug
= 1;
83 * We cannot use station (ethernet) address prefixes to detect the
84 * sonic controller since these are board manufacturer depended.
85 * So we check for known Silicon Revision IDs instead.
87 static unsigned short known_revisions
[] =
89 0x101, /* SONIC 83934 */
90 0xffff /* end of list */
93 static int xtsonic_open(struct net_device
*dev
)
95 if (request_irq(dev
->irq
,&sonic_interrupt
,IRQF_DISABLED
,"sonic",dev
)) {
96 printk(KERN_ERR
"%s: unable to get IRQ %d.\n",
100 return sonic_open(dev
);
103 static int xtsonic_close(struct net_device
*dev
)
106 err
= sonic_close(dev
);
107 free_irq(dev
->irq
, dev
);
111 static int __init
sonic_probe1(struct net_device
*dev
)
113 static unsigned version_printed
= 0;
114 unsigned int silicon_revision
;
115 struct sonic_local
*lp
= netdev_priv(dev
);
116 unsigned int base_addr
= dev
->base_addr
;
120 if (!request_mem_region(base_addr
, 0x100, xtsonic_string
))
124 * get the Silicon Revision ID. If this is one of the known
125 * one assume that we found a SONIC ethernet controller at
126 * the expected location.
128 silicon_revision
= SONIC_READ(SONIC_SR
);
130 printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision
);
133 while ((known_revisions
[i
] != 0xffff) &&
134 (known_revisions
[i
] != silicon_revision
))
137 if (known_revisions
[i
] == 0xffff) {
138 printk("SONIC ethernet controller not found (0x%4x)\n",
143 if (sonic_debug
&& version_printed
++ == 0)
147 * Put the sonic into software reset, then retrieve ethernet address.
148 * Note: we are assuming that the boot-loader has initialized the cam.
150 SONIC_WRITE(SONIC_CMD
,SONIC_CR_RST
);
151 SONIC_WRITE(SONIC_DCR
,
152 SONIC_DCR_WC0
|SONIC_DCR_DW
|SONIC_DCR_LBR
|SONIC_DCR_SBUS
);
153 SONIC_WRITE(SONIC_CEP
,0);
154 SONIC_WRITE(SONIC_IMR
,0);
156 SONIC_WRITE(SONIC_CMD
,SONIC_CR_RST
);
157 SONIC_WRITE(SONIC_CEP
,0);
159 for (i
=0; i
<3; i
++) {
160 unsigned int val
= SONIC_READ(SONIC_CAP0
-i
);
161 dev
->dev_addr
[i
*2] = val
;
162 dev
->dev_addr
[i
*2+1] = val
>> 8;
165 /* Initialize the device structure. */
167 lp
->dma_bitmode
= SONIC_BITMODE32
;
170 * Allocate local private descriptor areas in uncached space.
171 * The entire structure must be located within the same 64kb segment.
172 * A simple way to ensure this is to allocate twice the
173 * size of the structure -- given that the structure is
174 * much less than 64 kB, at least one of the halves of
175 * the allocated area will be contained entirely in 64 kB.
176 * We also allocate extra space for a pointer to allow freeing
177 * this structure later on (in xtsonic_cleanup_module()).
180 dma_alloc_coherent(lp
->device
,
181 SIZEOF_SONIC_DESC
* SONIC_BUS_SCALE(lp
->dma_bitmode
),
182 &lp
->descriptors_laddr
, GFP_KERNEL
);
184 if (lp
->descriptors
== NULL
) {
185 printk(KERN_ERR
"%s: couldn't alloc DMA memory for "
186 " descriptors.\n", lp
->device
->bus_id
);
190 lp
->cda
= lp
->descriptors
;
191 lp
->tda
= lp
->cda
+ (SIZEOF_SONIC_CDA
192 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
193 lp
->rda
= lp
->tda
+ (SIZEOF_SONIC_TD
* SONIC_NUM_TDS
194 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
195 lp
->rra
= lp
->rda
+ (SIZEOF_SONIC_RD
* SONIC_NUM_RDS
196 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
198 /* get the virtual dma address */
200 lp
->cda_laddr
= lp
->descriptors_laddr
;
201 lp
->tda_laddr
= lp
->cda_laddr
+ (SIZEOF_SONIC_CDA
202 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
203 lp
->rda_laddr
= lp
->tda_laddr
+ (SIZEOF_SONIC_TD
* SONIC_NUM_TDS
204 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
205 lp
->rra_laddr
= lp
->rda_laddr
+ (SIZEOF_SONIC_RD
* SONIC_NUM_RDS
206 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
208 dev
->open
= xtsonic_open
;
209 dev
->stop
= xtsonic_close
;
210 dev
->hard_start_xmit
= sonic_send_packet
;
211 dev
->get_stats
= sonic_get_stats
;
212 dev
->set_multicast_list
= &sonic_multicast_list
;
213 dev
->tx_timeout
= sonic_tx_timeout
;
214 dev
->watchdog_timeo
= TX_TIMEOUT
;
217 * clear tally counter
219 SONIC_WRITE(SONIC_CRCT
,0xffff);
220 SONIC_WRITE(SONIC_FAET
,0xffff);
221 SONIC_WRITE(SONIC_MPT
,0xffff);
225 release_region(dev
->base_addr
, SONIC_MEM_SIZE
);
231 * Probe for a SONIC ethernet controller on an XT2000 board.
232 * Actually probing is superfluous but we're paranoid.
235 int __init
xtsonic_probe(struct platform_device
*pdev
)
237 struct net_device
*dev
;
238 struct sonic_local
*lp
;
239 struct resource
*resmem
, *resirq
;
242 if ((resmem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0)) == NULL
)
245 if ((resirq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0)) == NULL
)
248 if ((dev
= alloc_etherdev(sizeof(struct sonic_local
))) == NULL
)
251 lp
= netdev_priv(dev
);
252 lp
->device
= &pdev
->dev
;
253 SET_NETDEV_DEV(dev
, &pdev
->dev
);
254 netdev_boot_setup_check(dev
);
256 dev
->base_addr
= resmem
->start
;
257 dev
->irq
= resirq
->start
;
259 if ((err
= sonic_probe1(dev
)))
261 if ((err
= register_netdev(dev
)))
264 printk("%s: SONIC ethernet @%08lx, MAC %pM, IRQ %d\n", dev
->name
,
265 dev
->base_addr
, dev
->dev_addr
, dev
->irq
);
270 release_region(dev
->base_addr
, SONIC_MEM_SIZE
);
277 MODULE_DESCRIPTION("Xtensa XT2000 SONIC ethernet driver");
278 module_param(sonic_debug
, int, 0);
279 MODULE_PARM_DESC(sonic_debug
, "xtsonic debug level (1-4)");
283 static int __devexit
xtsonic_device_remove (struct platform_device
*pdev
)
285 struct net_device
*dev
= platform_get_drvdata(pdev
);
286 struct sonic_local
*lp
= netdev_priv(dev
);
288 unregister_netdev(dev
);
289 dma_free_coherent(lp
->device
,
290 SIZEOF_SONIC_DESC
* SONIC_BUS_SCALE(lp
->dma_bitmode
),
291 lp
->descriptors
, lp
->descriptors_laddr
);
292 release_region (dev
->base_addr
, SONIC_MEM_SIZE
);
298 static struct platform_driver xtsonic_driver
= {
299 .probe
= xtsonic_probe
,
300 .remove
= __devexit_p(xtsonic_device_remove
),
302 .name
= xtsonic_string
,
306 static int __init
xtsonic_init(void)
308 return platform_driver_register(&xtsonic_driver
);
311 static void __exit
xtsonic_cleanup(void)
313 platform_driver_unregister(&xtsonic_driver
);
316 module_init(xtsonic_init
);
317 module_exit(xtsonic_cleanup
);