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