dwmac-sun8i.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * dwmac-sun8i.c - Allwinner sun8i DWMAC specific glue layer
  4. *
  5. * Copyright (C) 2017 Corentin Labbe <clabbe.montjoie@gmail.com>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/io.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/mdio-mux.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_mdio.h>
  15. #include <linux/of_net.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/phy.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/regmap.h>
  22. #include <linux/stmmac.h>
  23. #include "stmmac.h"
  24. #include "stmmac_platform.h"
  25. /* General notes on dwmac-sun8i:
  26. * Locking: no locking is necessary in this file because all necessary locking
  27. * is done in the "stmmac files"
  28. */
  29. /* struct emac_variant - Describe dwmac-sun8i hardware variant
  30. * @default_syscon_value: The default value of the EMAC register in syscon
  31. * This value is used for disabling properly EMAC
  32. * and used as a good starting value in case of the
  33. * boot process(uboot) leave some stuff.
  34. * @syscon_field reg_field for the syscon's gmac register
  35. * @soc_has_internal_phy: Does the MAC embed an internal PHY
  36. * @support_mii: Does the MAC handle MII
  37. * @support_rmii: Does the MAC handle RMII
  38. * @support_rgmii: Does the MAC handle RGMII
  39. *
  40. * @rx_delay_max: Maximum raw value for RX delay chain
  41. * @tx_delay_max: Maximum raw value for TX delay chain
  42. * These two also indicate the bitmask for
  43. * the RX and TX delay chain registers. A
  44. * value of zero indicates this is not supported.
  45. */
  46. struct emac_variant {
  47. u32 default_syscon_value;
  48. const struct reg_field *syscon_field;
  49. bool soc_has_internal_phy;
  50. bool support_mii;
  51. bool support_rmii;
  52. bool support_rgmii;
  53. u8 rx_delay_max;
  54. u8 tx_delay_max;
  55. };
  56. /* struct sunxi_priv_data - hold all sunxi private data
  57. * @ephy_clk: reference to the optional EPHY clock for the internal PHY
  58. * @regulator: reference to the optional regulator
  59. * @rst_ephy: reference to the optional EPHY reset for the internal PHY
  60. * @variant: reference to the current board variant
  61. * @regmap: regmap for using the syscon
  62. * @internal_phy_powered: Does the internal PHY is enabled
  63. * @use_internal_phy: Is the internal PHY selected for use
  64. * @mux_handle: Internal pointer used by mdio-mux lib
  65. */
  66. struct sunxi_priv_data {
  67. struct clk *ephy_clk;
  68. struct regulator *regulator;
  69. struct reset_control *rst_ephy;
  70. const struct emac_variant *variant;
  71. struct regmap_field *regmap_field;
  72. bool internal_phy_powered;
  73. bool use_internal_phy;
  74. void *mux_handle;
  75. };
  76. /* EMAC clock register @ 0x30 in the "system control" address range */
  77. static const struct reg_field sun8i_syscon_reg_field = {
  78. .reg = 0x30,
  79. .lsb = 0,
  80. .msb = 31,
  81. };
  82. /* EMAC clock register @ 0x164 in the CCU address range */
  83. static const struct reg_field sun8i_ccu_reg_field = {
  84. .reg = 0x164,
  85. .lsb = 0,
  86. .msb = 31,
  87. };
  88. static const struct emac_variant emac_variant_h3 = {
  89. .default_syscon_value = 0x58000,
  90. .syscon_field = &sun8i_syscon_reg_field,
  91. .soc_has_internal_phy = true,
  92. .support_mii = true,
  93. .support_rmii = true,
  94. .support_rgmii = true,
  95. .rx_delay_max = 31,
  96. .tx_delay_max = 7,
  97. };
  98. static const struct emac_variant emac_variant_v3s = {
  99. .default_syscon_value = 0x38000,
  100. .syscon_field = &sun8i_syscon_reg_field,
  101. .soc_has_internal_phy = true,
  102. .support_mii = true
  103. };
  104. static const struct emac_variant emac_variant_a83t = {
  105. .default_syscon_value = 0,
  106. .syscon_field = &sun8i_syscon_reg_field,
  107. .soc_has_internal_phy = false,
  108. .support_mii = true,
  109. .support_rgmii = true,
  110. .rx_delay_max = 31,
  111. .tx_delay_max = 7,
  112. };
  113. static const struct emac_variant emac_variant_r40 = {
  114. .default_syscon_value = 0,
  115. .syscon_field = &sun8i_ccu_reg_field,
  116. .support_mii = true,
  117. .support_rgmii = true,
  118. .rx_delay_max = 7,
  119. };
  120. static const struct emac_variant emac_variant_a64 = {
  121. .default_syscon_value = 0,
  122. .syscon_field = &sun8i_syscon_reg_field,
  123. .soc_has_internal_phy = false,
  124. .support_mii = true,
  125. .support_rmii = true,
  126. .support_rgmii = true,
  127. .rx_delay_max = 31,
  128. .tx_delay_max = 7,
  129. };
  130. static const struct emac_variant emac_variant_h6 = {
  131. .default_syscon_value = 0x50000,
  132. .syscon_field = &sun8i_syscon_reg_field,
  133. /* The "Internal PHY" of H6 is not on the die. It's on the
  134. * co-packaged AC200 chip instead.
  135. */
  136. .soc_has_internal_phy = false,
  137. .support_mii = true,
  138. .support_rmii = true,
  139. .support_rgmii = true,
  140. .rx_delay_max = 31,
  141. .tx_delay_max = 7,
  142. };
  143. #define EMAC_BASIC_CTL0 0x00
  144. #define EMAC_BASIC_CTL1 0x04
  145. #define EMAC_INT_STA 0x08
  146. #define EMAC_INT_EN 0x0C
  147. #define EMAC_TX_CTL0 0x10
  148. #define EMAC_TX_CTL1 0x14
  149. #define EMAC_TX_FLOW_CTL 0x1C
  150. #define EMAC_TX_DESC_LIST 0x20
  151. #define EMAC_RX_CTL0 0x24
  152. #define EMAC_RX_CTL1 0x28
  153. #define EMAC_RX_DESC_LIST 0x34
  154. #define EMAC_RX_FRM_FLT 0x38
  155. #define EMAC_MDIO_CMD 0x48
  156. #define EMAC_MDIO_DATA 0x4C
  157. #define EMAC_MACADDR_HI(reg) (0x50 + (reg) * 8)
  158. #define EMAC_MACADDR_LO(reg) (0x54 + (reg) * 8)
  159. #define EMAC_TX_DMA_STA 0xB0
  160. #define EMAC_TX_CUR_DESC 0xB4
  161. #define EMAC_TX_CUR_BUF 0xB8
  162. #define EMAC_RX_DMA_STA 0xC0
  163. #define EMAC_RX_CUR_DESC 0xC4
  164. #define EMAC_RX_CUR_BUF 0xC8
  165. /* Use in EMAC_BASIC_CTL0 */
  166. #define EMAC_DUPLEX_FULL BIT(0)
  167. #define EMAC_LOOPBACK BIT(1)
  168. #define EMAC_SPEED_1000 0
  169. #define EMAC_SPEED_100 (0x03 << 2)
  170. #define EMAC_SPEED_10 (0x02 << 2)
  171. /* Use in EMAC_BASIC_CTL1 */
  172. #define EMAC_BURSTLEN_SHIFT 24
  173. /* Used in EMAC_RX_FRM_FLT */
  174. #define EMAC_FRM_FLT_RXALL BIT(0)
  175. #define EMAC_FRM_FLT_CTL BIT(13)
  176. #define EMAC_FRM_FLT_MULTICAST BIT(16)
  177. /* Used in RX_CTL1*/
  178. #define EMAC_RX_MD BIT(1)
  179. #define EMAC_RX_TH_MASK GENMASK(5, 4)
  180. #define EMAC_RX_TH_32 0
  181. #define EMAC_RX_TH_64 (0x1 << 4)
  182. #define EMAC_RX_TH_96 (0x2 << 4)
  183. #define EMAC_RX_TH_128 (0x3 << 4)
  184. #define EMAC_RX_DMA_EN BIT(30)
  185. #define EMAC_RX_DMA_START BIT(31)
  186. /* Used in TX_CTL1*/
  187. #define EMAC_TX_MD BIT(1)
  188. #define EMAC_TX_NEXT_FRM BIT(2)
  189. #define EMAC_TX_TH_MASK GENMASK(10, 8)
  190. #define EMAC_TX_TH_64 0
  191. #define EMAC_TX_TH_128 (0x1 << 8)
  192. #define EMAC_TX_TH_192 (0x2 << 8)
  193. #define EMAC_TX_TH_256 (0x3 << 8)
  194. #define EMAC_TX_DMA_EN BIT(30)
  195. #define EMAC_TX_DMA_START BIT(31)
  196. /* Used in RX_CTL0 */
  197. #define EMAC_RX_RECEIVER_EN BIT(31)
  198. #define EMAC_RX_DO_CRC BIT(27)
  199. #define EMAC_RX_FLOW_CTL_EN BIT(16)
  200. /* Used in TX_CTL0 */
  201. #define EMAC_TX_TRANSMITTER_EN BIT(31)
  202. /* Used in EMAC_TX_FLOW_CTL */
  203. #define EMAC_TX_FLOW_CTL_EN BIT(0)
  204. /* Used in EMAC_INT_STA */
  205. #define EMAC_TX_INT BIT(0)
  206. #define EMAC_TX_DMA_STOP_INT BIT(1)
  207. #define EMAC_TX_BUF_UA_INT BIT(2)
  208. #define EMAC_TX_TIMEOUT_INT BIT(3)
  209. #define EMAC_TX_UNDERFLOW_INT BIT(4)
  210. #define EMAC_TX_EARLY_INT BIT(5)
  211. #define EMAC_RX_INT BIT(8)
  212. #define EMAC_RX_BUF_UA_INT BIT(9)
  213. #define EMAC_RX_DMA_STOP_INT BIT(10)
  214. #define EMAC_RX_TIMEOUT_INT BIT(11)
  215. #define EMAC_RX_OVERFLOW_INT BIT(12)
  216. #define EMAC_RX_EARLY_INT BIT(13)
  217. #define EMAC_RGMII_STA_INT BIT(16)
  218. #define EMAC_INT_MSK_COMMON EMAC_RGMII_STA_INT
  219. #define EMAC_INT_MSK_TX (EMAC_TX_INT | \
  220. EMAC_TX_DMA_STOP_INT | \
  221. EMAC_TX_BUF_UA_INT | \
  222. EMAC_TX_TIMEOUT_INT | \
  223. EMAC_TX_UNDERFLOW_INT | \
  224. EMAC_TX_EARLY_INT |\
  225. EMAC_INT_MSK_COMMON)
  226. #define EMAC_INT_MSK_RX (EMAC_RX_INT | \
  227. EMAC_RX_BUF_UA_INT | \
  228. EMAC_RX_DMA_STOP_INT | \
  229. EMAC_RX_TIMEOUT_INT | \
  230. EMAC_RX_OVERFLOW_INT | \
  231. EMAC_RX_EARLY_INT | \
  232. EMAC_INT_MSK_COMMON)
  233. #define MAC_ADDR_TYPE_DST BIT(31)
  234. /* H3 specific bits for EPHY */
  235. #define H3_EPHY_ADDR_SHIFT 20
  236. #define H3_EPHY_CLK_SEL BIT(18) /* 1: 24MHz, 0: 25MHz */
  237. #define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
  238. #define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
  239. #define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
  240. #define H3_EPHY_MUX_MASK (H3_EPHY_SHUTDOWN | H3_EPHY_SELECT)
  241. #define DWMAC_SUN8I_MDIO_MUX_INTERNAL_ID 1
  242. #define DWMAC_SUN8I_MDIO_MUX_EXTERNAL_ID 2
  243. /* H3/A64 specific bits */
  244. #define SYSCON_RMII_EN BIT(13) /* 1: enable RMII (overrides EPIT) */
  245. /* Generic system control EMAC_CLK bits */
  246. #define SYSCON_ETXDC_SHIFT 10
  247. #define SYSCON_ERXDC_SHIFT 5
  248. /* EMAC PHY Interface Type */
  249. #define SYSCON_EPIT BIT(2) /* 1: RGMII, 0: MII */
  250. #define SYSCON_ETCS_MASK GENMASK(1, 0)
  251. #define SYSCON_ETCS_MII 0x0
  252. #define SYSCON_ETCS_EXT_GMII 0x1
  253. #define SYSCON_ETCS_INT_GMII 0x2
  254. /* sun8i_dwmac_dma_reset() - reset the EMAC
  255. * Called from stmmac via stmmac_dma_ops->reset
  256. */
  257. static int sun8i_dwmac_dma_reset(void __iomem *ioaddr)
  258. {
  259. writel(0, ioaddr + EMAC_RX_CTL1);
  260. writel(0, ioaddr + EMAC_TX_CTL1);
  261. writel(0, ioaddr + EMAC_RX_FRM_FLT);
  262. writel(0, ioaddr + EMAC_RX_DESC_LIST);
  263. writel(0, ioaddr + EMAC_TX_DESC_LIST);
  264. writel(0, ioaddr + EMAC_INT_EN);
  265. writel(0x1FFFFFF, ioaddr + EMAC_INT_STA);
  266. return 0;
  267. }
  268. /* sun8i_dwmac_dma_init() - initialize the EMAC
  269. * Called from stmmac via stmmac_dma_ops->init
  270. */
  271. static void sun8i_dwmac_dma_init(void __iomem *ioaddr,
  272. struct stmmac_dma_cfg *dma_cfg)
  273. {
  274. writel(EMAC_RX_INT | EMAC_TX_INT, ioaddr + EMAC_INT_EN);
  275. writel(0x1FFFFFF, ioaddr + EMAC_INT_STA);
  276. }
  277. static void sun8i_dwmac_dma_init_rx(struct stmmac_priv *priv,
  278. void __iomem *ioaddr,
  279. struct stmmac_dma_cfg *dma_cfg,
  280. dma_addr_t dma_rx_phy, u32 chan)
  281. {
  282. /* Write RX descriptors address */
  283. writel(lower_32_bits(dma_rx_phy), ioaddr + EMAC_RX_DESC_LIST);
  284. }
  285. static void sun8i_dwmac_dma_init_tx(struct stmmac_priv *priv,
  286. void __iomem *ioaddr,
  287. struct stmmac_dma_cfg *dma_cfg,
  288. dma_addr_t dma_tx_phy, u32 chan)
  289. {
  290. /* Write TX descriptors address */
  291. writel(lower_32_bits(dma_tx_phy), ioaddr + EMAC_TX_DESC_LIST);
  292. }
  293. /* sun8i_dwmac_dump_regs() - Dump EMAC address space
  294. * Called from stmmac_dma_ops->dump_regs
  295. * Used for ethtool
  296. */
  297. static void sun8i_dwmac_dump_regs(struct stmmac_priv *priv,
  298. void __iomem *ioaddr, u32 *reg_space)
  299. {
  300. int i;
  301. for (i = 0; i < 0xC8; i += 4) {
  302. if (i == 0x32 || i == 0x3C)
  303. continue;
  304. reg_space[i / 4] = readl(ioaddr + i);
  305. }
  306. }
  307. /* sun8i_dwmac_dump_mac_regs() - Dump EMAC address space
  308. * Called from stmmac_ops->dump_regs
  309. * Used for ethtool
  310. */
  311. static void sun8i_dwmac_dump_mac_regs(struct mac_device_info *hw,
  312. u32 *reg_space)
  313. {
  314. int i;
  315. void __iomem *ioaddr = hw->pcsr;
  316. for (i = 0; i < 0xC8; i += 4) {
  317. if (i == 0x32 || i == 0x3C)
  318. continue;
  319. reg_space[i / 4] = readl(ioaddr + i);
  320. }
  321. }
  322. static void sun8i_dwmac_enable_dma_irq(struct stmmac_priv *priv,
  323. void __iomem *ioaddr, u32 chan,
  324. bool rx, bool tx)
  325. {
  326. u32 value = readl(ioaddr + EMAC_INT_EN);
  327. if (rx)
  328. value |= EMAC_RX_INT;
  329. if (tx)
  330. value |= EMAC_TX_INT;
  331. writel(value, ioaddr + EMAC_INT_EN);
  332. }
  333. static void sun8i_dwmac_disable_dma_irq(struct stmmac_priv *priv,
  334. void __iomem *ioaddr, u32 chan,
  335. bool rx, bool tx)
  336. {
  337. u32 value = readl(ioaddr + EMAC_INT_EN);
  338. if (rx)
  339. value &= ~EMAC_RX_INT;
  340. if (tx)
  341. value &= ~EMAC_TX_INT;
  342. writel(value, ioaddr + EMAC_INT_EN);
  343. }
  344. static void sun8i_dwmac_dma_start_tx(struct stmmac_priv *priv,
  345. void __iomem *ioaddr, u32 chan)
  346. {
  347. u32 v;
  348. v = readl(ioaddr + EMAC_TX_CTL1);
  349. v |= EMAC_TX_DMA_START;
  350. v |= EMAC_TX_DMA_EN;
  351. writel(v, ioaddr + EMAC_TX_CTL1);
  352. }
  353. static void sun8i_dwmac_enable_dma_transmission(void __iomem *ioaddr, u32 chan)
  354. {
  355. u32 v;
  356. v = readl(ioaddr + EMAC_TX_CTL1);
  357. v |= EMAC_TX_DMA_START;
  358. v |= EMAC_TX_DMA_EN;
  359. writel(v, ioaddr + EMAC_TX_CTL1);
  360. }
  361. static void sun8i_dwmac_dma_stop_tx(struct stmmac_priv *priv,
  362. void __iomem *ioaddr, u32 chan)
  363. {
  364. u32 v;
  365. v = readl(ioaddr + EMAC_TX_CTL1);
  366. v &= ~EMAC_TX_DMA_EN;
  367. writel(v, ioaddr + EMAC_TX_CTL1);
  368. }
  369. static void sun8i_dwmac_dma_start_rx(struct stmmac_priv *priv,
  370. void __iomem *ioaddr, u32 chan)
  371. {
  372. u32 v;
  373. v = readl(ioaddr + EMAC_RX_CTL1);
  374. v |= EMAC_RX_DMA_START;
  375. v |= EMAC_RX_DMA_EN;
  376. writel(v, ioaddr + EMAC_RX_CTL1);
  377. }
  378. static void sun8i_dwmac_dma_stop_rx(struct stmmac_priv *priv,
  379. void __iomem *ioaddr, u32 chan)
  380. {
  381. u32 v;
  382. v = readl(ioaddr + EMAC_RX_CTL1);
  383. v &= ~EMAC_RX_DMA_EN;
  384. writel(v, ioaddr + EMAC_RX_CTL1);
  385. }
  386. static int sun8i_dwmac_dma_interrupt(struct stmmac_priv *priv,
  387. void __iomem *ioaddr,
  388. struct stmmac_extra_stats *x, u32 chan,
  389. u32 dir)
  390. {
  391. struct stmmac_pcpu_stats *stats = this_cpu_ptr(priv->xstats.pcpu_stats);
  392. int ret = 0;
  393. u32 v;
  394. v = readl(ioaddr + EMAC_INT_STA);
  395. if (dir == DMA_DIR_RX)
  396. v &= EMAC_INT_MSK_RX;
  397. else if (dir == DMA_DIR_TX)
  398. v &= EMAC_INT_MSK_TX;
  399. if (v & EMAC_TX_INT) {
  400. ret |= handle_tx;
  401. u64_stats_update_begin(&stats->syncp);
  402. u64_stats_inc(&stats->tx_normal_irq_n[chan]);
  403. u64_stats_update_end(&stats->syncp);
  404. }
  405. if (v & EMAC_TX_DMA_STOP_INT)
  406. x->tx_process_stopped_irq++;
  407. if (v & EMAC_TX_BUF_UA_INT)
  408. x->tx_process_stopped_irq++;
  409. if (v & EMAC_TX_TIMEOUT_INT)
  410. ret |= tx_hard_error;
  411. if (v & EMAC_TX_UNDERFLOW_INT) {
  412. ret |= tx_hard_error;
  413. x->tx_undeflow_irq++;
  414. }
  415. if (v & EMAC_TX_EARLY_INT)
  416. x->tx_early_irq++;
  417. if (v & EMAC_RX_INT) {
  418. ret |= handle_rx;
  419. u64_stats_update_begin(&stats->syncp);
  420. u64_stats_inc(&stats->rx_normal_irq_n[chan]);
  421. u64_stats_update_end(&stats->syncp);
  422. }
  423. if (v & EMAC_RX_BUF_UA_INT)
  424. x->rx_buf_unav_irq++;
  425. if (v & EMAC_RX_DMA_STOP_INT)
  426. x->rx_process_stopped_irq++;
  427. if (v & EMAC_RX_TIMEOUT_INT)
  428. ret |= tx_hard_error;
  429. if (v & EMAC_RX_OVERFLOW_INT) {
  430. ret |= tx_hard_error;
  431. x->rx_overflow_irq++;
  432. }
  433. if (v & EMAC_RX_EARLY_INT)
  434. x->rx_early_irq++;
  435. if (v & EMAC_RGMII_STA_INT)
  436. x->irq_rgmii_n++;
  437. writel(v, ioaddr + EMAC_INT_STA);
  438. return ret;
  439. }
  440. static void sun8i_dwmac_dma_operation_mode_rx(struct stmmac_priv *priv,
  441. void __iomem *ioaddr, int mode,
  442. u32 channel, int fifosz, u8 qmode)
  443. {
  444. u32 v;
  445. v = readl(ioaddr + EMAC_RX_CTL1);
  446. if (mode == SF_DMA_MODE) {
  447. v |= EMAC_RX_MD;
  448. } else {
  449. v &= ~EMAC_RX_MD;
  450. v &= ~EMAC_RX_TH_MASK;
  451. if (mode < 32)
  452. v |= EMAC_RX_TH_32;
  453. else if (mode < 64)
  454. v |= EMAC_RX_TH_64;
  455. else if (mode < 96)
  456. v |= EMAC_RX_TH_96;
  457. else if (mode < 128)
  458. v |= EMAC_RX_TH_128;
  459. }
  460. writel(v, ioaddr + EMAC_RX_CTL1);
  461. }
  462. static void sun8i_dwmac_dma_operation_mode_tx(struct stmmac_priv *priv,
  463. void __iomem *ioaddr, int mode,
  464. u32 channel, int fifosz, u8 qmode)
  465. {
  466. u32 v;
  467. v = readl(ioaddr + EMAC_TX_CTL1);
  468. if (mode == SF_DMA_MODE) {
  469. v |= EMAC_TX_MD;
  470. /* Undocumented bit (called TX_NEXT_FRM in BSP), the original
  471. * comment is
  472. * "Operating on second frame increase the performance
  473. * especially when transmit store-and-forward is used."
  474. */
  475. v |= EMAC_TX_NEXT_FRM;
  476. } else {
  477. v &= ~EMAC_TX_MD;
  478. v &= ~EMAC_TX_TH_MASK;
  479. if (mode < 64)
  480. v |= EMAC_TX_TH_64;
  481. else if (mode < 128)
  482. v |= EMAC_TX_TH_128;
  483. else if (mode < 192)
  484. v |= EMAC_TX_TH_192;
  485. else if (mode < 256)
  486. v |= EMAC_TX_TH_256;
  487. }
  488. writel(v, ioaddr + EMAC_TX_CTL1);
  489. }
  490. static const struct stmmac_dma_ops sun8i_dwmac_dma_ops = {
  491. .reset = sun8i_dwmac_dma_reset,
  492. .init = sun8i_dwmac_dma_init,
  493. .init_rx_chan = sun8i_dwmac_dma_init_rx,
  494. .init_tx_chan = sun8i_dwmac_dma_init_tx,
  495. .dump_regs = sun8i_dwmac_dump_regs,
  496. .dma_rx_mode = sun8i_dwmac_dma_operation_mode_rx,
  497. .dma_tx_mode = sun8i_dwmac_dma_operation_mode_tx,
  498. .enable_dma_transmission = sun8i_dwmac_enable_dma_transmission,
  499. .enable_dma_irq = sun8i_dwmac_enable_dma_irq,
  500. .disable_dma_irq = sun8i_dwmac_disable_dma_irq,
  501. .start_tx = sun8i_dwmac_dma_start_tx,
  502. .stop_tx = sun8i_dwmac_dma_stop_tx,
  503. .start_rx = sun8i_dwmac_dma_start_rx,
  504. .stop_rx = sun8i_dwmac_dma_stop_rx,
  505. .dma_interrupt = sun8i_dwmac_dma_interrupt,
  506. };
  507. static int sun8i_dwmac_power_internal_phy(struct stmmac_priv *priv);
  508. static int sun8i_dwmac_init(struct platform_device *pdev, void *priv)
  509. {
  510. struct net_device *ndev = platform_get_drvdata(pdev);
  511. struct sunxi_priv_data *gmac = priv;
  512. int ret;
  513. if (gmac->regulator) {
  514. ret = regulator_enable(gmac->regulator);
  515. if (ret) {
  516. dev_err(&pdev->dev, "Fail to enable regulator\n");
  517. return ret;
  518. }
  519. }
  520. if (gmac->use_internal_phy) {
  521. ret = sun8i_dwmac_power_internal_phy(netdev_priv(ndev));
  522. if (ret)
  523. goto err_disable_regulator;
  524. }
  525. return 0;
  526. err_disable_regulator:
  527. if (gmac->regulator)
  528. regulator_disable(gmac->regulator);
  529. return ret;
  530. }
  531. static void sun8i_dwmac_core_init(struct mac_device_info *hw,
  532. struct net_device *dev)
  533. {
  534. void __iomem *ioaddr = hw->pcsr;
  535. u32 v;
  536. v = (8 << EMAC_BURSTLEN_SHIFT); /* burst len */
  537. writel(v, ioaddr + EMAC_BASIC_CTL1);
  538. }
  539. static void sun8i_dwmac_set_mac(void __iomem *ioaddr, bool enable)
  540. {
  541. u32 t, r;
  542. t = readl(ioaddr + EMAC_TX_CTL0);
  543. r = readl(ioaddr + EMAC_RX_CTL0);
  544. if (enable) {
  545. t |= EMAC_TX_TRANSMITTER_EN;
  546. r |= EMAC_RX_RECEIVER_EN;
  547. } else {
  548. t &= ~EMAC_TX_TRANSMITTER_EN;
  549. r &= ~EMAC_RX_RECEIVER_EN;
  550. }
  551. writel(t, ioaddr + EMAC_TX_CTL0);
  552. writel(r, ioaddr + EMAC_RX_CTL0);
  553. }
  554. /* Set MAC address at slot reg_n
  555. * All slot > 0 need to be enabled with MAC_ADDR_TYPE_DST
  556. * If addr is NULL, clear the slot
  557. */
  558. static void sun8i_dwmac_set_umac_addr(struct mac_device_info *hw,
  559. const unsigned char *addr,
  560. unsigned int reg_n)
  561. {
  562. void __iomem *ioaddr = hw->pcsr;
  563. u32 v;
  564. if (!addr) {
  565. writel(0, ioaddr + EMAC_MACADDR_HI(reg_n));
  566. return;
  567. }
  568. stmmac_set_mac_addr(ioaddr, addr, EMAC_MACADDR_HI(reg_n),
  569. EMAC_MACADDR_LO(reg_n));
  570. if (reg_n > 0) {
  571. v = readl(ioaddr + EMAC_MACADDR_HI(reg_n));
  572. v |= MAC_ADDR_TYPE_DST;
  573. writel(v, ioaddr + EMAC_MACADDR_HI(reg_n));
  574. }
  575. }
  576. static void sun8i_dwmac_get_umac_addr(struct mac_device_info *hw,
  577. unsigned char *addr,
  578. unsigned int reg_n)
  579. {
  580. void __iomem *ioaddr = hw->pcsr;
  581. stmmac_get_mac_addr(ioaddr, addr, EMAC_MACADDR_HI(reg_n),
  582. EMAC_MACADDR_LO(reg_n));
  583. }
  584. /* caution this function must return non 0 to work */
  585. static int sun8i_dwmac_rx_ipc_enable(struct mac_device_info *hw)
  586. {
  587. void __iomem *ioaddr = hw->pcsr;
  588. u32 v;
  589. v = readl(ioaddr + EMAC_RX_CTL0);
  590. v |= EMAC_RX_DO_CRC;
  591. writel(v, ioaddr + EMAC_RX_CTL0);
  592. return 1;
  593. }
  594. static void sun8i_dwmac_set_filter(struct mac_device_info *hw,
  595. struct net_device *dev)
  596. {
  597. void __iomem *ioaddr = hw->pcsr;
  598. u32 v;
  599. int i = 1;
  600. struct netdev_hw_addr *ha;
  601. int macaddrs = netdev_uc_count(dev) + netdev_mc_count(dev) + 1;
  602. v = EMAC_FRM_FLT_CTL;
  603. if (dev->flags & IFF_PROMISC) {
  604. v = EMAC_FRM_FLT_RXALL;
  605. } else if (dev->flags & IFF_ALLMULTI) {
  606. v |= EMAC_FRM_FLT_MULTICAST;
  607. } else if (macaddrs <= hw->unicast_filter_entries) {
  608. if (!netdev_mc_empty(dev)) {
  609. netdev_for_each_mc_addr(ha, dev) {
  610. sun8i_dwmac_set_umac_addr(hw, ha->addr, i);
  611. i++;
  612. }
  613. }
  614. if (!netdev_uc_empty(dev)) {
  615. netdev_for_each_uc_addr(ha, dev) {
  616. sun8i_dwmac_set_umac_addr(hw, ha->addr, i);
  617. i++;
  618. }
  619. }
  620. } else {
  621. if (!(readl(ioaddr + EMAC_RX_FRM_FLT) & EMAC_FRM_FLT_RXALL))
  622. netdev_info(dev, "Too many address, switching to promiscuous\n");
  623. v = EMAC_FRM_FLT_RXALL;
  624. }
  625. /* Disable unused address filter slots */
  626. while (i < hw->unicast_filter_entries)
  627. sun8i_dwmac_set_umac_addr(hw, NULL, i++);
  628. writel(v, ioaddr + EMAC_RX_FRM_FLT);
  629. }
  630. static void sun8i_dwmac_flow_ctrl(struct mac_device_info *hw,
  631. unsigned int duplex, unsigned int fc,
  632. unsigned int pause_time, u32 tx_cnt)
  633. {
  634. void __iomem *ioaddr = hw->pcsr;
  635. u32 v;
  636. v = readl(ioaddr + EMAC_RX_CTL0);
  637. if (fc == FLOW_AUTO)
  638. v |= EMAC_RX_FLOW_CTL_EN;
  639. else
  640. v &= ~EMAC_RX_FLOW_CTL_EN;
  641. writel(v, ioaddr + EMAC_RX_CTL0);
  642. v = readl(ioaddr + EMAC_TX_FLOW_CTL);
  643. if (fc == FLOW_AUTO)
  644. v |= EMAC_TX_FLOW_CTL_EN;
  645. else
  646. v &= ~EMAC_TX_FLOW_CTL_EN;
  647. writel(v, ioaddr + EMAC_TX_FLOW_CTL);
  648. }
  649. static int sun8i_dwmac_reset(struct stmmac_priv *priv)
  650. {
  651. u32 v;
  652. int err;
  653. v = readl(priv->ioaddr + EMAC_BASIC_CTL1);
  654. writel(v | 0x01, priv->ioaddr + EMAC_BASIC_CTL1);
  655. /* The timeout was previoulsy set to 10ms, but some board (OrangePI0)
  656. * need more if no cable plugged. 100ms seems OK
  657. */
  658. err = readl_poll_timeout(priv->ioaddr + EMAC_BASIC_CTL1, v,
  659. !(v & 0x01), 100, 100000);
  660. if (err) {
  661. dev_err(priv->device, "EMAC reset timeout\n");
  662. return err;
  663. }
  664. return 0;
  665. }
  666. /* Search in mdio-mux node for internal PHY node and get its clk/reset */
  667. static int get_ephy_nodes(struct stmmac_priv *priv)
  668. {
  669. struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
  670. struct device_node *mdio_internal;
  671. struct device_node *mdio_mux;
  672. int ret;
  673. mdio_mux = of_get_child_by_name(priv->device->of_node, "mdio-mux");
  674. if (!mdio_mux) {
  675. dev_err(priv->device, "Cannot get mdio-mux node\n");
  676. return -ENODEV;
  677. }
  678. mdio_internal = of_get_compatible_child(mdio_mux,
  679. "allwinner,sun8i-h3-mdio-internal");
  680. of_node_put(mdio_mux);
  681. if (!mdio_internal) {
  682. dev_err(priv->device, "Cannot get internal_mdio node\n");
  683. return -ENODEV;
  684. }
  685. /* Seek for internal PHY */
  686. for_each_child_of_node_scoped(mdio_internal, iphynode) {
  687. gmac->ephy_clk = of_clk_get(iphynode, 0);
  688. if (IS_ERR(gmac->ephy_clk))
  689. continue;
  690. gmac->rst_ephy = of_reset_control_get_exclusive(iphynode, NULL);
  691. if (IS_ERR(gmac->rst_ephy)) {
  692. ret = PTR_ERR(gmac->rst_ephy);
  693. if (ret == -EPROBE_DEFER) {
  694. of_node_put(mdio_internal);
  695. return ret;
  696. }
  697. continue;
  698. }
  699. dev_info(priv->device, "Found internal PHY node\n");
  700. of_node_put(mdio_internal);
  701. return 0;
  702. }
  703. of_node_put(mdio_internal);
  704. return -ENODEV;
  705. }
  706. static int sun8i_dwmac_power_internal_phy(struct stmmac_priv *priv)
  707. {
  708. struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
  709. int ret;
  710. if (gmac->internal_phy_powered) {
  711. dev_warn(priv->device, "Internal PHY already powered\n");
  712. return 0;
  713. }
  714. dev_info(priv->device, "Powering internal PHY\n");
  715. ret = clk_prepare_enable(gmac->ephy_clk);
  716. if (ret) {
  717. dev_err(priv->device, "Cannot enable internal PHY\n");
  718. return ret;
  719. }
  720. /* Make sure the EPHY is properly reseted, as U-Boot may leave
  721. * it at deasserted state, and thus it may fail to reset EMAC.
  722. *
  723. * This assumes the driver has exclusive access to the EPHY reset.
  724. */
  725. ret = reset_control_reset(gmac->rst_ephy);
  726. if (ret) {
  727. dev_err(priv->device, "Cannot reset internal PHY\n");
  728. clk_disable_unprepare(gmac->ephy_clk);
  729. return ret;
  730. }
  731. gmac->internal_phy_powered = true;
  732. return 0;
  733. }
  734. static void sun8i_dwmac_unpower_internal_phy(struct sunxi_priv_data *gmac)
  735. {
  736. if (!gmac->internal_phy_powered)
  737. return;
  738. clk_disable_unprepare(gmac->ephy_clk);
  739. reset_control_assert(gmac->rst_ephy);
  740. gmac->internal_phy_powered = false;
  741. }
  742. /* MDIO multiplexing switch function
  743. * This function is called by the mdio-mux layer when it thinks the mdio bus
  744. * multiplexer needs to switch.
  745. * 'current_child' is the current value of the mux register
  746. * 'desired_child' is the value of the 'reg' property of the target child MDIO
  747. * node.
  748. * The first time this function is called, current_child == -1.
  749. * If current_child == desired_child, then the mux is already set to the
  750. * correct bus.
  751. */
  752. static int mdio_mux_syscon_switch_fn(int current_child, int desired_child,
  753. void *data)
  754. {
  755. struct stmmac_priv *priv = data;
  756. struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
  757. u32 reg, val;
  758. int ret = 0;
  759. if (current_child ^ desired_child) {
  760. regmap_field_read(gmac->regmap_field, &reg);
  761. switch (desired_child) {
  762. case DWMAC_SUN8I_MDIO_MUX_INTERNAL_ID:
  763. dev_info(priv->device, "Switch mux to internal PHY");
  764. val = (reg & ~H3_EPHY_MUX_MASK) | H3_EPHY_SELECT;
  765. gmac->use_internal_phy = true;
  766. break;
  767. case DWMAC_SUN8I_MDIO_MUX_EXTERNAL_ID:
  768. dev_info(priv->device, "Switch mux to external PHY");
  769. val = (reg & ~H3_EPHY_MUX_MASK) | H3_EPHY_SHUTDOWN;
  770. gmac->use_internal_phy = false;
  771. break;
  772. default:
  773. dev_err(priv->device, "Invalid child ID %x\n",
  774. desired_child);
  775. return -EINVAL;
  776. }
  777. regmap_field_write(gmac->regmap_field, val);
  778. if (gmac->use_internal_phy) {
  779. ret = sun8i_dwmac_power_internal_phy(priv);
  780. if (ret)
  781. return ret;
  782. } else {
  783. sun8i_dwmac_unpower_internal_phy(gmac);
  784. }
  785. /* After changing syscon value, the MAC need reset or it will
  786. * use the last value (and so the last PHY set).
  787. */
  788. ret = sun8i_dwmac_reset(priv);
  789. }
  790. return ret;
  791. }
  792. static int sun8i_dwmac_register_mdio_mux(struct stmmac_priv *priv)
  793. {
  794. int ret;
  795. struct device_node *mdio_mux;
  796. struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
  797. mdio_mux = of_get_child_by_name(priv->device->of_node, "mdio-mux");
  798. if (!mdio_mux)
  799. return -ENODEV;
  800. ret = mdio_mux_init(priv->device, mdio_mux, mdio_mux_syscon_switch_fn,
  801. &gmac->mux_handle, priv, priv->mii);
  802. of_node_put(mdio_mux);
  803. return ret;
  804. }
  805. static int sun8i_dwmac_set_syscon(struct device *dev,
  806. struct plat_stmmacenet_data *plat)
  807. {
  808. struct sunxi_priv_data *gmac = plat->bsp_priv;
  809. struct device_node *node = dev->of_node;
  810. int ret;
  811. u32 reg, val;
  812. ret = regmap_field_read(gmac->regmap_field, &val);
  813. if (ret) {
  814. dev_err(dev, "Fail to read from regmap field.\n");
  815. return ret;
  816. }
  817. reg = gmac->variant->default_syscon_value;
  818. if (reg != val)
  819. dev_warn(dev,
  820. "Current syscon value is not the default %x (expect %x)\n",
  821. val, reg);
  822. if (gmac->variant->soc_has_internal_phy) {
  823. if (of_property_read_bool(node, "allwinner,leds-active-low"))
  824. reg |= H3_EPHY_LED_POL;
  825. else
  826. reg &= ~H3_EPHY_LED_POL;
  827. /* Force EPHY xtal frequency to 24MHz. */
  828. reg |= H3_EPHY_CLK_SEL;
  829. ret = of_mdio_parse_addr(dev, plat->phy_node);
  830. if (ret < 0) {
  831. dev_err(dev, "Could not parse MDIO addr\n");
  832. return ret;
  833. }
  834. /* of_mdio_parse_addr returns a valid (0 ~ 31) PHY
  835. * address. No need to mask it again.
  836. */
  837. reg |= ret << H3_EPHY_ADDR_SHIFT;
  838. } else {
  839. /* For SoCs without internal PHY the PHY selection bit should be
  840. * set to 0 (external PHY).
  841. */
  842. reg &= ~H3_EPHY_SELECT;
  843. }
  844. if (!of_property_read_u32(node, "allwinner,tx-delay-ps", &val)) {
  845. if (val % 100) {
  846. dev_err(dev, "tx-delay must be a multiple of 100\n");
  847. return -EINVAL;
  848. }
  849. val /= 100;
  850. dev_dbg(dev, "set tx-delay to %x\n", val);
  851. if (val <= gmac->variant->tx_delay_max) {
  852. reg &= ~(gmac->variant->tx_delay_max <<
  853. SYSCON_ETXDC_SHIFT);
  854. reg |= (val << SYSCON_ETXDC_SHIFT);
  855. } else {
  856. dev_err(dev, "Invalid TX clock delay: %d\n",
  857. val);
  858. return -EINVAL;
  859. }
  860. }
  861. if (!of_property_read_u32(node, "allwinner,rx-delay-ps", &val)) {
  862. if (val % 100) {
  863. dev_err(dev, "rx-delay must be a multiple of 100\n");
  864. return -EINVAL;
  865. }
  866. val /= 100;
  867. dev_dbg(dev, "set rx-delay to %x\n", val);
  868. if (val <= gmac->variant->rx_delay_max) {
  869. reg &= ~(gmac->variant->rx_delay_max <<
  870. SYSCON_ERXDC_SHIFT);
  871. reg |= (val << SYSCON_ERXDC_SHIFT);
  872. } else {
  873. dev_err(dev, "Invalid RX clock delay: %d\n",
  874. val);
  875. return -EINVAL;
  876. }
  877. }
  878. /* Clear interface mode bits */
  879. reg &= ~(SYSCON_ETCS_MASK | SYSCON_EPIT);
  880. if (gmac->variant->support_rmii)
  881. reg &= ~SYSCON_RMII_EN;
  882. switch (plat->mac_interface) {
  883. case PHY_INTERFACE_MODE_MII:
  884. /* default */
  885. break;
  886. case PHY_INTERFACE_MODE_RGMII:
  887. case PHY_INTERFACE_MODE_RGMII_ID:
  888. case PHY_INTERFACE_MODE_RGMII_RXID:
  889. case PHY_INTERFACE_MODE_RGMII_TXID:
  890. reg |= SYSCON_EPIT | SYSCON_ETCS_INT_GMII;
  891. break;
  892. case PHY_INTERFACE_MODE_RMII:
  893. reg |= SYSCON_RMII_EN | SYSCON_ETCS_EXT_GMII;
  894. break;
  895. default:
  896. dev_err(dev, "Unsupported interface mode: %s",
  897. phy_modes(plat->mac_interface));
  898. return -EINVAL;
  899. }
  900. regmap_field_write(gmac->regmap_field, reg);
  901. return 0;
  902. }
  903. static void sun8i_dwmac_unset_syscon(struct sunxi_priv_data *gmac)
  904. {
  905. u32 reg = gmac->variant->default_syscon_value;
  906. regmap_field_write(gmac->regmap_field, reg);
  907. }
  908. static void sun8i_dwmac_exit(struct platform_device *pdev, void *priv)
  909. {
  910. struct sunxi_priv_data *gmac = priv;
  911. if (gmac->variant->soc_has_internal_phy)
  912. sun8i_dwmac_unpower_internal_phy(gmac);
  913. if (gmac->regulator)
  914. regulator_disable(gmac->regulator);
  915. }
  916. static void sun8i_dwmac_set_mac_loopback(void __iomem *ioaddr, bool enable)
  917. {
  918. u32 value = readl(ioaddr + EMAC_BASIC_CTL0);
  919. if (enable)
  920. value |= EMAC_LOOPBACK;
  921. else
  922. value &= ~EMAC_LOOPBACK;
  923. writel(value, ioaddr + EMAC_BASIC_CTL0);
  924. }
  925. static const struct stmmac_ops sun8i_dwmac_ops = {
  926. .core_init = sun8i_dwmac_core_init,
  927. .set_mac = sun8i_dwmac_set_mac,
  928. .dump_regs = sun8i_dwmac_dump_mac_regs,
  929. .rx_ipc = sun8i_dwmac_rx_ipc_enable,
  930. .set_filter = sun8i_dwmac_set_filter,
  931. .flow_ctrl = sun8i_dwmac_flow_ctrl,
  932. .set_umac_addr = sun8i_dwmac_set_umac_addr,
  933. .get_umac_addr = sun8i_dwmac_get_umac_addr,
  934. .set_mac_loopback = sun8i_dwmac_set_mac_loopback,
  935. };
  936. static struct mac_device_info *sun8i_dwmac_setup(void *ppriv)
  937. {
  938. struct mac_device_info *mac;
  939. struct stmmac_priv *priv = ppriv;
  940. mac = devm_kzalloc(priv->device, sizeof(*mac), GFP_KERNEL);
  941. if (!mac)
  942. return NULL;
  943. mac->pcsr = priv->ioaddr;
  944. mac->mac = &sun8i_dwmac_ops;
  945. mac->dma = &sun8i_dwmac_dma_ops;
  946. priv->dev->priv_flags |= IFF_UNICAST_FLT;
  947. mac->link.caps = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
  948. MAC_10 | MAC_100 | MAC_1000;
  949. /* The loopback bit seems to be re-set when link change
  950. * Simply mask it each time
  951. * Speed 10/100/1000 are set in BIT(2)/BIT(3)
  952. */
  953. mac->link.speed_mask = GENMASK(3, 2) | EMAC_LOOPBACK;
  954. mac->link.speed10 = EMAC_SPEED_10;
  955. mac->link.speed100 = EMAC_SPEED_100;
  956. mac->link.speed1000 = EMAC_SPEED_1000;
  957. mac->link.duplex = EMAC_DUPLEX_FULL;
  958. mac->mii.addr = EMAC_MDIO_CMD;
  959. mac->mii.data = EMAC_MDIO_DATA;
  960. mac->mii.reg_shift = 4;
  961. mac->mii.reg_mask = GENMASK(8, 4);
  962. mac->mii.addr_shift = 12;
  963. mac->mii.addr_mask = GENMASK(16, 12);
  964. mac->mii.clk_csr_shift = 20;
  965. mac->mii.clk_csr_mask = GENMASK(22, 20);
  966. mac->unicast_filter_entries = 8;
  967. /* Synopsys Id is not available */
  968. priv->synopsys_id = 0;
  969. return mac;
  970. }
  971. static struct regmap *sun8i_dwmac_get_syscon_from_dev(struct device_node *node)
  972. {
  973. struct device_node *syscon_node;
  974. struct platform_device *syscon_pdev;
  975. struct regmap *regmap = NULL;
  976. syscon_node = of_parse_phandle(node, "syscon", 0);
  977. if (!syscon_node)
  978. return ERR_PTR(-ENODEV);
  979. syscon_pdev = of_find_device_by_node(syscon_node);
  980. if (!syscon_pdev) {
  981. /* platform device might not be probed yet */
  982. regmap = ERR_PTR(-EPROBE_DEFER);
  983. goto out_put_node;
  984. }
  985. /* If no regmap is found then the other device driver is at fault */
  986. regmap = dev_get_regmap(&syscon_pdev->dev, NULL);
  987. if (!regmap)
  988. regmap = ERR_PTR(-EINVAL);
  989. platform_device_put(syscon_pdev);
  990. out_put_node:
  991. of_node_put(syscon_node);
  992. return regmap;
  993. }
  994. static int sun8i_dwmac_probe(struct platform_device *pdev)
  995. {
  996. struct plat_stmmacenet_data *plat_dat;
  997. struct stmmac_resources stmmac_res;
  998. struct sunxi_priv_data *gmac;
  999. struct device *dev = &pdev->dev;
  1000. phy_interface_t interface;
  1001. int ret;
  1002. struct stmmac_priv *priv;
  1003. struct net_device *ndev;
  1004. struct regmap *regmap;
  1005. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  1006. if (ret)
  1007. return ret;
  1008. gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL);
  1009. if (!gmac)
  1010. return -ENOMEM;
  1011. gmac->variant = of_device_get_match_data(&pdev->dev);
  1012. if (!gmac->variant) {
  1013. dev_err(&pdev->dev, "Missing dwmac-sun8i variant\n");
  1014. return -EINVAL;
  1015. }
  1016. /* Optional regulator for PHY */
  1017. gmac->regulator = devm_regulator_get_optional(dev, "phy");
  1018. if (IS_ERR(gmac->regulator)) {
  1019. if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER)
  1020. return -EPROBE_DEFER;
  1021. dev_info(dev, "No regulator found\n");
  1022. gmac->regulator = NULL;
  1023. }
  1024. /* The "GMAC clock control" register might be located in the
  1025. * CCU address range (on the R40), or the system control address
  1026. * range (on most other sun8i and later SoCs).
  1027. *
  1028. * The former controls most if not all clocks in the SoC. The
  1029. * latter has an SoC identification register, and on some SoCs,
  1030. * controls to map device specific SRAM to either the intended
  1031. * peripheral, or the CPU address space.
  1032. *
  1033. * In either case, there should be a coordinated and restricted
  1034. * method of accessing the register needed here. This is done by
  1035. * having the device export a custom regmap, instead of a generic
  1036. * syscon, which grants all access to all registers.
  1037. *
  1038. * To support old device trees, we fall back to using the syscon
  1039. * interface if possible.
  1040. */
  1041. regmap = sun8i_dwmac_get_syscon_from_dev(pdev->dev.of_node);
  1042. if (IS_ERR(regmap))
  1043. regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  1044. "syscon");
  1045. if (IS_ERR(regmap)) {
  1046. ret = PTR_ERR(regmap);
  1047. dev_err(&pdev->dev, "Unable to map syscon: %d\n", ret);
  1048. return ret;
  1049. }
  1050. gmac->regmap_field = devm_regmap_field_alloc(dev, regmap,
  1051. *gmac->variant->syscon_field);
  1052. if (IS_ERR(gmac->regmap_field)) {
  1053. ret = PTR_ERR(gmac->regmap_field);
  1054. dev_err(dev, "Unable to map syscon register: %d\n", ret);
  1055. return ret;
  1056. }
  1057. ret = of_get_phy_mode(dev->of_node, &interface);
  1058. if (ret)
  1059. return -EINVAL;
  1060. plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac);
  1061. if (IS_ERR(plat_dat))
  1062. return PTR_ERR(plat_dat);
  1063. /* platform data specifying hardware features and callbacks.
  1064. * hardware features were copied from Allwinner drivers.
  1065. */
  1066. plat_dat->mac_interface = interface;
  1067. plat_dat->rx_coe = STMMAC_RX_COE_TYPE2;
  1068. plat_dat->tx_coe = 1;
  1069. plat_dat->flags |= STMMAC_FLAG_HAS_SUN8I;
  1070. plat_dat->bsp_priv = gmac;
  1071. plat_dat->init = sun8i_dwmac_init;
  1072. plat_dat->exit = sun8i_dwmac_exit;
  1073. plat_dat->setup = sun8i_dwmac_setup;
  1074. plat_dat->tx_fifo_size = 4096;
  1075. plat_dat->rx_fifo_size = 16384;
  1076. ret = sun8i_dwmac_set_syscon(&pdev->dev, plat_dat);
  1077. if (ret)
  1078. return ret;
  1079. ret = sun8i_dwmac_init(pdev, plat_dat->bsp_priv);
  1080. if (ret)
  1081. goto dwmac_syscon;
  1082. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  1083. if (ret)
  1084. goto dwmac_exit;
  1085. ndev = dev_get_drvdata(&pdev->dev);
  1086. priv = netdev_priv(ndev);
  1087. /* the MAC is runtime suspended after stmmac_dvr_probe(), so we
  1088. * need to ensure the MAC resume back before other operations such
  1089. * as reset.
  1090. */
  1091. pm_runtime_get_sync(&pdev->dev);
  1092. /* The mux must be registered after parent MDIO
  1093. * so after stmmac_dvr_probe()
  1094. */
  1095. if (gmac->variant->soc_has_internal_phy) {
  1096. ret = get_ephy_nodes(priv);
  1097. if (ret)
  1098. goto dwmac_remove;
  1099. ret = sun8i_dwmac_register_mdio_mux(priv);
  1100. if (ret) {
  1101. dev_err(&pdev->dev, "Failed to register mux\n");
  1102. goto dwmac_mux;
  1103. }
  1104. } else {
  1105. ret = sun8i_dwmac_reset(priv);
  1106. if (ret)
  1107. goto dwmac_remove;
  1108. }
  1109. pm_runtime_put(&pdev->dev);
  1110. return 0;
  1111. dwmac_mux:
  1112. reset_control_put(gmac->rst_ephy);
  1113. clk_put(gmac->ephy_clk);
  1114. dwmac_remove:
  1115. pm_runtime_put_noidle(&pdev->dev);
  1116. stmmac_dvr_remove(&pdev->dev);
  1117. dwmac_exit:
  1118. sun8i_dwmac_exit(pdev, gmac);
  1119. dwmac_syscon:
  1120. sun8i_dwmac_unset_syscon(gmac);
  1121. return ret;
  1122. }
  1123. static void sun8i_dwmac_remove(struct platform_device *pdev)
  1124. {
  1125. struct net_device *ndev = platform_get_drvdata(pdev);
  1126. struct stmmac_priv *priv = netdev_priv(ndev);
  1127. struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
  1128. if (gmac->variant->soc_has_internal_phy) {
  1129. mdio_mux_uninit(gmac->mux_handle);
  1130. sun8i_dwmac_unpower_internal_phy(gmac);
  1131. reset_control_put(gmac->rst_ephy);
  1132. clk_put(gmac->ephy_clk);
  1133. }
  1134. stmmac_pltfr_remove(pdev);
  1135. sun8i_dwmac_unset_syscon(gmac);
  1136. }
  1137. static void sun8i_dwmac_shutdown(struct platform_device *pdev)
  1138. {
  1139. struct net_device *ndev = platform_get_drvdata(pdev);
  1140. struct stmmac_priv *priv = netdev_priv(ndev);
  1141. struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
  1142. sun8i_dwmac_exit(pdev, gmac);
  1143. }
  1144. static const struct of_device_id sun8i_dwmac_match[] = {
  1145. { .compatible = "allwinner,sun8i-h3-emac",
  1146. .data = &emac_variant_h3 },
  1147. { .compatible = "allwinner,sun8i-v3s-emac",
  1148. .data = &emac_variant_v3s },
  1149. { .compatible = "allwinner,sun8i-a83t-emac",
  1150. .data = &emac_variant_a83t },
  1151. { .compatible = "allwinner,sun8i-r40-gmac",
  1152. .data = &emac_variant_r40 },
  1153. { .compatible = "allwinner,sun50i-a64-emac",
  1154. .data = &emac_variant_a64 },
  1155. { .compatible = "allwinner,sun50i-h6-emac",
  1156. .data = &emac_variant_h6 },
  1157. { }
  1158. };
  1159. MODULE_DEVICE_TABLE(of, sun8i_dwmac_match);
  1160. static struct platform_driver sun8i_dwmac_driver = {
  1161. .probe = sun8i_dwmac_probe,
  1162. .remove_new = sun8i_dwmac_remove,
  1163. .shutdown = sun8i_dwmac_shutdown,
  1164. .driver = {
  1165. .name = "dwmac-sun8i",
  1166. .pm = &stmmac_pltfr_pm_ops,
  1167. .of_match_table = sun8i_dwmac_match,
  1168. },
  1169. };
  1170. module_platform_driver(sun8i_dwmac_driver);
  1171. MODULE_AUTHOR("Corentin Labbe <clabbe.montjoie@gmail.com>");
  1172. MODULE_DESCRIPTION("Allwinner sun8i DWMAC specific glue layer");
  1173. MODULE_LICENSE("GPL");