fec_mxc.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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() && !is_mx6ull()) {
  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. char mac[16];
  888. uint32_t start;
  889. int ret = 0;
  890. /* create and fill edev struct */
  891. edev = (struct eth_device *)malloc(sizeof(struct eth_device));
  892. if (!edev) {
  893. puts("fec_mxc: not enough malloc memory for eth_device\n");
  894. ret = -ENOMEM;
  895. goto err1;
  896. }
  897. fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
  898. if (!fec) {
  899. puts("fec_mxc: not enough malloc memory for fec_priv\n");
  900. ret = -ENOMEM;
  901. goto err2;
  902. }
  903. memset(edev, 0, sizeof(*edev));
  904. memset(fec, 0, sizeof(*fec));
  905. ret = fec_alloc_descs(fec);
  906. if (ret)
  907. goto err3;
  908. edev->priv = fec;
  909. edev->init = fec_init;
  910. edev->send = fec_send;
  911. edev->recv = fec_recv;
  912. edev->halt = fec_halt;
  913. edev->write_hwaddr = fec_set_hwaddr;
  914. fec->eth = (struct ethernet_regs *)base_addr;
  915. fec->bd = bd;
  916. fec->xcv_type = CONFIG_FEC_XCV_TYPE;
  917. /* Reset chip. */
  918. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
  919. start = get_timer(0);
  920. while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
  921. if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
  922. printf("FEC MXC: Timeout resetting chip\n");
  923. goto err4;
  924. }
  925. udelay(10);
  926. }
  927. fec_reg_setup(fec);
  928. fec_set_dev_name(edev->name, dev_id);
  929. fec->dev_id = (dev_id == -1) ? 0 : dev_id;
  930. fec->bus = bus;
  931. fec_mii_setspeed(bus->priv);
  932. #ifdef CONFIG_PHYLIB
  933. fec->phydev = phydev;
  934. phy_connect_dev(phydev, edev);
  935. /* Configure phy */
  936. phy_config(phydev);
  937. #else
  938. fec->phy_id = phy_id;
  939. #endif
  940. eth_register(edev);
  941. /* only support one eth device, the index number pointed by dev_id */
  942. edev->index = fec->dev_id;
  943. if (fec_get_hwaddr(fec->dev_id, ethaddr) == 0) {
  944. debug("got MAC%d address from fuse: %pM\n", fec->dev_id, ethaddr);
  945. memcpy(edev->enetaddr, ethaddr, 6);
  946. if (fec->dev_id)
  947. sprintf(mac, "eth%daddr", fec->dev_id);
  948. else
  949. strcpy(mac, "ethaddr");
  950. if (!getenv(mac))
  951. eth_setenv_enetaddr(mac, ethaddr);
  952. }
  953. return ret;
  954. err4:
  955. fec_free_descs(fec);
  956. err3:
  957. free(fec);
  958. err2:
  959. free(edev);
  960. err1:
  961. return ret;
  962. }
  963. int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
  964. {
  965. uint32_t base_mii;
  966. struct mii_dev *bus = NULL;
  967. #ifdef CONFIG_PHYLIB
  968. struct phy_device *phydev = NULL;
  969. #endif
  970. int ret;
  971. #ifdef CONFIG_MX28
  972. /*
  973. * The i.MX28 has two ethernet interfaces, but they are not equal.
  974. * Only the first one can access the MDIO bus.
  975. */
  976. base_mii = MXS_ENET0_BASE;
  977. #else
  978. base_mii = addr;
  979. #endif
  980. debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
  981. bus = fec_get_miibus(base_mii, dev_id);
  982. if (!bus)
  983. return -ENOMEM;
  984. #ifdef CONFIG_PHYLIB
  985. phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
  986. if (!phydev) {
  987. mdio_unregister(bus);
  988. free(bus);
  989. return -ENOMEM;
  990. }
  991. ret = fec_probe(bd, dev_id, addr, bus, phydev);
  992. #else
  993. ret = fec_probe(bd, dev_id, addr, bus, phy_id);
  994. #endif
  995. if (ret) {
  996. #ifdef CONFIG_PHYLIB
  997. free(phydev);
  998. #endif
  999. mdio_unregister(bus);
  1000. free(bus);
  1001. }
  1002. return ret;
  1003. }
  1004. #ifdef CONFIG_FEC_MXC_PHYADDR
  1005. int fecmxc_initialize(bd_t *bd)
  1006. {
  1007. return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
  1008. IMX_FEC_BASE);
  1009. }
  1010. #endif
  1011. #ifndef CONFIG_PHYLIB
  1012. int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
  1013. {
  1014. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  1015. fec->mii_postcall = cb;
  1016. return 0;
  1017. }
  1018. #endif
  1019. #else
  1020. static int fecmxc_read_rom_hwaddr(struct udevice *dev)
  1021. {
  1022. struct fec_priv *priv = dev_get_priv(dev);
  1023. struct eth_pdata *pdata = dev_get_platdata(dev);
  1024. return fec_get_hwaddr(priv->dev_id, pdata->enetaddr);
  1025. }
  1026. static const struct eth_ops fecmxc_ops = {
  1027. .start = fecmxc_init,
  1028. .send = fecmxc_send,
  1029. .recv = fecmxc_recv,
  1030. .stop = fecmxc_halt,
  1031. .write_hwaddr = fecmxc_set_hwaddr,
  1032. .read_rom_hwaddr = fecmxc_read_rom_hwaddr,
  1033. };
  1034. static int fec_phy_init(struct fec_priv *priv, struct udevice *dev)
  1035. {
  1036. struct phy_device *phydev;
  1037. int mask = 0xffffffff;
  1038. #ifdef CONFIG_PHYLIB
  1039. mask = 1 << CONFIG_FEC_MXC_PHYADDR;
  1040. #endif
  1041. phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
  1042. if (!phydev)
  1043. return -ENODEV;
  1044. phy_connect_dev(phydev, dev);
  1045. priv->phydev = phydev;
  1046. phy_config(phydev);
  1047. return 0;
  1048. }
  1049. static int fecmxc_probe(struct udevice *dev)
  1050. {
  1051. struct eth_pdata *pdata = dev_get_platdata(dev);
  1052. struct fec_priv *priv = dev_get_priv(dev);
  1053. struct mii_dev *bus = NULL;
  1054. int dev_id = -1;
  1055. uint32_t start;
  1056. int ret;
  1057. ret = fec_alloc_descs(priv);
  1058. if (ret)
  1059. return ret;
  1060. bus = fec_get_miibus((uint32_t)priv->eth, dev_id);
  1061. if (!bus)
  1062. goto err_mii;
  1063. priv->bus = bus;
  1064. priv->xcv_type = CONFIG_FEC_XCV_TYPE;
  1065. priv->interface = pdata->phy_interface;
  1066. ret = fec_phy_init(priv, dev);
  1067. if (ret)
  1068. goto err_phy;
  1069. /* Reset chip. */
  1070. writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET,
  1071. &priv->eth->ecntrl);
  1072. start = get_timer(0);
  1073. while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) {
  1074. if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
  1075. printf("FEC MXC: Timeout reseting chip\n");
  1076. goto err_timeout;
  1077. }
  1078. udelay(10);
  1079. }
  1080. fec_reg_setup(priv);
  1081. priv->dev_id = (dev_id == -1) ? 0 : dev_id;
  1082. return 0;
  1083. err_timeout:
  1084. free(priv->phydev);
  1085. err_phy:
  1086. mdio_unregister(bus);
  1087. free(bus);
  1088. err_mii:
  1089. fec_free_descs(priv);
  1090. return ret;
  1091. }
  1092. static int fecmxc_remove(struct udevice *dev)
  1093. {
  1094. struct fec_priv *priv = dev_get_priv(dev);
  1095. free(priv->phydev);
  1096. fec_free_descs(priv);
  1097. mdio_unregister(priv->bus);
  1098. mdio_free(priv->bus);
  1099. return 0;
  1100. }
  1101. static int fecmxc_ofdata_to_platdata(struct udevice *dev)
  1102. {
  1103. struct eth_pdata *pdata = dev_get_platdata(dev);
  1104. struct fec_priv *priv = dev_get_priv(dev);
  1105. const char *phy_mode;
  1106. pdata->iobase = (phys_addr_t)dev_get_addr(dev);
  1107. priv->eth = (struct ethernet_regs *)pdata->iobase;
  1108. pdata->phy_interface = -1;
  1109. phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
  1110. NULL);
  1111. if (phy_mode)
  1112. pdata->phy_interface = phy_get_interface_by_name(phy_mode);
  1113. if (pdata->phy_interface == -1) {
  1114. debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
  1115. return -EINVAL;
  1116. }
  1117. /* TODO
  1118. * Need to get the reset-gpio and related properties from DT
  1119. * and implemet the enet reset code on .probe call
  1120. */
  1121. return 0;
  1122. }
  1123. static const struct udevice_id fecmxc_ids[] = {
  1124. { .compatible = "fsl,imx6q-fec" },
  1125. { }
  1126. };
  1127. U_BOOT_DRIVER(fecmxc_gem) = {
  1128. .name = "fecmxc",
  1129. .id = UCLASS_ETH,
  1130. .of_match = fecmxc_ids,
  1131. .ofdata_to_platdata = fecmxc_ofdata_to_platdata,
  1132. .probe = fecmxc_probe,
  1133. .remove = fecmxc_remove,
  1134. .ops = &fecmxc_ops,
  1135. .priv_auto_alloc_size = sizeof(struct fec_priv),
  1136. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  1137. };
  1138. #endif