fec_mxc.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <malloc.h>
  25. #include <net.h>
  26. #include <miiphy.h>
  27. #include "fec_mxc.h"
  28. #include <asm/arch/clock.h>
  29. #include <asm/arch/imx-regs.h>
  30. #include <asm/io.h>
  31. #include <asm/errno.h>
  32. #include <linux/compiler.h>
  33. DECLARE_GLOBAL_DATA_PTR;
  34. /*
  35. * Timeout the transfer after 5 mS. This is usually a bit more, since
  36. * the code in the tightloops this timeout is used in adds some overhead.
  37. */
  38. #define FEC_XFER_TIMEOUT 5000
  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. struct nbuf {
  63. uint8_t data[1500]; /**< actual data */
  64. int length; /**< actual length */
  65. int used; /**< buffer in use or not */
  66. uint8_t head[16]; /**< MAC header(6 + 6 + 2) + 2(aligned) */
  67. };
  68. #ifdef CONFIG_FEC_MXC_SWAP_PACKET
  69. static void swap_packet(uint32_t *packet, int length)
  70. {
  71. int i;
  72. for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
  73. packet[i] = __swab32(packet[i]);
  74. }
  75. #endif
  76. /*
  77. * MII-interface related functions
  78. */
  79. static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyAddr,
  80. uint8_t regAddr)
  81. {
  82. uint32_t reg; /* convenient holder for the PHY register */
  83. uint32_t phy; /* convenient holder for the PHY */
  84. uint32_t start;
  85. int val;
  86. /*
  87. * reading from any PHY's register is done by properly
  88. * programming the FEC's MII data register.
  89. */
  90. writel(FEC_IEVENT_MII, &eth->ievent);
  91. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  92. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  93. writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
  94. phy | reg, &eth->mii_data);
  95. /*
  96. * wait for the related interrupt
  97. */
  98. start = get_timer(0);
  99. while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
  100. if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
  101. printf("Read MDIO failed...\n");
  102. return -1;
  103. }
  104. }
  105. /*
  106. * clear mii interrupt bit
  107. */
  108. writel(FEC_IEVENT_MII, &eth->ievent);
  109. /*
  110. * it's now safe to read the PHY's register
  111. */
  112. val = (unsigned short)readl(&eth->mii_data);
  113. debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
  114. regAddr, val);
  115. return val;
  116. }
  117. static void fec_mii_setspeed(struct ethernet_regs *eth)
  118. {
  119. /*
  120. * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
  121. * and do not drop the Preamble.
  122. */
  123. writel((((imx_get_fecclk() / 1000000) + 2) / 5) << 1,
  124. &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. /*
  138. * wait for the MII interrupt
  139. */
  140. start = get_timer(0);
  141. while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
  142. if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
  143. printf("Write MDIO failed...\n");
  144. return -1;
  145. }
  146. }
  147. /*
  148. * clear MII interrupt bit
  149. */
  150. writel(FEC_IEVENT_MII, &eth->ievent);
  151. debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyAddr,
  152. regAddr, data);
  153. return 0;
  154. }
  155. int fec_phy_read(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr)
  156. {
  157. return fec_mdio_read(bus->priv, phyAddr, regAddr);
  158. }
  159. int fec_phy_write(struct mii_dev *bus, int phyAddr, int dev_addr, int regAddr,
  160. 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. * For this task we need additional memory for the data buffers. And each
  244. * data buffer requires some alignment. Thy must be aligned to a specific
  245. * boundary each.
  246. */
  247. static int fec_rbd_init(struct fec_priv *fec, int count, int dsize)
  248. {
  249. uint32_t size;
  250. int i;
  251. /*
  252. * Allocate memory for the buffers. This allocation respects the
  253. * alignment
  254. */
  255. size = roundup(dsize, ARCH_DMA_MINALIGN);
  256. for (i = 0; i < count; i++) {
  257. uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
  258. if (data_ptr == 0) {
  259. uint8_t *data = memalign(ARCH_DMA_MINALIGN,
  260. size);
  261. if (!data) {
  262. printf("%s: error allocating rxbuf %d\n",
  263. __func__, i);
  264. goto err;
  265. }
  266. writel((uint32_t)data, &fec->rbd_base[i].data_pointer);
  267. } /* needs allocation */
  268. writew(FEC_RBD_EMPTY, &fec->rbd_base[i].status);
  269. writew(0, &fec->rbd_base[i].data_length);
  270. }
  271. /* Mark the last RBD to close the ring. */
  272. writew(FEC_RBD_WRAP | FEC_RBD_EMPTY, &fec->rbd_base[i - 1].status);
  273. fec->rbd_index = 0;
  274. return 0;
  275. err:
  276. for (; i >= 0; i--) {
  277. uint32_t data_ptr = readl(&fec->rbd_base[i].data_pointer);
  278. free((void *)data_ptr);
  279. }
  280. return -ENOMEM;
  281. }
  282. /**
  283. * Initialize transmit task's buffer descriptors
  284. * @param[in] fec all we know about the device yet
  285. *
  286. * Transmit buffers are created externally. We only have to init the BDs here.\n
  287. * Note: There is a race condition in the hardware. When only one BD is in
  288. * use it must be marked with the WRAP bit to use it for every transmitt.
  289. * This bit in combination with the READY bit results into double transmit
  290. * of each data buffer. It seems the state machine checks READY earlier then
  291. * resetting it after the first transfer.
  292. * Using two BDs solves this issue.
  293. */
  294. static void fec_tbd_init(struct fec_priv *fec)
  295. {
  296. unsigned addr = (unsigned)fec->tbd_base;
  297. unsigned size = roundup(2 * sizeof(struct fec_bd),
  298. ARCH_DMA_MINALIGN);
  299. writew(0x0000, &fec->tbd_base[0].status);
  300. writew(FEC_TBD_WRAP, &fec->tbd_base[1].status);
  301. fec->tbd_index = 0;
  302. flush_dcache_range(addr, addr+size);
  303. }
  304. /**
  305. * Mark the given read buffer descriptor as free
  306. * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
  307. * @param[in] pRbd buffer descriptor to mark free again
  308. */
  309. static void fec_rbd_clean(int last, struct fec_bd *pRbd)
  310. {
  311. unsigned short flags = FEC_RBD_EMPTY;
  312. if (last)
  313. flags |= FEC_RBD_WRAP;
  314. writew(flags, &pRbd->status);
  315. writew(0, &pRbd->data_length);
  316. }
  317. static int fec_get_hwaddr(struct eth_device *dev, int dev_id,
  318. unsigned char *mac)
  319. {
  320. imx_get_mac_from_fuse(dev_id, mac);
  321. return !is_valid_ether_addr(mac);
  322. }
  323. static int fec_set_hwaddr(struct eth_device *dev)
  324. {
  325. uchar *mac = dev->enetaddr;
  326. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  327. writel(0, &fec->eth->iaddr1);
  328. writel(0, &fec->eth->iaddr2);
  329. writel(0, &fec->eth->gaddr1);
  330. writel(0, &fec->eth->gaddr2);
  331. /*
  332. * Set physical address
  333. */
  334. writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
  335. &fec->eth->paddr1);
  336. writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
  337. return 0;
  338. }
  339. /*
  340. * Do initial configuration of the FEC registers
  341. */
  342. static void fec_reg_setup(struct fec_priv *fec)
  343. {
  344. uint32_t rcntrl;
  345. /*
  346. * Set interrupt mask register
  347. */
  348. writel(0x00000000, &fec->eth->imask);
  349. /*
  350. * Clear FEC-Lite interrupt event register(IEVENT)
  351. */
  352. writel(0xffffffff, &fec->eth->ievent);
  353. /*
  354. * Set FEC-Lite receive control register(R_CNTRL):
  355. */
  356. /* Start with frame length = 1518, common for all modes. */
  357. rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
  358. if (fec->xcv_type != SEVENWIRE) /* xMII modes */
  359. rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
  360. if (fec->xcv_type == RGMII)
  361. rcntrl |= FEC_RCNTRL_RGMII;
  362. else if (fec->xcv_type == RMII)
  363. rcntrl |= FEC_RCNTRL_RMII;
  364. writel(rcntrl, &fec->eth->r_cntrl);
  365. }
  366. /**
  367. * Start the FEC engine
  368. * @param[in] dev Our device to handle
  369. */
  370. static int fec_open(struct eth_device *edev)
  371. {
  372. struct fec_priv *fec = (struct fec_priv *)edev->priv;
  373. int speed;
  374. uint32_t addr, size;
  375. int i;
  376. debug("fec_open: fec_open(dev)\n");
  377. /* full-duplex, heartbeat disabled */
  378. writel(1 << 2, &fec->eth->x_cntrl);
  379. fec->rbd_index = 0;
  380. /* Invalidate all descriptors */
  381. for (i = 0; i < FEC_RBD_NUM - 1; i++)
  382. fec_rbd_clean(0, &fec->rbd_base[i]);
  383. fec_rbd_clean(1, &fec->rbd_base[i]);
  384. /* Flush the descriptors into RAM */
  385. size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
  386. ARCH_DMA_MINALIGN);
  387. addr = (uint32_t)fec->rbd_base;
  388. flush_dcache_range(addr, addr + size);
  389. #ifdef FEC_QUIRK_ENET_MAC
  390. /* Enable ENET HW endian SWAP */
  391. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
  392. &fec->eth->ecntrl);
  393. /* Enable ENET store and forward mode */
  394. writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
  395. &fec->eth->x_wmrk);
  396. #endif
  397. /*
  398. * Enable FEC-Lite controller
  399. */
  400. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
  401. &fec->eth->ecntrl);
  402. #if defined(CONFIG_MX25) || defined(CONFIG_MX53)
  403. udelay(100);
  404. /*
  405. * setup the MII gasket for RMII mode
  406. */
  407. /* disable the gasket */
  408. writew(0, &fec->eth->miigsk_enr);
  409. /* wait for the gasket to be disabled */
  410. while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
  411. udelay(2);
  412. /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
  413. writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
  414. /* re-enable the gasket */
  415. writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
  416. /* wait until MII gasket is ready */
  417. int max_loops = 10;
  418. while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
  419. if (--max_loops <= 0) {
  420. printf("WAIT for MII Gasket ready timed out\n");
  421. break;
  422. }
  423. }
  424. #endif
  425. #ifdef CONFIG_PHYLIB
  426. {
  427. /* Start up the PHY */
  428. int ret = phy_startup(fec->phydev);
  429. if (ret) {
  430. printf("Could not initialize PHY %s\n",
  431. fec->phydev->dev->name);
  432. return ret;
  433. }
  434. speed = fec->phydev->speed;
  435. }
  436. #else
  437. miiphy_wait_aneg(edev);
  438. speed = miiphy_speed(edev->name, fec->phy_id);
  439. miiphy_duplex(edev->name, fec->phy_id);
  440. #endif
  441. #ifdef FEC_QUIRK_ENET_MAC
  442. {
  443. u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
  444. u32 rcr = (readl(&fec->eth->r_cntrl) &
  445. ~(FEC_RCNTRL_RMII | FEC_RCNTRL_RMII_10T)) |
  446. FEC_RCNTRL_RGMII | FEC_RCNTRL_MII_MODE;
  447. if (speed == _1000BASET)
  448. ecr |= FEC_ECNTRL_SPEED;
  449. else if (speed != _100BASET)
  450. rcr |= FEC_RCNTRL_RMII_10T;
  451. writel(ecr, &fec->eth->ecntrl);
  452. writel(rcr, &fec->eth->r_cntrl);
  453. }
  454. #endif
  455. debug("%s:Speed=%i\n", __func__, speed);
  456. /*
  457. * Enable SmartDMA receive task
  458. */
  459. fec_rx_task_enable(fec);
  460. udelay(100000);
  461. return 0;
  462. }
  463. static int fec_init(struct eth_device *dev, bd_t* bd)
  464. {
  465. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  466. uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
  467. uint32_t size;
  468. int i, ret;
  469. /* Initialize MAC address */
  470. fec_set_hwaddr(dev);
  471. /*
  472. * Allocate transmit descriptors, there are two in total. This
  473. * allocation respects cache alignment.
  474. */
  475. if (!fec->tbd_base) {
  476. size = roundup(2 * sizeof(struct fec_bd),
  477. ARCH_DMA_MINALIGN);
  478. fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
  479. if (!fec->tbd_base) {
  480. ret = -ENOMEM;
  481. goto err1;
  482. }
  483. memset(fec->tbd_base, 0, size);
  484. fec_tbd_init(fec);
  485. flush_dcache_range((unsigned)fec->tbd_base, size);
  486. }
  487. /*
  488. * Allocate receive descriptors. This allocation respects cache
  489. * alignment.
  490. */
  491. if (!fec->rbd_base) {
  492. size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
  493. ARCH_DMA_MINALIGN);
  494. fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
  495. if (!fec->rbd_base) {
  496. ret = -ENOMEM;
  497. goto err2;
  498. }
  499. memset(fec->rbd_base, 0, size);
  500. /*
  501. * Initialize RxBD ring
  502. */
  503. if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
  504. ret = -ENOMEM;
  505. goto err3;
  506. }
  507. flush_dcache_range((unsigned)fec->rbd_base,
  508. (unsigned)fec->rbd_base + size);
  509. }
  510. fec_reg_setup(fec);
  511. if (fec->xcv_type != SEVENWIRE)
  512. fec_mii_setspeed(fec->bus->priv);
  513. /*
  514. * Set Opcode/Pause Duration Register
  515. */
  516. writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
  517. writel(0x2, &fec->eth->x_wmrk);
  518. /*
  519. * Set multicast address filter
  520. */
  521. writel(0x00000000, &fec->eth->gaddr1);
  522. writel(0x00000000, &fec->eth->gaddr2);
  523. /* clear MIB RAM */
  524. for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
  525. writel(0, i);
  526. /* FIFO receive start register */
  527. writel(0x520, &fec->eth->r_fstart);
  528. /* size and address of each buffer */
  529. writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
  530. writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
  531. writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
  532. #ifndef CONFIG_PHYLIB
  533. if (fec->xcv_type != SEVENWIRE)
  534. miiphy_restart_aneg(dev);
  535. #endif
  536. fec_open(dev);
  537. return 0;
  538. err3:
  539. free(fec->rbd_base);
  540. err2:
  541. free(fec->tbd_base);
  542. err1:
  543. return ret;
  544. }
  545. /**
  546. * Halt the FEC engine
  547. * @param[in] dev Our device to handle
  548. */
  549. static void fec_halt(struct eth_device *dev)
  550. {
  551. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  552. int counter = 0xffff;
  553. /*
  554. * issue graceful stop command to the FEC transmitter if necessary
  555. */
  556. writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
  557. &fec->eth->x_cntrl);
  558. debug("eth_halt: wait for stop regs\n");
  559. /*
  560. * wait for graceful stop to register
  561. */
  562. while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
  563. udelay(1);
  564. /*
  565. * Disable SmartDMA tasks
  566. */
  567. fec_tx_task_disable(fec);
  568. fec_rx_task_disable(fec);
  569. /*
  570. * Disable the Ethernet Controller
  571. * Note: this will also reset the BD index counter!
  572. */
  573. writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
  574. &fec->eth->ecntrl);
  575. fec->rbd_index = 0;
  576. fec->tbd_index = 0;
  577. debug("eth_halt: done\n");
  578. }
  579. /**
  580. * Transmit one frame
  581. * @param[in] dev Our ethernet device to handle
  582. * @param[in] packet Pointer to the data to be transmitted
  583. * @param[in] length Data count in bytes
  584. * @return 0 on success
  585. */
  586. static int fec_send(struct eth_device *dev, void *packet, int length)
  587. {
  588. unsigned int status;
  589. uint32_t size, end;
  590. uint32_t addr;
  591. int timeout = FEC_XFER_TIMEOUT;
  592. int ret = 0;
  593. /*
  594. * This routine transmits one frame. This routine only accepts
  595. * 6-byte Ethernet addresses.
  596. */
  597. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  598. /*
  599. * Check for valid length of data.
  600. */
  601. if ((length > 1500) || (length <= 0)) {
  602. printf("Payload (%d) too large\n", length);
  603. return -1;
  604. }
  605. /*
  606. * Setup the transmit buffer. We are always using the first buffer for
  607. * transmission, the second will be empty and only used to stop the DMA
  608. * engine. We also flush the packet to RAM here to avoid cache trouble.
  609. */
  610. #ifdef CONFIG_FEC_MXC_SWAP_PACKET
  611. swap_packet((uint32_t *)packet, length);
  612. #endif
  613. addr = (uint32_t)packet;
  614. end = roundup(addr + length, ARCH_DMA_MINALIGN);
  615. addr &= ~(ARCH_DMA_MINALIGN - 1);
  616. flush_dcache_range(addr, end);
  617. writew(length, &fec->tbd_base[fec->tbd_index].data_length);
  618. writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
  619. /*
  620. * update BD's status now
  621. * This block:
  622. * - is always the last in a chain (means no chain)
  623. * - should transmitt the CRC
  624. * - might be the last BD in the list, so the address counter should
  625. * wrap (-> keep the WRAP flag)
  626. */
  627. status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
  628. status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  629. writew(status, &fec->tbd_base[fec->tbd_index].status);
  630. /*
  631. * Flush data cache. This code flushes both TX descriptors to RAM.
  632. * After this code, the descriptors will be safely in RAM and we
  633. * can start DMA.
  634. */
  635. size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
  636. addr = (uint32_t)fec->tbd_base;
  637. flush_dcache_range(addr, addr + size);
  638. /*
  639. * Enable SmartDMA transmit task
  640. */
  641. fec_tx_task_enable(fec);
  642. /*
  643. * Wait until frame is sent. On each turn of the wait cycle, we must
  644. * invalidate data cache to see what's really in RAM. Also, we need
  645. * barrier here.
  646. */
  647. while (--timeout) {
  648. if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
  649. break;
  650. }
  651. if (!timeout)
  652. ret = -EINVAL;
  653. invalidate_dcache_range(addr, addr + size);
  654. if (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY)
  655. ret = -EINVAL;
  656. debug("fec_send: status 0x%x index %d ret %i\n",
  657. readw(&fec->tbd_base[fec->tbd_index].status),
  658. fec->tbd_index, ret);
  659. /* for next transmission use the other buffer */
  660. if (fec->tbd_index)
  661. fec->tbd_index = 0;
  662. else
  663. fec->tbd_index = 1;
  664. return ret;
  665. }
  666. /**
  667. * Pull one frame from the card
  668. * @param[in] dev Our ethernet device to handle
  669. * @return Length of packet read
  670. */
  671. static int fec_recv(struct eth_device *dev)
  672. {
  673. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  674. struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
  675. unsigned long ievent;
  676. int frame_length, len = 0;
  677. struct nbuf *frame;
  678. uint16_t bd_status;
  679. uint32_t addr, size, end;
  680. int i;
  681. uchar buff[FEC_MAX_PKT_SIZE] __aligned(ARCH_DMA_MINALIGN);
  682. /*
  683. * Check if any critical events have happened
  684. */
  685. ievent = readl(&fec->eth->ievent);
  686. writel(ievent, &fec->eth->ievent);
  687. debug("fec_recv: ievent 0x%lx\n", ievent);
  688. if (ievent & FEC_IEVENT_BABR) {
  689. fec_halt(dev);
  690. fec_init(dev, fec->bd);
  691. printf("some error: 0x%08lx\n", ievent);
  692. return 0;
  693. }
  694. if (ievent & FEC_IEVENT_HBERR) {
  695. /* Heartbeat error */
  696. writel(0x00000001 | readl(&fec->eth->x_cntrl),
  697. &fec->eth->x_cntrl);
  698. }
  699. if (ievent & FEC_IEVENT_GRA) {
  700. /* Graceful stop complete */
  701. if (readl(&fec->eth->x_cntrl) & 0x00000001) {
  702. fec_halt(dev);
  703. writel(~0x00000001 & readl(&fec->eth->x_cntrl),
  704. &fec->eth->x_cntrl);
  705. fec_init(dev, fec->bd);
  706. }
  707. }
  708. /*
  709. * Read the buffer status. Before the status can be read, the data cache
  710. * must be invalidated, because the data in RAM might have been changed
  711. * by DMA. The descriptors are properly aligned to cachelines so there's
  712. * no need to worry they'd overlap.
  713. *
  714. * WARNING: By invalidating the descriptor here, we also invalidate
  715. * the descriptors surrounding this one. Therefore we can NOT change the
  716. * contents of this descriptor nor the surrounding ones. The problem is
  717. * that in order to mark the descriptor as processed, we need to change
  718. * the descriptor. The solution is to mark the whole cache line when all
  719. * descriptors in the cache line are processed.
  720. */
  721. addr = (uint32_t)rbd;
  722. addr &= ~(ARCH_DMA_MINALIGN - 1);
  723. size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
  724. invalidate_dcache_range(addr, addr + size);
  725. bd_status = readw(&rbd->status);
  726. debug("fec_recv: status 0x%x\n", bd_status);
  727. if (!(bd_status & FEC_RBD_EMPTY)) {
  728. if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
  729. ((readw(&rbd->data_length) - 4) > 14)) {
  730. /*
  731. * Get buffer address and size
  732. */
  733. frame = (struct nbuf *)readl(&rbd->data_pointer);
  734. frame_length = readw(&rbd->data_length) - 4;
  735. /*
  736. * Invalidate data cache over the buffer
  737. */
  738. addr = (uint32_t)frame;
  739. end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
  740. addr &= ~(ARCH_DMA_MINALIGN - 1);
  741. invalidate_dcache_range(addr, end);
  742. /*
  743. * Fill the buffer and pass it to upper layers
  744. */
  745. #ifdef CONFIG_FEC_MXC_SWAP_PACKET
  746. swap_packet((uint32_t *)frame->data, frame_length);
  747. #endif
  748. memcpy(buff, frame->data, frame_length);
  749. NetReceive(buff, frame_length);
  750. len = frame_length;
  751. } else {
  752. if (bd_status & FEC_RBD_ERR)
  753. printf("error frame: 0x%08lx 0x%08x\n",
  754. (ulong)rbd->data_pointer,
  755. bd_status);
  756. }
  757. /*
  758. * Free the current buffer, restart the engine and move forward
  759. * to the next buffer. Here we check if the whole cacheline of
  760. * descriptors was already processed and if so, we mark it free
  761. * as whole.
  762. */
  763. size = RXDESC_PER_CACHELINE - 1;
  764. if ((fec->rbd_index & size) == size) {
  765. i = fec->rbd_index - size;
  766. addr = (uint32_t)&fec->rbd_base[i];
  767. for (; i <= fec->rbd_index ; i++) {
  768. fec_rbd_clean(i == (FEC_RBD_NUM - 1),
  769. &fec->rbd_base[i]);
  770. }
  771. flush_dcache_range(addr,
  772. addr + ARCH_DMA_MINALIGN);
  773. }
  774. fec_rx_task_enable(fec);
  775. fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
  776. }
  777. debug("fec_recv: stop\n");
  778. return len;
  779. }
  780. static void fec_set_dev_name(char *dest, int dev_id)
  781. {
  782. sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
  783. }
  784. static int fec_probe(bd_t *bd, int dev_id, int phy_id, uint32_t base_addr)
  785. {
  786. struct phy_device *phydev;
  787. struct eth_device *edev;
  788. struct fec_priv *fec;
  789. struct mii_dev *bus;
  790. unsigned char ethaddr[6];
  791. uint32_t start;
  792. int ret = 0;
  793. /* create and fill edev struct */
  794. edev = (struct eth_device *)malloc(sizeof(struct eth_device));
  795. if (!edev) {
  796. puts("fec_mxc: not enough malloc memory for eth_device\n");
  797. ret = -ENOMEM;
  798. goto err1;
  799. }
  800. fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
  801. if (!fec) {
  802. puts("fec_mxc: not enough malloc memory for fec_priv\n");
  803. ret = -ENOMEM;
  804. goto err2;
  805. }
  806. memset(edev, 0, sizeof(*edev));
  807. memset(fec, 0, sizeof(*fec));
  808. edev->priv = fec;
  809. edev->init = fec_init;
  810. edev->send = fec_send;
  811. edev->recv = fec_recv;
  812. edev->halt = fec_halt;
  813. edev->write_hwaddr = fec_set_hwaddr;
  814. fec->eth = (struct ethernet_regs *)base_addr;
  815. fec->bd = bd;
  816. fec->xcv_type = CONFIG_FEC_XCV_TYPE;
  817. /* Reset chip. */
  818. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
  819. start = get_timer(0);
  820. while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
  821. if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
  822. printf("FEC MXC: Timeout reseting chip\n");
  823. goto err3;
  824. }
  825. udelay(10);
  826. }
  827. fec_reg_setup(fec);
  828. fec_set_dev_name(edev->name, dev_id);
  829. fec->dev_id = (dev_id == -1) ? 0 : dev_id;
  830. fec->phy_id = phy_id;
  831. bus = mdio_alloc();
  832. if (!bus) {
  833. printf("mdio_alloc failed\n");
  834. ret = -ENOMEM;
  835. goto err3;
  836. }
  837. bus->read = fec_phy_read;
  838. bus->write = fec_phy_write;
  839. fec_set_dev_name(bus->name, dev_id);
  840. #ifdef CONFIG_MX28
  841. /*
  842. * The i.MX28 has two ethernet interfaces, but they are not equal.
  843. * Only the first one can access the MDIO bus.
  844. */
  845. bus->priv = (struct ethernet_regs *)MXS_ENET0_BASE;
  846. #else
  847. bus->priv = fec->eth;
  848. #endif
  849. fec_mii_setspeed(bus->priv);
  850. ret = mdio_register(bus);
  851. if (ret) {
  852. printf("mdio_register failed\n");
  853. free(bus);
  854. ret = -ENOMEM;
  855. goto err3;
  856. }
  857. fec->bus = bus;
  858. eth_register(edev);
  859. if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
  860. debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
  861. memcpy(edev->enetaddr, ethaddr, 6);
  862. }
  863. /* Configure phy */
  864. #ifdef CONFIG_PHYLIB
  865. phydev = phy_connect(fec->bus, phy_id, edev, PHY_INTERFACE_MODE_RGMII);
  866. if (!phydev) {
  867. free(bus);
  868. ret = -ENOMEM;
  869. goto err3;
  870. }
  871. fec->phydev = phydev;
  872. phy_config(phydev);
  873. #endif
  874. return ret;
  875. err3:
  876. free(fec);
  877. err2:
  878. free(edev);
  879. err1:
  880. return ret;
  881. }
  882. int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
  883. {
  884. debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
  885. return fec_probe(bd, dev_id, phy_id, addr);
  886. }
  887. #ifdef CONFIG_FEC_MXC_PHYADDR
  888. int fecmxc_initialize(bd_t *bd)
  889. {
  890. return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
  891. IMX_FEC_BASE);
  892. }
  893. #endif
  894. #ifndef CONFIG_PHYLIB
  895. int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
  896. {
  897. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  898. fec->mii_postcall = cb;
  899. return 0;
  900. }
  901. #endif