blob: c6586cdb8b95976449c241a2d7c09e5933d2f7bd [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:
Aidan Williams775919b2008-01-30 16:52:23 +080014 * 2006-12-19 Aidan Williams, multicast hash support
Bryan Wue190d6b2007-07-17 14:43:44 +080015 * Copyright 2004-2006 Analog Devices Inc.
16 *
17 * Bugs: Enter bugs at http://blackfin.uclinux.org/
18 *
19 * This program is free software ; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation ; either version 2, or (at your option)
22 * any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program ; see the file COPYING.
31 * If not, write to the Free Software Foundation,
32 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 */
34
35#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/kernel.h>
38#include <linux/sched.h>
39#include <linux/slab.h>
40#include <linux/delay.h>
41#include <linux/timer.h>
42#include <linux/errno.h>
43#include <linux/irq.h>
44#include <linux/io.h>
45#include <linux/ioport.h>
46#include <linux/crc32.h>
47#include <linux/device.h>
48#include <linux/spinlock.h>
49#include <linux/ethtool.h>
50#include <linux/mii.h>
Bryan Wu4ae5a3a2007-09-19 23:37:36 +080051#include <linux/phy.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080052#include <linux/netdevice.h>
53#include <linux/etherdevice.h>
54#include <linux/skbuff.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080055#include <linux/platform_device.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080056
57#include <asm/dma.h>
58#include <linux/dma-mapping.h>
59
60#include <asm/blackfin.h>
61#include <asm/cacheflush.h>
62#include <asm/portmux.h>
63
64#include "bfin_mac.h"
65
66#define DRV_NAME "bfin_mac"
67#define DRV_VERSION "1.1"
68#define DRV_AUTHOR "Bryan Wu, Luke Yang"
69#define DRV_DESC "Blackfin BF53[67] on-chip Ethernet MAC driver"
70
71MODULE_AUTHOR(DRV_AUTHOR);
72MODULE_LICENSE("GPL");
73MODULE_DESCRIPTION(DRV_DESC);
74
75#if defined(CONFIG_BFIN_MAC_USE_L1)
76# define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size)
77# define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr)
78#else
79# define bfin_mac_alloc(dma_handle, size) \
80 dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
81# define bfin_mac_free(dma_handle, ptr) \
82 dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
83#endif
84
85#define PKT_BUF_SZ 1580
86
87#define MAX_TIMEOUT_CNT 500
88
89/* pointers to maintain transmit list */
90static struct net_dma_desc_tx *tx_list_head;
91static struct net_dma_desc_tx *tx_list_tail;
92static struct net_dma_desc_rx *rx_list_head;
93static struct net_dma_desc_rx *rx_list_tail;
94static struct net_dma_desc_rx *current_rx_ptr;
95static struct net_dma_desc_tx *current_tx_ptr;
96static struct net_dma_desc_tx *tx_desc;
97static struct net_dma_desc_rx *rx_desc;
98
Bryan Wu4ae5a3a2007-09-19 23:37:36 +080099static void bf537mac_disable(void);
100static void bf537mac_enable(void);
101
Bryan Wue190d6b2007-07-17 14:43:44 +0800102static 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
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800290/*
291 * MII operations
292 */
Bryan Wue190d6b2007-07-17 14:43:44 +0800293/* Wait until the previous MDC/MDIO transaction has completed */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800294static void mdio_poll(void)
Bryan Wue190d6b2007-07-17 14:43:44 +0800295{
296 int timeout_cnt = MAX_TIMEOUT_CNT;
297
298 /* poll the STABUSY bit */
299 while ((bfin_read_EMAC_STAADD()) & STABUSY) {
Bryan Wu6db9e462008-01-30 16:52:21 +0800300 udelay(1);
Bryan Wue190d6b2007-07-17 14:43:44 +0800301 if (timeout_cnt-- < 0) {
302 printk(KERN_ERR DRV_NAME
303 ": wait MDC/MDIO transaction to complete timeout\n");
304 break;
305 }
306 }
307}
308
309/* Read an off-chip register in a PHY through the MDC/MDIO port */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800310static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
Bryan Wue190d6b2007-07-17 14:43:44 +0800311{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800312 mdio_poll();
Bryan Wue190d6b2007-07-17 14:43:44 +0800313
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800314 /* read mode */
315 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
316 SET_REGAD((u16) regnum) |
317 STABUSY);
318
319 mdio_poll();
320
321 return (int) bfin_read_EMAC_STADAT();
Bryan Wue190d6b2007-07-17 14:43:44 +0800322}
323
324/* Write an off-chip register in a PHY through the MDC/MDIO port */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800325static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
326 u16 value)
Bryan Wue190d6b2007-07-17 14:43:44 +0800327{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800328 mdio_poll();
329
330 bfin_write_EMAC_STADAT((u32) value);
Bryan Wue190d6b2007-07-17 14:43:44 +0800331
332 /* write mode */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800333 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
334 SET_REGAD((u16) regnum) |
Bryan Wue190d6b2007-07-17 14:43:44 +0800335 STAOP |
336 STABUSY);
337
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800338 mdio_poll();
339
340 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800341}
342
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800343static int mdiobus_reset(struct mii_bus *bus)
Bryan Wue190d6b2007-07-17 14:43:44 +0800344{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800345 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800346}
347
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800348static void bf537_adjust_link(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800349{
Bryan Wue190d6b2007-07-17 14:43:44 +0800350 struct bf537mac_local *lp = netdev_priv(dev);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800351 struct phy_device *phydev = lp->phydev;
352 unsigned long flags;
353 int new_state = 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800354
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800355 spin_lock_irqsave(&lp->lock, flags);
356 if (phydev->link) {
357 /* Now we make sure that we can be in full duplex mode.
358 * If not, we operate in half-duplex mode. */
359 if (phydev->duplex != lp->old_duplex) {
360 u32 opmode = bfin_read_EMAC_OPMODE();
361 new_state = 1;
Bryan Wue190d6b2007-07-17 14:43:44 +0800362
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800363 if (phydev->duplex)
364 opmode |= FDMODE;
365 else
366 opmode &= ~(FDMODE);
Bryan Wue190d6b2007-07-17 14:43:44 +0800367
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800368 bfin_write_EMAC_OPMODE(opmode);
369 lp->old_duplex = phydev->duplex;
370 }
Bryan Wue190d6b2007-07-17 14:43:44 +0800371
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800372 if (phydev->speed != lp->old_speed) {
373#if defined(CONFIG_BFIN_MAC_RMII)
374 u32 opmode = bfin_read_EMAC_OPMODE();
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800375 switch (phydev->speed) {
376 case 10:
377 opmode |= RMII_10;
378 break;
379 case 100:
380 opmode &= ~(RMII_10);
381 break;
382 default:
383 printk(KERN_WARNING
384 "%s: Ack! Speed (%d) is not 10/100!\n",
385 DRV_NAME, phydev->speed);
386 break;
387 }
388 bfin_write_EMAC_OPMODE(opmode);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800389#endif
Bryan Wue190d6b2007-07-17 14:43:44 +0800390
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800391 new_state = 1;
392 lp->old_speed = phydev->speed;
393 }
Bryan Wue190d6b2007-07-17 14:43:44 +0800394
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800395 if (!lp->old_link) {
396 new_state = 1;
397 lp->old_link = 1;
398 netif_schedule(dev);
399 }
400 } else if (lp->old_link) {
401 new_state = 1;
402 lp->old_link = 0;
403 lp->old_speed = 0;
404 lp->old_duplex = -1;
Bryan Wue190d6b2007-07-17 14:43:44 +0800405 }
406
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800407 if (new_state) {
408 u32 opmode = bfin_read_EMAC_OPMODE();
409 phy_print_status(phydev);
410 pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
Bryan Wue190d6b2007-07-17 14:43:44 +0800411 }
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800412
413 spin_unlock_irqrestore(&lp->lock, flags);
414}
415
Bryan Wu7cc8f382008-01-30 16:52:22 +0800416/* MDC = 2.5 MHz */
417#define MDC_CLK 2500000
418
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800419static int mii_probe(struct net_device *dev)
420{
421 struct bf537mac_local *lp = netdev_priv(dev);
422 struct phy_device *phydev = NULL;
423 unsigned short sysctl;
424 int i;
Bryan Wu7cc8f382008-01-30 16:52:22 +0800425 u32 sclk, mdc_div;
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800426
427 /* Enable PHY output early */
428 if (!(bfin_read_VR_CTL() & PHYCLKOE))
429 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
430
Bryan Wu7cc8f382008-01-30 16:52:22 +0800431 sclk = get_sclk();
432 mdc_div = ((sclk / MDC_CLK) / 2) - 1;
433
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800434 sysctl = bfin_read_EMAC_SYSCTL();
Bryan Wu7cc8f382008-01-30 16:52:22 +0800435 sysctl |= SET_MDCDIV(mdc_div);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800436 bfin_write_EMAC_SYSCTL(sysctl);
437
438 /* search for connect PHY device */
439 for (i = 0; i < PHY_MAX_ADDR; i++) {
440 struct phy_device *const tmp_phydev = lp->mii_bus.phy_map[i];
441
442 if (!tmp_phydev)
443 continue; /* no PHY here... */
444
445 phydev = tmp_phydev;
446 break; /* found it */
447 }
448
449 /* now we are supposed to have a proper phydev, to attach to... */
450 if (!phydev) {
451 printk(KERN_INFO "%s: Don't found any phy device at all\n",
452 dev->name);
453 return -ENODEV;
454 }
455
456#if defined(CONFIG_BFIN_MAC_RMII)
457 phydev = phy_connect(dev, phydev->dev.bus_id, &bf537_adjust_link, 0,
458 PHY_INTERFACE_MODE_RMII);
459#else
460 phydev = phy_connect(dev, phydev->dev.bus_id, &bf537_adjust_link, 0,
461 PHY_INTERFACE_MODE_MII);
462#endif
463
464 if (IS_ERR(phydev)) {
465 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
466 return PTR_ERR(phydev);
467 }
468
469 /* mask with MAC supported features */
470 phydev->supported &= (SUPPORTED_10baseT_Half
471 | SUPPORTED_10baseT_Full
472 | SUPPORTED_100baseT_Half
473 | SUPPORTED_100baseT_Full
474 | SUPPORTED_Autoneg
475 | SUPPORTED_Pause | SUPPORTED_Asym_Pause
476 | SUPPORTED_MII
477 | SUPPORTED_TP);
478
479 phydev->advertising = phydev->supported;
480
481 lp->old_link = 0;
482 lp->old_speed = 0;
483 lp->old_duplex = -1;
484 lp->phydev = phydev;
485
486 printk(KERN_INFO "%s: attached PHY driver [%s] "
Bryan Wu7cc8f382008-01-30 16:52:22 +0800487 "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
488 "@sclk=%dMHz)\n",
489 DRV_NAME, phydev->drv->name, phydev->dev.bus_id, phydev->irq,
490 MDC_CLK, mdc_div, sclk/1000000);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800491
492 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800493}
494
495/**************************************************************************/
496void setup_system_regs(struct net_device *dev)
497{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800498 unsigned short sysctl;
Bryan Wue190d6b2007-07-17 14:43:44 +0800499
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800500 /*
501 * Odd word alignment for Receive Frame DMA word
502 * Configure checksum support and rcve frame word alignment
503 */
504 sysctl = bfin_read_EMAC_SYSCTL();
Bryan Wue190d6b2007-07-17 14:43:44 +0800505#if defined(BFIN_MAC_CSUM_OFFLOAD)
506 sysctl |= RXDWA | RXCKS;
507#else
508 sysctl |= RXDWA;
509#endif
510 bfin_write_EMAC_SYSCTL(sysctl);
Bryan Wue190d6b2007-07-17 14:43:44 +0800511
512 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
513
514 /* Initialize the TX DMA channel registers */
515 bfin_write_DMA2_X_COUNT(0);
516 bfin_write_DMA2_X_MODIFY(4);
517 bfin_write_DMA2_Y_COUNT(0);
518 bfin_write_DMA2_Y_MODIFY(0);
519
520 /* Initialize the RX DMA channel registers */
521 bfin_write_DMA1_X_COUNT(0);
522 bfin_write_DMA1_X_MODIFY(4);
523 bfin_write_DMA1_Y_COUNT(0);
524 bfin_write_DMA1_Y_MODIFY(0);
525}
526
Alex Landau73f83182007-09-19 23:14:18 +0800527static void setup_mac_addr(u8 *mac_addr)
Bryan Wue190d6b2007-07-17 14:43:44 +0800528{
529 u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
530 u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
531
532 /* this depends on a little-endian machine */
533 bfin_write_EMAC_ADDRLO(addr_low);
534 bfin_write_EMAC_ADDRHI(addr_hi);
535}
536
Alex Landau73f83182007-09-19 23:14:18 +0800537static int bf537mac_set_mac_address(struct net_device *dev, void *p)
538{
539 struct sockaddr *addr = p;
540 if (netif_running(dev))
541 return -EBUSY;
542 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
543 setup_mac_addr(dev->dev_addr);
544 return 0;
545}
546
Bryan Wue190d6b2007-07-17 14:43:44 +0800547static void adjust_tx_list(void)
548{
549 int timeout_cnt = MAX_TIMEOUT_CNT;
550
551 if (tx_list_head->status.status_word != 0
552 && current_tx_ptr != tx_list_head) {
553 goto adjust_head; /* released something, just return; */
554 }
555
556 /*
557 * if nothing released, check wait condition
558 * current's next can not be the head,
559 * otherwise the dma will not stop as we want
560 */
561 if (current_tx_ptr->next->next == tx_list_head) {
562 while (tx_list_head->status.status_word == 0) {
Bryan Wu6db9e462008-01-30 16:52:21 +0800563 mdelay(1);
Bryan Wue190d6b2007-07-17 14:43:44 +0800564 if (tx_list_head->status.status_word != 0
565 || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
566 goto adjust_head;
567 }
568 if (timeout_cnt-- < 0) {
569 printk(KERN_ERR DRV_NAME
570 ": wait for adjust tx list head timeout\n");
571 break;
572 }
573 }
574 if (tx_list_head->status.status_word != 0) {
575 goto adjust_head;
576 }
577 }
578
579 return;
580
581adjust_head:
582 do {
583 tx_list_head->desc_a.config &= ~DMAEN;
584 tx_list_head->status.status_word = 0;
585 if (tx_list_head->skb) {
586 dev_kfree_skb(tx_list_head->skb);
587 tx_list_head->skb = NULL;
588 } else {
589 printk(KERN_ERR DRV_NAME
590 ": no sk_buff in a transmitted frame!\n");
591 }
592 tx_list_head = tx_list_head->next;
593 } while (tx_list_head->status.status_word != 0
594 && current_tx_ptr != tx_list_head);
595 return;
596
597}
598
599static int bf537mac_hard_start_xmit(struct sk_buff *skb,
600 struct net_device *dev)
601{
602 struct bf537mac_local *lp = netdev_priv(dev);
603 unsigned int data;
604
605 current_tx_ptr->skb = skb;
606
607 /*
608 * Is skb->data always 16-bit aligned?
609 * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
610 */
611 if ((((unsigned int)(skb->data)) & 0x02) == 2) {
612 /* move skb->data to current_tx_ptr payload */
613 data = (unsigned int)(skb->data) - 2;
614 *((unsigned short *)data) = (unsigned short)(skb->len);
615 current_tx_ptr->desc_a.start_addr = (unsigned long)data;
616 /* this is important! */
617 blackfin_dcache_flush_range(data, (data + (skb->len)) + 2);
618
619 } else {
620 *((unsigned short *)(current_tx_ptr->packet)) =
621 (unsigned short)(skb->len);
622 memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
623 (skb->len));
624 current_tx_ptr->desc_a.start_addr =
625 (unsigned long)current_tx_ptr->packet;
626 if (current_tx_ptr->status.status_word != 0)
627 current_tx_ptr->status.status_word = 0;
628 blackfin_dcache_flush_range((unsigned int)current_tx_ptr->
629 packet,
630 (unsigned int)(current_tx_ptr->
631 packet + skb->len) +
632 2);
633 }
634
635 /* enable this packet's dma */
636 current_tx_ptr->desc_a.config |= DMAEN;
637
638 /* tx dma is running, just return */
639 if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
640 goto out;
641
642 /* tx dma is not running */
643 bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
644 /* dma enabled, read from memory, size is 6 */
645 bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
646 /* Turn on the EMAC tx */
647 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
648
649out:
650 adjust_tx_list();
651 current_tx_ptr = current_tx_ptr->next;
652 dev->trans_start = jiffies;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700653 dev->stats.tx_packets++;
654 dev->stats.tx_bytes += (skb->len);
Bryan Wue190d6b2007-07-17 14:43:44 +0800655 return 0;
656}
657
658static void bf537mac_rx(struct net_device *dev)
659{
660 struct sk_buff *skb, *new_skb;
661 struct bf537mac_local *lp = netdev_priv(dev);
662 unsigned short len;
663
664 /* allocate a new skb for next time receive */
665 skb = current_rx_ptr->skb;
666 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
667 if (!new_skb) {
668 printk(KERN_NOTICE DRV_NAME
669 ": rx: low on mem - packet dropped\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700670 dev->stats.rx_dropped++;
Bryan Wue190d6b2007-07-17 14:43:44 +0800671 goto out;
672 }
673 /* reserve 2 bytes for RXDWA padding */
674 skb_reserve(new_skb, 2);
675 current_rx_ptr->skb = new_skb;
676 current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
677
678 len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
679 skb_put(skb, len);
680 blackfin_dcache_invalidate_range((unsigned long)skb->head,
681 (unsigned long)skb->tail);
682
683 dev->last_rx = jiffies;
684 skb->dev = dev;
685 skb->protocol = eth_type_trans(skb, dev);
686#if defined(BFIN_MAC_CSUM_OFFLOAD)
687 skb->csum = current_rx_ptr->status.ip_payload_csum;
Vitja Makarov00ff49a2007-11-23 17:55:51 +0800688 skb->ip_summed = CHECKSUM_COMPLETE;
Bryan Wue190d6b2007-07-17 14:43:44 +0800689#endif
690
691 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700692 dev->stats.rx_packets++;
693 dev->stats.rx_bytes += len;
Bryan Wue190d6b2007-07-17 14:43:44 +0800694 current_rx_ptr->status.status_word = 0x00000000;
695 current_rx_ptr = current_rx_ptr->next;
696
697out:
698 return;
699}
700
701/* interrupt routine to handle rx and error signal */
702static irqreturn_t bf537mac_interrupt(int irq, void *dev_id)
703{
704 struct net_device *dev = dev_id;
705 int number = 0;
706
707get_one_packet:
708 if (current_rx_ptr->status.status_word == 0) {
709 /* no more new packet received */
710 if (number == 0) {
711 if (current_rx_ptr->next->status.status_word != 0) {
712 current_rx_ptr = current_rx_ptr->next;
713 goto real_rx;
714 }
715 }
716 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
717 DMA_DONE | DMA_ERR);
718 return IRQ_HANDLED;
719 }
720
721real_rx:
722 bf537mac_rx(dev);
723 number++;
724 goto get_one_packet;
725}
726
727#ifdef CONFIG_NET_POLL_CONTROLLER
728static void bf537mac_poll(struct net_device *dev)
729{
730 disable_irq(IRQ_MAC_RX);
731 bf537mac_interrupt(IRQ_MAC_RX, dev);
732 enable_irq(IRQ_MAC_RX);
733}
734#endif /* CONFIG_NET_POLL_CONTROLLER */
735
Bryan Wu496a34c2007-09-19 23:37:14 +0800736static void bf537mac_disable(void)
Bryan Wue190d6b2007-07-17 14:43:44 +0800737{
738 unsigned int opmode;
739
740 opmode = bfin_read_EMAC_OPMODE();
741 opmode &= (~RE);
742 opmode &= (~TE);
743 /* Turn off the EMAC */
744 bfin_write_EMAC_OPMODE(opmode);
745}
746
747/*
748 * Enable Interrupts, Receive, and Transmit
749 */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800750static void bf537mac_enable(void)
Bryan Wue190d6b2007-07-17 14:43:44 +0800751{
752 u32 opmode;
753
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800754 pr_debug("%s: %s\n", DRV_NAME, __FUNCTION__);
Bryan Wue190d6b2007-07-17 14:43:44 +0800755
756 /* Set RX DMA */
757 bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
758 bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
759
760 /* Wait MII done */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800761 mdio_poll();
Bryan Wue190d6b2007-07-17 14:43:44 +0800762
763 /* We enable only RX here */
764 /* ASTP : Enable Automatic Pad Stripping
765 PR : Promiscuous Mode for test
766 PSF : Receive frames with total length less than 64 bytes.
767 FDMODE : Full Duplex Mode
768 LB : Internal Loopback for test
769 RE : Receiver Enable */
770 opmode = bfin_read_EMAC_OPMODE();
771 if (opmode & FDMODE)
772 opmode |= PSF;
773 else
774 opmode |= DRO | DC | PSF;
775 opmode |= RE;
776
777#if defined(CONFIG_BFIN_MAC_RMII)
778 opmode |= RMII; /* For Now only 100MBit are supported */
779#ifdef CONFIG_BF_REV_0_2
780 opmode |= TE;
781#endif
782#endif
783 /* Turn on the EMAC rx */
784 bfin_write_EMAC_OPMODE(opmode);
Bryan Wue190d6b2007-07-17 14:43:44 +0800785}
786
787/* Our watchdog timed out. Called by the networking layer */
788static void bf537mac_timeout(struct net_device *dev)
789{
790 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
791
Bryan Wu496a34c2007-09-19 23:37:14 +0800792 bf537mac_disable();
Bryan Wue190d6b2007-07-17 14:43:44 +0800793
794 /* reset tx queue */
795 tx_list_tail = tx_list_head->next;
796
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800797 bf537mac_enable();
Bryan Wue190d6b2007-07-17 14:43:44 +0800798
799 /* We can accept TX packets again */
800 dev->trans_start = jiffies;
801 netif_wake_queue(dev);
802}
803
Aidan Williams775919b2008-01-30 16:52:23 +0800804static void bf537mac_multicast_hash(struct net_device *dev)
805{
806 u32 emac_hashhi, emac_hashlo;
807 struct dev_mc_list *dmi = dev->mc_list;
808 char *addrs;
809 int i;
810 u32 crc;
811
812 emac_hashhi = emac_hashlo = 0;
813
814 for (i = 0; i < dev->mc_count; i++) {
815 addrs = dmi->dmi_addr;
816 dmi = dmi->next;
817
818 /* skip non-multicast addresses */
819 if (!(*addrs & 1))
820 continue;
821
822 crc = ether_crc(ETH_ALEN, addrs);
823 crc >>= 26;
824
825 if (crc & 0x20)
826 emac_hashhi |= 1 << (crc & 0x1f);
827 else
828 emac_hashlo |= 1 << (crc & 0x1f);
829 }
830
831 bfin_write_EMAC_HASHHI(emac_hashhi);
832 bfin_write_EMAC_HASHLO(emac_hashlo);
833
834 return;
835}
836
Bryan Wue190d6b2007-07-17 14:43:44 +0800837/*
Bryan Wue190d6b2007-07-17 14:43:44 +0800838 * This routine will, depending on the values passed to it,
839 * either make it accept multicast packets, go into
840 * promiscuous mode (for TCPDUMP and cousins) or accept
841 * a select set of multicast packets
842 */
843static void bf537mac_set_multicast_list(struct net_device *dev)
844{
845 u32 sysctl;
846
847 if (dev->flags & IFF_PROMISC) {
848 printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
849 sysctl = bfin_read_EMAC_OPMODE();
850 sysctl |= RAF;
851 bfin_write_EMAC_OPMODE(sysctl);
Aidan Williams775919b2008-01-30 16:52:23 +0800852 } else if (dev->flags & IFF_ALLMULTI) {
Bryan Wue190d6b2007-07-17 14:43:44 +0800853 /* accept all multicast */
854 sysctl = bfin_read_EMAC_OPMODE();
855 sysctl |= PAM;
856 bfin_write_EMAC_OPMODE(sysctl);
Aidan Williams775919b2008-01-30 16:52:23 +0800857 } else if (dev->mc_count) {
858 /* set up multicast hash table */
859 sysctl = bfin_read_EMAC_OPMODE();
860 sysctl |= HM;
861 bfin_write_EMAC_OPMODE(sysctl);
862 bf537mac_multicast_hash(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800863 } else {
864 /* clear promisc or multicast mode */
865 sysctl = bfin_read_EMAC_OPMODE();
866 sysctl &= ~(RAF | PAM);
867 bfin_write_EMAC_OPMODE(sysctl);
868 }
869}
870
871/*
872 * this puts the device in an inactive state
873 */
874static void bf537mac_shutdown(struct net_device *dev)
875{
876 /* Turn off the EMAC */
877 bfin_write_EMAC_OPMODE(0x00000000);
878 /* Turn off the EMAC RX DMA */
879 bfin_write_DMA1_CONFIG(0x0000);
880 bfin_write_DMA2_CONFIG(0x0000);
881}
882
883/*
884 * Open and Initialize the interface
885 *
886 * Set up everything, reset the card, etc..
887 */
888static int bf537mac_open(struct net_device *dev)
889{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800890 struct bf537mac_local *lp = netdev_priv(dev);
Michael Hennerich4af4b842007-07-25 14:09:54 +0800891 int retval;
Bryan Wue190d6b2007-07-17 14:43:44 +0800892 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
893
894 /*
895 * Check that the address is valid. If its not, refuse
896 * to bring the device up. The user must specify an
897 * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
898 */
899 if (!is_valid_ether_addr(dev->dev_addr)) {
900 printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
901 return -EINVAL;
902 }
903
904 /* initial rx and tx list */
Michael Hennerich4af4b842007-07-25 14:09:54 +0800905 retval = desc_list_init();
906
907 if (retval)
908 return retval;
Bryan Wue190d6b2007-07-17 14:43:44 +0800909
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800910 phy_start(lp->phydev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800911 setup_system_regs(dev);
Bryan Wu496a34c2007-09-19 23:37:14 +0800912 bf537mac_disable();
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800913 bf537mac_enable();
Bryan Wue190d6b2007-07-17 14:43:44 +0800914
915 pr_debug("hardware init finished\n");
916 netif_start_queue(dev);
917 netif_carrier_on(dev);
918
919 return 0;
920}
921
922/*
923 *
924 * this makes the board clean up everything that it can
925 * and not talk to the outside world. Caused by
926 * an 'ifconfig ethX down'
927 */
928static int bf537mac_close(struct net_device *dev)
929{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800930 struct bf537mac_local *lp = netdev_priv(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800931 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
932
933 netif_stop_queue(dev);
934 netif_carrier_off(dev);
935
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800936 phy_stop(lp->phydev);
937
Bryan Wue190d6b2007-07-17 14:43:44 +0800938 /* clear everything */
939 bf537mac_shutdown(dev);
940
941 /* free the rx/tx buffers */
942 desc_list_free();
943
944 return 0;
945}
946
947static int __init bf537mac_probe(struct net_device *dev)
948{
949 struct bf537mac_local *lp = netdev_priv(dev);
950 int retval;
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800951 int i;
Bryan Wue190d6b2007-07-17 14:43:44 +0800952
953 /* Grab the MAC address in the MAC */
954 *(__le32 *) (&(dev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
955 *(__le16 *) (&(dev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
956
957 /* probe mac */
958 /*todo: how to proble? which is revision_register */
959 bfin_write_EMAC_ADDRLO(0x12345678);
960 if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
961 pr_debug("can't detect bf537 mac!\n");
962 retval = -ENODEV;
963 goto err_out;
964 }
965
966 /* set the GPIO pins to Ethernet mode */
967 retval = setup_pin_mux(1);
Bryan Wue190d6b2007-07-17 14:43:44 +0800968 if (retval)
969 return retval;
970
971 /*Is it valid? (Did bootloader initialize it?) */
972 if (!is_valid_ether_addr(dev->dev_addr)) {
973 /* Grab the MAC from the board somehow - this is done in the
974 arch/blackfin/mach-bf537/boards/eth_mac.c */
Mike Frysinger9862cc52007-11-15 21:21:20 +0800975 bfin_get_ether_addr(dev->dev_addr);
Bryan Wue190d6b2007-07-17 14:43:44 +0800976 }
977
978 /* If still not valid, get a random one */
979 if (!is_valid_ether_addr(dev->dev_addr)) {
980 random_ether_addr(dev->dev_addr);
981 }
982
983 setup_mac_addr(dev->dev_addr);
984
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800985 /* MDIO bus initial */
986 lp->mii_bus.priv = dev;
987 lp->mii_bus.read = mdiobus_read;
988 lp->mii_bus.write = mdiobus_write;
989 lp->mii_bus.reset = mdiobus_reset;
990 lp->mii_bus.name = "bfin_mac_mdio";
991 lp->mii_bus.id = 0;
992 lp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
993 for (i = 0; i < PHY_MAX_ADDR; ++i)
994 lp->mii_bus.irq[i] = PHY_POLL;
995
996 mdiobus_register(&lp->mii_bus);
997
998 retval = mii_probe(dev);
999 if (retval)
1000 return retval;
1001
Bryan Wue190d6b2007-07-17 14:43:44 +08001002 /* Fill in the fields of the device structure with ethernet values. */
1003 ether_setup(dev);
1004
1005 dev->open = bf537mac_open;
1006 dev->stop = bf537mac_close;
1007 dev->hard_start_xmit = bf537mac_hard_start_xmit;
Alex Landau73f83182007-09-19 23:14:18 +08001008 dev->set_mac_address = bf537mac_set_mac_address;
Bryan Wue190d6b2007-07-17 14:43:44 +08001009 dev->tx_timeout = bf537mac_timeout;
Bryan Wue190d6b2007-07-17 14:43:44 +08001010 dev->set_multicast_list = bf537mac_set_multicast_list;
1011#ifdef CONFIG_NET_POLL_CONTROLLER
1012 dev->poll_controller = bf537mac_poll;
1013#endif
1014
Bryan Wue190d6b2007-07-17 14:43:44 +08001015 spin_lock_init(&lp->lock);
1016
1017 /* now, enable interrupts */
1018 /* register irq handler */
1019 if (request_irq
1020 (IRQ_MAC_RX, bf537mac_interrupt, IRQF_DISABLED | IRQF_SHARED,
1021 "BFIN537_MAC_RX", dev)) {
1022 printk(KERN_WARNING DRV_NAME
1023 ": Unable to attach BlackFin MAC RX interrupt\n");
1024 return -EBUSY;
1025 }
1026
Bryan Wue190d6b2007-07-17 14:43:44 +08001027
1028 retval = register_netdev(dev);
1029 if (retval == 0) {
1030 /* now, print out the card info, in a short format.. */
1031 printk(KERN_INFO "%s: Version %s, %s\n",
1032 DRV_NAME, DRV_VERSION, DRV_DESC);
1033 }
1034
1035err_out:
1036 return retval;
1037}
1038
1039static int bfin_mac_probe(struct platform_device *pdev)
1040{
1041 struct net_device *ndev;
1042
1043 ndev = alloc_etherdev(sizeof(struct bf537mac_local));
1044 if (!ndev) {
1045 printk(KERN_WARNING DRV_NAME ": could not allocate device\n");
1046 return -ENOMEM;
1047 }
1048
Bryan Wue190d6b2007-07-17 14:43:44 +08001049 SET_NETDEV_DEV(ndev, &pdev->dev);
1050
1051 platform_set_drvdata(pdev, ndev);
1052
1053 if (bf537mac_probe(ndev) != 0) {
1054 platform_set_drvdata(pdev, NULL);
1055 free_netdev(ndev);
1056 printk(KERN_WARNING DRV_NAME ": not found\n");
1057 return -ENODEV;
1058 }
1059
1060 return 0;
1061}
1062
1063static int bfin_mac_remove(struct platform_device *pdev)
1064{
1065 struct net_device *ndev = platform_get_drvdata(pdev);
1066
1067 platform_set_drvdata(pdev, NULL);
1068
1069 unregister_netdev(ndev);
1070
1071 free_irq(IRQ_MAC_RX, ndev);
1072
1073 free_netdev(ndev);
1074
1075 setup_pin_mux(0);
1076
1077 return 0;
1078}
1079
Bryan Wu496a34c2007-09-19 23:37:14 +08001080#ifdef CONFIG_PM
1081static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
Bryan Wue190d6b2007-07-17 14:43:44 +08001082{
Bryan Wu496a34c2007-09-19 23:37:14 +08001083 struct net_device *net_dev = platform_get_drvdata(pdev);
1084
1085 if (netif_running(net_dev))
1086 bf537mac_close(net_dev);
1087
Bryan Wue190d6b2007-07-17 14:43:44 +08001088 return 0;
1089}
1090
1091static int bfin_mac_resume(struct platform_device *pdev)
1092{
Bryan Wu496a34c2007-09-19 23:37:14 +08001093 struct net_device *net_dev = platform_get_drvdata(pdev);
1094
1095 if (netif_running(net_dev))
1096 bf537mac_open(net_dev);
1097
Bryan Wue190d6b2007-07-17 14:43:44 +08001098 return 0;
1099}
Bryan Wu496a34c2007-09-19 23:37:14 +08001100#else
1101#define bfin_mac_suspend NULL
1102#define bfin_mac_resume NULL
1103#endif /* CONFIG_PM */
Bryan Wue190d6b2007-07-17 14:43:44 +08001104
1105static struct platform_driver bfin_mac_driver = {
1106 .probe = bfin_mac_probe,
1107 .remove = bfin_mac_remove,
1108 .resume = bfin_mac_resume,
1109 .suspend = bfin_mac_suspend,
1110 .driver = {
1111 .name = DRV_NAME,
1112 },
1113};
1114
1115static int __init bfin_mac_init(void)
1116{
1117 return platform_driver_register(&bfin_mac_driver);
1118}
1119
1120module_init(bfin_mac_init);
1121
1122static void __exit bfin_mac_cleanup(void)
1123{
1124 platform_driver_unregister(&bfin_mac_driver);
1125}
1126
1127module_exit(bfin_mac_cleanup);