cpsw.c 64 KB

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