fec_mxc.c 34 KB

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