1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2017 Microchip Technology Inc. All rights reserved.
*/
#include <dm.h>
#include <usb.h>
#include "usb_ether.h"
#include "lan7x.h"
/* LAN78xx specific register/bit defines */
#define LAN78XX_HW_CFG_LED1_EN BIT(21) /* Muxed with EEDO */
#define LAN78XX_HW_CFG_LED0_EN BIT(20) /* Muxed with EECLK */
#define LAN78XX_USB_CFG0 0x080
#define LAN78XX_USB_CFG0_BIR BIT(6)
#define LAN78XX_BURST_CAP 0x090
#define LAN78XX_BULK_IN_DLY 0x094
#define LAN78XX_RFE_CTL 0x0B0
#define LAN78XX_FCT_RX_CTL 0x0C0
#define LAN78XX_FCT_TX_CTL 0x0C4
#define LAN78XX_FCT_RX_FIFO_END 0x0C8
#define LAN78XX_FCT_TX_FIFO_END 0x0CC
#define LAN78XX_FCT_FLOW 0x0D0
#define LAN78XX_MAF_BASE 0x400
#define LAN78XX_MAF_HIX 0x00
#define LAN78XX_MAF_LOX 0x04
#define LAN78XX_MAF_HI_BEGIN (LAN78XX_MAF_BASE + LAN78XX_MAF_HIX)
#define LAN78XX_MAF_LO_BEGIN (LAN78XX_MAF_BASE + LAN78XX_MAF_LOX)
#define LAN78XX_MAF_HI(index) (LAN78XX_MAF_BASE + (8 * (index)) + \
LAN78XX_MAF_HIX)
#define LAN78XX_MAF_LO(index) (LAN78XX_MAF_BASE + (8 * (index)) + \
LAN78XX_MAF_LOX)
#define LAN78XX_MAF_HI_VALID BIT(31)
/* OTP registers */
#define LAN78XX_OTP_BASE_ADDR 0x00001000
#define LAN78XX_OTP_PWR_DN (LAN78XX_OTP_BASE_ADDR + 4 * 0x00)
#define LAN78XX_OTP_PWR_DN_PWRDN_N BIT(0)
#define LAN78XX_OTP_ADDR1 (LAN78XX_OTP_BASE_ADDR + 4 * 0x01)
#define LAN78XX_OTP_ADDR1_15_11 0x1F
#define LAN78XX_OTP_ADDR2 (LAN78XX_OTP_BASE_ADDR + 4 * 0x02)
#define LAN78XX_OTP_ADDR2_10_3 0xFF
#define LAN78XX_OTP_RD_DATA (LAN78XX_OTP_BASE_ADDR + 4 * 0x06)
#define LAN78XX_OTP_FUNC_CMD (LAN78XX_OTP_BASE_ADDR + 4 * 0x08)
#define LAN78XX_OTP_FUNC_CMD_READ BIT(0)
#define LAN78XX_OTP_CMD_GO (LAN78XX_OTP_BASE_ADDR + 4 * 0x0A)
#define LAN78XX_OTP_CMD_GO_GO BIT(0)
#define LAN78XX_OTP_STATUS (LAN78XX_OTP_BASE_ADDR + 4 * 0x0C)
#define LAN78XX_OTP_STATUS_BUSY BIT(0)
#define LAN78XX_OTP_INDICATOR_1 0xF3
#define LAN78XX_OTP_INDICATOR_2 0xF7
/*
* Lan78xx infrastructure commands
*/
static int lan78xx_read_raw_otp(struct usb_device *udev, u32 offset,
u32 length, u8 *data)
{
int i;
int ret;
u32 buf;
ret = lan7x_read_reg(udev, LAN78XX_OTP_PWR_DN, &buf);
if (ret)
return ret;
if (buf & LAN78XX_OTP_PWR_DN_PWRDN_N) {
/* clear it and wait to be cleared */
ret = lan7x_write_reg(udev, LAN78XX_OTP_PWR_DN, 0);
if (ret)
return ret;
ret = lan7x_wait_for_bit(udev, "LAN78XX_OTP_PWR_DN_PWRDN_N",
LAN78XX_OTP_PWR_DN,
LAN78XX_OTP_PWR_DN_PWRDN_N,
false, 1000, 0);
if (ret)
return ret;
}
for (i = 0; i < length; i++) {
ret = lan7x_write_reg(udev, LAN78XX_OTP_ADDR1,
((offset + i) >> 8) &
LAN78XX_OTP_ADDR1_15_11);
if (ret)
return ret;
ret = lan7x_write_reg(udev, LAN78XX_OTP_ADDR2,
((offset + i) & LAN78XX_OTP_ADDR2_10_3));
if (ret)
return ret;
ret = lan7x_write_reg(udev, LAN78XX_OTP_FUNC_CMD,
LAN78XX_OTP_FUNC_CMD_READ);
if (ret)
return ret;
ret = lan7x_write_reg(udev, LAN78XX_OTP_CMD_GO,
LAN78XX_OTP_CMD_GO_GO);
if (ret)
return ret;
ret = lan7x_wait_for_bit(udev, "LAN78XX_OTP_STATUS_BUSY",
LAN78XX_OTP_STATUS,
LAN78XX_OTP_STATUS_BUSY,
false, 1000, 0);
if (ret)
return ret;
ret = lan7x_read_reg(udev, LAN78XX_OTP_RD_DATA, &buf);
if (ret)
return ret;
data[i] = (u8)(buf & 0xFF);
}
return 0;
}
static int lan78xx_read_otp(struct usb_device *udev, u32 offset,
u32 length, u8 *data)
{
u8 sig;
int ret;
ret = lan78xx_read_raw_otp(udev, 0, 1, &sig);
if (!ret) {
if (sig == LAN78XX_OTP_INDICATOR_1)
offset = offset;
else if (sig == LAN78XX_OTP_INDICATOR_2)
offset += 0x100;
else
return -EINVAL;
ret = lan78xx_read_raw_otp(udev, offset, length, data);
if (ret)
return ret;
}
debug("LAN78x: MAC address from OTP = %pM\n", data);
return ret;
}
static int lan78xx_read_otp_mac(unsigned char *enetaddr,
struct usb_device *udev)
{
int ret;
memset(enetaddr, 0, 6);
ret = lan78xx_read_otp(udev,
EEPROM_MAC_OFFSET,
ETH_ALEN,
enetaddr);
if (!ret && is_valid_ethaddr(enetaddr)) {
/* eeprom values are valid so use them */
debug("MAC address read from OTP %pM\n", enetaddr);
return 0;
}
debug("MAC address read from OTP invalid %pM\n", enetaddr);
memset(enetaddr, 0, 6);
return -EINVAL;
}
static int lan78xx_update_flowcontrol(struct usb_device *udev,
struct ueth_data *dev)
{
uint32_t flow = 0, fct_flow = 0;
int ret;
ret = lan7x_update_flowcontrol(udev, dev, &flow, &fct_flow);
if (ret)
return ret;
ret = lan7x_write_reg(udev, LAN78XX_FCT_FLOW, fct_flow);
if (ret)
return ret;
return lan7x_write_reg(udev, FLOW, flow);
}
static int lan78xx_read_mac(unsigned char *enetaddr,
struct usb_device *udev,
struct lan7x_private *priv)
{
u32 val;
int ret;
int saved = 0, done = 0;
/*
* Depends on chip, some EEPROM pins are muxed with LED function.
* disable & restore LED function to access EEPROM.
*/
if ((priv->chipid == ID_REV_CHIP_ID_7800) ||
(priv->chipid == ID_REV_CHIP_ID_7850)) {
ret = lan7x_read_reg(udev, HW_CFG, &val);
if (ret)
return ret;
saved = val;
val &= ~(LAN78XX_HW_CFG_LED1_EN | LAN78XX_HW_CFG_LED0_EN);
ret = lan7x_write_reg(udev, HW_CFG, val);
if (ret)
goto restore;
}
/*
* Refer to the doc/README.enetaddr and doc/README.usb for
* the U-Boot MAC address policy
*/
/* try reading mac address from EEPROM, then from OTP */
ret = lan7x_read_eeprom_mac(enetaddr, udev);
if (!ret)
done = 1;
restore:
if ((priv->chipid == ID_REV_CHIP_ID_7800) ||
(priv->chipid == ID_REV_CHIP_ID_7850)) {
ret = lan7x_write_reg(udev, HW_CFG, saved);
if (ret)
return ret;
}
/* if the EEPROM mac address is good, then exit */
if (done)
return 0;
/* try reading mac address from OTP if the device is LAN78xx */
return lan78xx_read_otp_mac(enetaddr, udev);
}
static int lan78xx_set_receive_filter(struct usb_device *udev)
{
/* No multicast in u-boot for now */
return lan7x_write_reg(udev, LAN78XX_RFE_CTL,
RFE_CTL_BCAST_EN | RFE_CTL_DA_PERFECT);
}
/* starts the TX path */
static void lan78xx_start_tx_path(struct usb_device *udev)
{
/* Enable Tx at MAC */
lan7x_write_reg(udev, MAC_TX, MAC_TX_TXEN);
/* Enable Tx at SCSRs */
lan7x_write_reg(udev, LAN78XX_FCT_TX_CTL, FCT_TX_CTL_EN);
}
/* Starts the Receive path */
static void lan78xx_start_rx_path(struct usb_device *udev)
{
/* Enable Rx at MAC */
lan7x_write_reg(udev, MAC_RX,
LAN7X_MAC_RX_MAX_SIZE_DEFAULT |
MAC_RX_FCS_STRIP | MAC_RX_RXEN);
/* Enable Rx at SCSRs */
lan7x_write_reg(udev, LAN78XX_FCT_RX_CTL, FCT_RX_CTL_EN);
}
static int lan78xx_basic_reset(struct usb_device *udev,
struct ueth_data *dev,
struct lan7x_private *priv)
{
int ret;
u32 val;
ret = lan7x_basic_reset(udev, dev);
if (ret)
return ret;
/* Keep the chip ID */
ret = lan7x_read_reg(udev, ID_REV, &val);
if (ret)
return ret;
debug("LAN78xx ID_REV = 0x%08x\n", val);
priv->chipid = (val & ID_REV_CHIP_ID_MASK) >> 16;
/* Respond to the IN token with a NAK */
ret = lan7x_read_reg(udev, LAN78XX_USB_CFG0, &val);
if (ret)
return ret;
val &= ~LAN78XX_USB_CFG0_BIR;
return lan7x_write_reg(udev, LAN78XX_USB_CFG0, val);
}
int lan78xx_write_hwaddr(struct udevice *dev)
{
struct usb_device *udev = dev_get_parent_priv(dev);
struct eth_pdata *pdata = dev_get_platdata(dev);
unsigned char *enetaddr = pdata->enetaddr;
u32 addr_lo = get_unaligned_le32(&enetaddr[0]);
u32 addr_hi = (u32)get_unaligned_le16(&enetaddr[4]);
int ret;
/* set hardware address */
ret = lan7x_write_reg(udev, RX_ADDRL, addr_lo);
if (ret)
return ret;
ret = lan7x_write_reg(udev, RX_ADDRH, addr_hi);
if (ret)
return ret;
ret = lan7x_write_reg(udev, LAN78XX_MAF_LO(0), addr_lo);
if (ret)
return ret;
ret = lan7x_write_reg(udev, LAN78XX_MAF_HI(0),
addr_hi | LAN78XX_MAF_HI_VALID);
if (ret)
return ret;
debug("MAC addr %pM written\n", enetaddr);
return 0;
}
static int lan78xx_eth_start(struct udevice *dev)
{
struct usb_device *udev = dev_get_parent_priv(dev);
struct lan7x_private *priv = dev_get_priv(dev);
int ret;
u32 write_buf;
/* Reset and read Mac addr were done in probe() */
ret = lan78xx_write_hwaddr(dev);
if (ret)
return ret;
ret = lan7x_write_reg(udev, LAN78XX_BURST_CAP, 0);
if (ret)
return ret;
ret = lan7x_write_reg(udev, LAN78XX_BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
if (ret)
return ret;
ret = lan7x_write_reg(udev, INT_STS, 0xFFFFFFFF);
if (ret)
return ret;
/* set FIFO sizes */
ret = lan7x_write_reg(udev, LAN78XX_FCT_RX_FIFO_END,
(MAX_RX_FIFO_SIZE - 512) / 512);
if (ret)
return ret;
ret = lan7x_write_reg(udev, LAN78XX_FCT_TX_FIFO_END,
(MAX_TX_FIFO_SIZE - 512) / 512);
if (ret)
return ret;
/* Init Tx */
ret = lan7x_write_reg(udev, FLOW, 0);
if (ret)
return ret;
/* Init Rx. Set Vlan, keep default for VLAN on 78xx */
ret = lan78xx_set_receive_filter(udev);
if (ret)
return ret;
/* Init PHY, autonego, and link */
ret = lan7x_eth_phylib_connect(dev, &priv->ueth);
if (ret)
return ret;
ret = lan7x_eth_phylib_config_start(dev);
if (ret)
return ret;
/*
* MAC_CR has to be set after PHY init.
* MAC will auto detect the PHY speed.
*/
ret = lan7x_read_reg(udev, MAC_CR, &write_buf);
if (ret)
return ret;
write_buf |= MAC_CR_AUTO_DUPLEX | MAC_CR_AUTO_SPEED | MAC_CR_ADP;
ret = lan7x_write_reg(udev, MAC_CR, write_buf);
if (ret)
return ret;
lan78xx_start_tx_path(udev);
lan78xx_start_rx_path(udev);
return lan78xx_update_flowcontrol(udev, &priv->ueth);
}
int lan78xx_read_rom_hwaddr(struct udevice *dev)
{
struct usb_device *udev = dev_get_parent_priv(dev);
struct eth_pdata *pdata = dev_get_platdata(dev);
struct lan7x_private *priv = dev_get_priv(dev);
int ret;
ret = lan78xx_read_mac(pdata->enetaddr, udev, priv);
if (ret)
memset(pdata->enetaddr, 0, 6);
return 0;
}
static int lan78xx_eth_probe(struct udevice *dev)
{
struct usb_device *udev = dev_get_parent_priv(dev);
struct lan7x_private *priv = dev_get_priv(dev);
struct ueth_data *ueth = &priv->ueth;
struct eth_pdata *pdata = dev_get_platdata(dev);
int ret;
/* Do a reset in order to get the MAC address from HW */
if (lan78xx_basic_reset(udev, ueth, priv))
return 0;
/* Get the MAC address */
/*
* We must set the eth->enetaddr from HW because the upper layer
* will force to use the environmental var (usbethaddr) or random if
* there is no valid MAC address in eth->enetaddr.
*/
lan78xx_read_mac(pdata->enetaddr, udev, priv);
/* Do not return 0 for not finding MAC addr in HW */
ret = usb_ether_register(dev, ueth, RX_URB_SIZE);
if (ret)
return ret;
/* Register phylib */
return lan7x_phylib_register(dev);
}
static const struct eth_ops lan78xx_eth_ops = {
.start = lan78xx_eth_start,
.send = lan7x_eth_send,
.recv = lan7x_eth_recv,
.free_pkt = lan7x_free_pkt,
.stop = lan7x_eth_stop,
.write_hwaddr = lan78xx_write_hwaddr,
.read_rom_hwaddr = lan78xx_read_rom_hwaddr,
};
U_BOOT_DRIVER(lan78xx_eth) = {
.name = "lan78xx_eth",
.id = UCLASS_ETH,
.probe = lan78xx_eth_probe,
.remove = lan7x_eth_remove,
.ops = &lan78xx_eth_ops,
.priv_auto_alloc_size = sizeof(struct lan7x_private),
.platdata_auto_alloc_size = sizeof(struct eth_pdata),
};
static const struct usb_device_id lan78xx_eth_id_table[] = {
{ USB_DEVICE(0x0424, 0x7800) }, /* LAN7800 USB Ethernet */
{ USB_DEVICE(0x0424, 0x7850) }, /* LAN7850 USB Ethernet */
{ } /* Terminating entry */
};
U_BOOT_USB_DEVICE(lan78xx_eth, lan78xx_eth_id_table);
|