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 */
43 #ifdef NE2000_ODDOFFSET
45 * A lot of the ColdFire boards use a separate address region for odd offset
46 * register addresses. The following functions convert and map as required.
47 * Note that the data port accesses are treated a little differently, and
48 * always accessed via the insX/outsX functions.
50 static inline u32 NE_PTR(u32 addr)
53 return addr - 1 + NE2000_ODDOFFSET;
57 static inline u32 NE_DATA_PTR(u32 addr)
62 void ei_outb(u32 val, u32 addr)
66 rp = (NE2000_BYTE *) NE_PTR(addr);
75 rp = (NE2000_BYTE *) NE_PTR(addr);
77 return (u8) (RSWAP(val) & 0xff);
80 void ei_insb(u32 addr, void *vbuf, int len)
86 rp = (NE2000_BYTE *) NE_DATA_PTR(addr);
87 for (; (len > 0); len--) {
93 void ei_insw(u32 addr, void *vbuf, int len)
99 rp = (volatile u16 *) NE_DATA_PTR(addr);
100 for (; (len > 0); len--) {
106 void ei_outsb(u32 addr, const void *vbuf, int len)
108 NE2000_BYTE *rp, val;
112 rp = (NE2000_BYTE *) NE_DATA_PTR(addr);
113 for (; (len > 0); len--) {
119 void ei_outsw(u32 addr, const void *vbuf, int len)
125 rp = (volatile u16 *) NE_DATA_PTR(addr);
126 for (; (len > 0); len--) {
132 #else /* !NE2000_ODDOFFSET */
138 #define ei_outsb outsb
139 #define ei_outsw outsw
141 #endif /* !NE2000_ODDOFFSET */
143 #define ei_inb_p ei_inb
144 #define ei_outb_p ei_outb
149 * Hard reset the card. This used to pause for the same period that a
150 * 8390 reset command required, but that shouldn't be necessary.
152 static void mcf8390_reset_8390(struct net_device *dev)
154 unsigned long reset_start_time = jiffies;
155 u32 addr = dev->base_addr;
158 netdev_dbg(dev, "resetting the 8390 t=%ld...\n", jiffies);
160 ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
163 ei_status.dmaing = 0;
165 /* This check _should_not_ be necessary, omit eventually. */
166 while ((ei_inb(addr + NE_EN0_ISR) & ENISR_RESET) == 0) {
167 if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
168 netdev_warn(dev, "%s: did not complete\n", __func__);
173 ei_outb(ENISR_RESET, addr + NE_EN0_ISR);
177 * This *shouldn't* happen.
178 * If it does, it's the last thing you'll see
180 static void mcf8390_dmaing_err(const char *func, struct net_device *dev,
181 struct ei_device *ei_local)
183 netdev_err(dev, "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
184 func, ei_local->dmaing, ei_local->irqlock);
188 * Grab the 8390 specific header. Similar to the block_input routine, but
189 * we don't need to be concerned with ring wrap as the header will be at
190 * the start of a page, so we optimize accordingly.
192 static void mcf8390_get_8390_hdr(struct net_device *dev,
193 struct e8390_pkt_hdr *hdr, int ring_page)
195 struct ei_device *ei_local = netdev_priv(dev);
196 u32 addr = dev->base_addr;
198 if (ei_local->dmaing) {
199 mcf8390_dmaing_err(__func__, dev, ei_local);
203 ei_local->dmaing |= 0x01;
204 ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, addr + NE_CMD);
205 ei_outb(ENISR_RDC, addr + NE_EN0_ISR);
206 ei_outb(sizeof(struct e8390_pkt_hdr), addr + NE_EN0_RCNTLO);
207 ei_outb(0, addr + NE_EN0_RCNTHI);
208 ei_outb(0, addr + NE_EN0_RSARLO); /* On page boundary */
209 ei_outb(ring_page, addr + NE_EN0_RSARHI);
210 ei_outb(E8390_RREAD + E8390_START, addr + NE_CMD);
212 ei_insw(addr + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr) >> 1);
214 outb(ENISR_RDC, addr + NE_EN0_ISR); /* Ack intr */
215 ei_local->dmaing &= ~0x01;
217 hdr->count = cpu_to_le16(hdr->count);
221 * Block input and output, similar to the Crynwr packet driver.
222 * If you are porting to a new ethercard, look at the packet driver source
223 * for hints. The NEx000 doesn't share the on-board packet memory --
224 * you have to put the packet out through the "remote DMA" dataport
227 static void mcf8390_block_input(struct net_device *dev, int count,
228 struct sk_buff *skb, int ring_offset)
230 struct ei_device *ei_local = netdev_priv(dev);
231 u32 addr = dev->base_addr;
232 char *buf = skb->data;
234 if (ei_local->dmaing) {
235 mcf8390_dmaing_err(__func__, dev, ei_local);
239 ei_local->dmaing |= 0x01;
240 ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, addr + NE_CMD);
241 ei_outb(ENISR_RDC, addr + NE_EN0_ISR);
242 ei_outb(count & 0xff, addr + NE_EN0_RCNTLO);
243 ei_outb(count >> 8, addr + NE_EN0_RCNTHI);
244 ei_outb(ring_offset & 0xff, addr + NE_EN0_RSARLO);
245 ei_outb(ring_offset >> 8, addr + NE_EN0_RSARHI);
246 ei_outb(E8390_RREAD + E8390_START, addr + NE_CMD);
248 ei_insw(addr + NE_DATAPORT, buf, count >> 1);
250 buf[count - 1] = ei_inb(addr + NE_DATAPORT);
252 ei_outb(ENISR_RDC, addr + NE_EN0_ISR); /* Ack intr */
253 ei_local->dmaing &= ~0x01;
256 static void mcf8390_block_output(struct net_device *dev, int count,
257 const unsigned char *buf,
258 const int start_page)
260 struct ei_device *ei_local = netdev_priv(dev);
261 u32 addr = dev->base_addr;
262 unsigned long dma_start;
264 /* Make sure we transfer all bytes if 16bit IO writes */
268 if (ei_local->dmaing) {
269 mcf8390_dmaing_err(__func__, dev, ei_local);
273 ei_local->dmaing |= 0x01;
274 /* We should already be in page 0, but to be safe... */
275 ei_outb(E8390_PAGE0 + E8390_START + E8390_NODMA, addr + NE_CMD);
277 ei_outb(ENISR_RDC, addr + NE_EN0_ISR);
279 /* Now the normal output. */
280 ei_outb(count & 0xff, addr + NE_EN0_RCNTLO);
281 ei_outb(count >> 8, addr + NE_EN0_RCNTHI);
282 ei_outb(0x00, addr + NE_EN0_RSARLO);
283 ei_outb(start_page, addr + NE_EN0_RSARHI);
284 ei_outb(E8390_RWRITE + E8390_START, addr + NE_CMD);
286 ei_outsw(addr + NE_DATAPORT, buf, count >> 1);
289 while ((ei_inb(addr + NE_EN0_ISR) & ENISR_RDC) == 0) {
290 if (time_after(jiffies, dma_start + 2 * HZ / 100)) { /* 20ms */
291 netdev_err(dev, "timeout waiting for Tx RDC\n");
292 mcf8390_reset_8390(dev);
293 __NS8390_init(dev, 1);
298 ei_outb(ENISR_RDC, addr + NE_EN0_ISR); /* Ack intr */
299 ei_local->dmaing &= ~0x01;
302 static const struct net_device_ops mcf8390_netdev_ops = {
303 .ndo_open = __ei_open,
304 .ndo_stop = __ei_close,
305 .ndo_start_xmit = __ei_start_xmit,
306 .ndo_tx_timeout = __ei_tx_timeout,
307 .ndo_get_stats = __ei_get_stats,
308 .ndo_set_rx_mode = __ei_set_multicast_list,
309 .ndo_validate_addr = eth_validate_addr,
310 .ndo_set_mac_address = eth_mac_addr,
311 .ndo_change_mtu = eth_change_mtu,
312 #ifdef CONFIG_NET_POLL_CONTROLLER
313 .ndo_poll_controller = __ei_poll,
317 static int mcf8390_init(struct net_device *dev)
319 static u32 offsets[] = {
320 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
321 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
323 struct ei_device *ei_local = netdev_priv(dev);
324 unsigned char SA_prom[32];
325 u32 addr = dev->base_addr;
326 int start_page, stop_page;
329 mcf8390_reset_8390(dev);
332 * Read the 16 bytes of station address PROM.
333 * We must first initialize registers,
334 * similar to NS8390_init(eifdev, 0).
335 * We can't reliably read the SAPROM address without this.
336 * (I learned the hard way!).
339 static const struct {
343 {E8390_NODMA + E8390_PAGE0 + E8390_STOP, NE_CMD},
345 {0x48, NE_EN0_DCFG}, /* 0x48: Set byte-wide access */
346 {0x00, NE_EN0_RCNTLO}, /* Clear the count regs */
347 {0x00, NE_EN0_RCNTHI},
348 {0x00, NE_EN0_IMR}, /* Mask completion irq */
350 {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20 Set to monitor */
351 {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02 and loopback mode */
353 {0x00, NE_EN0_RCNTHI},
354 {0x00, NE_EN0_RSARLO}, /* DMA starting at 0x0000 */
355 {0x00, NE_EN0_RSARHI},
356 {E8390_RREAD + E8390_START, NE_CMD},
358 for (i = 0; i < ARRAY_SIZE(program_seq); i++) {
359 ei_outb(program_seq[i].value,
360 addr + program_seq[i].offset);
364 for (i = 0; i < 16; i++) {
365 SA_prom[i] = ei_inb(addr + NE_DATAPORT);
366 ei_inb(addr + NE_DATAPORT);
369 /* We must set the 8390 for word mode. */
370 ei_outb(0x49, addr + NE_EN0_DCFG);
371 start_page = NESM_START_PG;
372 stop_page = NESM_STOP_PG;
374 /* Install the Interrupt handler */
375 ret = request_irq(dev->irq, __ei_interrupt, 0, dev->name, dev);
379 for (i = 0; i < ETH_ALEN; i++)
380 dev->dev_addr[i] = SA_prom[i];
382 netdev_dbg(dev, "Found ethernet address: %pM\n", dev->dev_addr);
384 ei_local->name = "mcf8390";
385 ei_local->tx_start_page = start_page;
386 ei_local->stop_page = stop_page;
387 ei_local->word16 = 1;
388 ei_local->rx_start_page = start_page + TX_PAGES;
389 ei_local->reset_8390 = mcf8390_reset_8390;
390 ei_local->block_input = mcf8390_block_input;
391 ei_local->block_output = mcf8390_block_output;
392 ei_local->get_8390_hdr = mcf8390_get_8390_hdr;
393 ei_local->reg_offset = offsets;
395 dev->netdev_ops = &mcf8390_netdev_ops;
396 __NS8390_init(dev, 0);
397 ret = register_netdev(dev);
399 free_irq(dev->irq, dev);
403 netdev_info(dev, "addr=0x%08x irq=%d, Ethernet Address %pM\n",
404 addr, dev->irq, dev->dev_addr);
408 static int mcf8390_probe(struct platform_device *pdev)
410 struct net_device *dev;
411 struct ei_device *ei_local;
412 struct resource *mem, *irq;
413 resource_size_t msize;
416 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
418 dev_err(&pdev->dev, "no IRQ specified?\n");
422 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
424 dev_err(&pdev->dev, "no memory address specified?\n");
427 msize = resource_size(mem);
428 if (!request_mem_region(mem->start, msize, pdev->name))
431 dev = ____alloc_ei_netdev(0);
433 release_mem_region(mem->start, msize);
437 SET_NETDEV_DEV(dev, &pdev->dev);
438 platform_set_drvdata(pdev, dev);
439 ei_local = netdev_priv(dev);
441 dev->irq = irq->start;
442 dev->base_addr = mem->start;
444 ret = mcf8390_init(dev);
446 release_mem_region(mem->start, msize);
453 static int mcf8390_remove(struct platform_device *pdev)
455 struct net_device *dev = platform_get_drvdata(pdev);
456 struct resource *mem;
458 unregister_netdev(dev);
459 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
461 release_mem_region(mem->start, resource_size(mem));
466 static struct platform_driver mcf8390_drv = {
469 .owner = THIS_MODULE,
471 .probe = mcf8390_probe,
472 .remove = mcf8390_remove,
475 module_platform_driver(mcf8390_drv);
477 MODULE_DESCRIPTION("MCF8390 ColdFire NS8390 driver");
478 MODULE_AUTHOR("Greg Ungerer <gerg@uclinux.org>");
479 MODULE_LICENSE("GPL");
480 MODULE_ALIAS("platform:mcf8390");