blob: fb5c76c600be878dfa06249e7c75e3401d060b9c [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
340static int ksz_port_enable(struct udevice *dev, int port, struct phy_device *phy)
341{
342 struct dsa_pdata *pdata = dev_get_uclass_plat(dev);
343 struct ksz_dsa_priv *priv = dev_get_priv(dev);
344 int supported = PHY_GBIT_FEATURES;
345 u8 data8;
346 int ret;
347
348 dev_dbg(dev, "%s P%d 0x%x %s\n", __func__, port + 1, phy->phy_id,
349 phy_string_for_interface(phy->interface));
350
351 /* setup this port */
352 ret = ksz_port_setup(dev, port, phy->interface);
353 if (ret) {
354 dev_err(dev, "port setup failed: %d\n", ret);
355 return ret;
356 }
357
358 /* enable port forwarding for this port */
359 ksz_pread8(priv->dev, port, REG_PORT_MSTP_STATE, &data8);
360 data8 &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
361 data8 |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
362 ksz_pwrite8(priv->dev, port, REG_PORT_MSTP_STATE, data8);
363
364 /* if cpu master we are done */
365 if (port == pdata->cpu_port)
366 return 0;
367
368 /* configure phy */
369 phy->supported &= supported;
370 phy->advertising &= supported;
371 ret = phy_config(phy);
372 if (ret)
373 return ret;
374
375 ret = phy_startup(phy);
376 if (ret)
377 return ret;
378
379 /* start switch */
380 ksz_read8(priv->dev, REG_SW_OPERATION, &data8);
381 data8 |= SW_START;
382 ksz_write8(priv->dev, REG_SW_OPERATION, data8);
383
Tim Harvey668e2052021-06-30 16:50:08 -0700384 return 0;
385}
386
387static void ksz_port_disable(struct udevice *dev, int port, struct phy_device *phy)
388{
389 struct dsa_pdata *pdata = dev_get_uclass_plat(dev);
390 struct ksz_dsa_priv *priv = dev_get_priv(dev);
391 u8 data8;
392
393 dev_dbg(dev, "%s P%d 0x%x\n", __func__, port + 1, phy->phy_id);
394
395 /* can't disable CPU port without re-configuring/re-starting switch */
396 if (port == pdata->cpu_port)
397 return;
398
399 /* disable port */
400 ksz_pread8(priv->dev, port, REG_PORT_MSTP_STATE, &data8);
401 data8 &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
402 data8 |= PORT_LEARN_DISABLE;
403 ksz_pwrite8(priv->dev, port, REG_PORT_MSTP_STATE, data8);
404
405 /*
406 * we don't call phy_shutdown here to avoid waiting next time we use
407 * the port, but the downside is that remote side will think we're
408 * actively processing traffic although we are not.
409 */
410}
411
Tim Harvey668e2052021-06-30 16:50:08 -0700412static const struct dsa_ops ksz_dsa_ops = {
413 .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);
446 struct udevice *master = dsa_get_master(dev);
447 int i, ret;
448 u8 data8;
449 u32 id;
450
451 if (!master)
452 return -ENODEV;
453
454 dev_dbg(dev, "%s %s master:%s\n", __func__, dev->name, master->name);
455 dev_set_parent_priv(dev, priv);
456
457 ret = i2c_set_chip_offset_len(dev, 2);
458 if (ret) {
459 printf("i2c_set_chip_offset_len failed: %d\n", ret);
460 return ret;
461 }
462
463 /* default config */
464 priv->dev = dev;
465
466 /* chip level reset */
467 ksz_read8(priv->dev, REG_SW_OPERATION, &data8);
468 data8 |= SW_RESET;
469 ksz_write8(priv->dev, REG_SW_OPERATION, data8);
470
471 /* read chip id */
472 ret = ksz_read32(dev, REG_CHIP_ID0__1, &id);
473 if (ret)
474 return ret;
475 id = __swab32(id);
476 dev_dbg(dev, "%s id=0x%08x\n", __func__, id);
477 switch (id & 0xffffff00) {
478 case 0x00947700:
479 puts("KSZ9477S: ");
480 break;
481 case 0x00956700:
482 puts("KSZ9567R: ");
483 break;
484 case 0x00989700:
485 puts("KSZ9897S: ");
486 break;
487 default:
488 dev_err(dev, "invalid chip id: 0x%08x\n", id);
489 return -EINVAL;
490 }
491
492 /* probe mdio bus */
493 ret = ksz_probe_mdio(dev);
494 if (ret)
495 return ret;
496
497 /* disable ports by default */
498 for (i = 0; i < pdata->num_ports; i++) {
499 ksz_pread8(priv->dev, i, REG_PORT_MSTP_STATE, &data8);
500 data8 &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
501 ksz_pwrite8(priv->dev, i, REG_PORT_MSTP_STATE, data8);
502 }
503
504 dsa_set_tagging(dev, 0, 0);
505
506 return 0;
507};
508
509static const struct udevice_id ksz_i2c_ids[] = {
510 { .compatible = "microchip,ksz9897" },
511 { .compatible = "microchip,ksz9477" },
512 { .compatible = "microchip,ksz9567" },
513 { }
514};
515
516U_BOOT_DRIVER(ksz) = {
517 .name = "ksz-switch",
518 .id = UCLASS_DSA,
519 .of_match = ksz_i2c_ids,
520 .probe = ksz_i2c_probe,
521 .ops = &ksz_dsa_ops,
522 .priv_auto = sizeof(struct ksz_dsa_priv),
523};