cpsw.c 71 KB

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