fec_mxc.c 26 KB

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