ether.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. unsigned mode_reg;
  61. #if defined(CONFIG_440GX) || \
  62. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  63. defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  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. out32 (EMAC_M1 + hw_addr, EMAC_M1_FDE | EMAC_M1_ILE |
  168. EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K |
  169. EMAC_M1_MF_100MBPS | EMAC_M1_IST |
  170. in32 (EMAC_M1));
  171. /* set transmit enable & receive enable */
  172. out32 (EMAC_M0 + hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
  173. /* enable broadcast address */
  174. out32 (EMAC_RXM + hw_addr, EMAC_RMR_BAE);
  175. /* set transmit request threshold register */
  176. out32 (EMAC_TRTR + hw_addr, 0x18000000); /* 256 byte threshold */
  177. /* set receive low/high water mark register */
  178. #if defined(CONFIG_440)
  179. /* 440s has a 64 byte burst length */
  180. out32 (EMAC_RX_HI_LO_WMARK + hw_addr, 0x80009000);
  181. #else
  182. /* 405s have a 16 byte burst length */
  183. out32 (EMAC_RX_HI_LO_WMARK + hw_addr, 0x0f002000);
  184. #endif /* defined(CONFIG_440) */
  185. out32 (EMAC_TXM1 + hw_addr, 0xf8640000);
  186. /* Set fifo limit entry in tx mode 0 */
  187. out32 (EMAC_TXM0 + hw_addr, 0x00000003);
  188. /* Frame gap set */
  189. out32 (EMAC_I_FRAME_GAP_REG + hw_addr, 0x00000008);
  190. sync ();
  191. }
  192. static void ether_post_halt (int devnum, int hw_addr)
  193. {
  194. int i = 0;
  195. #if defined(CONFIG_440SPE) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  196. unsigned long mfr;
  197. #endif
  198. /* 1st reset MAL channel */
  199. /* Note: writing a 0 to a channel has no effect */
  200. #if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
  201. mtdcr (maltxcarr, MAL_TXRX_CASR >> (devnum * 2));
  202. #else
  203. mtdcr (maltxcarr, MAL_TXRX_CASR >> devnum);
  204. #endif
  205. mtdcr (malrxcarr, MAL_TXRX_CASR >> devnum);
  206. /* wait for reset */
  207. while (mfdcr (malrxcasr) & (MAL_TXRX_CASR >> devnum)) {
  208. if (i++ >= 1000)
  209. break;
  210. udelay (1000);
  211. }
  212. /* emac reset */
  213. out32 (EMAC_M0 + hw_addr, EMAC_M0_SRST);
  214. #if defined(CONFIG_440SPE) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  215. /* remove clocks for EMAC internal loopback */
  216. mfsdr (sdr_mfr, mfr);
  217. mfr &= ~SDR0_MFR_ETH_CLK_SEL_V(devnum);
  218. mtsdr (sdr_mfr, mfr);
  219. #endif
  220. }
  221. static void ether_post_send (int devnum, int hw_addr, void *packet, int length)
  222. {
  223. int i = 0;
  224. while (tx.ctrl & MAL_TX_CTRL_READY) {
  225. if (i++ > 100) {
  226. printf ("TX timeout\n");
  227. return;
  228. }
  229. udelay (1000);
  230. }
  231. tx.ctrl = MAL_TX_CTRL_READY | MAL_TX_CTRL_WRAP | MAL_TX_CTRL_LAST |
  232. EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP;
  233. tx.data_len = length;
  234. memcpy (tx.data_ptr, packet, length);
  235. sync ();
  236. out32 (EMAC_TXM0 + hw_addr, in32 (EMAC_TXM0 + hw_addr) | EMAC_TXM0_GNP0);
  237. sync ();
  238. }
  239. static int ether_post_recv (int devnum, int hw_addr, void *packet, int max_length)
  240. {
  241. int length;
  242. int i = 0;
  243. while (rx.ctrl & MAL_RX_CTRL_EMPTY) {
  244. if (i++ > 100) {
  245. printf ("RX timeout\n");
  246. return 0;
  247. }
  248. udelay (1000);
  249. }
  250. length = rx.data_len - 4;
  251. if (length <= max_length)
  252. memcpy(packet, rx.data_ptr, length);
  253. sync ();
  254. rx.ctrl |= MAL_RX_CTRL_EMPTY;
  255. sync ();
  256. return length;
  257. }
  258. /*
  259. * Test routines
  260. */
  261. static void packet_fill (char *packet, int length)
  262. {
  263. char c = (char) length;
  264. int i;
  265. /* set up ethernet header */
  266. memset (packet, 0xff, 14);
  267. for (i = 14; i < length; i++) {
  268. packet[i] = c++;
  269. }
  270. }
  271. static int packet_check (char *packet, int length)
  272. {
  273. char c = (char) length;
  274. int i;
  275. for (i = 14; i < length; i++) {
  276. if (packet[i] != c++)
  277. return -1;
  278. }
  279. return 0;
  280. }
  281. static int test_ctlr (int devnum, int hw_addr)
  282. {
  283. int res = -1;
  284. char packet_send[MAX_PACKET_LENGTH];
  285. char packet_recv[MAX_PACKET_LENGTH];
  286. int length;
  287. int i;
  288. int l;
  289. ether_post_init (devnum, hw_addr);
  290. for (i = 0; i < TEST_NUM; i++) {
  291. for (l = MIN_PACKET_LENGTH; l <= MAX_PACKET_LENGTH; l++) {
  292. packet_fill (packet_send, l);
  293. ether_post_send (devnum, hw_addr, packet_send, l);
  294. length = ether_post_recv (devnum, hw_addr, packet_recv,
  295. sizeof (packet_recv));
  296. if (length != l || packet_check (packet_recv, length) < 0) {
  297. goto Done;
  298. }
  299. }
  300. }
  301. res = 0;
  302. Done:
  303. ether_post_halt (devnum, hw_addr);
  304. if (res != 0) {
  305. post_log ("EMAC%d test failed\n", devnum);
  306. }
  307. return res;
  308. }
  309. int ether_post_test (int flags)
  310. {
  311. int res = 0;
  312. /* Allocate tx & rx packet buffers */
  313. tx_buf = malloc (PKTSIZE_ALIGN + CFG_CACHELINE_SIZE);
  314. rx_buf = malloc (PKTSIZE_ALIGN + CFG_CACHELINE_SIZE);
  315. if (!tx_buf || !rx_buf) {
  316. printf ("Failed to allocate packet buffers\n");
  317. res = -1;
  318. goto out_free;
  319. }
  320. /* EMAC0 */
  321. if (test_ctlr (0, 0))
  322. res = -1;
  323. /* EMAC1 */
  324. if (test_ctlr (1, 0x100))
  325. res = -1;
  326. out_free:
  327. free (tx_buf);
  328. free (rx_buf);
  329. return res;
  330. }
  331. #endif /* CONFIG_POST & CFG_POST_ETHER */
  332. #endif /* CONFIG_POST */