wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * tsec.c |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 3 | * Freescale Three Speed Ethernet Controller driver |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 4 | * |
| 5 | * This software may be used and distributed according to the |
| 6 | * terms of the GNU Public License, Version 2, incorporated |
| 7 | * herein by reference. |
| 8 | * |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 9 | * Copyright 2004 Freescale Semiconductor. |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 10 | * (C) Copyright 2003, Motorola, Inc. |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 11 | * author Andy Fleming |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <config.h> |
| 16 | #include <mpc85xx.h> |
| 17 | #include <common.h> |
| 18 | #include <malloc.h> |
| 19 | #include <net.h> |
| 20 | #include <command.h> |
| 21 | |
| 22 | #if defined(CONFIG_TSEC_ENET) |
| 23 | #include "tsec.h" |
| 24 | |
| 25 | #define TX_BUF_CNT 2 |
| 26 | |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 27 | static uint rxIdx; /* index of the current RX buffer */ |
| 28 | static uint txIdx; /* index of the current TX buffer */ |
| 29 | |
| 30 | typedef volatile struct rtxbd { |
| 31 | txbd8_t txbd[TX_BUF_CNT]; |
| 32 | rxbd8_t rxbd[PKTBUFSRX]; |
| 33 | } RTXBD; |
| 34 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 35 | struct tsec_info_struct { |
| 36 | unsigned int phyaddr; |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 37 | u32 flags; |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 38 | unsigned int phyregidx; |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | /* The tsec_info structure contains 3 values which the |
| 43 | * driver uses to determine how to operate a given ethernet |
| 44 | * device. For now, the structure is initialized with the |
| 45 | * knowledge that all current implementations have 2 TSEC |
| 46 | * devices, and one FEC. The information needed is: |
| 47 | * phyaddr - The address of the PHY which is attached to |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 48 | * the given device. |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 49 | * |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 50 | * flags - This variable indicates whether the device |
| 51 | * supports gigabit speed ethernet, and whether it should be |
| 52 | * in reduced mode. |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 53 | * |
| 54 | * phyregidx - This variable specifies which ethernet device |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 55 | * controls the MII Management registers which are connected |
| 56 | * to the PHY. For 8540/8560, only TSEC1 (index 0) has |
| 57 | * access to the PHYs, so all of the entries have "0". |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 58 | * |
| 59 | * The values specified in the table are taken from the board's |
| 60 | * config file in include/configs/. When implementing a new |
| 61 | * board with ethernet capability, it is necessary to define: |
| 62 | * TSEC1_PHY_ADDR |
| 63 | * TSEC1_PHYIDX |
| 64 | * TSEC2_PHY_ADDR |
| 65 | * TSEC2_PHYIDX |
| 66 | * |
| 67 | * and for 8560: |
| 68 | * FEC_PHY_ADDR |
| 69 | * FEC_PHYIDX |
| 70 | */ |
| 71 | static struct tsec_info_struct tsec_info[] = { |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 72 | #if defined(CONFIG_MPC85XX_TSEC1) || defined(CONFIG_MPC83XX_TSEC1) |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 73 | {TSEC1_PHY_ADDR, TSEC_GIGABIT, TSEC1_PHYIDX}, |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 74 | #else |
| 75 | { 0, 0, 0}, |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 76 | #endif |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 77 | #if defined(CONFIG_MPC85XX_TSEC2) || defined(CONFIG_MPC83XX_TSEC2) |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 78 | {TSEC2_PHY_ADDR, TSEC_GIGABIT, TSEC2_PHYIDX}, |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 79 | #else |
| 80 | { 0, 0, 0}, |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 81 | #endif |
| 82 | #ifdef CONFIG_MPC85XX_FEC |
| 83 | {FEC_PHY_ADDR, 0, FEC_PHYIDX}, |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 84 | #else |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 85 | # if defined(CONFIG_MPC85XX_TSEC3) || defined(CONFIG_MPC83XX_TSEC3) |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 86 | {TSEC3_PHY_ADDR, TSEC_GIGABIT | TSEC_REDUCED, TSEC3_PHYIDX}, |
| 87 | # else |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 88 | { 0, 0, 0}, |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 89 | # endif |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 90 | # if defined(CONFIG_MPC85XX_TSEC4) || defined(CONFIG_MPC83XX_TSEC4) |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 91 | {TSEC4_PHY_ADDR, TSEC_REDUCED, TSEC4_PHYIDX}, |
| 92 | # else |
| 93 | { 0, 0, 0}, |
| 94 | # endif |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 95 | #endif |
| 96 | }; |
| 97 | |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 98 | #define MAXCONTROLLERS (4) |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 99 | |
| 100 | static int relocated = 0; |
| 101 | |
| 102 | static struct tsec_private *privlist[MAXCONTROLLERS]; |
| 103 | |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 104 | #ifdef __GNUC__ |
| 105 | static RTXBD rtx __attribute__ ((aligned(8))); |
| 106 | #else |
| 107 | #error "rtx must be 64-bit aligned" |
| 108 | #endif |
| 109 | |
| 110 | static int tsec_send(struct eth_device* dev, volatile void *packet, int length); |
| 111 | static int tsec_recv(struct eth_device* dev); |
| 112 | static int tsec_init(struct eth_device* dev, bd_t * bd); |
| 113 | static void tsec_halt(struct eth_device* dev); |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 114 | static void init_registers(volatile tsec_t *regs); |
| 115 | static void startup_tsec(struct eth_device *dev); |
| 116 | static int init_phy(struct eth_device *dev); |
| 117 | void write_phy_reg(struct tsec_private *priv, uint regnum, uint value); |
| 118 | uint read_phy_reg(struct tsec_private *priv, uint regnum); |
| 119 | struct phy_info * get_phy_info(struct eth_device *dev); |
| 120 | void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd); |
| 121 | static void adjust_link(struct eth_device *dev); |
| 122 | static void relocate_cmds(void); |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 123 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 124 | /* Initialize device structure. Returns success if PHY |
| 125 | * initialization succeeded (i.e. if it recognizes the PHY) |
| 126 | */ |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 127 | int tsec_initialize(bd_t *bis, int index, char *devname) |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 128 | { |
| 129 | struct eth_device* dev; |
| 130 | int i; |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 131 | struct tsec_private *priv; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 132 | |
| 133 | dev = (struct eth_device*) malloc(sizeof *dev); |
| 134 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 135 | if(NULL == dev) |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 136 | return 0; |
| 137 | |
| 138 | memset(dev, 0, sizeof *dev); |
| 139 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 140 | priv = (struct tsec_private *) malloc(sizeof(*priv)); |
| 141 | |
| 142 | if(NULL == priv) |
| 143 | return 0; |
| 144 | |
| 145 | privlist[index] = priv; |
| 146 | priv->regs = (volatile tsec_t *)(TSEC_BASE_ADDR + index*TSEC_SIZE); |
| 147 | priv->phyregs = (volatile tsec_t *)(TSEC_BASE_ADDR + |
| 148 | tsec_info[index].phyregidx*TSEC_SIZE); |
| 149 | |
| 150 | priv->phyaddr = tsec_info[index].phyaddr; |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 151 | priv->flags = tsec_info[index].flags; |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 152 | |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 153 | sprintf(dev->name, devname); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 154 | dev->iobase = 0; |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 155 | dev->priv = priv; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 156 | dev->init = tsec_init; |
| 157 | dev->halt = tsec_halt; |
| 158 | dev->send = tsec_send; |
| 159 | dev->recv = tsec_recv; |
| 160 | |
| 161 | /* Tell u-boot to get the addr from the env */ |
| 162 | for(i=0;i<6;i++) |
| 163 | dev->enetaddr[i] = 0; |
| 164 | |
| 165 | eth_register(dev); |
| 166 | |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 167 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 168 | /* Reset the MAC */ |
| 169 | priv->regs->maccfg1 |= MACCFG1_SOFT_RESET; |
| 170 | priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET); |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 171 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 172 | /* Try to initialize PHY here, and return */ |
| 173 | return init_phy(dev); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | |
| 177 | /* Initializes data structures and registers for the controller, |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 178 | * and brings the interface up. Returns the link status, meaning |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 179 | * that it returns success if the link is up, failure otherwise. |
| 180 | * This allows u-boot to find the first active controller. */ |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 181 | int tsec_init(struct eth_device* dev, bd_t * bd) |
| 182 | { |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 183 | uint tempval; |
| 184 | char tmpbuf[MAC_ADDR_LEN]; |
| 185 | int i; |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 186 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 187 | volatile tsec_t *regs = priv->regs; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 188 | |
| 189 | /* Make sure the controller is stopped */ |
| 190 | tsec_halt(dev); |
| 191 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 192 | /* Init MACCFG2. Defaults to GMII */ |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 193 | regs->maccfg2 = MACCFG2_INIT_SETTINGS; |
| 194 | |
| 195 | /* Init ECNTRL */ |
| 196 | regs->ecntrl = ECNTRL_INIT_SETTINGS; |
| 197 | |
| 198 | /* Copy the station address into the address registers. |
| 199 | * Backwards, because little endian MACS are dumb */ |
| 200 | for(i=0;i<MAC_ADDR_LEN;i++) { |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 201 | tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i]; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 202 | } |
| 203 | (uint)(regs->macstnaddr1) = *((uint *)(tmpbuf)); |
| 204 | |
| 205 | tempval = *((uint *)(tmpbuf +4)); |
| 206 | |
| 207 | (uint)(regs->macstnaddr2) = tempval; |
| 208 | |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 209 | /* reset the indices to zero */ |
| 210 | rxIdx = 0; |
| 211 | txIdx = 0; |
| 212 | |
| 213 | /* Clear out (for the most part) the other registers */ |
| 214 | init_registers(regs); |
| 215 | |
| 216 | /* Ready the device for tx/rx */ |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 217 | startup_tsec(dev); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 218 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 219 | /* If there's no link, fail */ |
| 220 | return priv->link; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 221 | |
| 222 | } |
| 223 | |
| 224 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 225 | /* Write value to the device's PHY through the registers |
| 226 | * specified in priv, modifying the register specified in regnum. |
| 227 | * It will wait for the write to be done (or for a timeout to |
| 228 | * expire) before exiting |
| 229 | */ |
| 230 | void write_phy_reg(struct tsec_private *priv, uint regnum, uint value) |
| 231 | { |
| 232 | volatile tsec_t *regbase = priv->phyregs; |
| 233 | uint phyid = priv->phyaddr; |
| 234 | int timeout=1000000; |
| 235 | |
| 236 | regbase->miimadd = (phyid << 8) | regnum; |
| 237 | regbase->miimcon = value; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 238 | asm("sync"); |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 239 | |
| 240 | timeout=1000000; |
| 241 | while((regbase->miimind & MIIMIND_BUSY) && timeout--); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /* Reads register regnum on the device's PHY through the |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 246 | * registers specified in priv. It lowers and raises the read |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 247 | * command, and waits for the data to become valid (miimind |
| 248 | * notvalid bit cleared), and the bus to cease activity (miimind |
| 249 | * busy bit cleared), and then returns the value |
| 250 | */ |
| 251 | uint read_phy_reg(struct tsec_private *priv, uint regnum) |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 252 | { |
| 253 | uint value; |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 254 | volatile tsec_t *regbase = priv->phyregs; |
| 255 | uint phyid = priv->phyaddr; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 256 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 257 | /* Put the address of the phy, and the register |
| 258 | * number into MIIMADD */ |
| 259 | regbase->miimadd = (phyid << 8) | regnum; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 260 | |
| 261 | /* Clear the command register, and wait */ |
| 262 | regbase->miimcom = 0; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 263 | asm("sync"); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 264 | |
| 265 | /* Initiate a read command, and wait */ |
| 266 | regbase->miimcom = MIIM_READ_COMMAND; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 267 | asm("sync"); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 268 | |
| 269 | /* Wait for the the indication that the read is done */ |
| 270 | while((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY))); |
| 271 | |
| 272 | /* Grab the value read from the PHY */ |
| 273 | value = regbase->miimstat; |
| 274 | |
| 275 | return value; |
| 276 | } |
| 277 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 278 | |
| 279 | /* Discover which PHY is attached to the device, and configure it |
| 280 | * properly. If the PHY is not recognized, then return 0 |
| 281 | * (failure). Otherwise, return 1 |
| 282 | */ |
| 283 | static int init_phy(struct eth_device *dev) |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 284 | { |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 285 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 286 | struct phy_info *curphy; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 287 | |
| 288 | /* Assign a Physical address to the TBI */ |
wdenk | 3c2b3d4 | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 289 | |
wdenk | 3dd7f0f | 2005-04-04 23:43:44 +0000 | [diff] [blame] | 290 | { |
| 291 | volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR); |
| 292 | regs->tbipa = TBIPA_VALUE; |
| 293 | regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE); |
| 294 | regs->tbipa = TBIPA_VALUE; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 295 | asm("sync"); |
wdenk | 3dd7f0f | 2005-04-04 23:43:44 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* Reset MII (due to new addresses) */ |
| 299 | priv->phyregs->miimcfg = MIIMCFG_RESET; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 300 | asm("sync"); |
wdenk | 3dd7f0f | 2005-04-04 23:43:44 +0000 | [diff] [blame] | 301 | priv->phyregs->miimcfg = MIIMCFG_INIT_VALUE; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 302 | asm("sync"); |
wdenk | 3dd7f0f | 2005-04-04 23:43:44 +0000 | [diff] [blame] | 303 | while(priv->phyregs->miimind & MIIMIND_BUSY); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 304 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 305 | if(0 == relocated) |
| 306 | relocate_cmds(); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 307 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 308 | /* Get the cmd structure corresponding to the attached |
| 309 | * PHY */ |
| 310 | curphy = get_phy_info(dev); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 311 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 312 | if(NULL == curphy) { |
| 313 | printf("%s: No PHY found\n", dev->name); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 314 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 315 | return 0; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 316 | } |
| 317 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 318 | priv->phyinfo = curphy; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 319 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 320 | phy_run_commands(priv, priv->phyinfo->config); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 321 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 322 | return 1; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 326 | /* Returns which value to write to the control register. */ |
| 327 | /* For 10/100, the value is slightly different */ |
| 328 | uint mii_cr_init(uint mii_reg, struct tsec_private *priv) |
| 329 | { |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 330 | if(priv->flags & TSEC_GIGABIT) |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 331 | return MIIM_CONTROL_INIT; |
| 332 | else |
| 333 | return MIIM_CR_INIT; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | /* Parse the status register for link, and then do |
| 338 | * auto-negotiation */ |
| 339 | uint mii_parse_sr(uint mii_reg, struct tsec_private *priv) |
| 340 | { |
Stefan Roese | 5810dc3 | 2005-09-21 18:20:22 +0200 | [diff] [blame] | 341 | /* |
| 342 | * Wait if PHY is capable of autonegotiation and autonegotiation is not complete |
| 343 | */ |
| 344 | mii_reg = read_phy_reg(priv, MIIM_STATUS); |
| 345 | if ((mii_reg & PHY_BMSR_AUTN_ABLE) && !(mii_reg & PHY_BMSR_AUTN_COMP)) { |
| 346 | int i = 0; |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 347 | |
Stefan Roese | 5810dc3 | 2005-09-21 18:20:22 +0200 | [diff] [blame] | 348 | puts ("Waiting for PHY auto negotiation to complete"); |
| 349 | while (!((mii_reg & PHY_BMSR_AUTN_COMP) && (mii_reg & MIIM_STATUS_LINK))) { |
| 350 | /* |
| 351 | * Timeout reached ? |
| 352 | */ |
| 353 | if (i > PHY_AUTONEGOTIATE_TIMEOUT) { |
| 354 | puts (" TIMEOUT !\n"); |
| 355 | priv->link = 0; |
| 356 | break; |
| 357 | } |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 358 | |
Stefan Roese | 5810dc3 | 2005-09-21 18:20:22 +0200 | [diff] [blame] | 359 | if ((i++ % 1000) == 0) { |
| 360 | putc ('.'); |
| 361 | } |
| 362 | udelay (1000); /* 1 ms */ |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 363 | mii_reg = read_phy_reg(priv, MIIM_STATUS); |
Stefan Roese | 5810dc3 | 2005-09-21 18:20:22 +0200 | [diff] [blame] | 364 | } |
| 365 | puts (" done\n"); |
| 366 | priv->link = 1; |
| 367 | udelay (500000); /* another 500 ms (results in faster booting) */ |
| 368 | } else { |
| 369 | priv->link = 1; |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /* Parse the 88E1011's status register for speed and duplex |
| 377 | * information */ |
| 378 | uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private *priv) |
| 379 | { |
| 380 | uint speed; |
| 381 | |
Stefan Roese | 5810dc3 | 2005-09-21 18:20:22 +0200 | [diff] [blame] | 382 | mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS); |
| 383 | |
| 384 | if (!((mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE) && |
| 385 | (mii_reg & MIIM_88E1011_PHYSTAT_LINK))) { |
| 386 | int i = 0; |
| 387 | |
| 388 | puts ("Waiting for PHY realtime link"); |
| 389 | while (!((mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE) && |
| 390 | (mii_reg & MIIM_88E1011_PHYSTAT_LINK))) { |
| 391 | /* |
| 392 | * Timeout reached ? |
| 393 | */ |
| 394 | if (i > PHY_AUTONEGOTIATE_TIMEOUT) { |
| 395 | puts (" TIMEOUT !\n"); |
| 396 | priv->link = 0; |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | if ((i++ % 1000) == 0) { |
| 401 | putc ('.'); |
| 402 | } |
| 403 | udelay (1000); /* 1 ms */ |
| 404 | mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS); |
| 405 | } |
| 406 | puts (" done\n"); |
| 407 | udelay (500000); /* another 500 ms (results in faster booting) */ |
| 408 | } |
| 409 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 410 | if(mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX) |
| 411 | priv->duplexity = 1; |
| 412 | else |
| 413 | priv->duplexity = 0; |
| 414 | |
| 415 | speed = (mii_reg &MIIM_88E1011_PHYSTAT_SPEED); |
| 416 | |
| 417 | switch(speed) { |
| 418 | case MIIM_88E1011_PHYSTAT_GBIT: |
| 419 | priv->speed = 1000; |
| 420 | break; |
| 421 | case MIIM_88E1011_PHYSTAT_100: |
| 422 | priv->speed = 100; |
| 423 | break; |
| 424 | default: |
| 425 | priv->speed = 10; |
| 426 | } |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | /* Parse the cis8201's status register for speed and duplex |
| 433 | * information */ |
| 434 | uint mii_parse_cis8201(uint mii_reg, struct tsec_private *priv) |
| 435 | { |
| 436 | uint speed; |
| 437 | |
| 438 | if(mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX) |
| 439 | priv->duplexity = 1; |
| 440 | else |
| 441 | priv->duplexity = 0; |
| 442 | |
| 443 | speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED; |
| 444 | switch(speed) { |
| 445 | case MIIM_CIS8201_AUXCONSTAT_GBIT: |
| 446 | priv->speed = 1000; |
| 447 | break; |
| 448 | case MIIM_CIS8201_AUXCONSTAT_100: |
| 449 | priv->speed = 100; |
| 450 | break; |
| 451 | default: |
| 452 | priv->speed = 10; |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | |
| 460 | /* Parse the DM9161's status register for speed and duplex |
| 461 | * information */ |
| 462 | uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private *priv) |
| 463 | { |
| 464 | if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H)) |
| 465 | priv->speed = 100; |
| 466 | else |
| 467 | priv->speed = 10; |
| 468 | |
| 469 | if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F)) |
| 470 | priv->duplexity = 1; |
| 471 | else |
| 472 | priv->duplexity = 0; |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | /* Hack to write all 4 PHYs with the LED values */ |
| 479 | uint mii_cis8204_fixled(uint mii_reg, struct tsec_private *priv) |
| 480 | { |
| 481 | uint phyid; |
| 482 | volatile tsec_t *regbase = priv->phyregs; |
| 483 | int timeout=1000000; |
| 484 | |
| 485 | for(phyid=0;phyid<4;phyid++) { |
| 486 | regbase->miimadd = (phyid << 8) | mii_reg; |
| 487 | regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 488 | asm("sync"); |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 489 | |
| 490 | timeout=1000000; |
| 491 | while((regbase->miimind & MIIMIND_BUSY) && timeout--); |
| 492 | } |
| 493 | |
| 494 | return MIIM_CIS8204_SLEDCON_INIT; |
| 495 | } |
| 496 | |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 497 | uint mii_cis8204_setmode(uint mii_reg, struct tsec_private *priv) |
| 498 | { |
| 499 | if (priv->flags & TSEC_REDUCED) |
| 500 | return MIIM_CIS8204_EPHYCON_INIT | MIIM_CIS8204_EPHYCON_RGMII; |
| 501 | else |
| 502 | return MIIM_CIS8204_EPHYCON_INIT; |
| 503 | } |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 504 | |
| 505 | /* Initialized required registers to appropriate values, zeroing |
| 506 | * those we don't care about (unless zero is bad, in which case, |
| 507 | * choose a more appropriate value) */ |
| 508 | static void init_registers(volatile tsec_t *regs) |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 509 | { |
| 510 | /* Clear IEVENT */ |
| 511 | regs->ievent = IEVENT_INIT_CLEAR; |
| 512 | |
| 513 | regs->imask = IMASK_INIT_CLEAR; |
| 514 | |
| 515 | regs->hash.iaddr0 = 0; |
| 516 | regs->hash.iaddr1 = 0; |
| 517 | regs->hash.iaddr2 = 0; |
| 518 | regs->hash.iaddr3 = 0; |
| 519 | regs->hash.iaddr4 = 0; |
| 520 | regs->hash.iaddr5 = 0; |
| 521 | regs->hash.iaddr6 = 0; |
| 522 | regs->hash.iaddr7 = 0; |
| 523 | |
| 524 | regs->hash.gaddr0 = 0; |
| 525 | regs->hash.gaddr1 = 0; |
| 526 | regs->hash.gaddr2 = 0; |
| 527 | regs->hash.gaddr3 = 0; |
| 528 | regs->hash.gaddr4 = 0; |
| 529 | regs->hash.gaddr5 = 0; |
| 530 | regs->hash.gaddr6 = 0; |
| 531 | regs->hash.gaddr7 = 0; |
| 532 | |
| 533 | regs->rctrl = 0x00000000; |
| 534 | |
| 535 | /* Init RMON mib registers */ |
| 536 | memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t)); |
| 537 | |
| 538 | regs->rmon.cam1 = 0xffffffff; |
| 539 | regs->rmon.cam2 = 0xffffffff; |
| 540 | |
| 541 | regs->mrblr = MRBLR_INIT_SETTINGS; |
| 542 | |
| 543 | regs->minflr = MINFLR_INIT_SETTINGS; |
| 544 | |
| 545 | regs->attr = ATTR_INIT_SETTINGS; |
| 546 | regs->attreli = ATTRELI_INIT_SETTINGS; |
| 547 | |
| 548 | } |
| 549 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 550 | |
| 551 | /* Configure maccfg2 based on negotiated speed and duplex |
| 552 | * reported by PHY handling code */ |
| 553 | static void adjust_link(struct eth_device *dev) |
| 554 | { |
| 555 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 556 | volatile tsec_t *regs = priv->regs; |
| 557 | |
| 558 | if(priv->link) { |
| 559 | if(priv->duplexity != 0) |
| 560 | regs->maccfg2 |= MACCFG2_FULL_DUPLEX; |
| 561 | else |
| 562 | regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX); |
| 563 | |
| 564 | switch(priv->speed) { |
| 565 | case 1000: |
| 566 | regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF)) |
| 567 | | MACCFG2_GMII); |
| 568 | break; |
| 569 | case 100: |
| 570 | case 10: |
| 571 | regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF)) |
| 572 | | MACCFG2_MII); |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 573 | |
Jon Loeliger | de1d0a6 | 2005-08-01 13:20:47 -0500 | [diff] [blame] | 574 | /* If We're in reduced mode, we need |
| 575 | * to say whether we're 10 or 100 MB. |
| 576 | */ |
| 577 | if ((priv->speed == 100) |
| 578 | && (priv->flags & TSEC_REDUCED)) |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 579 | regs->ecntrl |= ECNTRL_R100; |
| 580 | else |
| 581 | regs->ecntrl &= ~(ECNTRL_R100); |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 582 | break; |
| 583 | default: |
| 584 | printf("%s: Speed was bad\n", dev->name); |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | printf("Speed: %d, %s duplex\n", priv->speed, |
| 589 | (priv->duplexity) ? "full" : "half"); |
| 590 | |
| 591 | } else { |
| 592 | printf("%s: No link.\n", dev->name); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | |
| 597 | /* Set up the buffers and their descriptors, and bring up the |
| 598 | * interface */ |
| 599 | static void startup_tsec(struct eth_device *dev) |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 600 | { |
| 601 | int i; |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 602 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 603 | volatile tsec_t *regs = priv->regs; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 604 | |
| 605 | /* Point to the buffer descriptors */ |
| 606 | regs->tbase = (unsigned int)(&rtx.txbd[txIdx]); |
| 607 | regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]); |
| 608 | |
| 609 | /* Initialize the Rx Buffer descriptors */ |
| 610 | for (i = 0; i < PKTBUFSRX; i++) { |
| 611 | rtx.rxbd[i].status = RXBD_EMPTY; |
| 612 | rtx.rxbd[i].length = 0; |
| 613 | rtx.rxbd[i].bufPtr = (uint)NetRxPackets[i]; |
| 614 | } |
| 615 | rtx.rxbd[PKTBUFSRX -1].status |= RXBD_WRAP; |
| 616 | |
| 617 | /* Initialize the TX Buffer Descriptors */ |
| 618 | for(i=0; i<TX_BUF_CNT; i++) { |
| 619 | rtx.txbd[i].status = 0; |
| 620 | rtx.txbd[i].length = 0; |
| 621 | rtx.txbd[i].bufPtr = 0; |
| 622 | } |
| 623 | rtx.txbd[TX_BUF_CNT -1].status |= TXBD_WRAP; |
| 624 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 625 | /* Start up the PHY */ |
| 626 | phy_run_commands(priv, priv->phyinfo->startup); |
| 627 | adjust_link(dev); |
| 628 | |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 629 | /* Enable Transmit and Receive */ |
| 630 | regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN); |
| 631 | |
| 632 | /* Tell the DMA it is clear to go */ |
| 633 | regs->dmactrl |= DMACTRL_INIT_SETTINGS; |
| 634 | regs->tstat = TSTAT_CLEAR_THALT; |
| 635 | regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS); |
| 636 | } |
| 637 | |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 638 | /* This returns the status bits of the device. The return value |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 639 | * is never checked, and this is what the 8260 driver did, so we |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 640 | * do the same. Presumably, this would be zero if there were no |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 641 | * errors */ |
| 642 | static int tsec_send(struct eth_device* dev, volatile void *packet, int length) |
| 643 | { |
| 644 | int i; |
| 645 | int result = 0; |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 646 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 647 | volatile tsec_t *regs = priv->regs; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 648 | |
| 649 | /* Find an empty buffer descriptor */ |
| 650 | for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) { |
| 651 | if (i >= TOUT_LOOP) { |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame] | 652 | debug ("%s: tsec: tx buffers full\n", dev->name); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 653 | return result; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | rtx.txbd[txIdx].bufPtr = (uint)packet; |
| 658 | rtx.txbd[txIdx].length = length; |
| 659 | rtx.txbd[txIdx].status |= (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT); |
| 660 | |
| 661 | /* Tell the DMA to go */ |
| 662 | regs->tstat = TSTAT_CLEAR_THALT; |
| 663 | |
| 664 | /* Wait for buffer to be transmitted */ |
| 665 | for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) { |
| 666 | if (i >= TOUT_LOOP) { |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame] | 667 | debug ("%s: tsec: tx error\n", dev->name); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 668 | return result; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | txIdx = (txIdx + 1) % TX_BUF_CNT; |
| 673 | result = rtx.txbd[txIdx].status & TXBD_STATS; |
| 674 | |
| 675 | return result; |
| 676 | } |
| 677 | |
| 678 | static int tsec_recv(struct eth_device* dev) |
| 679 | { |
| 680 | int length; |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 681 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 682 | volatile tsec_t *regs = priv->regs; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 683 | |
| 684 | while(!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) { |
| 685 | |
| 686 | length = rtx.rxbd[rxIdx].length; |
| 687 | |
| 688 | /* Send the packet up if there were no errors */ |
| 689 | if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) { |
| 690 | NetReceive(NetRxPackets[rxIdx], length - 4); |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 691 | } else { |
| 692 | printf("Got error %x\n", |
| 693 | (rtx.rxbd[rxIdx].status & RXBD_STATS)); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | rtx.rxbd[rxIdx].length = 0; |
| 697 | |
| 698 | /* Set the wrap bit if this is the last element in the list */ |
| 699 | rtx.rxbd[rxIdx].status = RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0); |
| 700 | |
| 701 | rxIdx = (rxIdx + 1) % PKTBUFSRX; |
| 702 | } |
| 703 | |
| 704 | if(regs->ievent&IEVENT_BSY) { |
| 705 | regs->ievent = IEVENT_BSY; |
| 706 | regs->rstat = RSTAT_CLEAR_RHALT; |
| 707 | } |
| 708 | |
| 709 | return -1; |
| 710 | |
| 711 | } |
| 712 | |
| 713 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 714 | /* Stop the interface */ |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 715 | static void tsec_halt(struct eth_device* dev) |
| 716 | { |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 717 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 718 | volatile tsec_t *regs = priv->regs; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 719 | |
| 720 | regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS); |
| 721 | regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS); |
| 722 | |
| 723 | while(!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC))); |
| 724 | |
| 725 | regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN); |
| 726 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 727 | /* Shut down the PHY, as needed */ |
| 728 | phy_run_commands(priv, priv->phyinfo->shutdown); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 729 | } |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 730 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 731 | |
| 732 | struct phy_info phy_info_M88E1011S = { |
| 733 | 0x01410c6, |
| 734 | "Marvell 88E1011S", |
| 735 | 4, |
| 736 | (struct phy_cmd[]) { /* config */ |
| 737 | /* Reset and configure the PHY */ |
| 738 | {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL}, |
| 739 | {0x1d, 0x1f, NULL}, |
| 740 | {0x1e, 0x200c, NULL}, |
| 741 | {0x1d, 0x5, NULL}, |
| 742 | {0x1e, 0x0, NULL}, |
| 743 | {0x1e, 0x100, NULL}, |
| 744 | {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL}, |
| 745 | {MIIM_ANAR, MIIM_ANAR_INIT, NULL}, |
| 746 | {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL}, |
| 747 | {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init}, |
| 748 | {miim_end,} |
| 749 | }, |
| 750 | (struct phy_cmd[]) { /* startup */ |
| 751 | /* Status is read once to clear old link state */ |
| 752 | {MIIM_STATUS, miim_read, NULL}, |
| 753 | /* Auto-negotiate */ |
| 754 | {MIIM_STATUS, miim_read, &mii_parse_sr}, |
| 755 | /* Read the status */ |
| 756 | {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr}, |
| 757 | {miim_end,} |
| 758 | }, |
| 759 | (struct phy_cmd[]) { /* shutdown */ |
| 760 | {miim_end,} |
| 761 | }, |
| 762 | }; |
| 763 | |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 764 | struct phy_info phy_info_M88E1111S = { |
| 765 | 0x01410cc, |
| 766 | "Marvell 88E1111S", |
| 767 | 4, |
| 768 | (struct phy_cmd[]) { /* config */ |
| 769 | /* Reset and configure the PHY */ |
| 770 | {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL}, |
| 771 | {0x1d, 0x1f, NULL}, |
| 772 | {0x1e, 0x200c, NULL}, |
| 773 | {0x1d, 0x5, NULL}, |
| 774 | {0x1e, 0x0, NULL}, |
| 775 | {0x1e, 0x100, NULL}, |
| 776 | {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL}, |
| 777 | {MIIM_ANAR, MIIM_ANAR_INIT, NULL}, |
| 778 | {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL}, |
| 779 | {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init}, |
| 780 | {miim_end,} |
| 781 | }, |
| 782 | (struct phy_cmd[]) { /* startup */ |
| 783 | /* Status is read once to clear old link state */ |
| 784 | {MIIM_STATUS, miim_read, NULL}, |
| 785 | /* Auto-negotiate */ |
| 786 | {MIIM_STATUS, miim_read, &mii_parse_sr}, |
| 787 | /* Read the status */ |
| 788 | {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr}, |
| 789 | {miim_end,} |
| 790 | }, |
| 791 | (struct phy_cmd[]) { /* shutdown */ |
| 792 | {miim_end,} |
| 793 | }, |
| 794 | }; |
| 795 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 796 | struct phy_info phy_info_cis8204 = { |
| 797 | 0x3f11, |
| 798 | "Cicada Cis8204", |
| 799 | 6, |
| 800 | (struct phy_cmd[]) { /* config */ |
| 801 | /* Override PHY config settings */ |
| 802 | {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL}, |
| 803 | /* Configure some basic stuff */ |
| 804 | {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init}, |
| 805 | {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT, &mii_cis8204_fixled}, |
Jon Loeliger | d9b94f2 | 2005-07-25 14:05:07 -0500 | [diff] [blame] | 806 | {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT, &mii_cis8204_setmode}, |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 807 | {miim_end,} |
| 808 | }, |
| 809 | (struct phy_cmd[]) { /* startup */ |
| 810 | /* Read the Status (2x to make sure link is right) */ |
| 811 | {MIIM_STATUS, miim_read, NULL}, |
| 812 | /* Auto-negotiate */ |
| 813 | {MIIM_STATUS, miim_read, &mii_parse_sr}, |
| 814 | /* Read the status */ |
| 815 | {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201}, |
| 816 | {miim_end,} |
| 817 | }, |
| 818 | (struct phy_cmd[]) { /* shutdown */ |
| 819 | {miim_end,} |
| 820 | }, |
| 821 | }; |
| 822 | |
| 823 | /* Cicada 8201 */ |
| 824 | struct phy_info phy_info_cis8201 = { |
| 825 | 0xfc41, |
| 826 | "CIS8201", |
| 827 | 4, |
| 828 | (struct phy_cmd[]) { /* config */ |
| 829 | /* Override PHY config settings */ |
| 830 | {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL}, |
| 831 | /* Set up the interface mode */ |
| 832 | {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT, NULL}, |
| 833 | /* Configure some basic stuff */ |
| 834 | {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init}, |
| 835 | {miim_end,} |
| 836 | }, |
| 837 | (struct phy_cmd[]) { /* startup */ |
| 838 | /* Read the Status (2x to make sure link is right) */ |
| 839 | {MIIM_STATUS, miim_read, NULL}, |
| 840 | /* Auto-negotiate */ |
| 841 | {MIIM_STATUS, miim_read, &mii_parse_sr}, |
| 842 | /* Read the status */ |
| 843 | {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201}, |
| 844 | {miim_end,} |
| 845 | }, |
| 846 | (struct phy_cmd[]) { /* shutdown */ |
| 847 | {miim_end,} |
| 848 | }, |
| 849 | }; |
| 850 | |
| 851 | |
| 852 | struct phy_info phy_info_dm9161 = { |
| 853 | 0x0181b88, |
| 854 | "Davicom DM9161E", |
| 855 | 4, |
| 856 | (struct phy_cmd[]) { /* config */ |
| 857 | {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL}, |
| 858 | /* Do not bypass the scrambler/descrambler */ |
| 859 | {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL}, |
| 860 | /* Clear 10BTCSR to default */ |
| 861 | {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT, NULL}, |
| 862 | /* Configure some basic stuff */ |
| 863 | {MIIM_CONTROL, MIIM_CR_INIT, NULL}, |
| 864 | /* Restart Auto Negotiation */ |
| 865 | {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL}, |
| 866 | {miim_end,} |
| 867 | }, |
| 868 | (struct phy_cmd[]) { /* startup */ |
| 869 | /* Status is read once to clear old link state */ |
| 870 | {MIIM_STATUS, miim_read, NULL}, |
| 871 | /* Auto-negotiate */ |
| 872 | {MIIM_STATUS, miim_read, &mii_parse_sr}, |
| 873 | /* Read the status */ |
| 874 | {MIIM_DM9161_SCSR, miim_read, &mii_parse_dm9161_scsr}, |
| 875 | {miim_end,} |
| 876 | }, |
| 877 | (struct phy_cmd[]) { /* shutdown */ |
| 878 | {miim_end,} |
| 879 | }, |
| 880 | }; |
| 881 | |
wdenk | 3dd7f0f | 2005-04-04 23:43:44 +0000 | [diff] [blame] | 882 | uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv) |
| 883 | { |
wdenk | 3c2b3d4 | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 884 | unsigned int speed; |
| 885 | if (priv->link) { |
| 886 | speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK; |
wdenk | 3dd7f0f | 2005-04-04 23:43:44 +0000 | [diff] [blame] | 887 | |
wdenk | 3c2b3d4 | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 888 | switch (speed) { |
| 889 | case MIIM_LXT971_SR2_10HDX: |
| 890 | priv->speed = 10; |
| 891 | priv->duplexity = 0; |
| 892 | break; |
| 893 | case MIIM_LXT971_SR2_10FDX: |
| 894 | priv->speed = 10; |
| 895 | priv->duplexity = 1; |
| 896 | break; |
| 897 | case MIIM_LXT971_SR2_100HDX: |
| 898 | priv->speed = 100; |
| 899 | priv->duplexity = 0; |
| 900 | default: |
| 901 | priv->speed = 100; |
| 902 | priv->duplexity = 1; |
| 903 | break; |
| 904 | } |
| 905 | } else { |
| 906 | priv->speed = 0; |
| 907 | priv->duplexity = 0; |
| 908 | } |
wdenk | 3dd7f0f | 2005-04-04 23:43:44 +0000 | [diff] [blame] | 909 | |
wdenk | 3c2b3d4 | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 910 | return 0; |
wdenk | 3dd7f0f | 2005-04-04 23:43:44 +0000 | [diff] [blame] | 911 | } |
| 912 | |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 913 | static struct phy_info phy_info_lxt971 = { |
| 914 | 0x0001378e, |
| 915 | "LXT971", |
| 916 | 4, |
| 917 | (struct phy_cmd []) { /* config */ |
wdenk | 3dd7f0f | 2005-04-04 23:43:44 +0000 | [diff] [blame] | 918 | { MIIM_CR, MIIM_CR_INIT, mii_cr_init }, /* autonegotiate */ |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 919 | { miim_end, } |
| 920 | }, |
| 921 | (struct phy_cmd []) { /* startup - enable interrupts */ |
| 922 | /* { 0x12, 0x00f2, NULL }, */ |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 923 | { MIIM_STATUS, miim_read, NULL }, |
wdenk | 3dd7f0f | 2005-04-04 23:43:44 +0000 | [diff] [blame] | 924 | { MIIM_STATUS, miim_read, &mii_parse_sr }, |
| 925 | { MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2 }, |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 926 | { miim_end, } |
| 927 | }, |
| 928 | (struct phy_cmd []) { /* shutdown - disable interrupts */ |
| 929 | { miim_end, } |
| 930 | }, |
| 931 | }; |
| 932 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 933 | struct phy_info *phy_info[] = { |
| 934 | #if 0 |
| 935 | &phy_info_cis8201, |
| 936 | #endif |
| 937 | &phy_info_cis8204, |
| 938 | &phy_info_M88E1011S, |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 939 | &phy_info_M88E1111S, |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 940 | &phy_info_dm9161, |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 941 | &phy_info_lxt971, |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 942 | NULL |
| 943 | }; |
| 944 | |
| 945 | |
| 946 | /* Grab the identifier of the device's PHY, and search through |
wdenk | 9d46ea4 | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 947 | * all of the known PHYs to see if one matches. If so, return |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 948 | * it, if not, return NULL */ |
| 949 | struct phy_info * get_phy_info(struct eth_device *dev) |
| 950 | { |
| 951 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 952 | uint phy_reg, phy_ID; |
| 953 | int i; |
| 954 | struct phy_info *theInfo = NULL; |
| 955 | |
| 956 | /* Grab the bits from PHYIR1, and put them in the upper half */ |
| 957 | phy_reg = read_phy_reg(priv, MIIM_PHYIR1); |
| 958 | phy_ID = (phy_reg & 0xffff) << 16; |
| 959 | |
| 960 | /* Grab the bits from PHYIR2, and put them in the lower half */ |
| 961 | phy_reg = read_phy_reg(priv, MIIM_PHYIR2); |
| 962 | phy_ID |= (phy_reg & 0xffff); |
| 963 | |
| 964 | /* loop through all the known PHY types, and find one that */ |
| 965 | /* matches the ID we read from the PHY. */ |
| 966 | for(i=0; phy_info[i]; i++) { |
| 967 | if(phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) |
| 968 | theInfo = phy_info[i]; |
| 969 | } |
| 970 | |
| 971 | if(theInfo == NULL) |
| 972 | { |
| 973 | printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID); |
| 974 | return NULL; |
| 975 | } else { |
Stefan Roese | 5810dc3 | 2005-09-21 18:20:22 +0200 | [diff] [blame] | 976 | debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID); |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | return theInfo; |
| 980 | } |
| 981 | |
| 982 | |
| 983 | /* Execute the given series of commands on the given device's |
| 984 | * PHY, running functions as necessary*/ |
| 985 | void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd) |
| 986 | { |
| 987 | int i; |
| 988 | uint result; |
| 989 | volatile tsec_t *phyregs = priv->phyregs; |
| 990 | |
| 991 | phyregs->miimcfg = MIIMCFG_RESET; |
| 992 | |
| 993 | phyregs->miimcfg = MIIMCFG_INIT_VALUE; |
| 994 | |
| 995 | while(phyregs->miimind & MIIMIND_BUSY); |
| 996 | |
| 997 | for(i=0;cmd->mii_reg != miim_end;i++) { |
| 998 | if(cmd->mii_data == miim_read) { |
| 999 | result = read_phy_reg(priv, cmd->mii_reg); |
| 1000 | |
| 1001 | if(cmd->funct != NULL) |
| 1002 | (*(cmd->funct))(result, priv); |
| 1003 | |
| 1004 | } else { |
| 1005 | if(cmd->funct != NULL) |
| 1006 | result = (*(cmd->funct))(cmd->mii_reg, priv); |
| 1007 | else |
| 1008 | result = cmd->mii_data; |
| 1009 | |
| 1010 | write_phy_reg(priv, cmd->mii_reg, result); |
| 1011 | |
| 1012 | } |
| 1013 | cmd++; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | /* Relocate the function pointers in the phy cmd lists */ |
| 1019 | static void relocate_cmds(void) |
| 1020 | { |
| 1021 | struct phy_cmd **cmdlistptr; |
| 1022 | struct phy_cmd *cmd; |
| 1023 | int i,j,k; |
| 1024 | DECLARE_GLOBAL_DATA_PTR; |
| 1025 | |
| 1026 | for(i=0; phy_info[i]; i++) { |
| 1027 | /* First thing's first: relocate the pointers to the |
| 1028 | * PHY command structures (the structs were done) */ |
| 1029 | phy_info[i] = (struct phy_info *) ((uint)phy_info[i] |
| 1030 | + gd->reloc_off); |
| 1031 | phy_info[i]->name += gd->reloc_off; |
| 1032 | phy_info[i]->config = |
| 1033 | (struct phy_cmd *)((uint)phy_info[i]->config |
| 1034 | + gd->reloc_off); |
| 1035 | phy_info[i]->startup = |
| 1036 | (struct phy_cmd *)((uint)phy_info[i]->startup |
| 1037 | + gd->reloc_off); |
| 1038 | phy_info[i]->shutdown = |
| 1039 | (struct phy_cmd *)((uint)phy_info[i]->shutdown |
| 1040 | + gd->reloc_off); |
| 1041 | |
| 1042 | cmdlistptr = &phy_info[i]->config; |
| 1043 | j=0; |
| 1044 | for(;cmdlistptr <= &phy_info[i]->shutdown;cmdlistptr++) { |
| 1045 | k=0; |
| 1046 | for(cmd=*cmdlistptr;cmd->mii_reg != miim_end;cmd++) { |
| 1047 | /* Only relocate non-NULL pointers */ |
| 1048 | if(cmd->funct) |
| 1049 | cmd->funct += gd->reloc_off; |
| 1050 | |
| 1051 | k++; |
| 1052 | } |
| 1053 | j++; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | relocated = 1; |
| 1058 | } |
| 1059 | |
| 1060 | |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 1061 | #ifndef CONFIG_BITBANGMII |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 1062 | |
| 1063 | struct tsec_private * get_priv_for_phy(unsigned char phyaddr) |
| 1064 | { |
| 1065 | int i; |
| 1066 | |
| 1067 | for(i=0;i<MAXCONTROLLERS;i++) { |
| 1068 | if(privlist[i]->phyaddr == phyaddr) |
| 1069 | return privlist[i]; |
| 1070 | } |
| 1071 | |
| 1072 | return NULL; |
| 1073 | } |
| 1074 | |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 1075 | /* |
| 1076 | * Read a MII PHY register. |
| 1077 | * |
| 1078 | * Returns: |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 1079 | * 0 on success |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 1080 | */ |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 1081 | int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value) |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 1082 | { |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 1083 | unsigned short ret; |
| 1084 | struct tsec_private *priv = get_priv_for_phy(addr); |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 1085 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 1086 | if(NULL == priv) { |
| 1087 | printf("Can't read PHY at address %d\n", addr); |
| 1088 | return -1; |
| 1089 | } |
| 1090 | |
| 1091 | ret = (unsigned short)read_phy_reg(priv, reg); |
| 1092 | *value = ret; |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 1093 | |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * Write a MII PHY register. |
| 1099 | * |
| 1100 | * Returns: |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 1101 | * 0 on success |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 1102 | */ |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 1103 | int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value) |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 1104 | { |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 1105 | struct tsec_private *priv = get_priv_for_phy(addr); |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 1106 | |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 1107 | if(NULL == priv) { |
| 1108 | printf("Can't write PHY at address %d\n", addr); |
| 1109 | return -1; |
| 1110 | } |
| 1111 | |
| 1112 | write_phy_reg(priv, reg, value); |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 1113 | |
| 1114 | return 0; |
| 1115 | } |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 1116 | |
wdenk | 7abf0c5 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 1117 | #endif /* CONFIG_BITBANGMII */ |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 1118 | |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 1119 | #endif /* CONFIG_TSEC_ENET */ |