davinci_emac.c 23 KB

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