fec_mxc.c 25 KB

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