cpsw.c 85 KB

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