fec_mxc.c 25 KB

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