xilinx_axi_emac.c 20 KB

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