xilinx_axi_emac.c 19 KB

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