1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip KSZ8863 series register access through SMI
8 #include <linux/mod_devicetable.h>
9 #include <linux/property.h>
12 #include "ksz_common.h"
14 /* Serial Management Interface (SMI) uses the following frame format:
16 * preamble|start|Read/Write| PHY | REG |TA| Data bits | Idle
17 * |frame| OP code |address |address| | |
18 * read | 32x1´s | 01 | 00 | 1xRRR | RRRRR |Z0| 00000000DDDDDDDD | Z
19 * write| 32x1´s | 01 | 00 | 0xRRR | RRRRR |10| xxxxxxxxDDDDDDDD | Z
23 #define SMI_KSZ88XX_READ_PHY BIT(4)
25 static int ksz8863_mdio_read(void *ctx, const void *reg_buf, size_t reg_len,
26 void *val_buf, size_t val_len)
28 struct ksz_device *dev = ctx;
29 struct mdio_device *mdev;
30 u8 reg = *(u8 *)reg_buf;
36 mutex_lock_nested(&mdev->bus->mdio_lock, MDIO_MUTEX_NESTED);
37 for (i = 0; i < val_len; i++) {
40 ret = __mdiobus_read(mdev->bus, ((tmp & 0xE0) >> 5) |
41 SMI_KSZ88XX_READ_PHY, tmp);
50 mutex_unlock(&mdev->bus->mdio_lock);
55 static int ksz8863_mdio_write(void *ctx, const void *data, size_t count)
57 struct ksz_device *dev = ctx;
58 struct mdio_device *mdev;
65 val = (u8 *)(data + 4);
68 mutex_lock_nested(&mdev->bus->mdio_lock, MDIO_MUTEX_NESTED);
69 for (i = 0; i < (count - 4); i++) {
72 ret = __mdiobus_write(mdev->bus, ((tmp & 0xE0) >> 5),
79 mutex_unlock(&mdev->bus->mdio_lock);
84 static const struct regmap_bus regmap_smi[] = {
86 .read = ksz8863_mdio_read,
87 .write = ksz8863_mdio_write,
90 .read = ksz8863_mdio_read,
91 .write = ksz8863_mdio_write,
92 .val_format_endian_default = REGMAP_ENDIAN_BIG,
95 .read = ksz8863_mdio_read,
96 .write = ksz8863_mdio_write,
97 .val_format_endian_default = REGMAP_ENDIAN_BIG,
101 static const struct regmap_config ksz8863_regmap_config[] = {
107 .cache_type = REGCACHE_NONE,
108 .lock = ksz_regmap_lock,
109 .unlock = ksz_regmap_unlock,
110 .max_register = U8_MAX,
117 .cache_type = REGCACHE_NONE,
118 .lock = ksz_regmap_lock,
119 .unlock = ksz_regmap_unlock,
120 .max_register = U8_MAX,
127 .cache_type = REGCACHE_NONE,
128 .lock = ksz_regmap_lock,
129 .unlock = ksz_regmap_unlock,
130 .max_register = U8_MAX,
134 static int ksz8863_smi_probe(struct mdio_device *mdiodev)
136 struct device *ddev = &mdiodev->dev;
137 const struct ksz_chip_data *chip;
138 struct regmap_config rc;
139 struct ksz_device *dev;
143 dev = ksz_switch_alloc(&mdiodev->dev, mdiodev);
147 chip = device_get_match_data(ddev);
151 for (i = 0; i < __KSZ_NUM_REGMAPS; i++) {
152 rc = ksz8863_regmap_config[i];
153 rc.lock_arg = &dev->regmap_mutex;
154 rc.wr_table = chip->wr_table;
155 rc.rd_table = chip->rd_table;
156 dev->regmap[i] = devm_regmap_init(&mdiodev->dev,
159 if (IS_ERR(dev->regmap[i])) {
160 return dev_err_probe(&mdiodev->dev,
161 PTR_ERR(dev->regmap[i]),
162 "Failed to initialize regmap%i\n",
163 ksz8863_regmap_config[i].val_bits);
167 if (mdiodev->dev.platform_data)
168 dev->pdata = mdiodev->dev.platform_data;
170 ret = ksz_switch_register(dev);
172 /* Main DSA driver may not be started yet. */
176 dev_set_drvdata(&mdiodev->dev, dev);
181 static void ksz8863_smi_remove(struct mdio_device *mdiodev)
183 struct ksz_device *dev = dev_get_drvdata(&mdiodev->dev);
186 ksz_switch_remove(dev);
189 static void ksz8863_smi_shutdown(struct mdio_device *mdiodev)
191 struct ksz_device *dev = dev_get_drvdata(&mdiodev->dev);
194 dsa_switch_shutdown(dev->ds);
196 dev_set_drvdata(&mdiodev->dev, NULL);
199 static const struct of_device_id ksz8863_dt_ids[] = {
201 .compatible = "microchip,ksz8863",
202 .data = &ksz_switch_chips[KSZ8830]
205 .compatible = "microchip,ksz8873",
206 .data = &ksz_switch_chips[KSZ8830]
210 MODULE_DEVICE_TABLE(of, ksz8863_dt_ids);
212 static struct mdio_driver ksz8863_driver = {
213 .probe = ksz8863_smi_probe,
214 .remove = ksz8863_smi_remove,
215 .shutdown = ksz8863_smi_shutdown,
217 .name = "ksz8863-switch",
218 .of_match_table = ksz8863_dt_ids,
222 mdio_module_driver(ksz8863_driver);
225 MODULE_DESCRIPTION("Microchip KSZ8863 SMI Switch driver");
226 MODULE_LICENSE("GPL v2");