fec_mxc.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. flush_dcache_range((unsigned)fec->tbd_base, size);
  484. }
  485. /*
  486. * Allocate receive descriptors. This allocation respects cache
  487. * alignment.
  488. */
  489. if (!fec->rbd_base) {
  490. size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
  491. ARCH_DMA_MINALIGN);
  492. fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
  493. if (!fec->rbd_base) {
  494. ret = -ENOMEM;
  495. goto err2;
  496. }
  497. memset(fec->rbd_base, 0, size);
  498. /*
  499. * Initialize RxBD ring
  500. */
  501. if (fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE) < 0) {
  502. ret = -ENOMEM;
  503. goto err3;
  504. }
  505. flush_dcache_range((unsigned)fec->rbd_base,
  506. (unsigned)fec->rbd_base + size);
  507. }
  508. fec_reg_setup(fec);
  509. if (fec->xcv_type != SEVENWIRE)
  510. fec_mii_setspeed(fec->bus->priv);
  511. /*
  512. * Set Opcode/Pause Duration Register
  513. */
  514. writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
  515. writel(0x2, &fec->eth->x_wmrk);
  516. /*
  517. * Set multicast address filter
  518. */
  519. writel(0x00000000, &fec->eth->gaddr1);
  520. writel(0x00000000, &fec->eth->gaddr2);
  521. /* clear MIB RAM */
  522. for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
  523. writel(0, i);
  524. /* FIFO receive start register */
  525. writel(0x520, &fec->eth->r_fstart);
  526. /* size and address of each buffer */
  527. writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
  528. writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
  529. writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
  530. #ifndef CONFIG_PHYLIB
  531. if (fec->xcv_type != SEVENWIRE)
  532. miiphy_restart_aneg(dev);
  533. #endif
  534. fec_open(dev);
  535. return 0;
  536. err3:
  537. free(fec->rbd_base);
  538. err2:
  539. free(fec->tbd_base);
  540. err1:
  541. return ret;
  542. }
  543. /**
  544. * Halt the FEC engine
  545. * @param[in] dev Our device to handle
  546. */
  547. static void fec_halt(struct eth_device *dev)
  548. {
  549. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  550. int counter = 0xffff;
  551. /*
  552. * issue graceful stop command to the FEC transmitter if necessary
  553. */
  554. writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
  555. &fec->eth->x_cntrl);
  556. debug("eth_halt: wait for stop regs\n");
  557. /*
  558. * wait for graceful stop to register
  559. */
  560. while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
  561. udelay(1);
  562. /*
  563. * Disable SmartDMA tasks
  564. */
  565. fec_tx_task_disable(fec);
  566. fec_rx_task_disable(fec);
  567. /*
  568. * Disable the Ethernet Controller
  569. * Note: this will also reset the BD index counter!
  570. */
  571. writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
  572. &fec->eth->ecntrl);
  573. fec->rbd_index = 0;
  574. fec->tbd_index = 0;
  575. debug("eth_halt: done\n");
  576. }
  577. /**
  578. * Transmit one frame
  579. * @param[in] dev Our ethernet device to handle
  580. * @param[in] packet Pointer to the data to be transmitted
  581. * @param[in] length Data count in bytes
  582. * @return 0 on success
  583. */
  584. static int fec_send(struct eth_device *dev, void *packet, int length)
  585. {
  586. unsigned int status;
  587. uint32_t size, end;
  588. uint32_t addr;
  589. int timeout = FEC_XFER_TIMEOUT;
  590. int ret = 0;
  591. /*
  592. * This routine transmits one frame. This routine only accepts
  593. * 6-byte Ethernet addresses.
  594. */
  595. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  596. /*
  597. * Check for valid length of data.
  598. */
  599. if ((length > 1500) || (length <= 0)) {
  600. printf("Payload (%d) too large\n", length);
  601. return -1;
  602. }
  603. /*
  604. * Setup the transmit buffer. We are always using the first buffer for
  605. * transmission, the second will be empty and only used to stop the DMA
  606. * engine. We also flush the packet to RAM here to avoid cache trouble.
  607. */
  608. #ifdef CONFIG_FEC_MXC_SWAP_PACKET
  609. swap_packet((uint32_t *)packet, length);
  610. #endif
  611. addr = (uint32_t)packet;
  612. end = roundup(addr + length, ARCH_DMA_MINALIGN);
  613. addr &= ~(ARCH_DMA_MINALIGN - 1);
  614. flush_dcache_range(addr, end);
  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. while (--timeout) {
  646. if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
  647. break;
  648. }
  649. if (!timeout)
  650. ret = -EINVAL;
  651. invalidate_dcache_range(addr, addr + size);
  652. if (readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_READY)
  653. ret = -EINVAL;
  654. debug("fec_send: status 0x%x index %d ret %i\n",
  655. readw(&fec->tbd_base[fec->tbd_index].status),
  656. fec->tbd_index, ret);
  657. /* for next transmission use the other buffer */
  658. if (fec->tbd_index)
  659. fec->tbd_index = 0;
  660. else
  661. fec->tbd_index = 1;
  662. return ret;
  663. }
  664. /**
  665. * Pull one frame from the card
  666. * @param[in] dev Our ethernet device to handle
  667. * @return Length of packet read
  668. */
  669. static int fec_recv(struct eth_device *dev)
  670. {
  671. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  672. struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
  673. unsigned long ievent;
  674. int frame_length, len = 0;
  675. struct nbuf *frame;
  676. uint16_t bd_status;
  677. uint32_t addr, size, end;
  678. int i;
  679. uchar buff[FEC_MAX_PKT_SIZE] __aligned(ARCH_DMA_MINALIGN);
  680. /*
  681. * Check if any critical events have happened
  682. */
  683. ievent = readl(&fec->eth->ievent);
  684. writel(ievent, &fec->eth->ievent);
  685. debug("fec_recv: ievent 0x%lx\n", ievent);
  686. if (ievent & FEC_IEVENT_BABR) {
  687. fec_halt(dev);
  688. fec_init(dev, fec->bd);
  689. printf("some error: 0x%08lx\n", ievent);
  690. return 0;
  691. }
  692. if (ievent & FEC_IEVENT_HBERR) {
  693. /* Heartbeat error */
  694. writel(0x00000001 | readl(&fec->eth->x_cntrl),
  695. &fec->eth->x_cntrl);
  696. }
  697. if (ievent & FEC_IEVENT_GRA) {
  698. /* Graceful stop complete */
  699. if (readl(&fec->eth->x_cntrl) & 0x00000001) {
  700. fec_halt(dev);
  701. writel(~0x00000001 & readl(&fec->eth->x_cntrl),
  702. &fec->eth->x_cntrl);
  703. fec_init(dev, fec->bd);
  704. }
  705. }
  706. /*
  707. * Read the buffer status. Before the status can be read, the data cache
  708. * must be invalidated, because the data in RAM might have been changed
  709. * by DMA. The descriptors are properly aligned to cachelines so there's
  710. * no need to worry they'd overlap.
  711. *
  712. * WARNING: By invalidating the descriptor here, we also invalidate
  713. * the descriptors surrounding this one. Therefore we can NOT change the
  714. * contents of this descriptor nor the surrounding ones. The problem is
  715. * that in order to mark the descriptor as processed, we need to change
  716. * the descriptor. The solution is to mark the whole cache line when all
  717. * descriptors in the cache line are processed.
  718. */
  719. addr = (uint32_t)rbd;
  720. addr &= ~(ARCH_DMA_MINALIGN - 1);
  721. size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
  722. invalidate_dcache_range(addr, addr + size);
  723. bd_status = readw(&rbd->status);
  724. debug("fec_recv: status 0x%x\n", bd_status);
  725. if (!(bd_status & FEC_RBD_EMPTY)) {
  726. if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
  727. ((readw(&rbd->data_length) - 4) > 14)) {
  728. /*
  729. * Get buffer address and size
  730. */
  731. frame = (struct nbuf *)readl(&rbd->data_pointer);
  732. frame_length = readw(&rbd->data_length) - 4;
  733. /*
  734. * Invalidate data cache over the buffer
  735. */
  736. addr = (uint32_t)frame;
  737. end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
  738. addr &= ~(ARCH_DMA_MINALIGN - 1);
  739. invalidate_dcache_range(addr, end);
  740. /*
  741. * Fill the buffer and pass it to upper layers
  742. */
  743. #ifdef CONFIG_FEC_MXC_SWAP_PACKET
  744. swap_packet((uint32_t *)frame->data, frame_length);
  745. #endif
  746. memcpy(buff, frame->data, frame_length);
  747. NetReceive(buff, frame_length);
  748. len = frame_length;
  749. } else {
  750. if (bd_status & FEC_RBD_ERR)
  751. printf("error frame: 0x%08lx 0x%08x\n",
  752. (ulong)rbd->data_pointer,
  753. bd_status);
  754. }
  755. /*
  756. * Free the current buffer, restart the engine and move forward
  757. * to the next buffer. Here we check if the whole cacheline of
  758. * descriptors was already processed and if so, we mark it free
  759. * as whole.
  760. */
  761. size = RXDESC_PER_CACHELINE - 1;
  762. if ((fec->rbd_index & size) == size) {
  763. i = fec->rbd_index - size;
  764. addr = (uint32_t)&fec->rbd_base[i];
  765. for (; i <= fec->rbd_index ; i++) {
  766. fec_rbd_clean(i == (FEC_RBD_NUM - 1),
  767. &fec->rbd_base[i]);
  768. }
  769. flush_dcache_range(addr,
  770. addr + ARCH_DMA_MINALIGN);
  771. }
  772. fec_rx_task_enable(fec);
  773. fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
  774. }
  775. debug("fec_recv: stop\n");
  776. return len;
  777. }
  778. static void fec_set_dev_name(char *dest, int dev_id)
  779. {
  780. sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
  781. }
  782. #ifdef CONFIG_PHYLIB
  783. int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
  784. struct mii_dev *bus, struct phy_device *phydev)
  785. #else
  786. static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
  787. struct mii_dev *bus, int phy_id)
  788. #endif
  789. {
  790. struct eth_device *edev;
  791. struct fec_priv *fec;
  792. unsigned char ethaddr[6];
  793. uint32_t start;
  794. int ret = 0;
  795. /* create and fill edev struct */
  796. edev = (struct eth_device *)malloc(sizeof(struct eth_device));
  797. if (!edev) {
  798. puts("fec_mxc: not enough malloc memory for eth_device\n");
  799. ret = -ENOMEM;
  800. goto err1;
  801. }
  802. fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
  803. if (!fec) {
  804. puts("fec_mxc: not enough malloc memory for fec_priv\n");
  805. ret = -ENOMEM;
  806. goto err2;
  807. }
  808. memset(edev, 0, sizeof(*edev));
  809. memset(fec, 0, sizeof(*fec));
  810. edev->priv = fec;
  811. edev->init = fec_init;
  812. edev->send = fec_send;
  813. edev->recv = fec_recv;
  814. edev->halt = fec_halt;
  815. edev->write_hwaddr = fec_set_hwaddr;
  816. fec->eth = (struct ethernet_regs *)base_addr;
  817. fec->bd = bd;
  818. fec->xcv_type = CONFIG_FEC_XCV_TYPE;
  819. /* Reset chip. */
  820. writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
  821. start = get_timer(0);
  822. while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
  823. if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
  824. printf("FEC MXC: Timeout reseting chip\n");
  825. goto err3;
  826. }
  827. udelay(10);
  828. }
  829. fec_reg_setup(fec);
  830. fec_set_dev_name(edev->name, dev_id);
  831. fec->dev_id = (dev_id == -1) ? 0 : dev_id;
  832. fec->bus = bus;
  833. fec_mii_setspeed(bus->priv);
  834. #ifdef CONFIG_PHYLIB
  835. fec->phydev = phydev;
  836. phy_connect_dev(phydev, edev);
  837. /* Configure phy */
  838. phy_config(phydev);
  839. #else
  840. fec->phy_id = phy_id;
  841. #endif
  842. eth_register(edev);
  843. if (fec_get_hwaddr(edev, dev_id, ethaddr) == 0) {
  844. debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
  845. memcpy(edev->enetaddr, ethaddr, 6);
  846. }
  847. return ret;
  848. err3:
  849. free(fec);
  850. err2:
  851. free(edev);
  852. err1:
  853. return ret;
  854. }
  855. struct mii_dev *fec_get_miibus(uint32_t base_addr, int dev_id)
  856. {
  857. struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
  858. struct mii_dev *bus;
  859. int ret;
  860. bus = mdio_alloc();
  861. if (!bus) {
  862. printf("mdio_alloc failed\n");
  863. return NULL;
  864. }
  865. bus->read = fec_phy_read;
  866. bus->write = fec_phy_write;
  867. bus->priv = eth;
  868. fec_set_dev_name(bus->name, dev_id);
  869. ret = mdio_register(bus);
  870. if (ret) {
  871. printf("mdio_register failed\n");
  872. free(bus);
  873. return NULL;
  874. }
  875. fec_mii_setspeed(eth);
  876. return bus;
  877. }
  878. int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
  879. {
  880. uint32_t base_mii;
  881. struct mii_dev *bus = NULL;
  882. #ifdef CONFIG_PHYLIB
  883. struct phy_device *phydev = NULL;
  884. #endif
  885. int ret;
  886. #ifdef CONFIG_MX28
  887. /*
  888. * The i.MX28 has two ethernet interfaces, but they are not equal.
  889. * Only the first one can access the MDIO bus.
  890. */
  891. base_mii = MXS_ENET0_BASE;
  892. #else
  893. base_mii = addr;
  894. #endif
  895. debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
  896. bus = fec_get_miibus(base_mii, dev_id);
  897. if (!bus)
  898. return -ENOMEM;
  899. #ifdef CONFIG_PHYLIB
  900. phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
  901. if (!phydev) {
  902. free(bus);
  903. return -ENOMEM;
  904. }
  905. ret = fec_probe(bd, dev_id, addr, bus, phydev);
  906. #else
  907. ret = fec_probe(bd, dev_id, addr, bus, phy_id);
  908. #endif
  909. if (ret) {
  910. #ifdef CONFIG_PHYLIB
  911. free(phydev);
  912. #endif
  913. free(bus);
  914. }
  915. return ret;
  916. }
  917. #ifdef CONFIG_FEC_MXC_PHYADDR
  918. int fecmxc_initialize(bd_t *bd)
  919. {
  920. return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
  921. IMX_FEC_BASE);
  922. }
  923. #endif
  924. #ifndef CONFIG_PHYLIB
  925. int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
  926. {
  927. struct fec_priv *fec = (struct fec_priv *)dev->priv;
  928. fec->mii_postcall = cb;
  929. return 0;
  930. }
  931. #endif