fec_mxc.c 33 KB

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