1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for ColdFire CPU based boards using a NS8390 Ethernet device.
5 * Derived from the many other 8390 drivers.
7 * (C) Copyright 2012, Greg Ungerer <gerg@uclinux.org>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/platform_device.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/jiffies.h>
19 #include <asm/mcf8390.h>
21 static const char version
[] =
22 "mcf8390.c: (15-06-2012) Greg Ungerer <gerg@uclinux.org>";
25 #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset */
26 #define NE_RESET 0x1f /* Issue a read to reset ,a write to clear */
27 #define NE_EN0_ISR 0x07
28 #define NE_EN0_DCFG 0x0e
29 #define NE_EN0_RSARLO 0x08
30 #define NE_EN0_RSARHI 0x09
31 #define NE_EN0_RCNTLO 0x0a
32 #define NE_EN0_RXCR 0x0c
33 #define NE_EN0_TXCR 0x0d
34 #define NE_EN0_RCNTHI 0x0b
35 #define NE_EN0_IMR 0x0f
37 #define NESM_START_PG 0x40 /* First page of TX buffer */
38 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
40 #ifdef NE2000_ODDOFFSET
42 * A lot of the ColdFire boards use a separate address region for odd offset
43 * register addresses. The following functions convert and map as required.
44 * Note that the data port accesses are treated a little differently, and
45 * always accessed via the insX/outsX functions.
47 static inline u32
NE_PTR(u32 addr
)
50 return addr
- 1 + NE2000_ODDOFFSET
;
54 static inline u32
NE_DATA_PTR(u32 addr
)
59 void ei_outb(u32 val
, u32 addr
)
63 rp
= (NE2000_BYTE
*) NE_PTR(addr
);
72 rp
= (NE2000_BYTE
*) NE_PTR(addr
);
74 return (u8
) (RSWAP(val
) & 0xff);
77 void ei_insb(u32 addr
, void *vbuf
, int len
)
83 rp
= (NE2000_BYTE
*) NE_DATA_PTR(addr
);
84 for (; (len
> 0); len
--) {
90 void ei_insw(u32 addr
, void *vbuf
, int len
)
96 rp
= (volatile u16
*) NE_DATA_PTR(addr
);
97 for (; (len
> 0); len
--) {
103 void ei_outsb(u32 addr
, const void *vbuf
, int len
)
105 NE2000_BYTE
*rp
, val
;
109 rp
= (NE2000_BYTE
*) NE_DATA_PTR(addr
);
110 for (; (len
> 0); len
--) {
116 void ei_outsw(u32 addr
, const void *vbuf
, int len
)
122 rp
= (volatile u16
*) NE_DATA_PTR(addr
);
123 for (; (len
> 0); len
--) {
129 #else /* !NE2000_ODDOFFSET */
135 #define ei_outsb outsb
136 #define ei_outsw outsw
138 #endif /* !NE2000_ODDOFFSET */
140 #define ei_inb_p ei_inb
141 #define ei_outb_p ei_outb
146 * Hard reset the card. This used to pause for the same period that a
147 * 8390 reset command required, but that shouldn't be necessary.
149 static void mcf8390_reset_8390(struct net_device
*dev
)
151 unsigned long reset_start_time
= jiffies
;
152 u32 addr
= dev
->base_addr
;
153 struct ei_device
*ei_local
= netdev_priv(dev
);
155 netif_dbg(ei_local
, hw
, dev
, "resetting the 8390 t=%ld...\n", jiffies
);
157 ei_outb(ei_inb(addr
+ NE_RESET
), addr
+ NE_RESET
);
160 ei_status
.dmaing
= 0;
162 /* This check _should_not_ be necessary, omit eventually. */
163 while ((ei_inb(addr
+ NE_EN0_ISR
) & ENISR_RESET
) == 0) {
164 if (time_after(jiffies
, reset_start_time
+ 2 * HZ
/ 100)) {
165 netdev_warn(dev
, "%s: did not complete\n", __func__
);
170 ei_outb(ENISR_RESET
, addr
+ NE_EN0_ISR
);
174 * This *shouldn't* happen.
175 * If it does, it's the last thing you'll see
177 static void mcf8390_dmaing_err(const char *func
, struct net_device
*dev
,
178 struct ei_device
*ei_local
)
180 netdev_err(dev
, "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
181 func
, ei_local
->dmaing
, ei_local
->irqlock
);
185 * Grab the 8390 specific header. Similar to the block_input routine, but
186 * we don't need to be concerned with ring wrap as the header will be at
187 * the start of a page, so we optimize accordingly.
189 static void mcf8390_get_8390_hdr(struct net_device
*dev
,
190 struct e8390_pkt_hdr
*hdr
, int ring_page
)
192 struct ei_device
*ei_local
= netdev_priv(dev
);
193 u32 addr
= dev
->base_addr
;
195 if (ei_local
->dmaing
) {
196 mcf8390_dmaing_err(__func__
, dev
, ei_local
);
200 ei_local
->dmaing
|= 0x01;
201 ei_outb(E8390_NODMA
+ E8390_PAGE0
+ E8390_START
, addr
+ NE_CMD
);
202 ei_outb(ENISR_RDC
, addr
+ NE_EN0_ISR
);
203 ei_outb(sizeof(struct e8390_pkt_hdr
), addr
+ NE_EN0_RCNTLO
);
204 ei_outb(0, addr
+ NE_EN0_RCNTHI
);
205 ei_outb(0, addr
+ NE_EN0_RSARLO
); /* On page boundary */
206 ei_outb(ring_page
, addr
+ NE_EN0_RSARHI
);
207 ei_outb(E8390_RREAD
+ E8390_START
, addr
+ NE_CMD
);
209 ei_insw(addr
+ NE_DATAPORT
, hdr
, sizeof(struct e8390_pkt_hdr
) >> 1);
211 outb(ENISR_RDC
, addr
+ NE_EN0_ISR
); /* Ack intr */
212 ei_local
->dmaing
&= ~0x01;
214 hdr
->count
= cpu_to_le16(hdr
->count
);
218 * Block input and output, similar to the Crynwr packet driver.
219 * If you are porting to a new ethercard, look at the packet driver source
220 * for hints. The NEx000 doesn't share the on-board packet memory --
221 * you have to put the packet out through the "remote DMA" dataport
224 static void mcf8390_block_input(struct net_device
*dev
, int count
,
225 struct sk_buff
*skb
, int ring_offset
)
227 struct ei_device
*ei_local
= netdev_priv(dev
);
228 u32 addr
= dev
->base_addr
;
229 char *buf
= skb
->data
;
231 if (ei_local
->dmaing
) {
232 mcf8390_dmaing_err(__func__
, dev
, ei_local
);
236 ei_local
->dmaing
|= 0x01;
237 ei_outb(E8390_NODMA
+ E8390_PAGE0
+ E8390_START
, addr
+ NE_CMD
);
238 ei_outb(ENISR_RDC
, addr
+ NE_EN0_ISR
);
239 ei_outb(count
& 0xff, addr
+ NE_EN0_RCNTLO
);
240 ei_outb(count
>> 8, addr
+ NE_EN0_RCNTHI
);
241 ei_outb(ring_offset
& 0xff, addr
+ NE_EN0_RSARLO
);
242 ei_outb(ring_offset
>> 8, addr
+ NE_EN0_RSARHI
);
243 ei_outb(E8390_RREAD
+ E8390_START
, addr
+ NE_CMD
);
245 ei_insw(addr
+ NE_DATAPORT
, buf
, count
>> 1);
247 buf
[count
- 1] = ei_inb(addr
+ NE_DATAPORT
);
249 ei_outb(ENISR_RDC
, addr
+ NE_EN0_ISR
); /* Ack intr */
250 ei_local
->dmaing
&= ~0x01;
253 static void mcf8390_block_output(struct net_device
*dev
, int count
,
254 const unsigned char *buf
,
255 const int start_page
)
257 struct ei_device
*ei_local
= netdev_priv(dev
);
258 u32 addr
= dev
->base_addr
;
259 unsigned long dma_start
;
261 /* Make sure we transfer all bytes if 16bit IO writes */
265 if (ei_local
->dmaing
) {
266 mcf8390_dmaing_err(__func__
, dev
, ei_local
);
270 ei_local
->dmaing
|= 0x01;
271 /* We should already be in page 0, but to be safe... */
272 ei_outb(E8390_PAGE0
+ E8390_START
+ E8390_NODMA
, addr
+ NE_CMD
);
274 ei_outb(ENISR_RDC
, addr
+ NE_EN0_ISR
);
276 /* Now the normal output. */
277 ei_outb(count
& 0xff, addr
+ NE_EN0_RCNTLO
);
278 ei_outb(count
>> 8, addr
+ NE_EN0_RCNTHI
);
279 ei_outb(0x00, addr
+ NE_EN0_RSARLO
);
280 ei_outb(start_page
, addr
+ NE_EN0_RSARHI
);
281 ei_outb(E8390_RWRITE
+ E8390_START
, addr
+ NE_CMD
);
283 ei_outsw(addr
+ NE_DATAPORT
, buf
, count
>> 1);
286 while ((ei_inb(addr
+ NE_EN0_ISR
) & ENISR_RDC
) == 0) {
287 if (time_after(jiffies
, dma_start
+ 2 * HZ
/ 100)) { /* 20ms */
288 netdev_warn(dev
, "timeout waiting for Tx RDC\n");
289 mcf8390_reset_8390(dev
);
290 __NS8390_init(dev
, 1);
295 ei_outb(ENISR_RDC
, addr
+ NE_EN0_ISR
); /* Ack intr */
296 ei_local
->dmaing
&= ~0x01;
299 static const struct net_device_ops mcf8390_netdev_ops
= {
300 .ndo_open
= __ei_open
,
301 .ndo_stop
= __ei_close
,
302 .ndo_start_xmit
= __ei_start_xmit
,
303 .ndo_tx_timeout
= __ei_tx_timeout
,
304 .ndo_get_stats
= __ei_get_stats
,
305 .ndo_set_rx_mode
= __ei_set_multicast_list
,
306 .ndo_validate_addr
= eth_validate_addr
,
307 .ndo_set_mac_address
= eth_mac_addr
,
308 #ifdef CONFIG_NET_POLL_CONTROLLER
309 .ndo_poll_controller
= __ei_poll
,
313 static int mcf8390_init(struct net_device
*dev
)
315 static u32 offsets
[] = {
316 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
317 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
319 struct ei_device
*ei_local
= netdev_priv(dev
);
320 unsigned char SA_prom
[32];
321 u32 addr
= dev
->base_addr
;
322 int start_page
, stop_page
;
325 mcf8390_reset_8390(dev
);
328 * Read the 16 bytes of station address PROM.
329 * We must first initialize registers,
330 * similar to NS8390_init(eifdev, 0).
331 * We can't reliably read the SAPROM address without this.
332 * (I learned the hard way!).
335 static const struct {
339 {E8390_NODMA
+ E8390_PAGE0
+ E8390_STOP
, NE_CMD
},
341 {0x48, NE_EN0_DCFG
}, /* 0x48: Set byte-wide access */
342 {0x00, NE_EN0_RCNTLO
}, /* Clear the count regs */
343 {0x00, NE_EN0_RCNTHI
},
344 {0x00, NE_EN0_IMR
}, /* Mask completion irq */
346 {E8390_RXOFF
, NE_EN0_RXCR
}, /* 0x20 Set to monitor */
347 {E8390_TXOFF
, NE_EN0_TXCR
}, /* 0x02 and loopback mode */
349 {0x00, NE_EN0_RCNTHI
},
350 {0x00, NE_EN0_RSARLO
}, /* DMA starting at 0x0000 */
351 {0x00, NE_EN0_RSARHI
},
352 {E8390_RREAD
+ E8390_START
, NE_CMD
},
354 for (i
= 0; i
< ARRAY_SIZE(program_seq
); i
++) {
355 ei_outb(program_seq
[i
].value
,
356 addr
+ program_seq
[i
].offset
);
360 for (i
= 0; i
< 16; i
++) {
361 SA_prom
[i
] = ei_inb(addr
+ NE_DATAPORT
);
362 ei_inb(addr
+ NE_DATAPORT
);
365 /* We must set the 8390 for word mode. */
366 ei_outb(0x49, addr
+ NE_EN0_DCFG
);
367 start_page
= NESM_START_PG
;
368 stop_page
= NESM_STOP_PG
;
370 /* Install the Interrupt handler */
371 ret
= request_irq(dev
->irq
, __ei_interrupt
, 0, dev
->name
, dev
);
375 eth_hw_addr_set(dev
, SA_prom
);
377 netdev_dbg(dev
, "Found ethernet address: %pM\n", dev
->dev_addr
);
379 ei_local
->name
= "mcf8390";
380 ei_local
->tx_start_page
= start_page
;
381 ei_local
->stop_page
= stop_page
;
382 ei_local
->word16
= 1;
383 ei_local
->rx_start_page
= start_page
+ TX_PAGES
;
384 ei_local
->reset_8390
= mcf8390_reset_8390
;
385 ei_local
->block_input
= mcf8390_block_input
;
386 ei_local
->block_output
= mcf8390_block_output
;
387 ei_local
->get_8390_hdr
= mcf8390_get_8390_hdr
;
388 ei_local
->reg_offset
= offsets
;
390 dev
->netdev_ops
= &mcf8390_netdev_ops
;
391 __NS8390_init(dev
, 0);
392 ret
= register_netdev(dev
);
394 free_irq(dev
->irq
, dev
);
398 netdev_info(dev
, "addr=0x%08x irq=%d, Ethernet Address %pM\n",
399 addr
, dev
->irq
, dev
->dev_addr
);
403 static int mcf8390_probe(struct platform_device
*pdev
)
405 struct net_device
*dev
;
406 struct resource
*mem
;
407 resource_size_t msize
;
410 irq
= platform_get_irq(pdev
, 0);
414 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
416 dev_err(&pdev
->dev
, "no memory address specified?\n");
419 msize
= resource_size(mem
);
420 if (!request_mem_region(mem
->start
, msize
, pdev
->name
))
423 dev
= ____alloc_ei_netdev(0);
425 release_mem_region(mem
->start
, msize
);
429 SET_NETDEV_DEV(dev
, &pdev
->dev
);
430 platform_set_drvdata(pdev
, dev
);
433 dev
->base_addr
= mem
->start
;
435 ret
= mcf8390_init(dev
);
437 release_mem_region(mem
->start
, msize
);
444 static void mcf8390_remove(struct platform_device
*pdev
)
446 struct net_device
*dev
= platform_get_drvdata(pdev
);
447 struct resource
*mem
;
449 unregister_netdev(dev
);
450 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
451 release_mem_region(mem
->start
, resource_size(mem
));
455 static struct platform_driver mcf8390_drv
= {
459 .probe
= mcf8390_probe
,
460 .remove
= mcf8390_remove
,
463 module_platform_driver(mcf8390_drv
);
465 MODULE_DESCRIPTION("MCF8390 ColdFire NS8390 driver");
466 MODULE_AUTHOR("Greg Ungerer <gerg@uclinux.org>");
467 MODULE_LICENSE("GPL");
468 MODULE_ALIAS("platform:mcf8390");