blob: da79b766a62241e4708af9d5f27cc6e09d0edc81 [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>
Alex Kiernan9925f1d2018-04-01 09:22:38 +000013#include <environment.h>
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090014#include <malloc.h>
15#include <net.h>
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090016#include <netdev.h>
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +090017#include <miiphy.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090018#include <linux/errno.h>
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090019#include <asm/io.h>
20
Marek Vasut31920262018-01-19 18:57:17 +010021#ifdef CONFIG_DM_ETH
22#include <clk.h>
23#include <dm.h>
24#include <linux/mii.h>
25#include <asm/gpio.h>
26#endif
27
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090028#include "sh_eth.h"
29
30#ifndef CONFIG_SH_ETHER_USE_PORT
31# error "Please define CONFIG_SH_ETHER_USE_PORT"
32#endif
33#ifndef CONFIG_SH_ETHER_PHY_ADDR
34# error "Please define CONFIG_SH_ETHER_PHY_ADDR"
35#endif
Nobuhiro Iwamatsu870cc232013-08-22 13:22:01 +090036
Trevor Woerner10015022019-05-03 09:41:00 -040037#if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && \
38 !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090039#define flush_cache_wback(addr, len) \
Nobuhiro Iwamatsuaae5d232017-12-01 13:56:08 +090040 flush_dcache_range((u32)addr, \
41 (u32)(addr + ALIGN(len, CONFIG_SH_ETHER_ALIGNE_SIZE)))
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090042#else
43#define flush_cache_wback(...)
44#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090045
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090046#if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
47#define invalidate_cache(addr, len) \
48 { \
49 u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \
50 u32 start, end; \
51 \
52 start = (u32)addr; \
53 end = start + len; \
54 start &= ~(line_size - 1); \
55 end = ((end + line_size - 1) & ~(line_size - 1)); \
56 \
57 invalidate_dcache_range(start, end); \
58 }
59#else
60#define invalidate_cache(...)
61#endif
62
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +090063#define TIMEOUT_CNT 1000
64
Marek Vasutdca221b2018-01-21 14:27:51 +010065static int sh_eth_send_common(struct sh_eth_dev *eth, void *packet, int len)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090066{
Marek Vasut3c5a7b72018-02-17 00:46:26 +010067 int ret = 0, timeout;
68 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090069
70 if (!packet || len > 0xffff) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090071 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
72 ret = -EINVAL;
73 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090074 }
75
76 /* packet must be a 4 byte boundary */
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +000077 if ((int)packet & 3) {
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +090078 printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n"
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +090079 , __func__);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090080 ret = -EFAULT;
81 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090082 }
83
84 /* Update tx descriptor */
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090085 flush_cache_wback(packet, len);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090086 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
87 port_info->tx_desc_cur->td1 = len << 16;
88 /* Must preserve the end of descriptor list indication */
89 if (port_info->tx_desc_cur->td0 & TD_TDLE)
90 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
91 else
92 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
93
Nobuhiro Iwamatsuf7ca1f72014-11-04 09:15:48 +090094 flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s));
95
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090096 /* Restart the transmitter if disabled */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +090097 if (!(sh_eth_read(port_info, EDTRR) & EDTRR_TRNS))
98 sh_eth_write(port_info, EDTRR_TRNS, EDTRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090099
100 /* Wait until packet is transmitted */
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900101 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +0900102 do {
103 invalidate_cache(port_info->tx_desc_cur,
104 sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900105 udelay(100);
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +0900106 } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900107
108 if (timeout < 0) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900109 printf(SHETHER_NAME ": transmit timeout\n");
110 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900111 goto err;
112 }
113
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900114 port_info->tx_desc_cur++;
115 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
116 port_info->tx_desc_cur = port_info->tx_desc_base;
117
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900118err:
119 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900120}
121
Marek Vasut52c15e22018-01-21 15:39:50 +0100122static int sh_eth_recv_start(struct sh_eth_dev *eth)
123{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100124 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasut52c15e22018-01-21 15:39:50 +0100125
126 /* Check if the rx descriptor is ready */
127 invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
128 if (port_info->rx_desc_cur->rd0 & RD_RACT)
129 return -EINVAL;
130
131 /* Check for errors */
132 if (port_info->rx_desc_cur->rd0 & RD_RFE)
133 return -EINVAL;
134
Marek Vasut60279b52018-02-17 00:47:38 +0100135 return port_info->rx_desc_cur->rd1 & 0xffff;
Marek Vasut52c15e22018-01-21 15:39:50 +0100136}
137
138static void sh_eth_recv_finish(struct sh_eth_dev *eth)
139{
140 struct sh_eth_info *port_info = &eth->port_info[eth->port];
141
142 /* Make current descriptor available again */
143 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
144 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
145 else
146 port_info->rx_desc_cur->rd0 = RD_RACT;
147
148 flush_cache_wback(port_info->rx_desc_cur,
149 sizeof(struct rx_desc_s));
150
151 /* Point to the next descriptor */
152 port_info->rx_desc_cur++;
153 if (port_info->rx_desc_cur >=
154 port_info->rx_desc_base + NUM_RX_DESC)
155 port_info->rx_desc_cur = port_info->rx_desc_base;
156}
157
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900158static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900159{
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900160 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900161#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900162 int ret = 0, i;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900163
164 /* Start e-dmac transmitter and receiver */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900165 sh_eth_write(port_info, EDSR_ENALL, EDSR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900166
167 /* Perform a software reset and wait for it to complete */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900168 sh_eth_write(port_info, EDMR_SRST, EDMR);
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900169 for (i = 0; i < TIMEOUT_CNT; i++) {
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900170 if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST))
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900171 break;
172 udelay(1000);
173 }
174
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900175 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900176 printf(SHETHER_NAME ": Software reset timeout\n");
177 ret = -EIO;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900178 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900179
180 return ret;
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900181#else
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900182 sh_eth_write(port_info, sh_eth_read(port_info, EDMR) | EDMR_SRST, EDMR);
Marek Vasut52627672018-02-17 00:57:49 +0100183 mdelay(3);
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900184 sh_eth_write(port_info,
185 sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900186
187 return 0;
188#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900189}
190
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900191static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900192{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100193 int i, ret = 0;
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900194 u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s);
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100195 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900196 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900197
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900198 /*
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900199 * Allocate rx descriptors. They must be aligned to size of struct
200 * tx_desc_s.
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900201 */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900202 port_info->tx_desc_alloc =
203 memalign(sizeof(struct tx_desc_s), alloc_desc_size);
204 if (!port_info->tx_desc_alloc) {
205 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900206 ret = -ENOMEM;
207 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900208 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900209
Nobuhiro Iwamatsuaae5d232017-12-01 13:56:08 +0900210 flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size);
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900211
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900212 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900213 port_info->tx_desc_base =
214 (struct tx_desc_s *)ADDR_TO_P2((u32)port_info->tx_desc_alloc);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900215 port_info->tx_desc_cur = port_info->tx_desc_base;
216
217 /* Initialize all descriptors */
218 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
219 cur_tx_desc++, i++) {
220 cur_tx_desc->td0 = 0x00;
221 cur_tx_desc->td1 = 0x00;
222 cur_tx_desc->td2 = 0x00;
223 }
224
225 /* Mark the end of the descriptors */
226 cur_tx_desc--;
227 cur_tx_desc->td0 |= TD_TDLE;
228
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900229 /*
230 * Point the controller to the tx descriptor list. Must use physical
231 * addresses
232 */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900233 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900234#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900235 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
236 sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR);
237 sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900238#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900239
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900240err:
241 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900242}
243
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900244static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900245{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100246 int i, ret = 0;
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900247 u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s);
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100248 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900249 struct rx_desc_s *cur_rx_desc;
250 u8 *rx_buf;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900251
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900252 /*
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900253 * Allocate rx descriptors. They must be aligned to size of struct
254 * rx_desc_s.
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900255 */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900256 port_info->rx_desc_alloc =
257 memalign(sizeof(struct rx_desc_s), alloc_desc_size);
258 if (!port_info->rx_desc_alloc) {
259 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900260 ret = -ENOMEM;
261 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900262 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900263
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900264 flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size);
265
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900266 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900267 port_info->rx_desc_base =
268 (struct rx_desc_s *)ADDR_TO_P2((u32)port_info->rx_desc_alloc);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900269
270 port_info->rx_desc_cur = port_info->rx_desc_base;
271
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900272 /*
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900273 * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes
274 * aligned and in P2 area.
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900275 */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900276 port_info->rx_buf_alloc =
277 memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE);
278 if (!port_info->rx_buf_alloc) {
279 printf(SHETHER_NAME ": alloc failed\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900280 ret = -ENOMEM;
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900281 goto err_buf_alloc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900282 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900283
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900284 port_info->rx_buf_base = (u8 *)ADDR_TO_P2((u32)port_info->rx_buf_alloc);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900285
286 /* Initialize all descriptors */
287 for (cur_rx_desc = port_info->rx_desc_base,
288 rx_buf = port_info->rx_buf_base, i = 0;
289 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
290 cur_rx_desc->rd0 = RD_RACT;
291 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900292 cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900293 }
294
295 /* Mark the end of the descriptors */
296 cur_rx_desc--;
297 cur_rx_desc->rd0 |= RD_RDLE;
298
299 /* Point the controller to the rx descriptor list */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900300 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900301#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900302 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
303 sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR);
304 sh_eth_write(port_info, RDFFR_RDLF, RDFFR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900305#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900306
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900307 return ret;
308
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900309err_buf_alloc:
310 free(port_info->rx_desc_alloc);
311 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900312
313err:
314 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900315}
316
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900317static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900318{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100319 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900320
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900321 if (port_info->tx_desc_alloc) {
322 free(port_info->tx_desc_alloc);
323 port_info->tx_desc_alloc = NULL;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900324 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900325}
326
327static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
328{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100329 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900330
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900331 if (port_info->rx_desc_alloc) {
332 free(port_info->rx_desc_alloc);
333 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900334 }
335
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900336 if (port_info->rx_buf_alloc) {
337 free(port_info->rx_buf_alloc);
338 port_info->rx_buf_alloc = NULL;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900339 }
340}
341
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900342static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900343{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900344 int ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900345
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900346 ret = sh_eth_tx_desc_init(eth);
347 if (ret)
348 goto err_tx_init;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900349
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900350 ret = sh_eth_rx_desc_init(eth);
351 if (ret)
352 goto err_rx_init;
353
354 return ret;
355err_rx_init:
356 sh_eth_tx_desc_free(eth);
357
358err_tx_init:
359 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900360}
361
Marek Vasut68ac92e2018-01-21 14:55:44 +0100362static void sh_eth_write_hwaddr(struct sh_eth_info *port_info,
363 unsigned char *mac)
364{
365 u32 val;
366
367 val = (mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3];
368 sh_eth_write(port_info, val, MAHR);
369
370 val = (mac[4] << 8) | mac[5];
371 sh_eth_write(port_info, val, MALR);
372}
373
Marek Vasut013af642018-01-21 15:10:21 +0100374static void sh_eth_mac_regs_config(struct sh_eth_dev *eth, unsigned char *mac)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900375{
Marek Vasut013af642018-01-21 15:10:21 +0100376 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900377
378 /* Configure e-dmac registers */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900379 sh_eth_write(port_info, (sh_eth_read(port_info, EDMR) & ~EMDR_DESC_R) |
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900380 (EMDR_DESC | EDMR_EL), EDMR);
381
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900382 sh_eth_write(port_info, 0, EESIPR);
383 sh_eth_write(port_info, 0, TRSCER);
384 sh_eth_write(port_info, 0, TFTR);
385 sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
386 sh_eth_write(port_info, RMCR_RST, RMCR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900387#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900388 sh_eth_write(port_info, 0, RPADIR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900389#endif
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900390 sh_eth_write(port_info, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900391
392 /* Configure e-mac registers */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900393 sh_eth_write(port_info, 0, ECSIPR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900394
395 /* Set Mac address */
Marek Vasut013af642018-01-21 15:10:21 +0100396 sh_eth_write_hwaddr(port_info, mac);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900397
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900398 sh_eth_write(port_info, RFLR_RFL_MIN, RFLR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000399#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900400 sh_eth_write(port_info, 0, PIPR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900401#endif
402#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900403 sh_eth_write(port_info, APR_AP, APR);
404 sh_eth_write(port_info, MPR_MP, MPR);
405 sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER);
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900406#endif
407
Nobuhiro Iwamatsudcd5a592012-08-02 22:08:40 +0000408#if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900409 sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
Marek Vasuteffb7902018-01-22 01:42:32 +0100410#elif defined(CONFIG_RCAR_GEN2)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900411 sh_eth_write(port_info, sh_eth_read(port_info, RMIIMR) | 0x1, RMIIMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000412#endif
Marek Vasut013af642018-01-21 15:10:21 +0100413}
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900414
Marek Vasut013af642018-01-21 15:10:21 +0100415static int sh_eth_phy_regs_config(struct sh_eth_dev *eth)
416{
417 struct sh_eth_info *port_info = &eth->port_info[eth->port];
418 struct phy_device *phy = port_info->phydev;
419 int ret = 0;
420 u32 val = 0;
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900421
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900422 /* Set the transfer speed */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900423 if (phy->speed == 100) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900424 printf(SHETHER_NAME ": 100Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000425#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900426 sh_eth_write(port_info, GECMR_100B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000427#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900428 sh_eth_write(port_info, 1, RTRATE);
Marek Vasutd13a6142019-05-04 13:31:06 +0200429#elif defined(CONFIG_RCAR_GEN2)
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900430 val = ECMR_RTM;
431#endif
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900432 } else if (phy->speed == 10) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900433 printf(SHETHER_NAME ": 10Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000434#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900435 sh_eth_write(port_info, GECMR_10B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000436#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900437 sh_eth_write(port_info, 0, RTRATE);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900438#endif
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900439 }
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000440#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000441 else if (phy->speed == 1000) {
442 printf(SHETHER_NAME ": 1000Base/");
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900443 sh_eth_write(port_info, GECMR_1000B, GECMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000444 }
445#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900446
447 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900448 if (phy->duplex) {
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900449 printf("Full\n");
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900450 sh_eth_write(port_info,
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900451 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM),
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000452 ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900453 } else {
454 printf("Half\n");
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900455 sh_eth_write(port_info,
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900456 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE),
457 ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900458 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900459
460 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900461}
462
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900463static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900464{
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900465 struct sh_eth_info *port_info = &eth->port_info[eth->port];
466
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900467 /*
468 * Enable the e-dmac receiver only. The transmitter will be enabled when
469 * we have something to transmit
470 */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900471 sh_eth_write(port_info, EDRRR_R, EDRRR);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900472}
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900473
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900474static void sh_eth_stop(struct sh_eth_dev *eth)
475{
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900476 struct sh_eth_info *port_info = &eth->port_info[eth->port];
477
478 sh_eth_write(port_info, ~EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900479}
480
Marek Vasut013af642018-01-21 15:10:21 +0100481static int sh_eth_init_common(struct sh_eth_dev *eth, unsigned char *mac)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900482{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900483 int ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900484
485 ret = sh_eth_reset(eth);
486 if (ret)
Marek Vasut013af642018-01-21 15:10:21 +0100487 return ret;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900488
489 ret = sh_eth_desc_init(eth);
490 if (ret)
Marek Vasut013af642018-01-21 15:10:21 +0100491 return ret;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900492
Marek Vasut013af642018-01-21 15:10:21 +0100493 sh_eth_mac_regs_config(eth, mac);
494
495 return 0;
496}
497
498static int sh_eth_start_common(struct sh_eth_dev *eth)
499{
500 struct sh_eth_info *port_info = &eth->port_info[eth->port];
501 int ret;
502
503 ret = phy_startup(port_info->phydev);
504 if (ret) {
505 printf(SHETHER_NAME ": phy startup failure\n");
506 return ret;
507 }
508
509 ret = sh_eth_phy_regs_config(eth);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900510 if (ret)
Marek Vasut013af642018-01-21 15:10:21 +0100511 return ret;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900512
513 sh_eth_start(eth);
514
Marek Vasut013af642018-01-21 15:10:21 +0100515 return 0;
516}
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900517
Marek Vasut31920262018-01-19 18:57:17 +0100518#ifndef CONFIG_DM_ETH
Marek Vasuta2207842018-01-21 15:31:48 +0100519static int sh_eth_phy_config_legacy(struct sh_eth_dev *eth)
520{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100521 int ret = 0;
522 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasuta2207842018-01-21 15:31:48 +0100523 struct eth_device *dev = port_info->dev;
524 struct phy_device *phydev;
525
526 phydev = phy_connect(
527 miiphy_get_dev_by_name(dev->name),
528 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
529 port_info->phydev = phydev;
530 phy_config(phydev);
531
532 return ret;
533}
534
535static int sh_eth_send_legacy(struct eth_device *dev, void *packet, int len)
536{
537 struct sh_eth_dev *eth = dev->priv;
538
539 return sh_eth_send_common(eth, packet, len);
540}
541
542static int sh_eth_recv_common(struct sh_eth_dev *eth)
543{
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100544 int len = 0;
545 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasuta2207842018-01-21 15:31:48 +0100546 uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2);
547
548 len = sh_eth_recv_start(eth);
549 if (len > 0) {
550 invalidate_cache(packet, len);
551 net_process_received_packet(packet, len);
552 sh_eth_recv_finish(eth);
553 } else
554 len = 0;
555
556 /* Restart the receiver if disabled */
557 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
558 sh_eth_write(port_info, EDRRR_R, EDRRR);
559
560 return len;
561}
562
563static int sh_eth_recv_legacy(struct eth_device *dev)
564{
565 struct sh_eth_dev *eth = dev->priv;
566
567 return sh_eth_recv_common(eth);
568}
569
Marek Vasut013af642018-01-21 15:10:21 +0100570static int sh_eth_init_legacy(struct eth_device *dev, bd_t *bd)
571{
572 struct sh_eth_dev *eth = dev->priv;
573 int ret;
574
575 ret = sh_eth_init_common(eth, dev->enetaddr);
576 if (ret)
577 return ret;
578
579 ret = sh_eth_phy_config_legacy(eth);
580 if (ret) {
581 printf(SHETHER_NAME ": phy config timeout\n");
582 goto err_start;
583 }
584
585 ret = sh_eth_start_common(eth);
586 if (ret)
587 goto err_start;
588
589 return 0;
590
591err_start:
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900592 sh_eth_tx_desc_free(eth);
593 sh_eth_rx_desc_free(eth);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900594 return ret;
595}
596
Marek Vasut013af642018-01-21 15:10:21 +0100597void sh_eth_halt_legacy(struct eth_device *dev)
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900598{
599 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900600
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900601 sh_eth_stop(eth);
602}
603
604int sh_eth_initialize(bd_t *bd)
605{
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900606 int ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900607 struct sh_eth_dev *eth = NULL;
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900608 struct eth_device *dev = NULL;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900609 struct mii_dev *mdiodev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900610
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900611 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900612 if (!eth) {
613 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
614 ret = -ENOMEM;
615 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900616 }
617
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900618 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900619 if (!dev) {
620 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
621 ret = -ENOMEM;
622 goto err;
623 }
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900624 memset(dev, 0, sizeof(struct eth_device));
625 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900626
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900627 eth->port = CONFIG_SH_ETHER_USE_PORT;
628 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900629 eth->port_info[eth->port].iobase =
630 (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900631
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900632 dev->priv = (void *)eth;
633 dev->iobase = 0;
Marek Vasut013af642018-01-21 15:10:21 +0100634 dev->init = sh_eth_init_legacy;
635 dev->halt = sh_eth_halt_legacy;
Marek Vasutdca221b2018-01-21 14:27:51 +0100636 dev->send = sh_eth_send_legacy;
637 dev->recv = sh_eth_recv_legacy;
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900638 eth->port_info[eth->port].dev = dev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900639
Ben Whitten192bc692015-12-30 13:05:58 +0000640 strcpy(dev->name, SHETHER_NAME);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900641
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900642 /* Register Device to EtherNet subsystem */
643 eth_register(dev);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900644
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900645 bb_miiphy_buses[0].priv = eth;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900646 mdiodev = mdio_alloc();
Joe Hershberger5a49f172016-08-08 11:28:38 -0500647 if (!mdiodev)
648 return -ENOMEM;
649 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
650 mdiodev->read = bb_miiphy_read;
651 mdiodev->write = bb_miiphy_write;
652
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900653 ret = mdio_register(mdiodev);
654 if (ret < 0)
655 return ret;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900656
Simon Glass35affd72017-08-03 12:22:14 -0600657 if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr))
Mike Frysingerc527ce92009-02-11 19:14:09 -0500658 puts("Please set MAC address\n");
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900659
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900660 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900661
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900662err:
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900663 if (dev)
664 free(dev);
665
666 if (eth)
667 free(eth);
668
669 printf(SHETHER_NAME ": Failed\n");
670 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900671}
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900672
Marek Vasut31920262018-01-19 18:57:17 +0100673#else /* CONFIG_DM_ETH */
674
675struct sh_ether_priv {
676 struct sh_eth_dev shdev;
677
678 struct mii_dev *bus;
Marek Vasut5abcbd72018-02-17 00:57:49 +0100679 phys_addr_t iobase;
Marek Vasut31920262018-01-19 18:57:17 +0100680 struct clk clk;
681 struct gpio_desc reset_gpio;
682};
683
684static int sh_ether_send(struct udevice *dev, void *packet, int len)
685{
686 struct sh_ether_priv *priv = dev_get_priv(dev);
687 struct sh_eth_dev *eth = &priv->shdev;
688
689 return sh_eth_send_common(eth, packet, len);
690}
691
692static int sh_ether_recv(struct udevice *dev, int flags, uchar **packetp)
693{
694 struct sh_ether_priv *priv = dev_get_priv(dev);
695 struct sh_eth_dev *eth = &priv->shdev;
696 struct sh_eth_info *port_info = &eth->port_info[eth->port];
697 uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2);
698 int len;
699
700 len = sh_eth_recv_start(eth);
701 if (len > 0) {
702 invalidate_cache(packet, len);
703 *packetp = packet;
704
705 return len;
706 } else {
707 len = 0;
708
709 /* Restart the receiver if disabled */
710 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
711 sh_eth_write(port_info, EDRRR_R, EDRRR);
712
713 return -EAGAIN;
714 }
715}
716
717static int sh_ether_free_pkt(struct udevice *dev, uchar *packet, int length)
718{
719 struct sh_ether_priv *priv = dev_get_priv(dev);
720 struct sh_eth_dev *eth = &priv->shdev;
721 struct sh_eth_info *port_info = &eth->port_info[eth->port];
722
723 sh_eth_recv_finish(eth);
724
725 /* Restart the receiver if disabled */
726 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
727 sh_eth_write(port_info, EDRRR_R, EDRRR);
728
729 return 0;
730}
731
732static int sh_ether_write_hwaddr(struct udevice *dev)
733{
734 struct sh_ether_priv *priv = dev_get_priv(dev);
735 struct sh_eth_dev *eth = &priv->shdev;
736 struct sh_eth_info *port_info = &eth->port_info[eth->port];
737 struct eth_pdata *pdata = dev_get_platdata(dev);
738
739 sh_eth_write_hwaddr(port_info, pdata->enetaddr);
740
741 return 0;
742}
743
744static int sh_eth_phy_config(struct udevice *dev)
745{
746 struct sh_ether_priv *priv = dev_get_priv(dev);
747 struct eth_pdata *pdata = dev_get_platdata(dev);
748 struct sh_eth_dev *eth = &priv->shdev;
Marek Vasut3c5a7b72018-02-17 00:46:26 +0100749 int ret = 0;
750 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasut31920262018-01-19 18:57:17 +0100751 struct phy_device *phydev;
752 int mask = 0xffffffff;
753
754 phydev = phy_find_by_mask(priv->bus, mask, pdata->phy_interface);
755 if (!phydev)
756 return -ENODEV;
757
758 phy_connect_dev(phydev, dev);
759
760 port_info->phydev = phydev;
761 phy_config(phydev);
762
763 return ret;
764}
765
766static int sh_ether_start(struct udevice *dev)
767{
768 struct sh_ether_priv *priv = dev_get_priv(dev);
769 struct eth_pdata *pdata = dev_get_platdata(dev);
770 struct sh_eth_dev *eth = &priv->shdev;
771 int ret;
772
Marek Vasut31920262018-01-19 18:57:17 +0100773 ret = sh_eth_init_common(eth, pdata->enetaddr);
774 if (ret)
Marek Vasut4a45e932019-03-30 07:22:09 +0100775 return ret;
Marek Vasut31920262018-01-19 18:57:17 +0100776
777 ret = sh_eth_start_common(eth);
778 if (ret)
779 goto err_start;
780
781 return 0;
782
783err_start:
784 sh_eth_tx_desc_free(eth);
785 sh_eth_rx_desc_free(eth);
Marek Vasut31920262018-01-19 18:57:17 +0100786 return ret;
787}
788
789static void sh_ether_stop(struct udevice *dev)
790{
791 struct sh_ether_priv *priv = dev_get_priv(dev);
Marek Vasut4a45e932019-03-30 07:22:09 +0100792 struct sh_eth_dev *eth = &priv->shdev;
793 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Marek Vasut31920262018-01-19 18:57:17 +0100794
Marek Vasut4a45e932019-03-30 07:22:09 +0100795 phy_shutdown(port_info->phydev);
Marek Vasut31920262018-01-19 18:57:17 +0100796 sh_eth_stop(&priv->shdev);
Marek Vasut31920262018-01-19 18:57:17 +0100797}
798
799static int sh_ether_probe(struct udevice *udev)
800{
801 struct eth_pdata *pdata = dev_get_platdata(udev);
802 struct sh_ether_priv *priv = dev_get_priv(udev);
803 struct sh_eth_dev *eth = &priv->shdev;
Marek Vasut159b3292018-06-18 04:03:01 +0200804 struct ofnode_phandle_args phandle_args;
Marek Vasut31920262018-01-19 18:57:17 +0100805 struct mii_dev *mdiodev;
Marek Vasut31920262018-01-19 18:57:17 +0100806 int ret;
807
Marek Vasut5abcbd72018-02-17 00:57:49 +0100808 priv->iobase = pdata->iobase;
Marek Vasut31920262018-01-19 18:57:17 +0100809
Marek Vasut24b32472019-05-02 00:03:26 +0200810#if CONFIG_IS_ENABLED(CLK)
Marek Vasut31920262018-01-19 18:57:17 +0100811 ret = clk_get_by_index(udev, 0, &priv->clk);
812 if (ret < 0)
Marek Vasut5abcbd72018-02-17 00:57:49 +0100813 return ret;
Marek Vasut24b32472019-05-02 00:03:26 +0200814#endif
Marek Vasut31920262018-01-19 18:57:17 +0100815
Marek Vasut159b3292018-06-18 04:03:01 +0200816 ret = dev_read_phandle_with_args(udev, "phy-handle", NULL, 0, 0, &phandle_args);
817 if (!ret) {
818 gpio_request_by_name_nodev(phandle_args.node, "reset-gpios", 0,
819 &priv->reset_gpio, GPIOD_IS_OUT);
820 }
821
822 if (!dm_gpio_is_valid(&priv->reset_gpio)) {
823 gpio_request_by_name(udev, "reset-gpios", 0, &priv->reset_gpio,
824 GPIOD_IS_OUT);
825 }
Marek Vasut31920262018-01-19 18:57:17 +0100826
827 mdiodev = mdio_alloc();
828 if (!mdiodev) {
829 ret = -ENOMEM;
Marek Vasut5abcbd72018-02-17 00:57:49 +0100830 return ret;
Marek Vasut31920262018-01-19 18:57:17 +0100831 }
832
833 mdiodev->read = bb_miiphy_read;
834 mdiodev->write = bb_miiphy_write;
835 bb_miiphy_buses[0].priv = eth;
836 snprintf(mdiodev->name, sizeof(mdiodev->name), udev->name);
837
838 ret = mdio_register(mdiodev);
839 if (ret < 0)
840 goto err_mdio_register;
841
842 priv->bus = miiphy_get_dev_by_name(udev->name);
843
844 eth->port = CONFIG_SH_ETHER_USE_PORT;
845 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
846 eth->port_info[eth->port].iobase =
847 (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port);
848
Marek Vasut24b32472019-05-02 00:03:26 +0200849#if CONFIG_IS_ENABLED(CLK)
Marek Vasut4a45e932019-03-30 07:22:09 +0100850 ret = clk_enable(&priv->clk);
851 if (ret)
852 goto err_mdio_register;
Marek Vasut24b32472019-05-02 00:03:26 +0200853#endif
Marek Vasut4a45e932019-03-30 07:22:09 +0100854
855 ret = sh_eth_phy_config(udev);
856 if (ret) {
857 printf(SHETHER_NAME ": phy config timeout\n");
858 goto err_phy_config;
859 }
860
Marek Vasut31920262018-01-19 18:57:17 +0100861 return 0;
862
Marek Vasut4a45e932019-03-30 07:22:09 +0100863err_phy_config:
Marek Vasut24b32472019-05-02 00:03:26 +0200864#if CONFIG_IS_ENABLED(CLK)
Marek Vasut4a45e932019-03-30 07:22:09 +0100865 clk_disable(&priv->clk);
Marek Vasut24b32472019-05-02 00:03:26 +0200866#endif
Marek Vasut31920262018-01-19 18:57:17 +0100867err_mdio_register:
868 mdio_free(mdiodev);
Marek Vasut31920262018-01-19 18:57:17 +0100869 return ret;
870}
871
872static int sh_ether_remove(struct udevice *udev)
873{
874 struct sh_ether_priv *priv = dev_get_priv(udev);
875 struct sh_eth_dev *eth = &priv->shdev;
876 struct sh_eth_info *port_info = &eth->port_info[eth->port];
877
Marek Vasut24b32472019-05-02 00:03:26 +0200878#if CONFIG_IS_ENABLED(CLK)
Marek Vasut4a45e932019-03-30 07:22:09 +0100879 clk_disable(&priv->clk);
Marek Vasut24b32472019-05-02 00:03:26 +0200880#endif
Marek Vasut31920262018-01-19 18:57:17 +0100881 free(port_info->phydev);
882 mdio_unregister(priv->bus);
883 mdio_free(priv->bus);
884
885 if (dm_gpio_is_valid(&priv->reset_gpio))
886 dm_gpio_free(udev, &priv->reset_gpio);
887
Marek Vasut31920262018-01-19 18:57:17 +0100888 return 0;
889}
890
891static const struct eth_ops sh_ether_ops = {
892 .start = sh_ether_start,
893 .send = sh_ether_send,
894 .recv = sh_ether_recv,
895 .free_pkt = sh_ether_free_pkt,
896 .stop = sh_ether_stop,
897 .write_hwaddr = sh_ether_write_hwaddr,
898};
899
900int sh_ether_ofdata_to_platdata(struct udevice *dev)
901{
902 struct eth_pdata *pdata = dev_get_platdata(dev);
903 const char *phy_mode;
904 const fdt32_t *cell;
905 int ret = 0;
906
907 pdata->iobase = devfdt_get_addr(dev);
908 pdata->phy_interface = -1;
909 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
910 NULL);
911 if (phy_mode)
912 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
913 if (pdata->phy_interface == -1) {
914 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
915 return -EINVAL;
916 }
917
918 pdata->max_speed = 1000;
919 cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL);
920 if (cell)
921 pdata->max_speed = fdt32_to_cpu(*cell);
922
923 sprintf(bb_miiphy_buses[0].name, dev->name);
924
925 return ret;
926}
927
928static const struct udevice_id sh_ether_ids[] = {
Marek Vasut24b32472019-05-02 00:03:26 +0200929 { .compatible = "renesas,ether-r7s72100" },
Marek Vasutd5268012018-04-12 15:23:46 +0200930 { .compatible = "renesas,ether-r8a7790" },
Marek Vasut31920262018-01-19 18:57:17 +0100931 { .compatible = "renesas,ether-r8a7791" },
Marek Vasutd5268012018-04-12 15:23:46 +0200932 { .compatible = "renesas,ether-r8a7793" },
933 { .compatible = "renesas,ether-r8a7794" },
Marek Vasut31920262018-01-19 18:57:17 +0100934 { }
935};
936
937U_BOOT_DRIVER(eth_sh_ether) = {
938 .name = "sh_ether",
939 .id = UCLASS_ETH,
940 .of_match = sh_ether_ids,
941 .ofdata_to_platdata = sh_ether_ofdata_to_platdata,
942 .probe = sh_ether_probe,
943 .remove = sh_ether_remove,
944 .ops = &sh_ether_ops,
945 .priv_auto_alloc_size = sizeof(struct sh_ether_priv),
946 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
947 .flags = DM_FLAG_ALLOC_PRIV_DMA,
948};
949#endif
950
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900951/******* for bb_miiphy *******/
952static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
953{
954 return 0;
955}
956
957static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
958{
959 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900960 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900961
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900962 sh_eth_write(port_info, sh_eth_read(port_info, PIR) | PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900963
964 return 0;
965}
966
967static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
968{
969 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900970 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900971
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900972 sh_eth_write(port_info, sh_eth_read(port_info, PIR) & ~PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900973
974 return 0;
975}
976
977static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
978{
979 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900980 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900981
982 if (v)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900983 sh_eth_write(port_info,
984 sh_eth_read(port_info, PIR) | PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900985 else
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900986 sh_eth_write(port_info,
987 sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900988
989 return 0;
990}
991
992static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
993{
994 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900995 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900996
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900997 *v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900998
999 return 0;
1000}
1001
1002static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
1003{
1004 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +09001005 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +09001006
1007 if (v)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +09001008 sh_eth_write(port_info,
1009 sh_eth_read(port_info, PIR) | PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +09001010 else
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +09001011 sh_eth_write(port_info,
1012 sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +09001013
1014 return 0;
1015}
1016
1017static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
1018{
1019 udelay(10);
1020
1021 return 0;
1022}
1023
1024struct bb_miiphy_bus bb_miiphy_buses[] = {
1025 {
1026 .name = "sh_eth",
1027 .init = sh_eth_bb_init,
1028 .mdio_active = sh_eth_bb_mdio_active,
1029 .mdio_tristate = sh_eth_bb_mdio_tristate,
1030 .set_mdio = sh_eth_bb_set_mdio,
1031 .get_mdio = sh_eth_bb_get_mdio,
1032 .set_mdc = sh_eth_bb_set_mdc,
1033 .delay = sh_eth_bb_delay,
1034 }
1035};
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +09001036
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +09001037int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);