blob: 1aaa5a79b3f6c51547c5e9246dbda364fa2a4d8e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Ted Chen9b6dbd42016-01-20 14:24:52 +08002/*
3 * Copyright (c) 2015 Realtek Semiconductor Corp. All rights reserved.
4 *
Stefan Roese47c50972016-06-29 07:58:05 +02005 */
Ted Chen9b6dbd42016-01-20 14:24:52 +08006
7#include <common.h>
Stefan Roese47c50972016-06-29 07:58:05 +02008#include <dm.h>
Ted Chen9b6dbd42016-01-20 14:24:52 +08009#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Ted Chen9b6dbd42016-01-20 14:24:52 +080011#include <malloc.h>
Stefan Roese01094402016-11-22 16:14:23 +010012#include <memalign.h>
Simon Glass274e0b02020-05-10 11:39:56 -060013#include <net.h>
Ted Chen9b6dbd42016-01-20 14:24:52 +080014#include <usb.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Ted Chen9b6dbd42016-01-20 14:24:52 +080016#include <linux/mii.h>
17#include <linux/bitops.h>
18#include "usb_ether.h"
19#include "r8152.h"
20
Stefan Roese47c50972016-06-29 07:58:05 +020021#ifndef CONFIG_DM_ETH
Ted Chen9b6dbd42016-01-20 14:24:52 +080022/* local vars */
23static int curr_eth_dev; /* index for name of next device detected */
24
25struct r8152_dongle {
26 unsigned short vendor;
27 unsigned short product;
28};
29
Philipp Tomsich657bc532017-06-25 22:47:15 +020030static const struct r8152_dongle r8152_dongles[] = {
Ted Chen9b6dbd42016-01-20 14:24:52 +080031 /* Realtek */
32 { 0x0bda, 0x8050 },
33 { 0x0bda, 0x8152 },
34 { 0x0bda, 0x8153 },
35
36 /* Samsung */
37 { 0x04e8, 0xa101 },
38
39 /* Lenovo */
40 { 0x17ef, 0x304f },
41 { 0x17ef, 0x3052 },
42 { 0x17ef, 0x3054 },
43 { 0x17ef, 0x3057 },
44 { 0x17ef, 0x7205 },
45 { 0x17ef, 0x720a },
46 { 0x17ef, 0x720b },
47 { 0x17ef, 0x720c },
48
49 /* TP-LINK */
50 { 0x2357, 0x0601 },
Oleksii Titov0c929352022-04-20 11:23:25 +030051 { 0x2357, 0x0602 },
Ted Chen9b6dbd42016-01-20 14:24:52 +080052
53 /* Nvidia */
54 { 0x0955, 0x09ff },
55};
Stefan Roese47c50972016-06-29 07:58:05 +020056#endif
57
58struct r8152_version {
59 unsigned short tcr;
60 unsigned short version;
61 bool gmii;
62};
Ted Chen9b6dbd42016-01-20 14:24:52 +080063
Philipp Tomsich657bc532017-06-25 22:47:15 +020064static const struct r8152_version r8152_versions[] = {
Ted Chen9b6dbd42016-01-20 14:24:52 +080065 { 0x4c00, RTL_VER_01, 0 },
66 { 0x4c10, RTL_VER_02, 0 },
67 { 0x5c00, RTL_VER_03, 1 },
68 { 0x5c10, RTL_VER_04, 1 },
69 { 0x5c20, RTL_VER_05, 1 },
70 { 0x5c30, RTL_VER_06, 1 },
71 { 0x4800, RTL_VER_07, 0 },
Hayes Wangd215ca22020-06-16 17:09:47 +080072 { 0x6000, RTL_VER_08, 1 },
73 { 0x6010, RTL_VER_09, 1 },
Ted Chen9b6dbd42016-01-20 14:24:52 +080074};
75
76static
77int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
78{
Stefan Roese01094402016-11-22 16:14:23 +010079 ALLOC_CACHE_ALIGN_BUFFER(void *, tmp, size);
80 int ret;
81
82 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
83 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
84 value, index, tmp, size, 500);
85 memcpy(data, tmp, size);
86 return ret;
Ted Chen9b6dbd42016-01-20 14:24:52 +080087}
88
89static
90int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
91{
Stefan Roese01094402016-11-22 16:14:23 +010092 ALLOC_CACHE_ALIGN_BUFFER(void *, tmp, size);
93
94 memcpy(tmp, data, size);
Ted Chen9b6dbd42016-01-20 14:24:52 +080095 return usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
96 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
Stefan Roese01094402016-11-22 16:14:23 +010097 value, index, tmp, size, 500);
Ted Chen9b6dbd42016-01-20 14:24:52 +080098}
99
100int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
101 void *data, u16 type)
102{
103 u16 burst_size = 64;
104 int ret;
105 int txsize;
106
107 /* both size and index must be 4 bytes align */
108 if ((size & 3) || !size || (index & 3) || !data)
109 return -EINVAL;
110
111 if (index + size > 0xffff)
112 return -EINVAL;
113
114 while (size) {
115 txsize = min(size, burst_size);
116 ret = get_registers(tp, index, type, txsize, data);
117 if (ret < 0)
118 break;
119
120 index += txsize;
121 data += txsize;
122 size -= txsize;
123 }
124
125 return ret;
126}
127
128int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
129 u16 size, void *data, u16 type)
130{
131 int ret;
132 u16 byteen_start, byteen_end, byte_en_to_hw;
133 u16 burst_size = 512;
134 int txsize;
135
136 /* both size and index must be 4 bytes align */
137 if ((size & 3) || !size || (index & 3) || !data)
138 return -EINVAL;
139
140 if (index + size > 0xffff)
141 return -EINVAL;
142
143 byteen_start = byteen & BYTE_EN_START_MASK;
144 byteen_end = byteen & BYTE_EN_END_MASK;
145
146 byte_en_to_hw = byteen_start | (byteen_start << 4);
147 ret = set_registers(tp, index, type | byte_en_to_hw, 4, data);
148 if (ret < 0)
149 return ret;
150
151 index += 4;
152 data += 4;
153 size -= 4;
154
155 if (size) {
156 size -= 4;
157
158 while (size) {
159 txsize = min(size, burst_size);
160
161 ret = set_registers(tp, index,
162 type | BYTE_EN_DWORD,
163 txsize, data);
164 if (ret < 0)
165 return ret;
166
167 index += txsize;
168 data += txsize;
169 size -= txsize;
170 }
171
172 byte_en_to_hw = byteen_end | (byteen_end >> 4);
173 ret = set_registers(tp, index, type | byte_en_to_hw, 4, data);
174 if (ret < 0)
175 return ret;
176 }
177
178 return ret;
179}
180
181int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
182{
183 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
184}
185
186int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
187{
188 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
189}
190
191int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
192{
193 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
194}
195
196int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
197{
198 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
199}
200
201u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
202{
203 __le32 data;
204
205 generic_ocp_read(tp, index, sizeof(data), &data, type);
206
207 return __le32_to_cpu(data);
208}
209
210void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
211{
212 __le32 tmp = __cpu_to_le32(data);
213
214 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
215}
216
217u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
218{
219 u32 data;
220 __le32 tmp;
221 u8 shift = index & 2;
222
223 index &= ~3;
224
225 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
226
227 data = __le32_to_cpu(tmp);
228 data >>= (shift * 8);
229 data &= 0xffff;
230
231 return data;
232}
233
234void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
235{
236 u32 mask = 0xffff;
237 __le32 tmp;
238 u16 byen = BYTE_EN_WORD;
239 u8 shift = index & 2;
240
241 data &= mask;
242
243 if (index & 2) {
244 byen <<= shift;
245 mask <<= (shift * 8);
246 data <<= (shift * 8);
247 index &= ~3;
248 }
249
250 tmp = __cpu_to_le32(data);
251
252 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
253}
254
255u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
256{
257 u32 data;
258 __le32 tmp;
259 u8 shift = index & 3;
260
261 index &= ~3;
262
263 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
264
265 data = __le32_to_cpu(tmp);
266 data >>= (shift * 8);
267 data &= 0xff;
268
269 return data;
270}
271
272void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
273{
274 u32 mask = 0xff;
275 __le32 tmp;
276 u16 byen = BYTE_EN_BYTE;
277 u8 shift = index & 3;
278
279 data &= mask;
280
281 if (index & 3) {
282 byen <<= shift;
283 mask <<= (shift * 8);
284 data <<= (shift * 8);
285 index &= ~3;
286 }
287
288 tmp = __cpu_to_le32(data);
289
290 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
291}
292
293u16 ocp_reg_read(struct r8152 *tp, u16 addr)
294{
295 u16 ocp_base, ocp_index;
296
297 ocp_base = addr & 0xf000;
298 if (ocp_base != tp->ocp_base) {
299 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
300 tp->ocp_base = ocp_base;
301 }
302
303 ocp_index = (addr & 0x0fff) | 0xb000;
304 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
305}
306
307void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
308{
309 u16 ocp_base, ocp_index;
310
311 ocp_base = addr & 0xf000;
312 if (ocp_base != tp->ocp_base) {
313 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
314 tp->ocp_base = ocp_base;
315 }
316
317 ocp_index = (addr & 0x0fff) | 0xb000;
318 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
319}
320
321static void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
322{
323 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
324}
325
326static int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
327{
328 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
329}
330
331void sram_write(struct r8152 *tp, u16 addr, u16 data)
332{
333 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
334 ocp_reg_write(tp, OCP_SRAM_DATA, data);
335}
336
Hayes Wangd215ca22020-06-16 17:09:47 +0800337static u16 sram_read(struct r8152 *tp, u16 addr)
338{
339 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
340 return ocp_reg_read(tp, OCP_SRAM_DATA);
341}
342
Ted Chen9b6dbd42016-01-20 14:24:52 +0800343int r8152_wait_for_bit(struct r8152 *tp, bool ocp_reg, u16 type, u16 index,
344 const u32 mask, bool set, unsigned int timeout)
345{
346 u32 val;
347
348 while (--timeout) {
349 if (ocp_reg)
350 val = ocp_reg_read(tp, index);
351 else
352 val = ocp_read_dword(tp, type, index);
353
354 if (!set)
355 val = ~val;
356
357 if ((val & mask) == mask)
358 return 0;
359
360 mdelay(1);
361 }
362
363 debug("%s: Timeout (index=%04x mask=%08x timeout=%d)\n",
364 __func__, index, mask, timeout);
365
366 return -ETIMEDOUT;
367}
368
369static void r8152b_reset_packet_filter(struct r8152 *tp)
370{
371 u32 ocp_data;
372
373 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
374 ocp_data &= ~FMC_FCR_MCU_EN;
375 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
376 ocp_data |= FMC_FCR_MCU_EN;
377 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
378}
379
380static void rtl8152_wait_fifo_empty(struct r8152 *tp)
381{
382 int ret;
383
384 ret = r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_PHY_PWR,
385 PLA_PHY_PWR_TXEMP, 1, R8152_WAIT_TIMEOUT);
386 if (ret)
387 debug("Timeout waiting for FIFO empty\n");
388
389 ret = r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_TCR0,
390 TCR0_TX_EMPTY, 1, R8152_WAIT_TIMEOUT);
391 if (ret)
392 debug("Timeout waiting for TX empty\n");
393}
394
395static void rtl8152_nic_reset(struct r8152 *tp)
396{
397 int ret;
398 u32 ocp_data;
399
400 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, BIST_CTRL);
401 ocp_data |= BIST_CTRL_SW_RESET;
402 ocp_write_dword(tp, MCU_TYPE_PLA, BIST_CTRL, ocp_data);
403
404 ret = r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, BIST_CTRL,
405 BIST_CTRL_SW_RESET, 0, R8152_WAIT_TIMEOUT);
406 if (ret)
407 debug("Timeout waiting for NIC reset\n");
408}
409
410static u8 rtl8152_get_speed(struct r8152 *tp)
411{
412 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
413}
414
415static void rtl_set_eee_plus(struct r8152 *tp)
416{
417 u32 ocp_data;
418
419 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
420 ocp_data &= ~EEEP_CR_EEEP_TX;
421 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
422}
423
424static void rxdy_gated_en(struct r8152 *tp, bool enable)
425{
426 u32 ocp_data;
427
428 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
429 if (enable)
430 ocp_data |= RXDY_GATED_EN;
431 else
432 ocp_data &= ~RXDY_GATED_EN;
433 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
434}
435
436static void rtl8152_set_rx_mode(struct r8152 *tp)
437{
438 u32 ocp_data;
439 __le32 tmp[2];
440
441 tmp[0] = 0xffffffff;
442 tmp[1] = 0xffffffff;
443
444 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
445
446 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
447 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
448 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
449}
450
Hayes Wangb08cd542020-12-16 17:03:23 +0800451static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
452{
453 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
454 OWN_UPDATE | OWN_CLEAR);
455}
456
Ted Chen9b6dbd42016-01-20 14:24:52 +0800457static int rtl_enable(struct r8152 *tp)
458{
459 u32 ocp_data;
460
461 r8152b_reset_packet_filter(tp);
462
463 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
464 ocp_data |= PLA_CR_RE | PLA_CR_TE;
465 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
466
Hayes Wangb08cd542020-12-16 17:03:23 +0800467 switch (tp->version) {
468 case RTL_VER_08:
469 case RTL_VER_09:
470 r8153b_rx_agg_chg_indicate(tp);
471 break;
472 default:
473 break;
474 }
475
Ted Chen9b6dbd42016-01-20 14:24:52 +0800476 rxdy_gated_en(tp, false);
477
478 rtl8152_set_rx_mode(tp);
479
480 return 0;
481}
482
483static int rtl8152_enable(struct r8152 *tp)
484{
485 rtl_set_eee_plus(tp);
486
487 return rtl_enable(tp);
488}
489
490static void r8153_set_rx_early_timeout(struct r8152 *tp)
491{
492 u32 ocp_data = tp->coalesce / 8;
493
Hayes Wangd215ca22020-06-16 17:09:47 +0800494 switch (tp->version) {
495 case RTL_VER_03:
496 case RTL_VER_04:
497 case RTL_VER_05:
498 case RTL_VER_06:
499 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
500 ocp_data);
501 break;
502
503 case RTL_VER_08:
504 case RTL_VER_09:
505 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
506 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 1264ns.
507 */
508 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
509 RX_AUXILIARY_TIMER / 8);
510 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
511 ocp_data);
512 break;
513
514 default:
515 debug("** %s Invalid Device\n", __func__);
516 break;
517 }
Ted Chen9b6dbd42016-01-20 14:24:52 +0800518}
519
520static void r8153_set_rx_early_size(struct r8152 *tp)
521{
Hayes Wangd215ca22020-06-16 17:09:47 +0800522 u32 ocp_data = (RTL8152_AGG_BUF_SZ - RTL8153_RMS -
523 sizeof(struct rx_desc));
524
525 switch (tp->version) {
526 case RTL_VER_03:
527 case RTL_VER_04:
528 case RTL_VER_05:
529 case RTL_VER_06:
530 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
531 ocp_data / 4);
532 break;
533
534 case RTL_VER_08:
535 case RTL_VER_09:
536 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
537 ocp_data / 8);
538 break;
539
540 default:
541 debug("** %s Invalid Device\n", __func__);
542 break;
543 }
Ted Chen9b6dbd42016-01-20 14:24:52 +0800544}
545
546static int rtl8153_enable(struct r8152 *tp)
547{
548 rtl_set_eee_plus(tp);
549 r8153_set_rx_early_timeout(tp);
550 r8153_set_rx_early_size(tp);
551
552 return rtl_enable(tp);
553}
554
555static void rtl_disable(struct r8152 *tp)
556{
557 u32 ocp_data;
558
559 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
560 ocp_data &= ~RCR_ACPT_ALL;
561 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
562
563 rxdy_gated_en(tp, true);
564
565 rtl8152_wait_fifo_empty(tp);
566 rtl8152_nic_reset(tp);
567}
568
569static void r8152_power_cut_en(struct r8152 *tp, bool enable)
570{
571 u32 ocp_data;
572
573 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
574 if (enable)
575 ocp_data |= POWER_CUT;
576 else
577 ocp_data &= ~POWER_CUT;
578 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
579
580 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
581 ocp_data &= ~RESUME_INDICATE;
582 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
583}
584
585static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
586{
587 u32 ocp_data;
588
589 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
590 if (enable)
591 ocp_data |= CPCR_RX_VLAN;
592 else
593 ocp_data &= ~CPCR_RX_VLAN;
594 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
595}
596
597static void r8153_u1u2en(struct r8152 *tp, bool enable)
598{
599 u8 u1u2[8];
600
601 if (enable)
602 memset(u1u2, 0xff, sizeof(u1u2));
603 else
604 memset(u1u2, 0x00, sizeof(u1u2));
605
606 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
607}
608
Hayes Wangd215ca22020-06-16 17:09:47 +0800609static void r8153b_u1u2en(struct r8152 *tp, bool enable)
610{
611 u16 ocp_data;
612
613 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
614 if (enable)
615 ocp_data |= LPM_U1U2_EN;
616 else
617 ocp_data &= ~LPM_U1U2_EN;
618
619 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
620}
621
Ted Chen9b6dbd42016-01-20 14:24:52 +0800622static void r8153_u2p3en(struct r8152 *tp, bool enable)
623{
624 u32 ocp_data;
625
626 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
627 if (enable && tp->version != RTL_VER_03 && tp->version != RTL_VER_04)
628 ocp_data |= U2P3_ENABLE;
629 else
630 ocp_data &= ~U2P3_ENABLE;
631 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
632}
633
634static void r8153_power_cut_en(struct r8152 *tp, bool enable)
635{
636 u32 ocp_data;
637
638 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
639 if (enable)
640 ocp_data |= PWR_EN | PHASE2_EN;
641 else
642 ocp_data &= ~(PWR_EN | PHASE2_EN);
643 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
644
645 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
646 ocp_data &= ~PCUT_STATUS;
647 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
648}
649
Hayes Wang653d2e72020-06-16 17:09:44 +0800650static void rtl_reset_bmu(struct r8152 *tp)
651{
652 u8 ocp_data;
653
654 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
655 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
656 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
657 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
658 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
659}
660
Ted Chen9b6dbd42016-01-20 14:24:52 +0800661static int r8152_read_mac(struct r8152 *tp, unsigned char *macaddr)
662{
663 int ret;
664 unsigned char enetaddr[8] = {0};
665
666 ret = pla_ocp_read(tp, PLA_IDR, 8, enetaddr);
667 if (ret < 0)
668 return ret;
669
670 memcpy(macaddr, enetaddr, ETH_ALEN);
671 return 0;
672}
673
674static void r8152b_disable_aldps(struct r8152 *tp)
675{
676 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
677 mdelay(20);
678}
679
680static void r8152b_enable_aldps(struct r8152 *tp)
681{
682 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
683 LINKENA | DIS_SDSAVE);
684}
685
686static void rtl8152_disable(struct r8152 *tp)
687{
688 r8152b_disable_aldps(tp);
689 rtl_disable(tp);
690 r8152b_enable_aldps(tp);
691}
692
693static void r8152b_hw_phy_cfg(struct r8152 *tp)
694{
695 u16 data;
696
697 data = r8152_mdio_read(tp, MII_BMCR);
698 if (data & BMCR_PDOWN) {
699 data &= ~BMCR_PDOWN;
700 r8152_mdio_write(tp, MII_BMCR, data);
701 }
702
703 r8152b_firmware(tp);
704}
705
706static void rtl8152_reinit_ll(struct r8152 *tp)
707{
708 u32 ocp_data;
709 int ret;
710
711 ret = r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_PHY_PWR,
712 PLA_PHY_PWR_LLR, 1, R8152_WAIT_TIMEOUT);
713 if (ret)
714 debug("Timeout waiting for link list ready\n");
715
716 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
717 ocp_data |= RE_INIT_LL;
718 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
719
720 ret = r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_PHY_PWR,
721 PLA_PHY_PWR_LLR, 1, R8152_WAIT_TIMEOUT);
722 if (ret)
723 debug("Timeout waiting for link list ready\n");
724}
725
726static void r8152b_exit_oob(struct r8152 *tp)
727{
728 u32 ocp_data;
729
730 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
731 ocp_data &= ~RCR_ACPT_ALL;
732 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
733
734 rxdy_gated_en(tp, true);
735 r8152b_hw_phy_cfg(tp);
736
737 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
738 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
739
740 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
741 ocp_data &= ~NOW_IS_OOB;
742 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
743
744 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
745 ocp_data &= ~MCU_BORW_EN;
746 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
747
748 rtl8152_reinit_ll(tp);
749 rtl8152_nic_reset(tp);
750
751 /* rx share fifo credit full threshold */
752 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
753
754 if (tp->udev->speed == USB_SPEED_FULL ||
755 tp->udev->speed == USB_SPEED_LOW) {
756 /* rx share fifo credit near full threshold */
757 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
758 RXFIFO_THR2_FULL);
759 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
760 RXFIFO_THR3_FULL);
761 } else {
762 /* rx share fifo credit near full threshold */
763 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
764 RXFIFO_THR2_HIGH);
765 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
766 RXFIFO_THR3_HIGH);
767 }
768
769 /* TX share fifo free credit full threshold */
770 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
771
772 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
773 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
774 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
775 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
776
777 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
778
779 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
780 ocp_data |= TCR0_AUTO_FIFO;
781 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
782}
783
784static void r8152b_enter_oob(struct r8152 *tp)
785{
786 u32 ocp_data;
787
788 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
789 ocp_data &= ~NOW_IS_OOB;
790 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
791
792 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
793 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
794 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
795
796 rtl_disable(tp);
797
798 rtl8152_reinit_ll(tp);
799
800 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
801
802 rtl_rx_vlan_en(tp, false);
803
Hayes Wang1bb3ee42020-05-22 16:54:11 +0800804 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
Ted Chen9b6dbd42016-01-20 14:24:52 +0800805 ocp_data |= ALDPS_PROXY_MODE;
Hayes Wang1bb3ee42020-05-22 16:54:11 +0800806 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
Ted Chen9b6dbd42016-01-20 14:24:52 +0800807
808 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
809 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
810 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
811
812 rxdy_gated_en(tp, false);
813
814 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
815 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
816 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
817}
818
819static void r8153_hw_phy_cfg(struct r8152 *tp)
820{
821 u32 ocp_data;
822 u16 data;
823
824 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
825 tp->version == RTL_VER_05)
826 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
827
828 data = r8152_mdio_read(tp, MII_BMCR);
829 if (data & BMCR_PDOWN) {
830 data &= ~BMCR_PDOWN;
831 r8152_mdio_write(tp, MII_BMCR, data);
832 }
833
834 r8153_firmware(tp);
835
836 if (tp->version == RTL_VER_03) {
837 data = ocp_reg_read(tp, OCP_EEE_CFG);
838 data &= ~CTAP_SHORT_EN;
839 ocp_reg_write(tp, OCP_EEE_CFG, data);
840 }
841
842 data = ocp_reg_read(tp, OCP_POWER_CFG);
843 data |= EEE_CLKDIV_EN;
844 ocp_reg_write(tp, OCP_POWER_CFG, data);
845
846 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
847 data |= EN_10M_BGOFF;
848 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
849 data = ocp_reg_read(tp, OCP_POWER_CFG);
850 data |= EN_10M_PLLOFF;
851 ocp_reg_write(tp, OCP_POWER_CFG, data);
852 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
853
854 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
855 ocp_data |= PFM_PWM_SWITCH;
856 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
857
858 /* Enable LPF corner auto tune */
859 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
860
861 /* Adjust 10M Amplitude */
862 sram_write(tp, SRAM_10M_AMP1, 0x00af);
863 sram_write(tp, SRAM_10M_AMP2, 0x0208);
864}
865
Hayes Wangd215ca22020-06-16 17:09:47 +0800866static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
867{
868 u32 ocp_data;
869
870 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
871 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
872 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
873 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
874
875 return ocp_data;
876}
877
878static void r8153b_hw_phy_cfg(struct r8152 *tp)
879{
880 u32 ocp_data;
881 u16 data;
882
883 data = r8152_mdio_read(tp, MII_BMCR);
884 if (data & BMCR_PDOWN) {
885 data &= ~BMCR_PDOWN;
886 r8152_mdio_write(tp, MII_BMCR, data);
887 }
888
889 /* U1/U2/L1 idle timer. 500 us */
890 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
891
892 r8153b_firmware(tp);
893
894 data = sram_read(tp, SRAM_GREEN_CFG);
895 data |= R_TUNE_EN;
896 sram_write(tp, SRAM_GREEN_CFG, data);
897 data = ocp_reg_read(tp, OCP_NCTL_CFG);
898 data |= PGA_RETURN_EN;
899 ocp_reg_write(tp, OCP_NCTL_CFG, data);
900
901 /* ADC Bias Calibration:
902 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
903 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
904 * ADC ioffset.
905 */
906 ocp_data = r8152_efuse_read(tp, 0x7d);
907 ocp_data = ((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7);
908 if (ocp_data != 0xffff)
909 ocp_reg_write(tp, OCP_ADC_IOFFSET, ocp_data);
910
911 /* ups mode tx-link-pulse timing adjustment:
912 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
913 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
914 */
915 ocp_data = ocp_reg_read(tp, 0xc426);
916 ocp_data &= 0x3fff;
917 if (ocp_data) {
918 u32 swr_cnt_1ms_ini;
919
920 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
921 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
922 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
923 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
924 }
925
926 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
927 ocp_data |= PFM_PWM_SWITCH;
928 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
929}
930
Ted Chen9b6dbd42016-01-20 14:24:52 +0800931static void r8153_first_init(struct r8152 *tp)
932{
933 u32 ocp_data;
934
935 rxdy_gated_en(tp, true);
936
937 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
938 ocp_data &= ~RCR_ACPT_ALL;
939 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
940
941 r8153_hw_phy_cfg(tp);
942
943 rtl8152_nic_reset(tp);
Hayes Wang653d2e72020-06-16 17:09:44 +0800944 rtl_reset_bmu(tp);
Ted Chen9b6dbd42016-01-20 14:24:52 +0800945
946 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
947 ocp_data &= ~NOW_IS_OOB;
948 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
949
950 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
951 ocp_data &= ~MCU_BORW_EN;
952 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
953
954 rtl8152_reinit_ll(tp);
955
956 rtl_rx_vlan_en(tp, false);
957
958 ocp_data = RTL8153_RMS;
959 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
960 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
961
962 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
963 ocp_data |= TCR0_AUTO_FIFO;
964 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
965
966 rtl8152_nic_reset(tp);
967
968 /* rx share fifo credit full threshold */
969 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
970 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
971 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
972 /* TX share fifo free credit full threshold */
973 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
974
975 /* rx aggregation */
976 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
977
978 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
979 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
980}
981
982static void r8153_enter_oob(struct r8152 *tp)
983{
984 u32 ocp_data;
985
986 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
987 ocp_data &= ~NOW_IS_OOB;
988 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
989
990 rtl_disable(tp);
Hayes Wang653d2e72020-06-16 17:09:44 +0800991 rtl_reset_bmu(tp);
Ted Chen9b6dbd42016-01-20 14:24:52 +0800992
993 rtl8152_reinit_ll(tp);
994
995 ocp_data = RTL8153_RMS;
996 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
997
998 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
999 ocp_data &= ~TEREDO_WAKE_MASK;
1000 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
1001
1002 rtl_rx_vlan_en(tp, false);
1003
Hayes Wang1bb3ee42020-05-22 16:54:11 +08001004 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
Ted Chen9b6dbd42016-01-20 14:24:52 +08001005 ocp_data |= ALDPS_PROXY_MODE;
Hayes Wang1bb3ee42020-05-22 16:54:11 +08001006 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
Ted Chen9b6dbd42016-01-20 14:24:52 +08001007
1008 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1009 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
1010 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1011
1012 rxdy_gated_en(tp, false);
1013
1014 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1015 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
1016 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1017}
1018
1019static void r8153_disable_aldps(struct r8152 *tp)
1020{
1021 u16 data;
1022
1023 data = ocp_reg_read(tp, OCP_POWER_CFG);
1024 data &= ~EN_ALDPS;
1025 ocp_reg_write(tp, OCP_POWER_CFG, data);
1026 mdelay(20);
1027}
1028
1029static void rtl8153_disable(struct r8152 *tp)
1030{
1031 r8153_disable_aldps(tp);
1032 rtl_disable(tp);
Hayes Wang653d2e72020-06-16 17:09:44 +08001033 rtl_reset_bmu(tp);
Ted Chen9b6dbd42016-01-20 14:24:52 +08001034}
1035
1036static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
1037{
1038 u16 bmcr, anar, gbcr;
1039
1040 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1041 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1042 ADVERTISE_100HALF | ADVERTISE_100FULL);
1043 if (tp->supports_gmii) {
1044 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
1045 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1046 } else {
1047 gbcr = 0;
1048 }
1049
1050 if (autoneg == AUTONEG_DISABLE) {
1051 if (speed == SPEED_10) {
1052 bmcr = 0;
1053 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1054 } else if (speed == SPEED_100) {
1055 bmcr = BMCR_SPEED100;
1056 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1057 } else if (speed == SPEED_1000 && tp->supports_gmii) {
1058 bmcr = BMCR_SPEED1000;
1059 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
1060 } else {
1061 return -EINVAL;
1062 }
1063
1064 if (duplex == DUPLEX_FULL)
1065 bmcr |= BMCR_FULLDPLX;
1066 } else {
1067 if (speed == SPEED_10) {
1068 if (duplex == DUPLEX_FULL)
1069 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1070 else
1071 anar |= ADVERTISE_10HALF;
1072 } else if (speed == SPEED_100) {
1073 if (duplex == DUPLEX_FULL) {
1074 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1075 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1076 } else {
1077 anar |= ADVERTISE_10HALF;
1078 anar |= ADVERTISE_100HALF;
1079 }
1080 } else if (speed == SPEED_1000 && tp->supports_gmii) {
1081 if (duplex == DUPLEX_FULL) {
1082 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1083 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1084 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
1085 } else {
1086 anar |= ADVERTISE_10HALF;
1087 anar |= ADVERTISE_100HALF;
1088 gbcr |= ADVERTISE_1000HALF;
1089 }
1090 } else {
1091 return -EINVAL;
1092 }
1093
Hayes Wang27217bc2020-06-16 17:09:45 +08001094 bmcr = BMCR_ANENABLE | BMCR_ANRESTART | BMCR_RESET;
Ted Chen9b6dbd42016-01-20 14:24:52 +08001095 }
1096
1097 if (tp->supports_gmii)
1098 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
1099
1100 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1101 r8152_mdio_write(tp, MII_BMCR, bmcr);
1102
1103 return 0;
1104}
1105
1106static void rtl8152_up(struct r8152 *tp)
1107{
1108 r8152b_disable_aldps(tp);
1109 r8152b_exit_oob(tp);
1110 r8152b_enable_aldps(tp);
1111}
1112
1113static void rtl8152_down(struct r8152 *tp)
1114{
1115 r8152_power_cut_en(tp, false);
1116 r8152b_disable_aldps(tp);
1117 r8152b_enter_oob(tp);
1118 r8152b_enable_aldps(tp);
1119}
1120
1121static void rtl8153_up(struct r8152 *tp)
1122{
1123 r8153_u1u2en(tp, false);
1124 r8153_disable_aldps(tp);
1125 r8153_first_init(tp);
1126 r8153_u2p3en(tp, false);
1127}
1128
1129static void rtl8153_down(struct r8152 *tp)
1130{
1131 r8153_u1u2en(tp, false);
1132 r8153_u2p3en(tp, false);
1133 r8153_power_cut_en(tp, false);
1134 r8153_disable_aldps(tp);
1135 r8153_enter_oob(tp);
1136}
1137
Hayes Wangd215ca22020-06-16 17:09:47 +08001138static void rtl8153b_up(struct r8152 *tp)
1139{
1140 r8153_first_init(tp);
1141}
1142
1143static void rtl8153b_down(struct r8152 *tp)
1144{
1145 r8153_enter_oob(tp);
1146}
1147
Ted Chen9b6dbd42016-01-20 14:24:52 +08001148static void r8152b_get_version(struct r8152 *tp)
1149{
1150 u32 ocp_data;
1151 u16 tcr;
1152 int i;
1153
1154 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
1155 tcr = (u16)(ocp_data & VERSION_MASK);
1156
1157 for (i = 0; i < ARRAY_SIZE(r8152_versions); i++) {
1158 if (tcr == r8152_versions[i].tcr) {
1159 /* Found a supported version */
1160 tp->version = r8152_versions[i].version;
1161 tp->supports_gmii = r8152_versions[i].gmii;
1162 break;
1163 }
1164 }
1165
1166 if (tp->version == RTL_VER_UNKNOWN)
1167 debug("r8152 Unknown tcr version 0x%04x\n", tcr);
1168}
1169
1170static void r8152b_enable_fc(struct r8152 *tp)
1171{
1172 u16 anar;
1173 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1174 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1175 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1176}
1177
1178static void rtl_tally_reset(struct r8152 *tp)
1179{
1180 u32 ocp_data;
1181
1182 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
1183 ocp_data |= TALLY_RESET;
1184 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
1185}
1186
1187static void r8152b_init(struct r8152 *tp)
1188{
1189 u32 ocp_data;
1190
1191 r8152b_disable_aldps(tp);
1192
1193 if (tp->version == RTL_VER_01) {
1194 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
1195 ocp_data &= ~LED_MODE_MASK;
1196 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
1197 }
1198
1199 r8152_power_cut_en(tp, false);
1200
1201 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
1202 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
1203 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
1204 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
1205 ocp_data &= ~MCU_CLK_RATIO_MASK;
1206 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
1207 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
1208 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
1209 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
1210 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
1211
1212 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_TIMER);
1213 ocp_data |= BIT(15);
1214 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_TIMER, ocp_data);
1215 ocp_write_word(tp, MCU_TYPE_USB, 0xcbfc, 0x03e8);
1216 ocp_data &= ~BIT(15);
1217 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_TIMER, ocp_data);
1218
1219 r8152b_enable_fc(tp);
1220 rtl_tally_reset(tp);
1221
1222 /* enable rx aggregation */
1223 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
1224
1225 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
1226 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
1227}
1228
1229static void r8153_init(struct r8152 *tp)
1230{
1231 int i;
1232 u32 ocp_data;
1233
1234 r8153_disable_aldps(tp);
1235 r8153_u1u2en(tp, false);
1236
1237 r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_BOOT_CTRL,
1238 AUTOLOAD_DONE, 1, R8152_WAIT_TIMEOUT);
1239
1240 for (i = 0; i < R8152_WAIT_TIMEOUT; i++) {
1241 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
1242 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
1243 break;
1244
1245 mdelay(1);
1246 }
1247
1248 r8153_u2p3en(tp, false);
1249
1250 if (tp->version == RTL_VER_04) {
1251 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
1252 ocp_data &= ~pwd_dn_scale_mask;
1253 ocp_data |= pwd_dn_scale(96);
1254 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
1255
1256 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
1257 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
1258 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
1259 } else if (tp->version == RTL_VER_05) {
1260 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
1261 ocp_data &= ~ECM_ALDPS;
1262 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
1263
1264 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
1265 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
1266 ocp_data &= ~DYNAMIC_BURST;
1267 else
1268 ocp_data |= DYNAMIC_BURST;
1269 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
1270 } else if (tp->version == RTL_VER_06) {
1271 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
1272 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
1273 ocp_data &= ~DYNAMIC_BURST;
1274 else
1275 ocp_data |= DYNAMIC_BURST;
1276 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
1277 }
1278
1279 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
1280 ocp_data |= EP4_FULL_FC;
1281 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
1282
1283 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
1284 ocp_data &= ~TIMER11_EN;
1285 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
1286
1287 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
1288 ocp_data &= ~LED_MODE_MASK;
1289 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
1290
1291 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
1292 if (tp->version == RTL_VER_04 && tp->udev->speed != USB_SPEED_SUPER)
1293 ocp_data |= LPM_TIMER_500MS;
1294 else
1295 ocp_data |= LPM_TIMER_500US;
1296 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
1297
1298 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
1299 ocp_data &= ~SEN_VAL_MASK;
1300 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
1301 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
1302
1303 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
1304
1305 r8153_power_cut_en(tp, false);
1306
1307 r8152b_enable_fc(tp);
1308 rtl_tally_reset(tp);
1309}
1310
Hayes Wangd215ca22020-06-16 17:09:47 +08001311static void r8153b_init(struct r8152 *tp)
1312{
1313 u32 ocp_data;
1314 int i;
1315
1316 r8153_disable_aldps(tp);
1317 r8153b_u1u2en(tp, false);
1318
1319 r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_BOOT_CTRL,
1320 AUTOLOAD_DONE, 1, R8152_WAIT_TIMEOUT);
1321
1322 for (i = 0; i < R8152_WAIT_TIMEOUT; i++) {
1323 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
1324 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
1325 break;
1326
1327 mdelay(1);
1328 }
1329
1330 r8153_u2p3en(tp, false);
1331
1332 /* MSC timer = 0xfff * 8ms = 32760 ms */
1333 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
1334
1335 r8153_power_cut_en(tp, false);
1336
1337 /* MAC clock speed down */
1338 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
1339 ocp_data |= MAC_CLK_SPDWN_EN;
1340 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
1341
1342 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
1343 ocp_data &= ~PLA_MCU_SPDWN_EN;
1344 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
1345
1346 if (tp->version == RTL_VER_09) {
1347 /* Disable Test IO for 32QFN */
1348 if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) {
1349 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
1350 ocp_data |= TEST_IO_OFF;
1351 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
1352 }
1353 }
1354
1355 /* rx aggregation */
1356 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
1357 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
1358 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
1359
1360 rtl_tally_reset(tp);
1361 r8153b_hw_phy_cfg(tp);
1362 r8152b_enable_fc(tp);
1363}
1364
Ted Chen9b6dbd42016-01-20 14:24:52 +08001365static void rtl8152_unload(struct r8152 *tp)
1366{
1367 if (tp->version != RTL_VER_01)
1368 r8152_power_cut_en(tp, true);
1369}
1370
1371static void rtl8153_unload(struct r8152 *tp)
1372{
1373 r8153_power_cut_en(tp, false);
1374}
1375
1376static int rtl_ops_init(struct r8152 *tp)
1377{
1378 struct rtl_ops *ops = &tp->rtl_ops;
1379 int ret = 0;
1380
1381 switch (tp->version) {
1382 case RTL_VER_01:
1383 case RTL_VER_02:
1384 case RTL_VER_07:
1385 ops->init = r8152b_init;
1386 ops->enable = rtl8152_enable;
1387 ops->disable = rtl8152_disable;
1388 ops->up = rtl8152_up;
1389 ops->down = rtl8152_down;
1390 ops->unload = rtl8152_unload;
1391 break;
1392
1393 case RTL_VER_03:
1394 case RTL_VER_04:
1395 case RTL_VER_05:
1396 case RTL_VER_06:
1397 ops->init = r8153_init;
1398 ops->enable = rtl8153_enable;
1399 ops->disable = rtl8153_disable;
1400 ops->up = rtl8153_up;
1401 ops->down = rtl8153_down;
1402 ops->unload = rtl8153_unload;
1403 break;
1404
Hayes Wangd215ca22020-06-16 17:09:47 +08001405 case RTL_VER_08:
1406 case RTL_VER_09:
1407 ops->init = r8153b_init;
1408 ops->enable = rtl8153_enable;
1409 ops->disable = rtl8153_disable;
1410 ops->up = rtl8153b_up;
1411 ops->down = rtl8153b_down;
1412 break;
1413
Ted Chen9b6dbd42016-01-20 14:24:52 +08001414 default:
1415 ret = -ENODEV;
1416 printf("r8152 Unknown Device\n");
1417 break;
1418 }
1419
1420 return ret;
1421}
1422
Stefan Roese47c50972016-06-29 07:58:05 +02001423static int r8152_init_common(struct r8152 *tp)
Ted Chen9b6dbd42016-01-20 14:24:52 +08001424{
Ted Chen9b6dbd42016-01-20 14:24:52 +08001425 u8 speed;
1426 int timeout = 0;
1427 int link_detected;
1428
1429 debug("** %s()\n", __func__);
1430
1431 do {
1432 speed = rtl8152_get_speed(tp);
1433
1434 link_detected = speed & LINK_STATUS;
1435 if (!link_detected) {
1436 if (timeout == 0)
1437 printf("Waiting for Ethernet connection... ");
1438 mdelay(TIMEOUT_RESOLUTION);
1439 timeout += TIMEOUT_RESOLUTION;
1440 }
1441 } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT);
1442 if (link_detected) {
1443 tp->rtl_ops.enable(tp);
1444
1445 if (timeout != 0)
1446 printf("done.\n");
1447 } else {
1448 printf("unable to connect.\n");
1449 }
1450
1451 return 0;
1452}
1453
Stefan Roese47c50972016-06-29 07:58:05 +02001454static int r8152_send_common(struct ueth_data *ueth, void *packet, int length)
Ted Chen9b6dbd42016-01-20 14:24:52 +08001455{
Stefan Roese47c50972016-06-29 07:58:05 +02001456 struct usb_device *udev = ueth->pusb_dev;
Ted Chen9b6dbd42016-01-20 14:24:52 +08001457 u32 opts1, opts2 = 0;
Ted Chen9b6dbd42016-01-20 14:24:52 +08001458 int err;
Ted Chen9b6dbd42016-01-20 14:24:52 +08001459 int actual_len;
Stefan Roese01094402016-11-22 16:14:23 +01001460 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, msg,
1461 PKTSIZE + sizeof(struct tx_desc));
Ted Chen9b6dbd42016-01-20 14:24:52 +08001462 struct tx_desc *tx_desc = (struct tx_desc *)msg;
1463
1464 debug("** %s(), len %d\n", __func__, length);
1465
1466 opts1 = length | TX_FS | TX_LS;
1467
1468 tx_desc->opts2 = cpu_to_le32(opts2);
1469 tx_desc->opts1 = cpu_to_le32(opts1);
1470
1471 memcpy(msg + sizeof(struct tx_desc), (void *)packet, length);
1472
Stefan Roese47c50972016-06-29 07:58:05 +02001473 err = usb_bulk_msg(udev, usb_sndbulkpipe(udev, ueth->ep_out),
1474 (void *)msg, length + sizeof(struct tx_desc),
1475 &actual_len, USB_BULK_SEND_TIMEOUT);
Ted Chen9b6dbd42016-01-20 14:24:52 +08001476 debug("Tx: len = %zu, actual = %u, err = %d\n",
1477 length + sizeof(struct tx_desc), actual_len, err);
1478
1479 return err;
1480}
1481
Stefan Roese47c50972016-06-29 07:58:05 +02001482#ifndef CONFIG_DM_ETH
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001483static int r8152_init(struct eth_device *eth, struct bd_info *bd)
Stefan Roese47c50972016-06-29 07:58:05 +02001484{
1485 struct ueth_data *dev = (struct ueth_data *)eth->priv;
1486 struct r8152 *tp = (struct r8152 *)dev->dev_priv;
1487
1488 return r8152_init_common(tp);
1489}
1490
1491static int r8152_send(struct eth_device *eth, void *packet, int length)
1492{
1493 struct ueth_data *dev = (struct ueth_data *)eth->priv;
1494
1495 return r8152_send_common(dev, packet, length);
1496}
1497
Ted Chen9b6dbd42016-01-20 14:24:52 +08001498static int r8152_recv(struct eth_device *eth)
1499{
1500 struct ueth_data *dev = (struct ueth_data *)eth->priv;
1501
Stefan Roese01094402016-11-22 16:14:23 +01001502 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, recv_buf, RTL8152_AGG_BUF_SZ);
Ted Chen9b6dbd42016-01-20 14:24:52 +08001503 unsigned char *pkt_ptr;
1504 int err;
1505 int actual_len;
1506 u16 packet_len;
1507
1508 u32 bytes_process = 0;
1509 struct rx_desc *rx_desc;
1510
1511 debug("** %s()\n", __func__);
1512
1513 err = usb_bulk_msg(dev->pusb_dev,
1514 usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in),
1515 (void *)recv_buf,
1516 RTL8152_AGG_BUF_SZ,
1517 &actual_len,
1518 USB_BULK_RECV_TIMEOUT);
1519 debug("Rx: len = %u, actual = %u, err = %d\n", RTL8152_AGG_BUF_SZ,
1520 actual_len, err);
1521 if (err != 0) {
1522 debug("Rx: failed to receive\n");
1523 return -1;
1524 }
1525 if (actual_len > RTL8152_AGG_BUF_SZ) {
1526 debug("Rx: received too many bytes %d\n", actual_len);
1527 return -1;
1528 }
1529
1530 while (bytes_process < actual_len) {
1531 rx_desc = (struct rx_desc *)(recv_buf + bytes_process);
1532 pkt_ptr = recv_buf + sizeof(struct rx_desc) + bytes_process;
1533
1534 packet_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
1535 packet_len -= CRC_SIZE;
1536
1537 net_process_received_packet(pkt_ptr, packet_len);
1538
1539 bytes_process +=
1540 (packet_len + sizeof(struct rx_desc) + CRC_SIZE);
1541
1542 if (bytes_process % 8)
1543 bytes_process = bytes_process + 8 - (bytes_process % 8);
1544 }
1545
1546 return 0;
1547}
1548
1549static void r8152_halt(struct eth_device *eth)
1550{
1551 struct ueth_data *dev = (struct ueth_data *)eth->priv;
1552 struct r8152 *tp = (struct r8152 *)dev->dev_priv;
1553
1554 debug("** %s()\n", __func__);
1555
1556 tp->rtl_ops.disable(tp);
1557}
1558
1559static int r8152_write_hwaddr(struct eth_device *eth)
1560{
1561 struct ueth_data *dev = (struct ueth_data *)eth->priv;
1562 struct r8152 *tp = (struct r8152 *)dev->dev_priv;
1563
1564 unsigned char enetaddr[8] = {0};
1565
1566 memcpy(enetaddr, eth->enetaddr, ETH_ALEN);
1567
1568 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1569 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, enetaddr);
1570 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1571
1572 debug("MAC %pM\n", eth->enetaddr);
1573 return 0;
1574}
1575
1576void r8152_eth_before_probe(void)
1577{
1578 curr_eth_dev = 0;
1579}
1580
1581/* Probe to see if a new device is actually an realtek device */
1582int r8152_eth_probe(struct usb_device *dev, unsigned int ifnum,
1583 struct ueth_data *ss)
1584{
1585 struct usb_interface *iface;
1586 struct usb_interface_descriptor *iface_desc;
1587 int ep_in_found = 0, ep_out_found = 0;
Ted Chen9b6dbd42016-01-20 14:24:52 +08001588 struct r8152 *tp;
Hayes Wangf65b6432020-05-22 16:54:10 +08001589 int i;
Ted Chen9b6dbd42016-01-20 14:24:52 +08001590
1591 /* let's examine the device now */
1592 iface = &dev->config.if_desc[ifnum];
1593 iface_desc = &dev->config.if_desc[ifnum].desc;
1594
1595 for (i = 0; i < ARRAY_SIZE(r8152_dongles); i++) {
1596 if (dev->descriptor.idVendor == r8152_dongles[i].vendor &&
1597 dev->descriptor.idProduct == r8152_dongles[i].product)
1598 /* Found a supported dongle */
1599 break;
1600 }
1601
1602 if (i == ARRAY_SIZE(r8152_dongles))
1603 return 0;
1604
1605 memset(ss, 0, sizeof(struct ueth_data));
1606
1607 /* At this point, we know we've got a live one */
1608 debug("\n\nUSB Ethernet device detected: %#04x:%#04x\n",
1609 dev->descriptor.idVendor, dev->descriptor.idProduct);
1610
1611 /* Initialize the ueth_data structure with some useful info */
1612 ss->ifnum = ifnum;
1613 ss->pusb_dev = dev;
1614 ss->subclass = iface_desc->bInterfaceSubClass;
1615 ss->protocol = iface_desc->bInterfaceProtocol;
1616
1617 /* alloc driver private */
1618 ss->dev_priv = calloc(1, sizeof(struct r8152));
1619
1620 if (!ss->dev_priv)
1621 return 0;
1622
1623 /*
1624 * We are expecting a minimum of 3 endpoints - in, out (bulk), and
1625 * int. We will ignore any others.
1626 */
1627 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
1628 /* is it an BULK endpoint? */
1629 if ((iface->ep_desc[i].bmAttributes &
1630 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
1631 u8 ep_addr = iface->ep_desc[i].bEndpointAddress;
Hayes Wangf65b6432020-05-22 16:54:10 +08001632
1633 if (ep_addr & USB_DIR_IN) {
1634 if (!ep_in_found) {
1635 ss->ep_in = ep_addr &
1636 USB_ENDPOINT_NUMBER_MASK;
1637 ep_in_found = 1;
1638 }
Ted Chen9b6dbd42016-01-20 14:24:52 +08001639 } else {
1640 if (!ep_out_found) {
1641 ss->ep_out = ep_addr &
1642 USB_ENDPOINT_NUMBER_MASK;
1643 ep_out_found = 1;
1644 }
1645 }
1646 }
1647
1648 /* is it an interrupt endpoint? */
1649 if ((iface->ep_desc[i].bmAttributes &
1650 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
1651 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1652 USB_ENDPOINT_NUMBER_MASK;
1653 ss->irqinterval = iface->ep_desc[i].bInterval;
1654 }
1655 }
1656
1657 debug("Endpoints In %d Out %d Int %d\n",
1658 ss->ep_in, ss->ep_out, ss->ep_int);
1659
1660 /* Do some basic sanity checks, and bail if we find a problem */
1661 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
1662 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
1663 debug("Problems with device\n");
Hayes Wang5573bf52020-12-16 17:03:22 +08001664 goto error;
Ted Chen9b6dbd42016-01-20 14:24:52 +08001665 }
1666
1667 dev->privptr = (void *)ss;
1668
1669 tp = ss->dev_priv;
1670 tp->udev = dev;
1671 tp->intf = iface;
1672
1673 r8152b_get_version(tp);
1674
1675 if (rtl_ops_init(tp))
Hayes Wang5573bf52020-12-16 17:03:22 +08001676 goto error;
Ted Chen9b6dbd42016-01-20 14:24:52 +08001677
1678 tp->rtl_ops.init(tp);
1679 tp->rtl_ops.up(tp);
1680
1681 rtl8152_set_speed(tp, AUTONEG_ENABLE,
1682 tp->supports_gmii ? SPEED_1000 : SPEED_100,
1683 DUPLEX_FULL);
1684
1685 return 1;
Hayes Wang5573bf52020-12-16 17:03:22 +08001686
1687error:
1688 cfree(ss->dev_priv);
1689 ss->dev_priv = 0;
1690 return 0;
Ted Chen9b6dbd42016-01-20 14:24:52 +08001691}
1692
1693int r8152_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
1694 struct eth_device *eth)
1695{
1696 if (!eth) {
1697 debug("%s: missing parameter.\n", __func__);
1698 return 0;
1699 }
1700
1701 sprintf(eth->name, "%s#%d", R8152_BASE_NAME, curr_eth_dev++);
1702 eth->init = r8152_init;
1703 eth->send = r8152_send;
1704 eth->recv = r8152_recv;
1705 eth->halt = r8152_halt;
1706 eth->write_hwaddr = r8152_write_hwaddr;
1707 eth->priv = ss;
1708
1709 /* Get the MAC address */
1710 if (r8152_read_mac(ss->dev_priv, eth->enetaddr) < 0)
1711 return 0;
1712
1713 debug("MAC %pM\n", eth->enetaddr);
1714 return 1;
1715}
Stefan Roese47c50972016-06-29 07:58:05 +02001716#endif /* !CONFIG_DM_ETH */
1717
1718#ifdef CONFIG_DM_ETH
1719static int r8152_eth_start(struct udevice *dev)
1720{
1721 struct r8152 *tp = dev_get_priv(dev);
1722
1723 debug("** %s (%d)\n", __func__, __LINE__);
1724
1725 return r8152_init_common(tp);
1726}
1727
1728void r8152_eth_stop(struct udevice *dev)
1729{
1730 struct r8152 *tp = dev_get_priv(dev);
1731
1732 debug("** %s (%d)\n", __func__, __LINE__);
1733
1734 tp->rtl_ops.disable(tp);
1735}
1736
1737int r8152_eth_send(struct udevice *dev, void *packet, int length)
1738{
1739 struct r8152 *tp = dev_get_priv(dev);
1740
1741 return r8152_send_common(&tp->ueth, packet, length);
1742}
1743
1744int r8152_eth_recv(struct udevice *dev, int flags, uchar **packetp)
1745{
1746 struct r8152 *tp = dev_get_priv(dev);
1747 struct ueth_data *ueth = &tp->ueth;
1748 uint8_t *ptr;
1749 int ret, len;
1750 struct rx_desc *rx_desc;
1751 u16 packet_len;
1752
1753 len = usb_ether_get_rx_bytes(ueth, &ptr);
1754 debug("%s: first try, len=%d\n", __func__, len);
1755 if (!len) {
1756 if (!(flags & ETH_RECV_CHECK_DEVICE))
1757 return -EAGAIN;
1758 ret = usb_ether_receive(ueth, RTL8152_AGG_BUF_SZ);
1759 if (ret)
1760 return ret;
1761
1762 len = usb_ether_get_rx_bytes(ueth, &ptr);
1763 debug("%s: second try, len=%d\n", __func__, len);
1764 }
1765
1766 rx_desc = (struct rx_desc *)ptr;
1767 packet_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
1768 packet_len -= CRC_SIZE;
1769
1770 if (packet_len > len - (sizeof(struct rx_desc) + CRC_SIZE)) {
1771 debug("Rx: too large packet: %d\n", packet_len);
1772 goto err;
1773 }
1774
1775 *packetp = ptr + sizeof(struct rx_desc);
1776 return packet_len;
1777
1778err:
1779 usb_ether_advance_rxbuf(ueth, -1);
1780 return -ENOSPC;
1781}
1782
1783static int r8152_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
1784{
1785 struct r8152 *tp = dev_get_priv(dev);
1786
1787 packet_len += sizeof(struct rx_desc) + CRC_SIZE;
1788 packet_len = ALIGN(packet_len, 8);
1789 usb_ether_advance_rxbuf(&tp->ueth, packet_len);
1790
1791 return 0;
1792}
1793
1794static int r8152_write_hwaddr(struct udevice *dev)
1795{
Simon Glassfa20e932020-12-03 16:55:20 -07001796 struct eth_pdata *pdata = dev_get_plat(dev);
Stefan Roese47c50972016-06-29 07:58:05 +02001797 struct r8152 *tp = dev_get_priv(dev);
1798
1799 unsigned char enetaddr[8] = { 0 };
1800
1801 debug("** %s (%d)\n", __func__, __LINE__);
1802 memcpy(enetaddr, pdata->enetaddr, ETH_ALEN);
1803
1804 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1805 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, enetaddr);
1806 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1807
1808 debug("MAC %pM\n", pdata->enetaddr);
1809 return 0;
1810}
1811
1812int r8152_read_rom_hwaddr(struct udevice *dev)
1813{
Simon Glassfa20e932020-12-03 16:55:20 -07001814 struct eth_pdata *pdata = dev_get_plat(dev);
Stefan Roese47c50972016-06-29 07:58:05 +02001815 struct r8152 *tp = dev_get_priv(dev);
1816
1817 debug("** %s (%d)\n", __func__, __LINE__);
1818 r8152_read_mac(tp, pdata->enetaddr);
1819 return 0;
1820}
1821
1822static int r8152_eth_probe(struct udevice *dev)
1823{
1824 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07001825 struct eth_pdata *pdata = dev_get_plat(dev);
Stefan Roese47c50972016-06-29 07:58:05 +02001826 struct r8152 *tp = dev_get_priv(dev);
1827 struct ueth_data *ueth = &tp->ueth;
1828 int ret;
1829
1830 tp->udev = udev;
1831 r8152_read_mac(tp, pdata->enetaddr);
1832
1833 r8152b_get_version(tp);
1834
1835 ret = rtl_ops_init(tp);
1836 if (ret)
1837 return ret;
1838
1839 tp->rtl_ops.init(tp);
1840 tp->rtl_ops.up(tp);
1841
1842 rtl8152_set_speed(tp, AUTONEG_ENABLE,
1843 tp->supports_gmii ? SPEED_1000 : SPEED_100,
1844 DUPLEX_FULL);
1845
1846 return usb_ether_register(dev, ueth, RTL8152_AGG_BUF_SZ);
1847}
1848
1849static const struct eth_ops r8152_eth_ops = {
1850 .start = r8152_eth_start,
1851 .send = r8152_eth_send,
1852 .recv = r8152_eth_recv,
1853 .free_pkt = r8152_free_pkt,
1854 .stop = r8152_eth_stop,
1855 .write_hwaddr = r8152_write_hwaddr,
1856 .read_rom_hwaddr = r8152_read_rom_hwaddr,
1857};
1858
1859U_BOOT_DRIVER(r8152_eth) = {
1860 .name = "r8152_eth",
1861 .id = UCLASS_ETH,
1862 .probe = r8152_eth_probe,
1863 .ops = &r8152_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001864 .priv_auto = sizeof(struct r8152),
Simon Glass71fa5b42020-12-03 16:55:18 -07001865 .plat_auto = sizeof(struct eth_pdata),
Stefan Roese47c50972016-06-29 07:58:05 +02001866};
1867
1868static const struct usb_device_id r8152_eth_id_table[] = {
1869 /* Realtek */
1870 { USB_DEVICE(0x0bda, 0x8050) },
1871 { USB_DEVICE(0x0bda, 0x8152) },
1872 { USB_DEVICE(0x0bda, 0x8153) },
1873
1874 /* Samsung */
1875 { USB_DEVICE(0x04e8, 0xa101) },
1876
1877 /* Lenovo */
1878 { USB_DEVICE(0x17ef, 0x304f) },
1879 { USB_DEVICE(0x17ef, 0x3052) },
1880 { USB_DEVICE(0x17ef, 0x3054) },
1881 { USB_DEVICE(0x17ef, 0x3057) },
1882 { USB_DEVICE(0x17ef, 0x7205) },
1883 { USB_DEVICE(0x17ef, 0x720a) },
1884 { USB_DEVICE(0x17ef, 0x720b) },
1885 { USB_DEVICE(0x17ef, 0x720c) },
1886
1887 /* TP-LINK */
1888 { USB_DEVICE(0x2357, 0x0601) },
Oleksii Titov0c929352022-04-20 11:23:25 +03001889 { USB_DEVICE(0x2357, 0x0602) },
Stefan Roese47c50972016-06-29 07:58:05 +02001890
1891 /* Nvidia */
1892 { USB_DEVICE(0x0955, 0x09ff) },
1893
1894 { } /* Terminating entry */
1895};
1896
1897U_BOOT_USB_DEVICE(r8152_eth, r8152_eth_id_table);
1898#endif /* CONFIG_DM_ETH */