ether.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * (C) Copyright 2007
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Author: Igor Lisitsin <igor@emcraft.com>
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. /*
  27. * Ethernet test
  28. *
  29. * The Ethernet Media Access Controllers (EMAC) are tested in the
  30. * internal loopback mode.
  31. * The controllers are configured accordingly and several packets
  32. * are transmitted. The configurable test parameters are:
  33. * MIN_PACKET_LENGTH - minimum size of packet to transmit
  34. * MAX_PACKET_LENGTH - maximum size of packet to transmit
  35. * CONFIG_SYS_POST_ETH_LOOPS - Number of test loops. Each loop
  36. * is tested with a different frame length. Starting with
  37. * MAX_PACKET_LENGTH and going down to MIN_PACKET_LENGTH.
  38. * Defaults to 10 and can be overriden in the board config header.
  39. */
  40. #include <post.h>
  41. #if CONFIG_POST & CONFIG_SYS_POST_ETHER
  42. #include <asm/cache.h>
  43. #include <asm/io.h>
  44. #include <asm/processor.h>
  45. #include <asm/ppc4xx-mal.h>
  46. #include <asm/ppc4xx-emac.h>
  47. #include <malloc.h>
  48. DECLARE_GLOBAL_DATA_PTR;
  49. /*
  50. * Get count of EMAC devices (doesn't have to be the max. possible number
  51. * supported by the cpu)
  52. *
  53. * CONFIG_BOARD_EMAC_COUNT added so now a "dynamic" way to configure the
  54. * EMAC count is possible. As it is needed for the Kilauea/Haleakala
  55. * 405EX/405EXr eval board, using the same binary.
  56. */
  57. #if defined(CONFIG_BOARD_EMAC_COUNT)
  58. #define LAST_EMAC_NUM board_emac_count()
  59. #else /* CONFIG_BOARD_EMAC_COUNT */
  60. #if defined(CONFIG_HAS_ETH3)
  61. #define LAST_EMAC_NUM 4
  62. #elif defined(CONFIG_HAS_ETH2)
  63. #define LAST_EMAC_NUM 3
  64. #elif defined(CONFIG_HAS_ETH1)
  65. #define LAST_EMAC_NUM 2
  66. #else
  67. #define LAST_EMAC_NUM 1
  68. #endif
  69. #endif /* CONFIG_BOARD_EMAC_COUNT */
  70. #if defined(CONFIG_440SPE) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  71. #define SDR0_MFR_ETH_CLK_SEL_V(n) ((0x01<<27) / (n+1))
  72. #endif
  73. #define MIN_PACKET_LENGTH 64
  74. #define MAX_PACKET_LENGTH 1514
  75. #ifndef CONFIG_SYS_POST_ETH_LOOPS
  76. #define CONFIG_SYS_POST_ETH_LOOPS 10
  77. #endif
  78. #define PACKET_INCR ((MAX_PACKET_LENGTH - MIN_PACKET_LENGTH) / \
  79. CONFIG_SYS_POST_ETH_LOOPS)
  80. static volatile mal_desc_t tx __cacheline_aligned;
  81. static volatile mal_desc_t rx __cacheline_aligned;
  82. static char *tx_buf;
  83. static char *rx_buf;
  84. int board_emac_count(void);
  85. static void ether_post_init (int devnum, int hw_addr)
  86. {
  87. int i;
  88. #if defined(CONFIG_440GX) || \
  89. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  90. defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  91. unsigned mode_reg;
  92. sys_info_t sysinfo;
  93. #endif
  94. #if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || defined(CONFIG_440SPE)
  95. unsigned long mfr;
  96. #endif
  97. #if defined(CONFIG_440GX) || \
  98. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  99. defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  100. /* Need to get the OPB frequency so we can access the PHY */
  101. get_sys_info (&sysinfo);
  102. #endif
  103. #if defined(CONFIG_440SPE) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  104. /* provide clocks for EMAC internal loopback */
  105. mfsdr (SDR0_MFR, mfr);
  106. mfr |= SDR0_MFR_ETH_CLK_SEL_V(devnum);
  107. mtsdr (SDR0_MFR, mfr);
  108. sync ();
  109. #endif
  110. /* reset emac */
  111. out_be32 ((void*)(EMAC0_MR0 + hw_addr), EMAC_MR0_SRST);
  112. sync ();
  113. for (i = 0;; i++) {
  114. if (!(in_be32 ((void*)(EMAC0_MR0 + hw_addr)) & EMAC_MR0_SRST))
  115. break;
  116. if (i >= 1000) {
  117. printf ("Timeout resetting EMAC\n");
  118. break;
  119. }
  120. udelay (1000);
  121. }
  122. #if defined(CONFIG_440GX) || \
  123. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  124. defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  125. /* Whack the M1 register */
  126. mode_reg = 0x0;
  127. if (sysinfo.freqOPB <= 50000000);
  128. else if (sysinfo.freqOPB <= 66666667)
  129. mode_reg |= EMAC_MR1_OBCI_66;
  130. else if (sysinfo.freqOPB <= 83333333)
  131. mode_reg |= EMAC_MR1_OBCI_83;
  132. else if (sysinfo.freqOPB <= 100000000)
  133. mode_reg |= EMAC_MR1_OBCI_100;
  134. else
  135. mode_reg |= EMAC_MR1_OBCI_GT100;
  136. out_be32 ((void*)(EMAC0_MR1 + hw_addr), mode_reg);
  137. #endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
  138. /* set the Mal configuration reg */
  139. #if defined(CONFIG_440GX) || \
  140. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  141. defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  142. mtdcr (MAL0_CFG, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
  143. MAL_CR_PLBLT_DEFAULT | 0x00330000);
  144. #else
  145. mtdcr (MAL0_CFG, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
  146. /* Errata 1.12: MAL_1 -- Disable MAL bursting */
  147. if (get_pvr() == PVR_440GP_RB) {
  148. mtdcr (MAL0_CFG, mfdcr(MAL0_CFG) & ~MAL_CR_PLBB);
  149. }
  150. #endif
  151. /* setup buffer descriptors */
  152. tx.ctrl = MAL_TX_CTRL_WRAP;
  153. tx.data_len = 0;
  154. tx.data_ptr = (char*)L1_CACHE_ALIGN((u32)tx_buf);
  155. rx.ctrl = MAL_TX_CTRL_WRAP | MAL_RX_CTRL_EMPTY;
  156. rx.data_len = 0;
  157. rx.data_ptr = (char*)L1_CACHE_ALIGN((u32)rx_buf);
  158. flush_dcache_range((u32)&rx, (u32)&rx + sizeof(mal_desc_t));
  159. flush_dcache_range((u32)&tx, (u32)&tx + sizeof(mal_desc_t));
  160. switch (devnum) {
  161. case 1:
  162. /* setup MAL tx & rx channel pointers */
  163. #if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
  164. mtdcr (MAL0_TXCTP2R, &tx);
  165. #else
  166. mtdcr (MAL0_TXCTP1R, &tx);
  167. #endif
  168. #if defined(CONFIG_440)
  169. mtdcr (MAL0_TXBADDR, 0x0);
  170. mtdcr (MAL0_RXBADDR, 0x0);
  171. #endif
  172. mtdcr (MAL0_RXCTP1R, &rx);
  173. /* set RX buffer size */
  174. mtdcr (MAL0_RCBS1, PKTSIZE_ALIGN / 16);
  175. break;
  176. case 0:
  177. default:
  178. /* setup MAL tx & rx channel pointers */
  179. #if defined(CONFIG_440)
  180. mtdcr (MAL0_TXBADDR, 0x0);
  181. mtdcr (MAL0_RXBADDR, 0x0);
  182. #endif
  183. mtdcr (MAL0_TXCTP0R, &tx);
  184. mtdcr (MAL0_RXCTP0R, &rx);
  185. /* set RX buffer size */
  186. mtdcr (MAL0_RCBS0, PKTSIZE_ALIGN / 16);
  187. break;
  188. }
  189. /* Enable MAL transmit and receive channels */
  190. #if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
  191. mtdcr (MAL0_TXCASR, (MAL_TXRX_CASR >> (devnum*2)));
  192. #else
  193. mtdcr (MAL0_TXCASR, (MAL_TXRX_CASR >> devnum));
  194. #endif
  195. mtdcr (MAL0_RXCASR, (MAL_TXRX_CASR >> devnum));
  196. /* set internal loopback mode */
  197. #ifdef CONFIG_SYS_POST_ETHER_EXT_LOOPBACK
  198. out_be32 ((void*)(EMAC0_MR1 + hw_addr), EMAC_MR1_FDE | 0 |
  199. EMAC_MR1_RFS_4K | EMAC_MR1_TX_FIFO_2K |
  200. EMAC_MR1_MF_100MBPS | EMAC_MR1_IST |
  201. in_be32 ((void*)(EMAC0_MR1 + hw_addr)));
  202. #else
  203. out_be32 ((void*)(EMAC0_MR1 + hw_addr), EMAC_MR1_FDE | EMAC_MR1_ILE |
  204. EMAC_MR1_RFS_4K | EMAC_MR1_TX_FIFO_2K |
  205. EMAC_MR1_MF_100MBPS | EMAC_MR1_IST |
  206. in_be32 ((void*)(EMAC0_MR1 + hw_addr)));
  207. #endif
  208. /* set transmit enable & receive enable */
  209. out_be32 ((void*)(EMAC0_MR0 + hw_addr), EMAC_MR0_TXE | EMAC_MR0_RXE);
  210. /* enable broadcast address */
  211. out_be32 ((void*)(EMAC0_RXM + hw_addr), EMAC_RMR_BAE);
  212. /* set transmit request threshold register */
  213. out_be32 ((void*)(EMAC0_TRTR + hw_addr), 0x18000000); /* 256 byte threshold */
  214. /* set receive low/high water mark register */
  215. #if defined(CONFIG_440)
  216. /* 440s has a 64 byte burst length */
  217. out_be32 ((void*)(EMAC0_RX_HI_LO_WMARK + hw_addr), 0x80009000);
  218. #else
  219. /* 405s have a 16 byte burst length */
  220. out_be32 ((void*)(EMAC0_RX_HI_LO_WMARK + hw_addr), 0x0f002000);
  221. #endif /* defined(CONFIG_440) */
  222. out_be32 ((void*)(EMAC0_TMR1 + hw_addr), 0xf8640000);
  223. /* Set fifo limit entry in tx mode 0 */
  224. out_be32 ((void*)(EMAC0_TMR0 + hw_addr), 0x00000003);
  225. /* Frame gap set */
  226. out_be32 ((void*)(EMAC0_I_FRAME_GAP_REG + hw_addr), 0x00000008);
  227. sync ();
  228. }
  229. static void ether_post_halt (int devnum, int hw_addr)
  230. {
  231. int i = 0;
  232. #if defined(CONFIG_440SPE) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  233. unsigned long mfr;
  234. #endif
  235. /* 1st reset MAL channel */
  236. /* Note: writing a 0 to a channel has no effect */
  237. #if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
  238. mtdcr (MAL0_TXCARR, MAL_TXRX_CASR >> (devnum * 2));
  239. #else
  240. mtdcr (MAL0_TXCARR, MAL_TXRX_CASR >> devnum);
  241. #endif
  242. mtdcr (MAL0_RXCARR, MAL_TXRX_CASR >> devnum);
  243. /* wait for reset */
  244. while (mfdcr (MAL0_RXCASR) & (MAL_TXRX_CASR >> devnum)) {
  245. if (i++ >= 1000)
  246. break;
  247. udelay (1000);
  248. }
  249. /* emac reset */
  250. out_be32 ((void*)(EMAC0_MR0 + hw_addr), EMAC_MR0_SRST);
  251. #if defined(CONFIG_440SPE) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  252. /* remove clocks for EMAC internal loopback */
  253. mfsdr (SDR0_MFR, mfr);
  254. mfr &= ~SDR0_MFR_ETH_CLK_SEL_V(devnum);
  255. mtsdr (SDR0_MFR, mfr);
  256. #endif
  257. }
  258. static void ether_post_send (int devnum, int hw_addr, void *packet, int length)
  259. {
  260. int i = 0;
  261. while (tx.ctrl & MAL_TX_CTRL_READY) {
  262. if (i++ > 100) {
  263. printf ("TX timeout\n");
  264. return;
  265. }
  266. udelay (1000);
  267. invalidate_dcache_range((u32)&tx, (u32)&tx + sizeof(mal_desc_t));
  268. }
  269. tx.ctrl = MAL_TX_CTRL_READY | MAL_TX_CTRL_WRAP | MAL_TX_CTRL_LAST |
  270. EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP;
  271. tx.data_len = length;
  272. memcpy (tx.data_ptr, packet, length);
  273. flush_dcache_range((u32)&tx, (u32)&tx + sizeof(mal_desc_t));
  274. flush_dcache_range((u32)tx.data_ptr, (u32)tx.data_ptr + length);
  275. sync ();
  276. out_be32 ((void*)(EMAC0_TMR0 + hw_addr), in_be32 ((void*)(EMAC0_TMR0 + hw_addr)) | EMAC_TMR0_GNP0);
  277. sync ();
  278. }
  279. static int ether_post_recv (int devnum, int hw_addr, void *packet, int max_length)
  280. {
  281. int length;
  282. int i = 0;
  283. while (rx.ctrl & MAL_RX_CTRL_EMPTY) {
  284. if (i++ > 100) {
  285. printf ("RX timeout\n");
  286. return 0;
  287. }
  288. udelay (1000);
  289. invalidate_dcache_range((u32)&rx, (u32)&rx + sizeof(mal_desc_t));
  290. }
  291. length = rx.data_len - 4;
  292. if (length <= max_length) {
  293. invalidate_dcache_range((u32)rx.data_ptr, (u32)rx.data_ptr + length);
  294. memcpy(packet, rx.data_ptr, length);
  295. }
  296. sync ();
  297. rx.ctrl |= MAL_RX_CTRL_EMPTY;
  298. flush_dcache_range((u32)&rx, (u32)&rx + sizeof(mal_desc_t));
  299. sync ();
  300. return length;
  301. }
  302. /*
  303. * Test routines
  304. */
  305. static void packet_fill (char *packet, int length)
  306. {
  307. char c = (char) length;
  308. int i;
  309. /* set up ethernet header */
  310. memset (packet, 0xff, 14);
  311. for (i = 14; i < length; i++) {
  312. packet[i] = c++;
  313. }
  314. }
  315. static int packet_check (char *packet, int length)
  316. {
  317. char c = (char) length;
  318. int i;
  319. for (i = 14; i < length; i++) {
  320. if (packet[i] != c++)
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. char packet_send[MAX_PACKET_LENGTH];
  326. char packet_recv[MAX_PACKET_LENGTH];
  327. static int test_ctlr (int devnum, int hw_addr)
  328. {
  329. int res = -1;
  330. int length;
  331. int l;
  332. ether_post_init (devnum, hw_addr);
  333. for (l = MAX_PACKET_LENGTH; l >= MIN_PACKET_LENGTH;
  334. l -= PACKET_INCR) {
  335. packet_fill (packet_send, l);
  336. ether_post_send (devnum, hw_addr, packet_send, l);
  337. length = ether_post_recv (devnum, hw_addr, packet_recv,
  338. sizeof (packet_recv));
  339. if (length != l || packet_check (packet_recv, length) < 0) {
  340. goto Done;
  341. }
  342. }
  343. res = 0;
  344. Done:
  345. ether_post_halt (devnum, hw_addr);
  346. if (res != 0) {
  347. post_log ("EMAC%d test failed\n", devnum);
  348. }
  349. return res;
  350. }
  351. int ether_post_test (int flags)
  352. {
  353. int res = 0;
  354. int i;
  355. /* Allocate tx & rx packet buffers */
  356. tx_buf = malloc (PKTSIZE_ALIGN + CONFIG_SYS_CACHELINE_SIZE);
  357. rx_buf = malloc (PKTSIZE_ALIGN + CONFIG_SYS_CACHELINE_SIZE);
  358. if (!tx_buf || !rx_buf) {
  359. printf ("Failed to allocate packet buffers\n");
  360. res = -1;
  361. goto out_free;
  362. }
  363. for (i = 0; i < LAST_EMAC_NUM; i++) {
  364. if (test_ctlr (i, i*0x100))
  365. res = -1;
  366. }
  367. out_free:
  368. free (tx_buf);
  369. free (rx_buf);
  370. return res;
  371. }
  372. #endif /* CONFIG_POST & CONFIG_SYS_POST_ETHER */