cpsw.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603
  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/of.h>
  32. #include <linux/of_mdio.h>
  33. #include <linux/of_net.h>
  34. #include <linux/of_device.h>
  35. #include <linux/if_vlan.h>
  36. #include <linux/pinctrl/consumer.h>
  37. #include "cpsw.h"
  38. #include "cpsw_ale.h"
  39. #include "cpts.h"
  40. #include "davinci_cpdma.h"
  41. #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
  42. NETIF_MSG_DRV | NETIF_MSG_LINK | \
  43. NETIF_MSG_IFUP | NETIF_MSG_INTR | \
  44. NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
  45. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
  46. NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
  47. NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
  48. NETIF_MSG_RX_STATUS)
  49. #define cpsw_info(priv, type, format, ...) \
  50. do { \
  51. if (netif_msg_##type(priv) && net_ratelimit()) \
  52. dev_info(priv->dev, format, ## __VA_ARGS__); \
  53. } while (0)
  54. #define cpsw_err(priv, type, format, ...) \
  55. do { \
  56. if (netif_msg_##type(priv) && net_ratelimit()) \
  57. dev_err(priv->dev, format, ## __VA_ARGS__); \
  58. } while (0)
  59. #define cpsw_dbg(priv, type, format, ...) \
  60. do { \
  61. if (netif_msg_##type(priv) && net_ratelimit()) \
  62. dev_dbg(priv->dev, format, ## __VA_ARGS__); \
  63. } while (0)
  64. #define cpsw_notice(priv, type, format, ...) \
  65. do { \
  66. if (netif_msg_##type(priv) && net_ratelimit()) \
  67. dev_notice(priv->dev, format, ## __VA_ARGS__); \
  68. } while (0)
  69. #define ALE_ALL_PORTS 0x7
  70. #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
  71. #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
  72. #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
  73. #define CPSW_VERSION_1 0x19010a
  74. #define CPSW_VERSION_2 0x19010c
  75. #define CPSW_VERSION_3 0x19010f
  76. #define CPSW_VERSION_4 0x190112
  77. #define HOST_PORT_NUM 0
  78. #define SLIVER_SIZE 0x40
  79. #define CPSW1_HOST_PORT_OFFSET 0x028
  80. #define CPSW1_SLAVE_OFFSET 0x050
  81. #define CPSW1_SLAVE_SIZE 0x040
  82. #define CPSW1_CPDMA_OFFSET 0x100
  83. #define CPSW1_STATERAM_OFFSET 0x200
  84. #define CPSW1_HW_STATS 0x400
  85. #define CPSW1_CPTS_OFFSET 0x500
  86. #define CPSW1_ALE_OFFSET 0x600
  87. #define CPSW1_SLIVER_OFFSET 0x700
  88. #define CPSW2_HOST_PORT_OFFSET 0x108
  89. #define CPSW2_SLAVE_OFFSET 0x200
  90. #define CPSW2_SLAVE_SIZE 0x100
  91. #define CPSW2_CPDMA_OFFSET 0x800
  92. #define CPSW2_HW_STATS 0x900
  93. #define CPSW2_STATERAM_OFFSET 0xa00
  94. #define CPSW2_CPTS_OFFSET 0xc00
  95. #define CPSW2_ALE_OFFSET 0xd00
  96. #define CPSW2_SLIVER_OFFSET 0xd80
  97. #define CPSW2_BD_OFFSET 0x2000
  98. #define CPDMA_RXTHRESH 0x0c0
  99. #define CPDMA_RXFREE 0x0e0
  100. #define CPDMA_TXHDP 0x00
  101. #define CPDMA_RXHDP 0x20
  102. #define CPDMA_TXCP 0x40
  103. #define CPDMA_RXCP 0x60
  104. #define CPSW_POLL_WEIGHT 64
  105. #define CPSW_MIN_PACKET_SIZE 60
  106. #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
  107. #define RX_PRIORITY_MAPPING 0x76543210
  108. #define TX_PRIORITY_MAPPING 0x33221100
  109. #define CPDMA_TX_PRIORITY_MAP 0x76543210
  110. #define CPSW_VLAN_AWARE BIT(1)
  111. #define CPSW_ALE_VLAN_AWARE 1
  112. #define CPSW_FIFO_NORMAL_MODE (0 << 16)
  113. #define CPSW_FIFO_DUAL_MAC_MODE (1 << 16)
  114. #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16)
  115. #define CPSW_INTPACEEN (0x3f << 16)
  116. #define CPSW_INTPRESCALE_MASK (0x7FF << 0)
  117. #define CPSW_CMINTMAX_CNT 63
  118. #define CPSW_CMINTMIN_CNT 2
  119. #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
  120. #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
  121. #define cpsw_slave_index(priv) \
  122. ((priv->data.dual_emac) ? priv->emac_port : \
  123. priv->data.active_slave)
  124. static int debug_level;
  125. module_param(debug_level, int, 0);
  126. MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
  127. static int ale_ageout = 10;
  128. module_param(ale_ageout, int, 0);
  129. MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
  130. static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  131. module_param(rx_packet_max, int, 0);
  132. MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
  133. struct cpsw_wr_regs {
  134. u32 id_ver;
  135. u32 soft_reset;
  136. u32 control;
  137. u32 int_control;
  138. u32 rx_thresh_en;
  139. u32 rx_en;
  140. u32 tx_en;
  141. u32 misc_en;
  142. u32 mem_allign1[8];
  143. u32 rx_thresh_stat;
  144. u32 rx_stat;
  145. u32 tx_stat;
  146. u32 misc_stat;
  147. u32 mem_allign2[8];
  148. u32 rx_imax;
  149. u32 tx_imax;
  150. };
  151. struct cpsw_ss_regs {
  152. u32 id_ver;
  153. u32 control;
  154. u32 soft_reset;
  155. u32 stat_port_en;
  156. u32 ptype;
  157. u32 soft_idle;
  158. u32 thru_rate;
  159. u32 gap_thresh;
  160. u32 tx_start_wds;
  161. u32 flow_control;
  162. u32 vlan_ltype;
  163. u32 ts_ltype;
  164. u32 dlr_ltype;
  165. };
  166. /* CPSW_PORT_V1 */
  167. #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
  168. #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
  169. #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
  170. #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
  171. #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
  172. #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
  173. #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
  174. #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
  175. /* CPSW_PORT_V2 */
  176. #define CPSW2_CONTROL 0x00 /* Control Register */
  177. #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
  178. #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
  179. #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
  180. #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
  181. #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
  182. #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
  183. /* CPSW_PORT_V1 and V2 */
  184. #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
  185. #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
  186. #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
  187. /* CPSW_PORT_V2 only */
  188. #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
  189. #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
  190. #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
  191. #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
  192. #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
  193. #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
  194. #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
  195. #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
  196. /* Bit definitions for the CPSW2_CONTROL register */
  197. #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
  198. #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
  199. #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
  200. #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
  201. #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
  202. #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
  203. #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
  204. #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
  205. #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
  206. #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
  207. #define TS_TTL_NONZERO (1<<8) /* Time Sync Time To Live Non-zero enable */
  208. #define TS_ANNEX_F_EN (1<<6) /* Time Sync Annex F enable */
  209. #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
  210. #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
  211. #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
  212. #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
  213. #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
  214. #define CTRL_V2_TS_BITS \
  215. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
  216. TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
  217. #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
  218. #define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
  219. #define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
  220. #define CTRL_V3_TS_BITS \
  221. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
  222. TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
  223. TS_LTYPE1_EN)
  224. #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
  225. #define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
  226. #define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
  227. /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
  228. #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
  229. #define TS_SEQ_ID_OFFSET_MASK (0x3f)
  230. #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
  231. #define TS_MSG_TYPE_EN_MASK (0xffff)
  232. /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
  233. #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
  234. /* Bit definitions for the CPSW1_TS_CTL register */
  235. #define CPSW_V1_TS_RX_EN BIT(0)
  236. #define CPSW_V1_TS_TX_EN BIT(4)
  237. #define CPSW_V1_MSG_TYPE_OFS 16
  238. /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
  239. #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
  240. struct cpsw_host_regs {
  241. u32 max_blks;
  242. u32 blk_cnt;
  243. u32 tx_in_ctl;
  244. u32 port_vlan;
  245. u32 tx_pri_map;
  246. u32 cpdma_tx_pri_map;
  247. u32 cpdma_rx_chan_map;
  248. };
  249. struct cpsw_sliver_regs {
  250. u32 id_ver;
  251. u32 mac_control;
  252. u32 mac_status;
  253. u32 soft_reset;
  254. u32 rx_maxlen;
  255. u32 __reserved_0;
  256. u32 rx_pause;
  257. u32 tx_pause;
  258. u32 __reserved_1;
  259. u32 rx_pri_map;
  260. };
  261. struct cpsw_hw_stats {
  262. u32 rxgoodframes;
  263. u32 rxbroadcastframes;
  264. u32 rxmulticastframes;
  265. u32 rxpauseframes;
  266. u32 rxcrcerrors;
  267. u32 rxaligncodeerrors;
  268. u32 rxoversizedframes;
  269. u32 rxjabberframes;
  270. u32 rxundersizedframes;
  271. u32 rxfragments;
  272. u32 __pad_0[2];
  273. u32 rxoctets;
  274. u32 txgoodframes;
  275. u32 txbroadcastframes;
  276. u32 txmulticastframes;
  277. u32 txpauseframes;
  278. u32 txdeferredframes;
  279. u32 txcollisionframes;
  280. u32 txsinglecollframes;
  281. u32 txmultcollframes;
  282. u32 txexcessivecollisions;
  283. u32 txlatecollisions;
  284. u32 txunderrun;
  285. u32 txcarriersenseerrors;
  286. u32 txoctets;
  287. u32 octetframes64;
  288. u32 octetframes65t127;
  289. u32 octetframes128t255;
  290. u32 octetframes256t511;
  291. u32 octetframes512t1023;
  292. u32 octetframes1024tup;
  293. u32 netoctets;
  294. u32 rxsofoverruns;
  295. u32 rxmofoverruns;
  296. u32 rxdmaoverruns;
  297. };
  298. struct cpsw_slave {
  299. void __iomem *regs;
  300. struct cpsw_sliver_regs __iomem *sliver;
  301. int slave_num;
  302. u32 mac_control;
  303. struct cpsw_slave_data *data;
  304. struct phy_device *phy;
  305. struct net_device *ndev;
  306. u32 port_vlan;
  307. u32 open_stat;
  308. };
  309. static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
  310. {
  311. return __raw_readl(slave->regs + offset);
  312. }
  313. static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
  314. {
  315. __raw_writel(val, slave->regs + offset);
  316. }
  317. struct cpsw_priv {
  318. spinlock_t lock;
  319. struct platform_device *pdev;
  320. struct net_device *ndev;
  321. struct device_node *phy_node;
  322. struct napi_struct napi_rx;
  323. struct napi_struct napi_tx;
  324. struct device *dev;
  325. struct cpsw_platform_data data;
  326. struct cpsw_ss_regs __iomem *regs;
  327. struct cpsw_wr_regs __iomem *wr_regs;
  328. u8 __iomem *hw_stats;
  329. struct cpsw_host_regs __iomem *host_port_regs;
  330. u32 msg_enable;
  331. u32 version;
  332. u32 coal_intvl;
  333. u32 bus_freq_mhz;
  334. int rx_packet_max;
  335. int host_port;
  336. struct clk *clk;
  337. u8 mac_addr[ETH_ALEN];
  338. struct cpsw_slave *slaves;
  339. struct cpdma_ctlr *dma;
  340. struct cpdma_chan *txch, *rxch;
  341. struct cpsw_ale *ale;
  342. bool rx_pause;
  343. bool tx_pause;
  344. bool quirk_irq;
  345. bool rx_irq_disabled;
  346. bool tx_irq_disabled;
  347. /* snapshot of IRQ numbers */
  348. u32 irqs_table[4];
  349. u32 num_irqs;
  350. struct cpts *cpts;
  351. u32 emac_port;
  352. };
  353. struct cpsw_stats {
  354. char stat_string[ETH_GSTRING_LEN];
  355. int type;
  356. int sizeof_stat;
  357. int stat_offset;
  358. };
  359. enum {
  360. CPSW_STATS,
  361. CPDMA_RX_STATS,
  362. CPDMA_TX_STATS,
  363. };
  364. #define CPSW_STAT(m) CPSW_STATS, \
  365. sizeof(((struct cpsw_hw_stats *)0)->m), \
  366. offsetof(struct cpsw_hw_stats, m)
  367. #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
  368. sizeof(((struct cpdma_chan_stats *)0)->m), \
  369. offsetof(struct cpdma_chan_stats, m)
  370. #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
  371. sizeof(((struct cpdma_chan_stats *)0)->m), \
  372. offsetof(struct cpdma_chan_stats, m)
  373. static const struct cpsw_stats cpsw_gstrings_stats[] = {
  374. { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
  375. { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
  376. { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
  377. { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
  378. { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
  379. { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
  380. { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
  381. { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
  382. { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
  383. { "Rx Fragments", CPSW_STAT(rxfragments) },
  384. { "Rx Octets", CPSW_STAT(rxoctets) },
  385. { "Good Tx Frames", CPSW_STAT(txgoodframes) },
  386. { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
  387. { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
  388. { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
  389. { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
  390. { "Collisions", CPSW_STAT(txcollisionframes) },
  391. { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
  392. { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
  393. { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
  394. { "Late Collisions", CPSW_STAT(txlatecollisions) },
  395. { "Tx Underrun", CPSW_STAT(txunderrun) },
  396. { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
  397. { "Tx Octets", CPSW_STAT(txoctets) },
  398. { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
  399. { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
  400. { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
  401. { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
  402. { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
  403. { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
  404. { "Net Octets", CPSW_STAT(netoctets) },
  405. { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
  406. { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
  407. { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
  408. { "Rx DMA chan: head_enqueue", CPDMA_RX_STAT(head_enqueue) },
  409. { "Rx DMA chan: tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
  410. { "Rx DMA chan: pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
  411. { "Rx DMA chan: misqueued", CPDMA_RX_STAT(misqueued) },
  412. { "Rx DMA chan: desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
  413. { "Rx DMA chan: pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
  414. { "Rx DMA chan: runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
  415. { "Rx DMA chan: runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
  416. { "Rx DMA chan: empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
  417. { "Rx DMA chan: busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
  418. { "Rx DMA chan: good_dequeue", CPDMA_RX_STAT(good_dequeue) },
  419. { "Rx DMA chan: requeue", CPDMA_RX_STAT(requeue) },
  420. { "Rx DMA chan: teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
  421. { "Tx DMA chan: head_enqueue", CPDMA_TX_STAT(head_enqueue) },
  422. { "Tx DMA chan: tail_enqueue", CPDMA_TX_STAT(tail_enqueue) },
  423. { "Tx DMA chan: pad_enqueue", CPDMA_TX_STAT(pad_enqueue) },
  424. { "Tx DMA chan: misqueued", CPDMA_TX_STAT(misqueued) },
  425. { "Tx DMA chan: desc_alloc_fail", CPDMA_TX_STAT(desc_alloc_fail) },
  426. { "Tx DMA chan: pad_alloc_fail", CPDMA_TX_STAT(pad_alloc_fail) },
  427. { "Tx DMA chan: runt_receive_buf", CPDMA_TX_STAT(runt_receive_buff) },
  428. { "Tx DMA chan: runt_transmit_buf", CPDMA_TX_STAT(runt_transmit_buff) },
  429. { "Tx DMA chan: empty_dequeue", CPDMA_TX_STAT(empty_dequeue) },
  430. { "Tx DMA chan: busy_dequeue", CPDMA_TX_STAT(busy_dequeue) },
  431. { "Tx DMA chan: good_dequeue", CPDMA_TX_STAT(good_dequeue) },
  432. { "Tx DMA chan: requeue", CPDMA_TX_STAT(requeue) },
  433. { "Tx DMA chan: teardown_dequeue", CPDMA_TX_STAT(teardown_dequeue) },
  434. };
  435. #define CPSW_STATS_LEN ARRAY_SIZE(cpsw_gstrings_stats)
  436. #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
  437. #define for_each_slave(priv, func, arg...) \
  438. do { \
  439. struct cpsw_slave *slave; \
  440. int n; \
  441. if (priv->data.dual_emac) \
  442. (func)((priv)->slaves + priv->emac_port, ##arg);\
  443. else \
  444. for (n = (priv)->data.slaves, \
  445. slave = (priv)->slaves; \
  446. n; n--) \
  447. (func)(slave++, ##arg); \
  448. } while (0)
  449. #define cpsw_get_slave_ndev(priv, __slave_no__) \
  450. ((__slave_no__ < priv->data.slaves) ? \
  451. priv->slaves[__slave_no__].ndev : NULL)
  452. #define cpsw_get_slave_priv(priv, __slave_no__) \
  453. (((__slave_no__ < priv->data.slaves) && \
  454. (priv->slaves[__slave_no__].ndev)) ? \
  455. netdev_priv(priv->slaves[__slave_no__].ndev) : NULL) \
  456. #define cpsw_dual_emac_src_port_detect(status, priv, ndev, skb) \
  457. do { \
  458. if (!priv->data.dual_emac) \
  459. break; \
  460. if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
  461. ndev = cpsw_get_slave_ndev(priv, 0); \
  462. priv = netdev_priv(ndev); \
  463. skb->dev = ndev; \
  464. } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
  465. ndev = cpsw_get_slave_ndev(priv, 1); \
  466. priv = netdev_priv(ndev); \
  467. skb->dev = ndev; \
  468. } \
  469. } while (0)
  470. #define cpsw_add_mcast(priv, addr) \
  471. do { \
  472. if (priv->data.dual_emac) { \
  473. struct cpsw_slave *slave = priv->slaves + \
  474. priv->emac_port; \
  475. int slave_port = cpsw_get_slave_port(priv, \
  476. slave->slave_num); \
  477. cpsw_ale_add_mcast(priv->ale, addr, \
  478. 1 << slave_port | 1 << priv->host_port, \
  479. ALE_VLAN, slave->port_vlan, 0); \
  480. } else { \
  481. cpsw_ale_add_mcast(priv->ale, addr, \
  482. ALE_ALL_PORTS << priv->host_port, \
  483. 0, 0, 0); \
  484. } \
  485. } while (0)
  486. static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
  487. {
  488. if (priv->host_port == 0)
  489. return slave_num + 1;
  490. else
  491. return slave_num;
  492. }
  493. static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
  494. {
  495. struct cpsw_priv *priv = netdev_priv(ndev);
  496. struct cpsw_ale *ale = priv->ale;
  497. int i;
  498. if (priv->data.dual_emac) {
  499. bool flag = false;
  500. /* Enabling promiscuous mode for one interface will be
  501. * common for both the interface as the interface shares
  502. * the same hardware resource.
  503. */
  504. for (i = 0; i < priv->data.slaves; i++)
  505. if (priv->slaves[i].ndev->flags & IFF_PROMISC)
  506. flag = true;
  507. if (!enable && flag) {
  508. enable = true;
  509. dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
  510. }
  511. if (enable) {
  512. /* Enable Bypass */
  513. cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
  514. dev_dbg(&ndev->dev, "promiscuity enabled\n");
  515. } else {
  516. /* Disable Bypass */
  517. cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
  518. dev_dbg(&ndev->dev, "promiscuity disabled\n");
  519. }
  520. } else {
  521. if (enable) {
  522. unsigned long timeout = jiffies + HZ;
  523. /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
  524. for (i = 0; i <= priv->data.slaves; i++) {
  525. cpsw_ale_control_set(ale, i,
  526. ALE_PORT_NOLEARN, 1);
  527. cpsw_ale_control_set(ale, i,
  528. ALE_PORT_NO_SA_UPDATE, 1);
  529. }
  530. /* Clear All Untouched entries */
  531. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  532. do {
  533. cpu_relax();
  534. if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
  535. break;
  536. } while (time_after(timeout, jiffies));
  537. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  538. /* Clear all mcast from ALE */
  539. cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS <<
  540. priv->host_port, -1);
  541. /* Flood All Unicast Packets to Host port */
  542. cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
  543. dev_dbg(&ndev->dev, "promiscuity enabled\n");
  544. } else {
  545. /* Don't Flood All Unicast Packets to Host port */
  546. cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
  547. /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
  548. for (i = 0; i <= priv->data.slaves; i++) {
  549. cpsw_ale_control_set(ale, i,
  550. ALE_PORT_NOLEARN, 0);
  551. cpsw_ale_control_set(ale, i,
  552. ALE_PORT_NO_SA_UPDATE, 0);
  553. }
  554. dev_dbg(&ndev->dev, "promiscuity disabled\n");
  555. }
  556. }
  557. }
  558. static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
  559. {
  560. struct cpsw_priv *priv = netdev_priv(ndev);
  561. int vid;
  562. if (priv->data.dual_emac)
  563. vid = priv->slaves[priv->emac_port].port_vlan;
  564. else
  565. vid = priv->data.default_vlan;
  566. if (ndev->flags & IFF_PROMISC) {
  567. /* Enable promiscuous mode */
  568. cpsw_set_promiscious(ndev, true);
  569. cpsw_ale_set_allmulti(priv->ale, IFF_ALLMULTI);
  570. return;
  571. } else {
  572. /* Disable promiscuous mode */
  573. cpsw_set_promiscious(ndev, false);
  574. }
  575. /* Restore allmulti on vlans if necessary */
  576. cpsw_ale_set_allmulti(priv->ale, priv->ndev->flags & IFF_ALLMULTI);
  577. /* Clear all mcast from ALE */
  578. cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port,
  579. vid);
  580. if (!netdev_mc_empty(ndev)) {
  581. struct netdev_hw_addr *ha;
  582. /* program multicast address list into ALE register */
  583. netdev_for_each_mc_addr(ha, ndev) {
  584. cpsw_add_mcast(priv, (u8 *)ha->addr);
  585. }
  586. }
  587. }
  588. static void cpsw_intr_enable(struct cpsw_priv *priv)
  589. {
  590. __raw_writel(0xFF, &priv->wr_regs->tx_en);
  591. __raw_writel(0xFF, &priv->wr_regs->rx_en);
  592. cpdma_ctlr_int_ctrl(priv->dma, true);
  593. return;
  594. }
  595. static void cpsw_intr_disable(struct cpsw_priv *priv)
  596. {
  597. __raw_writel(0, &priv->wr_regs->tx_en);
  598. __raw_writel(0, &priv->wr_regs->rx_en);
  599. cpdma_ctlr_int_ctrl(priv->dma, false);
  600. return;
  601. }
  602. static void cpsw_tx_handler(void *token, int len, int status)
  603. {
  604. struct sk_buff *skb = token;
  605. struct net_device *ndev = skb->dev;
  606. struct cpsw_priv *priv = netdev_priv(ndev);
  607. /* Check whether the queue is stopped due to stalled tx dma, if the
  608. * queue is stopped then start the queue as we have free desc for tx
  609. */
  610. if (unlikely(netif_queue_stopped(ndev)))
  611. netif_wake_queue(ndev);
  612. cpts_tx_timestamp(priv->cpts, skb);
  613. ndev->stats.tx_packets++;
  614. ndev->stats.tx_bytes += len;
  615. dev_kfree_skb_any(skb);
  616. }
  617. static void cpsw_rx_handler(void *token, int len, int status)
  618. {
  619. struct sk_buff *skb = token;
  620. struct sk_buff *new_skb;
  621. struct net_device *ndev = skb->dev;
  622. struct cpsw_priv *priv = netdev_priv(ndev);
  623. int ret = 0;
  624. cpsw_dual_emac_src_port_detect(status, priv, ndev, skb);
  625. if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
  626. bool ndev_status = false;
  627. struct cpsw_slave *slave = priv->slaves;
  628. int n;
  629. if (priv->data.dual_emac) {
  630. /* In dual emac mode check for all interfaces */
  631. for (n = priv->data.slaves; n; n--, slave++)
  632. if (netif_running(slave->ndev))
  633. ndev_status = true;
  634. }
  635. if (ndev_status && (status >= 0)) {
  636. /* The packet received is for the interface which
  637. * is already down and the other interface is up
  638. * and running, instead of freeing which results
  639. * in reducing of the number of rx descriptor in
  640. * DMA engine, requeue skb back to cpdma.
  641. */
  642. new_skb = skb;
  643. goto requeue;
  644. }
  645. /* the interface is going down, skbs are purged */
  646. dev_kfree_skb_any(skb);
  647. return;
  648. }
  649. new_skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
  650. if (new_skb) {
  651. skb_put(skb, len);
  652. cpts_rx_timestamp(priv->cpts, skb);
  653. skb->protocol = eth_type_trans(skb, ndev);
  654. netif_receive_skb(skb);
  655. ndev->stats.rx_bytes += len;
  656. ndev->stats.rx_packets++;
  657. } else {
  658. ndev->stats.rx_dropped++;
  659. new_skb = skb;
  660. }
  661. requeue:
  662. ret = cpdma_chan_submit(priv->rxch, new_skb, new_skb->data,
  663. skb_tailroom(new_skb), 0);
  664. if (WARN_ON(ret < 0))
  665. dev_kfree_skb_any(new_skb);
  666. }
  667. static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
  668. {
  669. struct cpsw_priv *priv = dev_id;
  670. writel(0, &priv->wr_regs->tx_en);
  671. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  672. if (priv->quirk_irq) {
  673. disable_irq_nosync(priv->irqs_table[1]);
  674. priv->tx_irq_disabled = true;
  675. }
  676. napi_schedule(&priv->napi_tx);
  677. return IRQ_HANDLED;
  678. }
  679. static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
  680. {
  681. struct cpsw_priv *priv = dev_id;
  682. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  683. writel(0, &priv->wr_regs->rx_en);
  684. if (priv->quirk_irq) {
  685. disable_irq_nosync(priv->irqs_table[0]);
  686. priv->rx_irq_disabled = true;
  687. }
  688. napi_schedule(&priv->napi_rx);
  689. return IRQ_HANDLED;
  690. }
  691. static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
  692. {
  693. struct cpsw_priv *priv = napi_to_priv(napi_tx);
  694. int num_tx;
  695. num_tx = cpdma_chan_process(priv->txch, budget);
  696. if (num_tx < budget) {
  697. napi_complete(napi_tx);
  698. writel(0xff, &priv->wr_regs->tx_en);
  699. if (priv->quirk_irq && priv->tx_irq_disabled) {
  700. priv->tx_irq_disabled = false;
  701. enable_irq(priv->irqs_table[1]);
  702. }
  703. }
  704. if (num_tx)
  705. cpsw_dbg(priv, intr, "poll %d tx pkts\n", num_tx);
  706. return num_tx;
  707. }
  708. static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
  709. {
  710. struct cpsw_priv *priv = napi_to_priv(napi_rx);
  711. int num_rx;
  712. num_rx = cpdma_chan_process(priv->rxch, budget);
  713. if (num_rx < budget) {
  714. napi_complete(napi_rx);
  715. writel(0xff, &priv->wr_regs->rx_en);
  716. if (priv->quirk_irq && priv->rx_irq_disabled) {
  717. priv->rx_irq_disabled = false;
  718. enable_irq(priv->irqs_table[0]);
  719. }
  720. }
  721. if (num_rx)
  722. cpsw_dbg(priv, intr, "poll %d rx pkts\n", num_rx);
  723. return num_rx;
  724. }
  725. static inline void soft_reset(const char *module, void __iomem *reg)
  726. {
  727. unsigned long timeout = jiffies + HZ;
  728. __raw_writel(1, reg);
  729. do {
  730. cpu_relax();
  731. } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
  732. WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
  733. }
  734. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  735. ((mac)[2] << 16) | ((mac)[3] << 24))
  736. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  737. static void cpsw_set_slave_mac(struct cpsw_slave *slave,
  738. struct cpsw_priv *priv)
  739. {
  740. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  741. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  742. }
  743. static void _cpsw_adjust_link(struct cpsw_slave *slave,
  744. struct cpsw_priv *priv, bool *link)
  745. {
  746. struct phy_device *phy = slave->phy;
  747. u32 mac_control = 0;
  748. u32 slave_port;
  749. if (!phy)
  750. return;
  751. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  752. if (phy->link) {
  753. mac_control = priv->data.mac_control;
  754. /* enable forwarding */
  755. cpsw_ale_control_set(priv->ale, slave_port,
  756. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  757. if (phy->speed == 1000)
  758. mac_control |= BIT(7); /* GIGABITEN */
  759. if (phy->duplex)
  760. mac_control |= BIT(0); /* FULLDUPLEXEN */
  761. /* set speed_in input in case RMII mode is used in 100Mbps */
  762. if (phy->speed == 100)
  763. mac_control |= BIT(15);
  764. else if (phy->speed == 10)
  765. mac_control |= BIT(18); /* In Band mode */
  766. if (priv->rx_pause)
  767. mac_control |= BIT(3);
  768. if (priv->tx_pause)
  769. mac_control |= BIT(4);
  770. *link = true;
  771. } else {
  772. mac_control = 0;
  773. /* disable forwarding */
  774. cpsw_ale_control_set(priv->ale, slave_port,
  775. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  776. }
  777. if (mac_control != slave->mac_control) {
  778. phy_print_status(phy);
  779. __raw_writel(mac_control, &slave->sliver->mac_control);
  780. }
  781. slave->mac_control = mac_control;
  782. }
  783. static void cpsw_adjust_link(struct net_device *ndev)
  784. {
  785. struct cpsw_priv *priv = netdev_priv(ndev);
  786. bool link = false;
  787. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  788. if (link) {
  789. netif_carrier_on(ndev);
  790. if (netif_running(ndev))
  791. netif_wake_queue(ndev);
  792. } else {
  793. netif_carrier_off(ndev);
  794. netif_stop_queue(ndev);
  795. }
  796. }
  797. static int cpsw_get_coalesce(struct net_device *ndev,
  798. struct ethtool_coalesce *coal)
  799. {
  800. struct cpsw_priv *priv = netdev_priv(ndev);
  801. coal->rx_coalesce_usecs = priv->coal_intvl;
  802. return 0;
  803. }
  804. static int cpsw_set_coalesce(struct net_device *ndev,
  805. struct ethtool_coalesce *coal)
  806. {
  807. struct cpsw_priv *priv = netdev_priv(ndev);
  808. u32 int_ctrl;
  809. u32 num_interrupts = 0;
  810. u32 prescale = 0;
  811. u32 addnl_dvdr = 1;
  812. u32 coal_intvl = 0;
  813. coal_intvl = coal->rx_coalesce_usecs;
  814. int_ctrl = readl(&priv->wr_regs->int_control);
  815. prescale = priv->bus_freq_mhz * 4;
  816. if (!coal->rx_coalesce_usecs) {
  817. int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
  818. goto update_return;
  819. }
  820. if (coal_intvl < CPSW_CMINTMIN_INTVL)
  821. coal_intvl = CPSW_CMINTMIN_INTVL;
  822. if (coal_intvl > CPSW_CMINTMAX_INTVL) {
  823. /* Interrupt pacer works with 4us Pulse, we can
  824. * throttle further by dilating the 4us pulse.
  825. */
  826. addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
  827. if (addnl_dvdr > 1) {
  828. prescale *= addnl_dvdr;
  829. if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
  830. coal_intvl = (CPSW_CMINTMAX_INTVL
  831. * addnl_dvdr);
  832. } else {
  833. addnl_dvdr = 1;
  834. coal_intvl = CPSW_CMINTMAX_INTVL;
  835. }
  836. }
  837. num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
  838. writel(num_interrupts, &priv->wr_regs->rx_imax);
  839. writel(num_interrupts, &priv->wr_regs->tx_imax);
  840. int_ctrl |= CPSW_INTPACEEN;
  841. int_ctrl &= (~CPSW_INTPRESCALE_MASK);
  842. int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
  843. update_return:
  844. writel(int_ctrl, &priv->wr_regs->int_control);
  845. cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
  846. if (priv->data.dual_emac) {
  847. int i;
  848. for (i = 0; i < priv->data.slaves; i++) {
  849. priv = netdev_priv(priv->slaves[i].ndev);
  850. priv->coal_intvl = coal_intvl;
  851. }
  852. } else {
  853. priv->coal_intvl = coal_intvl;
  854. }
  855. return 0;
  856. }
  857. static int cpsw_get_sset_count(struct net_device *ndev, int sset)
  858. {
  859. switch (sset) {
  860. case ETH_SS_STATS:
  861. return CPSW_STATS_LEN;
  862. default:
  863. return -EOPNOTSUPP;
  864. }
  865. }
  866. static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  867. {
  868. u8 *p = data;
  869. int i;
  870. switch (stringset) {
  871. case ETH_SS_STATS:
  872. for (i = 0; i < CPSW_STATS_LEN; i++) {
  873. memcpy(p, cpsw_gstrings_stats[i].stat_string,
  874. ETH_GSTRING_LEN);
  875. p += ETH_GSTRING_LEN;
  876. }
  877. break;
  878. }
  879. }
  880. static void cpsw_get_ethtool_stats(struct net_device *ndev,
  881. struct ethtool_stats *stats, u64 *data)
  882. {
  883. struct cpsw_priv *priv = netdev_priv(ndev);
  884. struct cpdma_chan_stats rx_stats;
  885. struct cpdma_chan_stats tx_stats;
  886. u32 val;
  887. u8 *p;
  888. int i;
  889. /* Collect Davinci CPDMA stats for Rx and Tx Channel */
  890. cpdma_chan_get_stats(priv->rxch, &rx_stats);
  891. cpdma_chan_get_stats(priv->txch, &tx_stats);
  892. for (i = 0; i < CPSW_STATS_LEN; i++) {
  893. switch (cpsw_gstrings_stats[i].type) {
  894. case CPSW_STATS:
  895. val = readl(priv->hw_stats +
  896. cpsw_gstrings_stats[i].stat_offset);
  897. data[i] = val;
  898. break;
  899. case CPDMA_RX_STATS:
  900. p = (u8 *)&rx_stats +
  901. cpsw_gstrings_stats[i].stat_offset;
  902. data[i] = *(u32 *)p;
  903. break;
  904. case CPDMA_TX_STATS:
  905. p = (u8 *)&tx_stats +
  906. cpsw_gstrings_stats[i].stat_offset;
  907. data[i] = *(u32 *)p;
  908. break;
  909. }
  910. }
  911. }
  912. static int cpsw_common_res_usage_state(struct cpsw_priv *priv)
  913. {
  914. u32 i;
  915. u32 usage_count = 0;
  916. if (!priv->data.dual_emac)
  917. return 0;
  918. for (i = 0; i < priv->data.slaves; i++)
  919. if (priv->slaves[i].open_stat)
  920. usage_count++;
  921. return usage_count;
  922. }
  923. static inline int cpsw_tx_packet_submit(struct net_device *ndev,
  924. struct cpsw_priv *priv, struct sk_buff *skb)
  925. {
  926. if (!priv->data.dual_emac)
  927. return cpdma_chan_submit(priv->txch, skb, skb->data,
  928. skb->len, 0);
  929. if (ndev == cpsw_get_slave_ndev(priv, 0))
  930. return cpdma_chan_submit(priv->txch, skb, skb->data,
  931. skb->len, 1);
  932. else
  933. return cpdma_chan_submit(priv->txch, skb, skb->data,
  934. skb->len, 2);
  935. }
  936. static inline void cpsw_add_dual_emac_def_ale_entries(
  937. struct cpsw_priv *priv, struct cpsw_slave *slave,
  938. u32 slave_port)
  939. {
  940. u32 port_mask = 1 << slave_port | 1 << priv->host_port;
  941. if (priv->version == CPSW_VERSION_1)
  942. slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
  943. else
  944. slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
  945. cpsw_ale_add_vlan(priv->ale, slave->port_vlan, port_mask,
  946. port_mask, port_mask, 0);
  947. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  948. port_mask, ALE_VLAN, slave->port_vlan, 0);
  949. cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
  950. priv->host_port, ALE_VLAN | ALE_SECURE, slave->port_vlan);
  951. }
  952. static void soft_reset_slave(struct cpsw_slave *slave)
  953. {
  954. char name[32];
  955. snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
  956. soft_reset(name, &slave->sliver->soft_reset);
  957. }
  958. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  959. {
  960. u32 slave_port;
  961. soft_reset_slave(slave);
  962. /* setup priority mapping */
  963. __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
  964. switch (priv->version) {
  965. case CPSW_VERSION_1:
  966. slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
  967. break;
  968. case CPSW_VERSION_2:
  969. case CPSW_VERSION_3:
  970. case CPSW_VERSION_4:
  971. slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
  972. break;
  973. }
  974. /* setup max packet size, and mac address */
  975. __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
  976. cpsw_set_slave_mac(slave, priv);
  977. slave->mac_control = 0; /* no link yet */
  978. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  979. if (priv->data.dual_emac)
  980. cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
  981. else
  982. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  983. 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
  984. if (priv->phy_node)
  985. slave->phy = of_phy_connect(priv->ndev, priv->phy_node,
  986. &cpsw_adjust_link, 0, slave->data->phy_if);
  987. else
  988. slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
  989. &cpsw_adjust_link, slave->data->phy_if);
  990. if (IS_ERR(slave->phy)) {
  991. dev_err(priv->dev, "phy %s not found on slave %d\n",
  992. slave->data->phy_id, slave->slave_num);
  993. slave->phy = NULL;
  994. } else {
  995. dev_info(priv->dev, "phy found : id is : 0x%x\n",
  996. slave->phy->phy_id);
  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. info->regdump_len = cpsw_get_regs_len(ndev);
  1502. }
  1503. static u32 cpsw_get_msglevel(struct net_device *ndev)
  1504. {
  1505. struct cpsw_priv *priv = netdev_priv(ndev);
  1506. return priv->msg_enable;
  1507. }
  1508. static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  1509. {
  1510. struct cpsw_priv *priv = netdev_priv(ndev);
  1511. priv->msg_enable = value;
  1512. }
  1513. static int cpsw_get_ts_info(struct net_device *ndev,
  1514. struct ethtool_ts_info *info)
  1515. {
  1516. #ifdef CONFIG_TI_CPTS
  1517. struct cpsw_priv *priv = netdev_priv(ndev);
  1518. info->so_timestamping =
  1519. SOF_TIMESTAMPING_TX_HARDWARE |
  1520. SOF_TIMESTAMPING_TX_SOFTWARE |
  1521. SOF_TIMESTAMPING_RX_HARDWARE |
  1522. SOF_TIMESTAMPING_RX_SOFTWARE |
  1523. SOF_TIMESTAMPING_SOFTWARE |
  1524. SOF_TIMESTAMPING_RAW_HARDWARE;
  1525. info->phc_index = priv->cpts->phc_index;
  1526. info->tx_types =
  1527. (1 << HWTSTAMP_TX_OFF) |
  1528. (1 << HWTSTAMP_TX_ON);
  1529. info->rx_filters =
  1530. (1 << HWTSTAMP_FILTER_NONE) |
  1531. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  1532. #else
  1533. info->so_timestamping =
  1534. SOF_TIMESTAMPING_TX_SOFTWARE |
  1535. SOF_TIMESTAMPING_RX_SOFTWARE |
  1536. SOF_TIMESTAMPING_SOFTWARE;
  1537. info->phc_index = -1;
  1538. info->tx_types = 0;
  1539. info->rx_filters = 0;
  1540. #endif
  1541. return 0;
  1542. }
  1543. static int cpsw_get_settings(struct net_device *ndev,
  1544. struct ethtool_cmd *ecmd)
  1545. {
  1546. struct cpsw_priv *priv = netdev_priv(ndev);
  1547. int slave_no = cpsw_slave_index(priv);
  1548. if (priv->slaves[slave_no].phy)
  1549. return phy_ethtool_gset(priv->slaves[slave_no].phy, ecmd);
  1550. else
  1551. return -EOPNOTSUPP;
  1552. }
  1553. static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
  1554. {
  1555. struct cpsw_priv *priv = netdev_priv(ndev);
  1556. int slave_no = cpsw_slave_index(priv);
  1557. if (priv->slaves[slave_no].phy)
  1558. return phy_ethtool_sset(priv->slaves[slave_no].phy, ecmd);
  1559. else
  1560. return -EOPNOTSUPP;
  1561. }
  1562. static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1563. {
  1564. struct cpsw_priv *priv = netdev_priv(ndev);
  1565. int slave_no = cpsw_slave_index(priv);
  1566. wol->supported = 0;
  1567. wol->wolopts = 0;
  1568. if (priv->slaves[slave_no].phy)
  1569. phy_ethtool_get_wol(priv->slaves[slave_no].phy, wol);
  1570. }
  1571. static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1572. {
  1573. struct cpsw_priv *priv = netdev_priv(ndev);
  1574. int slave_no = cpsw_slave_index(priv);
  1575. if (priv->slaves[slave_no].phy)
  1576. return phy_ethtool_set_wol(priv->slaves[slave_no].phy, wol);
  1577. else
  1578. return -EOPNOTSUPP;
  1579. }
  1580. static void cpsw_get_pauseparam(struct net_device *ndev,
  1581. struct ethtool_pauseparam *pause)
  1582. {
  1583. struct cpsw_priv *priv = netdev_priv(ndev);
  1584. pause->autoneg = AUTONEG_DISABLE;
  1585. pause->rx_pause = priv->rx_pause ? true : false;
  1586. pause->tx_pause = priv->tx_pause ? true : false;
  1587. }
  1588. static int cpsw_set_pauseparam(struct net_device *ndev,
  1589. struct ethtool_pauseparam *pause)
  1590. {
  1591. struct cpsw_priv *priv = netdev_priv(ndev);
  1592. bool link;
  1593. priv->rx_pause = pause->rx_pause ? true : false;
  1594. priv->tx_pause = pause->tx_pause ? true : false;
  1595. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  1596. return 0;
  1597. }
  1598. static const struct ethtool_ops cpsw_ethtool_ops = {
  1599. .get_drvinfo = cpsw_get_drvinfo,
  1600. .get_msglevel = cpsw_get_msglevel,
  1601. .set_msglevel = cpsw_set_msglevel,
  1602. .get_link = ethtool_op_get_link,
  1603. .get_ts_info = cpsw_get_ts_info,
  1604. .get_settings = cpsw_get_settings,
  1605. .set_settings = cpsw_set_settings,
  1606. .get_coalesce = cpsw_get_coalesce,
  1607. .set_coalesce = cpsw_set_coalesce,
  1608. .get_sset_count = cpsw_get_sset_count,
  1609. .get_strings = cpsw_get_strings,
  1610. .get_ethtool_stats = cpsw_get_ethtool_stats,
  1611. .get_pauseparam = cpsw_get_pauseparam,
  1612. .set_pauseparam = cpsw_set_pauseparam,
  1613. .get_wol = cpsw_get_wol,
  1614. .set_wol = cpsw_set_wol,
  1615. .get_regs_len = cpsw_get_regs_len,
  1616. .get_regs = cpsw_get_regs,
  1617. };
  1618. static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
  1619. u32 slave_reg_ofs, u32 sliver_reg_ofs)
  1620. {
  1621. void __iomem *regs = priv->regs;
  1622. int slave_num = slave->slave_num;
  1623. struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
  1624. slave->data = data;
  1625. slave->regs = regs + slave_reg_ofs;
  1626. slave->sliver = regs + sliver_reg_ofs;
  1627. slave->port_vlan = data->dual_emac_res_vlan;
  1628. }
  1629. static int cpsw_probe_dt(struct cpsw_priv *priv,
  1630. struct platform_device *pdev)
  1631. {
  1632. struct device_node *node = pdev->dev.of_node;
  1633. struct device_node *slave_node;
  1634. struct cpsw_platform_data *data = &priv->data;
  1635. int i = 0, ret;
  1636. u32 prop;
  1637. if (!node)
  1638. return -EINVAL;
  1639. if (of_property_read_u32(node, "slaves", &prop)) {
  1640. dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
  1641. return -EINVAL;
  1642. }
  1643. data->slaves = prop;
  1644. if (of_property_read_u32(node, "active_slave", &prop)) {
  1645. dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
  1646. return -EINVAL;
  1647. }
  1648. data->active_slave = prop;
  1649. if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
  1650. dev_err(&pdev->dev, "Missing cpts_clock_mult property in the DT.\n");
  1651. return -EINVAL;
  1652. }
  1653. data->cpts_clock_mult = prop;
  1654. if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
  1655. dev_err(&pdev->dev, "Missing cpts_clock_shift property in the DT.\n");
  1656. return -EINVAL;
  1657. }
  1658. data->cpts_clock_shift = prop;
  1659. data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
  1660. * sizeof(struct cpsw_slave_data),
  1661. GFP_KERNEL);
  1662. if (!data->slave_data)
  1663. return -ENOMEM;
  1664. if (of_property_read_u32(node, "cpdma_channels", &prop)) {
  1665. dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
  1666. return -EINVAL;
  1667. }
  1668. data->channels = prop;
  1669. if (of_property_read_u32(node, "ale_entries", &prop)) {
  1670. dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
  1671. return -EINVAL;
  1672. }
  1673. data->ale_entries = prop;
  1674. if (of_property_read_u32(node, "bd_ram_size", &prop)) {
  1675. dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
  1676. return -EINVAL;
  1677. }
  1678. data->bd_ram_size = prop;
  1679. if (of_property_read_u32(node, "rx_descs", &prop)) {
  1680. dev_err(&pdev->dev, "Missing rx_descs property in the DT.\n");
  1681. return -EINVAL;
  1682. }
  1683. data->rx_descs = prop;
  1684. if (of_property_read_u32(node, "mac_control", &prop)) {
  1685. dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
  1686. return -EINVAL;
  1687. }
  1688. data->mac_control = prop;
  1689. if (of_property_read_bool(node, "dual_emac"))
  1690. data->dual_emac = 1;
  1691. /*
  1692. * Populate all the child nodes here...
  1693. */
  1694. ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
  1695. /* We do not want to force this, as in some cases may not have child */
  1696. if (ret)
  1697. dev_warn(&pdev->dev, "Doesn't have any child node\n");
  1698. for_each_child_of_node(node, slave_node) {
  1699. struct cpsw_slave_data *slave_data = data->slave_data + i;
  1700. const void *mac_addr = NULL;
  1701. u32 phyid;
  1702. int lenp;
  1703. const __be32 *parp;
  1704. struct device_node *mdio_node;
  1705. struct platform_device *mdio;
  1706. /* This is no slave child node, continue */
  1707. if (strcmp(slave_node->name, "slave"))
  1708. continue;
  1709. priv->phy_node = of_parse_phandle(slave_node, "phy-handle", 0);
  1710. parp = of_get_property(slave_node, "phy_id", &lenp);
  1711. if ((parp == NULL) || (lenp != (sizeof(void *) * 2))) {
  1712. dev_err(&pdev->dev, "Missing slave[%d] phy_id property\n", i);
  1713. goto no_phy_slave;
  1714. }
  1715. mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
  1716. phyid = be32_to_cpup(parp+1);
  1717. mdio = of_find_device_by_node(mdio_node);
  1718. of_node_put(mdio_node);
  1719. if (!mdio) {
  1720. dev_err(&pdev->dev, "Missing mdio platform device\n");
  1721. return -EINVAL;
  1722. }
  1723. snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
  1724. PHY_ID_FMT, mdio->name, phyid);
  1725. slave_data->phy_if = of_get_phy_mode(slave_node);
  1726. if (slave_data->phy_if < 0) {
  1727. dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
  1728. i);
  1729. return slave_data->phy_if;
  1730. }
  1731. no_phy_slave:
  1732. mac_addr = of_get_mac_address(slave_node);
  1733. if (mac_addr) {
  1734. memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
  1735. } else {
  1736. if (of_machine_is_compatible("ti,am33xx")) {
  1737. ret = cpsw_am33xx_cm_get_macid(&pdev->dev,
  1738. 0x630, i,
  1739. slave_data->mac_addr);
  1740. if (ret)
  1741. return ret;
  1742. }
  1743. }
  1744. if (data->dual_emac) {
  1745. if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
  1746. &prop)) {
  1747. dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
  1748. slave_data->dual_emac_res_vlan = i+1;
  1749. dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
  1750. slave_data->dual_emac_res_vlan, i);
  1751. } else {
  1752. slave_data->dual_emac_res_vlan = prop;
  1753. }
  1754. }
  1755. i++;
  1756. if (i == data->slaves)
  1757. break;
  1758. }
  1759. return 0;
  1760. }
  1761. static int cpsw_probe_dual_emac(struct platform_device *pdev,
  1762. struct cpsw_priv *priv)
  1763. {
  1764. struct cpsw_platform_data *data = &priv->data;
  1765. struct net_device *ndev;
  1766. struct cpsw_priv *priv_sl2;
  1767. int ret = 0, i;
  1768. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  1769. if (!ndev) {
  1770. dev_err(&pdev->dev, "cpsw: error allocating net_device\n");
  1771. return -ENOMEM;
  1772. }
  1773. priv_sl2 = netdev_priv(ndev);
  1774. spin_lock_init(&priv_sl2->lock);
  1775. priv_sl2->data = *data;
  1776. priv_sl2->pdev = pdev;
  1777. priv_sl2->ndev = ndev;
  1778. priv_sl2->dev = &ndev->dev;
  1779. priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1780. priv_sl2->rx_packet_max = max(rx_packet_max, 128);
  1781. if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
  1782. memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
  1783. ETH_ALEN);
  1784. dev_info(&pdev->dev, "cpsw: Detected MACID = %pM\n", priv_sl2->mac_addr);
  1785. } else {
  1786. random_ether_addr(priv_sl2->mac_addr);
  1787. dev_info(&pdev->dev, "cpsw: Random MACID = %pM\n", priv_sl2->mac_addr);
  1788. }
  1789. memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
  1790. priv_sl2->slaves = priv->slaves;
  1791. priv_sl2->clk = priv->clk;
  1792. priv_sl2->coal_intvl = 0;
  1793. priv_sl2->bus_freq_mhz = priv->bus_freq_mhz;
  1794. priv_sl2->regs = priv->regs;
  1795. priv_sl2->host_port = priv->host_port;
  1796. priv_sl2->host_port_regs = priv->host_port_regs;
  1797. priv_sl2->wr_regs = priv->wr_regs;
  1798. priv_sl2->hw_stats = priv->hw_stats;
  1799. priv_sl2->dma = priv->dma;
  1800. priv_sl2->txch = priv->txch;
  1801. priv_sl2->rxch = priv->rxch;
  1802. priv_sl2->ale = priv->ale;
  1803. priv_sl2->emac_port = 1;
  1804. priv->slaves[1].ndev = ndev;
  1805. priv_sl2->cpts = priv->cpts;
  1806. priv_sl2->version = priv->version;
  1807. for (i = 0; i < priv->num_irqs; i++) {
  1808. priv_sl2->irqs_table[i] = priv->irqs_table[i];
  1809. priv_sl2->num_irqs = priv->num_irqs;
  1810. }
  1811. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1812. ndev->netdev_ops = &cpsw_netdev_ops;
  1813. ndev->ethtool_ops = &cpsw_ethtool_ops;
  1814. /* register the network device */
  1815. SET_NETDEV_DEV(ndev, &pdev->dev);
  1816. ret = register_netdev(ndev);
  1817. if (ret) {
  1818. dev_err(&pdev->dev, "cpsw: error registering net device\n");
  1819. free_netdev(ndev);
  1820. ret = -ENODEV;
  1821. }
  1822. return ret;
  1823. }
  1824. #define CPSW_QUIRK_IRQ BIT(0)
  1825. static struct platform_device_id cpsw_devtype[] = {
  1826. {
  1827. /* keep it for existing comaptibles */
  1828. .name = "cpsw",
  1829. .driver_data = CPSW_QUIRK_IRQ,
  1830. }, {
  1831. .name = "am335x-cpsw",
  1832. .driver_data = CPSW_QUIRK_IRQ,
  1833. }, {
  1834. .name = "am4372-cpsw",
  1835. .driver_data = 0,
  1836. }, {
  1837. .name = "dra7-cpsw",
  1838. .driver_data = 0,
  1839. }, {
  1840. /* sentinel */
  1841. }
  1842. };
  1843. MODULE_DEVICE_TABLE(platform, cpsw_devtype);
  1844. enum ti_cpsw_type {
  1845. CPSW = 0,
  1846. AM335X_CPSW,
  1847. AM4372_CPSW,
  1848. DRA7_CPSW,
  1849. };
  1850. static const struct of_device_id cpsw_of_mtable[] = {
  1851. { .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], },
  1852. { .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], },
  1853. { .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], },
  1854. { .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], },
  1855. { /* sentinel */ },
  1856. };
  1857. MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
  1858. static int cpsw_probe(struct platform_device *pdev)
  1859. {
  1860. struct cpsw_platform_data *data;
  1861. struct net_device *ndev;
  1862. struct cpsw_priv *priv;
  1863. struct cpdma_params dma_params;
  1864. struct cpsw_ale_params ale_params;
  1865. void __iomem *ss_regs;
  1866. struct resource *res, *ss_res;
  1867. const struct of_device_id *of_id;
  1868. u32 slave_offset, sliver_offset, slave_size;
  1869. int ret = 0, i;
  1870. int irq;
  1871. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  1872. if (!ndev) {
  1873. dev_err(&pdev->dev, "error allocating net_device\n");
  1874. return -ENOMEM;
  1875. }
  1876. platform_set_drvdata(pdev, ndev);
  1877. priv = netdev_priv(ndev);
  1878. spin_lock_init(&priv->lock);
  1879. priv->pdev = pdev;
  1880. priv->ndev = ndev;
  1881. priv->dev = &ndev->dev;
  1882. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1883. priv->rx_packet_max = max(rx_packet_max, 128);
  1884. priv->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
  1885. if (!priv->cpts) {
  1886. dev_err(&pdev->dev, "error allocating cpts\n");
  1887. ret = -ENOMEM;
  1888. goto clean_ndev_ret;
  1889. }
  1890. /*
  1891. * This may be required here for child devices.
  1892. */
  1893. pm_runtime_enable(&pdev->dev);
  1894. /* Select default pin state */
  1895. pinctrl_pm_select_default_state(&pdev->dev);
  1896. if (cpsw_probe_dt(priv, pdev)) {
  1897. dev_err(&pdev->dev, "cpsw: platform data missing\n");
  1898. ret = -ENODEV;
  1899. goto clean_runtime_disable_ret;
  1900. }
  1901. data = &priv->data;
  1902. if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
  1903. memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
  1904. dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
  1905. } else {
  1906. eth_random_addr(priv->mac_addr);
  1907. dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
  1908. }
  1909. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1910. priv->slaves = devm_kzalloc(&pdev->dev,
  1911. sizeof(struct cpsw_slave) * data->slaves,
  1912. GFP_KERNEL);
  1913. if (!priv->slaves) {
  1914. ret = -ENOMEM;
  1915. goto clean_runtime_disable_ret;
  1916. }
  1917. for (i = 0; i < data->slaves; i++)
  1918. priv->slaves[i].slave_num = i;
  1919. priv->slaves[0].ndev = ndev;
  1920. priv->emac_port = 0;
  1921. priv->clk = devm_clk_get(&pdev->dev, "fck");
  1922. if (IS_ERR(priv->clk)) {
  1923. dev_err(priv->dev, "fck is not found\n");
  1924. ret = -ENODEV;
  1925. goto clean_runtime_disable_ret;
  1926. }
  1927. priv->coal_intvl = 0;
  1928. priv->bus_freq_mhz = clk_get_rate(priv->clk) / 1000000;
  1929. ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1930. ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
  1931. if (IS_ERR(ss_regs)) {
  1932. ret = PTR_ERR(ss_regs);
  1933. goto clean_runtime_disable_ret;
  1934. }
  1935. priv->regs = ss_regs;
  1936. priv->host_port = HOST_PORT_NUM;
  1937. /* Need to enable clocks with runtime PM api to access module
  1938. * registers
  1939. */
  1940. pm_runtime_get_sync(&pdev->dev);
  1941. priv->version = readl(&priv->regs->id_ver);
  1942. pm_runtime_put_sync(&pdev->dev);
  1943. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1944. priv->wr_regs = devm_ioremap_resource(&pdev->dev, res);
  1945. if (IS_ERR(priv->wr_regs)) {
  1946. ret = PTR_ERR(priv->wr_regs);
  1947. goto clean_runtime_disable_ret;
  1948. }
  1949. memset(&dma_params, 0, sizeof(dma_params));
  1950. memset(&ale_params, 0, sizeof(ale_params));
  1951. switch (priv->version) {
  1952. case CPSW_VERSION_1:
  1953. priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  1954. priv->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
  1955. priv->hw_stats = ss_regs + CPSW1_HW_STATS;
  1956. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  1957. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  1958. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  1959. slave_offset = CPSW1_SLAVE_OFFSET;
  1960. slave_size = CPSW1_SLAVE_SIZE;
  1961. sliver_offset = CPSW1_SLIVER_OFFSET;
  1962. dma_params.desc_mem_phys = 0;
  1963. break;
  1964. case CPSW_VERSION_2:
  1965. case CPSW_VERSION_3:
  1966. case CPSW_VERSION_4:
  1967. priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  1968. priv->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
  1969. priv->hw_stats = ss_regs + CPSW2_HW_STATS;
  1970. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  1971. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  1972. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  1973. slave_offset = CPSW2_SLAVE_OFFSET;
  1974. slave_size = CPSW2_SLAVE_SIZE;
  1975. sliver_offset = CPSW2_SLIVER_OFFSET;
  1976. dma_params.desc_mem_phys =
  1977. (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
  1978. break;
  1979. default:
  1980. dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
  1981. ret = -ENODEV;
  1982. goto clean_runtime_disable_ret;
  1983. }
  1984. for (i = 0; i < priv->data.slaves; i++) {
  1985. struct cpsw_slave *slave = &priv->slaves[i];
  1986. cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
  1987. slave_offset += slave_size;
  1988. sliver_offset += SLIVER_SIZE;
  1989. }
  1990. dma_params.dev = &pdev->dev;
  1991. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  1992. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  1993. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  1994. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  1995. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  1996. dma_params.num_chan = data->channels;
  1997. dma_params.has_soft_reset = true;
  1998. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  1999. dma_params.desc_mem_size = data->bd_ram_size;
  2000. dma_params.desc_align = 16;
  2001. dma_params.has_ext_regs = true;
  2002. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  2003. priv->dma = cpdma_ctlr_create(&dma_params);
  2004. if (!priv->dma) {
  2005. dev_err(priv->dev, "error initializing dma\n");
  2006. ret = -ENOMEM;
  2007. goto clean_runtime_disable_ret;
  2008. }
  2009. priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
  2010. cpsw_tx_handler);
  2011. priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
  2012. cpsw_rx_handler);
  2013. if (WARN_ON(!priv->txch || !priv->rxch)) {
  2014. dev_err(priv->dev, "error initializing dma channels\n");
  2015. ret = -ENOMEM;
  2016. goto clean_dma_ret;
  2017. }
  2018. ale_params.dev = &ndev->dev;
  2019. ale_params.ale_ageout = ale_ageout;
  2020. ale_params.ale_entries = data->ale_entries;
  2021. ale_params.ale_ports = data->slaves;
  2022. priv->ale = cpsw_ale_create(&ale_params);
  2023. if (!priv->ale) {
  2024. dev_err(priv->dev, "error initializing ale engine\n");
  2025. ret = -ENODEV;
  2026. goto clean_dma_ret;
  2027. }
  2028. ndev->irq = platform_get_irq(pdev, 1);
  2029. if (ndev->irq < 0) {
  2030. dev_err(priv->dev, "error getting irq resource\n");
  2031. ret = -ENOENT;
  2032. goto clean_ale_ret;
  2033. }
  2034. of_id = of_match_device(cpsw_of_mtable, &pdev->dev);
  2035. if (of_id) {
  2036. pdev->id_entry = of_id->data;
  2037. if (pdev->id_entry->driver_data)
  2038. priv->quirk_irq = true;
  2039. }
  2040. /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
  2041. * MISC IRQs which are always kept disabled with this driver so
  2042. * we will not request them.
  2043. *
  2044. * If anyone wants to implement support for those, make sure to
  2045. * first request and append them to irqs_table array.
  2046. */
  2047. /* RX IRQ */
  2048. irq = platform_get_irq(pdev, 1);
  2049. if (irq < 0)
  2050. goto clean_ale_ret;
  2051. priv->irqs_table[0] = irq;
  2052. ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
  2053. 0, dev_name(&pdev->dev), priv);
  2054. if (ret < 0) {
  2055. dev_err(priv->dev, "error attaching irq (%d)\n", ret);
  2056. goto clean_ale_ret;
  2057. }
  2058. /* TX IRQ */
  2059. irq = platform_get_irq(pdev, 2);
  2060. if (irq < 0)
  2061. goto clean_ale_ret;
  2062. priv->irqs_table[1] = irq;
  2063. ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
  2064. 0, dev_name(&pdev->dev), priv);
  2065. if (ret < 0) {
  2066. dev_err(priv->dev, "error attaching irq (%d)\n", ret);
  2067. goto clean_ale_ret;
  2068. }
  2069. priv->num_irqs = 2;
  2070. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  2071. ndev->netdev_ops = &cpsw_netdev_ops;
  2072. ndev->ethtool_ops = &cpsw_ethtool_ops;
  2073. netif_napi_add(ndev, &priv->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT);
  2074. netif_napi_add(ndev, &priv->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT);
  2075. /* register the network device */
  2076. SET_NETDEV_DEV(ndev, &pdev->dev);
  2077. ret = register_netdev(ndev);
  2078. if (ret) {
  2079. dev_err(priv->dev, "error registering net device\n");
  2080. ret = -ENODEV;
  2081. goto clean_ale_ret;
  2082. }
  2083. cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n",
  2084. &ss_res->start, ndev->irq);
  2085. if (priv->data.dual_emac) {
  2086. ret = cpsw_probe_dual_emac(pdev, priv);
  2087. if (ret) {
  2088. cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
  2089. goto clean_ale_ret;
  2090. }
  2091. }
  2092. return 0;
  2093. clean_ale_ret:
  2094. cpsw_ale_destroy(priv->ale);
  2095. clean_dma_ret:
  2096. cpdma_chan_destroy(priv->txch);
  2097. cpdma_chan_destroy(priv->rxch);
  2098. cpdma_ctlr_destroy(priv->dma);
  2099. clean_runtime_disable_ret:
  2100. pm_runtime_disable(&pdev->dev);
  2101. clean_ndev_ret:
  2102. free_netdev(priv->ndev);
  2103. return ret;
  2104. }
  2105. static int cpsw_remove_child_device(struct device *dev, void *c)
  2106. {
  2107. struct platform_device *pdev = to_platform_device(dev);
  2108. of_device_unregister(pdev);
  2109. return 0;
  2110. }
  2111. static int cpsw_remove(struct platform_device *pdev)
  2112. {
  2113. struct net_device *ndev = platform_get_drvdata(pdev);
  2114. struct cpsw_priv *priv = netdev_priv(ndev);
  2115. if (priv->data.dual_emac)
  2116. unregister_netdev(cpsw_get_slave_ndev(priv, 1));
  2117. unregister_netdev(ndev);
  2118. cpsw_ale_destroy(priv->ale);
  2119. cpdma_chan_destroy(priv->txch);
  2120. cpdma_chan_destroy(priv->rxch);
  2121. cpdma_ctlr_destroy(priv->dma);
  2122. pm_runtime_disable(&pdev->dev);
  2123. device_for_each_child(&pdev->dev, NULL, cpsw_remove_child_device);
  2124. if (priv->data.dual_emac)
  2125. free_netdev(cpsw_get_slave_ndev(priv, 1));
  2126. free_netdev(ndev);
  2127. return 0;
  2128. }
  2129. #ifdef CONFIG_PM_SLEEP
  2130. static int cpsw_suspend(struct device *dev)
  2131. {
  2132. struct platform_device *pdev = to_platform_device(dev);
  2133. struct net_device *ndev = platform_get_drvdata(pdev);
  2134. struct cpsw_priv *priv = netdev_priv(ndev);
  2135. if (priv->data.dual_emac) {
  2136. int i;
  2137. for (i = 0; i < priv->data.slaves; i++) {
  2138. if (netif_running(priv->slaves[i].ndev))
  2139. cpsw_ndo_stop(priv->slaves[i].ndev);
  2140. soft_reset_slave(priv->slaves + i);
  2141. }
  2142. } else {
  2143. if (netif_running(ndev))
  2144. cpsw_ndo_stop(ndev);
  2145. for_each_slave(priv, soft_reset_slave);
  2146. }
  2147. pm_runtime_put_sync(&pdev->dev);
  2148. /* Select sleep pin state */
  2149. pinctrl_pm_select_sleep_state(&pdev->dev);
  2150. return 0;
  2151. }
  2152. static int cpsw_resume(struct device *dev)
  2153. {
  2154. struct platform_device *pdev = to_platform_device(dev);
  2155. struct net_device *ndev = platform_get_drvdata(pdev);
  2156. struct cpsw_priv *priv = netdev_priv(ndev);
  2157. pm_runtime_get_sync(&pdev->dev);
  2158. /* Select default pin state */
  2159. pinctrl_pm_select_default_state(&pdev->dev);
  2160. if (priv->data.dual_emac) {
  2161. int i;
  2162. for (i = 0; i < priv->data.slaves; i++) {
  2163. if (netif_running(priv->slaves[i].ndev))
  2164. cpsw_ndo_open(priv->slaves[i].ndev);
  2165. }
  2166. } else {
  2167. if (netif_running(ndev))
  2168. cpsw_ndo_open(ndev);
  2169. }
  2170. return 0;
  2171. }
  2172. #endif
  2173. static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
  2174. static struct platform_driver cpsw_driver = {
  2175. .driver = {
  2176. .name = "cpsw",
  2177. .pm = &cpsw_pm_ops,
  2178. .of_match_table = cpsw_of_mtable,
  2179. },
  2180. .probe = cpsw_probe,
  2181. .remove = cpsw_remove,
  2182. };
  2183. static int __init cpsw_init(void)
  2184. {
  2185. return platform_driver_register(&cpsw_driver);
  2186. }
  2187. late_initcall(cpsw_init);
  2188. static void __exit cpsw_exit(void)
  2189. {
  2190. platform_driver_unregister(&cpsw_driver);
  2191. }
  2192. module_exit(cpsw_exit);
  2193. MODULE_LICENSE("GPL");
  2194. MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
  2195. MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
  2196. MODULE_DESCRIPTION("TI CPSW Ethernet driver");