cpsw.c 78 KB

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