fec_mxc.c 37 KB

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