blob: 90d77ec48fb62cb9ae738d21e5928004b99d5857 [file] [log] [blame]
Lennert Buytenhek1d22e052006-09-22 02:28:13 +02001/*
2 * EP93xx ethernet network device driver
3 * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
4 * Dedicated to Marija Kulikova.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
Lennert Buytenhek1d22e052006-09-22 02:28:13 +020012#include <linux/dma-mapping.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/netdevice.h>
16#include <linux/mii.h>
17#include <linux/etherdevice.h>
18#include <linux/ethtool.h>
19#include <linux/init.h>
20#include <linux/moduleparam.h>
21#include <linux/platform_device.h>
22#include <linux/delay.h>
23#include <asm/arch/ep93xx-regs.h>
24#include <asm/arch/platform.h>
25#include <asm/io.h>
26
27#define DRV_MODULE_NAME "ep93xx-eth"
28#define DRV_MODULE_VERSION "0.1"
29
30#define RX_QUEUE_ENTRIES 64
31#define TX_QUEUE_ENTRIES 8
32
33#define MAX_PKT_SIZE 2044
34#define PKT_BUF_SIZE 2048
35
36#define REG_RXCTL 0x0000
37#define REG_RXCTL_DEFAULT 0x00073800
38#define REG_TXCTL 0x0004
39#define REG_TXCTL_ENABLE 0x00000001
40#define REG_MIICMD 0x0010
41#define REG_MIICMD_READ 0x00008000
42#define REG_MIICMD_WRITE 0x00004000
43#define REG_MIIDATA 0x0014
44#define REG_MIISTS 0x0018
45#define REG_MIISTS_BUSY 0x00000001
46#define REG_SELFCTL 0x0020
47#define REG_SELFCTL_RESET 0x00000001
48#define REG_INTEN 0x0024
49#define REG_INTEN_TX 0x00000008
50#define REG_INTEN_RX 0x00000007
51#define REG_INTSTSP 0x0028
52#define REG_INTSTS_TX 0x00000008
53#define REG_INTSTS_RX 0x00000004
54#define REG_INTSTSC 0x002c
55#define REG_AFP 0x004c
56#define REG_INDAD0 0x0050
57#define REG_INDAD1 0x0051
58#define REG_INDAD2 0x0052
59#define REG_INDAD3 0x0053
60#define REG_INDAD4 0x0054
61#define REG_INDAD5 0x0055
62#define REG_GIINTMSK 0x0064
63#define REG_GIINTMSK_ENABLE 0x00008000
64#define REG_BMCTL 0x0080
65#define REG_BMCTL_ENABLE_TX 0x00000100
66#define REG_BMCTL_ENABLE_RX 0x00000001
67#define REG_BMSTS 0x0084
68#define REG_BMSTS_RX_ACTIVE 0x00000008
69#define REG_RXDQBADD 0x0090
70#define REG_RXDQBLEN 0x0094
71#define REG_RXDCURADD 0x0098
72#define REG_RXDENQ 0x009c
73#define REG_RXSTSQBADD 0x00a0
74#define REG_RXSTSQBLEN 0x00a4
75#define REG_RXSTSQCURADD 0x00a8
76#define REG_RXSTSENQ 0x00ac
77#define REG_TXDQBADD 0x00b0
78#define REG_TXDQBLEN 0x00b4
79#define REG_TXDQCURADD 0x00b8
80#define REG_TXDENQ 0x00bc
81#define REG_TXSTSQBADD 0x00c0
82#define REG_TXSTSQBLEN 0x00c4
83#define REG_TXSTSQCURADD 0x00c8
84#define REG_MAXFRMLEN 0x00e8
85
86struct ep93xx_rdesc
87{
88 u32 buf_addr;
89 u32 rdesc1;
90};
91
92#define RDESC1_NSOF 0x80000000
93#define RDESC1_BUFFER_INDEX 0x7fff0000
94#define RDESC1_BUFFER_LENGTH 0x0000ffff
95
96struct ep93xx_rstat
97{
98 u32 rstat0;
99 u32 rstat1;
100};
101
102#define RSTAT0_RFP 0x80000000
103#define RSTAT0_RWE 0x40000000
104#define RSTAT0_EOF 0x20000000
105#define RSTAT0_EOB 0x10000000
106#define RSTAT0_AM 0x00c00000
107#define RSTAT0_RX_ERR 0x00200000
108#define RSTAT0_OE 0x00100000
109#define RSTAT0_FE 0x00080000
110#define RSTAT0_RUNT 0x00040000
111#define RSTAT0_EDATA 0x00020000
112#define RSTAT0_CRCE 0x00010000
113#define RSTAT0_CRCI 0x00008000
114#define RSTAT0_HTI 0x00003f00
115#define RSTAT1_RFP 0x80000000
116#define RSTAT1_BUFFER_INDEX 0x7fff0000
117#define RSTAT1_FRAME_LENGTH 0x0000ffff
118
119struct ep93xx_tdesc
120{
121 u32 buf_addr;
122 u32 tdesc1;
123};
124
125#define TDESC1_EOF 0x80000000
126#define TDESC1_BUFFER_INDEX 0x7fff0000
127#define TDESC1_BUFFER_ABORT 0x00008000
128#define TDESC1_BUFFER_LENGTH 0x00000fff
129
130struct ep93xx_tstat
131{
132 u32 tstat0;
133};
134
135#define TSTAT0_TXFP 0x80000000
136#define TSTAT0_TXWE 0x40000000
137#define TSTAT0_FA 0x20000000
138#define TSTAT0_LCRS 0x10000000
139#define TSTAT0_OW 0x04000000
140#define TSTAT0_TXU 0x02000000
141#define TSTAT0_ECOLL 0x01000000
142#define TSTAT0_NCOLL 0x001f0000
143#define TSTAT0_BUFFER_INDEX 0x00007fff
144
145struct ep93xx_descs
146{
147 struct ep93xx_rdesc rdesc[RX_QUEUE_ENTRIES];
148 struct ep93xx_tdesc tdesc[TX_QUEUE_ENTRIES];
149 struct ep93xx_rstat rstat[RX_QUEUE_ENTRIES];
150 struct ep93xx_tstat tstat[TX_QUEUE_ENTRIES];
151};
152
153struct ep93xx_priv
154{
155 struct resource *res;
156 void *base_addr;
157 int irq;
158
159 struct ep93xx_descs *descs;
160 dma_addr_t descs_dma_addr;
161
162 void *rx_buf[RX_QUEUE_ENTRIES];
163 void *tx_buf[TX_QUEUE_ENTRIES];
164
165 spinlock_t rx_lock;
166 unsigned int rx_pointer;
167 unsigned int tx_clean_pointer;
168 unsigned int tx_pointer;
169 spinlock_t tx_pending_lock;
170 unsigned int tx_pending;
171
172 struct net_device_stats stats;
173
174 struct mii_if_info mii;
175 u8 mdc_divisor;
176};
177
178#define rdb(ep, off) __raw_readb((ep)->base_addr + (off))
179#define rdw(ep, off) __raw_readw((ep)->base_addr + (off))
180#define rdl(ep, off) __raw_readl((ep)->base_addr + (off))
181#define wrb(ep, off, val) __raw_writeb((val), (ep)->base_addr + (off))
182#define wrw(ep, off, val) __raw_writew((val), (ep)->base_addr + (off))
183#define wrl(ep, off, val) __raw_writel((val), (ep)->base_addr + (off))
184
185static int ep93xx_mdio_read(struct net_device *dev, int phy_id, int reg);
186
187static struct net_device_stats *ep93xx_get_stats(struct net_device *dev)
188{
189 struct ep93xx_priv *ep = netdev_priv(dev);
190 return &(ep->stats);
191}
192
193static int ep93xx_rx(struct net_device *dev, int *budget)
194{
195 struct ep93xx_priv *ep = netdev_priv(dev);
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200196 int rx_done;
197 int processed;
198
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200199 rx_done = 0;
200 processed = 0;
201 while (*budget > 0) {
202 int entry;
203 struct ep93xx_rstat *rstat;
204 u32 rstat0;
205 u32 rstat1;
206 int length;
207 struct sk_buff *skb;
208
209 entry = ep->rx_pointer;
210 rstat = ep->descs->rstat + entry;
Lennert Buytenhek2d38cab2006-10-30 19:52:31 +0100211
212 rstat0 = rstat->rstat0;
213 rstat1 = rstat->rstat1;
214 if (!(rstat0 & RSTAT0_RFP) || !(rstat1 & RSTAT1_RFP)) {
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200215 rx_done = 1;
216 break;
217 }
218
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200219 rstat->rstat0 = 0;
220 rstat->rstat1 = 0;
221
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200222 if (!(rstat0 & RSTAT0_EOF))
223 printk(KERN_CRIT "ep93xx_rx: not end-of-frame "
224 " %.8x %.8x\n", rstat0, rstat1);
225 if (!(rstat0 & RSTAT0_EOB))
226 printk(KERN_CRIT "ep93xx_rx: not end-of-buffer "
227 " %.8x %.8x\n", rstat0, rstat1);
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200228 if ((rstat1 & RSTAT1_BUFFER_INDEX) >> 16 != entry)
229 printk(KERN_CRIT "ep93xx_rx: entry mismatch "
230 " %.8x %.8x\n", rstat0, rstat1);
231
232 if (!(rstat0 & RSTAT0_RWE)) {
233 printk(KERN_NOTICE "ep93xx_rx: receive error "
234 " %.8x %.8x\n", rstat0, rstat1);
235
236 ep->stats.rx_errors++;
237 if (rstat0 & RSTAT0_OE)
238 ep->stats.rx_fifo_errors++;
239 if (rstat0 & RSTAT0_FE)
240 ep->stats.rx_frame_errors++;
241 if (rstat0 & (RSTAT0_RUNT | RSTAT0_EDATA))
242 ep->stats.rx_length_errors++;
243 if (rstat0 & RSTAT0_CRCE)
244 ep->stats.rx_crc_errors++;
245 goto err;
246 }
247
248 length = rstat1 & RSTAT1_FRAME_LENGTH;
249 if (length > MAX_PKT_SIZE) {
250 printk(KERN_NOTICE "ep93xx_rx: invalid length "
251 " %.8x %.8x\n", rstat0, rstat1);
252 goto err;
253 }
254
255 /* Strip FCS. */
256 if (rstat0 & RSTAT0_CRCI)
257 length -= 4;
258
259 skb = dev_alloc_skb(length + 2);
260 if (likely(skb != NULL)) {
261 skb->dev = dev;
262 skb_reserve(skb, 2);
263 dma_sync_single(NULL, ep->descs->rdesc[entry].buf_addr,
264 length, DMA_FROM_DEVICE);
265 eth_copy_and_sum(skb, ep->rx_buf[entry], length, 0);
266 skb_put(skb, length);
267 skb->protocol = eth_type_trans(skb, dev);
268
269 dev->last_rx = jiffies;
270
271 netif_receive_skb(skb);
272
273 ep->stats.rx_packets++;
274 ep->stats.rx_bytes += length;
275 } else {
276 ep->stats.rx_dropped++;
277 }
278
279err:
280 ep->rx_pointer = (entry + 1) & (RX_QUEUE_ENTRIES - 1);
281 processed++;
282 dev->quota--;
283 (*budget)--;
284 }
285
286 if (processed) {
287 wrw(ep, REG_RXDENQ, processed);
288 wrw(ep, REG_RXSTSENQ, processed);
289 }
290
291 return !rx_done;
292}
293
294static int ep93xx_have_more_rx(struct ep93xx_priv *ep)
295{
Lennert Buytenhek2d38cab2006-10-30 19:52:31 +0100296 struct ep93xx_rstat *rstat = ep->descs->rstat + ep->rx_pointer;
297 return !!((rstat->rstat0 & RSTAT0_RFP) && (rstat->rstat1 & RSTAT1_RFP));
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200298}
299
300static int ep93xx_poll(struct net_device *dev, int *budget)
301{
302 struct ep93xx_priv *ep = netdev_priv(dev);
303
304 /*
305 * @@@ Have to stop polling if device is downed while we
306 * are polling.
307 */
308
309poll_some_more:
310 if (ep93xx_rx(dev, budget))
311 return 1;
312
313 netif_rx_complete(dev);
314
315 spin_lock_irq(&ep->rx_lock);
316 wrl(ep, REG_INTEN, REG_INTEN_TX | REG_INTEN_RX);
317 if (ep93xx_have_more_rx(ep)) {
318 wrl(ep, REG_INTEN, REG_INTEN_TX);
319 wrl(ep, REG_INTSTSP, REG_INTSTS_RX);
320 spin_unlock_irq(&ep->rx_lock);
321
322 if (netif_rx_reschedule(dev, 0))
323 goto poll_some_more;
324
325 return 0;
326 }
327 spin_unlock_irq(&ep->rx_lock);
328
329 return 0;
330}
331
332static int ep93xx_xmit(struct sk_buff *skb, struct net_device *dev)
333{
334 struct ep93xx_priv *ep = netdev_priv(dev);
335 int entry;
336
Lennert Buytenhek79c356f2006-10-30 19:52:54 +0100337 if (unlikely(skb->len > MAX_PKT_SIZE)) {
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200338 ep->stats.tx_dropped++;
339 dev_kfree_skb(skb);
340 return NETDEV_TX_OK;
341 }
342
343 entry = ep->tx_pointer;
344 ep->tx_pointer = (ep->tx_pointer + 1) & (TX_QUEUE_ENTRIES - 1);
345
346 ep->descs->tdesc[entry].tdesc1 =
347 TDESC1_EOF | (entry << 16) | (skb->len & 0xfff);
348 skb_copy_and_csum_dev(skb, ep->tx_buf[entry]);
349 dma_sync_single(NULL, ep->descs->tdesc[entry].buf_addr,
350 skb->len, DMA_TO_DEVICE);
351 dev_kfree_skb(skb);
352
353 dev->trans_start = jiffies;
354
355 spin_lock_irq(&ep->tx_pending_lock);
356 ep->tx_pending++;
357 if (ep->tx_pending == TX_QUEUE_ENTRIES)
358 netif_stop_queue(dev);
359 spin_unlock_irq(&ep->tx_pending_lock);
360
361 wrl(ep, REG_TXDENQ, 1);
362
363 return NETDEV_TX_OK;
364}
365
366static void ep93xx_tx_complete(struct net_device *dev)
367{
368 struct ep93xx_priv *ep = netdev_priv(dev);
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200369 int wake;
370
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200371 wake = 0;
372
373 spin_lock(&ep->tx_pending_lock);
374 while (1) {
375 int entry;
376 struct ep93xx_tstat *tstat;
377 u32 tstat0;
378
379 entry = ep->tx_clean_pointer;
380 tstat = ep->descs->tstat + entry;
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200381
382 tstat0 = tstat->tstat0;
Lennert Buytenhek2d38cab2006-10-30 19:52:31 +0100383 if (!(tstat0 & TSTAT0_TXFP))
384 break;
385
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200386 tstat->tstat0 = 0;
387
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200388 if (tstat0 & TSTAT0_FA)
389 printk(KERN_CRIT "ep93xx_tx_complete: frame aborted "
390 " %.8x\n", tstat0);
391 if ((tstat0 & TSTAT0_BUFFER_INDEX) != entry)
392 printk(KERN_CRIT "ep93xx_tx_complete: entry mismatch "
393 " %.8x\n", tstat0);
394
395 if (tstat0 & TSTAT0_TXWE) {
396 int length = ep->descs->tdesc[entry].tdesc1 & 0xfff;
397
398 ep->stats.tx_packets++;
399 ep->stats.tx_bytes += length;
400 } else {
401 ep->stats.tx_errors++;
402 }
403
404 if (tstat0 & TSTAT0_OW)
405 ep->stats.tx_window_errors++;
406 if (tstat0 & TSTAT0_TXU)
407 ep->stats.tx_fifo_errors++;
408 ep->stats.collisions += (tstat0 >> 16) & 0x1f;
409
410 ep->tx_clean_pointer = (entry + 1) & (TX_QUEUE_ENTRIES - 1);
411 if (ep->tx_pending == TX_QUEUE_ENTRIES)
412 wake = 1;
413 ep->tx_pending--;
414 }
415 spin_unlock(&ep->tx_pending_lock);
416
417 if (wake)
418 netif_wake_queue(dev);
419}
420
David Howells7d12e782006-10-05 14:55:46 +0100421static irqreturn_t ep93xx_irq(int irq, void *dev_id)
Lennert Buytenhek1d22e052006-09-22 02:28:13 +0200422{
423 struct net_device *dev = dev_id;
424 struct ep93xx_priv *ep = netdev_priv(dev);
425 u32 status;
426
427 status = rdl(ep, REG_INTSTSC);
428 if (status == 0)
429 return IRQ_NONE;
430
431 if (status & REG_INTSTS_RX) {
432 spin_lock(&ep->rx_lock);
433 if (likely(__netif_rx_schedule_prep(dev))) {
434 wrl(ep, REG_INTEN, REG_INTEN_TX);
435 __netif_rx_schedule(dev);
436 }
437 spin_unlock(&ep->rx_lock);
438 }
439
440 if (status & REG_INTSTS_TX)
441 ep93xx_tx_complete(dev);
442
443 return IRQ_HANDLED;
444}
445
446static void ep93xx_free_buffers(struct ep93xx_priv *ep)
447{
448 int i;
449
450 for (i = 0; i < RX_QUEUE_ENTRIES; i += 2) {
451 dma_addr_t d;
452
453 d = ep->descs->rdesc[i].buf_addr;
454 if (d)
455 dma_unmap_single(NULL, d, PAGE_SIZE, DMA_FROM_DEVICE);
456
457 if (ep->rx_buf[i] != NULL)
458 free_page((unsigned long)ep->rx_buf[i]);
459 }
460
461 for (i = 0; i < TX_QUEUE_ENTRIES; i += 2) {
462 dma_addr_t d;
463
464 d = ep->descs->tdesc[i].buf_addr;
465 if (d)
466 dma_unmap_single(NULL, d, PAGE_SIZE, DMA_TO_DEVICE);
467
468 if (ep->tx_buf[i] != NULL)
469 free_page((unsigned long)ep->tx_buf[i]);
470 }
471
472 dma_free_coherent(NULL, sizeof(struct ep93xx_descs), ep->descs,
473 ep->descs_dma_addr);
474}
475
476/*
477 * The hardware enforces a sub-2K maximum packet size, so we put
478 * two buffers on every hardware page.
479 */
480static int ep93xx_alloc_buffers(struct ep93xx_priv *ep)
481{
482 int i;
483
484 ep->descs = dma_alloc_coherent(NULL, sizeof(struct ep93xx_descs),
485 &ep->descs_dma_addr, GFP_KERNEL | GFP_DMA);
486 if (ep->descs == NULL)
487 return 1;
488
489 for (i = 0; i < RX_QUEUE_ENTRIES; i += 2) {
490 void *page;
491 dma_addr_t d;
492
493 page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
494 if (page == NULL)
495 goto err;
496
497 d = dma_map_single(NULL, page, PAGE_SIZE, DMA_FROM_DEVICE);
498 if (dma_mapping_error(d)) {
499 free_page((unsigned long)page);
500 goto err;
501 }
502
503 ep->rx_buf[i] = page;
504 ep->descs->rdesc[i].buf_addr = d;
505 ep->descs->rdesc[i].rdesc1 = (i << 16) | PKT_BUF_SIZE;
506
507 ep->rx_buf[i + 1] = page + PKT_BUF_SIZE;
508 ep->descs->rdesc[i + 1].buf_addr = d + PKT_BUF_SIZE;
509 ep->descs->rdesc[i + 1].rdesc1 = ((i + 1) << 16) | PKT_BUF_SIZE;
510 }
511
512 for (i = 0; i < TX_QUEUE_ENTRIES; i += 2) {
513 void *page;
514 dma_addr_t d;
515
516 page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
517 if (page == NULL)
518 goto err;
519
520 d = dma_map_single(NULL, page, PAGE_SIZE, DMA_TO_DEVICE);
521 if (dma_mapping_error(d)) {
522 free_page((unsigned long)page);
523 goto err;
524 }
525
526 ep->tx_buf[i] = page;
527 ep->descs->tdesc[i].buf_addr = d;
528
529 ep->tx_buf[i + 1] = page + PKT_BUF_SIZE;
530 ep->descs->tdesc[i + 1].buf_addr = d + PKT_BUF_SIZE;
531 }
532
533 return 0;
534
535err:
536 ep93xx_free_buffers(ep);
537 return 1;
538}
539
540static int ep93xx_start_hw(struct net_device *dev)
541{
542 struct ep93xx_priv *ep = netdev_priv(dev);
543 unsigned long addr;
544 int i;
545
546 wrl(ep, REG_SELFCTL, REG_SELFCTL_RESET);
547 for (i = 0; i < 10; i++) {
548 if ((rdl(ep, REG_SELFCTL) & REG_SELFCTL_RESET) == 0)
549 break;
550 msleep(1);
551 }
552
553 if (i == 10) {
554 printk(KERN_CRIT DRV_MODULE_NAME ": hw failed to reset\n");
555 return 1;
556 }
557
558 wrl(ep, REG_SELFCTL, ((ep->mdc_divisor - 1) << 9));
559
560 /* Does the PHY support preamble suppress? */
561 if ((ep93xx_mdio_read(dev, ep->mii.phy_id, MII_BMSR) & 0x0040) != 0)
562 wrl(ep, REG_SELFCTL, ((ep->mdc_divisor - 1) << 9) | (1 << 8));
563
564 /* Receive descriptor ring. */
565 addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, rdesc);
566 wrl(ep, REG_RXDQBADD, addr);
567 wrl(ep, REG_RXDCURADD, addr);
568 wrw(ep, REG_RXDQBLEN, RX_QUEUE_ENTRIES * sizeof(struct ep93xx_rdesc));
569
570 /* Receive status ring. */
571 addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, rstat);
572 wrl(ep, REG_RXSTSQBADD, addr);
573 wrl(ep, REG_RXSTSQCURADD, addr);
574 wrw(ep, REG_RXSTSQBLEN, RX_QUEUE_ENTRIES * sizeof(struct ep93xx_rstat));
575
576 /* Transmit descriptor ring. */
577 addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, tdesc);
578 wrl(ep, REG_TXDQBADD, addr);
579 wrl(ep, REG_TXDQCURADD, addr);
580 wrw(ep, REG_TXDQBLEN, TX_QUEUE_ENTRIES * sizeof(struct ep93xx_tdesc));
581
582 /* Transmit status ring. */
583 addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, tstat);
584 wrl(ep, REG_TXSTSQBADD, addr);
585 wrl(ep, REG_TXSTSQCURADD, addr);
586 wrw(ep, REG_TXSTSQBLEN, TX_QUEUE_ENTRIES * sizeof(struct ep93xx_tstat));
587
588 wrl(ep, REG_BMCTL, REG_BMCTL_ENABLE_TX | REG_BMCTL_ENABLE_RX);
589 wrl(ep, REG_INTEN, REG_INTEN_TX | REG_INTEN_RX);
590 wrl(ep, REG_GIINTMSK, 0);
591
592 for (i = 0; i < 10; i++) {
593 if ((rdl(ep, REG_BMSTS) & REG_BMSTS_RX_ACTIVE) != 0)
594 break;
595 msleep(1);
596 }
597
598 if (i == 10) {
599 printk(KERN_CRIT DRV_MODULE_NAME ": hw failed to start\n");
600 return 1;
601 }
602
603 wrl(ep, REG_RXDENQ, RX_QUEUE_ENTRIES);
604 wrl(ep, REG_RXSTSENQ, RX_QUEUE_ENTRIES);
605
606 wrb(ep, REG_INDAD0, dev->dev_addr[0]);
607 wrb(ep, REG_INDAD1, dev->dev_addr[1]);
608 wrb(ep, REG_INDAD2, dev->dev_addr[2]);
609 wrb(ep, REG_INDAD3, dev->dev_addr[3]);
610 wrb(ep, REG_INDAD4, dev->dev_addr[4]);
611 wrb(ep, REG_INDAD5, dev->dev_addr[5]);
612 wrl(ep, REG_AFP, 0);
613
614 wrl(ep, REG_MAXFRMLEN, (MAX_PKT_SIZE << 16) | MAX_PKT_SIZE);
615
616 wrl(ep, REG_RXCTL, REG_RXCTL_DEFAULT);
617 wrl(ep, REG_TXCTL, REG_TXCTL_ENABLE);
618
619 return 0;
620}
621
622static void ep93xx_stop_hw(struct net_device *dev)
623{
624 struct ep93xx_priv *ep = netdev_priv(dev);
625 int i;
626
627 wrl(ep, REG_SELFCTL, REG_SELFCTL_RESET);
628 for (i = 0; i < 10; i++) {
629 if ((rdl(ep, REG_SELFCTL) & REG_SELFCTL_RESET) == 0)
630 break;
631 msleep(1);
632 }
633
634 if (i == 10)
635 printk(KERN_CRIT DRV_MODULE_NAME ": hw failed to reset\n");
636}
637
638static int ep93xx_open(struct net_device *dev)
639{
640 struct ep93xx_priv *ep = netdev_priv(dev);
641 int err;
642
643 if (ep93xx_alloc_buffers(ep))
644 return -ENOMEM;
645
646 if (is_zero_ether_addr(dev->dev_addr)) {
647 random_ether_addr(dev->dev_addr);
648 printk(KERN_INFO "%s: generated random MAC address "
649 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x.\n", dev->name,
650 dev->dev_addr[0], dev->dev_addr[1],
651 dev->dev_addr[2], dev->dev_addr[3],
652 dev->dev_addr[4], dev->dev_addr[5]);
653 }
654
655 if (ep93xx_start_hw(dev)) {
656 ep93xx_free_buffers(ep);
657 return -EIO;
658 }
659
660 spin_lock_init(&ep->rx_lock);
661 ep->rx_pointer = 0;
662 ep->tx_clean_pointer = 0;
663 ep->tx_pointer = 0;
664 spin_lock_init(&ep->tx_pending_lock);
665 ep->tx_pending = 0;
666
667 err = request_irq(ep->irq, ep93xx_irq, IRQF_SHARED, dev->name, dev);
668 if (err) {
669 ep93xx_stop_hw(dev);
670 ep93xx_free_buffers(ep);
671 return err;
672 }
673
674 wrl(ep, REG_GIINTMSK, REG_GIINTMSK_ENABLE);
675
676 netif_start_queue(dev);
677
678 return 0;
679}
680
681static int ep93xx_close(struct net_device *dev)
682{
683 struct ep93xx_priv *ep = netdev_priv(dev);
684
685 netif_stop_queue(dev);
686
687 wrl(ep, REG_GIINTMSK, 0);
688 free_irq(ep->irq, dev);
689 ep93xx_stop_hw(dev);
690 ep93xx_free_buffers(ep);
691
692 return 0;
693}
694
695static int ep93xx_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
696{
697 struct ep93xx_priv *ep = netdev_priv(dev);
698 struct mii_ioctl_data *data = if_mii(ifr);
699
700 return generic_mii_ioctl(&ep->mii, data, cmd, NULL);
701}
702
703static int ep93xx_mdio_read(struct net_device *dev, int phy_id, int reg)
704{
705 struct ep93xx_priv *ep = netdev_priv(dev);
706 int data;
707 int i;
708
709 wrl(ep, REG_MIICMD, REG_MIICMD_READ | (phy_id << 5) | reg);
710
711 for (i = 0; i < 10; i++) {
712 if ((rdl(ep, REG_MIISTS) & REG_MIISTS_BUSY) == 0)
713 break;
714 msleep(1);
715 }
716
717 if (i == 10) {
718 printk(KERN_INFO DRV_MODULE_NAME ": mdio read timed out\n");
719 data = 0xffff;
720 } else {
721 data = rdl(ep, REG_MIIDATA);
722 }
723
724 return data;
725}
726
727static void ep93xx_mdio_write(struct net_device *dev, int phy_id, int reg, int data)
728{
729 struct ep93xx_priv *ep = netdev_priv(dev);
730 int i;
731
732 wrl(ep, REG_MIIDATA, data);
733 wrl(ep, REG_MIICMD, REG_MIICMD_WRITE | (phy_id << 5) | reg);
734
735 for (i = 0; i < 10; i++) {
736 if ((rdl(ep, REG_MIISTS) & REG_MIISTS_BUSY) == 0)
737 break;
738 msleep(1);
739 }
740
741 if (i == 10)
742 printk(KERN_INFO DRV_MODULE_NAME ": mdio write timed out\n");
743}
744
745static void ep93xx_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
746{
747 strcpy(info->driver, DRV_MODULE_NAME);
748 strcpy(info->version, DRV_MODULE_VERSION);
749}
750
751static int ep93xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
752{
753 struct ep93xx_priv *ep = netdev_priv(dev);
754 return mii_ethtool_gset(&ep->mii, cmd);
755}
756
757static int ep93xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
758{
759 struct ep93xx_priv *ep = netdev_priv(dev);
760 return mii_ethtool_sset(&ep->mii, cmd);
761}
762
763static int ep93xx_nway_reset(struct net_device *dev)
764{
765 struct ep93xx_priv *ep = netdev_priv(dev);
766 return mii_nway_restart(&ep->mii);
767}
768
769static u32 ep93xx_get_link(struct net_device *dev)
770{
771 struct ep93xx_priv *ep = netdev_priv(dev);
772 return mii_link_ok(&ep->mii);
773}
774
775static struct ethtool_ops ep93xx_ethtool_ops = {
776 .get_drvinfo = ep93xx_get_drvinfo,
777 .get_settings = ep93xx_get_settings,
778 .set_settings = ep93xx_set_settings,
779 .nway_reset = ep93xx_nway_reset,
780 .get_link = ep93xx_get_link,
781};
782
783struct net_device *ep93xx_dev_alloc(struct ep93xx_eth_data *data)
784{
785 struct net_device *dev;
786 struct ep93xx_priv *ep;
787
788 dev = alloc_etherdev(sizeof(struct ep93xx_priv));
789 if (dev == NULL)
790 return NULL;
791 ep = netdev_priv(dev);
792
793 memcpy(dev->dev_addr, data->dev_addr, ETH_ALEN);
794
795 dev->get_stats = ep93xx_get_stats;
796 dev->ethtool_ops = &ep93xx_ethtool_ops;
797 dev->poll = ep93xx_poll;
798 dev->hard_start_xmit = ep93xx_xmit;
799 dev->open = ep93xx_open;
800 dev->stop = ep93xx_close;
801 dev->do_ioctl = ep93xx_ioctl;
802
803 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
804 dev->weight = 64;
805
806 return dev;
807}
808
809
810static int ep93xx_eth_remove(struct platform_device *pdev)
811{
812 struct net_device *dev;
813 struct ep93xx_priv *ep;
814
815 dev = platform_get_drvdata(pdev);
816 if (dev == NULL)
817 return 0;
818 platform_set_drvdata(pdev, NULL);
819
820 ep = netdev_priv(dev);
821
822 /* @@@ Force down. */
823 unregister_netdev(dev);
824 ep93xx_free_buffers(ep);
825
826 if (ep->base_addr != NULL)
827 iounmap(ep->base_addr);
828
829 if (ep->res != NULL) {
830 release_resource(ep->res);
831 kfree(ep->res);
832 }
833
834 free_netdev(dev);
835
836 return 0;
837}
838
839static int ep93xx_eth_probe(struct platform_device *pdev)
840{
841 struct ep93xx_eth_data *data;
842 struct net_device *dev;
843 struct ep93xx_priv *ep;
844 int err;
845
846 data = pdev->dev.platform_data;
847 if (pdev == NULL)
848 return -ENODEV;
849
850 dev = ep93xx_dev_alloc(data);
851 if (dev == NULL) {
852 err = -ENOMEM;
853 goto err_out;
854 }
855 ep = netdev_priv(dev);
856
857 platform_set_drvdata(pdev, dev);
858
859 ep->res = request_mem_region(pdev->resource[0].start,
860 pdev->resource[0].end - pdev->resource[0].start + 1,
861 pdev->dev.bus_id);
862 if (ep->res == NULL) {
863 dev_err(&pdev->dev, "Could not reserve memory region\n");
864 err = -ENOMEM;
865 goto err_out;
866 }
867
868 ep->base_addr = ioremap(pdev->resource[0].start,
869 pdev->resource[0].end - pdev->resource[0].start);
870 if (ep->base_addr == NULL) {
871 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
872 err = -EIO;
873 goto err_out;
874 }
875 ep->irq = pdev->resource[1].start;
876
877 ep->mii.phy_id = data->phy_id;
878 ep->mii.phy_id_mask = 0x1f;
879 ep->mii.reg_num_mask = 0x1f;
880 ep->mii.dev = dev;
881 ep->mii.mdio_read = ep93xx_mdio_read;
882 ep->mii.mdio_write = ep93xx_mdio_write;
883 ep->mdc_divisor = 40; /* Max HCLK 100 MHz, min MDIO clk 2.5 MHz. */
884
885 err = register_netdev(dev);
886 if (err) {
887 dev_err(&pdev->dev, "Failed to register netdev\n");
888 goto err_out;
889 }
890
891 printk(KERN_INFO "%s: ep93xx on-chip ethernet, IRQ %d, "
892 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x.\n", dev->name,
893 ep->irq, data->dev_addr[0], data->dev_addr[1],
894 data->dev_addr[2], data->dev_addr[3],
895 data->dev_addr[4], data->dev_addr[5]);
896
897 return 0;
898
899err_out:
900 ep93xx_eth_remove(pdev);
901 return err;
902}
903
904
905static struct platform_driver ep93xx_eth_driver = {
906 .probe = ep93xx_eth_probe,
907 .remove = ep93xx_eth_remove,
908 .driver = {
909 .name = "ep93xx-eth",
910 },
911};
912
913static int __init ep93xx_eth_init_module(void)
914{
915 printk(KERN_INFO DRV_MODULE_NAME " version " DRV_MODULE_VERSION " loading\n");
916 return platform_driver_register(&ep93xx_eth_driver);
917}
918
919static void __exit ep93xx_eth_cleanup_module(void)
920{
921 platform_driver_unregister(&ep93xx_eth_driver);
922}
923
924module_init(ep93xx_eth_init_module);
925module_exit(ep93xx_eth_cleanup_module);
926MODULE_LICENSE("GPL");