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