davinci_emac.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * Ethernet driver for TI TMS320DM644x (DaVinci) chips.
  3. *
  4. * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
  5. *
  6. * Parts shamelessly stolen from TI's dm644x_emac.c. Original copyright
  7. * follows:
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * dm644x_emac.c
  12. *
  13. * TI DaVinci (DM644X) EMAC peripheral driver source for DV-EVM
  14. *
  15. * Copyright (C) 2005 Texas Instruments.
  16. *
  17. * ----------------------------------------------------------------------------
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. * ----------------------------------------------------------------------------
  33. * Modifications:
  34. * ver. 1.0: Sep 2005, Anant Gole - Created EMAC version for uBoot.
  35. * ver 1.1: Nov 2005, Anant Gole - Extended the RX logic for multiple descriptors
  36. *
  37. */
  38. #include <common.h>
  39. #include <command.h>
  40. #include <net.h>
  41. #include <miiphy.h>
  42. #include <malloc.h>
  43. #include <asm/arch/emac_defs.h>
  44. #include <asm/io.h>
  45. unsigned int emac_dbg = 0;
  46. #define debug_emac(fmt,args...) if (emac_dbg) printf(fmt,##args)
  47. #ifdef DAVINCI_EMAC_GIG_ENABLE
  48. #define emac_gigabit_enable() davinci_eth_gigabit_enable()
  49. #else
  50. #define emac_gigabit_enable() /* no gigabit to enable */
  51. #endif
  52. static void davinci_eth_mdio_enable(void);
  53. static int gen_init_phy(int phy_addr);
  54. static int gen_is_phy_connected(int phy_addr);
  55. static int gen_get_link_speed(int phy_addr);
  56. static int gen_auto_negotiate(int phy_addr);
  57. void eth_mdio_enable(void)
  58. {
  59. davinci_eth_mdio_enable();
  60. }
  61. /* EMAC Addresses */
  62. static volatile emac_regs *adap_emac = (emac_regs *)EMAC_BASE_ADDR;
  63. static volatile ewrap_regs *adap_ewrap = (ewrap_regs *)EMAC_WRAPPER_BASE_ADDR;
  64. static volatile mdio_regs *adap_mdio = (mdio_regs *)EMAC_MDIO_BASE_ADDR;
  65. /* EMAC descriptors */
  66. static volatile emac_desc *emac_rx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_RX_DESC_BASE);
  67. static volatile emac_desc *emac_tx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_TX_DESC_BASE);
  68. static volatile emac_desc *emac_rx_active_head = 0;
  69. static volatile emac_desc *emac_rx_active_tail = 0;
  70. static int emac_rx_queue_active = 0;
  71. /* Receive packet buffers */
  72. static unsigned char emac_rx_buffers[EMAC_MAX_RX_BUFFERS * (EMAC_MAX_ETHERNET_PKT_SIZE + EMAC_PKT_ALIGN)];
  73. /* PHY address for a discovered PHY (0xff - not found) */
  74. static volatile u_int8_t active_phy_addr = 0xff;
  75. phy_t phy;
  76. static int davinci_eth_set_mac_addr(struct eth_device *dev)
  77. {
  78. unsigned long mac_hi;
  79. unsigned long mac_lo;
  80. /*
  81. * Set MAC Addresses & Init multicast Hash to 0 (disable any multicast
  82. * receive)
  83. * Using channel 0 only - other channels are disabled
  84. * */
  85. writel(0, &adap_emac->MACINDEX);
  86. mac_hi = (dev->enetaddr[3] << 24) |
  87. (dev->enetaddr[2] << 16) |
  88. (dev->enetaddr[1] << 8) |
  89. (dev->enetaddr[0]);
  90. mac_lo = (dev->enetaddr[5] << 8) |
  91. (dev->enetaddr[4]);
  92. writel(mac_hi, &adap_emac->MACADDRHI);
  93. #if defined(DAVINCI_EMAC_VERSION2)
  94. writel(mac_lo | EMAC_MAC_ADDR_IS_VALID | EMAC_MAC_ADDR_MATCH,
  95. &adap_emac->MACADDRLO);
  96. #else
  97. writel(mac_lo, &adap_emac->MACADDRLO);
  98. #endif
  99. writel(0, &adap_emac->MACHASH1);
  100. writel(0, &adap_emac->MACHASH2);
  101. /* Set source MAC address - REQUIRED */
  102. writel(mac_hi, &adap_emac->MACSRCADDRHI);
  103. writel(mac_lo, &adap_emac->MACSRCADDRLO);
  104. return 0;
  105. }
  106. static void davinci_eth_mdio_enable(void)
  107. {
  108. u_int32_t clkdiv;
  109. clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1;
  110. writel((clkdiv & 0xff) |
  111. MDIO_CONTROL_ENABLE |
  112. MDIO_CONTROL_FAULT |
  113. MDIO_CONTROL_FAULT_ENABLE,
  114. &adap_mdio->CONTROL);
  115. while (readl(&adap_mdio->CONTROL) & MDIO_CONTROL_IDLE)
  116. ;
  117. }
  118. /*
  119. * Tries to find an active connected PHY. Returns 1 if address if found.
  120. * If no active PHY (or more than one PHY) found returns 0.
  121. * Sets active_phy_addr variable.
  122. */
  123. static int davinci_eth_phy_detect(void)
  124. {
  125. u_int32_t phy_act_state;
  126. int i;
  127. active_phy_addr = 0xff;
  128. phy_act_state = readl(&adap_mdio->ALIVE) & EMAC_MDIO_PHY_MASK;
  129. if (phy_act_state == 0)
  130. return(0); /* No active PHYs */
  131. debug_emac("davinci_eth_phy_detect(), ALIVE = 0x%08x\n", phy_act_state);
  132. for (i = 0; i < 32; i++) {
  133. if (phy_act_state & (1 << i)) {
  134. if (phy_act_state & ~(1 << i))
  135. return(0); /* More than one PHY */
  136. else {
  137. active_phy_addr = i;
  138. return(1);
  139. }
  140. }
  141. }
  142. return(0); /* Just to make GCC happy */
  143. }
  144. /* Read a PHY register via MDIO inteface. Returns 1 on success, 0 otherwise */
  145. int davinci_eth_phy_read(u_int8_t phy_addr, u_int8_t reg_num, u_int16_t *data)
  146. {
  147. int tmp;
  148. while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
  149. ;
  150. writel(MDIO_USERACCESS0_GO |
  151. MDIO_USERACCESS0_WRITE_READ |
  152. ((reg_num & 0x1f) << 21) |
  153. ((phy_addr & 0x1f) << 16),
  154. &adap_mdio->USERACCESS0);
  155. /* Wait for command to complete */
  156. while ((tmp = readl(&adap_mdio->USERACCESS0)) & MDIO_USERACCESS0_GO)
  157. ;
  158. if (tmp & MDIO_USERACCESS0_ACK) {
  159. *data = tmp & 0xffff;
  160. return(1);
  161. }
  162. *data = -1;
  163. return(0);
  164. }
  165. /* Write to a PHY register via MDIO inteface. Blocks until operation is complete. */
  166. int davinci_eth_phy_write(u_int8_t phy_addr, u_int8_t reg_num, u_int16_t data)
  167. {
  168. while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
  169. ;
  170. writel(MDIO_USERACCESS0_GO |
  171. MDIO_USERACCESS0_WRITE_WRITE |
  172. ((reg_num & 0x1f) << 21) |
  173. ((phy_addr & 0x1f) << 16) |
  174. (data & 0xffff),
  175. &adap_mdio->USERACCESS0);
  176. /* Wait for command to complete */
  177. while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
  178. ;
  179. return(1);
  180. }
  181. /* PHY functions for a generic PHY */
  182. static int gen_init_phy(int phy_addr)
  183. {
  184. int ret = 1;
  185. if (gen_get_link_speed(phy_addr)) {
  186. /* Try another time */
  187. ret = gen_get_link_speed(phy_addr);
  188. }
  189. return(ret);
  190. }
  191. static int gen_is_phy_connected(int phy_addr)
  192. {
  193. u_int16_t dummy;
  194. return(davinci_eth_phy_read(phy_addr, PHY_PHYIDR1, &dummy));
  195. }
  196. static int gen_get_link_speed(int phy_addr)
  197. {
  198. u_int16_t tmp;
  199. if (davinci_eth_phy_read(phy_addr, MII_STATUS_REG, &tmp) &&
  200. (tmp & 0x04)) {
  201. #if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
  202. defined(CONFIG_MACH_DAVINCI_DA850_EVM)
  203. davinci_eth_phy_read(phy_addr, PHY_ANLPAR, &tmp);
  204. /* Speed doesn't matter, there is no setting for it in EMAC. */
  205. if (tmp & (PHY_ANLPAR_TXFD | PHY_ANLPAR_10FD)) {
  206. /* set EMAC for Full Duplex */
  207. writel(EMAC_MACCONTROL_MIIEN_ENABLE |
  208. EMAC_MACCONTROL_FULLDUPLEX_ENABLE,
  209. &adap_emac->MACCONTROL);
  210. } else {
  211. /*set EMAC for Half Duplex */
  212. writel(EMAC_MACCONTROL_MIIEN_ENABLE,
  213. &adap_emac->MACCONTROL);
  214. }
  215. if (tmp & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX))
  216. writel(readl(&adap_emac->MACCONTROL) |
  217. EMAC_MACCONTROL_RMIISPEED_100,
  218. &adap_emac->MACCONTROL);
  219. else
  220. writel(readl(&adap_emac->MACCONTROL) &
  221. ~EMAC_MACCONTROL_RMIISPEED_100,
  222. &adap_emac->MACCONTROL);
  223. #endif
  224. return(1);
  225. }
  226. return(0);
  227. }
  228. static int gen_auto_negotiate(int phy_addr)
  229. {
  230. u_int16_t tmp;
  231. if (!davinci_eth_phy_read(phy_addr, PHY_BMCR, &tmp))
  232. return(0);
  233. /* Restart Auto_negotiation */
  234. tmp |= PHY_BMCR_AUTON;
  235. davinci_eth_phy_write(phy_addr, PHY_BMCR, tmp);
  236. /*check AutoNegotiate complete */
  237. udelay (10000);
  238. if (!davinci_eth_phy_read(phy_addr, PHY_BMSR, &tmp))
  239. return(0);
  240. if (!(tmp & PHY_BMSR_AUTN_COMP))
  241. return(0);
  242. return(gen_get_link_speed(phy_addr));
  243. }
  244. /* End of generic PHY functions */
  245. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  246. static int davinci_mii_phy_read(const char *devname, unsigned char addr, unsigned char reg, unsigned short *value)
  247. {
  248. return(davinci_eth_phy_read(addr, reg, value) ? 0 : 1);
  249. }
  250. static int davinci_mii_phy_write(const char *devname, unsigned char addr, unsigned char reg, unsigned short value)
  251. {
  252. return(davinci_eth_phy_write(addr, reg, value) ? 0 : 1);
  253. }
  254. #endif
  255. static void __attribute__((unused)) davinci_eth_gigabit_enable(void)
  256. {
  257. u_int16_t data;
  258. if (davinci_eth_phy_read(EMAC_MDIO_PHY_NUM, 0, &data)) {
  259. if (data & (1 << 6)) { /* speed selection MSB */
  260. /*
  261. * Check if link detected is giga-bit
  262. * If Gigabit mode detected, enable gigbit in MAC
  263. */
  264. writel(EMAC_MACCONTROL_GIGFORCE |
  265. EMAC_MACCONTROL_GIGABIT_ENABLE,
  266. &adap_emac->MACCONTROL);
  267. }
  268. }
  269. }
  270. /* Eth device open */
  271. static int davinci_eth_open(struct eth_device *dev, bd_t *bis)
  272. {
  273. dv_reg_p addr;
  274. u_int32_t clkdiv, cnt;
  275. volatile emac_desc *rx_desc;
  276. debug_emac("+ emac_open\n");
  277. /* Reset EMAC module and disable interrupts in wrapper */
  278. writel(1, &adap_emac->SOFTRESET);
  279. while (readl(&adap_emac->SOFTRESET) != 0)
  280. ;
  281. #if defined(DAVINCI_EMAC_VERSION2)
  282. writel(1, &adap_ewrap->softrst);
  283. while (readl(&adap_ewrap->softrst) != 0)
  284. ;
  285. #else
  286. writel(0, &adap_ewrap->EWCTL);
  287. for (cnt = 0; cnt < 5; cnt++) {
  288. clkdiv = readl(&adap_ewrap->EWCTL);
  289. }
  290. #endif
  291. #if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
  292. defined(CONFIG_MACH_DAVINCI_DA850_EVM)
  293. adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
  294. adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
  295. adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
  296. #endif
  297. rx_desc = emac_rx_desc;
  298. writel(1, &adap_emac->TXCONTROL);
  299. writel(1, &adap_emac->RXCONTROL);
  300. davinci_eth_set_mac_addr(dev);
  301. /* Set DMA 8 TX / 8 RX Head pointers to 0 */
  302. addr = &adap_emac->TX0HDP;
  303. for(cnt = 0; cnt < 16; cnt++)
  304. writel(0, addr++);
  305. addr = &adap_emac->RX0HDP;
  306. for(cnt = 0; cnt < 16; cnt++)
  307. writel(0, addr++);
  308. /* Clear Statistics (do this before setting MacControl register) */
  309. addr = &adap_emac->RXGOODFRAMES;
  310. for(cnt = 0; cnt < EMAC_NUM_STATS; cnt++)
  311. writel(0, addr++);
  312. /* No multicast addressing */
  313. writel(0, &adap_emac->MACHASH1);
  314. writel(0, &adap_emac->MACHASH2);
  315. /* Create RX queue and set receive process in place */
  316. emac_rx_active_head = emac_rx_desc;
  317. for (cnt = 0; cnt < EMAC_MAX_RX_BUFFERS; cnt++) {
  318. rx_desc->next = (u_int32_t)(rx_desc + 1);
  319. rx_desc->buffer = &emac_rx_buffers[cnt * (EMAC_MAX_ETHERNET_PKT_SIZE + EMAC_PKT_ALIGN)];
  320. rx_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
  321. rx_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
  322. rx_desc++;
  323. }
  324. /* Finalize the rx desc list */
  325. rx_desc--;
  326. rx_desc->next = 0;
  327. emac_rx_active_tail = rx_desc;
  328. emac_rx_queue_active = 1;
  329. /* Enable TX/RX */
  330. writel(EMAC_MAX_ETHERNET_PKT_SIZE, &adap_emac->RXMAXLEN);
  331. writel(0, &adap_emac->RXBUFFEROFFSET);
  332. /*
  333. * No fancy configs - Use this for promiscous debug
  334. * - EMAC_RXMBPENABLE_RXCAFEN_ENABLE
  335. */
  336. writel(EMAC_RXMBPENABLE_RXBROADEN, &adap_emac->RXMBPENABLE);
  337. /* Enable ch 0 only */
  338. writel(1, &adap_emac->RXUNICASTSET);
  339. /* Enable MII interface and Full duplex mode */
  340. #ifdef CONFIG_SOC_DA8XX
  341. writel((EMAC_MACCONTROL_MIIEN_ENABLE |
  342. EMAC_MACCONTROL_FULLDUPLEX_ENABLE |
  343. EMAC_MACCONTROL_RMIISPEED_100),
  344. &adap_emac->MACCONTROL);
  345. #else
  346. writel((EMAC_MACCONTROL_MIIEN_ENABLE |
  347. EMAC_MACCONTROL_FULLDUPLEX_ENABLE),
  348. &adap_emac->MACCONTROL);
  349. #endif
  350. /* Init MDIO & get link state */
  351. clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1;
  352. writel((clkdiv & 0xff) | MDIO_CONTROL_ENABLE | MDIO_CONTROL_FAULT,
  353. &adap_mdio->CONTROL);
  354. /* We need to wait for MDIO to start */
  355. udelay(1000);
  356. if (!phy.get_link_speed(active_phy_addr))
  357. return(0);
  358. emac_gigabit_enable();
  359. /* Start receive process */
  360. writel((u_int32_t)emac_rx_desc, &adap_emac->RX0HDP);
  361. debug_emac("- emac_open\n");
  362. return(1);
  363. }
  364. /* EMAC Channel Teardown */
  365. static void davinci_eth_ch_teardown(int ch)
  366. {
  367. dv_reg dly = 0xff;
  368. dv_reg cnt;
  369. debug_emac("+ emac_ch_teardown\n");
  370. if (ch == EMAC_CH_TX) {
  371. /* Init TX channel teardown */
  372. writel(1, &adap_emac->TXTEARDOWN);
  373. do {
  374. /*
  375. * Wait here for Tx teardown completion interrupt to
  376. * occur. Note: A task delay can be called here to pend
  377. * rather than occupying CPU cycles - anyway it has
  378. * been found that teardown takes very few cpu cycles
  379. * and does not affect functionality
  380. */
  381. dly--;
  382. udelay(1);
  383. if (dly == 0)
  384. break;
  385. cnt = readl(&adap_emac->TX0CP);
  386. } while (cnt != 0xfffffffc);
  387. writel(cnt, &adap_emac->TX0CP);
  388. writel(0, &adap_emac->TX0HDP);
  389. } else {
  390. /* Init RX channel teardown */
  391. writel(1, &adap_emac->RXTEARDOWN);
  392. do {
  393. /*
  394. * Wait here for Rx teardown completion interrupt to
  395. * occur. Note: A task delay can be called here to pend
  396. * rather than occupying CPU cycles - anyway it has
  397. * been found that teardown takes very few cpu cycles
  398. * and does not affect functionality
  399. */
  400. dly--;
  401. udelay(1);
  402. if (dly == 0)
  403. break;
  404. cnt = readl(&adap_emac->RX0CP);
  405. } while (cnt != 0xfffffffc);
  406. writel(cnt, &adap_emac->RX0CP);
  407. writel(0, &adap_emac->RX0HDP);
  408. }
  409. debug_emac("- emac_ch_teardown\n");
  410. }
  411. /* Eth device close */
  412. static void davinci_eth_close(struct eth_device *dev)
  413. {
  414. debug_emac("+ emac_close\n");
  415. davinci_eth_ch_teardown(EMAC_CH_TX); /* TX Channel teardown */
  416. davinci_eth_ch_teardown(EMAC_CH_RX); /* RX Channel teardown */
  417. /* Reset EMAC module and disable interrupts in wrapper */
  418. writel(1, &adap_emac->SOFTRESET);
  419. #if defined(DAVINCI_EMAC_VERSION2)
  420. writel(1, &adap_ewrap->softrst);
  421. #else
  422. writel(0, &adap_ewrap->EWCTL);
  423. #endif
  424. #if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
  425. defined(CONFIG_MACH_DAVINCI_DA850_EVM)
  426. adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
  427. adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
  428. adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
  429. #endif
  430. debug_emac("- emac_close\n");
  431. }
  432. static int tx_send_loop = 0;
  433. /*
  434. * This function sends a single packet on the network and returns
  435. * positive number (number of bytes transmitted) or negative for error
  436. */
  437. static int davinci_eth_send_packet (struct eth_device *dev,
  438. volatile void *packet, int length)
  439. {
  440. int ret_status = -1;
  441. tx_send_loop = 0;
  442. /* Return error if no link */
  443. if (!phy.get_link_speed (active_phy_addr)) {
  444. printf ("WARN: emac_send_packet: No link\n");
  445. return (ret_status);
  446. }
  447. emac_gigabit_enable();
  448. /* Check packet size and if < EMAC_MIN_ETHERNET_PKT_SIZE, pad it up */
  449. if (length < EMAC_MIN_ETHERNET_PKT_SIZE) {
  450. length = EMAC_MIN_ETHERNET_PKT_SIZE;
  451. }
  452. /* Populate the TX descriptor */
  453. emac_tx_desc->next = 0;
  454. emac_tx_desc->buffer = (u_int8_t *) packet;
  455. emac_tx_desc->buff_off_len = (length & 0xffff);
  456. emac_tx_desc->pkt_flag_len = ((length & 0xffff) |
  457. EMAC_CPPI_SOP_BIT |
  458. EMAC_CPPI_OWNERSHIP_BIT |
  459. EMAC_CPPI_EOP_BIT);
  460. /* Send the packet */
  461. writel((unsigned long)emac_tx_desc, &adap_emac->TX0HDP);
  462. /* Wait for packet to complete or link down */
  463. while (1) {
  464. if (!phy.get_link_speed (active_phy_addr)) {
  465. davinci_eth_ch_teardown (EMAC_CH_TX);
  466. return (ret_status);
  467. }
  468. emac_gigabit_enable();
  469. if (readl(&adap_emac->TXINTSTATRAW) & 0x01) {
  470. ret_status = length;
  471. break;
  472. }
  473. tx_send_loop++;
  474. }
  475. return (ret_status);
  476. }
  477. /*
  478. * This function handles receipt of a packet from the network
  479. */
  480. static int davinci_eth_rcv_packet (struct eth_device *dev)
  481. {
  482. volatile emac_desc *rx_curr_desc;
  483. volatile emac_desc *curr_desc;
  484. volatile emac_desc *tail_desc;
  485. int status, ret = -1;
  486. rx_curr_desc = emac_rx_active_head;
  487. status = rx_curr_desc->pkt_flag_len;
  488. if ((rx_curr_desc) && ((status & EMAC_CPPI_OWNERSHIP_BIT) == 0)) {
  489. if (status & EMAC_CPPI_RX_ERROR_FRAME) {
  490. /* Error in packet - discard it and requeue desc */
  491. printf ("WARN: emac_rcv_pkt: Error in packet\n");
  492. } else {
  493. NetReceive (rx_curr_desc->buffer,
  494. (rx_curr_desc->buff_off_len & 0xffff));
  495. ret = rx_curr_desc->buff_off_len & 0xffff;
  496. }
  497. /* Ack received packet descriptor */
  498. writel((unsigned long)rx_curr_desc, &adap_emac->RX0CP);
  499. curr_desc = rx_curr_desc;
  500. emac_rx_active_head =
  501. (volatile emac_desc *) rx_curr_desc->next;
  502. if (status & EMAC_CPPI_EOQ_BIT) {
  503. if (emac_rx_active_head) {
  504. writel((unsigned long)emac_rx_active_head,
  505. &adap_emac->RX0HDP);
  506. } else {
  507. emac_rx_queue_active = 0;
  508. printf ("INFO:emac_rcv_packet: RX Queue not active\n");
  509. }
  510. }
  511. /* Recycle RX descriptor */
  512. rx_curr_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
  513. rx_curr_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
  514. rx_curr_desc->next = 0;
  515. if (emac_rx_active_head == 0) {
  516. printf ("INFO: emac_rcv_pkt: active queue head = 0\n");
  517. emac_rx_active_head = curr_desc;
  518. emac_rx_active_tail = curr_desc;
  519. if (emac_rx_queue_active != 0) {
  520. writel((unsigned long)emac_rx_active_head,
  521. &adap_emac->RX0HDP);
  522. printf ("INFO: emac_rcv_pkt: active queue head = 0, HDP fired\n");
  523. emac_rx_queue_active = 1;
  524. }
  525. } else {
  526. tail_desc = emac_rx_active_tail;
  527. emac_rx_active_tail = curr_desc;
  528. tail_desc->next = (unsigned int) curr_desc;
  529. status = tail_desc->pkt_flag_len;
  530. if (status & EMAC_CPPI_EOQ_BIT) {
  531. writel((unsigned long)curr_desc,
  532. &adap_emac->RX0HDP);
  533. status &= ~EMAC_CPPI_EOQ_BIT;
  534. tail_desc->pkt_flag_len = status;
  535. }
  536. }
  537. return (ret);
  538. }
  539. return (0);
  540. }
  541. /*
  542. * This function initializes the emac hardware. It does NOT initialize
  543. * EMAC modules power or pin multiplexors, that is done by board_init()
  544. * much earlier in bootup process. Returns 1 on success, 0 otherwise.
  545. */
  546. int davinci_emac_initialize(void)
  547. {
  548. u_int32_t phy_id;
  549. u_int16_t tmp;
  550. int i;
  551. struct eth_device *dev;
  552. dev = malloc(sizeof *dev);
  553. if (dev == NULL)
  554. return -1;
  555. memset(dev, 0, sizeof *dev);
  556. dev->iobase = 0;
  557. dev->init = davinci_eth_open;
  558. dev->halt = davinci_eth_close;
  559. dev->send = davinci_eth_send_packet;
  560. dev->recv = davinci_eth_rcv_packet;
  561. dev->write_hwaddr = davinci_eth_set_mac_addr;
  562. eth_register(dev);
  563. davinci_eth_mdio_enable();
  564. for (i = 0; i < 256; i++) {
  565. if (readl(&adap_mdio->ALIVE))
  566. break;
  567. udelay(10);
  568. }
  569. if (i >= 256) {
  570. printf("No ETH PHY detected!!!\n");
  571. return(0);
  572. }
  573. /* Find if a PHY is connected and get it's address */
  574. if (!davinci_eth_phy_detect())
  575. return(0);
  576. /* Get PHY ID and initialize phy_ops for a detected PHY */
  577. if (!davinci_eth_phy_read(active_phy_addr, PHY_PHYIDR1, &tmp)) {
  578. active_phy_addr = 0xff;
  579. return(0);
  580. }
  581. phy_id = (tmp << 16) & 0xffff0000;
  582. if (!davinci_eth_phy_read(active_phy_addr, PHY_PHYIDR2, &tmp)) {
  583. active_phy_addr = 0xff;
  584. return(0);
  585. }
  586. phy_id |= tmp & 0x0000ffff;
  587. switch (phy_id) {
  588. case PHY_LXT972:
  589. sprintf(phy.name, "LXT972 @ 0x%02x", active_phy_addr);
  590. phy.init = lxt972_init_phy;
  591. phy.is_phy_connected = lxt972_is_phy_connected;
  592. phy.get_link_speed = lxt972_get_link_speed;
  593. phy.auto_negotiate = lxt972_auto_negotiate;
  594. break;
  595. case PHY_DP83848:
  596. sprintf(phy.name, "DP83848 @ 0x%02x", active_phy_addr);
  597. phy.init = dp83848_init_phy;
  598. phy.is_phy_connected = dp83848_is_phy_connected;
  599. phy.get_link_speed = dp83848_get_link_speed;
  600. phy.auto_negotiate = dp83848_auto_negotiate;
  601. break;
  602. default:
  603. sprintf(phy.name, "GENERIC @ 0x%02x", active_phy_addr);
  604. phy.init = gen_init_phy;
  605. phy.is_phy_connected = gen_is_phy_connected;
  606. phy.get_link_speed = gen_get_link_speed;
  607. phy.auto_negotiate = gen_auto_negotiate;
  608. }
  609. printf("Ethernet PHY: %s\n", phy.name);
  610. miiphy_register(phy.name, davinci_mii_phy_read, davinci_mii_phy_write);
  611. return(1);
  612. }