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