24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/ptrace.h>
28 #include <linux/errno.h>
30 #include <linux/slab.h>
32 #include <linux/pci.h>
35 #include <linux/netdevice.h>
40 #include <linux/bitops.h>
54 #include <asm/cacheflush.h>
63 #if defined(CONFIG_ARM)
64 #define FEC_ALIGNMENT 0xf
66 #define FEC_ALIGNMENT 0x3
69 #define DRIVER_NAME "fec"
72 #define FEC_QUIRK_ENET_MAC (1 << 0)
74 #define FEC_QUIRK_SWAP_FRAME (1 << 1)
76 #define FEC_QUIRK_USE_GASKET (1 << 2)
78 #define FEC_QUIRK_HAS_GBIT (1 << 3)
111 { .compatible =
"fsl,imx25-fec", .data = &fec_devtype[
IMX25_FEC], },
112 { .compatible =
"fsl,imx27-fec", .data = &fec_devtype[
IMX27_FEC], },
113 { .compatible =
"fsl,imx28-fec", .data = &fec_devtype[
IMX28_FEC], },
114 { .compatible =
"fsl,imx6q-fec", .data = &fec_devtype[
IMX6Q_FEC], },
123 #if defined(CONFIG_M5272)
128 #if defined(CONFIG_NETtel)
129 #define FEC_FLASHMAC 0xf0006006
130 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
131 #define FEC_FLASHMAC 0xf0006000
132 #elif defined(CONFIG_CANCam)
133 #define FEC_FLASHMAC 0xf0020000
134 #elif defined (CONFIG_M5272C3)
135 #define FEC_FLASHMAC (0xffe04000 + 4)
136 #elif defined(CONFIG_MOD5272)
137 #define FEC_FLASHMAC 0xffc0406b
139 #define FEC_FLASHMAC 0
149 #define FEC_ENET_RX_PAGES 8
150 #define FEC_ENET_RX_FRSIZE 2048
151 #define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
152 #define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
153 #define FEC_ENET_TX_FRSIZE 2048
154 #define FEC_ENET_TX_FRPPG (PAGE_SIZE / FEC_ENET_TX_FRSIZE)
155 #define TX_RING_SIZE 16
156 #define TX_RING_MOD_MASK 15
158 #if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
159 #error "FEC: descriptor ring size constants too large"
163 #define FEC_ENET_HBERR ((uint)0x80000000)
164 #define FEC_ENET_BABR ((uint)0x40000000)
165 #define FEC_ENET_BABT ((uint)0x20000000)
166 #define FEC_ENET_GRA ((uint)0x10000000)
167 #define FEC_ENET_TXF ((uint)0x08000000)
168 #define FEC_ENET_TXB ((uint)0x04000000)
169 #define FEC_ENET_RXF ((uint)0x02000000)
170 #define FEC_ENET_RXB ((uint)0x01000000)
171 #define FEC_ENET_MII ((uint)0x00800000)
172 #define FEC_ENET_EBERR ((uint)0x00400000)
174 #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
178 #define PKT_MAXBUF_SIZE 1518
179 #define PKT_MINBUF_SIZE 64
180 #define PKT_MAXBLR_SIZE 1520
183 #define FEC_IRQ_NUM 3
190 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
191 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
192 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
194 #define OPT_FRAME_SIZE 0
253 #define FEC_MMFR_ST (1 << 30)
254 #define FEC_MMFR_OP_READ (2 << 28)
255 #define FEC_MMFR_OP_WRITE (1 << 28)
256 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
257 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
258 #define FEC_MMFR_TA (2 << 16)
259 #define FEC_MMFR_DATA(v) (v & 0xffff)
261 #define FEC_MII_TIMEOUT 30000
264 #define TX_TIMEOUT (2 * HZ)
268 static void *swap_buffer(
void *bufaddr,
int len)
271 unsigned int *
buf = bufaddr;
273 for (i = 0; i < (len + 3) / 4; i++, buf++)
306 spin_unlock_irqrestore(&fep->
hw_lock, flags);
335 swap_buffer(bufaddr, skb->
len);
367 netif_stop_queue(ndev);
372 skb_tx_timestamp(skb);
374 spin_unlock_irqrestore(&fep->
hw_lock, flags);
415 writel(0, fep->
hwp + FEC_HASH_TABLE_HIGH);
416 writel(0, fep->
hwp + FEC_HASH_TABLE_LOW);
460 rcntl |= 0x40000000 | 0x00000020;
480 #ifdef FEC_MIIGSK_ENR
485 while (
readl(fep->
hwp + FEC_MIIGSK_ENR) & 4)
494 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
496 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
497 writel(cfgr, fep->
hwp + FEC_MIIGSK_CFGR);
534 printk(
"fec_stop : Graceful transmit stop did not complete !\n");
556 ndev->
stats.tx_errors++;
559 netif_wake_queue(ndev);
570 fep = netdev_priv(ndev);
574 while (((status = bdp->
cbd_sc) & BD_ENET_TX_READY) == 0) {
587 ndev->
stats.tx_errors++;
589 ndev->
stats.tx_heartbeat_errors++;
591 ndev->
stats.tx_window_errors++;
593 ndev->
stats.tx_aborted_errors++;
595 ndev->
stats.tx_fifo_errors++;
597 ndev->
stats.tx_carrier_errors++;
599 ndev->
stats.tx_packets++;
602 if (status & BD_ENET_TX_READY)
603 printk(
"HEY! Enet xmit interrupt and TX_READY.\n");
609 ndev->
stats.collisions++;
617 if (status & BD_ENET_TX_WRAP)
626 if (netif_queue_stopped(ndev))
627 netif_wake_queue(ndev);
669 printk(
"FEC ENET: rcv is not +last\n");
672 goto rx_processing_done;
677 ndev->
stats.rx_errors++;
680 ndev->
stats.rx_length_errors++;
683 ndev->
stats.rx_frame_errors++;
685 ndev->
stats.rx_crc_errors++;
687 ndev->
stats.rx_fifo_errors++;
695 ndev->
stats.rx_errors++;
696 ndev->
stats.rx_frame_errors++;
697 goto rx_processing_done;
701 ndev->
stats.rx_packets++;
710 swap_buffer(data, pkt_len);
717 skb = netdev_alloc_skb(ndev, pkt_len - 4 +
NET_IP_ALIGN);
720 printk(
"%s: Memory squeeze, dropping packet.\n",
722 ndev->
stats.rx_dropped++;
726 skb_copy_to_linear_data(skb, data, pkt_len - 4);
759 fec_enet_interrupt(
int irq,
void *
dev_id)
788 }
while (int_events);
800 unsigned char *iap, tmpaddr[
ETH_ALEN];
814 if (!is_valid_ether_addr(iap)) {
819 iap = (
unsigned char *) mac;
827 if (!is_valid_ether_addr(iap)) {
830 iap = (
unsigned char *)FEC_FLASHMAC;
833 iap = (
unsigned char *)&pdata->
mac;
840 if (!is_valid_ether_addr(iap)) {
841 *((
unsigned long *) &tmpaddr[0]) =
843 *((
unsigned short *) &tmpaddr[4]) =
860 static void fec_enet_adjust_link(
struct net_device *ndev)
866 int status_change = 0;
879 fec_restart(ndev, phy_dev->
duplex);
890 fec_restart(ndev, phy_dev->
duplex);
897 spin_unlock_irqrestore(&fep->
hw_lock, flags);
903 static int fec_enet_mdio_read(
struct mii_bus *
bus,
int mii_id,
int regnum)
906 unsigned long time_left;
919 if (time_left == 0) {
929 static int fec_enet_mdio_write(
struct mii_bus *bus,
int mii_id,
int regnum,
933 unsigned long time_left;
947 if (time_left == 0) {
956 static int fec_enet_mdio_reset(
struct mii_bus *bus)
961 static int fec_enet_mii_probe(
struct net_device *ndev)
976 if ((fep->
mii_bus->phy_mask & (1 << phy_id)))
980 if (fep->
mii_bus->phy_map[phy_id]->phy_id == 0)
990 "%s: no PHY, assuming direct connection to switch\n",
997 phy_dev =
phy_connect(ndev, phy_name, &fec_enet_adjust_link, 0,
999 if (IS_ERR(phy_dev)) {
1001 return PTR_ERR(phy_dev);
1017 "%s: Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1027 static struct mii_bus *fec0_mii_bus;
1028 struct net_device *ndev = platform_get_drvdata(pdev);
1052 if (mii_cnt && fec0_mii_bus) {
1076 fep->
mii_bus = mdiobus_alloc();
1082 fep->
mii_bus->name =
"fec_enet_mii_bus";
1083 fep->
mii_bus->read = fec_enet_mdio_read;
1084 fep->
mii_bus->write = fec_enet_mdio_write;
1085 fep->
mii_bus->reset = fec_enet_mdio_reset;
1094 goto err_out_free_mdiobus;
1101 goto err_out_free_mdio_irq;
1111 err_out_free_mdio_irq:
1113 err_out_free_mdiobus:
1121 if (--mii_cnt == 0) {
1128 static int fec_enet_get_settings(
struct net_device *ndev,
1140 static int fec_enet_set_settings(
struct net_device *ndev,
1152 static void fec_enet_get_drvinfo(
struct net_device *ndev,
1162 static const struct ethtool_ops fec_enet_ethtool_ops = {
1163 .get_settings = fec_enet_get_settings,
1164 .set_settings = fec_enet_set_settings,
1165 .get_drvinfo = fec_enet_get_drvinfo,
1175 if (!netif_running(ndev))
1184 static void fec_enet_free_buffers(
struct net_device *ndev)
1208 static int fec_enet_alloc_buffers(
struct net_device *ndev)
1219 fec_enet_free_buffers(ndev);
1260 ret = fec_enet_alloc_buffers(ndev);
1265 ret = fec_enet_mii_probe(ndev);
1267 fec_enet_free_buffers(ndev);
1271 netif_start_queue(ndev);
1283 netif_stop_queue(ndev);
1291 fec_enet_free_buffers(ndev);
1307 #define CRC32_POLY 0xEDB88320
1309 static void set_multicast_list(
struct net_device *ndev)
1346 for (i = 0; i < ndev->
addr_len; i++) {
1348 for (bit = 0; bit < 8; bit++, data >>= 1) {
1357 hash = (crc >> (32 -
HASH_BITS)) & 0x3f;
1361 tmp |= 1 << (hash - 32);
1373 fec_set_mac_address(
struct net_device *ndev,
void *
p)
1378 if (!is_valid_ether_addr(addr->
sa_data))
1391 #ifdef CONFIG_NET_POLL_CONTROLLER
1405 if (fep->
irq[i] > 0) {
1407 fec_enet_interrupt(fep->
irq[i], dev);
1415 .ndo_open = fec_enet_open,
1416 .ndo_stop = fec_enet_close,
1417 .ndo_start_xmit = fec_enet_start_xmit,
1418 .ndo_set_rx_mode = set_multicast_list,
1421 .ndo_tx_timeout = fec_timeout,
1422 .ndo_set_mac_address = fec_set_mac_address,
1423 .ndo_do_ioctl = fec_enet_ioctl,
1424 #ifdef CONFIG_NET_POLL_CONTROLLER
1425 .ndo_poll_controller = fec_poll_controller,
1433 static int fec_enet_init(
struct net_device *ndev)
1444 printk(
"FEC: allocate descriptor memory failed?\n");
1491 fec_restart(ndev, 0);
1516 of_property_read_u32(np,
"phy-reset-duration", &msec);
1521 phy_reset = of_get_named_gpio(np,
"phy-reset-gpios", 0);
1525 pr_debug(
"FEC: failed to get gpio phy-reset: %d\n", err);
1552 int i,
irq, ret = 0;
1575 goto failed_alloc_etherdev;
1581 fep = netdev_priv(ndev);
1589 goto failed_ioremap;
1592 platform_set_drvdata(pdev, ndev);
1594 ret = fec_get_phy_mode_dt(pdev);
1596 pdata = pdev->
dev.platform_data;
1623 pinctrl = devm_pinctrl_get_select_default(&pdev->
dev);
1624 if (IS_ERR(pinctrl)) {
1625 ret = PTR_ERR(pinctrl);
1641 clk_prepare_enable(fep->
clk_ahb);
1642 clk_prepare_enable(fep->
clk_ipg);
1645 if (!IS_ERR(reg_phy)) {
1649 "Failed to enable phy regulator: %d\n", ret);
1650 goto failed_regulator;
1654 fec_reset_phy(pdev);
1656 ret = fec_enet_init(ndev);
1660 ret = fec_enet_mii_init(pdev);
1662 goto failed_mii_init;
1669 goto failed_register;
1674 fec_enet_mii_remove(fep);
1678 clk_disable_unprepare(fep->
clk_ahb);
1679 clk_disable_unprepare(fep->
clk_ipg);
1691 failed_alloc_etherdev:
1700 struct net_device *ndev = platform_get_drvdata(pdev);
1706 fec_enet_mii_remove(fep);
1712 clk_disable_unprepare(fep->
clk_ahb);
1713 clk_disable_unprepare(fep->
clk_ipg);
1721 platform_set_drvdata(pdev,
NULL);
1728 fec_suspend(
struct device *dev)
1733 if (netif_running(ndev)) {
1737 clk_disable_unprepare(fep->
clk_ahb);
1738 clk_disable_unprepare(fep->
clk_ipg);
1744 fec_resume(
struct device *dev)
1749 clk_prepare_enable(fep->
clk_ahb);
1750 clk_prepare_enable(fep->
clk_ipg);
1751 if (netif_running(ndev)) {
1759 static const struct dev_pm_ops fec_pm_ops = {
1761 .resume = fec_resume,
1762 .freeze = fec_suspend,
1764 .poweroff = fec_suspend,
1765 .restore = fec_resume,
1776 .of_match_table = fec_dt_ids,
1778 .id_table = fec_devtype,