fec_mxc.c 34 KB

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