Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 1 | /* |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 2 | * sh_eth.c - Driver for Renesas ethernet controler. |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 3 | * |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 4 | * Copyright (C) 2008, 2011 Renesas Solutions Corp. |
| 5 | * Copyright (c) 2008, 2011 Nobuhiro Iwamatsu |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 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 | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 27 | #include <netdev.h> |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 28 | #include <miiphy.h> |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 29 | #include <asm/errno.h> |
| 30 | #include <asm/io.h> |
| 31 | |
| 32 | #include "sh_eth.h" |
| 33 | |
| 34 | #ifndef CONFIG_SH_ETHER_USE_PORT |
| 35 | # error "Please define CONFIG_SH_ETHER_USE_PORT" |
| 36 | #endif |
| 37 | #ifndef CONFIG_SH_ETHER_PHY_ADDR |
| 38 | # error "Please define CONFIG_SH_ETHER_PHY_ADDR" |
| 39 | #endif |
Yoshihiro Shimoda | 281aa05 | 2011-01-27 10:06:08 +0900 | [diff] [blame] | 40 | #ifdef CONFIG_SH_ETHER_CACHE_WRITEBACK |
| 41 | #define flush_cache_wback(addr, len) \ |
| 42 | dcache_wback_range((u32)addr, (u32)(addr + len - 1)) |
| 43 | #else |
| 44 | #define flush_cache_wback(...) |
| 45 | #endif |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 46 | |
Nobuhiro Iwamatsu | 71f507c | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 47 | #define TIMEOUT_CNT 1000 |
| 48 | |
Joe Hershberger | e4e0488 | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 49 | int sh_eth_send(struct eth_device *dev, void *packet, int len) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 50 | { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 51 | struct sh_eth_dev *eth = dev->priv; |
| 52 | int port = eth->port, ret = 0, timeout; |
| 53 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 54 | |
| 55 | if (!packet || len > 0xffff) { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 56 | printf(SHETHER_NAME ": %s: Invalid argument\n", __func__); |
| 57 | ret = -EINVAL; |
| 58 | goto err; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* packet must be a 4 byte boundary */ |
Nobuhiro Iwamatsu | 5880290 | 2012-02-02 21:28:49 +0000 | [diff] [blame] | 62 | if ((int)packet & 3) { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 63 | printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__); |
| 64 | ret = -EFAULT; |
| 65 | goto err; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* Update tx descriptor */ |
Yoshihiro Shimoda | 281aa05 | 2011-01-27 10:06:08 +0900 | [diff] [blame] | 69 | flush_cache_wback(packet, len); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 70 | port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet); |
| 71 | port_info->tx_desc_cur->td1 = len << 16; |
| 72 | /* Must preserve the end of descriptor list indication */ |
| 73 | if (port_info->tx_desc_cur->td0 & TD_TDLE) |
| 74 | port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE; |
| 75 | else |
| 76 | port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP; |
| 77 | |
| 78 | /* Restart the transmitter if disabled */ |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 79 | if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS)) |
| 80 | sh_eth_write(eth, EDTRR_TRNS, EDTRR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 81 | |
| 82 | /* Wait until packet is transmitted */ |
Nobuhiro Iwamatsu | 71f507c | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 83 | timeout = TIMEOUT_CNT; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 84 | while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--) |
| 85 | udelay(100); |
| 86 | |
| 87 | if (timeout < 0) { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 88 | printf(SHETHER_NAME ": transmit timeout\n"); |
| 89 | ret = -ETIMEDOUT; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 90 | goto err; |
| 91 | } |
| 92 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 93 | port_info->tx_desc_cur++; |
| 94 | if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC) |
| 95 | port_info->tx_desc_cur = port_info->tx_desc_base; |
| 96 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 97 | err: |
| 98 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 99 | } |
| 100 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 101 | int sh_eth_recv(struct eth_device *dev) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 102 | { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 103 | struct sh_eth_dev *eth = dev->priv; |
| 104 | int port = eth->port, len = 0; |
| 105 | struct sh_eth_info *port_info = ð->port_info[port]; |
Joe Hershberger | e4e0488 | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 106 | uchar *packet; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 107 | |
| 108 | /* Check if the rx descriptor is ready */ |
| 109 | if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) { |
| 110 | /* Check for errors */ |
| 111 | if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) { |
| 112 | len = port_info->rx_desc_cur->rd1 & 0xffff; |
Joe Hershberger | e4e0488 | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 113 | packet = (uchar *) |
| 114 | ADDR_TO_P2(port_info->rx_desc_cur->rd2); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 115 | NetReceive(packet, len); |
| 116 | } |
| 117 | |
| 118 | /* Make current descriptor available again */ |
| 119 | if (port_info->rx_desc_cur->rd0 & RD_RDLE) |
| 120 | port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE; |
| 121 | else |
| 122 | port_info->rx_desc_cur->rd0 = RD_RACT; |
| 123 | |
| 124 | /* Point to the next descriptor */ |
| 125 | port_info->rx_desc_cur++; |
| 126 | if (port_info->rx_desc_cur >= |
| 127 | port_info->rx_desc_base + NUM_RX_DESC) |
| 128 | port_info->rx_desc_cur = port_info->rx_desc_base; |
| 129 | } |
| 130 | |
| 131 | /* Restart the receiver if disabled */ |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 132 | if (!(sh_eth_read(eth, EDRRR) & EDRRR_R)) |
| 133 | sh_eth_write(eth, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 134 | |
| 135 | return len; |
| 136 | } |
| 137 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 138 | static int sh_eth_reset(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 139 | { |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 140 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 141 | int ret = 0, i; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 142 | |
| 143 | /* Start e-dmac transmitter and receiver */ |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 144 | sh_eth_write(eth, EDSR_ENALL, EDSR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 145 | |
| 146 | /* Perform a software reset and wait for it to complete */ |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 147 | sh_eth_write(eth, EDMR_SRST, EDMR); |
Nobuhiro Iwamatsu | 71f507c | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 148 | for (i = 0; i < TIMEOUT_CNT ; i++) { |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 149 | if (!(sh_eth_read(eth, EDMR) & EDMR_SRST)) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 150 | break; |
| 151 | udelay(1000); |
| 152 | } |
| 153 | |
Nobuhiro Iwamatsu | 71f507c | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 154 | if (i == TIMEOUT_CNT) { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 155 | printf(SHETHER_NAME ": Software reset timeout\n"); |
| 156 | ret = -EIO; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 157 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 158 | |
| 159 | return ret; |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 160 | #else |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 161 | sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR); |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 162 | udelay(3000); |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 163 | sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR); |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | #endif |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 167 | } |
| 168 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 169 | static int sh_eth_tx_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 170 | { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 171 | int port = eth->port, i, ret = 0; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 172 | u32 tmp_addr; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 173 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 174 | struct tx_desc_s *cur_tx_desc; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 175 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 176 | /* |
| 177 | * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned |
| 178 | */ |
| 179 | port_info->tx_desc_malloc = malloc(NUM_TX_DESC * |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 180 | sizeof(struct tx_desc_s) + |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 181 | TX_DESC_SIZE - 1); |
| 182 | if (!port_info->tx_desc_malloc) { |
| 183 | printf(SHETHER_NAME ": malloc failed\n"); |
| 184 | ret = -ENOMEM; |
| 185 | goto err; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 186 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 187 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 188 | tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) & |
| 189 | ~(TX_DESC_SIZE - 1)); |
Yoshihiro Shimoda | 281aa05 | 2011-01-27 10:06:08 +0900 | [diff] [blame] | 190 | flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s)); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 191 | /* Make sure we use a P2 address (non-cacheable) */ |
| 192 | port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 193 | port_info->tx_desc_cur = port_info->tx_desc_base; |
| 194 | |
| 195 | /* Initialize all descriptors */ |
| 196 | for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC; |
| 197 | cur_tx_desc++, i++) { |
| 198 | cur_tx_desc->td0 = 0x00; |
| 199 | cur_tx_desc->td1 = 0x00; |
| 200 | cur_tx_desc->td2 = 0x00; |
| 201 | } |
| 202 | |
| 203 | /* Mark the end of the descriptors */ |
| 204 | cur_tx_desc--; |
| 205 | cur_tx_desc->td0 |= TD_TDLE; |
| 206 | |
| 207 | /* Point the controller to the tx descriptor list. Must use physical |
| 208 | addresses */ |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 209 | sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR); |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 210 | #if defined(SH_ETH_TYPE_GETHER) |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 211 | sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR); |
| 212 | sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR); |
| 213 | sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */ |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 214 | #endif |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 215 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 216 | err: |
| 217 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 218 | } |
| 219 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 220 | static int sh_eth_rx_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 221 | { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 222 | int port = eth->port, i , ret = 0; |
| 223 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 224 | struct rx_desc_s *cur_rx_desc; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 225 | u32 tmp_addr; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 226 | u8 *rx_buf; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 227 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 228 | /* |
| 229 | * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned |
| 230 | */ |
| 231 | port_info->rx_desc_malloc = malloc(NUM_RX_DESC * |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 232 | sizeof(struct rx_desc_s) + |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 233 | RX_DESC_SIZE - 1); |
| 234 | if (!port_info->rx_desc_malloc) { |
| 235 | printf(SHETHER_NAME ": malloc failed\n"); |
| 236 | ret = -ENOMEM; |
| 237 | goto err; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 238 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 239 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 240 | tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) & |
| 241 | ~(RX_DESC_SIZE - 1)); |
Yoshihiro Shimoda | 281aa05 | 2011-01-27 10:06:08 +0900 | [diff] [blame] | 242 | flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s)); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 243 | /* Make sure we use a P2 address (non-cacheable) */ |
| 244 | port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr); |
| 245 | |
| 246 | port_info->rx_desc_cur = port_info->rx_desc_base; |
| 247 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 248 | /* |
| 249 | * Allocate rx data buffers. They must be 32 bytes aligned and in |
| 250 | * P2 area |
| 251 | */ |
| 252 | port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31); |
| 253 | if (!port_info->rx_buf_malloc) { |
| 254 | printf(SHETHER_NAME ": malloc failed\n"); |
| 255 | ret = -ENOMEM; |
| 256 | goto err_buf_malloc; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 257 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 258 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 259 | tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) & |
| 260 | ~(32 - 1)); |
| 261 | port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr); |
| 262 | |
| 263 | /* Initialize all descriptors */ |
| 264 | for (cur_rx_desc = port_info->rx_desc_base, |
| 265 | rx_buf = port_info->rx_buf_base, i = 0; |
| 266 | i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) { |
| 267 | cur_rx_desc->rd0 = RD_RACT; |
| 268 | cur_rx_desc->rd1 = MAX_BUF_SIZE << 16; |
| 269 | cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf); |
| 270 | } |
| 271 | |
| 272 | /* Mark the end of the descriptors */ |
| 273 | cur_rx_desc--; |
| 274 | cur_rx_desc->rd0 |= RD_RDLE; |
| 275 | |
| 276 | /* Point the controller to the rx descriptor list */ |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 277 | sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR); |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 278 | #if defined(SH_ETH_TYPE_GETHER) |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 279 | sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR); |
| 280 | sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR); |
| 281 | sh_eth_write(eth, RDFFR_RDLF, RDFFR); |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 282 | #endif |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 283 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 284 | return ret; |
| 285 | |
| 286 | err_buf_malloc: |
| 287 | free(port_info->rx_desc_malloc); |
| 288 | port_info->rx_desc_malloc = NULL; |
| 289 | |
| 290 | err: |
| 291 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 292 | } |
| 293 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 294 | static void sh_eth_tx_desc_free(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 295 | { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 296 | int port = eth->port; |
| 297 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 298 | |
| 299 | if (port_info->tx_desc_malloc) { |
| 300 | free(port_info->tx_desc_malloc); |
| 301 | port_info->tx_desc_malloc = NULL; |
| 302 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static void sh_eth_rx_desc_free(struct sh_eth_dev *eth) |
| 306 | { |
| 307 | int port = eth->port; |
| 308 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 309 | |
| 310 | if (port_info->rx_desc_malloc) { |
| 311 | free(port_info->rx_desc_malloc); |
| 312 | port_info->rx_desc_malloc = NULL; |
| 313 | } |
| 314 | |
| 315 | if (port_info->rx_buf_malloc) { |
| 316 | free(port_info->rx_buf_malloc); |
| 317 | port_info->rx_buf_malloc = NULL; |
| 318 | } |
| 319 | } |
| 320 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 321 | static int sh_eth_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 322 | { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 323 | int ret = 0; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 324 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 325 | ret = sh_eth_tx_desc_init(eth); |
| 326 | if (ret) |
| 327 | goto err_tx_init; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 328 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 329 | ret = sh_eth_rx_desc_init(eth); |
| 330 | if (ret) |
| 331 | goto err_rx_init; |
| 332 | |
| 333 | return ret; |
| 334 | err_rx_init: |
| 335 | sh_eth_tx_desc_free(eth); |
| 336 | |
| 337 | err_tx_init: |
| 338 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 339 | } |
| 340 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 341 | static int sh_eth_phy_config(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 342 | { |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 343 | int port = eth->port, ret = 0; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 344 | struct sh_eth_info *port_info = ð->port_info[port]; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 345 | struct eth_device *dev = port_info->dev; |
| 346 | struct phy_device *phydev; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 347 | |
Nobuhiro Iwamatsu | 5880290 | 2012-02-02 21:28:49 +0000 | [diff] [blame] | 348 | phydev = phy_connect( |
| 349 | miiphy_get_dev_by_name(dev->name), |
Nobuhiro Iwamatsu | 475f40d | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 350 | port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 351 | port_info->phydev = phydev; |
| 352 | phy_config(phydev); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 353 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 354 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 355 | } |
| 356 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 357 | static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 358 | { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 359 | int port = eth->port, ret = 0; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 360 | u32 val; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 361 | struct sh_eth_info *port_info = ð->port_info[port]; |
Mike Frysinger | a86bf13 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 362 | struct eth_device *dev = port_info->dev; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 363 | struct phy_device *phy; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 364 | |
| 365 | /* Configure e-dmac registers */ |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 366 | sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) | EDMR_EL, |
| 367 | EDMR); |
| 368 | sh_eth_write(eth, 0, EESIPR); |
| 369 | sh_eth_write(eth, 0, TRSCER); |
| 370 | sh_eth_write(eth, 0, TFTR); |
| 371 | sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR); |
| 372 | sh_eth_write(eth, RMCR_RST, RMCR); |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 373 | #if defined(SH_ETH_TYPE_GETHER) |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 374 | sh_eth_write(eth, 0, RPADIR); |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 375 | #endif |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 376 | sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 377 | |
| 378 | /* Configure e-mac registers */ |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 379 | sh_eth_write(eth, 0, ECSIPR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 380 | |
| 381 | /* Set Mac address */ |
Mike Frysinger | a86bf13 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 382 | val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 | |
| 383 | dev->enetaddr[2] << 8 | dev->enetaddr[3]; |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 384 | sh_eth_write(eth, val, MAHR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 385 | |
Mike Frysinger | a86bf13 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 386 | val = dev->enetaddr[4] << 8 | dev->enetaddr[5]; |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 387 | sh_eth_write(eth, val, MALR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 388 | |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 389 | sh_eth_write(eth, RFLR_RFL_MIN, RFLR); |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 390 | #if defined(SH_ETH_TYPE_GETHER) |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 391 | sh_eth_write(eth, 0, PIPR); |
| 392 | sh_eth_write(eth, APR_AP, APR); |
| 393 | sh_eth_write(eth, MPR_MP, MPR); |
| 394 | sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER); |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 395 | #endif |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 396 | |
Nobuhiro Iwamatsu | 475f40d | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 397 | #if defined(CONFIG_CPU_SH7734) |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 398 | sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII); |
Nobuhiro Iwamatsu | 475f40d | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 399 | #endif |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 400 | /* Configure phy */ |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 401 | ret = sh_eth_phy_config(eth); |
| 402 | if (ret) { |
Nobuhiro Iwamatsu | fc4b0a2 | 2009-06-25 16:33:04 +0900 | [diff] [blame] | 403 | printf(SHETHER_NAME ": phy config timeout\n"); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 404 | goto err_phy_cfg; |
| 405 | } |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 406 | phy = port_info->phydev; |
Timur Tabi | 4238746 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 407 | ret = phy_startup(phy); |
| 408 | if (ret) { |
| 409 | printf(SHETHER_NAME ": phy startup failure\n"); |
| 410 | return ret; |
| 411 | } |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 412 | |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 413 | val = 0; |
| 414 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 415 | /* Set the transfer speed */ |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 416 | if (phy->speed == 100) { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 417 | printf(SHETHER_NAME ": 100Base/"); |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 418 | #if defined(SH_ETH_TYPE_GETHER) |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 419 | sh_eth_write(eth, GECMR_100B, GECMR); |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 420 | #elif defined(CONFIG_CPU_SH7757) |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 421 | sh_eth_write(eth, 1, RTRATE); |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 422 | #elif defined(CONFIG_CPU_SH7724) |
| 423 | val = ECMR_RTM; |
| 424 | #endif |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 425 | } else if (phy->speed == 10) { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 426 | printf(SHETHER_NAME ": 10Base/"); |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 427 | #if defined(SH_ETH_TYPE_GETHER) |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 428 | sh_eth_write(eth, GECMR_10B, GECMR); |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 429 | #elif defined(CONFIG_CPU_SH7757) |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 430 | sh_eth_write(eth, 0, RTRATE); |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 431 | #endif |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 432 | } |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 433 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | 475f40d | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 434 | else if (phy->speed == 1000) { |
| 435 | printf(SHETHER_NAME ": 1000Base/"); |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 436 | sh_eth_write(eth, GECMR_1000B, GECMR); |
Nobuhiro Iwamatsu | 475f40d | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 437 | } |
| 438 | #endif |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 439 | |
| 440 | /* Check if full duplex mode is supported by the phy */ |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 441 | if (phy->duplex) { |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 442 | printf("Full\n"); |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 443 | sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), |
| 444 | ECMR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 445 | } else { |
| 446 | printf("Half\n"); |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 447 | sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 448 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 449 | |
| 450 | return ret; |
| 451 | |
| 452 | err_phy_cfg: |
| 453 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 454 | } |
| 455 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 456 | static void sh_eth_start(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 457 | { |
| 458 | /* |
| 459 | * Enable the e-dmac receiver only. The transmitter will be enabled when |
| 460 | * we have something to transmit |
| 461 | */ |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 462 | sh_eth_write(eth, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 463 | } |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 464 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 465 | static void sh_eth_stop(struct sh_eth_dev *eth) |
| 466 | { |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 467 | sh_eth_write(eth, ~EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 468 | } |
| 469 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 470 | int sh_eth_init(struct eth_device *dev, bd_t *bd) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 471 | { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 472 | int ret = 0; |
| 473 | struct sh_eth_dev *eth = dev->priv; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 474 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 475 | ret = sh_eth_reset(eth); |
| 476 | if (ret) |
| 477 | goto err; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 478 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 479 | ret = sh_eth_desc_init(eth); |
| 480 | if (ret) |
| 481 | goto err; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 482 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 483 | ret = sh_eth_config(eth, bd); |
| 484 | if (ret) |
| 485 | goto err_config; |
| 486 | |
| 487 | sh_eth_start(eth); |
| 488 | |
| 489 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 490 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 491 | err_config: |
| 492 | sh_eth_tx_desc_free(eth); |
| 493 | sh_eth_rx_desc_free(eth); |
| 494 | |
| 495 | err: |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | void sh_eth_halt(struct eth_device *dev) |
| 500 | { |
| 501 | struct sh_eth_dev *eth = dev->priv; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 502 | sh_eth_stop(eth); |
| 503 | } |
| 504 | |
| 505 | int sh_eth_initialize(bd_t *bd) |
| 506 | { |
| 507 | int ret = 0; |
| 508 | struct sh_eth_dev *eth = NULL; |
| 509 | struct eth_device *dev = NULL; |
| 510 | |
| 511 | eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev)); |
| 512 | if (!eth) { |
| 513 | printf(SHETHER_NAME ": %s: malloc failed\n", __func__); |
| 514 | ret = -ENOMEM; |
| 515 | goto err; |
| 516 | } |
| 517 | |
| 518 | dev = (struct eth_device *)malloc(sizeof(struct eth_device)); |
| 519 | if (!dev) { |
| 520 | printf(SHETHER_NAME ": %s: malloc failed\n", __func__); |
| 521 | ret = -ENOMEM; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 522 | goto err; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 523 | } |
| 524 | memset(dev, 0, sizeof(struct eth_device)); |
| 525 | memset(eth, 0, sizeof(struct sh_eth_dev)); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 526 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 527 | eth->port = CONFIG_SH_ETHER_USE_PORT; |
| 528 | eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 529 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 530 | dev->priv = (void *)eth; |
| 531 | dev->iobase = 0; |
| 532 | dev->init = sh_eth_init; |
| 533 | dev->halt = sh_eth_halt; |
| 534 | dev->send = sh_eth_send; |
| 535 | dev->recv = sh_eth_recv; |
| 536 | eth->port_info[eth->port].dev = dev; |
| 537 | |
| 538 | sprintf(dev->name, SHETHER_NAME); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 539 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 540 | /* Register Device to EtherNet subsystem */ |
| 541 | eth_register(dev); |
| 542 | |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 543 | bb_miiphy_buses[0].priv = eth; |
| 544 | miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write); |
| 545 | |
Mike Frysinger | a86bf13 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 546 | if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr)) |
| 547 | puts("Please set MAC address\n"); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 548 | |
| 549 | return ret; |
| 550 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 551 | err: |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 552 | if (dev) |
| 553 | free(dev); |
| 554 | |
| 555 | if (eth) |
| 556 | free(eth); |
| 557 | |
| 558 | printf(SHETHER_NAME ": Failed\n"); |
| 559 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 560 | } |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 561 | |
| 562 | /******* for bb_miiphy *******/ |
| 563 | static int sh_eth_bb_init(struct bb_miiphy_bus *bus) |
| 564 | { |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus) |
| 569 | { |
| 570 | struct sh_eth_dev *eth = bus->priv; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 571 | |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 572 | sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus) |
| 578 | { |
| 579 | struct sh_eth_dev *eth = bus->priv; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 580 | |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 581 | sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v) |
| 587 | { |
| 588 | struct sh_eth_dev *eth = bus->priv; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 589 | |
| 590 | if (v) |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 591 | sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 592 | else |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 593 | sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v) |
| 599 | { |
| 600 | struct sh_eth_dev *eth = bus->priv; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 601 | |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 602 | *v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v) |
| 608 | { |
| 609 | struct sh_eth_dev *eth = bus->priv; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 610 | |
| 611 | if (v) |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 612 | sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 613 | else |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 614 | sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int sh_eth_bb_delay(struct bb_miiphy_bus *bus) |
| 620 | { |
| 621 | udelay(10); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | struct bb_miiphy_bus bb_miiphy_buses[] = { |
| 627 | { |
| 628 | .name = "sh_eth", |
| 629 | .init = sh_eth_bb_init, |
| 630 | .mdio_active = sh_eth_bb_mdio_active, |
| 631 | .mdio_tristate = sh_eth_bb_mdio_tristate, |
| 632 | .set_mdio = sh_eth_bb_set_mdio, |
| 633 | .get_mdio = sh_eth_bb_get_mdio, |
| 634 | .set_mdc = sh_eth_bb_set_mdc, |
| 635 | .delay = sh_eth_bb_delay, |
| 636 | } |
| 637 | }; |
| 638 | int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses); |