df0ffca91c1ca65806670b5f4a5a1fb644c24f9c
[linux-drm-fsl-dcu.git] / drivers / net / ethernet / 8390 / mcf8390.c
1 /*
2  *  Support for ColdFire CPU based boards using a NS8390 Ethernet device.
3  *
4  *  Derived from the many other 8390 drivers.
5  *
6  *  (C) Copyright 2012,  Greg Ungerer <gerg@uclinux.org>
7  *
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.
11  */
12
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>
21 #include <linux/io.h>
22 #include <asm/mcf8390.h>
23
24 static const char version[] =
25         "mcf8390.c: (15-06-2012) Greg Ungerer <gerg@uclinux.org>";
26
27 #define NE_CMD          0x00
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
39
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;
43
44 #ifdef NE2000_ODDOFFSET
45 /*
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.
50  */
51 static inline u32 NE_PTR(u32 addr)
52 {
53         if (addr & 1)
54                 return addr - 1 + NE2000_ODDOFFSET;
55         return addr;
56 }
57
58 static inline u32 NE_DATA_PTR(u32 addr)
59 {
60         return addr;
61 }
62
63 void ei_outb(u32 val, u32 addr)
64 {
65         NE2000_BYTE *rp;
66
67         rp = (NE2000_BYTE *) NE_PTR(addr);
68         *rp = RSWAP(val);
69 }
70
71 #define ei_inb  ei_inb
72 u8 ei_inb(u32 addr)
73 {
74         NE2000_BYTE *rp, val;
75
76         rp = (NE2000_BYTE *) NE_PTR(addr);
77         val = *rp;
78         return (u8) (RSWAP(val) & 0xff);
79 }
80
81 void ei_insb(u32 addr, void *vbuf, int len)
82 {
83         NE2000_BYTE *rp, val;
84         u8 *buf;
85
86         buf = (u8 *) vbuf;
87         rp = (NE2000_BYTE *) NE_DATA_PTR(addr);
88         for (; (len > 0); len--) {
89                 val = *rp;
90                 *buf++ = RSWAP(val);
91         }
92 }
93
94 void ei_insw(u32 addr, void *vbuf, int len)
95 {
96         volatile u16 *rp;
97         u16 w, *buf;
98
99         buf = (u16 *) vbuf;
100         rp = (volatile u16 *) NE_DATA_PTR(addr);
101         for (; (len > 0); len--) {
102                 w = *rp;
103                 *buf++ = BSWAP(w);
104         }
105 }
106
107 void ei_outsb(u32 addr, const void *vbuf, int len)
108 {
109         NE2000_BYTE *rp, val;
110         u8 *buf;
111
112         buf = (u8 *) vbuf;
113         rp = (NE2000_BYTE *) NE_DATA_PTR(addr);
114         for (; (len > 0); len--) {
115                 val = *buf++;
116                 *rp = RSWAP(val);
117         }
118 }
119
120 void ei_outsw(u32 addr, const void *vbuf, int len)
121 {
122         volatile u16 *rp;
123         u16 w, *buf;
124
125         buf = (u16 *) vbuf;
126         rp = (volatile u16 *) NE_DATA_PTR(addr);
127         for (; (len > 0); len--) {
128                 w = *buf++;
129                 *rp = BSWAP(w);
130         }
131 }
132
133 #else /* !NE2000_ODDOFFSET */
134
135 #define ei_inb          inb
136 #define ei_outb         outb
137 #define ei_insb         insb
138 #define ei_insw         insw
139 #define ei_outsb        outsb
140 #define ei_outsw        outsw
141
142 #endif /* !NE2000_ODDOFFSET */
143
144 #define ei_inb_p        ei_inb
145 #define ei_outb_p       ei_outb
146
147 #include "lib8390.c"
148
149 /*
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.
152  */
153 static void mcf8390_reset_8390(struct net_device *dev)
154 {
155         unsigned long reset_start_time = jiffies;
156         u32 addr = dev->base_addr;
157         struct ei_device *ei_local = netdev_priv(dev);
158
159         netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n", jiffies);
160
161         ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
162
163         ei_status.txing = 0;
164         ei_status.dmaing = 0;
165
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__);
170                         break;
171                 }
172         }
173
174         ei_outb(ENISR_RESET, addr + NE_EN0_ISR);
175 }
176
177 /*
178  * This *shouldn't* happen.
179  * If it does, it's the last thing you'll see
180  */
181 static void mcf8390_dmaing_err(const char *func, struct net_device *dev,
182                                struct ei_device *ei_local)
183 {
184         netdev_err(dev, "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
185                 func, ei_local->dmaing, ei_local->irqlock);
186 }
187
188 /*
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.
192  */
193 static void mcf8390_get_8390_hdr(struct net_device *dev,
194                                  struct e8390_pkt_hdr *hdr, int ring_page)
195 {
196         struct ei_device *ei_local = netdev_priv(dev);
197         u32 addr = dev->base_addr;
198
199         if (ei_local->dmaing) {
200                 mcf8390_dmaing_err(__func__, dev, ei_local);
201                 return;
202         }
203
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);
212
213         ei_insw(addr + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr) >> 1);
214
215         outb(ENISR_RDC, addr + NE_EN0_ISR);     /* Ack intr */
216         ei_local->dmaing &= ~0x01;
217
218         hdr->count = cpu_to_le16(hdr->count);
219 }
220
221 /*
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
226  * using z_writeb.
227  */
228 static void mcf8390_block_input(struct net_device *dev, int count,
229                                 struct sk_buff *skb, int ring_offset)
230 {
231         struct ei_device *ei_local = netdev_priv(dev);
232         u32 addr = dev->base_addr;
233         char *buf = skb->data;
234
235         if (ei_local->dmaing) {
236                 mcf8390_dmaing_err(__func__, dev, ei_local);
237                 return;
238         }
239
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);
248
249         ei_insw(addr + NE_DATAPORT, buf, count >> 1);
250         if (count & 1)
251                 buf[count - 1] = ei_inb(addr + NE_DATAPORT);
252
253         ei_outb(ENISR_RDC, addr + NE_EN0_ISR);  /* Ack intr */
254         ei_local->dmaing &= ~0x01;
255 }
256
257 static void mcf8390_block_output(struct net_device *dev, int count,
258                                  const unsigned char *buf,
259                                  const int start_page)
260 {
261         struct ei_device *ei_local = netdev_priv(dev);
262         u32 addr = dev->base_addr;
263         unsigned long dma_start;
264
265         /* Make sure we transfer all bytes if 16bit IO writes */
266         if (count & 0x1)
267                 count++;
268
269         if (ei_local->dmaing) {
270                 mcf8390_dmaing_err(__func__, dev, ei_local);
271                 return;
272         }
273
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);
277
278         ei_outb(ENISR_RDC, addr + NE_EN0_ISR);
279
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);
286
287         ei_outsw(addr + NE_DATAPORT, buf, count >> 1);
288
289         dma_start = jiffies;
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);
295                         break;
296                 }
297         }
298
299         ei_outb(ENISR_RDC, addr + NE_EN0_ISR);  /* Ack intr */
300         ei_local->dmaing &= ~0x01;
301 }
302
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,
315 #endif
316 };
317
318 static int mcf8390_init(struct net_device *dev)
319 {
320         static u32 offsets[] = {
321                 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
322                 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
323         };
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;
328         int i, ret;
329
330         mcf8390_reset_8390(dev);
331
332         /*
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!).
338          */
339         {
340                 static const struct {
341                         u32 value;
342                         u32 offset;
343                 } program_seq[] = {
344                         {E8390_NODMA + E8390_PAGE0 + E8390_STOP, NE_CMD},
345                                                 /* Select page 0 */
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 */
350                         {0xFF,  NE_EN0_ISR},
351                         {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20 Set to monitor */
352                         {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02 and loopback mode */
353                         {32,    NE_EN0_RCNTLO},
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},
358                 };
359                 for (i = 0; i < ARRAY_SIZE(program_seq); i++) {
360                         ei_outb(program_seq[i].value,
361                                  addr + program_seq[i].offset);
362                 }
363         }
364
365         for (i = 0; i < 16; i++) {
366                 SA_prom[i] = ei_inb(addr + NE_DATAPORT);
367                 ei_inb(addr + NE_DATAPORT);
368         }
369
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;
374
375         /* Install the Interrupt handler */
376         ret = request_irq(dev->irq, __ei_interrupt, 0, dev->name, dev);
377         if (ret)
378                 return ret;
379
380         for (i = 0; i < ETH_ALEN; i++)
381                 dev->dev_addr[i] = SA_prom[i];
382
383         netdev_dbg(dev, "Found ethernet address: %pM\n", dev->dev_addr);
384
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;
395
396         dev->netdev_ops = &mcf8390_netdev_ops;
397         __NS8390_init(dev, 0);
398         ret = register_netdev(dev);
399         if (ret) {
400                 free_irq(dev->irq, dev);
401                 return ret;
402         }
403
404         netdev_info(dev, "addr=0x%08x irq=%d, Ethernet Address %pM\n",
405                 addr, dev->irq, dev->dev_addr);
406         return 0;
407 }
408
409 static int mcf8390_probe(struct platform_device *pdev)
410 {
411         struct net_device *dev;
412         struct ei_device *ei_local;
413         struct resource *mem, *irq;
414         resource_size_t msize;
415         int ret;
416
417         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
418         if (irq == NULL) {
419                 dev_err(&pdev->dev, "no IRQ specified?\n");
420                 return -ENXIO;
421         }
422
423         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
424         if (mem == NULL) {
425                 dev_err(&pdev->dev, "no memory address specified?\n");
426                 return -ENXIO;
427         }
428         msize = resource_size(mem);
429         if (!request_mem_region(mem->start, msize, pdev->name))
430                 return -EBUSY;
431
432         dev = ____alloc_ei_netdev(0);
433         if (dev == NULL) {
434                 release_mem_region(mem->start, msize);
435                 return -ENOMEM;
436         }
437
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;
442
443         dev->irq = irq->start;
444         dev->base_addr = mem->start;
445
446         ret = mcf8390_init(dev);
447         if (ret) {
448                 release_mem_region(mem->start, msize);
449                 free_netdev(dev);
450                 return ret;
451         }
452         return 0;
453 }
454
455 static int mcf8390_remove(struct platform_device *pdev)
456 {
457         struct net_device *dev = platform_get_drvdata(pdev);
458         struct resource *mem;
459
460         unregister_netdev(dev);
461         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
462         if (mem)
463                 release_mem_region(mem->start, resource_size(mem));
464         free_netdev(dev);
465         return 0;
466 }
467
468 static struct platform_driver mcf8390_drv = {
469         .driver = {
470                 .name   = "mcf8390",
471                 .owner  = THIS_MODULE,
472         },
473         .probe          = mcf8390_probe,
474         .remove         = mcf8390_remove,
475 };
476
477 module_platform_driver(mcf8390_drv);
478
479 MODULE_DESCRIPTION("MCF8390 ColdFire NS8390 driver");
480 MODULE_AUTHOR("Greg Ungerer <gerg@uclinux.org>");
481 MODULE_LICENSE("GPL");
482 MODULE_ALIAS("platform:mcf8390");