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