fec_mxc.c 21 KB

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