Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 1 | /* |
| 2 | * sh_eth.c - Driver for Renesas SH7763's ethernet controler. |
| 3 | * |
| 4 | * Copyright (C) 2008 Renesas Solutions Corp. |
| 5 | * Copyright (c) 2008 Nobuhiro Iwamatsu |
| 6 | * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <config.h> |
| 24 | #include <common.h> |
| 25 | #include <malloc.h> |
| 26 | #include <net.h> |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 27 | #include <netdev.h> |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 28 | #include <asm/errno.h> |
| 29 | #include <asm/io.h> |
| 30 | |
| 31 | #include "sh_eth.h" |
| 32 | |
| 33 | #ifndef CONFIG_SH_ETHER_USE_PORT |
| 34 | # error "Please define CONFIG_SH_ETHER_USE_PORT" |
| 35 | #endif |
| 36 | #ifndef CONFIG_SH_ETHER_PHY_ADDR |
| 37 | # error "Please define CONFIG_SH_ETHER_PHY_ADDR" |
| 38 | #endif |
| 39 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 40 | #define SH_ETH_PHY_DELAY 50000 |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Bits are written to the PHY serially using the |
| 44 | * PIR register, just like a bit banger. |
| 45 | */ |
| 46 | static void sh_eth_mii_write_phy_bits(int port, u32 val, int len) |
| 47 | { |
| 48 | int i; |
| 49 | u32 pir; |
| 50 | |
| 51 | /* Bit positions is 1 less than the number of bits */ |
| 52 | for (i = len - 1; i >= 0; i--) { |
| 53 | /* Write direction, bit to write, clock is low */ |
| 54 | pir = 2 | ((val & 1 << i) ? 1 << 2 : 0); |
| 55 | outl(pir, PIR(port)); |
| 56 | udelay(1); |
| 57 | /* Write direction, bit to write, clock is high */ |
| 58 | pir = 3 | ((val & 1 << i) ? 1 << 2 : 0); |
| 59 | outl(pir, PIR(port)); |
| 60 | udelay(1); |
| 61 | /* Write direction, bit to write, clock is low */ |
| 62 | pir = 2 | ((val & 1 << i) ? 1 << 2 : 0); |
| 63 | outl(pir, PIR(port)); |
| 64 | udelay(1); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static void sh_eth_mii_bus_release(int port) |
| 69 | { |
| 70 | /* Read direction, clock is low */ |
| 71 | outl(0, PIR(port)); |
| 72 | udelay(1); |
| 73 | /* Read direction, clock is high */ |
| 74 | outl(1, PIR(port)); |
| 75 | udelay(1); |
| 76 | /* Read direction, clock is low */ |
| 77 | outl(0, PIR(port)); |
| 78 | udelay(1); |
| 79 | } |
| 80 | |
| 81 | static void sh_eth_mii_ind_bus_release(int port) |
| 82 | { |
| 83 | /* Read direction, clock is low */ |
| 84 | outl(0, PIR(port)); |
| 85 | udelay(1); |
| 86 | } |
| 87 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 88 | static void sh_eth_mii_read_phy_bits(int port, u32 *val, int len) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 89 | { |
| 90 | int i; |
| 91 | u32 pir; |
| 92 | |
| 93 | *val = 0; |
| 94 | for (i = len - 1; i >= 0; i--) { |
| 95 | /* Read direction, clock is high */ |
| 96 | outl(1, PIR(port)); |
| 97 | udelay(1); |
| 98 | /* Read bit */ |
| 99 | pir = inl(PIR(port)); |
| 100 | *val |= (pir & 8) ? 1 << i : 0; |
| 101 | /* Read direction, clock is low */ |
| 102 | outl(0, PIR(port)); |
| 103 | udelay(1); |
| 104 | } |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | #define PHY_INIT 0xFFFFFFFF |
| 108 | #define PHY_READ 0x02 |
| 109 | #define PHY_WRITE 0x01 |
| 110 | /* |
| 111 | * To read a phy register, mii managements frames are sent to the phy. |
| 112 | * The frames look like this: |
| 113 | * pre (32 bits): 0xffff ffff |
| 114 | * st (2 bits): 01 |
| 115 | * op (2bits): 10: read 01: write |
| 116 | * phyad (5 bits): xxxxx |
| 117 | * regad (5 bits): xxxxx |
| 118 | * ta (Bus release): |
| 119 | * data (16 bits): read data |
| 120 | */ |
| 121 | static u32 sh_eth_mii_read_phy_reg(int port, u8 phy_addr, int reg) |
| 122 | { |
| 123 | u32 val; |
| 124 | |
| 125 | /* Sent mii management frame */ |
| 126 | /* pre */ |
| 127 | sh_eth_mii_write_phy_bits(port, PHY_INIT, 32); |
| 128 | /* st (start of frame) */ |
| 129 | sh_eth_mii_write_phy_bits(port, 0x1, 2); |
| 130 | /* op (code) */ |
| 131 | sh_eth_mii_write_phy_bits(port, PHY_READ, 2); |
| 132 | /* phy address */ |
| 133 | sh_eth_mii_write_phy_bits(port, phy_addr, 5); |
| 134 | /* Register to read */ |
| 135 | sh_eth_mii_write_phy_bits(port, reg, 5); |
| 136 | |
| 137 | /* Bus release */ |
| 138 | sh_eth_mii_bus_release(port); |
| 139 | |
| 140 | /* Read register */ |
| 141 | sh_eth_mii_read_phy_bits(port, &val, 16); |
| 142 | |
| 143 | return val; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * To write a phy register, mii managements frames are sent to the phy. |
| 148 | * The frames look like this: |
| 149 | * pre (32 bits): 0xffff ffff |
| 150 | * st (2 bits): 01 |
| 151 | * op (2bits): 10: read 01: write |
| 152 | * phyad (5 bits): xxxxx |
| 153 | * regad (5 bits): xxxxx |
| 154 | * ta (2 bits): 10 |
| 155 | * data (16 bits): write data |
| 156 | * idle (Independent bus release) |
| 157 | */ |
| 158 | static void sh_eth_mii_write_phy_reg(int port, u8 phy_addr, int reg, u16 val) |
| 159 | { |
| 160 | /* Sent mii management frame */ |
| 161 | /* pre */ |
| 162 | sh_eth_mii_write_phy_bits(port, PHY_INIT, 32); |
| 163 | /* st (start of frame) */ |
| 164 | sh_eth_mii_write_phy_bits(port, 0x1, 2); |
| 165 | /* op (code) */ |
| 166 | sh_eth_mii_write_phy_bits(port, PHY_WRITE, 2); |
| 167 | /* phy address */ |
| 168 | sh_eth_mii_write_phy_bits(port, phy_addr, 5); |
| 169 | /* Register to read */ |
| 170 | sh_eth_mii_write_phy_bits(port, reg, 5); |
| 171 | /* ta */ |
| 172 | sh_eth_mii_write_phy_bits(port, PHY_READ, 2); |
| 173 | /* Write register data */ |
| 174 | sh_eth_mii_write_phy_bits(port, val, 16); |
| 175 | |
| 176 | /* Independent bus release */ |
| 177 | sh_eth_mii_ind_bus_release(port); |
| 178 | } |
| 179 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 180 | int sh_eth_send(struct eth_device *dev, volatile void *packet, int len) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 181 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 182 | struct sh_eth_dev *eth = dev->priv; |
| 183 | int port = eth->port, ret = 0, timeout; |
| 184 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 185 | |
| 186 | if (!packet || len > 0xffff) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 187 | printf(SHETHER_NAME ": %s: Invalid argument\n", __func__); |
| 188 | ret = -EINVAL; |
| 189 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* packet must be a 4 byte boundary */ |
| 193 | if ((int)packet & (4 - 1)) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 194 | printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__); |
| 195 | ret = -EFAULT; |
| 196 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* Update tx descriptor */ |
| 200 | port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet); |
| 201 | port_info->tx_desc_cur->td1 = len << 16; |
| 202 | /* Must preserve the end of descriptor list indication */ |
| 203 | if (port_info->tx_desc_cur->td0 & TD_TDLE) |
| 204 | port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE; |
| 205 | else |
| 206 | port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP; |
| 207 | |
| 208 | /* Restart the transmitter if disabled */ |
| 209 | if (!(inl(EDTRR(port)) & EDTRR_TRNS)) |
| 210 | outl(EDTRR_TRNS, EDTRR(port)); |
| 211 | |
| 212 | /* Wait until packet is transmitted */ |
| 213 | timeout = 1000; |
| 214 | while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--) |
| 215 | udelay(100); |
| 216 | |
| 217 | if (timeout < 0) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 218 | printf(SHETHER_NAME ": transmit timeout\n"); |
| 219 | ret = -ETIMEDOUT; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 220 | goto err; |
| 221 | } |
| 222 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 223 | port_info->tx_desc_cur++; |
| 224 | if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC) |
| 225 | port_info->tx_desc_cur = port_info->tx_desc_base; |
| 226 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 227 | return ret; |
| 228 | err: |
| 229 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 230 | } |
| 231 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 232 | int sh_eth_recv(struct eth_device *dev) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 233 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 234 | struct sh_eth_dev *eth = dev->priv; |
| 235 | int port = eth->port, len = 0; |
| 236 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 237 | volatile u8 *packet; |
| 238 | |
| 239 | /* Check if the rx descriptor is ready */ |
| 240 | if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) { |
| 241 | /* Check for errors */ |
| 242 | if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) { |
| 243 | len = port_info->rx_desc_cur->rd1 & 0xffff; |
| 244 | packet = (volatile u8 *) |
| 245 | ADDR_TO_P2(port_info->rx_desc_cur->rd2); |
| 246 | NetReceive(packet, len); |
| 247 | } |
| 248 | |
| 249 | /* Make current descriptor available again */ |
| 250 | if (port_info->rx_desc_cur->rd0 & RD_RDLE) |
| 251 | port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE; |
| 252 | else |
| 253 | port_info->rx_desc_cur->rd0 = RD_RACT; |
| 254 | |
| 255 | /* Point to the next descriptor */ |
| 256 | port_info->rx_desc_cur++; |
| 257 | if (port_info->rx_desc_cur >= |
| 258 | port_info->rx_desc_base + NUM_RX_DESC) |
| 259 | port_info->rx_desc_cur = port_info->rx_desc_base; |
| 260 | } |
| 261 | |
| 262 | /* Restart the receiver if disabled */ |
| 263 | if (!(inl(EDRRR(port)) & EDRRR_R)) |
| 264 | outl(EDRRR_R, EDRRR(port)); |
| 265 | |
| 266 | return len; |
| 267 | } |
| 268 | |
| 269 | #define EDMR_INIT_CNT 1000 |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 270 | static int sh_eth_reset(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 271 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 272 | int port = eth->port; |
| 273 | int ret = 0, i; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 274 | |
| 275 | /* Start e-dmac transmitter and receiver */ |
| 276 | outl(EDSR_ENALL, EDSR(port)); |
| 277 | |
| 278 | /* Perform a software reset and wait for it to complete */ |
| 279 | outl(EDMR_SRST, EDMR(port)); |
| 280 | for (i = 0; i < EDMR_INIT_CNT; i++) { |
| 281 | if (!(inl(EDMR(port)) & EDMR_SRST)) |
| 282 | break; |
| 283 | udelay(1000); |
| 284 | } |
| 285 | |
| 286 | if (i == EDMR_INIT_CNT) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 287 | printf(SHETHER_NAME ": Software reset timeout\n"); |
| 288 | ret = -EIO; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 289 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 290 | |
| 291 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 292 | } |
| 293 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 294 | static int sh_eth_tx_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 295 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 296 | int port = eth->port, i, ret = 0; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 297 | u32 tmp_addr; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 298 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 299 | struct tx_desc_s *cur_tx_desc; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 300 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 301 | /* |
| 302 | * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned |
| 303 | */ |
| 304 | port_info->tx_desc_malloc = malloc(NUM_TX_DESC * |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 305 | sizeof(struct tx_desc_s) + |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 306 | TX_DESC_SIZE - 1); |
| 307 | if (!port_info->tx_desc_malloc) { |
| 308 | printf(SHETHER_NAME ": malloc failed\n"); |
| 309 | ret = -ENOMEM; |
| 310 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 311 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 312 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 313 | tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) & |
| 314 | ~(TX_DESC_SIZE - 1)); |
| 315 | /* Make sure we use a P2 address (non-cacheable) */ |
| 316 | port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 317 | port_info->tx_desc_cur = port_info->tx_desc_base; |
| 318 | |
| 319 | /* Initialize all descriptors */ |
| 320 | for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC; |
| 321 | cur_tx_desc++, i++) { |
| 322 | cur_tx_desc->td0 = 0x00; |
| 323 | cur_tx_desc->td1 = 0x00; |
| 324 | cur_tx_desc->td2 = 0x00; |
| 325 | } |
| 326 | |
| 327 | /* Mark the end of the descriptors */ |
| 328 | cur_tx_desc--; |
| 329 | cur_tx_desc->td0 |= TD_TDLE; |
| 330 | |
| 331 | /* Point the controller to the tx descriptor list. Must use physical |
| 332 | addresses */ |
| 333 | outl(ADDR_TO_PHY(port_info->tx_desc_base), TDLAR(port)); |
| 334 | outl(ADDR_TO_PHY(port_info->tx_desc_base), TDFAR(port)); |
| 335 | outl(ADDR_TO_PHY(cur_tx_desc), TDFXR(port)); |
| 336 | outl(0x01, TDFFR(port));/* Last discriptor bit */ |
| 337 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 338 | err: |
| 339 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 340 | } |
| 341 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 342 | static int sh_eth_rx_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 343 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 344 | int port = eth->port, i , ret = 0; |
| 345 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 346 | struct rx_desc_s *cur_rx_desc; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 347 | u32 tmp_addr; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 348 | u8 *rx_buf; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 349 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 350 | /* |
| 351 | * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned |
| 352 | */ |
| 353 | port_info->rx_desc_malloc = malloc(NUM_RX_DESC * |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 354 | sizeof(struct rx_desc_s) + |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 355 | RX_DESC_SIZE - 1); |
| 356 | if (!port_info->rx_desc_malloc) { |
| 357 | printf(SHETHER_NAME ": malloc failed\n"); |
| 358 | ret = -ENOMEM; |
| 359 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 360 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 361 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 362 | tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) & |
| 363 | ~(RX_DESC_SIZE - 1)); |
| 364 | /* Make sure we use a P2 address (non-cacheable) */ |
| 365 | port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr); |
| 366 | |
| 367 | port_info->rx_desc_cur = port_info->rx_desc_base; |
| 368 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 369 | /* |
| 370 | * Allocate rx data buffers. They must be 32 bytes aligned and in |
| 371 | * P2 area |
| 372 | */ |
| 373 | port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31); |
| 374 | if (!port_info->rx_buf_malloc) { |
| 375 | printf(SHETHER_NAME ": malloc failed\n"); |
| 376 | ret = -ENOMEM; |
| 377 | goto err_buf_malloc; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 378 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 379 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 380 | tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) & |
| 381 | ~(32 - 1)); |
| 382 | port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr); |
| 383 | |
| 384 | /* Initialize all descriptors */ |
| 385 | for (cur_rx_desc = port_info->rx_desc_base, |
| 386 | rx_buf = port_info->rx_buf_base, i = 0; |
| 387 | i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) { |
| 388 | cur_rx_desc->rd0 = RD_RACT; |
| 389 | cur_rx_desc->rd1 = MAX_BUF_SIZE << 16; |
| 390 | cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf); |
| 391 | } |
| 392 | |
| 393 | /* Mark the end of the descriptors */ |
| 394 | cur_rx_desc--; |
| 395 | cur_rx_desc->rd0 |= RD_RDLE; |
| 396 | |
| 397 | /* Point the controller to the rx descriptor list */ |
| 398 | outl(ADDR_TO_PHY(port_info->rx_desc_base), RDLAR(port)); |
| 399 | outl(ADDR_TO_PHY(port_info->rx_desc_base), RDFAR(port)); |
| 400 | outl(ADDR_TO_PHY(cur_rx_desc), RDFXR(port)); |
| 401 | outl(RDFFR_RDLF, RDFFR(port)); |
| 402 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 403 | return ret; |
| 404 | |
| 405 | err_buf_malloc: |
| 406 | free(port_info->rx_desc_malloc); |
| 407 | port_info->rx_desc_malloc = NULL; |
| 408 | |
| 409 | err: |
| 410 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 411 | } |
| 412 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 413 | static void sh_eth_tx_desc_free(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 414 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 415 | int port = eth->port; |
| 416 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 417 | |
| 418 | if (port_info->tx_desc_malloc) { |
| 419 | free(port_info->tx_desc_malloc); |
| 420 | port_info->tx_desc_malloc = NULL; |
| 421 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static void sh_eth_rx_desc_free(struct sh_eth_dev *eth) |
| 425 | { |
| 426 | int port = eth->port; |
| 427 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 428 | |
| 429 | if (port_info->rx_desc_malloc) { |
| 430 | free(port_info->rx_desc_malloc); |
| 431 | port_info->rx_desc_malloc = NULL; |
| 432 | } |
| 433 | |
| 434 | if (port_info->rx_buf_malloc) { |
| 435 | free(port_info->rx_buf_malloc); |
| 436 | port_info->rx_buf_malloc = NULL; |
| 437 | } |
| 438 | } |
| 439 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 440 | static int sh_eth_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 441 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 442 | int ret = 0; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 443 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 444 | ret = sh_eth_tx_desc_init(eth); |
| 445 | if (ret) |
| 446 | goto err_tx_init; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 447 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 448 | ret = sh_eth_rx_desc_init(eth); |
| 449 | if (ret) |
| 450 | goto err_rx_init; |
| 451 | |
| 452 | return ret; |
| 453 | err_rx_init: |
| 454 | sh_eth_tx_desc_free(eth); |
| 455 | |
| 456 | err_tx_init: |
| 457 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 458 | } |
| 459 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 460 | static int sh_eth_phy_config(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 461 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 462 | int port = eth->port, timeout, ret = 0; |
| 463 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 464 | u32 val; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 465 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 466 | /* Reset phy */ |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 467 | sh_eth_mii_write_phy_reg |
| 468 | (port, port_info->phy_addr, PHY_CTRL, PHY_C_RESET); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 469 | timeout = 10; |
| 470 | while (timeout--) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 471 | val = sh_eth_mii_read_phy_reg(port, |
| 472 | port_info->phy_addr, PHY_CTRL); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 473 | if (!(val & PHY_C_RESET)) |
| 474 | break; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 475 | udelay(SH_ETH_PHY_DELAY); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 476 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 477 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 478 | if (timeout < 0) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 479 | printf(SHETHER_NAME ": phy reset timeout\n"); |
| 480 | ret = -EIO; |
| 481 | goto err_tout; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /* Advertise 100/10 baseT full/half duplex */ |
| 485 | sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_ANA, |
| 486 | (PHY_A_FDX|PHY_A_HDX|PHY_A_10FDX|PHY_A_10HDX|PHY_A_EXT)); |
| 487 | /* Autonegotiation, normal operation, full duplex, enable tx */ |
| 488 | sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_CTRL, |
| 489 | (PHY_C_ANEGEN|PHY_C_RANEG)); |
| 490 | /* Wait for autonegotiation to complete */ |
| 491 | timeout = 100; |
| 492 | while (timeout--) { |
| 493 | val = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1); |
| 494 | if (val & PHY_S_ANEGC) |
| 495 | break; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 496 | |
| 497 | udelay(SH_ETH_PHY_DELAY); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 498 | } |
| 499 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 500 | if (timeout < 0) { |
| 501 | printf(SHETHER_NAME ": phy auto-negotiation failed\n"); |
| 502 | ret = -ETIMEDOUT; |
| 503 | goto err_tout; |
| 504 | } |
| 505 | |
| 506 | return ret; |
| 507 | |
| 508 | err_tout: |
| 509 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 510 | } |
| 511 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 512 | static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 513 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 514 | int port = eth->port, ret = 0; |
| 515 | u32 val, phy_status; |
| 516 | struct sh_eth_info *port_info = ð->port_info[port]; |
Mike Frysinger | c527ce9 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 517 | struct eth_device *dev = port_info->dev; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 518 | |
| 519 | /* Configure e-dmac registers */ |
| 520 | outl((inl(EDMR(port)) & ~EMDR_DESC_R) | EDMR_EL, EDMR(port)); |
| 521 | outl(0, EESIPR(port)); |
| 522 | outl(0, TRSCER(port)); |
| 523 | outl(0, TFTR(port)); |
| 524 | outl((FIFO_SIZE_T | FIFO_SIZE_R), FDR(port)); |
| 525 | outl(RMCR_RST, RMCR(port)); |
| 526 | outl(0, RPADIR(port)); |
| 527 | outl((FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR(port)); |
| 528 | |
| 529 | /* Configure e-mac registers */ |
| 530 | outl(0, ECSIPR(port)); |
| 531 | |
| 532 | /* Set Mac address */ |
Mike Frysinger | c527ce9 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 533 | val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 | |
| 534 | dev->enetaddr[2] << 8 | dev->enetaddr[3]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 535 | outl(val, MAHR(port)); |
| 536 | |
Mike Frysinger | c527ce9 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 537 | val = dev->enetaddr[4] << 8 | dev->enetaddr[5]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 538 | outl(val, MALR(port)); |
| 539 | |
| 540 | outl(RFLR_RFL_MIN, RFLR(port)); |
| 541 | outl(0, PIPR(port)); |
| 542 | outl(APR_AP, APR(port)); |
| 543 | outl(MPR_MP, MPR(port)); |
| 544 | outl(TPAUSER_TPAUSE, TPAUSER(port)); |
| 545 | |
| 546 | /* Configure phy */ |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 547 | ret = sh_eth_phy_config(eth); |
| 548 | if (ret) { |
Nobuhiro Iwamatsu | 88a4c2e | 2009-06-25 16:33:04 +0900 | [diff] [blame] | 549 | printf(SHETHER_NAME ": phy config timeout\n"); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 550 | goto err_phy_cfg; |
| 551 | } |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 552 | /* Read phy status to finish configuring the e-mac */ |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 553 | phy_status = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 554 | |
| 555 | /* Set the transfer speed */ |
| 556 | if (phy_status & (PHY_S_100X_F|PHY_S_100X_H)) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 557 | printf(SHETHER_NAME ": 100Base/"); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 558 | outl(GECMR_100B, GECMR(port)); |
| 559 | } else { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 560 | printf(SHETHER_NAME ": 10Base/"); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 561 | outl(GECMR_10B, GECMR(port)); |
| 562 | } |
| 563 | |
| 564 | /* Check if full duplex mode is supported by the phy */ |
| 565 | if (phy_status & (PHY_S_100X_F|PHY_S_10T_F)) { |
| 566 | printf("Full\n"); |
| 567 | outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), ECMR(port)); |
| 568 | } else { |
| 569 | printf("Half\n"); |
| 570 | outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR(port)); |
| 571 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 572 | |
| 573 | return ret; |
| 574 | |
| 575 | err_phy_cfg: |
| 576 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 577 | } |
| 578 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 579 | static void sh_eth_start(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 580 | { |
| 581 | /* |
| 582 | * Enable the e-dmac receiver only. The transmitter will be enabled when |
| 583 | * we have something to transmit |
| 584 | */ |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 585 | outl(EDRRR_R, EDRRR(eth->port)); |
| 586 | } |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 587 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 588 | static void sh_eth_stop(struct sh_eth_dev *eth) |
| 589 | { |
| 590 | outl(~EDRRR_R, EDRRR(eth->port)); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 591 | } |
| 592 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 593 | int sh_eth_init(struct eth_device *dev, bd_t *bd) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 594 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 595 | int ret = 0; |
| 596 | struct sh_eth_dev *eth = dev->priv; |
| 597 | |
| 598 | ret = sh_eth_reset(eth); |
| 599 | if (ret) |
| 600 | goto err; |
| 601 | |
| 602 | ret = sh_eth_desc_init(eth); |
| 603 | if (ret) |
| 604 | goto err; |
| 605 | |
| 606 | ret = sh_eth_config(eth, bd); |
| 607 | if (ret) |
| 608 | goto err_config; |
| 609 | |
| 610 | sh_eth_start(eth); |
| 611 | |
| 612 | return ret; |
| 613 | |
| 614 | err_config: |
| 615 | sh_eth_tx_desc_free(eth); |
| 616 | sh_eth_rx_desc_free(eth); |
| 617 | |
| 618 | err: |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | void sh_eth_halt(struct eth_device *dev) |
| 623 | { |
| 624 | struct sh_eth_dev *eth = dev->priv; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 625 | sh_eth_stop(eth); |
| 626 | } |
| 627 | |
| 628 | int sh_eth_initialize(bd_t *bd) |
| 629 | { |
| 630 | int ret = 0; |
| 631 | struct sh_eth_dev *eth = NULL; |
| 632 | struct eth_device *dev = NULL; |
| 633 | |
| 634 | eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev)); |
| 635 | if (!eth) { |
| 636 | printf(SHETHER_NAME ": %s: malloc failed\n", __func__); |
| 637 | ret = -ENOMEM; |
| 638 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 639 | } |
| 640 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 641 | dev = (struct eth_device *)malloc(sizeof(struct eth_device)); |
| 642 | if (!dev) { |
| 643 | printf(SHETHER_NAME ": %s: malloc failed\n", __func__); |
| 644 | ret = -ENOMEM; |
| 645 | goto err; |
| 646 | } |
| 647 | memset(dev, 0, sizeof(struct eth_device)); |
| 648 | memset(eth, 0, sizeof(struct sh_eth_dev)); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 649 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 650 | eth->port = CONFIG_SH_ETHER_USE_PORT; |
| 651 | eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR; |
| 652 | |
| 653 | dev->priv = (void *)eth; |
| 654 | dev->iobase = 0; |
| 655 | dev->init = sh_eth_init; |
| 656 | dev->halt = sh_eth_halt; |
| 657 | dev->send = sh_eth_send; |
| 658 | dev->recv = sh_eth_recv; |
| 659 | eth->port_info[eth->port].dev = dev; |
| 660 | |
| 661 | sprintf(dev->name, SHETHER_NAME); |
| 662 | |
| 663 | /* Register Device to EtherNet subsystem */ |
| 664 | eth_register(dev); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 665 | |
Mike Frysinger | c527ce9 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 666 | if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr)) |
| 667 | puts("Please set MAC address\n"); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 668 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 669 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 670 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 671 | err: |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 672 | if (dev) |
| 673 | free(dev); |
| 674 | |
| 675 | if (eth) |
| 676 | free(eth); |
| 677 | |
| 678 | printf(SHETHER_NAME ": Failed\n"); |
| 679 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 680 | } |