fec_mxc.c 27 KB

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