xilinx_axi_emac.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <config.h>
  25. #include <common.h>
  26. #include <net.h>
  27. #include <malloc.h>
  28. #include <asm/io.h>
  29. #include <phy.h>
  30. #include <miiphy.h>
  31. #if !defined(CONFIG_PHYLIB)
  32. # error AXI_ETHERNET requires PHYLIB
  33. #endif
  34. /* Link setup */
  35. #define XAE_EMMC_LINKSPEED_MASK 0xC0000000 /* Link speed */
  36. #define XAE_EMMC_LINKSPD_10 0x00000000 /* Link Speed mask for 10 Mbit */
  37. #define XAE_EMMC_LINKSPD_100 0x40000000 /* Link Speed mask for 100 Mbit */
  38. #define XAE_EMMC_LINKSPD_1000 0x80000000 /* Link Speed mask for 1000 Mbit */
  39. /* Interrupt Status/Enable/Mask Registers bit definitions */
  40. #define XAE_INT_RXRJECT_MASK 0x00000008 /* Rx frame rejected */
  41. #define XAE_INT_MGTRDY_MASK 0x00000080 /* MGT clock Lock */
  42. /* Receive Configuration Word 1 (RCW1) Register bit definitions */
  43. #define XAE_RCW1_RX_MASK 0x10000000 /* Receiver enable */
  44. /* Transmitter Configuration (TC) Register bit definitions */
  45. #define XAE_TC_TX_MASK 0x10000000 /* Transmitter enable */
  46. #define XAE_UAW1_UNICASTADDR_MASK 0x0000FFFF
  47. /* MDIO Management Configuration (MC) Register bit definitions */
  48. #define XAE_MDIO_MC_MDIOEN_MASK 0x00000040 /* MII management enable*/
  49. /* MDIO Management Control Register (MCR) Register bit definitions */
  50. #define XAE_MDIO_MCR_PHYAD_MASK 0x1F000000 /* Phy Address Mask */
  51. #define XAE_MDIO_MCR_PHYAD_SHIFT 24 /* Phy Address Shift */
  52. #define XAE_MDIO_MCR_REGAD_MASK 0x001F0000 /* Reg Address Mask */
  53. #define XAE_MDIO_MCR_REGAD_SHIFT 16 /* Reg Address Shift */
  54. #define XAE_MDIO_MCR_OP_READ_MASK 0x00008000 /* Op Code Read Mask */
  55. #define XAE_MDIO_MCR_OP_WRITE_MASK 0x00004000 /* Op Code Write Mask */
  56. #define XAE_MDIO_MCR_INITIATE_MASK 0x00000800 /* Ready Mask */
  57. #define XAE_MDIO_MCR_READY_MASK 0x00000080 /* Ready Mask */
  58. #define XAE_MDIO_DIV_DFT 29 /* Default MDIO clock divisor */
  59. /* DMA macros */
  60. /* Bitmasks of XAXIDMA_CR_OFFSET register */
  61. #define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */
  62. #define XAXIDMA_CR_RESET_MASK 0x00000004 /* Reset DMA engine */
  63. /* Bitmasks of XAXIDMA_SR_OFFSET register */
  64. #define XAXIDMA_HALTED_MASK 0x00000001 /* DMA channel halted */
  65. /* Bitmask for interrupts */
  66. #define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */
  67. #define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */
  68. #define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */
  69. /* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */
  70. #define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */
  71. #define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */
  72. #define DMAALIGN 128
  73. static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN)));
  74. /* Reflect dma offsets */
  75. struct axidma_reg {
  76. u32 control; /* DMACR */
  77. u32 status; /* DMASR */
  78. u32 current; /* CURDESC */
  79. u32 reserved;
  80. u32 tail; /* TAILDESC */
  81. };
  82. /* Private driver structures */
  83. struct axidma_priv {
  84. struct axidma_reg *dmatx;
  85. struct axidma_reg *dmarx;
  86. int phyaddr;
  87. struct phy_device *phydev;
  88. struct mii_dev *bus;
  89. };
  90. /* BD descriptors */
  91. struct axidma_bd {
  92. u32 next; /* Next descriptor pointer */
  93. u32 reserved1;
  94. u32 phys; /* Buffer address */
  95. u32 reserved2;
  96. u32 reserved3;
  97. u32 reserved4;
  98. u32 cntrl; /* Control */
  99. u32 status; /* Status */
  100. u32 app0;
  101. u32 app1; /* TX start << 16 | insert */
  102. u32 app2; /* TX csum seed */
  103. u32 app3;
  104. u32 app4;
  105. u32 sw_id_offset;
  106. u32 reserved5;
  107. u32 reserved6;
  108. };
  109. /* Static BDs - driver uses only one BD */
  110. static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN)));
  111. static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN)));
  112. struct axi_regs {
  113. u32 reserved[3];
  114. u32 is; /* 0xC: Interrupt status */
  115. u32 reserved2;
  116. u32 ie; /* 0x14: Interrupt enable */
  117. u32 reserved3[251];
  118. u32 rcw1; /* 0x404: Rx Configuration Word 1 */
  119. u32 tc; /* 0x408: Tx Configuration */
  120. u32 reserved4;
  121. u32 emmc; /* 0x410: EMAC mode configuration */
  122. u32 reserved5[59];
  123. u32 mdio_mc; /* 0x500: MII Management Config */
  124. u32 mdio_mcr; /* 0x504: MII Management Control */
  125. u32 mdio_mwd; /* 0x508: MII Management Write Data */
  126. u32 mdio_mrd; /* 0x50C: MII Management Read Data */
  127. u32 reserved6[124];
  128. u32 uaw0; /* 0x700: Unicast address word 0 */
  129. u32 uaw1; /* 0x704: Unicast address word 1 */
  130. };
  131. /* Use MII register 1 (MII status register) to detect PHY */
  132. #define PHY_DETECT_REG 1
  133. /*
  134. * Mask used to verify certain PHY features (or register contents)
  135. * in the register above:
  136. * 0x1000: 10Mbps full duplex support
  137. * 0x0800: 10Mbps half duplex support
  138. * 0x0008: Auto-negotiation support
  139. */
  140. #define PHY_DETECT_MASK 0x1808
  141. static inline int mdio_wait(struct eth_device *dev)
  142. {
  143. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  144. u32 timeout = 200;
  145. /* Wait till MDIO interface is ready to accept a new transaction. */
  146. while (timeout && (!(in_be32(&regs->mdio_mcr)
  147. & XAE_MDIO_MCR_READY_MASK))) {
  148. timeout--;
  149. udelay(1);
  150. }
  151. if (!timeout) {
  152. printf("%s: Timeout\n", __func__);
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. static u32 phyread(struct eth_device *dev, u32 phyaddress, u32 registernum,
  158. u16 *val)
  159. {
  160. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  161. u32 mdioctrlreg = 0;
  162. if (mdio_wait(dev))
  163. return 1;
  164. mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
  165. XAE_MDIO_MCR_PHYAD_MASK) |
  166. ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
  167. & XAE_MDIO_MCR_REGAD_MASK) |
  168. XAE_MDIO_MCR_INITIATE_MASK |
  169. XAE_MDIO_MCR_OP_READ_MASK;
  170. out_be32(&regs->mdio_mcr, mdioctrlreg);
  171. if (mdio_wait(dev))
  172. return 1;
  173. /* Read data */
  174. *val = in_be32(&regs->mdio_mrd);
  175. return 0;
  176. }
  177. static u32 phywrite(struct eth_device *dev, u32 phyaddress, u32 registernum,
  178. u32 data)
  179. {
  180. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  181. u32 mdioctrlreg = 0;
  182. if (mdio_wait(dev))
  183. return 1;
  184. mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
  185. XAE_MDIO_MCR_PHYAD_MASK) |
  186. ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
  187. & XAE_MDIO_MCR_REGAD_MASK) |
  188. XAE_MDIO_MCR_INITIATE_MASK |
  189. XAE_MDIO_MCR_OP_WRITE_MASK;
  190. /* Write data */
  191. out_be32(&regs->mdio_mwd, data);
  192. out_be32(&regs->mdio_mcr, mdioctrlreg);
  193. if (mdio_wait(dev))
  194. return 1;
  195. return 0;
  196. }
  197. /* Setting axi emac and phy to proper setting */
  198. static int setup_phy(struct eth_device *dev)
  199. {
  200. u16 phyreg;
  201. u32 i, speed, emmc_reg, ret;
  202. struct axidma_priv *priv = dev->priv;
  203. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  204. struct phy_device *phydev;
  205. u32 supported = SUPPORTED_10baseT_Half |
  206. SUPPORTED_10baseT_Full |
  207. SUPPORTED_100baseT_Half |
  208. SUPPORTED_100baseT_Full |
  209. SUPPORTED_1000baseT_Half |
  210. SUPPORTED_1000baseT_Full;
  211. if (priv->phyaddr == -1) {
  212. /* Detect the PHY address */
  213. for (i = 31; i >= 0; i--) {
  214. ret = phyread(dev, i, PHY_DETECT_REG, &phyreg);
  215. if (!ret && (phyreg != 0xFFFF) &&
  216. ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
  217. /* Found a valid PHY address */
  218. priv->phyaddr = i;
  219. debug("axiemac: Found valid phy address, %x\n",
  220. phyreg);
  221. break;
  222. }
  223. }
  224. }
  225. /* Interface - look at tsec */
  226. phydev = phy_connect(priv->bus, priv->phyaddr, dev, 0);
  227. phydev->supported &= supported;
  228. phydev->advertising = phydev->supported;
  229. priv->phydev = phydev;
  230. phy_config(phydev);
  231. if (phy_startup(phydev)) {
  232. printf("axiemac: could not initialize PHY %s\n",
  233. phydev->dev->name);
  234. return 0;
  235. }
  236. switch (phydev->speed) {
  237. case 1000:
  238. speed = XAE_EMMC_LINKSPD_1000;
  239. break;
  240. case 100:
  241. speed = XAE_EMMC_LINKSPD_100;
  242. break;
  243. case 10:
  244. speed = XAE_EMMC_LINKSPD_10;
  245. break;
  246. default:
  247. return 0;
  248. }
  249. /* Setup the emac for the phy speed */
  250. emmc_reg = in_be32(&regs->emmc);
  251. emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
  252. emmc_reg |= speed;
  253. /* Write new speed setting out to Axi Ethernet */
  254. out_be32(&regs->emmc, emmc_reg);
  255. /*
  256. * Setting the operating speed of the MAC needs a delay. There
  257. * doesn't seem to be register to poll, so please consider this
  258. * during your application design.
  259. */
  260. udelay(1);
  261. return 1;
  262. }
  263. /* STOP DMA transfers */
  264. static void axiemac_halt(struct eth_device *dev)
  265. {
  266. struct axidma_priv *priv = dev->priv;
  267. u32 temp;
  268. /* Stop the hardware */
  269. temp = in_be32(&priv->dmatx->control);
  270. temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
  271. out_be32(&priv->dmatx->control, temp);
  272. temp = in_be32(&priv->dmarx->control);
  273. temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
  274. out_be32(&priv->dmarx->control, temp);
  275. debug("axiemac: Halted\n");
  276. }
  277. static int axi_ethernet_init(struct eth_device *dev)
  278. {
  279. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  280. u32 timeout = 200;
  281. /*
  282. * Check the status of the MgtRdy bit in the interrupt status
  283. * registers. This must be done to allow the MGT clock to become stable
  284. * for the Sgmii and 1000BaseX PHY interfaces. No other register reads
  285. * will be valid until this bit is valid.
  286. * The bit is always a 1 for all other PHY interfaces.
  287. */
  288. while (timeout && (!(in_be32(&regs->is) & XAE_INT_MGTRDY_MASK))) {
  289. timeout--;
  290. udelay(1);
  291. }
  292. if (!timeout) {
  293. printf("%s: Timeout\n", __func__);
  294. return 1;
  295. }
  296. /* Stop the device and reset HW */
  297. /* Disable interrupts */
  298. out_be32(&regs->ie, 0);
  299. /* Disable the receiver */
  300. out_be32(&regs->rcw1, in_be32(&regs->rcw1) & ~XAE_RCW1_RX_MASK);
  301. /*
  302. * Stopping the receiver in mid-packet causes a dropped packet
  303. * indication from HW. Clear it.
  304. */
  305. /* Set the interrupt status register to clear the interrupt */
  306. out_be32(&regs->is, XAE_INT_RXRJECT_MASK);
  307. /* Setup HW */
  308. /* Set default MDIO divisor */
  309. out_be32(&regs->mdio_mc, XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK);
  310. debug("axiemac: InitHw done\n");
  311. return 0;
  312. }
  313. static int axiemac_setup_mac(struct eth_device *dev)
  314. {
  315. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  316. /* Set the MAC address */
  317. int val = ((dev->enetaddr[3] << 24) | (dev->enetaddr[2] << 16) |
  318. (dev->enetaddr[1] << 8) | (dev->enetaddr[0]));
  319. out_be32(&regs->uaw0, val);
  320. val = (dev->enetaddr[5] << 8) | dev->enetaddr[4] ;
  321. val |= in_be32(&regs->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
  322. out_be32(&regs->uaw1, val);
  323. return 0;
  324. }
  325. /* Reset DMA engine */
  326. static void axi_dma_init(struct eth_device *dev)
  327. {
  328. struct axidma_priv *priv = dev->priv;
  329. u32 timeout = 500;
  330. /* Reset the engine so the hardware starts from a known state */
  331. out_be32(&priv->dmatx->control, XAXIDMA_CR_RESET_MASK);
  332. out_be32(&priv->dmarx->control, XAXIDMA_CR_RESET_MASK);
  333. /* At the initialization time, hardware should finish reset quickly */
  334. while (timeout--) {
  335. /* Check transmit/receive channel */
  336. /* Reset is done when the reset bit is low */
  337. if (!(in_be32(&priv->dmatx->control) |
  338. in_be32(&priv->dmarx->control))
  339. & XAXIDMA_CR_RESET_MASK) {
  340. break;
  341. }
  342. }
  343. if (!timeout)
  344. printf("%s: Timeout\n", __func__);
  345. }
  346. static int axiemac_init(struct eth_device *dev, bd_t * bis)
  347. {
  348. struct axidma_priv *priv = dev->priv;
  349. struct axi_regs *regs = (struct axi_regs *)dev->iobase;
  350. u32 temp;
  351. debug("axiemac: Init started\n");
  352. /*
  353. * Initialize AXIDMA engine. AXIDMA engine must be initialized before
  354. * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is
  355. * reset, and since AXIDMA reset line is connected to AxiEthernet, this
  356. * would ensure a reset of AxiEthernet.
  357. */
  358. axi_dma_init(dev);
  359. /* Initialize AxiEthernet hardware. */
  360. if (axi_ethernet_init(dev))
  361. return -1;
  362. /* Disable all RX interrupts before RxBD space setup */
  363. temp = in_be32(&priv->dmarx->control);
  364. temp &= ~XAXIDMA_IRQ_ALL_MASK;
  365. out_be32(&priv->dmarx->control, temp);
  366. /* Start DMA RX channel. Now it's ready to receive data.*/
  367. out_be32(&priv->dmarx->current, (u32)&rx_bd);
  368. /* Setup the BD. */
  369. memset(&rx_bd, 0, sizeof(rx_bd));
  370. rx_bd.next = (u32)&rx_bd;
  371. rx_bd.phys = (u32)&rxframe;
  372. rx_bd.cntrl = sizeof(rxframe);
  373. /* Flush the last BD so DMA core could see the updates */
  374. flush_cache((u32)&rx_bd, sizeof(rx_bd));
  375. /* It is necessary to flush rxframe because if you don't do it
  376. * then cache can contain uninitialized data */
  377. flush_cache((u32)&rxframe, sizeof(rxframe));
  378. /* Start the hardware */
  379. temp = in_be32(&priv->dmarx->control);
  380. temp |= XAXIDMA_CR_RUNSTOP_MASK;
  381. out_be32(&priv->dmarx->control, temp);
  382. /* Rx BD is ready - start */
  383. out_be32(&priv->dmarx->tail, (u32)&rx_bd);
  384. /* Enable TX */
  385. out_be32(&regs->tc, XAE_TC_TX_MASK);
  386. /* Enable RX */
  387. out_be32(&regs->rcw1, XAE_RCW1_RX_MASK);
  388. /* PHY setup */
  389. if (!setup_phy(dev)) {
  390. axiemac_halt(dev);
  391. return -1;
  392. }
  393. debug("axiemac: Init complete\n");
  394. return 0;
  395. }
  396. static int axiemac_send(struct eth_device *dev, void *ptr, int len)
  397. {
  398. struct axidma_priv *priv = dev->priv;
  399. u32 timeout;
  400. if (len > PKTSIZE_ALIGN)
  401. len = PKTSIZE_ALIGN;
  402. /* Flush packet to main memory to be trasfered by DMA */
  403. flush_cache((u32)ptr, len);
  404. /* Setup Tx BD */
  405. memset(&tx_bd, 0, sizeof(tx_bd));
  406. /* At the end of the ring, link the last BD back to the top */
  407. tx_bd.next = (u32)&tx_bd;
  408. tx_bd.phys = (u32)ptr;
  409. /* Save len */
  410. tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
  411. XAXIDMA_BD_CTRL_TXEOF_MASK;
  412. /* Flush the last BD so DMA core could see the updates */
  413. flush_cache((u32)&tx_bd, sizeof(tx_bd));
  414. if (in_be32(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
  415. u32 temp;
  416. out_be32(&priv->dmatx->current, (u32)&tx_bd);
  417. /* Start the hardware */
  418. temp = in_be32(&priv->dmatx->control);
  419. temp |= XAXIDMA_CR_RUNSTOP_MASK;
  420. out_be32(&priv->dmatx->control, temp);
  421. }
  422. /* Start transfer */
  423. out_be32(&priv->dmatx->tail, (u32)&tx_bd);
  424. /* Wait for transmission to complete */
  425. debug("axiemac: Waiting for tx to be done\n");
  426. timeout = 200;
  427. while (timeout && (!in_be32(&priv->dmatx->status) &
  428. (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK))) {
  429. timeout--;
  430. udelay(1);
  431. }
  432. if (!timeout) {
  433. printf("%s: Timeout\n", __func__);
  434. return 1;
  435. }
  436. debug("axiemac: Sending complete\n");
  437. return 0;
  438. }
  439. static int isrxready(struct eth_device *dev)
  440. {
  441. u32 status;
  442. struct axidma_priv *priv = dev->priv;
  443. /* Read pending interrupts */
  444. status = in_be32(&priv->dmarx->status);
  445. /* Acknowledge pending interrupts */
  446. out_be32(&priv->dmarx->status, status & XAXIDMA_IRQ_ALL_MASK);
  447. /*
  448. * If Reception done interrupt is asserted, call RX call back function
  449. * to handle the processed BDs and then raise the according flag.
  450. */
  451. if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
  452. return 1;
  453. return 0;
  454. }
  455. static int axiemac_recv(struct eth_device *dev)
  456. {
  457. u32 length;
  458. struct axidma_priv *priv = dev->priv;
  459. u32 temp;
  460. /* Wait for an incoming packet */
  461. if (!isrxready(dev))
  462. return 0;
  463. debug("axiemac: RX data ready\n");
  464. /* Disable IRQ for a moment till packet is handled */
  465. temp = in_be32(&priv->dmarx->control);
  466. temp &= ~XAXIDMA_IRQ_ALL_MASK;
  467. out_be32(&priv->dmarx->control, temp);
  468. length = rx_bd.app4 & 0xFFFF; /* max length mask */
  469. #ifdef DEBUG
  470. print_buffer(&rxframe, &rxframe[0], 1, length, 16);
  471. #endif
  472. /* Pass the received frame up for processing */
  473. if (length)
  474. NetReceive(rxframe, length);
  475. #ifdef DEBUG
  476. /* It is useful to clear buffer to be sure that it is consistent */
  477. memset(rxframe, 0, sizeof(rxframe));
  478. #endif
  479. /* Setup RxBD */
  480. /* Clear the whole buffer and setup it again - all flags are cleared */
  481. memset(&rx_bd, 0, sizeof(rx_bd));
  482. rx_bd.next = (u32)&rx_bd;
  483. rx_bd.phys = (u32)&rxframe;
  484. rx_bd.cntrl = sizeof(rxframe);
  485. /* Write bd to HW */
  486. flush_cache((u32)&rx_bd, sizeof(rx_bd));
  487. /* It is necessary to flush rxframe because if you don't do it
  488. * then cache will contain previous packet */
  489. flush_cache((u32)&rxframe, sizeof(rxframe));
  490. /* Rx BD is ready - start again */
  491. out_be32(&priv->dmarx->tail, (u32)&rx_bd);
  492. debug("axiemac: RX completed, framelength = %d\n", length);
  493. return length;
  494. }
  495. static int axiemac_miiphy_read(const char *devname, uchar addr,
  496. uchar reg, ushort *val)
  497. {
  498. struct eth_device *dev = eth_get_dev();
  499. u32 ret;
  500. ret = phyread(dev, addr, reg, val);
  501. debug("axiemac: Read MII 0x%x, 0x%x, 0x%x\n", addr, reg, *val);
  502. return ret;
  503. }
  504. static int axiemac_miiphy_write(const char *devname, uchar addr,
  505. uchar reg, ushort val)
  506. {
  507. struct eth_device *dev = eth_get_dev();
  508. debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, val);
  509. return phywrite(dev, addr, reg, val);
  510. }
  511. static int axiemac_bus_reset(struct mii_dev *bus)
  512. {
  513. debug("axiemac: Bus reset\n");
  514. return 0;
  515. }
  516. int xilinx_axiemac_initialize(bd_t *bis, unsigned long base_addr,
  517. unsigned long dma_addr)
  518. {
  519. struct eth_device *dev;
  520. struct axidma_priv *priv;
  521. dev = calloc(1, sizeof(struct eth_device));
  522. if (dev == NULL)
  523. return -1;
  524. dev->priv = calloc(1, sizeof(struct axidma_priv));
  525. if (dev->priv == NULL) {
  526. free(dev);
  527. return -1;
  528. }
  529. priv = dev->priv;
  530. sprintf(dev->name, "aximac.%lx", base_addr);
  531. dev->iobase = base_addr;
  532. priv->dmatx = (struct axidma_reg *)dma_addr;
  533. /* RX channel offset is 0x30 */
  534. priv->dmarx = (struct axidma_reg *)(dma_addr + 0x30);
  535. dev->init = axiemac_init;
  536. dev->halt = axiemac_halt;
  537. dev->send = axiemac_send;
  538. dev->recv = axiemac_recv;
  539. dev->write_hwaddr = axiemac_setup_mac;
  540. #ifdef CONFIG_PHY_ADDR
  541. priv->phyaddr = CONFIG_PHY_ADDR;
  542. #else
  543. priv->phyaddr = -1;
  544. #endif
  545. eth_register(dev);
  546. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  547. miiphy_register(dev->name, axiemac_miiphy_read, axiemac_miiphy_write);
  548. priv->bus = miiphy_get_dev_by_name(dev->name);
  549. priv->bus->reset = axiemac_bus_reset;
  550. #endif
  551. return 1;
  552. }