dwmac-sun8i.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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 - Describe 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_get_compatible_child(mdio_mux,
  619. "allwinner,sun8i-h3-mdio-internal");
  620. of_node_put(mdio_mux);
  621. if (!mdio_internal) {
  622. dev_err(priv->device, "Cannot get internal_mdio node\n");
  623. return -ENODEV;
  624. }
  625. /* Seek for internal PHY */
  626. for_each_child_of_node(mdio_internal, iphynode) {
  627. gmac->ephy_clk = of_clk_get(iphynode, 0);
  628. if (IS_ERR(gmac->ephy_clk))
  629. continue;
  630. gmac->rst_ephy = of_reset_control_get_exclusive(iphynode, NULL);
  631. if (IS_ERR(gmac->rst_ephy)) {
  632. ret = PTR_ERR(gmac->rst_ephy);
  633. if (ret == -EPROBE_DEFER) {
  634. of_node_put(iphynode);
  635. of_node_put(mdio_internal);
  636. return ret;
  637. }
  638. continue;
  639. }
  640. dev_info(priv->device, "Found internal PHY node\n");
  641. of_node_put(iphynode);
  642. of_node_put(mdio_internal);
  643. return 0;
  644. }
  645. of_node_put(mdio_internal);
  646. return -ENODEV;
  647. }
  648. static int sun8i_dwmac_power_internal_phy(struct stmmac_priv *priv)
  649. {
  650. struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
  651. int ret;
  652. if (gmac->internal_phy_powered) {
  653. dev_warn(priv->device, "Internal PHY already powered\n");
  654. return 0;
  655. }
  656. dev_info(priv->device, "Powering internal PHY\n");
  657. ret = clk_prepare_enable(gmac->ephy_clk);
  658. if (ret) {
  659. dev_err(priv->device, "Cannot enable internal PHY\n");
  660. return ret;
  661. }
  662. /* Make sure the EPHY is properly reseted, as U-Boot may leave
  663. * it at deasserted state, and thus it may fail to reset EMAC.
  664. */
  665. reset_control_assert(gmac->rst_ephy);
  666. ret = reset_control_deassert(gmac->rst_ephy);
  667. if (ret) {
  668. dev_err(priv->device, "Cannot deassert internal phy\n");
  669. clk_disable_unprepare(gmac->ephy_clk);
  670. return ret;
  671. }
  672. gmac->internal_phy_powered = true;
  673. return 0;
  674. }
  675. static int sun8i_dwmac_unpower_internal_phy(struct sunxi_priv_data *gmac)
  676. {
  677. if (!gmac->internal_phy_powered)
  678. return 0;
  679. clk_disable_unprepare(gmac->ephy_clk);
  680. reset_control_assert(gmac->rst_ephy);
  681. gmac->internal_phy_powered = false;
  682. return 0;
  683. }
  684. /* MDIO multiplexing switch function
  685. * This function is called by the mdio-mux layer when it thinks the mdio bus
  686. * multiplexer needs to switch.
  687. * 'current_child' is the current value of the mux register
  688. * 'desired_child' is the value of the 'reg' property of the target child MDIO
  689. * node.
  690. * The first time this function is called, current_child == -1.
  691. * If current_child == desired_child, then the mux is already set to the
  692. * correct bus.
  693. */
  694. static int mdio_mux_syscon_switch_fn(int current_child, int desired_child,
  695. void *data)
  696. {
  697. struct stmmac_priv *priv = data;
  698. struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
  699. u32 reg, val;
  700. int ret = 0;
  701. bool need_power_ephy = false;
  702. if (current_child ^ desired_child) {
  703. regmap_field_read(gmac->regmap_field, &reg);
  704. switch (desired_child) {
  705. case DWMAC_SUN8I_MDIO_MUX_INTERNAL_ID:
  706. dev_info(priv->device, "Switch mux to internal PHY");
  707. val = (reg & ~H3_EPHY_MUX_MASK) | H3_EPHY_SELECT;
  708. need_power_ephy = true;
  709. break;
  710. case DWMAC_SUN8I_MDIO_MUX_EXTERNAL_ID:
  711. dev_info(priv->device, "Switch mux to external PHY");
  712. val = (reg & ~H3_EPHY_MUX_MASK) | H3_EPHY_SHUTDOWN;
  713. need_power_ephy = false;
  714. break;
  715. default:
  716. dev_err(priv->device, "Invalid child ID %x\n",
  717. desired_child);
  718. return -EINVAL;
  719. }
  720. regmap_field_write(gmac->regmap_field, val);
  721. if (need_power_ephy) {
  722. ret = sun8i_dwmac_power_internal_phy(priv);
  723. if (ret)
  724. return ret;
  725. } else {
  726. sun8i_dwmac_unpower_internal_phy(gmac);
  727. }
  728. /* After changing syscon value, the MAC need reset or it will
  729. * use the last value (and so the last PHY set).
  730. */
  731. ret = sun8i_dwmac_reset(priv);
  732. }
  733. return ret;
  734. }
  735. static int sun8i_dwmac_register_mdio_mux(struct stmmac_priv *priv)
  736. {
  737. int ret;
  738. struct device_node *mdio_mux;
  739. struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
  740. mdio_mux = of_get_child_by_name(priv->device->of_node, "mdio-mux");
  741. if (!mdio_mux)
  742. return -ENODEV;
  743. ret = mdio_mux_init(priv->device, mdio_mux, mdio_mux_syscon_switch_fn,
  744. &gmac->mux_handle, priv, priv->mii);
  745. return ret;
  746. }
  747. static int sun8i_dwmac_set_syscon(struct stmmac_priv *priv)
  748. {
  749. struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
  750. struct device_node *node = priv->device->of_node;
  751. int ret;
  752. u32 reg, val;
  753. regmap_field_read(gmac->regmap_field, &val);
  754. reg = gmac->variant->default_syscon_value;
  755. if (reg != val)
  756. dev_warn(priv->device,
  757. "Current syscon value is not the default %x (expect %x)\n",
  758. val, reg);
  759. if (gmac->variant->soc_has_internal_phy) {
  760. if (of_property_read_bool(node, "allwinner,leds-active-low"))
  761. reg |= H3_EPHY_LED_POL;
  762. else
  763. reg &= ~H3_EPHY_LED_POL;
  764. /* Force EPHY xtal frequency to 24MHz. */
  765. reg |= H3_EPHY_CLK_SEL;
  766. ret = of_mdio_parse_addr(priv->device, priv->plat->phy_node);
  767. if (ret < 0) {
  768. dev_err(priv->device, "Could not parse MDIO addr\n");
  769. return ret;
  770. }
  771. /* of_mdio_parse_addr returns a valid (0 ~ 31) PHY
  772. * address. No need to mask it again.
  773. */
  774. reg |= 1 << H3_EPHY_ADDR_SHIFT;
  775. } else {
  776. /* For SoCs without internal PHY the PHY selection bit should be
  777. * set to 0 (external PHY).
  778. */
  779. reg &= ~H3_EPHY_SELECT;
  780. }
  781. if (!of_property_read_u32(node, "allwinner,tx-delay-ps", &val)) {
  782. if (val % 100) {
  783. dev_err(priv->device, "tx-delay must be a multiple of 100\n");
  784. return -EINVAL;
  785. }
  786. val /= 100;
  787. dev_dbg(priv->device, "set tx-delay to %x\n", val);
  788. if (val <= gmac->variant->tx_delay_max) {
  789. reg &= ~(gmac->variant->tx_delay_max <<
  790. SYSCON_ETXDC_SHIFT);
  791. reg |= (val << SYSCON_ETXDC_SHIFT);
  792. } else {
  793. dev_err(priv->device, "Invalid TX clock delay: %d\n",
  794. val);
  795. return -EINVAL;
  796. }
  797. }
  798. if (!of_property_read_u32(node, "allwinner,rx-delay-ps", &val)) {
  799. if (val % 100) {
  800. dev_err(priv->device, "rx-delay must be a multiple of 100\n");
  801. return -EINVAL;
  802. }
  803. val /= 100;
  804. dev_dbg(priv->device, "set rx-delay to %x\n", val);
  805. if (val <= gmac->variant->rx_delay_max) {
  806. reg &= ~(gmac->variant->rx_delay_max <<
  807. SYSCON_ERXDC_SHIFT);
  808. reg |= (val << SYSCON_ERXDC_SHIFT);
  809. } else {
  810. dev_err(priv->device, "Invalid RX clock delay: %d\n",
  811. val);
  812. return -EINVAL;
  813. }
  814. }
  815. /* Clear interface mode bits */
  816. reg &= ~(SYSCON_ETCS_MASK | SYSCON_EPIT);
  817. if (gmac->variant->support_rmii)
  818. reg &= ~SYSCON_RMII_EN;
  819. switch (priv->plat->interface) {
  820. case PHY_INTERFACE_MODE_MII:
  821. /* default */
  822. break;
  823. case PHY_INTERFACE_MODE_RGMII:
  824. case PHY_INTERFACE_MODE_RGMII_ID:
  825. case PHY_INTERFACE_MODE_RGMII_RXID:
  826. case PHY_INTERFACE_MODE_RGMII_TXID:
  827. reg |= SYSCON_EPIT | SYSCON_ETCS_INT_GMII;
  828. break;
  829. case PHY_INTERFACE_MODE_RMII:
  830. reg |= SYSCON_RMII_EN | SYSCON_ETCS_EXT_GMII;
  831. break;
  832. default:
  833. dev_err(priv->device, "Unsupported interface mode: %s",
  834. phy_modes(priv->plat->interface));
  835. return -EINVAL;
  836. }
  837. regmap_field_write(gmac->regmap_field, reg);
  838. return 0;
  839. }
  840. static void sun8i_dwmac_unset_syscon(struct sunxi_priv_data *gmac)
  841. {
  842. u32 reg = gmac->variant->default_syscon_value;
  843. regmap_field_write(gmac->regmap_field, reg);
  844. }
  845. static void sun8i_dwmac_exit(struct platform_device *pdev, void *priv)
  846. {
  847. struct sunxi_priv_data *gmac = priv;
  848. if (gmac->variant->soc_has_internal_phy) {
  849. /* sun8i_dwmac_exit could be called with mdiomux uninit */
  850. if (gmac->mux_handle)
  851. mdio_mux_uninit(gmac->mux_handle);
  852. if (gmac->internal_phy_powered)
  853. sun8i_dwmac_unpower_internal_phy(gmac);
  854. }
  855. sun8i_dwmac_unset_syscon(gmac);
  856. reset_control_put(gmac->rst_ephy);
  857. clk_disable_unprepare(gmac->tx_clk);
  858. if (gmac->regulator)
  859. regulator_disable(gmac->regulator);
  860. }
  861. static const struct stmmac_ops sun8i_dwmac_ops = {
  862. .core_init = sun8i_dwmac_core_init,
  863. .set_mac = sun8i_dwmac_set_mac,
  864. .dump_regs = sun8i_dwmac_dump_mac_regs,
  865. .rx_ipc = sun8i_dwmac_rx_ipc_enable,
  866. .set_filter = sun8i_dwmac_set_filter,
  867. .flow_ctrl = sun8i_dwmac_flow_ctrl,
  868. .set_umac_addr = sun8i_dwmac_set_umac_addr,
  869. .get_umac_addr = sun8i_dwmac_get_umac_addr,
  870. };
  871. static struct mac_device_info *sun8i_dwmac_setup(void *ppriv)
  872. {
  873. struct mac_device_info *mac;
  874. struct stmmac_priv *priv = ppriv;
  875. int ret;
  876. mac = devm_kzalloc(priv->device, sizeof(*mac), GFP_KERNEL);
  877. if (!mac)
  878. return NULL;
  879. ret = sun8i_dwmac_set_syscon(priv);
  880. if (ret)
  881. return NULL;
  882. mac->pcsr = priv->ioaddr;
  883. mac->mac = &sun8i_dwmac_ops;
  884. mac->dma = &sun8i_dwmac_dma_ops;
  885. priv->dev->priv_flags |= IFF_UNICAST_FLT;
  886. /* The loopback bit seems to be re-set when link change
  887. * Simply mask it each time
  888. * Speed 10/100/1000 are set in BIT(2)/BIT(3)
  889. */
  890. mac->link.speed_mask = GENMASK(3, 2) | EMAC_LOOPBACK;
  891. mac->link.speed10 = EMAC_SPEED_10;
  892. mac->link.speed100 = EMAC_SPEED_100;
  893. mac->link.speed1000 = EMAC_SPEED_1000;
  894. mac->link.duplex = EMAC_DUPLEX_FULL;
  895. mac->mii.addr = EMAC_MDIO_CMD;
  896. mac->mii.data = EMAC_MDIO_DATA;
  897. mac->mii.reg_shift = 4;
  898. mac->mii.reg_mask = GENMASK(8, 4);
  899. mac->mii.addr_shift = 12;
  900. mac->mii.addr_mask = GENMASK(16, 12);
  901. mac->mii.clk_csr_shift = 20;
  902. mac->mii.clk_csr_mask = GENMASK(22, 20);
  903. mac->unicast_filter_entries = 8;
  904. /* Synopsys Id is not available */
  905. priv->synopsys_id = 0;
  906. return mac;
  907. }
  908. static struct regmap *sun8i_dwmac_get_syscon_from_dev(struct device_node *node)
  909. {
  910. struct device_node *syscon_node;
  911. struct platform_device *syscon_pdev;
  912. struct regmap *regmap = NULL;
  913. syscon_node = of_parse_phandle(node, "syscon", 0);
  914. if (!syscon_node)
  915. return ERR_PTR(-ENODEV);
  916. syscon_pdev = of_find_device_by_node(syscon_node);
  917. if (!syscon_pdev) {
  918. /* platform device might not be probed yet */
  919. regmap = ERR_PTR(-EPROBE_DEFER);
  920. goto out_put_node;
  921. }
  922. /* If no regmap is found then the other device driver is at fault */
  923. regmap = dev_get_regmap(&syscon_pdev->dev, NULL);
  924. if (!regmap)
  925. regmap = ERR_PTR(-EINVAL);
  926. platform_device_put(syscon_pdev);
  927. out_put_node:
  928. of_node_put(syscon_node);
  929. return regmap;
  930. }
  931. static int sun8i_dwmac_probe(struct platform_device *pdev)
  932. {
  933. struct plat_stmmacenet_data *plat_dat;
  934. struct stmmac_resources stmmac_res;
  935. struct sunxi_priv_data *gmac;
  936. struct device *dev = &pdev->dev;
  937. int ret;
  938. struct stmmac_priv *priv;
  939. struct net_device *ndev;
  940. struct regmap *regmap;
  941. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  942. if (ret)
  943. return ret;
  944. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  945. if (IS_ERR(plat_dat))
  946. return PTR_ERR(plat_dat);
  947. gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL);
  948. if (!gmac)
  949. return -ENOMEM;
  950. gmac->variant = of_device_get_match_data(&pdev->dev);
  951. if (!gmac->variant) {
  952. dev_err(&pdev->dev, "Missing dwmac-sun8i variant\n");
  953. return -EINVAL;
  954. }
  955. gmac->tx_clk = devm_clk_get(dev, "stmmaceth");
  956. if (IS_ERR(gmac->tx_clk)) {
  957. dev_err(dev, "Could not get TX clock\n");
  958. return PTR_ERR(gmac->tx_clk);
  959. }
  960. /* Optional regulator for PHY */
  961. gmac->regulator = devm_regulator_get_optional(dev, "phy");
  962. if (IS_ERR(gmac->regulator)) {
  963. if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER)
  964. return -EPROBE_DEFER;
  965. dev_info(dev, "No regulator found\n");
  966. gmac->regulator = NULL;
  967. }
  968. /* The "GMAC clock control" register might be located in the
  969. * CCU address range (on the R40), or the system control address
  970. * range (on most other sun8i and later SoCs).
  971. *
  972. * The former controls most if not all clocks in the SoC. The
  973. * latter has an SoC identification register, and on some SoCs,
  974. * controls to map device specific SRAM to either the intended
  975. * peripheral, or the CPU address space.
  976. *
  977. * In either case, there should be a coordinated and restricted
  978. * method of accessing the register needed here. This is done by
  979. * having the device export a custom regmap, instead of a generic
  980. * syscon, which grants all access to all registers.
  981. *
  982. * To support old device trees, we fall back to using the syscon
  983. * interface if possible.
  984. */
  985. regmap = sun8i_dwmac_get_syscon_from_dev(pdev->dev.of_node);
  986. if (IS_ERR(regmap))
  987. regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  988. "syscon");
  989. if (IS_ERR(regmap)) {
  990. ret = PTR_ERR(regmap);
  991. dev_err(&pdev->dev, "Unable to map syscon: %d\n", ret);
  992. return ret;
  993. }
  994. gmac->regmap_field = devm_regmap_field_alloc(dev, regmap,
  995. *gmac->variant->syscon_field);
  996. if (IS_ERR(gmac->regmap_field)) {
  997. ret = PTR_ERR(gmac->regmap_field);
  998. dev_err(dev, "Unable to map syscon register: %d\n", ret);
  999. return ret;
  1000. }
  1001. plat_dat->interface = of_get_phy_mode(dev->of_node);
  1002. /* platform data specifying hardware features and callbacks.
  1003. * hardware features were copied from Allwinner drivers.
  1004. */
  1005. plat_dat->rx_coe = STMMAC_RX_COE_TYPE2;
  1006. plat_dat->tx_coe = 1;
  1007. plat_dat->has_sun8i = true;
  1008. plat_dat->bsp_priv = gmac;
  1009. plat_dat->init = sun8i_dwmac_init;
  1010. plat_dat->exit = sun8i_dwmac_exit;
  1011. plat_dat->setup = sun8i_dwmac_setup;
  1012. ret = sun8i_dwmac_init(pdev, plat_dat->bsp_priv);
  1013. if (ret)
  1014. return ret;
  1015. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  1016. if (ret)
  1017. goto dwmac_exit;
  1018. ndev = dev_get_drvdata(&pdev->dev);
  1019. priv = netdev_priv(ndev);
  1020. /* The mux must be registered after parent MDIO
  1021. * so after stmmac_dvr_probe()
  1022. */
  1023. if (gmac->variant->soc_has_internal_phy) {
  1024. ret = get_ephy_nodes(priv);
  1025. if (ret)
  1026. goto dwmac_exit;
  1027. ret = sun8i_dwmac_register_mdio_mux(priv);
  1028. if (ret) {
  1029. dev_err(&pdev->dev, "Failed to register mux\n");
  1030. goto dwmac_mux;
  1031. }
  1032. } else {
  1033. ret = sun8i_dwmac_reset(priv);
  1034. if (ret)
  1035. goto dwmac_exit;
  1036. }
  1037. return ret;
  1038. dwmac_mux:
  1039. sun8i_dwmac_unset_syscon(gmac);
  1040. dwmac_exit:
  1041. stmmac_pltfr_remove(pdev);
  1042. return ret;
  1043. }
  1044. static const struct of_device_id sun8i_dwmac_match[] = {
  1045. { .compatible = "allwinner,sun8i-h3-emac",
  1046. .data = &emac_variant_h3 },
  1047. { .compatible = "allwinner,sun8i-v3s-emac",
  1048. .data = &emac_variant_v3s },
  1049. { .compatible = "allwinner,sun8i-a83t-emac",
  1050. .data = &emac_variant_a83t },
  1051. { .compatible = "allwinner,sun8i-r40-gmac",
  1052. .data = &emac_variant_r40 },
  1053. { .compatible = "allwinner,sun50i-a64-emac",
  1054. .data = &emac_variant_a64 },
  1055. { }
  1056. };
  1057. MODULE_DEVICE_TABLE(of, sun8i_dwmac_match);
  1058. static struct platform_driver sun8i_dwmac_driver = {
  1059. .probe = sun8i_dwmac_probe,
  1060. .remove = stmmac_pltfr_remove,
  1061. .driver = {
  1062. .name = "dwmac-sun8i",
  1063. .pm = &stmmac_pltfr_pm_ops,
  1064. .of_match_table = sun8i_dwmac_match,
  1065. },
  1066. };
  1067. module_platform_driver(sun8i_dwmac_driver);
  1068. MODULE_AUTHOR("Corentin Labbe <clabbe.montjoie@gmail.com>");
  1069. MODULE_DESCRIPTION("Allwinner sun8i DWMAC specific glue layer");
  1070. MODULE_LICENSE("GPL");