cpsw.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204
  1. /*
  2. * Texas Instruments Ethernet Switch Driver
  3. *
  4. * Copyright (C) 2012 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/io.h>
  17. #include <linux/clk.h>
  18. #include <linux/timer.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/irqreturn.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/net_tstamp.h>
  27. #include <linux/phy.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/delay.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/gpio.h>
  32. #include <linux/of.h>
  33. #include <linux/of_mdio.h>
  34. #include <linux/of_net.h>
  35. #include <linux/of_device.h>
  36. #include <linux/if_vlan.h>
  37. #include <linux/pinctrl/consumer.h>
  38. #include "cpsw.h"
  39. #include "cpsw_ale.h"
  40. #include "cpts.h"
  41. #include "davinci_cpdma.h"
  42. #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
  43. NETIF_MSG_DRV | NETIF_MSG_LINK | \
  44. NETIF_MSG_IFUP | NETIF_MSG_INTR | \
  45. NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
  46. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
  47. NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
  48. NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
  49. NETIF_MSG_RX_STATUS)
  50. #define cpsw_info(priv, type, format, ...) \
  51. do { \
  52. if (netif_msg_##type(priv) && net_ratelimit()) \
  53. dev_info(priv->dev, format, ## __VA_ARGS__); \
  54. } while (0)
  55. #define cpsw_err(priv, type, format, ...) \
  56. do { \
  57. if (netif_msg_##type(priv) && net_ratelimit()) \
  58. dev_err(priv->dev, format, ## __VA_ARGS__); \
  59. } while (0)
  60. #define cpsw_dbg(priv, type, format, ...) \
  61. do { \
  62. if (netif_msg_##type(priv) && net_ratelimit()) \
  63. dev_dbg(priv->dev, format, ## __VA_ARGS__); \
  64. } while (0)
  65. #define cpsw_notice(priv, type, format, ...) \
  66. do { \
  67. if (netif_msg_##type(priv) && net_ratelimit()) \
  68. dev_notice(priv->dev, format, ## __VA_ARGS__); \
  69. } while (0)
  70. #define ALE_ALL_PORTS 0x7
  71. #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
  72. #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
  73. #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
  74. #define CPSW_VERSION_1 0x19010a
  75. #define CPSW_VERSION_2 0x19010c
  76. #define CPSW_VERSION_3 0x19010f
  77. #define CPSW_VERSION_4 0x190112
  78. #define HOST_PORT_NUM 0
  79. #define SLIVER_SIZE 0x40
  80. #define CPSW1_HOST_PORT_OFFSET 0x028
  81. #define CPSW1_SLAVE_OFFSET 0x050
  82. #define CPSW1_SLAVE_SIZE 0x040
  83. #define CPSW1_CPDMA_OFFSET 0x100
  84. #define CPSW1_STATERAM_OFFSET 0x200
  85. #define CPSW1_HW_STATS 0x400
  86. #define CPSW1_CPTS_OFFSET 0x500
  87. #define CPSW1_ALE_OFFSET 0x600
  88. #define CPSW1_SLIVER_OFFSET 0x700
  89. #define CPSW2_HOST_PORT_OFFSET 0x108
  90. #define CPSW2_SLAVE_OFFSET 0x200
  91. #define CPSW2_SLAVE_SIZE 0x100
  92. #define CPSW2_CPDMA_OFFSET 0x800
  93. #define CPSW2_HW_STATS 0x900
  94. #define CPSW2_STATERAM_OFFSET 0xa00
  95. #define CPSW2_CPTS_OFFSET 0xc00
  96. #define CPSW2_ALE_OFFSET 0xd00
  97. #define CPSW2_SLIVER_OFFSET 0xd80
  98. #define CPSW2_BD_OFFSET 0x2000
  99. #define CPDMA_RXTHRESH 0x0c0
  100. #define CPDMA_RXFREE 0x0e0
  101. #define CPDMA_TXHDP 0x00
  102. #define CPDMA_RXHDP 0x20
  103. #define CPDMA_TXCP 0x40
  104. #define CPDMA_RXCP 0x60
  105. #define CPSW_POLL_WEIGHT 64
  106. #define CPSW_MIN_PACKET_SIZE 60
  107. #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
  108. #define RX_PRIORITY_MAPPING 0x76543210
  109. #define TX_PRIORITY_MAPPING 0x33221100
  110. #define CPDMA_TX_PRIORITY_MAP 0x01234567
  111. #define CPSW_VLAN_AWARE BIT(1)
  112. #define CPSW_ALE_VLAN_AWARE 1
  113. #define CPSW_FIFO_NORMAL_MODE (0 << 16)
  114. #define CPSW_FIFO_DUAL_MAC_MODE (1 << 16)
  115. #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16)
  116. #define CPSW_INTPACEEN (0x3f << 16)
  117. #define CPSW_INTPRESCALE_MASK (0x7FF << 0)
  118. #define CPSW_CMINTMAX_CNT 63
  119. #define CPSW_CMINTMIN_CNT 2
  120. #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
  121. #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
  122. #define cpsw_slave_index(cpsw, priv) \
  123. ((cpsw->data.dual_emac) ? priv->emac_port : \
  124. cpsw->data.active_slave)
  125. #define IRQ_NUM 2
  126. #define CPSW_MAX_QUEUES 8
  127. static int debug_level;
  128. module_param(debug_level, int, 0);
  129. MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
  130. static int ale_ageout = 10;
  131. module_param(ale_ageout, int, 0);
  132. MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
  133. static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  134. module_param(rx_packet_max, int, 0);
  135. MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
  136. struct cpsw_wr_regs {
  137. u32 id_ver;
  138. u32 soft_reset;
  139. u32 control;
  140. u32 int_control;
  141. u32 rx_thresh_en;
  142. u32 rx_en;
  143. u32 tx_en;
  144. u32 misc_en;
  145. u32 mem_allign1[8];
  146. u32 rx_thresh_stat;
  147. u32 rx_stat;
  148. u32 tx_stat;
  149. u32 misc_stat;
  150. u32 mem_allign2[8];
  151. u32 rx_imax;
  152. u32 tx_imax;
  153. };
  154. struct cpsw_ss_regs {
  155. u32 id_ver;
  156. u32 control;
  157. u32 soft_reset;
  158. u32 stat_port_en;
  159. u32 ptype;
  160. u32 soft_idle;
  161. u32 thru_rate;
  162. u32 gap_thresh;
  163. u32 tx_start_wds;
  164. u32 flow_control;
  165. u32 vlan_ltype;
  166. u32 ts_ltype;
  167. u32 dlr_ltype;
  168. };
  169. /* CPSW_PORT_V1 */
  170. #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
  171. #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
  172. #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
  173. #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
  174. #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
  175. #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
  176. #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
  177. #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
  178. /* CPSW_PORT_V2 */
  179. #define CPSW2_CONTROL 0x00 /* Control Register */
  180. #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
  181. #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
  182. #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
  183. #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
  184. #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
  185. #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
  186. /* CPSW_PORT_V1 and V2 */
  187. #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
  188. #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
  189. #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
  190. /* CPSW_PORT_V2 only */
  191. #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
  192. #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
  193. #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
  194. #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
  195. #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
  196. #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
  197. #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
  198. #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
  199. /* Bit definitions for the CPSW2_CONTROL register */
  200. #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
  201. #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
  202. #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
  203. #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
  204. #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
  205. #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
  206. #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
  207. #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
  208. #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
  209. #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
  210. #define TS_TTL_NONZERO (1<<8) /* Time Sync Time To Live Non-zero enable */
  211. #define TS_ANNEX_F_EN (1<<6) /* Time Sync Annex F enable */
  212. #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
  213. #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
  214. #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
  215. #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
  216. #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
  217. #define CTRL_V2_TS_BITS \
  218. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
  219. TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
  220. #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
  221. #define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
  222. #define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
  223. #define CTRL_V3_TS_BITS \
  224. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
  225. TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
  226. TS_LTYPE1_EN)
  227. #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
  228. #define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
  229. #define CTRL_V3_RX_TS_BITS (CTRL_V3_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_vector {
  321. struct cpdma_chan *ch;
  322. int budget;
  323. };
  324. struct cpsw_common {
  325. struct device *dev;
  326. struct cpsw_platform_data data;
  327. struct napi_struct napi_rx;
  328. struct napi_struct napi_tx;
  329. struct cpsw_ss_regs __iomem *regs;
  330. struct cpsw_wr_regs __iomem *wr_regs;
  331. u8 __iomem *hw_stats;
  332. struct cpsw_host_regs __iomem *host_port_regs;
  333. u32 version;
  334. u32 coal_intvl;
  335. u32 bus_freq_mhz;
  336. int rx_packet_max;
  337. struct cpsw_slave *slaves;
  338. struct cpdma_ctlr *dma;
  339. struct cpsw_vector txv[CPSW_MAX_QUEUES];
  340. struct cpsw_vector rxv[CPSW_MAX_QUEUES];
  341. struct cpsw_ale *ale;
  342. bool quirk_irq;
  343. bool rx_irq_disabled;
  344. bool tx_irq_disabled;
  345. u32 irqs_table[IRQ_NUM];
  346. struct cpts *cpts;
  347. int rx_ch_num, tx_ch_num;
  348. int speed;
  349. };
  350. struct cpsw_priv {
  351. struct net_device *ndev;
  352. struct device *dev;
  353. u32 msg_enable;
  354. u8 mac_addr[ETH_ALEN];
  355. bool rx_pause;
  356. bool tx_pause;
  357. u32 emac_port;
  358. struct cpsw_common *cpsw;
  359. };
  360. struct cpsw_stats {
  361. char stat_string[ETH_GSTRING_LEN];
  362. int type;
  363. int sizeof_stat;
  364. int stat_offset;
  365. };
  366. enum {
  367. CPSW_STATS,
  368. CPDMA_RX_STATS,
  369. CPDMA_TX_STATS,
  370. };
  371. #define CPSW_STAT(m) CPSW_STATS, \
  372. sizeof(((struct cpsw_hw_stats *)0)->m), \
  373. offsetof(struct cpsw_hw_stats, m)
  374. #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
  375. sizeof(((struct cpdma_chan_stats *)0)->m), \
  376. offsetof(struct cpdma_chan_stats, m)
  377. #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
  378. sizeof(((struct cpdma_chan_stats *)0)->m), \
  379. offsetof(struct cpdma_chan_stats, m)
  380. static const struct cpsw_stats cpsw_gstrings_stats[] = {
  381. { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
  382. { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
  383. { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
  384. { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
  385. { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
  386. { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
  387. { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
  388. { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
  389. { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
  390. { "Rx Fragments", CPSW_STAT(rxfragments) },
  391. { "Rx Octets", CPSW_STAT(rxoctets) },
  392. { "Good Tx Frames", CPSW_STAT(txgoodframes) },
  393. { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
  394. { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
  395. { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
  396. { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
  397. { "Collisions", CPSW_STAT(txcollisionframes) },
  398. { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
  399. { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
  400. { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
  401. { "Late Collisions", CPSW_STAT(txlatecollisions) },
  402. { "Tx Underrun", CPSW_STAT(txunderrun) },
  403. { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
  404. { "Tx Octets", CPSW_STAT(txoctets) },
  405. { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
  406. { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
  407. { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
  408. { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
  409. { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
  410. { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
  411. { "Net Octets", CPSW_STAT(netoctets) },
  412. { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
  413. { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
  414. { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
  415. };
  416. static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
  417. { "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
  418. { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
  419. { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
  420. { "misqueued", CPDMA_RX_STAT(misqueued) },
  421. { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
  422. { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
  423. { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
  424. { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
  425. { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
  426. { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
  427. { "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
  428. { "requeue", CPDMA_RX_STAT(requeue) },
  429. { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
  430. };
  431. #define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats)
  432. #define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats)
  433. #define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
  434. #define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi)
  435. #define for_each_slave(priv, func, arg...) \
  436. do { \
  437. struct cpsw_slave *slave; \
  438. struct cpsw_common *cpsw = (priv)->cpsw; \
  439. int n; \
  440. if (cpsw->data.dual_emac) \
  441. (func)((cpsw)->slaves + priv->emac_port, ##arg);\
  442. else \
  443. for (n = cpsw->data.slaves, \
  444. slave = cpsw->slaves; \
  445. n; n--) \
  446. (func)(slave++, ##arg); \
  447. } while (0)
  448. #define cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb) \
  449. do { \
  450. if (!cpsw->data.dual_emac) \
  451. break; \
  452. if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
  453. ndev = cpsw->slaves[0].ndev; \
  454. skb->dev = ndev; \
  455. } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
  456. ndev = cpsw->slaves[1].ndev; \
  457. skb->dev = ndev; \
  458. } \
  459. } while (0)
  460. #define cpsw_add_mcast(cpsw, priv, addr) \
  461. do { \
  462. if (cpsw->data.dual_emac) { \
  463. struct cpsw_slave *slave = cpsw->slaves + \
  464. priv->emac_port; \
  465. int slave_port = cpsw_get_slave_port( \
  466. slave->slave_num); \
  467. cpsw_ale_add_mcast(cpsw->ale, addr, \
  468. 1 << slave_port | ALE_PORT_HOST, \
  469. ALE_VLAN, slave->port_vlan, 0); \
  470. } else { \
  471. cpsw_ale_add_mcast(cpsw->ale, addr, \
  472. ALE_ALL_PORTS, \
  473. 0, 0, 0); \
  474. } \
  475. } while (0)
  476. static inline int cpsw_get_slave_port(u32 slave_num)
  477. {
  478. return slave_num + 1;
  479. }
  480. static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
  481. {
  482. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  483. struct cpsw_ale *ale = cpsw->ale;
  484. int i;
  485. if (cpsw->data.dual_emac) {
  486. bool flag = false;
  487. /* Enabling promiscuous mode for one interface will be
  488. * common for both the interface as the interface shares
  489. * the same hardware resource.
  490. */
  491. for (i = 0; i < cpsw->data.slaves; i++)
  492. if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
  493. flag = true;
  494. if (!enable && flag) {
  495. enable = true;
  496. dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
  497. }
  498. if (enable) {
  499. /* Enable Bypass */
  500. cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
  501. dev_dbg(&ndev->dev, "promiscuity enabled\n");
  502. } else {
  503. /* Disable Bypass */
  504. cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
  505. dev_dbg(&ndev->dev, "promiscuity disabled\n");
  506. }
  507. } else {
  508. if (enable) {
  509. unsigned long timeout = jiffies + HZ;
  510. /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
  511. for (i = 0; i <= cpsw->data.slaves; i++) {
  512. cpsw_ale_control_set(ale, i,
  513. ALE_PORT_NOLEARN, 1);
  514. cpsw_ale_control_set(ale, i,
  515. ALE_PORT_NO_SA_UPDATE, 1);
  516. }
  517. /* Clear All Untouched entries */
  518. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  519. do {
  520. cpu_relax();
  521. if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
  522. break;
  523. } while (time_after(timeout, jiffies));
  524. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  525. /* Clear all mcast from ALE */
  526. cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
  527. /* Flood All Unicast Packets to Host port */
  528. cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
  529. dev_dbg(&ndev->dev, "promiscuity enabled\n");
  530. } else {
  531. /* Don't Flood All Unicast Packets to Host port */
  532. cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
  533. /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
  534. for (i = 0; i <= cpsw->data.slaves; i++) {
  535. cpsw_ale_control_set(ale, i,
  536. ALE_PORT_NOLEARN, 0);
  537. cpsw_ale_control_set(ale, i,
  538. ALE_PORT_NO_SA_UPDATE, 0);
  539. }
  540. dev_dbg(&ndev->dev, "promiscuity disabled\n");
  541. }
  542. }
  543. }
  544. static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
  545. {
  546. struct cpsw_priv *priv = netdev_priv(ndev);
  547. struct cpsw_common *cpsw = priv->cpsw;
  548. int vid;
  549. if (cpsw->data.dual_emac)
  550. vid = cpsw->slaves[priv->emac_port].port_vlan;
  551. else
  552. vid = cpsw->data.default_vlan;
  553. if (ndev->flags & IFF_PROMISC) {
  554. /* Enable promiscuous mode */
  555. cpsw_set_promiscious(ndev, true);
  556. cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
  557. return;
  558. } else {
  559. /* Disable promiscuous mode */
  560. cpsw_set_promiscious(ndev, false);
  561. }
  562. /* Restore allmulti on vlans if necessary */
  563. cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI);
  564. /* Clear all mcast from ALE */
  565. cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid);
  566. if (!netdev_mc_empty(ndev)) {
  567. struct netdev_hw_addr *ha;
  568. /* program multicast address list into ALE register */
  569. netdev_for_each_mc_addr(ha, ndev) {
  570. cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr);
  571. }
  572. }
  573. }
  574. static void cpsw_intr_enable(struct cpsw_common *cpsw)
  575. {
  576. __raw_writel(0xFF, &cpsw->wr_regs->tx_en);
  577. __raw_writel(0xFF, &cpsw->wr_regs->rx_en);
  578. cpdma_ctlr_int_ctrl(cpsw->dma, true);
  579. return;
  580. }
  581. static void cpsw_intr_disable(struct cpsw_common *cpsw)
  582. {
  583. __raw_writel(0, &cpsw->wr_regs->tx_en);
  584. __raw_writel(0, &cpsw->wr_regs->rx_en);
  585. cpdma_ctlr_int_ctrl(cpsw->dma, false);
  586. return;
  587. }
  588. static void cpsw_tx_handler(void *token, int len, int status)
  589. {
  590. struct netdev_queue *txq;
  591. struct sk_buff *skb = token;
  592. struct net_device *ndev = skb->dev;
  593. struct cpsw_common *cpsw = ndev_to_cpsw(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. txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
  598. if (unlikely(netif_tx_queue_stopped(txq)))
  599. netif_tx_wake_queue(txq);
  600. cpts_tx_timestamp(cpsw->cpts, skb);
  601. ndev->stats.tx_packets++;
  602. ndev->stats.tx_bytes += len;
  603. dev_kfree_skb_any(skb);
  604. }
  605. static void cpsw_rx_handler(void *token, int len, int status)
  606. {
  607. struct cpdma_chan *ch;
  608. struct sk_buff *skb = token;
  609. struct sk_buff *new_skb;
  610. struct net_device *ndev = skb->dev;
  611. int ret = 0;
  612. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  613. cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb);
  614. if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
  615. bool ndev_status = false;
  616. struct cpsw_slave *slave = cpsw->slaves;
  617. int n;
  618. if (cpsw->data.dual_emac) {
  619. /* In dual emac mode check for all interfaces */
  620. for (n = cpsw->data.slaves; n; n--, slave++)
  621. if (netif_running(slave->ndev))
  622. ndev_status = true;
  623. }
  624. if (ndev_status && (status >= 0)) {
  625. /* The packet received is for the interface which
  626. * is already down and the other interface is up
  627. * and running, instead of freeing which results
  628. * in reducing of the number of rx descriptor in
  629. * DMA engine, requeue skb back to cpdma.
  630. */
  631. new_skb = skb;
  632. goto requeue;
  633. }
  634. /* the interface is going down, skbs are purged */
  635. dev_kfree_skb_any(skb);
  636. return;
  637. }
  638. new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
  639. if (new_skb) {
  640. skb_copy_queue_mapping(new_skb, skb);
  641. skb_put(skb, len);
  642. cpts_rx_timestamp(cpsw->cpts, skb);
  643. skb->protocol = eth_type_trans(skb, ndev);
  644. netif_receive_skb(skb);
  645. ndev->stats.rx_bytes += len;
  646. ndev->stats.rx_packets++;
  647. kmemleak_not_leak(new_skb);
  648. } else {
  649. ndev->stats.rx_dropped++;
  650. new_skb = skb;
  651. }
  652. requeue:
  653. if (netif_dormant(ndev)) {
  654. dev_kfree_skb_any(new_skb);
  655. return;
  656. }
  657. ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
  658. ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
  659. skb_tailroom(new_skb), 0);
  660. if (WARN_ON(ret < 0))
  661. dev_kfree_skb_any(new_skb);
  662. }
  663. static void cpsw_split_res(struct net_device *ndev)
  664. {
  665. struct cpsw_priv *priv = netdev_priv(ndev);
  666. u32 consumed_rate = 0, bigest_rate = 0;
  667. struct cpsw_common *cpsw = priv->cpsw;
  668. struct cpsw_vector *txv = cpsw->txv;
  669. int i, ch_weight, rlim_ch_num = 0;
  670. int budget, bigest_rate_ch = 0;
  671. u32 ch_rate, max_rate;
  672. int ch_budget = 0;
  673. for (i = 0; i < cpsw->tx_ch_num; i++) {
  674. ch_rate = cpdma_chan_get_rate(txv[i].ch);
  675. if (!ch_rate)
  676. continue;
  677. rlim_ch_num++;
  678. consumed_rate += ch_rate;
  679. }
  680. if (cpsw->tx_ch_num == rlim_ch_num) {
  681. max_rate = consumed_rate;
  682. } else if (!rlim_ch_num) {
  683. ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
  684. bigest_rate = 0;
  685. max_rate = consumed_rate;
  686. } else {
  687. max_rate = cpsw->speed * 1000;
  688. /* if max_rate is less then expected due to reduced link speed,
  689. * split proportionally according next potential max speed
  690. */
  691. if (max_rate < consumed_rate)
  692. max_rate *= 10;
  693. if (max_rate < consumed_rate)
  694. max_rate *= 10;
  695. ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
  696. ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
  697. (cpsw->tx_ch_num - rlim_ch_num);
  698. bigest_rate = (max_rate - consumed_rate) /
  699. (cpsw->tx_ch_num - rlim_ch_num);
  700. }
  701. /* split tx weight/budget */
  702. budget = CPSW_POLL_WEIGHT;
  703. for (i = 0; i < cpsw->tx_ch_num; i++) {
  704. ch_rate = cpdma_chan_get_rate(txv[i].ch);
  705. if (ch_rate) {
  706. txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
  707. if (!txv[i].budget)
  708. txv[i].budget++;
  709. if (ch_rate > bigest_rate) {
  710. bigest_rate_ch = i;
  711. bigest_rate = ch_rate;
  712. }
  713. ch_weight = (ch_rate * 100) / max_rate;
  714. if (!ch_weight)
  715. ch_weight++;
  716. cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
  717. } else {
  718. txv[i].budget = ch_budget;
  719. if (!bigest_rate_ch)
  720. bigest_rate_ch = i;
  721. cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
  722. }
  723. budget -= txv[i].budget;
  724. }
  725. if (budget)
  726. txv[bigest_rate_ch].budget += budget;
  727. /* split rx budget */
  728. budget = CPSW_POLL_WEIGHT;
  729. ch_budget = budget / cpsw->rx_ch_num;
  730. for (i = 0; i < cpsw->rx_ch_num; i++) {
  731. cpsw->rxv[i].budget = ch_budget;
  732. budget -= ch_budget;
  733. }
  734. if (budget)
  735. cpsw->rxv[0].budget += budget;
  736. }
  737. static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
  738. {
  739. struct cpsw_common *cpsw = dev_id;
  740. writel(0, &cpsw->wr_regs->tx_en);
  741. cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
  742. if (cpsw->quirk_irq) {
  743. disable_irq_nosync(cpsw->irqs_table[1]);
  744. cpsw->tx_irq_disabled = true;
  745. }
  746. napi_schedule(&cpsw->napi_tx);
  747. return IRQ_HANDLED;
  748. }
  749. static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
  750. {
  751. struct cpsw_common *cpsw = dev_id;
  752. cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
  753. writel(0, &cpsw->wr_regs->rx_en);
  754. if (cpsw->quirk_irq) {
  755. disable_irq_nosync(cpsw->irqs_table[0]);
  756. cpsw->rx_irq_disabled = true;
  757. }
  758. napi_schedule(&cpsw->napi_rx);
  759. return IRQ_HANDLED;
  760. }
  761. static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
  762. {
  763. u32 ch_map;
  764. int num_tx, cur_budget, ch;
  765. struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
  766. struct cpsw_vector *txv;
  767. /* process every unprocessed channel */
  768. ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
  769. for (ch = 0, num_tx = 0; ch_map; ch_map >>= 1, ch++) {
  770. if (!(ch_map & 0x01))
  771. continue;
  772. txv = &cpsw->txv[ch];
  773. if (unlikely(txv->budget > budget - num_tx))
  774. cur_budget = budget - num_tx;
  775. else
  776. cur_budget = txv->budget;
  777. num_tx += cpdma_chan_process(txv->ch, cur_budget);
  778. if (num_tx >= budget)
  779. break;
  780. }
  781. if (num_tx < budget) {
  782. napi_complete(napi_tx);
  783. writel(0xff, &cpsw->wr_regs->tx_en);
  784. if (cpsw->quirk_irq && cpsw->tx_irq_disabled) {
  785. cpsw->tx_irq_disabled = false;
  786. enable_irq(cpsw->irqs_table[1]);
  787. }
  788. }
  789. return num_tx;
  790. }
  791. static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
  792. {
  793. u32 ch_map;
  794. int num_rx, cur_budget, ch;
  795. struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
  796. struct cpsw_vector *rxv;
  797. /* process every unprocessed channel */
  798. ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
  799. for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
  800. if (!(ch_map & 0x01))
  801. continue;
  802. rxv = &cpsw->rxv[ch];
  803. if (unlikely(rxv->budget > budget - num_rx))
  804. cur_budget = budget - num_rx;
  805. else
  806. cur_budget = rxv->budget;
  807. num_rx += cpdma_chan_process(rxv->ch, cur_budget);
  808. if (num_rx >= budget)
  809. break;
  810. }
  811. if (num_rx < budget) {
  812. napi_complete(napi_rx);
  813. writel(0xff, &cpsw->wr_regs->rx_en);
  814. if (cpsw->quirk_irq && cpsw->rx_irq_disabled) {
  815. cpsw->rx_irq_disabled = false;
  816. enable_irq(cpsw->irqs_table[0]);
  817. }
  818. }
  819. return num_rx;
  820. }
  821. static inline void soft_reset(const char *module, void __iomem *reg)
  822. {
  823. unsigned long timeout = jiffies + HZ;
  824. __raw_writel(1, reg);
  825. do {
  826. cpu_relax();
  827. } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
  828. WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
  829. }
  830. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  831. ((mac)[2] << 16) | ((mac)[3] << 24))
  832. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  833. static void cpsw_set_slave_mac(struct cpsw_slave *slave,
  834. struct cpsw_priv *priv)
  835. {
  836. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  837. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  838. }
  839. static void _cpsw_adjust_link(struct cpsw_slave *slave,
  840. struct cpsw_priv *priv, bool *link)
  841. {
  842. struct phy_device *phy = slave->phy;
  843. u32 mac_control = 0;
  844. u32 slave_port;
  845. struct cpsw_common *cpsw = priv->cpsw;
  846. if (!phy)
  847. return;
  848. slave_port = cpsw_get_slave_port(slave->slave_num);
  849. if (phy->link) {
  850. mac_control = cpsw->data.mac_control;
  851. /* enable forwarding */
  852. cpsw_ale_control_set(cpsw->ale, slave_port,
  853. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  854. if (phy->speed == 1000)
  855. mac_control |= BIT(7); /* GIGABITEN */
  856. if (phy->duplex)
  857. mac_control |= BIT(0); /* FULLDUPLEXEN */
  858. /* set speed_in input in case RMII mode is used in 100Mbps */
  859. if (phy->speed == 100)
  860. mac_control |= BIT(15);
  861. else if (phy->speed == 10)
  862. mac_control |= BIT(18); /* In Band mode */
  863. if (priv->rx_pause)
  864. mac_control |= BIT(3);
  865. if (priv->tx_pause)
  866. mac_control |= BIT(4);
  867. *link = true;
  868. } else {
  869. mac_control = 0;
  870. /* disable forwarding */
  871. cpsw_ale_control_set(cpsw->ale, slave_port,
  872. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  873. }
  874. if (mac_control != slave->mac_control) {
  875. phy_print_status(phy);
  876. __raw_writel(mac_control, &slave->sliver->mac_control);
  877. }
  878. slave->mac_control = mac_control;
  879. }
  880. static int cpsw_get_common_speed(struct cpsw_common *cpsw)
  881. {
  882. int i, speed;
  883. for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
  884. if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
  885. speed += cpsw->slaves[i].phy->speed;
  886. return speed;
  887. }
  888. static int cpsw_need_resplit(struct cpsw_common *cpsw)
  889. {
  890. int i, rlim_ch_num;
  891. int speed, ch_rate;
  892. /* re-split resources only in case speed was changed */
  893. speed = cpsw_get_common_speed(cpsw);
  894. if (speed == cpsw->speed || !speed)
  895. return 0;
  896. cpsw->speed = speed;
  897. for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
  898. ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
  899. if (!ch_rate)
  900. break;
  901. rlim_ch_num++;
  902. }
  903. /* cases not dependent on speed */
  904. if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
  905. return 0;
  906. return 1;
  907. }
  908. static void cpsw_adjust_link(struct net_device *ndev)
  909. {
  910. struct cpsw_priv *priv = netdev_priv(ndev);
  911. struct cpsw_common *cpsw = priv->cpsw;
  912. bool link = false;
  913. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  914. if (link) {
  915. if (cpsw_need_resplit(cpsw))
  916. cpsw_split_res(ndev);
  917. netif_carrier_on(ndev);
  918. if (netif_running(ndev))
  919. netif_tx_wake_all_queues(ndev);
  920. } else {
  921. netif_carrier_off(ndev);
  922. netif_tx_stop_all_queues(ndev);
  923. }
  924. }
  925. static int cpsw_get_coalesce(struct net_device *ndev,
  926. struct ethtool_coalesce *coal)
  927. {
  928. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  929. coal->rx_coalesce_usecs = cpsw->coal_intvl;
  930. return 0;
  931. }
  932. static int cpsw_set_coalesce(struct net_device *ndev,
  933. struct ethtool_coalesce *coal)
  934. {
  935. struct cpsw_priv *priv = netdev_priv(ndev);
  936. u32 int_ctrl;
  937. u32 num_interrupts = 0;
  938. u32 prescale = 0;
  939. u32 addnl_dvdr = 1;
  940. u32 coal_intvl = 0;
  941. struct cpsw_common *cpsw = priv->cpsw;
  942. coal_intvl = coal->rx_coalesce_usecs;
  943. int_ctrl = readl(&cpsw->wr_regs->int_control);
  944. prescale = cpsw->bus_freq_mhz * 4;
  945. if (!coal->rx_coalesce_usecs) {
  946. int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
  947. goto update_return;
  948. }
  949. if (coal_intvl < CPSW_CMINTMIN_INTVL)
  950. coal_intvl = CPSW_CMINTMIN_INTVL;
  951. if (coal_intvl > CPSW_CMINTMAX_INTVL) {
  952. /* Interrupt pacer works with 4us Pulse, we can
  953. * throttle further by dilating the 4us pulse.
  954. */
  955. addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
  956. if (addnl_dvdr > 1) {
  957. prescale *= addnl_dvdr;
  958. if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
  959. coal_intvl = (CPSW_CMINTMAX_INTVL
  960. * addnl_dvdr);
  961. } else {
  962. addnl_dvdr = 1;
  963. coal_intvl = CPSW_CMINTMAX_INTVL;
  964. }
  965. }
  966. num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
  967. writel(num_interrupts, &cpsw->wr_regs->rx_imax);
  968. writel(num_interrupts, &cpsw->wr_regs->tx_imax);
  969. int_ctrl |= CPSW_INTPACEEN;
  970. int_ctrl &= (~CPSW_INTPRESCALE_MASK);
  971. int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
  972. update_return:
  973. writel(int_ctrl, &cpsw->wr_regs->int_control);
  974. cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
  975. cpsw->coal_intvl = coal_intvl;
  976. return 0;
  977. }
  978. static int cpsw_get_sset_count(struct net_device *ndev, int sset)
  979. {
  980. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  981. switch (sset) {
  982. case ETH_SS_STATS:
  983. return (CPSW_STATS_COMMON_LEN +
  984. (cpsw->rx_ch_num + cpsw->tx_ch_num) *
  985. CPSW_STATS_CH_LEN);
  986. default:
  987. return -EOPNOTSUPP;
  988. }
  989. }
  990. static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
  991. {
  992. int ch_stats_len;
  993. int line;
  994. int i;
  995. ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
  996. for (i = 0; i < ch_stats_len; i++) {
  997. line = i % CPSW_STATS_CH_LEN;
  998. snprintf(*p, ETH_GSTRING_LEN,
  999. "%s DMA chan %d: %s", rx_dir ? "Rx" : "Tx",
  1000. i / CPSW_STATS_CH_LEN,
  1001. cpsw_gstrings_ch_stats[line].stat_string);
  1002. *p += ETH_GSTRING_LEN;
  1003. }
  1004. }
  1005. static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  1006. {
  1007. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1008. u8 *p = data;
  1009. int i;
  1010. switch (stringset) {
  1011. case ETH_SS_STATS:
  1012. for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
  1013. memcpy(p, cpsw_gstrings_stats[i].stat_string,
  1014. ETH_GSTRING_LEN);
  1015. p += ETH_GSTRING_LEN;
  1016. }
  1017. cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
  1018. cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
  1019. break;
  1020. }
  1021. }
  1022. static void cpsw_get_ethtool_stats(struct net_device *ndev,
  1023. struct ethtool_stats *stats, u64 *data)
  1024. {
  1025. u8 *p;
  1026. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1027. struct cpdma_chan_stats ch_stats;
  1028. int i, l, ch;
  1029. /* Collect Davinci CPDMA stats for Rx and Tx Channel */
  1030. for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
  1031. data[l] = readl(cpsw->hw_stats +
  1032. cpsw_gstrings_stats[l].stat_offset);
  1033. for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
  1034. cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
  1035. for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
  1036. p = (u8 *)&ch_stats +
  1037. cpsw_gstrings_ch_stats[i].stat_offset;
  1038. data[l] = *(u32 *)p;
  1039. }
  1040. }
  1041. for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
  1042. cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
  1043. for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
  1044. p = (u8 *)&ch_stats +
  1045. cpsw_gstrings_ch_stats[i].stat_offset;
  1046. data[l] = *(u32 *)p;
  1047. }
  1048. }
  1049. }
  1050. static int cpsw_common_res_usage_state(struct cpsw_common *cpsw)
  1051. {
  1052. u32 i;
  1053. u32 usage_count = 0;
  1054. if (!cpsw->data.dual_emac)
  1055. return 0;
  1056. for (i = 0; i < cpsw->data.slaves; i++)
  1057. if (cpsw->slaves[i].open_stat)
  1058. usage_count++;
  1059. return usage_count;
  1060. }
  1061. static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
  1062. struct sk_buff *skb,
  1063. struct cpdma_chan *txch)
  1064. {
  1065. struct cpsw_common *cpsw = priv->cpsw;
  1066. return cpdma_chan_submit(txch, skb, skb->data, skb->len,
  1067. priv->emac_port + cpsw->data.dual_emac);
  1068. }
  1069. static inline void cpsw_add_dual_emac_def_ale_entries(
  1070. struct cpsw_priv *priv, struct cpsw_slave *slave,
  1071. u32 slave_port)
  1072. {
  1073. struct cpsw_common *cpsw = priv->cpsw;
  1074. u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
  1075. if (cpsw->version == CPSW_VERSION_1)
  1076. slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
  1077. else
  1078. slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
  1079. cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
  1080. port_mask, port_mask, 0);
  1081. cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
  1082. port_mask, ALE_VLAN, slave->port_vlan, 0);
  1083. cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
  1084. HOST_PORT_NUM, ALE_VLAN |
  1085. ALE_SECURE, slave->port_vlan);
  1086. }
  1087. static void soft_reset_slave(struct cpsw_slave *slave)
  1088. {
  1089. char name[32];
  1090. snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
  1091. soft_reset(name, &slave->sliver->soft_reset);
  1092. }
  1093. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  1094. {
  1095. u32 slave_port;
  1096. struct cpsw_common *cpsw = priv->cpsw;
  1097. soft_reset_slave(slave);
  1098. /* setup priority mapping */
  1099. __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
  1100. switch (cpsw->version) {
  1101. case CPSW_VERSION_1:
  1102. slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
  1103. break;
  1104. case CPSW_VERSION_2:
  1105. case CPSW_VERSION_3:
  1106. case CPSW_VERSION_4:
  1107. slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
  1108. break;
  1109. }
  1110. /* setup max packet size, and mac address */
  1111. __raw_writel(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
  1112. cpsw_set_slave_mac(slave, priv);
  1113. slave->mac_control = 0; /* no link yet */
  1114. slave_port = cpsw_get_slave_port(slave->slave_num);
  1115. if (cpsw->data.dual_emac)
  1116. cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
  1117. else
  1118. cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
  1119. 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
  1120. if (slave->data->phy_node) {
  1121. slave->phy = of_phy_connect(priv->ndev, slave->data->phy_node,
  1122. &cpsw_adjust_link, 0, slave->data->phy_if);
  1123. if (!slave->phy) {
  1124. dev_err(priv->dev, "phy \"%s\" not found on slave %d\n",
  1125. slave->data->phy_node->full_name,
  1126. slave->slave_num);
  1127. return;
  1128. }
  1129. } else {
  1130. slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
  1131. &cpsw_adjust_link, slave->data->phy_if);
  1132. if (IS_ERR(slave->phy)) {
  1133. dev_err(priv->dev,
  1134. "phy \"%s\" not found on slave %d, err %ld\n",
  1135. slave->data->phy_id, slave->slave_num,
  1136. PTR_ERR(slave->phy));
  1137. slave->phy = NULL;
  1138. return;
  1139. }
  1140. }
  1141. phy_attached_info(slave->phy);
  1142. phy_start(slave->phy);
  1143. /* Configure GMII_SEL register */
  1144. cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
  1145. }
  1146. static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
  1147. {
  1148. struct cpsw_common *cpsw = priv->cpsw;
  1149. const int vlan = cpsw->data.default_vlan;
  1150. u32 reg;
  1151. int i;
  1152. int unreg_mcast_mask;
  1153. reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
  1154. CPSW2_PORT_VLAN;
  1155. writel(vlan, &cpsw->host_port_regs->port_vlan);
  1156. for (i = 0; i < cpsw->data.slaves; i++)
  1157. slave_write(cpsw->slaves + i, vlan, reg);
  1158. if (priv->ndev->flags & IFF_ALLMULTI)
  1159. unreg_mcast_mask = ALE_ALL_PORTS;
  1160. else
  1161. unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
  1162. cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
  1163. ALE_ALL_PORTS, ALE_ALL_PORTS,
  1164. unreg_mcast_mask);
  1165. }
  1166. static void cpsw_init_host_port(struct cpsw_priv *priv)
  1167. {
  1168. u32 fifo_mode;
  1169. u32 control_reg;
  1170. struct cpsw_common *cpsw = priv->cpsw;
  1171. /* soft reset the controller and initialize ale */
  1172. soft_reset("cpsw", &cpsw->regs->soft_reset);
  1173. cpsw_ale_start(cpsw->ale);
  1174. /* switch to vlan unaware mode */
  1175. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
  1176. CPSW_ALE_VLAN_AWARE);
  1177. control_reg = readl(&cpsw->regs->control);
  1178. control_reg |= CPSW_VLAN_AWARE;
  1179. writel(control_reg, &cpsw->regs->control);
  1180. fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
  1181. CPSW_FIFO_NORMAL_MODE;
  1182. writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
  1183. /* setup host port priority mapping */
  1184. __raw_writel(CPDMA_TX_PRIORITY_MAP,
  1185. &cpsw->host_port_regs->cpdma_tx_pri_map);
  1186. __raw_writel(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
  1187. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
  1188. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  1189. if (!cpsw->data.dual_emac) {
  1190. cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
  1191. 0, 0);
  1192. cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
  1193. ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
  1194. }
  1195. }
  1196. static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
  1197. {
  1198. struct cpsw_common *cpsw = priv->cpsw;
  1199. struct sk_buff *skb;
  1200. int ch_buf_num;
  1201. int ch, i, ret;
  1202. for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
  1203. ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
  1204. for (i = 0; i < ch_buf_num; i++) {
  1205. skb = __netdev_alloc_skb_ip_align(priv->ndev,
  1206. cpsw->rx_packet_max,
  1207. GFP_KERNEL);
  1208. if (!skb) {
  1209. cpsw_err(priv, ifup, "cannot allocate skb\n");
  1210. return -ENOMEM;
  1211. }
  1212. skb_set_queue_mapping(skb, ch);
  1213. ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
  1214. skb->data, skb_tailroom(skb),
  1215. 0);
  1216. if (ret < 0) {
  1217. cpsw_err(priv, ifup,
  1218. "cannot submit skb to channel %d rx, error %d\n",
  1219. ch, ret);
  1220. kfree_skb(skb);
  1221. return ret;
  1222. }
  1223. kmemleak_not_leak(skb);
  1224. }
  1225. cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
  1226. ch, ch_buf_num);
  1227. }
  1228. return 0;
  1229. }
  1230. static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
  1231. {
  1232. u32 slave_port;
  1233. slave_port = cpsw_get_slave_port(slave->slave_num);
  1234. if (!slave->phy)
  1235. return;
  1236. phy_stop(slave->phy);
  1237. phy_disconnect(slave->phy);
  1238. slave->phy = NULL;
  1239. cpsw_ale_control_set(cpsw->ale, slave_port,
  1240. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  1241. soft_reset_slave(slave);
  1242. }
  1243. static int cpsw_ndo_open(struct net_device *ndev)
  1244. {
  1245. struct cpsw_priv *priv = netdev_priv(ndev);
  1246. struct cpsw_common *cpsw = priv->cpsw;
  1247. int ret;
  1248. u32 reg;
  1249. ret = pm_runtime_get_sync(cpsw->dev);
  1250. if (ret < 0) {
  1251. pm_runtime_put_noidle(cpsw->dev);
  1252. return ret;
  1253. }
  1254. if (!cpsw_common_res_usage_state(cpsw))
  1255. cpsw_intr_disable(cpsw);
  1256. netif_carrier_off(ndev);
  1257. /* Notify the stack of the actual queue counts. */
  1258. ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
  1259. if (ret) {
  1260. dev_err(priv->dev, "cannot set real number of tx queues\n");
  1261. goto err_cleanup;
  1262. }
  1263. ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
  1264. if (ret) {
  1265. dev_err(priv->dev, "cannot set real number of rx queues\n");
  1266. goto err_cleanup;
  1267. }
  1268. reg = cpsw->version;
  1269. dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
  1270. CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
  1271. CPSW_RTL_VERSION(reg));
  1272. /* initialize host and slave ports */
  1273. if (!cpsw_common_res_usage_state(cpsw))
  1274. cpsw_init_host_port(priv);
  1275. for_each_slave(priv, cpsw_slave_open, priv);
  1276. /* Add default VLAN */
  1277. if (!cpsw->data.dual_emac)
  1278. cpsw_add_default_vlan(priv);
  1279. else
  1280. cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
  1281. ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
  1282. if (!cpsw_common_res_usage_state(cpsw)) {
  1283. /* disable priority elevation */
  1284. __raw_writel(0, &cpsw->regs->ptype);
  1285. /* enable statistics collection only on all ports */
  1286. __raw_writel(0x7, &cpsw->regs->stat_port_en);
  1287. /* Enable internal fifo flow control */
  1288. writel(0x7, &cpsw->regs->flow_control);
  1289. napi_enable(&cpsw->napi_rx);
  1290. napi_enable(&cpsw->napi_tx);
  1291. if (cpsw->tx_irq_disabled) {
  1292. cpsw->tx_irq_disabled = false;
  1293. enable_irq(cpsw->irqs_table[1]);
  1294. }
  1295. if (cpsw->rx_irq_disabled) {
  1296. cpsw->rx_irq_disabled = false;
  1297. enable_irq(cpsw->irqs_table[0]);
  1298. }
  1299. ret = cpsw_fill_rx_channels(priv);
  1300. if (ret < 0)
  1301. goto err_cleanup;
  1302. if (cpts_register(cpsw->cpts))
  1303. dev_err(priv->dev, "error registering cpts device\n");
  1304. }
  1305. /* Enable Interrupt pacing if configured */
  1306. if (cpsw->coal_intvl != 0) {
  1307. struct ethtool_coalesce coal;
  1308. coal.rx_coalesce_usecs = cpsw->coal_intvl;
  1309. cpsw_set_coalesce(ndev, &coal);
  1310. }
  1311. cpdma_ctlr_start(cpsw->dma);
  1312. cpsw_intr_enable(cpsw);
  1313. if (cpsw->data.dual_emac)
  1314. cpsw->slaves[priv->emac_port].open_stat = true;
  1315. return 0;
  1316. err_cleanup:
  1317. cpdma_ctlr_stop(cpsw->dma);
  1318. for_each_slave(priv, cpsw_slave_stop, cpsw);
  1319. pm_runtime_put_sync(cpsw->dev);
  1320. netif_carrier_off(priv->ndev);
  1321. return ret;
  1322. }
  1323. static int cpsw_ndo_stop(struct net_device *ndev)
  1324. {
  1325. struct cpsw_priv *priv = netdev_priv(ndev);
  1326. struct cpsw_common *cpsw = priv->cpsw;
  1327. cpsw_info(priv, ifdown, "shutting down cpsw device\n");
  1328. netif_tx_stop_all_queues(priv->ndev);
  1329. netif_carrier_off(priv->ndev);
  1330. if (cpsw_common_res_usage_state(cpsw) <= 1) {
  1331. napi_disable(&cpsw->napi_rx);
  1332. napi_disable(&cpsw->napi_tx);
  1333. cpts_unregister(cpsw->cpts);
  1334. cpsw_intr_disable(cpsw);
  1335. cpdma_ctlr_stop(cpsw->dma);
  1336. cpsw_ale_stop(cpsw->ale);
  1337. }
  1338. for_each_slave(priv, cpsw_slave_stop, cpsw);
  1339. if (cpsw_need_resplit(cpsw))
  1340. cpsw_split_res(ndev);
  1341. pm_runtime_put_sync(cpsw->dev);
  1342. if (cpsw->data.dual_emac)
  1343. cpsw->slaves[priv->emac_port].open_stat = false;
  1344. return 0;
  1345. }
  1346. static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
  1347. struct net_device *ndev)
  1348. {
  1349. struct cpsw_priv *priv = netdev_priv(ndev);
  1350. struct cpsw_common *cpsw = priv->cpsw;
  1351. struct netdev_queue *txq;
  1352. struct cpdma_chan *txch;
  1353. int ret, q_idx;
  1354. netif_trans_update(ndev);
  1355. if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
  1356. cpsw_err(priv, tx_err, "packet pad failed\n");
  1357. ndev->stats.tx_dropped++;
  1358. return NETDEV_TX_OK;
  1359. }
  1360. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
  1361. cpts_is_tx_enabled(cpsw->cpts))
  1362. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1363. skb_tx_timestamp(skb);
  1364. q_idx = skb_get_queue_mapping(skb);
  1365. if (q_idx >= cpsw->tx_ch_num)
  1366. q_idx = q_idx % cpsw->tx_ch_num;
  1367. txch = cpsw->txv[q_idx].ch;
  1368. ret = cpsw_tx_packet_submit(priv, skb, txch);
  1369. if (unlikely(ret != 0)) {
  1370. cpsw_err(priv, tx_err, "desc submit failed\n");
  1371. goto fail;
  1372. }
  1373. /* If there is no more tx desc left free then we need to
  1374. * tell the kernel to stop sending us tx frames.
  1375. */
  1376. if (unlikely(!cpdma_check_free_tx_desc(txch))) {
  1377. txq = netdev_get_tx_queue(ndev, q_idx);
  1378. netif_tx_stop_queue(txq);
  1379. }
  1380. return NETDEV_TX_OK;
  1381. fail:
  1382. ndev->stats.tx_dropped++;
  1383. txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
  1384. netif_tx_stop_queue(txq);
  1385. return NETDEV_TX_BUSY;
  1386. }
  1387. #if IS_ENABLED(CONFIG_TI_CPTS)
  1388. static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
  1389. {
  1390. struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
  1391. u32 ts_en, seq_id;
  1392. if (!cpts_is_tx_enabled(cpsw->cpts) &&
  1393. !cpts_is_rx_enabled(cpsw->cpts)) {
  1394. slave_write(slave, 0, CPSW1_TS_CTL);
  1395. return;
  1396. }
  1397. seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
  1398. ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
  1399. if (cpts_is_tx_enabled(cpsw->cpts))
  1400. ts_en |= CPSW_V1_TS_TX_EN;
  1401. if (cpts_is_rx_enabled(cpsw->cpts))
  1402. ts_en |= CPSW_V1_TS_RX_EN;
  1403. slave_write(slave, ts_en, CPSW1_TS_CTL);
  1404. slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
  1405. }
  1406. static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
  1407. {
  1408. struct cpsw_slave *slave;
  1409. struct cpsw_common *cpsw = priv->cpsw;
  1410. u32 ctrl, mtype;
  1411. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  1412. ctrl = slave_read(slave, CPSW2_CONTROL);
  1413. switch (cpsw->version) {
  1414. case CPSW_VERSION_2:
  1415. ctrl &= ~CTRL_V2_ALL_TS_MASK;
  1416. if (cpts_is_tx_enabled(cpsw->cpts))
  1417. ctrl |= CTRL_V2_TX_TS_BITS;
  1418. if (cpts_is_rx_enabled(cpsw->cpts))
  1419. ctrl |= CTRL_V2_RX_TS_BITS;
  1420. break;
  1421. case CPSW_VERSION_3:
  1422. default:
  1423. ctrl &= ~CTRL_V3_ALL_TS_MASK;
  1424. if (cpts_is_tx_enabled(cpsw->cpts))
  1425. ctrl |= CTRL_V3_TX_TS_BITS;
  1426. if (cpts_is_rx_enabled(cpsw->cpts))
  1427. ctrl |= CTRL_V3_RX_TS_BITS;
  1428. break;
  1429. }
  1430. mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
  1431. slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
  1432. slave_write(slave, ctrl, CPSW2_CONTROL);
  1433. __raw_writel(ETH_P_1588, &cpsw->regs->ts_ltype);
  1434. }
  1435. static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  1436. {
  1437. struct cpsw_priv *priv = netdev_priv(dev);
  1438. struct hwtstamp_config cfg;
  1439. struct cpsw_common *cpsw = priv->cpsw;
  1440. struct cpts *cpts = cpsw->cpts;
  1441. if (cpsw->version != CPSW_VERSION_1 &&
  1442. cpsw->version != CPSW_VERSION_2 &&
  1443. cpsw->version != CPSW_VERSION_3)
  1444. return -EOPNOTSUPP;
  1445. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  1446. return -EFAULT;
  1447. /* reserved for future extensions */
  1448. if (cfg.flags)
  1449. return -EINVAL;
  1450. if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
  1451. return -ERANGE;
  1452. switch (cfg.rx_filter) {
  1453. case HWTSTAMP_FILTER_NONE:
  1454. cpts_rx_enable(cpts, 0);
  1455. break;
  1456. case HWTSTAMP_FILTER_ALL:
  1457. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1458. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1459. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1460. return -ERANGE;
  1461. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1462. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1463. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1464. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1465. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1466. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1467. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1468. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1469. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1470. cpts_rx_enable(cpts, 1);
  1471. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  1472. break;
  1473. default:
  1474. return -ERANGE;
  1475. }
  1476. cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
  1477. switch (cpsw->version) {
  1478. case CPSW_VERSION_1:
  1479. cpsw_hwtstamp_v1(cpsw);
  1480. break;
  1481. case CPSW_VERSION_2:
  1482. case CPSW_VERSION_3:
  1483. cpsw_hwtstamp_v2(priv);
  1484. break;
  1485. default:
  1486. WARN_ON(1);
  1487. }
  1488. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1489. }
  1490. static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  1491. {
  1492. struct cpsw_common *cpsw = ndev_to_cpsw(dev);
  1493. struct cpts *cpts = cpsw->cpts;
  1494. struct hwtstamp_config cfg;
  1495. if (cpsw->version != CPSW_VERSION_1 &&
  1496. cpsw->version != CPSW_VERSION_2 &&
  1497. cpsw->version != CPSW_VERSION_3)
  1498. return -EOPNOTSUPP;
  1499. cfg.flags = 0;
  1500. cfg.tx_type = cpts_is_tx_enabled(cpts) ?
  1501. HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  1502. cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
  1503. HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE);
  1504. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1505. }
  1506. #else
  1507. static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  1508. {
  1509. return -EOPNOTSUPP;
  1510. }
  1511. static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  1512. {
  1513. return -EOPNOTSUPP;
  1514. }
  1515. #endif /*CONFIG_TI_CPTS*/
  1516. static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  1517. {
  1518. struct cpsw_priv *priv = netdev_priv(dev);
  1519. struct cpsw_common *cpsw = priv->cpsw;
  1520. int slave_no = cpsw_slave_index(cpsw, priv);
  1521. if (!netif_running(dev))
  1522. return -EINVAL;
  1523. switch (cmd) {
  1524. case SIOCSHWTSTAMP:
  1525. return cpsw_hwtstamp_set(dev, req);
  1526. case SIOCGHWTSTAMP:
  1527. return cpsw_hwtstamp_get(dev, req);
  1528. }
  1529. if (!cpsw->slaves[slave_no].phy)
  1530. return -EOPNOTSUPP;
  1531. return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
  1532. }
  1533. static void cpsw_ndo_tx_timeout(struct net_device *ndev)
  1534. {
  1535. struct cpsw_priv *priv = netdev_priv(ndev);
  1536. struct cpsw_common *cpsw = priv->cpsw;
  1537. int ch;
  1538. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  1539. ndev->stats.tx_errors++;
  1540. cpsw_intr_disable(cpsw);
  1541. for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
  1542. cpdma_chan_stop(cpsw->txv[ch].ch);
  1543. cpdma_chan_start(cpsw->txv[ch].ch);
  1544. }
  1545. cpsw_intr_enable(cpsw);
  1546. }
  1547. static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
  1548. {
  1549. struct cpsw_priv *priv = netdev_priv(ndev);
  1550. struct sockaddr *addr = (struct sockaddr *)p;
  1551. struct cpsw_common *cpsw = priv->cpsw;
  1552. int flags = 0;
  1553. u16 vid = 0;
  1554. int ret;
  1555. if (!is_valid_ether_addr(addr->sa_data))
  1556. return -EADDRNOTAVAIL;
  1557. ret = pm_runtime_get_sync(cpsw->dev);
  1558. if (ret < 0) {
  1559. pm_runtime_put_noidle(cpsw->dev);
  1560. return ret;
  1561. }
  1562. if (cpsw->data.dual_emac) {
  1563. vid = cpsw->slaves[priv->emac_port].port_vlan;
  1564. flags = ALE_VLAN;
  1565. }
  1566. cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
  1567. flags, vid);
  1568. cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
  1569. flags, vid);
  1570. memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
  1571. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1572. for_each_slave(priv, cpsw_set_slave_mac, priv);
  1573. pm_runtime_put(cpsw->dev);
  1574. return 0;
  1575. }
  1576. #ifdef CONFIG_NET_POLL_CONTROLLER
  1577. static void cpsw_ndo_poll_controller(struct net_device *ndev)
  1578. {
  1579. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1580. cpsw_intr_disable(cpsw);
  1581. cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
  1582. cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
  1583. cpsw_intr_enable(cpsw);
  1584. }
  1585. #endif
  1586. static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
  1587. unsigned short vid)
  1588. {
  1589. int ret;
  1590. int unreg_mcast_mask = 0;
  1591. u32 port_mask;
  1592. struct cpsw_common *cpsw = priv->cpsw;
  1593. if (cpsw->data.dual_emac) {
  1594. port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
  1595. if (priv->ndev->flags & IFF_ALLMULTI)
  1596. unreg_mcast_mask = port_mask;
  1597. } else {
  1598. port_mask = ALE_ALL_PORTS;
  1599. if (priv->ndev->flags & IFF_ALLMULTI)
  1600. unreg_mcast_mask = ALE_ALL_PORTS;
  1601. else
  1602. unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
  1603. }
  1604. ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
  1605. unreg_mcast_mask);
  1606. if (ret != 0)
  1607. return ret;
  1608. ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
  1609. HOST_PORT_NUM, ALE_VLAN, vid);
  1610. if (ret != 0)
  1611. goto clean_vid;
  1612. ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
  1613. port_mask, ALE_VLAN, vid, 0);
  1614. if (ret != 0)
  1615. goto clean_vlan_ucast;
  1616. return 0;
  1617. clean_vlan_ucast:
  1618. cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
  1619. HOST_PORT_NUM, ALE_VLAN, vid);
  1620. clean_vid:
  1621. cpsw_ale_del_vlan(cpsw->ale, vid, 0);
  1622. return ret;
  1623. }
  1624. static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
  1625. __be16 proto, u16 vid)
  1626. {
  1627. struct cpsw_priv *priv = netdev_priv(ndev);
  1628. struct cpsw_common *cpsw = priv->cpsw;
  1629. int ret;
  1630. if (vid == cpsw->data.default_vlan)
  1631. return 0;
  1632. ret = pm_runtime_get_sync(cpsw->dev);
  1633. if (ret < 0) {
  1634. pm_runtime_put_noidle(cpsw->dev);
  1635. return ret;
  1636. }
  1637. if (cpsw->data.dual_emac) {
  1638. /* In dual EMAC, reserved VLAN id should not be used for
  1639. * creating VLAN interfaces as this can break the dual
  1640. * EMAC port separation
  1641. */
  1642. int i;
  1643. for (i = 0; i < cpsw->data.slaves; i++) {
  1644. if (vid == cpsw->slaves[i].port_vlan)
  1645. return -EINVAL;
  1646. }
  1647. }
  1648. dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
  1649. ret = cpsw_add_vlan_ale_entry(priv, vid);
  1650. pm_runtime_put(cpsw->dev);
  1651. return ret;
  1652. }
  1653. static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
  1654. __be16 proto, u16 vid)
  1655. {
  1656. struct cpsw_priv *priv = netdev_priv(ndev);
  1657. struct cpsw_common *cpsw = priv->cpsw;
  1658. int ret;
  1659. if (vid == cpsw->data.default_vlan)
  1660. return 0;
  1661. ret = pm_runtime_get_sync(cpsw->dev);
  1662. if (ret < 0) {
  1663. pm_runtime_put_noidle(cpsw->dev);
  1664. return ret;
  1665. }
  1666. if (cpsw->data.dual_emac) {
  1667. int i;
  1668. for (i = 0; i < cpsw->data.slaves; i++) {
  1669. if (vid == cpsw->slaves[i].port_vlan)
  1670. return -EINVAL;
  1671. }
  1672. }
  1673. dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
  1674. ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
  1675. if (ret != 0)
  1676. return ret;
  1677. ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
  1678. HOST_PORT_NUM, ALE_VLAN, vid);
  1679. if (ret != 0)
  1680. return ret;
  1681. ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
  1682. 0, ALE_VLAN, vid);
  1683. pm_runtime_put(cpsw->dev);
  1684. return ret;
  1685. }
  1686. static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
  1687. {
  1688. struct cpsw_priv *priv = netdev_priv(ndev);
  1689. struct cpsw_common *cpsw = priv->cpsw;
  1690. struct cpsw_slave *slave;
  1691. u32 min_rate;
  1692. u32 ch_rate;
  1693. int i, ret;
  1694. ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
  1695. if (ch_rate == rate)
  1696. return 0;
  1697. ch_rate = rate * 1000;
  1698. min_rate = cpdma_chan_get_min_rate(cpsw->dma);
  1699. if ((ch_rate < min_rate && ch_rate)) {
  1700. dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
  1701. min_rate);
  1702. return -EINVAL;
  1703. }
  1704. if (rate > cpsw->speed) {
  1705. dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
  1706. return -EINVAL;
  1707. }
  1708. ret = pm_runtime_get_sync(cpsw->dev);
  1709. if (ret < 0) {
  1710. pm_runtime_put_noidle(cpsw->dev);
  1711. return ret;
  1712. }
  1713. ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
  1714. pm_runtime_put(cpsw->dev);
  1715. if (ret)
  1716. return ret;
  1717. /* update rates for slaves tx queues */
  1718. for (i = 0; i < cpsw->data.slaves; i++) {
  1719. slave = &cpsw->slaves[i];
  1720. if (!slave->ndev)
  1721. continue;
  1722. netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
  1723. }
  1724. cpsw_split_res(ndev);
  1725. return ret;
  1726. }
  1727. static const struct net_device_ops cpsw_netdev_ops = {
  1728. .ndo_open = cpsw_ndo_open,
  1729. .ndo_stop = cpsw_ndo_stop,
  1730. .ndo_start_xmit = cpsw_ndo_start_xmit,
  1731. .ndo_set_mac_address = cpsw_ndo_set_mac_address,
  1732. .ndo_do_ioctl = cpsw_ndo_ioctl,
  1733. .ndo_validate_addr = eth_validate_addr,
  1734. .ndo_tx_timeout = cpsw_ndo_tx_timeout,
  1735. .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
  1736. .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate,
  1737. #ifdef CONFIG_NET_POLL_CONTROLLER
  1738. .ndo_poll_controller = cpsw_ndo_poll_controller,
  1739. #endif
  1740. .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
  1741. .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
  1742. };
  1743. static int cpsw_get_regs_len(struct net_device *ndev)
  1744. {
  1745. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1746. return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
  1747. }
  1748. static void cpsw_get_regs(struct net_device *ndev,
  1749. struct ethtool_regs *regs, void *p)
  1750. {
  1751. u32 *reg = p;
  1752. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1753. /* update CPSW IP version */
  1754. regs->version = cpsw->version;
  1755. cpsw_ale_dump(cpsw->ale, reg);
  1756. }
  1757. static void cpsw_get_drvinfo(struct net_device *ndev,
  1758. struct ethtool_drvinfo *info)
  1759. {
  1760. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1761. struct platform_device *pdev = to_platform_device(cpsw->dev);
  1762. strlcpy(info->driver, "cpsw", sizeof(info->driver));
  1763. strlcpy(info->version, "1.0", sizeof(info->version));
  1764. strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
  1765. }
  1766. static u32 cpsw_get_msglevel(struct net_device *ndev)
  1767. {
  1768. struct cpsw_priv *priv = netdev_priv(ndev);
  1769. return priv->msg_enable;
  1770. }
  1771. static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  1772. {
  1773. struct cpsw_priv *priv = netdev_priv(ndev);
  1774. priv->msg_enable = value;
  1775. }
  1776. #if IS_ENABLED(CONFIG_TI_CPTS)
  1777. static int cpsw_get_ts_info(struct net_device *ndev,
  1778. struct ethtool_ts_info *info)
  1779. {
  1780. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1781. info->so_timestamping =
  1782. SOF_TIMESTAMPING_TX_HARDWARE |
  1783. SOF_TIMESTAMPING_TX_SOFTWARE |
  1784. SOF_TIMESTAMPING_RX_HARDWARE |
  1785. SOF_TIMESTAMPING_RX_SOFTWARE |
  1786. SOF_TIMESTAMPING_SOFTWARE |
  1787. SOF_TIMESTAMPING_RAW_HARDWARE;
  1788. info->phc_index = cpsw->cpts->phc_index;
  1789. info->tx_types =
  1790. (1 << HWTSTAMP_TX_OFF) |
  1791. (1 << HWTSTAMP_TX_ON);
  1792. info->rx_filters =
  1793. (1 << HWTSTAMP_FILTER_NONE) |
  1794. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  1795. return 0;
  1796. }
  1797. #else
  1798. static int cpsw_get_ts_info(struct net_device *ndev,
  1799. struct ethtool_ts_info *info)
  1800. {
  1801. info->so_timestamping =
  1802. SOF_TIMESTAMPING_TX_SOFTWARE |
  1803. SOF_TIMESTAMPING_RX_SOFTWARE |
  1804. SOF_TIMESTAMPING_SOFTWARE;
  1805. info->phc_index = -1;
  1806. info->tx_types = 0;
  1807. info->rx_filters = 0;
  1808. return 0;
  1809. }
  1810. #endif
  1811. static int cpsw_get_link_ksettings(struct net_device *ndev,
  1812. struct ethtool_link_ksettings *ecmd)
  1813. {
  1814. struct cpsw_priv *priv = netdev_priv(ndev);
  1815. struct cpsw_common *cpsw = priv->cpsw;
  1816. int slave_no = cpsw_slave_index(cpsw, priv);
  1817. if (cpsw->slaves[slave_no].phy)
  1818. return phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy,
  1819. ecmd);
  1820. else
  1821. return -EOPNOTSUPP;
  1822. }
  1823. static int cpsw_set_link_ksettings(struct net_device *ndev,
  1824. const struct ethtool_link_ksettings *ecmd)
  1825. {
  1826. struct cpsw_priv *priv = netdev_priv(ndev);
  1827. struct cpsw_common *cpsw = priv->cpsw;
  1828. int slave_no = cpsw_slave_index(cpsw, priv);
  1829. if (cpsw->slaves[slave_no].phy)
  1830. return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
  1831. ecmd);
  1832. else
  1833. return -EOPNOTSUPP;
  1834. }
  1835. static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1836. {
  1837. struct cpsw_priv *priv = netdev_priv(ndev);
  1838. struct cpsw_common *cpsw = priv->cpsw;
  1839. int slave_no = cpsw_slave_index(cpsw, priv);
  1840. wol->supported = 0;
  1841. wol->wolopts = 0;
  1842. if (cpsw->slaves[slave_no].phy)
  1843. phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
  1844. }
  1845. static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1846. {
  1847. struct cpsw_priv *priv = netdev_priv(ndev);
  1848. struct cpsw_common *cpsw = priv->cpsw;
  1849. int slave_no = cpsw_slave_index(cpsw, priv);
  1850. if (cpsw->slaves[slave_no].phy)
  1851. return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
  1852. else
  1853. return -EOPNOTSUPP;
  1854. }
  1855. static void cpsw_get_pauseparam(struct net_device *ndev,
  1856. struct ethtool_pauseparam *pause)
  1857. {
  1858. struct cpsw_priv *priv = netdev_priv(ndev);
  1859. pause->autoneg = AUTONEG_DISABLE;
  1860. pause->rx_pause = priv->rx_pause ? true : false;
  1861. pause->tx_pause = priv->tx_pause ? true : false;
  1862. }
  1863. static int cpsw_set_pauseparam(struct net_device *ndev,
  1864. struct ethtool_pauseparam *pause)
  1865. {
  1866. struct cpsw_priv *priv = netdev_priv(ndev);
  1867. bool link;
  1868. priv->rx_pause = pause->rx_pause ? true : false;
  1869. priv->tx_pause = pause->tx_pause ? true : false;
  1870. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  1871. return 0;
  1872. }
  1873. static int cpsw_ethtool_op_begin(struct net_device *ndev)
  1874. {
  1875. struct cpsw_priv *priv = netdev_priv(ndev);
  1876. struct cpsw_common *cpsw = priv->cpsw;
  1877. int ret;
  1878. ret = pm_runtime_get_sync(cpsw->dev);
  1879. if (ret < 0) {
  1880. cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
  1881. pm_runtime_put_noidle(cpsw->dev);
  1882. }
  1883. return ret;
  1884. }
  1885. static void cpsw_ethtool_op_complete(struct net_device *ndev)
  1886. {
  1887. struct cpsw_priv *priv = netdev_priv(ndev);
  1888. int ret;
  1889. ret = pm_runtime_put(priv->cpsw->dev);
  1890. if (ret < 0)
  1891. cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
  1892. }
  1893. static void cpsw_get_channels(struct net_device *ndev,
  1894. struct ethtool_channels *ch)
  1895. {
  1896. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1897. ch->max_combined = 0;
  1898. ch->max_rx = CPSW_MAX_QUEUES;
  1899. ch->max_tx = CPSW_MAX_QUEUES;
  1900. ch->max_other = 0;
  1901. ch->other_count = 0;
  1902. ch->rx_count = cpsw->rx_ch_num;
  1903. ch->tx_count = cpsw->tx_ch_num;
  1904. ch->combined_count = 0;
  1905. }
  1906. static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
  1907. struct ethtool_channels *ch)
  1908. {
  1909. if (ch->combined_count)
  1910. return -EINVAL;
  1911. /* verify we have at least one channel in each direction */
  1912. if (!ch->rx_count || !ch->tx_count)
  1913. return -EINVAL;
  1914. if (ch->rx_count > cpsw->data.channels ||
  1915. ch->tx_count > cpsw->data.channels)
  1916. return -EINVAL;
  1917. return 0;
  1918. }
  1919. static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
  1920. {
  1921. int (*poll)(struct napi_struct *, int);
  1922. struct cpsw_common *cpsw = priv->cpsw;
  1923. void (*handler)(void *, int, int);
  1924. struct netdev_queue *queue;
  1925. struct cpsw_vector *vec;
  1926. int ret, *ch;
  1927. if (rx) {
  1928. ch = &cpsw->rx_ch_num;
  1929. vec = cpsw->rxv;
  1930. handler = cpsw_rx_handler;
  1931. poll = cpsw_rx_poll;
  1932. } else {
  1933. ch = &cpsw->tx_ch_num;
  1934. vec = cpsw->txv;
  1935. handler = cpsw_tx_handler;
  1936. poll = cpsw_tx_poll;
  1937. }
  1938. while (*ch < ch_num) {
  1939. vec[*ch].ch = cpdma_chan_create(cpsw->dma, *ch, handler, rx);
  1940. queue = netdev_get_tx_queue(priv->ndev, *ch);
  1941. queue->tx_maxrate = 0;
  1942. if (IS_ERR(vec[*ch].ch))
  1943. return PTR_ERR(vec[*ch].ch);
  1944. if (!vec[*ch].ch)
  1945. return -EINVAL;
  1946. cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
  1947. (rx ? "rx" : "tx"));
  1948. (*ch)++;
  1949. }
  1950. while (*ch > ch_num) {
  1951. (*ch)--;
  1952. ret = cpdma_chan_destroy(vec[*ch].ch);
  1953. if (ret)
  1954. return ret;
  1955. cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
  1956. (rx ? "rx" : "tx"));
  1957. }
  1958. return 0;
  1959. }
  1960. static int cpsw_update_channels(struct cpsw_priv *priv,
  1961. struct ethtool_channels *ch)
  1962. {
  1963. int ret;
  1964. ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
  1965. if (ret)
  1966. return ret;
  1967. ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
  1968. if (ret)
  1969. return ret;
  1970. return 0;
  1971. }
  1972. static int cpsw_set_channels(struct net_device *ndev,
  1973. struct ethtool_channels *chs)
  1974. {
  1975. struct cpsw_priv *priv = netdev_priv(ndev);
  1976. struct cpsw_common *cpsw = priv->cpsw;
  1977. struct cpsw_slave *slave;
  1978. int i, ret;
  1979. ret = cpsw_check_ch_settings(cpsw, chs);
  1980. if (ret < 0)
  1981. return ret;
  1982. /* Disable NAPI scheduling */
  1983. cpsw_intr_disable(cpsw);
  1984. /* Stop all transmit queues for every network device.
  1985. * Disable re-using rx descriptors with dormant_on.
  1986. */
  1987. for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
  1988. if (!(slave->ndev && netif_running(slave->ndev)))
  1989. continue;
  1990. netif_tx_stop_all_queues(slave->ndev);
  1991. netif_dormant_on(slave->ndev);
  1992. }
  1993. /* Handle rest of tx packets and stop cpdma channels */
  1994. cpdma_ctlr_stop(cpsw->dma);
  1995. ret = cpsw_update_channels(priv, chs);
  1996. if (ret)
  1997. goto err;
  1998. for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
  1999. if (!(slave->ndev && netif_running(slave->ndev)))
  2000. continue;
  2001. /* Inform stack about new count of queues */
  2002. ret = netif_set_real_num_tx_queues(slave->ndev,
  2003. cpsw->tx_ch_num);
  2004. if (ret) {
  2005. dev_err(priv->dev, "cannot set real number of tx queues\n");
  2006. goto err;
  2007. }
  2008. ret = netif_set_real_num_rx_queues(slave->ndev,
  2009. cpsw->rx_ch_num);
  2010. if (ret) {
  2011. dev_err(priv->dev, "cannot set real number of rx queues\n");
  2012. goto err;
  2013. }
  2014. /* Enable rx packets handling */
  2015. netif_dormant_off(slave->ndev);
  2016. }
  2017. if (cpsw_common_res_usage_state(cpsw)) {
  2018. ret = cpsw_fill_rx_channels(priv);
  2019. if (ret)
  2020. goto err;
  2021. cpsw_split_res(ndev);
  2022. /* After this receive is started */
  2023. cpdma_ctlr_start(cpsw->dma);
  2024. cpsw_intr_enable(cpsw);
  2025. }
  2026. /* Resume transmit for every affected interface */
  2027. for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
  2028. if (!(slave->ndev && netif_running(slave->ndev)))
  2029. continue;
  2030. netif_tx_start_all_queues(slave->ndev);
  2031. }
  2032. return 0;
  2033. err:
  2034. dev_err(priv->dev, "cannot update channels number, closing device\n");
  2035. dev_close(ndev);
  2036. return ret;
  2037. }
  2038. static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
  2039. {
  2040. struct cpsw_priv *priv = netdev_priv(ndev);
  2041. struct cpsw_common *cpsw = priv->cpsw;
  2042. int slave_no = cpsw_slave_index(cpsw, priv);
  2043. if (cpsw->slaves[slave_no].phy)
  2044. return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
  2045. else
  2046. return -EOPNOTSUPP;
  2047. }
  2048. static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
  2049. {
  2050. struct cpsw_priv *priv = netdev_priv(ndev);
  2051. struct cpsw_common *cpsw = priv->cpsw;
  2052. int slave_no = cpsw_slave_index(cpsw, priv);
  2053. if (cpsw->slaves[slave_no].phy)
  2054. return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
  2055. else
  2056. return -EOPNOTSUPP;
  2057. }
  2058. static int cpsw_nway_reset(struct net_device *ndev)
  2059. {
  2060. struct cpsw_priv *priv = netdev_priv(ndev);
  2061. struct cpsw_common *cpsw = priv->cpsw;
  2062. int slave_no = cpsw_slave_index(cpsw, priv);
  2063. if (cpsw->slaves[slave_no].phy)
  2064. return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
  2065. else
  2066. return -EOPNOTSUPP;
  2067. }
  2068. static const struct ethtool_ops cpsw_ethtool_ops = {
  2069. .get_drvinfo = cpsw_get_drvinfo,
  2070. .get_msglevel = cpsw_get_msglevel,
  2071. .set_msglevel = cpsw_set_msglevel,
  2072. .get_link = ethtool_op_get_link,
  2073. .get_ts_info = cpsw_get_ts_info,
  2074. .get_coalesce = cpsw_get_coalesce,
  2075. .set_coalesce = cpsw_set_coalesce,
  2076. .get_sset_count = cpsw_get_sset_count,
  2077. .get_strings = cpsw_get_strings,
  2078. .get_ethtool_stats = cpsw_get_ethtool_stats,
  2079. .get_pauseparam = cpsw_get_pauseparam,
  2080. .set_pauseparam = cpsw_set_pauseparam,
  2081. .get_wol = cpsw_get_wol,
  2082. .set_wol = cpsw_set_wol,
  2083. .get_regs_len = cpsw_get_regs_len,
  2084. .get_regs = cpsw_get_regs,
  2085. .begin = cpsw_ethtool_op_begin,
  2086. .complete = cpsw_ethtool_op_complete,
  2087. .get_channels = cpsw_get_channels,
  2088. .set_channels = cpsw_set_channels,
  2089. .get_link_ksettings = cpsw_get_link_ksettings,
  2090. .set_link_ksettings = cpsw_set_link_ksettings,
  2091. .get_eee = cpsw_get_eee,
  2092. .set_eee = cpsw_set_eee,
  2093. .nway_reset = cpsw_nway_reset,
  2094. };
  2095. static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
  2096. u32 slave_reg_ofs, u32 sliver_reg_ofs)
  2097. {
  2098. void __iomem *regs = cpsw->regs;
  2099. int slave_num = slave->slave_num;
  2100. struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
  2101. slave->data = data;
  2102. slave->regs = regs + slave_reg_ofs;
  2103. slave->sliver = regs + sliver_reg_ofs;
  2104. slave->port_vlan = data->dual_emac_res_vlan;
  2105. }
  2106. static int cpsw_probe_dt(struct cpsw_platform_data *data,
  2107. struct platform_device *pdev)
  2108. {
  2109. struct device_node *node = pdev->dev.of_node;
  2110. struct device_node *slave_node;
  2111. int i = 0, ret;
  2112. u32 prop;
  2113. if (!node)
  2114. return -EINVAL;
  2115. if (of_property_read_u32(node, "slaves", &prop)) {
  2116. dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
  2117. return -EINVAL;
  2118. }
  2119. data->slaves = prop;
  2120. if (of_property_read_u32(node, "active_slave", &prop)) {
  2121. dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
  2122. return -EINVAL;
  2123. }
  2124. data->active_slave = prop;
  2125. data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
  2126. * sizeof(struct cpsw_slave_data),
  2127. GFP_KERNEL);
  2128. if (!data->slave_data)
  2129. return -ENOMEM;
  2130. if (of_property_read_u32(node, "cpdma_channels", &prop)) {
  2131. dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
  2132. return -EINVAL;
  2133. }
  2134. data->channels = prop;
  2135. if (of_property_read_u32(node, "ale_entries", &prop)) {
  2136. dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
  2137. return -EINVAL;
  2138. }
  2139. data->ale_entries = prop;
  2140. if (of_property_read_u32(node, "bd_ram_size", &prop)) {
  2141. dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
  2142. return -EINVAL;
  2143. }
  2144. data->bd_ram_size = prop;
  2145. if (of_property_read_u32(node, "mac_control", &prop)) {
  2146. dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
  2147. return -EINVAL;
  2148. }
  2149. data->mac_control = prop;
  2150. if (of_property_read_bool(node, "dual_emac"))
  2151. data->dual_emac = 1;
  2152. /*
  2153. * Populate all the child nodes here...
  2154. */
  2155. ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
  2156. /* We do not want to force this, as in some cases may not have child */
  2157. if (ret)
  2158. dev_warn(&pdev->dev, "Doesn't have any child node\n");
  2159. for_each_available_child_of_node(node, slave_node) {
  2160. struct cpsw_slave_data *slave_data = data->slave_data + i;
  2161. const void *mac_addr = NULL;
  2162. int lenp;
  2163. const __be32 *parp;
  2164. /* This is no slave child node, continue */
  2165. if (strcmp(slave_node->name, "slave"))
  2166. continue;
  2167. slave_data->phy_node = of_parse_phandle(slave_node,
  2168. "phy-handle", 0);
  2169. parp = of_get_property(slave_node, "phy_id", &lenp);
  2170. if (slave_data->phy_node) {
  2171. dev_dbg(&pdev->dev,
  2172. "slave[%d] using phy-handle=\"%s\"\n",
  2173. i, slave_data->phy_node->full_name);
  2174. } else if (of_phy_is_fixed_link(slave_node)) {
  2175. /* In the case of a fixed PHY, the DT node associated
  2176. * to the PHY is the Ethernet MAC DT node.
  2177. */
  2178. ret = of_phy_register_fixed_link(slave_node);
  2179. if (ret) {
  2180. if (ret != -EPROBE_DEFER)
  2181. dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
  2182. return ret;
  2183. }
  2184. slave_data->phy_node = of_node_get(slave_node);
  2185. } else if (parp) {
  2186. u32 phyid;
  2187. struct device_node *mdio_node;
  2188. struct platform_device *mdio;
  2189. if (lenp != (sizeof(__be32) * 2)) {
  2190. dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
  2191. goto no_phy_slave;
  2192. }
  2193. mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
  2194. phyid = be32_to_cpup(parp+1);
  2195. mdio = of_find_device_by_node(mdio_node);
  2196. of_node_put(mdio_node);
  2197. if (!mdio) {
  2198. dev_err(&pdev->dev, "Missing mdio platform device\n");
  2199. return -EINVAL;
  2200. }
  2201. snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
  2202. PHY_ID_FMT, mdio->name, phyid);
  2203. put_device(&mdio->dev);
  2204. } else {
  2205. dev_err(&pdev->dev,
  2206. "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
  2207. i);
  2208. goto no_phy_slave;
  2209. }
  2210. slave_data->phy_if = of_get_phy_mode(slave_node);
  2211. if (slave_data->phy_if < 0) {
  2212. dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
  2213. i);
  2214. return slave_data->phy_if;
  2215. }
  2216. no_phy_slave:
  2217. mac_addr = of_get_mac_address(slave_node);
  2218. if (mac_addr) {
  2219. memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
  2220. } else {
  2221. ret = ti_cm_get_macid(&pdev->dev, i,
  2222. slave_data->mac_addr);
  2223. if (ret)
  2224. return ret;
  2225. }
  2226. if (data->dual_emac) {
  2227. if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
  2228. &prop)) {
  2229. dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
  2230. slave_data->dual_emac_res_vlan = i+1;
  2231. dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
  2232. slave_data->dual_emac_res_vlan, i);
  2233. } else {
  2234. slave_data->dual_emac_res_vlan = prop;
  2235. }
  2236. }
  2237. i++;
  2238. if (i == data->slaves)
  2239. break;
  2240. }
  2241. return 0;
  2242. }
  2243. static void cpsw_remove_dt(struct platform_device *pdev)
  2244. {
  2245. struct net_device *ndev = platform_get_drvdata(pdev);
  2246. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  2247. struct cpsw_platform_data *data = &cpsw->data;
  2248. struct device_node *node = pdev->dev.of_node;
  2249. struct device_node *slave_node;
  2250. int i = 0;
  2251. for_each_available_child_of_node(node, slave_node) {
  2252. struct cpsw_slave_data *slave_data = &data->slave_data[i];
  2253. if (strcmp(slave_node->name, "slave"))
  2254. continue;
  2255. if (of_phy_is_fixed_link(slave_node))
  2256. of_phy_deregister_fixed_link(slave_node);
  2257. of_node_put(slave_data->phy_node);
  2258. i++;
  2259. if (i == data->slaves)
  2260. break;
  2261. }
  2262. of_platform_depopulate(&pdev->dev);
  2263. }
  2264. static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
  2265. {
  2266. struct cpsw_common *cpsw = priv->cpsw;
  2267. struct cpsw_platform_data *data = &cpsw->data;
  2268. struct net_device *ndev;
  2269. struct cpsw_priv *priv_sl2;
  2270. int ret = 0;
  2271. ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
  2272. if (!ndev) {
  2273. dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
  2274. return -ENOMEM;
  2275. }
  2276. priv_sl2 = netdev_priv(ndev);
  2277. priv_sl2->cpsw = cpsw;
  2278. priv_sl2->ndev = ndev;
  2279. priv_sl2->dev = &ndev->dev;
  2280. priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  2281. if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
  2282. memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
  2283. ETH_ALEN);
  2284. dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
  2285. priv_sl2->mac_addr);
  2286. } else {
  2287. random_ether_addr(priv_sl2->mac_addr);
  2288. dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
  2289. priv_sl2->mac_addr);
  2290. }
  2291. memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
  2292. priv_sl2->emac_port = 1;
  2293. cpsw->slaves[1].ndev = ndev;
  2294. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  2295. ndev->netdev_ops = &cpsw_netdev_ops;
  2296. ndev->ethtool_ops = &cpsw_ethtool_ops;
  2297. /* register the network device */
  2298. SET_NETDEV_DEV(ndev, cpsw->dev);
  2299. ret = register_netdev(ndev);
  2300. if (ret) {
  2301. dev_err(cpsw->dev, "cpsw: error registering net device\n");
  2302. free_netdev(ndev);
  2303. ret = -ENODEV;
  2304. }
  2305. return ret;
  2306. }
  2307. #define CPSW_QUIRK_IRQ BIT(0)
  2308. static struct platform_device_id cpsw_devtype[] = {
  2309. {
  2310. /* keep it for existing comaptibles */
  2311. .name = "cpsw",
  2312. .driver_data = CPSW_QUIRK_IRQ,
  2313. }, {
  2314. .name = "am335x-cpsw",
  2315. .driver_data = CPSW_QUIRK_IRQ,
  2316. }, {
  2317. .name = "am4372-cpsw",
  2318. .driver_data = 0,
  2319. }, {
  2320. .name = "dra7-cpsw",
  2321. .driver_data = 0,
  2322. }, {
  2323. /* sentinel */
  2324. }
  2325. };
  2326. MODULE_DEVICE_TABLE(platform, cpsw_devtype);
  2327. enum ti_cpsw_type {
  2328. CPSW = 0,
  2329. AM335X_CPSW,
  2330. AM4372_CPSW,
  2331. DRA7_CPSW,
  2332. };
  2333. static const struct of_device_id cpsw_of_mtable[] = {
  2334. { .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], },
  2335. { .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], },
  2336. { .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], },
  2337. { .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], },
  2338. { /* sentinel */ },
  2339. };
  2340. MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
  2341. static int cpsw_probe(struct platform_device *pdev)
  2342. {
  2343. struct clk *clk;
  2344. struct cpsw_platform_data *data;
  2345. struct net_device *ndev;
  2346. struct cpsw_priv *priv;
  2347. struct cpdma_params dma_params;
  2348. struct cpsw_ale_params ale_params;
  2349. void __iomem *ss_regs;
  2350. void __iomem *cpts_regs;
  2351. struct resource *res, *ss_res;
  2352. const struct of_device_id *of_id;
  2353. struct gpio_descs *mode;
  2354. u32 slave_offset, sliver_offset, slave_size;
  2355. struct cpsw_common *cpsw;
  2356. int ret = 0, i;
  2357. int irq;
  2358. cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
  2359. if (!cpsw)
  2360. return -ENOMEM;
  2361. cpsw->dev = &pdev->dev;
  2362. ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
  2363. if (!ndev) {
  2364. dev_err(&pdev->dev, "error allocating net_device\n");
  2365. return -ENOMEM;
  2366. }
  2367. platform_set_drvdata(pdev, ndev);
  2368. priv = netdev_priv(ndev);
  2369. priv->cpsw = cpsw;
  2370. priv->ndev = ndev;
  2371. priv->dev = &ndev->dev;
  2372. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  2373. cpsw->rx_packet_max = max(rx_packet_max, 128);
  2374. mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
  2375. if (IS_ERR(mode)) {
  2376. ret = PTR_ERR(mode);
  2377. dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
  2378. goto clean_ndev_ret;
  2379. }
  2380. /*
  2381. * This may be required here for child devices.
  2382. */
  2383. pm_runtime_enable(&pdev->dev);
  2384. /* Select default pin state */
  2385. pinctrl_pm_select_default_state(&pdev->dev);
  2386. /* Need to enable clocks with runtime PM api to access module
  2387. * registers
  2388. */
  2389. ret = pm_runtime_get_sync(&pdev->dev);
  2390. if (ret < 0) {
  2391. pm_runtime_put_noidle(&pdev->dev);
  2392. goto clean_runtime_disable_ret;
  2393. }
  2394. ret = cpsw_probe_dt(&cpsw->data, pdev);
  2395. if (ret)
  2396. goto clean_dt_ret;
  2397. data = &cpsw->data;
  2398. cpsw->rx_ch_num = 1;
  2399. cpsw->tx_ch_num = 1;
  2400. if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
  2401. memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
  2402. dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
  2403. } else {
  2404. eth_random_addr(priv->mac_addr);
  2405. dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
  2406. }
  2407. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  2408. cpsw->slaves = devm_kzalloc(&pdev->dev,
  2409. sizeof(struct cpsw_slave) * data->slaves,
  2410. GFP_KERNEL);
  2411. if (!cpsw->slaves) {
  2412. ret = -ENOMEM;
  2413. goto clean_dt_ret;
  2414. }
  2415. for (i = 0; i < data->slaves; i++)
  2416. cpsw->slaves[i].slave_num = i;
  2417. cpsw->slaves[0].ndev = ndev;
  2418. priv->emac_port = 0;
  2419. clk = devm_clk_get(&pdev->dev, "fck");
  2420. if (IS_ERR(clk)) {
  2421. dev_err(priv->dev, "fck is not found\n");
  2422. ret = -ENODEV;
  2423. goto clean_dt_ret;
  2424. }
  2425. cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
  2426. ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2427. ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
  2428. if (IS_ERR(ss_regs)) {
  2429. ret = PTR_ERR(ss_regs);
  2430. goto clean_dt_ret;
  2431. }
  2432. cpsw->regs = ss_regs;
  2433. cpsw->version = readl(&cpsw->regs->id_ver);
  2434. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2435. cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
  2436. if (IS_ERR(cpsw->wr_regs)) {
  2437. ret = PTR_ERR(cpsw->wr_regs);
  2438. goto clean_dt_ret;
  2439. }
  2440. memset(&dma_params, 0, sizeof(dma_params));
  2441. memset(&ale_params, 0, sizeof(ale_params));
  2442. switch (cpsw->version) {
  2443. case CPSW_VERSION_1:
  2444. cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  2445. cpts_regs = ss_regs + CPSW1_CPTS_OFFSET;
  2446. cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
  2447. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  2448. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  2449. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  2450. slave_offset = CPSW1_SLAVE_OFFSET;
  2451. slave_size = CPSW1_SLAVE_SIZE;
  2452. sliver_offset = CPSW1_SLIVER_OFFSET;
  2453. dma_params.desc_mem_phys = 0;
  2454. break;
  2455. case CPSW_VERSION_2:
  2456. case CPSW_VERSION_3:
  2457. case CPSW_VERSION_4:
  2458. cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  2459. cpts_regs = ss_regs + CPSW2_CPTS_OFFSET;
  2460. cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
  2461. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  2462. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  2463. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  2464. slave_offset = CPSW2_SLAVE_OFFSET;
  2465. slave_size = CPSW2_SLAVE_SIZE;
  2466. sliver_offset = CPSW2_SLIVER_OFFSET;
  2467. dma_params.desc_mem_phys =
  2468. (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
  2469. break;
  2470. default:
  2471. dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
  2472. ret = -ENODEV;
  2473. goto clean_dt_ret;
  2474. }
  2475. for (i = 0; i < cpsw->data.slaves; i++) {
  2476. struct cpsw_slave *slave = &cpsw->slaves[i];
  2477. cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
  2478. slave_offset += slave_size;
  2479. sliver_offset += SLIVER_SIZE;
  2480. }
  2481. dma_params.dev = &pdev->dev;
  2482. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  2483. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  2484. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  2485. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  2486. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  2487. dma_params.num_chan = data->channels;
  2488. dma_params.has_soft_reset = true;
  2489. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  2490. dma_params.desc_mem_size = data->bd_ram_size;
  2491. dma_params.desc_align = 16;
  2492. dma_params.has_ext_regs = true;
  2493. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  2494. dma_params.bus_freq_mhz = cpsw->bus_freq_mhz;
  2495. cpsw->dma = cpdma_ctlr_create(&dma_params);
  2496. if (!cpsw->dma) {
  2497. dev_err(priv->dev, "error initializing dma\n");
  2498. ret = -ENOMEM;
  2499. goto clean_dt_ret;
  2500. }
  2501. cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_tx_handler, 0);
  2502. cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
  2503. if (WARN_ON(!cpsw->rxv[0].ch || !cpsw->txv[0].ch)) {
  2504. dev_err(priv->dev, "error initializing dma channels\n");
  2505. ret = -ENOMEM;
  2506. goto clean_dma_ret;
  2507. }
  2508. ale_params.dev = &ndev->dev;
  2509. ale_params.ale_ageout = ale_ageout;
  2510. ale_params.ale_entries = data->ale_entries;
  2511. ale_params.ale_ports = data->slaves;
  2512. cpsw->ale = cpsw_ale_create(&ale_params);
  2513. if (!cpsw->ale) {
  2514. dev_err(priv->dev, "error initializing ale engine\n");
  2515. ret = -ENODEV;
  2516. goto clean_dma_ret;
  2517. }
  2518. cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpsw->dev->of_node);
  2519. if (IS_ERR(cpsw->cpts)) {
  2520. ret = PTR_ERR(cpsw->cpts);
  2521. goto clean_ale_ret;
  2522. }
  2523. ndev->irq = platform_get_irq(pdev, 1);
  2524. if (ndev->irq < 0) {
  2525. dev_err(priv->dev, "error getting irq resource\n");
  2526. ret = ndev->irq;
  2527. goto clean_ale_ret;
  2528. }
  2529. of_id = of_match_device(cpsw_of_mtable, &pdev->dev);
  2530. if (of_id) {
  2531. pdev->id_entry = of_id->data;
  2532. if (pdev->id_entry->driver_data)
  2533. cpsw->quirk_irq = true;
  2534. }
  2535. /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
  2536. * MISC IRQs which are always kept disabled with this driver so
  2537. * we will not request them.
  2538. *
  2539. * If anyone wants to implement support for those, make sure to
  2540. * first request and append them to irqs_table array.
  2541. */
  2542. /* RX IRQ */
  2543. irq = platform_get_irq(pdev, 1);
  2544. if (irq < 0) {
  2545. ret = irq;
  2546. goto clean_ale_ret;
  2547. }
  2548. cpsw->irqs_table[0] = irq;
  2549. ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
  2550. 0, dev_name(&pdev->dev), cpsw);
  2551. if (ret < 0) {
  2552. dev_err(priv->dev, "error attaching irq (%d)\n", ret);
  2553. goto clean_ale_ret;
  2554. }
  2555. /* TX IRQ */
  2556. irq = platform_get_irq(pdev, 2);
  2557. if (irq < 0) {
  2558. ret = irq;
  2559. goto clean_ale_ret;
  2560. }
  2561. cpsw->irqs_table[1] = irq;
  2562. ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
  2563. 0, dev_name(&pdev->dev), cpsw);
  2564. if (ret < 0) {
  2565. dev_err(priv->dev, "error attaching irq (%d)\n", ret);
  2566. goto clean_ale_ret;
  2567. }
  2568. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  2569. ndev->netdev_ops = &cpsw_netdev_ops;
  2570. ndev->ethtool_ops = &cpsw_ethtool_ops;
  2571. netif_napi_add(ndev, &cpsw->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT);
  2572. netif_tx_napi_add(ndev, &cpsw->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT);
  2573. cpsw_split_res(ndev);
  2574. /* register the network device */
  2575. SET_NETDEV_DEV(ndev, &pdev->dev);
  2576. ret = register_netdev(ndev);
  2577. if (ret) {
  2578. dev_err(priv->dev, "error registering net device\n");
  2579. ret = -ENODEV;
  2580. goto clean_ale_ret;
  2581. }
  2582. cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n",
  2583. &ss_res->start, ndev->irq);
  2584. if (cpsw->data.dual_emac) {
  2585. ret = cpsw_probe_dual_emac(priv);
  2586. if (ret) {
  2587. cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
  2588. goto clean_unregister_netdev_ret;
  2589. }
  2590. }
  2591. pm_runtime_put(&pdev->dev);
  2592. return 0;
  2593. clean_unregister_netdev_ret:
  2594. unregister_netdev(ndev);
  2595. clean_ale_ret:
  2596. cpsw_ale_destroy(cpsw->ale);
  2597. clean_dma_ret:
  2598. cpdma_ctlr_destroy(cpsw->dma);
  2599. clean_dt_ret:
  2600. cpsw_remove_dt(pdev);
  2601. pm_runtime_put_sync(&pdev->dev);
  2602. clean_runtime_disable_ret:
  2603. pm_runtime_disable(&pdev->dev);
  2604. clean_ndev_ret:
  2605. free_netdev(priv->ndev);
  2606. return ret;
  2607. }
  2608. static int cpsw_remove(struct platform_device *pdev)
  2609. {
  2610. struct net_device *ndev = platform_get_drvdata(pdev);
  2611. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  2612. int ret;
  2613. ret = pm_runtime_get_sync(&pdev->dev);
  2614. if (ret < 0) {
  2615. pm_runtime_put_noidle(&pdev->dev);
  2616. return ret;
  2617. }
  2618. if (cpsw->data.dual_emac)
  2619. unregister_netdev(cpsw->slaves[1].ndev);
  2620. unregister_netdev(ndev);
  2621. cpts_release(cpsw->cpts);
  2622. cpsw_ale_destroy(cpsw->ale);
  2623. cpdma_ctlr_destroy(cpsw->dma);
  2624. cpsw_remove_dt(pdev);
  2625. pm_runtime_put_sync(&pdev->dev);
  2626. pm_runtime_disable(&pdev->dev);
  2627. if (cpsw->data.dual_emac)
  2628. free_netdev(cpsw->slaves[1].ndev);
  2629. free_netdev(ndev);
  2630. return 0;
  2631. }
  2632. #ifdef CONFIG_PM_SLEEP
  2633. static int cpsw_suspend(struct device *dev)
  2634. {
  2635. struct platform_device *pdev = to_platform_device(dev);
  2636. struct net_device *ndev = platform_get_drvdata(pdev);
  2637. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  2638. if (cpsw->data.dual_emac) {
  2639. int i;
  2640. for (i = 0; i < cpsw->data.slaves; i++) {
  2641. if (netif_running(cpsw->slaves[i].ndev))
  2642. cpsw_ndo_stop(cpsw->slaves[i].ndev);
  2643. }
  2644. } else {
  2645. if (netif_running(ndev))
  2646. cpsw_ndo_stop(ndev);
  2647. }
  2648. /* Select sleep pin state */
  2649. pinctrl_pm_select_sleep_state(dev);
  2650. return 0;
  2651. }
  2652. static int cpsw_resume(struct device *dev)
  2653. {
  2654. struct platform_device *pdev = to_platform_device(dev);
  2655. struct net_device *ndev = platform_get_drvdata(pdev);
  2656. struct cpsw_common *cpsw = netdev_priv(ndev);
  2657. /* Select default pin state */
  2658. pinctrl_pm_select_default_state(dev);
  2659. /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
  2660. rtnl_lock();
  2661. if (cpsw->data.dual_emac) {
  2662. int i;
  2663. for (i = 0; i < cpsw->data.slaves; i++) {
  2664. if (netif_running(cpsw->slaves[i].ndev))
  2665. cpsw_ndo_open(cpsw->slaves[i].ndev);
  2666. }
  2667. } else {
  2668. if (netif_running(ndev))
  2669. cpsw_ndo_open(ndev);
  2670. }
  2671. rtnl_unlock();
  2672. return 0;
  2673. }
  2674. #endif
  2675. static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
  2676. static struct platform_driver cpsw_driver = {
  2677. .driver = {
  2678. .name = "cpsw",
  2679. .pm = &cpsw_pm_ops,
  2680. .of_match_table = cpsw_of_mtable,
  2681. },
  2682. .probe = cpsw_probe,
  2683. .remove = cpsw_remove,
  2684. };
  2685. module_platform_driver(cpsw_driver);
  2686. MODULE_LICENSE("GPL");
  2687. MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
  2688. MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
  2689. MODULE_DESCRIPTION("TI CPSW Ethernet driver");