16 #include <linux/kernel.h>
20 #include <linux/module.h>
24 #include <linux/if_ether.h>
26 #include <linux/netdevice.h>
40 #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
41 NETIF_MSG_DRV | NETIF_MSG_LINK | \
42 NETIF_MSG_IFUP | NETIF_MSG_INTR | \
43 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
44 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
45 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
46 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
49 #define cpsw_info(priv, type, format, ...) \
51 if (netif_msg_##type(priv) && net_ratelimit()) \
52 dev_info(priv->dev, format, ## __VA_ARGS__); \
55 #define cpsw_err(priv, type, format, ...) \
57 if (netif_msg_##type(priv) && net_ratelimit()) \
58 dev_err(priv->dev, format, ## __VA_ARGS__); \
61 #define cpsw_dbg(priv, type, format, ...) \
63 if (netif_msg_##type(priv) && net_ratelimit()) \
64 dev_dbg(priv->dev, format, ## __VA_ARGS__); \
67 #define cpsw_notice(priv, type, format, ...) \
69 if (netif_msg_##type(priv) && net_ratelimit()) \
70 dev_notice(priv->dev, format, ## __VA_ARGS__); \
73 #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
74 #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
75 #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
77 #define CPDMA_RXTHRESH 0x0c0
78 #define CPDMA_RXFREE 0x0e0
79 #define CPDMA_TXHDP 0x00
80 #define CPDMA_RXHDP 0x20
81 #define CPDMA_TXCP 0x40
82 #define CPDMA_RXCP 0x60
84 #define cpsw_dma_regs(base, offset) \
85 (void __iomem *)((base) + (offset))
86 #define cpsw_dma_rxthresh(base, offset) \
87 (void __iomem *)((base) + (offset) + CPDMA_RXTHRESH)
88 #define cpsw_dma_rxfree(base, offset) \
89 (void __iomem *)((base) + (offset) + CPDMA_RXFREE)
90 #define cpsw_dma_txhdp(base, offset) \
91 (void __iomem *)((base) + (offset) + CPDMA_TXHDP)
92 #define cpsw_dma_rxhdp(base, offset) \
93 (void __iomem *)((base) + (offset) + CPDMA_RXHDP)
94 #define cpsw_dma_txcp(base, offset) \
95 (void __iomem *)((base) + (offset) + CPDMA_TXCP)
96 #define cpsw_dma_rxcp(base, offset) \
97 (void __iomem *)((base) + (offset) + CPDMA_RXCP)
99 #define CPSW_POLL_WEIGHT 64
100 #define CPSW_MIN_PACKET_SIZE 60
101 #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
103 #define RX_PRIORITY_MAPPING 0x76543210
104 #define TX_PRIORITY_MAPPING 0x33221100
105 #define CPDMA_TX_PRIORITY_MAP 0x76543210
107 #define cpsw_enable_irq(priv) \
110 for (i = 0; i < priv->num_irqs; i++) \
111 enable_irq(priv->irqs_table[i]); \
113 #define cpsw_disable_irq(priv) \
116 for (i = 0; i < priv->num_irqs; i++) \
117 disable_irq_nosync(priv->irqs_table[i]); \
120 static int debug_level;
124 static int ale_ageout = 10;
223 #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
224 #define for_each_slave(priv, func, arg...) \
227 for (idx = 0; idx < (priv)->data.slaves; idx++) \
228 (func)((priv)->slaves + idx, ##arg); \
255 if (
unlikely(netif_queue_stopped(ndev)))
256 netif_start_queue(ndev);
257 priv->
stats.tx_packets++;
258 priv->
stats.tx_bytes += len;
270 if (
unlikely(!netif_running(ndev)) ||
271 unlikely(!netif_carrier_ok(ndev))) {
275 if (
likely(status >= 0)) {
279 priv->
stats.rx_bytes += len;
280 priv->
stats.rx_packets++;
284 if (
unlikely(!netif_running(ndev))) {
306 cpsw_intr_disable(priv);
308 napi_schedule(&priv->
napi);
313 static inline int cpsw_get_slave_port(
struct cpsw_priv *priv,
u32 slave_num)
316 return slave_num + 1;
329 if (num_rx || num_tx)
333 if (num_rx < budget) {
335 cpsw_intr_enable(priv);
355 #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
356 ((mac)[2] << 16) | ((mac)[3] << 24))
357 #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
376 slave_port = cpsw_get_slave_port(priv, slave->
slave_num);
379 mac_control = priv->
data.mac_control;
385 if (phy->
speed == 1000)
386 mac_control |=
BIT(7);
388 mac_control |=
BIT(0);
391 if (phy->
speed == 100)
392 mac_control |=
BIT(15);
412 struct cpsw_priv *priv = netdev_priv(ndev);
419 if (netif_running(ndev))
420 netif_wake_queue(ndev);
423 netif_stop_queue(ndev);
427 static inline int __show_stat(
char *
buf,
int maxlen,
const char *
name,
u32 val)
429 static char *leader =
"........................................";
434 return snprintf(buf, maxlen,
"%s %s %10d\n", name,
435 leader +
strlen(name), val);
453 cpsw_set_slave_mac(slave, priv);
457 slave_port = cpsw_get_slave_port(priv, slave->
slave_num);
463 &cpsw_adjust_link, 0, slave->
data->phy_if);
464 if (IS_ERR(slave->
phy)) {
465 dev_err(priv->
dev,
"phy %s not found on slave %d\n",
475 static void cpsw_init_host_port(
struct cpsw_priv *priv)
497 static int cpsw_ndo_open(
struct net_device *ndev)
499 struct cpsw_priv *priv = netdev_priv(ndev);
503 cpsw_intr_disable(priv);
506 pm_runtime_get_sync(&priv->
pdev->dev);
510 dev_info(priv->
dev,
"initializing cpsw version %d.%d (%d)\n",
515 cpsw_init_host_port(priv);
529 priv->
data.rx_descs = 128;
531 for (i = 0; i < priv->
data.rx_descs; i++) {
535 skb = netdev_alloc_skb_ip_align(priv->
ndev,
545 cpsw_info(priv, ifup,
"submitted %d rx descriptors\n", i);
548 cpsw_intr_enable(priv);
549 napi_enable(&priv->
napi);
564 static int cpsw_ndo_stop(
struct net_device *ndev)
566 struct cpsw_priv *priv = netdev_priv(ndev);
568 cpsw_info(priv, ifdown,
"shutting down cpsw device\n");
569 cpsw_intr_disable(priv);
572 netif_stop_queue(priv->
ndev);
573 napi_disable(&priv->
napi);
577 pm_runtime_put_sync(&priv->
pdev->dev);
584 struct cpsw_priv *priv = netdev_priv(ndev);
590 cpsw_err(priv, tx_err,
"packet pad failed\n");
591 priv->
stats.tx_dropped++;
598 cpsw_err(priv, tx_err,
"desc submit failed\n");
604 priv->
stats.tx_dropped++;
605 netif_stop_queue(ndev);
609 static void cpsw_ndo_change_rx_flags(
struct net_device *ndev,
int flags)
620 dev_err(&ndev->
dev,
"promiscuity ignored!\n");
629 dev_err(&ndev->
dev,
"multicast traffic cannot be filtered!\n");
632 static void cpsw_ndo_tx_timeout(
struct net_device *ndev)
634 struct cpsw_priv *priv = netdev_priv(ndev);
636 cpsw_err(priv, tx_err,
"transmit timeout, restarting dma\n");
637 priv->
stats.tx_errors++;
638 cpsw_intr_disable(priv);
643 cpsw_intr_enable(priv);
649 struct cpsw_priv *priv = netdev_priv(ndev);
653 #ifdef CONFIG_NET_POLL_CONTROLLER
654 static void cpsw_ndo_poll_controller(
struct net_device *ndev)
656 struct cpsw_priv *priv = netdev_priv(ndev);
658 cpsw_intr_disable(priv);
660 cpsw_interrupt(ndev->
irq, priv);
662 cpsw_intr_enable(priv);
668 .ndo_open = cpsw_ndo_open,
669 .ndo_stop = cpsw_ndo_stop,
670 .ndo_start_xmit = cpsw_ndo_start_xmit,
671 .ndo_change_rx_flags = cpsw_ndo_change_rx_flags,
674 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
675 .ndo_get_stats = cpsw_ndo_get_stats,
676 #ifdef CONFIG_NET_POLL_CONTROLLER
677 .ndo_poll_controller = cpsw_ndo_poll_controller,
681 static void cpsw_get_drvinfo(
struct net_device *ndev,
684 struct cpsw_priv *priv = netdev_priv(ndev);
692 struct cpsw_priv *priv = netdev_priv(ndev);
698 struct cpsw_priv *priv = netdev_priv(ndev);
702 static const struct ethtool_ops cpsw_ethtool_ops = {
703 .get_drvinfo = cpsw_get_drvinfo,
704 .get_msglevel = cpsw_get_msglevel,
705 .set_msglevel = cpsw_set_msglevel,
731 if (of_property_read_u32(node,
"slaves", &prop)) {
732 pr_err(
"Missing slaves property in the DT.\n");
740 pr_err(
"Could not allocate slave memory.\n");
744 data->
no_bd_ram = of_property_read_bool(node,
"no_bd_ram");
746 if (of_property_read_u32(node,
"cpdma_channels", &prop)) {
747 pr_err(
"Missing cpdma_channels property in the DT.\n");
753 if (of_property_read_u32(node,
"host_port_no", &prop)) {
754 pr_err(
"Missing host_port_no property in the DT.\n");
760 if (of_property_read_u32(node,
"cpdma_reg_ofs", &prop)) {
761 pr_err(
"Missing cpdma_reg_ofs property in the DT.\n");
767 if (of_property_read_u32(node,
"cpdma_sram_ofs", &prop)) {
768 pr_err(
"Missing cpdma_sram_ofs property in the DT.\n");
774 if (of_property_read_u32(node,
"ale_reg_ofs", &prop)) {
775 pr_err(
"Missing ale_reg_ofs property in the DT.\n");
781 if (of_property_read_u32(node,
"ale_entries", &prop)) {
782 pr_err(
"Missing ale_entries property in the DT.\n");
788 if (of_property_read_u32(node,
"host_port_reg_ofs", &prop)) {
789 pr_err(
"Missing host_port_reg_ofs property in the DT.\n");
795 if (of_property_read_u32(node,
"hw_stats_reg_ofs", &prop)) {
796 pr_err(
"Missing hw_stats_reg_ofs property in the DT.\n");
802 if (of_property_read_u32(node,
"bd_ram_ofs", &prop)) {
803 pr_err(
"Missing bd_ram_ofs property in the DT.\n");
809 if (of_property_read_u32(node,
"bd_ram_size", &prop)) {
810 pr_err(
"Missing bd_ram_size property in the DT.\n");
816 if (of_property_read_u32(node,
"rx_descs", &prop)) {
817 pr_err(
"Missing rx_descs property in the DT.\n");
823 if (of_property_read_u32(node,
"mac_control", &prop)) {
824 pr_err(
"Missing mac_control property in the DT.\n");
836 pr_err(
"Missing slave[%d] phy_id property\n", i);
842 if (of_property_read_u32(slave_node,
"slave_reg_ofs", &prop)) {
843 pr_err(
"Missing slave[%d] slave_reg_ofs property\n", i);
849 if (of_property_read_u32(slave_node,
"sliver_reg_ofs",
851 pr_err(
"Missing slave[%d] sliver_reg_ofs property\n",
881 int ret = 0,
i,
k = 0;
883 ndev = alloc_etherdev(
sizeof(
struct cpsw_priv));
885 pr_err(
"error allocating net_device\n");
889 platform_set_drvdata(pdev, ndev);
890 priv = netdev_priv(ndev);
898 if (cpsw_probe_dt(&priv->
data, pdev)) {
899 pr_err(
"cpsw: platform data missing\n");
905 if (is_valid_ether_addr(data->
slave_data[0].mac_addr)) {
921 for (i = 0; i < data->
slaves; i++)
922 priv->
slaves[i].slave_num = i;
926 if (IS_ERR(priv->
clk)) {
929 goto clean_slave_ret;
934 dev_err(priv->
dev,
"error getting i/o resource\n");
941 dev_err(priv->
dev,
"failed request i/o region\n");
948 dev_err(priv->
dev,
"unable to map i/o region\n");
949 goto clean_cpsw_iores_ret;
957 dev_err(priv->
dev,
"error getting i/o resource\n");
964 dev_err(priv->
dev,
"failed request i/o region\n");
972 dev_err(priv->
dev,
"unable to map i/o region\n");
973 goto clean_cpsw_ss_iores_ret;
979 memset(&dma_params, 0,
sizeof(dma_params));
980 dma_params.dev = &pdev->
dev;
996 dma_params.num_chan = data->
channels;
997 dma_params.has_soft_reset =
true;
1000 dma_params.desc_align = 16;
1001 dma_params.has_ext_regs =
true;
1002 dma_params.desc_mem_phys = data->
no_bd_ram ? 0 :
1009 dev_err(priv->
dev,
"error initializing dma\n");
1011 goto clean_iomap_ret;
1020 dev_err(priv->
dev,
"error initializing dma channels\n");
1025 memset(&ale_params, 0,
sizeof(ale_params));
1026 ale_params.dev = &ndev->
dev;
1027 ale_params.ale_regs = (
void *)((
u32)priv->
regs) +
1029 ale_params.ale_ageout = ale_ageout;
1031 ale_params.ale_ports = data->
slaves;
1035 dev_err(priv->
dev,
"error initializing ale engine\n");
1041 if (ndev->
irq < 0) {
1042 dev_err(priv->
dev,
"error getting irq resource\n");
1048 for (i = res->
start; i <= res->
end; i++) {
1050 dev_name(&pdev->
dev), priv)) {
1070 dev_err(priv->
dev,
"error registering net device\n");
1075 cpsw_notice(priv, probe,
"initialized device (regs %x, irq %d)\n",
1090 clean_cpsw_ss_iores_ret:
1093 clean_cpsw_iores_ret:
1099 pm_runtime_disable(&pdev->
dev);
1108 struct net_device *ndev = platform_get_drvdata(pdev);
1109 struct cpsw_priv *priv = netdev_priv(ndev);
1112 platform_set_drvdata(pdev,
NULL);
1124 pm_runtime_disable(&pdev->
dev);
1132 static int cpsw_suspend(
struct device *
dev)
1135 struct net_device *ndev = platform_get_drvdata(pdev);
1137 if (netif_running(ndev))
1138 cpsw_ndo_stop(ndev);
1139 pm_runtime_put_sync(&pdev->
dev);
1144 static int cpsw_resume(
struct device *dev)
1147 struct net_device *ndev = platform_get_drvdata(pdev);
1149 pm_runtime_get_sync(&pdev->
dev);
1150 if (netif_running(ndev))
1151 cpsw_ndo_open(ndev);
1155 static const struct dev_pm_ops cpsw_pm_ops = {
1156 .suspend = cpsw_suspend,
1157 .resume = cpsw_resume,
1161 { .compatible =
"ti,cpsw", },
1172 .probe = cpsw_probe,
1176 static int __init cpsw_init(
void)
1182 static void __exit cpsw_exit(
void)