blob: 6b59b5fcd265c5cf3a0a70a2592a0a0eca3bfa7e [file] [log] [blame]
Tim Harvey668e2052021-06-30 16:50:08 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2020
4 * Tim Harvey, Gateworks Corporation
5 */
6
7#include <dm.h>
8#include <dm/device_compat.h>
9#include <dm/device-internal.h>
10#include <dm/lists.h>
11#include <eth_phy.h>
12#include <linux/delay.h>
13#include <miiphy.h>
14#include <i2c.h>
15#include <net/dsa.h>
16
17#include <asm-generic/gpio.h>
18
19/* Global registers */
20
21/* Chip ID */
22#define REG_CHIP_ID0__1 0x0000
23
24/* Operation control */
25#define REG_SW_OPERATION 0x0300
26#define SW_RESET BIT(1)
27#define SW_START BIT(0)
28
29/* Port Specific Registers */
30#define PORT_CTRL_ADDR(port, addr) ((addr) | (((port) + 1) << 12))
31
32/* Port Control */
33#define REG_PORT_XMII_CTRL_1 0x0301
34#define PORT_MII_NOT_1GBIT BIT(6)
35#define PORT_MII_SEL_EDGE BIT(5)
36#define PORT_RGMII_ID_IG_ENABLE BIT(4)
37#define PORT_RGMII_ID_EG_ENABLE BIT(3)
38#define PORT_MII_MAC_MODE BIT(2)
39#define PORT_MII_SEL_M 0x3
40#define PORT_RGMII_SEL 0x0
41#define PORT_RMII_SEL 0x1
42#define PORT_GMII_SEL 0x2
43#define PORT_MII_SEL 0x3
44
45/* Port MSTP State Register */
46#define REG_PORT_MSTP_STATE 0x0b04
47#define PORT_TX_ENABLE BIT(2)
48#define PORT_RX_ENABLE BIT(1)
49#define PORT_LEARN_DISABLE BIT(0)
50
51/* MMD */
52#define REG_PORT_PHY_MMD_SETUP 0x011A
53#define PORT_MMD_OP_MODE_M 0x3
54#define PORT_MMD_OP_MODE_S 14
55#define PORT_MMD_OP_INDEX 0
56#define PORT_MMD_OP_DATA_NO_INCR 1
57#define PORT_MMD_OP_DATA_INCR_RW 2
58#define PORT_MMD_OP_DATA_INCR_W 3
59#define PORT_MMD_DEVICE_ID_M 0x1F
60#define MMD_SETUP(mode, dev) (((u16)(mode) << PORT_MMD_OP_MODE_S) | (dev))
61#define REG_PORT_PHY_MMD_INDEX_DATA 0x011C
62
63struct ksz_dsa_priv {
64 struct udevice *dev;
Tim Harvey668e2052021-06-30 16:50:08 -070065};
66
67static inline int ksz_read8(struct udevice *dev, u32 reg, u8 *val)
68{
69 int ret = dm_i2c_read(dev, reg, val, 1);
70
71 dev_dbg(dev, "%s 0x%04x<<0x%02x\n", __func__, reg, *val);
72
73 return ret;
74}
75
76static inline int ksz_pread8(struct udevice *dev, int port, int reg, u8 *val)
77{
78 return ksz_read8(dev, PORT_CTRL_ADDR(port, reg), val);
79}
80
81static inline int ksz_write8(struct udevice *dev, u32 reg, u8 val)
82{
83 dev_dbg(dev, "%s 0x%04x>>0x%02x\n", __func__, reg, val);
84 return dm_i2c_write(dev, reg, &val, 1);
85}
86
87static inline int ksz_pwrite8(struct udevice *dev, int port, int reg, u8 val)
88{
89 return ksz_write8(dev, PORT_CTRL_ADDR(port, reg), val);
90}
91
92static inline int ksz_write16(struct udevice *dev, u32 reg, u16 val)
93{
94 u8 buf[2];
95
96 buf[1] = val & 0xff;
97 buf[0] = val >> 8;
98 dev_dbg(dev, "%s 0x%04x>>0x%04x\n", __func__, reg, val);
99
100 return dm_i2c_write(dev, reg, buf, 2);
101}
102
103static inline int ksz_pwrite16(struct udevice *dev, int port, int reg, u16 val)
104{
105 return ksz_write16(dev, PORT_CTRL_ADDR(port, reg), val);
106}
107
108static inline int ksz_read16(struct udevice *dev, u32 reg, u16 *val)
109{
110 u8 buf[2];
111 int ret;
112
113 ret = dm_i2c_read(dev, reg, buf, 2);
114 *val = (buf[0] << 8) | buf[1];
115 dev_dbg(dev, "%s 0x%04x<<0x%04x\n", __func__, reg, *val);
116
117 return ret;
118}
119
120static inline int ksz_pread16(struct udevice *dev, int port, int reg, u16 *val)
121{
122 return ksz_read16(dev, PORT_CTRL_ADDR(port, reg), val);
123}
124
125static inline int ksz_read32(struct udevice *dev, u32 reg, u32 *val)
126{
127 return dm_i2c_read(dev, reg, (u8 *)val, 4);
128}
129
130static inline int ksz_pread32(struct udevice *dev, int port, int reg, u32 *val)
131{
132 return ksz_read32(dev, PORT_CTRL_ADDR(port, reg), val);
133}
134
135static inline int ksz_write32(struct udevice *dev, u32 reg, u32 val)
136{
137 u8 buf[4];
138
139 buf[3] = val & 0xff;
140 buf[2] = (val >> 24) & 0xff;
141 buf[1] = (val >> 16) & 0xff;
142 buf[0] = (val >> 8) & 0xff;
143 dev_dbg(dev, "%s 0x%04x>>0x%04x\n", __func__, reg, val);
144
145 return dm_i2c_write(dev, reg, buf, 4);
146}
147
148static inline int ksz_pwrite32(struct udevice *dev, int port, int reg, u32 val)
149{
150 return ksz_write32(dev, PORT_CTRL_ADDR(port, reg), val);
151}
152
153static __maybe_unused void ksz_port_mmd_read(struct udevice *dev, int port,
154 u8 addr, u16 reg, u16 *val)
155{
156 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP, MMD_SETUP(PORT_MMD_OP_INDEX, addr));
157 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, reg);
158 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP, MMD_SETUP(PORT_MMD_OP_DATA_NO_INCR, addr));
159 ksz_pread16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, val);
160 dev_dbg(dev, "%s P%d 0x%02x:0x%04x<<0x%04x\n", __func__, port + 1, addr, reg, *val);
161}
162
163static void ksz_port_mmd_write(struct udevice *dev, int port, u8 addr, u16 reg, u16 val)
164{
165 dev_dbg(dev, "%s P%d 0x%02x:0x%04x>>0x%04x\n", __func__, port + 1, addr, addr, val);
166 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP, MMD_SETUP(PORT_MMD_OP_INDEX, addr));
167 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, addr);
168 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP, MMD_SETUP(PORT_MMD_OP_DATA_NO_INCR, addr));
169 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, val);
170}
171
172/* Apply PHY settings to address errata listed in KSZ9477, KSZ9897, KSZ9896, KSZ9567
173 * Silicon Errata and Data Sheet Clarification documents
174 */
175static void ksz_phy_errata_setup(struct udevice *dev, int port)
176{
177 dev_dbg(dev, "%s P%d\n", __func__, port + 1);
178
179 /* Register settings are needed to improve PHY receive performance */
180 ksz_port_mmd_write(dev, port, 0x01, 0x6f, 0xdd0b);
181 ksz_port_mmd_write(dev, port, 0x01, 0x8f, 0x6032);
182 ksz_port_mmd_write(dev, port, 0x01, 0x9d, 0x248c);
183 ksz_port_mmd_write(dev, port, 0x01, 0x75, 0x0060);
184 ksz_port_mmd_write(dev, port, 0x01, 0xd3, 0x7777);
185 ksz_port_mmd_write(dev, port, 0x1c, 0x06, 0x3008);
186 ksz_port_mmd_write(dev, port, 0x1c, 0x08, 0x2001);
187
188 /* Transmit waveform amplitude can be improved (1000BASE-T, 100BASE-TX, 10BASE-Te) */
189 ksz_port_mmd_write(dev, port, 0x1c, 0x04, 0x00d0);
190
191 /* Energy Efficient Ethernet (EEE) feature select must be manually disabled */
192 ksz_port_mmd_write(dev, port, 0x07, 0x3c, 0x0000);
193
194 /* Register settings are required to meet data sheet supply current specifications */
195 ksz_port_mmd_write(dev, port, 0x1c, 0x13, 0x6eff);
196 ksz_port_mmd_write(dev, port, 0x1c, 0x14, 0xe6ff);
197 ksz_port_mmd_write(dev, port, 0x1c, 0x15, 0x6eff);
198 ksz_port_mmd_write(dev, port, 0x1c, 0x16, 0xe6ff);
199 ksz_port_mmd_write(dev, port, 0x1c, 0x17, 0x00ff);
200 ksz_port_mmd_write(dev, port, 0x1c, 0x18, 0x43ff);
201 ksz_port_mmd_write(dev, port, 0x1c, 0x19, 0xc3ff);
202 ksz_port_mmd_write(dev, port, 0x1c, 0x1a, 0x6fff);
203 ksz_port_mmd_write(dev, port, 0x1c, 0x1b, 0x07ff);
204 ksz_port_mmd_write(dev, port, 0x1c, 0x1c, 0x0fff);
205 ksz_port_mmd_write(dev, port, 0x1c, 0x1d, 0xe7ff);
206 ksz_port_mmd_write(dev, port, 0x1c, 0x1e, 0xefff);
207 ksz_port_mmd_write(dev, port, 0x1c, 0x20, 0xeeee);
208}
209
210/*
211 * mii bus driver
212 */
213#define KSZ_MDIO_CHILD_DRV_NAME "ksz_mdio"
214
215struct ksz_mdio_priv {
216 struct ksz_dsa_priv *ksz;
217};
218
219static int dm_ksz_mdio_read(struct udevice *dev, int addr, int devad, int reg)
220{
221 struct ksz_mdio_priv *priv = dev_get_priv(dev);
222 struct ksz_dsa_priv *ksz = priv->ksz;
223 u16 val = 0xffff;
224
225 ksz_pread16(ksz->dev, addr, 0x100 + (reg << 1), &val);
226 dev_dbg(ksz->dev, "%s P%d reg=0x%04x:0x%04x<<0x%04x\n", __func__,
227 addr + 1, reg, 0x100 + (reg << 1), val);
228
229 return val;
230};
231
232static int dm_ksz_mdio_write(struct udevice *dev, int addr, int devad, int reg, u16 val)
233{
234 struct ksz_mdio_priv *priv = dev_get_priv(dev);
235 struct ksz_dsa_priv *ksz = priv->ksz;
236
237 dev_dbg(ksz->dev, "%s P%d reg=0x%04x:%04x>>0x%04x\n",
238 __func__, addr + 1, reg, 0x100 + (reg << 1), val);
239 ksz_pwrite16(ksz->dev, addr, 0x100 + (reg << 1), val);
240
241 return 0;
242}
243
244static const struct mdio_ops ksz_mdio_ops = {
245 .read = dm_ksz_mdio_read,
246 .write = dm_ksz_mdio_write,
247};
248
249static int ksz_mdio_bind(struct udevice *dev)
250{
251 char name[16];
252 static int num_devices;
253
254 dev_dbg(dev, "%s\n", __func__);
255 sprintf(name, "ksz-mdio-%d", num_devices++);
256 device_set_name(dev, name);
257
258 return 0;
259}
260
261static int ksz_mdio_probe(struct udevice *dev)
262{
263 struct ksz_mdio_priv *priv = dev_get_priv(dev);
264
265 dev_dbg(dev, "%s\n", __func__);
266 priv->ksz = dev_get_parent_priv(dev->parent);
267
268 return 0;
269}
270
271static const struct udevice_id ksz_mdio_ids[] = {
272 { .compatible = "microchip,ksz-mdio" },
273 { }
274};
275
276U_BOOT_DRIVER(ksz_mdio) = {
277 .name = KSZ_MDIO_CHILD_DRV_NAME,
278 .id = UCLASS_MDIO,
279 .of_match = ksz_mdio_ids,
280 .bind = ksz_mdio_bind,
281 .probe = ksz_mdio_probe,
282 .ops = &ksz_mdio_ops,
283 .priv_auto = sizeof(struct ksz_mdio_priv),
284 .plat_auto = sizeof(struct mdio_perdev_priv),
285};
286
287static int ksz_port_setup(struct udevice *dev, int port,
288 phy_interface_t interface)
289{
290 struct dsa_pdata *pdata = dev_get_uclass_plat(dev);
291 u8 data8;
292
293 dev_dbg(dev, "%s P%d %s\n", __func__, port + 1,
294 (port == pdata->cpu_port) ? "cpu" : "");
295
296 if (port != pdata->cpu_port) {
297 /* phy port: config errata and leds */
298 ksz_phy_errata_setup(dev, port);
299 } else {
300 /* cpu port: configure MAC interface mode */
301 ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
302 dev_dbg(dev, "%s P%d cpu interface %s\n", __func__, port + 1,
303 phy_string_for_interface(interface));
304 switch (interface) {
305 case PHY_INTERFACE_MODE_MII:
306 data8 &= ~PORT_MII_SEL_M;
307 data8 |= PORT_MII_SEL;
308 data8 |= PORT_MII_NOT_1GBIT;
309 break;
310 case PHY_INTERFACE_MODE_RMII:
311 data8 &= ~PORT_MII_SEL_M;
312 data8 |= PORT_RMII_SEL;
313 data8 |= PORT_MII_NOT_1GBIT;
314 break;
315 case PHY_INTERFACE_MODE_GMII:
316 data8 &= ~PORT_MII_SEL_M;
317 data8 |= PORT_GMII_SEL;
318 data8 &= ~PORT_MII_NOT_1GBIT;
319 break;
320 default:
321 data8 &= ~PORT_MII_SEL_M;
322 data8 |= PORT_RGMII_SEL;
323 data8 &= ~PORT_MII_NOT_1GBIT;
324 data8 &= ~PORT_RGMII_ID_IG_ENABLE;
325 data8 &= ~PORT_RGMII_ID_EG_ENABLE;
326 if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
327 interface == PHY_INTERFACE_MODE_RGMII_RXID)
328 data8 |= PORT_RGMII_ID_IG_ENABLE;
329 if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
330 interface == PHY_INTERFACE_MODE_RGMII_TXID)
331 data8 |= PORT_RGMII_ID_EG_ENABLE;
332 break;
333 }
334 ksz_write8(dev, PORT_CTRL_ADDR(port, REG_PORT_XMII_CTRL_1), data8);
335 }
336
337 return 0;
338}
339
Tim Harvey1416b802023-02-07 15:37:18 -0800340static int ksz_port_probe(struct udevice *dev, int port, struct phy_device *phy)
341{
342 int supported = PHY_GBIT_FEATURES;
343
344 /* configure phy */
345 phy->supported &= supported;
346 phy->advertising &= supported;
347
348 return phy_config(phy);
349}
350
Tim Harvey668e2052021-06-30 16:50:08 -0700351static int ksz_port_enable(struct udevice *dev, int port, struct phy_device *phy)
352{
353 struct dsa_pdata *pdata = dev_get_uclass_plat(dev);
354 struct ksz_dsa_priv *priv = dev_get_priv(dev);
Tim Harvey668e2052021-06-30 16:50:08 -0700355 u8 data8;
356 int ret;
357
358 dev_dbg(dev, "%s P%d 0x%x %s\n", __func__, port + 1, phy->phy_id,
359 phy_string_for_interface(phy->interface));
360
361 /* setup this port */
362 ret = ksz_port_setup(dev, port, phy->interface);
363 if (ret) {
364 dev_err(dev, "port setup failed: %d\n", ret);
365 return ret;
366 }
367
368 /* enable port forwarding for this port */
369 ksz_pread8(priv->dev, port, REG_PORT_MSTP_STATE, &data8);
370 data8 &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
371 data8 |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
372 ksz_pwrite8(priv->dev, port, REG_PORT_MSTP_STATE, data8);
373
374 /* if cpu master we are done */
375 if (port == pdata->cpu_port)
376 return 0;
377
Tim Harvey668e2052021-06-30 16:50:08 -0700378 /* start switch */
379 ksz_read8(priv->dev, REG_SW_OPERATION, &data8);
380 data8 |= SW_START;
381 ksz_write8(priv->dev, REG_SW_OPERATION, data8);
382
Tim Harvey1416b802023-02-07 15:37:18 -0800383 return phy_startup(phy);
Tim Harvey668e2052021-06-30 16:50:08 -0700384}
385
386static void ksz_port_disable(struct udevice *dev, int port, struct phy_device *phy)
387{
388 struct dsa_pdata *pdata = dev_get_uclass_plat(dev);
389 struct ksz_dsa_priv *priv = dev_get_priv(dev);
390 u8 data8;
391
392 dev_dbg(dev, "%s P%d 0x%x\n", __func__, port + 1, phy->phy_id);
393
394 /* can't disable CPU port without re-configuring/re-starting switch */
395 if (port == pdata->cpu_port)
396 return;
397
398 /* disable port */
399 ksz_pread8(priv->dev, port, REG_PORT_MSTP_STATE, &data8);
400 data8 &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
401 data8 |= PORT_LEARN_DISABLE;
402 ksz_pwrite8(priv->dev, port, REG_PORT_MSTP_STATE, data8);
403
404 /*
405 * we don't call phy_shutdown here to avoid waiting next time we use
406 * the port, but the downside is that remote side will think we're
407 * actively processing traffic although we are not.
408 */
409}
410
Tim Harvey668e2052021-06-30 16:50:08 -0700411static const struct dsa_ops ksz_dsa_ops = {
Tim Harvey1416b802023-02-07 15:37:18 -0800412 .port_probe = ksz_port_probe,
Tim Harvey668e2052021-06-30 16:50:08 -0700413 .port_enable = ksz_port_enable,
414 .port_disable = ksz_port_disable,
Tim Harvey668e2052021-06-30 16:50:08 -0700415};
416
417static int ksz_probe_mdio(struct udevice *dev)
418{
419 ofnode node, mdios;
420 int ret;
421
422 mdios = dev_read_subnode(dev, "mdios");
423 if (ofnode_valid(mdios)) {
424 ofnode_for_each_subnode(node, mdios) {
425 const char *name = ofnode_get_name(node);
426 struct udevice *pdev;
427
428 ret = device_bind_driver_to_node(dev,
429 KSZ_MDIO_CHILD_DRV_NAME,
430 name, node, &pdev);
431 if (ret)
432 dev_err(dev, "failed to probe %s: %d\n", name, ret);
433 }
434 }
435
436 return 0;
437}
438
439/*
440 * I2C driver
441 */
442static int ksz_i2c_probe(struct udevice *dev)
443{
444 struct dsa_pdata *pdata = dev_get_uclass_plat(dev);
445 struct ksz_dsa_priv *priv = dev_get_priv(dev);
Tim Harvey668e2052021-06-30 16:50:08 -0700446 int i, ret;
447 u8 data8;
448 u32 id;
449
Tim Harvey668e2052021-06-30 16:50:08 -0700450 dev_set_parent_priv(dev, priv);
451
452 ret = i2c_set_chip_offset_len(dev, 2);
453 if (ret) {
454 printf("i2c_set_chip_offset_len failed: %d\n", ret);
455 return ret;
456 }
457
458 /* default config */
459 priv->dev = dev;
460
461 /* chip level reset */
462 ksz_read8(priv->dev, REG_SW_OPERATION, &data8);
463 data8 |= SW_RESET;
464 ksz_write8(priv->dev, REG_SW_OPERATION, data8);
465
466 /* read chip id */
467 ret = ksz_read32(dev, REG_CHIP_ID0__1, &id);
468 if (ret)
469 return ret;
470 id = __swab32(id);
471 dev_dbg(dev, "%s id=0x%08x\n", __func__, id);
472 switch (id & 0xffffff00) {
473 case 0x00947700:
474 puts("KSZ9477S: ");
475 break;
476 case 0x00956700:
477 puts("KSZ9567R: ");
478 break;
479 case 0x00989700:
480 puts("KSZ9897S: ");
481 break;
482 default:
483 dev_err(dev, "invalid chip id: 0x%08x\n", id);
484 return -EINVAL;
485 }
486
487 /* probe mdio bus */
488 ret = ksz_probe_mdio(dev);
489 if (ret)
490 return ret;
491
492 /* disable ports by default */
493 for (i = 0; i < pdata->num_ports; i++) {
494 ksz_pread8(priv->dev, i, REG_PORT_MSTP_STATE, &data8);
495 data8 &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
496 ksz_pwrite8(priv->dev, i, REG_PORT_MSTP_STATE, data8);
497 }
498
Tim Harvey668e2052021-06-30 16:50:08 -0700499 return 0;
500};
501
502static const struct udevice_id ksz_i2c_ids[] = {
503 { .compatible = "microchip,ksz9897" },
504 { .compatible = "microchip,ksz9477" },
505 { .compatible = "microchip,ksz9567" },
506 { }
507};
508
509U_BOOT_DRIVER(ksz) = {
510 .name = "ksz-switch",
511 .id = UCLASS_DSA,
512 .of_match = ksz_i2c_ids,
513 .probe = ksz_i2c_probe,
514 .ops = &ksz_dsa_ops,
515 .priv_auto = sizeof(struct ksz_dsa_priv),
516};