fec_mxc.c 22 KB

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