blob: d75bf5467043756f6f90c4f118efa8f1555d24f3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09002/*
Robert P. J. Day1cc0a9f2016-05-04 04:47:31 -04003 * sh_eth.c - Driver for Renesas ethernet controller.
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09004 *
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +09005 * Copyright (C) 2008, 2011 Renesas Solutions Corp.
Nobuhiro Iwamatsuf7ca1f72014-11-04 09:15:48 +09006 * Copyright (c) 2008, 2011, 2014 2014 Nobuhiro Iwamatsu
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09007 * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
Nobuhiro Iwamatsuf7ca1f72014-11-04 09:15:48 +09008 * Copyright (C) 2013, 2014 Renesas Electronics Corporation
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09009 */
10
11#include <config.h>
12#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070013#include <cpu_func.h>
Simon Glass7b51b572019-08-01 09:46:52 -060014#include <env.h>
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090015#include <malloc.h>
16#include <net.h>
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090017#include <netdev.h>
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +090018#include <miiphy.h>
Simon Glass90526e92020-05-10 11:39:56 -060019#include <asm/cache.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090020#include <linux/errno.h>
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090021#include <asm/io.h>
22
Marek Vasut31920262018-01-19 18:57:17 +010023#ifdef CONFIG_DM_ETH
24#include <clk.h>
25#include <dm.h>
26#include <linux/mii.h>
27#include <asm/gpio.h>
28#endif
29
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090030#include "sh_eth.h"
31
32#ifndef CONFIG_SH_ETHER_USE_PORT
33# error "Please define CONFIG_SH_ETHER_USE_PORT"
34#endif
35#ifndef CONFIG_SH_ETHER_PHY_ADDR
36# error "Please define CONFIG_SH_ETHER_PHY_ADDR"
37#endif
Nobuhiro Iwamatsu870cc232013-08-22 13:22:01 +090038
Trevor Woerner10015022019-05-03 09:41:00 -040039#if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && \
40 !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090041#define flush_cache_wback(addr, len) \
Marek Vasut7234a282019-07-31 14:48:17 +020042 flush_dcache_range((unsigned long)addr, \
43 (unsigned long)(addr + ALIGN(len, CONFIG_SH_ETHER_ALIGNE_SIZE)))
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090044#else
45#define flush_cache_wback(...)
46#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090047
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090048#if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
49#define invalidate_cache(addr, len) \
50 { \
Marek Vasut7234a282019-07-31 14:48:17 +020051 unsigned long line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \
52 unsigned long start, end; \
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090053 \
Marek Vasut7234a282019-07-31 14:48:17 +020054 start = (unsigned long)addr; \
55 end = start + len; \
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090056 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 Iwamatsu4ba62c72012-01-11 10:23:51 +090065#define TIMEOUT_CNT 1000
66
Marek Vasutdca221b2018-01-21 14:27:51 +010067static int sh_eth_send_common(struct sh_eth_dev *eth, void *packet, int len)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090068{
Marek Vasut3c5a7b72018-02-17 00:46:26 +010069 int ret = 0, timeout;
70 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090071
72 if (!packet || len > 0xffff) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090073 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
74 ret = -EINVAL;
75 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090076 }
77
78 /* packet must be a 4 byte boundary */
Marek Vasut7234a282019-07-31 14:48:17 +020079 if ((uintptr_t)packet & 3) {
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +090080 printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n"
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +090081 , __func__);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090082 ret = -EFAULT;
83 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090084 }
85
86 /* Update tx descriptor */
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090087 flush_cache_wback(packet, len);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090088 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 Iwamatsuf7ca1f72014-11-04 09:15:48 +090096 flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s));
97
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090098 /* Restart the transmitter if disabled */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +090099 if (!(sh_eth_read(port_info, EDTRR) & EDTRR_TRNS))
100 sh_eth_write(port_info, EDTRR_TRNS, EDTRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900101
102 /* Wait until packet is transmitted */
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900103 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +0900104 do {
105 invalidate_cache(port_info->tx_desc_cur,
106 sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900107 udelay(100);
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +0900108 } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900109
110 if (timeout < 0) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900111 printf(SHETHER_NAME ": transmit timeout\n");
112 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900113 goto err;
114 }
115
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900116 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 Iwamatsubd3980c2008-11-21 12:04:18 +0900120err:
121 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900122}
123
Marek Vasut52c15e22018-01-21 15:39:50 +0100124static int sh_eth_recv_start(struct sh_eth_dev *eth)
125{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100126 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasut52c15e22018-01-21 15:39:50 +0100127
128 /* Check if the rx descriptor is ready */
129 invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
130 if (port_info->rx_desc_cur->rd0 & RD_RACT)
131 return -EINVAL;
132
133 /* Check for errors */
134 if (port_info->rx_desc_cur->rd0 & RD_RFE)
135 return -EINVAL;
136
Marek Vasut60279b52018-02-17 00:47:38 +0100137 return port_info->rx_desc_cur->rd1 & 0xffff;
Marek Vasut52c15e22018-01-21 15:39:50 +0100138}
139
140static void sh_eth_recv_finish(struct sh_eth_dev *eth)
141{
142 struct sh_eth_info *port_info = &eth->port_info[eth->port];
143
144 /* Make current descriptor available again */
145 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
146 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
147 else
148 port_info->rx_desc_cur->rd0 = RD_RACT;
149
150 flush_cache_wback(port_info->rx_desc_cur,
151 sizeof(struct rx_desc_s));
152
153 /* Point to the next descriptor */
154 port_info->rx_desc_cur++;
155 if (port_info->rx_desc_cur >=
156 port_info->rx_desc_base + NUM_RX_DESC)
157 port_info->rx_desc_cur = port_info->rx_desc_base;
158}
159
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900160static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900161{
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900162 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900163#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900164 int ret = 0, i;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900165
166 /* Start e-dmac transmitter and receiver */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900167 sh_eth_write(port_info, EDSR_ENALL, EDSR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900168
169 /* Perform a software reset and wait for it to complete */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900170 sh_eth_write(port_info, EDMR_SRST, EDMR);
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900171 for (i = 0; i < TIMEOUT_CNT; i++) {
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900172 if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST))
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900173 break;
174 udelay(1000);
175 }
176
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900177 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900178 printf(SHETHER_NAME ": Software reset timeout\n");
179 ret = -EIO;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900180 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900181
182 return ret;
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900183#else
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900184 sh_eth_write(port_info, sh_eth_read(port_info, EDMR) | EDMR_SRST, EDMR);
Marek Vasut52627672018-02-17 00:57:49 +0100185 mdelay(3);
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900186 sh_eth_write(port_info,
187 sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900188
189 return 0;
190#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900191}
192
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900193static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900194{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100195 int i, ret = 0;
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900196 u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s);
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100197 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900198 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900199
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900200 /*
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900201 * Allocate rx descriptors. They must be aligned to size of struct
202 * tx_desc_s.
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900203 */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900204 port_info->tx_desc_alloc =
205 memalign(sizeof(struct tx_desc_s), alloc_desc_size);
206 if (!port_info->tx_desc_alloc) {
207 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900208 ret = -ENOMEM;
209 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900210 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900211
Nobuhiro Iwamatsuaae5d232017-12-01 13:56:08 +0900212 flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size);
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900213
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900214 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900215 port_info->tx_desc_base =
Marek Vasut7234a282019-07-31 14:48:17 +0200216 (struct tx_desc_s *)ADDR_TO_P2((uintptr_t)port_info->tx_desc_alloc);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900217 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
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900231 /*
232 * Point the controller to the tx descriptor list. Must use physical
233 * addresses
234 */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900235 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900236#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900237 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
238 sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR);
239 sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900240#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900241
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900242err:
243 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900244}
245
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900246static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900247{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100248 int i, ret = 0;
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900249 u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s);
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100250 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900251 struct rx_desc_s *cur_rx_desc;
252 u8 *rx_buf;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900253
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900254 /*
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900255 * Allocate rx descriptors. They must be aligned to size of struct
256 * rx_desc_s.
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900257 */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900258 port_info->rx_desc_alloc =
259 memalign(sizeof(struct rx_desc_s), alloc_desc_size);
260 if (!port_info->rx_desc_alloc) {
261 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900262 ret = -ENOMEM;
263 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900264 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900265
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900266 flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size);
267
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900268 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900269 port_info->rx_desc_base =
Marek Vasut7234a282019-07-31 14:48:17 +0200270 (struct rx_desc_s *)ADDR_TO_P2((uintptr_t)port_info->rx_desc_alloc);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900271
272 port_info->rx_desc_cur = port_info->rx_desc_base;
273
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900274 /*
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900275 * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes
276 * aligned and in P2 area.
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900277 */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900278 port_info->rx_buf_alloc =
279 memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE);
280 if (!port_info->rx_buf_alloc) {
281 printf(SHETHER_NAME ": alloc failed\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900282 ret = -ENOMEM;
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900283 goto err_buf_alloc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900284 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900285
Marek Vasut7234a282019-07-31 14:48:17 +0200286 port_info->rx_buf_base = (u8 *)ADDR_TO_P2((uintptr_t)port_info->rx_buf_alloc);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900287
288 /* Initialize all descriptors */
289 for (cur_rx_desc = port_info->rx_desc_base,
290 rx_buf = port_info->rx_buf_base, i = 0;
291 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
292 cur_rx_desc->rd0 = RD_RACT;
293 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900294 cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900295 }
296
297 /* Mark the end of the descriptors */
298 cur_rx_desc--;
299 cur_rx_desc->rd0 |= RD_RDLE;
300
301 /* Point the controller to the rx descriptor list */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900302 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900303#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900304 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
305 sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR);
306 sh_eth_write(port_info, RDFFR_RDLF, RDFFR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900307#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900308
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900309 return ret;
310
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900311err_buf_alloc:
312 free(port_info->rx_desc_alloc);
313 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900314
315err:
316 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900317}
318
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900319static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900320{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100321 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900322
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900323 if (port_info->tx_desc_alloc) {
324 free(port_info->tx_desc_alloc);
325 port_info->tx_desc_alloc = NULL;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900326 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900327}
328
329static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
330{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100331 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900332
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900333 if (port_info->rx_desc_alloc) {
334 free(port_info->rx_desc_alloc);
335 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900336 }
337
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900338 if (port_info->rx_buf_alloc) {
339 free(port_info->rx_buf_alloc);
340 port_info->rx_buf_alloc = NULL;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900341 }
342}
343
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900344static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900345{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900346 int ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900347
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900348 ret = sh_eth_tx_desc_init(eth);
349 if (ret)
350 goto err_tx_init;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900351
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900352 ret = sh_eth_rx_desc_init(eth);
353 if (ret)
354 goto err_rx_init;
355
356 return ret;
357err_rx_init:
358 sh_eth_tx_desc_free(eth);
359
360err_tx_init:
361 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900362}
363
Marek Vasut68ac92e2018-01-21 14:55:44 +0100364static void sh_eth_write_hwaddr(struct sh_eth_info *port_info,
365 unsigned char *mac)
366{
367 u32 val;
368
369 val = (mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3];
370 sh_eth_write(port_info, val, MAHR);
371
372 val = (mac[4] << 8) | mac[5];
373 sh_eth_write(port_info, val, MALR);
374}
375
Marek Vasut013af642018-01-21 15:10:21 +0100376static void sh_eth_mac_regs_config(struct sh_eth_dev *eth, unsigned char *mac)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900377{
Marek Vasut013af642018-01-21 15:10:21 +0100378 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasut46c33162019-07-31 12:58:06 +0200379 unsigned long edmr;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900380
381 /* Configure e-dmac registers */
Marek Vasut46c33162019-07-31 12:58:06 +0200382 edmr = sh_eth_read(port_info, EDMR);
383 edmr &= ~EMDR_DESC_R;
384 edmr |= EMDR_DESC | EDMR_EL;
385#if defined(CONFIG_R8A77980)
386 edmr |= EDMR_NBST;
387#endif
388 sh_eth_write(port_info, edmr, EDMR);
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900389
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900390 sh_eth_write(port_info, 0, EESIPR);
391 sh_eth_write(port_info, 0, TRSCER);
392 sh_eth_write(port_info, 0, TFTR);
393 sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
394 sh_eth_write(port_info, RMCR_RST, RMCR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900395#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900396 sh_eth_write(port_info, 0, RPADIR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900397#endif
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900398 sh_eth_write(port_info, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900399
400 /* Configure e-mac registers */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900401 sh_eth_write(port_info, 0, ECSIPR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900402
403 /* Set Mac address */
Marek Vasut013af642018-01-21 15:10:21 +0100404 sh_eth_write_hwaddr(port_info, mac);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900405
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900406 sh_eth_write(port_info, RFLR_RFL_MIN, RFLR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000407#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900408 sh_eth_write(port_info, 0, PIPR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900409#endif
410#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900411 sh_eth_write(port_info, APR_AP, APR);
412 sh_eth_write(port_info, MPR_MP, MPR);
413 sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER);
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900414#endif
415
Nobuhiro Iwamatsudcd5a592012-08-02 22:08:40 +0000416#if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900417 sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
Marek Vasut46c33162019-07-31 12:58:06 +0200418#elif defined(CONFIG_RCAR_GEN2) || defined(CONFIG_R8A77980)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900419 sh_eth_write(port_info, sh_eth_read(port_info, RMIIMR) | 0x1, RMIIMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000420#endif
Marek Vasut013af642018-01-21 15:10:21 +0100421}
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900422
Marek Vasut013af642018-01-21 15:10:21 +0100423static int sh_eth_phy_regs_config(struct sh_eth_dev *eth)
424{
425 struct sh_eth_info *port_info = &eth->port_info[eth->port];
426 struct phy_device *phy = port_info->phydev;
427 int ret = 0;
428 u32 val = 0;
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900429
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900430 /* Set the transfer speed */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900431 if (phy->speed == 100) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900432 printf(SHETHER_NAME ": 100Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000433#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900434 sh_eth_write(port_info, GECMR_100B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000435#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900436 sh_eth_write(port_info, 1, RTRATE);
Marek Vasut46c33162019-07-31 12:58:06 +0200437#elif defined(CONFIG_RCAR_GEN2) || defined(CONFIG_R8A77980)
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900438 val = ECMR_RTM;
439#endif
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900440 } else if (phy->speed == 10) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900441 printf(SHETHER_NAME ": 10Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000442#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900443 sh_eth_write(port_info, GECMR_10B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000444#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900445 sh_eth_write(port_info, 0, RTRATE);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900446#endif
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900447 }
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000448#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000449 else if (phy->speed == 1000) {
450 printf(SHETHER_NAME ": 1000Base/");
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900451 sh_eth_write(port_info, GECMR_1000B, GECMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000452 }
453#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900454
455 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900456 if (phy->duplex) {
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900457 printf("Full\n");
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900458 sh_eth_write(port_info,
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900459 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM),
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000460 ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900461 } else {
462 printf("Half\n");
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900463 sh_eth_write(port_info,
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900464 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE),
465 ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900466 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900467
468 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900469}
470
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900471static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900472{
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900473 struct sh_eth_info *port_info = &eth->port_info[eth->port];
474
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900475 /*
476 * Enable the e-dmac receiver only. The transmitter will be enabled when
477 * we have something to transmit
478 */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900479 sh_eth_write(port_info, EDRRR_R, EDRRR);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900480}
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900481
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900482static void sh_eth_stop(struct sh_eth_dev *eth)
483{
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900484 struct sh_eth_info *port_info = &eth->port_info[eth->port];
485
486 sh_eth_write(port_info, ~EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900487}
488
Marek Vasut013af642018-01-21 15:10:21 +0100489static int sh_eth_init_common(struct sh_eth_dev *eth, unsigned char *mac)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900490{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900491 int ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900492
493 ret = sh_eth_reset(eth);
494 if (ret)
Marek Vasut013af642018-01-21 15:10:21 +0100495 return ret;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900496
497 ret = sh_eth_desc_init(eth);
498 if (ret)
Marek Vasut013af642018-01-21 15:10:21 +0100499 return ret;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900500
Marek Vasut013af642018-01-21 15:10:21 +0100501 sh_eth_mac_regs_config(eth, mac);
502
503 return 0;
504}
505
506static int sh_eth_start_common(struct sh_eth_dev *eth)
507{
508 struct sh_eth_info *port_info = &eth->port_info[eth->port];
509 int ret;
510
511 ret = phy_startup(port_info->phydev);
512 if (ret) {
513 printf(SHETHER_NAME ": phy startup failure\n");
514 return ret;
515 }
516
517 ret = sh_eth_phy_regs_config(eth);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900518 if (ret)
Marek Vasut013af642018-01-21 15:10:21 +0100519 return ret;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900520
521 sh_eth_start(eth);
522
Marek Vasut013af642018-01-21 15:10:21 +0100523 return 0;
524}
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900525
Marek Vasut31920262018-01-19 18:57:17 +0100526#ifndef CONFIG_DM_ETH
Marek Vasuta2207842018-01-21 15:31:48 +0100527static int sh_eth_phy_config_legacy(struct sh_eth_dev *eth)
528{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100529 int ret = 0;
530 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasuta2207842018-01-21 15:31:48 +0100531 struct eth_device *dev = port_info->dev;
532 struct phy_device *phydev;
533
534 phydev = phy_connect(
535 miiphy_get_dev_by_name(dev->name),
536 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
537 port_info->phydev = phydev;
538 phy_config(phydev);
539
540 return ret;
541}
542
543static int sh_eth_send_legacy(struct eth_device *dev, void *packet, int len)
544{
545 struct sh_eth_dev *eth = dev->priv;
546
547 return sh_eth_send_common(eth, packet, len);
548}
549
550static int sh_eth_recv_common(struct sh_eth_dev *eth)
551{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100552 int len = 0;
553 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasuta2207842018-01-21 15:31:48 +0100554 uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2);
555
556 len = sh_eth_recv_start(eth);
557 if (len > 0) {
558 invalidate_cache(packet, len);
559 net_process_received_packet(packet, len);
560 sh_eth_recv_finish(eth);
561 } else
562 len = 0;
563
564 /* Restart the receiver if disabled */
565 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
566 sh_eth_write(port_info, EDRRR_R, EDRRR);
567
568 return len;
569}
570
571static int sh_eth_recv_legacy(struct eth_device *dev)
572{
573 struct sh_eth_dev *eth = dev->priv;
574
575 return sh_eth_recv_common(eth);
576}
577
Marek Vasut013af642018-01-21 15:10:21 +0100578static int sh_eth_init_legacy(struct eth_device *dev, bd_t *bd)
579{
580 struct sh_eth_dev *eth = dev->priv;
581 int ret;
582
583 ret = sh_eth_init_common(eth, dev->enetaddr);
584 if (ret)
585 return ret;
586
587 ret = sh_eth_phy_config_legacy(eth);
588 if (ret) {
589 printf(SHETHER_NAME ": phy config timeout\n");
590 goto err_start;
591 }
592
593 ret = sh_eth_start_common(eth);
594 if (ret)
595 goto err_start;
596
597 return 0;
598
599err_start:
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900600 sh_eth_tx_desc_free(eth);
601 sh_eth_rx_desc_free(eth);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900602 return ret;
603}
604
Marek Vasut013af642018-01-21 15:10:21 +0100605void sh_eth_halt_legacy(struct eth_device *dev)
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900606{
607 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900608
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900609 sh_eth_stop(eth);
610}
611
612int sh_eth_initialize(bd_t *bd)
613{
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900614 int ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900615 struct sh_eth_dev *eth = NULL;
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900616 struct eth_device *dev = NULL;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900617 struct mii_dev *mdiodev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900618
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900619 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900620 if (!eth) {
621 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
622 ret = -ENOMEM;
623 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900624 }
625
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900626 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900627 if (!dev) {
628 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
629 ret = -ENOMEM;
630 goto err;
631 }
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900632 memset(dev, 0, sizeof(struct eth_device));
633 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900634
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900635 eth->port = CONFIG_SH_ETHER_USE_PORT;
636 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900637 eth->port_info[eth->port].iobase =
638 (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900639
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900640 dev->priv = (void *)eth;
641 dev->iobase = 0;
Marek Vasut013af642018-01-21 15:10:21 +0100642 dev->init = sh_eth_init_legacy;
643 dev->halt = sh_eth_halt_legacy;
Marek Vasutdca221b2018-01-21 14:27:51 +0100644 dev->send = sh_eth_send_legacy;
645 dev->recv = sh_eth_recv_legacy;
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900646 eth->port_info[eth->port].dev = dev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900647
Ben Whitten192bc692015-12-30 13:05:58 +0000648 strcpy(dev->name, SHETHER_NAME);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900649
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900650 /* Register Device to EtherNet subsystem */
651 eth_register(dev);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900652
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900653 bb_miiphy_buses[0].priv = eth;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900654 mdiodev = mdio_alloc();
Joe Hershberger5a49f172016-08-08 11:28:38 -0500655 if (!mdiodev)
656 return -ENOMEM;
657 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
658 mdiodev->read = bb_miiphy_read;
659 mdiodev->write = bb_miiphy_write;
660
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900661 ret = mdio_register(mdiodev);
662 if (ret < 0)
663 return ret;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900664
Simon Glass35affd72017-08-03 12:22:14 -0600665 if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr))
Mike Frysingerc527ce92009-02-11 19:14:09 -0500666 puts("Please set MAC address\n");
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900667
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900668 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900669
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900670err:
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900671 if (dev)
672 free(dev);
673
674 if (eth)
675 free(eth);
676
677 printf(SHETHER_NAME ": Failed\n");
678 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900679}
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900680
Marek Vasut31920262018-01-19 18:57:17 +0100681#else /* CONFIG_DM_ETH */
682
683struct sh_ether_priv {
684 struct sh_eth_dev shdev;
685
686 struct mii_dev *bus;
Marek Vasut5abcbd72018-02-17 00:57:49 +0100687 phys_addr_t iobase;
Marek Vasut31920262018-01-19 18:57:17 +0100688 struct clk clk;
689 struct gpio_desc reset_gpio;
690};
691
692static int sh_ether_send(struct udevice *dev, void *packet, int len)
693{
694 struct sh_ether_priv *priv = dev_get_priv(dev);
695 struct sh_eth_dev *eth = &priv->shdev;
696
697 return sh_eth_send_common(eth, packet, len);
698}
699
700static int sh_ether_recv(struct udevice *dev, int flags, uchar **packetp)
701{
702 struct sh_ether_priv *priv = dev_get_priv(dev);
703 struct sh_eth_dev *eth = &priv->shdev;
704 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasut7234a282019-07-31 14:48:17 +0200705 uchar *packet = (uchar *)ADDR_TO_P2((uintptr_t)port_info->rx_desc_cur->rd2);
Marek Vasut31920262018-01-19 18:57:17 +0100706 int len;
707
708 len = sh_eth_recv_start(eth);
709 if (len > 0) {
710 invalidate_cache(packet, len);
711 *packetp = packet;
712
713 return len;
714 } else {
715 len = 0;
716
717 /* Restart the receiver if disabled */
718 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
719 sh_eth_write(port_info, EDRRR_R, EDRRR);
720
721 return -EAGAIN;
722 }
723}
724
725static int sh_ether_free_pkt(struct udevice *dev, uchar *packet, int length)
726{
727 struct sh_ether_priv *priv = dev_get_priv(dev);
728 struct sh_eth_dev *eth = &priv->shdev;
729 struct sh_eth_info *port_info = &eth->port_info[eth->port];
730
731 sh_eth_recv_finish(eth);
732
733 /* Restart the receiver if disabled */
734 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
735 sh_eth_write(port_info, EDRRR_R, EDRRR);
736
737 return 0;
738}
739
740static int sh_ether_write_hwaddr(struct udevice *dev)
741{
742 struct sh_ether_priv *priv = dev_get_priv(dev);
743 struct sh_eth_dev *eth = &priv->shdev;
744 struct sh_eth_info *port_info = &eth->port_info[eth->port];
745 struct eth_pdata *pdata = dev_get_platdata(dev);
746
747 sh_eth_write_hwaddr(port_info, pdata->enetaddr);
748
749 return 0;
750}
751
752static int sh_eth_phy_config(struct udevice *dev)
753{
754 struct sh_ether_priv *priv = dev_get_priv(dev);
755 struct eth_pdata *pdata = dev_get_platdata(dev);
756 struct sh_eth_dev *eth = &priv->shdev;
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100757 int ret = 0;
758 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasut31920262018-01-19 18:57:17 +0100759 struct phy_device *phydev;
760 int mask = 0xffffffff;
761
762 phydev = phy_find_by_mask(priv->bus, mask, pdata->phy_interface);
763 if (!phydev)
764 return -ENODEV;
765
766 phy_connect_dev(phydev, dev);
767
768 port_info->phydev = phydev;
769 phy_config(phydev);
770
771 return ret;
772}
773
774static int sh_ether_start(struct udevice *dev)
775{
776 struct sh_ether_priv *priv = dev_get_priv(dev);
777 struct eth_pdata *pdata = dev_get_platdata(dev);
778 struct sh_eth_dev *eth = &priv->shdev;
779 int ret;
780
Marek Vasut31920262018-01-19 18:57:17 +0100781 ret = sh_eth_init_common(eth, pdata->enetaddr);
782 if (ret)
Marek Vasut4a45e932019-03-30 07:22:09 +0100783 return ret;
Marek Vasut31920262018-01-19 18:57:17 +0100784
785 ret = sh_eth_start_common(eth);
786 if (ret)
787 goto err_start;
788
789 return 0;
790
791err_start:
792 sh_eth_tx_desc_free(eth);
793 sh_eth_rx_desc_free(eth);
Marek Vasut31920262018-01-19 18:57:17 +0100794 return ret;
795}
796
797static void sh_ether_stop(struct udevice *dev)
798{
799 struct sh_ether_priv *priv = dev_get_priv(dev);
Marek Vasut4a45e932019-03-30 07:22:09 +0100800 struct sh_eth_dev *eth = &priv->shdev;
801 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasut31920262018-01-19 18:57:17 +0100802
Marek Vasut4a45e932019-03-30 07:22:09 +0100803 phy_shutdown(port_info->phydev);
Marek Vasut31920262018-01-19 18:57:17 +0100804 sh_eth_stop(&priv->shdev);
Marek Vasut31920262018-01-19 18:57:17 +0100805}
806
807static int sh_ether_probe(struct udevice *udev)
808{
809 struct eth_pdata *pdata = dev_get_platdata(udev);
810 struct sh_ether_priv *priv = dev_get_priv(udev);
811 struct sh_eth_dev *eth = &priv->shdev;
Marek Vasut159b3292018-06-18 04:03:01 +0200812 struct ofnode_phandle_args phandle_args;
Marek Vasut31920262018-01-19 18:57:17 +0100813 struct mii_dev *mdiodev;
Marek Vasut31920262018-01-19 18:57:17 +0100814 int ret;
815
Marek Vasut5abcbd72018-02-17 00:57:49 +0100816 priv->iobase = pdata->iobase;
Marek Vasut31920262018-01-19 18:57:17 +0100817
Marek Vasut24b32472019-05-02 00:03:26 +0200818#if CONFIG_IS_ENABLED(CLK)
Marek Vasut31920262018-01-19 18:57:17 +0100819 ret = clk_get_by_index(udev, 0, &priv->clk);
820 if (ret < 0)
Marek Vasut5abcbd72018-02-17 00:57:49 +0100821 return ret;
Marek Vasut24b32472019-05-02 00:03:26 +0200822#endif
Marek Vasut31920262018-01-19 18:57:17 +0100823
Marek Vasut159b3292018-06-18 04:03:01 +0200824 ret = dev_read_phandle_with_args(udev, "phy-handle", NULL, 0, 0, &phandle_args);
825 if (!ret) {
826 gpio_request_by_name_nodev(phandle_args.node, "reset-gpios", 0,
827 &priv->reset_gpio, GPIOD_IS_OUT);
828 }
829
830 if (!dm_gpio_is_valid(&priv->reset_gpio)) {
831 gpio_request_by_name(udev, "reset-gpios", 0, &priv->reset_gpio,
832 GPIOD_IS_OUT);
833 }
Marek Vasut31920262018-01-19 18:57:17 +0100834
835 mdiodev = mdio_alloc();
836 if (!mdiodev) {
837 ret = -ENOMEM;
Marek Vasut5abcbd72018-02-17 00:57:49 +0100838 return ret;
Marek Vasut31920262018-01-19 18:57:17 +0100839 }
840
841 mdiodev->read = bb_miiphy_read;
842 mdiodev->write = bb_miiphy_write;
843 bb_miiphy_buses[0].priv = eth;
844 snprintf(mdiodev->name, sizeof(mdiodev->name), udev->name);
845
846 ret = mdio_register(mdiodev);
847 if (ret < 0)
848 goto err_mdio_register;
849
850 priv->bus = miiphy_get_dev_by_name(udev->name);
851
852 eth->port = CONFIG_SH_ETHER_USE_PORT;
853 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
854 eth->port_info[eth->port].iobase =
Marek Vasut7234a282019-07-31 14:48:17 +0200855 (void __iomem *)(uintptr_t)(BASE_IO_ADDR + 0x800 * eth->port);
Marek Vasut31920262018-01-19 18:57:17 +0100856
Marek Vasut24b32472019-05-02 00:03:26 +0200857#if CONFIG_IS_ENABLED(CLK)
Marek Vasut4a45e932019-03-30 07:22:09 +0100858 ret = clk_enable(&priv->clk);
859 if (ret)
860 goto err_mdio_register;
Marek Vasut24b32472019-05-02 00:03:26 +0200861#endif
Marek Vasut4a45e932019-03-30 07:22:09 +0100862
Marek Vasutb13da112020-04-04 15:01:22 +0200863 ret = sh_eth_init_common(eth, pdata->enetaddr);
864 if (ret)
865 goto err_phy_config;
866
Marek Vasut4a45e932019-03-30 07:22:09 +0100867 ret = sh_eth_phy_config(udev);
868 if (ret) {
869 printf(SHETHER_NAME ": phy config timeout\n");
870 goto err_phy_config;
871 }
872
Marek Vasut31920262018-01-19 18:57:17 +0100873 return 0;
874
Marek Vasut4a45e932019-03-30 07:22:09 +0100875err_phy_config:
Marek Vasut24b32472019-05-02 00:03:26 +0200876#if CONFIG_IS_ENABLED(CLK)
Marek Vasut4a45e932019-03-30 07:22:09 +0100877 clk_disable(&priv->clk);
Marek Vasut24b32472019-05-02 00:03:26 +0200878#endif
Marek Vasut31920262018-01-19 18:57:17 +0100879err_mdio_register:
880 mdio_free(mdiodev);
Marek Vasut31920262018-01-19 18:57:17 +0100881 return ret;
882}
883
884static int sh_ether_remove(struct udevice *udev)
885{
886 struct sh_ether_priv *priv = dev_get_priv(udev);
887 struct sh_eth_dev *eth = &priv->shdev;
888 struct sh_eth_info *port_info = &eth->port_info[eth->port];
889
Marek Vasut24b32472019-05-02 00:03:26 +0200890#if CONFIG_IS_ENABLED(CLK)
Marek Vasut4a45e932019-03-30 07:22:09 +0100891 clk_disable(&priv->clk);
Marek Vasut24b32472019-05-02 00:03:26 +0200892#endif
Marek Vasut31920262018-01-19 18:57:17 +0100893 free(port_info->phydev);
894 mdio_unregister(priv->bus);
895 mdio_free(priv->bus);
896
897 if (dm_gpio_is_valid(&priv->reset_gpio))
898 dm_gpio_free(udev, &priv->reset_gpio);
899
Marek Vasut31920262018-01-19 18:57:17 +0100900 return 0;
901}
902
903static const struct eth_ops sh_ether_ops = {
904 .start = sh_ether_start,
905 .send = sh_ether_send,
906 .recv = sh_ether_recv,
907 .free_pkt = sh_ether_free_pkt,
908 .stop = sh_ether_stop,
909 .write_hwaddr = sh_ether_write_hwaddr,
910};
911
912int sh_ether_ofdata_to_platdata(struct udevice *dev)
913{
914 struct eth_pdata *pdata = dev_get_platdata(dev);
915 const char *phy_mode;
916 const fdt32_t *cell;
917 int ret = 0;
918
919 pdata->iobase = devfdt_get_addr(dev);
920 pdata->phy_interface = -1;
921 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
922 NULL);
923 if (phy_mode)
924 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
925 if (pdata->phy_interface == -1) {
926 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
927 return -EINVAL;
928 }
929
930 pdata->max_speed = 1000;
931 cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL);
932 if (cell)
933 pdata->max_speed = fdt32_to_cpu(*cell);
934
935 sprintf(bb_miiphy_buses[0].name, dev->name);
936
937 return ret;
938}
939
940static const struct udevice_id sh_ether_ids[] = {
Marek Vasut24b32472019-05-02 00:03:26 +0200941 { .compatible = "renesas,ether-r7s72100" },
Marek Vasutd5268012018-04-12 15:23:46 +0200942 { .compatible = "renesas,ether-r8a7790" },
Marek Vasut31920262018-01-19 18:57:17 +0100943 { .compatible = "renesas,ether-r8a7791" },
Marek Vasutd5268012018-04-12 15:23:46 +0200944 { .compatible = "renesas,ether-r8a7793" },
945 { .compatible = "renesas,ether-r8a7794" },
Marek Vasut46c33162019-07-31 12:58:06 +0200946 { .compatible = "renesas,gether-r8a77980" },
Marek Vasut31920262018-01-19 18:57:17 +0100947 { }
948};
949
950U_BOOT_DRIVER(eth_sh_ether) = {
951 .name = "sh_ether",
952 .id = UCLASS_ETH,
953 .of_match = sh_ether_ids,
954 .ofdata_to_platdata = sh_ether_ofdata_to_platdata,
955 .probe = sh_ether_probe,
956 .remove = sh_ether_remove,
957 .ops = &sh_ether_ops,
958 .priv_auto_alloc_size = sizeof(struct sh_ether_priv),
959 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
960 .flags = DM_FLAG_ALLOC_PRIV_DMA,
961};
962#endif
963
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900964/******* for bb_miiphy *******/
965static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
966{
967 return 0;
968}
969
970static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
971{
972 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900973 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900974
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900975 sh_eth_write(port_info, sh_eth_read(port_info, PIR) | PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900976
977 return 0;
978}
979
980static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
981{
982 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900983 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900984
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900985 sh_eth_write(port_info, sh_eth_read(port_info, PIR) & ~PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900986
987 return 0;
988}
989
990static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
991{
992 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900993 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900994
995 if (v)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900996 sh_eth_write(port_info,
997 sh_eth_read(port_info, PIR) | PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900998 else
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900999 sh_eth_write(port_info,
1000 sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +09001001
1002 return 0;
1003}
1004
1005static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
1006{
1007 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +09001008 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +09001009
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +09001010 *v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +09001011
1012 return 0;
1013}
1014
1015static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
1016{
1017 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +09001018 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +09001019
1020 if (v)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +09001021 sh_eth_write(port_info,
1022 sh_eth_read(port_info, PIR) | PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +09001023 else
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +09001024 sh_eth_write(port_info,
1025 sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +09001026
1027 return 0;
1028}
1029
1030static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
1031{
1032 udelay(10);
1033
1034 return 0;
1035}
1036
1037struct bb_miiphy_bus bb_miiphy_buses[] = {
1038 {
1039 .name = "sh_eth",
1040 .init = sh_eth_bb_init,
1041 .mdio_active = sh_eth_bb_mdio_active,
1042 .mdio_tristate = sh_eth_bb_mdio_tristate,
1043 .set_mdio = sh_eth_bb_set_mdio,
1044 .get_mdio = sh_eth_bb_get_mdio,
1045 .set_mdc = sh_eth_bb_set_mdc,
1046 .delay = sh_eth_bb_delay,
1047 }
1048};
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +09001049
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +09001050int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);