fec_mxc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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, int dev_id,
  305. unsigned char *mac)
  306. {
  307. imx_get_mac_from_fuse(dev_id, mac);
  308. return !is_valid_ether_addr(mac);
  309. }
  310. static int fec_set_hwaddr(struct eth_device *dev)
  311. {
  312. uchar *mac = dev->enetaddr;
  313. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  314. writel(0, &fec->eth->iaddr1);
  315. writel(0, &fec->eth->iaddr2);
  316. writel(0, &fec->eth->gaddr1);
  317. writel(0, &fec->eth->gaddr2);
  318. /*
  319. * Set physical address
  320. */
  321. writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
  322. &fec->eth->paddr1);
  323. writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
  324. return 0;
  325. }
  326. /**
  327. * Start the FEC engine
  328. * @param[in] dev Our device to handle
  329. */
  330. static int fec_open(struct eth_device *edev)
  331. {
  332. struct fec_priv *fec = (struct fec_priv *)edev->priv;
  333. debug("fec_open: fec_open(dev)\n");
  334. /* full-duplex, heartbeat disabled */
  335. writel(1 << 2, &fec->eth->x_cntrl);
  336. fec->rbd_index = 0;
  337. #if defined(CONFIG_MX6Q)
  338. /* Enable ENET HW endian SWAP */
  339. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
  340. &fec->eth->ecntrl);
  341. /* Enable ENET store and forward mode */
  342. writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
  343. &fec->eth->x_wmrk);
  344. #endif
  345. /*
  346. * Enable FEC-Lite controller
  347. */
  348. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
  349. &fec->eth->ecntrl);
  350. #if defined(CONFIG_MX25) || defined(CONFIG_MX53)
  351. udelay(100);
  352. /*
  353. * setup the MII gasket for RMII mode
  354. */
  355. /* disable the gasket */
  356. writew(0, &fec->eth->miigsk_enr);
  357. /* wait for the gasket to be disabled */
  358. while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
  359. udelay(2);
  360. /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
  361. writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
  362. /* re-enable the gasket */
  363. writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
  364. /* wait until MII gasket is ready */
  365. int max_loops = 10;
  366. while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
  367. if (--max_loops <= 0) {
  368. printf("WAIT for MII Gasket ready timed out\n");
  369. break;
  370. }
  371. }
  372. #endif
  373. miiphy_wait_aneg(edev);
  374. miiphy_speed(edev->name, fec->phy_id);
  375. miiphy_duplex(edev->name, fec->phy_id);
  376. /*
  377. * Enable SmartDMA receive task
  378. */
  379. fec_rx_task_enable(fec);
  380. udelay(100000);
  381. return 0;
  382. }
  383. static int fec_init(struct eth_device *dev, bd_t* bd)
  384. {
  385. uint32_t base;
  386. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  387. uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
  388. uint32_t rcntrl;
  389. int i;
  390. /* Initialize MAC address */
  391. fec_set_hwaddr(dev);
  392. /*
  393. * reserve memory for both buffer descriptor chains at once
  394. * Datasheet forces the startaddress of each chain is 16 byte
  395. * aligned
  396. */
  397. if (fec->base_ptr == NULL)
  398. fec->base_ptr = malloc((2 + FEC_RBD_NUM) *
  399. sizeof(struct fec_bd) + DB_ALIGNMENT);
  400. base = (uint32_t)fec->base_ptr;
  401. if (!base) {
  402. puts("fec_mxc: not enough malloc memory\n");
  403. return -ENOMEM;
  404. }
  405. memset((void *)base, 0, (2 + FEC_RBD_NUM) *
  406. sizeof(struct fec_bd) + DB_ALIGNMENT);
  407. base += (DB_ALIGNMENT-1);
  408. base &= ~(DB_ALIGNMENT-1);
  409. fec->rbd_base = (struct fec_bd *)base;
  410. base += FEC_RBD_NUM * sizeof(struct fec_bd);
  411. fec->tbd_base = (struct fec_bd *)base;
  412. /*
  413. * Set interrupt mask register
  414. */
  415. writel(0x00000000, &fec->eth->imask);
  416. /*
  417. * Clear FEC-Lite interrupt event register(IEVENT)
  418. */
  419. writel(0xffffffff, &fec->eth->ievent);
  420. /*
  421. * Set FEC-Lite receive control register(R_CNTRL):
  422. */
  423. /* Start with frame length = 1518, common for all modes. */
  424. rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
  425. if (fec->xcv_type == SEVENWIRE)
  426. rcntrl |= FEC_RCNTRL_FCE;
  427. else if (fec->xcv_type == RGMII)
  428. rcntrl |= FEC_RCNTRL_RGMII;
  429. else if (fec->xcv_type == RMII)
  430. rcntrl |= FEC_RCNTRL_RMII;
  431. else /* MII mode */
  432. rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
  433. writel(rcntrl, &fec->eth->r_cntrl);
  434. if (fec->xcv_type == MII10 || fec->xcv_type == MII100)
  435. fec_mii_setspeed(fec);
  436. /*
  437. * Set Opcode/Pause Duration Register
  438. */
  439. writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
  440. writel(0x2, &fec->eth->x_wmrk);
  441. /*
  442. * Set multicast address filter
  443. */
  444. writel(0x00000000, &fec->eth->gaddr1);
  445. writel(0x00000000, &fec->eth->gaddr2);
  446. /* clear MIB RAM */
  447. for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
  448. writel(0, i);
  449. /* FIFO receive start register */
  450. writel(0x520, &fec->eth->r_fstart);
  451. /* size and address of each buffer */
  452. writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
  453. writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
  454. writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
  455. /*
  456. * Initialize RxBD/TxBD rings
  457. */
  458. if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
  459. free(fec->base_ptr);
  460. fec->base_ptr = NULL;
  461. return -ENOMEM;
  462. }
  463. fec_tbd_init(fec);
  464. if (fec->xcv_type != SEVENWIRE)
  465. miiphy_restart_aneg(dev);
  466. fec_open(dev);
  467. return 0;
  468. }
  469. /**
  470. * Halt the FEC engine
  471. * @param[in] dev Our device to handle
  472. */
  473. static void fec_halt(struct eth_device *dev)
  474. {
  475. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  476. int counter = 0xffff;
  477. /*
  478. * issue graceful stop command to the FEC transmitter if necessary
  479. */
  480. writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
  481. &fec->eth->x_cntrl);
  482. debug("eth_halt: wait for stop regs\n");
  483. /*
  484. * wait for graceful stop to register
  485. */
  486. while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
  487. udelay(1);
  488. /*
  489. * Disable SmartDMA tasks
  490. */
  491. fec_tx_task_disable(fec);
  492. fec_rx_task_disable(fec);
  493. /*
  494. * Disable the Ethernet Controller
  495. * Note: this will also reset the BD index counter!
  496. */
  497. writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
  498. &fec->eth->ecntrl);
  499. fec->rbd_index = 0;
  500. fec->tbd_index = 0;
  501. debug("eth_halt: done\n");
  502. }
  503. /**
  504. * Transmit one frame
  505. * @param[in] dev Our ethernet device to handle
  506. * @param[in] packet Pointer to the data to be transmitted
  507. * @param[in] length Data count in bytes
  508. * @return 0 on success
  509. */
  510. static int fec_send(struct eth_device *dev, volatile void* packet, int length)
  511. {
  512. unsigned int status;
  513. /*
  514. * This routine transmits one frame. This routine only accepts
  515. * 6-byte Ethernet addresses.
  516. */
  517. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  518. /*
  519. * Check for valid length of data.
  520. */
  521. if ((length > 1500) || (length <= 0)) {
  522. printf("Payload (%d) too large\n", length);
  523. return -1;
  524. }
  525. /*
  526. * Setup the transmit buffer
  527. * Note: We are always using the first buffer for transmission,
  528. * the second will be empty and only used to stop the DMA engine
  529. */
  530. #ifdef CONFIG_FEC_MXC_SWAP_PACKET
  531. swap_packet((uint32_t *)packet, length);
  532. #endif
  533. writew(length, &fec->tbd_base[fec->tbd_index].data_length);
  534. writel((uint32_t)packet, &fec->tbd_base[fec->tbd_index].data_pointer);
  535. /*
  536. * update BD's status now
  537. * This block:
  538. * - is always the last in a chain (means no chain)
  539. * - should transmitt the CRC
  540. * - might be the last BD in the list, so the address counter should
  541. * wrap (-> keep the WRAP flag)
  542. */
  543. status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
  544. status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  545. writew(status, &fec->tbd_base[fec->tbd_index].status);
  546. /*
  547. * Enable SmartDMA transmit task
  548. */
  549. fec_tx_task_enable(fec);
  550. /*
  551. * wait until frame is sent .
  552. */
  553. while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
  554. udelay(1);
  555. }
  556. debug("fec_send: status 0x%x index %d\n",
  557. readw(&fec->tbd_base[fec->tbd_index].status),
  558. fec->tbd_index);
  559. /* for next transmission use the other buffer */
  560. if (fec->tbd_index)
  561. fec->tbd_index = 0;
  562. else
  563. fec->tbd_index = 1;
  564. return 0;
  565. }
  566. /**
  567. * Pull one frame from the card
  568. * @param[in] dev Our ethernet device to handle
  569. * @return Length of packet read
  570. */
  571. static int fec_recv(struct eth_device *dev)
  572. {
  573. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  574. struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
  575. unsigned long ievent;
  576. int frame_length, len = 0;
  577. struct nbuf *frame;
  578. uint16_t bd_status;
  579. uchar buff[FEC_MAX_PKT_SIZE];
  580. /*
  581. * Check if any critical events have happened
  582. */
  583. ievent = readl(&fec->eth->ievent);
  584. writel(ievent, &fec->eth->ievent);
  585. debug("fec_recv: ievent 0x%lx\n", ievent);
  586. if (ievent & FEC_IEVENT_BABR) {
  587. fec_halt(dev);
  588. fec_init(dev, fec->bd);
  589. printf("some error: 0x%08lx\n", ievent);
  590. return 0;
  591. }
  592. if (ievent & FEC_IEVENT_HBERR) {
  593. /* Heartbeat error */
  594. writel(0x00000001 | readl(&fec->eth->x_cntrl),
  595. &fec->eth->x_cntrl);
  596. }
  597. if (ievent & FEC_IEVENT_GRA) {
  598. /* Graceful stop complete */
  599. if (readl(&fec->eth->x_cntrl) & 0x00000001) {
  600. fec_halt(dev);
  601. writel(~0x00000001 & readl(&fec->eth->x_cntrl),
  602. &fec->eth->x_cntrl);
  603. fec_init(dev, fec->bd);
  604. }
  605. }
  606. /*
  607. * ensure reading the right buffer status
  608. */
  609. bd_status = readw(&rbd->status);
  610. debug("fec_recv: status 0x%x\n", bd_status);
  611. if (!(bd_status & FEC_RBD_EMPTY)) {
  612. if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
  613. ((readw(&rbd->data_length) - 4) > 14)) {
  614. /*
  615. * Get buffer address and size
  616. */
  617. frame = (struct nbuf *)readl(&rbd->data_pointer);
  618. frame_length = readw(&rbd->data_length) - 4;
  619. /*
  620. * Fill the buffer and pass it to upper layers
  621. */
  622. #ifdef CONFIG_FEC_MXC_SWAP_PACKET
  623. swap_packet((uint32_t *)frame->data, frame_length);
  624. #endif
  625. memcpy(buff, frame->data, frame_length);
  626. NetReceive(buff, frame_length);
  627. len = frame_length;
  628. } else {
  629. if (bd_status & FEC_RBD_ERR)
  630. printf("error frame: 0x%08lx 0x%08x\n",
  631. (ulong)rbd->data_pointer,
  632. bd_status);
  633. }
  634. /*
  635. * free the current buffer, restart the engine
  636. * and move forward to the next buffer
  637. */
  638. fec_rbd_clean(fec->rbd_index == (FEC_RBD_NUM - 1) ? 1 : 0, rbd);
  639. fec_rx_task_enable(fec);
  640. fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
  641. }
  642. debug("fec_recv: stop\n");
  643. return len;
  644. }
  645. static int fec_probe(bd_t *bd, int dev_id, int phy_id, uint32_t base_addr)
  646. {
  647. struct eth_device *edev;
  648. struct fec_priv *fec;
  649. unsigned char ethaddr[6];
  650. uint32_t start;
  651. int ret = 0;
  652. /* create and fill edev struct */
  653. edev = (struct eth_device *)malloc(sizeof(struct eth_device));
  654. if (!edev) {
  655. puts("fec_mxc: not enough malloc memory for eth_device\n");
  656. ret = -ENOMEM;
  657. goto err1;
  658. }
  659. fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
  660. if (!fec) {
  661. puts("fec_mxc: not enough malloc memory for fec_priv\n");
  662. ret = -ENOMEM;
  663. goto err2;
  664. }
  665. memset(edev, 0, sizeof(*edev));
  666. memset(fec, 0, sizeof(*fec));
  667. edev->priv = fec;
  668. edev->init = fec_init;
  669. edev->send = fec_send;
  670. edev->recv = fec_recv;
  671. edev->halt = fec_halt;
  672. edev->write_hwaddr = fec_set_hwaddr;
  673. fec->eth = (struct ethernet_regs *)base_addr;
  674. fec->bd = bd;
  675. fec->xcv_type = CONFIG_FEC_XCV_TYPE;
  676. /* Reset chip. */
  677. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
  678. start = get_timer(0);
  679. while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
  680. if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
  681. printf("FEC MXC: Timeout reseting chip\n");
  682. goto err3;
  683. }
  684. udelay(10);
  685. }
  686. /*
  687. * Set interrupt mask register
  688. */
  689. writel(0x00000000, &fec->eth->imask);
  690. /*
  691. * Clear FEC-Lite interrupt event register(IEVENT)
  692. */
  693. writel(0xffffffff, &fec->eth->ievent);
  694. /*
  695. * Set FEC-Lite receive control register(R_CNTRL):
  696. */
  697. /*
  698. * Frame length=1518; MII mode;
  699. */
  700. writel((PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT) | FEC_RCNTRL_FCE |
  701. FEC_RCNTRL_MII_MODE, &fec->eth->r_cntrl);
  702. fec_mii_setspeed(fec);
  703. if (dev_id == -1) {
  704. sprintf(edev->name, "FEC");
  705. fec->dev_id = 0;
  706. } else {
  707. sprintf(edev->name, "FEC%i", dev_id);
  708. fec->dev_id = dev_id;
  709. }
  710. fec->phy_id = phy_id;
  711. miiphy_register(edev->name, fec_miiphy_read, fec_miiphy_write);
  712. eth_register(edev);
  713. if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
  714. debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
  715. memcpy(edev->enetaddr, ethaddr, 6);
  716. }
  717. return ret;
  718. err3:
  719. free(fec);
  720. err2:
  721. free(edev);
  722. err1:
  723. return ret;
  724. }
  725. #ifndef CONFIG_FEC_MXC_MULTI
  726. int fecmxc_initialize(bd_t *bd)
  727. {
  728. int lout = 1;
  729. debug("eth_init: fec_probe(bd)\n");
  730. lout = fec_probe(bd, -1, CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
  731. return lout;
  732. }
  733. #endif
  734. int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
  735. {
  736. int lout = 1;
  737. debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
  738. lout = fec_probe(bd, dev_id, phy_id, addr);
  739. return lout;
  740. }
  741. int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
  742. {
  743. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  744. fec->mii_postcall = cb;
  745. return 0;
  746. }