fec_mxc.c 37 KB

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