blob: 5d962b86ee2266e190014de8e080a1261f0b8cf1 [file] [log] [blame]
Bryan Wue190d6b2007-07-17 14:43:44 +08001/*
Mike Frysinger2fb9d6f2008-01-30 16:52:24 +08002 * Blackfin On-Chip MAC Driver
Bryan Wue190d6b2007-07-17 14:43:44 +08003 *
Mike Frysinger2fb9d6f2008-01-30 16:52:24 +08004 * Copyright 2004-2007 Analog Devices Inc.
Bryan Wue190d6b2007-07-17 14:43:44 +08005 *
Mike Frysinger2fb9d6f2008-01-30 16:52:24 +08006 * Enter bugs at http://blackfin.uclinux.org/
Bryan Wue190d6b2007-07-17 14:43:44 +08007 *
Mike Frysinger2fb9d6f2008-01-30 16:52:24 +08008 * Licensed under the GPL-2 or later.
Bryan Wue190d6b2007-07-17 14:43:44 +08009 */
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/slab.h>
16#include <linux/delay.h>
17#include <linux/timer.h>
18#include <linux/errno.h>
19#include <linux/irq.h>
20#include <linux/io.h>
21#include <linux/ioport.h>
22#include <linux/crc32.h>
23#include <linux/device.h>
24#include <linux/spinlock.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080025#include <linux/mii.h>
Bryan Wu4ae5a3a2007-09-19 23:37:36 +080026#include <linux/phy.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080027#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
Bryan Wu679dce32008-04-25 11:53:11 +080029#include <linux/ethtool.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080030#include <linux/skbuff.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080031#include <linux/platform_device.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080032
33#include <asm/dma.h>
34#include <linux/dma-mapping.h>
35
Barry Songfe92afe2010-05-17 17:19:40 -070036#include <asm/div64.h>
Mike Frysinger98f672c2010-01-18 21:14:12 +000037#include <asm/dpmc.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080038#include <asm/blackfin.h>
39#include <asm/cacheflush.h>
40#include <asm/portmux.h>
41
42#include "bfin_mac.h"
43
44#define DRV_NAME "bfin_mac"
45#define DRV_VERSION "1.1"
46#define DRV_AUTHOR "Bryan Wu, Luke Yang"
Bryan Wu7ef0a7e2008-04-25 11:53:10 +080047#define DRV_DESC "Blackfin on-chip Ethernet MAC driver"
Bryan Wue190d6b2007-07-17 14:43:44 +080048
49MODULE_AUTHOR(DRV_AUTHOR);
50MODULE_LICENSE("GPL");
51MODULE_DESCRIPTION(DRV_DESC);
Kay Sievers72abb462008-04-18 13:50:44 -070052MODULE_ALIAS("platform:bfin_mac");
Bryan Wue190d6b2007-07-17 14:43:44 +080053
54#if defined(CONFIG_BFIN_MAC_USE_L1)
55# define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size)
56# define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr)
57#else
58# define bfin_mac_alloc(dma_handle, size) \
59 dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
60# define bfin_mac_free(dma_handle, ptr) \
61 dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
62#endif
63
64#define PKT_BUF_SZ 1580
65
66#define MAX_TIMEOUT_CNT 500
67
68/* pointers to maintain transmit list */
69static struct net_dma_desc_tx *tx_list_head;
70static struct net_dma_desc_tx *tx_list_tail;
71static struct net_dma_desc_rx *rx_list_head;
72static struct net_dma_desc_rx *rx_list_tail;
73static struct net_dma_desc_rx *current_rx_ptr;
74static struct net_dma_desc_tx *current_tx_ptr;
75static struct net_dma_desc_tx *tx_desc;
76static struct net_dma_desc_rx *rx_desc;
77
Bryan Wu7ef0a7e2008-04-25 11:53:10 +080078#if defined(CONFIG_BFIN_MAC_RMII)
79static u16 pin_req[] = P_RMII0;
80#else
81static u16 pin_req[] = P_MII0;
82#endif
83
84static void bfin_mac_disable(void);
85static void bfin_mac_enable(void);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +080086
Bryan Wue190d6b2007-07-17 14:43:44 +080087static void desc_list_free(void)
88{
89 struct net_dma_desc_rx *r;
90 struct net_dma_desc_tx *t;
91 int i;
92#if !defined(CONFIG_BFIN_MAC_USE_L1)
93 dma_addr_t dma_handle = 0;
94#endif
95
96 if (tx_desc) {
97 t = tx_list_head;
98 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
99 if (t) {
100 if (t->skb) {
101 dev_kfree_skb(t->skb);
102 t->skb = NULL;
103 }
104 t = t->next;
105 }
106 }
107 bfin_mac_free(dma_handle, tx_desc);
108 }
109
110 if (rx_desc) {
111 r = rx_list_head;
112 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
113 if (r) {
114 if (r->skb) {
115 dev_kfree_skb(r->skb);
116 r->skb = NULL;
117 }
118 r = r->next;
119 }
120 }
121 bfin_mac_free(dma_handle, rx_desc);
122 }
123}
124
125static int desc_list_init(void)
126{
127 int i;
128 struct sk_buff *new_skb;
129#if !defined(CONFIG_BFIN_MAC_USE_L1)
130 /*
131 * This dma_handle is useless in Blackfin dma_alloc_coherent().
132 * The real dma handler is the return value of dma_alloc_coherent().
133 */
134 dma_addr_t dma_handle;
135#endif
136
137 tx_desc = bfin_mac_alloc(&dma_handle,
138 sizeof(struct net_dma_desc_tx) *
139 CONFIG_BFIN_TX_DESC_NUM);
140 if (tx_desc == NULL)
141 goto init_error;
142
143 rx_desc = bfin_mac_alloc(&dma_handle,
144 sizeof(struct net_dma_desc_rx) *
145 CONFIG_BFIN_RX_DESC_NUM);
146 if (rx_desc == NULL)
147 goto init_error;
148
149 /* init tx_list */
150 tx_list_head = tx_list_tail = tx_desc;
151
152 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
153 struct net_dma_desc_tx *t = tx_desc + i;
154 struct dma_descriptor *a = &(t->desc_a);
155 struct dma_descriptor *b = &(t->desc_b);
156
157 /*
158 * disable DMA
159 * read from memory WNR = 0
160 * wordsize is 32 bits
161 * 6 half words is desc size
162 * large desc flow
163 */
164 a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
165 a->start_addr = (unsigned long)t->packet;
166 a->x_count = 0;
167 a->next_dma_desc = b;
168
169 /*
170 * enabled DMA
171 * write to memory WNR = 1
172 * wordsize is 32 bits
173 * disable interrupt
174 * 6 half words is desc size
175 * large desc flow
176 */
177 b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
178 b->start_addr = (unsigned long)(&(t->status));
179 b->x_count = 0;
180
181 t->skb = NULL;
182 tx_list_tail->desc_b.next_dma_desc = a;
183 tx_list_tail->next = t;
184 tx_list_tail = t;
185 }
186 tx_list_tail->next = tx_list_head; /* tx_list is a circle */
187 tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a);
188 current_tx_ptr = tx_list_head;
189
190 /* init rx_list */
191 rx_list_head = rx_list_tail = rx_desc;
192
193 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
194 struct net_dma_desc_rx *r = rx_desc + i;
195 struct dma_descriptor *a = &(r->desc_a);
196 struct dma_descriptor *b = &(r->desc_b);
197
198 /* allocate a new skb for next time receive */
Michael Hennerich015dac82009-05-29 03:41:15 +0000199 new_skb = dev_alloc_skb(PKT_BUF_SZ + NET_IP_ALIGN);
Bryan Wue190d6b2007-07-17 14:43:44 +0800200 if (!new_skb) {
201 printk(KERN_NOTICE DRV_NAME
202 ": init: low on mem - packet dropped\n");
203 goto init_error;
204 }
Michael Hennerich015dac82009-05-29 03:41:15 +0000205 skb_reserve(new_skb, NET_IP_ALIGN);
Sonic Zhangf6e1e4f2010-05-10 05:39:08 +0000206 /* Invidate the data cache of skb->data range when it is write back
207 * cache. It will prevent overwritting the new data from DMA
208 */
209 blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
210 (unsigned long)new_skb->end);
Bryan Wue190d6b2007-07-17 14:43:44 +0800211 r->skb = new_skb;
212
213 /*
214 * enabled DMA
215 * write to memory WNR = 1
216 * wordsize is 32 bits
217 * disable interrupt
218 * 6 half words is desc size
219 * large desc flow
220 */
221 a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
222 /* since RXDWA is enabled */
223 a->start_addr = (unsigned long)new_skb->data - 2;
224 a->x_count = 0;
225 a->next_dma_desc = b;
226
227 /*
228 * enabled DMA
229 * write to memory WNR = 1
230 * wordsize is 32 bits
231 * enable interrupt
232 * 6 half words is desc size
233 * large desc flow
234 */
235 b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
236 NDSIZE_6 | DMAFLOW_LARGE;
237 b->start_addr = (unsigned long)(&(r->status));
238 b->x_count = 0;
239
240 rx_list_tail->desc_b.next_dma_desc = a;
241 rx_list_tail->next = r;
242 rx_list_tail = r;
243 }
244 rx_list_tail->next = rx_list_head; /* rx_list is a circle */
245 rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
246 current_rx_ptr = rx_list_head;
247
248 return 0;
249
250init_error:
251 desc_list_free();
252 printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
253 return -ENOMEM;
254}
255
256
257/*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
258
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800259/*
260 * MII operations
261 */
Bryan Wue190d6b2007-07-17 14:43:44 +0800262/* Wait until the previous MDC/MDIO transaction has completed */
Adrian Bunk0ed0563e2008-10-12 21:15:17 -0700263static void bfin_mdio_poll(void)
Bryan Wue190d6b2007-07-17 14:43:44 +0800264{
265 int timeout_cnt = MAX_TIMEOUT_CNT;
266
267 /* poll the STABUSY bit */
268 while ((bfin_read_EMAC_STAADD()) & STABUSY) {
Bryan Wu6db9e462008-01-30 16:52:21 +0800269 udelay(1);
Bryan Wue190d6b2007-07-17 14:43:44 +0800270 if (timeout_cnt-- < 0) {
271 printk(KERN_ERR DRV_NAME
272 ": wait MDC/MDIO transaction to complete timeout\n");
273 break;
274 }
275 }
276}
277
278/* Read an off-chip register in a PHY through the MDC/MDIO port */
Adrian Bunk0ed0563e2008-10-12 21:15:17 -0700279static int bfin_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
Bryan Wue190d6b2007-07-17 14:43:44 +0800280{
Adrian Bunk0ed0563e2008-10-12 21:15:17 -0700281 bfin_mdio_poll();
Bryan Wue190d6b2007-07-17 14:43:44 +0800282
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800283 /* read mode */
284 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
285 SET_REGAD((u16) regnum) |
286 STABUSY);
287
Adrian Bunk0ed0563e2008-10-12 21:15:17 -0700288 bfin_mdio_poll();
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800289
290 return (int) bfin_read_EMAC_STADAT();
Bryan Wue190d6b2007-07-17 14:43:44 +0800291}
292
293/* Write an off-chip register in a PHY through the MDC/MDIO port */
Adrian Bunk0ed0563e2008-10-12 21:15:17 -0700294static int bfin_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
295 u16 value)
Bryan Wue190d6b2007-07-17 14:43:44 +0800296{
Adrian Bunk0ed0563e2008-10-12 21:15:17 -0700297 bfin_mdio_poll();
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800298
299 bfin_write_EMAC_STADAT((u32) value);
Bryan Wue190d6b2007-07-17 14:43:44 +0800300
301 /* write mode */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800302 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
303 SET_REGAD((u16) regnum) |
Bryan Wue190d6b2007-07-17 14:43:44 +0800304 STAOP |
305 STABUSY);
306
Adrian Bunk0ed0563e2008-10-12 21:15:17 -0700307 bfin_mdio_poll();
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800308
309 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800310}
311
Adrian Bunk0ed0563e2008-10-12 21:15:17 -0700312static int bfin_mdiobus_reset(struct mii_bus *bus)
Bryan Wue190d6b2007-07-17 14:43:44 +0800313{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800314 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800315}
316
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800317static void bfin_mac_adjust_link(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800318{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800319 struct bfin_mac_local *lp = netdev_priv(dev);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800320 struct phy_device *phydev = lp->phydev;
321 unsigned long flags;
322 int new_state = 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800323
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800324 spin_lock_irqsave(&lp->lock, flags);
325 if (phydev->link) {
326 /* Now we make sure that we can be in full duplex mode.
327 * If not, we operate in half-duplex mode. */
328 if (phydev->duplex != lp->old_duplex) {
329 u32 opmode = bfin_read_EMAC_OPMODE();
330 new_state = 1;
Bryan Wue190d6b2007-07-17 14:43:44 +0800331
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800332 if (phydev->duplex)
333 opmode |= FDMODE;
334 else
335 opmode &= ~(FDMODE);
Bryan Wue190d6b2007-07-17 14:43:44 +0800336
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800337 bfin_write_EMAC_OPMODE(opmode);
338 lp->old_duplex = phydev->duplex;
339 }
Bryan Wue190d6b2007-07-17 14:43:44 +0800340
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800341 if (phydev->speed != lp->old_speed) {
342#if defined(CONFIG_BFIN_MAC_RMII)
343 u32 opmode = bfin_read_EMAC_OPMODE();
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800344 switch (phydev->speed) {
345 case 10:
346 opmode |= RMII_10;
347 break;
348 case 100:
349 opmode &= ~(RMII_10);
350 break;
351 default:
352 printk(KERN_WARNING
353 "%s: Ack! Speed (%d) is not 10/100!\n",
354 DRV_NAME, phydev->speed);
355 break;
356 }
357 bfin_write_EMAC_OPMODE(opmode);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800358#endif
Bryan Wue190d6b2007-07-17 14:43:44 +0800359
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800360 new_state = 1;
361 lp->old_speed = phydev->speed;
362 }
Bryan Wue190d6b2007-07-17 14:43:44 +0800363
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800364 if (!lp->old_link) {
365 new_state = 1;
366 lp->old_link = 1;
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800367 }
368 } else if (lp->old_link) {
369 new_state = 1;
370 lp->old_link = 0;
371 lp->old_speed = 0;
372 lp->old_duplex = -1;
Bryan Wue190d6b2007-07-17 14:43:44 +0800373 }
374
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800375 if (new_state) {
376 u32 opmode = bfin_read_EMAC_OPMODE();
377 phy_print_status(phydev);
378 pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
Bryan Wue190d6b2007-07-17 14:43:44 +0800379 }
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800380
381 spin_unlock_irqrestore(&lp->lock, flags);
382}
383
Bryan Wu7cc8f382008-01-30 16:52:22 +0800384/* MDC = 2.5 MHz */
385#define MDC_CLK 2500000
386
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800387static int mii_probe(struct net_device *dev)
388{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800389 struct bfin_mac_local *lp = netdev_priv(dev);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800390 struct phy_device *phydev = NULL;
391 unsigned short sysctl;
392 int i;
Bryan Wu7cc8f382008-01-30 16:52:22 +0800393 u32 sclk, mdc_div;
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800394
395 /* Enable PHY output early */
Mike Frysinger98f672c2010-01-18 21:14:12 +0000396 if (!(bfin_read_VR_CTL() & CLKBUFOE))
397 bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800398
Bryan Wu7cc8f382008-01-30 16:52:22 +0800399 sclk = get_sclk();
400 mdc_div = ((sclk / MDC_CLK) / 2) - 1;
401
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800402 sysctl = bfin_read_EMAC_SYSCTL();
Bryan Wu9dc7f302008-01-30 16:52:28 +0800403 sysctl = (sysctl & ~MDCDIV) | SET_MDCDIV(mdc_div);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800404 bfin_write_EMAC_SYSCTL(sysctl);
405
406 /* search for connect PHY device */
407 for (i = 0; i < PHY_MAX_ADDR; i++) {
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700408 struct phy_device *const tmp_phydev = lp->mii_bus->phy_map[i];
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800409
410 if (!tmp_phydev)
411 continue; /* no PHY here... */
412
413 phydev = tmp_phydev;
414 break; /* found it */
415 }
416
417 /* now we are supposed to have a proper phydev, to attach to... */
418 if (!phydev) {
419 printk(KERN_INFO "%s: Don't found any phy device at all\n",
420 dev->name);
421 return -ENODEV;
422 }
423
424#if defined(CONFIG_BFIN_MAC_RMII)
Kay Sieversc2313552009-03-24 16:38:22 -0700425 phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link,
426 0, PHY_INTERFACE_MODE_RMII);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800427#else
Kay Sieversc2313552009-03-24 16:38:22 -0700428 phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link,
429 0, PHY_INTERFACE_MODE_MII);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800430#endif
431
432 if (IS_ERR(phydev)) {
433 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
434 return PTR_ERR(phydev);
435 }
436
437 /* mask with MAC supported features */
438 phydev->supported &= (SUPPORTED_10baseT_Half
439 | SUPPORTED_10baseT_Full
440 | SUPPORTED_100baseT_Half
441 | SUPPORTED_100baseT_Full
442 | SUPPORTED_Autoneg
443 | SUPPORTED_Pause | SUPPORTED_Asym_Pause
444 | SUPPORTED_MII
445 | SUPPORTED_TP);
446
447 phydev->advertising = phydev->supported;
448
449 lp->old_link = 0;
450 lp->old_speed = 0;
451 lp->old_duplex = -1;
452 lp->phydev = phydev;
453
454 printk(KERN_INFO "%s: attached PHY driver [%s] "
Bryan Wu7cc8f382008-01-30 16:52:22 +0800455 "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
456 "@sclk=%dMHz)\n",
Kay Sieversc2313552009-03-24 16:38:22 -0700457 DRV_NAME, phydev->drv->name, dev_name(&phydev->dev), phydev->irq,
Bryan Wu7cc8f382008-01-30 16:52:22 +0800458 MDC_CLK, mdc_div, sclk/1000000);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800459
460 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800461}
462
Bryan Wu679dce32008-04-25 11:53:11 +0800463/*
464 * Ethtool support
465 */
466
467static int
468bfin_mac_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
469{
470 struct bfin_mac_local *lp = netdev_priv(dev);
471
472 if (lp->phydev)
473 return phy_ethtool_gset(lp->phydev, cmd);
474
475 return -EINVAL;
476}
477
478static int
479bfin_mac_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
480{
481 struct bfin_mac_local *lp = netdev_priv(dev);
482
483 if (!capable(CAP_NET_ADMIN))
484 return -EPERM;
485
486 if (lp->phydev)
487 return phy_ethtool_sset(lp->phydev, cmd);
488
489 return -EINVAL;
490}
491
492static void bfin_mac_ethtool_getdrvinfo(struct net_device *dev,
493 struct ethtool_drvinfo *info)
494{
495 strcpy(info->driver, DRV_NAME);
496 strcpy(info->version, DRV_VERSION);
497 strcpy(info->fw_version, "N/A");
Kay Sieversc2313552009-03-24 16:38:22 -0700498 strcpy(info->bus_info, dev_name(&dev->dev));
Bryan Wu679dce32008-04-25 11:53:11 +0800499}
500
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700501static const struct ethtool_ops bfin_mac_ethtool_ops = {
Bryan Wu679dce32008-04-25 11:53:11 +0800502 .get_settings = bfin_mac_ethtool_getsettings,
503 .set_settings = bfin_mac_ethtool_setsettings,
504 .get_link = ethtool_op_get_link,
505 .get_drvinfo = bfin_mac_ethtool_getdrvinfo,
506};
507
Bryan Wue190d6b2007-07-17 14:43:44 +0800508/**************************************************************************/
509void setup_system_regs(struct net_device *dev)
510{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800511 unsigned short sysctl;
Bryan Wue190d6b2007-07-17 14:43:44 +0800512
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800513 /*
514 * Odd word alignment for Receive Frame DMA word
515 * Configure checksum support and rcve frame word alignment
516 */
517 sysctl = bfin_read_EMAC_SYSCTL();
Bryan Wue190d6b2007-07-17 14:43:44 +0800518 sysctl |= RXDWA;
Sonic Zhang812a9de2010-05-10 05:39:10 +0000519#if defined(BFIN_MAC_CSUM_OFFLOAD)
520 sysctl |= RXCKS;
521#else
522 sysctl &= ~RXCKS;
Bryan Wue190d6b2007-07-17 14:43:44 +0800523#endif
524 bfin_write_EMAC_SYSCTL(sysctl);
Bryan Wue190d6b2007-07-17 14:43:44 +0800525
526 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
527
528 /* Initialize the TX DMA channel registers */
529 bfin_write_DMA2_X_COUNT(0);
530 bfin_write_DMA2_X_MODIFY(4);
531 bfin_write_DMA2_Y_COUNT(0);
532 bfin_write_DMA2_Y_MODIFY(0);
533
534 /* Initialize the RX DMA channel registers */
535 bfin_write_DMA1_X_COUNT(0);
536 bfin_write_DMA1_X_MODIFY(4);
537 bfin_write_DMA1_Y_COUNT(0);
538 bfin_write_DMA1_Y_MODIFY(0);
539}
540
Alex Landau73f83182007-09-19 23:14:18 +0800541static void setup_mac_addr(u8 *mac_addr)
Bryan Wue190d6b2007-07-17 14:43:44 +0800542{
543 u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
544 u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
545
546 /* this depends on a little-endian machine */
547 bfin_write_EMAC_ADDRLO(addr_low);
548 bfin_write_EMAC_ADDRHI(addr_hi);
549}
550
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800551static int bfin_mac_set_mac_address(struct net_device *dev, void *p)
Alex Landau73f83182007-09-19 23:14:18 +0800552{
553 struct sockaddr *addr = p;
554 if (netif_running(dev))
555 return -EBUSY;
556 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
557 setup_mac_addr(dev->dev_addr);
558 return 0;
559}
560
Barry Songfe92afe2010-05-17 17:19:40 -0700561#ifdef CONFIG_BFIN_MAC_USE_HWSTAMP
562#define bfin_mac_hwtstamp_is_none(cfg) ((cfg) == HWTSTAMP_FILTER_NONE)
563
564static int bfin_mac_hwtstamp_ioctl(struct net_device *netdev,
565 struct ifreq *ifr, int cmd)
566{
567 struct hwtstamp_config config;
568 struct bfin_mac_local *lp = netdev_priv(netdev);
569 u16 ptpctl;
570 u32 ptpfv1, ptpfv2, ptpfv3, ptpfoff;
571
572 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
573 return -EFAULT;
574
575 pr_debug("%s config flag:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
576 __func__, config.flags, config.tx_type, config.rx_filter);
577
578 /* reserved for future extensions */
579 if (config.flags)
580 return -EINVAL;
581
582 if ((config.tx_type != HWTSTAMP_TX_OFF) &&
583 (config.tx_type != HWTSTAMP_TX_ON))
584 return -ERANGE;
585
586 ptpctl = bfin_read_EMAC_PTP_CTL();
587
588 switch (config.rx_filter) {
589 case HWTSTAMP_FILTER_NONE:
590 /*
591 * Dont allow any timestamping
592 */
593 ptpfv3 = 0xFFFFFFFF;
594 bfin_write_EMAC_PTP_FV3(ptpfv3);
595 break;
596 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
597 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
598 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
599 /*
600 * Clear the five comparison mask bits (bits[12:8]) in EMAC_PTP_CTL)
601 * to enable all the field matches.
602 */
603 ptpctl &= ~0x1F00;
604 bfin_write_EMAC_PTP_CTL(ptpctl);
605 /*
606 * Keep the default values of the EMAC_PTP_FOFF register.
607 */
608 ptpfoff = 0x4A24170C;
609 bfin_write_EMAC_PTP_FOFF(ptpfoff);
610 /*
611 * Keep the default values of the EMAC_PTP_FV1 and EMAC_PTP_FV2
612 * registers.
613 */
614 ptpfv1 = 0x11040800;
615 bfin_write_EMAC_PTP_FV1(ptpfv1);
616 ptpfv2 = 0x0140013F;
617 bfin_write_EMAC_PTP_FV2(ptpfv2);
618 /*
619 * The default value (0xFFFC) allows the timestamping of both
620 * received Sync messages and Delay_Req messages.
621 */
622 ptpfv3 = 0xFFFFFFFC;
623 bfin_write_EMAC_PTP_FV3(ptpfv3);
624
625 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
626 break;
627 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
628 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
629 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
630 /* Clear all five comparison mask bits (bits[12:8]) in the
631 * EMAC_PTP_CTL register to enable all the field matches.
632 */
633 ptpctl &= ~0x1F00;
634 bfin_write_EMAC_PTP_CTL(ptpctl);
635 /*
636 * Keep the default values of the EMAC_PTP_FOFF register, except set
637 * the PTPCOF field to 0x2A.
638 */
639 ptpfoff = 0x2A24170C;
640 bfin_write_EMAC_PTP_FOFF(ptpfoff);
641 /*
642 * Keep the default values of the EMAC_PTP_FV1 and EMAC_PTP_FV2
643 * registers.
644 */
645 ptpfv1 = 0x11040800;
646 bfin_write_EMAC_PTP_FV1(ptpfv1);
647 ptpfv2 = 0x0140013F;
648 bfin_write_EMAC_PTP_FV2(ptpfv2);
649 /*
650 * To allow the timestamping of Pdelay_Req and Pdelay_Resp, set
651 * the value to 0xFFF0.
652 */
653 ptpfv3 = 0xFFFFFFF0;
654 bfin_write_EMAC_PTP_FV3(ptpfv3);
655
656 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
657 break;
658 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
659 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
660 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
661 /*
662 * Clear bits 8 and 12 of the EMAC_PTP_CTL register to enable only the
663 * EFTM and PTPCM field comparison.
664 */
665 ptpctl &= ~0x1100;
666 bfin_write_EMAC_PTP_CTL(ptpctl);
667 /*
668 * Keep the default values of all the fields of the EMAC_PTP_FOFF
669 * register, except set the PTPCOF field to 0x0E.
670 */
671 ptpfoff = 0x0E24170C;
672 bfin_write_EMAC_PTP_FOFF(ptpfoff);
673 /*
674 * Program bits [15:0] of the EMAC_PTP_FV1 register to 0x88F7, which
675 * corresponds to PTP messages on the MAC layer.
676 */
677 ptpfv1 = 0x110488F7;
678 bfin_write_EMAC_PTP_FV1(ptpfv1);
679 ptpfv2 = 0x0140013F;
680 bfin_write_EMAC_PTP_FV2(ptpfv2);
681 /*
682 * To allow the timestamping of Pdelay_Req and Pdelay_Resp
683 * messages, set the value to 0xFFF0.
684 */
685 ptpfv3 = 0xFFFFFFF0;
686 bfin_write_EMAC_PTP_FV3(ptpfv3);
687
688 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
689 break;
690 default:
691 return -ERANGE;
692 }
693
694 if (config.tx_type == HWTSTAMP_TX_OFF &&
695 bfin_mac_hwtstamp_is_none(config.rx_filter)) {
696 ptpctl &= ~PTP_EN;
697 bfin_write_EMAC_PTP_CTL(ptpctl);
698
699 SSYNC();
700 } else {
701 ptpctl |= PTP_EN;
702 bfin_write_EMAC_PTP_CTL(ptpctl);
703
704 /*
705 * clear any existing timestamp
706 */
707 bfin_read_EMAC_PTP_RXSNAPLO();
708 bfin_read_EMAC_PTP_RXSNAPHI();
709
710 bfin_read_EMAC_PTP_TXSNAPLO();
711 bfin_read_EMAC_PTP_TXSNAPHI();
712
713 /*
714 * Set registers so that rollover occurs soon to test this.
715 */
716 bfin_write_EMAC_PTP_TIMELO(0x00000000);
717 bfin_write_EMAC_PTP_TIMEHI(0xFF800000);
718
719 SSYNC();
720
721 lp->compare.last_update = 0;
722 timecounter_init(&lp->clock,
723 &lp->cycles,
724 ktime_to_ns(ktime_get_real()));
725 timecompare_update(&lp->compare, 0);
726 }
727
728 lp->stamp_cfg = config;
729 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
730 -EFAULT : 0;
731}
732
733static void bfin_dump_hwtamp(char *s, ktime_t *hw, ktime_t *ts, struct timecompare *cmp)
734{
735 ktime_t sys = ktime_get_real();
736
737 pr_debug("%s %s hardware:%d,%d transform system:%d,%d system:%d,%d, cmp:%lld, %lld\n",
738 __func__, s, hw->tv.sec, hw->tv.nsec, ts->tv.sec, ts->tv.nsec, sys.tv.sec,
739 sys.tv.nsec, cmp->offset, cmp->skew);
740}
741
742static void bfin_tx_hwtstamp(struct net_device *netdev, struct sk_buff *skb)
743{
744 struct bfin_mac_local *lp = netdev_priv(netdev);
745 union skb_shared_tx *shtx = skb_tx(skb);
746
747 if (shtx->hardware) {
748 int timeout_cnt = MAX_TIMEOUT_CNT;
749
750 /* When doing time stamping, keep the connection to the socket
751 * a while longer
752 */
753 shtx->in_progress = 1;
754
755 /*
756 * The timestamping is done at the EMAC module's MII/RMII interface
757 * when the module sees the Start of Frame of an event message packet. This
758 * interface is the closest possible place to the physical Ethernet transmission
759 * medium, providing the best timing accuracy.
760 */
761 while ((!(bfin_read_EMAC_PTP_ISTAT() & TXTL)) && (--timeout_cnt))
762 udelay(1);
763 if (timeout_cnt == 0)
764 printk(KERN_ERR DRV_NAME
765 ": fails to timestamp the TX packet\n");
766 else {
767 struct skb_shared_hwtstamps shhwtstamps;
768 u64 ns;
769 u64 regval;
770
771 regval = bfin_read_EMAC_PTP_TXSNAPLO();
772 regval |= (u64)bfin_read_EMAC_PTP_TXSNAPHI() << 32;
773 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
774 ns = timecounter_cyc2time(&lp->clock,
775 regval);
776 timecompare_update(&lp->compare, ns);
777 shhwtstamps.hwtstamp = ns_to_ktime(ns);
778 shhwtstamps.syststamp =
779 timecompare_transform(&lp->compare, ns);
780 skb_tstamp_tx(skb, &shhwtstamps);
781
782 bfin_dump_hwtamp("TX", &shhwtstamps.hwtstamp, &shhwtstamps.syststamp, &lp->compare);
783 }
784 }
785}
786
787static void bfin_rx_hwtstamp(struct net_device *netdev, struct sk_buff *skb)
788{
789 struct bfin_mac_local *lp = netdev_priv(netdev);
790 u32 valid;
791 u64 regval, ns;
792 struct skb_shared_hwtstamps *shhwtstamps;
793
794 if (bfin_mac_hwtstamp_is_none(lp->stamp_cfg.rx_filter))
795 return;
796
797 valid = bfin_read_EMAC_PTP_ISTAT() & RXEL;
798 if (!valid)
799 return;
800
801 shhwtstamps = skb_hwtstamps(skb);
802
803 regval = bfin_read_EMAC_PTP_RXSNAPLO();
804 regval |= (u64)bfin_read_EMAC_PTP_RXSNAPHI() << 32;
805 ns = timecounter_cyc2time(&lp->clock, regval);
806 timecompare_update(&lp->compare, ns);
807 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
808 shhwtstamps->hwtstamp = ns_to_ktime(ns);
809 shhwtstamps->syststamp = timecompare_transform(&lp->compare, ns);
810
811 bfin_dump_hwtamp("RX", &shhwtstamps->hwtstamp, &shhwtstamps->syststamp, &lp->compare);
812}
813
814/*
815 * bfin_read_clock - read raw cycle counter (to be used by time counter)
816 */
817static cycle_t bfin_read_clock(const struct cyclecounter *tc)
818{
819 u64 stamp;
820
821 stamp = bfin_read_EMAC_PTP_TIMELO();
822 stamp |= (u64)bfin_read_EMAC_PTP_TIMEHI() << 32ULL;
823
824 return stamp;
825}
826
827#define PTP_CLK 25000000
828
829static void bfin_mac_hwtstamp_init(struct net_device *netdev)
830{
831 struct bfin_mac_local *lp = netdev_priv(netdev);
832 u64 append;
833
834 /* Initialize hardware timer */
835 append = PTP_CLK * (1ULL << 32);
836 do_div(append, get_sclk());
837 bfin_write_EMAC_PTP_ADDEND((u32)append);
838
839 memset(&lp->cycles, 0, sizeof(lp->cycles));
840 lp->cycles.read = bfin_read_clock;
841 lp->cycles.mask = CLOCKSOURCE_MASK(64);
842 lp->cycles.mult = 1000000000 / PTP_CLK;
843 lp->cycles.shift = 0;
844
845 /* Synchronize our NIC clock against system wall clock */
846 memset(&lp->compare, 0, sizeof(lp->compare));
847 lp->compare.source = &lp->clock;
848 lp->compare.target = ktime_get_real;
849 lp->compare.num_samples = 10;
850
851 /* Initialize hwstamp config */
852 lp->stamp_cfg.rx_filter = HWTSTAMP_FILTER_NONE;
853 lp->stamp_cfg.tx_type = HWTSTAMP_TX_OFF;
854}
855
856#else
857# define bfin_mac_hwtstamp_is_none(cfg) 0
858# define bfin_mac_hwtstamp_init(dev)
859# define bfin_mac_hwtstamp_ioctl(dev, ifr, cmd) (-EOPNOTSUPP)
860# define bfin_rx_hwtstamp(dev, skb)
861# define bfin_tx_hwtstamp(dev, skb)
862#endif
863
Bryan Wue190d6b2007-07-17 14:43:44 +0800864static void adjust_tx_list(void)
865{
866 int timeout_cnt = MAX_TIMEOUT_CNT;
867
Joe Perches8e95a202009-12-03 07:58:21 +0000868 if (tx_list_head->status.status_word != 0 &&
869 current_tx_ptr != tx_list_head) {
Bryan Wue190d6b2007-07-17 14:43:44 +0800870 goto adjust_head; /* released something, just return; */
871 }
872
873 /*
874 * if nothing released, check wait condition
875 * current's next can not be the head,
876 * otherwise the dma will not stop as we want
877 */
878 if (current_tx_ptr->next->next == tx_list_head) {
879 while (tx_list_head->status.status_word == 0) {
Michael Hennerich015dac82009-05-29 03:41:15 +0000880 udelay(10);
Joe Perches8e95a202009-12-03 07:58:21 +0000881 if (tx_list_head->status.status_word != 0 ||
882 !(bfin_read_DMA2_IRQ_STATUS() & DMA_RUN)) {
Bryan Wue190d6b2007-07-17 14:43:44 +0800883 goto adjust_head;
884 }
885 if (timeout_cnt-- < 0) {
886 printk(KERN_ERR DRV_NAME
887 ": wait for adjust tx list head timeout\n");
888 break;
889 }
890 }
891 if (tx_list_head->status.status_word != 0) {
892 goto adjust_head;
893 }
894 }
895
896 return;
897
898adjust_head:
899 do {
900 tx_list_head->desc_a.config &= ~DMAEN;
901 tx_list_head->status.status_word = 0;
902 if (tx_list_head->skb) {
903 dev_kfree_skb(tx_list_head->skb);
904 tx_list_head->skb = NULL;
905 } else {
906 printk(KERN_ERR DRV_NAME
907 ": no sk_buff in a transmitted frame!\n");
908 }
909 tx_list_head = tx_list_head->next;
Joe Perches8e95a202009-12-03 07:58:21 +0000910 } while (tx_list_head->status.status_word != 0 &&
911 current_tx_ptr != tx_list_head);
Bryan Wue190d6b2007-07-17 14:43:44 +0800912 return;
913
914}
915
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800916static int bfin_mac_hard_start_xmit(struct sk_buff *skb,
Bryan Wue190d6b2007-07-17 14:43:44 +0800917 struct net_device *dev)
918{
Bryan Wua50c0c02008-07-27 22:45:04 +0800919 u16 *data;
Michael Hennerich015dac82009-05-29 03:41:15 +0000920 u32 data_align = (unsigned long)(skb->data) & 0x3;
Barry Songfe92afe2010-05-17 17:19:40 -0700921 union skb_shared_tx *shtx = skb_tx(skb);
922
Bryan Wue190d6b2007-07-17 14:43:44 +0800923 current_tx_ptr->skb = skb;
924
Michael Hennerich015dac82009-05-29 03:41:15 +0000925 if (data_align == 0x2) {
926 /* move skb->data to current_tx_ptr payload */
927 data = (u16 *)(skb->data) - 1;
Barry Songfe92afe2010-05-17 17:19:40 -0700928 *data = (u16)(skb->len);
929 /*
930 * When transmitting an Ethernet packet, the PTP_TSYNC module requires
931 * a DMA_Length_Word field associated with the packet. The lower 12 bits
932 * of this field are the length of the packet payload in bytes and the higher
933 * 4 bits are the timestamping enable field.
934 */
935 if (shtx->hardware)
936 *data |= 0x1000;
937
Michael Hennerich015dac82009-05-29 03:41:15 +0000938 current_tx_ptr->desc_a.start_addr = (u32)data;
939 /* this is important! */
940 blackfin_dcache_flush_range((u32)data,
941 (u32)((u8 *)data + skb->len + 4));
Bryan Wue190d6b2007-07-17 14:43:44 +0800942 } else {
Michael Hennerich015dac82009-05-29 03:41:15 +0000943 *((u16 *)(current_tx_ptr->packet)) = (u16)(skb->len);
Barry Songfe92afe2010-05-17 17:19:40 -0700944 /* enable timestamping for the sent packet */
945 if (shtx->hardware)
946 *((u16 *)(current_tx_ptr->packet)) |= 0x1000;
Michael Hennerich015dac82009-05-29 03:41:15 +0000947 memcpy((u8 *)(current_tx_ptr->packet + 2), skb->data,
948 skb->len);
949 current_tx_ptr->desc_a.start_addr =
950 (u32)current_tx_ptr->packet;
951 if (current_tx_ptr->status.status_word != 0)
952 current_tx_ptr->status.status_word = 0;
953 blackfin_dcache_flush_range(
954 (u32)current_tx_ptr->packet,
955 (u32)(current_tx_ptr->packet + skb->len + 2));
Bryan Wue190d6b2007-07-17 14:43:44 +0800956 }
957
Sonic Zhang805a8ab2009-05-29 03:40:43 +0000958 /* make sure the internal data buffers in the core are drained
959 * so that the DMA descriptors are completely written when the
960 * DMA engine goes to fetch them below
961 */
962 SSYNC();
963
Bryan Wue190d6b2007-07-17 14:43:44 +0800964 /* enable this packet's dma */
965 current_tx_ptr->desc_a.config |= DMAEN;
966
967 /* tx dma is running, just return */
Michael Hennerich015dac82009-05-29 03:41:15 +0000968 if (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN)
Bryan Wue190d6b2007-07-17 14:43:44 +0800969 goto out;
970
971 /* tx dma is not running */
972 bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
973 /* dma enabled, read from memory, size is 6 */
974 bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
975 /* Turn on the EMAC tx */
976 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
977
978out:
979 adjust_tx_list();
Barry Songfe92afe2010-05-17 17:19:40 -0700980
981 bfin_tx_hwtstamp(dev, skb);
982
Bryan Wue190d6b2007-07-17 14:43:44 +0800983 current_tx_ptr = current_tx_ptr->next;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700984 dev->stats.tx_packets++;
985 dev->stats.tx_bytes += (skb->len);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000986 return NETDEV_TX_OK;
Bryan Wue190d6b2007-07-17 14:43:44 +0800987}
988
Sonic Zhangad2864d2010-05-10 05:39:09 +0000989#define IP_HEADER_OFF 0
Peter Meerwaldec497b32010-05-17 17:20:50 -0700990#define RX_ERROR_MASK (RX_LONG | RX_ALIGN | RX_CRC | RX_LEN | \
991 RX_FRAG | RX_ADDR | RX_DMAO | RX_PHY | RX_LATE | RX_RANGE)
992
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800993static void bfin_mac_rx(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800994{
995 struct sk_buff *skb, *new_skb;
Bryan Wue190d6b2007-07-17 14:43:44 +0800996 unsigned short len;
Barry Songfe92afe2010-05-17 17:19:40 -0700997 struct bfin_mac_local *lp __maybe_unused = netdev_priv(dev);
Sonic Zhangad2864d2010-05-10 05:39:09 +0000998#if defined(BFIN_MAC_CSUM_OFFLOAD)
999 unsigned int i;
1000 unsigned char fcs[ETH_FCS_LEN + 1];
1001#endif
Bryan Wue190d6b2007-07-17 14:43:44 +08001002
Peter Meerwaldec497b32010-05-17 17:20:50 -07001003 /* check if frame status word reports an error condition
1004 * we which case we simply drop the packet
1005 */
1006 if (current_rx_ptr->status.status_word & RX_ERROR_MASK) {
1007 printk(KERN_NOTICE DRV_NAME
1008 ": rx: receive error - packet dropped\n");
1009 dev->stats.rx_dropped++;
1010 goto out;
1011 }
1012
Bryan Wue190d6b2007-07-17 14:43:44 +08001013 /* allocate a new skb for next time receive */
1014 skb = current_rx_ptr->skb;
Barry Songfe92afe2010-05-17 17:19:40 -07001015
Michael Hennerich015dac82009-05-29 03:41:15 +00001016 new_skb = dev_alloc_skb(PKT_BUF_SZ + NET_IP_ALIGN);
Bryan Wue190d6b2007-07-17 14:43:44 +08001017 if (!new_skb) {
1018 printk(KERN_NOTICE DRV_NAME
1019 ": rx: low on mem - packet dropped\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001020 dev->stats.rx_dropped++;
Bryan Wue190d6b2007-07-17 14:43:44 +08001021 goto out;
1022 }
1023 /* reserve 2 bytes for RXDWA padding */
Michael Hennerich015dac82009-05-29 03:41:15 +00001024 skb_reserve(new_skb, NET_IP_ALIGN);
Alexey Demin6e01d1a2008-01-30 16:52:27 +08001025 /* Invidate the data cache of skb->data range when it is write back
1026 * cache. It will prevent overwritting the new data from DMA
1027 */
1028 blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
1029 (unsigned long)new_skb->end);
1030
Sonic Zhangf6e1e4f2010-05-10 05:39:08 +00001031 current_rx_ptr->skb = new_skb;
1032 current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
1033
Bryan Wue190d6b2007-07-17 14:43:44 +08001034 len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
Sonic Zhangad2864d2010-05-10 05:39:09 +00001035 /* Deduce Ethernet FCS length from Ethernet payload length */
1036 len -= ETH_FCS_LEN;
Bryan Wue190d6b2007-07-17 14:43:44 +08001037 skb_put(skb, len);
Bryan Wue190d6b2007-07-17 14:43:44 +08001038
Bryan Wue190d6b2007-07-17 14:43:44 +08001039 skb->protocol = eth_type_trans(skb, dev);
Barry Songfe92afe2010-05-17 17:19:40 -07001040
1041 bfin_rx_hwtstamp(dev, skb);
1042
Bryan Wue190d6b2007-07-17 14:43:44 +08001043#if defined(BFIN_MAC_CSUM_OFFLOAD)
Sonic Zhangad2864d2010-05-10 05:39:09 +00001044 /* Checksum offloading only works for IPv4 packets with the standard IP header
1045 * length of 20 bytes, because the blackfin MAC checksum calculation is
1046 * based on that assumption. We must NOT use the calculated checksum if our
1047 * IP version or header break that assumption.
1048 */
1049 if (skb->data[IP_HEADER_OFF] == 0x45) {
1050 skb->csum = current_rx_ptr->status.ip_payload_csum;
1051 /*
1052 * Deduce Ethernet FCS from hardware generated IP payload checksum.
1053 * IP checksum is based on 16-bit one's complement algorithm.
1054 * To deduce a value from checksum is equal to add its inversion.
1055 * If the IP payload len is odd, the inversed FCS should also
1056 * begin from odd address and leave first byte zero.
1057 */
1058 if (skb->len % 2) {
1059 fcs[0] = 0;
1060 for (i = 0; i < ETH_FCS_LEN; i++)
1061 fcs[i + 1] = ~skb->data[skb->len + i];
1062 skb->csum = csum_partial(fcs, ETH_FCS_LEN + 1, skb->csum);
1063 } else {
1064 for (i = 0; i < ETH_FCS_LEN; i++)
1065 fcs[i] = ~skb->data[skb->len + i];
1066 skb->csum = csum_partial(fcs, ETH_FCS_LEN, skb->csum);
1067 }
1068 skb->ip_summed = CHECKSUM_COMPLETE;
1069 }
Bryan Wue190d6b2007-07-17 14:43:44 +08001070#endif
1071
1072 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001073 dev->stats.rx_packets++;
1074 dev->stats.rx_bytes += len;
Peter Meerwaldec497b32010-05-17 17:20:50 -07001075out:
Bryan Wue190d6b2007-07-17 14:43:44 +08001076 current_rx_ptr->status.status_word = 0x00000000;
1077 current_rx_ptr = current_rx_ptr->next;
Bryan Wue190d6b2007-07-17 14:43:44 +08001078}
1079
1080/* interrupt routine to handle rx and error signal */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001081static irqreturn_t bfin_mac_interrupt(int irq, void *dev_id)
Bryan Wue190d6b2007-07-17 14:43:44 +08001082{
1083 struct net_device *dev = dev_id;
1084 int number = 0;
1085
1086get_one_packet:
1087 if (current_rx_ptr->status.status_word == 0) {
1088 /* no more new packet received */
1089 if (number == 0) {
1090 if (current_rx_ptr->next->status.status_word != 0) {
1091 current_rx_ptr = current_rx_ptr->next;
1092 goto real_rx;
1093 }
1094 }
1095 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
1096 DMA_DONE | DMA_ERR);
1097 return IRQ_HANDLED;
1098 }
1099
1100real_rx:
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001101 bfin_mac_rx(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +08001102 number++;
1103 goto get_one_packet;
1104}
1105
1106#ifdef CONFIG_NET_POLL_CONTROLLER
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001107static void bfin_mac_poll(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +08001108{
1109 disable_irq(IRQ_MAC_RX);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001110 bfin_mac_interrupt(IRQ_MAC_RX, dev);
Bryan Wue190d6b2007-07-17 14:43:44 +08001111 enable_irq(IRQ_MAC_RX);
1112}
1113#endif /* CONFIG_NET_POLL_CONTROLLER */
1114
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001115static void bfin_mac_disable(void)
Bryan Wue190d6b2007-07-17 14:43:44 +08001116{
1117 unsigned int opmode;
1118
1119 opmode = bfin_read_EMAC_OPMODE();
1120 opmode &= (~RE);
1121 opmode &= (~TE);
1122 /* Turn off the EMAC */
1123 bfin_write_EMAC_OPMODE(opmode);
1124}
1125
1126/*
1127 * Enable Interrupts, Receive, and Transmit
1128 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001129static void bfin_mac_enable(void)
Bryan Wue190d6b2007-07-17 14:43:44 +08001130{
1131 u32 opmode;
1132
Harvey Harrisonb39d66a2008-08-20 16:52:04 -07001133 pr_debug("%s: %s\n", DRV_NAME, __func__);
Bryan Wue190d6b2007-07-17 14:43:44 +08001134
1135 /* Set RX DMA */
1136 bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
1137 bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
1138
1139 /* Wait MII done */
Adrian Bunk0ed0563e2008-10-12 21:15:17 -07001140 bfin_mdio_poll();
Bryan Wue190d6b2007-07-17 14:43:44 +08001141
1142 /* We enable only RX here */
1143 /* ASTP : Enable Automatic Pad Stripping
1144 PR : Promiscuous Mode for test
1145 PSF : Receive frames with total length less than 64 bytes.
1146 FDMODE : Full Duplex Mode
1147 LB : Internal Loopback for test
1148 RE : Receiver Enable */
1149 opmode = bfin_read_EMAC_OPMODE();
1150 if (opmode & FDMODE)
1151 opmode |= PSF;
1152 else
1153 opmode |= DRO | DC | PSF;
1154 opmode |= RE;
1155
1156#if defined(CONFIG_BFIN_MAC_RMII)
1157 opmode |= RMII; /* For Now only 100MBit are supported */
Michael Hennerich6893ff12008-01-30 16:52:25 +08001158#if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
Bryan Wue190d6b2007-07-17 14:43:44 +08001159 opmode |= TE;
1160#endif
1161#endif
1162 /* Turn on the EMAC rx */
1163 bfin_write_EMAC_OPMODE(opmode);
Bryan Wue190d6b2007-07-17 14:43:44 +08001164}
1165
1166/* Our watchdog timed out. Called by the networking layer */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001167static void bfin_mac_timeout(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +08001168{
Harvey Harrisonb39d66a2008-08-20 16:52:04 -07001169 pr_debug("%s: %s\n", dev->name, __func__);
Bryan Wue190d6b2007-07-17 14:43:44 +08001170
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001171 bfin_mac_disable();
Bryan Wue190d6b2007-07-17 14:43:44 +08001172
1173 /* reset tx queue */
1174 tx_list_tail = tx_list_head->next;
1175
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001176 bfin_mac_enable();
Bryan Wue190d6b2007-07-17 14:43:44 +08001177
1178 /* We can accept TX packets again */
Eric Dumazet1ae5dc32010-05-10 05:01:31 -07001179 dev->trans_start = jiffies; /* prevent tx timeout */
Bryan Wue190d6b2007-07-17 14:43:44 +08001180 netif_wake_queue(dev);
1181}
1182
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001183static void bfin_mac_multicast_hash(struct net_device *dev)
Aidan Williams775919b2008-01-30 16:52:23 +08001184{
1185 u32 emac_hashhi, emac_hashlo;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001186 struct netdev_hw_addr *ha;
Aidan Williams775919b2008-01-30 16:52:23 +08001187 char *addrs;
Aidan Williams775919b2008-01-30 16:52:23 +08001188 u32 crc;
1189
1190 emac_hashhi = emac_hashlo = 0;
1191
Jiri Pirko22bedad32010-04-01 21:22:57 +00001192 netdev_for_each_mc_addr(ha, dev) {
1193 addrs = ha->addr;
Aidan Williams775919b2008-01-30 16:52:23 +08001194
1195 /* skip non-multicast addresses */
1196 if (!(*addrs & 1))
1197 continue;
1198
1199 crc = ether_crc(ETH_ALEN, addrs);
1200 crc >>= 26;
1201
1202 if (crc & 0x20)
1203 emac_hashhi |= 1 << (crc & 0x1f);
1204 else
1205 emac_hashlo |= 1 << (crc & 0x1f);
1206 }
1207
1208 bfin_write_EMAC_HASHHI(emac_hashhi);
1209 bfin_write_EMAC_HASHLO(emac_hashlo);
Aidan Williams775919b2008-01-30 16:52:23 +08001210}
1211
Bryan Wue190d6b2007-07-17 14:43:44 +08001212/*
Bryan Wue190d6b2007-07-17 14:43:44 +08001213 * This routine will, depending on the values passed to it,
1214 * either make it accept multicast packets, go into
1215 * promiscuous mode (for TCPDUMP and cousins) or accept
1216 * a select set of multicast packets
1217 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001218static void bfin_mac_set_multicast_list(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +08001219{
1220 u32 sysctl;
1221
1222 if (dev->flags & IFF_PROMISC) {
1223 printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
1224 sysctl = bfin_read_EMAC_OPMODE();
1225 sysctl |= RAF;
1226 bfin_write_EMAC_OPMODE(sysctl);
Aidan Williams775919b2008-01-30 16:52:23 +08001227 } else if (dev->flags & IFF_ALLMULTI) {
Bryan Wue190d6b2007-07-17 14:43:44 +08001228 /* accept all multicast */
1229 sysctl = bfin_read_EMAC_OPMODE();
1230 sysctl |= PAM;
1231 bfin_write_EMAC_OPMODE(sysctl);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001232 } else if (!netdev_mc_empty(dev)) {
Aidan Williams775919b2008-01-30 16:52:23 +08001233 /* set up multicast hash table */
1234 sysctl = bfin_read_EMAC_OPMODE();
1235 sysctl |= HM;
1236 bfin_write_EMAC_OPMODE(sysctl);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001237 bfin_mac_multicast_hash(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +08001238 } else {
1239 /* clear promisc or multicast mode */
1240 sysctl = bfin_read_EMAC_OPMODE();
1241 sysctl &= ~(RAF | PAM);
1242 bfin_write_EMAC_OPMODE(sysctl);
1243 }
1244}
1245
Barry Songfe92afe2010-05-17 17:19:40 -07001246static int bfin_mac_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1247{
1248 switch (cmd) {
1249 case SIOCSHWTSTAMP:
1250 return bfin_mac_hwtstamp_ioctl(netdev, ifr, cmd);
1251 default:
1252 return -EOPNOTSUPP;
1253 }
1254}
1255
Bryan Wue190d6b2007-07-17 14:43:44 +08001256/*
1257 * this puts the device in an inactive state
1258 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001259static void bfin_mac_shutdown(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +08001260{
1261 /* Turn off the EMAC */
1262 bfin_write_EMAC_OPMODE(0x00000000);
1263 /* Turn off the EMAC RX DMA */
1264 bfin_write_DMA1_CONFIG(0x0000);
1265 bfin_write_DMA2_CONFIG(0x0000);
1266}
1267
1268/*
1269 * Open and Initialize the interface
1270 *
1271 * Set up everything, reset the card, etc..
1272 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001273static int bfin_mac_open(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +08001274{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001275 struct bfin_mac_local *lp = netdev_priv(dev);
Michael Hennerich4af4b842007-07-25 14:09:54 +08001276 int retval;
Harvey Harrisonb39d66a2008-08-20 16:52:04 -07001277 pr_debug("%s: %s\n", dev->name, __func__);
Bryan Wue190d6b2007-07-17 14:43:44 +08001278
1279 /*
1280 * Check that the address is valid. If its not, refuse
1281 * to bring the device up. The user must specify an
1282 * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
1283 */
1284 if (!is_valid_ether_addr(dev->dev_addr)) {
1285 printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
1286 return -EINVAL;
1287 }
1288
1289 /* initial rx and tx list */
Michael Hennerich4af4b842007-07-25 14:09:54 +08001290 retval = desc_list_init();
1291
1292 if (retval)
1293 return retval;
Bryan Wue190d6b2007-07-17 14:43:44 +08001294
Bryan Wu4ae5a3a2007-09-19 23:37:36 +08001295 phy_start(lp->phydev);
Vitja Makarov136492b2008-01-30 16:52:26 +08001296 phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
Bryan Wue190d6b2007-07-17 14:43:44 +08001297 setup_system_regs(dev);
Michael Hennerichee02fee2008-07-27 22:45:05 +08001298 setup_mac_addr(dev->dev_addr);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001299 bfin_mac_disable();
1300 bfin_mac_enable();
Bryan Wue190d6b2007-07-17 14:43:44 +08001301 pr_debug("hardware init finished\n");
1302 netif_start_queue(dev);
1303 netif_carrier_on(dev);
1304
1305 return 0;
1306}
1307
1308/*
Bryan Wue190d6b2007-07-17 14:43:44 +08001309 * this makes the board clean up everything that it can
1310 * and not talk to the outside world. Caused by
1311 * an 'ifconfig ethX down'
1312 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001313static int bfin_mac_close(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +08001314{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001315 struct bfin_mac_local *lp = netdev_priv(dev);
Harvey Harrisonb39d66a2008-08-20 16:52:04 -07001316 pr_debug("%s: %s\n", dev->name, __func__);
Bryan Wue190d6b2007-07-17 14:43:44 +08001317
1318 netif_stop_queue(dev);
1319 netif_carrier_off(dev);
1320
Bryan Wu4ae5a3a2007-09-19 23:37:36 +08001321 phy_stop(lp->phydev);
Vitja Makarov136492b2008-01-30 16:52:26 +08001322 phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +08001323
Bryan Wue190d6b2007-07-17 14:43:44 +08001324 /* clear everything */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001325 bfin_mac_shutdown(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +08001326
1327 /* free the rx/tx buffers */
1328 desc_list_free();
1329
1330 return 0;
1331}
1332
Mike Frysingerb63dc8f2009-05-26 20:55:33 -07001333static const struct net_device_ops bfin_mac_netdev_ops = {
1334 .ndo_open = bfin_mac_open,
1335 .ndo_stop = bfin_mac_close,
1336 .ndo_start_xmit = bfin_mac_hard_start_xmit,
1337 .ndo_set_mac_address = bfin_mac_set_mac_address,
1338 .ndo_tx_timeout = bfin_mac_timeout,
1339 .ndo_set_multicast_list = bfin_mac_set_multicast_list,
Barry Songfe92afe2010-05-17 17:19:40 -07001340 .ndo_do_ioctl = bfin_mac_ioctl,
Mike Frysingerb63dc8f2009-05-26 20:55:33 -07001341 .ndo_validate_addr = eth_validate_addr,
1342 .ndo_change_mtu = eth_change_mtu,
1343#ifdef CONFIG_NET_POLL_CONTROLLER
1344 .ndo_poll_controller = bfin_mac_poll,
1345#endif
1346};
1347
Mike Frysingerd7b843d32008-07-27 22:45:03 +08001348static int __devinit bfin_mac_probe(struct platform_device *pdev)
Bryan Wue190d6b2007-07-17 14:43:44 +08001349{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001350 struct net_device *ndev;
1351 struct bfin_mac_local *lp;
Graf Yang080c8252009-05-29 03:41:48 +00001352 struct platform_device *pd;
1353 int rc;
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001354
1355 ndev = alloc_etherdev(sizeof(struct bfin_mac_local));
1356 if (!ndev) {
1357 dev_err(&pdev->dev, "Cannot allocate net device!\n");
1358 return -ENOMEM;
1359 }
1360
1361 SET_NETDEV_DEV(ndev, &pdev->dev);
1362 platform_set_drvdata(pdev, ndev);
1363 lp = netdev_priv(ndev);
Bryan Wue190d6b2007-07-17 14:43:44 +08001364
1365 /* Grab the MAC address in the MAC */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001366 *(__le32 *) (&(ndev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
1367 *(__le16 *) (&(ndev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
Bryan Wue190d6b2007-07-17 14:43:44 +08001368
1369 /* probe mac */
1370 /*todo: how to proble? which is revision_register */
1371 bfin_write_EMAC_ADDRLO(0x12345678);
1372 if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001373 dev_err(&pdev->dev, "Cannot detect Blackfin on-chip ethernet MAC controller!\n");
1374 rc = -ENODEV;
1375 goto out_err_probe_mac;
Bryan Wue190d6b2007-07-17 14:43:44 +08001376 }
1377
Bryan Wue190d6b2007-07-17 14:43:44 +08001378
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001379 /*
1380 * Is it valid? (Did bootloader initialize it?)
1381 * Grab the MAC from the board somehow
1382 * this is done in the arch/blackfin/mach-bfxxx/boards/eth_mac.c
1383 */
1384 if (!is_valid_ether_addr(ndev->dev_addr))
1385 bfin_get_ether_addr(ndev->dev_addr);
1386
Bryan Wue190d6b2007-07-17 14:43:44 +08001387 /* If still not valid, get a random one */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001388 if (!is_valid_ether_addr(ndev->dev_addr))
1389 random_ether_addr(ndev->dev_addr);
Bryan Wue190d6b2007-07-17 14:43:44 +08001390
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001391 setup_mac_addr(ndev->dev_addr);
Bryan Wue190d6b2007-07-17 14:43:44 +08001392
Graf Yang080c8252009-05-29 03:41:48 +00001393 if (!pdev->dev.platform_data) {
1394 dev_err(&pdev->dev, "Cannot get platform device bfin_mii_bus!\n");
1395 rc = -ENODEV;
1396 goto out_err_probe_mac;
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001397 }
Graf Yang080c8252009-05-29 03:41:48 +00001398 pd = pdev->dev.platform_data;
1399 lp->mii_bus = platform_get_drvdata(pd);
1400 lp->mii_bus->priv = ndev;
Bryan Wu4ae5a3a2007-09-19 23:37:36 +08001401
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001402 rc = mii_probe(ndev);
1403 if (rc) {
1404 dev_err(&pdev->dev, "MII Probe failed!\n");
1405 goto out_err_mii_probe;
1406 }
Bryan Wu4ae5a3a2007-09-19 23:37:36 +08001407
Bryan Wue190d6b2007-07-17 14:43:44 +08001408 /* Fill in the fields of the device structure with ethernet values. */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001409 ether_setup(ndev);
Bryan Wue190d6b2007-07-17 14:43:44 +08001410
Alexander Beregalov149da652009-04-14 18:30:24 +00001411 ndev->netdev_ops = &bfin_mac_netdev_ops;
Bryan Wu679dce32008-04-25 11:53:11 +08001412 ndev->ethtool_ops = &bfin_mac_ethtool_ops;
Bryan Wue190d6b2007-07-17 14:43:44 +08001413
Bryan Wue190d6b2007-07-17 14:43:44 +08001414 spin_lock_init(&lp->lock);
1415
1416 /* now, enable interrupts */
1417 /* register irq handler */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001418 rc = request_irq(IRQ_MAC_RX, bfin_mac_interrupt,
Michael Hennerich91a455f2009-05-29 03:39:45 +00001419 IRQF_DISABLED, "EMAC_RX", ndev);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001420 if (rc) {
1421 dev_err(&pdev->dev, "Cannot request Blackfin MAC RX IRQ!\n");
1422 rc = -EBUSY;
1423 goto out_err_request_irq;
Bryan Wue190d6b2007-07-17 14:43:44 +08001424 }
1425
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001426 rc = register_netdev(ndev);
1427 if (rc) {
1428 dev_err(&pdev->dev, "Cannot register net device!\n");
1429 goto out_err_reg_ndev;
Bryan Wue190d6b2007-07-17 14:43:44 +08001430 }
1431
Barry Songfe92afe2010-05-17 17:19:40 -07001432 bfin_mac_hwtstamp_init(ndev);
1433
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001434 /* now, print out the card info, in a short format.. */
1435 dev_info(&pdev->dev, "%s, Version %s\n", DRV_DESC, DRV_VERSION);
Bryan Wue190d6b2007-07-17 14:43:44 +08001436
1437 return 0;
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001438
1439out_err_reg_ndev:
1440 free_irq(IRQ_MAC_RX, ndev);
1441out_err_request_irq:
1442out_err_mii_probe:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001443 mdiobus_unregister(lp->mii_bus);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001444 mdiobus_free(lp->mii_bus);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001445 peripheral_free_list(pin_req);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001446out_err_probe_mac:
1447 platform_set_drvdata(pdev, NULL);
1448 free_netdev(ndev);
1449
1450 return rc;
Bryan Wue190d6b2007-07-17 14:43:44 +08001451}
1452
Mike Frysingerd7b843d32008-07-27 22:45:03 +08001453static int __devexit bfin_mac_remove(struct platform_device *pdev)
Bryan Wue190d6b2007-07-17 14:43:44 +08001454{
1455 struct net_device *ndev = platform_get_drvdata(pdev);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001456 struct bfin_mac_local *lp = netdev_priv(ndev);
Bryan Wue190d6b2007-07-17 14:43:44 +08001457
1458 platform_set_drvdata(pdev, NULL);
1459
Graf Yang080c8252009-05-29 03:41:48 +00001460 lp->mii_bus->priv = NULL;
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001461
Bryan Wue190d6b2007-07-17 14:43:44 +08001462 unregister_netdev(ndev);
1463
1464 free_irq(IRQ_MAC_RX, ndev);
1465
1466 free_netdev(ndev);
1467
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001468 peripheral_free_list(pin_req);
Bryan Wue190d6b2007-07-17 14:43:44 +08001469
1470 return 0;
1471}
1472
Bryan Wu496a34c2007-09-19 23:37:14 +08001473#ifdef CONFIG_PM
1474static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
Bryan Wue190d6b2007-07-17 14:43:44 +08001475{
Bryan Wu496a34c2007-09-19 23:37:14 +08001476 struct net_device *net_dev = platform_get_drvdata(pdev);
1477
1478 if (netif_running(net_dev))
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001479 bfin_mac_close(net_dev);
Bryan Wu496a34c2007-09-19 23:37:14 +08001480
Bryan Wue190d6b2007-07-17 14:43:44 +08001481 return 0;
1482}
1483
1484static int bfin_mac_resume(struct platform_device *pdev)
1485{
Bryan Wu496a34c2007-09-19 23:37:14 +08001486 struct net_device *net_dev = platform_get_drvdata(pdev);
1487
1488 if (netif_running(net_dev))
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001489 bfin_mac_open(net_dev);
Bryan Wu496a34c2007-09-19 23:37:14 +08001490
Bryan Wue190d6b2007-07-17 14:43:44 +08001491 return 0;
1492}
Bryan Wu496a34c2007-09-19 23:37:14 +08001493#else
1494#define bfin_mac_suspend NULL
1495#define bfin_mac_resume NULL
1496#endif /* CONFIG_PM */
Bryan Wue190d6b2007-07-17 14:43:44 +08001497
Graf Yang080c8252009-05-29 03:41:48 +00001498static int __devinit bfin_mii_bus_probe(struct platform_device *pdev)
1499{
1500 struct mii_bus *miibus;
1501 int rc, i;
1502
1503 /*
1504 * We are setting up a network card,
1505 * so set the GPIO pins to Ethernet mode
1506 */
1507 rc = peripheral_request_list(pin_req, DRV_NAME);
1508 if (rc) {
1509 dev_err(&pdev->dev, "Requesting peripherals failed!\n");
1510 return rc;
1511 }
1512
1513 rc = -ENOMEM;
1514 miibus = mdiobus_alloc();
1515 if (miibus == NULL)
1516 goto out_err_alloc;
1517 miibus->read = bfin_mdiobus_read;
1518 miibus->write = bfin_mdiobus_write;
1519 miibus->reset = bfin_mdiobus_reset;
1520
1521 miibus->parent = &pdev->dev;
1522 miibus->name = "bfin_mii_bus";
1523 snprintf(miibus->id, MII_BUS_ID_SIZE, "0");
1524 miibus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1525 if (miibus->irq == NULL)
1526 goto out_err_alloc;
1527 for (i = 0; i < PHY_MAX_ADDR; ++i)
1528 miibus->irq[i] = PHY_POLL;
1529
1530 rc = mdiobus_register(miibus);
1531 if (rc) {
1532 dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
1533 goto out_err_mdiobus_register;
1534 }
1535
1536 platform_set_drvdata(pdev, miibus);
1537 return 0;
1538
1539out_err_mdiobus_register:
1540 mdiobus_free(miibus);
1541out_err_alloc:
1542 peripheral_free_list(pin_req);
1543
1544 return rc;
1545}
1546
1547static int __devexit bfin_mii_bus_remove(struct platform_device *pdev)
1548{
1549 struct mii_bus *miibus = platform_get_drvdata(pdev);
1550 platform_set_drvdata(pdev, NULL);
1551 mdiobus_unregister(miibus);
1552 mdiobus_free(miibus);
1553 peripheral_free_list(pin_req);
1554 return 0;
1555}
1556
1557static struct platform_driver bfin_mii_bus_driver = {
1558 .probe = bfin_mii_bus_probe,
1559 .remove = __devexit_p(bfin_mii_bus_remove),
1560 .driver = {
1561 .name = "bfin_mii_bus",
1562 .owner = THIS_MODULE,
1563 },
1564};
1565
Bryan Wue190d6b2007-07-17 14:43:44 +08001566static struct platform_driver bfin_mac_driver = {
1567 .probe = bfin_mac_probe,
Mike Frysingerd7b843d32008-07-27 22:45:03 +08001568 .remove = __devexit_p(bfin_mac_remove),
Bryan Wue190d6b2007-07-17 14:43:44 +08001569 .resume = bfin_mac_resume,
1570 .suspend = bfin_mac_suspend,
1571 .driver = {
Kay Sievers72abb462008-04-18 13:50:44 -07001572 .name = DRV_NAME,
1573 .owner = THIS_MODULE,
1574 },
Bryan Wue190d6b2007-07-17 14:43:44 +08001575};
1576
1577static int __init bfin_mac_init(void)
1578{
Graf Yang080c8252009-05-29 03:41:48 +00001579 int ret;
1580 ret = platform_driver_register(&bfin_mii_bus_driver);
1581 if (!ret)
1582 return platform_driver_register(&bfin_mac_driver);
1583 return -ENODEV;
Bryan Wue190d6b2007-07-17 14:43:44 +08001584}
1585
1586module_init(bfin_mac_init);
1587
1588static void __exit bfin_mac_cleanup(void)
1589{
1590 platform_driver_unregister(&bfin_mac_driver);
Graf Yang080c8252009-05-29 03:41:48 +00001591 platform_driver_unregister(&bfin_mii_bus_driver);
Bryan Wue190d6b2007-07-17 14:43:44 +08001592}
1593
1594module_exit(bfin_mac_cleanup);
Kay Sievers72abb462008-04-18 13:50:44 -07001595