blob: 2bb97d464689e9bcbe90db886a08a7f06ef15243 [file] [log] [blame]
Bryan Wue190d6b2007-07-17 14:43:44 +08001/*
2 * File: drivers/net/bfin_mac.c
3 * Based on:
4 * Maintainer:
5 * Bryan Wu <bryan.wu@analog.com>
6 *
7 * Original author:
8 * Luke Yang <luke.yang@analog.com>
9 *
10 * Created:
11 * Description:
12 *
13 * Modified:
14 * Copyright 2004-2006 Analog Devices Inc.
15 *
16 * Bugs: Enter bugs at http://blackfin.uclinux.org/
17 *
18 * This program is free software ; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation ; either version 2, or (at your option)
21 * any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program ; see the file COPYING.
30 * If not, write to the Free Software Foundation,
31 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 */
33
34#include <linux/init.h>
35#include <linux/module.h>
36#include <linux/kernel.h>
37#include <linux/sched.h>
38#include <linux/slab.h>
39#include <linux/delay.h>
40#include <linux/timer.h>
41#include <linux/errno.h>
42#include <linux/irq.h>
43#include <linux/io.h>
44#include <linux/ioport.h>
45#include <linux/crc32.h>
46#include <linux/device.h>
47#include <linux/spinlock.h>
48#include <linux/ethtool.h>
49#include <linux/mii.h>
50
51#include <linux/netdevice.h>
52#include <linux/etherdevice.h>
53#include <linux/skbuff.h>
54
55#include <linux/platform_device.h>
56#include <linux/netdevice.h>
57#include <linux/etherdevice.h>
58#include <linux/skbuff.h>
59
60#include <asm/dma.h>
61#include <linux/dma-mapping.h>
62
63#include <asm/blackfin.h>
64#include <asm/cacheflush.h>
65#include <asm/portmux.h>
66
67#include "bfin_mac.h"
68
69#define DRV_NAME "bfin_mac"
70#define DRV_VERSION "1.1"
71#define DRV_AUTHOR "Bryan Wu, Luke Yang"
72#define DRV_DESC "Blackfin BF53[67] on-chip Ethernet MAC driver"
73
74MODULE_AUTHOR(DRV_AUTHOR);
75MODULE_LICENSE("GPL");
76MODULE_DESCRIPTION(DRV_DESC);
77
78#if defined(CONFIG_BFIN_MAC_USE_L1)
79# define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size)
80# define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr)
81#else
82# define bfin_mac_alloc(dma_handle, size) \
83 dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
84# define bfin_mac_free(dma_handle, ptr) \
85 dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
86#endif
87
88#define PKT_BUF_SZ 1580
89
90#define MAX_TIMEOUT_CNT 500
91
92/* pointers to maintain transmit list */
93static struct net_dma_desc_tx *tx_list_head;
94static struct net_dma_desc_tx *tx_list_tail;
95static struct net_dma_desc_rx *rx_list_head;
96static struct net_dma_desc_rx *rx_list_tail;
97static struct net_dma_desc_rx *current_rx_ptr;
98static struct net_dma_desc_tx *current_tx_ptr;
99static struct net_dma_desc_tx *tx_desc;
100static struct net_dma_desc_rx *rx_desc;
101
102static void desc_list_free(void)
103{
104 struct net_dma_desc_rx *r;
105 struct net_dma_desc_tx *t;
106 int i;
107#if !defined(CONFIG_BFIN_MAC_USE_L1)
108 dma_addr_t dma_handle = 0;
109#endif
110
111 if (tx_desc) {
112 t = tx_list_head;
113 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
114 if (t) {
115 if (t->skb) {
116 dev_kfree_skb(t->skb);
117 t->skb = NULL;
118 }
119 t = t->next;
120 }
121 }
122 bfin_mac_free(dma_handle, tx_desc);
123 }
124
125 if (rx_desc) {
126 r = rx_list_head;
127 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
128 if (r) {
129 if (r->skb) {
130 dev_kfree_skb(r->skb);
131 r->skb = NULL;
132 }
133 r = r->next;
134 }
135 }
136 bfin_mac_free(dma_handle, rx_desc);
137 }
138}
139
140static int desc_list_init(void)
141{
142 int i;
143 struct sk_buff *new_skb;
144#if !defined(CONFIG_BFIN_MAC_USE_L1)
145 /*
146 * This dma_handle is useless in Blackfin dma_alloc_coherent().
147 * The real dma handler is the return value of dma_alloc_coherent().
148 */
149 dma_addr_t dma_handle;
150#endif
151
152 tx_desc = bfin_mac_alloc(&dma_handle,
153 sizeof(struct net_dma_desc_tx) *
154 CONFIG_BFIN_TX_DESC_NUM);
155 if (tx_desc == NULL)
156 goto init_error;
157
158 rx_desc = bfin_mac_alloc(&dma_handle,
159 sizeof(struct net_dma_desc_rx) *
160 CONFIG_BFIN_RX_DESC_NUM);
161 if (rx_desc == NULL)
162 goto init_error;
163
164 /* init tx_list */
165 tx_list_head = tx_list_tail = tx_desc;
166
167 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
168 struct net_dma_desc_tx *t = tx_desc + i;
169 struct dma_descriptor *a = &(t->desc_a);
170 struct dma_descriptor *b = &(t->desc_b);
171
172 /*
173 * disable DMA
174 * read from memory WNR = 0
175 * wordsize is 32 bits
176 * 6 half words is desc size
177 * large desc flow
178 */
179 a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
180 a->start_addr = (unsigned long)t->packet;
181 a->x_count = 0;
182 a->next_dma_desc = b;
183
184 /*
185 * enabled DMA
186 * write to memory WNR = 1
187 * wordsize is 32 bits
188 * disable interrupt
189 * 6 half words is desc size
190 * large desc flow
191 */
192 b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
193 b->start_addr = (unsigned long)(&(t->status));
194 b->x_count = 0;
195
196 t->skb = NULL;
197 tx_list_tail->desc_b.next_dma_desc = a;
198 tx_list_tail->next = t;
199 tx_list_tail = t;
200 }
201 tx_list_tail->next = tx_list_head; /* tx_list is a circle */
202 tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a);
203 current_tx_ptr = tx_list_head;
204
205 /* init rx_list */
206 rx_list_head = rx_list_tail = rx_desc;
207
208 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
209 struct net_dma_desc_rx *r = rx_desc + i;
210 struct dma_descriptor *a = &(r->desc_a);
211 struct dma_descriptor *b = &(r->desc_b);
212
213 /* allocate a new skb for next time receive */
214 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
215 if (!new_skb) {
216 printk(KERN_NOTICE DRV_NAME
217 ": init: low on mem - packet dropped\n");
218 goto init_error;
219 }
220 skb_reserve(new_skb, 2);
221 r->skb = new_skb;
222
223 /*
224 * enabled DMA
225 * write to memory WNR = 1
226 * wordsize is 32 bits
227 * disable interrupt
228 * 6 half words is desc size
229 * large desc flow
230 */
231 a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
232 /* since RXDWA is enabled */
233 a->start_addr = (unsigned long)new_skb->data - 2;
234 a->x_count = 0;
235 a->next_dma_desc = b;
236
237 /*
238 * enabled DMA
239 * write to memory WNR = 1
240 * wordsize is 32 bits
241 * enable interrupt
242 * 6 half words is desc size
243 * large desc flow
244 */
245 b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
246 NDSIZE_6 | DMAFLOW_LARGE;
247 b->start_addr = (unsigned long)(&(r->status));
248 b->x_count = 0;
249
250 rx_list_tail->desc_b.next_dma_desc = a;
251 rx_list_tail->next = r;
252 rx_list_tail = r;
253 }
254 rx_list_tail->next = rx_list_head; /* rx_list is a circle */
255 rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
256 current_rx_ptr = rx_list_head;
257
258 return 0;
259
260init_error:
261 desc_list_free();
262 printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
263 return -ENOMEM;
264}
265
266
267/*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
268
269/* Set FER regs to MUX in Ethernet pins */
270static int setup_pin_mux(int action)
271{
272#if defined(CONFIG_BFIN_MAC_RMII)
273 u16 pin_req[] = P_RMII0;
274#else
275 u16 pin_req[] = P_MII0;
276#endif
277
278 if (action) {
279 if (peripheral_request_list(pin_req, DRV_NAME)) {
280 printk(KERN_ERR DRV_NAME
281 ": Requesting Peripherals failed\n");
282 return -EFAULT;
283 }
284 } else
285 peripheral_free_list(pin_req);
286
287 return 0;
288}
289
290/* Wait until the previous MDC/MDIO transaction has completed */
291static void poll_mdc_done(void)
292{
293 int timeout_cnt = MAX_TIMEOUT_CNT;
294
295 /* poll the STABUSY bit */
296 while ((bfin_read_EMAC_STAADD()) & STABUSY) {
297 mdelay(10);
298 if (timeout_cnt-- < 0) {
299 printk(KERN_ERR DRV_NAME
300 ": wait MDC/MDIO transaction to complete timeout\n");
301 break;
302 }
303 }
304}
305
306/* Read an off-chip register in a PHY through the MDC/MDIO port */
307static u16 read_phy_reg(u16 PHYAddr, u16 RegAddr)
308{
309 poll_mdc_done();
310 /* read mode */
311 bfin_write_EMAC_STAADD(SET_PHYAD(PHYAddr) |
312 SET_REGAD(RegAddr) |
313 STABUSY);
314 poll_mdc_done();
315
316 return (u16) bfin_read_EMAC_STADAT();
317}
318
319/* Write an off-chip register in a PHY through the MDC/MDIO port */
320static void raw_write_phy_reg(u16 PHYAddr, u16 RegAddr, u32 Data)
321{
322 bfin_write_EMAC_STADAT(Data);
323
324 /* write mode */
325 bfin_write_EMAC_STAADD(SET_PHYAD(PHYAddr) |
326 SET_REGAD(RegAddr) |
327 STAOP |
328 STABUSY);
329
330 poll_mdc_done();
331}
332
333static void write_phy_reg(u16 PHYAddr, u16 RegAddr, u32 Data)
334{
335 poll_mdc_done();
336 raw_write_phy_reg(PHYAddr, RegAddr, Data);
337}
338
339/* set up the phy */
340static void bf537mac_setphy(struct net_device *dev)
341{
342 u16 phydat;
343 struct bf537mac_local *lp = netdev_priv(dev);
344
345 /* Program PHY registers */
346 pr_debug("start setting up phy\n");
347
348 /* issue a reset */
349 raw_write_phy_reg(lp->PhyAddr, PHYREG_MODECTL, 0x8000);
350
351 /* wait half a second */
352 msleep(500);
353
354 phydat = read_phy_reg(lp->PhyAddr, PHYREG_MODECTL);
355
356 /* advertise flow control supported */
357 phydat = read_phy_reg(lp->PhyAddr, PHYREG_ANAR);
358 phydat |= (1 << 10);
359 write_phy_reg(lp->PhyAddr, PHYREG_ANAR, phydat);
360
361 phydat = 0;
362 if (lp->Negotiate)
363 phydat |= 0x1000; /* enable auto negotiation */
364 else {
365 if (lp->FullDuplex)
366 phydat |= (1 << 8); /* full duplex */
367 else
368 phydat &= (~(1 << 8)); /* half duplex */
369
370 if (!lp->Port10)
371 phydat |= (1 << 13); /* 100 Mbps */
372 else
373 phydat &= (~(1 << 13)); /* 10 Mbps */
374 }
375
376 if (lp->Loopback)
377 phydat |= (1 << 14); /* enable TX->RX loopback */
378
379 write_phy_reg(lp->PhyAddr, PHYREG_MODECTL, phydat);
380 msleep(500);
381
382 phydat = read_phy_reg(lp->PhyAddr, PHYREG_MODECTL);
383 /* check for SMSC PHY */
384 if ((read_phy_reg(lp->PhyAddr, PHYREG_PHYID1) == 0x7) &&
385 ((read_phy_reg(lp->PhyAddr, PHYREG_PHYID2) & 0xfff0) == 0xC0A0)) {
386 /*
387 * we have SMSC PHY so reqest interrupt
388 * on link down condition
389 */
390
391 /* enable interrupts */
392 write_phy_reg(lp->PhyAddr, 30, 0x0ff);
393 }
394}
395
396/**************************************************************************/
397void setup_system_regs(struct net_device *dev)
398{
399 int phyaddr;
400 unsigned short sysctl, phydat;
401 u32 opmode;
402 struct bf537mac_local *lp = netdev_priv(dev);
403 int count = 0;
404
405 phyaddr = lp->PhyAddr;
406
407 /* Enable PHY output */
408 if (!(bfin_read_VR_CTL() & PHYCLKOE))
409 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
410
411 /* MDC = 2.5 MHz */
412 sysctl = SET_MDCDIV(24);
413 /* Odd word alignment for Receive Frame DMA word */
414 /* Configure checksum support and rcve frame word alignment */
415#if defined(BFIN_MAC_CSUM_OFFLOAD)
416 sysctl |= RXDWA | RXCKS;
417#else
418 sysctl |= RXDWA;
419#endif
420 bfin_write_EMAC_SYSCTL(sysctl);
421 /* auto negotiation on */
422 /* full duplex */
423 /* 100 Mbps */
424 phydat = PHY_ANEG_EN | PHY_DUPLEX | PHY_SPD_SET;
425 write_phy_reg(phyaddr, PHYREG_MODECTL, phydat);
426
427 /* test if full duplex supported */
428 do {
429 msleep(100);
430 phydat = read_phy_reg(phyaddr, PHYREG_MODESTAT);
431 if (count > 30) {
432 printk(KERN_NOTICE DRV_NAME ": Link is down\n");
433 printk(KERN_NOTICE DRV_NAME
434 "please check your network connection\n");
435 break;
436 }
437 count++;
438 } while (!(phydat & 0x0004));
439
440 phydat = read_phy_reg(phyaddr, PHYREG_ANLPAR);
441
442 if ((phydat & 0x0100) || (phydat & 0x0040)) {
443 opmode = FDMODE;
444 } else {
445 opmode = 0;
446 printk(KERN_INFO DRV_NAME
447 ": Network is set to half duplex\n");
448 }
449
450#if defined(CONFIG_BFIN_MAC_RMII)
451 opmode |= RMII; /* For Now only 100MBit are supported */
452#endif
453
454 bfin_write_EMAC_OPMODE(opmode);
455
456 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
457
458 /* Initialize the TX DMA channel registers */
459 bfin_write_DMA2_X_COUNT(0);
460 bfin_write_DMA2_X_MODIFY(4);
461 bfin_write_DMA2_Y_COUNT(0);
462 bfin_write_DMA2_Y_MODIFY(0);
463
464 /* Initialize the RX DMA channel registers */
465 bfin_write_DMA1_X_COUNT(0);
466 bfin_write_DMA1_X_MODIFY(4);
467 bfin_write_DMA1_Y_COUNT(0);
468 bfin_write_DMA1_Y_MODIFY(0);
469}
470
471void setup_mac_addr(u8 * mac_addr)
472{
473 u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
474 u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
475
476 /* this depends on a little-endian machine */
477 bfin_write_EMAC_ADDRLO(addr_low);
478 bfin_write_EMAC_ADDRHI(addr_hi);
479}
480
481static void adjust_tx_list(void)
482{
483 int timeout_cnt = MAX_TIMEOUT_CNT;
484
485 if (tx_list_head->status.status_word != 0
486 && current_tx_ptr != tx_list_head) {
487 goto adjust_head; /* released something, just return; */
488 }
489
490 /*
491 * if nothing released, check wait condition
492 * current's next can not be the head,
493 * otherwise the dma will not stop as we want
494 */
495 if (current_tx_ptr->next->next == tx_list_head) {
496 while (tx_list_head->status.status_word == 0) {
497 mdelay(10);
498 if (tx_list_head->status.status_word != 0
499 || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
500 goto adjust_head;
501 }
502 if (timeout_cnt-- < 0) {
503 printk(KERN_ERR DRV_NAME
504 ": wait for adjust tx list head timeout\n");
505 break;
506 }
507 }
508 if (tx_list_head->status.status_word != 0) {
509 goto adjust_head;
510 }
511 }
512
513 return;
514
515adjust_head:
516 do {
517 tx_list_head->desc_a.config &= ~DMAEN;
518 tx_list_head->status.status_word = 0;
519 if (tx_list_head->skb) {
520 dev_kfree_skb(tx_list_head->skb);
521 tx_list_head->skb = NULL;
522 } else {
523 printk(KERN_ERR DRV_NAME
524 ": no sk_buff in a transmitted frame!\n");
525 }
526 tx_list_head = tx_list_head->next;
527 } while (tx_list_head->status.status_word != 0
528 && current_tx_ptr != tx_list_head);
529 return;
530
531}
532
533static int bf537mac_hard_start_xmit(struct sk_buff *skb,
534 struct net_device *dev)
535{
536 struct bf537mac_local *lp = netdev_priv(dev);
537 unsigned int data;
538
539 current_tx_ptr->skb = skb;
540
541 /*
542 * Is skb->data always 16-bit aligned?
543 * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
544 */
545 if ((((unsigned int)(skb->data)) & 0x02) == 2) {
546 /* move skb->data to current_tx_ptr payload */
547 data = (unsigned int)(skb->data) - 2;
548 *((unsigned short *)data) = (unsigned short)(skb->len);
549 current_tx_ptr->desc_a.start_addr = (unsigned long)data;
550 /* this is important! */
551 blackfin_dcache_flush_range(data, (data + (skb->len)) + 2);
552
553 } else {
554 *((unsigned short *)(current_tx_ptr->packet)) =
555 (unsigned short)(skb->len);
556 memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
557 (skb->len));
558 current_tx_ptr->desc_a.start_addr =
559 (unsigned long)current_tx_ptr->packet;
560 if (current_tx_ptr->status.status_word != 0)
561 current_tx_ptr->status.status_word = 0;
562 blackfin_dcache_flush_range((unsigned int)current_tx_ptr->
563 packet,
564 (unsigned int)(current_tx_ptr->
565 packet + skb->len) +
566 2);
567 }
568
569 /* enable this packet's dma */
570 current_tx_ptr->desc_a.config |= DMAEN;
571
572 /* tx dma is running, just return */
573 if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
574 goto out;
575
576 /* tx dma is not running */
577 bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
578 /* dma enabled, read from memory, size is 6 */
579 bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
580 /* Turn on the EMAC tx */
581 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
582
583out:
584 adjust_tx_list();
585 current_tx_ptr = current_tx_ptr->next;
586 dev->trans_start = jiffies;
587 lp->stats.tx_packets++;
588 lp->stats.tx_bytes += (skb->len);
589 return 0;
590}
591
592static void bf537mac_rx(struct net_device *dev)
593{
594 struct sk_buff *skb, *new_skb;
595 struct bf537mac_local *lp = netdev_priv(dev);
596 unsigned short len;
597
598 /* allocate a new skb for next time receive */
599 skb = current_rx_ptr->skb;
600 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
601 if (!new_skb) {
602 printk(KERN_NOTICE DRV_NAME
603 ": rx: low on mem - packet dropped\n");
604 lp->stats.rx_dropped++;
605 goto out;
606 }
607 /* reserve 2 bytes for RXDWA padding */
608 skb_reserve(new_skb, 2);
609 current_rx_ptr->skb = new_skb;
610 current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
611
612 len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
613 skb_put(skb, len);
614 blackfin_dcache_invalidate_range((unsigned long)skb->head,
615 (unsigned long)skb->tail);
616
617 dev->last_rx = jiffies;
618 skb->dev = dev;
619 skb->protocol = eth_type_trans(skb, dev);
620#if defined(BFIN_MAC_CSUM_OFFLOAD)
621 skb->csum = current_rx_ptr->status.ip_payload_csum;
622 skb->ip_summed = CHECKSUM_PARTIAL;
623#endif
624
625 netif_rx(skb);
626 lp->stats.rx_packets++;
627 lp->stats.rx_bytes += len;
628 current_rx_ptr->status.status_word = 0x00000000;
629 current_rx_ptr = current_rx_ptr->next;
630
631out:
632 return;
633}
634
635/* interrupt routine to handle rx and error signal */
636static irqreturn_t bf537mac_interrupt(int irq, void *dev_id)
637{
638 struct net_device *dev = dev_id;
639 int number = 0;
640
641get_one_packet:
642 if (current_rx_ptr->status.status_word == 0) {
643 /* no more new packet received */
644 if (number == 0) {
645 if (current_rx_ptr->next->status.status_word != 0) {
646 current_rx_ptr = current_rx_ptr->next;
647 goto real_rx;
648 }
649 }
650 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
651 DMA_DONE | DMA_ERR);
652 return IRQ_HANDLED;
653 }
654
655real_rx:
656 bf537mac_rx(dev);
657 number++;
658 goto get_one_packet;
659}
660
661#ifdef CONFIG_NET_POLL_CONTROLLER
662static void bf537mac_poll(struct net_device *dev)
663{
664 disable_irq(IRQ_MAC_RX);
665 bf537mac_interrupt(IRQ_MAC_RX, dev);
666 enable_irq(IRQ_MAC_RX);
667}
668#endif /* CONFIG_NET_POLL_CONTROLLER */
669
670static void bf537mac_reset(void)
671{
672 unsigned int opmode;
673
674 opmode = bfin_read_EMAC_OPMODE();
675 opmode &= (~RE);
676 opmode &= (~TE);
677 /* Turn off the EMAC */
678 bfin_write_EMAC_OPMODE(opmode);
679}
680
681/*
682 * Enable Interrupts, Receive, and Transmit
683 */
684static int bf537mac_enable(struct net_device *dev)
685{
686 u32 opmode;
687
688 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
689
690 /* Set RX DMA */
691 bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
692 bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
693
694 /* Wait MII done */
695 poll_mdc_done();
696
697 /* We enable only RX here */
698 /* ASTP : Enable Automatic Pad Stripping
699 PR : Promiscuous Mode for test
700 PSF : Receive frames with total length less than 64 bytes.
701 FDMODE : Full Duplex Mode
702 LB : Internal Loopback for test
703 RE : Receiver Enable */
704 opmode = bfin_read_EMAC_OPMODE();
705 if (opmode & FDMODE)
706 opmode |= PSF;
707 else
708 opmode |= DRO | DC | PSF;
709 opmode |= RE;
710
711#if defined(CONFIG_BFIN_MAC_RMII)
712 opmode |= RMII; /* For Now only 100MBit are supported */
713#ifdef CONFIG_BF_REV_0_2
714 opmode |= TE;
715#endif
716#endif
717 /* Turn on the EMAC rx */
718 bfin_write_EMAC_OPMODE(opmode);
719
720 return 0;
721}
722
723/* Our watchdog timed out. Called by the networking layer */
724static void bf537mac_timeout(struct net_device *dev)
725{
726 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
727
728 bf537mac_reset();
729
730 /* reset tx queue */
731 tx_list_tail = tx_list_head->next;
732
733 bf537mac_enable(dev);
734
735 /* We can accept TX packets again */
736 dev->trans_start = jiffies;
737 netif_wake_queue(dev);
738}
739
740/*
741 * Get the current statistics.
742 * This may be called with the card open or closed.
743 */
744static struct net_device_stats *bf537mac_query_statistics(struct net_device
745 *dev)
746{
747 struct bf537mac_local *lp = netdev_priv(dev);
748
749 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
750
751 return &lp->stats;
752}
753
754/*
755 * This routine will, depending on the values passed to it,
756 * either make it accept multicast packets, go into
757 * promiscuous mode (for TCPDUMP and cousins) or accept
758 * a select set of multicast packets
759 */
760static void bf537mac_set_multicast_list(struct net_device *dev)
761{
762 u32 sysctl;
763
764 if (dev->flags & IFF_PROMISC) {
765 printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
766 sysctl = bfin_read_EMAC_OPMODE();
767 sysctl |= RAF;
768 bfin_write_EMAC_OPMODE(sysctl);
769 } else if (dev->flags & IFF_ALLMULTI || dev->mc_count) {
770 /* accept all multicast */
771 sysctl = bfin_read_EMAC_OPMODE();
772 sysctl |= PAM;
773 bfin_write_EMAC_OPMODE(sysctl);
774 } else {
775 /* clear promisc or multicast mode */
776 sysctl = bfin_read_EMAC_OPMODE();
777 sysctl &= ~(RAF | PAM);
778 bfin_write_EMAC_OPMODE(sysctl);
779 }
780}
781
782/*
783 * this puts the device in an inactive state
784 */
785static void bf537mac_shutdown(struct net_device *dev)
786{
787 /* Turn off the EMAC */
788 bfin_write_EMAC_OPMODE(0x00000000);
789 /* Turn off the EMAC RX DMA */
790 bfin_write_DMA1_CONFIG(0x0000);
791 bfin_write_DMA2_CONFIG(0x0000);
792}
793
794/*
795 * Open and Initialize the interface
796 *
797 * Set up everything, reset the card, etc..
798 */
799static int bf537mac_open(struct net_device *dev)
800{
Michael Hennerich4af4b842007-07-25 14:09:54 +0800801 int retval;
Bryan Wue190d6b2007-07-17 14:43:44 +0800802 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
803
804 /*
805 * Check that the address is valid. If its not, refuse
806 * to bring the device up. The user must specify an
807 * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
808 */
809 if (!is_valid_ether_addr(dev->dev_addr)) {
810 printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
811 return -EINVAL;
812 }
813
814 /* initial rx and tx list */
Michael Hennerich4af4b842007-07-25 14:09:54 +0800815 retval = desc_list_init();
816
817 if (retval)
818 return retval;
Bryan Wue190d6b2007-07-17 14:43:44 +0800819
820 bf537mac_setphy(dev);
821 setup_system_regs(dev);
822 bf537mac_reset();
823 bf537mac_enable(dev);
824
825 pr_debug("hardware init finished\n");
826 netif_start_queue(dev);
827 netif_carrier_on(dev);
828
829 return 0;
830}
831
832/*
833 *
834 * this makes the board clean up everything that it can
835 * and not talk to the outside world. Caused by
836 * an 'ifconfig ethX down'
837 */
838static int bf537mac_close(struct net_device *dev)
839{
840 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
841
842 netif_stop_queue(dev);
843 netif_carrier_off(dev);
844
845 /* clear everything */
846 bf537mac_shutdown(dev);
847
848 /* free the rx/tx buffers */
849 desc_list_free();
850
851 return 0;
852}
853
854static int __init bf537mac_probe(struct net_device *dev)
855{
856 struct bf537mac_local *lp = netdev_priv(dev);
857 int retval;
858
859 /* Grab the MAC address in the MAC */
860 *(__le32 *) (&(dev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
861 *(__le16 *) (&(dev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
862
863 /* probe mac */
864 /*todo: how to proble? which is revision_register */
865 bfin_write_EMAC_ADDRLO(0x12345678);
866 if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
867 pr_debug("can't detect bf537 mac!\n");
868 retval = -ENODEV;
869 goto err_out;
870 }
871
872 /* set the GPIO pins to Ethernet mode */
873 retval = setup_pin_mux(1);
874
875 if (retval)
876 return retval;
877
878 /*Is it valid? (Did bootloader initialize it?) */
879 if (!is_valid_ether_addr(dev->dev_addr)) {
880 /* Grab the MAC from the board somehow - this is done in the
881 arch/blackfin/mach-bf537/boards/eth_mac.c */
882 get_bf537_ether_addr(dev->dev_addr);
883 }
884
885 /* If still not valid, get a random one */
886 if (!is_valid_ether_addr(dev->dev_addr)) {
887 random_ether_addr(dev->dev_addr);
888 }
889
890 setup_mac_addr(dev->dev_addr);
891
892 /* Fill in the fields of the device structure with ethernet values. */
893 ether_setup(dev);
894
895 dev->open = bf537mac_open;
896 dev->stop = bf537mac_close;
897 dev->hard_start_xmit = bf537mac_hard_start_xmit;
898 dev->tx_timeout = bf537mac_timeout;
899 dev->get_stats = bf537mac_query_statistics;
900 dev->set_multicast_list = bf537mac_set_multicast_list;
901#ifdef CONFIG_NET_POLL_CONTROLLER
902 dev->poll_controller = bf537mac_poll;
903#endif
904
905 /* fill in some of the fields */
906 lp->version = 1;
907 lp->PhyAddr = 0x01;
908 lp->CLKIN = 25;
909 lp->FullDuplex = 0;
910 lp->Negotiate = 1;
911 lp->FlowControl = 0;
912 spin_lock_init(&lp->lock);
913
914 /* now, enable interrupts */
915 /* register irq handler */
916 if (request_irq
917 (IRQ_MAC_RX, bf537mac_interrupt, IRQF_DISABLED | IRQF_SHARED,
918 "BFIN537_MAC_RX", dev)) {
919 printk(KERN_WARNING DRV_NAME
920 ": Unable to attach BlackFin MAC RX interrupt\n");
921 return -EBUSY;
922 }
923
924 /* Enable PHY output early */
925 if (!(bfin_read_VR_CTL() & PHYCLKOE))
926 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
927
928 retval = register_netdev(dev);
929 if (retval == 0) {
930 /* now, print out the card info, in a short format.. */
931 printk(KERN_INFO "%s: Version %s, %s\n",
932 DRV_NAME, DRV_VERSION, DRV_DESC);
933 }
934
935err_out:
936 return retval;
937}
938
939static int bfin_mac_probe(struct platform_device *pdev)
940{
941 struct net_device *ndev;
942
943 ndev = alloc_etherdev(sizeof(struct bf537mac_local));
944 if (!ndev) {
945 printk(KERN_WARNING DRV_NAME ": could not allocate device\n");
946 return -ENOMEM;
947 }
948
949 SET_MODULE_OWNER(ndev);
950 SET_NETDEV_DEV(ndev, &pdev->dev);
951
952 platform_set_drvdata(pdev, ndev);
953
954 if (bf537mac_probe(ndev) != 0) {
955 platform_set_drvdata(pdev, NULL);
956 free_netdev(ndev);
957 printk(KERN_WARNING DRV_NAME ": not found\n");
958 return -ENODEV;
959 }
960
961 return 0;
962}
963
964static int bfin_mac_remove(struct platform_device *pdev)
965{
966 struct net_device *ndev = platform_get_drvdata(pdev);
967
968 platform_set_drvdata(pdev, NULL);
969
970 unregister_netdev(ndev);
971
972 free_irq(IRQ_MAC_RX, ndev);
973
974 free_netdev(ndev);
975
976 setup_pin_mux(0);
977
978 return 0;
979}
980
981static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t state)
982{
983 return 0;
984}
985
986static int bfin_mac_resume(struct platform_device *pdev)
987{
988 return 0;
989}
990
991static struct platform_driver bfin_mac_driver = {
992 .probe = bfin_mac_probe,
993 .remove = bfin_mac_remove,
994 .resume = bfin_mac_resume,
995 .suspend = bfin_mac_suspend,
996 .driver = {
997 .name = DRV_NAME,
998 },
999};
1000
1001static int __init bfin_mac_init(void)
1002{
1003 return platform_driver_register(&bfin_mac_driver);
1004}
1005
1006module_init(bfin_mac_init);
1007
1008static void __exit bfin_mac_cleanup(void)
1009{
1010 platform_driver_unregister(&bfin_mac_driver);
1011}
1012
1013module_exit(bfin_mac_cleanup);