0101-net-mediatek-add-gsw-mt7530-driver.patch 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360
  1. From 6b8a7257e7bcb56782c3f8048311670fe6a80209 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Mon, 11 Apr 2016 03:11:54 +0200
  4. Subject: [PATCH 101/102] net: mediatek add gsw/mt7530 driver
  5. Signed-off-by: John Crispin <blogic@openwrt.org>
  6. ---
  7. drivers/net/ethernet/mediatek/Makefile | 2 +-
  8. drivers/net/ethernet/mediatek/gsw_mt7620.h | 251 +++++++
  9. drivers/net/ethernet/mediatek/gsw_mt7623.c | 1084 +++++++++++++++++++++++++++
  10. drivers/net/ethernet/mediatek/mt7530.c | 808 ++++++++++++++++++++
  11. drivers/net/ethernet/mediatek/mt7530.h | 20 +
  12. drivers/net/ethernet/mediatek/mtk_eth_soc.c | 59 +-
  13. drivers/net/ethernet/mediatek/mtk_eth_soc.h | 5 +
  14. 7 files changed, 2199 insertions(+), 30 deletions(-)
  15. create mode 100644 drivers/net/ethernet/mediatek/gsw_mt7620.h
  16. create mode 100644 drivers/net/ethernet/mediatek/gsw_mt7623.c
  17. create mode 100644 drivers/net/ethernet/mediatek/mt7530.c
  18. create mode 100644 drivers/net/ethernet/mediatek/mt7530.h
  19. --- a/drivers/net/ethernet/mediatek/Makefile
  20. +++ b/drivers/net/ethernet/mediatek/Makefile
  21. @@ -2,4 +2,4 @@
  22. # Makefile for the Mediatek SoCs built-in ethernet macs
  23. #
  24. -obj-$(CONFIG_NET_MEDIATEK_SOC) += mtk_eth_soc.o
  25. +obj-$(CONFIG_NET_MEDIATEK_SOC) += mt7530.o gsw_mt7623.o mtk_eth_soc.o
  26. --- /dev/null
  27. +++ b/drivers/net/ethernet/mediatek/gsw_mt7620.h
  28. @@ -0,0 +1,251 @@
  29. +/* This program is free software; you can redistribute it and/or modify
  30. + * it under the terms of the GNU General Public License as published by
  31. + * the Free Software Foundation; version 2 of the License
  32. + *
  33. + * This program is distributed in the hope that it will be useful,
  34. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. + * GNU General Public License for more details.
  37. + *
  38. + * Copyright (C) 2009-2016 John Crispin <blogic@openwrt.org>
  39. + * Copyright (C) 2009-2016 Felix Fietkau <nbd@nbd.name>
  40. + * Copyright (C) 2013-2016 Michael Lee <igvtee@gmail.com>
  41. + */
  42. +
  43. +#ifndef _RALINK_GSW_MT7620_H__
  44. +#define _RALINK_GSW_MT7620_H__
  45. +
  46. +#define GSW_REG_PHY_TIMEOUT (5 * HZ)
  47. +
  48. +#define MT7620_GSW_REG_PIAC 0x0004
  49. +
  50. +#define GSW_NUM_VLANS 16
  51. +#define GSW_NUM_VIDS 4096
  52. +#define GSW_NUM_PORTS 7
  53. +#define GSW_PORT6 6
  54. +
  55. +#define GSW_MDIO_ACCESS BIT(31)
  56. +#define GSW_MDIO_READ BIT(19)
  57. +#define GSW_MDIO_WRITE BIT(18)
  58. +#define GSW_MDIO_START BIT(16)
  59. +#define GSW_MDIO_ADDR_SHIFT 20
  60. +#define GSW_MDIO_REG_SHIFT 25
  61. +
  62. +#define GSW_REG_PORT_PMCR(x) (0x3000 + (x * 0x100))
  63. +#define GSW_REG_PORT_STATUS(x) (0x3008 + (x * 0x100))
  64. +#define GSW_REG_SMACCR0 0x3fE4
  65. +#define GSW_REG_SMACCR1 0x3fE8
  66. +#define GSW_REG_CKGCR 0x3ff0
  67. +
  68. +#define GSW_REG_IMR 0x7008
  69. +#define GSW_REG_ISR 0x700c
  70. +#define GSW_REG_GPC1 0x7014
  71. +
  72. +#define SYSC_REG_CHIP_REV_ID 0x0c
  73. +#define SYSC_REG_CFG 0x10
  74. +#define SYSC_REG_CFG1 0x14
  75. +#define RST_CTRL_MCM BIT(2)
  76. +#define SYSC_PAD_RGMII2_MDIO 0x58
  77. +#define SYSC_GPIO_MODE 0x60
  78. +
  79. +#define PORT_IRQ_ST_CHG 0x7f
  80. +
  81. +#define MT7621_ESW_PHY_POLLING 0x0000
  82. +#define MT7620_ESW_PHY_POLLING 0x7000
  83. +
  84. +#define PMCR_IPG BIT(18)
  85. +#define PMCR_MAC_MODE BIT(16)
  86. +#define PMCR_FORCE BIT(15)
  87. +#define PMCR_TX_EN BIT(14)
  88. +#define PMCR_RX_EN BIT(13)
  89. +#define PMCR_BACKOFF BIT(9)
  90. +#define PMCR_BACKPRES BIT(8)
  91. +#define PMCR_RX_FC BIT(5)
  92. +#define PMCR_TX_FC BIT(4)
  93. +#define PMCR_SPEED(_x) (_x << 2)
  94. +#define PMCR_DUPLEX BIT(1)
  95. +#define PMCR_LINK BIT(0)
  96. +
  97. +#define PHY_AN_EN BIT(31)
  98. +#define PHY_PRE_EN BIT(30)
  99. +#define PMY_MDC_CONF(_x) ((_x & 0x3f) << 24)
  100. +
  101. +/* ethernet subsystem config register */
  102. +#define ETHSYS_SYSCFG0 0x14
  103. +/* ethernet subsystem clock register */
  104. +#define ETHSYS_CLKCFG0 0x2c
  105. +#define ETHSYS_TRGMII_CLK_SEL362_5 BIT(11)
  106. +
  107. +/* p5 RGMII wrapper TX clock control register */
  108. +#define MT7530_P5RGMIITXCR 0x7b04
  109. +/* p5 RGMII wrapper RX clock control register */
  110. +#define MT7530_P5RGMIIRXCR 0x7b00
  111. +/* TRGMII TDX ODT registers */
  112. +#define MT7530_TRGMII_TD0_ODT 0x7a54
  113. +#define MT7530_TRGMII_TD1_ODT 0x7a5c
  114. +#define MT7530_TRGMII_TD2_ODT 0x7a64
  115. +#define MT7530_TRGMII_TD3_ODT 0x7a6c
  116. +#define MT7530_TRGMII_TD4_ODT 0x7a74
  117. +#define MT7530_TRGMII_TD5_ODT 0x7a7c
  118. +/* TRGMII TCK ctrl register */
  119. +#define MT7530_TRGMII_TCK_CTRL 0x7a78
  120. +/* TRGMII Tx ctrl register */
  121. +#define MT7530_TRGMII_TXCTRL 0x7a40
  122. +/* port 6 extended control register */
  123. +#define MT7530_P6ECR 0x7830
  124. +/* IO driver control register */
  125. +#define MT7530_IO_DRV_CR 0x7810
  126. +/* top signal control register */
  127. +#define MT7530_TOP_SIG_CTRL 0x7808
  128. +/* modified hwtrap register */
  129. +#define MT7530_MHWTRAP 0x7804
  130. +/* hwtrap status register */
  131. +#define MT7530_HWTRAP 0x7800
  132. +/* status interrupt register */
  133. +#define MT7530_SYS_INT_STS 0x700c
  134. +/* system nterrupt register */
  135. +#define MT7530_SYS_INT_EN 0x7008
  136. +/* system control register */
  137. +#define MT7530_SYS_CTRL 0x7000
  138. +/* port MAC status register */
  139. +#define MT7530_PMSR_P(x) (0x3008 + (x * 0x100))
  140. +/* port MAC control register */
  141. +#define MT7530_PMCR_P(x) (0x3000 + (x * 0x100))
  142. +
  143. +#define MT7621_XTAL_SHIFT 6
  144. +#define MT7621_XTAL_MASK 0x7
  145. +#define MT7621_XTAL_25 6
  146. +#define MT7621_XTAL_40 3
  147. +#define MT7621_MDIO_DRV_MASK (3 << 4)
  148. +#define MT7621_GE1_MODE_MASK (3 << 12)
  149. +
  150. +#define TRGMII_TXCTRL_TXC_INV BIT(30)
  151. +#define P6ECR_INTF_MODE_RGMII BIT(1)
  152. +#define P5RGMIIRXCR_C_ALIGN BIT(8)
  153. +#define P5RGMIIRXCR_DELAY_2 BIT(1)
  154. +#define P5RGMIITXCR_DELAY_2 (BIT(8) | BIT(2))
  155. +
  156. +/* TOP_SIG_CTRL bits */
  157. +#define TOP_SIG_CTRL_NORMAL (BIT(17) | BIT(16))
  158. +
  159. +/* MHWTRAP bits */
  160. +#define MHWTRAP_MANUAL BIT(16)
  161. +#define MHWTRAP_P5_MAC_SEL BIT(13)
  162. +#define MHWTRAP_P6_DIS BIT(8)
  163. +#define MHWTRAP_P5_RGMII_MODE BIT(7)
  164. +#define MHWTRAP_P5_DIS BIT(6)
  165. +#define MHWTRAP_PHY_ACCESS BIT(5)
  166. +
  167. +/* HWTRAP bits */
  168. +#define HWTRAP_XTAL_SHIFT 9
  169. +#define HWTRAP_XTAL_MASK 0x3
  170. +
  171. +/* SYS_CTRL bits */
  172. +#define SYS_CTRL_SW_RST BIT(1)
  173. +#define SYS_CTRL_REG_RST BIT(0)
  174. +
  175. +/* PMCR bits */
  176. +#define PMCR_IFG_XMIT_96 BIT(18)
  177. +#define PMCR_MAC_MODE BIT(16)
  178. +#define PMCR_FORCE_MODE BIT(15)
  179. +#define PMCR_TX_EN BIT(14)
  180. +#define PMCR_RX_EN BIT(13)
  181. +#define PMCR_BACK_PRES_EN BIT(9)
  182. +#define PMCR_BACKOFF_EN BIT(8)
  183. +#define PMCR_TX_FC_EN BIT(5)
  184. +#define PMCR_RX_FC_EN BIT(4)
  185. +#define PMCR_FORCE_SPEED_1000 BIT(3)
  186. +#define PMCR_FORCE_FDX BIT(1)
  187. +#define PMCR_FORCE_LNK BIT(0)
  188. +#define PMCR_FIXED_LINK (PMCR_IFG_XMIT_96 | PMCR_MAC_MODE | \
  189. + PMCR_FORCE_MODE | PMCR_TX_EN | PMCR_RX_EN | \
  190. + PMCR_BACK_PRES_EN | PMCR_BACKOFF_EN | \
  191. + PMCR_FORCE_SPEED_1000 | PMCR_FORCE_FDX | \
  192. + PMCR_FORCE_LNK)
  193. +
  194. +#define PMCR_FIXED_LINK_FC (PMCR_FIXED_LINK | \
  195. + PMCR_TX_FC_EN | PMCR_RX_FC_EN)
  196. +
  197. +/* TRGMII control registers */
  198. +#define GSW_INTF_MODE 0x390
  199. +#define GSW_TRGMII_TD0_ODT 0x354
  200. +#define GSW_TRGMII_TD1_ODT 0x35c
  201. +#define GSW_TRGMII_TD2_ODT 0x364
  202. +#define GSW_TRGMII_TD3_ODT 0x36c
  203. +#define GSW_TRGMII_TXCTL_ODT 0x374
  204. +#define GSW_TRGMII_TCK_ODT 0x37c
  205. +#define GSW_TRGMII_RCK_CTRL 0x300
  206. +
  207. +#define INTF_MODE_TRGMII BIT(1)
  208. +#define TRGMII_RCK_CTRL_RX_RST BIT(31)
  209. +
  210. +
  211. +/* possible XTAL speed */
  212. +#define MT7623_XTAL_40 0
  213. +#define MT7623_XTAL_20 1
  214. +#define MT7623_XTAL_25 3
  215. +
  216. +/* GPIO port control registers */
  217. +#define GPIO_OD33_CTRL8 0x4c0
  218. +#define GPIO_BIAS_CTRL 0xed0
  219. +#define GPIO_DRV_SEL10 0xf00
  220. +
  221. +/* on MT7620 the functio of port 4 can be software configured */
  222. +enum {
  223. + PORT4_EPHY = 0,
  224. + PORT4_EXT,
  225. +};
  226. +
  227. +/* struct mt7620_gsw - the structure that holds the SoC specific data
  228. + * @dev: The Device struct
  229. + * @base: The base address
  230. + * @piac_offset: The PIAC base may change depending on SoC
  231. + * @irq: The IRQ we are using
  232. + * @port4: The port4 mode on MT7620
  233. + * @autopoll: Is MDIO autopolling enabled
  234. + * @ethsys: The ethsys register map
  235. + * @pctl: The pin control register map
  236. + * @clk_trgpll: The trgmii pll clock
  237. + */
  238. +struct mt7620_gsw {
  239. + struct mtk_eth *eth;
  240. + struct device *dev;
  241. + void __iomem *base;
  242. + u32 piac_offset;
  243. + int irq;
  244. + int port4;
  245. + unsigned long int autopoll;
  246. +
  247. + struct regmap *ethsys;
  248. + struct regmap *pctl;
  249. +
  250. + struct clk *clk_trgpll;
  251. +
  252. + int trgmii_force;
  253. + bool wllll;
  254. +};
  255. +
  256. +/* switch register I/O wrappers */
  257. +void mtk_switch_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg);
  258. +u32 mtk_switch_r32(struct mt7620_gsw *gsw, unsigned reg);
  259. +
  260. +/* the callback used by the driver core to bringup the switch */
  261. +int mtk_gsw_init(struct mtk_eth *eth);
  262. +
  263. +/* MDIO access wrappers */
  264. +int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
  265. +int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
  266. +void mt7620_mdio_link_adjust(struct mtk_eth *eth, int port);
  267. +int mt7620_has_carrier(struct mtk_eth *eth);
  268. +void mt7620_print_link_state(struct mtk_eth *eth, int port, int link,
  269. + int speed, int duplex);
  270. +void mt7530_mdio_w32(struct mt7620_gsw *gsw, u32 reg, u32 val);
  271. +u32 mt7530_mdio_r32(struct mt7620_gsw *gsw, u32 reg);
  272. +void mt7530_mdio_m32(struct mt7620_gsw *gsw, u32 mask, u32 set, u32 reg);
  273. +
  274. +u32 _mtk_mdio_write(struct mtk_eth *eth, u32 phy_addr,
  275. + u32 phy_register, u32 write_data);
  276. +u32 _mtk_mdio_read(struct mtk_eth *eth, int phy_addr, int phy_reg);
  277. +void mt7620_handle_carrier(struct mtk_eth *eth);
  278. +
  279. +#endif
  280. --- /dev/null
  281. +++ b/drivers/net/ethernet/mediatek/gsw_mt7623.c
  282. @@ -0,0 +1,1084 @@
  283. +/* This program is free software; you can redistribute it and/or modify
  284. + * it under the terms of the GNU General Public License as published by
  285. + * the Free Software Foundation; version 2 of the License
  286. + *
  287. + * This program is distributed in the hope that it will be useful,
  288. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  289. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  290. + * GNU General Public License for more details.
  291. + *
  292. + * Copyright (C) 2009-2016 John Crispin <blogic@openwrt.org>
  293. + * Copyright (C) 2009-2016 Felix Fietkau <nbd@nbd.name>
  294. + * Copyright (C) 2013-2016 Michael Lee <igvtee@gmail.com>
  295. + */
  296. +
  297. +#include <linux/module.h>
  298. +#include <linux/kernel.h>
  299. +#include <linux/types.h>
  300. +#include <linux/platform_device.h>
  301. +#include <linux/of_device.h>
  302. +#include <linux/of_irq.h>
  303. +#include <linux/of_gpio.h>
  304. +#include <linux/of_mdio.h>
  305. +#include <linux/clk.h>
  306. +#include <linux/mfd/syscon.h>
  307. +#include <linux/regulator/consumer.h>
  308. +#include <linux/pm_runtime.h>
  309. +#include <linux/regmap.h>
  310. +#include <linux/reset.h>
  311. +#include <linux/mii.h>
  312. +#include <linux/interrupt.h>
  313. +#include <linux/netdevice.h>
  314. +#include <linux/dma-mapping.h>
  315. +#include <linux/phy.h>
  316. +#include <linux/ethtool.h>
  317. +#include <linux/version.h>
  318. +#include <linux/atomic.h>
  319. +
  320. +#include "mtk_eth_soc.h"
  321. +#include "gsw_mt7620.h"
  322. +#include "mt7530.h"
  323. +
  324. +#define ETHSYS_CLKCFG0 0x2c
  325. +#define ETHSYS_TRGMII_CLK_SEL362_5 BIT(11)
  326. +
  327. +void mt7530_mdio_w32(struct mt7620_gsw *gsw, u32 reg, u32 val)
  328. +{
  329. + _mtk_mdio_write(gsw->eth, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
  330. + _mtk_mdio_write(gsw->eth, 0x1f, (reg >> 2) & 0xf, val & 0xffff);
  331. + _mtk_mdio_write(gsw->eth, 0x1f, 0x10, val >> 16);
  332. +}
  333. +
  334. +u32 mt7530_mdio_r32(struct mt7620_gsw *gsw, u32 reg)
  335. +{
  336. + u16 high, low;
  337. +
  338. + _mtk_mdio_write(gsw->eth, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
  339. + low = _mtk_mdio_read(gsw->eth, 0x1f, (reg >> 2) & 0xf);
  340. + high = _mtk_mdio_read(gsw->eth, 0x1f, 0x10);
  341. +
  342. + return (high << 16) | (low & 0xffff);
  343. +}
  344. +
  345. +void mt7530_mdio_m32(struct mt7620_gsw *gsw, u32 mask, u32 set, u32 reg)
  346. +{
  347. + u32 val = mt7530_mdio_r32(gsw, reg);
  348. +
  349. + val &= mask;
  350. + val |= set;
  351. + mt7530_mdio_w32(gsw, reg, val);
  352. +}
  353. +
  354. +void mtk_switch_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
  355. +{
  356. + mtk_w32(gsw->eth, val, reg + 0x10000);
  357. +}
  358. +
  359. +u32 mtk_switch_r32(struct mt7620_gsw *gsw, unsigned reg)
  360. +{
  361. + return mtk_r32(gsw->eth, reg + 0x10000);
  362. +}
  363. +
  364. +void mtk_switch_m32(struct mt7620_gsw *gsw, u32 mask, u32 set, unsigned reg)
  365. +{
  366. + u32 val = mtk_switch_r32(gsw, reg);
  367. +
  368. + val &= mask;
  369. + val |= set;
  370. +
  371. + mtk_switch_w32(gsw, val, reg);
  372. +}
  373. +
  374. +int mt7623_gsw_config(struct mtk_eth *eth)
  375. +{
  376. + if (eth->mii_bus && eth->mii_bus->phy_map[0x1f])
  377. + mt7530_probe(eth->dev, NULL, eth->mii_bus, 1);
  378. +
  379. + return 0;
  380. +}
  381. +
  382. +static irqreturn_t gsw_interrupt_mt7623(int irq, void *_eth)
  383. +{
  384. + struct mtk_eth *eth = (struct mtk_eth *)_eth;
  385. + struct mt7620_gsw *gsw = (struct mt7620_gsw *)eth->sw_priv;
  386. + u32 reg, i;
  387. +
  388. + reg = mt7530_mdio_r32(gsw, 0x700c);
  389. +
  390. + for (i = 0; i < 5; i++)
  391. + if (reg & BIT(i)) {
  392. + unsigned int link;
  393. +
  394. + link = mt7530_mdio_r32(gsw,
  395. + 0x3008 + (i * 0x100)) & 0x1;
  396. +
  397. + if (link)
  398. + dev_info(gsw->dev,
  399. + "port %d link up\n", i);
  400. + else
  401. + dev_info(gsw->dev,
  402. + "port %d link down\n", i);
  403. + }
  404. +
  405. +// mt7620_handle_carrier(eth);
  406. + mt7530_mdio_w32(gsw, 0x700c, 0x1f);
  407. +
  408. + return IRQ_HANDLED;
  409. +}
  410. +
  411. +static void wait_loop(struct mt7620_gsw *gsw)
  412. +{
  413. + int i;
  414. + int read_data;
  415. +
  416. + for (i = 0; i < 320; i = i + 1)
  417. + read_data = mtk_switch_r32(gsw, 0x610);
  418. +}
  419. +
  420. +static void trgmii_calibration_7623(struct mt7620_gsw *gsw)
  421. +{
  422. +
  423. + unsigned int tap_a[5] = { 0, 0, 0, 0, 0 }; /* minumum delay for all correct */
  424. + unsigned int tap_b[5] = { 0, 0, 0, 0, 0 }; /* maximum delay for all correct */
  425. + unsigned int final_tap[5];
  426. + unsigned int rxc_step_size;
  427. + unsigned int rxd_step_size;
  428. + unsigned int read_data;
  429. + unsigned int tmp;
  430. + unsigned int rd_wd;
  431. + int i;
  432. + unsigned int err_cnt[5];
  433. + unsigned int init_toggle_data;
  434. + unsigned int err_flag[5];
  435. + unsigned int err_total_flag;
  436. + unsigned int training_word;
  437. + unsigned int rd_tap;
  438. + u32 val;
  439. +
  440. + u32 TRGMII_7623_base;
  441. + u32 TRGMII_7623_RD_0;
  442. + u32 TRGMII_RCK_CTRL;
  443. +
  444. + TRGMII_7623_base = 0x300; /* 0xFB110300 */
  445. + TRGMII_7623_RD_0 = TRGMII_7623_base + 0x10;
  446. + TRGMII_RCK_CTRL = TRGMII_7623_base;
  447. + rxd_step_size = 0x1;
  448. + rxc_step_size = 0x4;
  449. + init_toggle_data = 0x00000055;
  450. + training_word = 0x000000AC;
  451. +
  452. + /* RX clock gating in MT7623 */
  453. + mtk_switch_m32(gsw, 0x3fffffff, 0, TRGMII_7623_base + 0x04);
  454. +
  455. + /* Assert RX reset in MT7623 */
  456. + mtk_switch_m32(gsw, 0, 0x80000000, TRGMII_7623_base + 0x00);
  457. +
  458. + /* Set TX OE edge in MT7623 */
  459. + mtk_switch_m32(gsw, 0, 0x00002000, TRGMII_7623_base + 0x78);
  460. +
  461. + /* Disable RX clock gating in MT7623 */
  462. + mtk_switch_m32(gsw, 0, 0xC0000000, TRGMII_7623_base + 0x04);
  463. +
  464. + /* Release RX reset in MT7623 */
  465. + mtk_switch_m32(gsw, 0x7fffffff, 0, TRGMII_7623_base);
  466. +
  467. + for (i = 0; i < 5; i++)
  468. + mtk_switch_m32(gsw, 0, 0x80000000, TRGMII_7623_RD_0 + i * 8);
  469. +
  470. + pr_err("Enable Training Mode in MT7530\n");
  471. + read_data = mt7530_mdio_r32(gsw, 0x7A40);
  472. + read_data |= 0xC0000000;
  473. + mt7530_mdio_w32(gsw, 0x7A40, read_data); /* Enable Training Mode in MT7530 */
  474. + err_total_flag = 0;
  475. + pr_err("Adjust RXC delay in MT7623\n");
  476. + read_data = 0x0;
  477. + while (err_total_flag == 0 && read_data != 0x68) {
  478. + pr_err("2nd Enable EDGE CHK in MT7623\n");
  479. + /* Enable EDGE CHK in MT7623 */
  480. + for (i = 0; i < 5; i++)
  481. + mtk_switch_m32(gsw, 0x4fffffff, 0x40000000, TRGMII_7623_RD_0 + i * 8);
  482. +
  483. + wait_loop(gsw);
  484. + err_total_flag = 1;
  485. + for (i = 0; i < 5; i++) {
  486. + err_cnt[i] =
  487. + mtk_switch_r32(gsw, TRGMII_7623_RD_0 + i * 8) >> 8;
  488. + err_cnt[i] &= 0x0000000f;
  489. + rd_wd = mtk_switch_r32(gsw, TRGMII_7623_RD_0 + i * 8) >> 16;
  490. + rd_wd &= 0x000000ff;
  491. + val = mtk_switch_r32(gsw, TRGMII_7623_RD_0 + i * 8);
  492. + pr_err("ERR_CNT = %d, RD_WD =%x, TRGMII_7623_RD_0=%x\n",
  493. + err_cnt[i], rd_wd, val);
  494. + if (err_cnt[i] != 0) {
  495. + err_flag[i] = 1;
  496. + } else if (rd_wd != 0x55) {
  497. + err_flag[i] = 1;
  498. + } else {
  499. + err_flag[i] = 0;
  500. + }
  501. + err_total_flag = err_flag[i] & err_total_flag;
  502. + }
  503. +
  504. + pr_err("2nd Disable EDGE CHK in MT7623\n");
  505. + /* Disable EDGE CHK in MT7623 */
  506. + for (i = 0; i < 5; i++)
  507. + mtk_switch_m32(gsw, 0x4fffffff, 0x40000000, TRGMII_7623_RD_0 + i * 8);
  508. + wait_loop(gsw);
  509. + pr_err("2nd Disable EDGE CHK in MT7623\n");
  510. + /* Adjust RXC delay */
  511. + /* RX clock gating in MT7623 */
  512. + mtk_switch_m32(gsw, 0x3fffffff, 0, TRGMII_7623_base + 0x04);
  513. + read_data = mtk_switch_r32(gsw, TRGMII_7623_base);
  514. + if (err_total_flag == 0) {
  515. + tmp = (read_data & 0x0000007f) + rxc_step_size;
  516. + pr_err(" RXC delay = %d\n", tmp);
  517. + read_data >>= 8;
  518. + read_data &= 0xffffff80;
  519. + read_data |= tmp;
  520. + read_data <<= 8;
  521. + read_data &= 0xffffff80;
  522. + read_data |= tmp;
  523. + mtk_switch_w32(gsw, read_data, TRGMII_7623_base);
  524. + } else {
  525. + tmp = (read_data & 0x0000007f) + 16;
  526. + pr_err(" RXC delay = %d\n", tmp);
  527. + read_data >>= 8;
  528. + read_data &= 0xffffff80;
  529. + read_data |= tmp;
  530. + read_data <<= 8;
  531. + read_data &= 0xffffff80;
  532. + read_data |= tmp;
  533. + mtk_switch_w32(gsw, read_data, TRGMII_7623_base);
  534. + }
  535. + read_data &= 0x000000ff;
  536. +
  537. + /* Disable RX clock gating in MT7623 */
  538. + mtk_switch_m32(gsw, 0, 0xC0000000, TRGMII_7623_base + 0x04);
  539. + for (i = 0; i < 5; i++)
  540. + mtk_switch_m32(gsw, 0, 0x80000000, TRGMII_7623_RD_0 + i * 8);
  541. + }
  542. +
  543. + /* Read RD_WD MT7623 */
  544. + for (i = 0; i < 5; i++) {
  545. + rd_tap = 0;
  546. + while (err_flag[i] != 0 && rd_tap != 128) {
  547. + /* Enable EDGE CHK in MT7623 */
  548. + mtk_switch_m32(gsw, 0x4fffffff, 0x40000000, TRGMII_7623_RD_0 + i * 8);
  549. + wait_loop(gsw);
  550. +
  551. + read_data = mtk_switch_r32(gsw, TRGMII_7623_RD_0 + i * 8);
  552. + err_cnt[i] = (read_data >> 8) & 0x0000000f; /* Read MT7623 Errcnt */
  553. + rd_wd = (read_data >> 16) & 0x000000ff;
  554. + if (err_cnt[i] != 0 || rd_wd != 0x55) {
  555. + err_flag[i] = 1;
  556. + } else {
  557. + err_flag[i] = 0;
  558. + }
  559. + /* Disable EDGE CHK in MT7623 */
  560. + mtk_switch_m32(gsw, 0x4fffffff, 0x40000000, TRGMII_7623_RD_0 + i * 8);
  561. + wait_loop(gsw);
  562. + if (err_flag[i] != 0) {
  563. + rd_tap = (read_data & 0x0000007f) + rxd_step_size; /* Add RXD delay in MT7623 */
  564. + read_data = (read_data & 0xffffff80) | rd_tap;
  565. + mtk_switch_w32(gsw, read_data,
  566. + TRGMII_7623_RD_0 + i * 8);
  567. + tap_a[i] = rd_tap;
  568. + } else {
  569. + rd_tap = (read_data & 0x0000007f) + 48;
  570. + read_data = (read_data & 0xffffff80) | rd_tap;
  571. + mtk_switch_w32(gsw, read_data,
  572. + TRGMII_7623_RD_0 + i * 8);
  573. + }
  574. +
  575. + }
  576. + pr_err("MT7623 %dth bit Tap_a = %d\n", i, tap_a[i]);
  577. + }
  578. + /* pr_err("Last While Loop\n"); */
  579. + for (i = 0; i < 5; i++) {
  580. + while ((err_flag[i] == 0) && (rd_tap != 128)) {
  581. + read_data = mtk_switch_r32(gsw, TRGMII_7623_RD_0 + i * 8);
  582. + rd_tap = (read_data & 0x0000007f) + rxd_step_size; /* Add RXD delay in MT7623 */
  583. + read_data = (read_data & 0xffffff80) | rd_tap;
  584. + mtk_switch_w32(gsw, read_data, TRGMII_7623_RD_0 + i * 8);
  585. + /* Enable EDGE CHK in MT7623 */
  586. + val =
  587. + mtk_switch_r32(gsw, TRGMII_7623_RD_0 + i * 8) | 0x40000000;
  588. + val &= 0x4fffffff;
  589. + mtk_switch_w32(gsw, val, TRGMII_7623_RD_0 + i * 8);
  590. + wait_loop(gsw);
  591. + read_data = mtk_switch_r32(gsw, TRGMII_7623_RD_0 + i * 8);
  592. + err_cnt[i] = (read_data >> 8) & 0x0000000f; /* Read MT7623 Errcnt */
  593. + rd_wd = (read_data >> 16) & 0x000000ff;
  594. + if (err_cnt[i] != 0 || rd_wd != 0x55) {
  595. + err_flag[i] = 1;
  596. + } else {
  597. + err_flag[i] = 0;
  598. + }
  599. +
  600. + /* Disable EDGE CHK in MT7623 */
  601. + mtk_switch_m32(gsw, 0x4fffffff, 0x40000000, TRGMII_7623_RD_0 + i * 8);
  602. + wait_loop(gsw);
  603. +
  604. + }
  605. +
  606. + tap_b[i] = rd_tap; /* -rxd_step_size; */
  607. + pr_err("MT7623 %dth bit Tap_b = %d\n", i, tap_b[i]);
  608. + final_tap[i] = (tap_a[i] + tap_b[i]) / 2; /* Calculate RXD delay = (TAP_A + TAP_B)/2 */
  609. + read_data = (read_data & 0xffffff80) | final_tap[i];
  610. + mtk_switch_w32(gsw, read_data, TRGMII_7623_RD_0 + i * 8);
  611. + }
  612. +
  613. + read_data = mt7530_mdio_r32(gsw, 0x7A40);
  614. + read_data &= 0x3fffffff;
  615. + mt7530_mdio_w32(gsw, 0x7A40, read_data);
  616. +}
  617. +
  618. +static void trgmii_calibration_7530(struct mt7620_gsw *gsw)
  619. +{
  620. +
  621. + unsigned int tap_a[5] = { 0, 0, 0, 0, 0 };
  622. + unsigned int tap_b[5] = { 0, 0, 0, 0, 0 };
  623. + unsigned int final_tap[5];
  624. + unsigned int rxc_step_size;
  625. + unsigned int rxd_step_size;
  626. + unsigned int read_data;
  627. + unsigned int tmp = 0;
  628. + int i;
  629. + unsigned int err_cnt[5];
  630. + unsigned int rd_wd;
  631. + unsigned int init_toggle_data;
  632. + unsigned int err_flag[5];
  633. + unsigned int err_total_flag;
  634. + unsigned int training_word;
  635. + unsigned int rd_tap;
  636. +
  637. + u32 TRGMII_7623_base;
  638. + u32 TRGMII_7530_RD_0;
  639. + u32 TRGMII_RCK_CTRL;
  640. + u32 TRGMII_7530_base;
  641. + u32 TRGMII_7530_TX_base;
  642. + u32 val;
  643. +
  644. + TRGMII_7623_base = 0x300;
  645. + TRGMII_7530_base = 0x7A00;
  646. + TRGMII_7530_RD_0 = TRGMII_7530_base + 0x10;
  647. + TRGMII_RCK_CTRL = TRGMII_7623_base;
  648. + rxd_step_size = 0x1;
  649. + rxc_step_size = 0x8;
  650. + init_toggle_data = 0x00000055;
  651. + training_word = 0x000000AC;
  652. +
  653. + TRGMII_7530_TX_base = TRGMII_7530_base + 0x50;
  654. +
  655. + /* pr_err("Calibration begin ........\n"); */
  656. + val = mtk_switch_r32(gsw, TRGMII_7623_base + 0x40) | 0x80000000;
  657. + mtk_switch_w32(gsw, val, TRGMII_7623_base + 0x40);
  658. + read_data = mt7530_mdio_r32(gsw, 0x7a10);
  659. + /* pr_err("TRGMII_7530_RD_0 is %x\n", read_data); */
  660. +
  661. + read_data = mt7530_mdio_r32(gsw, TRGMII_7530_base + 0x04);
  662. + read_data &= 0x3fffffff;
  663. + mt7530_mdio_w32(gsw, TRGMII_7530_base + 0x04, read_data); /* RX clock gating in MT7530 */
  664. +
  665. + read_data = mt7530_mdio_r32(gsw, TRGMII_7530_base + 0x78);
  666. + read_data |= 0x00002000;
  667. + mt7530_mdio_w32(gsw, TRGMII_7530_base + 0x78, read_data); /* Set TX OE edge in MT7530 */
  668. +
  669. + read_data = mt7530_mdio_r32(gsw, TRGMII_7530_base);
  670. + read_data |= 0x80000000;
  671. + mt7530_mdio_w32(gsw, TRGMII_7530_base, read_data); /* Assert RX reset in MT7530 */
  672. +
  673. + read_data = mt7530_mdio_r32(gsw, TRGMII_7530_base);
  674. + read_data &= 0x7fffffff;
  675. + mt7530_mdio_w32(gsw, TRGMII_7530_base, read_data); /* Release RX reset in MT7530 */
  676. +
  677. + read_data = mt7530_mdio_r32(gsw, TRGMII_7530_base + 0x04);
  678. + read_data |= 0xC0000000;
  679. + mt7530_mdio_w32(gsw, TRGMII_7530_base + 0x04, read_data); /* Disable RX clock gating in MT7530 */
  680. +
  681. + /* pr_err("Enable Training Mode in MT7623\n"); */
  682. + /*Enable Training Mode in MT7623 */
  683. + val = mtk_switch_r32(gsw, TRGMII_7623_base + 0x40) | 0x80000000;
  684. + mtk_switch_w32(gsw, val, TRGMII_7623_base + 0x40);
  685. + if (gsw->trgmii_force == 2000) {
  686. + val = mtk_switch_r32(gsw, TRGMII_7623_base + 0x40) | 0xC0000000;
  687. + mtk_switch_w32(gsw, val, TRGMII_7623_base + 0x40);
  688. + } else {
  689. + val = mtk_switch_r32(gsw, TRGMII_7623_base + 0x40) | 0x80000000;
  690. + mtk_switch_w32(gsw, val, TRGMII_7623_base + 0x40);
  691. + }
  692. + val = mtk_switch_r32(gsw, TRGMII_7623_base + 0x078) & 0xfffff0ff;
  693. + mtk_switch_w32(gsw, val, TRGMII_7623_base + 0x078);
  694. + val = mtk_switch_r32(gsw, TRGMII_7623_base + 0x50) & 0xfffff0ff;
  695. + mtk_switch_w32(gsw, val, TRGMII_7623_base + 0x50);
  696. + val = mtk_switch_r32(gsw, TRGMII_7623_base + 0x58) & 0xfffff0ff;
  697. + mtk_switch_w32(gsw, val, TRGMII_7623_base + 0x58);
  698. + val = mtk_switch_r32(gsw, TRGMII_7623_base + 0x60) & 0xfffff0ff;
  699. + mtk_switch_w32(gsw, val, TRGMII_7623_base + 0x60);
  700. + val = mtk_switch_r32(gsw, TRGMII_7623_base + 0x68) & 0xfffff0ff;
  701. + mtk_switch_w32(gsw, val, TRGMII_7623_base + 0x68);
  702. + val = mtk_switch_r32(gsw, TRGMII_7623_base + 0x70) & 0xfffff0ff;
  703. + mtk_switch_w32(gsw, val, TRGMII_7623_base + 0x70);
  704. + val = mtk_switch_r32(gsw, TRGMII_7623_base + 0x78) & 0x00000800;
  705. + mtk_switch_w32(gsw, val, TRGMII_7623_base + 0x78);
  706. + err_total_flag = 0;
  707. + /* pr_err("Adjust RXC delay in MT7530\n"); */
  708. + read_data = 0x0;
  709. + while (err_total_flag == 0 && (read_data != 0x68)) {
  710. + /* pr_err("2nd Enable EDGE CHK in MT7530\n"); */
  711. + /* Enable EDGE CHK in MT7530 */
  712. + for (i = 0; i < 5; i++) {
  713. + read_data =
  714. + mt7530_mdio_r32(gsw, TRGMII_7530_RD_0 + i * 8);
  715. + read_data |= 0x40000000;
  716. + read_data &= 0x4fffffff;
  717. + mt7530_mdio_w32(gsw, TRGMII_7530_RD_0 + i * 8,
  718. + read_data);
  719. + wait_loop(gsw);
  720. + /* pr_err("2nd Disable EDGE CHK in MT7530\n"); */
  721. + err_cnt[i] =
  722. + mt7530_mdio_r32(gsw, TRGMII_7530_RD_0 + i * 8);
  723. + /* pr_err("***** MT7530 %dth bit ERR_CNT =%x\n",i, err_cnt[i]); */
  724. + /* pr_err("MT7530 %dth bit ERR_CNT =%x\n",i, err_cnt[i]); */
  725. + err_cnt[i] >>= 8;
  726. + err_cnt[i] &= 0x0000ff0f;
  727. + rd_wd = err_cnt[i] >> 8;
  728. + rd_wd &= 0x000000ff;
  729. + err_cnt[i] &= 0x0000000f;
  730. + /* read_data = mt7530_mdio_r32(gsw,0x7a10,&read_data); */
  731. + if (err_cnt[i] != 0) {
  732. + err_flag[i] = 1;
  733. + } else if (rd_wd != 0x55) {
  734. + err_flag[i] = 1;
  735. + } else {
  736. + err_flag[i] = 0;
  737. + }
  738. + if (i == 0) {
  739. + err_total_flag = err_flag[i];
  740. + } else {
  741. + err_total_flag = err_flag[i] & err_total_flag;
  742. + }
  743. + /* Disable EDGE CHK in MT7530 */
  744. + read_data =
  745. + mt7530_mdio_r32(gsw, TRGMII_7530_RD_0 + i * 8);
  746. + read_data |= 0x40000000;
  747. + read_data &= 0x4fffffff;
  748. + mt7530_mdio_w32(gsw, TRGMII_7530_RD_0 + i * 8,
  749. + read_data);
  750. + wait_loop(gsw);
  751. + }
  752. + /*Adjust RXC delay */
  753. + if (err_total_flag == 0) {
  754. + read_data = mt7530_mdio_r32(gsw, TRGMII_7530_base);
  755. + read_data |= 0x80000000;
  756. + mt7530_mdio_w32(gsw, TRGMII_7530_base, read_data); /* Assert RX reset in MT7530 */
  757. +
  758. + read_data =
  759. + mt7530_mdio_r32(gsw, TRGMII_7530_base + 0x04);
  760. + read_data &= 0x3fffffff;
  761. + mt7530_mdio_w32(gsw, TRGMII_7530_base + 0x04, read_data); /* RX clock gating in MT7530 */
  762. +
  763. + read_data = mt7530_mdio_r32(gsw, TRGMII_7530_base);
  764. + tmp = read_data;
  765. + tmp &= 0x0000007f;
  766. + tmp += rxc_step_size;
  767. + /* pr_err("Current rxc delay = %d\n", tmp); */
  768. + read_data &= 0xffffff80;
  769. + read_data |= tmp;
  770. + mt7530_mdio_w32(gsw, TRGMII_7530_base, read_data);
  771. + read_data = mt7530_mdio_r32(gsw, TRGMII_7530_base);
  772. + /* pr_err("Current RXC delay = %x\n", read_data); */
  773. +
  774. + read_data = mt7530_mdio_r32(gsw, TRGMII_7530_base);
  775. + read_data &= 0x7fffffff;
  776. + mt7530_mdio_w32(gsw, TRGMII_7530_base, read_data); /* Release RX reset in MT7530 */
  777. +
  778. + read_data =
  779. + mt7530_mdio_r32(gsw, TRGMII_7530_base + 0x04);
  780. + read_data |= 0xc0000000;
  781. + mt7530_mdio_w32(gsw, TRGMII_7530_base + 0x04, read_data); /* Disable RX clock gating in MT7530 */
  782. + pr_err("####### MT7530 RXC delay is %d\n", tmp);
  783. + }
  784. + read_data = tmp;
  785. + }
  786. + pr_err("Finish RXC Adjustment while loop\n");
  787. +
  788. + /* pr_err("Read RD_WD MT7530\n"); */
  789. + /* Read RD_WD MT7530 */
  790. + for (i = 0; i < 5; i++) {
  791. + rd_tap = 0;
  792. + while (err_flag[i] != 0 && rd_tap != 128) {
  793. + /* Enable EDGE CHK in MT7530 */
  794. + read_data =
  795. + mt7530_mdio_r32(gsw, TRGMII_7530_RD_0 + i * 8);
  796. + read_data |= 0x40000000;
  797. + read_data &= 0x4fffffff;
  798. + mt7530_mdio_w32(gsw, TRGMII_7530_RD_0 + i * 8,
  799. + read_data);
  800. + wait_loop(gsw);
  801. + err_cnt[i] = (read_data >> 8) & 0x0000000f;
  802. + rd_wd = (read_data >> 16) & 0x000000ff;
  803. + if (err_cnt[i] != 0 || rd_wd != 0x55) {
  804. + err_flag[i] = 1;
  805. + } else {
  806. + err_flag[i] = 0;
  807. + }
  808. + if (err_flag[i] != 0) {
  809. + rd_tap = (read_data & 0x0000007f) + rxd_step_size; /* Add RXD delay in MT7530 */
  810. + read_data = (read_data & 0xffffff80) | rd_tap;
  811. + mt7530_mdio_w32(gsw, TRGMII_7530_RD_0 + i * 8,
  812. + read_data);
  813. + tap_a[i] = rd_tap;
  814. + } else {
  815. + tap_a[i] = (read_data & 0x0000007f); /* Record the min delay TAP_A */
  816. + rd_tap = tap_a[i] + 0x4;
  817. + read_data = (read_data & 0xffffff80) | rd_tap;
  818. + mt7530_mdio_w32(gsw, TRGMII_7530_RD_0 + i * 8,
  819. + read_data);
  820. + }
  821. +
  822. + /* Disable EDGE CHK in MT7530 */
  823. + read_data =
  824. + mt7530_mdio_r32(gsw, TRGMII_7530_RD_0 + i * 8);
  825. + read_data |= 0x40000000;
  826. + read_data &= 0x4fffffff;
  827. + mt7530_mdio_w32(gsw, TRGMII_7530_RD_0 + i * 8,
  828. + read_data);
  829. + wait_loop(gsw);
  830. +
  831. + }
  832. + pr_err("MT7530 %dth bit Tap_a = %d\n", i, tap_a[i]);
  833. + }
  834. +
  835. + /* pr_err("Last While Loop\n"); */
  836. + for (i = 0; i < 5; i++) {
  837. + rd_tap = 0;
  838. + while (err_flag[i] == 0 && (rd_tap != 128)) {
  839. + /* Enable EDGE CHK in MT7530 */
  840. + read_data = mt7530_mdio_r32(gsw, TRGMII_7530_RD_0 + i * 8);
  841. + read_data |= 0x40000000;
  842. + read_data &= 0x4fffffff;
  843. + mt7530_mdio_w32(gsw, TRGMII_7530_RD_0 + i * 8,
  844. + read_data);
  845. + wait_loop(gsw);
  846. + err_cnt[i] = (read_data >> 8) & 0x0000000f;
  847. + rd_wd = (read_data >> 16) & 0x000000ff;
  848. + if (err_cnt[i] != 0 || rd_wd != 0x55)
  849. + err_flag[i] = 1;
  850. + else
  851. + err_flag[i] = 0;
  852. +
  853. + if (err_flag[i] == 0 && (rd_tap != 128)) {
  854. + /* Add RXD delay in MT7530 */
  855. + rd_tap = (read_data & 0x0000007f) + rxd_step_size;
  856. + read_data = (read_data & 0xffffff80) | rd_tap;
  857. + mt7530_mdio_w32(gsw, TRGMII_7530_RD_0 + i * 8,
  858. + read_data);
  859. + }
  860. + /* Disable EDGE CHK in MT7530 */
  861. + read_data =
  862. + mt7530_mdio_r32(gsw, TRGMII_7530_RD_0 + i * 8);
  863. + read_data |= 0x40000000;
  864. + read_data &= 0x4fffffff;
  865. + mt7530_mdio_w32(gsw, TRGMII_7530_RD_0 + i * 8,
  866. + read_data);
  867. + wait_loop(gsw);
  868. + }
  869. + tap_b[i] = rd_tap; /* - rxd_step_size; */
  870. + pr_err("MT7530 %dth bit Tap_b = %d\n", i, tap_b[i]);
  871. + /* Calculate RXD delay = (TAP_A + TAP_B)/2 */
  872. + final_tap[i] = (tap_a[i] + tap_b[i]) / 2;
  873. + /* pr_err("########****** MT7530 %dth bit Final Tap = %d\n", i, final_tap[i]); */
  874. +
  875. + read_data = (read_data & 0xffffff80) | final_tap[i];
  876. + mt7530_mdio_w32(gsw, TRGMII_7530_RD_0 + i * 8, read_data);
  877. + }
  878. +
  879. + if (gsw->trgmii_force == 2000)
  880. + mtk_switch_m32(gsw, 0x7fffffff, 0, TRGMII_7623_base + 0x40);
  881. + else
  882. + mtk_switch_m32(gsw, 0x3fffffff, 0, TRGMII_7623_base + 0x40);
  883. +
  884. +}
  885. +
  886. +static void mt7530_trgmii_clock_setting(struct mt7620_gsw *gsw, u32 xtal_mode)
  887. +{
  888. +
  889. + u32 regValue;
  890. +
  891. + /* TRGMII Clock */
  892. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  893. + _mtk_mdio_write(gsw->eth, 0, 14, 0x410);
  894. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  895. + _mtk_mdio_write(gsw->eth, 0, 14, 0x1);
  896. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  897. + _mtk_mdio_write(gsw->eth, 0, 14, 0x404);
  898. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  899. +
  900. + if (xtal_mode == 1) {
  901. + /* 25MHz */
  902. + if (gsw->trgmii_force == 2600)
  903. + /* 325MHz */
  904. + _mtk_mdio_write(gsw->eth, 0, 14, 0x1a00);
  905. + else if (gsw->trgmii_force == 2000)
  906. + /* 250MHz */
  907. + _mtk_mdio_write(gsw->eth, 0, 14, 0x1400);
  908. + } else if (xtal_mode == 2) {
  909. + /* 40MHz */
  910. + if (gsw->trgmii_force == 2600)
  911. + /* 325MHz */
  912. + _mtk_mdio_write(gsw->eth, 0, 14, 0x1040);
  913. + else if (gsw->trgmii_force == 2000)
  914. + /* 250MHz */
  915. + _mtk_mdio_write(gsw->eth, 0, 14, 0x0c80);
  916. + }
  917. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  918. + _mtk_mdio_write(gsw->eth, 0, 14, 0x405);
  919. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  920. + _mtk_mdio_write(gsw->eth, 0, 14, 0x0);
  921. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  922. + _mtk_mdio_write(gsw->eth, 0, 14, 0x409);
  923. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  924. + if (xtal_mode == 1)
  925. + /* 25MHz */
  926. + _mtk_mdio_write(gsw->eth, 0, 14, 0x0057);
  927. + else
  928. + /* 40MHz */
  929. + _mtk_mdio_write(gsw->eth, 0, 14, 0x0087);
  930. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  931. + _mtk_mdio_write(gsw->eth, 0, 14, 0x40a);
  932. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  933. + if (xtal_mode == 1)
  934. + /* 25MHz */
  935. + _mtk_mdio_write(gsw->eth, 0, 14, 0x0057);
  936. + else
  937. + /* 40MHz */
  938. + _mtk_mdio_write(gsw->eth, 0, 14, 0x0087);
  939. +
  940. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  941. + _mtk_mdio_write(gsw->eth, 0, 14, 0x403);
  942. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  943. + _mtk_mdio_write(gsw->eth, 0, 14, 0x1800);
  944. +
  945. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  946. + _mtk_mdio_write(gsw->eth, 0, 14, 0x403);
  947. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  948. + _mtk_mdio_write(gsw->eth, 0, 14, 0x1c00);
  949. +
  950. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  951. + _mtk_mdio_write(gsw->eth, 0, 14, 0x401);
  952. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  953. + _mtk_mdio_write(gsw->eth, 0, 14, 0xc020);
  954. +
  955. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  956. + _mtk_mdio_write(gsw->eth, 0, 14, 0x406);
  957. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  958. + _mtk_mdio_write(gsw->eth, 0, 14, 0xa030);
  959. +
  960. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  961. + _mtk_mdio_write(gsw->eth, 0, 14, 0x406);
  962. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  963. + _mtk_mdio_write(gsw->eth, 0, 14, 0xa038);
  964. +
  965. +// udelay(120); /* for MT7623 bring up test */
  966. +
  967. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  968. + _mtk_mdio_write(gsw->eth, 0, 14, 0x410);
  969. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  970. + _mtk_mdio_write(gsw->eth, 0, 14, 0x3);
  971. +
  972. + regValue = mt7530_mdio_r32(gsw, 0x7830);
  973. + regValue &= 0xFFFFFFFC;
  974. + regValue |= 0x00000001;
  975. + mt7530_mdio_w32(gsw, 0x7830, regValue);
  976. +
  977. + regValue = mt7530_mdio_r32(gsw, 0x7a40);
  978. + regValue &= ~(0x1 << 30);
  979. + regValue &= ~(0x1 << 28);
  980. + mt7530_mdio_w32(gsw, 0x7a40, regValue);
  981. +
  982. + mt7530_mdio_w32(gsw, 0x7a78, 0x55);
  983. +// udelay(100); /* for mt7623 bring up test */
  984. +
  985. + mtk_switch_m32(gsw, 0x7fffffff, 0, 0x300);
  986. +
  987. + trgmii_calibration_7623(gsw);
  988. + trgmii_calibration_7530(gsw);
  989. +
  990. + mtk_switch_m32(gsw, 0, 0x80000000, 0x300);
  991. + mtk_switch_m32(gsw, 0, 0x7fffffff, 0x300);
  992. +
  993. + /*MT7530 RXC reset */
  994. + regValue = mt7530_mdio_r32(gsw, 0x7a00);
  995. + regValue |= (0x1 << 31);
  996. + mt7530_mdio_w32(gsw, 0x7a00, regValue);
  997. + mdelay(1);
  998. + regValue &= ~(0x1 << 31);
  999. + mt7530_mdio_w32(gsw, 0x7a00, regValue);
  1000. + mdelay(100);
  1001. +}
  1002. +
  1003. +static void mt7623_hw_init(struct mtk_eth *eth, struct mt7620_gsw *gsw, struct device_node *np)
  1004. +{
  1005. + u32 i;
  1006. + u32 val;
  1007. + u32 xtal_mode;
  1008. +
  1009. + regmap_update_bits(gsw->ethsys, ETHSYS_CLKCFG0,
  1010. + ETHSYS_TRGMII_CLK_SEL362_5,
  1011. + ETHSYS_TRGMII_CLK_SEL362_5);
  1012. +
  1013. + /* reset the TRGMII core */
  1014. + mtk_switch_m32(gsw, 0, INTF_MODE_TRGMII, GSW_INTF_MODE);
  1015. + /* Assert MT7623 RXC reset */
  1016. + mtk_switch_m32(gsw, 0, TRGMII_RCK_CTRL_RX_RST, GSW_TRGMII_RCK_CTRL);
  1017. +
  1018. + /* Hardware reset Switch */
  1019. + device_reset(eth->dev);
  1020. +
  1021. + /* Wait for Switch Reset Completed*/
  1022. + for (i = 0; i < 100; i++) {
  1023. + mdelay(10);
  1024. + if (mt7530_mdio_r32(gsw, MT7530_HWTRAP))
  1025. + break;
  1026. + }
  1027. +
  1028. + /* turn off all PHYs */
  1029. + for (i = 0; i <= 4; i++) {
  1030. + val = _mtk_mdio_read(gsw->eth, i, 0x0);
  1031. + val |= BIT(11);
  1032. + _mtk_mdio_write(gsw->eth, i, 0x0, val);
  1033. + }
  1034. +
  1035. + /* reset the switch */
  1036. + mt7530_mdio_w32(gsw, MT7530_SYS_CTRL,
  1037. + SYS_CTRL_SW_RST | SYS_CTRL_REG_RST);
  1038. + udelay(100);
  1039. +
  1040. + /* GE1, Force 1000M/FD, FC ON */
  1041. + mt7530_mdio_w32(gsw, MT7530_PMCR_P(6), PMCR_FIXED_LINK_FC);
  1042. +
  1043. + /* GE2, Force 1000M/FD, FC ON */
  1044. + mt7530_mdio_w32(gsw, MT7530_PMCR_P(5), PMCR_FIXED_LINK_FC);
  1045. +
  1046. + /* Enable Port 6, P5 as GMAC5, P5 disable */
  1047. + val = mt7530_mdio_r32(gsw, MT7530_MHWTRAP);
  1048. + if (gsw->eth->mac[0] &&
  1049. + of_phy_is_fixed_link(gsw->eth->mac[0]->of_node))
  1050. + /* Enable Port 6 */
  1051. + val &= ~MHWTRAP_P6_DIS;
  1052. + else
  1053. + /* Disable Port 6 */
  1054. + val |= MHWTRAP_P6_DIS;
  1055. + if (gsw->eth->mac[1] &&
  1056. + of_phy_is_fixed_link(gsw->eth->mac[1]->of_node)) {
  1057. + /* Enable Port 5 */
  1058. + val &= ~MHWTRAP_P5_DIS;
  1059. + /* Port 5 as PHY */
  1060. + val &= ~MHWTRAP_P5_MAC_SEL;
  1061. + } else {
  1062. + /* Disable Port 5 */
  1063. + val |= MHWTRAP_P5_DIS;
  1064. + /* Port 5 as GMAC */
  1065. + val |= MHWTRAP_P5_MAC_SEL;
  1066. + val |= BIT(7);
  1067. + mt7530_mdio_w32(gsw, MT7530_PMCR_P(5), 0x8000);
  1068. + }
  1069. + /* gphy to port 0/4 */
  1070. + if (gsw->wllll)
  1071. + val |= BIT(20);
  1072. + else
  1073. + val &= ~BIT(20);
  1074. +
  1075. + /* Set MT7530 phy direct access mode**/
  1076. + val &= ~MHWTRAP_PHY_ACCESS;
  1077. + /* manual override of HW-Trap */
  1078. + val |= MHWTRAP_MANUAL;
  1079. + mt7530_mdio_w32(gsw, MT7530_MHWTRAP, val);
  1080. + dev_info(gsw->dev, "Setting MHWTRAP to 0x%08x\n", val);
  1081. +
  1082. + val = mt7530_mdio_r32(gsw, 0x7800);
  1083. + val = (val >> 9) & 0x3;
  1084. + if (val == 0x3) {
  1085. + xtal_mode = 1;
  1086. + /* 25Mhz Xtal - do nothing */
  1087. + } else if (val == 0x2) {
  1088. + /* 40Mhz */
  1089. + xtal_mode = 2;
  1090. +
  1091. + /* disable MT7530 core clock */
  1092. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  1093. + _mtk_mdio_write(gsw->eth, 0, 14, 0x410);
  1094. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  1095. + _mtk_mdio_write(gsw->eth, 0, 14, 0x0);
  1096. +
  1097. + /* disable MT7530 PLL */
  1098. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  1099. + _mtk_mdio_write(gsw->eth, 0, 14, 0x40d);
  1100. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  1101. + _mtk_mdio_write(gsw->eth, 0, 14, 0x2020);
  1102. +
  1103. + /* for MT7530 core clock = 500Mhz */
  1104. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  1105. + _mtk_mdio_write(gsw->eth, 0, 14, 0x40e);
  1106. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  1107. + _mtk_mdio_write(gsw->eth, 0, 14, 0x119);
  1108. +
  1109. + /* enable MT7530 PLL */
  1110. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  1111. + _mtk_mdio_write(gsw->eth, 0, 14, 0x40d);
  1112. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  1113. + _mtk_mdio_write(gsw->eth, 0, 14, 0x2820);
  1114. +
  1115. + udelay(20);
  1116. +
  1117. + /* enable MT7530 core clock */
  1118. + _mtk_mdio_write(gsw->eth, 0, 13, 0x1f);
  1119. + _mtk_mdio_write(gsw->eth, 0, 14, 0x410);
  1120. + _mtk_mdio_write(gsw->eth, 0, 13, 0x401f);
  1121. + } else {
  1122. + xtal_mode = 3;
  1123. + /* 20Mhz Xtal - TODO */
  1124. + }
  1125. +
  1126. + /* RGMII */
  1127. + _mtk_mdio_write(gsw->eth, 0, 14, 0x1);
  1128. +
  1129. + /* set MT7530 central align */
  1130. + val = mt7530_mdio_r32(gsw, 0x7830);
  1131. + val &= ~1;
  1132. + val |= 1<<1;
  1133. + mt7530_mdio_w32(gsw, 0x7830, val);
  1134. +
  1135. + val = mt7530_mdio_r32(gsw, 0x7a40);
  1136. + val &= ~(1<<30);
  1137. + mt7530_mdio_w32(gsw, 0x7a40, val);
  1138. +
  1139. + mt7530_mdio_w32(gsw, 0x7a78, 0x855);
  1140. +
  1141. + /* delay setting for 10/1000M */
  1142. + mt7530_mdio_w32(gsw, 0x7b00, 0x104);
  1143. + mt7530_mdio_w32(gsw, 0x7b04, 0x10);
  1144. +
  1145. + /* lower Tx Driving */
  1146. + mt7530_mdio_w32(gsw, 0x7a54, 0x88);
  1147. + mt7530_mdio_w32(gsw, 0x7a5c, 0x88);
  1148. + mt7530_mdio_w32(gsw, 0x7a64, 0x88);
  1149. + mt7530_mdio_w32(gsw, 0x7a6c, 0x88);
  1150. + mt7530_mdio_w32(gsw, 0x7a74, 0x88);
  1151. + mt7530_mdio_w32(gsw, 0x7a7c, 0x88);
  1152. + mt7530_mdio_w32(gsw, 0x7810, 0x11);
  1153. +
  1154. + /* Set MT7623/MT7683 TX Driving */
  1155. + mtk_switch_w32(gsw, 0x88, 0x354);
  1156. + mtk_switch_w32(gsw, 0x88, 0x35c);
  1157. + mtk_switch_w32(gsw, 0x88, 0x364);
  1158. + mtk_switch_w32(gsw, 0x88, 0x36c);
  1159. + mtk_switch_w32(gsw, 0x88, 0x374);
  1160. + mtk_switch_w32(gsw, 0x88, 0x37c);
  1161. +
  1162. + /* Set GE2 driving and slew rate */
  1163. + regmap_write(gsw->pctl, 0xF00, 0xe00);
  1164. + /* set GE2 TDSEL */
  1165. + regmap_write(gsw->pctl, 0x4C0, 0x5);
  1166. + /* set GE2 TUNE */
  1167. + regmap_write(gsw->pctl, 0xED0, 0x0);
  1168. +
  1169. + regmap_write(gsw->pctl, 0xb70, 0);
  1170. + regmap_write(gsw->pctl, 0x250, 0xffff);
  1171. + regmap_write(gsw->pctl, 0x260, 0xff);
  1172. + regmap_write(gsw->pctl, 0x380, 0x37);
  1173. + regmap_write(gsw->pctl, 0x390, 0x40);
  1174. +
  1175. + mt7530_trgmii_clock_setting(gsw, xtal_mode);
  1176. +
  1177. + //LANWANPartition(gsw);
  1178. +
  1179. + /* disable EEE */
  1180. + for (i = 0; i <= 4; i++) {
  1181. + _mtk_mdio_write(gsw->eth, i, 13, 0x7);
  1182. + _mtk_mdio_write(gsw->eth, i, 14, 0x3C);
  1183. + _mtk_mdio_write(gsw->eth, i, 13, 0x4007);
  1184. + _mtk_mdio_write(gsw->eth, i, 14, 0x0);
  1185. +
  1186. + /* Increase SlvDPSready time */
  1187. + _mtk_mdio_write(gsw->eth, i, 31, 0x52b5);
  1188. + _mtk_mdio_write(gsw->eth, i, 16, 0xafae);
  1189. + _mtk_mdio_write(gsw->eth, i, 18, 0x2f);
  1190. + _mtk_mdio_write(gsw->eth, i, 16, 0x8fae);
  1191. +
  1192. + /* Incease post_update_timer */
  1193. + _mtk_mdio_write(gsw->eth, i, 31, 0x3);
  1194. + _mtk_mdio_write(gsw->eth, i, 17, 0x4b);
  1195. +
  1196. + /* Adjust 100_mse_threshold */
  1197. + _mtk_mdio_write(gsw->eth, i, 13, 0x1e);
  1198. + _mtk_mdio_write(gsw->eth, i, 14, 0x123);
  1199. + _mtk_mdio_write(gsw->eth, i, 13, 0x401e);
  1200. + _mtk_mdio_write(gsw->eth, i, 14, 0xffff);
  1201. +
  1202. + /* Disable mcc */
  1203. + _mtk_mdio_write(gsw->eth, i, 13, 0x1e);
  1204. + _mtk_mdio_write(gsw->eth, i, 14, 0xa6);
  1205. + _mtk_mdio_write(gsw->eth, i, 13, 0x401e);
  1206. + _mtk_mdio_write(gsw->eth, i, 14, 0x300);
  1207. +
  1208. + /* Disable HW auto downshift*/
  1209. + _mtk_mdio_write(gsw->eth, i, 31, 0x1);
  1210. + val = _mtk_mdio_read(gsw->eth, i, 0x14);
  1211. + val &= ~(1<<4);
  1212. + _mtk_mdio_write(gsw->eth, i, 0x14, val);
  1213. + }
  1214. +
  1215. + /* turn on all PHYs */
  1216. + for (i = 0; i <= 4; i++) {
  1217. + val = _mtk_mdio_read(gsw->eth, i, 0);
  1218. + val &= ~BIT(11);
  1219. + _mtk_mdio_write(gsw->eth, i, 0, val);
  1220. + }
  1221. +
  1222. + /* enable irq */
  1223. + mt7530_mdio_m32(gsw, 0, TOP_SIG_CTRL_NORMAL, MT7530_TOP_SIG_CTRL);
  1224. +}
  1225. +
  1226. +static const struct of_device_id mediatek_gsw_match[] = {
  1227. + { .compatible = "mediatek,mt7623-gsw" },
  1228. + {},
  1229. +};
  1230. +MODULE_DEVICE_TABLE(of, mediatek_gsw_match);
  1231. +
  1232. +int mtk_gsw_init(struct mtk_eth *eth)
  1233. +{
  1234. + struct device_node *np = eth->switch_np;
  1235. + struct platform_device *pdev = of_find_device_by_node(np);
  1236. + struct mt7620_gsw *gsw;
  1237. +
  1238. + if (!pdev)
  1239. + return -ENODEV;
  1240. +
  1241. + if (!of_device_is_compatible(np, mediatek_gsw_match->compatible))
  1242. + return -EINVAL;
  1243. +
  1244. + gsw = platform_get_drvdata(pdev);
  1245. + if (!gsw)
  1246. + return -ENODEV;
  1247. + gsw->eth = eth;
  1248. + eth->sw_priv = gsw;
  1249. +
  1250. + mt7623_hw_init(eth, gsw, np);
  1251. +
  1252. + if (request_threaded_irq(gsw->irq, gsw_interrupt_mt7623, NULL, 0,
  1253. + "gsw", eth))
  1254. + pr_err("fail to request irq\n");
  1255. + mt7530_mdio_w32(gsw, 0x7008, 0x1f);
  1256. +
  1257. + return 0;
  1258. +}
  1259. +
  1260. +static int mt7623_gsw_probe(struct platform_device *pdev)
  1261. +{
  1262. + struct device_node *np = pdev->dev.of_node;
  1263. + struct device_node *pctl;
  1264. + int reset_pin, ret;
  1265. + struct mt7620_gsw *gsw;
  1266. + struct regulator *supply;
  1267. +
  1268. + gsw = devm_kzalloc(&pdev->dev, sizeof(struct mt7620_gsw), GFP_KERNEL);
  1269. + if (!gsw)
  1270. + return -ENOMEM;
  1271. +
  1272. + gsw->dev = &pdev->dev;
  1273. + gsw->trgmii_force = 2000;
  1274. + gsw->irq = irq_of_parse_and_map(np, 0);
  1275. + if (gsw->irq < 0)
  1276. + return -EINVAL;
  1277. +
  1278. + gsw->ethsys = syscon_regmap_lookup_by_phandle(np, "mediatek,ethsys");
  1279. + if (IS_ERR(gsw->ethsys))
  1280. + return PTR_ERR(gsw->ethsys);
  1281. +
  1282. + reset_pin = of_get_named_gpio(np, "mediatek,reset-pin", 0);
  1283. + if (reset_pin < 0)
  1284. + return reset_pin;
  1285. +
  1286. + pctl = of_parse_phandle(np, "mediatek,pctl-regmap", 0);
  1287. + if (IS_ERR(pctl))
  1288. + return PTR_ERR(pctl);
  1289. +
  1290. + gsw->pctl = syscon_node_to_regmap(pctl);
  1291. + if (IS_ERR(pctl))
  1292. + return PTR_ERR(pctl);
  1293. +
  1294. + ret = devm_gpio_request(&pdev->dev, reset_pin, "mt7530-reset");
  1295. + if (ret)
  1296. + return ret;
  1297. +
  1298. + gsw->clk_trgpll = devm_clk_get(&pdev->dev, "trgpll");
  1299. +
  1300. + if (IS_ERR(gsw->clk_trgpll))
  1301. + return -ENODEV;
  1302. +
  1303. + supply = devm_regulator_get(&pdev->dev, "mt7530");
  1304. + if (IS_ERR(supply))
  1305. + return PTR_ERR(supply);
  1306. +
  1307. + regulator_set_voltage(supply, 1000000, 1000000);
  1308. + ret = regulator_enable(supply);
  1309. + if (ret) {
  1310. + dev_err(&pdev->dev, "Failed to enable reg-7530: %d\n", ret);
  1311. + return ret;
  1312. + }
  1313. +
  1314. + gsw->wllll = of_property_read_bool(np, "mediatek,wllll");
  1315. +
  1316. + pm_runtime_enable(&pdev->dev);
  1317. + pm_runtime_get_sync(&pdev->dev);
  1318. +
  1319. + ret = clk_set_rate(gsw->clk_trgpll, 500000000);
  1320. + if (ret)
  1321. + return ret;
  1322. +
  1323. + regmap_write(gsw->ethsys, 0x34, 0x800000);
  1324. + regmap_write(gsw->ethsys, 0x34, 0x0);
  1325. +
  1326. + clk_prepare_enable(gsw->clk_trgpll);
  1327. +
  1328. + gpio_direction_output(reset_pin, 0);
  1329. + udelay(1000);
  1330. + gpio_set_value(reset_pin, 1);
  1331. + mdelay(100);
  1332. +
  1333. + platform_set_drvdata(pdev, gsw);
  1334. +
  1335. + return 0;
  1336. +}
  1337. +
  1338. +static int mt7623_gsw_remove(struct platform_device *pdev)
  1339. +{
  1340. + struct mt7620_gsw *gsw = platform_get_drvdata(pdev);
  1341. +
  1342. + clk_disable_unprepare(gsw->clk_trgpll);
  1343. +
  1344. + pm_runtime_put_sync(&pdev->dev);
  1345. + pm_runtime_disable(&pdev->dev);
  1346. +
  1347. + platform_set_drvdata(pdev, NULL);
  1348. +
  1349. + return 0;
  1350. +}
  1351. +
  1352. +static struct platform_driver gsw_driver = {
  1353. + .probe = mt7623_gsw_probe,
  1354. + .remove = mt7623_gsw_remove,
  1355. + .driver = {
  1356. + .name = "mt7623-gsw",
  1357. + .owner = THIS_MODULE,
  1358. + .of_match_table = mediatek_gsw_match,
  1359. + },
  1360. +};
  1361. +
  1362. +module_platform_driver(gsw_driver);
  1363. +
  1364. +MODULE_LICENSE("GPL");
  1365. +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
  1366. +MODULE_DESCRIPTION("GBit switch driver for Mediatek MT7623 SoC");
  1367. --- /dev/null
  1368. +++ b/drivers/net/ethernet/mediatek/mt7530.c
  1369. @@ -0,0 +1,808 @@
  1370. +/*
  1371. + * This program is free software; you can redistribute it and/or
  1372. + * modify it under the terms of the GNU General Public License
  1373. + * as published by the Free Software Foundation; either version 2
  1374. + * of the License, or (at your option) any later version.
  1375. + *
  1376. + * This program is distributed in the hope that it will be useful,
  1377. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1378. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1379. + * GNU General Public License for more details.
  1380. + *
  1381. + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  1382. + */
  1383. +
  1384. +#include <linux/if.h>
  1385. +#include <linux/module.h>
  1386. +#include <linux/init.h>
  1387. +#include <linux/list.h>
  1388. +#include <linux/if_ether.h>
  1389. +#include <linux/skbuff.h>
  1390. +#include <linux/netdevice.h>
  1391. +#include <linux/netlink.h>
  1392. +#include <linux/bitops.h>
  1393. +#include <net/genetlink.h>
  1394. +#include <linux/switch.h>
  1395. +#include <linux/delay.h>
  1396. +#include <linux/phy.h>
  1397. +#include <linux/netdevice.h>
  1398. +#include <linux/etherdevice.h>
  1399. +#include <linux/lockdep.h>
  1400. +#include <linux/workqueue.h>
  1401. +#include <linux/of_device.h>
  1402. +
  1403. +#include "mt7530.h"
  1404. +
  1405. +#define MT7530_CPU_PORT 6
  1406. +#define MT7530_NUM_PORTS 8
  1407. +#define MT7530_NUM_VLANS 16
  1408. +#define MT7530_MAX_VID 4095
  1409. +#define MT7530_MIN_VID 0
  1410. +
  1411. +/* registers */
  1412. +#define REG_ESW_VLAN_VTCR 0x90
  1413. +#define REG_ESW_VLAN_VAWD1 0x94
  1414. +#define REG_ESW_VLAN_VAWD2 0x98
  1415. +#define REG_ESW_VLAN_VTIM(x) (0x100 + 4 * ((x) / 2))
  1416. +
  1417. +#define REG_ESW_VLAN_VAWD1_IVL_MAC BIT(30)
  1418. +#define REG_ESW_VLAN_VAWD1_VTAG_EN BIT(28)
  1419. +#define REG_ESW_VLAN_VAWD1_VALID BIT(0)
  1420. +
  1421. +/* vlan egress mode */
  1422. +enum {
  1423. + ETAG_CTRL_UNTAG = 0,
  1424. + ETAG_CTRL_TAG = 2,
  1425. + ETAG_CTRL_SWAP = 1,
  1426. + ETAG_CTRL_STACK = 3,
  1427. +};
  1428. +
  1429. +#define REG_ESW_PORT_PCR(x) (0x2004 | ((x) << 8))
  1430. +#define REG_ESW_PORT_PVC(x) (0x2010 | ((x) << 8))
  1431. +#define REG_ESW_PORT_PPBV1(x) (0x2014 | ((x) << 8))
  1432. +
  1433. +#define REG_HWTRAP 0x7804
  1434. +
  1435. +#define MIB_DESC(_s , _o, _n) \
  1436. + { \
  1437. + .size = (_s), \
  1438. + .offset = (_o), \
  1439. + .name = (_n), \
  1440. + }
  1441. +
  1442. +struct mt7xxx_mib_desc {
  1443. + unsigned int size;
  1444. + unsigned int offset;
  1445. + const char *name;
  1446. +};
  1447. +
  1448. +#define MT7621_MIB_COUNTER_BASE 0x4000
  1449. +#define MT7621_MIB_COUNTER_PORT_OFFSET 0x100
  1450. +#define MT7621_STATS_TDPC 0x00
  1451. +#define MT7621_STATS_TCRC 0x04
  1452. +#define MT7621_STATS_TUPC 0x08
  1453. +#define MT7621_STATS_TMPC 0x0C
  1454. +#define MT7621_STATS_TBPC 0x10
  1455. +#define MT7621_STATS_TCEC 0x14
  1456. +#define MT7621_STATS_TSCEC 0x18
  1457. +#define MT7621_STATS_TMCEC 0x1C
  1458. +#define MT7621_STATS_TDEC 0x20
  1459. +#define MT7621_STATS_TLCEC 0x24
  1460. +#define MT7621_STATS_TXCEC 0x28
  1461. +#define MT7621_STATS_TPPC 0x2C
  1462. +#define MT7621_STATS_TL64PC 0x30
  1463. +#define MT7621_STATS_TL65PC 0x34
  1464. +#define MT7621_STATS_TL128PC 0x38
  1465. +#define MT7621_STATS_TL256PC 0x3C
  1466. +#define MT7621_STATS_TL512PC 0x40
  1467. +#define MT7621_STATS_TL1024PC 0x44
  1468. +#define MT7621_STATS_TOC 0x48
  1469. +#define MT7621_STATS_RDPC 0x60
  1470. +#define MT7621_STATS_RFPC 0x64
  1471. +#define MT7621_STATS_RUPC 0x68
  1472. +#define MT7621_STATS_RMPC 0x6C
  1473. +#define MT7621_STATS_RBPC 0x70
  1474. +#define MT7621_STATS_RAEPC 0x74
  1475. +#define MT7621_STATS_RCEPC 0x78
  1476. +#define MT7621_STATS_RUSPC 0x7C
  1477. +#define MT7621_STATS_RFEPC 0x80
  1478. +#define MT7621_STATS_ROSPC 0x84
  1479. +#define MT7621_STATS_RJEPC 0x88
  1480. +#define MT7621_STATS_RPPC 0x8C
  1481. +#define MT7621_STATS_RL64PC 0x90
  1482. +#define MT7621_STATS_RL65PC 0x94
  1483. +#define MT7621_STATS_RL128PC 0x98
  1484. +#define MT7621_STATS_RL256PC 0x9C
  1485. +#define MT7621_STATS_RL512PC 0xA0
  1486. +#define MT7621_STATS_RL1024PC 0xA4
  1487. +#define MT7621_STATS_ROC 0xA8
  1488. +#define MT7621_STATS_RDPC_CTRL 0xB0
  1489. +#define MT7621_STATS_RDPC_ING 0xB4
  1490. +#define MT7621_STATS_RDPC_ARL 0xB8
  1491. +
  1492. +static const struct mt7xxx_mib_desc mt7621_mibs[] = {
  1493. + MIB_DESC(1, MT7621_STATS_TDPC, "TxDrop"),
  1494. + MIB_DESC(1, MT7621_STATS_TCRC, "TxCRC"),
  1495. + MIB_DESC(1, MT7621_STATS_TUPC, "TxUni"),
  1496. + MIB_DESC(1, MT7621_STATS_TMPC, "TxMulti"),
  1497. + MIB_DESC(1, MT7621_STATS_TBPC, "TxBroad"),
  1498. + MIB_DESC(1, MT7621_STATS_TCEC, "TxCollision"),
  1499. + MIB_DESC(1, MT7621_STATS_TSCEC, "TxSingleCol"),
  1500. + MIB_DESC(1, MT7621_STATS_TMCEC, "TxMultiCol"),
  1501. + MIB_DESC(1, MT7621_STATS_TDEC, "TxDefer"),
  1502. + MIB_DESC(1, MT7621_STATS_TLCEC, "TxLateCol"),
  1503. + MIB_DESC(1, MT7621_STATS_TXCEC, "TxExcCol"),
  1504. + MIB_DESC(1, MT7621_STATS_TPPC, "TxPause"),
  1505. + MIB_DESC(1, MT7621_STATS_TL64PC, "Tx64Byte"),
  1506. + MIB_DESC(1, MT7621_STATS_TL65PC, "Tx65Byte"),
  1507. + MIB_DESC(1, MT7621_STATS_TL128PC, "Tx128Byte"),
  1508. + MIB_DESC(1, MT7621_STATS_TL256PC, "Tx256Byte"),
  1509. + MIB_DESC(1, MT7621_STATS_TL512PC, "Tx512Byte"),
  1510. + MIB_DESC(1, MT7621_STATS_TL1024PC, "Tx1024Byte"),
  1511. + MIB_DESC(2, MT7621_STATS_TOC, "TxByte"),
  1512. + MIB_DESC(1, MT7621_STATS_RDPC, "RxDrop"),
  1513. + MIB_DESC(1, MT7621_STATS_RFPC, "RxFiltered"),
  1514. + MIB_DESC(1, MT7621_STATS_RUPC, "RxUni"),
  1515. + MIB_DESC(1, MT7621_STATS_RMPC, "RxMulti"),
  1516. + MIB_DESC(1, MT7621_STATS_RBPC, "RxBroad"),
  1517. + MIB_DESC(1, MT7621_STATS_RAEPC, "RxAlignErr"),
  1518. + MIB_DESC(1, MT7621_STATS_RCEPC, "RxCRC"),
  1519. + MIB_DESC(1, MT7621_STATS_RUSPC, "RxUnderSize"),
  1520. + MIB_DESC(1, MT7621_STATS_RFEPC, "RxFragment"),
  1521. + MIB_DESC(1, MT7621_STATS_ROSPC, "RxOverSize"),
  1522. + MIB_DESC(1, MT7621_STATS_RJEPC, "RxJabber"),
  1523. + MIB_DESC(1, MT7621_STATS_RPPC, "RxPause"),
  1524. + MIB_DESC(1, MT7621_STATS_RL64PC, "Rx64Byte"),
  1525. + MIB_DESC(1, MT7621_STATS_RL65PC, "Rx65Byte"),
  1526. + MIB_DESC(1, MT7621_STATS_RL128PC, "Rx128Byte"),
  1527. + MIB_DESC(1, MT7621_STATS_RL256PC, "Rx256Byte"),
  1528. + MIB_DESC(1, MT7621_STATS_RL512PC, "Rx512Byte"),
  1529. + MIB_DESC(1, MT7621_STATS_RL1024PC, "Rx1024Byte"),
  1530. + MIB_DESC(2, MT7621_STATS_ROC, "RxByte"),
  1531. + MIB_DESC(1, MT7621_STATS_RDPC_CTRL, "RxCtrlDrop"),
  1532. + MIB_DESC(1, MT7621_STATS_RDPC_ING, "RxIngDrop"),
  1533. + MIB_DESC(1, MT7621_STATS_RDPC_ARL, "RxARLDrop")
  1534. +};
  1535. +
  1536. +enum {
  1537. + /* Global attributes. */
  1538. + MT7530_ATTR_ENABLE_VLAN,
  1539. +};
  1540. +
  1541. +struct mt7530_port_entry {
  1542. + u16 pvid;
  1543. +};
  1544. +
  1545. +struct mt7530_vlan_entry {
  1546. + u16 vid;
  1547. + u8 member;
  1548. + u8 etags;
  1549. +};
  1550. +
  1551. +struct mt7530_priv {
  1552. + void __iomem *base;
  1553. + struct mii_bus *bus;
  1554. + struct switch_dev swdev;
  1555. +
  1556. + bool global_vlan_enable;
  1557. + struct mt7530_vlan_entry vlan_entries[MT7530_NUM_VLANS];
  1558. + struct mt7530_port_entry port_entries[MT7530_NUM_PORTS];
  1559. +};
  1560. +
  1561. +struct mt7530_mapping {
  1562. + char *name;
  1563. + u16 pvids[MT7530_NUM_PORTS];
  1564. + u8 members[MT7530_NUM_VLANS];
  1565. + u8 etags[MT7530_NUM_VLANS];
  1566. + u16 vids[MT7530_NUM_VLANS];
  1567. +} mt7530_defaults[] = {
  1568. + {
  1569. + .name = "llllw",
  1570. + .pvids = { 1, 1, 1, 1, 2, 1, 1 },
  1571. + .members = { 0, 0x6f, 0x50 },
  1572. + .etags = { 0, 0x40, 0x40 },
  1573. + .vids = { 0, 1, 2 },
  1574. + }, {
  1575. + .name = "wllll",
  1576. + .pvids = { 2, 1, 1, 1, 1, 1, 1 },
  1577. + .members = { 0, 0x7e, 0x41 },
  1578. + .etags = { 0, 0x40, 0x40 },
  1579. + .vids = { 0, 1, 2 },
  1580. + },
  1581. +};
  1582. +
  1583. +struct mt7530_mapping*
  1584. +mt7530_find_mapping(struct device_node *np)
  1585. +{
  1586. + const char *map;
  1587. + int i;
  1588. +
  1589. + if (of_property_read_string(np, "mediatek,portmap", &map))
  1590. + return NULL;
  1591. +
  1592. + for (i = 0; i < ARRAY_SIZE(mt7530_defaults); i++)
  1593. + if (!strcmp(map, mt7530_defaults[i].name))
  1594. + return &mt7530_defaults[i];
  1595. +
  1596. + return NULL;
  1597. +}
  1598. +
  1599. +static void
  1600. +mt7530_apply_mapping(struct mt7530_priv *mt7530, struct mt7530_mapping *map)
  1601. +{
  1602. + int i = 0;
  1603. +
  1604. + for (i = 0; i < MT7530_NUM_PORTS; i++)
  1605. + mt7530->port_entries[i].pvid = map->pvids[i];
  1606. +
  1607. + for (i = 0; i < MT7530_NUM_VLANS; i++) {
  1608. + mt7530->vlan_entries[i].member = map->members[i];
  1609. + mt7530->vlan_entries[i].etags = map->etags[i];
  1610. + mt7530->vlan_entries[i].vid = map->vids[i];
  1611. + }
  1612. +}
  1613. +
  1614. +static int
  1615. +mt7530_reset_switch(struct switch_dev *dev)
  1616. +{
  1617. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  1618. + int i;
  1619. +
  1620. + memset(eth->port_entries, 0, sizeof(eth->port_entries));
  1621. + memset(eth->vlan_entries, 0, sizeof(eth->vlan_entries));
  1622. +
  1623. + /* set default vid of each vlan to the same number of vlan, so the vid
  1624. + * won't need be set explicitly.
  1625. + */
  1626. + for (i = 0; i < MT7530_NUM_VLANS; i++) {
  1627. + eth->vlan_entries[i].vid = i;
  1628. + }
  1629. +
  1630. + return 0;
  1631. +}
  1632. +
  1633. +static int
  1634. +mt7530_get_vlan_enable(struct switch_dev *dev,
  1635. + const struct switch_attr *attr,
  1636. + struct switch_val *val)
  1637. +{
  1638. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  1639. +
  1640. + val->value.i = eth->global_vlan_enable;
  1641. +
  1642. + return 0;
  1643. +}
  1644. +
  1645. +static int
  1646. +mt7530_set_vlan_enable(struct switch_dev *dev,
  1647. + const struct switch_attr *attr,
  1648. + struct switch_val *val)
  1649. +{
  1650. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  1651. +
  1652. + eth->global_vlan_enable = val->value.i != 0;
  1653. +
  1654. + return 0;
  1655. +}
  1656. +
  1657. +static u32
  1658. +mt7530_r32(struct mt7530_priv *eth, u32 reg)
  1659. +{
  1660. + u32 val;
  1661. + if (eth->bus) {
  1662. + u16 high, low;
  1663. +
  1664. + mdiobus_write(eth->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
  1665. + low = mdiobus_read(eth->bus, 0x1f, (reg >> 2) & 0xf);
  1666. + high = mdiobus_read(eth->bus, 0x1f, 0x10);
  1667. +
  1668. + return (high << 16) | (low & 0xffff);
  1669. + }
  1670. +
  1671. + val = ioread32(eth->base + reg);
  1672. + pr_debug("MT7530 MDIO Read [%04x]=%08x\n", reg, val);
  1673. +
  1674. + return val;
  1675. +}
  1676. +
  1677. +static void
  1678. +mt7530_w32(struct mt7530_priv *eth, u32 reg, u32 val)
  1679. +{
  1680. + if (eth->bus) {
  1681. + mdiobus_write(eth->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
  1682. + mdiobus_write(eth->bus, 0x1f, (reg >> 2) & 0xf, val & 0xffff);
  1683. + mdiobus_write(eth->bus, 0x1f, 0x10, val >> 16);
  1684. + return;
  1685. + }
  1686. +
  1687. + pr_debug("MT7530 MDIO Write[%04x]=%08x\n", reg, val);
  1688. + iowrite32(val, eth->base + reg);
  1689. +}
  1690. +
  1691. +static void
  1692. +mt7530_vtcr(struct mt7530_priv *eth, u32 cmd, u32 val)
  1693. +{
  1694. + int i;
  1695. +
  1696. + mt7530_w32(eth, REG_ESW_VLAN_VTCR, BIT(31) | (cmd << 12) | val);
  1697. +
  1698. + for (i = 0; i < 20; i++) {
  1699. + u32 val = mt7530_r32(eth, REG_ESW_VLAN_VTCR);
  1700. +
  1701. + if ((val & BIT(31)) == 0)
  1702. + break;
  1703. +
  1704. + udelay(1000);
  1705. + }
  1706. + if (i == 20)
  1707. + printk("mt7530: vtcr timeout\n");
  1708. +}
  1709. +
  1710. +static int
  1711. +mt7530_get_port_pvid(struct switch_dev *dev, int port, int *val)
  1712. +{
  1713. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  1714. +
  1715. + if (port >= MT7530_NUM_PORTS)
  1716. + return -EINVAL;
  1717. +
  1718. + *val = mt7530_r32(eth, REG_ESW_PORT_PPBV1(port));
  1719. + *val &= 0xfff;
  1720. +
  1721. + return 0;
  1722. +}
  1723. +
  1724. +static int
  1725. +mt7530_set_port_pvid(struct switch_dev *dev, int port, int pvid)
  1726. +{
  1727. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  1728. +
  1729. + if (port >= MT7530_NUM_PORTS)
  1730. + return -EINVAL;
  1731. +
  1732. + if (pvid < MT7530_MIN_VID || pvid > MT7530_MAX_VID)
  1733. + return -EINVAL;
  1734. +
  1735. + eth->port_entries[port].pvid = pvid;
  1736. +
  1737. + return 0;
  1738. +}
  1739. +
  1740. +static int
  1741. +mt7530_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  1742. +{
  1743. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  1744. + u32 member;
  1745. + u32 etags;
  1746. + int i;
  1747. +
  1748. + val->len = 0;
  1749. +
  1750. + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS)
  1751. + return -EINVAL;
  1752. +
  1753. + mt7530_vtcr(eth, 0, val->port_vlan);
  1754. +
  1755. + member = mt7530_r32(eth, REG_ESW_VLAN_VAWD1);
  1756. + member >>= 16;
  1757. + member &= 0xff;
  1758. +
  1759. + etags = mt7530_r32(eth, REG_ESW_VLAN_VAWD2);
  1760. +
  1761. + for (i = 0; i < MT7530_NUM_PORTS; i++) {
  1762. + struct switch_port *p;
  1763. + int etag;
  1764. +
  1765. + if (!(member & BIT(i)))
  1766. + continue;
  1767. +
  1768. + p = &val->value.ports[val->len++];
  1769. + p->id = i;
  1770. +
  1771. + etag = (etags >> (i * 2)) & 0x3;
  1772. +
  1773. + if (etag == ETAG_CTRL_TAG)
  1774. + p->flags |= BIT(SWITCH_PORT_FLAG_TAGGED);
  1775. + else if (etag != ETAG_CTRL_UNTAG)
  1776. + printk("vlan egress tag control neither untag nor tag.\n");
  1777. + }
  1778. +
  1779. + return 0;
  1780. +}
  1781. +
  1782. +static int
  1783. +mt7530_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  1784. +{
  1785. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  1786. + u8 member = 0;
  1787. + u8 etags = 0;
  1788. + int i;
  1789. +
  1790. + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS ||
  1791. + val->len > MT7530_NUM_PORTS)
  1792. + return -EINVAL;
  1793. +
  1794. + for (i = 0; i < val->len; i++) {
  1795. + struct switch_port *p = &val->value.ports[i];
  1796. +
  1797. + if (p->id >= MT7530_NUM_PORTS)
  1798. + return -EINVAL;
  1799. +
  1800. + member |= BIT(p->id);
  1801. +
  1802. + if (p->flags & BIT(SWITCH_PORT_FLAG_TAGGED))
  1803. + etags |= BIT(p->id);
  1804. + }
  1805. + eth->vlan_entries[val->port_vlan].member = member;
  1806. + eth->vlan_entries[val->port_vlan].etags = etags;
  1807. +
  1808. + return 0;
  1809. +}
  1810. +
  1811. +static int
  1812. +mt7530_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  1813. + struct switch_val *val)
  1814. +{
  1815. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  1816. + int vlan;
  1817. + u16 vid;
  1818. +
  1819. + vlan = val->port_vlan;
  1820. + vid = (u16)val->value.i;
  1821. +
  1822. + if (vlan < 0 || vlan >= MT7530_NUM_VLANS)
  1823. + return -EINVAL;
  1824. +
  1825. + if (vid < MT7530_MIN_VID || vid > MT7530_MAX_VID)
  1826. + return -EINVAL;
  1827. +
  1828. + eth->vlan_entries[vlan].vid = vid;
  1829. + return 0;
  1830. +}
  1831. +
  1832. +static int
  1833. +mt7530_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  1834. + struct switch_val *val)
  1835. +{
  1836. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  1837. + u32 vid;
  1838. + int vlan;
  1839. +
  1840. + vlan = val->port_vlan;
  1841. +
  1842. + vid = mt7530_r32(eth, REG_ESW_VLAN_VTIM(vlan));
  1843. + if (vlan & 1)
  1844. + vid = vid >> 12;
  1845. + vid &= 0xfff;
  1846. +
  1847. + val->value.i = vid;
  1848. + return 0;
  1849. +}
  1850. +
  1851. +static int
  1852. +mt7530_apply_config(struct switch_dev *dev)
  1853. +{
  1854. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  1855. + int i, j;
  1856. + u8 tag_ports;
  1857. + u8 untag_ports;
  1858. +
  1859. + if (!eth->global_vlan_enable) {
  1860. + for (i = 0; i < MT7530_NUM_PORTS; i++)
  1861. + mt7530_w32(eth, REG_ESW_PORT_PCR(i), 0x00ff0000);
  1862. +
  1863. + for (i = 0; i < MT7530_NUM_PORTS; i++)
  1864. + mt7530_w32(eth, REG_ESW_PORT_PVC(i), 0x810000c0);
  1865. +
  1866. + return 0;
  1867. + }
  1868. +
  1869. + /* set all ports as security mode */
  1870. + for (i = 0; i < MT7530_NUM_PORTS; i++)
  1871. + mt7530_w32(eth, REG_ESW_PORT_PCR(i), 0x00ff0003);
  1872. +
  1873. + /* check if a port is used in tag/untag vlan egress mode */
  1874. + tag_ports = 0;
  1875. + untag_ports = 0;
  1876. +
  1877. + for (i = 0; i < MT7530_NUM_VLANS; i++) {
  1878. + u8 member = eth->vlan_entries[i].member;
  1879. + u8 etags = eth->vlan_entries[i].etags;
  1880. +
  1881. + if (!member)
  1882. + continue;
  1883. +
  1884. + for (j = 0; j < MT7530_NUM_PORTS; j++) {
  1885. + if (!(member & BIT(j)))
  1886. + continue;
  1887. +
  1888. + if (etags & BIT(j))
  1889. + tag_ports |= 1u << j;
  1890. + else
  1891. + untag_ports |= 1u << j;
  1892. + }
  1893. + }
  1894. +
  1895. + /* set all untag-only ports as transparent and the rest as user port */
  1896. + for (i = 0; i < MT7530_NUM_PORTS; i++) {
  1897. + u32 pvc_mode = 0x81000000;
  1898. +
  1899. + if (untag_ports & BIT(i) && !(tag_ports & BIT(i)))
  1900. + pvc_mode = 0x810000c0;
  1901. +
  1902. + mt7530_w32(eth, REG_ESW_PORT_PVC(i), pvc_mode);
  1903. + }
  1904. +
  1905. + for (i = 0; i < MT7530_NUM_VLANS; i++) {
  1906. + u16 vid = eth->vlan_entries[i].vid;
  1907. + u8 member = eth->vlan_entries[i].member;
  1908. + u8 etags = eth->vlan_entries[i].etags;
  1909. + u32 val;
  1910. +
  1911. + /* vid of vlan */
  1912. + val = mt7530_r32(eth, REG_ESW_VLAN_VTIM(i));
  1913. + if (i % 2 == 0) {
  1914. + val &= 0xfff000;
  1915. + val |= vid;
  1916. + } else {
  1917. + val &= 0xfff;
  1918. + val |= (vid << 12);
  1919. + }
  1920. + mt7530_w32(eth, REG_ESW_VLAN_VTIM(i), val);
  1921. +
  1922. + /* vlan port membership */
  1923. + if (member)
  1924. + mt7530_w32(eth, REG_ESW_VLAN_VAWD1, REG_ESW_VLAN_VAWD1_IVL_MAC |
  1925. + REG_ESW_VLAN_VAWD1_VTAG_EN | (member << 16) |
  1926. + REG_ESW_VLAN_VAWD1_VALID);
  1927. + else
  1928. + mt7530_w32(eth, REG_ESW_VLAN_VAWD1, 0);
  1929. +
  1930. + /* egress mode */
  1931. + val = 0;
  1932. + for (j = 0; j < MT7530_NUM_PORTS; j++) {
  1933. + if (etags & BIT(j))
  1934. + val |= ETAG_CTRL_TAG << (j * 2);
  1935. + else
  1936. + val |= ETAG_CTRL_UNTAG << (j * 2);
  1937. + }
  1938. + mt7530_w32(eth, REG_ESW_VLAN_VAWD2, val);
  1939. +
  1940. + /* write to vlan table */
  1941. + mt7530_vtcr(eth, 1, i);
  1942. + }
  1943. +
  1944. + /* Port Default PVID */
  1945. + for (i = 0; i < MT7530_NUM_PORTS; i++) {
  1946. + u32 val;
  1947. + val = mt7530_r32(eth, REG_ESW_PORT_PPBV1(i));
  1948. + val &= ~0xfff;
  1949. + val |= eth->port_entries[i].pvid;
  1950. + mt7530_w32(eth, REG_ESW_PORT_PPBV1(i), val);
  1951. + }
  1952. +
  1953. + return 0;
  1954. +}
  1955. +
  1956. +static int
  1957. +mt7530_get_port_link(struct switch_dev *dev, int port,
  1958. + struct switch_port_link *link)
  1959. +{
  1960. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  1961. + u32 speed, pmsr;
  1962. +
  1963. + if (port < 0 || port >= MT7530_NUM_PORTS)
  1964. + return -EINVAL;
  1965. +
  1966. + pmsr = mt7530_r32(eth, 0x3008 + (0x100 * port));
  1967. +
  1968. + link->link = pmsr & 1;
  1969. + link->duplex = (pmsr >> 1) & 1;
  1970. + speed = (pmsr >> 2) & 3;
  1971. +
  1972. + switch (speed) {
  1973. + case 0:
  1974. + link->speed = SWITCH_PORT_SPEED_10;
  1975. + break;
  1976. + case 1:
  1977. + link->speed = SWITCH_PORT_SPEED_100;
  1978. + break;
  1979. + case 2:
  1980. + case 3: /* forced gige speed can be 2 or 3 */
  1981. + link->speed = SWITCH_PORT_SPEED_1000;
  1982. + break;
  1983. + default:
  1984. + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  1985. + break;
  1986. + }
  1987. +
  1988. + return 0;
  1989. +}
  1990. +
  1991. +static const struct switch_attr mt7530_global[] = {
  1992. + {
  1993. + .type = SWITCH_TYPE_INT,
  1994. + .name = "enable_vlan",
  1995. + .description = "VLAN mode (1:enabled)",
  1996. + .max = 1,
  1997. + .id = MT7530_ATTR_ENABLE_VLAN,
  1998. + .get = mt7530_get_vlan_enable,
  1999. + .set = mt7530_set_vlan_enable,
  2000. + },
  2001. +};
  2002. +
  2003. +static u64 get_mib_counter(struct mt7530_priv *eth, int i, int port)
  2004. +{
  2005. + unsigned int port_base;
  2006. + u64 t;
  2007. +
  2008. + port_base = MT7621_MIB_COUNTER_BASE +
  2009. + MT7621_MIB_COUNTER_PORT_OFFSET * port;
  2010. +
  2011. + t = mt7530_r32(eth, port_base + mt7621_mibs[i].offset);
  2012. + if (mt7621_mibs[i].size == 2) {
  2013. + u64 hi;
  2014. +
  2015. + hi = mt7530_r32(eth, port_base + mt7621_mibs[i].offset + 4);
  2016. + t |= hi << 32;
  2017. + }
  2018. +
  2019. + return t;
  2020. +}
  2021. +
  2022. +static int mt7621_sw_get_port_mib(struct switch_dev *dev,
  2023. + const struct switch_attr *attr,
  2024. + struct switch_val *val)
  2025. +{
  2026. + static char buf[4096];
  2027. + struct mt7530_priv *eth = container_of(dev, struct mt7530_priv, swdev);
  2028. + int i, len = 0;
  2029. +
  2030. + if (val->port_vlan >= MT7530_NUM_PORTS)
  2031. + return -EINVAL;
  2032. +
  2033. + len += snprintf(buf + len, sizeof(buf) - len,
  2034. + "Port %d MIB counters\n", val->port_vlan);
  2035. +
  2036. + for (i = 0; i < sizeof(mt7621_mibs) / sizeof(*mt7621_mibs); ++i) {
  2037. + u64 counter;
  2038. + len += snprintf(buf + len, sizeof(buf) - len,
  2039. + "%-11s: ", mt7621_mibs[i].name);
  2040. + counter = get_mib_counter(eth, i, val->port_vlan);
  2041. + len += snprintf(buf + len, sizeof(buf) - len, "%llu\n",
  2042. + counter);
  2043. + }
  2044. +
  2045. + val->value.s = buf;
  2046. + val->len = len;
  2047. + return 0;
  2048. +}
  2049. +
  2050. +static const struct switch_attr mt7621_port[] = {
  2051. + {
  2052. + .type = SWITCH_TYPE_STRING,
  2053. + .name = "mib",
  2054. + .description = "Get MIB counters for port",
  2055. + .get = mt7621_sw_get_port_mib,
  2056. + .set = NULL,
  2057. + },
  2058. +};
  2059. +
  2060. +static const struct switch_attr mt7530_port[] = {
  2061. +};
  2062. +
  2063. +static const struct switch_attr mt7530_vlan[] = {
  2064. + {
  2065. + .type = SWITCH_TYPE_INT,
  2066. + .name = "vid",
  2067. + .description = "VLAN ID (0-4094)",
  2068. + .set = mt7530_set_vid,
  2069. + .get = mt7530_get_vid,
  2070. + .max = 4094,
  2071. + },
  2072. +};
  2073. +
  2074. +static const struct switch_dev_ops mt7621_ops = {
  2075. + .attr_global = {
  2076. + .attr = mt7530_global,
  2077. + .n_attr = ARRAY_SIZE(mt7530_global),
  2078. + },
  2079. +/* .attr_port = {
  2080. + .attr = mt7621_port,
  2081. + .n_attr = ARRAY_SIZE(mt7621_port),
  2082. + },*/
  2083. + .attr_vlan = {
  2084. + .attr = mt7530_vlan,
  2085. + .n_attr = ARRAY_SIZE(mt7530_vlan),
  2086. + },
  2087. + .get_vlan_ports = mt7530_get_vlan_ports,
  2088. + .set_vlan_ports = mt7530_set_vlan_ports,
  2089. + .get_port_pvid = mt7530_get_port_pvid,
  2090. + .set_port_pvid = mt7530_set_port_pvid,
  2091. + .get_port_link = mt7530_get_port_link,
  2092. + .apply_config = mt7530_apply_config,
  2093. + .reset_switch = mt7530_reset_switch,
  2094. +};
  2095. +
  2096. +static const struct switch_dev_ops mt7530_ops = {
  2097. + .attr_global = {
  2098. + .attr = mt7530_global,
  2099. + .n_attr = ARRAY_SIZE(mt7530_global),
  2100. + },
  2101. + .attr_port = {
  2102. + .attr = mt7530_port,
  2103. + .n_attr = ARRAY_SIZE(mt7530_port),
  2104. + },
  2105. + .attr_vlan = {
  2106. + .attr = mt7530_vlan,
  2107. + .n_attr = ARRAY_SIZE(mt7530_vlan),
  2108. + },
  2109. + .get_vlan_ports = mt7530_get_vlan_ports,
  2110. + .set_vlan_ports = mt7530_set_vlan_ports,
  2111. + .get_port_pvid = mt7530_get_port_pvid,
  2112. + .set_port_pvid = mt7530_set_port_pvid,
  2113. + .get_port_link = mt7530_get_port_link,
  2114. + .apply_config = mt7530_apply_config,
  2115. + .reset_switch = mt7530_reset_switch,
  2116. +};
  2117. +
  2118. +int
  2119. +mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus, int vlan)
  2120. +{
  2121. + struct switch_dev *swdev;
  2122. + struct mt7530_priv *mt7530;
  2123. + struct mt7530_mapping *map;
  2124. + int ret;
  2125. +
  2126. + mt7530 = devm_kzalloc(dev, sizeof(struct mt7530_priv), GFP_KERNEL);
  2127. + if (!mt7530)
  2128. + return -ENOMEM;
  2129. +
  2130. + mt7530->base = base;
  2131. + mt7530->bus = bus;
  2132. + mt7530->global_vlan_enable = vlan;
  2133. +
  2134. + swdev = &mt7530->swdev;
  2135. + if (bus) {
  2136. + swdev->alias = "mt7530";
  2137. + swdev->name = "mt7530";
  2138. + } else if (IS_ENABLED(CONFIG_MACH_MT7623)) {
  2139. + swdev->alias = "mt7623";
  2140. + swdev->name = "mt7623";
  2141. + } else if (IS_ENABLED(CONFIG_SOC_MT7621)) {
  2142. + swdev->alias = "mt7621";
  2143. + swdev->name = "mt7621";
  2144. + } else {
  2145. + swdev->alias = "mt7620";
  2146. + swdev->name = "mt7620";
  2147. + }
  2148. + swdev->cpu_port = MT7530_CPU_PORT;
  2149. + swdev->ports = MT7530_NUM_PORTS;
  2150. + swdev->vlans = MT7530_NUM_VLANS;
  2151. + if (IS_ENABLED(CONFIG_SOC_MT7621) || IS_ENABLED(CONFIG_MACH_MT7623))
  2152. + swdev->ops = &mt7621_ops;
  2153. + else
  2154. + swdev->ops = &mt7530_ops;
  2155. +
  2156. + ret = register_switch(swdev, NULL);
  2157. + if (ret) {
  2158. + dev_err(dev, "failed to register mt7530\n");
  2159. + return ret;
  2160. + }
  2161. +
  2162. + mt7530_reset_switch(swdev);
  2163. +
  2164. + map = mt7530_find_mapping(dev->of_node);
  2165. + if (map)
  2166. + mt7530_apply_mapping(mt7530, map);
  2167. + mt7530_apply_config(swdev);
  2168. +
  2169. + /* magic vodoo */
  2170. + if (!(IS_ENABLED(CONFIG_SOC_MT7621) || IS_ENABLED(CONFIG_MACH_MT7623)) && bus && mt7530_r32(mt7530, REG_HWTRAP) != 0x1117edf) {
  2171. + dev_info(dev, "fixing up MHWTRAP register - bootloader probably played with it\n");
  2172. + mt7530_w32(mt7530, REG_HWTRAP, 0x1117edf);
  2173. + }
  2174. + dev_info(dev, "loaded %s driver\n", swdev->name);
  2175. +
  2176. + return 0;
  2177. +}
  2178. --- /dev/null
  2179. +++ b/drivers/net/ethernet/mediatek/mt7530.h
  2180. @@ -0,0 +1,20 @@
  2181. +/*
  2182. + * This program is free software; you can redistribute it and/or
  2183. + * modify it under the terms of the GNU General Public License
  2184. + * as published by the Free Software Foundation; either version 2
  2185. + * of the License, or (at your option) any later version.
  2186. + *
  2187. + * This program is distributed in the hope that it will be useful,
  2188. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2189. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2190. + * GNU General Public License for more details.
  2191. + *
  2192. + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  2193. + */
  2194. +
  2195. +#ifndef _MT7530_H__
  2196. +#define _MT7530_H__
  2197. +
  2198. +int mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus, int vlan);
  2199. +
  2200. +#endif
  2201. --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
  2202. +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
  2203. @@ -24,6 +24,9 @@
  2204. #include "mtk_eth_soc.h"
  2205. +/* the callback used by the driver core to bringup the switch */
  2206. +int mtk_gsw_init(struct mtk_eth *eth);
  2207. +
  2208. static int mtk_msg_level = -1;
  2209. module_param_named(msg_level, mtk_msg_level, int, 0);
  2210. MODULE_PARM_DESC(msg_level, "Message level (-1=defaults,0=none,...,16=all)");
  2211. @@ -69,7 +72,7 @@ static int mtk_mdio_busy_wait(struct mtk
  2212. return 0;
  2213. if (time_after(jiffies, t_start + PHY_IAC_TIMEOUT))
  2214. break;
  2215. - usleep_range(10, 20);
  2216. +// usleep_range(10, 20);
  2217. }
  2218. dev_err(eth->dev, "mdio: MDIO timeout\n");
  2219. @@ -1421,15 +1424,6 @@ static int __init mtk_hw_init(struct mtk
  2220. reset_control_deassert(eth->rstc);
  2221. usleep_range(10, 20);
  2222. - /* Set GE2 driving and slew rate */
  2223. - regmap_write(eth->pctl, GPIO_DRV_SEL10, 0xa00);
  2224. -
  2225. - /* set GE2 TDSEL */
  2226. - regmap_write(eth->pctl, GPIO_OD33_CTRL8, 0x5);
  2227. -
  2228. - /* set GE2 TUNE */
  2229. - regmap_write(eth->pctl, GPIO_BIAS_CTRL, 0x0);
  2230. -
  2231. /* GE1, Force 1000M/FD, FC ON */
  2232. mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(0));
  2233. @@ -1452,6 +1446,8 @@ static int __init mtk_hw_init(struct mtk
  2234. if (err)
  2235. return err;
  2236. + mtk_gsw_init(eth);
  2237. +
  2238. /* disable delay and normal interrupt */
  2239. mtk_w32(eth, 0, MTK_QDMA_DELAY_INT);
  2240. mtk_irq_disable(eth, ~0);
  2241. @@ -1479,6 +1475,8 @@ static int __init mtk_hw_init(struct mtk
  2242. mtk_w32(eth, val, MTK_GDMA_FWD_CFG(i));
  2243. }
  2244. + mt7623_gsw_config(eth);
  2245. +
  2246. return 0;
  2247. }
  2248. @@ -1734,7 +1732,7 @@ static int mtk_add_mac(struct mtk_eth *e
  2249. {
  2250. struct mtk_mac *mac;
  2251. const __be32 *_id = of_get_property(np, "reg", NULL);
  2252. - int id, err;
  2253. + int id;
  2254. if (!_id) {
  2255. dev_err(eth->dev, "missing mac id\n");
  2256. @@ -1768,8 +1766,8 @@ static int mtk_add_mac(struct mtk_eth *e
  2257. GFP_KERNEL);
  2258. if (!mac->hw_stats) {
  2259. dev_err(eth->dev, "failed to allocate counter memory\n");
  2260. - err = -ENOMEM;
  2261. - goto free_netdev;
  2262. + free_netdev(eth->netdev[id]);
  2263. + return -ENOMEM;
  2264. }
  2265. spin_lock_init(&mac->hw_stats->stats_lock);
  2266. mac->hw_stats->reg_offset = id * MTK_STAT_OFFSET;
  2267. @@ -1783,21 +1781,9 @@ static int mtk_add_mac(struct mtk_eth *e
  2268. eth->netdev[id]->features |= MTK_HW_FEATURES;
  2269. eth->netdev[id]->ethtool_ops = &mtk_ethtool_ops;
  2270. - err = register_netdev(eth->netdev[id]);
  2271. - if (err) {
  2272. - dev_err(eth->dev, "error bringing up device\n");
  2273. - goto free_netdev;
  2274. - }
  2275. eth->netdev[id]->irq = eth->irq[0];
  2276. - netif_info(eth, probe, eth->netdev[id],
  2277. - "mediatek frame engine at 0x%08lx, irq %d\n",
  2278. - eth->netdev[id]->base_addr, eth->irq[0]);
  2279. return 0;
  2280. -
  2281. -free_netdev:
  2282. - free_netdev(eth->netdev[id]);
  2283. - return err;
  2284. }
  2285. static int mtk_probe(struct platform_device *pdev)
  2286. @@ -1865,14 +1851,13 @@ static int mtk_probe(struct platform_dev
  2287. clk_prepare_enable(eth->clk_gp1);
  2288. clk_prepare_enable(eth->clk_gp2);
  2289. + eth->switch_np = of_parse_phandle(pdev->dev.of_node,
  2290. + "mediatek,switch", 0);
  2291. +
  2292. eth->dev = &pdev->dev;
  2293. eth->msg_enable = netif_msg_init(mtk_msg_level, MTK_DEFAULT_MSG_ENABLE);
  2294. INIT_WORK(&eth->pending_work, mtk_pending_work);
  2295. - err = mtk_hw_init(eth);
  2296. - if (err)
  2297. - return err;
  2298. -
  2299. for_each_child_of_node(pdev->dev.of_node, mac_np) {
  2300. if (!of_device_is_compatible(mac_np,
  2301. "mediatek,eth-mac"))
  2302. @@ -1886,6 +1871,22 @@ static int mtk_probe(struct platform_dev
  2303. goto err_free_dev;
  2304. }
  2305. + err = mtk_hw_init(eth);
  2306. + if (err)
  2307. + return err;
  2308. +
  2309. + for (i = 0; i < MTK_MAX_DEVS; i++) {
  2310. + if (!eth->netdev[i])
  2311. + continue;
  2312. + err = register_netdev(eth->netdev[i]);
  2313. + if (err)
  2314. + dev_err(eth->dev, "error bringing up device\n");
  2315. + else
  2316. + netif_info(eth, probe, eth->netdev[i],
  2317. + "mediatek frame engine at 0x%08lx, irq %d\n",
  2318. + eth->netdev[i]->base_addr, eth->irq[0]);
  2319. + }
  2320. +
  2321. /* we run 2 devices on the same DMA ring so we need a dummy device
  2322. * for NAPI to work
  2323. */
  2324. --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
  2325. +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
  2326. @@ -407,6 +407,9 @@ struct mtk_eth {
  2327. struct clk *clk_gp2;
  2328. struct mii_bus *mii_bus;
  2329. struct work_struct pending_work;
  2330. +
  2331. + struct device_node *switch_np;
  2332. + void *sw_priv;
  2333. };
  2334. /* struct mtk_mac - the structure that holds the info about the MACs of the
  2335. @@ -434,4 +437,6 @@ void mtk_stats_update_mac(struct mtk_mac
  2336. void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg);
  2337. u32 mtk_r32(struct mtk_eth *eth, unsigned reg);
  2338. +int mt7623_gsw_config(struct mtk_eth *eth);
  2339. +
  2340. #endif /* MTK_ETH_H */