Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 2 | /* |
Robert P. J. Day | 8c60f92 | 2016-05-04 04:47:31 -0400 | [diff] [blame] | 3 | * sh_eth.c - Driver for Renesas ethernet controller. |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 4 | * |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 5 | * Copyright (C) 2008, 2011 Renesas Solutions Corp. |
Nobuhiro Iwamatsu | 5ba66ad | 2014-11-04 09:15:48 +0900 | [diff] [blame] | 6 | * Copyright (c) 2008, 2011, 2014 2014 Nobuhiro Iwamatsu |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 7 | * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com> |
Nobuhiro Iwamatsu | 5ba66ad | 2014-11-04 09:15:48 +0900 | [diff] [blame] | 8 | * Copyright (C) 2013, 2014 Renesas Electronics Corporation |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <config.h> |
| 12 | #include <common.h> |
Alex Kiernan | 9c21549 | 2018-04-01 09:22:38 +0000 | [diff] [blame] | 13 | #include <environment.h> |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 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> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 18 | #include <linux/errno.h> |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 19 | #include <asm/io.h> |
| 20 | |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 21 | #ifdef CONFIG_DM_ETH |
| 22 | #include <clk.h> |
| 23 | #include <dm.h> |
| 24 | #include <linux/mii.h> |
| 25 | #include <asm/gpio.h> |
| 26 | #endif |
| 27 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 28 | #include "sh_eth.h" |
| 29 | |
| 30 | #ifndef CONFIG_SH_ETHER_USE_PORT |
| 31 | # error "Please define CONFIG_SH_ETHER_USE_PORT" |
| 32 | #endif |
| 33 | #ifndef CONFIG_SH_ETHER_PHY_ADDR |
| 34 | # error "Please define CONFIG_SH_ETHER_PHY_ADDR" |
| 35 | #endif |
Nobuhiro Iwamatsu | 6bff09d | 2013-08-22 13:22:01 +0900 | [diff] [blame] | 36 | |
Nobuhiro Iwamatsu | ee74c70 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 37 | #if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && !defined(CONFIG_SYS_DCACHE_OFF) |
| 38 | #define flush_cache_wback(addr, len) \ |
Nobuhiro Iwamatsu | 425a3a5 | 2017-12-01 13:56:08 +0900 | [diff] [blame] | 39 | flush_dcache_range((u32)addr, \ |
| 40 | (u32)(addr + ALIGN(len, CONFIG_SH_ETHER_ALIGNE_SIZE))) |
Yoshihiro Shimoda | 281aa05 | 2011-01-27 10:06:08 +0900 | [diff] [blame] | 41 | #else |
| 42 | #define flush_cache_wback(...) |
| 43 | #endif |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 44 | |
Nobuhiro Iwamatsu | ee74c70 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 45 | #if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM) |
| 46 | #define invalidate_cache(addr, len) \ |
| 47 | { \ |
| 48 | u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \ |
| 49 | u32 start, end; \ |
| 50 | \ |
| 51 | start = (u32)addr; \ |
| 52 | end = start + len; \ |
| 53 | start &= ~(line_size - 1); \ |
| 54 | end = ((end + line_size - 1) & ~(line_size - 1)); \ |
| 55 | \ |
| 56 | invalidate_dcache_range(start, end); \ |
| 57 | } |
| 58 | #else |
| 59 | #define invalidate_cache(...) |
| 60 | #endif |
| 61 | |
Nobuhiro Iwamatsu | 71f507c | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 62 | #define TIMEOUT_CNT 1000 |
| 63 | |
Marek Vasut | 044eb2d | 2018-01-21 14:27:51 +0100 | [diff] [blame] | 64 | static int sh_eth_send_common(struct sh_eth_dev *eth, void *packet, int len) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 65 | { |
Marek Vasut | 7a309cf | 2018-02-17 00:46:26 +0100 | [diff] [blame] | 66 | int ret = 0, timeout; |
| 67 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 68 | |
| 69 | if (!packet || len > 0xffff) { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 70 | printf(SHETHER_NAME ": %s: Invalid argument\n", __func__); |
| 71 | ret = -EINVAL; |
| 72 | goto err; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* packet must be a 4 byte boundary */ |
Nobuhiro Iwamatsu | 5880290 | 2012-02-02 21:28:49 +0000 | [diff] [blame] | 76 | if ((int)packet & 3) { |
Nobuhiro Iwamatsu | ca36b0e | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 77 | printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n" |
Nobuhiro Iwamatsu | 31e84df | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 78 | , __func__); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 79 | ret = -EFAULT; |
| 80 | goto err; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* Update tx descriptor */ |
Yoshihiro Shimoda | 281aa05 | 2011-01-27 10:06:08 +0900 | [diff] [blame] | 84 | flush_cache_wback(packet, len); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 85 | port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet); |
| 86 | port_info->tx_desc_cur->td1 = len << 16; |
| 87 | /* Must preserve the end of descriptor list indication */ |
| 88 | if (port_info->tx_desc_cur->td0 & TD_TDLE) |
| 89 | port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE; |
| 90 | else |
| 91 | port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP; |
| 92 | |
Nobuhiro Iwamatsu | 5ba66ad | 2014-11-04 09:15:48 +0900 | [diff] [blame] | 93 | flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s)); |
| 94 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 95 | /* Restart the transmitter if disabled */ |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 96 | if (!(sh_eth_read(port_info, EDTRR) & EDTRR_TRNS)) |
| 97 | sh_eth_write(port_info, EDTRR_TRNS, EDTRR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 98 | |
| 99 | /* Wait until packet is transmitted */ |
Nobuhiro Iwamatsu | 71f507c | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 100 | timeout = TIMEOUT_CNT; |
Nobuhiro Iwamatsu | ee74c70 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 101 | do { |
| 102 | invalidate_cache(port_info->tx_desc_cur, |
| 103 | sizeof(struct tx_desc_s)); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 104 | udelay(100); |
Nobuhiro Iwamatsu | ee74c70 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 105 | } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 106 | |
| 107 | if (timeout < 0) { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 108 | printf(SHETHER_NAME ": transmit timeout\n"); |
| 109 | ret = -ETIMEDOUT; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 110 | goto err; |
| 111 | } |
| 112 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 113 | port_info->tx_desc_cur++; |
| 114 | if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC) |
| 115 | port_info->tx_desc_cur = port_info->tx_desc_base; |
| 116 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 117 | err: |
| 118 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 119 | } |
| 120 | |
Marek Vasut | 48de90d | 2018-01-21 15:39:50 +0100 | [diff] [blame] | 121 | static int sh_eth_recv_start(struct sh_eth_dev *eth) |
Marek Vasut | 044eb2d | 2018-01-21 14:27:51 +0100 | [diff] [blame] | 122 | { |
Marek Vasut | 7a309cf | 2018-02-17 00:46:26 +0100 | [diff] [blame] | 123 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 124 | |
| 125 | /* Check if the rx descriptor is ready */ |
Nobuhiro Iwamatsu | ee74c70 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 126 | invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s)); |
Marek Vasut | 48de90d | 2018-01-21 15:39:50 +0100 | [diff] [blame] | 127 | if (port_info->rx_desc_cur->rd0 & RD_RACT) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | /* Check for errors */ |
| 131 | if (port_info->rx_desc_cur->rd0 & RD_RFE) |
| 132 | return -EINVAL; |
| 133 | |
Marek Vasut | 2526b79 | 2018-02-17 00:47:38 +0100 | [diff] [blame] | 134 | return port_info->rx_desc_cur->rd1 & 0xffff; |
Marek Vasut | 48de90d | 2018-01-21 15:39:50 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void sh_eth_recv_finish(struct sh_eth_dev *eth) |
| 138 | { |
| 139 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 140 | |
Marek Vasut | 48de90d | 2018-01-21 15:39:50 +0100 | [diff] [blame] | 141 | /* Make current descriptor available again */ |
| 142 | if (port_info->rx_desc_cur->rd0 & RD_RDLE) |
| 143 | port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE; |
| 144 | else |
| 145 | port_info->rx_desc_cur->rd0 = RD_RACT; |
Nobuhiro Iwamatsu | 5ba66ad | 2014-11-04 09:15:48 +0900 | [diff] [blame] | 146 | |
Marek Vasut | 48de90d | 2018-01-21 15:39:50 +0100 | [diff] [blame] | 147 | flush_cache_wback(port_info->rx_desc_cur, |
| 148 | sizeof(struct rx_desc_s)); |
| 149 | |
| 150 | /* Point to the next descriptor */ |
| 151 | port_info->rx_desc_cur++; |
| 152 | if (port_info->rx_desc_cur >= |
| 153 | port_info->rx_desc_base + NUM_RX_DESC) |
| 154 | port_info->rx_desc_cur = port_info->rx_desc_base; |
| 155 | } |
| 156 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 157 | static int sh_eth_reset(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 158 | { |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 159 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 46288f4 | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 160 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 161 | int ret = 0, i; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 162 | |
| 163 | /* Start e-dmac transmitter and receiver */ |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 164 | sh_eth_write(port_info, EDSR_ENALL, EDSR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 165 | |
| 166 | /* Perform a software reset and wait for it to complete */ |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 167 | sh_eth_write(port_info, EDMR_SRST, EDMR); |
Nobuhiro Iwamatsu | 31e84df | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 168 | for (i = 0; i < TIMEOUT_CNT; i++) { |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 169 | if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST)) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 170 | break; |
| 171 | udelay(1000); |
| 172 | } |
| 173 | |
Nobuhiro Iwamatsu | 71f507c | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 174 | if (i == TIMEOUT_CNT) { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 175 | printf(SHETHER_NAME ": Software reset timeout\n"); |
| 176 | ret = -EIO; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 177 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 178 | |
| 179 | return ret; |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 180 | #else |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 181 | sh_eth_write(port_info, sh_eth_read(port_info, EDMR) | EDMR_SRST, EDMR); |
Marek Vasut | 42a3340 | 2018-02-17 00:57:49 +0100 | [diff] [blame] | 182 | mdelay(3); |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 183 | sh_eth_write(port_info, |
| 184 | sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR); |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | #endif |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 188 | } |
| 189 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 190 | static int sh_eth_tx_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 191 | { |
Marek Vasut | 7a309cf | 2018-02-17 00:46:26 +0100 | [diff] [blame] | 192 | int i, ret = 0; |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 193 | u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s); |
Marek Vasut | 7a309cf | 2018-02-17 00:46:26 +0100 | [diff] [blame] | 194 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 195 | struct tx_desc_s *cur_tx_desc; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 196 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 197 | /* |
Nobuhiro Iwamatsu | c24b3eb | 2014-11-04 09:15:46 +0900 | [diff] [blame] | 198 | * Allocate rx descriptors. They must be aligned to size of struct |
| 199 | * tx_desc_s. |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 200 | */ |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 201 | port_info->tx_desc_alloc = |
| 202 | memalign(sizeof(struct tx_desc_s), alloc_desc_size); |
| 203 | if (!port_info->tx_desc_alloc) { |
| 204 | printf(SHETHER_NAME ": memalign failed\n"); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 205 | ret = -ENOMEM; |
| 206 | goto err; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 207 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 208 | |
Nobuhiro Iwamatsu | 425a3a5 | 2017-12-01 13:56:08 +0900 | [diff] [blame] | 209 | flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size); |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 210 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 211 | /* Make sure we use a P2 address (non-cacheable) */ |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 212 | port_info->tx_desc_base = |
| 213 | (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] | 214 | port_info->tx_desc_cur = port_info->tx_desc_base; |
| 215 | |
| 216 | /* Initialize all descriptors */ |
| 217 | for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC; |
| 218 | cur_tx_desc++, i++) { |
| 219 | cur_tx_desc->td0 = 0x00; |
| 220 | cur_tx_desc->td1 = 0x00; |
| 221 | cur_tx_desc->td2 = 0x00; |
| 222 | } |
| 223 | |
| 224 | /* Mark the end of the descriptors */ |
| 225 | cur_tx_desc--; |
| 226 | cur_tx_desc->td0 |= TD_TDLE; |
| 227 | |
Nobuhiro Iwamatsu | ca36b0e | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 228 | /* |
| 229 | * Point the controller to the tx descriptor list. Must use physical |
| 230 | * addresses |
| 231 | */ |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 232 | sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR); |
Nobuhiro Iwamatsu | 46288f4 | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 233 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 234 | sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR); |
| 235 | sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR); |
| 236 | sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */ |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 237 | #endif |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 238 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 239 | err: |
| 240 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 241 | } |
| 242 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 243 | static int sh_eth_rx_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 244 | { |
Marek Vasut | 7a309cf | 2018-02-17 00:46:26 +0100 | [diff] [blame] | 245 | int i, ret = 0; |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 246 | u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s); |
Marek Vasut | 7a309cf | 2018-02-17 00:46:26 +0100 | [diff] [blame] | 247 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 248 | struct rx_desc_s *cur_rx_desc; |
| 249 | u8 *rx_buf; |
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 | c24b3eb | 2014-11-04 09:15:46 +0900 | [diff] [blame] | 252 | * Allocate rx descriptors. They must be aligned to size of struct |
| 253 | * rx_desc_s. |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 254 | */ |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 255 | port_info->rx_desc_alloc = |
| 256 | memalign(sizeof(struct rx_desc_s), alloc_desc_size); |
| 257 | if (!port_info->rx_desc_alloc) { |
| 258 | printf(SHETHER_NAME ": memalign failed\n"); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 259 | ret = -ENOMEM; |
| 260 | goto err; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 261 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 262 | |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 263 | flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size); |
| 264 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 265 | /* Make sure we use a P2 address (non-cacheable) */ |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 266 | port_info->rx_desc_base = |
| 267 | (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] | 268 | |
| 269 | port_info->rx_desc_cur = port_info->rx_desc_base; |
| 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 | * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes |
| 273 | * aligned and in P2 area. |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 274 | */ |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 275 | port_info->rx_buf_alloc = |
| 276 | memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE); |
| 277 | if (!port_info->rx_buf_alloc) { |
| 278 | printf(SHETHER_NAME ": alloc failed\n"); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 279 | ret = -ENOMEM; |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 280 | goto err_buf_alloc; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 281 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 282 | |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 283 | 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] | 284 | |
| 285 | /* Initialize all descriptors */ |
| 286 | for (cur_rx_desc = port_info->rx_desc_base, |
| 287 | rx_buf = port_info->rx_buf_base, i = 0; |
| 288 | i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) { |
| 289 | cur_rx_desc->rd0 = RD_RACT; |
| 290 | cur_rx_desc->rd1 = MAX_BUF_SIZE << 16; |
Nobuhiro Iwamatsu | ca36b0e | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 291 | cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* Mark the end of the descriptors */ |
| 295 | cur_rx_desc--; |
| 296 | cur_rx_desc->rd0 |= RD_RDLE; |
| 297 | |
| 298 | /* Point the controller to the rx descriptor list */ |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 299 | sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR); |
Nobuhiro Iwamatsu | 46288f4 | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 300 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 301 | sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR); |
| 302 | sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR); |
| 303 | sh_eth_write(port_info, RDFFR_RDLF, RDFFR); |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 304 | #endif |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 305 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 306 | return ret; |
| 307 | |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 308 | err_buf_alloc: |
| 309 | free(port_info->rx_desc_alloc); |
| 310 | port_info->rx_desc_alloc = NULL; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 311 | |
| 312 | err: |
| 313 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 314 | } |
| 315 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 316 | static void sh_eth_tx_desc_free(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 317 | { |
Marek Vasut | 7a309cf | 2018-02-17 00:46:26 +0100 | [diff] [blame] | 318 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 319 | |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 320 | if (port_info->tx_desc_alloc) { |
| 321 | free(port_info->tx_desc_alloc); |
| 322 | port_info->tx_desc_alloc = NULL; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 323 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void sh_eth_rx_desc_free(struct sh_eth_dev *eth) |
| 327 | { |
Marek Vasut | 7a309cf | 2018-02-17 00:46:26 +0100 | [diff] [blame] | 328 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 329 | |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 330 | if (port_info->rx_desc_alloc) { |
| 331 | free(port_info->rx_desc_alloc); |
| 332 | port_info->rx_desc_alloc = NULL; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 333 | } |
| 334 | |
Nobuhiro Iwamatsu | 1c82211 | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 335 | if (port_info->rx_buf_alloc) { |
| 336 | free(port_info->rx_buf_alloc); |
| 337 | port_info->rx_buf_alloc = NULL; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 341 | static int sh_eth_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 342 | { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 343 | int ret = 0; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 344 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 345 | ret = sh_eth_tx_desc_init(eth); |
| 346 | if (ret) |
| 347 | goto err_tx_init; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 348 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 349 | ret = sh_eth_rx_desc_init(eth); |
| 350 | if (ret) |
| 351 | goto err_rx_init; |
| 352 | |
| 353 | return ret; |
| 354 | err_rx_init: |
| 355 | sh_eth_tx_desc_free(eth); |
| 356 | |
| 357 | err_tx_init: |
| 358 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 359 | } |
| 360 | |
Marek Vasut | ccdfc5e | 2018-01-21 14:55:44 +0100 | [diff] [blame] | 361 | static void sh_eth_write_hwaddr(struct sh_eth_info *port_info, |
| 362 | unsigned char *mac) |
| 363 | { |
| 364 | u32 val; |
| 365 | |
| 366 | val = (mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3]; |
| 367 | sh_eth_write(port_info, val, MAHR); |
| 368 | |
| 369 | val = (mac[4] << 8) | mac[5]; |
| 370 | sh_eth_write(port_info, val, MALR); |
| 371 | } |
| 372 | |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 373 | static void sh_eth_mac_regs_config(struct sh_eth_dev *eth, unsigned char *mac) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 374 | { |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 375 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 376 | |
| 377 | /* Configure e-dmac registers */ |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 378 | sh_eth_write(port_info, (sh_eth_read(port_info, EDMR) & ~EMDR_DESC_R) | |
Nobuhiro Iwamatsu | 7a2142c | 2013-08-22 13:22:02 +0900 | [diff] [blame] | 379 | (EMDR_DESC | EDMR_EL), EDMR); |
| 380 | |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 381 | sh_eth_write(port_info, 0, EESIPR); |
| 382 | sh_eth_write(port_info, 0, TRSCER); |
| 383 | sh_eth_write(port_info, 0, TFTR); |
| 384 | sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR); |
| 385 | sh_eth_write(port_info, RMCR_RST, RMCR); |
Nobuhiro Iwamatsu | 46288f4 | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 386 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 387 | sh_eth_write(port_info, 0, RPADIR); |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 388 | #endif |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 389 | sh_eth_write(port_info, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 390 | |
| 391 | /* Configure e-mac registers */ |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 392 | sh_eth_write(port_info, 0, ECSIPR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 393 | |
| 394 | /* Set Mac address */ |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 395 | sh_eth_write_hwaddr(port_info, mac); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 396 | |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 397 | sh_eth_write(port_info, RFLR_RFL_MIN, RFLR); |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 398 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 399 | sh_eth_write(port_info, 0, PIPR); |
Nobuhiro Iwamatsu | 46288f4 | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 400 | #endif |
| 401 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 402 | sh_eth_write(port_info, APR_AP, APR); |
| 403 | sh_eth_write(port_info, MPR_MP, MPR); |
| 404 | sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER); |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 405 | #endif |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 406 | |
Nobuhiro Iwamatsu | 4ad2c2a | 2012-08-02 22:08:40 +0000 | [diff] [blame] | 407 | #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 408 | sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII); |
Marek Vasut | ee2f21b | 2018-01-22 01:42:32 +0100 | [diff] [blame] | 409 | #elif defined(CONFIG_RCAR_GEN2) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 410 | sh_eth_write(port_info, sh_eth_read(port_info, RMIIMR) | 0x1, RMIIMR); |
Nobuhiro Iwamatsu | 475f40d | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 411 | #endif |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 412 | } |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 413 | |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 414 | static int sh_eth_phy_regs_config(struct sh_eth_dev *eth) |
| 415 | { |
| 416 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 417 | struct phy_device *phy = port_info->phydev; |
| 418 | int ret = 0; |
| 419 | u32 val = 0; |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 420 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 421 | /* Set the transfer speed */ |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 422 | if (phy->speed == 100) { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 423 | printf(SHETHER_NAME ": 100Base/"); |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 424 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 425 | sh_eth_write(port_info, GECMR_100B, GECMR); |
Yoshihiro Shimoda | d27e8c9 | 2012-11-04 15:54:30 +0000 | [diff] [blame] | 426 | #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 427 | sh_eth_write(port_info, 1, RTRATE); |
Marek Vasut | a71ce47 | 2019-05-04 13:31:06 +0200 | [diff] [blame] | 428 | #elif defined(CONFIG_RCAR_GEN2) |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 429 | val = ECMR_RTM; |
| 430 | #endif |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 431 | } else if (phy->speed == 10) { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 432 | printf(SHETHER_NAME ": 10Base/"); |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 433 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 434 | sh_eth_write(port_info, GECMR_10B, GECMR); |
Yoshihiro Shimoda | d27e8c9 | 2012-11-04 15:54:30 +0000 | [diff] [blame] | 435 | #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 436 | sh_eth_write(port_info, 0, RTRATE); |
Yoshihiro Shimoda | 34cca92 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 437 | #endif |
Nobuhiro Iwamatsu | 9dfac0a | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 438 | } |
Yoshihiro Shimoda | 9d55303 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 439 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | 475f40d | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 440 | else if (phy->speed == 1000) { |
| 441 | printf(SHETHER_NAME ": 1000Base/"); |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 442 | sh_eth_write(port_info, GECMR_1000B, GECMR); |
Nobuhiro Iwamatsu | 475f40d | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 443 | } |
| 444 | #endif |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 445 | |
| 446 | /* Check if full duplex mode is supported by the phy */ |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 447 | if (phy->duplex) { |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 448 | printf("Full\n"); |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 449 | sh_eth_write(port_info, |
Nobuhiro Iwamatsu | ca36b0e | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 450 | val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM), |
Yoshihiro Shimoda | 4c4aa6c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 451 | ECMR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 452 | } else { |
| 453 | printf("Half\n"); |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 454 | sh_eth_write(port_info, |
Nobuhiro Iwamatsu | ca36b0e | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 455 | val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE), |
| 456 | ECMR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 457 | } |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 458 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 459 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 460 | } |
| 461 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 462 | static void sh_eth_start(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 463 | { |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 464 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 465 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 466 | /* |
| 467 | * Enable the e-dmac receiver only. The transmitter will be enabled when |
| 468 | * we have something to transmit |
| 469 | */ |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 470 | sh_eth_write(port_info, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 471 | } |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 472 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 473 | static void sh_eth_stop(struct sh_eth_dev *eth) |
| 474 | { |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 475 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 476 | |
| 477 | sh_eth_write(port_info, ~EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 478 | } |
| 479 | |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 480 | static int sh_eth_init_common(struct sh_eth_dev *eth, unsigned char *mac) |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 481 | { |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 482 | int ret = 0; |
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 | ret = sh_eth_reset(eth); |
| 485 | if (ret) |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 486 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 487 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 488 | ret = sh_eth_desc_init(eth); |
| 489 | if (ret) |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 490 | return ret; |
| 491 | |
| 492 | sh_eth_mac_regs_config(eth, mac); |
| 493 | |
| 494 | return 0; |
| 495 | } |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 496 | |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 497 | static int sh_eth_start_common(struct sh_eth_dev *eth) |
| 498 | { |
| 499 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 500 | int ret; |
| 501 | |
| 502 | ret = phy_startup(port_info->phydev); |
| 503 | if (ret) { |
| 504 | printf(SHETHER_NAME ": phy startup failure\n"); |
| 505 | return ret; |
| 506 | } |
| 507 | |
| 508 | ret = sh_eth_phy_regs_config(eth); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 509 | if (ret) |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 510 | return ret; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 511 | |
| 512 | sh_eth_start(eth); |
| 513 | |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 514 | return 0; |
| 515 | } |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 516 | |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 517 | #ifndef CONFIG_DM_ETH |
Marek Vasut | 7ba5262 | 2018-01-21 15:31:48 +0100 | [diff] [blame] | 518 | static int sh_eth_phy_config_legacy(struct sh_eth_dev *eth) |
| 519 | { |
Marek Vasut | 7a309cf | 2018-02-17 00:46:26 +0100 | [diff] [blame] | 520 | int ret = 0; |
| 521 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Marek Vasut | 7ba5262 | 2018-01-21 15:31:48 +0100 | [diff] [blame] | 522 | struct eth_device *dev = port_info->dev; |
| 523 | struct phy_device *phydev; |
| 524 | |
| 525 | phydev = phy_connect( |
| 526 | miiphy_get_dev_by_name(dev->name), |
| 527 | port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE); |
| 528 | port_info->phydev = phydev; |
| 529 | phy_config(phydev); |
| 530 | |
| 531 | return ret; |
| 532 | } |
| 533 | |
| 534 | static int sh_eth_send_legacy(struct eth_device *dev, void *packet, int len) |
| 535 | { |
| 536 | struct sh_eth_dev *eth = dev->priv; |
| 537 | |
| 538 | return sh_eth_send_common(eth, packet, len); |
| 539 | } |
| 540 | |
| 541 | static int sh_eth_recv_common(struct sh_eth_dev *eth) |
| 542 | { |
Marek Vasut | 7a309cf | 2018-02-17 00:46:26 +0100 | [diff] [blame] | 543 | int len = 0; |
| 544 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Marek Vasut | 7ba5262 | 2018-01-21 15:31:48 +0100 | [diff] [blame] | 545 | uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2); |
| 546 | |
| 547 | len = sh_eth_recv_start(eth); |
| 548 | if (len > 0) { |
| 549 | invalidate_cache(packet, len); |
| 550 | net_process_received_packet(packet, len); |
| 551 | sh_eth_recv_finish(eth); |
| 552 | } else |
| 553 | len = 0; |
| 554 | |
| 555 | /* Restart the receiver if disabled */ |
| 556 | if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R)) |
| 557 | sh_eth_write(port_info, EDRRR_R, EDRRR); |
| 558 | |
| 559 | return len; |
| 560 | } |
| 561 | |
| 562 | static int sh_eth_recv_legacy(struct eth_device *dev) |
| 563 | { |
| 564 | struct sh_eth_dev *eth = dev->priv; |
| 565 | |
| 566 | return sh_eth_recv_common(eth); |
| 567 | } |
| 568 | |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 569 | static int sh_eth_init_legacy(struct eth_device *dev, bd_t *bd) |
| 570 | { |
| 571 | struct sh_eth_dev *eth = dev->priv; |
| 572 | int ret; |
| 573 | |
| 574 | ret = sh_eth_init_common(eth, dev->enetaddr); |
| 575 | if (ret) |
| 576 | return ret; |
| 577 | |
| 578 | ret = sh_eth_phy_config_legacy(eth); |
| 579 | if (ret) { |
| 580 | printf(SHETHER_NAME ": phy config timeout\n"); |
| 581 | goto err_start; |
| 582 | } |
| 583 | |
| 584 | ret = sh_eth_start_common(eth); |
| 585 | if (ret) |
| 586 | goto err_start; |
| 587 | |
| 588 | return 0; |
| 589 | |
| 590 | err_start: |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 591 | sh_eth_tx_desc_free(eth); |
| 592 | sh_eth_rx_desc_free(eth); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 593 | return ret; |
| 594 | } |
| 595 | |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 596 | void sh_eth_halt_legacy(struct eth_device *dev) |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 597 | { |
| 598 | struct sh_eth_dev *eth = dev->priv; |
Nobuhiro Iwamatsu | ca36b0e | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 599 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 600 | sh_eth_stop(eth); |
| 601 | } |
| 602 | |
| 603 | int sh_eth_initialize(bd_t *bd) |
| 604 | { |
Nobuhiro Iwamatsu | 31e84df | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 605 | int ret = 0; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 606 | struct sh_eth_dev *eth = NULL; |
Nobuhiro Iwamatsu | 31e84df | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 607 | struct eth_device *dev = NULL; |
Nobuhiro Iwamatsu | ca36b0e | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 608 | struct mii_dev *mdiodev; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 609 | |
Nobuhiro Iwamatsu | 31e84df | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 610 | eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev)); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 611 | if (!eth) { |
| 612 | printf(SHETHER_NAME ": %s: malloc failed\n", __func__); |
| 613 | ret = -ENOMEM; |
| 614 | goto err; |
| 615 | } |
| 616 | |
Nobuhiro Iwamatsu | 31e84df | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 617 | dev = (struct eth_device *)malloc(sizeof(struct eth_device)); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 618 | if (!dev) { |
| 619 | printf(SHETHER_NAME ": %s: malloc failed\n", __func__); |
| 620 | ret = -ENOMEM; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 621 | goto err; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 622 | } |
Nobuhiro Iwamatsu | 31e84df | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 623 | memset(dev, 0, sizeof(struct eth_device)); |
| 624 | memset(eth, 0, sizeof(struct sh_eth_dev)); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 625 | |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 626 | eth->port = CONFIG_SH_ETHER_USE_PORT; |
| 627 | eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR; |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 628 | eth->port_info[eth->port].iobase = |
| 629 | (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 630 | |
Nobuhiro Iwamatsu | 31e84df | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 631 | dev->priv = (void *)eth; |
| 632 | dev->iobase = 0; |
Marek Vasut | c13be6a | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 633 | dev->init = sh_eth_init_legacy; |
| 634 | dev->halt = sh_eth_halt_legacy; |
Marek Vasut | 044eb2d | 2018-01-21 14:27:51 +0100 | [diff] [blame] | 635 | dev->send = sh_eth_send_legacy; |
| 636 | dev->recv = sh_eth_recv_legacy; |
Nobuhiro Iwamatsu | 31e84df | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 637 | eth->port_info[eth->port].dev = dev; |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 638 | |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 639 | strcpy(dev->name, SHETHER_NAME); |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 640 | |
Nobuhiro Iwamatsu | 31e84df | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 641 | /* Register Device to EtherNet subsystem */ |
| 642 | eth_register(dev); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 643 | |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 644 | bb_miiphy_buses[0].priv = eth; |
Nobuhiro Iwamatsu | ca36b0e | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 645 | mdiodev = mdio_alloc(); |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 646 | if (!mdiodev) |
| 647 | return -ENOMEM; |
| 648 | strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN); |
| 649 | mdiodev->read = bb_miiphy_read; |
| 650 | mdiodev->write = bb_miiphy_write; |
| 651 | |
Nobuhiro Iwamatsu | ca36b0e | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 652 | ret = mdio_register(mdiodev); |
| 653 | if (ret < 0) |
| 654 | return ret; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 655 | |
Simon Glass | 399a9ce | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 656 | if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr)) |
Mike Frysinger | a86bf13 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 657 | puts("Please set MAC address\n"); |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 658 | |
| 659 | return ret; |
| 660 | |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 661 | err: |
Nobuhiro Iwamatsu | d8f5d50 | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 662 | if (dev) |
| 663 | free(dev); |
| 664 | |
| 665 | if (eth) |
| 666 | free(eth); |
| 667 | |
| 668 | printf(SHETHER_NAME ": Failed\n"); |
| 669 | return ret; |
Nobuhiro Iwamatsu | 240b723 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 670 | } |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 671 | |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 672 | #else /* CONFIG_DM_ETH */ |
| 673 | |
| 674 | struct sh_ether_priv { |
| 675 | struct sh_eth_dev shdev; |
| 676 | |
| 677 | struct mii_dev *bus; |
Marek Vasut | 63ab72c | 2018-02-17 00:57:49 +0100 | [diff] [blame] | 678 | phys_addr_t iobase; |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 679 | struct clk clk; |
| 680 | struct gpio_desc reset_gpio; |
| 681 | }; |
| 682 | |
| 683 | static int sh_ether_send(struct udevice *dev, void *packet, int len) |
| 684 | { |
| 685 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 686 | struct sh_eth_dev *eth = &priv->shdev; |
| 687 | |
| 688 | return sh_eth_send_common(eth, packet, len); |
| 689 | } |
| 690 | |
| 691 | static int sh_ether_recv(struct udevice *dev, int flags, uchar **packetp) |
| 692 | { |
| 693 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 694 | struct sh_eth_dev *eth = &priv->shdev; |
| 695 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 696 | uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2); |
| 697 | int len; |
| 698 | |
| 699 | len = sh_eth_recv_start(eth); |
| 700 | if (len > 0) { |
| 701 | invalidate_cache(packet, len); |
| 702 | *packetp = packet; |
| 703 | |
| 704 | return len; |
| 705 | } else { |
| 706 | len = 0; |
| 707 | |
| 708 | /* Restart the receiver if disabled */ |
| 709 | if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R)) |
| 710 | sh_eth_write(port_info, EDRRR_R, EDRRR); |
| 711 | |
| 712 | return -EAGAIN; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | static int sh_ether_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 717 | { |
| 718 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 719 | struct sh_eth_dev *eth = &priv->shdev; |
| 720 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 721 | |
| 722 | sh_eth_recv_finish(eth); |
| 723 | |
| 724 | /* Restart the receiver if disabled */ |
| 725 | if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R)) |
| 726 | sh_eth_write(port_info, EDRRR_R, EDRRR); |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static int sh_ether_write_hwaddr(struct udevice *dev) |
| 732 | { |
| 733 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 734 | struct sh_eth_dev *eth = &priv->shdev; |
| 735 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 736 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 737 | |
| 738 | sh_eth_write_hwaddr(port_info, pdata->enetaddr); |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | static int sh_eth_phy_config(struct udevice *dev) |
| 744 | { |
| 745 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 746 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 747 | struct sh_eth_dev *eth = &priv->shdev; |
Marek Vasut | 7a309cf | 2018-02-17 00:46:26 +0100 | [diff] [blame] | 748 | int ret = 0; |
| 749 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 750 | struct phy_device *phydev; |
| 751 | int mask = 0xffffffff; |
| 752 | |
| 753 | phydev = phy_find_by_mask(priv->bus, mask, pdata->phy_interface); |
| 754 | if (!phydev) |
| 755 | return -ENODEV; |
| 756 | |
| 757 | phy_connect_dev(phydev, dev); |
| 758 | |
| 759 | port_info->phydev = phydev; |
| 760 | phy_config(phydev); |
| 761 | |
| 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | static int sh_ether_start(struct udevice *dev) |
| 766 | { |
| 767 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 768 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 769 | struct sh_eth_dev *eth = &priv->shdev; |
| 770 | int ret; |
| 771 | |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 772 | ret = sh_eth_init_common(eth, pdata->enetaddr); |
| 773 | if (ret) |
Marek Vasut | f6cf4ba | 2019-03-30 07:22:09 +0100 | [diff] [blame] | 774 | return ret; |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 775 | |
| 776 | ret = sh_eth_start_common(eth); |
| 777 | if (ret) |
| 778 | goto err_start; |
| 779 | |
| 780 | return 0; |
| 781 | |
| 782 | err_start: |
| 783 | sh_eth_tx_desc_free(eth); |
| 784 | sh_eth_rx_desc_free(eth); |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 785 | return ret; |
| 786 | } |
| 787 | |
| 788 | static void sh_ether_stop(struct udevice *dev) |
| 789 | { |
| 790 | struct sh_ether_priv *priv = dev_get_priv(dev); |
Marek Vasut | f6cf4ba | 2019-03-30 07:22:09 +0100 | [diff] [blame] | 791 | struct sh_eth_dev *eth = &priv->shdev; |
| 792 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 793 | |
Marek Vasut | f6cf4ba | 2019-03-30 07:22:09 +0100 | [diff] [blame] | 794 | phy_shutdown(port_info->phydev); |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 795 | sh_eth_stop(&priv->shdev); |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | static int sh_ether_probe(struct udevice *udev) |
| 799 | { |
| 800 | struct eth_pdata *pdata = dev_get_platdata(udev); |
| 801 | struct sh_ether_priv *priv = dev_get_priv(udev); |
| 802 | struct sh_eth_dev *eth = &priv->shdev; |
Marek Vasut | 27e0633 | 2018-06-18 04:03:01 +0200 | [diff] [blame] | 803 | struct ofnode_phandle_args phandle_args; |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 804 | struct mii_dev *mdiodev; |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 805 | int ret; |
| 806 | |
Marek Vasut | 63ab72c | 2018-02-17 00:57:49 +0100 | [diff] [blame] | 807 | priv->iobase = pdata->iobase; |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 808 | |
Marek Vasut | 77f69f8 | 2019-05-02 00:03:26 +0200 | [diff] [blame^] | 809 | #if CONFIG_IS_ENABLED(CLK) |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 810 | ret = clk_get_by_index(udev, 0, &priv->clk); |
| 811 | if (ret < 0) |
Marek Vasut | 63ab72c | 2018-02-17 00:57:49 +0100 | [diff] [blame] | 812 | return ret; |
Marek Vasut | 77f69f8 | 2019-05-02 00:03:26 +0200 | [diff] [blame^] | 813 | #endif |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 814 | |
Marek Vasut | 27e0633 | 2018-06-18 04:03:01 +0200 | [diff] [blame] | 815 | ret = dev_read_phandle_with_args(udev, "phy-handle", NULL, 0, 0, &phandle_args); |
| 816 | if (!ret) { |
| 817 | gpio_request_by_name_nodev(phandle_args.node, "reset-gpios", 0, |
| 818 | &priv->reset_gpio, GPIOD_IS_OUT); |
| 819 | } |
| 820 | |
| 821 | if (!dm_gpio_is_valid(&priv->reset_gpio)) { |
| 822 | gpio_request_by_name(udev, "reset-gpios", 0, &priv->reset_gpio, |
| 823 | GPIOD_IS_OUT); |
| 824 | } |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 825 | |
| 826 | mdiodev = mdio_alloc(); |
| 827 | if (!mdiodev) { |
| 828 | ret = -ENOMEM; |
Marek Vasut | 63ab72c | 2018-02-17 00:57:49 +0100 | [diff] [blame] | 829 | return ret; |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | mdiodev->read = bb_miiphy_read; |
| 833 | mdiodev->write = bb_miiphy_write; |
| 834 | bb_miiphy_buses[0].priv = eth; |
| 835 | snprintf(mdiodev->name, sizeof(mdiodev->name), udev->name); |
| 836 | |
| 837 | ret = mdio_register(mdiodev); |
| 838 | if (ret < 0) |
| 839 | goto err_mdio_register; |
| 840 | |
| 841 | priv->bus = miiphy_get_dev_by_name(udev->name); |
| 842 | |
| 843 | eth->port = CONFIG_SH_ETHER_USE_PORT; |
| 844 | eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR; |
| 845 | eth->port_info[eth->port].iobase = |
| 846 | (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port); |
| 847 | |
Marek Vasut | 77f69f8 | 2019-05-02 00:03:26 +0200 | [diff] [blame^] | 848 | #if CONFIG_IS_ENABLED(CLK) |
Marek Vasut | f6cf4ba | 2019-03-30 07:22:09 +0100 | [diff] [blame] | 849 | ret = clk_enable(&priv->clk); |
| 850 | if (ret) |
| 851 | goto err_mdio_register; |
Marek Vasut | 77f69f8 | 2019-05-02 00:03:26 +0200 | [diff] [blame^] | 852 | #endif |
Marek Vasut | f6cf4ba | 2019-03-30 07:22:09 +0100 | [diff] [blame] | 853 | |
| 854 | ret = sh_eth_phy_config(udev); |
| 855 | if (ret) { |
| 856 | printf(SHETHER_NAME ": phy config timeout\n"); |
| 857 | goto err_phy_config; |
| 858 | } |
| 859 | |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 860 | return 0; |
| 861 | |
Marek Vasut | f6cf4ba | 2019-03-30 07:22:09 +0100 | [diff] [blame] | 862 | err_phy_config: |
Marek Vasut | 77f69f8 | 2019-05-02 00:03:26 +0200 | [diff] [blame^] | 863 | #if CONFIG_IS_ENABLED(CLK) |
Marek Vasut | f6cf4ba | 2019-03-30 07:22:09 +0100 | [diff] [blame] | 864 | clk_disable(&priv->clk); |
Marek Vasut | 77f69f8 | 2019-05-02 00:03:26 +0200 | [diff] [blame^] | 865 | #endif |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 866 | err_mdio_register: |
| 867 | mdio_free(mdiodev); |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 868 | return ret; |
| 869 | } |
| 870 | |
| 871 | static int sh_ether_remove(struct udevice *udev) |
| 872 | { |
| 873 | struct sh_ether_priv *priv = dev_get_priv(udev); |
| 874 | struct sh_eth_dev *eth = &priv->shdev; |
| 875 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 876 | |
Marek Vasut | 77f69f8 | 2019-05-02 00:03:26 +0200 | [diff] [blame^] | 877 | #if CONFIG_IS_ENABLED(CLK) |
Marek Vasut | f6cf4ba | 2019-03-30 07:22:09 +0100 | [diff] [blame] | 878 | clk_disable(&priv->clk); |
Marek Vasut | 77f69f8 | 2019-05-02 00:03:26 +0200 | [diff] [blame^] | 879 | #endif |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 880 | free(port_info->phydev); |
| 881 | mdio_unregister(priv->bus); |
| 882 | mdio_free(priv->bus); |
| 883 | |
| 884 | if (dm_gpio_is_valid(&priv->reset_gpio)) |
| 885 | dm_gpio_free(udev, &priv->reset_gpio); |
| 886 | |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static const struct eth_ops sh_ether_ops = { |
| 891 | .start = sh_ether_start, |
| 892 | .send = sh_ether_send, |
| 893 | .recv = sh_ether_recv, |
| 894 | .free_pkt = sh_ether_free_pkt, |
| 895 | .stop = sh_ether_stop, |
| 896 | .write_hwaddr = sh_ether_write_hwaddr, |
| 897 | }; |
| 898 | |
| 899 | int sh_ether_ofdata_to_platdata(struct udevice *dev) |
| 900 | { |
| 901 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 902 | const char *phy_mode; |
| 903 | const fdt32_t *cell; |
| 904 | int ret = 0; |
| 905 | |
| 906 | pdata->iobase = devfdt_get_addr(dev); |
| 907 | pdata->phy_interface = -1; |
| 908 | phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode", |
| 909 | NULL); |
| 910 | if (phy_mode) |
| 911 | pdata->phy_interface = phy_get_interface_by_name(phy_mode); |
| 912 | if (pdata->phy_interface == -1) { |
| 913 | debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); |
| 914 | return -EINVAL; |
| 915 | } |
| 916 | |
| 917 | pdata->max_speed = 1000; |
| 918 | cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL); |
| 919 | if (cell) |
| 920 | pdata->max_speed = fdt32_to_cpu(*cell); |
| 921 | |
| 922 | sprintf(bb_miiphy_buses[0].name, dev->name); |
| 923 | |
| 924 | return ret; |
| 925 | } |
| 926 | |
| 927 | static const struct udevice_id sh_ether_ids[] = { |
Marek Vasut | 77f69f8 | 2019-05-02 00:03:26 +0200 | [diff] [blame^] | 928 | { .compatible = "renesas,ether-r7s72100" }, |
Marek Vasut | 337ab3b | 2018-04-12 15:23:46 +0200 | [diff] [blame] | 929 | { .compatible = "renesas,ether-r8a7790" }, |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 930 | { .compatible = "renesas,ether-r8a7791" }, |
Marek Vasut | 337ab3b | 2018-04-12 15:23:46 +0200 | [diff] [blame] | 931 | { .compatible = "renesas,ether-r8a7793" }, |
| 932 | { .compatible = "renesas,ether-r8a7794" }, |
Marek Vasut | 020d394 | 2018-01-19 18:57:17 +0100 | [diff] [blame] | 933 | { } |
| 934 | }; |
| 935 | |
| 936 | U_BOOT_DRIVER(eth_sh_ether) = { |
| 937 | .name = "sh_ether", |
| 938 | .id = UCLASS_ETH, |
| 939 | .of_match = sh_ether_ids, |
| 940 | .ofdata_to_platdata = sh_ether_ofdata_to_platdata, |
| 941 | .probe = sh_ether_probe, |
| 942 | .remove = sh_ether_remove, |
| 943 | .ops = &sh_ether_ops, |
| 944 | .priv_auto_alloc_size = sizeof(struct sh_ether_priv), |
| 945 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 946 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 947 | }; |
| 948 | #endif |
| 949 | |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 950 | /******* for bb_miiphy *******/ |
| 951 | static int sh_eth_bb_init(struct bb_miiphy_bus *bus) |
| 952 | { |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus) |
| 957 | { |
| 958 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 959 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 960 | |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 961 | sh_eth_write(port_info, sh_eth_read(port_info, PIR) | PIR_MMD, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 962 | |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus) |
| 967 | { |
| 968 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 969 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 970 | |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 971 | sh_eth_write(port_info, sh_eth_read(port_info, PIR) & ~PIR_MMD, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v) |
| 977 | { |
| 978 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 979 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 980 | |
| 981 | if (v) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 982 | sh_eth_write(port_info, |
| 983 | sh_eth_read(port_info, PIR) | PIR_MDO, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 984 | else |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 985 | sh_eth_write(port_info, |
| 986 | sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v) |
| 992 | { |
| 993 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 994 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 995 | |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 996 | *v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 997 | |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v) |
| 1002 | { |
| 1003 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 1004 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 1005 | |
| 1006 | if (v) |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 1007 | sh_eth_write(port_info, |
| 1008 | sh_eth_read(port_info, PIR) | PIR_MDC, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 1009 | else |
Nobuhiro Iwamatsu | ec921f1 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 1010 | sh_eth_write(port_info, |
| 1011 | sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR); |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | static int sh_eth_bb_delay(struct bb_miiphy_bus *bus) |
| 1017 | { |
| 1018 | udelay(10); |
| 1019 | |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
| 1023 | struct bb_miiphy_bus bb_miiphy_buses[] = { |
| 1024 | { |
| 1025 | .name = "sh_eth", |
| 1026 | .init = sh_eth_bb_init, |
| 1027 | .mdio_active = sh_eth_bb_mdio_active, |
| 1028 | .mdio_tristate = sh_eth_bb_mdio_tristate, |
| 1029 | .set_mdio = sh_eth_bb_set_mdio, |
| 1030 | .get_mdio = sh_eth_bb_get_mdio, |
| 1031 | .set_mdc = sh_eth_bb_set_mdc, |
| 1032 | .delay = sh_eth_bb_delay, |
| 1033 | } |
| 1034 | }; |
Nobuhiro Iwamatsu | ca36b0e | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 1035 | |
Yoshihiro Shimoda | 677f6cd | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 1036 | int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses); |