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