blob: 5711a59e2ac94900c985666dba8734a9ff21d9a3 [file] [log] [blame]
Michael Grzeschik60a36472021-04-27 09:09:08 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Microchip KSZ8863 series register access through SMI
4 *
5 * Copyright (C) 2019 Pengutronix, Michael Grzeschik <kernel@pengutronix.de>
6 */
7
Rob Herringf44a9012023-07-24 15:18:58 -06008#include <linux/mod_devicetable.h>
9#include <linux/property.h>
10
Michael Grzeschik60a36472021-04-27 09:09:08 +020011#include "ksz8.h"
12#include "ksz_common.h"
13
14/* Serial Management Interface (SMI) uses the following frame format:
15 *
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
20 *
21 */
22
23#define SMI_KSZ88XX_READ_PHY BIT(4)
24
25static int ksz8863_mdio_read(void *ctx, const void *reg_buf, size_t reg_len,
26 void *val_buf, size_t val_len)
27{
28 struct ksz_device *dev = ctx;
29 struct mdio_device *mdev;
30 u8 reg = *(u8 *)reg_buf;
31 u8 *val = val_buf;
Michael Grzeschik60a36472021-04-27 09:09:08 +020032 int i, ret = 0;
33
Arun Ramadoss47d82862022-06-28 22:43:26 +053034 mdev = dev->priv;
Michael Grzeschik60a36472021-04-27 09:09:08 +020035
36 mutex_lock_nested(&mdev->bus->mdio_lock, MDIO_MUTEX_NESTED);
37 for (i = 0; i < val_len; i++) {
38 int tmp = reg + i;
39
40 ret = __mdiobus_read(mdev->bus, ((tmp & 0xE0) >> 5) |
41 SMI_KSZ88XX_READ_PHY, tmp);
42 if (ret < 0)
43 goto out;
44
45 val[i] = ret;
46 }
47 ret = 0;
48
49 out:
50 mutex_unlock(&mdev->bus->mdio_lock);
51
52 return ret;
53}
54
55static int ksz8863_mdio_write(void *ctx, const void *data, size_t count)
56{
57 struct ksz_device *dev = ctx;
58 struct mdio_device *mdev;
Michael Grzeschik60a36472021-04-27 09:09:08 +020059 int i, ret = 0;
60 u32 reg;
61 u8 *val;
62
Arun Ramadoss47d82862022-06-28 22:43:26 +053063 mdev = dev->priv;
Michael Grzeschik60a36472021-04-27 09:09:08 +020064
65 val = (u8 *)(data + 4);
66 reg = *(u32 *)data;
67
68 mutex_lock_nested(&mdev->bus->mdio_lock, MDIO_MUTEX_NESTED);
69 for (i = 0; i < (count - 4); i++) {
70 int tmp = reg + i;
71
72 ret = __mdiobus_write(mdev->bus, ((tmp & 0xE0) >> 5),
73 tmp, val[i]);
74 if (ret < 0)
75 goto out;
76 }
77
78 out:
79 mutex_unlock(&mdev->bus->mdio_lock);
80
81 return ret;
82}
83
84static const struct regmap_bus regmap_smi[] = {
85 {
86 .read = ksz8863_mdio_read,
87 .write = ksz8863_mdio_write,
Michael Grzeschik60a36472021-04-27 09:09:08 +020088 },
89 {
90 .read = ksz8863_mdio_read,
91 .write = ksz8863_mdio_write,
92 .val_format_endian_default = REGMAP_ENDIAN_BIG,
Michael Grzeschik60a36472021-04-27 09:09:08 +020093 },
94 {
95 .read = ksz8863_mdio_read,
96 .write = ksz8863_mdio_write,
97 .val_format_endian_default = REGMAP_ENDIAN_BIG,
Michael Grzeschik60a36472021-04-27 09:09:08 +020098 }
99};
100
101static const struct regmap_config ksz8863_regmap_config[] = {
102 {
103 .name = "#8",
104 .reg_bits = 8,
105 .pad_bits = 24,
106 .val_bits = 8,
107 .cache_type = REGCACHE_NONE,
Michael Grzeschik60a36472021-04-27 09:09:08 +0200108 .lock = ksz_regmap_lock,
109 .unlock = ksz_regmap_unlock,
Oleksij Rempelae1ad122023-05-26 09:34:44 +0200110 .max_register = U8_MAX,
Michael Grzeschik60a36472021-04-27 09:09:08 +0200111 },
112 {
113 .name = "#16",
114 .reg_bits = 8,
115 .pad_bits = 24,
116 .val_bits = 16,
117 .cache_type = REGCACHE_NONE,
Michael Grzeschik60a36472021-04-27 09:09:08 +0200118 .lock = ksz_regmap_lock,
119 .unlock = ksz_regmap_unlock,
Oleksij Rempelae1ad122023-05-26 09:34:44 +0200120 .max_register = U8_MAX,
Michael Grzeschik60a36472021-04-27 09:09:08 +0200121 },
122 {
123 .name = "#32",
124 .reg_bits = 8,
125 .pad_bits = 24,
126 .val_bits = 32,
127 .cache_type = REGCACHE_NONE,
Michael Grzeschik60a36472021-04-27 09:09:08 +0200128 .lock = ksz_regmap_lock,
129 .unlock = ksz_regmap_unlock,
Oleksij Rempelae1ad122023-05-26 09:34:44 +0200130 .max_register = U8_MAX,
Michael Grzeschik60a36472021-04-27 09:09:08 +0200131 }
132};
133
134static int ksz8863_smi_probe(struct mdio_device *mdiodev)
135{
Oleksij Rempelae1ad122023-05-26 09:34:44 +0200136 struct device *ddev = &mdiodev->dev;
137 const struct ksz_chip_data *chip;
Michael Grzeschik60a36472021-04-27 09:09:08 +0200138 struct regmap_config rc;
139 struct ksz_device *dev;
Michael Grzeschik60a36472021-04-27 09:09:08 +0200140 int ret;
141 int i;
142
Arun Ramadoss47d82862022-06-28 22:43:26 +0530143 dev = ksz_switch_alloc(&mdiodev->dev, mdiodev);
Michael Grzeschik60a36472021-04-27 09:09:08 +0200144 if (!dev)
Oleksij Rempeld4eecfb2021-04-29 13:08:33 +0200145 return -ENOMEM;
Michael Grzeschik60a36472021-04-27 09:09:08 +0200146
Oleksij Rempelae1ad122023-05-26 09:34:44 +0200147 chip = device_get_match_data(ddev);
148 if (!chip)
149 return -EINVAL;
150
Vladimir Olteanb8311f42023-05-26 09:34:42 +0200151 for (i = 0; i < __KSZ_NUM_REGMAPS; i++) {
Michael Grzeschik60a36472021-04-27 09:09:08 +0200152 rc = ksz8863_regmap_config[i];
153 rc.lock_arg = &dev->regmap_mutex;
Oleksij Rempelae1ad122023-05-26 09:34:44 +0200154 rc.wr_table = chip->wr_table;
155 rc.rd_table = chip->rd_table;
Michael Grzeschik60a36472021-04-27 09:09:08 +0200156 dev->regmap[i] = devm_regmap_init(&mdiodev->dev,
157 &regmap_smi[i], dev,
158 &rc);
159 if (IS_ERR(dev->regmap[i])) {
Rakesh Sankaranarayanan9b183312022-11-07 14:59:22 +0530160 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);
Michael Grzeschik60a36472021-04-27 09:09:08 +0200164 }
165 }
166
167 if (mdiodev->dev.platform_data)
168 dev->pdata = mdiodev->dev.platform_data;
169
Arun Ramadossff3f3a32022-06-22 14:34:24 +0530170 ret = ksz_switch_register(dev);
Michael Grzeschik60a36472021-04-27 09:09:08 +0200171
172 /* Main DSA driver may not be started yet. */
173 if (ret)
174 return ret;
175
176 dev_set_drvdata(&mdiodev->dev, dev);
177
178 return 0;
179}
180
181static void ksz8863_smi_remove(struct mdio_device *mdiodev)
182{
183 struct ksz_device *dev = dev_get_drvdata(&mdiodev->dev);
184
185 if (dev)
186 ksz_switch_remove(dev);
Vladimir Olteanfe405302021-09-17 16:34:35 +0300187}
188
189static void ksz8863_smi_shutdown(struct mdio_device *mdiodev)
190{
191 struct ksz_device *dev = dev_get_drvdata(&mdiodev->dev);
192
193 if (dev)
194 dsa_switch_shutdown(dev->ds);
195
196 dev_set_drvdata(&mdiodev->dev, NULL);
Michael Grzeschik60a36472021-04-27 09:09:08 +0200197}
198
199static const struct of_device_id ksz8863_dt_ids[] = {
Arun Ramadosseee16b12022-05-17 15:13:27 +0530200 {
201 .compatible = "microchip,ksz8863",
202 .data = &ksz_switch_chips[KSZ8830]
203 },
204 {
205 .compatible = "microchip,ksz8873",
206 .data = &ksz_switch_chips[KSZ8830]
207 },
Michael Grzeschik60a36472021-04-27 09:09:08 +0200208 { },
209};
210MODULE_DEVICE_TABLE(of, ksz8863_dt_ids);
211
212static struct mdio_driver ksz8863_driver = {
213 .probe = ksz8863_smi_probe,
214 .remove = ksz8863_smi_remove,
Vladimir Olteanfe405302021-09-17 16:34:35 +0300215 .shutdown = ksz8863_smi_shutdown,
Michael Grzeschik60a36472021-04-27 09:09:08 +0200216 .mdiodrv.driver = {
217 .name = "ksz8863-switch",
218 .of_match_table = ksz8863_dt_ids,
219 },
220};
221
222mdio_module_driver(ksz8863_driver);
223
224MODULE_AUTHOR("Michael Grzeschik <m.grzeschik@pengutronix.de>");
225MODULE_DESCRIPTION("Microchip KSZ8863 SMI Switch driver");
226MODULE_LICENSE("GPL v2");