cpsw.c 88 KB

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