fec_mxc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * (C) Copyright 2009 Ilya Yanok, Emcraft Systems Ltd <yanok@emcraft.com>
  3. * (C) Copyright 2008,2009 Eric Jarrige <eric.jarrige@armadeus.org>
  4. * (C) Copyright 2008 Armadeus Systems nc
  5. * (C) Copyright 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
  6. * (C) Copyright 2007 Pengutronix, Juergen Beisert <j.beisert@pengutronix.de>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <malloc.h>
  25. #include <net.h>
  26. #include <miiphy.h>
  27. #include "fec_mxc.h"
  28. #include <asm/arch/clock.h>
  29. #include <asm/arch/imx-regs.h>
  30. #include <asm/io.h>
  31. #include <asm/errno.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. #ifndef CONFIG_MII
  34. #error "CONFIG_MII has to be defined!"
  35. #endif
  36. #undef DEBUG
  37. struct nbuf {
  38. uint8_t data[1500]; /**< actual data */
  39. int length; /**< actual length */
  40. int used; /**< buffer in use or not */
  41. uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
  42. };
  43. struct fec_priv gfec = {
  44. .eth = (struct ethernet_regs *)IMX_FEC_BASE,
  45. .xcv_type = MII100,
  46. .rbd_base = NULL,
  47. .rbd_index = 0,
  48. .tbd_base = NULL,
  49. .tbd_index = 0,
  50. .bd = NULL,
  51. .rdb_ptr = NULL,
  52. .base_ptr = NULL,
  53. };
  54. /*
  55. * MII-interface related functions
  56. */
  57. static int fec_miiphy_read(const char *dev, uint8_t phyAddr, uint8_t regAddr,
  58. uint16_t *retVal)
  59. {
  60. struct eth_device *edev = eth_get_dev_by_name(dev);
  61. struct fec_priv *fec = (struct fec_priv *)edev->priv;
  62. uint32_t reg; /* convenient holder for the PHY register */
  63. uint32_t phy; /* convenient holder for the PHY */
  64. uint32_t start;
  65. /*
  66. * reading from any PHY's register is done by properly
  67. * programming the FEC's MII data register.
  68. */
  69. writel(FEC_IEVENT_MII, &fec->eth->ievent);
  70. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  71. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  72. writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
  73. phy | reg, &fec->eth->mii_data);
  74. /*
  75. * wait for the related interrupt
  76. */
  77. start = get_timer_masked();
  78. while (!(readl(&fec->eth->ievent) & FEC_IEVENT_MII)) {
  79. if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
  80. printf("Read MDIO failed...\n");
  81. return -1;
  82. }
  83. }
  84. /*
  85. * clear mii interrupt bit
  86. */
  87. writel(FEC_IEVENT_MII, &fec->eth->ievent);
  88. /*
  89. * it's now safe to read the PHY's register
  90. */
  91. *retVal = readl(&fec->eth->mii_data);
  92. debug("fec_miiphy_read: phy: %02x reg:%02x val:%#x\n", phyAddr,
  93. regAddr, *retVal);
  94. return 0;
  95. }
  96. static void fec_mii_setspeed(struct fec_priv *fec)
  97. {
  98. /*
  99. * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
  100. * and do not drop the Preamble.
  101. */
  102. writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
  103. &fec->eth->mii_speed);
  104. debug("fec_init: mii_speed %#lx\n",
  105. fec->eth->mii_speed);
  106. }
  107. static int fec_miiphy_write(const char *dev, uint8_t phyAddr, uint8_t regAddr,
  108. uint16_t data)
  109. {
  110. struct eth_device *edev = eth_get_dev_by_name(dev);
  111. struct fec_priv *fec = (struct fec_priv *)edev->priv;
  112. uint32_t reg; /* convenient holder for the PHY register */
  113. uint32_t phy; /* convenient holder for the PHY */
  114. uint32_t start;
  115. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  116. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  117. writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
  118. FEC_MII_DATA_TA | phy | reg | data, &fec->eth->mii_data);
  119. /*
  120. * wait for the MII interrupt
  121. */
  122. start = get_timer_masked();
  123. while (!(readl(&fec->eth->ievent) & FEC_IEVENT_MII)) {
  124. if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
  125. printf("Write MDIO failed...\n");
  126. return -1;
  127. }
  128. }
  129. /*
  130. * clear MII interrupt bit
  131. */
  132. writel(FEC_IEVENT_MII, &fec->eth->ievent);
  133. debug("fec_miiphy_write: phy: %02x reg:%02x val:%#x\n", phyAddr,
  134. regAddr, data);
  135. return 0;
  136. }
  137. static int miiphy_restart_aneg(struct eth_device *dev)
  138. {
  139. /*
  140. * Wake up from sleep if necessary
  141. * Reset PHY, then delay 300ns
  142. */
  143. #ifdef CONFIG_MX27
  144. miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, PHY_MIPGSR, 0x00FF);
  145. #endif
  146. miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, PHY_BMCR,
  147. PHY_BMCR_RESET);
  148. udelay(1000);
  149. /*
  150. * Set the auto-negotiation advertisement register bits
  151. */
  152. miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, PHY_ANAR,
  153. PHY_ANLPAR_TXFD | PHY_ANLPAR_TX | PHY_ANLPAR_10FD |
  154. PHY_ANLPAR_10 | PHY_ANLPAR_PSB_802_3);
  155. miiphy_write(dev->name, CONFIG_FEC_MXC_PHYADDR, PHY_BMCR,
  156. PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
  157. return 0;
  158. }
  159. static int miiphy_wait_aneg(struct eth_device *dev)
  160. {
  161. uint32_t start;
  162. uint16_t status;
  163. /*
  164. * Wait for AN completion
  165. */
  166. start = get_timer_masked();
  167. do {
  168. if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
  169. printf("%s: Autonegotiation timeout\n", dev->name);
  170. return -1;
  171. }
  172. if (miiphy_read(dev->name, CONFIG_FEC_MXC_PHYADDR,
  173. PHY_BMSR, &status)) {
  174. printf("%s: Autonegotiation failed. status: 0x%04x\n",
  175. dev->name, status);
  176. return -1;
  177. }
  178. } while (!(status & PHY_BMSR_LS));
  179. return 0;
  180. }
  181. static int fec_rx_task_enable(struct fec_priv *fec)
  182. {
  183. writel(1 << 24, &fec->eth->r_des_active);
  184. return 0;
  185. }
  186. static int fec_rx_task_disable(struct fec_priv *fec)
  187. {
  188. return 0;
  189. }
  190. static int fec_tx_task_enable(struct fec_priv *fec)
  191. {
  192. writel(1 << 24, &fec->eth->x_des_active);
  193. return 0;
  194. }
  195. static int fec_tx_task_disable(struct fec_priv *fec)
  196. {
  197. return 0;
  198. }
  199. /**
  200. * Initialize receive task's buffer descriptors
  201. * @param[in] fec all we know about the device yet
  202. * @param[in] count receive buffer count to be allocated
  203. * @param[in] size size of each receive buffer
  204. * @return 0 on success
  205. *
  206. * For this task we need additional memory for the data buffers. And each
  207. * data buffer requires some alignment. Thy must be aligned to a specific
  208. * boundary each (DB_DATA_ALIGNMENT).
  209. */
  210. static int fec_rbd_init(struct fec_priv *fec, int count, int size)
  211. {
  212. int ix;
  213. uint32_t p = 0;
  214. /* reserve data memory and consider alignment */
  215. if (fec->rdb_ptr == NULL)
  216. fec->rdb_ptr = malloc(size * count + DB_DATA_ALIGNMENT);
  217. p = (uint32_t)fec->rdb_ptr;
  218. if (!p) {
  219. puts("fec_mxc: not enough malloc memory\n");
  220. return -ENOMEM;
  221. }
  222. memset((void *)p, 0, size * count + DB_DATA_ALIGNMENT);
  223. p += DB_DATA_ALIGNMENT-1;
  224. p &= ~(DB_DATA_ALIGNMENT-1);
  225. for (ix = 0; ix < count; ix++) {
  226. writel(p, &fec->rbd_base[ix].data_pointer);
  227. p += size;
  228. writew(FEC_RBD_EMPTY, &fec->rbd_base[ix].status);
  229. writew(0, &fec->rbd_base[ix].data_length);
  230. }
  231. /*
  232. * mark the last RBD to close the ring
  233. */
  234. writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[ix - 1].status);
  235. fec->rbd_index = 0;
  236. return 0;
  237. }
  238. /**
  239. * Initialize transmit task's buffer descriptors
  240. * @param[in] fec all we know about the device yet
  241. *
  242. * Transmit buffers are created externally. We only have to init the BDs here.\n
  243. * Note: There is a race condition in the hardware. When only one BD is in
  244. * use it must be marked with the WRAP bit to use it for every transmitt.
  245. * This bit in combination with the READY bit results into double transmit
  246. * of each data buffer. It seems the state machine checks READY earlier then
  247. * resetting it after the first transfer.
  248. * Using two BDs solves this issue.
  249. */
  250. static void fec_tbd_init(struct fec_priv *fec)
  251. {
  252. writew(0x0000, &fec->tbd_base[0].status);
  253. writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
  254. fec->tbd_index = 0;
  255. }
  256. /**
  257. * Mark the given read buffer descriptor as free
  258. * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
  259. * @param[in] pRbd buffer descriptor to mark free again
  260. */
  261. static void fec_rbd_clean(int last, struct fec_bd *pRbd)
  262. {
  263. /*
  264. * Reset buffer descriptor as empty
  265. */
  266. if (last)
  267. writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &pRbd->status);
  268. else
  269. writew(FEC_RBD_EMPTY, &pRbd->status);
  270. /*
  271. * no data in it
  272. */
  273. writew(0, &pRbd->data_length);
  274. }
  275. static int fec_get_hwaddr(struct eth_device *dev, unsigned char *mac)
  276. {
  277. /*
  278. * The MX27 can store the mac address in internal eeprom
  279. * This mechanism is not supported now by MX51 or MX25
  280. */
  281. #if defined(CONFIG_MX51) || defined(CONFIG_MX25)
  282. return -1;
  283. #else
  284. struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
  285. int i;
  286. for (i = 0; i < 6; i++)
  287. mac[6-1-i] = readl(&iim->iim_bank_area0[IIM0_MAC + i]);
  288. return !is_valid_ether_addr(mac);
  289. #endif
  290. }
  291. static int fec_set_hwaddr(struct eth_device *dev)
  292. {
  293. uchar *mac = dev->enetaddr;
  294. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  295. writel(0, &fec->eth->iaddr1);
  296. writel(0, &fec->eth->iaddr2);
  297. writel(0, &fec->eth->gaddr1);
  298. writel(0, &fec->eth->gaddr2);
  299. /*
  300. * Set physical address
  301. */
  302. writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
  303. &fec->eth->paddr1);
  304. writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
  305. return 0;
  306. }
  307. /**
  308. * Start the FEC engine
  309. * @param[in] dev Our device to handle
  310. */
  311. static int fec_open(struct eth_device *edev)
  312. {
  313. struct fec_priv *fec = (struct fec_priv *)edev->priv;
  314. debug("fec_open: fec_open(dev)\n");
  315. /* full-duplex, heartbeat disabled */
  316. writel(1 << 2, &fec->eth->x_cntrl);
  317. fec->rbd_index = 0;
  318. /*
  319. * Enable FEC-Lite controller
  320. */
  321. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
  322. &fec->eth->ecntrl);
  323. #ifdef CONFIG_MX25
  324. udelay(100);
  325. /*
  326. * setup the MII gasket for RMII mode
  327. */
  328. /* disable the gasket */
  329. writew(0, &fec->eth->miigsk_enr);
  330. /* wait for the gasket to be disabled */
  331. while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
  332. udelay(2);
  333. /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
  334. writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
  335. /* re-enable the gasket */
  336. writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
  337. /* wait until MII gasket is ready */
  338. int max_loops = 10;
  339. while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
  340. if (--max_loops <= 0) {
  341. printf("WAIT for MII Gasket ready timed out\n");
  342. break;
  343. }
  344. }
  345. #endif
  346. miiphy_wait_aneg(edev);
  347. miiphy_speed(edev->name, CONFIG_FEC_MXC_PHYADDR);
  348. miiphy_duplex(edev->name, CONFIG_FEC_MXC_PHYADDR);
  349. /*
  350. * Enable SmartDMA receive task
  351. */
  352. fec_rx_task_enable(fec);
  353. udelay(100000);
  354. return 0;
  355. }
  356. static int fec_init(struct eth_device *dev, bd_t* bd)
  357. {
  358. uint32_t base;
  359. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  360. /* Initialize MAC address */
  361. fec_set_hwaddr(dev);
  362. /*
  363. * reserve memory for both buffer descriptor chains at once
  364. * Datasheet forces the startaddress of each chain is 16 byte
  365. * aligned
  366. */
  367. if (fec->base_ptr == NULL)
  368. fec->base_ptr = malloc((2 + FEC_RBD_NUM) *
  369. sizeof(struct fec_bd) + DB_ALIGNMENT);
  370. base = (uint32_t)fec->base_ptr;
  371. if (!base) {
  372. puts("fec_mxc: not enough malloc memory\n");
  373. return -ENOMEM;
  374. }
  375. memset((void *)base, 0, (2 + FEC_RBD_NUM) *
  376. sizeof(struct fec_bd) + DB_ALIGNMENT);
  377. base += (DB_ALIGNMENT-1);
  378. base &= ~(DB_ALIGNMENT-1);
  379. fec->rbd_base = (struct fec_bd *)base;
  380. base += FEC_RBD_NUM * sizeof(struct fec_bd);
  381. fec->tbd_base = (struct fec_bd *)base;
  382. /*
  383. * Set interrupt mask register
  384. */
  385. writel(0x00000000, &fec->eth->imask);
  386. /*
  387. * Clear FEC-Lite interrupt event register(IEVENT)
  388. */
  389. writel(0xffffffff, &fec->eth->ievent);
  390. /*
  391. * Set FEC-Lite receive control register(R_CNTRL):
  392. */
  393. if (fec->xcv_type == SEVENWIRE) {
  394. /*
  395. * Frame length=1518; 7-wire mode
  396. */
  397. writel(0x05ee0020, &fec->eth->r_cntrl); /* FIXME 0x05ee0000 */
  398. } else {
  399. /*
  400. * Frame length=1518; MII mode;
  401. */
  402. writel(0x05ee0024, &fec->eth->r_cntrl); /* FIXME 0x05ee0004 */
  403. fec_mii_setspeed(fec);
  404. }
  405. /*
  406. * Set Opcode/Pause Duration Register
  407. */
  408. writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
  409. writel(0x2, &fec->eth->x_wmrk);
  410. /*
  411. * Set multicast address filter
  412. */
  413. writel(0x00000000, &fec->eth->gaddr1);
  414. writel(0x00000000, &fec->eth->gaddr2);
  415. /* clear MIB RAM */
  416. long *mib_ptr = (long *)(IMX_FEC_BASE + 0x200);
  417. while (mib_ptr <= (long *)(IMX_FEC_BASE + 0x2FC))
  418. *mib_ptr++ = 0;
  419. /* FIFO receive start register */
  420. writel(0x520, &fec->eth->r_fstart);
  421. /* size and address of each buffer */
  422. writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
  423. writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
  424. writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
  425. /*
  426. * Initialize RxBD/TxBD rings
  427. */
  428. if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
  429. free(fec->base_ptr);
  430. fec->base_ptr = NULL;
  431. return -ENOMEM;
  432. }
  433. fec_tbd_init(fec);
  434. if (fec->xcv_type != SEVENWIRE)
  435. miiphy_restart_aneg(dev);
  436. fec_open(dev);
  437. return 0;
  438. }
  439. /**
  440. * Halt the FEC engine
  441. * @param[in] dev Our device to handle
  442. */
  443. static void fec_halt(struct eth_device *dev)
  444. {
  445. struct fec_priv *fec = &gfec;
  446. int counter = 0xffff;
  447. /*
  448. * issue graceful stop command to the FEC transmitter if necessary
  449. */
  450. writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
  451. &fec->eth->x_cntrl);
  452. debug("eth_halt: wait for stop regs\n");
  453. /*
  454. * wait for graceful stop to register
  455. */
  456. while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
  457. udelay(1);
  458. /*
  459. * Disable SmartDMA tasks
  460. */
  461. fec_tx_task_disable(fec);
  462. fec_rx_task_disable(fec);
  463. /*
  464. * Disable the Ethernet Controller
  465. * Note: this will also reset the BD index counter!
  466. */
  467. writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
  468. &fec->eth->ecntrl);
  469. fec->rbd_index = 0;
  470. fec->tbd_index = 0;
  471. debug("eth_halt: done\n");
  472. }
  473. /**
  474. * Transmit one frame
  475. * @param[in] dev Our ethernet device to handle
  476. * @param[in] packet Pointer to the data to be transmitted
  477. * @param[in] length Data count in bytes
  478. * @return 0 on success
  479. */
  480. static int fec_send(struct eth_device *dev, volatile void* packet, int length)
  481. {
  482. unsigned int status;
  483. /*
  484. * This routine transmits one frame. This routine only accepts
  485. * 6-byte Ethernet addresses.
  486. */
  487. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  488. /*
  489. * Check for valid length of data.
  490. */
  491. if ((length > 1500) || (length <= 0)) {
  492. printf("Payload (%d) too large\n", length);
  493. return -1;
  494. }
  495. /*
  496. * Setup the transmit buffer
  497. * Note: We are always using the first buffer for transmission,
  498. * the second will be empty and only used to stop the DMA engine
  499. */
  500. writew(length, &fec->tbd_base[fec->tbd_index].data_length);
  501. writel((uint32_t)packet, &fec->tbd_base[fec->tbd_index].data_pointer);
  502. /*
  503. * update BD's status now
  504. * This block:
  505. * - is always the last in a chain (means no chain)
  506. * - should transmitt the CRC
  507. * - might be the last BD in the list, so the address counter should
  508. * wrap (-> keep the WRAP flag)
  509. */
  510. status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
  511. status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  512. writew(status, &fec->tbd_base[fec->tbd_index].status);
  513. /*
  514. * Enable SmartDMA transmit task
  515. */
  516. fec_tx_task_enable(fec);
  517. /*
  518. * wait until frame is sent .
  519. */
  520. while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
  521. udelay(1);
  522. }
  523. debug("fec_send: status 0x%x index %d\n",
  524. readw(&fec->tbd_base[fec->tbd_index].status),
  525. fec->tbd_index);
  526. /* for next transmission use the other buffer */
  527. if (fec->tbd_index)
  528. fec->tbd_index = 0;
  529. else
  530. fec->tbd_index = 1;
  531. return 0;
  532. }
  533. /**
  534. * Pull one frame from the card
  535. * @param[in] dev Our ethernet device to handle
  536. * @return Length of packet read
  537. */
  538. static int fec_recv(struct eth_device *dev)
  539. {
  540. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  541. struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
  542. unsigned long ievent;
  543. int frame_length, len = 0;
  544. struct nbuf *frame;
  545. uint16_t bd_status;
  546. uchar buff[FEC_MAX_PKT_SIZE];
  547. /*
  548. * Check if any critical events have happened
  549. */
  550. ievent = readl(&fec->eth->ievent);
  551. writel(ievent, &fec->eth->ievent);
  552. debug("fec_recv: ievent 0x%x\n", ievent);
  553. if (ievent & FEC_IEVENT_BABR) {
  554. fec_halt(dev);
  555. fec_init(dev, fec->bd);
  556. printf("some error: 0x%08lx\n", ievent);
  557. return 0;
  558. }
  559. if (ievent & FEC_IEVENT_HBERR) {
  560. /* Heartbeat error */
  561. writel(0x00000001 | readl(&fec->eth->x_cntrl),
  562. &fec->eth->x_cntrl);
  563. }
  564. if (ievent & FEC_IEVENT_GRA) {
  565. /* Graceful stop complete */
  566. if (readl(&fec->eth->x_cntrl) & 0x00000001) {
  567. fec_halt(dev);
  568. writel(~0x00000001 & readl(&fec->eth->x_cntrl),
  569. &fec->eth->x_cntrl);
  570. fec_init(dev, fec->bd);
  571. }
  572. }
  573. /*
  574. * ensure reading the right buffer status
  575. */
  576. bd_status = readw(&rbd->status);
  577. debug("fec_recv: status 0x%x\n", bd_status);
  578. if (!(bd_status & FEC_RBD_EMPTY)) {
  579. if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
  580. ((readw(&rbd->data_length) - 4) > 14)) {
  581. /*
  582. * Get buffer address and size
  583. */
  584. frame = (struct nbuf *)readl(&rbd->data_pointer);
  585. frame_length = readw(&rbd->data_length) - 4;
  586. /*
  587. * Fill the buffer and pass it to upper layers
  588. */
  589. memcpy(buff, frame->data, frame_length);
  590. NetReceive(buff, frame_length);
  591. len = frame_length;
  592. } else {
  593. if (bd_status & FEC_RBD_ERR)
  594. printf("error frame: 0x%08lx 0x%08x\n",
  595. (ulong)rbd->data_pointer,
  596. bd_status);
  597. }
  598. /*
  599. * free the current buffer, restart the engine
  600. * and move forward to the next buffer
  601. */
  602. fec_rbd_clean(fec->rbd_index == (FEC_RBD_NUM - 1) ? 1 : 0, rbd);
  603. fec_rx_task_enable(fec);
  604. fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
  605. }
  606. debug("fec_recv: stop\n");
  607. return len;
  608. }
  609. static int fec_probe(bd_t *bd)
  610. {
  611. struct eth_device *edev;
  612. struct fec_priv *fec = &gfec;
  613. unsigned char ethaddr[6];
  614. /* create and fill edev struct */
  615. edev = (struct eth_device *)malloc(sizeof(struct eth_device));
  616. if (!edev) {
  617. puts("fec_mxc: not enough malloc memory\n");
  618. return -ENOMEM;
  619. }
  620. edev->priv = fec;
  621. edev->init = fec_init;
  622. edev->send = fec_send;
  623. edev->recv = fec_recv;
  624. edev->halt = fec_halt;
  625. edev->write_hwaddr = fec_set_hwaddr;
  626. fec->eth = (struct ethernet_regs *)IMX_FEC_BASE;
  627. fec->bd = bd;
  628. fec->xcv_type = MII100;
  629. /* Reset chip. */
  630. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
  631. while (readl(&fec->eth->ecntrl) & 1)
  632. udelay(10);
  633. /*
  634. * Set interrupt mask register
  635. */
  636. writel(0x00000000, &fec->eth->imask);
  637. /*
  638. * Clear FEC-Lite interrupt event register(IEVENT)
  639. */
  640. writel(0xffffffff, &fec->eth->ievent);
  641. /*
  642. * Set FEC-Lite receive control register(R_CNTRL):
  643. */
  644. /*
  645. * Frame length=1518; MII mode;
  646. */
  647. writel(0x05ee0024, &fec->eth->r_cntrl); /* FIXME 0x05ee0004 */
  648. fec_mii_setspeed(fec);
  649. sprintf(edev->name, "FEC");
  650. miiphy_register(edev->name, fec_miiphy_read, fec_miiphy_write);
  651. eth_register(edev);
  652. if (fec_get_hwaddr(edev, ethaddr) == 0) {
  653. printf("got MAC address from EEPROM: %pM\n", ethaddr);
  654. memcpy(edev->enetaddr, ethaddr, 6);
  655. }
  656. return 0;
  657. }
  658. int fecmxc_initialize(bd_t *bd)
  659. {
  660. int lout = 1;
  661. debug("eth_init: fec_probe(bd)\n");
  662. lout = fec_probe(bd);
  663. return lout;
  664. }