fec_mxc.c 29 KB

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