fec_mxc.c 28 KB

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