fec_mxc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. static void fec_eth_phy_config(struct eth_device *dev)
  340. {
  341. #ifdef CONFIG_PHYLIB
  342. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  343. struct phy_device *phydev;
  344. phydev = phy_connect(fec->bus, fec->phy_id, dev,
  345. PHY_INTERFACE_MODE_RGMII);
  346. if (phydev) {
  347. fec->phydev = phydev;
  348. phy_config(phydev);
  349. }
  350. #endif
  351. }
  352. /*
  353. * Do initial configuration of the FEC registers
  354. */
  355. static void fec_reg_setup(struct fec_priv *fec)
  356. {
  357. uint32_t rcntrl;
  358. /*
  359. * Set interrupt mask register
  360. */
  361. writel(0x00000000, &fec->eth->imask);
  362. /*
  363. * Clear FEC-Lite interrupt event register(IEVENT)
  364. */
  365. writel(0xffffffff, &fec->eth->ievent);
  366. /*
  367. * Set FEC-Lite receive control register(R_CNTRL):
  368. */
  369. /* Start with frame length = 1518, common for all modes. */
  370. rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
  371. if (fec->xcv_type != SEVENWIRE) /* xMII modes */
  372. rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
  373. if (fec->xcv_type == RGMII)
  374. rcntrl |= FEC_RCNTRL_RGMII;
  375. else if (fec->xcv_type == RMII)
  376. rcntrl |= FEC_RCNTRL_RMII;
  377. writel(rcntrl, &fec->eth->r_cntrl);
  378. }
  379. /**
  380. * Start the FEC engine
  381. * @param[in] dev Our device to handle
  382. */
  383. static int fec_open(struct eth_device *edev)
  384. {
  385. struct fec_priv *fec = (struct fec_priv *)edev->priv;
  386. int speed;
  387. uint32_t addr, size;
  388. int i;
  389. debug("fec_open: fec_open(dev)\n");
  390. /* full-duplex, heartbeat disabled */
  391. writel(1 << 2, &fec->eth->x_cntrl);
  392. fec->rbd_index = 0;
  393. /* Invalidate all descriptors */
  394. for (i = 0; i < FEC_RBD_NUM - 1; i++)
  395. fec_rbd_clean(0, &fec->rbd_base[i]);
  396. fec_rbd_clean(1, &fec->rbd_base[i]);
  397. /* Flush the descriptors into RAM */
  398. size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
  399. ARCH_DMA_MINALIGN);
  400. addr = (uint32_t)fec->rbd_base;
  401. flush_dcache_range(addr, addr + size);
  402. #ifdef FEC_QUIRK_ENET_MAC
  403. /* Enable ENET HW endian SWAP */
  404. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
  405. &fec->eth->ecntrl);
  406. /* Enable ENET store and forward mode */
  407. writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
  408. &fec->eth->x_wmrk);
  409. #endif
  410. /*
  411. * Enable FEC-Lite controller
  412. */
  413. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
  414. &fec->eth->ecntrl);
  415. #if defined(CONFIG_MX25) || defined(CONFIG_MX53)
  416. udelay(100);
  417. /*
  418. * setup the MII gasket for RMII mode
  419. */
  420. /* disable the gasket */
  421. writew(0, &fec->eth->miigsk_enr);
  422. /* wait for the gasket to be disabled */
  423. while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
  424. udelay(2);
  425. /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
  426. writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
  427. /* re-enable the gasket */
  428. writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
  429. /* wait until MII gasket is ready */
  430. int max_loops = 10;
  431. while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
  432. if (--max_loops <= 0) {
  433. printf("WAIT for MII Gasket ready timed out\n");
  434. break;
  435. }
  436. }
  437. #endif
  438. #ifdef CONFIG_PHYLIB
  439. if (!fec->phydev)
  440. fec_eth_phy_config(edev);
  441. if (fec->phydev) {
  442. /* Start up the PHY */
  443. int ret = phy_startup(fec->phydev);
  444. if (ret) {
  445. printf("Could not initialize PHY %s\n",
  446. fec->phydev->dev->name);
  447. return ret;
  448. }
  449. speed = fec->phydev->speed;
  450. } else {
  451. speed = _100BASET;
  452. }
  453. #else
  454. miiphy_wait_aneg(edev);
  455. speed = miiphy_speed(edev->name, fec->phy_id);
  456. miiphy_duplex(edev->name, fec->phy_id);
  457. #endif
  458. #ifdef FEC_QUIRK_ENET_MAC
  459. {
  460. u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
  461. u32 rcr = (readl(&fec->eth->r_cntrl) &
  462. ~(FEC_RCNTRL_RMII | FEC_RCNTRL_RMII_10T)) |
  463. FEC_RCNTRL_RGMII | FEC_RCNTRL_MII_MODE;
  464. if (speed == _1000BASET)
  465. ecr |= FEC_ECNTRL_SPEED;
  466. else if (speed != _100BASET)
  467. rcr |= FEC_RCNTRL_RMII_10T;
  468. writel(ecr, &fec->eth->ecntrl);
  469. writel(rcr, &fec->eth->r_cntrl);
  470. }
  471. #endif
  472. debug("%s:Speed=%i\n", __func__, speed);
  473. /*
  474. * Enable SmartDMA receive task
  475. */
  476. fec_rx_task_enable(fec);
  477. udelay(100000);
  478. return 0;
  479. }
  480. static int fec_init(struct eth_device *dev, bd_t* bd)
  481. {
  482. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  483. uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
  484. uint32_t size;
  485. int i, ret;
  486. /* Initialize MAC address */
  487. fec_set_hwaddr(dev);
  488. /*
  489. * Allocate transmit descriptors, there are two in total. This
  490. * allocation respects cache alignment.
  491. */
  492. if (!fec->tbd_base) {
  493. size = roundup(2 * sizeof(struct fec_bd),
  494. ARCH_DMA_MINALIGN);
  495. fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
  496. if (!fec->tbd_base) {
  497. ret = -ENOMEM;
  498. goto err1;
  499. }
  500. memset(fec->tbd_base, 0, size);
  501. fec_tbd_init(fec);
  502. flush_dcache_range((unsigned)fec->tbd_base, size);
  503. }
  504. /*
  505. * Allocate receive descriptors. This allocation respects cache
  506. * alignment.
  507. */
  508. if (!fec->rbd_base) {
  509. size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
  510. ARCH_DMA_MINALIGN);
  511. fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
  512. if (!fec->rbd_base) {
  513. ret = -ENOMEM;
  514. goto err2;
  515. }
  516. memset(fec->rbd_base, 0, size);
  517. /*
  518. * Initialize RxBD ring
  519. */
  520. if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
  521. ret = -ENOMEM;
  522. goto err3;
  523. }
  524. flush_dcache_range((unsigned)fec->rbd_base,
  525. (unsigned)fec->rbd_base + size);
  526. }
  527. fec_reg_setup(fec);
  528. if (fec->xcv_type != SEVENWIRE)
  529. fec_mii_setspeed(fec->bus->priv);
  530. /*
  531. * Set Opcode/Pause Duration Register
  532. */
  533. writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
  534. writel(0x2, &fec->eth->x_wmrk);
  535. /*
  536. * Set multicast address filter
  537. */
  538. writel(0x00000000, &fec->eth->gaddr1);
  539. writel(0x00000000, &fec->eth->gaddr2);
  540. /* clear MIB RAM */
  541. for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
  542. writel(0, i);
  543. /* FIFO receive start register */
  544. writel(0x520, &fec->eth->r_fstart);
  545. /* size and address of each buffer */
  546. writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
  547. writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
  548. writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
  549. #ifndef CONFIG_PHYLIB
  550. if (fec->xcv_type != SEVENWIRE)
  551. miiphy_restart_aneg(dev);
  552. #endif
  553. fec_open(dev);
  554. return 0;
  555. err3:
  556. free(fec->rbd_base);
  557. err2:
  558. free(fec->tbd_base);
  559. err1:
  560. return ret;
  561. }
  562. /**
  563. * Halt the FEC engine
  564. * @param[in] dev Our device to handle
  565. */
  566. static void fec_halt(struct eth_device *dev)
  567. {
  568. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  569. int counter = 0xffff;
  570. /*
  571. * issue graceful stop command to the FEC transmitter if necessary
  572. */
  573. writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
  574. &fec->eth->x_cntrl);
  575. debug("eth_halt: wait for stop regs\n");
  576. /*
  577. * wait for graceful stop to register
  578. */
  579. while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
  580. udelay(1);
  581. /*
  582. * Disable SmartDMA tasks
  583. */
  584. fec_tx_task_disable(fec);
  585. fec_rx_task_disable(fec);
  586. /*
  587. * Disable the Ethernet Controller
  588. * Note: this will also reset the BD index counter!
  589. */
  590. writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
  591. &fec->eth->ecntrl);
  592. fec->rbd_index = 0;
  593. fec->tbd_index = 0;
  594. debug("eth_halt: done\n");
  595. }
  596. /**
  597. * Transmit one frame
  598. * @param[in] dev Our ethernet device to handle
  599. * @param[in] packet Pointer to the data to be transmitted
  600. * @param[in] length Data count in bytes
  601. * @return 0 on success
  602. */
  603. static int fec_send(struct eth_device *dev, void *packet, int length)
  604. {
  605. unsigned int status;
  606. uint32_t size, end;
  607. uint32_t addr;
  608. int timeout = FEC_XFER_TIMEOUT;
  609. int ret = 0;
  610. /*
  611. * This routine transmits one frame. This routine only accepts
  612. * 6-byte Ethernet addresses.
  613. */
  614. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  615. /*
  616. * Check for valid length of data.
  617. */
  618. if ((length > 1500) || (length <= 0)) {
  619. printf("Payload (%d) too large\n", length);
  620. return -1;
  621. }
  622. /*
  623. * Setup the transmit buffer. We are always using the first buffer for
  624. * transmission, the second will be empty and only used to stop the DMA
  625. * engine. We also flush the packet to RAM here to avoid cache trouble.
  626. */
  627. #ifdef CONFIG_FEC_MXC_SWAP_PACKET
  628. swap_packet((uint32_t *)packet, length);
  629. #endif
  630. addr = (uint32_t)packet;
  631. end = roundup(addr + length, ARCH_DMA_MINALIGN);
  632. addr &= ~(ARCH_DMA_MINALIGN - 1);
  633. flush_dcache_range(addr, end);
  634. writew(length, &fec->tbd_base[fec->tbd_index].data_length);
  635. writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
  636. /*
  637. * update BD's status now
  638. * This block:
  639. * - is always the last in a chain (means no chain)
  640. * - should transmitt the CRC
  641. * - might be the last BD in the list, so the address counter should
  642. * wrap (-> keep the WRAP flag)
  643. */
  644. status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
  645. status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  646. writew(status, &fec->tbd_base[fec->tbd_index].status);
  647. /*
  648. * Flush data cache. This code flushes both TX descriptors to RAM.
  649. * After this code, the descriptors will be safely in RAM and we
  650. * can start DMA.
  651. */
  652. size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
  653. addr = (uint32_t)fec->tbd_base;
  654. flush_dcache_range(addr, addr + size);
  655. /*
  656. * Enable SmartDMA transmit task
  657. */
  658. fec_tx_task_enable(fec);
  659. /*
  660. * Wait until frame is sent. On each turn of the wait cycle, we must
  661. * invalidate data cache to see what's really in RAM. Also, we need
  662. * barrier here.
  663. */
  664. while (--timeout) {
  665. if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
  666. break;
  667. }
  668. if (!timeout)
  669. ret = -EINVAL;
  670. invalidate_dcache_range(addr, addr + size);
  671. if (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY)
  672. ret = -EINVAL;
  673. debug("fec_send: status 0x%x index %d ret %i\n",
  674. readw(&fec->tbd_base[fec->tbd_index].status),
  675. fec->tbd_index, ret);
  676. /* for next transmission use the other buffer */
  677. if (fec->tbd_index)
  678. fec->tbd_index = 0;
  679. else
  680. fec->tbd_index = 1;
  681. return ret;
  682. }
  683. /**
  684. * Pull one frame from the card
  685. * @param[in] dev Our ethernet device to handle
  686. * @return Length of packet read
  687. */
  688. static int fec_recv(struct eth_device *dev)
  689. {
  690. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  691. struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
  692. unsigned long ievent;
  693. int frame_length, len = 0;
  694. struct nbuf *frame;
  695. uint16_t bd_status;
  696. uint32_t addr, size, end;
  697. int i;
  698. uchar buff[FEC_MAX_PKT_SIZE] __aligned(ARCH_DMA_MINALIGN);
  699. /*
  700. * Check if any critical events have happened
  701. */
  702. ievent = readl(&fec->eth->ievent);
  703. writel(ievent, &fec->eth->ievent);
  704. debug("fec_recv: ievent 0x%lx\n", ievent);
  705. if (ievent & FEC_IEVENT_BABR) {
  706. fec_halt(dev);
  707. fec_init(dev, fec->bd);
  708. printf("some error: 0x%08lx\n", ievent);
  709. return 0;
  710. }
  711. if (ievent & FEC_IEVENT_HBERR) {
  712. /* Heartbeat error */
  713. writel(0x00000001 | readl(&fec->eth->x_cntrl),
  714. &fec->eth->x_cntrl);
  715. }
  716. if (ievent & FEC_IEVENT_GRA) {
  717. /* Graceful stop complete */
  718. if (readl(&fec->eth->x_cntrl) & 0x00000001) {
  719. fec_halt(dev);
  720. writel(~0x00000001 & readl(&fec->eth->x_cntrl),
  721. &fec->eth->x_cntrl);
  722. fec_init(dev, fec->bd);
  723. }
  724. }
  725. /*
  726. * Read the buffer status. Before the status can be read, the data cache
  727. * must be invalidated, because the data in RAM might have been changed
  728. * by DMA. The descriptors are properly aligned to cachelines so there's
  729. * no need to worry they'd overlap.
  730. *
  731. * WARNING: By invalidating the descriptor here, we also invalidate
  732. * the descriptors surrounding this one. Therefore we can NOT change the
  733. * contents of this descriptor nor the surrounding ones. The problem is
  734. * that in order to mark the descriptor as processed, we need to change
  735. * the descriptor. The solution is to mark the whole cache line when all
  736. * descriptors in the cache line are processed.
  737. */
  738. addr = (uint32_t)rbd;
  739. addr &= ~(ARCH_DMA_MINALIGN - 1);
  740. size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
  741. invalidate_dcache_range(addr, addr + size);
  742. bd_status = readw(&rbd->status);
  743. debug("fec_recv: status 0x%x\n", bd_status);
  744. if (!(bd_status & FEC_RBD_EMPTY)) {
  745. if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
  746. ((readw(&rbd->data_length) - 4) > 14)) {
  747. /*
  748. * Get buffer address and size
  749. */
  750. frame = (struct nbuf *)readl(&rbd->data_pointer);
  751. frame_length = readw(&rbd->data_length) - 4;
  752. /*
  753. * Invalidate data cache over the buffer
  754. */
  755. addr = (uint32_t)frame;
  756. end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
  757. addr &= ~(ARCH_DMA_MINALIGN - 1);
  758. invalidate_dcache_range(addr, end);
  759. /*
  760. * Fill the buffer and pass it to upper layers
  761. */
  762. #ifdef CONFIG_FEC_MXC_SWAP_PACKET
  763. swap_packet((uint32_t *)frame->data, frame_length);
  764. #endif
  765. memcpy(buff, frame->data, frame_length);
  766. NetReceive(buff, frame_length);
  767. len = frame_length;
  768. } else {
  769. if (bd_status & FEC_RBD_ERR)
  770. printf("error frame: 0x%08lx 0x%08x\n",
  771. (ulong)rbd->data_pointer,
  772. bd_status);
  773. }
  774. /*
  775. * Free the current buffer, restart the engine and move forward
  776. * to the next buffer. Here we check if the whole cacheline of
  777. * descriptors was already processed and if so, we mark it free
  778. * as whole.
  779. */
  780. size = RXDESC_PER_CACHELINE - 1;
  781. if ((fec->rbd_index & size) == size) {
  782. i = fec->rbd_index - size;
  783. addr = (uint32_t)&fec->rbd_base[i];
  784. for (; i <= fec->rbd_index ; i++) {
  785. fec_rbd_clean(i == (FEC_RBD_NUM - 1),
  786. &fec->rbd_base[i]);
  787. }
  788. flush_dcache_range(addr,
  789. addr + ARCH_DMA_MINALIGN);
  790. }
  791. fec_rx_task_enable(fec);
  792. fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
  793. }
  794. debug("fec_recv: stop\n");
  795. return len;
  796. }
  797. static int fec_probe(bd_t *bd, int dev_id, int phy_id, uint32_t base_addr)
  798. {
  799. struct eth_device *edev;
  800. struct fec_priv *fec;
  801. struct mii_dev *bus;
  802. unsigned char ethaddr[6];
  803. uint32_t start;
  804. int ret = 0;
  805. /* create and fill edev struct */
  806. edev = (struct eth_device *)malloc(sizeof(struct eth_device));
  807. if (!edev) {
  808. puts("fec_mxc: not enough malloc memory for eth_device\n");
  809. ret = -ENOMEM;
  810. goto err1;
  811. }
  812. fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
  813. if (!fec) {
  814. puts("fec_mxc: not enough malloc memory for fec_priv\n");
  815. ret = -ENOMEM;
  816. goto err2;
  817. }
  818. memset(edev, 0, sizeof(*edev));
  819. memset(fec, 0, sizeof(*fec));
  820. edev->priv = fec;
  821. edev->init = fec_init;
  822. edev->send = fec_send;
  823. edev->recv = fec_recv;
  824. edev->halt = fec_halt;
  825. edev->write_hwaddr = fec_set_hwaddr;
  826. fec->eth = (struct ethernet_regs *)base_addr;
  827. fec->bd = bd;
  828. fec->xcv_type = CONFIG_FEC_XCV_TYPE;
  829. /* Reset chip. */
  830. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
  831. start = get_timer(0);
  832. while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
  833. if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
  834. printf("FEC MXC: Timeout reseting chip\n");
  835. goto err3;
  836. }
  837. udelay(10);
  838. }
  839. fec_reg_setup(fec);
  840. if (dev_id == -1) {
  841. sprintf(edev->name, "FEC");
  842. fec->dev_id = 0;
  843. } else {
  844. sprintf(edev->name, "FEC%i", dev_id);
  845. fec->dev_id = dev_id;
  846. }
  847. fec->phy_id = phy_id;
  848. bus = mdio_alloc();
  849. if (!bus) {
  850. printf("mdio_alloc failed\n");
  851. ret = -ENOMEM;
  852. goto err3;
  853. }
  854. bus->read = fec_phy_read;
  855. bus->write = fec_phy_write;
  856. sprintf(bus->name, edev->name);
  857. #ifdef CONFIG_MX28
  858. /*
  859. * The i.MX28 has two ethernet interfaces, but they are not equal.
  860. * Only the first one can access the MDIO bus.
  861. */
  862. bus->priv = (struct ethernet_regs *)MXS_ENET0_BASE;
  863. #else
  864. bus->priv = fec->eth;
  865. #endif
  866. fec_mii_setspeed(bus->priv);
  867. ret = mdio_register(bus);
  868. if (ret) {
  869. printf("mdio_register failed\n");
  870. free(bus);
  871. ret = -ENOMEM;
  872. goto err3;
  873. }
  874. fec->bus = bus;
  875. eth_register(edev);
  876. if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
  877. debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
  878. memcpy(edev->enetaddr, ethaddr, 6);
  879. }
  880. /* Configure phy */
  881. fec_eth_phy_config(edev);
  882. return ret;
  883. err3:
  884. free(fec);
  885. err2:
  886. free(edev);
  887. err1:
  888. return ret;
  889. }
  890. #ifdef CONFIG_FEC_MXC_PHYADDR
  891. int fecmxc_initialize(bd_t *bd)
  892. {
  893. int lout = 1;
  894. debug("eth_init: fec_probe(bd)\n");
  895. lout = fec_probe(bd, -1, CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
  896. return lout;
  897. }
  898. #endif
  899. int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
  900. {
  901. int lout = 1;
  902. debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
  903. lout = fec_probe(bd, dev_id, phy_id, addr);
  904. return lout;
  905. }
  906. #ifndef CONFIG_PHYLIB
  907. int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
  908. {
  909. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  910. fec->mii_postcall = cb;
  911. return 0;
  912. }
  913. #endif