xilinx_axi_emac.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Waymo LLC
  4. * Copyright (C) 2011 Michal Simek <monstr@monstr.eu>
  5. * Copyright (C) 2011 PetaLogix
  6. * Copyright (C) 2010 Xilinx, Inc. All rights reserved.
  7. */
  8. #include <config.h>
  9. #include <common.h>
  10. #include <cpu_func.h>
  11. #include <display_options.h>
  12. #include <dm.h>
  13. #include <dm/device_compat.h>
  14. #include <log.h>
  15. #include <net.h>
  16. #include <malloc.h>
  17. #include <asm/global_data.h>
  18. #include <asm/io.h>
  19. #include <phy.h>
  20. #include <miiphy.h>
  21. #include <wait_bit.h>
  22. #include <linux/delay.h>
  23. #include <eth_phy.h>
  24. DECLARE_GLOBAL_DATA_PTR;
  25. /* Link setup */
  26. #define XAE_EMMC_LINKSPEED_MASK 0xC0000000 /* Link speed */
  27. #define XAE_EMMC_LINKSPD_10 0x00000000 /* Link Speed mask for 10 Mbit */
  28. #define XAE_EMMC_LINKSPD_100 0x40000000 /* Link Speed mask for 100 Mbit */
  29. #define XAE_EMMC_LINKSPD_1000 0x80000000 /* Link Speed mask for 1000 Mbit */
  30. /* Interrupt Status/Enable/Mask Registers bit definitions */
  31. #define XAE_INT_RXRJECT_MASK 0x00000008 /* Rx frame rejected */
  32. #define XAE_INT_MGTRDY_MASK 0x00000080 /* MGT clock Lock */
  33. /* Receive Configuration Word 1 (RCW1) Register bit definitions */
  34. #define XAE_RCW1_RX_MASK 0x10000000 /* Receiver enable */
  35. /* Transmitter Configuration (TC) Register bit definitions */
  36. #define XAE_TC_TX_MASK 0x10000000 /* Transmitter enable */
  37. #define XAE_UAW1_UNICASTADDR_MASK 0x0000FFFF
  38. /* MDIO Management Configuration (MC) Register bit definitions */
  39. #define XAE_MDIO_MC_MDIOEN_MASK 0x00000040 /* MII management enable*/
  40. /* MDIO Management Control Register (MCR) Register bit definitions */
  41. #define XAE_MDIO_MCR_PHYAD_MASK 0x1F000000 /* Phy Address Mask */
  42. #define XAE_MDIO_MCR_PHYAD_SHIFT 24 /* Phy Address Shift */
  43. #define XAE_MDIO_MCR_REGAD_MASK 0x001F0000 /* Reg Address Mask */
  44. #define XAE_MDIO_MCR_REGAD_SHIFT 16 /* Reg Address Shift */
  45. #define XAE_MDIO_MCR_OP_READ_MASK 0x00008000 /* Op Code Read Mask */
  46. #define XAE_MDIO_MCR_OP_WRITE_MASK 0x00004000 /* Op Code Write Mask */
  47. #define XAE_MDIO_MCR_INITIATE_MASK 0x00000800 /* Ready Mask */
  48. #define XAE_MDIO_MCR_READY_MASK 0x00000080 /* Ready Mask */
  49. #define XAE_MDIO_DIV_DFT 29 /* Default MDIO clock divisor */
  50. #define XAXIDMA_BD_STS_ACTUAL_LEN_MASK 0x007FFFFF /* Actual len */
  51. /* DMA macros */
  52. /* Bitmasks of XAXIDMA_CR_OFFSET register */
  53. #define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */
  54. #define XAXIDMA_CR_RESET_MASK 0x00000004 /* Reset DMA engine */
  55. /* Bitmasks of XAXIDMA_SR_OFFSET register */
  56. #define XAXIDMA_HALTED_MASK 0x00000001 /* DMA channel halted */
  57. /* Bitmask for interrupts */
  58. #define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */
  59. #define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */
  60. #define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */
  61. /* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */
  62. #define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */
  63. #define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */
  64. /* Bitmasks for XXV Ethernet MAC */
  65. #define XXV_TC_TX_MASK 0x00000001
  66. #define XXV_TC_FCS_MASK 0x00000002
  67. #define XXV_RCW1_RX_MASK 0x00000001
  68. #define XXV_RCW1_FCS_MASK 0x00000002
  69. #define DMAALIGN 128
  70. #define XXV_MIN_PKT_SIZE 60
  71. static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN)));
  72. static u8 txminframe[XXV_MIN_PKT_SIZE] __attribute((aligned(DMAALIGN)));
  73. enum emac_variant {
  74. EMAC_1G = 0,
  75. EMAC_10G_25G = 1,
  76. };
  77. /* Reflect dma offsets */
  78. struct axidma_reg {
  79. u32 control; /* DMACR */
  80. u32 status; /* DMASR */
  81. u32 current; /* CURDESC low 32 bit */
  82. u32 current_hi; /* CURDESC high 32 bit */
  83. u32 tail; /* TAILDESC low 32 bit */
  84. u32 tail_hi; /* TAILDESC high 32 bit */
  85. };
  86. /* Platform data structures */
  87. struct axidma_plat {
  88. struct eth_pdata eth_pdata;
  89. struct axidma_reg *dmatx;
  90. struct axidma_reg *dmarx;
  91. int pcsaddr;
  92. int phyaddr;
  93. u8 eth_hasnobuf;
  94. int phy_of_handle;
  95. enum emac_variant mactype;
  96. };
  97. /* Private driver structures */
  98. struct axidma_priv {
  99. struct axidma_reg *dmatx;
  100. struct axidma_reg *dmarx;
  101. int pcsaddr;
  102. int phyaddr;
  103. struct axi_regs *iobase;
  104. phy_interface_t interface;
  105. struct phy_device *phydev;
  106. struct mii_dev *bus;
  107. u8 eth_hasnobuf;
  108. int phy_of_handle;
  109. enum emac_variant mactype;
  110. };
  111. /* BD descriptors */
  112. struct axidma_bd {
  113. u32 next_desc; /* Next descriptor pointer */
  114. u32 next_desc_msb;
  115. u32 buf_addr; /* Buffer address */
  116. u32 buf_addr_msb;
  117. u32 reserved3;
  118. u32 reserved4;
  119. u32 cntrl; /* Control */
  120. u32 status; /* Status */
  121. u32 app0;
  122. u32 app1; /* TX start << 16 | insert */
  123. u32 app2; /* TX csum seed */
  124. u32 app3;
  125. u32 app4;
  126. u32 sw_id_offset;
  127. u32 reserved5;
  128. u32 reserved6;
  129. };
  130. /* Static BDs - driver uses only one BD */
  131. static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN)));
  132. static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN)));
  133. struct axi_regs {
  134. u32 reserved[3];
  135. u32 is; /* 0xC: Interrupt status */
  136. u32 reserved2;
  137. u32 ie; /* 0x14: Interrupt enable */
  138. u32 reserved3[251];
  139. u32 rcw1; /* 0x404: Rx Configuration Word 1 */
  140. u32 tc; /* 0x408: Tx Configuration */
  141. u32 reserved4;
  142. u32 emmc; /* 0x410: EMAC mode configuration */
  143. u32 reserved5[59];
  144. u32 mdio_mc; /* 0x500: MII Management Config */
  145. u32 mdio_mcr; /* 0x504: MII Management Control */
  146. u32 mdio_mwd; /* 0x508: MII Management Write Data */
  147. u32 mdio_mrd; /* 0x50C: MII Management Read Data */
  148. u32 reserved6[124];
  149. u32 uaw0; /* 0x700: Unicast address word 0 */
  150. u32 uaw1; /* 0x704: Unicast address word 1 */
  151. };
  152. struct xxv_axi_regs {
  153. u32 gt_reset; /* 0x0 */
  154. u32 reserved[2];
  155. u32 tc; /* 0xC: Tx Configuration */
  156. u32 reserved2;
  157. u32 rcw1; /* 0x14: Rx Configuration Word 1 */
  158. };
  159. /* Use MII register 1 (MII status register) to detect PHY */
  160. #define PHY_DETECT_REG 1
  161. /*
  162. * Mask used to verify certain PHY features (or register contents)
  163. * in the register above:
  164. * 0x1000: 10Mbps full duplex support
  165. * 0x0800: 10Mbps half duplex support
  166. * 0x0008: Auto-negotiation support
  167. */
  168. #define PHY_DETECT_MASK 0x1808
  169. static inline int mdio_wait(struct axi_regs *regs)
  170. {
  171. u32 timeout = 200;
  172. /* Wait till MDIO interface is ready to accept a new transaction. */
  173. while (timeout && (!(readl(&regs->mdio_mcr)
  174. & XAE_MDIO_MCR_READY_MASK))) {
  175. timeout--;
  176. udelay(1);
  177. }
  178. if (!timeout) {
  179. printf("%s: Timeout\n", __func__);
  180. return 1;
  181. }
  182. return 0;
  183. }
  184. /**
  185. * axienet_dma_write - Memory mapped Axi DMA register Buffer Descriptor write.
  186. * @bd: pointer to BD descriptor structure
  187. * @desc: Address offset of DMA descriptors
  188. *
  189. * This function writes the value into the corresponding Axi DMA register.
  190. */
  191. static inline void axienet_dma_write(struct axidma_bd *bd, u32 *desc)
  192. {
  193. #if defined(CONFIG_PHYS_64BIT)
  194. writeq((unsigned long)bd, desc);
  195. #else
  196. writel((u32)bd, desc);
  197. #endif
  198. }
  199. static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
  200. u16 *val)
  201. {
  202. struct axi_regs *regs = priv->iobase;
  203. u32 mdioctrlreg = 0;
  204. if (mdio_wait(regs))
  205. return 1;
  206. mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
  207. XAE_MDIO_MCR_PHYAD_MASK) |
  208. ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
  209. & XAE_MDIO_MCR_REGAD_MASK) |
  210. XAE_MDIO_MCR_INITIATE_MASK |
  211. XAE_MDIO_MCR_OP_READ_MASK;
  212. writel(mdioctrlreg, &regs->mdio_mcr);
  213. if (mdio_wait(regs))
  214. return 1;
  215. /* Read data */
  216. *val = readl(&regs->mdio_mrd);
  217. return 0;
  218. }
  219. static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
  220. u32 data)
  221. {
  222. struct axi_regs *regs = priv->iobase;
  223. u32 mdioctrlreg = 0;
  224. if (mdio_wait(regs))
  225. return 1;
  226. mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
  227. XAE_MDIO_MCR_PHYAD_MASK) |
  228. ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
  229. & XAE_MDIO_MCR_REGAD_MASK) |
  230. XAE_MDIO_MCR_INITIATE_MASK |
  231. XAE_MDIO_MCR_OP_WRITE_MASK;
  232. /* Write data */
  233. writel(data, &regs->mdio_mwd);
  234. writel(mdioctrlreg, &regs->mdio_mcr);
  235. if (mdio_wait(regs))
  236. return 1;
  237. return 0;
  238. }
  239. static int axiemac_phy_init(struct udevice *dev)
  240. {
  241. u16 phyreg;
  242. int i;
  243. u32 ret;
  244. struct axidma_priv *priv = dev_get_priv(dev);
  245. struct axi_regs *regs = priv->iobase;
  246. struct phy_device *phydev;
  247. u32 supported = SUPPORTED_10baseT_Half |
  248. SUPPORTED_10baseT_Full |
  249. SUPPORTED_100baseT_Half |
  250. SUPPORTED_100baseT_Full |
  251. SUPPORTED_1000baseT_Half |
  252. SUPPORTED_1000baseT_Full;
  253. /* Set default MDIO divisor */
  254. writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
  255. if (IS_ENABLED(CONFIG_DM_ETH_PHY))
  256. priv->phyaddr = eth_phy_get_addr(dev);
  257. /*
  258. * Set address of PCS/PMA PHY to the one pointed by phy-handle for
  259. * backward compatibility.
  260. */
  261. if (priv->phyaddr != -1 && priv->pcsaddr == 0)
  262. priv->pcsaddr = priv->phyaddr;
  263. if (priv->phyaddr == -1) {
  264. /* Detect the PHY address */
  265. for (i = 31; i >= 0; i--) {
  266. ret = phyread(priv, i, PHY_DETECT_REG, &phyreg);
  267. if (!ret && (phyreg != 0xFFFF) &&
  268. ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
  269. /* Found a valid PHY address */
  270. priv->phyaddr = i;
  271. debug("axiemac: Found valid phy address, %x\n",
  272. i);
  273. break;
  274. }
  275. }
  276. }
  277. /* Interface - look at tsec */
  278. phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
  279. if (IS_ERR_OR_NULL(phydev)) {
  280. dev_err(dev, "phy_connect() failed\n");
  281. return -ENODEV;
  282. }
  283. phydev->supported &= supported;
  284. phydev->advertising = phydev->supported;
  285. priv->phydev = phydev;
  286. if (priv->phy_of_handle)
  287. priv->phydev->node = offset_to_ofnode(priv->phy_of_handle);
  288. phy_config(phydev);
  289. return 0;
  290. }
  291. static int pcs_pma_startup(struct axidma_priv *priv)
  292. {
  293. u32 rc, retry_cnt = 0;
  294. u16 mii_reg;
  295. rc = phyread(priv, priv->pcsaddr, MII_BMCR, &mii_reg);
  296. if (rc)
  297. goto failed_mdio;
  298. if (!(mii_reg & BMCR_ANENABLE)) {
  299. mii_reg |= BMCR_ANENABLE;
  300. if (phywrite(priv, priv->pcsaddr, MII_BMCR, mii_reg))
  301. goto failed_mdio;
  302. }
  303. /*
  304. * Check the internal PHY status and warn user if the link between it
  305. * and the external PHY is not obtained.
  306. */
  307. debug("axiemac: waiting for link status of the PCS/PMA PHY");
  308. while (retry_cnt * 10 < PHY_ANEG_TIMEOUT) {
  309. rc = phyread(priv, priv->pcsaddr, MII_BMSR, &mii_reg);
  310. if ((mii_reg & BMSR_LSTATUS) && mii_reg != 0xffff && !rc) {
  311. debug(".Done\n");
  312. return 0;
  313. }
  314. if ((retry_cnt++ % 10) == 0)
  315. debug(".");
  316. mdelay(10);
  317. }
  318. debug("\n");
  319. printf("axiemac: Warning, PCS/PMA PHY@%d is not ready, link is down\n",
  320. priv->pcsaddr);
  321. return 1;
  322. failed_mdio:
  323. printf("axiemac: MDIO to the PCS/PMA PHY has failed\n");
  324. return 1;
  325. }
  326. /* Setting axi emac and phy to proper setting */
  327. static int setup_phy(struct udevice *dev)
  328. {
  329. u16 temp;
  330. u32 speed, emmc_reg, ret;
  331. struct axidma_priv *priv = dev_get_priv(dev);
  332. struct axi_regs *regs = priv->iobase;
  333. struct phy_device *phydev = priv->phydev;
  334. if (priv->interface == PHY_INTERFACE_MODE_SGMII) {
  335. /*
  336. * In SGMII cases the isolate bit might set
  337. * after DMA and ethernet resets and hence
  338. * check and clear if set.
  339. */
  340. ret = phyread(priv, priv->pcsaddr, MII_BMCR, &temp);
  341. if (ret)
  342. return 0;
  343. if (temp & BMCR_ISOLATE) {
  344. temp &= ~BMCR_ISOLATE;
  345. ret = phywrite(priv, priv->pcsaddr, MII_BMCR, temp);
  346. if (ret)
  347. return 0;
  348. }
  349. }
  350. if (phy_startup(phydev)) {
  351. printf("axiemac: could not initialize PHY %s\n",
  352. phydev->dev->name);
  353. return 0;
  354. }
  355. if (priv->interface == PHY_INTERFACE_MODE_SGMII ||
  356. priv->interface == PHY_INTERFACE_MODE_1000BASEX) {
  357. if (pcs_pma_startup(priv))
  358. return 0;
  359. }
  360. if (!phydev->link) {
  361. printf("%s: No link.\n", phydev->dev->name);
  362. return 0;
  363. }
  364. switch (phydev->speed) {
  365. case 1000:
  366. speed = XAE_EMMC_LINKSPD_1000;
  367. break;
  368. case 100:
  369. speed = XAE_EMMC_LINKSPD_100;
  370. break;
  371. case 10:
  372. speed = XAE_EMMC_LINKSPD_10;
  373. break;
  374. default:
  375. return 0;
  376. }
  377. /* Setup the emac for the phy speed */
  378. emmc_reg = readl(&regs->emmc);
  379. emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
  380. emmc_reg |= speed;
  381. /* Write new speed setting out to Axi Ethernet */
  382. writel(emmc_reg, &regs->emmc);
  383. /*
  384. * Setting the operating speed of the MAC needs a delay. There
  385. * doesn't seem to be register to poll, so please consider this
  386. * during your application design.
  387. */
  388. udelay(1);
  389. return 1;
  390. }
  391. /* STOP DMA transfers */
  392. static void axiemac_stop(struct udevice *dev)
  393. {
  394. struct axidma_priv *priv = dev_get_priv(dev);
  395. u32 temp;
  396. /* Stop the hardware */
  397. temp = readl(&priv->dmatx->control);
  398. temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
  399. writel(temp, &priv->dmatx->control);
  400. temp = readl(&priv->dmarx->control);
  401. temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
  402. writel(temp, &priv->dmarx->control);
  403. debug("axiemac: Halted\n");
  404. }
  405. static int xxv_axi_ethernet_init(struct axidma_priv *priv)
  406. {
  407. struct xxv_axi_regs *regs = (struct xxv_axi_regs *)priv->iobase;
  408. writel(readl(&regs->rcw1) | XXV_RCW1_FCS_MASK, &regs->rcw1);
  409. writel(readl(&regs->tc) | XXV_TC_FCS_MASK, &regs->tc);
  410. writel(readl(&regs->tc) | XXV_TC_TX_MASK, &regs->tc);
  411. writel(readl(&regs->rcw1) | XXV_RCW1_RX_MASK, &regs->rcw1);
  412. return 0;
  413. }
  414. static int axi_ethernet_init(struct axidma_priv *priv)
  415. {
  416. struct axi_regs *regs = priv->iobase;
  417. int err;
  418. /*
  419. * Check the status of the MgtRdy bit in the interrupt status
  420. * registers. This must be done to allow the MGT clock to become stable
  421. * for the Sgmii and 1000BaseX PHY interfaces. No other register reads
  422. * will be valid until this bit is valid.
  423. * The bit is always a 1 for all other PHY interfaces.
  424. * Interrupt status and enable registers are not available in non
  425. * processor mode and hence bypass in this mode
  426. */
  427. if (!priv->eth_hasnobuf) {
  428. err = wait_for_bit_le32(&regs->is, XAE_INT_MGTRDY_MASK,
  429. true, 200, false);
  430. if (err) {
  431. printf("%s: Timeout\n", __func__);
  432. return 1;
  433. }
  434. /*
  435. * Stop the device and reset HW
  436. * Disable interrupts
  437. */
  438. writel(0, &regs->ie);
  439. }
  440. /* Disable the receiver */
  441. writel(readl(&regs->rcw1) & ~XAE_RCW1_RX_MASK, &regs->rcw1);
  442. /*
  443. * Stopping the receiver in mid-packet causes a dropped packet
  444. * indication from HW. Clear it.
  445. */
  446. if (!priv->eth_hasnobuf) {
  447. /* Set the interrupt status register to clear the interrupt */
  448. writel(XAE_INT_RXRJECT_MASK, &regs->is);
  449. }
  450. /* Setup HW */
  451. /* Set default MDIO divisor */
  452. writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
  453. debug("axiemac: InitHw done\n");
  454. return 0;
  455. }
  456. static int axiemac_write_hwaddr(struct udevice *dev)
  457. {
  458. struct eth_pdata *pdata = dev_get_plat(dev);
  459. struct axidma_priv *priv = dev_get_priv(dev);
  460. struct axi_regs *regs = priv->iobase;
  461. if (priv->mactype != EMAC_1G)
  462. return 0;
  463. /* Set the MAC address */
  464. int val = ((pdata->enetaddr[3] << 24) | (pdata->enetaddr[2] << 16) |
  465. (pdata->enetaddr[1] << 8) | (pdata->enetaddr[0]));
  466. writel(val, &regs->uaw0);
  467. val = (pdata->enetaddr[5] << 8) | pdata->enetaddr[4];
  468. val |= readl(&regs->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
  469. writel(val, &regs->uaw1);
  470. return 0;
  471. }
  472. /* Reset DMA engine */
  473. static void axi_dma_init(struct axidma_priv *priv)
  474. {
  475. u32 timeout = 500;
  476. /* Reset the engine so the hardware starts from a known state */
  477. writel(XAXIDMA_CR_RESET_MASK, &priv->dmatx->control);
  478. writel(XAXIDMA_CR_RESET_MASK, &priv->dmarx->control);
  479. /* At the initialization time, hardware should finish reset quickly */
  480. while (timeout--) {
  481. /* Check transmit/receive channel */
  482. /* Reset is done when the reset bit is low */
  483. if (!((readl(&priv->dmatx->control) |
  484. readl(&priv->dmarx->control))
  485. & XAXIDMA_CR_RESET_MASK)) {
  486. break;
  487. }
  488. }
  489. if (!timeout)
  490. printf("%s: Timeout\n", __func__);
  491. }
  492. static int axiemac_start(struct udevice *dev)
  493. {
  494. struct axidma_priv *priv = dev_get_priv(dev);
  495. u32 temp;
  496. debug("axiemac: Init started\n");
  497. /*
  498. * Initialize AXIDMA engine. AXIDMA engine must be initialized before
  499. * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is
  500. * reset, and since AXIDMA reset line is connected to AxiEthernet, this
  501. * would ensure a reset of AxiEthernet.
  502. */
  503. axi_dma_init(priv);
  504. /* Initialize AxiEthernet hardware. */
  505. if (priv->mactype == EMAC_1G) {
  506. if (axi_ethernet_init(priv))
  507. return -1;
  508. } else {
  509. if (xxv_axi_ethernet_init(priv))
  510. return -1;
  511. }
  512. /* Disable all RX interrupts before RxBD space setup */
  513. temp = readl(&priv->dmarx->control);
  514. temp &= ~XAXIDMA_IRQ_ALL_MASK;
  515. writel(temp, &priv->dmarx->control);
  516. /* Start DMA RX channel. Now it's ready to receive data.*/
  517. axienet_dma_write(&rx_bd, &priv->dmarx->current);
  518. /* Setup the BD. */
  519. memset(&rx_bd, 0, sizeof(rx_bd));
  520. rx_bd.next_desc = lower_32_bits((unsigned long)&rx_bd);
  521. rx_bd.buf_addr = lower_32_bits((unsigned long)&rxframe);
  522. #if defined(CONFIG_PHYS_64BIT)
  523. rx_bd.next_desc_msb = upper_32_bits((unsigned long)&rx_bd);
  524. rx_bd.buf_addr_msb = upper_32_bits((unsigned long)&rxframe);
  525. #endif
  526. rx_bd.cntrl = sizeof(rxframe);
  527. /* Flush the last BD so DMA core could see the updates */
  528. flush_cache((phys_addr_t)&rx_bd, sizeof(rx_bd));
  529. /* It is necessary to flush rxframe because if you don't do it
  530. * then cache can contain uninitialized data */
  531. flush_cache((phys_addr_t)&rxframe, sizeof(rxframe));
  532. /* Start the hardware */
  533. temp = readl(&priv->dmarx->control);
  534. temp |= XAXIDMA_CR_RUNSTOP_MASK;
  535. writel(temp, &priv->dmarx->control);
  536. /* Rx BD is ready - start */
  537. axienet_dma_write(&rx_bd, &priv->dmarx->tail);
  538. if (priv->mactype == EMAC_1G) {
  539. struct axi_regs *regs = priv->iobase;
  540. /* Enable TX */
  541. writel(XAE_TC_TX_MASK, &regs->tc);
  542. /* Enable RX */
  543. writel(XAE_RCW1_RX_MASK, &regs->rcw1);
  544. /* PHY setup */
  545. if (!setup_phy(dev)) {
  546. axiemac_stop(dev);
  547. return -1;
  548. }
  549. } else {
  550. struct xxv_axi_regs *regs = (struct xxv_axi_regs *)priv->iobase;
  551. /* Enable TX */
  552. writel(readl(&regs->tc) | XXV_TC_TX_MASK, &regs->tc);
  553. /* Enable RX */
  554. writel(readl(&regs->rcw1) | XXV_RCW1_RX_MASK, &regs->rcw1);
  555. }
  556. debug("axiemac: Init complete\n");
  557. return 0;
  558. }
  559. static int axiemac_send(struct udevice *dev, void *ptr, int len)
  560. {
  561. struct axidma_priv *priv = dev_get_priv(dev);
  562. u32 timeout;
  563. if (len > PKTSIZE_ALIGN)
  564. len = PKTSIZE_ALIGN;
  565. /* If size is less than min packet size, pad to min size */
  566. if (priv->mactype == EMAC_10G_25G && len < XXV_MIN_PKT_SIZE) {
  567. memset(txminframe, 0, XXV_MIN_PKT_SIZE);
  568. memcpy(txminframe, ptr, len);
  569. len = XXV_MIN_PKT_SIZE;
  570. ptr = txminframe;
  571. }
  572. /* Flush packet to main memory to be trasfered by DMA */
  573. flush_cache((phys_addr_t)ptr, len);
  574. /* Setup Tx BD */
  575. memset(&tx_bd, 0, sizeof(tx_bd));
  576. /* At the end of the ring, link the last BD back to the top */
  577. tx_bd.next_desc = lower_32_bits((unsigned long)&tx_bd);
  578. tx_bd.buf_addr = lower_32_bits((unsigned long)ptr);
  579. #if defined(CONFIG_PHYS_64BIT)
  580. tx_bd.next_desc_msb = upper_32_bits((unsigned long)&tx_bd);
  581. tx_bd.buf_addr_msb = upper_32_bits((unsigned long)ptr);
  582. #endif
  583. /* Save len */
  584. tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
  585. XAXIDMA_BD_CTRL_TXEOF_MASK;
  586. /* Flush the last BD so DMA core could see the updates */
  587. flush_cache((phys_addr_t)&tx_bd, sizeof(tx_bd));
  588. if (readl(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
  589. u32 temp;
  590. axienet_dma_write(&tx_bd, &priv->dmatx->current);
  591. /* Start the hardware */
  592. temp = readl(&priv->dmatx->control);
  593. temp |= XAXIDMA_CR_RUNSTOP_MASK;
  594. writel(temp, &priv->dmatx->control);
  595. }
  596. /* Start transfer */
  597. axienet_dma_write(&tx_bd, &priv->dmatx->tail);
  598. /* Wait for transmission to complete */
  599. debug("axiemac: Waiting for tx to be done\n");
  600. timeout = 200;
  601. while (timeout && (!(readl(&priv->dmatx->status) &
  602. (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) {
  603. timeout--;
  604. udelay(1);
  605. }
  606. if (!timeout) {
  607. printf("%s: Timeout\n", __func__);
  608. return 1;
  609. }
  610. debug("axiemac: Sending complete\n");
  611. return 0;
  612. }
  613. static int isrxready(struct axidma_priv *priv)
  614. {
  615. u32 status;
  616. /* Read pending interrupts */
  617. status = readl(&priv->dmarx->status);
  618. /* Acknowledge pending interrupts */
  619. writel(status & XAXIDMA_IRQ_ALL_MASK, &priv->dmarx->status);
  620. /*
  621. * If Reception done interrupt is asserted, call RX call back function
  622. * to handle the processed BDs and then raise the according flag.
  623. */
  624. if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
  625. return 1;
  626. return 0;
  627. }
  628. static int axiemac_recv(struct udevice *dev, int flags, uchar **packetp)
  629. {
  630. u32 length;
  631. struct axidma_priv *priv = dev_get_priv(dev);
  632. u32 temp;
  633. /* Wait for an incoming packet */
  634. if (!isrxready(priv))
  635. return -EAGAIN;
  636. debug("axiemac: RX data ready\n");
  637. /* Disable IRQ for a moment till packet is handled */
  638. temp = readl(&priv->dmarx->control);
  639. temp &= ~XAXIDMA_IRQ_ALL_MASK;
  640. writel(temp, &priv->dmarx->control);
  641. if (!priv->eth_hasnobuf && priv->mactype == EMAC_1G)
  642. length = rx_bd.app4 & 0xFFFF; /* max length mask */
  643. else
  644. length = rx_bd.status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
  645. #ifdef DEBUG
  646. print_buffer(&rxframe, &rxframe[0], 1, length, 16);
  647. #endif
  648. *packetp = rxframe;
  649. return length;
  650. }
  651. static int axiemac_free_pkt(struct udevice *dev, uchar *packet, int length)
  652. {
  653. struct axidma_priv *priv = dev_get_priv(dev);
  654. #ifdef DEBUG
  655. /* It is useful to clear buffer to be sure that it is consistent */
  656. memset(rxframe, 0, sizeof(rxframe));
  657. #endif
  658. /* Setup RxBD */
  659. /* Clear the whole buffer and setup it again - all flags are cleared */
  660. memset(&rx_bd, 0, sizeof(rx_bd));
  661. rx_bd.next_desc = lower_32_bits((unsigned long)&rx_bd);
  662. rx_bd.buf_addr = lower_32_bits((unsigned long)&rxframe);
  663. #if defined(CONFIG_PHYS_64BIT)
  664. rx_bd.next_desc_msb = upper_32_bits((unsigned long)&rx_bd);
  665. rx_bd.buf_addr_msb = upper_32_bits((unsigned long)&rxframe);
  666. #endif
  667. rx_bd.cntrl = sizeof(rxframe);
  668. /* Write bd to HW */
  669. flush_cache((phys_addr_t)&rx_bd, sizeof(rx_bd));
  670. /* It is necessary to flush rxframe because if you don't do it
  671. * then cache will contain previous packet */
  672. flush_cache((phys_addr_t)&rxframe, sizeof(rxframe));
  673. /* Rx BD is ready - start again */
  674. axienet_dma_write(&rx_bd, &priv->dmarx->tail);
  675. debug("axiemac: RX completed, framelength = %d\n", length);
  676. return 0;
  677. }
  678. static int axiemac_miiphy_read(struct mii_dev *bus, int addr,
  679. int devad, int reg)
  680. {
  681. int ret;
  682. u16 value;
  683. ret = phyread(bus->priv, addr, reg, &value);
  684. debug("axiemac: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg,
  685. value, ret);
  686. return value;
  687. }
  688. static int axiemac_miiphy_write(struct mii_dev *bus, int addr, int devad,
  689. int reg, u16 value)
  690. {
  691. debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
  692. return phywrite(bus->priv, addr, reg, value);
  693. }
  694. static int axi_emac_probe(struct udevice *dev)
  695. {
  696. struct axidma_plat *plat = dev_get_plat(dev);
  697. struct eth_pdata *pdata = &plat->eth_pdata;
  698. struct axidma_priv *priv = dev_get_priv(dev);
  699. int ret;
  700. priv->iobase = (struct axi_regs *)pdata->iobase;
  701. priv->dmatx = plat->dmatx;
  702. /* RX channel offset is 0x30 */
  703. priv->dmarx = (struct axidma_reg *)((phys_addr_t)priv->dmatx + 0x30);
  704. priv->mactype = plat->mactype;
  705. if (priv->mactype == EMAC_1G) {
  706. priv->eth_hasnobuf = plat->eth_hasnobuf;
  707. priv->pcsaddr = plat->pcsaddr;
  708. priv->phyaddr = plat->phyaddr;
  709. priv->phy_of_handle = plat->phy_of_handle;
  710. priv->interface = pdata->phy_interface;
  711. if (IS_ENABLED(CONFIG_DM_ETH_PHY))
  712. priv->bus = eth_phy_get_mdio_bus(dev);
  713. if (!priv->bus) {
  714. priv->bus = mdio_alloc();
  715. priv->bus->read = axiemac_miiphy_read;
  716. priv->bus->write = axiemac_miiphy_write;
  717. priv->bus->priv = priv;
  718. ret = mdio_register_seq(priv->bus, dev_seq(dev));
  719. if (ret)
  720. return ret;
  721. }
  722. if (IS_ENABLED(CONFIG_DM_ETH_PHY))
  723. eth_phy_set_mdio_bus(dev, priv->bus);
  724. axiemac_phy_init(dev);
  725. }
  726. printf("AXI EMAC: %lx, phyaddr %d, interface %s\n", (ulong)pdata->iobase,
  727. priv->phyaddr, phy_string_for_interface(pdata->phy_interface));
  728. return 0;
  729. }
  730. static int axi_emac_remove(struct udevice *dev)
  731. {
  732. struct axidma_priv *priv = dev_get_priv(dev);
  733. if (priv->mactype == EMAC_1G) {
  734. free(priv->phydev);
  735. mdio_unregister(priv->bus);
  736. mdio_free(priv->bus);
  737. }
  738. return 0;
  739. }
  740. static const struct eth_ops axi_emac_ops = {
  741. .start = axiemac_start,
  742. .send = axiemac_send,
  743. .recv = axiemac_recv,
  744. .free_pkt = axiemac_free_pkt,
  745. .stop = axiemac_stop,
  746. .write_hwaddr = axiemac_write_hwaddr,
  747. };
  748. static int axi_emac_of_to_plat(struct udevice *dev)
  749. {
  750. struct axidma_plat *plat = dev_get_plat(dev);
  751. struct eth_pdata *pdata = &plat->eth_pdata;
  752. int node = dev_of_offset(dev);
  753. int offset = 0;
  754. pdata->iobase = dev_read_addr(dev);
  755. plat->mactype = dev_get_driver_data(dev);
  756. offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
  757. "axistream-connected");
  758. if (offset <= 0) {
  759. printf("%s: axistream is not found\n", __func__);
  760. return -EINVAL;
  761. }
  762. plat->dmatx = (struct axidma_reg *)fdtdec_get_addr_size_auto_parent
  763. (gd->fdt_blob, 0, offset, "reg", 0, NULL, false);
  764. if (!plat->dmatx) {
  765. printf("%s: axi_dma register space not found\n", __func__);
  766. return -EINVAL;
  767. }
  768. if (plat->mactype == EMAC_1G) {
  769. plat->phyaddr = -1;
  770. /* PHYAD 0 always redirects to the PCS/PMA PHY */
  771. plat->pcsaddr = 0;
  772. offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
  773. "phy-handle");
  774. if (offset > 0) {
  775. if (!(IS_ENABLED(CONFIG_DM_ETH_PHY)))
  776. plat->phyaddr = fdtdec_get_int(gd->fdt_blob,
  777. offset,
  778. "reg", -1);
  779. plat->phy_of_handle = offset;
  780. }
  781. pdata->phy_interface = dev_read_phy_mode(dev);
  782. if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
  783. return -EINVAL;
  784. plat->eth_hasnobuf = fdtdec_get_bool(gd->fdt_blob, node,
  785. "xlnx,eth-hasnobuf");
  786. if (pdata->phy_interface == PHY_INTERFACE_MODE_SGMII ||
  787. pdata->phy_interface == PHY_INTERFACE_MODE_1000BASEX) {
  788. offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
  789. "pcs-handle");
  790. if (offset > 0) {
  791. plat->pcsaddr = fdtdec_get_int(gd->fdt_blob,
  792. offset, "reg", -1);
  793. }
  794. }
  795. }
  796. return 0;
  797. }
  798. static const struct udevice_id axi_emac_ids[] = {
  799. { .compatible = "xlnx,axi-ethernet-1.00.a", .data = (uintptr_t)EMAC_1G },
  800. { .compatible = "xlnx,xxv-ethernet-1.0", .data = (uintptr_t)EMAC_10G_25G },
  801. { }
  802. };
  803. U_BOOT_DRIVER(axi_emac) = {
  804. .name = "axi_emac",
  805. .id = UCLASS_ETH,
  806. .of_match = axi_emac_ids,
  807. .of_to_plat = axi_emac_of_to_plat,
  808. .probe = axi_emac_probe,
  809. .remove = axi_emac_remove,
  810. .ops = &axi_emac_ops,
  811. .priv_auto = sizeof(struct axidma_priv),
  812. .plat_auto = sizeof(struct axidma_plat),
  813. };