cpsw.c 89 KB

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