cpsw.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /*
  2. * Texas Instruments Ethernet Switch Driver
  3. *
  4. * Copyright (C) 2012 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/io.h>
  17. #include <linux/clk.h>
  18. #include <linux/timer.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/irqreturn.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/net_tstamp.h>
  27. #include <linux/phy.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/delay.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/of.h>
  32. #include <linux/of_net.h>
  33. #include <linux/of_device.h>
  34. #include <linux/platform_data/cpsw.h>
  35. #include "cpsw_ale.h"
  36. #include "cpts.h"
  37. #include "davinci_cpdma.h"
  38. #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
  39. NETIF_MSG_DRV | NETIF_MSG_LINK | \
  40. NETIF_MSG_IFUP | NETIF_MSG_INTR | \
  41. NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
  42. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
  43. NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
  44. NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
  45. NETIF_MSG_RX_STATUS)
  46. #define cpsw_info(priv, type, format, ...) \
  47. do { \
  48. if (netif_msg_##type(priv) && net_ratelimit()) \
  49. dev_info(priv->dev, format, ## __VA_ARGS__); \
  50. } while (0)
  51. #define cpsw_err(priv, type, format, ...) \
  52. do { \
  53. if (netif_msg_##type(priv) && net_ratelimit()) \
  54. dev_err(priv->dev, format, ## __VA_ARGS__); \
  55. } while (0)
  56. #define cpsw_dbg(priv, type, format, ...) \
  57. do { \
  58. if (netif_msg_##type(priv) && net_ratelimit()) \
  59. dev_dbg(priv->dev, format, ## __VA_ARGS__); \
  60. } while (0)
  61. #define cpsw_notice(priv, type, format, ...) \
  62. do { \
  63. if (netif_msg_##type(priv) && net_ratelimit()) \
  64. dev_notice(priv->dev, format, ## __VA_ARGS__); \
  65. } while (0)
  66. #define ALE_ALL_PORTS 0x7
  67. #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
  68. #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
  69. #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
  70. #define CPSW_VERSION_1 0x19010a
  71. #define CPSW_VERSION_2 0x19010c
  72. #define HOST_PORT_NUM 0
  73. #define SLIVER_SIZE 0x40
  74. #define CPSW1_HOST_PORT_OFFSET 0x028
  75. #define CPSW1_SLAVE_OFFSET 0x050
  76. #define CPSW1_SLAVE_SIZE 0x040
  77. #define CPSW1_CPDMA_OFFSET 0x100
  78. #define CPSW1_STATERAM_OFFSET 0x200
  79. #define CPSW1_CPTS_OFFSET 0x500
  80. #define CPSW1_ALE_OFFSET 0x600
  81. #define CPSW1_SLIVER_OFFSET 0x700
  82. #define CPSW2_HOST_PORT_OFFSET 0x108
  83. #define CPSW2_SLAVE_OFFSET 0x200
  84. #define CPSW2_SLAVE_SIZE 0x100
  85. #define CPSW2_CPDMA_OFFSET 0x800
  86. #define CPSW2_STATERAM_OFFSET 0xa00
  87. #define CPSW2_CPTS_OFFSET 0xc00
  88. #define CPSW2_ALE_OFFSET 0xd00
  89. #define CPSW2_SLIVER_OFFSET 0xd80
  90. #define CPSW2_BD_OFFSET 0x2000
  91. #define CPDMA_RXTHRESH 0x0c0
  92. #define CPDMA_RXFREE 0x0e0
  93. #define CPDMA_TXHDP 0x00
  94. #define CPDMA_RXHDP 0x20
  95. #define CPDMA_TXCP 0x40
  96. #define CPDMA_RXCP 0x60
  97. #define CPSW_POLL_WEIGHT 64
  98. #define CPSW_MIN_PACKET_SIZE 60
  99. #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
  100. #define RX_PRIORITY_MAPPING 0x76543210
  101. #define TX_PRIORITY_MAPPING 0x33221100
  102. #define CPDMA_TX_PRIORITY_MAP 0x76543210
  103. #define cpsw_enable_irq(priv) \
  104. do { \
  105. u32 i; \
  106. for (i = 0; i < priv->num_irqs; i++) \
  107. enable_irq(priv->irqs_table[i]); \
  108. } while (0);
  109. #define cpsw_disable_irq(priv) \
  110. do { \
  111. u32 i; \
  112. for (i = 0; i < priv->num_irqs; i++) \
  113. disable_irq_nosync(priv->irqs_table[i]); \
  114. } while (0);
  115. static int debug_level;
  116. module_param(debug_level, int, 0);
  117. MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
  118. static int ale_ageout = 10;
  119. module_param(ale_ageout, int, 0);
  120. MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
  121. static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  122. module_param(rx_packet_max, int, 0);
  123. MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
  124. struct cpsw_wr_regs {
  125. u32 id_ver;
  126. u32 soft_reset;
  127. u32 control;
  128. u32 int_control;
  129. u32 rx_thresh_en;
  130. u32 rx_en;
  131. u32 tx_en;
  132. u32 misc_en;
  133. };
  134. struct cpsw_ss_regs {
  135. u32 id_ver;
  136. u32 control;
  137. u32 soft_reset;
  138. u32 stat_port_en;
  139. u32 ptype;
  140. u32 soft_idle;
  141. u32 thru_rate;
  142. u32 gap_thresh;
  143. u32 tx_start_wds;
  144. u32 flow_control;
  145. u32 vlan_ltype;
  146. u32 ts_ltype;
  147. u32 dlr_ltype;
  148. };
  149. /* CPSW_PORT_V1 */
  150. #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
  151. #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
  152. #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
  153. #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
  154. #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
  155. #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
  156. #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
  157. #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
  158. /* CPSW_PORT_V2 */
  159. #define CPSW2_CONTROL 0x00 /* Control Register */
  160. #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
  161. #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
  162. #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
  163. #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
  164. #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
  165. #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
  166. /* CPSW_PORT_V1 and V2 */
  167. #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
  168. #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
  169. #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
  170. /* CPSW_PORT_V2 only */
  171. #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
  172. #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
  173. #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
  174. #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
  175. #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
  176. #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
  177. #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
  178. #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
  179. /* Bit definitions for the CPSW2_CONTROL register */
  180. #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
  181. #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
  182. #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
  183. #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
  184. #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
  185. #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
  186. #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
  187. #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
  188. #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
  189. #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
  190. #define TS_BIT8 (1<<8) /* ts_ttl_nonzero? */
  191. #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
  192. #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
  193. #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
  194. #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
  195. #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
  196. #define CTRL_TS_BITS \
  197. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 | TS_BIT8 | \
  198. TS_ANNEX_D_EN | TS_LTYPE1_EN)
  199. #define CTRL_ALL_TS_MASK (CTRL_TS_BITS | TS_TX_EN | TS_RX_EN)
  200. #define CTRL_TX_TS_BITS (CTRL_TS_BITS | TS_TX_EN)
  201. #define CTRL_RX_TS_BITS (CTRL_TS_BITS | TS_RX_EN)
  202. /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
  203. #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
  204. #define TS_SEQ_ID_OFFSET_MASK (0x3f)
  205. #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
  206. #define TS_MSG_TYPE_EN_MASK (0xffff)
  207. /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
  208. #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
  209. /* Bit definitions for the CPSW1_TS_CTL register */
  210. #define CPSW_V1_TS_RX_EN BIT(0)
  211. #define CPSW_V1_TS_TX_EN BIT(4)
  212. #define CPSW_V1_MSG_TYPE_OFS 16
  213. /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
  214. #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
  215. struct cpsw_host_regs {
  216. u32 max_blks;
  217. u32 blk_cnt;
  218. u32 flow_thresh;
  219. u32 port_vlan;
  220. u32 tx_pri_map;
  221. u32 cpdma_tx_pri_map;
  222. u32 cpdma_rx_chan_map;
  223. };
  224. struct cpsw_sliver_regs {
  225. u32 id_ver;
  226. u32 mac_control;
  227. u32 mac_status;
  228. u32 soft_reset;
  229. u32 rx_maxlen;
  230. u32 __reserved_0;
  231. u32 rx_pause;
  232. u32 tx_pause;
  233. u32 __reserved_1;
  234. u32 rx_pri_map;
  235. };
  236. struct cpsw_slave {
  237. void __iomem *regs;
  238. struct cpsw_sliver_regs __iomem *sliver;
  239. int slave_num;
  240. u32 mac_control;
  241. struct cpsw_slave_data *data;
  242. struct phy_device *phy;
  243. };
  244. static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
  245. {
  246. return __raw_readl(slave->regs + offset);
  247. }
  248. static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
  249. {
  250. __raw_writel(val, slave->regs + offset);
  251. }
  252. struct cpsw_priv {
  253. spinlock_t lock;
  254. struct platform_device *pdev;
  255. struct net_device *ndev;
  256. struct resource *cpsw_res;
  257. struct resource *cpsw_wr_res;
  258. struct napi_struct napi;
  259. struct device *dev;
  260. struct cpsw_platform_data data;
  261. struct cpsw_ss_regs __iomem *regs;
  262. struct cpsw_wr_regs __iomem *wr_regs;
  263. struct cpsw_host_regs __iomem *host_port_regs;
  264. u32 msg_enable;
  265. u32 version;
  266. struct net_device_stats stats;
  267. int rx_packet_max;
  268. int host_port;
  269. struct clk *clk;
  270. u8 mac_addr[ETH_ALEN];
  271. struct cpsw_slave *slaves;
  272. struct cpdma_ctlr *dma;
  273. struct cpdma_chan *txch, *rxch;
  274. struct cpsw_ale *ale;
  275. /* snapshot of IRQ numbers */
  276. u32 irqs_table[4];
  277. u32 num_irqs;
  278. struct cpts cpts;
  279. };
  280. #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
  281. #define for_each_slave(priv, func, arg...) \
  282. do { \
  283. int idx; \
  284. for (idx = 0; idx < (priv)->data.slaves; idx++) \
  285. (func)((priv)->slaves + idx, ##arg); \
  286. } while (0)
  287. static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
  288. {
  289. struct cpsw_priv *priv = netdev_priv(ndev);
  290. if (ndev->flags & IFF_PROMISC) {
  291. /* Enable promiscuous mode */
  292. dev_err(priv->dev, "Ignoring Promiscuous mode\n");
  293. return;
  294. }
  295. /* Clear all mcast from ALE */
  296. cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port);
  297. if (!netdev_mc_empty(ndev)) {
  298. struct netdev_hw_addr *ha;
  299. /* program multicast address list into ALE register */
  300. netdev_for_each_mc_addr(ha, ndev) {
  301. cpsw_ale_add_mcast(priv->ale, (u8 *)ha->addr,
  302. ALE_ALL_PORTS << priv->host_port, 0, 0);
  303. }
  304. }
  305. }
  306. static void cpsw_intr_enable(struct cpsw_priv *priv)
  307. {
  308. __raw_writel(0xFF, &priv->wr_regs->tx_en);
  309. __raw_writel(0xFF, &priv->wr_regs->rx_en);
  310. cpdma_ctlr_int_ctrl(priv->dma, true);
  311. return;
  312. }
  313. static void cpsw_intr_disable(struct cpsw_priv *priv)
  314. {
  315. __raw_writel(0, &priv->wr_regs->tx_en);
  316. __raw_writel(0, &priv->wr_regs->rx_en);
  317. cpdma_ctlr_int_ctrl(priv->dma, false);
  318. return;
  319. }
  320. void cpsw_tx_handler(void *token, int len, int status)
  321. {
  322. struct sk_buff *skb = token;
  323. struct net_device *ndev = skb->dev;
  324. struct cpsw_priv *priv = netdev_priv(ndev);
  325. /* Check whether the queue is stopped due to stalled tx dma, if the
  326. * queue is stopped then start the queue as we have free desc for tx
  327. */
  328. if (unlikely(netif_queue_stopped(ndev)))
  329. netif_start_queue(ndev);
  330. cpts_tx_timestamp(&priv->cpts, skb);
  331. priv->stats.tx_packets++;
  332. priv->stats.tx_bytes += len;
  333. dev_kfree_skb_any(skb);
  334. }
  335. void cpsw_rx_handler(void *token, int len, int status)
  336. {
  337. struct sk_buff *skb = token;
  338. struct net_device *ndev = skb->dev;
  339. struct cpsw_priv *priv = netdev_priv(ndev);
  340. int ret = 0;
  341. /* free and bail if we are shutting down */
  342. if (unlikely(!netif_running(ndev)) ||
  343. unlikely(!netif_carrier_ok(ndev))) {
  344. dev_kfree_skb_any(skb);
  345. return;
  346. }
  347. if (likely(status >= 0)) {
  348. skb_put(skb, len);
  349. cpts_rx_timestamp(&priv->cpts, skb);
  350. skb->protocol = eth_type_trans(skb, ndev);
  351. netif_receive_skb(skb);
  352. priv->stats.rx_bytes += len;
  353. priv->stats.rx_packets++;
  354. skb = NULL;
  355. }
  356. if (unlikely(!netif_running(ndev))) {
  357. if (skb)
  358. dev_kfree_skb_any(skb);
  359. return;
  360. }
  361. if (likely(!skb)) {
  362. skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
  363. if (WARN_ON(!skb))
  364. return;
  365. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  366. skb_tailroom(skb), GFP_KERNEL);
  367. }
  368. WARN_ON(ret < 0);
  369. }
  370. static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
  371. {
  372. struct cpsw_priv *priv = dev_id;
  373. if (likely(netif_running(priv->ndev))) {
  374. cpsw_intr_disable(priv);
  375. cpsw_disable_irq(priv);
  376. napi_schedule(&priv->napi);
  377. }
  378. return IRQ_HANDLED;
  379. }
  380. static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
  381. {
  382. if (priv->host_port == 0)
  383. return slave_num + 1;
  384. else
  385. return slave_num;
  386. }
  387. static int cpsw_poll(struct napi_struct *napi, int budget)
  388. {
  389. struct cpsw_priv *priv = napi_to_priv(napi);
  390. int num_tx, num_rx;
  391. num_tx = cpdma_chan_process(priv->txch, 128);
  392. num_rx = cpdma_chan_process(priv->rxch, budget);
  393. if (num_rx || num_tx)
  394. cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
  395. num_rx, num_tx);
  396. if (num_rx < budget) {
  397. napi_complete(napi);
  398. cpsw_intr_enable(priv);
  399. cpdma_ctlr_eoi(priv->dma);
  400. cpsw_enable_irq(priv);
  401. }
  402. return num_rx;
  403. }
  404. static inline void soft_reset(const char *module, void __iomem *reg)
  405. {
  406. unsigned long timeout = jiffies + HZ;
  407. __raw_writel(1, reg);
  408. do {
  409. cpu_relax();
  410. } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
  411. WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
  412. }
  413. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  414. ((mac)[2] << 16) | ((mac)[3] << 24))
  415. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  416. static void cpsw_set_slave_mac(struct cpsw_slave *slave,
  417. struct cpsw_priv *priv)
  418. {
  419. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  420. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  421. }
  422. static void _cpsw_adjust_link(struct cpsw_slave *slave,
  423. struct cpsw_priv *priv, bool *link)
  424. {
  425. struct phy_device *phy = slave->phy;
  426. u32 mac_control = 0;
  427. u32 slave_port;
  428. if (!phy)
  429. return;
  430. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  431. if (phy->link) {
  432. mac_control = priv->data.mac_control;
  433. /* enable forwarding */
  434. cpsw_ale_control_set(priv->ale, slave_port,
  435. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  436. if (phy->speed == 1000)
  437. mac_control |= BIT(7); /* GIGABITEN */
  438. if (phy->duplex)
  439. mac_control |= BIT(0); /* FULLDUPLEXEN */
  440. /* set speed_in input in case RMII mode is used in 100Mbps */
  441. if (phy->speed == 100)
  442. mac_control |= BIT(15);
  443. *link = true;
  444. } else {
  445. mac_control = 0;
  446. /* disable forwarding */
  447. cpsw_ale_control_set(priv->ale, slave_port,
  448. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  449. }
  450. if (mac_control != slave->mac_control) {
  451. phy_print_status(phy);
  452. __raw_writel(mac_control, &slave->sliver->mac_control);
  453. }
  454. slave->mac_control = mac_control;
  455. }
  456. static void cpsw_adjust_link(struct net_device *ndev)
  457. {
  458. struct cpsw_priv *priv = netdev_priv(ndev);
  459. bool link = false;
  460. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  461. if (link) {
  462. netif_carrier_on(ndev);
  463. if (netif_running(ndev))
  464. netif_wake_queue(ndev);
  465. } else {
  466. netif_carrier_off(ndev);
  467. netif_stop_queue(ndev);
  468. }
  469. }
  470. static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val)
  471. {
  472. static char *leader = "........................................";
  473. if (!val)
  474. return 0;
  475. else
  476. return snprintf(buf, maxlen, "%s %s %10d\n", name,
  477. leader + strlen(name), val);
  478. }
  479. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  480. {
  481. char name[32];
  482. u32 slave_port;
  483. sprintf(name, "slave-%d", slave->slave_num);
  484. soft_reset(name, &slave->sliver->soft_reset);
  485. /* setup priority mapping */
  486. __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
  487. switch (priv->version) {
  488. case CPSW_VERSION_1:
  489. slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
  490. break;
  491. case CPSW_VERSION_2:
  492. slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
  493. break;
  494. }
  495. /* setup max packet size, and mac address */
  496. __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
  497. cpsw_set_slave_mac(slave, priv);
  498. slave->mac_control = 0; /* no link yet */
  499. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  500. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  501. 1 << slave_port, 0, ALE_MCAST_FWD_2);
  502. slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
  503. &cpsw_adjust_link, slave->data->phy_if);
  504. if (IS_ERR(slave->phy)) {
  505. dev_err(priv->dev, "phy %s not found on slave %d\n",
  506. slave->data->phy_id, slave->slave_num);
  507. slave->phy = NULL;
  508. } else {
  509. dev_info(priv->dev, "phy found : id is : 0x%x\n",
  510. slave->phy->phy_id);
  511. phy_start(slave->phy);
  512. }
  513. }
  514. static void cpsw_init_host_port(struct cpsw_priv *priv)
  515. {
  516. /* soft reset the controller and initialize ale */
  517. soft_reset("cpsw", &priv->regs->soft_reset);
  518. cpsw_ale_start(priv->ale);
  519. /* switch to vlan unaware mode */
  520. cpsw_ale_control_set(priv->ale, 0, ALE_VLAN_AWARE, 0);
  521. /* setup host port priority mapping */
  522. __raw_writel(CPDMA_TX_PRIORITY_MAP,
  523. &priv->host_port_regs->cpdma_tx_pri_map);
  524. __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
  525. cpsw_ale_control_set(priv->ale, priv->host_port,
  526. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  527. cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port, 0);
  528. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  529. 1 << priv->host_port, 0, ALE_MCAST_FWD_2);
  530. }
  531. static int cpsw_ndo_open(struct net_device *ndev)
  532. {
  533. struct cpsw_priv *priv = netdev_priv(ndev);
  534. int i, ret;
  535. u32 reg;
  536. cpsw_intr_disable(priv);
  537. netif_carrier_off(ndev);
  538. pm_runtime_get_sync(&priv->pdev->dev);
  539. reg = priv->version;
  540. dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
  541. CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
  542. CPSW_RTL_VERSION(reg));
  543. /* initialize host and slave ports */
  544. cpsw_init_host_port(priv);
  545. for_each_slave(priv, cpsw_slave_open, priv);
  546. /* setup tx dma to fixed prio and zero offset */
  547. cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
  548. cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
  549. /* disable priority elevation and enable statistics on all ports */
  550. __raw_writel(0, &priv->regs->ptype);
  551. /* enable statistics collection only on the host port */
  552. __raw_writel(0x7, &priv->regs->stat_port_en);
  553. if (WARN_ON(!priv->data.rx_descs))
  554. priv->data.rx_descs = 128;
  555. for (i = 0; i < priv->data.rx_descs; i++) {
  556. struct sk_buff *skb;
  557. ret = -ENOMEM;
  558. skb = netdev_alloc_skb_ip_align(priv->ndev,
  559. priv->rx_packet_max);
  560. if (!skb)
  561. break;
  562. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  563. skb_tailroom(skb), GFP_KERNEL);
  564. if (WARN_ON(ret < 0))
  565. break;
  566. }
  567. /* continue even if we didn't manage to submit all receive descs */
  568. cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
  569. cpdma_ctlr_start(priv->dma);
  570. cpsw_intr_enable(priv);
  571. napi_enable(&priv->napi);
  572. cpdma_ctlr_eoi(priv->dma);
  573. return 0;
  574. }
  575. static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
  576. {
  577. if (!slave->phy)
  578. return;
  579. phy_stop(slave->phy);
  580. phy_disconnect(slave->phy);
  581. slave->phy = NULL;
  582. }
  583. static int cpsw_ndo_stop(struct net_device *ndev)
  584. {
  585. struct cpsw_priv *priv = netdev_priv(ndev);
  586. cpsw_info(priv, ifdown, "shutting down cpsw device\n");
  587. netif_stop_queue(priv->ndev);
  588. napi_disable(&priv->napi);
  589. netif_carrier_off(priv->ndev);
  590. cpsw_intr_disable(priv);
  591. cpdma_ctlr_int_ctrl(priv->dma, false);
  592. cpdma_ctlr_stop(priv->dma);
  593. cpsw_ale_stop(priv->ale);
  594. for_each_slave(priv, cpsw_slave_stop, priv);
  595. pm_runtime_put_sync(&priv->pdev->dev);
  596. return 0;
  597. }
  598. static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
  599. struct net_device *ndev)
  600. {
  601. struct cpsw_priv *priv = netdev_priv(ndev);
  602. int ret;
  603. ndev->trans_start = jiffies;
  604. if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
  605. cpsw_err(priv, tx_err, "packet pad failed\n");
  606. priv->stats.tx_dropped++;
  607. return NETDEV_TX_OK;
  608. }
  609. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && priv->cpts.tx_enable)
  610. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  611. skb_tx_timestamp(skb);
  612. ret = cpdma_chan_submit(priv->txch, skb, skb->data,
  613. skb->len, GFP_KERNEL);
  614. if (unlikely(ret != 0)) {
  615. cpsw_err(priv, tx_err, "desc submit failed\n");
  616. goto fail;
  617. }
  618. /* If there is no more tx desc left free then we need to
  619. * tell the kernel to stop sending us tx frames.
  620. */
  621. if (unlikely(cpdma_check_free_tx_desc(priv->txch)))
  622. netif_stop_queue(ndev);
  623. return NETDEV_TX_OK;
  624. fail:
  625. priv->stats.tx_dropped++;
  626. netif_stop_queue(ndev);
  627. return NETDEV_TX_BUSY;
  628. }
  629. static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
  630. {
  631. /*
  632. * The switch cannot operate in promiscuous mode without substantial
  633. * headache. For promiscuous mode to work, we would need to put the
  634. * ALE in bypass mode and route all traffic to the host port.
  635. * Subsequently, the host will need to operate as a "bridge", learn,
  636. * and flood as needed. For now, we simply complain here and
  637. * do nothing about it :-)
  638. */
  639. if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
  640. dev_err(&ndev->dev, "promiscuity ignored!\n");
  641. /*
  642. * The switch cannot filter multicast traffic unless it is configured
  643. * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a
  644. * whole bunch of additional logic that this driver does not implement
  645. * at present.
  646. */
  647. if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
  648. dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
  649. }
  650. #ifdef CONFIG_TI_CPTS
  651. static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
  652. {
  653. struct cpsw_slave *slave = &priv->slaves[priv->data.cpts_active_slave];
  654. u32 ts_en, seq_id;
  655. if (!priv->cpts.tx_enable && !priv->cpts.rx_enable) {
  656. slave_write(slave, 0, CPSW1_TS_CTL);
  657. return;
  658. }
  659. seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
  660. ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
  661. if (priv->cpts.tx_enable)
  662. ts_en |= CPSW_V1_TS_TX_EN;
  663. if (priv->cpts.rx_enable)
  664. ts_en |= CPSW_V1_TS_RX_EN;
  665. slave_write(slave, ts_en, CPSW1_TS_CTL);
  666. slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
  667. }
  668. static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
  669. {
  670. struct cpsw_slave *slave = &priv->slaves[priv->data.cpts_active_slave];
  671. u32 ctrl, mtype;
  672. ctrl = slave_read(slave, CPSW2_CONTROL);
  673. ctrl &= ~CTRL_ALL_TS_MASK;
  674. if (priv->cpts.tx_enable)
  675. ctrl |= CTRL_TX_TS_BITS;
  676. if (priv->cpts.rx_enable)
  677. ctrl |= CTRL_RX_TS_BITS;
  678. mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
  679. slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
  680. slave_write(slave, ctrl, CPSW2_CONTROL);
  681. __raw_writel(ETH_P_1588, &priv->regs->ts_ltype);
  682. }
  683. static int cpsw_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
  684. {
  685. struct cpsw_priv *priv = netdev_priv(dev);
  686. struct cpts *cpts = &priv->cpts;
  687. struct hwtstamp_config cfg;
  688. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  689. return -EFAULT;
  690. /* reserved for future extensions */
  691. if (cfg.flags)
  692. return -EINVAL;
  693. switch (cfg.tx_type) {
  694. case HWTSTAMP_TX_OFF:
  695. cpts->tx_enable = 0;
  696. break;
  697. case HWTSTAMP_TX_ON:
  698. cpts->tx_enable = 1;
  699. break;
  700. default:
  701. return -ERANGE;
  702. }
  703. switch (cfg.rx_filter) {
  704. case HWTSTAMP_FILTER_NONE:
  705. cpts->rx_enable = 0;
  706. break;
  707. case HWTSTAMP_FILTER_ALL:
  708. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  709. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  710. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  711. return -ERANGE;
  712. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  713. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  714. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  715. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  716. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  717. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  718. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  719. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  720. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  721. cpts->rx_enable = 1;
  722. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  723. break;
  724. default:
  725. return -ERANGE;
  726. }
  727. switch (priv->version) {
  728. case CPSW_VERSION_1:
  729. cpsw_hwtstamp_v1(priv);
  730. break;
  731. case CPSW_VERSION_2:
  732. cpsw_hwtstamp_v2(priv);
  733. break;
  734. default:
  735. return -ENOTSUPP;
  736. }
  737. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  738. }
  739. #endif /*CONFIG_TI_CPTS*/
  740. static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  741. {
  742. if (!netif_running(dev))
  743. return -EINVAL;
  744. #ifdef CONFIG_TI_CPTS
  745. if (cmd == SIOCSHWTSTAMP)
  746. return cpsw_hwtstamp_ioctl(dev, req);
  747. #endif
  748. return -ENOTSUPP;
  749. }
  750. static void cpsw_ndo_tx_timeout(struct net_device *ndev)
  751. {
  752. struct cpsw_priv *priv = netdev_priv(ndev);
  753. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  754. priv->stats.tx_errors++;
  755. cpsw_intr_disable(priv);
  756. cpdma_ctlr_int_ctrl(priv->dma, false);
  757. cpdma_chan_stop(priv->txch);
  758. cpdma_chan_start(priv->txch);
  759. cpdma_ctlr_int_ctrl(priv->dma, true);
  760. cpsw_intr_enable(priv);
  761. cpdma_ctlr_eoi(priv->dma);
  762. }
  763. static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
  764. {
  765. struct cpsw_priv *priv = netdev_priv(ndev);
  766. return &priv->stats;
  767. }
  768. #ifdef CONFIG_NET_POLL_CONTROLLER
  769. static void cpsw_ndo_poll_controller(struct net_device *ndev)
  770. {
  771. struct cpsw_priv *priv = netdev_priv(ndev);
  772. cpsw_intr_disable(priv);
  773. cpdma_ctlr_int_ctrl(priv->dma, false);
  774. cpsw_interrupt(ndev->irq, priv);
  775. cpdma_ctlr_int_ctrl(priv->dma, true);
  776. cpsw_intr_enable(priv);
  777. cpdma_ctlr_eoi(priv->dma);
  778. }
  779. #endif
  780. static const struct net_device_ops cpsw_netdev_ops = {
  781. .ndo_open = cpsw_ndo_open,
  782. .ndo_stop = cpsw_ndo_stop,
  783. .ndo_start_xmit = cpsw_ndo_start_xmit,
  784. .ndo_change_rx_flags = cpsw_ndo_change_rx_flags,
  785. .ndo_do_ioctl = cpsw_ndo_ioctl,
  786. .ndo_validate_addr = eth_validate_addr,
  787. .ndo_change_mtu = eth_change_mtu,
  788. .ndo_tx_timeout = cpsw_ndo_tx_timeout,
  789. .ndo_get_stats = cpsw_ndo_get_stats,
  790. .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
  791. #ifdef CONFIG_NET_POLL_CONTROLLER
  792. .ndo_poll_controller = cpsw_ndo_poll_controller,
  793. #endif
  794. };
  795. static void cpsw_get_drvinfo(struct net_device *ndev,
  796. struct ethtool_drvinfo *info)
  797. {
  798. struct cpsw_priv *priv = netdev_priv(ndev);
  799. strlcpy(info->driver, "TI CPSW Driver v1.0", sizeof(info->driver));
  800. strlcpy(info->version, "1.0", sizeof(info->version));
  801. strlcpy(info->bus_info, priv->pdev->name, sizeof(info->bus_info));
  802. }
  803. static u32 cpsw_get_msglevel(struct net_device *ndev)
  804. {
  805. struct cpsw_priv *priv = netdev_priv(ndev);
  806. return priv->msg_enable;
  807. }
  808. static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  809. {
  810. struct cpsw_priv *priv = netdev_priv(ndev);
  811. priv->msg_enable = value;
  812. }
  813. static int cpsw_get_ts_info(struct net_device *ndev,
  814. struct ethtool_ts_info *info)
  815. {
  816. #ifdef CONFIG_TI_CPTS
  817. struct cpsw_priv *priv = netdev_priv(ndev);
  818. info->so_timestamping =
  819. SOF_TIMESTAMPING_TX_HARDWARE |
  820. SOF_TIMESTAMPING_TX_SOFTWARE |
  821. SOF_TIMESTAMPING_RX_HARDWARE |
  822. SOF_TIMESTAMPING_RX_SOFTWARE |
  823. SOF_TIMESTAMPING_SOFTWARE |
  824. SOF_TIMESTAMPING_RAW_HARDWARE;
  825. info->phc_index = priv->cpts.phc_index;
  826. info->tx_types =
  827. (1 << HWTSTAMP_TX_OFF) |
  828. (1 << HWTSTAMP_TX_ON);
  829. info->rx_filters =
  830. (1 << HWTSTAMP_FILTER_NONE) |
  831. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  832. #else
  833. info->so_timestamping =
  834. SOF_TIMESTAMPING_TX_SOFTWARE |
  835. SOF_TIMESTAMPING_RX_SOFTWARE |
  836. SOF_TIMESTAMPING_SOFTWARE;
  837. info->phc_index = -1;
  838. info->tx_types = 0;
  839. info->rx_filters = 0;
  840. #endif
  841. return 0;
  842. }
  843. static const struct ethtool_ops cpsw_ethtool_ops = {
  844. .get_drvinfo = cpsw_get_drvinfo,
  845. .get_msglevel = cpsw_get_msglevel,
  846. .set_msglevel = cpsw_set_msglevel,
  847. .get_link = ethtool_op_get_link,
  848. .get_ts_info = cpsw_get_ts_info,
  849. };
  850. static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
  851. u32 slave_reg_ofs, u32 sliver_reg_ofs)
  852. {
  853. void __iomem *regs = priv->regs;
  854. int slave_num = slave->slave_num;
  855. struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
  856. slave->data = data;
  857. slave->regs = regs + slave_reg_ofs;
  858. slave->sliver = regs + sliver_reg_ofs;
  859. }
  860. static int cpsw_probe_dt(struct cpsw_platform_data *data,
  861. struct platform_device *pdev)
  862. {
  863. struct device_node *node = pdev->dev.of_node;
  864. struct device_node *slave_node;
  865. int i = 0, ret;
  866. u32 prop;
  867. if (!node)
  868. return -EINVAL;
  869. if (of_property_read_u32(node, "slaves", &prop)) {
  870. pr_err("Missing slaves property in the DT.\n");
  871. return -EINVAL;
  872. }
  873. data->slaves = prop;
  874. if (of_property_read_u32(node, "cpts_active_slave", &prop)) {
  875. pr_err("Missing cpts_active_slave property in the DT.\n");
  876. ret = -EINVAL;
  877. goto error_ret;
  878. }
  879. data->cpts_active_slave = prop;
  880. if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
  881. pr_err("Missing cpts_clock_mult property in the DT.\n");
  882. ret = -EINVAL;
  883. goto error_ret;
  884. }
  885. data->cpts_clock_mult = prop;
  886. if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
  887. pr_err("Missing cpts_clock_shift property in the DT.\n");
  888. ret = -EINVAL;
  889. goto error_ret;
  890. }
  891. data->cpts_clock_shift = prop;
  892. data->slave_data = kzalloc(sizeof(struct cpsw_slave_data) *
  893. data->slaves, GFP_KERNEL);
  894. if (!data->slave_data) {
  895. pr_err("Could not allocate slave memory.\n");
  896. return -EINVAL;
  897. }
  898. if (of_property_read_u32(node, "cpdma_channels", &prop)) {
  899. pr_err("Missing cpdma_channels property in the DT.\n");
  900. ret = -EINVAL;
  901. goto error_ret;
  902. }
  903. data->channels = prop;
  904. if (of_property_read_u32(node, "ale_entries", &prop)) {
  905. pr_err("Missing ale_entries property in the DT.\n");
  906. ret = -EINVAL;
  907. goto error_ret;
  908. }
  909. data->ale_entries = prop;
  910. if (of_property_read_u32(node, "bd_ram_size", &prop)) {
  911. pr_err("Missing bd_ram_size property in the DT.\n");
  912. ret = -EINVAL;
  913. goto error_ret;
  914. }
  915. data->bd_ram_size = prop;
  916. if (of_property_read_u32(node, "rx_descs", &prop)) {
  917. pr_err("Missing rx_descs property in the DT.\n");
  918. ret = -EINVAL;
  919. goto error_ret;
  920. }
  921. data->rx_descs = prop;
  922. if (of_property_read_u32(node, "mac_control", &prop)) {
  923. pr_err("Missing mac_control property in the DT.\n");
  924. ret = -EINVAL;
  925. goto error_ret;
  926. }
  927. data->mac_control = prop;
  928. /*
  929. * Populate all the child nodes here...
  930. */
  931. ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
  932. /* We do not want to force this, as in some cases may not have child */
  933. if (ret)
  934. pr_warn("Doesn't have any child node\n");
  935. for_each_node_by_name(slave_node, "slave") {
  936. struct cpsw_slave_data *slave_data = data->slave_data + i;
  937. const void *mac_addr = NULL;
  938. u32 phyid;
  939. int lenp;
  940. const __be32 *parp;
  941. struct device_node *mdio_node;
  942. struct platform_device *mdio;
  943. parp = of_get_property(slave_node, "phy_id", &lenp);
  944. if ((parp == NULL) && (lenp != (sizeof(void *) * 2))) {
  945. pr_err("Missing slave[%d] phy_id property\n", i);
  946. ret = -EINVAL;
  947. goto error_ret;
  948. }
  949. mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
  950. phyid = be32_to_cpup(parp+1);
  951. mdio = of_find_device_by_node(mdio_node);
  952. snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
  953. PHY_ID_FMT, mdio->name, phyid);
  954. mac_addr = of_get_mac_address(slave_node);
  955. if (mac_addr)
  956. memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
  957. i++;
  958. }
  959. return 0;
  960. error_ret:
  961. kfree(data->slave_data);
  962. return ret;
  963. }
  964. static int cpsw_probe(struct platform_device *pdev)
  965. {
  966. struct cpsw_platform_data *data = pdev->dev.platform_data;
  967. struct net_device *ndev;
  968. struct cpsw_priv *priv;
  969. struct cpdma_params dma_params;
  970. struct cpsw_ale_params ale_params;
  971. void __iomem *ss_regs, *wr_regs;
  972. struct resource *res;
  973. u32 slave_offset, sliver_offset, slave_size;
  974. int ret = 0, i, k = 0;
  975. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  976. if (!ndev) {
  977. pr_err("error allocating net_device\n");
  978. return -ENOMEM;
  979. }
  980. platform_set_drvdata(pdev, ndev);
  981. priv = netdev_priv(ndev);
  982. spin_lock_init(&priv->lock);
  983. priv->pdev = pdev;
  984. priv->ndev = ndev;
  985. priv->dev = &ndev->dev;
  986. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  987. priv->rx_packet_max = max(rx_packet_max, 128);
  988. /*
  989. * This may be required here for child devices.
  990. */
  991. pm_runtime_enable(&pdev->dev);
  992. if (cpsw_probe_dt(&priv->data, pdev)) {
  993. pr_err("cpsw: platform data missing\n");
  994. ret = -ENODEV;
  995. goto clean_ndev_ret;
  996. }
  997. data = &priv->data;
  998. if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
  999. memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
  1000. pr_info("Detected MACID = %pM", priv->mac_addr);
  1001. } else {
  1002. eth_random_addr(priv->mac_addr);
  1003. pr_info("Random MACID = %pM", priv->mac_addr);
  1004. }
  1005. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1006. priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
  1007. GFP_KERNEL);
  1008. if (!priv->slaves) {
  1009. ret = -EBUSY;
  1010. goto clean_ndev_ret;
  1011. }
  1012. for (i = 0; i < data->slaves; i++)
  1013. priv->slaves[i].slave_num = i;
  1014. priv->clk = clk_get(&pdev->dev, "fck");
  1015. if (IS_ERR(priv->clk)) {
  1016. dev_err(&pdev->dev, "fck is not found\n");
  1017. ret = -ENODEV;
  1018. goto clean_slave_ret;
  1019. }
  1020. priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1021. if (!priv->cpsw_res) {
  1022. dev_err(priv->dev, "error getting i/o resource\n");
  1023. ret = -ENOENT;
  1024. goto clean_clk_ret;
  1025. }
  1026. if (!request_mem_region(priv->cpsw_res->start,
  1027. resource_size(priv->cpsw_res), ndev->name)) {
  1028. dev_err(priv->dev, "failed request i/o region\n");
  1029. ret = -ENXIO;
  1030. goto clean_clk_ret;
  1031. }
  1032. ss_regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
  1033. if (!ss_regs) {
  1034. dev_err(priv->dev, "unable to map i/o region\n");
  1035. goto clean_cpsw_iores_ret;
  1036. }
  1037. priv->regs = ss_regs;
  1038. priv->version = __raw_readl(&priv->regs->id_ver);
  1039. priv->host_port = HOST_PORT_NUM;
  1040. priv->cpsw_wr_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1041. if (!priv->cpsw_wr_res) {
  1042. dev_err(priv->dev, "error getting i/o resource\n");
  1043. ret = -ENOENT;
  1044. goto clean_iomap_ret;
  1045. }
  1046. if (!request_mem_region(priv->cpsw_wr_res->start,
  1047. resource_size(priv->cpsw_wr_res), ndev->name)) {
  1048. dev_err(priv->dev, "failed request i/o region\n");
  1049. ret = -ENXIO;
  1050. goto clean_iomap_ret;
  1051. }
  1052. wr_regs = ioremap(priv->cpsw_wr_res->start,
  1053. resource_size(priv->cpsw_wr_res));
  1054. if (!wr_regs) {
  1055. dev_err(priv->dev, "unable to map i/o region\n");
  1056. goto clean_cpsw_wr_iores_ret;
  1057. }
  1058. priv->wr_regs = wr_regs;
  1059. memset(&dma_params, 0, sizeof(dma_params));
  1060. memset(&ale_params, 0, sizeof(ale_params));
  1061. switch (priv->version) {
  1062. case CPSW_VERSION_1:
  1063. priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  1064. priv->cpts.reg = ss_regs + CPSW1_CPTS_OFFSET;
  1065. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  1066. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  1067. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  1068. slave_offset = CPSW1_SLAVE_OFFSET;
  1069. slave_size = CPSW1_SLAVE_SIZE;
  1070. sliver_offset = CPSW1_SLIVER_OFFSET;
  1071. dma_params.desc_mem_phys = 0;
  1072. break;
  1073. case CPSW_VERSION_2:
  1074. priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  1075. priv->cpts.reg = ss_regs + CPSW2_CPTS_OFFSET;
  1076. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  1077. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  1078. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  1079. slave_offset = CPSW2_SLAVE_OFFSET;
  1080. slave_size = CPSW2_SLAVE_SIZE;
  1081. sliver_offset = CPSW2_SLIVER_OFFSET;
  1082. dma_params.desc_mem_phys =
  1083. (u32 __force) priv->cpsw_res->start + CPSW2_BD_OFFSET;
  1084. break;
  1085. default:
  1086. dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
  1087. ret = -ENODEV;
  1088. goto clean_cpsw_wr_iores_ret;
  1089. }
  1090. for (i = 0; i < priv->data.slaves; i++) {
  1091. struct cpsw_slave *slave = &priv->slaves[i];
  1092. cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
  1093. slave_offset += slave_size;
  1094. sliver_offset += SLIVER_SIZE;
  1095. }
  1096. dma_params.dev = &pdev->dev;
  1097. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  1098. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  1099. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  1100. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  1101. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  1102. dma_params.num_chan = data->channels;
  1103. dma_params.has_soft_reset = true;
  1104. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  1105. dma_params.desc_mem_size = data->bd_ram_size;
  1106. dma_params.desc_align = 16;
  1107. dma_params.has_ext_regs = true;
  1108. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  1109. priv->dma = cpdma_ctlr_create(&dma_params);
  1110. if (!priv->dma) {
  1111. dev_err(priv->dev, "error initializing dma\n");
  1112. ret = -ENOMEM;
  1113. goto clean_wr_iomap_ret;
  1114. }
  1115. priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
  1116. cpsw_tx_handler);
  1117. priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
  1118. cpsw_rx_handler);
  1119. if (WARN_ON(!priv->txch || !priv->rxch)) {
  1120. dev_err(priv->dev, "error initializing dma channels\n");
  1121. ret = -ENOMEM;
  1122. goto clean_dma_ret;
  1123. }
  1124. ale_params.dev = &ndev->dev;
  1125. ale_params.ale_ageout = ale_ageout;
  1126. ale_params.ale_entries = data->ale_entries;
  1127. ale_params.ale_ports = data->slaves;
  1128. priv->ale = cpsw_ale_create(&ale_params);
  1129. if (!priv->ale) {
  1130. dev_err(priv->dev, "error initializing ale engine\n");
  1131. ret = -ENODEV;
  1132. goto clean_dma_ret;
  1133. }
  1134. ndev->irq = platform_get_irq(pdev, 0);
  1135. if (ndev->irq < 0) {
  1136. dev_err(priv->dev, "error getting irq resource\n");
  1137. ret = -ENOENT;
  1138. goto clean_ale_ret;
  1139. }
  1140. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
  1141. for (i = res->start; i <= res->end; i++) {
  1142. if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
  1143. dev_name(&pdev->dev), priv)) {
  1144. dev_err(priv->dev, "error attaching irq\n");
  1145. goto clean_ale_ret;
  1146. }
  1147. priv->irqs_table[k] = i;
  1148. priv->num_irqs = k;
  1149. }
  1150. k++;
  1151. }
  1152. ndev->flags |= IFF_ALLMULTI; /* see cpsw_ndo_change_rx_flags() */
  1153. ndev->netdev_ops = &cpsw_netdev_ops;
  1154. SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
  1155. netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
  1156. /* register the network device */
  1157. SET_NETDEV_DEV(ndev, &pdev->dev);
  1158. ret = register_netdev(ndev);
  1159. if (ret) {
  1160. dev_err(priv->dev, "error registering net device\n");
  1161. ret = -ENODEV;
  1162. goto clean_irq_ret;
  1163. }
  1164. if (cpts_register(&pdev->dev, &priv->cpts,
  1165. data->cpts_clock_mult, data->cpts_clock_shift))
  1166. dev_err(priv->dev, "error registering cpts device\n");
  1167. cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n",
  1168. priv->cpsw_res->start, ndev->irq);
  1169. return 0;
  1170. clean_irq_ret:
  1171. free_irq(ndev->irq, priv);
  1172. clean_ale_ret:
  1173. cpsw_ale_destroy(priv->ale);
  1174. clean_dma_ret:
  1175. cpdma_chan_destroy(priv->txch);
  1176. cpdma_chan_destroy(priv->rxch);
  1177. cpdma_ctlr_destroy(priv->dma);
  1178. clean_wr_iomap_ret:
  1179. iounmap(priv->wr_regs);
  1180. clean_cpsw_wr_iores_ret:
  1181. release_mem_region(priv->cpsw_wr_res->start,
  1182. resource_size(priv->cpsw_wr_res));
  1183. clean_iomap_ret:
  1184. iounmap(priv->regs);
  1185. clean_cpsw_iores_ret:
  1186. release_mem_region(priv->cpsw_res->start,
  1187. resource_size(priv->cpsw_res));
  1188. clean_clk_ret:
  1189. clk_put(priv->clk);
  1190. clean_slave_ret:
  1191. pm_runtime_disable(&pdev->dev);
  1192. kfree(priv->slaves);
  1193. clean_ndev_ret:
  1194. free_netdev(ndev);
  1195. return ret;
  1196. }
  1197. static int cpsw_remove(struct platform_device *pdev)
  1198. {
  1199. struct net_device *ndev = platform_get_drvdata(pdev);
  1200. struct cpsw_priv *priv = netdev_priv(ndev);
  1201. pr_info("removing device");
  1202. platform_set_drvdata(pdev, NULL);
  1203. cpts_unregister(&priv->cpts);
  1204. free_irq(ndev->irq, priv);
  1205. cpsw_ale_destroy(priv->ale);
  1206. cpdma_chan_destroy(priv->txch);
  1207. cpdma_chan_destroy(priv->rxch);
  1208. cpdma_ctlr_destroy(priv->dma);
  1209. iounmap(priv->regs);
  1210. release_mem_region(priv->cpsw_res->start,
  1211. resource_size(priv->cpsw_res));
  1212. iounmap(priv->wr_regs);
  1213. release_mem_region(priv->cpsw_wr_res->start,
  1214. resource_size(priv->cpsw_wr_res));
  1215. pm_runtime_disable(&pdev->dev);
  1216. clk_put(priv->clk);
  1217. kfree(priv->slaves);
  1218. free_netdev(ndev);
  1219. return 0;
  1220. }
  1221. static int cpsw_suspend(struct device *dev)
  1222. {
  1223. struct platform_device *pdev = to_platform_device(dev);
  1224. struct net_device *ndev = platform_get_drvdata(pdev);
  1225. if (netif_running(ndev))
  1226. cpsw_ndo_stop(ndev);
  1227. pm_runtime_put_sync(&pdev->dev);
  1228. return 0;
  1229. }
  1230. static int cpsw_resume(struct device *dev)
  1231. {
  1232. struct platform_device *pdev = to_platform_device(dev);
  1233. struct net_device *ndev = platform_get_drvdata(pdev);
  1234. pm_runtime_get_sync(&pdev->dev);
  1235. if (netif_running(ndev))
  1236. cpsw_ndo_open(ndev);
  1237. return 0;
  1238. }
  1239. static const struct dev_pm_ops cpsw_pm_ops = {
  1240. .suspend = cpsw_suspend,
  1241. .resume = cpsw_resume,
  1242. };
  1243. static const struct of_device_id cpsw_of_mtable[] = {
  1244. { .compatible = "ti,cpsw", },
  1245. { /* sentinel */ },
  1246. };
  1247. static struct platform_driver cpsw_driver = {
  1248. .driver = {
  1249. .name = "cpsw",
  1250. .owner = THIS_MODULE,
  1251. .pm = &cpsw_pm_ops,
  1252. .of_match_table = of_match_ptr(cpsw_of_mtable),
  1253. },
  1254. .probe = cpsw_probe,
  1255. .remove = cpsw_remove,
  1256. };
  1257. static int __init cpsw_init(void)
  1258. {
  1259. return platform_driver_register(&cpsw_driver);
  1260. }
  1261. late_initcall(cpsw_init);
  1262. static void __exit cpsw_exit(void)
  1263. {
  1264. platform_driver_unregister(&cpsw_driver);
  1265. }
  1266. module_exit(cpsw_exit);
  1267. MODULE_LICENSE("GPL");
  1268. MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
  1269. MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
  1270. MODULE_DESCRIPTION("TI CPSW Ethernet driver");