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