2 * Support for ColdFire CPU based boards using a NS8390 Ethernet device.
4 * Derived from the many other 8390 drivers.
6 * (C) Copyright 2012, Greg Ungerer <gerg@uclinux.org>
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of the Linux
10 * distribution for more details.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/platform_device.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/jiffies.h>
22 #include <asm/mcf8390.h>
24 static const char version[] =
25 "mcf8390.c: (15-06-2012) Greg Ungerer <gerg@uclinux.org>";
28 #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset */
29 #define NE_RESET 0x1f /* Issue a read to reset ,a write to clear */
30 #define NE_EN0_ISR 0x07
31 #define NE_EN0_DCFG 0x0e
32 #define NE_EN0_RSARLO 0x08
33 #define NE_EN0_RSARHI 0x09
34 #define NE_EN0_RCNTLO 0x0a
35 #define NE_EN0_RXCR 0x0c
36 #define NE_EN0_TXCR 0x0d
37 #define NE_EN0_RCNTHI 0x0b
38 #define NE_EN0_IMR 0x0f
40 #define NESM_START_PG 0x40 /* First page of TX buffer */
41 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
42 static u32 mcf8390_msg_enable;
44 #ifdef NE2000_ODDOFFSET
46 * A lot of the ColdFire boards use a separate address region for odd offset
47 * register addresses. The following functions convert and map as required.
48 * Note that the data port accesses are treated a little differently, and
49 * always accessed via the insX/outsX functions.
51 static inline u32 NE_PTR(u32 addr)
54 return addr - 1 + NE2000_ODDOFFSET;
58 static inline u32 NE_DATA_PTR(u32 addr)
63 void ei_outb(u32 val, u32 addr)
67 rp = (NE2000_BYTE *) NE_PTR(addr);
76 rp = (NE2000_BYTE *) NE_PTR(addr);
78 return (u8) (RSWAP(val) & 0xff);
81 void ei_insb(u32 addr, void *vbuf, int len)
87 rp = (NE2000_BYTE *) NE_DATA_PTR(addr);
88 for (; (len > 0); len--) {
94 void ei_insw(u32 addr, void *vbuf, int len)
100 rp = (volatile u16 *) NE_DATA_PTR(addr);
101 for (; (len > 0); len--) {
107 void ei_outsb(u32 addr, const void *vbuf, int len)
109 NE2000_BYTE *rp, val;
113 rp = (NE2000_BYTE *) NE_DATA_PTR(addr);
114 for (; (len > 0); len--) {
120 void ei_outsw(u32 addr, const void *vbuf, int len)
126 rp = (volatile u16 *) NE_DATA_PTR(addr);
127 for (; (len > 0); len--) {
133 #else /* !NE2000_ODDOFFSET */
139 #define ei_outsb outsb
140 #define ei_outsw outsw
142 #endif /* !NE2000_ODDOFFSET */
144 #define ei_inb_p ei_inb
145 #define ei_outb_p ei_outb
150 * Hard reset the card. This used to pause for the same period that a
151 * 8390 reset command required, but that shouldn't be necessary.
153 static void mcf8390_reset_8390(struct net_device *dev)
155 unsigned long reset_start_time = jiffies;
156 u32 addr = dev->base_addr;
157 struct ei_device *ei_local = netdev_priv(dev);
159 netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n", jiffies);
161 ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
164 ei_status.dmaing = 0;
166 /* This check _should_not_ be necessary, omit eventually. */
167 while ((ei_inb(addr + NE_EN0_ISR) & ENISR_RESET) == 0) {
168 if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
169 netdev_warn(dev, "%s: did not complete\n", __func__);
174 ei_outb(ENISR_RESET, addr + NE_EN0_ISR);
178 * This *shouldn't* happen.
179 * If it does, it's the last thing you'll see
181 static void mcf8390_dmaing_err(const char *func, struct net_device *dev,
182 struct ei_device *ei_local)
184 netdev_err(dev, "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
185 func, ei_local->dmaing, ei_local->irqlock);
189 * Grab the 8390 specific header. Similar to the block_input routine, but
190 * we don't need to be concerned with ring wrap as the header will be at
191 * the start of a page, so we optimize accordingly.
193 static void mcf8390_get_8390_hdr(struct net_device *dev,
194 struct e8390_pkt_hdr *hdr, int ring_page)
196 struct ei_device *ei_local = netdev_priv(dev);
197 u32 addr = dev->base_addr;
199 if (ei_local->dmaing) {
200 mcf8390_dmaing_err(__func__, dev, ei_local);
204 ei_local->dmaing |= 0x01;
205 ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, addr + NE_CMD);
206 ei_outb(ENISR_RDC, addr + NE_EN0_ISR);
207 ei_outb(sizeof(struct e8390_pkt_hdr), addr + NE_EN0_RCNTLO);
208 ei_outb(0, addr + NE_EN0_RCNTHI);
209 ei_outb(0, addr + NE_EN0_RSARLO); /* On page boundary */
210 ei_outb(ring_page, addr + NE_EN0_RSARHI);
211 ei_outb(E8390_RREAD + E8390_START, addr + NE_CMD);
213 ei_insw(addr + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr) >> 1);
215 outb(ENISR_RDC, addr + NE_EN0_ISR); /* Ack intr */
216 ei_local->dmaing &= ~0x01;
218 hdr->count = cpu_to_le16(hdr->count);
222 * Block input and output, similar to the Crynwr packet driver.
223 * If you are porting to a new ethercard, look at the packet driver source
224 * for hints. The NEx000 doesn't share the on-board packet memory --
225 * you have to put the packet out through the "remote DMA" dataport
228 static void mcf8390_block_input(struct net_device *dev, int count,
229 struct sk_buff *skb, int ring_offset)
231 struct ei_device *ei_local = netdev_priv(dev);
232 u32 addr = dev->base_addr;
233 char *buf = skb->data;
235 if (ei_local->dmaing) {
236 mcf8390_dmaing_err(__func__, dev, ei_local);
240 ei_local->dmaing |= 0x01;
241 ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, addr + NE_CMD);
242 ei_outb(ENISR_RDC, addr + NE_EN0_ISR);
243 ei_outb(count & 0xff, addr + NE_EN0_RCNTLO);
244 ei_outb(count >> 8, addr + NE_EN0_RCNTHI);
245 ei_outb(ring_offset & 0xff, addr + NE_EN0_RSARLO);
246 ei_outb(ring_offset >> 8, addr + NE_EN0_RSARHI);
247 ei_outb(E8390_RREAD + E8390_START, addr + NE_CMD);
249 ei_insw(addr + NE_DATAPORT, buf, count >> 1);
251 buf[count - 1] = ei_inb(addr + NE_DATAPORT);
253 ei_outb(ENISR_RDC, addr + NE_EN0_ISR); /* Ack intr */
254 ei_local->dmaing &= ~0x01;
257 static void mcf8390_block_output(struct net_device *dev, int count,
258 const unsigned char *buf,
259 const int start_page)
261 struct ei_device *ei_local = netdev_priv(dev);
262 u32 addr = dev->base_addr;
263 unsigned long dma_start;
265 /* Make sure we transfer all bytes if 16bit IO writes */
269 if (ei_local->dmaing) {
270 mcf8390_dmaing_err(__func__, dev, ei_local);
274 ei_local->dmaing |= 0x01;
275 /* We should already be in page 0, but to be safe... */
276 ei_outb(E8390_PAGE0 + E8390_START + E8390_NODMA, addr + NE_CMD);
278 ei_outb(ENISR_RDC, addr + NE_EN0_ISR);
280 /* Now the normal output. */
281 ei_outb(count & 0xff, addr + NE_EN0_RCNTLO);
282 ei_outb(count >> 8, addr + NE_EN0_RCNTHI);
283 ei_outb(0x00, addr + NE_EN0_RSARLO);
284 ei_outb(start_page, addr + NE_EN0_RSARHI);
285 ei_outb(E8390_RWRITE + E8390_START, addr + NE_CMD);
287 ei_outsw(addr + NE_DATAPORT, buf, count >> 1);
290 while ((ei_inb(addr + NE_EN0_ISR) & ENISR_RDC) == 0) {
291 if (time_after(jiffies, dma_start + 2 * HZ / 100)) { /* 20ms */
292 netdev_warn(dev, "timeout waiting for Tx RDC\n");
293 mcf8390_reset_8390(dev);
294 __NS8390_init(dev, 1);
299 ei_outb(ENISR_RDC, addr + NE_EN0_ISR); /* Ack intr */
300 ei_local->dmaing &= ~0x01;
303 static const struct net_device_ops mcf8390_netdev_ops = {
304 .ndo_open = __ei_open,
305 .ndo_stop = __ei_close,
306 .ndo_start_xmit = __ei_start_xmit,
307 .ndo_tx_timeout = __ei_tx_timeout,
308 .ndo_get_stats = __ei_get_stats,
309 .ndo_set_rx_mode = __ei_set_multicast_list,
310 .ndo_validate_addr = eth_validate_addr,
311 .ndo_set_mac_address = eth_mac_addr,
312 .ndo_change_mtu = eth_change_mtu,
313 #ifdef CONFIG_NET_POLL_CONTROLLER
314 .ndo_poll_controller = __ei_poll,
318 static int mcf8390_init(struct net_device *dev)
320 static u32 offsets[] = {
321 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
322 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
324 struct ei_device *ei_local = netdev_priv(dev);
325 unsigned char SA_prom[32];
326 u32 addr = dev->base_addr;
327 int start_page, stop_page;
330 mcf8390_reset_8390(dev);
333 * Read the 16 bytes of station address PROM.
334 * We must first initialize registers,
335 * similar to NS8390_init(eifdev, 0).
336 * We can't reliably read the SAPROM address without this.
337 * (I learned the hard way!).
340 static const struct {
344 {E8390_NODMA + E8390_PAGE0 + E8390_STOP, NE_CMD},
346 {0x48, NE_EN0_DCFG}, /* 0x48: Set byte-wide access */
347 {0x00, NE_EN0_RCNTLO}, /* Clear the count regs */
348 {0x00, NE_EN0_RCNTHI},
349 {0x00, NE_EN0_IMR}, /* Mask completion irq */
351 {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20 Set to monitor */
352 {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02 and loopback mode */
354 {0x00, NE_EN0_RCNTHI},
355 {0x00, NE_EN0_RSARLO}, /* DMA starting at 0x0000 */
356 {0x00, NE_EN0_RSARHI},
357 {E8390_RREAD + E8390_START, NE_CMD},
359 for (i = 0; i < ARRAY_SIZE(program_seq); i++) {
360 ei_outb(program_seq[i].value,
361 addr + program_seq[i].offset);
365 for (i = 0; i < 16; i++) {
366 SA_prom[i] = ei_inb(addr + NE_DATAPORT);
367 ei_inb(addr + NE_DATAPORT);
370 /* We must set the 8390 for word mode. */
371 ei_outb(0x49, addr + NE_EN0_DCFG);
372 start_page = NESM_START_PG;
373 stop_page = NESM_STOP_PG;
375 /* Install the Interrupt handler */
376 ret = request_irq(dev->irq, __ei_interrupt, 0, dev->name, dev);
380 for (i = 0; i < ETH_ALEN; i++)
381 dev->dev_addr[i] = SA_prom[i];
383 netdev_dbg(dev, "Found ethernet address: %pM\n", dev->dev_addr);
385 ei_local->name = "mcf8390";
386 ei_local->tx_start_page = start_page;
387 ei_local->stop_page = stop_page;
388 ei_local->word16 = 1;
389 ei_local->rx_start_page = start_page + TX_PAGES;
390 ei_local->reset_8390 = mcf8390_reset_8390;
391 ei_local->block_input = mcf8390_block_input;
392 ei_local->block_output = mcf8390_block_output;
393 ei_local->get_8390_hdr = mcf8390_get_8390_hdr;
394 ei_local->reg_offset = offsets;
396 dev->netdev_ops = &mcf8390_netdev_ops;
397 __NS8390_init(dev, 0);
398 ret = register_netdev(dev);
400 free_irq(dev->irq, dev);
404 netdev_info(dev, "addr=0x%08x irq=%d, Ethernet Address %pM\n",
405 addr, dev->irq, dev->dev_addr);
409 static int mcf8390_probe(struct platform_device *pdev)
411 struct net_device *dev;
412 struct ei_device *ei_local;
413 struct resource *mem, *irq;
414 resource_size_t msize;
417 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
419 dev_err(&pdev->dev, "no IRQ specified?\n");
423 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
425 dev_err(&pdev->dev, "no memory address specified?\n");
428 msize = resource_size(mem);
429 if (!request_mem_region(mem->start, msize, pdev->name))
432 dev = ____alloc_ei_netdev(0);
434 release_mem_region(mem->start, msize);
438 SET_NETDEV_DEV(dev, &pdev->dev);
439 platform_set_drvdata(pdev, dev);
440 ei_local = netdev_priv(dev);
441 ei_local->msg_enable = mcf8390_msg_enable;
443 dev->irq = irq->start;
444 dev->base_addr = mem->start;
446 ret = mcf8390_init(dev);
448 release_mem_region(mem->start, msize);
455 static int mcf8390_remove(struct platform_device *pdev)
457 struct net_device *dev = platform_get_drvdata(pdev);
458 struct resource *mem;
460 unregister_netdev(dev);
461 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
463 release_mem_region(mem->start, resource_size(mem));
468 static struct platform_driver mcf8390_drv = {
471 .owner = THIS_MODULE,
473 .probe = mcf8390_probe,
474 .remove = mcf8390_remove,
477 module_platform_driver(mcf8390_drv);
479 MODULE_DESCRIPTION("MCF8390 ColdFire NS8390 driver");
480 MODULE_AUTHOR("Greg Ungerer <gerg@uclinux.org>");
481 MODULE_LICENSE("GPL");
482 MODULE_ALIAS("platform:mcf8390");