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