cpsw.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514
  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/mfd/syscon.h>
  36. #include <linux/regmap.h>
  37. #include <linux/pinctrl/consumer.h>
  38. #include "cpsw.h"
  39. #include "cpsw_ale.h"
  40. #include "cpts.h"
  41. #include "davinci_cpdma.h"
  42. #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
  43. NETIF_MSG_DRV | NETIF_MSG_LINK | \
  44. NETIF_MSG_IFUP | NETIF_MSG_INTR | \
  45. NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
  46. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
  47. NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
  48. NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
  49. NETIF_MSG_RX_STATUS)
  50. #define cpsw_info(priv, type, format, ...) \
  51. do { \
  52. if (netif_msg_##type(priv) && net_ratelimit()) \
  53. dev_info(priv->dev, format, ## __VA_ARGS__); \
  54. } while (0)
  55. #define cpsw_err(priv, type, format, ...) \
  56. do { \
  57. if (netif_msg_##type(priv) && net_ratelimit()) \
  58. dev_err(priv->dev, format, ## __VA_ARGS__); \
  59. } while (0)
  60. #define cpsw_dbg(priv, type, format, ...) \
  61. do { \
  62. if (netif_msg_##type(priv) && net_ratelimit()) \
  63. dev_dbg(priv->dev, format, ## __VA_ARGS__); \
  64. } while (0)
  65. #define cpsw_notice(priv, type, format, ...) \
  66. do { \
  67. if (netif_msg_##type(priv) && net_ratelimit()) \
  68. dev_notice(priv->dev, format, ## __VA_ARGS__); \
  69. } while (0)
  70. #define ALE_ALL_PORTS 0x7
  71. #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
  72. #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
  73. #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
  74. #define CPSW_VERSION_1 0x19010a
  75. #define CPSW_VERSION_2 0x19010c
  76. #define CPSW_VERSION_3 0x19010f
  77. #define CPSW_VERSION_4 0x190112
  78. #define HOST_PORT_NUM 0
  79. #define SLIVER_SIZE 0x40
  80. #define CPSW1_HOST_PORT_OFFSET 0x028
  81. #define CPSW1_SLAVE_OFFSET 0x050
  82. #define CPSW1_SLAVE_SIZE 0x040
  83. #define CPSW1_CPDMA_OFFSET 0x100
  84. #define CPSW1_STATERAM_OFFSET 0x200
  85. #define CPSW1_HW_STATS 0x400
  86. #define CPSW1_CPTS_OFFSET 0x500
  87. #define CPSW1_ALE_OFFSET 0x600
  88. #define CPSW1_SLIVER_OFFSET 0x700
  89. #define CPSW2_HOST_PORT_OFFSET 0x108
  90. #define CPSW2_SLAVE_OFFSET 0x200
  91. #define CPSW2_SLAVE_SIZE 0x100
  92. #define CPSW2_CPDMA_OFFSET 0x800
  93. #define CPSW2_HW_STATS 0x900
  94. #define CPSW2_STATERAM_OFFSET 0xa00
  95. #define CPSW2_CPTS_OFFSET 0xc00
  96. #define CPSW2_ALE_OFFSET 0xd00
  97. #define CPSW2_SLIVER_OFFSET 0xd80
  98. #define CPSW2_BD_OFFSET 0x2000
  99. #define CPDMA_RXTHRESH 0x0c0
  100. #define CPDMA_RXFREE 0x0e0
  101. #define CPDMA_TXHDP 0x00
  102. #define CPDMA_RXHDP 0x20
  103. #define CPDMA_TXCP 0x40
  104. #define CPDMA_RXCP 0x60
  105. #define CPSW_POLL_WEIGHT 64
  106. #define CPSW_MIN_PACKET_SIZE 60
  107. #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
  108. #define RX_PRIORITY_MAPPING 0x76543210
  109. #define TX_PRIORITY_MAPPING 0x33221100
  110. #define CPDMA_TX_PRIORITY_MAP 0x76543210
  111. #define CPSW_VLAN_AWARE BIT(1)
  112. #define CPSW_ALE_VLAN_AWARE 1
  113. #define CPSW_FIFO_NORMAL_MODE (0 << 15)
  114. #define CPSW_FIFO_DUAL_MAC_MODE (1 << 15)
  115. #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 15)
  116. #define CPSW_INTPACEEN (0x3f << 16)
  117. #define CPSW_INTPRESCALE_MASK (0x7FF << 0)
  118. #define CPSW_CMINTMAX_CNT 63
  119. #define CPSW_CMINTMIN_CNT 2
  120. #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
  121. #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
  122. #define cpsw_enable_irq(priv) \
  123. do { \
  124. u32 i; \
  125. for (i = 0; i < priv->num_irqs; i++) \
  126. enable_irq(priv->irqs_table[i]); \
  127. } while (0)
  128. #define cpsw_disable_irq(priv) \
  129. do { \
  130. u32 i; \
  131. for (i = 0; i < priv->num_irqs; i++) \
  132. disable_irq_nosync(priv->irqs_table[i]); \
  133. } while (0)
  134. #define cpsw_slave_index(priv) \
  135. ((priv->data.dual_emac) ? priv->emac_port : \
  136. priv->data.active_slave)
  137. static int debug_level;
  138. module_param(debug_level, int, 0);
  139. MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
  140. static int ale_ageout = 10;
  141. module_param(ale_ageout, int, 0);
  142. MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
  143. static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  144. module_param(rx_packet_max, int, 0);
  145. MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
  146. struct cpsw_wr_regs {
  147. u32 id_ver;
  148. u32 soft_reset;
  149. u32 control;
  150. u32 int_control;
  151. u32 rx_thresh_en;
  152. u32 rx_en;
  153. u32 tx_en;
  154. u32 misc_en;
  155. u32 mem_allign1[8];
  156. u32 rx_thresh_stat;
  157. u32 rx_stat;
  158. u32 tx_stat;
  159. u32 misc_stat;
  160. u32 mem_allign2[8];
  161. u32 rx_imax;
  162. u32 tx_imax;
  163. };
  164. struct cpsw_ss_regs {
  165. u32 id_ver;
  166. u32 control;
  167. u32 soft_reset;
  168. u32 stat_port_en;
  169. u32 ptype;
  170. u32 soft_idle;
  171. u32 thru_rate;
  172. u32 gap_thresh;
  173. u32 tx_start_wds;
  174. u32 flow_control;
  175. u32 vlan_ltype;
  176. u32 ts_ltype;
  177. u32 dlr_ltype;
  178. };
  179. /* CPSW_PORT_V1 */
  180. #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
  181. #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
  182. #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
  183. #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
  184. #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
  185. #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
  186. #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
  187. #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
  188. /* CPSW_PORT_V2 */
  189. #define CPSW2_CONTROL 0x00 /* Control Register */
  190. #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
  191. #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
  192. #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
  193. #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
  194. #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
  195. #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
  196. /* CPSW_PORT_V1 and V2 */
  197. #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
  198. #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
  199. #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
  200. /* CPSW_PORT_V2 only */
  201. #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
  202. #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
  203. #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
  204. #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
  205. #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
  206. #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
  207. #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
  208. #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
  209. /* Bit definitions for the CPSW2_CONTROL register */
  210. #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
  211. #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
  212. #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
  213. #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
  214. #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
  215. #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
  216. #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
  217. #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
  218. #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
  219. #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
  220. #define TS_TTL_NONZERO (1<<8) /* Time Sync Time To Live Non-zero enable */
  221. #define TS_ANNEX_F_EN (1<<6) /* Time Sync Annex F enable */
  222. #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
  223. #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
  224. #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
  225. #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
  226. #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
  227. #define CTRL_V2_TS_BITS \
  228. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
  229. TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
  230. #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
  231. #define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
  232. #define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
  233. #define CTRL_V3_TS_BITS \
  234. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
  235. TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
  236. TS_LTYPE1_EN)
  237. #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
  238. #define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
  239. #define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
  240. /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
  241. #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
  242. #define TS_SEQ_ID_OFFSET_MASK (0x3f)
  243. #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
  244. #define TS_MSG_TYPE_EN_MASK (0xffff)
  245. /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
  246. #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
  247. /* Bit definitions for the CPSW1_TS_CTL register */
  248. #define CPSW_V1_TS_RX_EN BIT(0)
  249. #define CPSW_V1_TS_TX_EN BIT(4)
  250. #define CPSW_V1_MSG_TYPE_OFS 16
  251. /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
  252. #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
  253. struct cpsw_host_regs {
  254. u32 max_blks;
  255. u32 blk_cnt;
  256. u32 tx_in_ctl;
  257. u32 port_vlan;
  258. u32 tx_pri_map;
  259. u32 cpdma_tx_pri_map;
  260. u32 cpdma_rx_chan_map;
  261. };
  262. struct cpsw_sliver_regs {
  263. u32 id_ver;
  264. u32 mac_control;
  265. u32 mac_status;
  266. u32 soft_reset;
  267. u32 rx_maxlen;
  268. u32 __reserved_0;
  269. u32 rx_pause;
  270. u32 tx_pause;
  271. u32 __reserved_1;
  272. u32 rx_pri_map;
  273. };
  274. struct cpsw_hw_stats {
  275. u32 rxgoodframes;
  276. u32 rxbroadcastframes;
  277. u32 rxmulticastframes;
  278. u32 rxpauseframes;
  279. u32 rxcrcerrors;
  280. u32 rxaligncodeerrors;
  281. u32 rxoversizedframes;
  282. u32 rxjabberframes;
  283. u32 rxundersizedframes;
  284. u32 rxfragments;
  285. u32 __pad_0[2];
  286. u32 rxoctets;
  287. u32 txgoodframes;
  288. u32 txbroadcastframes;
  289. u32 txmulticastframes;
  290. u32 txpauseframes;
  291. u32 txdeferredframes;
  292. u32 txcollisionframes;
  293. u32 txsinglecollframes;
  294. u32 txmultcollframes;
  295. u32 txexcessivecollisions;
  296. u32 txlatecollisions;
  297. u32 txunderrun;
  298. u32 txcarriersenseerrors;
  299. u32 txoctets;
  300. u32 octetframes64;
  301. u32 octetframes65t127;
  302. u32 octetframes128t255;
  303. u32 octetframes256t511;
  304. u32 octetframes512t1023;
  305. u32 octetframes1024tup;
  306. u32 netoctets;
  307. u32 rxsofoverruns;
  308. u32 rxmofoverruns;
  309. u32 rxdmaoverruns;
  310. };
  311. struct cpsw_slave {
  312. void __iomem *regs;
  313. struct cpsw_sliver_regs __iomem *sliver;
  314. int slave_num;
  315. u32 mac_control;
  316. struct cpsw_slave_data *data;
  317. struct phy_device *phy;
  318. struct net_device *ndev;
  319. u32 port_vlan;
  320. u32 open_stat;
  321. };
  322. static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
  323. {
  324. return __raw_readl(slave->regs + offset);
  325. }
  326. static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
  327. {
  328. __raw_writel(val, slave->regs + offset);
  329. }
  330. struct cpsw_priv {
  331. spinlock_t lock;
  332. struct platform_device *pdev;
  333. struct net_device *ndev;
  334. struct napi_struct napi;
  335. struct device *dev;
  336. struct cpsw_platform_data data;
  337. struct cpsw_ss_regs __iomem *regs;
  338. struct cpsw_wr_regs __iomem *wr_regs;
  339. u8 __iomem *hw_stats;
  340. struct cpsw_host_regs __iomem *host_port_regs;
  341. u32 msg_enable;
  342. u32 version;
  343. u32 coal_intvl;
  344. u32 bus_freq_mhz;
  345. int rx_packet_max;
  346. int host_port;
  347. struct clk *clk;
  348. u8 mac_addr[ETH_ALEN];
  349. struct cpsw_slave *slaves;
  350. struct cpdma_ctlr *dma;
  351. struct cpdma_chan *txch, *rxch;
  352. struct cpsw_ale *ale;
  353. bool rx_pause;
  354. bool tx_pause;
  355. /* snapshot of IRQ numbers */
  356. u32 irqs_table[4];
  357. u32 num_irqs;
  358. bool irq_enabled;
  359. struct cpts *cpts;
  360. u32 emac_port;
  361. };
  362. struct cpsw_stats {
  363. char stat_string[ETH_GSTRING_LEN];
  364. int type;
  365. int sizeof_stat;
  366. int stat_offset;
  367. };
  368. enum {
  369. CPSW_STATS,
  370. CPDMA_RX_STATS,
  371. CPDMA_TX_STATS,
  372. };
  373. #define CPSW_STAT(m) CPSW_STATS, \
  374. sizeof(((struct cpsw_hw_stats *)0)->m), \
  375. offsetof(struct cpsw_hw_stats, m)
  376. #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
  377. sizeof(((struct cpdma_chan_stats *)0)->m), \
  378. offsetof(struct cpdma_chan_stats, m)
  379. #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
  380. sizeof(((struct cpdma_chan_stats *)0)->m), \
  381. offsetof(struct cpdma_chan_stats, m)
  382. static const struct cpsw_stats cpsw_gstrings_stats[] = {
  383. { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
  384. { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
  385. { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
  386. { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
  387. { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
  388. { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
  389. { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
  390. { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
  391. { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
  392. { "Rx Fragments", CPSW_STAT(rxfragments) },
  393. { "Rx Octets", CPSW_STAT(rxoctets) },
  394. { "Good Tx Frames", CPSW_STAT(txgoodframes) },
  395. { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
  396. { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
  397. { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
  398. { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
  399. { "Collisions", CPSW_STAT(txcollisionframes) },
  400. { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
  401. { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
  402. { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
  403. { "Late Collisions", CPSW_STAT(txlatecollisions) },
  404. { "Tx Underrun", CPSW_STAT(txunderrun) },
  405. { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
  406. { "Tx Octets", CPSW_STAT(txoctets) },
  407. { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
  408. { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
  409. { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
  410. { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
  411. { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
  412. { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
  413. { "Net Octets", CPSW_STAT(netoctets) },
  414. { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
  415. { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
  416. { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
  417. { "Rx DMA chan: head_enqueue", CPDMA_RX_STAT(head_enqueue) },
  418. { "Rx DMA chan: tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
  419. { "Rx DMA chan: pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
  420. { "Rx DMA chan: misqueued", CPDMA_RX_STAT(misqueued) },
  421. { "Rx DMA chan: desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
  422. { "Rx DMA chan: pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
  423. { "Rx DMA chan: runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
  424. { "Rx DMA chan: runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
  425. { "Rx DMA chan: empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
  426. { "Rx DMA chan: busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
  427. { "Rx DMA chan: good_dequeue", CPDMA_RX_STAT(good_dequeue) },
  428. { "Rx DMA chan: requeue", CPDMA_RX_STAT(requeue) },
  429. { "Rx DMA chan: teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
  430. { "Tx DMA chan: head_enqueue", CPDMA_TX_STAT(head_enqueue) },
  431. { "Tx DMA chan: tail_enqueue", CPDMA_TX_STAT(tail_enqueue) },
  432. { "Tx DMA chan: pad_enqueue", CPDMA_TX_STAT(pad_enqueue) },
  433. { "Tx DMA chan: misqueued", CPDMA_TX_STAT(misqueued) },
  434. { "Tx DMA chan: desc_alloc_fail", CPDMA_TX_STAT(desc_alloc_fail) },
  435. { "Tx DMA chan: pad_alloc_fail", CPDMA_TX_STAT(pad_alloc_fail) },
  436. { "Tx DMA chan: runt_receive_buf", CPDMA_TX_STAT(runt_receive_buff) },
  437. { "Tx DMA chan: runt_transmit_buf", CPDMA_TX_STAT(runt_transmit_buff) },
  438. { "Tx DMA chan: empty_dequeue", CPDMA_TX_STAT(empty_dequeue) },
  439. { "Tx DMA chan: busy_dequeue", CPDMA_TX_STAT(busy_dequeue) },
  440. { "Tx DMA chan: good_dequeue", CPDMA_TX_STAT(good_dequeue) },
  441. { "Tx DMA chan: requeue", CPDMA_TX_STAT(requeue) },
  442. { "Tx DMA chan: teardown_dequeue", CPDMA_TX_STAT(teardown_dequeue) },
  443. };
  444. #define CPSW_STATS_LEN ARRAY_SIZE(cpsw_gstrings_stats)
  445. #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
  446. #define for_each_slave(priv, func, arg...) \
  447. do { \
  448. struct cpsw_slave *slave; \
  449. int n; \
  450. if (priv->data.dual_emac) \
  451. (func)((priv)->slaves + priv->emac_port, ##arg);\
  452. else \
  453. for (n = (priv)->data.slaves, \
  454. slave = (priv)->slaves; \
  455. n; n--) \
  456. (func)(slave++, ##arg); \
  457. } while (0)
  458. #define cpsw_get_slave_ndev(priv, __slave_no__) \
  459. (priv->slaves[__slave_no__].ndev)
  460. #define cpsw_get_slave_priv(priv, __slave_no__) \
  461. ((priv->slaves[__slave_no__].ndev) ? \
  462. netdev_priv(priv->slaves[__slave_no__].ndev) : NULL) \
  463. #define cpsw_dual_emac_src_port_detect(status, priv, ndev, skb) \
  464. do { \
  465. if (!priv->data.dual_emac) \
  466. break; \
  467. if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
  468. ndev = cpsw_get_slave_ndev(priv, 0); \
  469. priv = netdev_priv(ndev); \
  470. skb->dev = ndev; \
  471. } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
  472. ndev = cpsw_get_slave_ndev(priv, 1); \
  473. priv = netdev_priv(ndev); \
  474. skb->dev = ndev; \
  475. } \
  476. } while (0)
  477. #define cpsw_add_mcast(priv, addr) \
  478. do { \
  479. if (priv->data.dual_emac) { \
  480. struct cpsw_slave *slave = priv->slaves + \
  481. priv->emac_port; \
  482. int slave_port = cpsw_get_slave_port(priv, \
  483. slave->slave_num); \
  484. cpsw_ale_add_mcast(priv->ale, addr, \
  485. 1 << slave_port | 1 << priv->host_port, \
  486. ALE_VLAN, slave->port_vlan, 0); \
  487. } else { \
  488. cpsw_ale_add_mcast(priv->ale, addr, \
  489. ALE_ALL_PORTS << priv->host_port, \
  490. 0, 0, 0); \
  491. } \
  492. } while (0)
  493. static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
  494. {
  495. if (priv->host_port == 0)
  496. return slave_num + 1;
  497. else
  498. return slave_num;
  499. }
  500. static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
  501. {
  502. struct cpsw_priv *priv = netdev_priv(ndev);
  503. struct cpsw_ale *ale = priv->ale;
  504. int i;
  505. if (priv->data.dual_emac) {
  506. bool flag = false;
  507. /* Enabling promiscuous mode for one interface will be
  508. * common for both the interface as the interface shares
  509. * the same hardware resource.
  510. */
  511. for (i = 0; i < priv->data.slaves; i++)
  512. if (priv->slaves[i].ndev->flags & IFF_PROMISC)
  513. flag = true;
  514. if (!enable && flag) {
  515. enable = true;
  516. dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
  517. }
  518. if (enable) {
  519. /* Enable Bypass */
  520. cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
  521. dev_dbg(&ndev->dev, "promiscuity enabled\n");
  522. } else {
  523. /* Disable Bypass */
  524. cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
  525. dev_dbg(&ndev->dev, "promiscuity disabled\n");
  526. }
  527. } else {
  528. if (enable) {
  529. unsigned long timeout = jiffies + HZ;
  530. /* Disable Learn for all ports */
  531. for (i = 0; i < priv->data.slaves; i++) {
  532. cpsw_ale_control_set(ale, i,
  533. ALE_PORT_NOLEARN, 1);
  534. cpsw_ale_control_set(ale, i,
  535. ALE_PORT_NO_SA_UPDATE, 1);
  536. }
  537. /* Clear All Untouched entries */
  538. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  539. do {
  540. cpu_relax();
  541. if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
  542. break;
  543. } while (time_after(timeout, jiffies));
  544. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  545. /* Clear all mcast from ALE */
  546. cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS <<
  547. priv->host_port);
  548. /* Flood All Unicast Packets to Host port */
  549. cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
  550. dev_dbg(&ndev->dev, "promiscuity enabled\n");
  551. } else {
  552. /* Flood All Unicast Packets to Host port */
  553. cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
  554. /* Enable Learn for all ports */
  555. for (i = 0; i < priv->data.slaves; i++) {
  556. cpsw_ale_control_set(ale, i,
  557. ALE_PORT_NOLEARN, 0);
  558. cpsw_ale_control_set(ale, i,
  559. ALE_PORT_NO_SA_UPDATE, 0);
  560. }
  561. dev_dbg(&ndev->dev, "promiscuity disabled\n");
  562. }
  563. }
  564. }
  565. static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
  566. {
  567. struct cpsw_priv *priv = netdev_priv(ndev);
  568. if (ndev->flags & IFF_PROMISC) {
  569. /* Enable promiscuous mode */
  570. cpsw_set_promiscious(ndev, true);
  571. return;
  572. } else {
  573. /* Disable promiscuous mode */
  574. cpsw_set_promiscious(ndev, false);
  575. }
  576. /* Clear all mcast from ALE */
  577. cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port);
  578. if (!netdev_mc_empty(ndev)) {
  579. struct netdev_hw_addr *ha;
  580. /* program multicast address list into ALE register */
  581. netdev_for_each_mc_addr(ha, ndev) {
  582. cpsw_add_mcast(priv, (u8 *)ha->addr);
  583. }
  584. }
  585. }
  586. static void cpsw_intr_enable(struct cpsw_priv *priv)
  587. {
  588. __raw_writel(0xFF, &priv->wr_regs->tx_en);
  589. __raw_writel(0xFF, &priv->wr_regs->rx_en);
  590. cpdma_ctlr_int_ctrl(priv->dma, true);
  591. return;
  592. }
  593. static void cpsw_intr_disable(struct cpsw_priv *priv)
  594. {
  595. __raw_writel(0, &priv->wr_regs->tx_en);
  596. __raw_writel(0, &priv->wr_regs->rx_en);
  597. cpdma_ctlr_int_ctrl(priv->dma, false);
  598. return;
  599. }
  600. static void cpsw_tx_handler(void *token, int len, int status)
  601. {
  602. struct sk_buff *skb = token;
  603. struct net_device *ndev = skb->dev;
  604. struct cpsw_priv *priv = netdev_priv(ndev);
  605. /* Check whether the queue is stopped due to stalled tx dma, if the
  606. * queue is stopped then start the queue as we have free desc for tx
  607. */
  608. if (unlikely(netif_queue_stopped(ndev)))
  609. netif_wake_queue(ndev);
  610. cpts_tx_timestamp(priv->cpts, skb);
  611. ndev->stats.tx_packets++;
  612. ndev->stats.tx_bytes += len;
  613. dev_kfree_skb_any(skb);
  614. }
  615. static void cpsw_rx_handler(void *token, int len, int status)
  616. {
  617. struct sk_buff *skb = token;
  618. struct sk_buff *new_skb;
  619. struct net_device *ndev = skb->dev;
  620. struct cpsw_priv *priv = netdev_priv(ndev);
  621. int ret = 0;
  622. cpsw_dual_emac_src_port_detect(status, priv, ndev, skb);
  623. if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
  624. bool ndev_status = false;
  625. struct cpsw_slave *slave = priv->slaves;
  626. int n;
  627. if (priv->data.dual_emac) {
  628. /* In dual emac mode check for all interfaces */
  629. for (n = priv->data.slaves; n; n--, slave++)
  630. if (netif_running(slave->ndev))
  631. ndev_status = true;
  632. }
  633. if (ndev_status && (status >= 0)) {
  634. /* The packet received is for the interface which
  635. * is already down and the other interface is up
  636. * and running, intead of freeing which results
  637. * in reducing of the number of rx descriptor in
  638. * DMA engine, requeue skb back to cpdma.
  639. */
  640. new_skb = skb;
  641. goto requeue;
  642. }
  643. /* the interface is going down, skbs are purged */
  644. dev_kfree_skb_any(skb);
  645. return;
  646. }
  647. new_skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
  648. if (new_skb) {
  649. skb_put(skb, len);
  650. cpts_rx_timestamp(priv->cpts, skb);
  651. skb->protocol = eth_type_trans(skb, ndev);
  652. netif_receive_skb(skb);
  653. ndev->stats.rx_bytes += len;
  654. ndev->stats.rx_packets++;
  655. } else {
  656. ndev->stats.rx_dropped++;
  657. new_skb = skb;
  658. }
  659. requeue:
  660. ret = cpdma_chan_submit(priv->rxch, new_skb, new_skb->data,
  661. skb_tailroom(new_skb), 0);
  662. if (WARN_ON(ret < 0))
  663. dev_kfree_skb_any(new_skb);
  664. }
  665. static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
  666. {
  667. struct cpsw_priv *priv = dev_id;
  668. cpsw_intr_disable(priv);
  669. if (priv->irq_enabled == true) {
  670. cpsw_disable_irq(priv);
  671. priv->irq_enabled = false;
  672. }
  673. if (netif_running(priv->ndev)) {
  674. napi_schedule(&priv->napi);
  675. return IRQ_HANDLED;
  676. }
  677. priv = cpsw_get_slave_priv(priv, 1);
  678. if (!priv)
  679. return IRQ_NONE;
  680. if (netif_running(priv->ndev)) {
  681. napi_schedule(&priv->napi);
  682. return IRQ_HANDLED;
  683. }
  684. return IRQ_NONE;
  685. }
  686. static int cpsw_poll(struct napi_struct *napi, int budget)
  687. {
  688. struct cpsw_priv *priv = napi_to_priv(napi);
  689. int num_tx, num_rx;
  690. num_tx = cpdma_chan_process(priv->txch, 128);
  691. if (num_tx)
  692. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  693. num_rx = cpdma_chan_process(priv->rxch, budget);
  694. if (num_rx < budget) {
  695. struct cpsw_priv *prim_cpsw;
  696. napi_complete(napi);
  697. cpsw_intr_enable(priv);
  698. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  699. prim_cpsw = cpsw_get_slave_priv(priv, 0);
  700. if (prim_cpsw->irq_enabled == false) {
  701. prim_cpsw->irq_enabled = true;
  702. cpsw_enable_irq(priv);
  703. }
  704. }
  705. if (num_rx || num_tx)
  706. cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
  707. num_rx, num_tx);
  708. return num_rx;
  709. }
  710. static inline void soft_reset(const char *module, void __iomem *reg)
  711. {
  712. unsigned long timeout = jiffies + HZ;
  713. __raw_writel(1, reg);
  714. do {
  715. cpu_relax();
  716. } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
  717. WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
  718. }
  719. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  720. ((mac)[2] << 16) | ((mac)[3] << 24))
  721. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  722. static void cpsw_set_slave_mac(struct cpsw_slave *slave,
  723. struct cpsw_priv *priv)
  724. {
  725. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  726. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  727. }
  728. static void _cpsw_adjust_link(struct cpsw_slave *slave,
  729. struct cpsw_priv *priv, bool *link)
  730. {
  731. struct phy_device *phy = slave->phy;
  732. u32 mac_control = 0;
  733. u32 slave_port;
  734. if (!phy)
  735. return;
  736. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  737. if (phy->link) {
  738. mac_control = priv->data.mac_control;
  739. /* enable forwarding */
  740. cpsw_ale_control_set(priv->ale, slave_port,
  741. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  742. if (phy->speed == 1000)
  743. mac_control |= BIT(7); /* GIGABITEN */
  744. if (phy->duplex)
  745. mac_control |= BIT(0); /* FULLDUPLEXEN */
  746. /* set speed_in input in case RMII mode is used in 100Mbps */
  747. if (phy->speed == 100)
  748. mac_control |= BIT(15);
  749. else if (phy->speed == 10)
  750. mac_control |= BIT(18); /* In Band mode */
  751. if (priv->rx_pause)
  752. mac_control |= BIT(3);
  753. if (priv->tx_pause)
  754. mac_control |= BIT(4);
  755. *link = true;
  756. } else {
  757. mac_control = 0;
  758. /* disable forwarding */
  759. cpsw_ale_control_set(priv->ale, slave_port,
  760. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  761. }
  762. if (mac_control != slave->mac_control) {
  763. phy_print_status(phy);
  764. __raw_writel(mac_control, &slave->sliver->mac_control);
  765. }
  766. slave->mac_control = mac_control;
  767. }
  768. static void cpsw_adjust_link(struct net_device *ndev)
  769. {
  770. struct cpsw_priv *priv = netdev_priv(ndev);
  771. bool link = false;
  772. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  773. if (link) {
  774. netif_carrier_on(ndev);
  775. if (netif_running(ndev))
  776. netif_wake_queue(ndev);
  777. } else {
  778. netif_carrier_off(ndev);
  779. netif_stop_queue(ndev);
  780. }
  781. }
  782. static int cpsw_get_coalesce(struct net_device *ndev,
  783. struct ethtool_coalesce *coal)
  784. {
  785. struct cpsw_priv *priv = netdev_priv(ndev);
  786. coal->rx_coalesce_usecs = priv->coal_intvl;
  787. return 0;
  788. }
  789. static int cpsw_set_coalesce(struct net_device *ndev,
  790. struct ethtool_coalesce *coal)
  791. {
  792. struct cpsw_priv *priv = netdev_priv(ndev);
  793. u32 int_ctrl;
  794. u32 num_interrupts = 0;
  795. u32 prescale = 0;
  796. u32 addnl_dvdr = 1;
  797. u32 coal_intvl = 0;
  798. coal_intvl = coal->rx_coalesce_usecs;
  799. int_ctrl = readl(&priv->wr_regs->int_control);
  800. prescale = priv->bus_freq_mhz * 4;
  801. if (!coal->rx_coalesce_usecs) {
  802. int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
  803. goto update_return;
  804. }
  805. if (coal_intvl < CPSW_CMINTMIN_INTVL)
  806. coal_intvl = CPSW_CMINTMIN_INTVL;
  807. if (coal_intvl > CPSW_CMINTMAX_INTVL) {
  808. /* Interrupt pacer works with 4us Pulse, we can
  809. * throttle further by dilating the 4us pulse.
  810. */
  811. addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
  812. if (addnl_dvdr > 1) {
  813. prescale *= addnl_dvdr;
  814. if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
  815. coal_intvl = (CPSW_CMINTMAX_INTVL
  816. * addnl_dvdr);
  817. } else {
  818. addnl_dvdr = 1;
  819. coal_intvl = CPSW_CMINTMAX_INTVL;
  820. }
  821. }
  822. num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
  823. writel(num_interrupts, &priv->wr_regs->rx_imax);
  824. writel(num_interrupts, &priv->wr_regs->tx_imax);
  825. int_ctrl |= CPSW_INTPACEEN;
  826. int_ctrl &= (~CPSW_INTPRESCALE_MASK);
  827. int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
  828. update_return:
  829. writel(int_ctrl, &priv->wr_regs->int_control);
  830. cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
  831. if (priv->data.dual_emac) {
  832. int i;
  833. for (i = 0; i < priv->data.slaves; i++) {
  834. priv = netdev_priv(priv->slaves[i].ndev);
  835. priv->coal_intvl = coal_intvl;
  836. }
  837. } else {
  838. priv->coal_intvl = coal_intvl;
  839. }
  840. return 0;
  841. }
  842. static int cpsw_get_sset_count(struct net_device *ndev, int sset)
  843. {
  844. switch (sset) {
  845. case ETH_SS_STATS:
  846. return CPSW_STATS_LEN;
  847. default:
  848. return -EOPNOTSUPP;
  849. }
  850. }
  851. static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  852. {
  853. u8 *p = data;
  854. int i;
  855. switch (stringset) {
  856. case ETH_SS_STATS:
  857. for (i = 0; i < CPSW_STATS_LEN; i++) {
  858. memcpy(p, cpsw_gstrings_stats[i].stat_string,
  859. ETH_GSTRING_LEN);
  860. p += ETH_GSTRING_LEN;
  861. }
  862. break;
  863. }
  864. }
  865. static void cpsw_get_ethtool_stats(struct net_device *ndev,
  866. struct ethtool_stats *stats, u64 *data)
  867. {
  868. struct cpsw_priv *priv = netdev_priv(ndev);
  869. struct cpdma_chan_stats rx_stats;
  870. struct cpdma_chan_stats tx_stats;
  871. u32 val;
  872. u8 *p;
  873. int i;
  874. /* Collect Davinci CPDMA stats for Rx and Tx Channel */
  875. cpdma_chan_get_stats(priv->rxch, &rx_stats);
  876. cpdma_chan_get_stats(priv->txch, &tx_stats);
  877. for (i = 0; i < CPSW_STATS_LEN; i++) {
  878. switch (cpsw_gstrings_stats[i].type) {
  879. case CPSW_STATS:
  880. val = readl(priv->hw_stats +
  881. cpsw_gstrings_stats[i].stat_offset);
  882. data[i] = val;
  883. break;
  884. case CPDMA_RX_STATS:
  885. p = (u8 *)&rx_stats +
  886. cpsw_gstrings_stats[i].stat_offset;
  887. data[i] = *(u32 *)p;
  888. break;
  889. case CPDMA_TX_STATS:
  890. p = (u8 *)&tx_stats +
  891. cpsw_gstrings_stats[i].stat_offset;
  892. data[i] = *(u32 *)p;
  893. break;
  894. }
  895. }
  896. }
  897. static int cpsw_common_res_usage_state(struct cpsw_priv *priv)
  898. {
  899. u32 i;
  900. u32 usage_count = 0;
  901. if (!priv->data.dual_emac)
  902. return 0;
  903. for (i = 0; i < priv->data.slaves; i++)
  904. if (priv->slaves[i].open_stat)
  905. usage_count++;
  906. return usage_count;
  907. }
  908. static inline int cpsw_tx_packet_submit(struct net_device *ndev,
  909. struct cpsw_priv *priv, struct sk_buff *skb)
  910. {
  911. if (!priv->data.dual_emac)
  912. return cpdma_chan_submit(priv->txch, skb, skb->data,
  913. skb->len, 0);
  914. if (ndev == cpsw_get_slave_ndev(priv, 0))
  915. return cpdma_chan_submit(priv->txch, skb, skb->data,
  916. skb->len, 1);
  917. else
  918. return cpdma_chan_submit(priv->txch, skb, skb->data,
  919. skb->len, 2);
  920. }
  921. static inline void cpsw_add_dual_emac_def_ale_entries(
  922. struct cpsw_priv *priv, struct cpsw_slave *slave,
  923. u32 slave_port)
  924. {
  925. u32 port_mask = 1 << slave_port | 1 << priv->host_port;
  926. if (priv->version == CPSW_VERSION_1)
  927. slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
  928. else
  929. slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
  930. cpsw_ale_add_vlan(priv->ale, slave->port_vlan, port_mask,
  931. port_mask, port_mask, 0);
  932. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  933. port_mask, ALE_VLAN, slave->port_vlan, 0);
  934. cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
  935. priv->host_port, ALE_VLAN, slave->port_vlan);
  936. }
  937. static void soft_reset_slave(struct cpsw_slave *slave)
  938. {
  939. char name[32];
  940. snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
  941. soft_reset(name, &slave->sliver->soft_reset);
  942. }
  943. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  944. {
  945. u32 slave_port;
  946. soft_reset_slave(slave);
  947. /* setup priority mapping */
  948. __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
  949. switch (priv->version) {
  950. case CPSW_VERSION_1:
  951. slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
  952. break;
  953. case CPSW_VERSION_2:
  954. case CPSW_VERSION_3:
  955. case CPSW_VERSION_4:
  956. slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
  957. break;
  958. }
  959. /* setup max packet size, and mac address */
  960. __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
  961. cpsw_set_slave_mac(slave, priv);
  962. slave->mac_control = 0; /* no link yet */
  963. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  964. if (priv->data.dual_emac)
  965. cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
  966. else
  967. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  968. 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
  969. slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
  970. &cpsw_adjust_link, slave->data->phy_if);
  971. if (IS_ERR(slave->phy)) {
  972. dev_err(priv->dev, "phy %s not found on slave %d\n",
  973. slave->data->phy_id, slave->slave_num);
  974. slave->phy = NULL;
  975. } else {
  976. dev_info(priv->dev, "phy found : id is : 0x%x\n",
  977. slave->phy->phy_id);
  978. phy_start(slave->phy);
  979. /* Configure GMII_SEL register */
  980. cpsw_phy_sel(&priv->pdev->dev, slave->phy->interface,
  981. slave->slave_num);
  982. }
  983. }
  984. static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
  985. {
  986. const int vlan = priv->data.default_vlan;
  987. const int port = priv->host_port;
  988. u32 reg;
  989. int i;
  990. reg = (priv->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
  991. CPSW2_PORT_VLAN;
  992. writel(vlan, &priv->host_port_regs->port_vlan);
  993. for (i = 0; i < priv->data.slaves; i++)
  994. slave_write(priv->slaves + i, vlan, reg);
  995. cpsw_ale_add_vlan(priv->ale, vlan, ALE_ALL_PORTS << port,
  996. ALE_ALL_PORTS << port, ALE_ALL_PORTS << port,
  997. (ALE_PORT_1 | ALE_PORT_2) << port);
  998. }
  999. static void cpsw_init_host_port(struct cpsw_priv *priv)
  1000. {
  1001. u32 control_reg;
  1002. u32 fifo_mode;
  1003. /* soft reset the controller and initialize ale */
  1004. soft_reset("cpsw", &priv->regs->soft_reset);
  1005. cpsw_ale_start(priv->ale);
  1006. /* switch to vlan unaware mode */
  1007. cpsw_ale_control_set(priv->ale, priv->host_port, ALE_VLAN_AWARE,
  1008. CPSW_ALE_VLAN_AWARE);
  1009. control_reg = readl(&priv->regs->control);
  1010. control_reg |= CPSW_VLAN_AWARE;
  1011. writel(control_reg, &priv->regs->control);
  1012. fifo_mode = (priv->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
  1013. CPSW_FIFO_NORMAL_MODE;
  1014. writel(fifo_mode, &priv->host_port_regs->tx_in_ctl);
  1015. /* setup host port priority mapping */
  1016. __raw_writel(CPDMA_TX_PRIORITY_MAP,
  1017. &priv->host_port_regs->cpdma_tx_pri_map);
  1018. __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
  1019. cpsw_ale_control_set(priv->ale, priv->host_port,
  1020. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  1021. if (!priv->data.dual_emac) {
  1022. cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port,
  1023. 0, 0);
  1024. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  1025. 1 << priv->host_port, 0, 0, ALE_MCAST_FWD_2);
  1026. }
  1027. }
  1028. static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
  1029. {
  1030. u32 slave_port;
  1031. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  1032. if (!slave->phy)
  1033. return;
  1034. phy_stop(slave->phy);
  1035. phy_disconnect(slave->phy);
  1036. slave->phy = NULL;
  1037. cpsw_ale_control_set(priv->ale, slave_port,
  1038. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  1039. }
  1040. static int cpsw_ndo_open(struct net_device *ndev)
  1041. {
  1042. struct cpsw_priv *priv = netdev_priv(ndev);
  1043. struct cpsw_priv *prim_cpsw;
  1044. int i, ret;
  1045. u32 reg;
  1046. if (!cpsw_common_res_usage_state(priv))
  1047. cpsw_intr_disable(priv);
  1048. netif_carrier_off(ndev);
  1049. pm_runtime_get_sync(&priv->pdev->dev);
  1050. reg = priv->version;
  1051. dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
  1052. CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
  1053. CPSW_RTL_VERSION(reg));
  1054. /* initialize host and slave ports */
  1055. if (!cpsw_common_res_usage_state(priv))
  1056. cpsw_init_host_port(priv);
  1057. for_each_slave(priv, cpsw_slave_open, priv);
  1058. /* Add default VLAN */
  1059. if (!priv->data.dual_emac)
  1060. cpsw_add_default_vlan(priv);
  1061. else
  1062. cpsw_ale_add_vlan(priv->ale, priv->data.default_vlan,
  1063. ALE_ALL_PORTS << priv->host_port,
  1064. ALE_ALL_PORTS << priv->host_port, 0, 0);
  1065. if (!cpsw_common_res_usage_state(priv)) {
  1066. /* setup tx dma to fixed prio and zero offset */
  1067. cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
  1068. cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
  1069. /* disable priority elevation */
  1070. __raw_writel(0, &priv->regs->ptype);
  1071. /* enable statistics collection only on all ports */
  1072. __raw_writel(0x7, &priv->regs->stat_port_en);
  1073. /* Enable internal fifo flow control */
  1074. writel(0x7, &priv->regs->flow_control);
  1075. if (WARN_ON(!priv->data.rx_descs))
  1076. priv->data.rx_descs = 128;
  1077. for (i = 0; i < priv->data.rx_descs; i++) {
  1078. struct sk_buff *skb;
  1079. ret = -ENOMEM;
  1080. skb = __netdev_alloc_skb_ip_align(priv->ndev,
  1081. priv->rx_packet_max, GFP_KERNEL);
  1082. if (!skb)
  1083. goto err_cleanup;
  1084. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  1085. skb_tailroom(skb), 0);
  1086. if (ret < 0) {
  1087. kfree_skb(skb);
  1088. goto err_cleanup;
  1089. }
  1090. }
  1091. /* continue even if we didn't manage to submit all
  1092. * receive descs
  1093. */
  1094. cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
  1095. if (cpts_register(&priv->pdev->dev, priv->cpts,
  1096. priv->data.cpts_clock_mult,
  1097. priv->data.cpts_clock_shift))
  1098. dev_err(priv->dev, "error registering cpts device\n");
  1099. }
  1100. /* Enable Interrupt pacing if configured */
  1101. if (priv->coal_intvl != 0) {
  1102. struct ethtool_coalesce coal;
  1103. coal.rx_coalesce_usecs = (priv->coal_intvl << 4);
  1104. cpsw_set_coalesce(ndev, &coal);
  1105. }
  1106. napi_enable(&priv->napi);
  1107. cpdma_ctlr_start(priv->dma);
  1108. cpsw_intr_enable(priv);
  1109. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  1110. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  1111. prim_cpsw = cpsw_get_slave_priv(priv, 0);
  1112. if (prim_cpsw->irq_enabled == false) {
  1113. if ((priv == prim_cpsw) || !netif_running(prim_cpsw->ndev)) {
  1114. prim_cpsw->irq_enabled = true;
  1115. cpsw_enable_irq(prim_cpsw);
  1116. }
  1117. }
  1118. if (priv->data.dual_emac)
  1119. priv->slaves[priv->emac_port].open_stat = true;
  1120. return 0;
  1121. err_cleanup:
  1122. cpdma_ctlr_stop(priv->dma);
  1123. for_each_slave(priv, cpsw_slave_stop, priv);
  1124. pm_runtime_put_sync(&priv->pdev->dev);
  1125. netif_carrier_off(priv->ndev);
  1126. return ret;
  1127. }
  1128. static int cpsw_ndo_stop(struct net_device *ndev)
  1129. {
  1130. struct cpsw_priv *priv = netdev_priv(ndev);
  1131. cpsw_info(priv, ifdown, "shutting down cpsw device\n");
  1132. netif_stop_queue(priv->ndev);
  1133. napi_disable(&priv->napi);
  1134. netif_carrier_off(priv->ndev);
  1135. if (cpsw_common_res_usage_state(priv) <= 1) {
  1136. cpts_unregister(priv->cpts);
  1137. cpsw_intr_disable(priv);
  1138. cpdma_ctlr_int_ctrl(priv->dma, false);
  1139. cpdma_ctlr_stop(priv->dma);
  1140. cpsw_ale_stop(priv->ale);
  1141. }
  1142. for_each_slave(priv, cpsw_slave_stop, priv);
  1143. pm_runtime_put_sync(&priv->pdev->dev);
  1144. if (priv->data.dual_emac)
  1145. priv->slaves[priv->emac_port].open_stat = false;
  1146. return 0;
  1147. }
  1148. static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
  1149. struct net_device *ndev)
  1150. {
  1151. struct cpsw_priv *priv = netdev_priv(ndev);
  1152. int ret;
  1153. ndev->trans_start = jiffies;
  1154. if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
  1155. cpsw_err(priv, tx_err, "packet pad failed\n");
  1156. ndev->stats.tx_dropped++;
  1157. return NETDEV_TX_OK;
  1158. }
  1159. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
  1160. priv->cpts->tx_enable)
  1161. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1162. skb_tx_timestamp(skb);
  1163. ret = cpsw_tx_packet_submit(ndev, priv, skb);
  1164. if (unlikely(ret != 0)) {
  1165. cpsw_err(priv, tx_err, "desc submit failed\n");
  1166. goto fail;
  1167. }
  1168. /* If there is no more tx desc left free then we need to
  1169. * tell the kernel to stop sending us tx frames.
  1170. */
  1171. if (unlikely(!cpdma_check_free_tx_desc(priv->txch)))
  1172. netif_stop_queue(ndev);
  1173. return NETDEV_TX_OK;
  1174. fail:
  1175. ndev->stats.tx_dropped++;
  1176. netif_stop_queue(ndev);
  1177. return NETDEV_TX_BUSY;
  1178. }
  1179. #ifdef CONFIG_TI_CPTS
  1180. static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
  1181. {
  1182. struct cpsw_slave *slave = &priv->slaves[priv->data.active_slave];
  1183. u32 ts_en, seq_id;
  1184. if (!priv->cpts->tx_enable && !priv->cpts->rx_enable) {
  1185. slave_write(slave, 0, CPSW1_TS_CTL);
  1186. return;
  1187. }
  1188. seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
  1189. ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
  1190. if (priv->cpts->tx_enable)
  1191. ts_en |= CPSW_V1_TS_TX_EN;
  1192. if (priv->cpts->rx_enable)
  1193. ts_en |= CPSW_V1_TS_RX_EN;
  1194. slave_write(slave, ts_en, CPSW1_TS_CTL);
  1195. slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
  1196. }
  1197. static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
  1198. {
  1199. struct cpsw_slave *slave;
  1200. u32 ctrl, mtype;
  1201. if (priv->data.dual_emac)
  1202. slave = &priv->slaves[priv->emac_port];
  1203. else
  1204. slave = &priv->slaves[priv->data.active_slave];
  1205. ctrl = slave_read(slave, CPSW2_CONTROL);
  1206. switch (priv->version) {
  1207. case CPSW_VERSION_2:
  1208. ctrl &= ~CTRL_V2_ALL_TS_MASK;
  1209. if (priv->cpts->tx_enable)
  1210. ctrl |= CTRL_V2_TX_TS_BITS;
  1211. if (priv->cpts->rx_enable)
  1212. ctrl |= CTRL_V2_RX_TS_BITS;
  1213. break;
  1214. case CPSW_VERSION_3:
  1215. default:
  1216. ctrl &= ~CTRL_V3_ALL_TS_MASK;
  1217. if (priv->cpts->tx_enable)
  1218. ctrl |= CTRL_V3_TX_TS_BITS;
  1219. if (priv->cpts->rx_enable)
  1220. ctrl |= CTRL_V3_RX_TS_BITS;
  1221. break;
  1222. }
  1223. mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
  1224. slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
  1225. slave_write(slave, ctrl, CPSW2_CONTROL);
  1226. __raw_writel(ETH_P_1588, &priv->regs->ts_ltype);
  1227. }
  1228. static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  1229. {
  1230. struct cpsw_priv *priv = netdev_priv(dev);
  1231. struct cpts *cpts = priv->cpts;
  1232. struct hwtstamp_config cfg;
  1233. if (priv->version != CPSW_VERSION_1 &&
  1234. priv->version != CPSW_VERSION_2 &&
  1235. priv->version != CPSW_VERSION_3)
  1236. return -EOPNOTSUPP;
  1237. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  1238. return -EFAULT;
  1239. /* reserved for future extensions */
  1240. if (cfg.flags)
  1241. return -EINVAL;
  1242. if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
  1243. return -ERANGE;
  1244. switch (cfg.rx_filter) {
  1245. case HWTSTAMP_FILTER_NONE:
  1246. cpts->rx_enable = 0;
  1247. break;
  1248. case HWTSTAMP_FILTER_ALL:
  1249. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1250. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1251. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1252. return -ERANGE;
  1253. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1254. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1255. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1256. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1257. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1258. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1259. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1260. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1261. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1262. cpts->rx_enable = 1;
  1263. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  1264. break;
  1265. default:
  1266. return -ERANGE;
  1267. }
  1268. cpts->tx_enable = cfg.tx_type == HWTSTAMP_TX_ON;
  1269. switch (priv->version) {
  1270. case CPSW_VERSION_1:
  1271. cpsw_hwtstamp_v1(priv);
  1272. break;
  1273. case CPSW_VERSION_2:
  1274. case CPSW_VERSION_3:
  1275. cpsw_hwtstamp_v2(priv);
  1276. break;
  1277. default:
  1278. WARN_ON(1);
  1279. }
  1280. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1281. }
  1282. static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  1283. {
  1284. struct cpsw_priv *priv = netdev_priv(dev);
  1285. struct cpts *cpts = priv->cpts;
  1286. struct hwtstamp_config cfg;
  1287. if (priv->version != CPSW_VERSION_1 &&
  1288. priv->version != CPSW_VERSION_2 &&
  1289. priv->version != CPSW_VERSION_3)
  1290. return -EOPNOTSUPP;
  1291. cfg.flags = 0;
  1292. cfg.tx_type = cpts->tx_enable ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  1293. cfg.rx_filter = (cpts->rx_enable ?
  1294. HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE);
  1295. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1296. }
  1297. #endif /*CONFIG_TI_CPTS*/
  1298. static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  1299. {
  1300. struct cpsw_priv *priv = netdev_priv(dev);
  1301. int slave_no = cpsw_slave_index(priv);
  1302. if (!netif_running(dev))
  1303. return -EINVAL;
  1304. switch (cmd) {
  1305. #ifdef CONFIG_TI_CPTS
  1306. case SIOCSHWTSTAMP:
  1307. return cpsw_hwtstamp_set(dev, req);
  1308. case SIOCGHWTSTAMP:
  1309. return cpsw_hwtstamp_get(dev, req);
  1310. #endif
  1311. }
  1312. if (!priv->slaves[slave_no].phy)
  1313. return -EOPNOTSUPP;
  1314. return phy_mii_ioctl(priv->slaves[slave_no].phy, req, cmd);
  1315. }
  1316. static void cpsw_ndo_tx_timeout(struct net_device *ndev)
  1317. {
  1318. struct cpsw_priv *priv = netdev_priv(ndev);
  1319. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  1320. ndev->stats.tx_errors++;
  1321. cpsw_intr_disable(priv);
  1322. cpdma_ctlr_int_ctrl(priv->dma, false);
  1323. cpdma_chan_stop(priv->txch);
  1324. cpdma_chan_start(priv->txch);
  1325. cpdma_ctlr_int_ctrl(priv->dma, true);
  1326. cpsw_intr_enable(priv);
  1327. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  1328. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  1329. }
  1330. static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
  1331. {
  1332. struct cpsw_priv *priv = netdev_priv(ndev);
  1333. struct sockaddr *addr = (struct sockaddr *)p;
  1334. int flags = 0;
  1335. u16 vid = 0;
  1336. if (!is_valid_ether_addr(addr->sa_data))
  1337. return -EADDRNOTAVAIL;
  1338. if (priv->data.dual_emac) {
  1339. vid = priv->slaves[priv->emac_port].port_vlan;
  1340. flags = ALE_VLAN;
  1341. }
  1342. cpsw_ale_del_ucast(priv->ale, priv->mac_addr, priv->host_port,
  1343. flags, vid);
  1344. cpsw_ale_add_ucast(priv->ale, addr->sa_data, priv->host_port,
  1345. flags, vid);
  1346. memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
  1347. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1348. for_each_slave(priv, cpsw_set_slave_mac, priv);
  1349. return 0;
  1350. }
  1351. #ifdef CONFIG_NET_POLL_CONTROLLER
  1352. static void cpsw_ndo_poll_controller(struct net_device *ndev)
  1353. {
  1354. struct cpsw_priv *priv = netdev_priv(ndev);
  1355. cpsw_intr_disable(priv);
  1356. cpdma_ctlr_int_ctrl(priv->dma, false);
  1357. cpsw_interrupt(ndev->irq, priv);
  1358. cpdma_ctlr_int_ctrl(priv->dma, true);
  1359. cpsw_intr_enable(priv);
  1360. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  1361. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  1362. }
  1363. #endif
  1364. static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
  1365. unsigned short vid)
  1366. {
  1367. int ret;
  1368. ret = cpsw_ale_add_vlan(priv->ale, vid,
  1369. ALE_ALL_PORTS << priv->host_port,
  1370. 0, ALE_ALL_PORTS << priv->host_port,
  1371. (ALE_PORT_1 | ALE_PORT_2) << priv->host_port);
  1372. if (ret != 0)
  1373. return ret;
  1374. ret = cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
  1375. priv->host_port, ALE_VLAN, vid);
  1376. if (ret != 0)
  1377. goto clean_vid;
  1378. ret = cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  1379. ALE_ALL_PORTS << priv->host_port,
  1380. ALE_VLAN, vid, 0);
  1381. if (ret != 0)
  1382. goto clean_vlan_ucast;
  1383. return 0;
  1384. clean_vlan_ucast:
  1385. cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
  1386. priv->host_port, ALE_VLAN, vid);
  1387. clean_vid:
  1388. cpsw_ale_del_vlan(priv->ale, vid, 0);
  1389. return ret;
  1390. }
  1391. static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
  1392. __be16 proto, u16 vid)
  1393. {
  1394. struct cpsw_priv *priv = netdev_priv(ndev);
  1395. if (vid == priv->data.default_vlan)
  1396. return 0;
  1397. dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
  1398. return cpsw_add_vlan_ale_entry(priv, vid);
  1399. }
  1400. static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
  1401. __be16 proto, u16 vid)
  1402. {
  1403. struct cpsw_priv *priv = netdev_priv(ndev);
  1404. int ret;
  1405. if (vid == priv->data.default_vlan)
  1406. return 0;
  1407. dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
  1408. ret = cpsw_ale_del_vlan(priv->ale, vid, 0);
  1409. if (ret != 0)
  1410. return ret;
  1411. ret = cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
  1412. priv->host_port, ALE_VLAN, vid);
  1413. if (ret != 0)
  1414. return ret;
  1415. return cpsw_ale_del_mcast(priv->ale, priv->ndev->broadcast,
  1416. 0, ALE_VLAN, vid);
  1417. }
  1418. static const struct net_device_ops cpsw_netdev_ops = {
  1419. .ndo_open = cpsw_ndo_open,
  1420. .ndo_stop = cpsw_ndo_stop,
  1421. .ndo_start_xmit = cpsw_ndo_start_xmit,
  1422. .ndo_set_mac_address = cpsw_ndo_set_mac_address,
  1423. .ndo_do_ioctl = cpsw_ndo_ioctl,
  1424. .ndo_validate_addr = eth_validate_addr,
  1425. .ndo_change_mtu = eth_change_mtu,
  1426. .ndo_tx_timeout = cpsw_ndo_tx_timeout,
  1427. .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
  1428. #ifdef CONFIG_NET_POLL_CONTROLLER
  1429. .ndo_poll_controller = cpsw_ndo_poll_controller,
  1430. #endif
  1431. .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
  1432. .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
  1433. };
  1434. static int cpsw_get_regs_len(struct net_device *ndev)
  1435. {
  1436. struct cpsw_priv *priv = netdev_priv(ndev);
  1437. return priv->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
  1438. }
  1439. static void cpsw_get_regs(struct net_device *ndev,
  1440. struct ethtool_regs *regs, void *p)
  1441. {
  1442. struct cpsw_priv *priv = netdev_priv(ndev);
  1443. u32 *reg = p;
  1444. /* update CPSW IP version */
  1445. regs->version = priv->version;
  1446. cpsw_ale_dump(priv->ale, reg);
  1447. }
  1448. static void cpsw_get_drvinfo(struct net_device *ndev,
  1449. struct ethtool_drvinfo *info)
  1450. {
  1451. struct cpsw_priv *priv = netdev_priv(ndev);
  1452. strlcpy(info->driver, "cpsw", sizeof(info->driver));
  1453. strlcpy(info->version, "1.0", sizeof(info->version));
  1454. strlcpy(info->bus_info, priv->pdev->name, sizeof(info->bus_info));
  1455. info->regdump_len = cpsw_get_regs_len(ndev);
  1456. }
  1457. static u32 cpsw_get_msglevel(struct net_device *ndev)
  1458. {
  1459. struct cpsw_priv *priv = netdev_priv(ndev);
  1460. return priv->msg_enable;
  1461. }
  1462. static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  1463. {
  1464. struct cpsw_priv *priv = netdev_priv(ndev);
  1465. priv->msg_enable = value;
  1466. }
  1467. static int cpsw_get_ts_info(struct net_device *ndev,
  1468. struct ethtool_ts_info *info)
  1469. {
  1470. #ifdef CONFIG_TI_CPTS
  1471. struct cpsw_priv *priv = netdev_priv(ndev);
  1472. info->so_timestamping =
  1473. SOF_TIMESTAMPING_TX_HARDWARE |
  1474. SOF_TIMESTAMPING_TX_SOFTWARE |
  1475. SOF_TIMESTAMPING_RX_HARDWARE |
  1476. SOF_TIMESTAMPING_RX_SOFTWARE |
  1477. SOF_TIMESTAMPING_SOFTWARE |
  1478. SOF_TIMESTAMPING_RAW_HARDWARE;
  1479. info->phc_index = priv->cpts->phc_index;
  1480. info->tx_types =
  1481. (1 << HWTSTAMP_TX_OFF) |
  1482. (1 << HWTSTAMP_TX_ON);
  1483. info->rx_filters =
  1484. (1 << HWTSTAMP_FILTER_NONE) |
  1485. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  1486. #else
  1487. info->so_timestamping =
  1488. SOF_TIMESTAMPING_TX_SOFTWARE |
  1489. SOF_TIMESTAMPING_RX_SOFTWARE |
  1490. SOF_TIMESTAMPING_SOFTWARE;
  1491. info->phc_index = -1;
  1492. info->tx_types = 0;
  1493. info->rx_filters = 0;
  1494. #endif
  1495. return 0;
  1496. }
  1497. static int cpsw_get_settings(struct net_device *ndev,
  1498. struct ethtool_cmd *ecmd)
  1499. {
  1500. struct cpsw_priv *priv = netdev_priv(ndev);
  1501. int slave_no = cpsw_slave_index(priv);
  1502. if (priv->slaves[slave_no].phy)
  1503. return phy_ethtool_gset(priv->slaves[slave_no].phy, ecmd);
  1504. else
  1505. return -EOPNOTSUPP;
  1506. }
  1507. static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
  1508. {
  1509. struct cpsw_priv *priv = netdev_priv(ndev);
  1510. int slave_no = cpsw_slave_index(priv);
  1511. if (priv->slaves[slave_no].phy)
  1512. return phy_ethtool_sset(priv->slaves[slave_no].phy, ecmd);
  1513. else
  1514. return -EOPNOTSUPP;
  1515. }
  1516. static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1517. {
  1518. struct cpsw_priv *priv = netdev_priv(ndev);
  1519. int slave_no = cpsw_slave_index(priv);
  1520. wol->supported = 0;
  1521. wol->wolopts = 0;
  1522. if (priv->slaves[slave_no].phy)
  1523. phy_ethtool_get_wol(priv->slaves[slave_no].phy, wol);
  1524. }
  1525. static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1526. {
  1527. struct cpsw_priv *priv = netdev_priv(ndev);
  1528. int slave_no = cpsw_slave_index(priv);
  1529. if (priv->slaves[slave_no].phy)
  1530. return phy_ethtool_set_wol(priv->slaves[slave_no].phy, wol);
  1531. else
  1532. return -EOPNOTSUPP;
  1533. }
  1534. static void cpsw_get_pauseparam(struct net_device *ndev,
  1535. struct ethtool_pauseparam *pause)
  1536. {
  1537. struct cpsw_priv *priv = netdev_priv(ndev);
  1538. pause->autoneg = AUTONEG_DISABLE;
  1539. pause->rx_pause = priv->rx_pause ? true : false;
  1540. pause->tx_pause = priv->tx_pause ? true : false;
  1541. }
  1542. static int cpsw_set_pauseparam(struct net_device *ndev,
  1543. struct ethtool_pauseparam *pause)
  1544. {
  1545. struct cpsw_priv *priv = netdev_priv(ndev);
  1546. bool link;
  1547. priv->rx_pause = pause->rx_pause ? true : false;
  1548. priv->tx_pause = pause->tx_pause ? true : false;
  1549. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  1550. return 0;
  1551. }
  1552. static const struct ethtool_ops cpsw_ethtool_ops = {
  1553. .get_drvinfo = cpsw_get_drvinfo,
  1554. .get_msglevel = cpsw_get_msglevel,
  1555. .set_msglevel = cpsw_set_msglevel,
  1556. .get_link = ethtool_op_get_link,
  1557. .get_ts_info = cpsw_get_ts_info,
  1558. .get_settings = cpsw_get_settings,
  1559. .set_settings = cpsw_set_settings,
  1560. .get_coalesce = cpsw_get_coalesce,
  1561. .set_coalesce = cpsw_set_coalesce,
  1562. .get_sset_count = cpsw_get_sset_count,
  1563. .get_strings = cpsw_get_strings,
  1564. .get_ethtool_stats = cpsw_get_ethtool_stats,
  1565. .get_pauseparam = cpsw_get_pauseparam,
  1566. .set_pauseparam = cpsw_set_pauseparam,
  1567. .get_wol = cpsw_get_wol,
  1568. .set_wol = cpsw_set_wol,
  1569. .get_regs_len = cpsw_get_regs_len,
  1570. .get_regs = cpsw_get_regs,
  1571. };
  1572. static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
  1573. u32 slave_reg_ofs, u32 sliver_reg_ofs)
  1574. {
  1575. void __iomem *regs = priv->regs;
  1576. int slave_num = slave->slave_num;
  1577. struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
  1578. slave->data = data;
  1579. slave->regs = regs + slave_reg_ofs;
  1580. slave->sliver = regs + sliver_reg_ofs;
  1581. slave->port_vlan = data->dual_emac_res_vlan;
  1582. }
  1583. #define AM33XX_CTRL_MAC_LO_REG(id) (0x630 + 0x8 * id)
  1584. #define AM33XX_CTRL_MAC_HI_REG(id) (0x630 + 0x8 * id + 0x4)
  1585. static int cpsw_am33xx_cm_get_macid(struct device *dev, int slave,
  1586. u8 *mac_addr)
  1587. {
  1588. u32 macid_lo;
  1589. u32 macid_hi;
  1590. struct regmap *syscon;
  1591. syscon = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
  1592. if (IS_ERR(syscon)) {
  1593. if (PTR_ERR(syscon) == -ENODEV)
  1594. return 0;
  1595. return PTR_ERR(syscon);
  1596. }
  1597. regmap_read(syscon, AM33XX_CTRL_MAC_LO_REG(slave), &macid_lo);
  1598. regmap_read(syscon, AM33XX_CTRL_MAC_HI_REG(slave), &macid_hi);
  1599. mac_addr[5] = (macid_lo >> 8) & 0xff;
  1600. mac_addr[4] = macid_lo & 0xff;
  1601. mac_addr[3] = (macid_hi >> 24) & 0xff;
  1602. mac_addr[2] = (macid_hi >> 16) & 0xff;
  1603. mac_addr[1] = (macid_hi >> 8) & 0xff;
  1604. mac_addr[0] = macid_hi & 0xff;
  1605. return 0;
  1606. }
  1607. static int cpsw_probe_dt(struct cpsw_platform_data *data,
  1608. struct platform_device *pdev)
  1609. {
  1610. struct device_node *node = pdev->dev.of_node;
  1611. struct device_node *slave_node;
  1612. int i = 0, ret;
  1613. u32 prop;
  1614. if (!node)
  1615. return -EINVAL;
  1616. if (of_property_read_u32(node, "slaves", &prop)) {
  1617. dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
  1618. return -EINVAL;
  1619. }
  1620. data->slaves = prop;
  1621. if (of_property_read_u32(node, "active_slave", &prop)) {
  1622. dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
  1623. return -EINVAL;
  1624. }
  1625. data->active_slave = prop;
  1626. if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
  1627. dev_err(&pdev->dev, "Missing cpts_clock_mult property in the DT.\n");
  1628. return -EINVAL;
  1629. }
  1630. data->cpts_clock_mult = prop;
  1631. if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
  1632. dev_err(&pdev->dev, "Missing cpts_clock_shift property in the DT.\n");
  1633. return -EINVAL;
  1634. }
  1635. data->cpts_clock_shift = prop;
  1636. data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
  1637. * sizeof(struct cpsw_slave_data),
  1638. GFP_KERNEL);
  1639. if (!data->slave_data)
  1640. return -ENOMEM;
  1641. if (of_property_read_u32(node, "cpdma_channels", &prop)) {
  1642. dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
  1643. return -EINVAL;
  1644. }
  1645. data->channels = prop;
  1646. if (of_property_read_u32(node, "ale_entries", &prop)) {
  1647. dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
  1648. return -EINVAL;
  1649. }
  1650. data->ale_entries = prop;
  1651. if (of_property_read_u32(node, "bd_ram_size", &prop)) {
  1652. dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
  1653. return -EINVAL;
  1654. }
  1655. data->bd_ram_size = prop;
  1656. if (of_property_read_u32(node, "rx_descs", &prop)) {
  1657. dev_err(&pdev->dev, "Missing rx_descs property in the DT.\n");
  1658. return -EINVAL;
  1659. }
  1660. data->rx_descs = prop;
  1661. if (of_property_read_u32(node, "mac_control", &prop)) {
  1662. dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
  1663. return -EINVAL;
  1664. }
  1665. data->mac_control = prop;
  1666. if (of_property_read_bool(node, "dual_emac"))
  1667. data->dual_emac = 1;
  1668. /*
  1669. * Populate all the child nodes here...
  1670. */
  1671. ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
  1672. /* We do not want to force this, as in some cases may not have child */
  1673. if (ret)
  1674. dev_warn(&pdev->dev, "Doesn't have any child node\n");
  1675. for_each_child_of_node(node, slave_node) {
  1676. struct cpsw_slave_data *slave_data = data->slave_data + i;
  1677. const void *mac_addr = NULL;
  1678. u32 phyid;
  1679. int lenp;
  1680. const __be32 *parp;
  1681. struct device_node *mdio_node;
  1682. struct platform_device *mdio;
  1683. /* This is no slave child node, continue */
  1684. if (strcmp(slave_node->name, "slave"))
  1685. continue;
  1686. parp = of_get_property(slave_node, "phy_id", &lenp);
  1687. if ((parp == NULL) || (lenp != (sizeof(void *) * 2))) {
  1688. dev_err(&pdev->dev, "Missing slave[%d] phy_id property\n", i);
  1689. return -EINVAL;
  1690. }
  1691. mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
  1692. phyid = be32_to_cpup(parp+1);
  1693. mdio = of_find_device_by_node(mdio_node);
  1694. of_node_put(mdio_node);
  1695. if (!mdio) {
  1696. dev_err(&pdev->dev, "Missing mdio platform device\n");
  1697. return -EINVAL;
  1698. }
  1699. snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
  1700. PHY_ID_FMT, mdio->name, phyid);
  1701. mac_addr = of_get_mac_address(slave_node);
  1702. if (mac_addr) {
  1703. memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
  1704. } else {
  1705. if (of_machine_is_compatible("ti,am33xx")) {
  1706. ret = cpsw_am33xx_cm_get_macid(&pdev->dev, i,
  1707. slave_data->mac_addr);
  1708. if (ret)
  1709. return ret;
  1710. }
  1711. }
  1712. slave_data->phy_if = of_get_phy_mode(slave_node);
  1713. if (slave_data->phy_if < 0) {
  1714. dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
  1715. i);
  1716. return slave_data->phy_if;
  1717. }
  1718. if (data->dual_emac) {
  1719. if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
  1720. &prop)) {
  1721. dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
  1722. slave_data->dual_emac_res_vlan = i+1;
  1723. dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
  1724. slave_data->dual_emac_res_vlan, i);
  1725. } else {
  1726. slave_data->dual_emac_res_vlan = prop;
  1727. }
  1728. }
  1729. i++;
  1730. if (i == data->slaves)
  1731. break;
  1732. }
  1733. return 0;
  1734. }
  1735. static int cpsw_probe_dual_emac(struct platform_device *pdev,
  1736. struct cpsw_priv *priv)
  1737. {
  1738. struct cpsw_platform_data *data = &priv->data;
  1739. struct net_device *ndev;
  1740. struct cpsw_priv *priv_sl2;
  1741. int ret = 0, i;
  1742. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  1743. if (!ndev) {
  1744. dev_err(&pdev->dev, "cpsw: error allocating net_device\n");
  1745. return -ENOMEM;
  1746. }
  1747. priv_sl2 = netdev_priv(ndev);
  1748. spin_lock_init(&priv_sl2->lock);
  1749. priv_sl2->data = *data;
  1750. priv_sl2->pdev = pdev;
  1751. priv_sl2->ndev = ndev;
  1752. priv_sl2->dev = &ndev->dev;
  1753. priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1754. priv_sl2->rx_packet_max = max(rx_packet_max, 128);
  1755. if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
  1756. memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
  1757. ETH_ALEN);
  1758. dev_info(&pdev->dev, "cpsw: Detected MACID = %pM\n", priv_sl2->mac_addr);
  1759. } else {
  1760. random_ether_addr(priv_sl2->mac_addr);
  1761. dev_info(&pdev->dev, "cpsw: Random MACID = %pM\n", priv_sl2->mac_addr);
  1762. }
  1763. memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
  1764. priv_sl2->slaves = priv->slaves;
  1765. priv_sl2->clk = priv->clk;
  1766. priv_sl2->coal_intvl = 0;
  1767. priv_sl2->bus_freq_mhz = priv->bus_freq_mhz;
  1768. priv_sl2->regs = priv->regs;
  1769. priv_sl2->host_port = priv->host_port;
  1770. priv_sl2->host_port_regs = priv->host_port_regs;
  1771. priv_sl2->wr_regs = priv->wr_regs;
  1772. priv_sl2->hw_stats = priv->hw_stats;
  1773. priv_sl2->dma = priv->dma;
  1774. priv_sl2->txch = priv->txch;
  1775. priv_sl2->rxch = priv->rxch;
  1776. priv_sl2->ale = priv->ale;
  1777. priv_sl2->emac_port = 1;
  1778. priv->slaves[1].ndev = ndev;
  1779. priv_sl2->cpts = priv->cpts;
  1780. priv_sl2->version = priv->version;
  1781. for (i = 0; i < priv->num_irqs; i++) {
  1782. priv_sl2->irqs_table[i] = priv->irqs_table[i];
  1783. priv_sl2->num_irqs = priv->num_irqs;
  1784. }
  1785. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1786. ndev->netdev_ops = &cpsw_netdev_ops;
  1787. ndev->ethtool_ops = &cpsw_ethtool_ops;
  1788. netif_napi_add(ndev, &priv_sl2->napi, cpsw_poll, CPSW_POLL_WEIGHT);
  1789. /* register the network device */
  1790. SET_NETDEV_DEV(ndev, &pdev->dev);
  1791. ret = register_netdev(ndev);
  1792. if (ret) {
  1793. dev_err(&pdev->dev, "cpsw: error registering net device\n");
  1794. free_netdev(ndev);
  1795. ret = -ENODEV;
  1796. }
  1797. return ret;
  1798. }
  1799. static int cpsw_probe(struct platform_device *pdev)
  1800. {
  1801. struct cpsw_platform_data *data;
  1802. struct net_device *ndev;
  1803. struct cpsw_priv *priv;
  1804. struct cpdma_params dma_params;
  1805. struct cpsw_ale_params ale_params;
  1806. void __iomem *ss_regs;
  1807. struct resource *res, *ss_res;
  1808. u32 slave_offset, sliver_offset, slave_size;
  1809. int ret = 0, i, k = 0;
  1810. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  1811. if (!ndev) {
  1812. dev_err(&pdev->dev, "error allocating net_device\n");
  1813. return -ENOMEM;
  1814. }
  1815. platform_set_drvdata(pdev, ndev);
  1816. priv = netdev_priv(ndev);
  1817. spin_lock_init(&priv->lock);
  1818. priv->pdev = pdev;
  1819. priv->ndev = ndev;
  1820. priv->dev = &ndev->dev;
  1821. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1822. priv->rx_packet_max = max(rx_packet_max, 128);
  1823. priv->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
  1824. priv->irq_enabled = true;
  1825. if (!priv->cpts) {
  1826. dev_err(&pdev->dev, "error allocating cpts\n");
  1827. ret = -ENOMEM;
  1828. goto clean_ndev_ret;
  1829. }
  1830. /*
  1831. * This may be required here for child devices.
  1832. */
  1833. pm_runtime_enable(&pdev->dev);
  1834. /* Select default pin state */
  1835. pinctrl_pm_select_default_state(&pdev->dev);
  1836. if (cpsw_probe_dt(&priv->data, pdev)) {
  1837. dev_err(&pdev->dev, "cpsw: platform data missing\n");
  1838. ret = -ENODEV;
  1839. goto clean_runtime_disable_ret;
  1840. }
  1841. data = &priv->data;
  1842. if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
  1843. memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
  1844. dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
  1845. } else {
  1846. eth_random_addr(priv->mac_addr);
  1847. dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
  1848. }
  1849. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1850. priv->slaves = devm_kzalloc(&pdev->dev,
  1851. sizeof(struct cpsw_slave) * data->slaves,
  1852. GFP_KERNEL);
  1853. if (!priv->slaves) {
  1854. ret = -ENOMEM;
  1855. goto clean_runtime_disable_ret;
  1856. }
  1857. for (i = 0; i < data->slaves; i++)
  1858. priv->slaves[i].slave_num = i;
  1859. priv->slaves[0].ndev = ndev;
  1860. priv->emac_port = 0;
  1861. priv->clk = devm_clk_get(&pdev->dev, "fck");
  1862. if (IS_ERR(priv->clk)) {
  1863. dev_err(priv->dev, "fck is not found\n");
  1864. ret = -ENODEV;
  1865. goto clean_runtime_disable_ret;
  1866. }
  1867. priv->coal_intvl = 0;
  1868. priv->bus_freq_mhz = clk_get_rate(priv->clk) / 1000000;
  1869. ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1870. ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
  1871. if (IS_ERR(ss_regs)) {
  1872. ret = PTR_ERR(ss_regs);
  1873. goto clean_runtime_disable_ret;
  1874. }
  1875. priv->regs = ss_regs;
  1876. priv->host_port = HOST_PORT_NUM;
  1877. /* Need to enable clocks with runtime PM api to access module
  1878. * registers
  1879. */
  1880. pm_runtime_get_sync(&pdev->dev);
  1881. priv->version = readl(&priv->regs->id_ver);
  1882. pm_runtime_put_sync(&pdev->dev);
  1883. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1884. priv->wr_regs = devm_ioremap_resource(&pdev->dev, res);
  1885. if (IS_ERR(priv->wr_regs)) {
  1886. ret = PTR_ERR(priv->wr_regs);
  1887. goto clean_runtime_disable_ret;
  1888. }
  1889. memset(&dma_params, 0, sizeof(dma_params));
  1890. memset(&ale_params, 0, sizeof(ale_params));
  1891. switch (priv->version) {
  1892. case CPSW_VERSION_1:
  1893. priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  1894. priv->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
  1895. priv->hw_stats = ss_regs + CPSW1_HW_STATS;
  1896. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  1897. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  1898. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  1899. slave_offset = CPSW1_SLAVE_OFFSET;
  1900. slave_size = CPSW1_SLAVE_SIZE;
  1901. sliver_offset = CPSW1_SLIVER_OFFSET;
  1902. dma_params.desc_mem_phys = 0;
  1903. break;
  1904. case CPSW_VERSION_2:
  1905. case CPSW_VERSION_3:
  1906. case CPSW_VERSION_4:
  1907. priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  1908. priv->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
  1909. priv->hw_stats = ss_regs + CPSW2_HW_STATS;
  1910. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  1911. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  1912. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  1913. slave_offset = CPSW2_SLAVE_OFFSET;
  1914. slave_size = CPSW2_SLAVE_SIZE;
  1915. sliver_offset = CPSW2_SLIVER_OFFSET;
  1916. dma_params.desc_mem_phys =
  1917. (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
  1918. break;
  1919. default:
  1920. dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
  1921. ret = -ENODEV;
  1922. goto clean_runtime_disable_ret;
  1923. }
  1924. for (i = 0; i < priv->data.slaves; i++) {
  1925. struct cpsw_slave *slave = &priv->slaves[i];
  1926. cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
  1927. slave_offset += slave_size;
  1928. sliver_offset += SLIVER_SIZE;
  1929. }
  1930. dma_params.dev = &pdev->dev;
  1931. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  1932. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  1933. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  1934. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  1935. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  1936. dma_params.num_chan = data->channels;
  1937. dma_params.has_soft_reset = true;
  1938. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  1939. dma_params.desc_mem_size = data->bd_ram_size;
  1940. dma_params.desc_align = 16;
  1941. dma_params.has_ext_regs = true;
  1942. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  1943. priv->dma = cpdma_ctlr_create(&dma_params);
  1944. if (!priv->dma) {
  1945. dev_err(priv->dev, "error initializing dma\n");
  1946. ret = -ENOMEM;
  1947. goto clean_runtime_disable_ret;
  1948. }
  1949. priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
  1950. cpsw_tx_handler);
  1951. priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
  1952. cpsw_rx_handler);
  1953. if (WARN_ON(!priv->txch || !priv->rxch)) {
  1954. dev_err(priv->dev, "error initializing dma channels\n");
  1955. ret = -ENOMEM;
  1956. goto clean_dma_ret;
  1957. }
  1958. ale_params.dev = &ndev->dev;
  1959. ale_params.ale_ageout = ale_ageout;
  1960. ale_params.ale_entries = data->ale_entries;
  1961. ale_params.ale_ports = data->slaves;
  1962. priv->ale = cpsw_ale_create(&ale_params);
  1963. if (!priv->ale) {
  1964. dev_err(priv->dev, "error initializing ale engine\n");
  1965. ret = -ENODEV;
  1966. goto clean_dma_ret;
  1967. }
  1968. ndev->irq = platform_get_irq(pdev, 0);
  1969. if (ndev->irq < 0) {
  1970. dev_err(priv->dev, "error getting irq resource\n");
  1971. ret = -ENOENT;
  1972. goto clean_ale_ret;
  1973. }
  1974. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
  1975. if (k >= ARRAY_SIZE(priv->irqs_table)) {
  1976. ret = -EINVAL;
  1977. goto clean_ale_ret;
  1978. }
  1979. ret = devm_request_irq(&pdev->dev, res->start, cpsw_interrupt,
  1980. 0, dev_name(&pdev->dev), priv);
  1981. if (ret < 0) {
  1982. dev_err(priv->dev, "error attaching irq (%d)\n", ret);
  1983. goto clean_ale_ret;
  1984. }
  1985. priv->irqs_table[k] = res->start;
  1986. k++;
  1987. }
  1988. priv->num_irqs = k;
  1989. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1990. ndev->netdev_ops = &cpsw_netdev_ops;
  1991. ndev->ethtool_ops = &cpsw_ethtool_ops;
  1992. netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
  1993. /* register the network device */
  1994. SET_NETDEV_DEV(ndev, &pdev->dev);
  1995. ret = register_netdev(ndev);
  1996. if (ret) {
  1997. dev_err(priv->dev, "error registering net device\n");
  1998. ret = -ENODEV;
  1999. goto clean_ale_ret;
  2000. }
  2001. cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n",
  2002. &ss_res->start, ndev->irq);
  2003. if (priv->data.dual_emac) {
  2004. ret = cpsw_probe_dual_emac(pdev, priv);
  2005. if (ret) {
  2006. cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
  2007. goto clean_ale_ret;
  2008. }
  2009. }
  2010. return 0;
  2011. clean_ale_ret:
  2012. cpsw_ale_destroy(priv->ale);
  2013. clean_dma_ret:
  2014. cpdma_chan_destroy(priv->txch);
  2015. cpdma_chan_destroy(priv->rxch);
  2016. cpdma_ctlr_destroy(priv->dma);
  2017. clean_runtime_disable_ret:
  2018. pm_runtime_disable(&pdev->dev);
  2019. clean_ndev_ret:
  2020. free_netdev(priv->ndev);
  2021. return ret;
  2022. }
  2023. static int cpsw_remove_child_device(struct device *dev, void *c)
  2024. {
  2025. struct platform_device *pdev = to_platform_device(dev);
  2026. of_device_unregister(pdev);
  2027. return 0;
  2028. }
  2029. static int cpsw_remove(struct platform_device *pdev)
  2030. {
  2031. struct net_device *ndev = platform_get_drvdata(pdev);
  2032. struct cpsw_priv *priv = netdev_priv(ndev);
  2033. if (priv->data.dual_emac)
  2034. unregister_netdev(cpsw_get_slave_ndev(priv, 1));
  2035. unregister_netdev(ndev);
  2036. cpsw_ale_destroy(priv->ale);
  2037. cpdma_chan_destroy(priv->txch);
  2038. cpdma_chan_destroy(priv->rxch);
  2039. cpdma_ctlr_destroy(priv->dma);
  2040. pm_runtime_disable(&pdev->dev);
  2041. device_for_each_child(&pdev->dev, NULL, cpsw_remove_child_device);
  2042. if (priv->data.dual_emac)
  2043. free_netdev(cpsw_get_slave_ndev(priv, 1));
  2044. free_netdev(ndev);
  2045. return 0;
  2046. }
  2047. static int cpsw_suspend(struct device *dev)
  2048. {
  2049. struct platform_device *pdev = to_platform_device(dev);
  2050. struct net_device *ndev = platform_get_drvdata(pdev);
  2051. struct cpsw_priv *priv = netdev_priv(ndev);
  2052. if (priv->data.dual_emac) {
  2053. int i;
  2054. for (i = 0; i < priv->data.slaves; i++) {
  2055. if (netif_running(priv->slaves[i].ndev))
  2056. cpsw_ndo_stop(priv->slaves[i].ndev);
  2057. soft_reset_slave(priv->slaves + i);
  2058. }
  2059. } else {
  2060. if (netif_running(ndev))
  2061. cpsw_ndo_stop(ndev);
  2062. for_each_slave(priv, soft_reset_slave);
  2063. }
  2064. pm_runtime_put_sync(&pdev->dev);
  2065. /* Select sleep pin state */
  2066. pinctrl_pm_select_sleep_state(&pdev->dev);
  2067. return 0;
  2068. }
  2069. static int cpsw_resume(struct device *dev)
  2070. {
  2071. struct platform_device *pdev = to_platform_device(dev);
  2072. struct net_device *ndev = platform_get_drvdata(pdev);
  2073. struct cpsw_priv *priv = netdev_priv(ndev);
  2074. pm_runtime_get_sync(&pdev->dev);
  2075. /* Select default pin state */
  2076. pinctrl_pm_select_default_state(&pdev->dev);
  2077. if (priv->data.dual_emac) {
  2078. int i;
  2079. for (i = 0; i < priv->data.slaves; i++) {
  2080. if (netif_running(priv->slaves[i].ndev))
  2081. cpsw_ndo_open(priv->slaves[i].ndev);
  2082. }
  2083. } else {
  2084. if (netif_running(ndev))
  2085. cpsw_ndo_open(ndev);
  2086. }
  2087. return 0;
  2088. }
  2089. static const struct dev_pm_ops cpsw_pm_ops = {
  2090. .suspend = cpsw_suspend,
  2091. .resume = cpsw_resume,
  2092. };
  2093. static const struct of_device_id cpsw_of_mtable[] = {
  2094. { .compatible = "ti,cpsw", },
  2095. { /* sentinel */ },
  2096. };
  2097. MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
  2098. static struct platform_driver cpsw_driver = {
  2099. .driver = {
  2100. .name = "cpsw",
  2101. .pm = &cpsw_pm_ops,
  2102. .of_match_table = cpsw_of_mtable,
  2103. },
  2104. .probe = cpsw_probe,
  2105. .remove = cpsw_remove,
  2106. };
  2107. static int __init cpsw_init(void)
  2108. {
  2109. return platform_driver_register(&cpsw_driver);
  2110. }
  2111. late_initcall(cpsw_init);
  2112. static void __exit cpsw_exit(void)
  2113. {
  2114. platform_driver_unregister(&cpsw_driver);
  2115. }
  2116. module_exit(cpsw_exit);
  2117. MODULE_LICENSE("GPL");
  2118. MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
  2119. MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
  2120. MODULE_DESCRIPTION("TI CPSW Ethernet driver");