davinci_emac.c 22 KB

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