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