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/platform_device.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/jiffies.h>
21 #include <asm/mcf8390.h>
23 static const char version
[] =
24 "mcf8390.c: (15-06-2012) Greg Ungerer <gerg@uclinux.org>";
27 #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset */
28 #define NE_RESET 0x1f /* Issue a read to reset ,a write to clear */
29 #define NE_EN0_ISR 0x07
30 #define NE_EN0_DCFG 0x0e
31 #define NE_EN0_RSARLO 0x08
32 #define NE_EN0_RSARHI 0x09
33 #define NE_EN0_RCNTLO 0x0a
34 #define NE_EN0_RXCR 0x0c
35 #define NE_EN0_TXCR 0x0d
36 #define NE_EN0_RCNTHI 0x0b
37 #define NE_EN0_IMR 0x0f
39 #define NESM_START_PG 0x40 /* First page of TX buffer */
40 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
42 #ifdef NE2000_ODDOFFSET
44 * A lot of the ColdFire boards use a separate address region for odd offset
45 * register addresses. The following functions convert and map as required.
46 * Note that the data port accesses are treated a little differently, and
47 * always accessed via the insX/outsX functions.
49 static inline u32
NE_PTR(u32 addr
)
52 return addr
- 1 + NE2000_ODDOFFSET
;
56 static inline u32
NE_DATA_PTR(u32 addr
)
61 void ei_outb(u32 val
, u32 addr
)
65 rp
= (NE2000_BYTE
*) NE_PTR(addr
);
74 rp
= (NE2000_BYTE
*) NE_PTR(addr
);
76 return (u8
) (RSWAP(val
) & 0xff);
79 void ei_insb(u32 addr
, void *vbuf
, int len
)
85 rp
= (NE2000_BYTE
*) NE_DATA_PTR(addr
);
86 for (; (len
> 0); len
--) {
92 void ei_insw(u32 addr
, void *vbuf
, int len
)
98 rp
= (volatile u16
*) NE_DATA_PTR(addr
);
99 for (; (len
> 0); len
--) {
105 void ei_outsb(u32 addr
, const void *vbuf
, int len
)
107 NE2000_BYTE
*rp
, val
;
111 rp
= (NE2000_BYTE
*) NE_DATA_PTR(addr
);
112 for (; (len
> 0); len
--) {
118 void ei_outsw(u32 addr
, const void *vbuf
, int len
)
124 rp
= (volatile u16
*) NE_DATA_PTR(addr
);
125 for (; (len
> 0); len
--) {
131 #else /* !NE2000_ODDOFFSET */
137 #define ei_outsb outsb
138 #define ei_outsw outsw
140 #endif /* !NE2000_ODDOFFSET */
142 #define ei_inb_p ei_inb
143 #define ei_outb_p ei_outb
148 * Hard reset the card. This used to pause for the same period that a
149 * 8390 reset command required, but that shouldn't be necessary.
151 static void mcf8390_reset_8390(struct net_device
*dev
)
153 unsigned long reset_start_time
= jiffies
;
154 u32 addr
= dev
->base_addr
;
155 struct ei_device
*ei_local
= netdev_priv(dev
);
157 netif_dbg(ei_local
, hw
, dev
, "resetting the 8390 t=%ld...\n", jiffies
);
159 ei_outb(ei_inb(addr
+ NE_RESET
), addr
+ NE_RESET
);
162 ei_status
.dmaing
= 0;
164 /* This check _should_not_ be necessary, omit eventually. */
165 while ((ei_inb(addr
+ NE_EN0_ISR
) & ENISR_RESET
) == 0) {
166 if (time_after(jiffies
, reset_start_time
+ 2 * HZ
/ 100)) {
167 netdev_warn(dev
, "%s: did not complete\n", __func__
);
172 ei_outb(ENISR_RESET
, addr
+ NE_EN0_ISR
);
176 * This *shouldn't* happen.
177 * If it does, it's the last thing you'll see
179 static void mcf8390_dmaing_err(const char *func
, struct net_device
*dev
,
180 struct ei_device
*ei_local
)
182 netdev_err(dev
, "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
183 func
, ei_local
->dmaing
, ei_local
->irqlock
);
187 * Grab the 8390 specific header. Similar to the block_input routine, but
188 * we don't need to be concerned with ring wrap as the header will be at
189 * the start of a page, so we optimize accordingly.
191 static void mcf8390_get_8390_hdr(struct net_device
*dev
,
192 struct e8390_pkt_hdr
*hdr
, int ring_page
)
194 struct ei_device
*ei_local
= netdev_priv(dev
);
195 u32 addr
= dev
->base_addr
;
197 if (ei_local
->dmaing
) {
198 mcf8390_dmaing_err(__func__
, dev
, ei_local
);
202 ei_local
->dmaing
|= 0x01;
203 ei_outb(E8390_NODMA
+ E8390_PAGE0
+ E8390_START
, addr
+ NE_CMD
);
204 ei_outb(ENISR_RDC
, addr
+ NE_EN0_ISR
);
205 ei_outb(sizeof(struct e8390_pkt_hdr
), addr
+ NE_EN0_RCNTLO
);
206 ei_outb(0, addr
+ NE_EN0_RCNTHI
);
207 ei_outb(0, addr
+ NE_EN0_RSARLO
); /* On page boundary */
208 ei_outb(ring_page
, addr
+ NE_EN0_RSARHI
);
209 ei_outb(E8390_RREAD
+ E8390_START
, addr
+ NE_CMD
);
211 ei_insw(addr
+ NE_DATAPORT
, hdr
, sizeof(struct e8390_pkt_hdr
) >> 1);
213 outb(ENISR_RDC
, addr
+ NE_EN0_ISR
); /* Ack intr */
214 ei_local
->dmaing
&= ~0x01;
216 hdr
->count
= cpu_to_le16(hdr
->count
);
220 * Block input and output, similar to the Crynwr packet driver.
221 * If you are porting to a new ethercard, look at the packet driver source
222 * for hints. The NEx000 doesn't share the on-board packet memory --
223 * you have to put the packet out through the "remote DMA" dataport
226 static void mcf8390_block_input(struct net_device
*dev
, int count
,
227 struct sk_buff
*skb
, int ring_offset
)
229 struct ei_device
*ei_local
= netdev_priv(dev
);
230 u32 addr
= dev
->base_addr
;
231 char *buf
= skb
->data
;
233 if (ei_local
->dmaing
) {
234 mcf8390_dmaing_err(__func__
, dev
, ei_local
);
238 ei_local
->dmaing
|= 0x01;
239 ei_outb(E8390_NODMA
+ E8390_PAGE0
+ E8390_START
, addr
+ NE_CMD
);
240 ei_outb(ENISR_RDC
, addr
+ NE_EN0_ISR
);
241 ei_outb(count
& 0xff, addr
+ NE_EN0_RCNTLO
);
242 ei_outb(count
>> 8, addr
+ NE_EN0_RCNTHI
);
243 ei_outb(ring_offset
& 0xff, addr
+ NE_EN0_RSARLO
);
244 ei_outb(ring_offset
>> 8, addr
+ NE_EN0_RSARHI
);
245 ei_outb(E8390_RREAD
+ E8390_START
, addr
+ NE_CMD
);
247 ei_insw(addr
+ NE_DATAPORT
, buf
, count
>> 1);
249 buf
[count
- 1] = ei_inb(addr
+ NE_DATAPORT
);
251 ei_outb(ENISR_RDC
, addr
+ NE_EN0_ISR
); /* Ack intr */
252 ei_local
->dmaing
&= ~0x01;
255 static void mcf8390_block_output(struct net_device
*dev
, int count
,
256 const unsigned char *buf
,
257 const int start_page
)
259 struct ei_device
*ei_local
= netdev_priv(dev
);
260 u32 addr
= dev
->base_addr
;
261 unsigned long dma_start
;
263 /* Make sure we transfer all bytes if 16bit IO writes */
267 if (ei_local
->dmaing
) {
268 mcf8390_dmaing_err(__func__
, dev
, ei_local
);
272 ei_local
->dmaing
|= 0x01;
273 /* We should already be in page 0, but to be safe... */
274 ei_outb(E8390_PAGE0
+ E8390_START
+ E8390_NODMA
, addr
+ NE_CMD
);
276 ei_outb(ENISR_RDC
, addr
+ NE_EN0_ISR
);
278 /* Now the normal output. */
279 ei_outb(count
& 0xff, addr
+ NE_EN0_RCNTLO
);
280 ei_outb(count
>> 8, addr
+ NE_EN0_RCNTHI
);
281 ei_outb(0x00, addr
+ NE_EN0_RSARLO
);
282 ei_outb(start_page
, addr
+ NE_EN0_RSARHI
);
283 ei_outb(E8390_RWRITE
+ E8390_START
, addr
+ NE_CMD
);
285 ei_outsw(addr
+ NE_DATAPORT
, buf
, count
>> 1);
288 while ((ei_inb(addr
+ NE_EN0_ISR
) & ENISR_RDC
) == 0) {
289 if (time_after(jiffies
, dma_start
+ 2 * HZ
/ 100)) { /* 20ms */
290 netdev_warn(dev
, "timeout waiting for Tx RDC\n");
291 mcf8390_reset_8390(dev
);
292 __NS8390_init(dev
, 1);
297 ei_outb(ENISR_RDC
, addr
+ NE_EN0_ISR
); /* Ack intr */
298 ei_local
->dmaing
&= ~0x01;
301 static const struct net_device_ops mcf8390_netdev_ops
= {
302 .ndo_open
= __ei_open
,
303 .ndo_stop
= __ei_close
,
304 .ndo_start_xmit
= __ei_start_xmit
,
305 .ndo_tx_timeout
= __ei_tx_timeout
,
306 .ndo_get_stats
= __ei_get_stats
,
307 .ndo_set_rx_mode
= __ei_set_multicast_list
,
308 .ndo_validate_addr
= eth_validate_addr
,
309 .ndo_set_mac_address
= eth_mac_addr
,
310 #ifdef CONFIG_NET_POLL_CONTROLLER
311 .ndo_poll_controller
= __ei_poll
,
315 static int mcf8390_init(struct net_device
*dev
)
317 static u32 offsets
[] = {
318 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
319 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
321 struct ei_device
*ei_local
= netdev_priv(dev
);
322 unsigned char SA_prom
[32];
323 u32 addr
= dev
->base_addr
;
324 int start_page
, stop_page
;
327 mcf8390_reset_8390(dev
);
330 * Read the 16 bytes of station address PROM.
331 * We must first initialize registers,
332 * similar to NS8390_init(eifdev, 0).
333 * We can't reliably read the SAPROM address without this.
334 * (I learned the hard way!).
337 static const struct {
341 {E8390_NODMA
+ E8390_PAGE0
+ E8390_STOP
, NE_CMD
},
343 {0x48, NE_EN0_DCFG
}, /* 0x48: Set byte-wide access */
344 {0x00, NE_EN0_RCNTLO
}, /* Clear the count regs */
345 {0x00, NE_EN0_RCNTHI
},
346 {0x00, NE_EN0_IMR
}, /* Mask completion irq */
348 {E8390_RXOFF
, NE_EN0_RXCR
}, /* 0x20 Set to monitor */
349 {E8390_TXOFF
, NE_EN0_TXCR
}, /* 0x02 and loopback mode */
351 {0x00, NE_EN0_RCNTHI
},
352 {0x00, NE_EN0_RSARLO
}, /* DMA starting at 0x0000 */
353 {0x00, NE_EN0_RSARHI
},
354 {E8390_RREAD
+ E8390_START
, NE_CMD
},
356 for (i
= 0; i
< ARRAY_SIZE(program_seq
); i
++) {
357 ei_outb(program_seq
[i
].value
,
358 addr
+ program_seq
[i
].offset
);
362 for (i
= 0; i
< 16; i
++) {
363 SA_prom
[i
] = ei_inb(addr
+ NE_DATAPORT
);
364 ei_inb(addr
+ NE_DATAPORT
);
367 /* We must set the 8390 for word mode. */
368 ei_outb(0x49, addr
+ NE_EN0_DCFG
);
369 start_page
= NESM_START_PG
;
370 stop_page
= NESM_STOP_PG
;
372 /* Install the Interrupt handler */
373 ret
= request_irq(dev
->irq
, __ei_interrupt
, 0, dev
->name
, dev
);
377 for (i
= 0; i
< ETH_ALEN
; i
++)
378 dev
->dev_addr
[i
] = SA_prom
[i
];
380 netdev_dbg(dev
, "Found ethernet address: %pM\n", dev
->dev_addr
);
382 ei_local
->name
= "mcf8390";
383 ei_local
->tx_start_page
= start_page
;
384 ei_local
->stop_page
= stop_page
;
385 ei_local
->word16
= 1;
386 ei_local
->rx_start_page
= start_page
+ TX_PAGES
;
387 ei_local
->reset_8390
= mcf8390_reset_8390
;
388 ei_local
->block_input
= mcf8390_block_input
;
389 ei_local
->block_output
= mcf8390_block_output
;
390 ei_local
->get_8390_hdr
= mcf8390_get_8390_hdr
;
391 ei_local
->reg_offset
= offsets
;
393 dev
->netdev_ops
= &mcf8390_netdev_ops
;
394 __NS8390_init(dev
, 0);
395 ret
= register_netdev(dev
);
397 free_irq(dev
->irq
, dev
);
401 netdev_info(dev
, "addr=0x%08x irq=%d, Ethernet Address %pM\n",
402 addr
, dev
->irq
, dev
->dev_addr
);
406 static int mcf8390_probe(struct platform_device
*pdev
)
408 struct net_device
*dev
;
409 struct resource
*mem
, *irq
;
410 resource_size_t msize
;
413 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
415 dev_err(&pdev
->dev
, "no IRQ specified?\n");
419 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
421 dev_err(&pdev
->dev
, "no memory address specified?\n");
424 msize
= resource_size(mem
);
425 if (!request_mem_region(mem
->start
, msize
, pdev
->name
))
428 dev
= ____alloc_ei_netdev(0);
430 release_mem_region(mem
->start
, msize
);
434 SET_NETDEV_DEV(dev
, &pdev
->dev
);
435 platform_set_drvdata(pdev
, dev
);
437 dev
->irq
= irq
->start
;
438 dev
->base_addr
= mem
->start
;
440 ret
= mcf8390_init(dev
);
442 release_mem_region(mem
->start
, msize
);
449 static int mcf8390_remove(struct platform_device
*pdev
)
451 struct net_device
*dev
= platform_get_drvdata(pdev
);
452 struct resource
*mem
;
454 unregister_netdev(dev
);
455 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
457 release_mem_region(mem
->start
, resource_size(mem
));
462 static struct platform_driver mcf8390_drv
= {
466 .probe
= mcf8390_probe
,
467 .remove
= mcf8390_remove
,
470 module_platform_driver(mcf8390_drv
);
472 MODULE_DESCRIPTION("MCF8390 ColdFire NS8390 driver");
473 MODULE_AUTHOR("Greg Ungerer <gerg@uclinux.org>");
474 MODULE_LICENSE("GPL");
475 MODULE_ALIAS("platform:mcf8390");