cpsw.c 64 KB

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