dwmac-sun8i.c 32 KB

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