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