davinci_emac.c 18 KB

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