fec_mxc.c 19 KB

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