fec_mxc.c 33 KB

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