davinci_emac.c 23 KB

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