fec_mxc.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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;
  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. size = roundup(length, ARCH_DMA_MINALIGN);
  625. flush_dcache_range(addr, addr + size);
  626. writew(length, &fec->tbd_base[fec->tbd_index].data_length);
  627. writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
  628. /*
  629. * update BD's status now
  630. * This block:
  631. * - is always the last in a chain (means no chain)
  632. * - should transmitt the CRC
  633. * - might be the last BD in the list, so the address counter should
  634. * wrap (-> keep the WRAP flag)
  635. */
  636. status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
  637. status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  638. writew(status, &fec->tbd_base[fec->tbd_index].status);
  639. /*
  640. * Flush data cache. This code flushes both TX descriptors to RAM.
  641. * After this code, the descriptors will be safely in RAM and we
  642. * can start DMA.
  643. */
  644. size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
  645. addr = (uint32_t)fec->tbd_base;
  646. flush_dcache_range(addr, addr + size);
  647. /*
  648. * Enable SmartDMA transmit task
  649. */
  650. fec_tx_task_enable(fec);
  651. /*
  652. * Wait until frame is sent. On each turn of the wait cycle, we must
  653. * invalidate data cache to see what's really in RAM. Also, we need
  654. * barrier here.
  655. */
  656. invalidate_dcache_range(addr, addr + size);
  657. while (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY) {
  658. udelay(1);
  659. invalidate_dcache_range(addr, addr + size);
  660. }
  661. debug("fec_send: status 0x%x index %d\n",
  662. readw(&fec->tbd_base[fec->tbd_index].status),
  663. fec->tbd_index);
  664. /* for next transmission use the other buffer */
  665. if (fec->tbd_index)
  666. fec->tbd_index = 0;
  667. else
  668. fec->tbd_index = 1;
  669. return 0;
  670. }
  671. /**
  672. * Pull one frame from the card
  673. * @param[in] dev Our ethernet device to handle
  674. * @return Length of packet read
  675. */
  676. static int fec_recv(struct eth_device *dev)
  677. {
  678. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  679. struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
  680. unsigned long ievent;
  681. int frame_length, len = 0;
  682. struct nbuf *frame;
  683. uint16_t bd_status;
  684. uint32_t addr, size;
  685. int i;
  686. uchar buff[FEC_MAX_PKT_SIZE] __aligned(ARCH_DMA_MINALIGN);
  687. /*
  688. * Check if any critical events have happened
  689. */
  690. ievent = readl(&fec->eth->ievent);
  691. writel(ievent, &fec->eth->ievent);
  692. debug("fec_recv: ievent 0x%lx\n", ievent);
  693. if (ievent & FEC_IEVENT_BABR) {
  694. fec_halt(dev);
  695. fec_init(dev, fec->bd);
  696. printf("some error: 0x%08lx\n", ievent);
  697. return 0;
  698. }
  699. if (ievent & FEC_IEVENT_HBERR) {
  700. /* Heartbeat error */
  701. writel(0x00000001 | readl(&fec->eth->x_cntrl),
  702. &fec->eth->x_cntrl);
  703. }
  704. if (ievent & FEC_IEVENT_GRA) {
  705. /* Graceful stop complete */
  706. if (readl(&fec->eth->x_cntrl) & 0x00000001) {
  707. fec_halt(dev);
  708. writel(~0x00000001 & readl(&fec->eth->x_cntrl),
  709. &fec->eth->x_cntrl);
  710. fec_init(dev, fec->bd);
  711. }
  712. }
  713. /*
  714. * Read the buffer status. Before the status can be read, the data cache
  715. * must be invalidated, because the data in RAM might have been changed
  716. * by DMA. The descriptors are properly aligned to cachelines so there's
  717. * no need to worry they'd overlap.
  718. *
  719. * WARNING: By invalidating the descriptor here, we also invalidate
  720. * the descriptors surrounding this one. Therefore we can NOT change the
  721. * contents of this descriptor nor the surrounding ones. The problem is
  722. * that in order to mark the descriptor as processed, we need to change
  723. * the descriptor. The solution is to mark the whole cache line when all
  724. * descriptors in the cache line are processed.
  725. */
  726. addr = (uint32_t)rbd;
  727. addr &= ~(ARCH_DMA_MINALIGN - 1);
  728. size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
  729. invalidate_dcache_range(addr, addr + size);
  730. bd_status = readw(&rbd->status);
  731. debug("fec_recv: status 0x%x\n", bd_status);
  732. if (!(bd_status & FEC_RBD_EMPTY)) {
  733. if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
  734. ((readw(&rbd->data_length) - 4) > 14)) {
  735. /*
  736. * Get buffer address and size
  737. */
  738. frame = (struct nbuf *)readl(&rbd->data_pointer);
  739. frame_length = readw(&rbd->data_length) - 4;
  740. /*
  741. * Invalidate data cache over the buffer
  742. */
  743. addr = (uint32_t)frame;
  744. size = roundup(frame_length, ARCH_DMA_MINALIGN);
  745. invalidate_dcache_range(addr, addr + size);
  746. /*
  747. * Fill the buffer and pass it to upper layers
  748. */
  749. #ifdef CONFIG_FEC_MXC_SWAP_PACKET
  750. swap_packet((uint32_t *)frame->data, frame_length);
  751. #endif
  752. memcpy(buff, frame->data, frame_length);
  753. NetReceive(buff, frame_length);
  754. len = frame_length;
  755. } else {
  756. if (bd_status & FEC_RBD_ERR)
  757. printf("error frame: 0x%08lx 0x%08x\n",
  758. (ulong)rbd->data_pointer,
  759. bd_status);
  760. }
  761. /*
  762. * Free the current buffer, restart the engine and move forward
  763. * to the next buffer. Here we check if the whole cacheline of
  764. * descriptors was already processed and if so, we mark it free
  765. * as whole.
  766. */
  767. size = RXDESC_PER_CACHELINE - 1;
  768. if ((fec->rbd_index & size) == size) {
  769. i = fec->rbd_index - size;
  770. addr = (uint32_t)&fec->rbd_base[i];
  771. for (; i <= fec->rbd_index ; i++) {
  772. fec_rbd_clean(i == (FEC_RBD_NUM - 1),
  773. &fec->rbd_base[i]);
  774. }
  775. flush_dcache_range(addr,
  776. addr + ARCH_DMA_MINALIGN);
  777. }
  778. fec_rx_task_enable(fec);
  779. fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
  780. }
  781. debug("fec_recv: stop\n");
  782. return len;
  783. }
  784. static int fec_probe(bd_t *bd, int dev_id, int phy_id, uint32_t base_addr)
  785. {
  786. struct eth_device *edev;
  787. struct fec_priv *fec;
  788. struct mii_dev *bus;
  789. unsigned char ethaddr[6];
  790. uint32_t start;
  791. int ret = 0;
  792. /* create and fill edev struct */
  793. edev = (struct eth_device *)malloc(sizeof(struct eth_device));
  794. if (!edev) {
  795. puts("fec_mxc: not enough malloc memory for eth_device\n");
  796. ret = -ENOMEM;
  797. goto err1;
  798. }
  799. fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
  800. if (!fec) {
  801. puts("fec_mxc: not enough malloc memory for fec_priv\n");
  802. ret = -ENOMEM;
  803. goto err2;
  804. }
  805. memset(edev, 0, sizeof(*edev));
  806. memset(fec, 0, sizeof(*fec));
  807. edev->priv = fec;
  808. edev->init = fec_init;
  809. edev->send = fec_send;
  810. edev->recv = fec_recv;
  811. edev->halt = fec_halt;
  812. edev->write_hwaddr = fec_set_hwaddr;
  813. fec->eth = (struct ethernet_regs *)base_addr;
  814. fec->bd = bd;
  815. fec->xcv_type = CONFIG_FEC_XCV_TYPE;
  816. /* Reset chip. */
  817. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
  818. start = get_timer(0);
  819. while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
  820. if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
  821. printf("FEC MXC: Timeout reseting chip\n");
  822. goto err3;
  823. }
  824. udelay(10);
  825. }
  826. fec_reg_setup(fec);
  827. fec_mii_setspeed(fec);
  828. if (dev_id == -1) {
  829. sprintf(edev->name, "FEC");
  830. fec->dev_id = 0;
  831. } else {
  832. sprintf(edev->name, "FEC%i", dev_id);
  833. fec->dev_id = dev_id;
  834. }
  835. fec->phy_id = phy_id;
  836. bus = mdio_alloc();
  837. if (!bus) {
  838. printf("mdio_alloc failed\n");
  839. ret = -ENOMEM;
  840. goto err3;
  841. }
  842. bus->read = fec_phy_read;
  843. bus->write = fec_phy_write;
  844. sprintf(bus->name, edev->name);
  845. #ifdef CONFIG_MX28
  846. /*
  847. * The i.MX28 has two ethernet interfaces, but they are not equal.
  848. * Only the first one can access the MDIO bus.
  849. */
  850. bus->priv = (struct ethernet_regs *)MXS_ENET0_BASE;
  851. #else
  852. bus->priv = fec->eth;
  853. #endif
  854. ret = mdio_register(bus);
  855. if (ret) {
  856. printf("mdio_register failed\n");
  857. free(bus);
  858. ret = -ENOMEM;
  859. goto err3;
  860. }
  861. fec->bus = bus;
  862. eth_register(edev);
  863. if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
  864. debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
  865. memcpy(edev->enetaddr, ethaddr, 6);
  866. }
  867. /* Configure phy */
  868. fec_eth_phy_config(edev);
  869. return ret;
  870. err3:
  871. free(fec);
  872. err2:
  873. free(edev);
  874. err1:
  875. return ret;
  876. }
  877. #ifndef CONFIG_FEC_MXC_MULTI
  878. int fecmxc_initialize(bd_t *bd)
  879. {
  880. int lout = 1;
  881. debug("eth_init: fec_probe(bd)\n");
  882. lout = fec_probe(bd, -1, CONFIG_FEC_MXC_PHYADDR, IMX_FEC_BASE);
  883. return lout;
  884. }
  885. #endif
  886. int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
  887. {
  888. int lout = 1;
  889. debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
  890. lout = fec_probe(bd, dev_id, phy_id, addr);
  891. return lout;
  892. }
  893. #ifndef CONFIG_PHYLIB
  894. int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
  895. {
  896. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  897. fec->mii_postcall = cb;
  898. return 0;
  899. }
  900. #endif