xilinx_axi_emac.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 <net.h>
  11. #include <malloc.h>
  12. #include <asm/io.h>
  13. #include <phy.h>
  14. #include <miiphy.h>
  15. #if !defined(CONFIG_PHYLIB)
  16. # error AXI_ETHERNET requires PHYLIB
  17. #endif
  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 phy_device *phydev;
  72. struct mii_dev *bus;
  73. };
  74. /* BD descriptors */
  75. struct axidma_bd {
  76. u32 next; /* Next descriptor pointer */
  77. u32 reserved1;
  78. u32 phys; /* Buffer address */
  79. u32 reserved2;
  80. u32 reserved3;
  81. u32 reserved4;
  82. u32 cntrl; /* Control */
  83. u32 status; /* Status */
  84. u32 app0;
  85. u32 app1; /* TX start << 16 | insert */
  86. u32 app2; /* TX csum seed */
  87. u32 app3;
  88. u32 app4;
  89. u32 sw_id_offset;
  90. u32 reserved5;
  91. u32 reserved6;
  92. };
  93. /* Static BDs - driver uses only one BD */
  94. static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN)));
  95. static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN)));
  96. struct axi_regs {
  97. u32 reserved[3];
  98. u32 is; /* 0xC: Interrupt status */
  99. u32 reserved2;
  100. u32 ie; /* 0x14: Interrupt enable */
  101. u32 reserved3[251];
  102. u32 rcw1; /* 0x404: Rx Configuration Word 1 */
  103. u32 tc; /* 0x408: Tx Configuration */
  104. u32 reserved4;
  105. u32 emmc; /* 0x410: EMAC mode configuration */
  106. u32 reserved5[59];
  107. u32 mdio_mc; /* 0x500: MII Management Config */
  108. u32 mdio_mcr; /* 0x504: MII Management Control */
  109. u32 mdio_mwd; /* 0x508: MII Management Write Data */
  110. u32 mdio_mrd; /* 0x50C: MII Management Read Data */
  111. u32 reserved6[124];
  112. u32 uaw0; /* 0x700: Unicast address word 0 */
  113. u32 uaw1; /* 0x704: Unicast address word 1 */
  114. };
  115. /* Use MII register 1 (MII status register) to detect PHY */
  116. #define PHY_DETECT_REG 1
  117. /*
  118. * Mask used to verify certain PHY features (or register contents)
  119. * in the register above:
  120. * 0x1000: 10Mbps full duplex support
  121. * 0x0800: 10Mbps half duplex support
  122. * 0x0008: Auto-negotiation support
  123. */
  124. #define PHY_DETECT_MASK 0x1808
  125. static inline int mdio_wait(struct eth_device *dev)
  126. {
  127. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  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 eth_device *dev, u32 phyaddress, u32 registernum,
  142. u16 *val)
  143. {
  144. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  145. u32 mdioctrlreg = 0;
  146. if (mdio_wait(dev))
  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(dev))
  156. return 1;
  157. /* Read data */
  158. *val = in_be32(&regs->mdio_mrd);
  159. return 0;
  160. }
  161. static u32 phywrite(struct eth_device *dev, u32 phyaddress, u32 registernum,
  162. u32 data)
  163. {
  164. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  165. u32 mdioctrlreg = 0;
  166. if (mdio_wait(dev))
  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(dev))
  178. return 1;
  179. return 0;
  180. }
  181. /* Setting axi emac and phy to proper setting */
  182. static int setup_phy(struct eth_device *dev)
  183. {
  184. u16 phyreg;
  185. u32 i, speed, emmc_reg, ret;
  186. struct axidma_priv *priv = dev->priv;
  187. struct axi_regs *regs = (struct axi_regs *)dev->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. if (priv->phyaddr == -1) {
  196. /* Detect the PHY address */
  197. for (i = 31; i >= 0; i--) {
  198. ret = phyread(dev, i, PHY_DETECT_REG, &phyreg);
  199. if (!ret && (phyreg != 0xFFFF) &&
  200. ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
  201. /* Found a valid PHY address */
  202. priv->phyaddr = i;
  203. debug("axiemac: Found valid phy address, %x\n",
  204. phyreg);
  205. break;
  206. }
  207. }
  208. }
  209. /* Interface - look at tsec */
  210. phydev = phy_connect(priv->bus, priv->phyaddr, dev, 0);
  211. phydev->supported &= supported;
  212. phydev->advertising = phydev->supported;
  213. priv->phydev = phydev;
  214. phy_config(phydev);
  215. if (phy_startup(phydev)) {
  216. printf("axiemac: could not initialize PHY %s\n",
  217. phydev->dev->name);
  218. return 0;
  219. }
  220. if (!phydev->link) {
  221. printf("%s: No link.\n", phydev->dev->name);
  222. return 0;
  223. }
  224. switch (phydev->speed) {
  225. case 1000:
  226. speed = XAE_EMMC_LINKSPD_1000;
  227. break;
  228. case 100:
  229. speed = XAE_EMMC_LINKSPD_100;
  230. break;
  231. case 10:
  232. speed = XAE_EMMC_LINKSPD_10;
  233. break;
  234. default:
  235. return 0;
  236. }
  237. /* Setup the emac for the phy speed */
  238. emmc_reg = in_be32(&regs->emmc);
  239. emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
  240. emmc_reg |= speed;
  241. /* Write new speed setting out to Axi Ethernet */
  242. out_be32(&regs->emmc, emmc_reg);
  243. /*
  244. * Setting the operating speed of the MAC needs a delay. There
  245. * doesn't seem to be register to poll, so please consider this
  246. * during your application design.
  247. */
  248. udelay(1);
  249. return 1;
  250. }
  251. /* STOP DMA transfers */
  252. static void axiemac_halt(struct eth_device *dev)
  253. {
  254. struct axidma_priv *priv = dev->priv;
  255. u32 temp;
  256. /* Stop the hardware */
  257. temp = in_be32(&priv->dmatx->control);
  258. temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
  259. out_be32(&priv->dmatx->control, temp);
  260. temp = in_be32(&priv->dmarx->control);
  261. temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
  262. out_be32(&priv->dmarx->control, temp);
  263. debug("axiemac: Halted\n");
  264. }
  265. static int axi_ethernet_init(struct eth_device *dev)
  266. {
  267. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  268. u32 timeout = 200;
  269. /*
  270. * Check the status of the MgtRdy bit in the interrupt status
  271. * registers. This must be done to allow the MGT clock to become stable
  272. * for the Sgmii and 1000BaseX PHY interfaces. No other register reads
  273. * will be valid until this bit is valid.
  274. * The bit is always a 1 for all other PHY interfaces.
  275. */
  276. while (timeout && (!(in_be32(&regs->is) & XAE_INT_MGTRDY_MASK))) {
  277. timeout--;
  278. udelay(1);
  279. }
  280. if (!timeout) {
  281. printf("%s: Timeout\n", __func__);
  282. return 1;
  283. }
  284. /* Stop the device and reset HW */
  285. /* Disable interrupts */
  286. out_be32(&regs->ie, 0);
  287. /* Disable the receiver */
  288. out_be32(&regs->rcw1, in_be32(&regs->rcw1) & ~XAE_RCW1_RX_MASK);
  289. /*
  290. * Stopping the receiver in mid-packet causes a dropped packet
  291. * indication from HW. Clear it.
  292. */
  293. /* Set the interrupt status register to clear the interrupt */
  294. out_be32(&regs->is, XAE_INT_RXRJECT_MASK);
  295. /* Setup HW */
  296. /* Set default MDIO divisor */
  297. out_be32(&regs->mdio_mc, XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK);
  298. debug("axiemac: InitHw done\n");
  299. return 0;
  300. }
  301. static int axiemac_setup_mac(struct eth_device *dev)
  302. {
  303. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  304. /* Set the MAC address */
  305. int val = ((dev->enetaddr[3] << 24) | (dev->enetaddr[2] << 16) |
  306. (dev->enetaddr[1] << 8) | (dev->enetaddr[0]));
  307. out_be32(&regs->uaw0, val);
  308. val = (dev->enetaddr[5] << 8) | dev->enetaddr[4] ;
  309. val |= in_be32(&regs->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
  310. out_be32(&regs->uaw1, val);
  311. return 0;
  312. }
  313. /* Reset DMA engine */
  314. static void axi_dma_init(struct eth_device *dev)
  315. {
  316. struct axidma_priv *priv = dev->priv;
  317. u32 timeout = 500;
  318. /* Reset the engine so the hardware starts from a known state */
  319. out_be32(&priv->dmatx->control, XAXIDMA_CR_RESET_MASK);
  320. out_be32(&priv->dmarx->control, XAXIDMA_CR_RESET_MASK);
  321. /* At the initialization time, hardware should finish reset quickly */
  322. while (timeout--) {
  323. /* Check transmit/receive channel */
  324. /* Reset is done when the reset bit is low */
  325. if (!(in_be32(&priv->dmatx->control) |
  326. in_be32(&priv->dmarx->control))
  327. & XAXIDMA_CR_RESET_MASK) {
  328. break;
  329. }
  330. }
  331. if (!timeout)
  332. printf("%s: Timeout\n", __func__);
  333. }
  334. static int axiemac_init(struct eth_device *dev, bd_t * bis)
  335. {
  336. struct axidma_priv *priv = dev->priv;
  337. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  338. u32 temp;
  339. debug("axiemac: Init started\n");
  340. /*
  341. * Initialize AXIDMA engine. AXIDMA engine must be initialized before
  342. * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is
  343. * reset, and since AXIDMA reset line is connected to AxiEthernet, this
  344. * would ensure a reset of AxiEthernet.
  345. */
  346. axi_dma_init(dev);
  347. /* Initialize AxiEthernet hardware. */
  348. if (axi_ethernet_init(dev))
  349. return -1;
  350. /* Disable all RX interrupts before RxBD space setup */
  351. temp = in_be32(&priv->dmarx->control);
  352. temp &= ~XAXIDMA_IRQ_ALL_MASK;
  353. out_be32(&priv->dmarx->control, temp);
  354. /* Start DMA RX channel. Now it's ready to receive data.*/
  355. out_be32(&priv->dmarx->current, (u32)&rx_bd);
  356. /* Setup the BD. */
  357. memset(&rx_bd, 0, sizeof(rx_bd));
  358. rx_bd.next = (u32)&rx_bd;
  359. rx_bd.phys = (u32)&rxframe;
  360. rx_bd.cntrl = sizeof(rxframe);
  361. /* Flush the last BD so DMA core could see the updates */
  362. flush_cache((u32)&rx_bd, sizeof(rx_bd));
  363. /* It is necessary to flush rxframe because if you don't do it
  364. * then cache can contain uninitialized data */
  365. flush_cache((u32)&rxframe, sizeof(rxframe));
  366. /* Start the hardware */
  367. temp = in_be32(&priv->dmarx->control);
  368. temp |= XAXIDMA_CR_RUNSTOP_MASK;
  369. out_be32(&priv->dmarx->control, temp);
  370. /* Rx BD is ready - start */
  371. out_be32(&priv->dmarx->tail, (u32)&rx_bd);
  372. /* Enable TX */
  373. out_be32(&regs->tc, XAE_TC_TX_MASK);
  374. /* Enable RX */
  375. out_be32(&regs->rcw1, XAE_RCW1_RX_MASK);
  376. /* PHY setup */
  377. if (!setup_phy(dev)) {
  378. axiemac_halt(dev);
  379. return -1;
  380. }
  381. debug("axiemac: Init complete\n");
  382. return 0;
  383. }
  384. static int axiemac_send(struct eth_device *dev, void *ptr, int len)
  385. {
  386. struct axidma_priv *priv = dev->priv;
  387. u32 timeout;
  388. if (len > PKTSIZE_ALIGN)
  389. len = PKTSIZE_ALIGN;
  390. /* Flush packet to main memory to be trasfered by DMA */
  391. flush_cache((u32)ptr, len);
  392. /* Setup Tx BD */
  393. memset(&tx_bd, 0, sizeof(tx_bd));
  394. /* At the end of the ring, link the last BD back to the top */
  395. tx_bd.next = (u32)&tx_bd;
  396. tx_bd.phys = (u32)ptr;
  397. /* Save len */
  398. tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
  399. XAXIDMA_BD_CTRL_TXEOF_MASK;
  400. /* Flush the last BD so DMA core could see the updates */
  401. flush_cache((u32)&tx_bd, sizeof(tx_bd));
  402. if (in_be32(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
  403. u32 temp;
  404. out_be32(&priv->dmatx->current, (u32)&tx_bd);
  405. /* Start the hardware */
  406. temp = in_be32(&priv->dmatx->control);
  407. temp |= XAXIDMA_CR_RUNSTOP_MASK;
  408. out_be32(&priv->dmatx->control, temp);
  409. }
  410. /* Start transfer */
  411. out_be32(&priv->dmatx->tail, (u32)&tx_bd);
  412. /* Wait for transmission to complete */
  413. debug("axiemac: Waiting for tx to be done\n");
  414. timeout = 200;
  415. while (timeout && (!in_be32(&priv->dmatx->status) &
  416. (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK))) {
  417. timeout--;
  418. udelay(1);
  419. }
  420. if (!timeout) {
  421. printf("%s: Timeout\n", __func__);
  422. return 1;
  423. }
  424. debug("axiemac: Sending complete\n");
  425. return 0;
  426. }
  427. static int isrxready(struct eth_device *dev)
  428. {
  429. u32 status;
  430. struct axidma_priv *priv = dev->priv;
  431. /* Read pending interrupts */
  432. status = in_be32(&priv->dmarx->status);
  433. /* Acknowledge pending interrupts */
  434. out_be32(&priv->dmarx->status, status & XAXIDMA_IRQ_ALL_MASK);
  435. /*
  436. * If Reception done interrupt is asserted, call RX call back function
  437. * to handle the processed BDs and then raise the according flag.
  438. */
  439. if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
  440. return 1;
  441. return 0;
  442. }
  443. static int axiemac_recv(struct eth_device *dev)
  444. {
  445. u32 length;
  446. struct axidma_priv *priv = dev->priv;
  447. u32 temp;
  448. /* Wait for an incoming packet */
  449. if (!isrxready(dev))
  450. return 0;
  451. debug("axiemac: RX data ready\n");
  452. /* Disable IRQ for a moment till packet is handled */
  453. temp = in_be32(&priv->dmarx->control);
  454. temp &= ~XAXIDMA_IRQ_ALL_MASK;
  455. out_be32(&priv->dmarx->control, temp);
  456. length = rx_bd.app4 & 0xFFFF; /* max length mask */
  457. #ifdef DEBUG
  458. print_buffer(&rxframe, &rxframe[0], 1, length, 16);
  459. #endif
  460. /* Pass the received frame up for processing */
  461. if (length)
  462. net_process_received_packet(rxframe, length);
  463. #ifdef DEBUG
  464. /* It is useful to clear buffer to be sure that it is consistent */
  465. memset(rxframe, 0, sizeof(rxframe));
  466. #endif
  467. /* Setup RxBD */
  468. /* Clear the whole buffer and setup it again - all flags are cleared */
  469. memset(&rx_bd, 0, sizeof(rx_bd));
  470. rx_bd.next = (u32)&rx_bd;
  471. rx_bd.phys = (u32)&rxframe;
  472. rx_bd.cntrl = sizeof(rxframe);
  473. /* Write bd to HW */
  474. flush_cache((u32)&rx_bd, sizeof(rx_bd));
  475. /* It is necessary to flush rxframe because if you don't do it
  476. * then cache will contain previous packet */
  477. flush_cache((u32)&rxframe, sizeof(rxframe));
  478. /* Rx BD is ready - start again */
  479. out_be32(&priv->dmarx->tail, (u32)&rx_bd);
  480. debug("axiemac: RX completed, framelength = %d\n", length);
  481. return length;
  482. }
  483. static int axiemac_miiphy_read(const char *devname, uchar addr,
  484. uchar reg, ushort *val)
  485. {
  486. struct eth_device *dev = eth_get_dev();
  487. u32 ret;
  488. ret = phyread(dev, addr, reg, val);
  489. debug("axiemac: Read MII 0x%x, 0x%x, 0x%x\n", addr, reg, *val);
  490. return ret;
  491. }
  492. static int axiemac_miiphy_write(const char *devname, uchar addr,
  493. uchar reg, ushort val)
  494. {
  495. struct eth_device *dev = eth_get_dev();
  496. debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, val);
  497. return phywrite(dev, addr, reg, val);
  498. }
  499. static int axiemac_bus_reset(struct mii_dev *bus)
  500. {
  501. debug("axiemac: Bus reset\n");
  502. return 0;
  503. }
  504. int xilinx_axiemac_initialize(bd_t *bis, unsigned long base_addr,
  505. unsigned long dma_addr)
  506. {
  507. struct eth_device *dev;
  508. struct axidma_priv *priv;
  509. dev = calloc(1, sizeof(struct eth_device));
  510. if (dev == NULL)
  511. return -1;
  512. dev->priv = calloc(1, sizeof(struct axidma_priv));
  513. if (dev->priv == NULL) {
  514. free(dev);
  515. return -1;
  516. }
  517. priv = dev->priv;
  518. sprintf(dev->name, "aximac.%lx", base_addr);
  519. dev->iobase = base_addr;
  520. priv->dmatx = (struct axidma_reg *)dma_addr;
  521. /* RX channel offset is 0x30 */
  522. priv->dmarx = (struct axidma_reg *)(dma_addr + 0x30);
  523. dev->init = axiemac_init;
  524. dev->halt = axiemac_halt;
  525. dev->send = axiemac_send;
  526. dev->recv = axiemac_recv;
  527. dev->write_hwaddr = axiemac_setup_mac;
  528. #ifdef CONFIG_PHY_ADDR
  529. priv->phyaddr = CONFIG_PHY_ADDR;
  530. #else
  531. priv->phyaddr = -1;
  532. #endif
  533. eth_register(dev);
  534. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  535. miiphy_register(dev->name, axiemac_miiphy_read, axiemac_miiphy_write);
  536. priv->bus = miiphy_get_dev_by_name(dev->name);
  537. priv->bus->reset = axiemac_bus_reset;
  538. #endif
  539. return 1;
  540. }