fec_mxc.c 27 KB

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