davinci_emac.c 21 KB

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