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