davinci_emac.c 21 KB

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