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