xilinx_axi_emac.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. #if !defined(CONFIG_PHYLIB)
  18. # error AXI_ETHERNET requires PHYLIB
  19. #endif
  20. /* Link setup */
  21. #define XAE_EMMC_LINKSPEED_MASK 0xC0000000 /* Link speed */
  22. #define XAE_EMMC_LINKSPD_10 0x00000000 /* Link Speed mask for 10 Mbit */
  23. #define XAE_EMMC_LINKSPD_100 0x40000000 /* Link Speed mask for 100 Mbit */
  24. #define XAE_EMMC_LINKSPD_1000 0x80000000 /* Link Speed mask for 1000 Mbit */
  25. /* Interrupt Status/Enable/Mask Registers bit definitions */
  26. #define XAE_INT_RXRJECT_MASK 0x00000008 /* Rx frame rejected */
  27. #define XAE_INT_MGTRDY_MASK 0x00000080 /* MGT clock Lock */
  28. /* Receive Configuration Word 1 (RCW1) Register bit definitions */
  29. #define XAE_RCW1_RX_MASK 0x10000000 /* Receiver enable */
  30. /* Transmitter Configuration (TC) Register bit definitions */
  31. #define XAE_TC_TX_MASK 0x10000000 /* Transmitter enable */
  32. #define XAE_UAW1_UNICASTADDR_MASK 0x0000FFFF
  33. /* MDIO Management Configuration (MC) Register bit definitions */
  34. #define XAE_MDIO_MC_MDIOEN_MASK 0x00000040 /* MII management enable*/
  35. /* MDIO Management Control Register (MCR) Register bit definitions */
  36. #define XAE_MDIO_MCR_PHYAD_MASK 0x1F000000 /* Phy Address Mask */
  37. #define XAE_MDIO_MCR_PHYAD_SHIFT 24 /* Phy Address Shift */
  38. #define XAE_MDIO_MCR_REGAD_MASK 0x001F0000 /* Reg Address Mask */
  39. #define XAE_MDIO_MCR_REGAD_SHIFT 16 /* Reg Address Shift */
  40. #define XAE_MDIO_MCR_OP_READ_MASK 0x00008000 /* Op Code Read Mask */
  41. #define XAE_MDIO_MCR_OP_WRITE_MASK 0x00004000 /* Op Code Write Mask */
  42. #define XAE_MDIO_MCR_INITIATE_MASK 0x00000800 /* Ready Mask */
  43. #define XAE_MDIO_MCR_READY_MASK 0x00000080 /* Ready Mask */
  44. #define XAE_MDIO_DIV_DFT 29 /* Default MDIO clock divisor */
  45. /* DMA macros */
  46. /* Bitmasks of XAXIDMA_CR_OFFSET register */
  47. #define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */
  48. #define XAXIDMA_CR_RESET_MASK 0x00000004 /* Reset DMA engine */
  49. /* Bitmasks of XAXIDMA_SR_OFFSET register */
  50. #define XAXIDMA_HALTED_MASK 0x00000001 /* DMA channel halted */
  51. /* Bitmask for interrupts */
  52. #define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */
  53. #define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */
  54. #define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */
  55. /* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */
  56. #define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */
  57. #define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */
  58. #define DMAALIGN 128
  59. static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN)));
  60. /* Reflect dma offsets */
  61. struct axidma_reg {
  62. u32 control; /* DMACR */
  63. u32 status; /* DMASR */
  64. u32 current; /* CURDESC */
  65. u32 reserved;
  66. u32 tail; /* TAILDESC */
  67. };
  68. /* Private driver structures */
  69. struct axidma_priv {
  70. struct axidma_reg *dmatx;
  71. struct axidma_reg *dmarx;
  72. int phyaddr;
  73. struct axi_regs *iobase;
  74. phy_interface_t interface;
  75. struct phy_device *phydev;
  76. struct mii_dev *bus;
  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 && (!(in_be32(&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. static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
  145. u16 *val)
  146. {
  147. struct axi_regs *regs = priv->iobase;
  148. u32 mdioctrlreg = 0;
  149. if (mdio_wait(regs))
  150. return 1;
  151. mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
  152. XAE_MDIO_MCR_PHYAD_MASK) |
  153. ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
  154. & XAE_MDIO_MCR_REGAD_MASK) |
  155. XAE_MDIO_MCR_INITIATE_MASK |
  156. XAE_MDIO_MCR_OP_READ_MASK;
  157. out_be32(&regs->mdio_mcr, mdioctrlreg);
  158. if (mdio_wait(regs))
  159. return 1;
  160. /* Read data */
  161. *val = in_be32(&regs->mdio_mrd);
  162. return 0;
  163. }
  164. static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
  165. u32 data)
  166. {
  167. struct axi_regs *regs = priv->iobase;
  168. u32 mdioctrlreg = 0;
  169. if (mdio_wait(regs))
  170. return 1;
  171. mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
  172. XAE_MDIO_MCR_PHYAD_MASK) |
  173. ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
  174. & XAE_MDIO_MCR_REGAD_MASK) |
  175. XAE_MDIO_MCR_INITIATE_MASK |
  176. XAE_MDIO_MCR_OP_WRITE_MASK;
  177. /* Write data */
  178. out_be32(&regs->mdio_mwd, data);
  179. out_be32(&regs->mdio_mcr, mdioctrlreg);
  180. if (mdio_wait(regs))
  181. return 1;
  182. return 0;
  183. }
  184. /* Setting axi emac and phy to proper setting */
  185. static int setup_phy(struct udevice *dev)
  186. {
  187. u16 phyreg;
  188. u32 i, speed, emmc_reg, ret;
  189. struct axidma_priv *priv = dev_get_priv(dev);
  190. struct axi_regs *regs = priv->iobase;
  191. struct phy_device *phydev;
  192. u32 supported = SUPPORTED_10baseT_Half |
  193. SUPPORTED_10baseT_Full |
  194. SUPPORTED_100baseT_Half |
  195. SUPPORTED_100baseT_Full |
  196. SUPPORTED_1000baseT_Half |
  197. SUPPORTED_1000baseT_Full;
  198. if (priv->phyaddr == -1) {
  199. /* Detect the PHY address */
  200. for (i = 31; i >= 0; i--) {
  201. ret = phyread(priv, i, PHY_DETECT_REG, &phyreg);
  202. if (!ret && (phyreg != 0xFFFF) &&
  203. ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
  204. /* Found a valid PHY address */
  205. priv->phyaddr = i;
  206. debug("axiemac: Found valid phy address, %x\n",
  207. i);
  208. break;
  209. }
  210. }
  211. }
  212. /* Interface - look at tsec */
  213. phydev = phy_connect(priv->bus, priv->phyaddr, dev, 0);
  214. phydev->supported &= supported;
  215. phydev->advertising = phydev->supported;
  216. priv->phydev = phydev;
  217. phy_config(phydev);
  218. if (phy_startup(phydev)) {
  219. printf("axiemac: could not initialize PHY %s\n",
  220. phydev->dev->name);
  221. return 0;
  222. }
  223. if (!phydev->link) {
  224. printf("%s: No link.\n", phydev->dev->name);
  225. return 0;
  226. }
  227. switch (phydev->speed) {
  228. case 1000:
  229. speed = XAE_EMMC_LINKSPD_1000;
  230. break;
  231. case 100:
  232. speed = XAE_EMMC_LINKSPD_100;
  233. break;
  234. case 10:
  235. speed = XAE_EMMC_LINKSPD_10;
  236. break;
  237. default:
  238. return 0;
  239. }
  240. /* Setup the emac for the phy speed */
  241. emmc_reg = in_be32(&regs->emmc);
  242. emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
  243. emmc_reg |= speed;
  244. /* Write new speed setting out to Axi Ethernet */
  245. out_be32(&regs->emmc, emmc_reg);
  246. /*
  247. * Setting the operating speed of the MAC needs a delay. There
  248. * doesn't seem to be register to poll, so please consider this
  249. * during your application design.
  250. */
  251. udelay(1);
  252. return 1;
  253. }
  254. /* STOP DMA transfers */
  255. static void axiemac_halt(struct udevice *dev)
  256. {
  257. struct axidma_priv *priv = dev_get_priv(dev);
  258. u32 temp;
  259. /* Stop the hardware */
  260. temp = in_be32(&priv->dmatx->control);
  261. temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
  262. out_be32(&priv->dmatx->control, temp);
  263. temp = in_be32(&priv->dmarx->control);
  264. temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
  265. out_be32(&priv->dmarx->control, temp);
  266. debug("axiemac: Halted\n");
  267. }
  268. static int axi_ethernet_init(struct axidma_priv *priv)
  269. {
  270. struct axi_regs *regs = priv->iobase;
  271. u32 timeout = 200;
  272. /*
  273. * Check the status of the MgtRdy bit in the interrupt status
  274. * registers. This must be done to allow the MGT clock to become stable
  275. * for the Sgmii and 1000BaseX PHY interfaces. No other register reads
  276. * will be valid until this bit is valid.
  277. * The bit is always a 1 for all other PHY interfaces.
  278. */
  279. while (timeout && (!(in_be32(&regs->is) & XAE_INT_MGTRDY_MASK))) {
  280. timeout--;
  281. udelay(1);
  282. }
  283. if (!timeout) {
  284. printf("%s: Timeout\n", __func__);
  285. return 1;
  286. }
  287. /* Stop the device and reset HW */
  288. /* Disable interrupts */
  289. out_be32(&regs->ie, 0);
  290. /* Disable the receiver */
  291. out_be32(&regs->rcw1, in_be32(&regs->rcw1) & ~XAE_RCW1_RX_MASK);
  292. /*
  293. * Stopping the receiver in mid-packet causes a dropped packet
  294. * indication from HW. Clear it.
  295. */
  296. /* Set the interrupt status register to clear the interrupt */
  297. out_be32(&regs->is, XAE_INT_RXRJECT_MASK);
  298. /* Setup HW */
  299. /* Set default MDIO divisor */
  300. out_be32(&regs->mdio_mc, XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK);
  301. debug("axiemac: InitHw done\n");
  302. return 0;
  303. }
  304. static int axiemac_setup_mac(struct udevice *dev)
  305. {
  306. struct eth_pdata *pdata = dev_get_platdata(dev);
  307. struct axidma_priv *priv = dev_get_priv(dev);
  308. struct axi_regs *regs = priv->iobase;
  309. /* Set the MAC address */
  310. int val = ((pdata->enetaddr[3] << 24) | (pdata->enetaddr[2] << 16) |
  311. (pdata->enetaddr[1] << 8) | (pdata->enetaddr[0]));
  312. out_be32(&regs->uaw0, val);
  313. val = (pdata->enetaddr[5] << 8) | pdata->enetaddr[4];
  314. val |= in_be32(&regs->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
  315. out_be32(&regs->uaw1, val);
  316. return 0;
  317. }
  318. /* Reset DMA engine */
  319. static void axi_dma_init(struct axidma_priv *priv)
  320. {
  321. u32 timeout = 500;
  322. /* Reset the engine so the hardware starts from a known state */
  323. out_be32(&priv->dmatx->control, XAXIDMA_CR_RESET_MASK);
  324. out_be32(&priv->dmarx->control, XAXIDMA_CR_RESET_MASK);
  325. /* At the initialization time, hardware should finish reset quickly */
  326. while (timeout--) {
  327. /* Check transmit/receive channel */
  328. /* Reset is done when the reset bit is low */
  329. if (!((in_be32(&priv->dmatx->control) |
  330. in_be32(&priv->dmarx->control))
  331. & XAXIDMA_CR_RESET_MASK)) {
  332. break;
  333. }
  334. }
  335. if (!timeout)
  336. printf("%s: Timeout\n", __func__);
  337. }
  338. static int axiemac_init(struct udevice *dev)
  339. {
  340. struct axidma_priv *priv = dev_get_priv(dev);
  341. struct axi_regs *regs = priv->iobase;
  342. u32 temp;
  343. debug("axiemac: Init started\n");
  344. /*
  345. * Initialize AXIDMA engine. AXIDMA engine must be initialized before
  346. * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is
  347. * reset, and since AXIDMA reset line is connected to AxiEthernet, this
  348. * would ensure a reset of AxiEthernet.
  349. */
  350. axi_dma_init(priv);
  351. /* Initialize AxiEthernet hardware. */
  352. if (axi_ethernet_init(priv))
  353. return -1;
  354. /* Disable all RX interrupts before RxBD space setup */
  355. temp = in_be32(&priv->dmarx->control);
  356. temp &= ~XAXIDMA_IRQ_ALL_MASK;
  357. out_be32(&priv->dmarx->control, temp);
  358. /* Start DMA RX channel. Now it's ready to receive data.*/
  359. out_be32(&priv->dmarx->current, (u32)&rx_bd);
  360. /* Setup the BD. */
  361. memset(&rx_bd, 0, sizeof(rx_bd));
  362. rx_bd.next = (u32)&rx_bd;
  363. rx_bd.phys = (u32)&rxframe;
  364. rx_bd.cntrl = sizeof(rxframe);
  365. /* Flush the last BD so DMA core could see the updates */
  366. flush_cache((u32)&rx_bd, sizeof(rx_bd));
  367. /* It is necessary to flush rxframe because if you don't do it
  368. * then cache can contain uninitialized data */
  369. flush_cache((u32)&rxframe, sizeof(rxframe));
  370. /* Start the hardware */
  371. temp = in_be32(&priv->dmarx->control);
  372. temp |= XAXIDMA_CR_RUNSTOP_MASK;
  373. out_be32(&priv->dmarx->control, temp);
  374. /* Rx BD is ready - start */
  375. out_be32(&priv->dmarx->tail, (u32)&rx_bd);
  376. /* Enable TX */
  377. out_be32(&regs->tc, XAE_TC_TX_MASK);
  378. /* Enable RX */
  379. out_be32(&regs->rcw1, XAE_RCW1_RX_MASK);
  380. /* PHY setup */
  381. if (!setup_phy(dev)) {
  382. axiemac_halt(dev);
  383. return -1;
  384. }
  385. debug("axiemac: Init complete\n");
  386. return 0;
  387. }
  388. static int axiemac_send(struct udevice *dev, void *ptr, int len)
  389. {
  390. struct axidma_priv *priv = dev_get_priv(dev);
  391. u32 timeout;
  392. if (len > PKTSIZE_ALIGN)
  393. len = PKTSIZE_ALIGN;
  394. /* Flush packet to main memory to be trasfered by DMA */
  395. flush_cache((u32)ptr, len);
  396. /* Setup Tx BD */
  397. memset(&tx_bd, 0, sizeof(tx_bd));
  398. /* At the end of the ring, link the last BD back to the top */
  399. tx_bd.next = (u32)&tx_bd;
  400. tx_bd.phys = (u32)ptr;
  401. /* Save len */
  402. tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
  403. XAXIDMA_BD_CTRL_TXEOF_MASK;
  404. /* Flush the last BD so DMA core could see the updates */
  405. flush_cache((u32)&tx_bd, sizeof(tx_bd));
  406. if (in_be32(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
  407. u32 temp;
  408. out_be32(&priv->dmatx->current, (u32)&tx_bd);
  409. /* Start the hardware */
  410. temp = in_be32(&priv->dmatx->control);
  411. temp |= XAXIDMA_CR_RUNSTOP_MASK;
  412. out_be32(&priv->dmatx->control, temp);
  413. }
  414. /* Start transfer */
  415. out_be32(&priv->dmatx->tail, (u32)&tx_bd);
  416. /* Wait for transmission to complete */
  417. debug("axiemac: Waiting for tx to be done\n");
  418. timeout = 200;
  419. while (timeout && (!(in_be32(&priv->dmatx->status) &
  420. (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) {
  421. timeout--;
  422. udelay(1);
  423. }
  424. if (!timeout) {
  425. printf("%s: Timeout\n", __func__);
  426. return 1;
  427. }
  428. debug("axiemac: Sending complete\n");
  429. return 0;
  430. }
  431. static int isrxready(struct axidma_priv *priv)
  432. {
  433. u32 status;
  434. /* Read pending interrupts */
  435. status = in_be32(&priv->dmarx->status);
  436. /* Acknowledge pending interrupts */
  437. out_be32(&priv->dmarx->status, status & XAXIDMA_IRQ_ALL_MASK);
  438. /*
  439. * If Reception done interrupt is asserted, call RX call back function
  440. * to handle the processed BDs and then raise the according flag.
  441. */
  442. if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
  443. return 1;
  444. return 0;
  445. }
  446. static int axiemac_recv(struct udevice *dev, int flags, uchar **packetp)
  447. {
  448. u32 length;
  449. struct axidma_priv *priv = dev_get_priv(dev);
  450. u32 temp;
  451. /* Wait for an incoming packet */
  452. if (!isrxready(priv))
  453. return -1;
  454. debug("axiemac: RX data ready\n");
  455. /* Disable IRQ for a moment till packet is handled */
  456. temp = in_be32(&priv->dmarx->control);
  457. temp &= ~XAXIDMA_IRQ_ALL_MASK;
  458. out_be32(&priv->dmarx->control, temp);
  459. length = rx_bd.app4 & 0xFFFF; /* max length mask */
  460. #ifdef DEBUG
  461. print_buffer(&rxframe, &rxframe[0], 1, length, 16);
  462. #endif
  463. /* Pass the received frame up for processing */
  464. if (length)
  465. net_process_received_packet(rxframe, length);
  466. #ifdef DEBUG
  467. /* It is useful to clear buffer to be sure that it is consistent */
  468. memset(rxframe, 0, sizeof(rxframe));
  469. #endif
  470. /* Setup RxBD */
  471. /* Clear the whole buffer and setup it again - all flags are cleared */
  472. memset(&rx_bd, 0, sizeof(rx_bd));
  473. rx_bd.next = (u32)&rx_bd;
  474. rx_bd.phys = (u32)&rxframe;
  475. rx_bd.cntrl = sizeof(rxframe);
  476. /* Write bd to HW */
  477. flush_cache((u32)&rx_bd, sizeof(rx_bd));
  478. /* It is necessary to flush rxframe because if you don't do it
  479. * then cache will contain previous packet */
  480. flush_cache((u32)&rxframe, sizeof(rxframe));
  481. /* Rx BD is ready - start again */
  482. out_be32(&priv->dmarx->tail, (u32)&rx_bd);
  483. debug("axiemac: RX completed, framelength = %d\n", length);
  484. return 0;
  485. }
  486. static int axiemac_miiphy_read(struct mii_dev *bus, int addr,
  487. int devad, int reg)
  488. {
  489. int ret;
  490. u16 value;
  491. ret = phyread(bus->priv, addr, reg, &value);
  492. debug("axiemac: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg,
  493. value, ret);
  494. return value;
  495. }
  496. static int axiemac_miiphy_write(struct mii_dev *bus, int addr, int devad,
  497. int reg, u16 value)
  498. {
  499. debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
  500. return phywrite(bus->priv, addr, reg, value);
  501. }
  502. static int axi_emac_probe(struct udevice *dev)
  503. {
  504. struct axidma_priv *priv = dev_get_priv(dev);
  505. int ret;
  506. priv->bus = mdio_alloc();
  507. priv->bus->read = axiemac_miiphy_read;
  508. priv->bus->write = axiemac_miiphy_write;
  509. priv->bus->priv = priv;
  510. strcpy(priv->bus->name, "axi_emac");
  511. ret = mdio_register(priv->bus);
  512. if (ret)
  513. return ret;
  514. return 0;
  515. }
  516. static int axi_emac_remove(struct udevice *dev)
  517. {
  518. struct axidma_priv *priv = dev_get_priv(dev);
  519. free(priv->phydev);
  520. mdio_unregister(priv->bus);
  521. mdio_free(priv->bus);
  522. return 0;
  523. }
  524. static const struct eth_ops axi_emac_ops = {
  525. .start = axiemac_init,
  526. .send = axiemac_send,
  527. .recv = axiemac_recv,
  528. .stop = axiemac_halt,
  529. .write_hwaddr = axiemac_setup_mac,
  530. };
  531. static int axi_emac_ofdata_to_platdata(struct udevice *dev)
  532. {
  533. struct eth_pdata *pdata = dev_get_platdata(dev);
  534. struct axidma_priv *priv = dev_get_priv(dev);
  535. int offset = 0;
  536. const char *phy_mode;
  537. pdata->iobase = (phys_addr_t)dev_get_addr(dev);
  538. priv->iobase = (struct axi_regs *)pdata->iobase;
  539. offset = fdtdec_lookup_phandle(gd->fdt_blob, dev->of_offset,
  540. "axistream-connected");
  541. if (offset <= 0) {
  542. printf("%s: axistream is not found\n", __func__);
  543. return -EINVAL;
  544. }
  545. priv->dmatx = (struct axidma_reg *)fdtdec_get_int(gd->fdt_blob,
  546. offset, "reg", 0);
  547. if (!priv->dmatx) {
  548. printf("%s: axi_dma register space not found\n", __func__);
  549. return -EINVAL;
  550. }
  551. /* RX channel offset is 0x30 */
  552. priv->dmarx = (struct axidma_reg *)((u32)priv->dmatx + 0x30);
  553. priv->phyaddr = -1;
  554. offset = fdtdec_lookup_phandle(gd->fdt_blob, dev->of_offset,
  555. "phy-handle");
  556. if (offset > 0)
  557. priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
  558. phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
  559. if (phy_mode)
  560. pdata->phy_interface = phy_get_interface_by_name(phy_mode);
  561. if (pdata->phy_interface == -1) {
  562. debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
  563. return -EINVAL;
  564. }
  565. priv->interface = pdata->phy_interface;
  566. printf("AXI EMAC: %lx, phyaddr %d, interface %s\n", (ulong)priv->iobase,
  567. priv->phyaddr, phy_string_for_interface(priv->interface));
  568. return 0;
  569. }
  570. static const struct udevice_id axi_emac_ids[] = {
  571. { .compatible = "xlnx,axi-ethernet-1.00.a" },
  572. { }
  573. };
  574. U_BOOT_DRIVER(axi_emac) = {
  575. .name = "axi_emac",
  576. .id = UCLASS_ETH,
  577. .of_match = axi_emac_ids,
  578. .ofdata_to_platdata = axi_emac_ofdata_to_platdata,
  579. .probe = axi_emac_probe,
  580. .remove = axi_emac_remove,
  581. .ops = &axi_emac_ops,
  582. .priv_auto_alloc_size = sizeof(struct axidma_priv),
  583. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  584. };