fec_mxc.c 27 KB

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