cpsw.c 71 KB

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