cpsw.c 55 KB

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