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 const struct net_device_ops xtsonic_netdev_ops
= {
112 .ndo_open
= xtsonic_open
,
113 .ndo_stop
= xtsonic_close
,
114 .ndo_start_xmit
= sonic_send_packet
,
115 .ndo_get_stats
= sonic_get_stats
,
116 .ndo_set_multicast_list
= sonic_multicast_list
,
117 .ndo_tx_timeout
= sonic_tx_timeout
,
118 .ndo_validate_addr
= eth_validate_addr
,
119 .ndo_change_mtu
= eth_change_mtu
,
120 .ndo_set_mac_address
= eth_mac_addr
,
123 static int __init
sonic_probe1(struct net_device
*dev
)
125 static unsigned version_printed
= 0;
126 unsigned int silicon_revision
;
127 struct sonic_local
*lp
= netdev_priv(dev
);
128 unsigned int base_addr
= dev
->base_addr
;
132 if (!request_mem_region(base_addr
, 0x100, xtsonic_string
))
136 * get the Silicon Revision ID. If this is one of the known
137 * one assume that we found a SONIC ethernet controller at
138 * the expected location.
140 silicon_revision
= SONIC_READ(SONIC_SR
);
142 printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision
);
145 while ((known_revisions
[i
] != 0xffff) &&
146 (known_revisions
[i
] != silicon_revision
))
149 if (known_revisions
[i
] == 0xffff) {
150 printk("SONIC ethernet controller not found (0x%4x)\n",
155 if (sonic_debug
&& version_printed
++ == 0)
159 * Put the sonic into software reset, then retrieve ethernet address.
160 * Note: we are assuming that the boot-loader has initialized the cam.
162 SONIC_WRITE(SONIC_CMD
,SONIC_CR_RST
);
163 SONIC_WRITE(SONIC_DCR
,
164 SONIC_DCR_WC0
|SONIC_DCR_DW
|SONIC_DCR_LBR
|SONIC_DCR_SBUS
);
165 SONIC_WRITE(SONIC_CEP
,0);
166 SONIC_WRITE(SONIC_IMR
,0);
168 SONIC_WRITE(SONIC_CMD
,SONIC_CR_RST
);
169 SONIC_WRITE(SONIC_CEP
,0);
171 for (i
=0; i
<3; i
++) {
172 unsigned int val
= SONIC_READ(SONIC_CAP0
-i
);
173 dev
->dev_addr
[i
*2] = val
;
174 dev
->dev_addr
[i
*2+1] = val
>> 8;
177 /* Initialize the device structure. */
179 lp
->dma_bitmode
= SONIC_BITMODE32
;
182 * Allocate local private descriptor areas in uncached space.
183 * The entire structure must be located within the same 64kb segment.
184 * A simple way to ensure this is to allocate twice the
185 * size of the structure -- given that the structure is
186 * much less than 64 kB, at least one of the halves of
187 * the allocated area will be contained entirely in 64 kB.
188 * We also allocate extra space for a pointer to allow freeing
189 * this structure later on (in xtsonic_cleanup_module()).
192 dma_alloc_coherent(lp
->device
,
193 SIZEOF_SONIC_DESC
* SONIC_BUS_SCALE(lp
->dma_bitmode
),
194 &lp
->descriptors_laddr
, GFP_KERNEL
);
196 if (lp
->descriptors
== NULL
) {
197 printk(KERN_ERR
"%s: couldn't alloc DMA memory for "
198 " descriptors.\n", dev_name(lp
->device
));
202 lp
->cda
= lp
->descriptors
;
203 lp
->tda
= lp
->cda
+ (SIZEOF_SONIC_CDA
204 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
205 lp
->rda
= lp
->tda
+ (SIZEOF_SONIC_TD
* SONIC_NUM_TDS
206 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
207 lp
->rra
= lp
->rda
+ (SIZEOF_SONIC_RD
* SONIC_NUM_RDS
208 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
210 /* get the virtual dma address */
212 lp
->cda_laddr
= lp
->descriptors_laddr
;
213 lp
->tda_laddr
= lp
->cda_laddr
+ (SIZEOF_SONIC_CDA
214 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
215 lp
->rda_laddr
= lp
->tda_laddr
+ (SIZEOF_SONIC_TD
* SONIC_NUM_TDS
216 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
217 lp
->rra_laddr
= lp
->rda_laddr
+ (SIZEOF_SONIC_RD
* SONIC_NUM_RDS
218 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
220 dev
->netdev_ops
= &xtsonic_netdev_ops
;
221 dev
->watchdog_timeo
= TX_TIMEOUT
;
224 * clear tally counter
226 SONIC_WRITE(SONIC_CRCT
,0xffff);
227 SONIC_WRITE(SONIC_FAET
,0xffff);
228 SONIC_WRITE(SONIC_MPT
,0xffff);
232 release_region(dev
->base_addr
, SONIC_MEM_SIZE
);
238 * Probe for a SONIC ethernet controller on an XT2000 board.
239 * Actually probing is superfluous but we're paranoid.
242 int __devinit
xtsonic_probe(struct platform_device
*pdev
)
244 struct net_device
*dev
;
245 struct sonic_local
*lp
;
246 struct resource
*resmem
, *resirq
;
249 if ((resmem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0)) == NULL
)
252 if ((resirq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0)) == NULL
)
255 if ((dev
= alloc_etherdev(sizeof(struct sonic_local
))) == NULL
)
258 lp
= netdev_priv(dev
);
259 lp
->device
= &pdev
->dev
;
260 SET_NETDEV_DEV(dev
, &pdev
->dev
);
261 netdev_boot_setup_check(dev
);
263 dev
->base_addr
= resmem
->start
;
264 dev
->irq
= resirq
->start
;
266 if ((err
= sonic_probe1(dev
)))
268 if ((err
= register_netdev(dev
)))
271 printk("%s: SONIC ethernet @%08lx, MAC %pM, IRQ %d\n", dev
->name
,
272 dev
->base_addr
, dev
->dev_addr
, dev
->irq
);
277 release_region(dev
->base_addr
, SONIC_MEM_SIZE
);
284 MODULE_DESCRIPTION("Xtensa XT2000 SONIC ethernet driver");
285 module_param(sonic_debug
, int, 0);
286 MODULE_PARM_DESC(sonic_debug
, "xtsonic debug level (1-4)");
290 static int __devexit
xtsonic_device_remove (struct platform_device
*pdev
)
292 struct net_device
*dev
= platform_get_drvdata(pdev
);
293 struct sonic_local
*lp
= netdev_priv(dev
);
295 unregister_netdev(dev
);
296 dma_free_coherent(lp
->device
,
297 SIZEOF_SONIC_DESC
* SONIC_BUS_SCALE(lp
->dma_bitmode
),
298 lp
->descriptors
, lp
->descriptors_laddr
);
299 release_region (dev
->base_addr
, SONIC_MEM_SIZE
);
305 static struct platform_driver xtsonic_driver
= {
306 .probe
= xtsonic_probe
,
307 .remove
= __devexit_p(xtsonic_device_remove
),
309 .name
= xtsonic_string
,
313 static int __init
xtsonic_init(void)
315 return platform_driver_register(&xtsonic_driver
);
318 static void __exit
xtsonic_cleanup(void)
320 platform_driver_unregister(&xtsonic_driver
);
323 module_init(xtsonic_init
);
324 module_exit(xtsonic_cleanup
);