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