1 // SPDX-License-Identifier: GPL-2.0
3 * Vitesse VSC7385 SparX-G5 5+1-port Integrated Gigabit Ethernet Switch
4 * Vitesse VSC7388 SparX-G8 8-port Integrated Gigabit Ethernet Switch
5 * Vitesse VSC7395 SparX-G5e 5+1-port Integrated Gigabit Ethernet Switch
6 * Vitesse VSC7398 SparX-G8e 8-port Integrated Gigabit Ethernet Switch
8 * This driver takes control of the switch chip over SPI and
9 * configures it to route packages around when connected to a CPU port.
11 * Copyright (C) 2018 Linus Wallej <linus.walleij@linaro.org>
12 * Includes portions of code from the firmware uploader by:
13 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/spi/spi.h>
20 #include "vitesse-vsc73xx.h"
22 #define VSC73XX_CMD_SPI_MODE_READ 0
23 #define VSC73XX_CMD_SPI_MODE_WRITE 1
24 #define VSC73XX_CMD_SPI_MODE_SHIFT 4
25 #define VSC73XX_CMD_SPI_BLOCK_SHIFT 5
26 #define VSC73XX_CMD_SPI_BLOCK_MASK 0x7
27 #define VSC73XX_CMD_SPI_SUBBLOCK_MASK 0xf
30 * struct vsc73xx_spi - VSC73xx SPI state container
33 struct spi_device *spi;
34 struct mutex lock; /* Protects SPI traffic */
38 static const struct vsc73xx_ops vsc73xx_spi_ops;
40 static u8 vsc73xx_make_addr(u8 mode, u8 block, u8 subblock)
45 (block & VSC73XX_CMD_SPI_BLOCK_MASK) << VSC73XX_CMD_SPI_BLOCK_SHIFT;
46 ret |= (mode & 1) << VSC73XX_CMD_SPI_MODE_SHIFT;
47 ret |= subblock & VSC73XX_CMD_SPI_SUBBLOCK_MASK;
52 static int vsc73xx_spi_read(struct vsc73xx *vsc, u8 block, u8 subblock, u8 reg,
55 struct vsc73xx_spi *vsc_spi = vsc->priv;
56 struct spi_transfer t[2];
62 if (!vsc73xx_is_addr_valid(block, subblock))
67 memset(&t, 0, sizeof(t));
70 t[0].len = sizeof(cmd);
71 spi_message_add_tail(&t[0], &m);
74 t[1].len = sizeof(buf);
75 spi_message_add_tail(&t[1], &m);
77 cmd[0] = vsc73xx_make_addr(VSC73XX_CMD_SPI_MODE_READ, block, subblock);
82 mutex_lock(&vsc_spi->lock);
83 ret = spi_sync(vsc_spi->spi, &m);
84 mutex_unlock(&vsc_spi->lock);
89 *val = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
94 static int vsc73xx_spi_write(struct vsc73xx *vsc, u8 block, u8 subblock, u8 reg,
97 struct vsc73xx_spi *vsc_spi = vsc->priv;
98 struct spi_transfer t[2];
104 if (!vsc73xx_is_addr_valid(block, subblock))
107 spi_message_init(&m);
109 memset(&t, 0, sizeof(t));
112 t[0].len = sizeof(cmd);
113 spi_message_add_tail(&t[0], &m);
116 t[1].len = sizeof(buf);
117 spi_message_add_tail(&t[1], &m);
119 cmd[0] = vsc73xx_make_addr(VSC73XX_CMD_SPI_MODE_WRITE, block, subblock);
122 buf[0] = (val >> 24) & 0xff;
123 buf[1] = (val >> 16) & 0xff;
124 buf[2] = (val >> 8) & 0xff;
127 mutex_lock(&vsc_spi->lock);
128 ret = spi_sync(vsc_spi->spi, &m);
129 mutex_unlock(&vsc_spi->lock);
134 static int vsc73xx_spi_probe(struct spi_device *spi)
136 struct device *dev = &spi->dev;
137 struct vsc73xx_spi *vsc_spi;
140 vsc_spi = devm_kzalloc(dev, sizeof(*vsc_spi), GFP_KERNEL);
144 spi_set_drvdata(spi, vsc_spi);
145 vsc_spi->spi = spi_dev_get(spi);
146 vsc_spi->vsc.dev = dev;
147 vsc_spi->vsc.priv = vsc_spi;
148 vsc_spi->vsc.ops = &vsc73xx_spi_ops;
149 mutex_init(&vsc_spi->lock);
151 spi->mode = SPI_MODE_0;
152 spi->bits_per_word = 8;
153 ret = spi_setup(spi);
155 dev_err(dev, "spi setup failed.\n");
159 return vsc73xx_probe(&vsc_spi->vsc);
162 static int vsc73xx_spi_remove(struct spi_device *spi)
164 struct vsc73xx_spi *vsc_spi = spi_get_drvdata(spi);
169 vsc73xx_remove(&vsc_spi->vsc);
171 spi_set_drvdata(spi, NULL);
176 static void vsc73xx_spi_shutdown(struct spi_device *spi)
178 struct vsc73xx_spi *vsc_spi = spi_get_drvdata(spi);
183 vsc73xx_shutdown(&vsc_spi->vsc);
185 spi_set_drvdata(spi, NULL);
188 static const struct vsc73xx_ops vsc73xx_spi_ops = {
189 .read = vsc73xx_spi_read,
190 .write = vsc73xx_spi_write,
193 static const struct of_device_id vsc73xx_of_match[] = {
195 .compatible = "vitesse,vsc7385",
198 .compatible = "vitesse,vsc7388",
201 .compatible = "vitesse,vsc7395",
204 .compatible = "vitesse,vsc7398",
208 MODULE_DEVICE_TABLE(of, vsc73xx_of_match);
210 static struct spi_driver vsc73xx_spi_driver = {
211 .probe = vsc73xx_spi_probe,
212 .remove = vsc73xx_spi_remove,
213 .shutdown = vsc73xx_spi_shutdown,
215 .name = "vsc73xx-spi",
216 .of_match_table = vsc73xx_of_match,
219 module_spi_driver(vsc73xx_spi_driver);
221 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
222 MODULE_DESCRIPTION("Vitesse VSC7385/7388/7395/7398 SPI driver");
223 MODULE_LICENSE("GPL v2");