jazzsonic.c revision 6980cbe4a6dbb3d7871e99a3cbc74a572d14d327
1/*
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.
8 *
9 * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
10 *
11 * This driver is based on work from Andreas Busse, but most of
12 * the code is rewritten.
13 *
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/gfp.h>
26#include <linux/interrupt.h>
27#include <linux/init.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>
39
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>
46
47static char jazz_sonic_string[] = "jazzsonic";
48
49#define SONIC_MEM_SIZE	0x100
50
51#include "sonic.h"
52
53/*
54 * Macros to access SONIC registers
55 */
56#define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg))
57
58#define SONIC_WRITE(reg,val)						\
59do {									\
60	*((volatile unsigned int *)dev->base_addr+(reg)) = (val);		\
61} while (0)
62
63
64/* use 0 for production, 1 for verification, >1 for debug */
65#ifdef SONIC_DEBUG
66static unsigned int sonic_debug = SONIC_DEBUG;
67#else
68static unsigned int sonic_debug = 1;
69#endif
70
71/*
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.
75 */
76static unsigned short known_revisions[] =
77{
78	0x04,			/* Mips Magnum 4000 */
79	0xffff			/* end of list */
80};
81
82static int jazzsonic_open(struct net_device* dev)
83{
84	int retval;
85
86	retval = request_irq(dev->irq, sonic_interrupt, IRQF_DISABLED,
87				"sonic", dev);
88	if (retval) {
89		printk(KERN_ERR "%s: unable to get IRQ %d.\n",
90				dev->name, dev->irq);
91		return retval;
92	}
93
94	retval = sonic_open(dev);
95	if (retval)
96		free_irq(dev->irq, dev);
97	return retval;
98}
99
100static 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
108static const struct net_device_ops sonic_netdev_ops = {
109	.ndo_open		= jazzsonic_open,
110	.ndo_stop		= jazzsonic_close,
111	.ndo_start_xmit		= sonic_send_packet,
112	.ndo_get_stats		= sonic_get_stats,
113	.ndo_set_rx_mode	= sonic_multicast_list,
114	.ndo_tx_timeout		= sonic_tx_timeout,
115	.ndo_change_mtu		= eth_change_mtu,
116	.ndo_validate_addr	= eth_validate_addr,
117	.ndo_set_mac_address	= eth_mac_addr,
118};
119
120static int sonic_probe1(struct net_device *dev)
121{
122	static unsigned version_printed;
123	unsigned int silicon_revision;
124	unsigned int val;
125	struct sonic_local *lp = netdev_priv(dev);
126	int err = -ENODEV;
127	int i;
128
129	if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
130		return -EBUSY;
131
132	/*
133	 * get the Silicon Revision ID. If this is one of the known
134	 * one assume that we found a SONIC ethernet controller at
135	 * the expected location.
136	 */
137	silicon_revision = SONIC_READ(SONIC_SR);
138	if (sonic_debug > 1)
139		printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
140
141	i = 0;
142	while (known_revisions[i] != 0xffff &&
143	       known_revisions[i] != silicon_revision)
144		i++;
145
146	if (known_revisions[i] == 0xffff) {
147		printk("SONIC ethernet controller not found (0x%4x)\n",
148		       silicon_revision);
149		goto out;
150	}
151
152	if (sonic_debug  &&  version_printed++ == 0)
153		printk(version);
154
155	printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ",
156	       dev_name(lp->device), dev->base_addr);
157
158	/*
159	 * Put the sonic into software reset, then
160	 * retrieve and print the ethernet address.
161	 */
162	SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
163	SONIC_WRITE(SONIC_CEP,0);
164	for (i=0; i<3; i++) {
165		val = SONIC_READ(SONIC_CAP0-i);
166		dev->dev_addr[i*2] = val;
167		dev->dev_addr[i*2+1] = val >> 8;
168	}
169
170	err = -ENOMEM;
171
172	/* Initialize the device structure. */
173
174	lp->dma_bitmode = SONIC_BITMODE32;
175
176	/* Allocate the entire chunk of memory for the descriptors.
177           Note that this cannot cross a 64K boundary. */
178	if ((lp->descriptors = dma_alloc_coherent(lp->device,
179				SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
180				&lp->descriptors_laddr, GFP_KERNEL)) == NULL) {
181		printk(KERN_ERR "%s: couldn't alloc DMA memory for descriptors.\n",
182		       dev_name(lp->device));
183		goto out;
184	}
185
186	/* Now set up the pointers to point to the appropriate places */
187	lp->cda = lp->descriptors;
188	lp->tda = lp->cda + (SIZEOF_SONIC_CDA
189	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
190	lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
191	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
192	lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
193	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
194
195	lp->cda_laddr = lp->descriptors_laddr;
196	lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
197	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
198	lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
199	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
200	lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
201	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
202
203	dev->netdev_ops = &sonic_netdev_ops;
204	dev->watchdog_timeo = TX_TIMEOUT;
205
206	/*
207	 * clear tally counter
208	 */
209	SONIC_WRITE(SONIC_CRCT,0xffff);
210	SONIC_WRITE(SONIC_FAET,0xffff);
211	SONIC_WRITE(SONIC_MPT,0xffff);
212
213	return 0;
214out:
215	release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
216	return err;
217}
218
219/*
220 * Probe for a SONIC ethernet controller on a Mips Jazz board.
221 * Actually probing is superfluous but we're paranoid.
222 */
223static int jazz_sonic_probe(struct platform_device *pdev)
224{
225	struct net_device *dev;
226	struct sonic_local *lp;
227	struct resource *res;
228	int err = 0;
229
230	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
231	if (!res)
232		return -ENODEV;
233
234	dev = alloc_etherdev(sizeof(struct sonic_local));
235	if (!dev)
236		return -ENOMEM;
237
238	lp = netdev_priv(dev);
239	lp->device = &pdev->dev;
240	SET_NETDEV_DEV(dev, &pdev->dev);
241	platform_set_drvdata(pdev, dev);
242
243	netdev_boot_setup_check(dev);
244
245	dev->base_addr = res->start;
246	dev->irq = platform_get_irq(pdev, 0);
247	err = sonic_probe1(dev);
248	if (err)
249		goto out;
250	err = register_netdev(dev);
251	if (err)
252		goto out1;
253
254	printk("%s: MAC %pM IRQ %d\n", dev->name, dev->dev_addr, dev->irq);
255
256	return 0;
257
258out1:
259	release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
260out:
261	free_netdev(dev);
262
263	return err;
264}
265
266MODULE_DESCRIPTION("Jazz SONIC ethernet driver");
267module_param(sonic_debug, int, 0);
268MODULE_PARM_DESC(sonic_debug, "jazzsonic debug level (1-4)");
269MODULE_ALIAS("platform:jazzsonic");
270
271#include "sonic.c"
272
273static int jazz_sonic_device_remove(struct platform_device *pdev)
274{
275	struct net_device *dev = platform_get_drvdata(pdev);
276	struct sonic_local* lp = netdev_priv(dev);
277
278	unregister_netdev(dev);
279	dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
280	                  lp->descriptors, lp->descriptors_laddr);
281	release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
282	free_netdev(dev);
283
284	return 0;
285}
286
287static struct platform_driver jazz_sonic_driver = {
288	.probe	= jazz_sonic_probe,
289	.remove	= jazz_sonic_device_remove,
290	.driver	= {
291		.name	= jazz_sonic_string,
292		.owner	= THIS_MODULE,
293	},
294};
295
296module_platform_driver(jazz_sonic_driver);
297