ether.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. * TEST_NUM - number of tests
  36. */
  37. #ifdef CONFIG_POST
  38. #include <post.h>
  39. #if CONFIG_POST & CFG_POST_ETHER
  40. #include <asm/cache.h>
  41. #include <asm/io.h>
  42. #include <asm/processor.h>
  43. #include <405_mal.h>
  44. #include <ppc4xx_enet.h>
  45. #include <malloc.h>
  46. DECLARE_GLOBAL_DATA_PTR;
  47. #if defined(CONFIG_440SPE) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  48. #define SDR0_MFR_ETH_CLK_SEL_V(n) ((0x01<<27) / (n+1))
  49. #endif
  50. #define MIN_PACKET_LENGTH 64
  51. #define MAX_PACKET_LENGTH 256
  52. #define TEST_NUM 1
  53. static volatile mal_desc_t tx __cacheline_aligned;
  54. static volatile mal_desc_t rx __cacheline_aligned;
  55. static char *tx_buf;
  56. static char *rx_buf;
  57. static void ether_post_init (int devnum, int hw_addr)
  58. {
  59. int i;
  60. #if defined(CONFIG_440GX) || \
  61. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  62. defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  63. unsigned mode_reg;
  64. sys_info_t sysinfo;
  65. #endif
  66. #if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || defined(CONFIG_440SPE)
  67. unsigned long mfr;
  68. #endif
  69. #if defined(CONFIG_440GX) || \
  70. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  71. defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  72. /* Need to get the OPB frequency so we can access the PHY */
  73. get_sys_info (&sysinfo);
  74. #endif
  75. #if defined(CONFIG_440SPE) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  76. /* provide clocks for EMAC internal loopback */
  77. mfsdr (sdr_mfr, mfr);
  78. mfr |= SDR0_MFR_ETH_CLK_SEL_V(devnum);
  79. mtsdr (sdr_mfr, mfr);
  80. sync ();
  81. #endif
  82. /* reset emac */
  83. out32 (EMAC_M0 + hw_addr, EMAC_M0_SRST);
  84. sync ();
  85. for (i = 0;; i++) {
  86. if (!(in32 (EMAC_M0 + hw_addr) & EMAC_M0_SRST))
  87. break;
  88. if (i >= 1000) {
  89. printf ("Timeout resetting EMAC\n");
  90. break;
  91. }
  92. udelay (1000);
  93. }
  94. #if defined(CONFIG_440GX) || \
  95. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  96. defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  97. /* Whack the M1 register */
  98. mode_reg = 0x0;
  99. if (sysinfo.freqOPB <= 50000000);
  100. else if (sysinfo.freqOPB <= 66666667)
  101. mode_reg |= EMAC_M1_OBCI_66;
  102. else if (sysinfo.freqOPB <= 83333333)
  103. mode_reg |= EMAC_M1_OBCI_83;
  104. else if (sysinfo.freqOPB <= 100000000)
  105. mode_reg |= EMAC_M1_OBCI_100;
  106. else
  107. mode_reg |= EMAC_M1_OBCI_GT100;
  108. out32 (EMAC_M1 + hw_addr, mode_reg);
  109. #endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
  110. /* set the Mal configuration reg */
  111. #if defined(CONFIG_440GX) || \
  112. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  113. defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  114. mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
  115. MAL_CR_PLBLT_DEFAULT | 0x00330000);
  116. #else
  117. mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
  118. /* Errata 1.12: MAL_1 -- Disable MAL bursting */
  119. if (get_pvr() == PVR_440GP_RB) {
  120. mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
  121. }
  122. #endif
  123. /* setup buffer descriptors */
  124. tx.ctrl = MAL_TX_CTRL_WRAP;
  125. tx.data_len = 0;
  126. tx.data_ptr = (char*)L1_CACHE_ALIGN((u32)tx_buf);
  127. rx.ctrl = MAL_TX_CTRL_WRAP | MAL_RX_CTRL_EMPTY;
  128. rx.data_len = 0;
  129. rx.data_ptr = (char*)L1_CACHE_ALIGN((u32)rx_buf);
  130. switch (devnum) {
  131. case 1:
  132. /* setup MAL tx & rx channel pointers */
  133. #if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
  134. mtdcr (maltxctp2r, &tx);
  135. #else
  136. mtdcr (maltxctp1r, &tx);
  137. #endif
  138. #if defined(CONFIG_440)
  139. mtdcr (maltxbattr, 0x0);
  140. mtdcr (malrxbattr, 0x0);
  141. #endif
  142. mtdcr (malrxctp1r, &rx);
  143. /* set RX buffer size */
  144. mtdcr (malrcbs1, PKTSIZE_ALIGN / 16);
  145. break;
  146. case 0:
  147. default:
  148. /* setup MAL tx & rx channel pointers */
  149. #if defined(CONFIG_440)
  150. mtdcr (maltxbattr, 0x0);
  151. mtdcr (malrxbattr, 0x0);
  152. #endif
  153. mtdcr (maltxctp0r, &tx);
  154. mtdcr (malrxctp0r, &rx);
  155. /* set RX buffer size */
  156. mtdcr (malrcbs0, PKTSIZE_ALIGN / 16);
  157. break;
  158. }
  159. /* Enable MAL transmit and receive channels */
  160. #if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
  161. mtdcr (maltxcasr, (MAL_TXRX_CASR >> (devnum*2)));
  162. #else
  163. mtdcr (maltxcasr, (MAL_TXRX_CASR >> devnum));
  164. #endif
  165. mtdcr (malrxcasr, (MAL_TXRX_CASR >> devnum));
  166. /* set internal loopback mode */
  167. #ifdef CFG_POST_ETHER_EXT_LOOPBACK
  168. out32 (EMAC_M1 + hw_addr, EMAC_M1_FDE | 0 |
  169. EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K |
  170. EMAC_M1_MF_100MBPS | EMAC_M1_IST |
  171. in32 (EMAC_M1));
  172. #else
  173. out32 (EMAC_M1 + hw_addr, EMAC_M1_FDE | EMAC_M1_ILE |
  174. EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K |
  175. EMAC_M1_MF_100MBPS | EMAC_M1_IST |
  176. in32 (EMAC_M1));
  177. #endif
  178. /* set transmit enable & receive enable */
  179. out32 (EMAC_M0 + hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
  180. /* enable broadcast address */
  181. out32 (EMAC_RXM + hw_addr, EMAC_RMR_BAE);
  182. /* set transmit request threshold register */
  183. out32 (EMAC_TRTR + hw_addr, 0x18000000); /* 256 byte threshold */
  184. /* set receive low/high water mark register */
  185. #if defined(CONFIG_440)
  186. /* 440s has a 64 byte burst length */
  187. out32 (EMAC_RX_HI_LO_WMARK + hw_addr, 0x80009000);
  188. #else
  189. /* 405s have a 16 byte burst length */
  190. out32 (EMAC_RX_HI_LO_WMARK + hw_addr, 0x0f002000);
  191. #endif /* defined(CONFIG_440) */
  192. out32 (EMAC_TXM1 + hw_addr, 0xf8640000);
  193. /* Set fifo limit entry in tx mode 0 */
  194. out32 (EMAC_TXM0 + hw_addr, 0x00000003);
  195. /* Frame gap set */
  196. out32 (EMAC_I_FRAME_GAP_REG + hw_addr, 0x00000008);
  197. sync ();
  198. }
  199. static void ether_post_halt (int devnum, int hw_addr)
  200. {
  201. int i = 0;
  202. #if defined(CONFIG_440SPE) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  203. unsigned long mfr;
  204. #endif
  205. /* 1st reset MAL channel */
  206. /* Note: writing a 0 to a channel has no effect */
  207. #if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
  208. mtdcr (maltxcarr, MAL_TXRX_CASR >> (devnum * 2));
  209. #else
  210. mtdcr (maltxcarr, MAL_TXRX_CASR >> devnum);
  211. #endif
  212. mtdcr (malrxcarr, MAL_TXRX_CASR >> devnum);
  213. /* wait for reset */
  214. while (mfdcr (malrxcasr) & (MAL_TXRX_CASR >> devnum)) {
  215. if (i++ >= 1000)
  216. break;
  217. udelay (1000);
  218. }
  219. /* emac reset */
  220. out32 (EMAC_M0 + hw_addr, EMAC_M0_SRST);
  221. #if defined(CONFIG_440SPE) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  222. /* remove clocks for EMAC internal loopback */
  223. mfsdr (sdr_mfr, mfr);
  224. mfr &= ~SDR0_MFR_ETH_CLK_SEL_V(devnum);
  225. mtsdr (sdr_mfr, mfr);
  226. #endif
  227. }
  228. static void ether_post_send (int devnum, int hw_addr, void *packet, int length)
  229. {
  230. int i = 0;
  231. while (tx.ctrl & MAL_TX_CTRL_READY) {
  232. if (i++ > 100) {
  233. printf ("TX timeout\n");
  234. return;
  235. }
  236. udelay (1000);
  237. }
  238. tx.ctrl = MAL_TX_CTRL_READY | MAL_TX_CTRL_WRAP | MAL_TX_CTRL_LAST |
  239. EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP;
  240. tx.data_len = length;
  241. memcpy (tx.data_ptr, packet, length);
  242. sync ();
  243. out32 (EMAC_TXM0 + hw_addr, in32 (EMAC_TXM0 + hw_addr) | EMAC_TXM0_GNP0);
  244. sync ();
  245. }
  246. static int ether_post_recv (int devnum, int hw_addr, void *packet, int max_length)
  247. {
  248. int length;
  249. int i = 0;
  250. while (rx.ctrl & MAL_RX_CTRL_EMPTY) {
  251. if (i++ > 100) {
  252. printf ("RX timeout\n");
  253. return 0;
  254. }
  255. udelay (1000);
  256. }
  257. length = rx.data_len - 4;
  258. if (length <= max_length)
  259. memcpy(packet, rx.data_ptr, length);
  260. sync ();
  261. rx.ctrl |= MAL_RX_CTRL_EMPTY;
  262. sync ();
  263. return length;
  264. }
  265. /*
  266. * Test routines
  267. */
  268. static void packet_fill (char *packet, int length)
  269. {
  270. char c = (char) length;
  271. int i;
  272. /* set up ethernet header */
  273. memset (packet, 0xff, 14);
  274. for (i = 14; i < length; i++) {
  275. packet[i] = c++;
  276. }
  277. }
  278. static int packet_check (char *packet, int length)
  279. {
  280. char c = (char) length;
  281. int i;
  282. for (i = 14; i < length; i++) {
  283. if (packet[i] != c++)
  284. return -1;
  285. }
  286. return 0;
  287. }
  288. static int test_ctlr (int devnum, int hw_addr)
  289. {
  290. int res = -1;
  291. char packet_send[MAX_PACKET_LENGTH];
  292. char packet_recv[MAX_PACKET_LENGTH];
  293. int length;
  294. int i;
  295. int l;
  296. ether_post_init (devnum, hw_addr);
  297. for (i = 0; i < TEST_NUM; i++) {
  298. for (l = MIN_PACKET_LENGTH; l <= MAX_PACKET_LENGTH; l++) {
  299. packet_fill (packet_send, l);
  300. ether_post_send (devnum, hw_addr, packet_send, l);
  301. length = ether_post_recv (devnum, hw_addr, packet_recv,
  302. sizeof (packet_recv));
  303. if (length != l || packet_check (packet_recv, length) < 0) {
  304. goto Done;
  305. }
  306. }
  307. }
  308. res = 0;
  309. Done:
  310. ether_post_halt (devnum, hw_addr);
  311. if (res != 0) {
  312. post_log ("EMAC%d test failed\n", devnum);
  313. }
  314. return res;
  315. }
  316. int ether_post_test (int flags)
  317. {
  318. int res = 0;
  319. /* Allocate tx & rx packet buffers */
  320. tx_buf = malloc (PKTSIZE_ALIGN + CFG_CACHELINE_SIZE);
  321. rx_buf = malloc (PKTSIZE_ALIGN + CFG_CACHELINE_SIZE);
  322. if (!tx_buf || !rx_buf) {
  323. printf ("Failed to allocate packet buffers\n");
  324. res = -1;
  325. goto out_free;
  326. }
  327. /* EMAC0 */
  328. if (test_ctlr (0, 0))
  329. res = -1;
  330. /* EMAC1 */
  331. if (test_ctlr (1, 0x100))
  332. res = -1;
  333. out_free:
  334. free (tx_buf);
  335. free (rx_buf);
  336. return res;
  337. }
  338. #endif /* CONFIG_POST & CFG_POST_ETHER */
  339. #endif /* CONFIG_POST */