fec_mxc.c 20 KB

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