cpsw.c 49 KB

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