netdev: bfin_mac: add support for IEEE 1588 PTP
[deliverable/linux.git] / drivers / net / bfin_mac.c
CommitLineData
e190d6b1 1/*
2fb9d6f5 2 * Blackfin On-Chip MAC Driver
e190d6b1 3 *
2fb9d6f5 4 * Copyright 2004-2007 Analog Devices Inc.
e190d6b1 5 *
2fb9d6f5 6 * Enter bugs at http://blackfin.uclinux.org/
e190d6b1 7 *
2fb9d6f5 8 * Licensed under the GPL-2 or later.
e190d6b1
BW
9 */
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>
e190d6b1 25#include <linux/mii.h>
4ae5a3ad 26#include <linux/phy.h>
e190d6b1
BW
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
679dce39 29#include <linux/ethtool.h>
e190d6b1 30#include <linux/skbuff.h>
e190d6b1 31#include <linux/platform_device.h>
e190d6b1
BW
32
33#include <asm/dma.h>
34#include <linux/dma-mapping.h>
35
fe92afed 36#include <asm/div64.h>
98f672ca 37#include <asm/dpmc.h>
e190d6b1
BW
38#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"
7ef0a7ee 47#define DRV_DESC "Blackfin on-chip Ethernet MAC driver"
e190d6b1
BW
48
49MODULE_AUTHOR(DRV_AUTHOR);
50MODULE_LICENSE("GPL");
51MODULE_DESCRIPTION(DRV_DESC);
72abb461 52MODULE_ALIAS("platform:bfin_mac");
e190d6b1
BW
53
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
7ef0a7ee
BW
78#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);
4ae5a3ad 86
e190d6b1
BW
87static 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 */
015dac88 199 new_skb = dev_alloc_skb(PKT_BUF_SZ + NET_IP_ALIGN);
e190d6b1
BW
200 if (!new_skb) {
201 printk(KERN_NOTICE DRV_NAME
202 ": init: low on mem - packet dropped\n");
203 goto init_error;
204 }
015dac88 205 skb_reserve(new_skb, NET_IP_ALIGN);
e190d6b1
BW
206 r->skb = new_skb;
207
208 /*
209 * enabled DMA
210 * write to memory WNR = 1
211 * wordsize is 32 bits
212 * disable interrupt
213 * 6 half words is desc size
214 * large desc flow
215 */
216 a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
217 /* since RXDWA is enabled */
218 a->start_addr = (unsigned long)new_skb->data - 2;
219 a->x_count = 0;
220 a->next_dma_desc = b;
221
222 /*
223 * enabled DMA
224 * write to memory WNR = 1
225 * wordsize is 32 bits
226 * enable interrupt
227 * 6 half words is desc size
228 * large desc flow
229 */
230 b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
231 NDSIZE_6 | DMAFLOW_LARGE;
232 b->start_addr = (unsigned long)(&(r->status));
233 b->x_count = 0;
234
235 rx_list_tail->desc_b.next_dma_desc = a;
236 rx_list_tail->next = r;
237 rx_list_tail = r;
238 }
239 rx_list_tail->next = rx_list_head; /* rx_list is a circle */
240 rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
241 current_rx_ptr = rx_list_head;
242
243 return 0;
244
245init_error:
246 desc_list_free();
247 printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
248 return -ENOMEM;
249}
250
251
252/*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
253
4ae5a3ad
BW
254/*
255 * MII operations
256 */
e190d6b1 257/* Wait until the previous MDC/MDIO transaction has completed */
0ed0563e 258static void bfin_mdio_poll(void)
e190d6b1
BW
259{
260 int timeout_cnt = MAX_TIMEOUT_CNT;
261
262 /* poll the STABUSY bit */
263 while ((bfin_read_EMAC_STAADD()) & STABUSY) {
6db9e461 264 udelay(1);
e190d6b1
BW
265 if (timeout_cnt-- < 0) {
266 printk(KERN_ERR DRV_NAME
267 ": wait MDC/MDIO transaction to complete timeout\n");
268 break;
269 }
270 }
271}
272
273/* Read an off-chip register in a PHY through the MDC/MDIO port */
0ed0563e 274static int bfin_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
e190d6b1 275{
0ed0563e 276 bfin_mdio_poll();
4ae5a3ad 277
e190d6b1 278 /* read mode */
4ae5a3ad
BW
279 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
280 SET_REGAD((u16) regnum) |
e190d6b1 281 STABUSY);
e190d6b1 282
0ed0563e 283 bfin_mdio_poll();
4ae5a3ad
BW
284
285 return (int) bfin_read_EMAC_STADAT();
e190d6b1
BW
286}
287
288/* Write an off-chip register in a PHY through the MDC/MDIO port */
0ed0563e
AB
289static int bfin_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
290 u16 value)
e190d6b1 291{
0ed0563e 292 bfin_mdio_poll();
4ae5a3ad
BW
293
294 bfin_write_EMAC_STADAT((u32) value);
e190d6b1
BW
295
296 /* write mode */
4ae5a3ad
BW
297 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
298 SET_REGAD((u16) regnum) |
e190d6b1
BW
299 STAOP |
300 STABUSY);
301
0ed0563e 302 bfin_mdio_poll();
4ae5a3ad
BW
303
304 return 0;
e190d6b1
BW
305}
306
0ed0563e 307static int bfin_mdiobus_reset(struct mii_bus *bus)
e190d6b1 308{
4ae5a3ad 309 return 0;
e190d6b1
BW
310}
311
7ef0a7ee 312static void bfin_mac_adjust_link(struct net_device *dev)
e190d6b1 313{
7ef0a7ee 314 struct bfin_mac_local *lp = netdev_priv(dev);
4ae5a3ad
BW
315 struct phy_device *phydev = lp->phydev;
316 unsigned long flags;
317 int new_state = 0;
318
319 spin_lock_irqsave(&lp->lock, flags);
320 if (phydev->link) {
321 /* Now we make sure that we can be in full duplex mode.
322 * If not, we operate in half-duplex mode. */
323 if (phydev->duplex != lp->old_duplex) {
324 u32 opmode = bfin_read_EMAC_OPMODE();
325 new_state = 1;
326
327 if (phydev->duplex)
328 opmode |= FDMODE;
329 else
330 opmode &= ~(FDMODE);
331
332 bfin_write_EMAC_OPMODE(opmode);
333 lp->old_duplex = phydev->duplex;
334 }
e190d6b1 335
4ae5a3ad
BW
336 if (phydev->speed != lp->old_speed) {
337#if defined(CONFIG_BFIN_MAC_RMII)
338 u32 opmode = bfin_read_EMAC_OPMODE();
4ae5a3ad
BW
339 switch (phydev->speed) {
340 case 10:
341 opmode |= RMII_10;
342 break;
343 case 100:
344 opmode &= ~(RMII_10);
345 break;
346 default:
347 printk(KERN_WARNING
348 "%s: Ack! Speed (%d) is not 10/100!\n",
349 DRV_NAME, phydev->speed);
350 break;
351 }
352 bfin_write_EMAC_OPMODE(opmode);
4ae5a3ad 353#endif
e190d6b1 354
4ae5a3ad
BW
355 new_state = 1;
356 lp->old_speed = phydev->speed;
357 }
e190d6b1 358
4ae5a3ad
BW
359 if (!lp->old_link) {
360 new_state = 1;
361 lp->old_link = 1;
4ae5a3ad
BW
362 }
363 } else if (lp->old_link) {
364 new_state = 1;
365 lp->old_link = 0;
366 lp->old_speed = 0;
367 lp->old_duplex = -1;
e190d6b1
BW
368 }
369
4ae5a3ad
BW
370 if (new_state) {
371 u32 opmode = bfin_read_EMAC_OPMODE();
372 phy_print_status(phydev);
373 pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
e190d6b1 374 }
4ae5a3ad
BW
375
376 spin_unlock_irqrestore(&lp->lock, flags);
e190d6b1
BW
377}
378
7cc8f381
BW
379/* MDC = 2.5 MHz */
380#define MDC_CLK 2500000
381
4ae5a3ad 382static int mii_probe(struct net_device *dev)
e190d6b1 383{
7ef0a7ee 384 struct bfin_mac_local *lp = netdev_priv(dev);
4ae5a3ad
BW
385 struct phy_device *phydev = NULL;
386 unsigned short sysctl;
387 int i;
7cc8f381 388 u32 sclk, mdc_div;
e190d6b1 389
4ae5a3ad 390 /* Enable PHY output early */
98f672ca
MF
391 if (!(bfin_read_VR_CTL() & CLKBUFOE))
392 bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE);
e190d6b1 393
7cc8f381
BW
394 sclk = get_sclk();
395 mdc_div = ((sclk / MDC_CLK) / 2) - 1;
396
4ae5a3ad 397 sysctl = bfin_read_EMAC_SYSCTL();
9dc7f30e 398 sysctl = (sysctl & ~MDCDIV) | SET_MDCDIV(mdc_div);
e190d6b1 399 bfin_write_EMAC_SYSCTL(sysctl);
e190d6b1 400
4ae5a3ad
BW
401 /* search for connect PHY device */
402 for (i = 0; i < PHY_MAX_ADDR; i++) {
298cf9be 403 struct phy_device *const tmp_phydev = lp->mii_bus->phy_map[i];
e190d6b1 404
4ae5a3ad
BW
405 if (!tmp_phydev)
406 continue; /* no PHY here... */
e190d6b1 407
4ae5a3ad
BW
408 phydev = tmp_phydev;
409 break; /* found it */
410 }
411
412 /* now we are supposed to have a proper phydev, to attach to... */
413 if (!phydev) {
414 printk(KERN_INFO "%s: Don't found any phy device at all\n",
415 dev->name);
416 return -ENODEV;
e190d6b1
BW
417 }
418
419#if defined(CONFIG_BFIN_MAC_RMII)
c2313557
KS
420 phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link,
421 0, PHY_INTERFACE_MODE_RMII);
4ae5a3ad 422#else
c2313557
KS
423 phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link,
424 0, PHY_INTERFACE_MODE_MII);
e190d6b1
BW
425#endif
426
4ae5a3ad
BW
427 if (IS_ERR(phydev)) {
428 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
429 return PTR_ERR(phydev);
430 }
431
432 /* mask with MAC supported features */
433 phydev->supported &= (SUPPORTED_10baseT_Half
434 | SUPPORTED_10baseT_Full
435 | SUPPORTED_100baseT_Half
436 | SUPPORTED_100baseT_Full
437 | SUPPORTED_Autoneg
438 | SUPPORTED_Pause | SUPPORTED_Asym_Pause
439 | SUPPORTED_MII
440 | SUPPORTED_TP);
441
442 phydev->advertising = phydev->supported;
443
444 lp->old_link = 0;
445 lp->old_speed = 0;
446 lp->old_duplex = -1;
447 lp->phydev = phydev;
448
449 printk(KERN_INFO "%s: attached PHY driver [%s] "
7cc8f381
BW
450 "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
451 "@sclk=%dMHz)\n",
c2313557 452 DRV_NAME, phydev->drv->name, dev_name(&phydev->dev), phydev->irq,
7cc8f381 453 MDC_CLK, mdc_div, sclk/1000000);
4ae5a3ad
BW
454
455 return 0;
456}
457
679dce39
BW
458/*
459 * Ethtool support
460 */
461
462static int
463bfin_mac_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
464{
465 struct bfin_mac_local *lp = netdev_priv(dev);
466
467 if (lp->phydev)
468 return phy_ethtool_gset(lp->phydev, cmd);
469
470 return -EINVAL;
471}
472
473static int
474bfin_mac_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
475{
476 struct bfin_mac_local *lp = netdev_priv(dev);
477
478 if (!capable(CAP_NET_ADMIN))
479 return -EPERM;
480
481 if (lp->phydev)
482 return phy_ethtool_sset(lp->phydev, cmd);
483
484 return -EINVAL;
485}
486
487static void bfin_mac_ethtool_getdrvinfo(struct net_device *dev,
488 struct ethtool_drvinfo *info)
489{
490 strcpy(info->driver, DRV_NAME);
491 strcpy(info->version, DRV_VERSION);
492 strcpy(info->fw_version, "N/A");
c2313557 493 strcpy(info->bus_info, dev_name(&dev->dev));
679dce39
BW
494}
495
0fc0b732 496static const struct ethtool_ops bfin_mac_ethtool_ops = {
679dce39
BW
497 .get_settings = bfin_mac_ethtool_getsettings,
498 .set_settings = bfin_mac_ethtool_setsettings,
499 .get_link = ethtool_op_get_link,
500 .get_drvinfo = bfin_mac_ethtool_getdrvinfo,
501};
502
4ae5a3ad
BW
503/**************************************************************************/
504void setup_system_regs(struct net_device *dev)
505{
506 unsigned short sysctl;
507
508 /*
509 * Odd word alignment for Receive Frame DMA word
510 * Configure checksum support and rcve frame word alignment
511 */
512 sysctl = bfin_read_EMAC_SYSCTL();
513#if defined(BFIN_MAC_CSUM_OFFLOAD)
514 sysctl |= RXDWA | RXCKS;
515#else
516 sysctl |= RXDWA;
517#endif
518 bfin_write_EMAC_SYSCTL(sysctl);
e190d6b1
BW
519
520 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
521
522 /* Initialize the TX DMA channel registers */
523 bfin_write_DMA2_X_COUNT(0);
524 bfin_write_DMA2_X_MODIFY(4);
525 bfin_write_DMA2_Y_COUNT(0);
526 bfin_write_DMA2_Y_MODIFY(0);
527
528 /* Initialize the RX DMA channel registers */
529 bfin_write_DMA1_X_COUNT(0);
530 bfin_write_DMA1_X_MODIFY(4);
531 bfin_write_DMA1_Y_COUNT(0);
532 bfin_write_DMA1_Y_MODIFY(0);
533}
534
73f83182 535static void setup_mac_addr(u8 *mac_addr)
e190d6b1
BW
536{
537 u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
538 u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
539
540 /* this depends on a little-endian machine */
541 bfin_write_EMAC_ADDRLO(addr_low);
542 bfin_write_EMAC_ADDRHI(addr_hi);
543}
544
7ef0a7ee 545static int bfin_mac_set_mac_address(struct net_device *dev, void *p)
73f83182
AL
546{
547 struct sockaddr *addr = p;
548 if (netif_running(dev))
549 return -EBUSY;
550 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
551 setup_mac_addr(dev->dev_addr);
552 return 0;
553}
554
fe92afed
BS
555#ifdef CONFIG_BFIN_MAC_USE_HWSTAMP
556#define bfin_mac_hwtstamp_is_none(cfg) ((cfg) == HWTSTAMP_FILTER_NONE)
557
558static int bfin_mac_hwtstamp_ioctl(struct net_device *netdev,
559 struct ifreq *ifr, int cmd)
560{
561 struct hwtstamp_config config;
562 struct bfin_mac_local *lp = netdev_priv(netdev);
563 u16 ptpctl;
564 u32 ptpfv1, ptpfv2, ptpfv3, ptpfoff;
565
566 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
567 return -EFAULT;
568
569 pr_debug("%s config flag:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
570 __func__, config.flags, config.tx_type, config.rx_filter);
571
572 /* reserved for future extensions */
573 if (config.flags)
574 return -EINVAL;
575
576 if ((config.tx_type != HWTSTAMP_TX_OFF) &&
577 (config.tx_type != HWTSTAMP_TX_ON))
578 return -ERANGE;
579
580 ptpctl = bfin_read_EMAC_PTP_CTL();
581
582 switch (config.rx_filter) {
583 case HWTSTAMP_FILTER_NONE:
584 /*
585 * Dont allow any timestamping
586 */
587 ptpfv3 = 0xFFFFFFFF;
588 bfin_write_EMAC_PTP_FV3(ptpfv3);
589 break;
590 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
591 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
592 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
593 /*
594 * Clear the five comparison mask bits (bits[12:8]) in EMAC_PTP_CTL)
595 * to enable all the field matches.
596 */
597 ptpctl &= ~0x1F00;
598 bfin_write_EMAC_PTP_CTL(ptpctl);
599 /*
600 * Keep the default values of the EMAC_PTP_FOFF register.
601 */
602 ptpfoff = 0x4A24170C;
603 bfin_write_EMAC_PTP_FOFF(ptpfoff);
604 /*
605 * Keep the default values of the EMAC_PTP_FV1 and EMAC_PTP_FV2
606 * registers.
607 */
608 ptpfv1 = 0x11040800;
609 bfin_write_EMAC_PTP_FV1(ptpfv1);
610 ptpfv2 = 0x0140013F;
611 bfin_write_EMAC_PTP_FV2(ptpfv2);
612 /*
613 * The default value (0xFFFC) allows the timestamping of both
614 * received Sync messages and Delay_Req messages.
615 */
616 ptpfv3 = 0xFFFFFFFC;
617 bfin_write_EMAC_PTP_FV3(ptpfv3);
618
619 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
620 break;
621 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
622 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
623 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
624 /* Clear all five comparison mask bits (bits[12:8]) in the
625 * EMAC_PTP_CTL register to enable all the field matches.
626 */
627 ptpctl &= ~0x1F00;
628 bfin_write_EMAC_PTP_CTL(ptpctl);
629 /*
630 * Keep the default values of the EMAC_PTP_FOFF register, except set
631 * the PTPCOF field to 0x2A.
632 */
633 ptpfoff = 0x2A24170C;
634 bfin_write_EMAC_PTP_FOFF(ptpfoff);
635 /*
636 * Keep the default values of the EMAC_PTP_FV1 and EMAC_PTP_FV2
637 * registers.
638 */
639 ptpfv1 = 0x11040800;
640 bfin_write_EMAC_PTP_FV1(ptpfv1);
641 ptpfv2 = 0x0140013F;
642 bfin_write_EMAC_PTP_FV2(ptpfv2);
643 /*
644 * To allow the timestamping of Pdelay_Req and Pdelay_Resp, set
645 * the value to 0xFFF0.
646 */
647 ptpfv3 = 0xFFFFFFF0;
648 bfin_write_EMAC_PTP_FV3(ptpfv3);
649
650 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
651 break;
652 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
653 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
654 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
655 /*
656 * Clear bits 8 and 12 of the EMAC_PTP_CTL register to enable only the
657 * EFTM and PTPCM field comparison.
658 */
659 ptpctl &= ~0x1100;
660 bfin_write_EMAC_PTP_CTL(ptpctl);
661 /*
662 * Keep the default values of all the fields of the EMAC_PTP_FOFF
663 * register, except set the PTPCOF field to 0x0E.
664 */
665 ptpfoff = 0x0E24170C;
666 bfin_write_EMAC_PTP_FOFF(ptpfoff);
667 /*
668 * Program bits [15:0] of the EMAC_PTP_FV1 register to 0x88F7, which
669 * corresponds to PTP messages on the MAC layer.
670 */
671 ptpfv1 = 0x110488F7;
672 bfin_write_EMAC_PTP_FV1(ptpfv1);
673 ptpfv2 = 0x0140013F;
674 bfin_write_EMAC_PTP_FV2(ptpfv2);
675 /*
676 * To allow the timestamping of Pdelay_Req and Pdelay_Resp
677 * messages, set the value to 0xFFF0.
678 */
679 ptpfv3 = 0xFFFFFFF0;
680 bfin_write_EMAC_PTP_FV3(ptpfv3);
681
682 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
683 break;
684 default:
685 return -ERANGE;
686 }
687
688 if (config.tx_type == HWTSTAMP_TX_OFF &&
689 bfin_mac_hwtstamp_is_none(config.rx_filter)) {
690 ptpctl &= ~PTP_EN;
691 bfin_write_EMAC_PTP_CTL(ptpctl);
692
693 SSYNC();
694 } else {
695 ptpctl |= PTP_EN;
696 bfin_write_EMAC_PTP_CTL(ptpctl);
697
698 /*
699 * clear any existing timestamp
700 */
701 bfin_read_EMAC_PTP_RXSNAPLO();
702 bfin_read_EMAC_PTP_RXSNAPHI();
703
704 bfin_read_EMAC_PTP_TXSNAPLO();
705 bfin_read_EMAC_PTP_TXSNAPHI();
706
707 /*
708 * Set registers so that rollover occurs soon to test this.
709 */
710 bfin_write_EMAC_PTP_TIMELO(0x00000000);
711 bfin_write_EMAC_PTP_TIMEHI(0xFF800000);
712
713 SSYNC();
714
715 lp->compare.last_update = 0;
716 timecounter_init(&lp->clock,
717 &lp->cycles,
718 ktime_to_ns(ktime_get_real()));
719 timecompare_update(&lp->compare, 0);
720 }
721
722 lp->stamp_cfg = config;
723 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
724 -EFAULT : 0;
725}
726
727static void bfin_dump_hwtamp(char *s, ktime_t *hw, ktime_t *ts, struct timecompare *cmp)
728{
729 ktime_t sys = ktime_get_real();
730
731 pr_debug("%s %s hardware:%d,%d transform system:%d,%d system:%d,%d, cmp:%lld, %lld\n",
732 __func__, s, hw->tv.sec, hw->tv.nsec, ts->tv.sec, ts->tv.nsec, sys.tv.sec,
733 sys.tv.nsec, cmp->offset, cmp->skew);
734}
735
736static void bfin_tx_hwtstamp(struct net_device *netdev, struct sk_buff *skb)
737{
738 struct bfin_mac_local *lp = netdev_priv(netdev);
739 union skb_shared_tx *shtx = skb_tx(skb);
740
741 if (shtx->hardware) {
742 int timeout_cnt = MAX_TIMEOUT_CNT;
743
744 /* When doing time stamping, keep the connection to the socket
745 * a while longer
746 */
747 shtx->in_progress = 1;
748
749 /*
750 * The timestamping is done at the EMAC module's MII/RMII interface
751 * when the module sees the Start of Frame of an event message packet. This
752 * interface is the closest possible place to the physical Ethernet transmission
753 * medium, providing the best timing accuracy.
754 */
755 while ((!(bfin_read_EMAC_PTP_ISTAT() & TXTL)) && (--timeout_cnt))
756 udelay(1);
757 if (timeout_cnt == 0)
758 printk(KERN_ERR DRV_NAME
759 ": fails to timestamp the TX packet\n");
760 else {
761 struct skb_shared_hwtstamps shhwtstamps;
762 u64 ns;
763 u64 regval;
764
765 regval = bfin_read_EMAC_PTP_TXSNAPLO();
766 regval |= (u64)bfin_read_EMAC_PTP_TXSNAPHI() << 32;
767 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
768 ns = timecounter_cyc2time(&lp->clock,
769 regval);
770 timecompare_update(&lp->compare, ns);
771 shhwtstamps.hwtstamp = ns_to_ktime(ns);
772 shhwtstamps.syststamp =
773 timecompare_transform(&lp->compare, ns);
774 skb_tstamp_tx(skb, &shhwtstamps);
775
776 bfin_dump_hwtamp("TX", &shhwtstamps.hwtstamp, &shhwtstamps.syststamp, &lp->compare);
777 }
778 }
779}
780
781static void bfin_rx_hwtstamp(struct net_device *netdev, struct sk_buff *skb)
782{
783 struct bfin_mac_local *lp = netdev_priv(netdev);
784 u32 valid;
785 u64 regval, ns;
786 struct skb_shared_hwtstamps *shhwtstamps;
787
788 if (bfin_mac_hwtstamp_is_none(lp->stamp_cfg.rx_filter))
789 return;
790
791 valid = bfin_read_EMAC_PTP_ISTAT() & RXEL;
792 if (!valid)
793 return;
794
795 shhwtstamps = skb_hwtstamps(skb);
796
797 regval = bfin_read_EMAC_PTP_RXSNAPLO();
798 regval |= (u64)bfin_read_EMAC_PTP_RXSNAPHI() << 32;
799 ns = timecounter_cyc2time(&lp->clock, regval);
800 timecompare_update(&lp->compare, ns);
801 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
802 shhwtstamps->hwtstamp = ns_to_ktime(ns);
803 shhwtstamps->syststamp = timecompare_transform(&lp->compare, ns);
804
805 bfin_dump_hwtamp("RX", &shhwtstamps->hwtstamp, &shhwtstamps->syststamp, &lp->compare);
806}
807
808/*
809 * bfin_read_clock - read raw cycle counter (to be used by time counter)
810 */
811static cycle_t bfin_read_clock(const struct cyclecounter *tc)
812{
813 u64 stamp;
814
815 stamp = bfin_read_EMAC_PTP_TIMELO();
816 stamp |= (u64)bfin_read_EMAC_PTP_TIMEHI() << 32ULL;
817
818 return stamp;
819}
820
821#define PTP_CLK 25000000
822
823static void bfin_mac_hwtstamp_init(struct net_device *netdev)
824{
825 struct bfin_mac_local *lp = netdev_priv(netdev);
826 u64 append;
827
828 /* Initialize hardware timer */
829 append = PTP_CLK * (1ULL << 32);
830 do_div(append, get_sclk());
831 bfin_write_EMAC_PTP_ADDEND((u32)append);
832
833 memset(&lp->cycles, 0, sizeof(lp->cycles));
834 lp->cycles.read = bfin_read_clock;
835 lp->cycles.mask = CLOCKSOURCE_MASK(64);
836 lp->cycles.mult = 1000000000 / PTP_CLK;
837 lp->cycles.shift = 0;
838
839 /* Synchronize our NIC clock against system wall clock */
840 memset(&lp->compare, 0, sizeof(lp->compare));
841 lp->compare.source = &lp->clock;
842 lp->compare.target = ktime_get_real;
843 lp->compare.num_samples = 10;
844
845 /* Initialize hwstamp config */
846 lp->stamp_cfg.rx_filter = HWTSTAMP_FILTER_NONE;
847 lp->stamp_cfg.tx_type = HWTSTAMP_TX_OFF;
848}
849
850#else
851# define bfin_mac_hwtstamp_is_none(cfg) 0
852# define bfin_mac_hwtstamp_init(dev)
853# define bfin_mac_hwtstamp_ioctl(dev, ifr, cmd) (-EOPNOTSUPP)
854# define bfin_rx_hwtstamp(dev, skb)
855# define bfin_tx_hwtstamp(dev, skb)
856#endif
857
e190d6b1
BW
858static void adjust_tx_list(void)
859{
860 int timeout_cnt = MAX_TIMEOUT_CNT;
861
8e95a202
JP
862 if (tx_list_head->status.status_word != 0 &&
863 current_tx_ptr != tx_list_head) {
e190d6b1
BW
864 goto adjust_head; /* released something, just return; */
865 }
866
867 /*
868 * if nothing released, check wait condition
869 * current's next can not be the head,
870 * otherwise the dma will not stop as we want
871 */
872 if (current_tx_ptr->next->next == tx_list_head) {
873 while (tx_list_head->status.status_word == 0) {
015dac88 874 udelay(10);
8e95a202
JP
875 if (tx_list_head->status.status_word != 0 ||
876 !(bfin_read_DMA2_IRQ_STATUS() & DMA_RUN)) {
e190d6b1
BW
877 goto adjust_head;
878 }
879 if (timeout_cnt-- < 0) {
880 printk(KERN_ERR DRV_NAME
881 ": wait for adjust tx list head timeout\n");
882 break;
883 }
884 }
885 if (tx_list_head->status.status_word != 0) {
886 goto adjust_head;
887 }
888 }
889
890 return;
891
892adjust_head:
893 do {
894 tx_list_head->desc_a.config &= ~DMAEN;
895 tx_list_head->status.status_word = 0;
896 if (tx_list_head->skb) {
897 dev_kfree_skb(tx_list_head->skb);
898 tx_list_head->skb = NULL;
899 } else {
900 printk(KERN_ERR DRV_NAME
901 ": no sk_buff in a transmitted frame!\n");
902 }
903 tx_list_head = tx_list_head->next;
8e95a202
JP
904 } while (tx_list_head->status.status_word != 0 &&
905 current_tx_ptr != tx_list_head);
e190d6b1
BW
906 return;
907
908}
909
7ef0a7ee 910static int bfin_mac_hard_start_xmit(struct sk_buff *skb,
e190d6b1
BW
911 struct net_device *dev)
912{
a50c0c05 913 u16 *data;
015dac88 914 u32 data_align = (unsigned long)(skb->data) & 0x3;
fe92afed
BS
915 union skb_shared_tx *shtx = skb_tx(skb);
916
e190d6b1
BW
917 current_tx_ptr->skb = skb;
918
015dac88
MH
919 if (data_align == 0x2) {
920 /* move skb->data to current_tx_ptr payload */
921 data = (u16 *)(skb->data) - 1;
fe92afed
BS
922 *data = (u16)(skb->len);
923 /*
924 * When transmitting an Ethernet packet, the PTP_TSYNC module requires
925 * a DMA_Length_Word field associated with the packet. The lower 12 bits
926 * of this field are the length of the packet payload in bytes and the higher
927 * 4 bits are the timestamping enable field.
928 */
929 if (shtx->hardware)
930 *data |= 0x1000;
931
015dac88
MH
932 current_tx_ptr->desc_a.start_addr = (u32)data;
933 /* this is important! */
934 blackfin_dcache_flush_range((u32)data,
935 (u32)((u8 *)data + skb->len + 4));
e190d6b1 936 } else {
015dac88 937 *((u16 *)(current_tx_ptr->packet)) = (u16)(skb->len);
fe92afed
BS
938 /* enable timestamping for the sent packet */
939 if (shtx->hardware)
940 *((u16 *)(current_tx_ptr->packet)) |= 0x1000;
015dac88
MH
941 memcpy((u8 *)(current_tx_ptr->packet + 2), skb->data,
942 skb->len);
943 current_tx_ptr->desc_a.start_addr =
944 (u32)current_tx_ptr->packet;
945 if (current_tx_ptr->status.status_word != 0)
946 current_tx_ptr->status.status_word = 0;
947 blackfin_dcache_flush_range(
948 (u32)current_tx_ptr->packet,
949 (u32)(current_tx_ptr->packet + skb->len + 2));
e190d6b1
BW
950 }
951
805a8ab3
SZ
952 /* make sure the internal data buffers in the core are drained
953 * so that the DMA descriptors are completely written when the
954 * DMA engine goes to fetch them below
955 */
956 SSYNC();
957
e190d6b1
BW
958 /* enable this packet's dma */
959 current_tx_ptr->desc_a.config |= DMAEN;
960
961 /* tx dma is running, just return */
015dac88 962 if (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN)
e190d6b1
BW
963 goto out;
964
965 /* tx dma is not running */
966 bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
967 /* dma enabled, read from memory, size is 6 */
968 bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
969 /* Turn on the EMAC tx */
970 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
971
972out:
973 adjust_tx_list();
fe92afed
BS
974
975 bfin_tx_hwtstamp(dev, skb);
976
e190d6b1 977 current_tx_ptr = current_tx_ptr->next;
09f75cd7
JG
978 dev->stats.tx_packets++;
979 dev->stats.tx_bytes += (skb->len);
6ed10654 980 return NETDEV_TX_OK;
e190d6b1
BW
981}
982
7ef0a7ee 983static void bfin_mac_rx(struct net_device *dev)
e190d6b1
BW
984{
985 struct sk_buff *skb, *new_skb;
e190d6b1 986 unsigned short len;
fe92afed 987 struct bfin_mac_local *lp __maybe_unused = netdev_priv(dev);
e190d6b1
BW
988
989 /* allocate a new skb for next time receive */
990 skb = current_rx_ptr->skb;
fe92afed 991
015dac88 992 new_skb = dev_alloc_skb(PKT_BUF_SZ + NET_IP_ALIGN);
e190d6b1
BW
993 if (!new_skb) {
994 printk(KERN_NOTICE DRV_NAME
995 ": rx: low on mem - packet dropped\n");
09f75cd7 996 dev->stats.rx_dropped++;
e190d6b1
BW
997 goto out;
998 }
999 /* reserve 2 bytes for RXDWA padding */
015dac88 1000 skb_reserve(new_skb, NET_IP_ALIGN);
e190d6b1
BW
1001 current_rx_ptr->skb = new_skb;
1002 current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
1003
6e01d1a4
AD
1004 /* Invidate the data cache of skb->data range when it is write back
1005 * cache. It will prevent overwritting the new data from DMA
1006 */
1007 blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
1008 (unsigned long)new_skb->end);
1009
e190d6b1
BW
1010 len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
1011 skb_put(skb, len);
1012 blackfin_dcache_invalidate_range((unsigned long)skb->head,
1013 (unsigned long)skb->tail);
1014
e190d6b1 1015 skb->protocol = eth_type_trans(skb, dev);
fe92afed
BS
1016
1017 bfin_rx_hwtstamp(dev, skb);
1018
e190d6b1
BW
1019#if defined(BFIN_MAC_CSUM_OFFLOAD)
1020 skb->csum = current_rx_ptr->status.ip_payload_csum;
00ff49a9 1021 skb->ip_summed = CHECKSUM_COMPLETE;
e190d6b1
BW
1022#endif
1023
1024 netif_rx(skb);
09f75cd7
JG
1025 dev->stats.rx_packets++;
1026 dev->stats.rx_bytes += len;
e190d6b1
BW
1027 current_rx_ptr->status.status_word = 0x00000000;
1028 current_rx_ptr = current_rx_ptr->next;
1029
1030out:
1031 return;
1032}
1033
1034/* interrupt routine to handle rx and error signal */
7ef0a7ee 1035static irqreturn_t bfin_mac_interrupt(int irq, void *dev_id)
e190d6b1
BW
1036{
1037 struct net_device *dev = dev_id;
1038 int number = 0;
1039
1040get_one_packet:
1041 if (current_rx_ptr->status.status_word == 0) {
1042 /* no more new packet received */
1043 if (number == 0) {
1044 if (current_rx_ptr->next->status.status_word != 0) {
1045 current_rx_ptr = current_rx_ptr->next;
1046 goto real_rx;
1047 }
1048 }
1049 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
1050 DMA_DONE | DMA_ERR);
1051 return IRQ_HANDLED;
1052 }
1053
1054real_rx:
7ef0a7ee 1055 bfin_mac_rx(dev);
e190d6b1
BW
1056 number++;
1057 goto get_one_packet;
1058}
1059
1060#ifdef CONFIG_NET_POLL_CONTROLLER
7ef0a7ee 1061static void bfin_mac_poll(struct net_device *dev)
e190d6b1
BW
1062{
1063 disable_irq(IRQ_MAC_RX);
7ef0a7ee 1064 bfin_mac_interrupt(IRQ_MAC_RX, dev);
e190d6b1
BW
1065 enable_irq(IRQ_MAC_RX);
1066}
1067#endif /* CONFIG_NET_POLL_CONTROLLER */
1068
7ef0a7ee 1069static void bfin_mac_disable(void)
e190d6b1
BW
1070{
1071 unsigned int opmode;
1072
1073 opmode = bfin_read_EMAC_OPMODE();
1074 opmode &= (~RE);
1075 opmode &= (~TE);
1076 /* Turn off the EMAC */
1077 bfin_write_EMAC_OPMODE(opmode);
1078}
1079
1080/*
1081 * Enable Interrupts, Receive, and Transmit
1082 */
7ef0a7ee 1083static void bfin_mac_enable(void)
e190d6b1
BW
1084{
1085 u32 opmode;
1086
b39d66a8 1087 pr_debug("%s: %s\n", DRV_NAME, __func__);
e190d6b1
BW
1088
1089 /* Set RX DMA */
1090 bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
1091 bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
1092
1093 /* Wait MII done */
0ed0563e 1094 bfin_mdio_poll();
e190d6b1
BW
1095
1096 /* We enable only RX here */
1097 /* ASTP : Enable Automatic Pad Stripping
1098 PR : Promiscuous Mode for test
1099 PSF : Receive frames with total length less than 64 bytes.
1100 FDMODE : Full Duplex Mode
1101 LB : Internal Loopback for test
1102 RE : Receiver Enable */
1103 opmode = bfin_read_EMAC_OPMODE();
1104 if (opmode & FDMODE)
1105 opmode |= PSF;
1106 else
1107 opmode |= DRO | DC | PSF;
1108 opmode |= RE;
1109
1110#if defined(CONFIG_BFIN_MAC_RMII)
1111 opmode |= RMII; /* For Now only 100MBit are supported */
6893ff1c 1112#if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
e190d6b1
BW
1113 opmode |= TE;
1114#endif
1115#endif
1116 /* Turn on the EMAC rx */
1117 bfin_write_EMAC_OPMODE(opmode);
e190d6b1
BW
1118}
1119
1120/* Our watchdog timed out. Called by the networking layer */
7ef0a7ee 1121static void bfin_mac_timeout(struct net_device *dev)
e190d6b1 1122{
b39d66a8 1123 pr_debug("%s: %s\n", dev->name, __func__);
e190d6b1 1124
7ef0a7ee 1125 bfin_mac_disable();
e190d6b1
BW
1126
1127 /* reset tx queue */
1128 tx_list_tail = tx_list_head->next;
1129
7ef0a7ee 1130 bfin_mac_enable();
e190d6b1
BW
1131
1132 /* We can accept TX packets again */
1ae5dc34 1133 dev->trans_start = jiffies; /* prevent tx timeout */
e190d6b1
BW
1134 netif_wake_queue(dev);
1135}
1136
7ef0a7ee 1137static void bfin_mac_multicast_hash(struct net_device *dev)
775919bc
AW
1138{
1139 u32 emac_hashhi, emac_hashlo;
22bedad3 1140 struct netdev_hw_addr *ha;
775919bc 1141 char *addrs;
775919bc
AW
1142 u32 crc;
1143
1144 emac_hashhi = emac_hashlo = 0;
1145
22bedad3
JP
1146 netdev_for_each_mc_addr(ha, dev) {
1147 addrs = ha->addr;
775919bc
AW
1148
1149 /* skip non-multicast addresses */
1150 if (!(*addrs & 1))
1151 continue;
1152
1153 crc = ether_crc(ETH_ALEN, addrs);
1154 crc >>= 26;
1155
1156 if (crc & 0x20)
1157 emac_hashhi |= 1 << (crc & 0x1f);
1158 else
1159 emac_hashlo |= 1 << (crc & 0x1f);
1160 }
1161
1162 bfin_write_EMAC_HASHHI(emac_hashhi);
1163 bfin_write_EMAC_HASHLO(emac_hashlo);
775919bc
AW
1164}
1165
e190d6b1
BW
1166/*
1167 * This routine will, depending on the values passed to it,
1168 * either make it accept multicast packets, go into
1169 * promiscuous mode (for TCPDUMP and cousins) or accept
1170 * a select set of multicast packets
1171 */
7ef0a7ee 1172static void bfin_mac_set_multicast_list(struct net_device *dev)
e190d6b1
BW
1173{
1174 u32 sysctl;
1175
1176 if (dev->flags & IFF_PROMISC) {
1177 printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
1178 sysctl = bfin_read_EMAC_OPMODE();
1179 sysctl |= RAF;
1180 bfin_write_EMAC_OPMODE(sysctl);
775919bc 1181 } else if (dev->flags & IFF_ALLMULTI) {
e190d6b1
BW
1182 /* accept all multicast */
1183 sysctl = bfin_read_EMAC_OPMODE();
1184 sysctl |= PAM;
1185 bfin_write_EMAC_OPMODE(sysctl);
4cd24eaf 1186 } else if (!netdev_mc_empty(dev)) {
775919bc
AW
1187 /* set up multicast hash table */
1188 sysctl = bfin_read_EMAC_OPMODE();
1189 sysctl |= HM;
1190 bfin_write_EMAC_OPMODE(sysctl);
7ef0a7ee 1191 bfin_mac_multicast_hash(dev);
e190d6b1
BW
1192 } else {
1193 /* clear promisc or multicast mode */
1194 sysctl = bfin_read_EMAC_OPMODE();
1195 sysctl &= ~(RAF | PAM);
1196 bfin_write_EMAC_OPMODE(sysctl);
1197 }
1198}
1199
fe92afed
BS
1200static int bfin_mac_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1201{
1202 switch (cmd) {
1203 case SIOCSHWTSTAMP:
1204 return bfin_mac_hwtstamp_ioctl(netdev, ifr, cmd);
1205 default:
1206 return -EOPNOTSUPP;
1207 }
1208}
1209
e190d6b1
BW
1210/*
1211 * this puts the device in an inactive state
1212 */
7ef0a7ee 1213static void bfin_mac_shutdown(struct net_device *dev)
e190d6b1
BW
1214{
1215 /* Turn off the EMAC */
1216 bfin_write_EMAC_OPMODE(0x00000000);
1217 /* Turn off the EMAC RX DMA */
1218 bfin_write_DMA1_CONFIG(0x0000);
1219 bfin_write_DMA2_CONFIG(0x0000);
1220}
1221
1222/*
1223 * Open and Initialize the interface
1224 *
1225 * Set up everything, reset the card, etc..
1226 */
7ef0a7ee 1227static int bfin_mac_open(struct net_device *dev)
e190d6b1 1228{
7ef0a7ee 1229 struct bfin_mac_local *lp = netdev_priv(dev);
4af4b840 1230 int retval;
b39d66a8 1231 pr_debug("%s: %s\n", dev->name, __func__);
e190d6b1
BW
1232
1233 /*
1234 * Check that the address is valid. If its not, refuse
1235 * to bring the device up. The user must specify an
1236 * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
1237 */
1238 if (!is_valid_ether_addr(dev->dev_addr)) {
1239 printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
1240 return -EINVAL;
1241 }
1242
1243 /* initial rx and tx list */
4af4b840
MH
1244 retval = desc_list_init();
1245
1246 if (retval)
1247 return retval;
e190d6b1 1248
4ae5a3ad 1249 phy_start(lp->phydev);
136492b2 1250 phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
e190d6b1 1251 setup_system_regs(dev);
ee02fee8 1252 setup_mac_addr(dev->dev_addr);
7ef0a7ee
BW
1253 bfin_mac_disable();
1254 bfin_mac_enable();
e190d6b1
BW
1255 pr_debug("hardware init finished\n");
1256 netif_start_queue(dev);
1257 netif_carrier_on(dev);
1258
1259 return 0;
1260}
1261
1262/*
e190d6b1
BW
1263 * this makes the board clean up everything that it can
1264 * and not talk to the outside world. Caused by
1265 * an 'ifconfig ethX down'
1266 */
7ef0a7ee 1267static int bfin_mac_close(struct net_device *dev)
e190d6b1 1268{
7ef0a7ee 1269 struct bfin_mac_local *lp = netdev_priv(dev);
b39d66a8 1270 pr_debug("%s: %s\n", dev->name, __func__);
e190d6b1
BW
1271
1272 netif_stop_queue(dev);
1273 netif_carrier_off(dev);
1274
4ae5a3ad 1275 phy_stop(lp->phydev);
136492b2 1276 phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN);
4ae5a3ad 1277
e190d6b1 1278 /* clear everything */
7ef0a7ee 1279 bfin_mac_shutdown(dev);
e190d6b1
BW
1280
1281 /* free the rx/tx buffers */
1282 desc_list_free();
1283
1284 return 0;
1285}
1286
b63dc8fe
MF
1287static const struct net_device_ops bfin_mac_netdev_ops = {
1288 .ndo_open = bfin_mac_open,
1289 .ndo_stop = bfin_mac_close,
1290 .ndo_start_xmit = bfin_mac_hard_start_xmit,
1291 .ndo_set_mac_address = bfin_mac_set_mac_address,
1292 .ndo_tx_timeout = bfin_mac_timeout,
1293 .ndo_set_multicast_list = bfin_mac_set_multicast_list,
fe92afed 1294 .ndo_do_ioctl = bfin_mac_ioctl,
b63dc8fe
MF
1295 .ndo_validate_addr = eth_validate_addr,
1296 .ndo_change_mtu = eth_change_mtu,
1297#ifdef CONFIG_NET_POLL_CONTROLLER
1298 .ndo_poll_controller = bfin_mac_poll,
1299#endif
1300};
1301
d7b843d3 1302static int __devinit bfin_mac_probe(struct platform_device *pdev)
e190d6b1 1303{
7ef0a7ee
BW
1304 struct net_device *ndev;
1305 struct bfin_mac_local *lp;
080c8255
GY
1306 struct platform_device *pd;
1307 int rc;
7ef0a7ee
BW
1308
1309 ndev = alloc_etherdev(sizeof(struct bfin_mac_local));
1310 if (!ndev) {
1311 dev_err(&pdev->dev, "Cannot allocate net device!\n");
1312 return -ENOMEM;
1313 }
1314
1315 SET_NETDEV_DEV(ndev, &pdev->dev);
1316 platform_set_drvdata(pdev, ndev);
1317 lp = netdev_priv(ndev);
e190d6b1
BW
1318
1319 /* Grab the MAC address in the MAC */
7ef0a7ee
BW
1320 *(__le32 *) (&(ndev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
1321 *(__le16 *) (&(ndev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
e190d6b1
BW
1322
1323 /* probe mac */
1324 /*todo: how to proble? which is revision_register */
1325 bfin_write_EMAC_ADDRLO(0x12345678);
1326 if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
7ef0a7ee
BW
1327 dev_err(&pdev->dev, "Cannot detect Blackfin on-chip ethernet MAC controller!\n");
1328 rc = -ENODEV;
1329 goto out_err_probe_mac;
e190d6b1
BW
1330 }
1331
e190d6b1 1332
7ef0a7ee
BW
1333 /*
1334 * Is it valid? (Did bootloader initialize it?)
1335 * Grab the MAC from the board somehow
1336 * this is done in the arch/blackfin/mach-bfxxx/boards/eth_mac.c
1337 */
1338 if (!is_valid_ether_addr(ndev->dev_addr))
1339 bfin_get_ether_addr(ndev->dev_addr);
1340
e190d6b1 1341 /* If still not valid, get a random one */
7ef0a7ee
BW
1342 if (!is_valid_ether_addr(ndev->dev_addr))
1343 random_ether_addr(ndev->dev_addr);
e190d6b1 1344
7ef0a7ee 1345 setup_mac_addr(ndev->dev_addr);
e190d6b1 1346
080c8255
GY
1347 if (!pdev->dev.platform_data) {
1348 dev_err(&pdev->dev, "Cannot get platform device bfin_mii_bus!\n");
1349 rc = -ENODEV;
1350 goto out_err_probe_mac;
7ef0a7ee 1351 }
080c8255
GY
1352 pd = pdev->dev.platform_data;
1353 lp->mii_bus = platform_get_drvdata(pd);
1354 lp->mii_bus->priv = ndev;
4ae5a3ad 1355
7ef0a7ee
BW
1356 rc = mii_probe(ndev);
1357 if (rc) {
1358 dev_err(&pdev->dev, "MII Probe failed!\n");
1359 goto out_err_mii_probe;
1360 }
4ae5a3ad 1361
e190d6b1 1362 /* Fill in the fields of the device structure with ethernet values. */
7ef0a7ee
BW
1363 ether_setup(ndev);
1364
149da651 1365 ndev->netdev_ops = &bfin_mac_netdev_ops;
679dce39 1366 ndev->ethtool_ops = &bfin_mac_ethtool_ops;
e190d6b1 1367
e190d6b1
BW
1368 spin_lock_init(&lp->lock);
1369
1370 /* now, enable interrupts */
1371 /* register irq handler */
7ef0a7ee 1372 rc = request_irq(IRQ_MAC_RX, bfin_mac_interrupt,
91a455f0 1373 IRQF_DISABLED, "EMAC_RX", ndev);
7ef0a7ee
BW
1374 if (rc) {
1375 dev_err(&pdev->dev, "Cannot request Blackfin MAC RX IRQ!\n");
1376 rc = -EBUSY;
1377 goto out_err_request_irq;
e190d6b1
BW
1378 }
1379
7ef0a7ee
BW
1380 rc = register_netdev(ndev);
1381 if (rc) {
1382 dev_err(&pdev->dev, "Cannot register net device!\n");
1383 goto out_err_reg_ndev;
e190d6b1
BW
1384 }
1385
fe92afed
BS
1386 bfin_mac_hwtstamp_init(ndev);
1387
7ef0a7ee
BW
1388 /* now, print out the card info, in a short format.. */
1389 dev_info(&pdev->dev, "%s, Version %s\n", DRV_DESC, DRV_VERSION);
e190d6b1 1390
7ef0a7ee 1391 return 0;
e190d6b1 1392
7ef0a7ee
BW
1393out_err_reg_ndev:
1394 free_irq(IRQ_MAC_RX, ndev);
1395out_err_request_irq:
1396out_err_mii_probe:
298cf9be 1397 mdiobus_unregister(lp->mii_bus);
298cf9be 1398 mdiobus_free(lp->mii_bus);
7ef0a7ee 1399 peripheral_free_list(pin_req);
7ef0a7ee
BW
1400out_err_probe_mac:
1401 platform_set_drvdata(pdev, NULL);
1402 free_netdev(ndev);
e190d6b1 1403
7ef0a7ee 1404 return rc;
e190d6b1
BW
1405}
1406
d7b843d3 1407static int __devexit bfin_mac_remove(struct platform_device *pdev)
e190d6b1
BW
1408{
1409 struct net_device *ndev = platform_get_drvdata(pdev);
7ef0a7ee 1410 struct bfin_mac_local *lp = netdev_priv(ndev);
e190d6b1
BW
1411
1412 platform_set_drvdata(pdev, NULL);
1413
080c8255 1414 lp->mii_bus->priv = NULL;
7ef0a7ee 1415
e190d6b1
BW
1416 unregister_netdev(ndev);
1417
1418 free_irq(IRQ_MAC_RX, ndev);
1419
1420 free_netdev(ndev);
1421
7ef0a7ee 1422 peripheral_free_list(pin_req);
e190d6b1
BW
1423
1424 return 0;
1425}
1426
496a34c2
BW
1427#ifdef CONFIG_PM
1428static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
e190d6b1 1429{
496a34c2
BW
1430 struct net_device *net_dev = platform_get_drvdata(pdev);
1431
1432 if (netif_running(net_dev))
7ef0a7ee 1433 bfin_mac_close(net_dev);
496a34c2 1434
e190d6b1
BW
1435 return 0;
1436}
1437
1438static int bfin_mac_resume(struct platform_device *pdev)
1439{
496a34c2
BW
1440 struct net_device *net_dev = platform_get_drvdata(pdev);
1441
1442 if (netif_running(net_dev))
7ef0a7ee 1443 bfin_mac_open(net_dev);
496a34c2 1444
e190d6b1
BW
1445 return 0;
1446}
496a34c2
BW
1447#else
1448#define bfin_mac_suspend NULL
1449#define bfin_mac_resume NULL
1450#endif /* CONFIG_PM */
e190d6b1 1451
080c8255
GY
1452static int __devinit bfin_mii_bus_probe(struct platform_device *pdev)
1453{
1454 struct mii_bus *miibus;
1455 int rc, i;
1456
1457 /*
1458 * We are setting up a network card,
1459 * so set the GPIO pins to Ethernet mode
1460 */
1461 rc = peripheral_request_list(pin_req, DRV_NAME);
1462 if (rc) {
1463 dev_err(&pdev->dev, "Requesting peripherals failed!\n");
1464 return rc;
1465 }
1466
1467 rc = -ENOMEM;
1468 miibus = mdiobus_alloc();
1469 if (miibus == NULL)
1470 goto out_err_alloc;
1471 miibus->read = bfin_mdiobus_read;
1472 miibus->write = bfin_mdiobus_write;
1473 miibus->reset = bfin_mdiobus_reset;
1474
1475 miibus->parent = &pdev->dev;
1476 miibus->name = "bfin_mii_bus";
1477 snprintf(miibus->id, MII_BUS_ID_SIZE, "0");
1478 miibus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1479 if (miibus->irq == NULL)
1480 goto out_err_alloc;
1481 for (i = 0; i < PHY_MAX_ADDR; ++i)
1482 miibus->irq[i] = PHY_POLL;
1483
1484 rc = mdiobus_register(miibus);
1485 if (rc) {
1486 dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
1487 goto out_err_mdiobus_register;
1488 }
1489
1490 platform_set_drvdata(pdev, miibus);
1491 return 0;
1492
1493out_err_mdiobus_register:
1494 mdiobus_free(miibus);
1495out_err_alloc:
1496 peripheral_free_list(pin_req);
1497
1498 return rc;
1499}
1500
1501static int __devexit bfin_mii_bus_remove(struct platform_device *pdev)
1502{
1503 struct mii_bus *miibus = platform_get_drvdata(pdev);
1504 platform_set_drvdata(pdev, NULL);
1505 mdiobus_unregister(miibus);
1506 mdiobus_free(miibus);
1507 peripheral_free_list(pin_req);
1508 return 0;
1509}
1510
1511static struct platform_driver bfin_mii_bus_driver = {
1512 .probe = bfin_mii_bus_probe,
1513 .remove = __devexit_p(bfin_mii_bus_remove),
1514 .driver = {
1515 .name = "bfin_mii_bus",
1516 .owner = THIS_MODULE,
1517 },
1518};
1519
e190d6b1
BW
1520static struct platform_driver bfin_mac_driver = {
1521 .probe = bfin_mac_probe,
d7b843d3 1522 .remove = __devexit_p(bfin_mac_remove),
e190d6b1
BW
1523 .resume = bfin_mac_resume,
1524 .suspend = bfin_mac_suspend,
1525 .driver = {
72abb461
KS
1526 .name = DRV_NAME,
1527 .owner = THIS_MODULE,
1528 },
e190d6b1
BW
1529};
1530
1531static int __init bfin_mac_init(void)
1532{
080c8255
GY
1533 int ret;
1534 ret = platform_driver_register(&bfin_mii_bus_driver);
1535 if (!ret)
1536 return platform_driver_register(&bfin_mac_driver);
1537 return -ENODEV;
e190d6b1
BW
1538}
1539
1540module_init(bfin_mac_init);
1541
1542static void __exit bfin_mac_cleanup(void)
1543{
1544 platform_driver_unregister(&bfin_mac_driver);
080c8255 1545 platform_driver_unregister(&bfin_mii_bus_driver);
e190d6b1
BW
1546}
1547
1548module_exit(bfin_mac_cleanup);
72abb461 1549
This page took 0.385439 seconds and 5 git commands to generate.