fec_mxc.c 18 KB

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