dwmac-sun8i.c 35 KB

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