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