xilinx_axi_emac.c 19 KB

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