cpsw.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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. if (unlikely(netif_queue_stopped(ndev)))
  326. netif_start_queue(ndev);
  327. cpts_tx_timestamp(&priv->cpts, skb);
  328. priv->stats.tx_packets++;
  329. priv->stats.tx_bytes += len;
  330. dev_kfree_skb_any(skb);
  331. }
  332. void cpsw_rx_handler(void *token, int len, int status)
  333. {
  334. struct sk_buff *skb = token;
  335. struct net_device *ndev = skb->dev;
  336. struct cpsw_priv *priv = netdev_priv(ndev);
  337. int ret = 0;
  338. /* free and bail if we are shutting down */
  339. if (unlikely(!netif_running(ndev)) ||
  340. unlikely(!netif_carrier_ok(ndev))) {
  341. dev_kfree_skb_any(skb);
  342. return;
  343. }
  344. if (likely(status >= 0)) {
  345. skb_put(skb, len);
  346. cpts_rx_timestamp(&priv->cpts, skb);
  347. skb->protocol = eth_type_trans(skb, ndev);
  348. netif_receive_skb(skb);
  349. priv->stats.rx_bytes += len;
  350. priv->stats.rx_packets++;
  351. skb = NULL;
  352. }
  353. if (unlikely(!netif_running(ndev))) {
  354. if (skb)
  355. dev_kfree_skb_any(skb);
  356. return;
  357. }
  358. if (likely(!skb)) {
  359. skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
  360. if (WARN_ON(!skb))
  361. return;
  362. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  363. skb_tailroom(skb), GFP_KERNEL);
  364. }
  365. WARN_ON(ret < 0);
  366. }
  367. static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
  368. {
  369. struct cpsw_priv *priv = dev_id;
  370. if (likely(netif_running(priv->ndev))) {
  371. cpsw_intr_disable(priv);
  372. cpsw_disable_irq(priv);
  373. napi_schedule(&priv->napi);
  374. }
  375. return IRQ_HANDLED;
  376. }
  377. static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
  378. {
  379. if (priv->host_port == 0)
  380. return slave_num + 1;
  381. else
  382. return slave_num;
  383. }
  384. static int cpsw_poll(struct napi_struct *napi, int budget)
  385. {
  386. struct cpsw_priv *priv = napi_to_priv(napi);
  387. int num_tx, num_rx;
  388. num_tx = cpdma_chan_process(priv->txch, 128);
  389. num_rx = cpdma_chan_process(priv->rxch, budget);
  390. if (num_rx || num_tx)
  391. cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
  392. num_rx, num_tx);
  393. if (num_rx < budget) {
  394. napi_complete(napi);
  395. cpsw_intr_enable(priv);
  396. cpdma_ctlr_eoi(priv->dma);
  397. cpsw_enable_irq(priv);
  398. }
  399. return num_rx;
  400. }
  401. static inline void soft_reset(const char *module, void __iomem *reg)
  402. {
  403. unsigned long timeout = jiffies + HZ;
  404. __raw_writel(1, reg);
  405. do {
  406. cpu_relax();
  407. } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
  408. WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
  409. }
  410. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  411. ((mac)[2] << 16) | ((mac)[3] << 24))
  412. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  413. static void cpsw_set_slave_mac(struct cpsw_slave *slave,
  414. struct cpsw_priv *priv)
  415. {
  416. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  417. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  418. }
  419. static void _cpsw_adjust_link(struct cpsw_slave *slave,
  420. struct cpsw_priv *priv, bool *link)
  421. {
  422. struct phy_device *phy = slave->phy;
  423. u32 mac_control = 0;
  424. u32 slave_port;
  425. if (!phy)
  426. return;
  427. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  428. if (phy->link) {
  429. mac_control = priv->data.mac_control;
  430. /* enable forwarding */
  431. cpsw_ale_control_set(priv->ale, slave_port,
  432. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  433. if (phy->speed == 1000)
  434. mac_control |= BIT(7); /* GIGABITEN */
  435. if (phy->duplex)
  436. mac_control |= BIT(0); /* FULLDUPLEXEN */
  437. /* set speed_in input in case RMII mode is used in 100Mbps */
  438. if (phy->speed == 100)
  439. mac_control |= BIT(15);
  440. *link = true;
  441. } else {
  442. mac_control = 0;
  443. /* disable forwarding */
  444. cpsw_ale_control_set(priv->ale, slave_port,
  445. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  446. }
  447. if (mac_control != slave->mac_control) {
  448. phy_print_status(phy);
  449. __raw_writel(mac_control, &slave->sliver->mac_control);
  450. }
  451. slave->mac_control = mac_control;
  452. }
  453. static void cpsw_adjust_link(struct net_device *ndev)
  454. {
  455. struct cpsw_priv *priv = netdev_priv(ndev);
  456. bool link = false;
  457. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  458. if (link) {
  459. netif_carrier_on(ndev);
  460. if (netif_running(ndev))
  461. netif_wake_queue(ndev);
  462. } else {
  463. netif_carrier_off(ndev);
  464. netif_stop_queue(ndev);
  465. }
  466. }
  467. static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val)
  468. {
  469. static char *leader = "........................................";
  470. if (!val)
  471. return 0;
  472. else
  473. return snprintf(buf, maxlen, "%s %s %10d\n", name,
  474. leader + strlen(name), val);
  475. }
  476. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  477. {
  478. char name[32];
  479. u32 slave_port;
  480. sprintf(name, "slave-%d", slave->slave_num);
  481. soft_reset(name, &slave->sliver->soft_reset);
  482. /* setup priority mapping */
  483. __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
  484. switch (priv->version) {
  485. case CPSW_VERSION_1:
  486. slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
  487. break;
  488. case CPSW_VERSION_2:
  489. slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
  490. break;
  491. }
  492. /* setup max packet size, and mac address */
  493. __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
  494. cpsw_set_slave_mac(slave, priv);
  495. slave->mac_control = 0; /* no link yet */
  496. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  497. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  498. 1 << slave_port, 0, ALE_MCAST_FWD_2);
  499. slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
  500. &cpsw_adjust_link, 0, slave->data->phy_if);
  501. if (IS_ERR(slave->phy)) {
  502. dev_err(priv->dev, "phy %s not found on slave %d\n",
  503. slave->data->phy_id, slave->slave_num);
  504. slave->phy = NULL;
  505. } else {
  506. dev_info(priv->dev, "phy found : id is : 0x%x\n",
  507. slave->phy->phy_id);
  508. phy_start(slave->phy);
  509. }
  510. }
  511. static void cpsw_init_host_port(struct cpsw_priv *priv)
  512. {
  513. /* soft reset the controller and initialize ale */
  514. soft_reset("cpsw", &priv->regs->soft_reset);
  515. cpsw_ale_start(priv->ale);
  516. /* switch to vlan unaware mode */
  517. cpsw_ale_control_set(priv->ale, 0, ALE_VLAN_AWARE, 0);
  518. /* setup host port priority mapping */
  519. __raw_writel(CPDMA_TX_PRIORITY_MAP,
  520. &priv->host_port_regs->cpdma_tx_pri_map);
  521. __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
  522. cpsw_ale_control_set(priv->ale, priv->host_port,
  523. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  524. cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port, 0);
  525. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  526. 1 << priv->host_port, 0, ALE_MCAST_FWD_2);
  527. }
  528. static int cpsw_ndo_open(struct net_device *ndev)
  529. {
  530. struct cpsw_priv *priv = netdev_priv(ndev);
  531. int i, ret;
  532. u32 reg;
  533. cpsw_intr_disable(priv);
  534. netif_carrier_off(ndev);
  535. pm_runtime_get_sync(&priv->pdev->dev);
  536. reg = priv->version;
  537. dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
  538. CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
  539. CPSW_RTL_VERSION(reg));
  540. /* initialize host and slave ports */
  541. cpsw_init_host_port(priv);
  542. for_each_slave(priv, cpsw_slave_open, priv);
  543. /* setup tx dma to fixed prio and zero offset */
  544. cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
  545. cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
  546. /* disable priority elevation and enable statistics on all ports */
  547. __raw_writel(0, &priv->regs->ptype);
  548. /* enable statistics collection only on the host port */
  549. __raw_writel(0x7, &priv->regs->stat_port_en);
  550. if (WARN_ON(!priv->data.rx_descs))
  551. priv->data.rx_descs = 128;
  552. for (i = 0; i < priv->data.rx_descs; i++) {
  553. struct sk_buff *skb;
  554. ret = -ENOMEM;
  555. skb = netdev_alloc_skb_ip_align(priv->ndev,
  556. priv->rx_packet_max);
  557. if (!skb)
  558. break;
  559. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  560. skb_tailroom(skb), GFP_KERNEL);
  561. if (WARN_ON(ret < 0))
  562. break;
  563. }
  564. /* continue even if we didn't manage to submit all receive descs */
  565. cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
  566. cpdma_ctlr_start(priv->dma);
  567. cpsw_intr_enable(priv);
  568. napi_enable(&priv->napi);
  569. cpdma_ctlr_eoi(priv->dma);
  570. return 0;
  571. }
  572. static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
  573. {
  574. if (!slave->phy)
  575. return;
  576. phy_stop(slave->phy);
  577. phy_disconnect(slave->phy);
  578. slave->phy = NULL;
  579. }
  580. static int cpsw_ndo_stop(struct net_device *ndev)
  581. {
  582. struct cpsw_priv *priv = netdev_priv(ndev);
  583. cpsw_info(priv, ifdown, "shutting down cpsw device\n");
  584. netif_stop_queue(priv->ndev);
  585. napi_disable(&priv->napi);
  586. netif_carrier_off(priv->ndev);
  587. cpsw_intr_disable(priv);
  588. cpdma_ctlr_int_ctrl(priv->dma, false);
  589. cpdma_ctlr_stop(priv->dma);
  590. cpsw_ale_stop(priv->ale);
  591. for_each_slave(priv, cpsw_slave_stop, priv);
  592. pm_runtime_put_sync(&priv->pdev->dev);
  593. return 0;
  594. }
  595. static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
  596. struct net_device *ndev)
  597. {
  598. struct cpsw_priv *priv = netdev_priv(ndev);
  599. int ret;
  600. ndev->trans_start = jiffies;
  601. if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
  602. cpsw_err(priv, tx_err, "packet pad failed\n");
  603. priv->stats.tx_dropped++;
  604. return NETDEV_TX_OK;
  605. }
  606. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && priv->cpts.tx_enable)
  607. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  608. skb_tx_timestamp(skb);
  609. ret = cpdma_chan_submit(priv->txch, skb, skb->data,
  610. skb->len, GFP_KERNEL);
  611. if (unlikely(ret != 0)) {
  612. cpsw_err(priv, tx_err, "desc submit failed\n");
  613. goto fail;
  614. }
  615. return NETDEV_TX_OK;
  616. fail:
  617. priv->stats.tx_dropped++;
  618. netif_stop_queue(ndev);
  619. return NETDEV_TX_BUSY;
  620. }
  621. static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
  622. {
  623. /*
  624. * The switch cannot operate in promiscuous mode without substantial
  625. * headache. For promiscuous mode to work, we would need to put the
  626. * ALE in bypass mode and route all traffic to the host port.
  627. * Subsequently, the host will need to operate as a "bridge", learn,
  628. * and flood as needed. For now, we simply complain here and
  629. * do nothing about it :-)
  630. */
  631. if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
  632. dev_err(&ndev->dev, "promiscuity ignored!\n");
  633. /*
  634. * The switch cannot filter multicast traffic unless it is configured
  635. * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a
  636. * whole bunch of additional logic that this driver does not implement
  637. * at present.
  638. */
  639. if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
  640. dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
  641. }
  642. #ifdef CONFIG_TI_CPTS
  643. static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
  644. {
  645. struct cpsw_slave *slave = &priv->slaves[priv->data.cpts_active_slave];
  646. u32 ts_en, seq_id;
  647. if (!priv->cpts.tx_enable && !priv->cpts.rx_enable) {
  648. slave_write(slave, 0, CPSW1_TS_CTL);
  649. return;
  650. }
  651. seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
  652. ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
  653. if (priv->cpts.tx_enable)
  654. ts_en |= CPSW_V1_TS_TX_EN;
  655. if (priv->cpts.rx_enable)
  656. ts_en |= CPSW_V1_TS_RX_EN;
  657. slave_write(slave, ts_en, CPSW1_TS_CTL);
  658. slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
  659. }
  660. static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
  661. {
  662. struct cpsw_slave *slave = &priv->slaves[priv->data.cpts_active_slave];
  663. u32 ctrl, mtype;
  664. ctrl = slave_read(slave, CPSW2_CONTROL);
  665. ctrl &= ~CTRL_ALL_TS_MASK;
  666. if (priv->cpts.tx_enable)
  667. ctrl |= CTRL_TX_TS_BITS;
  668. if (priv->cpts.rx_enable)
  669. ctrl |= CTRL_RX_TS_BITS;
  670. mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
  671. slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
  672. slave_write(slave, ctrl, CPSW2_CONTROL);
  673. __raw_writel(ETH_P_1588, &priv->regs->ts_ltype);
  674. }
  675. static int cpsw_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
  676. {
  677. struct cpsw_priv *priv = netdev_priv(dev);
  678. struct cpts *cpts = &priv->cpts;
  679. struct hwtstamp_config cfg;
  680. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  681. return -EFAULT;
  682. /* reserved for future extensions */
  683. if (cfg.flags)
  684. return -EINVAL;
  685. switch (cfg.tx_type) {
  686. case HWTSTAMP_TX_OFF:
  687. cpts->tx_enable = 0;
  688. break;
  689. case HWTSTAMP_TX_ON:
  690. cpts->tx_enable = 1;
  691. break;
  692. default:
  693. return -ERANGE;
  694. }
  695. switch (cfg.rx_filter) {
  696. case HWTSTAMP_FILTER_NONE:
  697. cpts->rx_enable = 0;
  698. break;
  699. case HWTSTAMP_FILTER_ALL:
  700. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  701. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  702. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  703. return -ERANGE;
  704. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  705. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  706. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  707. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  708. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  709. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  710. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  711. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  712. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  713. cpts->rx_enable = 1;
  714. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  715. break;
  716. default:
  717. return -ERANGE;
  718. }
  719. switch (priv->version) {
  720. case CPSW_VERSION_1:
  721. cpsw_hwtstamp_v1(priv);
  722. break;
  723. case CPSW_VERSION_2:
  724. cpsw_hwtstamp_v2(priv);
  725. break;
  726. default:
  727. return -ENOTSUPP;
  728. }
  729. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  730. }
  731. #endif /*CONFIG_TI_CPTS*/
  732. static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  733. {
  734. if (!netif_running(dev))
  735. return -EINVAL;
  736. #ifdef CONFIG_TI_CPTS
  737. if (cmd == SIOCSHWTSTAMP)
  738. return cpsw_hwtstamp_ioctl(dev, req);
  739. #endif
  740. return -ENOTSUPP;
  741. }
  742. static void cpsw_ndo_tx_timeout(struct net_device *ndev)
  743. {
  744. struct cpsw_priv *priv = netdev_priv(ndev);
  745. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  746. priv->stats.tx_errors++;
  747. cpsw_intr_disable(priv);
  748. cpdma_ctlr_int_ctrl(priv->dma, false);
  749. cpdma_chan_stop(priv->txch);
  750. cpdma_chan_start(priv->txch);
  751. cpdma_ctlr_int_ctrl(priv->dma, true);
  752. cpsw_intr_enable(priv);
  753. cpdma_ctlr_eoi(priv->dma);
  754. }
  755. static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
  756. {
  757. struct cpsw_priv *priv = netdev_priv(ndev);
  758. return &priv->stats;
  759. }
  760. #ifdef CONFIG_NET_POLL_CONTROLLER
  761. static void cpsw_ndo_poll_controller(struct net_device *ndev)
  762. {
  763. struct cpsw_priv *priv = netdev_priv(ndev);
  764. cpsw_intr_disable(priv);
  765. cpdma_ctlr_int_ctrl(priv->dma, false);
  766. cpsw_interrupt(ndev->irq, priv);
  767. cpdma_ctlr_int_ctrl(priv->dma, true);
  768. cpsw_intr_enable(priv);
  769. cpdma_ctlr_eoi(priv->dma);
  770. }
  771. #endif
  772. static const struct net_device_ops cpsw_netdev_ops = {
  773. .ndo_open = cpsw_ndo_open,
  774. .ndo_stop = cpsw_ndo_stop,
  775. .ndo_start_xmit = cpsw_ndo_start_xmit,
  776. .ndo_change_rx_flags = cpsw_ndo_change_rx_flags,
  777. .ndo_do_ioctl = cpsw_ndo_ioctl,
  778. .ndo_validate_addr = eth_validate_addr,
  779. .ndo_change_mtu = eth_change_mtu,
  780. .ndo_tx_timeout = cpsw_ndo_tx_timeout,
  781. .ndo_get_stats = cpsw_ndo_get_stats,
  782. .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
  783. #ifdef CONFIG_NET_POLL_CONTROLLER
  784. .ndo_poll_controller = cpsw_ndo_poll_controller,
  785. #endif
  786. };
  787. static void cpsw_get_drvinfo(struct net_device *ndev,
  788. struct ethtool_drvinfo *info)
  789. {
  790. struct cpsw_priv *priv = netdev_priv(ndev);
  791. strcpy(info->driver, "TI CPSW Driver v1.0");
  792. strcpy(info->version, "1.0");
  793. strcpy(info->bus_info, priv->pdev->name);
  794. }
  795. static u32 cpsw_get_msglevel(struct net_device *ndev)
  796. {
  797. struct cpsw_priv *priv = netdev_priv(ndev);
  798. return priv->msg_enable;
  799. }
  800. static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  801. {
  802. struct cpsw_priv *priv = netdev_priv(ndev);
  803. priv->msg_enable = value;
  804. }
  805. static int cpsw_get_ts_info(struct net_device *ndev,
  806. struct ethtool_ts_info *info)
  807. {
  808. #ifdef CONFIG_TI_CPTS
  809. struct cpsw_priv *priv = netdev_priv(ndev);
  810. info->so_timestamping =
  811. SOF_TIMESTAMPING_TX_HARDWARE |
  812. SOF_TIMESTAMPING_TX_SOFTWARE |
  813. SOF_TIMESTAMPING_RX_HARDWARE |
  814. SOF_TIMESTAMPING_RX_SOFTWARE |
  815. SOF_TIMESTAMPING_SOFTWARE |
  816. SOF_TIMESTAMPING_RAW_HARDWARE;
  817. info->phc_index = priv->cpts.phc_index;
  818. info->tx_types =
  819. (1 << HWTSTAMP_TX_OFF) |
  820. (1 << HWTSTAMP_TX_ON);
  821. info->rx_filters =
  822. (1 << HWTSTAMP_FILTER_NONE) |
  823. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  824. #else
  825. info->so_timestamping =
  826. SOF_TIMESTAMPING_TX_SOFTWARE |
  827. SOF_TIMESTAMPING_RX_SOFTWARE |
  828. SOF_TIMESTAMPING_SOFTWARE;
  829. info->phc_index = -1;
  830. info->tx_types = 0;
  831. info->rx_filters = 0;
  832. #endif
  833. return 0;
  834. }
  835. static const struct ethtool_ops cpsw_ethtool_ops = {
  836. .get_drvinfo = cpsw_get_drvinfo,
  837. .get_msglevel = cpsw_get_msglevel,
  838. .set_msglevel = cpsw_set_msglevel,
  839. .get_link = ethtool_op_get_link,
  840. .get_ts_info = cpsw_get_ts_info,
  841. };
  842. static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
  843. u32 slave_reg_ofs, u32 sliver_reg_ofs)
  844. {
  845. void __iomem *regs = priv->regs;
  846. int slave_num = slave->slave_num;
  847. struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
  848. slave->data = data;
  849. slave->regs = regs + slave_reg_ofs;
  850. slave->sliver = regs + sliver_reg_ofs;
  851. }
  852. static int cpsw_probe_dt(struct cpsw_platform_data *data,
  853. struct platform_device *pdev)
  854. {
  855. struct device_node *node = pdev->dev.of_node;
  856. struct device_node *slave_node;
  857. int i = 0, ret;
  858. u32 prop;
  859. if (!node)
  860. return -EINVAL;
  861. if (of_property_read_u32(node, "slaves", &prop)) {
  862. pr_err("Missing slaves property in the DT.\n");
  863. return -EINVAL;
  864. }
  865. data->slaves = prop;
  866. if (of_property_read_u32(node, "cpts_active_slave", &prop)) {
  867. pr_err("Missing cpts_active_slave property in the DT.\n");
  868. ret = -EINVAL;
  869. goto error_ret;
  870. }
  871. data->cpts_active_slave = prop;
  872. if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
  873. pr_err("Missing cpts_clock_mult property in the DT.\n");
  874. ret = -EINVAL;
  875. goto error_ret;
  876. }
  877. data->cpts_clock_mult = prop;
  878. if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
  879. pr_err("Missing cpts_clock_shift property in the DT.\n");
  880. ret = -EINVAL;
  881. goto error_ret;
  882. }
  883. data->cpts_clock_shift = prop;
  884. data->slave_data = kzalloc(sizeof(struct cpsw_slave_data) *
  885. data->slaves, GFP_KERNEL);
  886. if (!data->slave_data) {
  887. pr_err("Could not allocate slave memory.\n");
  888. return -EINVAL;
  889. }
  890. if (of_property_read_u32(node, "cpdma_channels", &prop)) {
  891. pr_err("Missing cpdma_channels property in the DT.\n");
  892. ret = -EINVAL;
  893. goto error_ret;
  894. }
  895. data->channels = prop;
  896. if (of_property_read_u32(node, "ale_entries", &prop)) {
  897. pr_err("Missing ale_entries property in the DT.\n");
  898. ret = -EINVAL;
  899. goto error_ret;
  900. }
  901. data->ale_entries = prop;
  902. if (of_property_read_u32(node, "bd_ram_size", &prop)) {
  903. pr_err("Missing bd_ram_size property in the DT.\n");
  904. ret = -EINVAL;
  905. goto error_ret;
  906. }
  907. data->bd_ram_size = prop;
  908. if (of_property_read_u32(node, "rx_descs", &prop)) {
  909. pr_err("Missing rx_descs property in the DT.\n");
  910. ret = -EINVAL;
  911. goto error_ret;
  912. }
  913. data->rx_descs = prop;
  914. if (of_property_read_u32(node, "mac_control", &prop)) {
  915. pr_err("Missing mac_control property in the DT.\n");
  916. ret = -EINVAL;
  917. goto error_ret;
  918. }
  919. data->mac_control = prop;
  920. /*
  921. * Populate all the child nodes here...
  922. */
  923. ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
  924. /* We do not want to force this, as in some cases may not have child */
  925. if (ret)
  926. pr_warn("Doesn't have any child node\n");
  927. for_each_node_by_name(slave_node, "slave") {
  928. struct cpsw_slave_data *slave_data = data->slave_data + i;
  929. const void *mac_addr = NULL;
  930. u32 phyid;
  931. int lenp;
  932. const __be32 *parp;
  933. struct device_node *mdio_node;
  934. struct platform_device *mdio;
  935. parp = of_get_property(slave_node, "phy_id", &lenp);
  936. if ((parp == NULL) && (lenp != (sizeof(void *) * 2))) {
  937. pr_err("Missing slave[%d] phy_id property\n", i);
  938. ret = -EINVAL;
  939. goto error_ret;
  940. }
  941. mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
  942. phyid = be32_to_cpup(parp+1);
  943. mdio = of_find_device_by_node(mdio_node);
  944. snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
  945. PHY_ID_FMT, mdio->name, phyid);
  946. mac_addr = of_get_mac_address(slave_node);
  947. if (mac_addr)
  948. memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
  949. i++;
  950. }
  951. return 0;
  952. error_ret:
  953. kfree(data->slave_data);
  954. return ret;
  955. }
  956. static int cpsw_probe(struct platform_device *pdev)
  957. {
  958. struct cpsw_platform_data *data = pdev->dev.platform_data;
  959. struct net_device *ndev;
  960. struct cpsw_priv *priv;
  961. struct cpdma_params dma_params;
  962. struct cpsw_ale_params ale_params;
  963. void __iomem *ss_regs, *wr_regs;
  964. struct resource *res;
  965. u32 slave_offset, sliver_offset, slave_size;
  966. int ret = 0, i, k = 0;
  967. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  968. if (!ndev) {
  969. pr_err("error allocating net_device\n");
  970. return -ENOMEM;
  971. }
  972. platform_set_drvdata(pdev, ndev);
  973. priv = netdev_priv(ndev);
  974. spin_lock_init(&priv->lock);
  975. priv->pdev = pdev;
  976. priv->ndev = ndev;
  977. priv->dev = &ndev->dev;
  978. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  979. priv->rx_packet_max = max(rx_packet_max, 128);
  980. /*
  981. * This may be required here for child devices.
  982. */
  983. pm_runtime_enable(&pdev->dev);
  984. if (cpsw_probe_dt(&priv->data, pdev)) {
  985. pr_err("cpsw: platform data missing\n");
  986. ret = -ENODEV;
  987. goto clean_ndev_ret;
  988. }
  989. data = &priv->data;
  990. if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
  991. memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
  992. pr_info("Detected MACID = %pM", priv->mac_addr);
  993. } else {
  994. eth_random_addr(priv->mac_addr);
  995. pr_info("Random MACID = %pM", priv->mac_addr);
  996. }
  997. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  998. priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
  999. GFP_KERNEL);
  1000. if (!priv->slaves) {
  1001. ret = -EBUSY;
  1002. goto clean_ndev_ret;
  1003. }
  1004. for (i = 0; i < data->slaves; i++)
  1005. priv->slaves[i].slave_num = i;
  1006. priv->clk = clk_get(&pdev->dev, "fck");
  1007. if (IS_ERR(priv->clk)) {
  1008. dev_err(&pdev->dev, "fck is not found\n");
  1009. ret = -ENODEV;
  1010. goto clean_slave_ret;
  1011. }
  1012. priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1013. if (!priv->cpsw_res) {
  1014. dev_err(priv->dev, "error getting i/o resource\n");
  1015. ret = -ENOENT;
  1016. goto clean_clk_ret;
  1017. }
  1018. if (!request_mem_region(priv->cpsw_res->start,
  1019. resource_size(priv->cpsw_res), ndev->name)) {
  1020. dev_err(priv->dev, "failed request i/o region\n");
  1021. ret = -ENXIO;
  1022. goto clean_clk_ret;
  1023. }
  1024. ss_regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
  1025. if (!ss_regs) {
  1026. dev_err(priv->dev, "unable to map i/o region\n");
  1027. goto clean_cpsw_iores_ret;
  1028. }
  1029. priv->regs = ss_regs;
  1030. priv->version = __raw_readl(&priv->regs->id_ver);
  1031. priv->host_port = HOST_PORT_NUM;
  1032. priv->cpsw_wr_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1033. if (!priv->cpsw_wr_res) {
  1034. dev_err(priv->dev, "error getting i/o resource\n");
  1035. ret = -ENOENT;
  1036. goto clean_iomap_ret;
  1037. }
  1038. if (!request_mem_region(priv->cpsw_wr_res->start,
  1039. resource_size(priv->cpsw_wr_res), ndev->name)) {
  1040. dev_err(priv->dev, "failed request i/o region\n");
  1041. ret = -ENXIO;
  1042. goto clean_iomap_ret;
  1043. }
  1044. wr_regs = ioremap(priv->cpsw_wr_res->start,
  1045. resource_size(priv->cpsw_wr_res));
  1046. if (!wr_regs) {
  1047. dev_err(priv->dev, "unable to map i/o region\n");
  1048. goto clean_cpsw_wr_iores_ret;
  1049. }
  1050. priv->wr_regs = wr_regs;
  1051. memset(&dma_params, 0, sizeof(dma_params));
  1052. memset(&ale_params, 0, sizeof(ale_params));
  1053. switch (priv->version) {
  1054. case CPSW_VERSION_1:
  1055. priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  1056. priv->cpts.reg = ss_regs + CPSW1_CPTS_OFFSET;
  1057. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  1058. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  1059. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  1060. slave_offset = CPSW1_SLAVE_OFFSET;
  1061. slave_size = CPSW1_SLAVE_SIZE;
  1062. sliver_offset = CPSW1_SLIVER_OFFSET;
  1063. dma_params.desc_mem_phys = 0;
  1064. break;
  1065. case CPSW_VERSION_2:
  1066. priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  1067. priv->cpts.reg = ss_regs + CPSW2_CPTS_OFFSET;
  1068. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  1069. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  1070. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  1071. slave_offset = CPSW2_SLAVE_OFFSET;
  1072. slave_size = CPSW2_SLAVE_SIZE;
  1073. sliver_offset = CPSW2_SLIVER_OFFSET;
  1074. dma_params.desc_mem_phys =
  1075. (u32 __force) priv->cpsw_res->start + CPSW2_BD_OFFSET;
  1076. break;
  1077. default:
  1078. dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
  1079. ret = -ENODEV;
  1080. goto clean_cpsw_wr_iores_ret;
  1081. }
  1082. for (i = 0; i < priv->data.slaves; i++) {
  1083. struct cpsw_slave *slave = &priv->slaves[i];
  1084. cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
  1085. slave_offset += slave_size;
  1086. sliver_offset += SLIVER_SIZE;
  1087. }
  1088. dma_params.dev = &pdev->dev;
  1089. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  1090. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  1091. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  1092. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  1093. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  1094. dma_params.num_chan = data->channels;
  1095. dma_params.has_soft_reset = true;
  1096. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  1097. dma_params.desc_mem_size = data->bd_ram_size;
  1098. dma_params.desc_align = 16;
  1099. dma_params.has_ext_regs = true;
  1100. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  1101. priv->dma = cpdma_ctlr_create(&dma_params);
  1102. if (!priv->dma) {
  1103. dev_err(priv->dev, "error initializing dma\n");
  1104. ret = -ENOMEM;
  1105. goto clean_wr_iomap_ret;
  1106. }
  1107. priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
  1108. cpsw_tx_handler);
  1109. priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
  1110. cpsw_rx_handler);
  1111. if (WARN_ON(!priv->txch || !priv->rxch)) {
  1112. dev_err(priv->dev, "error initializing dma channels\n");
  1113. ret = -ENOMEM;
  1114. goto clean_dma_ret;
  1115. }
  1116. ale_params.dev = &ndev->dev;
  1117. ale_params.ale_ageout = ale_ageout;
  1118. ale_params.ale_entries = data->ale_entries;
  1119. ale_params.ale_ports = data->slaves;
  1120. priv->ale = cpsw_ale_create(&ale_params);
  1121. if (!priv->ale) {
  1122. dev_err(priv->dev, "error initializing ale engine\n");
  1123. ret = -ENODEV;
  1124. goto clean_dma_ret;
  1125. }
  1126. ndev->irq = platform_get_irq(pdev, 0);
  1127. if (ndev->irq < 0) {
  1128. dev_err(priv->dev, "error getting irq resource\n");
  1129. ret = -ENOENT;
  1130. goto clean_ale_ret;
  1131. }
  1132. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
  1133. for (i = res->start; i <= res->end; i++) {
  1134. if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
  1135. dev_name(&pdev->dev), priv)) {
  1136. dev_err(priv->dev, "error attaching irq\n");
  1137. goto clean_ale_ret;
  1138. }
  1139. priv->irqs_table[k] = i;
  1140. priv->num_irqs = k;
  1141. }
  1142. k++;
  1143. }
  1144. ndev->flags |= IFF_ALLMULTI; /* see cpsw_ndo_change_rx_flags() */
  1145. ndev->netdev_ops = &cpsw_netdev_ops;
  1146. SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
  1147. netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
  1148. /* register the network device */
  1149. SET_NETDEV_DEV(ndev, &pdev->dev);
  1150. ret = register_netdev(ndev);
  1151. if (ret) {
  1152. dev_err(priv->dev, "error registering net device\n");
  1153. ret = -ENODEV;
  1154. goto clean_irq_ret;
  1155. }
  1156. if (cpts_register(&pdev->dev, &priv->cpts,
  1157. data->cpts_clock_mult, data->cpts_clock_shift))
  1158. dev_err(priv->dev, "error registering cpts device\n");
  1159. cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n",
  1160. priv->cpsw_res->start, ndev->irq);
  1161. return 0;
  1162. clean_irq_ret:
  1163. free_irq(ndev->irq, priv);
  1164. clean_ale_ret:
  1165. cpsw_ale_destroy(priv->ale);
  1166. clean_dma_ret:
  1167. cpdma_chan_destroy(priv->txch);
  1168. cpdma_chan_destroy(priv->rxch);
  1169. cpdma_ctlr_destroy(priv->dma);
  1170. clean_wr_iomap_ret:
  1171. iounmap(priv->wr_regs);
  1172. clean_cpsw_wr_iores_ret:
  1173. release_mem_region(priv->cpsw_wr_res->start,
  1174. resource_size(priv->cpsw_wr_res));
  1175. clean_iomap_ret:
  1176. iounmap(priv->regs);
  1177. clean_cpsw_iores_ret:
  1178. release_mem_region(priv->cpsw_res->start,
  1179. resource_size(priv->cpsw_res));
  1180. clean_clk_ret:
  1181. clk_put(priv->clk);
  1182. clean_slave_ret:
  1183. pm_runtime_disable(&pdev->dev);
  1184. kfree(priv->slaves);
  1185. clean_ndev_ret:
  1186. free_netdev(ndev);
  1187. return ret;
  1188. }
  1189. static int cpsw_remove(struct platform_device *pdev)
  1190. {
  1191. struct net_device *ndev = platform_get_drvdata(pdev);
  1192. struct cpsw_priv *priv = netdev_priv(ndev);
  1193. pr_info("removing device");
  1194. platform_set_drvdata(pdev, NULL);
  1195. cpts_unregister(&priv->cpts);
  1196. free_irq(ndev->irq, priv);
  1197. cpsw_ale_destroy(priv->ale);
  1198. cpdma_chan_destroy(priv->txch);
  1199. cpdma_chan_destroy(priv->rxch);
  1200. cpdma_ctlr_destroy(priv->dma);
  1201. iounmap(priv->regs);
  1202. release_mem_region(priv->cpsw_res->start,
  1203. resource_size(priv->cpsw_res));
  1204. iounmap(priv->wr_regs);
  1205. release_mem_region(priv->cpsw_wr_res->start,
  1206. resource_size(priv->cpsw_wr_res));
  1207. pm_runtime_disable(&pdev->dev);
  1208. clk_put(priv->clk);
  1209. kfree(priv->slaves);
  1210. free_netdev(ndev);
  1211. return 0;
  1212. }
  1213. static int cpsw_suspend(struct device *dev)
  1214. {
  1215. struct platform_device *pdev = to_platform_device(dev);
  1216. struct net_device *ndev = platform_get_drvdata(pdev);
  1217. if (netif_running(ndev))
  1218. cpsw_ndo_stop(ndev);
  1219. pm_runtime_put_sync(&pdev->dev);
  1220. return 0;
  1221. }
  1222. static int cpsw_resume(struct device *dev)
  1223. {
  1224. struct platform_device *pdev = to_platform_device(dev);
  1225. struct net_device *ndev = platform_get_drvdata(pdev);
  1226. pm_runtime_get_sync(&pdev->dev);
  1227. if (netif_running(ndev))
  1228. cpsw_ndo_open(ndev);
  1229. return 0;
  1230. }
  1231. static const struct dev_pm_ops cpsw_pm_ops = {
  1232. .suspend = cpsw_suspend,
  1233. .resume = cpsw_resume,
  1234. };
  1235. static const struct of_device_id cpsw_of_mtable[] = {
  1236. { .compatible = "ti,cpsw", },
  1237. { /* sentinel */ },
  1238. };
  1239. static struct platform_driver cpsw_driver = {
  1240. .driver = {
  1241. .name = "cpsw",
  1242. .owner = THIS_MODULE,
  1243. .pm = &cpsw_pm_ops,
  1244. .of_match_table = of_match_ptr(cpsw_of_mtable),
  1245. },
  1246. .probe = cpsw_probe,
  1247. .remove = cpsw_remove,
  1248. };
  1249. static int __init cpsw_init(void)
  1250. {
  1251. return platform_driver_register(&cpsw_driver);
  1252. }
  1253. late_initcall(cpsw_init);
  1254. static void __exit cpsw_exit(void)
  1255. {
  1256. platform_driver_unregister(&cpsw_driver);
  1257. }
  1258. module_exit(cpsw_exit);
  1259. MODULE_LICENSE("GPL");
  1260. MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
  1261. MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
  1262. MODULE_DESCRIPTION("TI CPSW Ethernet driver");