davinci_emac.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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) && (tmp & 0x04))
  200. return(1);
  201. return(0);
  202. }
  203. static int gen_auto_negotiate(int phy_addr)
  204. {
  205. u_int16_t tmp;
  206. if (!davinci_eth_phy_read(phy_addr, PHY_BMCR, &tmp))
  207. return(0);
  208. /* Restart Auto_negotiation */
  209. tmp |= PHY_BMCR_AUTON;
  210. davinci_eth_phy_write(phy_addr, PHY_BMCR, tmp);
  211. /*check AutoNegotiate complete */
  212. udelay (10000);
  213. if (!davinci_eth_phy_read(phy_addr, PHY_BMSR, &tmp))
  214. return(0);
  215. if (!(tmp & PHY_BMSR_AUTN_COMP))
  216. return(0);
  217. return(gen_get_link_speed(phy_addr));
  218. }
  219. /* End of generic PHY functions */
  220. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  221. static int davinci_mii_phy_read(const char *devname, unsigned char addr, unsigned char reg, unsigned short *value)
  222. {
  223. return(davinci_eth_phy_read(addr, reg, value) ? 0 : 1);
  224. }
  225. static int davinci_mii_phy_write(const char *devname, unsigned char addr, unsigned char reg, unsigned short value)
  226. {
  227. return(davinci_eth_phy_write(addr, reg, value) ? 0 : 1);
  228. }
  229. #endif
  230. static void __attribute__((unused)) davinci_eth_gigabit_enable(void)
  231. {
  232. u_int16_t data;
  233. if (davinci_eth_phy_read(EMAC_MDIO_PHY_NUM, 0, &data)) {
  234. if (data & (1 << 6)) { /* speed selection MSB */
  235. /*
  236. * Check if link detected is giga-bit
  237. * If Gigabit mode detected, enable gigbit in MAC
  238. */
  239. writel(EMAC_MACCONTROL_GIGFORCE |
  240. EMAC_MACCONTROL_GIGABIT_ENABLE,
  241. &adap_emac->MACCONTROL);
  242. }
  243. }
  244. }
  245. /* Eth device open */
  246. static int davinci_eth_open(struct eth_device *dev, bd_t *bis)
  247. {
  248. dv_reg_p addr;
  249. u_int32_t clkdiv, cnt;
  250. volatile emac_desc *rx_desc;
  251. debug_emac("+ emac_open\n");
  252. /* Reset EMAC module and disable interrupts in wrapper */
  253. writel(1, &adap_emac->SOFTRESET);
  254. while (readl(&adap_emac->SOFTRESET) != 0)
  255. ;
  256. #if defined(DAVINCI_EMAC_VERSION2)
  257. writel(1, &adap_ewrap->softrst);
  258. while (readl(&adap_ewrap->softrst) != 0)
  259. ;
  260. #else
  261. writel(0, &adap_ewrap->EWCTL);
  262. for (cnt = 0; cnt < 5; cnt++) {
  263. clkdiv = readl(&adap_ewrap->EWCTL);
  264. }
  265. #endif
  266. rx_desc = emac_rx_desc;
  267. writel(1, &adap_emac->TXCONTROL);
  268. writel(1, &adap_emac->RXCONTROL);
  269. davinci_eth_set_mac_addr(dev);
  270. /* Set DMA 8 TX / 8 RX Head pointers to 0 */
  271. addr = &adap_emac->TX0HDP;
  272. for(cnt = 0; cnt < 16; cnt++)
  273. writel(0, addr++);
  274. addr = &adap_emac->RX0HDP;
  275. for(cnt = 0; cnt < 16; cnt++)
  276. writel(0, addr++);
  277. /* Clear Statistics (do this before setting MacControl register) */
  278. addr = &adap_emac->RXGOODFRAMES;
  279. for(cnt = 0; cnt < EMAC_NUM_STATS; cnt++)
  280. writel(0, addr++);
  281. /* No multicast addressing */
  282. writel(0, &adap_emac->MACHASH1);
  283. writel(0, &adap_emac->MACHASH2);
  284. /* Create RX queue and set receive process in place */
  285. emac_rx_active_head = emac_rx_desc;
  286. for (cnt = 0; cnt < EMAC_MAX_RX_BUFFERS; cnt++) {
  287. rx_desc->next = (u_int32_t)(rx_desc + 1);
  288. rx_desc->buffer = &emac_rx_buffers[cnt * (EMAC_MAX_ETHERNET_PKT_SIZE + EMAC_PKT_ALIGN)];
  289. rx_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
  290. rx_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
  291. rx_desc++;
  292. }
  293. /* Finalize the rx desc list */
  294. rx_desc--;
  295. rx_desc->next = 0;
  296. emac_rx_active_tail = rx_desc;
  297. emac_rx_queue_active = 1;
  298. /* Enable TX/RX */
  299. writel(EMAC_MAX_ETHERNET_PKT_SIZE, &adap_emac->RXMAXLEN);
  300. writel(0, &adap_emac->RXBUFFEROFFSET);
  301. /*
  302. * No fancy configs - Use this for promiscous debug
  303. * - EMAC_RXMBPENABLE_RXCAFEN_ENABLE
  304. */
  305. writel(EMAC_RXMBPENABLE_RXBROADEN, &adap_emac->RXMBPENABLE);
  306. /* Enable ch 0 only */
  307. writel(1, &adap_emac->RXUNICASTSET);
  308. /* Enable MII interface and Full duplex mode */
  309. #ifdef CONFIG_SOC_DA8XX
  310. writel((EMAC_MACCONTROL_MIIEN_ENABLE |
  311. EMAC_MACCONTROL_FULLDUPLEX_ENABLE |
  312. EMAC_MACCONTROL_RMIISPEED_100),
  313. &adap_emac->MACCONTROL);
  314. #else
  315. writel((EMAC_MACCONTROL_MIIEN_ENABLE |
  316. EMAC_MACCONTROL_FULLDUPLEX_ENABLE),
  317. &adap_emac->MACCONTROL);
  318. #endif
  319. /* Init MDIO & get link state */
  320. clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1;
  321. writel((clkdiv & 0xff) | MDIO_CONTROL_ENABLE | MDIO_CONTROL_FAULT,
  322. &adap_mdio->CONTROL);
  323. /* We need to wait for MDIO to start */
  324. udelay(1000);
  325. if (!phy.get_link_speed(active_phy_addr))
  326. return(0);
  327. emac_gigabit_enable();
  328. /* Start receive process */
  329. writel((u_int32_t)emac_rx_desc, &adap_emac->RX0HDP);
  330. debug_emac("- emac_open\n");
  331. return(1);
  332. }
  333. /* EMAC Channel Teardown */
  334. static void davinci_eth_ch_teardown(int ch)
  335. {
  336. dv_reg dly = 0xff;
  337. dv_reg cnt;
  338. debug_emac("+ emac_ch_teardown\n");
  339. if (ch == EMAC_CH_TX) {
  340. /* Init TX channel teardown */
  341. writel(1, &adap_emac->TXTEARDOWN);
  342. do {
  343. /*
  344. * Wait here for Tx teardown completion interrupt to
  345. * occur. Note: A task delay can be called here to pend
  346. * rather than occupying CPU cycles - anyway it has
  347. * been found that teardown takes very few cpu cycles
  348. * and does not affect functionality
  349. */
  350. dly--;
  351. udelay(1);
  352. if (dly == 0)
  353. break;
  354. cnt = readl(&adap_emac->TX0CP);
  355. } while (cnt != 0xfffffffc);
  356. writel(cnt, &adap_emac->TX0CP);
  357. writel(0, &adap_emac->TX0HDP);
  358. } else {
  359. /* Init RX channel teardown */
  360. writel(1, &adap_emac->RXTEARDOWN);
  361. do {
  362. /*
  363. * Wait here for Rx teardown completion interrupt to
  364. * occur. Note: A task delay can be called here to pend
  365. * rather than occupying CPU cycles - anyway it has
  366. * been found that teardown takes very few cpu cycles
  367. * and does not affect functionality
  368. */
  369. dly--;
  370. udelay(1);
  371. if (dly == 0)
  372. break;
  373. cnt = readl(&adap_emac->RX0CP);
  374. } while (cnt != 0xfffffffc);
  375. writel(cnt, &adap_emac->RX0CP);
  376. writel(0, &adap_emac->RX0HDP);
  377. }
  378. debug_emac("- emac_ch_teardown\n");
  379. }
  380. /* Eth device close */
  381. static void davinci_eth_close(struct eth_device *dev)
  382. {
  383. debug_emac("+ emac_close\n");
  384. davinci_eth_ch_teardown(EMAC_CH_TX); /* TX Channel teardown */
  385. davinci_eth_ch_teardown(EMAC_CH_RX); /* RX Channel teardown */
  386. /* Reset EMAC module and disable interrupts in wrapper */
  387. writel(1, &adap_emac->SOFTRESET);
  388. #if defined(DAVINCI_EMAC_VERSION2)
  389. writel(1, &adap_ewrap->softrst);
  390. #else
  391. writel(0, &adap_ewrap->EWCTL);
  392. #endif
  393. debug_emac("- emac_close\n");
  394. }
  395. static int tx_send_loop = 0;
  396. /*
  397. * This function sends a single packet on the network and returns
  398. * positive number (number of bytes transmitted) or negative for error
  399. */
  400. static int davinci_eth_send_packet (struct eth_device *dev,
  401. volatile void *packet, int length)
  402. {
  403. int ret_status = -1;
  404. tx_send_loop = 0;
  405. /* Return error if no link */
  406. if (!phy.get_link_speed (active_phy_addr)) {
  407. printf ("WARN: emac_send_packet: No link\n");
  408. return (ret_status);
  409. }
  410. emac_gigabit_enable();
  411. /* Check packet size and if < EMAC_MIN_ETHERNET_PKT_SIZE, pad it up */
  412. if (length < EMAC_MIN_ETHERNET_PKT_SIZE) {
  413. length = EMAC_MIN_ETHERNET_PKT_SIZE;
  414. }
  415. /* Populate the TX descriptor */
  416. emac_tx_desc->next = 0;
  417. emac_tx_desc->buffer = (u_int8_t *) packet;
  418. emac_tx_desc->buff_off_len = (length & 0xffff);
  419. emac_tx_desc->pkt_flag_len = ((length & 0xffff) |
  420. EMAC_CPPI_SOP_BIT |
  421. EMAC_CPPI_OWNERSHIP_BIT |
  422. EMAC_CPPI_EOP_BIT);
  423. /* Send the packet */
  424. writel((unsigned long)emac_tx_desc, &adap_emac->TX0HDP);
  425. /* Wait for packet to complete or link down */
  426. while (1) {
  427. if (!phy.get_link_speed (active_phy_addr)) {
  428. davinci_eth_ch_teardown (EMAC_CH_TX);
  429. return (ret_status);
  430. }
  431. emac_gigabit_enable();
  432. if (readl(&adap_emac->TXINTSTATRAW) & 0x01) {
  433. ret_status = length;
  434. break;
  435. }
  436. tx_send_loop++;
  437. }
  438. return (ret_status);
  439. }
  440. /*
  441. * This function handles receipt of a packet from the network
  442. */
  443. static int davinci_eth_rcv_packet (struct eth_device *dev)
  444. {
  445. volatile emac_desc *rx_curr_desc;
  446. volatile emac_desc *curr_desc;
  447. volatile emac_desc *tail_desc;
  448. int status, ret = -1;
  449. rx_curr_desc = emac_rx_active_head;
  450. status = rx_curr_desc->pkt_flag_len;
  451. if ((rx_curr_desc) && ((status & EMAC_CPPI_OWNERSHIP_BIT) == 0)) {
  452. if (status & EMAC_CPPI_RX_ERROR_FRAME) {
  453. /* Error in packet - discard it and requeue desc */
  454. printf ("WARN: emac_rcv_pkt: Error in packet\n");
  455. } else {
  456. NetReceive (rx_curr_desc->buffer,
  457. (rx_curr_desc->buff_off_len & 0xffff));
  458. ret = rx_curr_desc->buff_off_len & 0xffff;
  459. }
  460. /* Ack received packet descriptor */
  461. writel((unsigned long)rx_curr_desc, &adap_emac->RX0CP);
  462. curr_desc = rx_curr_desc;
  463. emac_rx_active_head =
  464. (volatile emac_desc *) rx_curr_desc->next;
  465. if (status & EMAC_CPPI_EOQ_BIT) {
  466. if (emac_rx_active_head) {
  467. writel((unsigned long)emac_rx_active_head,
  468. &adap_emac->RX0HDP);
  469. } else {
  470. emac_rx_queue_active = 0;
  471. printf ("INFO:emac_rcv_packet: RX Queue not active\n");
  472. }
  473. }
  474. /* Recycle RX descriptor */
  475. rx_curr_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
  476. rx_curr_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
  477. rx_curr_desc->next = 0;
  478. if (emac_rx_active_head == 0) {
  479. printf ("INFO: emac_rcv_pkt: active queue head = 0\n");
  480. emac_rx_active_head = curr_desc;
  481. emac_rx_active_tail = curr_desc;
  482. if (emac_rx_queue_active != 0) {
  483. writel((unsigned long)emac_rx_active_head,
  484. &adap_emac->RX0HDP);
  485. printf ("INFO: emac_rcv_pkt: active queue head = 0, HDP fired\n");
  486. emac_rx_queue_active = 1;
  487. }
  488. } else {
  489. tail_desc = emac_rx_active_tail;
  490. emac_rx_active_tail = curr_desc;
  491. tail_desc->next = (unsigned int) curr_desc;
  492. status = tail_desc->pkt_flag_len;
  493. if (status & EMAC_CPPI_EOQ_BIT) {
  494. writel((unsigned long)curr_desc,
  495. &adap_emac->RX0HDP);
  496. status &= ~EMAC_CPPI_EOQ_BIT;
  497. tail_desc->pkt_flag_len = status;
  498. }
  499. }
  500. return (ret);
  501. }
  502. return (0);
  503. }
  504. /*
  505. * This function initializes the emac hardware. It does NOT initialize
  506. * EMAC modules power or pin multiplexors, that is done by board_init()
  507. * much earlier in bootup process. Returns 1 on success, 0 otherwise.
  508. */
  509. int davinci_emac_initialize(void)
  510. {
  511. u_int32_t phy_id;
  512. u_int16_t tmp;
  513. int i;
  514. struct eth_device *dev;
  515. dev = malloc(sizeof *dev);
  516. if (dev == NULL)
  517. return -1;
  518. memset(dev, 0, sizeof *dev);
  519. dev->iobase = 0;
  520. dev->init = davinci_eth_open;
  521. dev->halt = davinci_eth_close;
  522. dev->send = davinci_eth_send_packet;
  523. dev->recv = davinci_eth_rcv_packet;
  524. dev->write_hwaddr = davinci_eth_set_mac_addr;
  525. eth_register(dev);
  526. davinci_eth_mdio_enable();
  527. for (i = 0; i < 256; i++) {
  528. if (readl(&adap_mdio->ALIVE))
  529. break;
  530. udelay(10);
  531. }
  532. if (i >= 256) {
  533. printf("No ETH PHY detected!!!\n");
  534. return(0);
  535. }
  536. /* Find if a PHY is connected and get it's address */
  537. if (!davinci_eth_phy_detect())
  538. return(0);
  539. /* Get PHY ID and initialize phy_ops for a detected PHY */
  540. if (!davinci_eth_phy_read(active_phy_addr, PHY_PHYIDR1, &tmp)) {
  541. active_phy_addr = 0xff;
  542. return(0);
  543. }
  544. phy_id = (tmp << 16) & 0xffff0000;
  545. if (!davinci_eth_phy_read(active_phy_addr, PHY_PHYIDR2, &tmp)) {
  546. active_phy_addr = 0xff;
  547. return(0);
  548. }
  549. phy_id |= tmp & 0x0000ffff;
  550. switch (phy_id) {
  551. case PHY_LXT972:
  552. sprintf(phy.name, "LXT972 @ 0x%02x", active_phy_addr);
  553. phy.init = lxt972_init_phy;
  554. phy.is_phy_connected = lxt972_is_phy_connected;
  555. phy.get_link_speed = lxt972_get_link_speed;
  556. phy.auto_negotiate = lxt972_auto_negotiate;
  557. break;
  558. case PHY_DP83848:
  559. sprintf(phy.name, "DP83848 @ 0x%02x", active_phy_addr);
  560. phy.init = dp83848_init_phy;
  561. phy.is_phy_connected = dp83848_is_phy_connected;
  562. phy.get_link_speed = dp83848_get_link_speed;
  563. phy.auto_negotiate = dp83848_auto_negotiate;
  564. break;
  565. default:
  566. sprintf(phy.name, "GENERIC @ 0x%02x", active_phy_addr);
  567. phy.init = gen_init_phy;
  568. phy.is_phy_connected = gen_is_phy_connected;
  569. phy.get_link_speed = gen_get_link_speed;
  570. phy.auto_negotiate = gen_auto_negotiate;
  571. }
  572. printf("Ethernet PHY: %s\n", phy.name);
  573. miiphy_register(phy.name, davinci_mii_phy_read, davinci_mii_phy_write);
  574. return(1);
  575. }