11 #define DRV_VERSION "1.1"
12 #define DRV_DESC "Blackfin on-chip Ethernet MAC driver"
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
23 #include <linux/errno.h>
28 #include <linux/device.h>
30 #include <linux/mii.h>
31 #include <linux/netdevice.h>
33 #include <linux/ethtool.h>
40 #include <asm/div64.h>
43 #include <asm/cacheflush.h>
54 #if defined(CONFIG_BFIN_MAC_USE_L1)
55 # define bfin_mac_alloc(dma_handle, size, num) l1_data_sram_zalloc(size*num)
56 # define bfin_mac_free(dma_handle, ptr, num) l1_data_sram_free(ptr)
58 # define bfin_mac_alloc(dma_handle, size, num) \
59 dma_alloc_coherent(NULL, size*num, dma_handle, GFP_KERNEL)
60 # define bfin_mac_free(dma_handle, ptr, num) \
61 dma_free_coherent(NULL, sizeof(*ptr)*num, ptr, dma_handle)
64 #define PKT_BUF_SZ 1580
66 #define MAX_TIMEOUT_CNT 500
78 static void desc_list_free(
void)
83 #if !defined(CONFIG_BFIN_MAC_USE_L1)
89 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
92 dev_kfree_skb(t->
skb);
103 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
106 dev_kfree_skb(r->
skb);
120 #if !defined(CONFIG_BFIN_MAC_USE_L1)
130 CONFIG_BFIN_TX_DESC_NUM);
136 CONFIG_BFIN_RX_DESC_NUM);
141 tx_list_head = tx_list_tail = tx_desc;
143 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
173 tx_list_tail->
desc_b.next_dma_desc =
a;
174 tx_list_tail->
next =
t;
177 tx_list_tail->
next = tx_list_head;
178 tx_list_tail->
desc_b.next_dma_desc = &(tx_list_head->
desc_a);
179 current_tx_ptr = tx_list_head;
182 rx_list_head = rx_list_tail = rx_desc;
184 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
192 pr_notice(
"init: low on mem - packet dropped\n");
200 (
unsigned long)new_skb->
end);
230 rx_list_tail->
desc_b.next_dma_desc =
a;
231 rx_list_tail->
next =
r;
234 rx_list_tail->
next = rx_list_head;
235 rx_list_tail->
desc_b.next_dma_desc = &(rx_list_head->
desc_a);
236 current_rx_ptr = rx_list_head;
242 pr_err(
"kmalloc failed\n");
253 static int bfin_mdio_poll(
void)
260 if (timeout_cnt-- < 0) {
261 pr_err(
"wait MDC/MDIO transaction to complete timeout\n");
270 static int bfin_mdiobus_read(
struct mii_bus *
bus,
int phy_addr,
int regnum)
274 ret = bfin_mdio_poll();
283 ret = bfin_mdio_poll();
291 static int bfin_mdiobus_write(
struct mii_bus *bus,
int phy_addr,
int regnum,
296 ret = bfin_mdio_poll();
308 return bfin_mdio_poll();
311 static int bfin_mdiobus_reset(
struct mii_bus *bus)
316 static void bfin_mac_adjust_link(
struct net_device *dev)
343 switch (phydev->
speed) {
352 "Ack! Speed (%d) is not 10/100!\n",
377 pr_debug(
"EMAC_OPMODE = 0x%08x\n", opmode);
380 spin_unlock_irqrestore(&lp->
lock, flags);
384 #define MDC_CLK 2500000
386 static int mii_probe(
struct net_device *dev,
int phy_mode)
390 unsigned short sysctl;
399 mdc_div = ((sclk /
MDC_CLK) / 2) - 1;
418 netdev_err(dev,
"no phy device found\n");
424 netdev_err(dev,
"invalid phy interface mode\n");
428 phydev =
phy_connect(dev, dev_name(&phydev->
dev), &bfin_mac_adjust_link,
431 if (IS_ERR(phydev)) {
432 netdev_err(dev,
"could not attach PHY\n");
433 return PTR_ERR(phydev);
453 pr_info(
"attached PHY driver [%s] "
454 "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)@sclk=%dMHz)\n",
455 phydev->
drv->name, dev_name(&phydev->
dev), phydev->
irq,
456 MDC_CLK, mdc_div, sclk/1000000);
498 static void bfin_mac_ethtool_getdrvinfo(
struct net_device *dev,
507 static void bfin_mac_ethtool_getwol(
struct net_device *dev,
516 static int bfin_mac_ethtool_setwol(
struct net_device *dev,
551 static int bfin_mac_ethtool_get_ts_info(
struct net_device *dev,
570 static const struct ethtool_ops bfin_mac_ethtool_ops = {
571 .get_settings = bfin_mac_ethtool_getsettings,
572 .set_settings = bfin_mac_ethtool_setsettings,
574 .get_drvinfo = bfin_mac_ethtool_getdrvinfo,
575 .get_wol = bfin_mac_ethtool_getwol,
576 .set_wol = bfin_mac_ethtool_setwol,
577 .get_ts_info = bfin_mac_ethtool_get_ts_info,
581 static void setup_system_regs(
struct net_device *dev)
585 unsigned short sysctl;
599 if (i < PHY_MAX_ADDR)
602 #if defined(BFIN_MAC_CSUM_OFFLOAD)
638 static int bfin_mac_set_mac_address(
struct net_device *dev,
void *
p)
641 if (netif_running(dev))
649 #ifdef CONFIG_BFIN_MAC_USE_HWSTAMP
650 #define bfin_mac_hwtstamp_is_none(cfg) ((cfg) == HWTSTAMP_FILTER_NONE)
653 struct ifreq *ifr,
int cmd)
658 u32 ptpfv1, ptpfv2, ptpfv3, ptpfoff;
663 pr_debug(
"%s config flag:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
676 switch (
config.rx_filter) {
696 ptpfoff = 0x4A24170C;
727 ptpfoff = 0x2A24170C;
759 ptpfoff = 0x0E24170C;
809 lp->compare.last_update = 0;
813 timecompare_update(&lp->compare, 0);
825 pr_debug(
"%s %s hardware:%d,%d transform system:%d,%d system:%d,%d, cmp:%lld, %lld\n",
826 __func__, s, hw->
tv.sec, hw->
tv.nsec, ts->
tv.sec, ts->
tv.nsec, sys.
tv.sec,
850 if (timeout_cnt == 0)
851 netdev_err(netdev,
"timestamp the TX packet failed\n");
859 memset(&shhwtstamps, 0,
sizeof(shhwtstamps));
862 timecompare_update(&lp->compare, ns);
863 shhwtstamps.hwtstamp = ns_to_ktime(ns);
864 shhwtstamps.syststamp =
868 bfin_dump_hwtamp(
"TX", &shhwtstamps.hwtstamp, &shhwtstamps.syststamp, &lp->compare);
887 shhwtstamps = skb_hwtstamps(skb);
892 timecompare_update(&lp->compare, ns);
893 memset(shhwtstamps, 0,
sizeof(*shhwtstamps));
894 shhwtstamps->
hwtstamp = ns_to_ktime(ns);
897 bfin_dump_hwtamp(
"RX", &shhwtstamps->
hwtstamp, &shhwtstamps->
syststamp, &lp->compare);
913 #define PTP_CLK 25000000
921 append = PTP_CLK * (1ULL << 32);
925 memset(&lp->cycles, 0,
sizeof(lp->cycles));
926 lp->cycles.read = bfin_read_clock;
928 lp->cycles.mult = 1000000000 / PTP_CLK;
929 lp->cycles.shift = 0;
932 memset(&lp->compare, 0,
sizeof(lp->compare));
933 lp->compare.source = &lp->clock;
935 lp->compare.num_samples = 10;
943 # define bfin_mac_hwtstamp_is_none(cfg) 0
944 # define bfin_mac_hwtstamp_init(dev)
945 # define bfin_mac_hwtstamp_ioctl(dev, ifr, cmd) (-EOPNOTSUPP)
946 # define bfin_rx_hwtstamp(dev, skb)
947 # define bfin_tx_hwtstamp(dev, skb)
950 static inline void _tx_reclaim_skb(
void)
954 tx_list_head->
status.status_word = 0;
955 if (tx_list_head->
skb) {
956 dev_kfree_skb(tx_list_head->
skb);
959 tx_list_head = tx_list_head->
next;
961 }
while (tx_list_head->
status.status_word != 0);
968 if (tx_list_head->
status.status_word != 0)
971 if (current_tx_ptr->
next == tx_list_head) {
972 while (tx_list_head->
status.status_word == 0) {
978 if (timeout_cnt-- < 0)
982 if (timeout_cnt >= 0)
985 netif_stop_queue(lp->
ndev);
988 if (current_tx_ptr->
next != tx_list_head &&
989 netif_queue_stopped(lp->
ndev))
990 netif_wake_queue(lp->
ndev);
992 if (tx_list_head != current_tx_ptr) {
994 if (netif_queue_stopped(lp->
ndev))
1008 static void tx_reclaim_skb_timeout(
unsigned long lp)
1013 static int bfin_mac_hard_start_xmit(
struct sk_buff *skb,
1018 u32 data_align = (
unsigned long)(skb->
data) & 0x3;
1020 current_tx_ptr->
skb =
skb;
1022 if (data_align == 0x2) {
1024 data = (
u16 *)(skb->
data) - 1;
1035 current_tx_ptr->
desc_a.start_addr = (
u32)data;
1043 *((
u16 *)(current_tx_ptr->
packet)) |= 0x1000;
1046 current_tx_ptr->
desc_a.start_addr =
1060 current_tx_ptr->
status.status_word = 0;
1079 current_tx_ptr = current_tx_ptr->
next;
1080 dev->
stats.tx_packets++;
1088 #define IP_HEADER_OFF 0
1089 #define RX_ERROR_MASK (RX_LONG | RX_ALIGN | RX_CRC | RX_LEN | \
1090 RX_FRAG | RX_ADDR | RX_DMAO | RX_PHY | RX_LATE | RX_RANGE)
1092 static void bfin_mac_rx(
struct net_device *dev)
1097 #if defined(BFIN_MAC_CSUM_OFFLOAD)
1106 netdev_notice(dev,
"rx: receive error - packet dropped\n");
1107 dev->
stats.rx_dropped++;
1112 skb = current_rx_ptr->
skb;
1116 netdev_notice(dev,
"rx: low on mem - packet dropped\n");
1117 dev->
stats.rx_dropped++;
1126 (
unsigned long)new_skb->
end);
1128 current_rx_ptr->
skb = new_skb;
1129 current_rx_ptr->
desc_a.start_addr = (
unsigned long)new_skb->
data - 2;
1131 len = (
unsigned short)((current_rx_ptr->
status.status_word) &
RX_FRLEN);
1140 #if defined(BFIN_MAC_CSUM_OFFLOAD)
1147 skb->
csum = current_rx_ptr->
status.ip_payload_csum;
1158 fcs[i + 1] = ~skb->
data[skb->
len + i];
1162 fcs[i] = ~skb->
data[skb->
len + i];
1170 dev->
stats.rx_packets++;
1171 dev->
stats.rx_bytes += len;
1173 current_rx_ptr->
status.status_word = 0x00000000;
1174 current_rx_ptr = current_rx_ptr->
next;
1184 if (current_rx_ptr->
status.status_word == 0) {
1187 if (current_rx_ptr->
next->status.status_word != 0) {
1188 current_rx_ptr = current_rx_ptr->
next;
1200 goto get_one_packet;
1203 #ifdef CONFIG_NET_POLL_CONTROLLER
1204 static void bfin_mac_poll(
struct net_device *dev)
1215 static void bfin_mac_disable(
void)
1217 unsigned int opmode;
1229 static int bfin_mac_enable(
struct phy_device *phydev)
1241 ret = bfin_mdio_poll();
1261 #if defined(CONFIG_BF537) || defined(CONFIG_BF536)
1262 if (__SILICON_REVISION__ < 3) {
1282 static void bfin_mac_timeout(
struct net_device *dev)
1293 while (tx_list_head != current_tx_ptr) {
1295 tx_list_head->
status.status_word = 0;
1296 if (tx_list_head->
skb) {
1297 dev_kfree_skb(tx_list_head->
skb);
1300 tx_list_head = tx_list_head->
next;
1303 if (netif_queue_stopped(lp->
ndev))
1304 netif_wake_queue(lp->
ndev);
1306 bfin_mac_enable(lp->
phydev);
1310 netif_wake_queue(dev);
1313 static void bfin_mac_multicast_hash(
struct net_device *dev)
1315 u32 emac_hashhi, emac_hashlo;
1319 emac_hashhi = emac_hashlo = 0;
1326 emac_hashhi |= 1 << (crc & 0x1f);
1328 emac_hashlo |= 1 << (crc & 0x1f);
1341 static void bfin_mac_set_multicast_list(
struct net_device *dev)
1346 netdev_info(dev,
"set promisc mode\n");
1360 bfin_mac_multicast_hash(dev);
1369 static int bfin_mac_ioctl(
struct net_device *netdev,
struct ifreq *ifr,
int cmd)
1373 if (!netif_running(netdev))
1390 static void bfin_mac_shutdown(
struct net_device *dev)
1404 static int bfin_mac_open(
struct net_device *dev)
1415 if (!is_valid_ether_addr(dev->
dev_addr)) {
1416 netdev_warn(dev,
"no valid ethernet hw addr\n");
1421 ret = desc_list_init(dev);
1427 setup_system_regs(dev);
1431 ret = bfin_mac_enable(lp->
phydev);
1434 pr_debug(
"hardware init finished\n");
1436 netif_start_queue(dev);
1447 static int bfin_mac_close(
struct net_device *dev)
1452 netif_stop_queue(dev);
1459 bfin_mac_shutdown(dev);
1468 .ndo_open = bfin_mac_open,
1469 .ndo_stop = bfin_mac_close,
1470 .ndo_start_xmit = bfin_mac_hard_start_xmit,
1471 .ndo_set_mac_address = bfin_mac_set_mac_address,
1472 .ndo_tx_timeout = bfin_mac_timeout,
1473 .ndo_set_rx_mode = bfin_mac_set_multicast_list,
1474 .ndo_do_ioctl = bfin_mac_ioctl,
1477 #ifdef CONFIG_NET_POLL_CONTROLLER
1478 .ndo_poll_controller = bfin_mac_poll,
1495 platform_set_drvdata(pdev, ndev);
1496 lp = netdev_priv(ndev);
1507 dev_err(&pdev->
dev,
"Cannot detect Blackfin on-chip ethernet MAC controller!\n");
1509 goto out_err_probe_mac;
1518 if (!is_valid_ether_addr(ndev->
dev_addr)) {
1520 !is_valid_ether_addr(ndev->
dev_addr)) {
1522 netdev_warn(ndev,
"Setting Ethernet MAC to a random one\n");
1523 eth_hw_addr_random(ndev);
1529 if (!pdev->
dev.platform_data) {
1530 dev_err(&pdev->
dev,
"Cannot get platform device bfin_mii_bus!\n");
1532 goto out_err_probe_mac;
1534 pd = pdev->
dev.platform_data;
1535 lp->
mii_bus = platform_get_drvdata(pd);
1537 dev_err(&pdev->
dev,
"Cannot get mii_bus!\n");
1539 goto out_err_probe_mac;
1542 mii_bus_data = pd->
dev.platform_data;
1544 rc = mii_probe(ndev, mii_bus_data->
phy_mode);
1547 goto out_err_mii_probe;
1570 dev_err(&pdev->
dev,
"Cannot request Blackfin MAC RX IRQ!\n");
1572 goto out_err_request_irq;
1577 dev_err(&pdev->
dev,
"Cannot register net device!\n");
1578 goto out_err_reg_ndev;
1590 out_err_request_irq:
1595 platform_set_drvdata(pdev,
NULL);
1603 struct net_device *ndev = platform_get_drvdata(pdev);
1606 platform_set_drvdata(pdev,
NULL);
1622 struct net_device *net_dev = platform_get_drvdata(pdev);
1630 if (netif_running(net_dev))
1631 bfin_mac_close(net_dev);
1639 struct net_device *net_dev = platform_get_drvdata(pdev);
1647 if (netif_running(net_dev))
1648 bfin_mac_open(net_dev);
1654 #define bfin_mac_suspend NULL
1655 #define bfin_mac_resume NULL
1662 const unsigned short *pin_req;
1665 mii_bus_pd = dev_get_platdata(&pdev->
dev);
1667 dev_err(&pdev->
dev,
"No peripherals in platform data!\n");
1678 dev_err(&pdev->
dev,
"Requesting peripherals failed!\n");
1683 miibus = mdiobus_alloc();
1686 miibus->
read = bfin_mdiobus_read;
1687 miibus->
write = bfin_mdiobus_write;
1688 miibus->
reset = bfin_mdiobus_reset;
1691 miibus->
name =
"bfin_mii_bus";
1698 goto out_err_irq_alloc;
1705 dev_err(&pdev->
dev,
"Invalid number (%i) of phydevs\n",
1707 for (i = 0; i <
rc; ++
i) {
1709 if (phyaddr < PHY_MAX_ADDR)
1713 "Invalid PHY address %i for phydev %i\n",
1719 dev_err(&pdev->
dev,
"Cannot register MDIO bus!\n");
1720 goto out_err_mdiobus_register;
1723 platform_set_drvdata(pdev, miibus);
1726 out_err_mdiobus_register:
1738 struct mii_bus *miibus = platform_get_drvdata(pdev);
1740 dev_get_platdata(&pdev->
dev);
1742 platform_set_drvdata(pdev,
NULL);
1752 .probe = bfin_mii_bus_probe,
1755 .name =
"bfin_mii_bus",
1761 .probe = bfin_mac_probe,
1766 .name = KBUILD_MODNAME,
1771 static int __init bfin_mac_init(
void)
1782 static void __exit bfin_mac_cleanup(
void)