davinci_emac.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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, MII_PHYSID1, &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, MII_LPA, &tmp);
  204. /* Speed doesn't matter, there is no setting for it in EMAC. */
  205. if (tmp & (LPA_100FULL | LPA_10FULL)) {
  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 & (LPA_100FULL | LPA_100HALF))
  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, MII_BMCR, &tmp))
  232. return(0);
  233. /* Restart Auto_negotiation */
  234. tmp |= BMCR_ANENABLE;
  235. davinci_eth_phy_write(phy_addr, MII_BMCR, tmp);
  236. /*check AutoNegotiate complete */
  237. udelay (10000);
  238. if (!davinci_eth_phy_read(phy_addr, MII_BMSR, &tmp))
  239. return(0);
  240. if (!(tmp & BMSR_ANEGCOMPLETE))
  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(readl(&adap_emac->MACCONTROL) |
  265. EMAC_MACCONTROL_GIGFORCE |
  266. EMAC_MACCONTROL_GIGABIT_ENABLE,
  267. &adap_emac->MACCONTROL);
  268. }
  269. }
  270. }
  271. /* Eth device open */
  272. static int davinci_eth_open(struct eth_device *dev, bd_t *bis)
  273. {
  274. dv_reg_p addr;
  275. u_int32_t clkdiv, cnt;
  276. volatile emac_desc *rx_desc;
  277. debug_emac("+ emac_open\n");
  278. /* Reset EMAC module and disable interrupts in wrapper */
  279. writel(1, &adap_emac->SOFTRESET);
  280. while (readl(&adap_emac->SOFTRESET) != 0)
  281. ;
  282. #if defined(DAVINCI_EMAC_VERSION2)
  283. writel(1, &adap_ewrap->softrst);
  284. while (readl(&adap_ewrap->softrst) != 0)
  285. ;
  286. #else
  287. writel(0, &adap_ewrap->EWCTL);
  288. for (cnt = 0; cnt < 5; cnt++) {
  289. clkdiv = readl(&adap_ewrap->EWCTL);
  290. }
  291. #endif
  292. #if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
  293. defined(CONFIG_MACH_DAVINCI_DA850_EVM)
  294. adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
  295. adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
  296. adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
  297. #endif
  298. rx_desc = emac_rx_desc;
  299. writel(1, &adap_emac->TXCONTROL);
  300. writel(1, &adap_emac->RXCONTROL);
  301. davinci_eth_set_mac_addr(dev);
  302. /* Set DMA 8 TX / 8 RX Head pointers to 0 */
  303. addr = &adap_emac->TX0HDP;
  304. for(cnt = 0; cnt < 16; cnt++)
  305. writel(0, addr++);
  306. addr = &adap_emac->RX0HDP;
  307. for(cnt = 0; cnt < 16; cnt++)
  308. writel(0, addr++);
  309. /* Clear Statistics (do this before setting MacControl register) */
  310. addr = &adap_emac->RXGOODFRAMES;
  311. for(cnt = 0; cnt < EMAC_NUM_STATS; cnt++)
  312. writel(0, addr++);
  313. /* No multicast addressing */
  314. writel(0, &adap_emac->MACHASH1);
  315. writel(0, &adap_emac->MACHASH2);
  316. /* Create RX queue and set receive process in place */
  317. emac_rx_active_head = emac_rx_desc;
  318. for (cnt = 0; cnt < EMAC_MAX_RX_BUFFERS; cnt++) {
  319. rx_desc->next = (u_int32_t)(rx_desc + 1);
  320. rx_desc->buffer = &emac_rx_buffers[cnt * (EMAC_MAX_ETHERNET_PKT_SIZE + EMAC_PKT_ALIGN)];
  321. rx_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
  322. rx_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
  323. rx_desc++;
  324. }
  325. /* Finalize the rx desc list */
  326. rx_desc--;
  327. rx_desc->next = 0;
  328. emac_rx_active_tail = rx_desc;
  329. emac_rx_queue_active = 1;
  330. /* Enable TX/RX */
  331. writel(EMAC_MAX_ETHERNET_PKT_SIZE, &adap_emac->RXMAXLEN);
  332. writel(0, &adap_emac->RXBUFFEROFFSET);
  333. /*
  334. * No fancy configs - Use this for promiscous debug
  335. * - EMAC_RXMBPENABLE_RXCAFEN_ENABLE
  336. */
  337. writel(EMAC_RXMBPENABLE_RXBROADEN, &adap_emac->RXMBPENABLE);
  338. /* Enable ch 0 only */
  339. writel(1, &adap_emac->RXUNICASTSET);
  340. /* Enable MII interface and Full duplex mode */
  341. #ifdef CONFIG_SOC_DA8XX
  342. writel((EMAC_MACCONTROL_MIIEN_ENABLE |
  343. EMAC_MACCONTROL_FULLDUPLEX_ENABLE |
  344. EMAC_MACCONTROL_RMIISPEED_100),
  345. &adap_emac->MACCONTROL);
  346. #else
  347. writel((EMAC_MACCONTROL_MIIEN_ENABLE |
  348. EMAC_MACCONTROL_FULLDUPLEX_ENABLE),
  349. &adap_emac->MACCONTROL);
  350. #endif
  351. /* Init MDIO & get link state */
  352. clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1;
  353. writel((clkdiv & 0xff) | MDIO_CONTROL_ENABLE | MDIO_CONTROL_FAULT,
  354. &adap_mdio->CONTROL);
  355. /* We need to wait for MDIO to start */
  356. udelay(1000);
  357. if (!phy.get_link_speed(active_phy_addr))
  358. return(0);
  359. emac_gigabit_enable();
  360. /* Start receive process */
  361. writel((u_int32_t)emac_rx_desc, &adap_emac->RX0HDP);
  362. debug_emac("- emac_open\n");
  363. return(1);
  364. }
  365. /* EMAC Channel Teardown */
  366. static void davinci_eth_ch_teardown(int ch)
  367. {
  368. dv_reg dly = 0xff;
  369. dv_reg cnt;
  370. debug_emac("+ emac_ch_teardown\n");
  371. if (ch == EMAC_CH_TX) {
  372. /* Init TX channel teardown */
  373. writel(0, &adap_emac->TXTEARDOWN);
  374. do {
  375. /*
  376. * Wait here for Tx teardown completion interrupt to
  377. * occur. Note: A task delay can be called here to pend
  378. * rather than occupying CPU cycles - anyway it has
  379. * been found that teardown takes very few cpu cycles
  380. * and does not affect functionality
  381. */
  382. dly--;
  383. udelay(1);
  384. if (dly == 0)
  385. break;
  386. cnt = readl(&adap_emac->TX0CP);
  387. } while (cnt != 0xfffffffc);
  388. writel(cnt, &adap_emac->TX0CP);
  389. writel(0, &adap_emac->TX0HDP);
  390. } else {
  391. /* Init RX channel teardown */
  392. writel(0, &adap_emac->RXTEARDOWN);
  393. do {
  394. /*
  395. * Wait here for Rx teardown completion interrupt to
  396. * occur. Note: A task delay can be called here to pend
  397. * rather than occupying CPU cycles - anyway it has
  398. * been found that teardown takes very few cpu cycles
  399. * and does not affect functionality
  400. */
  401. dly--;
  402. udelay(1);
  403. if (dly == 0)
  404. break;
  405. cnt = readl(&adap_emac->RX0CP);
  406. } while (cnt != 0xfffffffc);
  407. writel(cnt, &adap_emac->RX0CP);
  408. writel(0, &adap_emac->RX0HDP);
  409. }
  410. debug_emac("- emac_ch_teardown\n");
  411. }
  412. /* Eth device close */
  413. static void davinci_eth_close(struct eth_device *dev)
  414. {
  415. debug_emac("+ emac_close\n");
  416. davinci_eth_ch_teardown(EMAC_CH_TX); /* TX Channel teardown */
  417. davinci_eth_ch_teardown(EMAC_CH_RX); /* RX Channel teardown */
  418. /* Reset EMAC module and disable interrupts in wrapper */
  419. writel(1, &adap_emac->SOFTRESET);
  420. #if defined(DAVINCI_EMAC_VERSION2)
  421. writel(1, &adap_ewrap->softrst);
  422. #else
  423. writel(0, &adap_ewrap->EWCTL);
  424. #endif
  425. #if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
  426. defined(CONFIG_MACH_DAVINCI_DA850_EVM)
  427. adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
  428. adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
  429. adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
  430. #endif
  431. debug_emac("- emac_close\n");
  432. }
  433. static int tx_send_loop = 0;
  434. /*
  435. * This function sends a single packet on the network and returns
  436. * positive number (number of bytes transmitted) or negative for error
  437. */
  438. static int davinci_eth_send_packet (struct eth_device *dev,
  439. volatile void *packet, int length)
  440. {
  441. int ret_status = -1;
  442. tx_send_loop = 0;
  443. /* Return error if no link */
  444. if (!phy.get_link_speed (active_phy_addr)) {
  445. printf ("WARN: emac_send_packet: No link\n");
  446. return (ret_status);
  447. }
  448. emac_gigabit_enable();
  449. /* Check packet size and if < EMAC_MIN_ETHERNET_PKT_SIZE, pad it up */
  450. if (length < EMAC_MIN_ETHERNET_PKT_SIZE) {
  451. length = EMAC_MIN_ETHERNET_PKT_SIZE;
  452. }
  453. /* Populate the TX descriptor */
  454. emac_tx_desc->next = 0;
  455. emac_tx_desc->buffer = (u_int8_t *) packet;
  456. emac_tx_desc->buff_off_len = (length & 0xffff);
  457. emac_tx_desc->pkt_flag_len = ((length & 0xffff) |
  458. EMAC_CPPI_SOP_BIT |
  459. EMAC_CPPI_OWNERSHIP_BIT |
  460. EMAC_CPPI_EOP_BIT);
  461. /* Send the packet */
  462. writel((unsigned long)emac_tx_desc, &adap_emac->TX0HDP);
  463. /* Wait for packet to complete or link down */
  464. while (1) {
  465. if (!phy.get_link_speed (active_phy_addr)) {
  466. davinci_eth_ch_teardown (EMAC_CH_TX);
  467. return (ret_status);
  468. }
  469. emac_gigabit_enable();
  470. if (readl(&adap_emac->TXINTSTATRAW) & 0x01) {
  471. ret_status = length;
  472. break;
  473. }
  474. tx_send_loop++;
  475. }
  476. return (ret_status);
  477. }
  478. /*
  479. * This function handles receipt of a packet from the network
  480. */
  481. static int davinci_eth_rcv_packet (struct eth_device *dev)
  482. {
  483. volatile emac_desc *rx_curr_desc;
  484. volatile emac_desc *curr_desc;
  485. volatile emac_desc *tail_desc;
  486. int status, ret = -1;
  487. rx_curr_desc = emac_rx_active_head;
  488. status = rx_curr_desc->pkt_flag_len;
  489. if ((rx_curr_desc) && ((status & EMAC_CPPI_OWNERSHIP_BIT) == 0)) {
  490. if (status & EMAC_CPPI_RX_ERROR_FRAME) {
  491. /* Error in packet - discard it and requeue desc */
  492. printf ("WARN: emac_rcv_pkt: Error in packet\n");
  493. } else {
  494. NetReceive (rx_curr_desc->buffer,
  495. (rx_curr_desc->buff_off_len & 0xffff));
  496. ret = rx_curr_desc->buff_off_len & 0xffff;
  497. }
  498. /* Ack received packet descriptor */
  499. writel((unsigned long)rx_curr_desc, &adap_emac->RX0CP);
  500. curr_desc = rx_curr_desc;
  501. emac_rx_active_head =
  502. (volatile emac_desc *) rx_curr_desc->next;
  503. if (status & EMAC_CPPI_EOQ_BIT) {
  504. if (emac_rx_active_head) {
  505. writel((unsigned long)emac_rx_active_head,
  506. &adap_emac->RX0HDP);
  507. } else {
  508. emac_rx_queue_active = 0;
  509. printf ("INFO:emac_rcv_packet: RX Queue not active\n");
  510. }
  511. }
  512. /* Recycle RX descriptor */
  513. rx_curr_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
  514. rx_curr_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
  515. rx_curr_desc->next = 0;
  516. if (emac_rx_active_head == 0) {
  517. printf ("INFO: emac_rcv_pkt: active queue head = 0\n");
  518. emac_rx_active_head = curr_desc;
  519. emac_rx_active_tail = curr_desc;
  520. if (emac_rx_queue_active != 0) {
  521. writel((unsigned long)emac_rx_active_head,
  522. &adap_emac->RX0HDP);
  523. printf ("INFO: emac_rcv_pkt: active queue head = 0, HDP fired\n");
  524. emac_rx_queue_active = 1;
  525. }
  526. } else {
  527. tail_desc = emac_rx_active_tail;
  528. emac_rx_active_tail = curr_desc;
  529. tail_desc->next = (unsigned int) curr_desc;
  530. status = tail_desc->pkt_flag_len;
  531. if (status & EMAC_CPPI_EOQ_BIT) {
  532. writel((unsigned long)curr_desc,
  533. &adap_emac->RX0HDP);
  534. status &= ~EMAC_CPPI_EOQ_BIT;
  535. tail_desc->pkt_flag_len = status;
  536. }
  537. }
  538. return (ret);
  539. }
  540. return (0);
  541. }
  542. /*
  543. * This function initializes the emac hardware. It does NOT initialize
  544. * EMAC modules power or pin multiplexors, that is done by board_init()
  545. * much earlier in bootup process. Returns 1 on success, 0 otherwise.
  546. */
  547. int davinci_emac_initialize(void)
  548. {
  549. u_int32_t phy_id;
  550. u_int16_t tmp;
  551. int i;
  552. struct eth_device *dev;
  553. dev = malloc(sizeof *dev);
  554. if (dev == NULL)
  555. return -1;
  556. memset(dev, 0, sizeof *dev);
  557. sprintf(dev->name, "DaVinci-EMAC");
  558. dev->iobase = 0;
  559. dev->init = davinci_eth_open;
  560. dev->halt = davinci_eth_close;
  561. dev->send = davinci_eth_send_packet;
  562. dev->recv = davinci_eth_rcv_packet;
  563. dev->write_hwaddr = davinci_eth_set_mac_addr;
  564. eth_register(dev);
  565. davinci_eth_mdio_enable();
  566. /* let the EMAC detect the PHYs */
  567. udelay(5000);
  568. for (i = 0; i < 256; i++) {
  569. if (readl(&adap_mdio->ALIVE))
  570. break;
  571. udelay(10);
  572. }
  573. if (i >= 256) {
  574. printf("No ETH PHY detected!!!\n");
  575. return(0);
  576. }
  577. /* Find if a PHY is connected and get it's address */
  578. if (!davinci_eth_phy_detect())
  579. return(0);
  580. /* Get PHY ID and initialize phy_ops for a detected PHY */
  581. if (!davinci_eth_phy_read(active_phy_addr, MII_PHYSID1, &tmp)) {
  582. active_phy_addr = 0xff;
  583. return(0);
  584. }
  585. phy_id = (tmp << 16) & 0xffff0000;
  586. if (!davinci_eth_phy_read(active_phy_addr, MII_PHYSID2, &tmp)) {
  587. active_phy_addr = 0xff;
  588. return(0);
  589. }
  590. phy_id |= tmp & 0x0000ffff;
  591. switch (phy_id) {
  592. case PHY_KSZ8873:
  593. sprintf(phy.name, "KSZ8873 @ 0x%02x", active_phy_addr);
  594. phy.init = ksz8873_init_phy;
  595. phy.is_phy_connected = ksz8873_is_phy_connected;
  596. phy.get_link_speed = ksz8873_get_link_speed;
  597. phy.auto_negotiate = ksz8873_auto_negotiate;
  598. break;
  599. case PHY_LXT972:
  600. sprintf(phy.name, "LXT972 @ 0x%02x", active_phy_addr);
  601. phy.init = lxt972_init_phy;
  602. phy.is_phy_connected = lxt972_is_phy_connected;
  603. phy.get_link_speed = lxt972_get_link_speed;
  604. phy.auto_negotiate = lxt972_auto_negotiate;
  605. break;
  606. case PHY_DP83848:
  607. sprintf(phy.name, "DP83848 @ 0x%02x", active_phy_addr);
  608. phy.init = dp83848_init_phy;
  609. phy.is_phy_connected = dp83848_is_phy_connected;
  610. phy.get_link_speed = dp83848_get_link_speed;
  611. phy.auto_negotiate = dp83848_auto_negotiate;
  612. break;
  613. case PHY_ET1011C:
  614. sprintf(phy.name, "ET1011C @ 0x%02x", active_phy_addr);
  615. phy.init = gen_init_phy;
  616. phy.is_phy_connected = gen_is_phy_connected;
  617. phy.get_link_speed = et1011c_get_link_speed;
  618. phy.auto_negotiate = gen_auto_negotiate;
  619. break;
  620. default:
  621. sprintf(phy.name, "GENERIC @ 0x%02x", active_phy_addr);
  622. phy.init = gen_init_phy;
  623. phy.is_phy_connected = gen_is_phy_connected;
  624. phy.get_link_speed = gen_get_link_speed;
  625. phy.auto_negotiate = gen_auto_negotiate;
  626. }
  627. debug("Ethernet PHY: %s\n", phy.name);
  628. miiphy_register(phy.name, davinci_mii_phy_read, davinci_mii_phy_write);
  629. return(1);
  630. }