Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / dsa / microchip / ksz_spi.c
blob8c1778b42701a9573fff89c1486df4ccdb733662
1 /*
2 * Microchip KSZ series register access through SPI
4 * Copyright (C) 2017
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <asm/unaligned.h>
21 #include <linux/delay.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/spi/spi.h>
26 #include "ksz_priv.h"
28 /* SPI frame opcodes */
29 #define KS_SPIOP_RD 3
30 #define KS_SPIOP_WR 2
32 #define SPI_ADDR_SHIFT 24
33 #define SPI_ADDR_MASK (BIT(SPI_ADDR_SHIFT) - 1)
34 #define SPI_TURNAROUND_SHIFT 5
36 static int ksz_spi_read_reg(struct spi_device *spi, u32 reg, u8 *val,
37 unsigned int len)
39 u32 txbuf;
40 int ret;
42 txbuf = reg & SPI_ADDR_MASK;
43 txbuf |= KS_SPIOP_RD << SPI_ADDR_SHIFT;
44 txbuf <<= SPI_TURNAROUND_SHIFT;
45 txbuf = cpu_to_be32(txbuf);
47 ret = spi_write_then_read(spi, &txbuf, 4, val, len);
48 return ret;
51 static int ksz_spi_read(struct ksz_device *dev, u32 reg, u8 *data,
52 unsigned int len)
54 struct spi_device *spi = dev->priv;
56 return ksz_spi_read_reg(spi, reg, data, len);
59 static int ksz_spi_read8(struct ksz_device *dev, u32 reg, u8 *val)
61 return ksz_spi_read(dev, reg, val, 1);
64 static int ksz_spi_read16(struct ksz_device *dev, u32 reg, u16 *val)
66 int ret = ksz_spi_read(dev, reg, (u8 *)val, 2);
68 if (!ret)
69 *val = be16_to_cpu(*val);
71 return ret;
74 static int ksz_spi_read24(struct ksz_device *dev, u32 reg, u32 *val)
76 int ret;
78 *val = 0;
79 ret = ksz_spi_read(dev, reg, (u8 *)val, 3);
80 if (!ret) {
81 *val = be32_to_cpu(*val);
82 /* convert to 24bit */
83 *val >>= 8;
86 return ret;
89 static int ksz_spi_read32(struct ksz_device *dev, u32 reg, u32 *val)
91 int ret = ksz_spi_read(dev, reg, (u8 *)val, 4);
93 if (!ret)
94 *val = be32_to_cpu(*val);
96 return ret;
99 static int ksz_spi_write_reg(struct spi_device *spi, u32 reg, u8 *val,
100 unsigned int len)
102 u32 txbuf;
103 u8 data[12];
104 int i;
106 txbuf = reg & SPI_ADDR_MASK;
107 txbuf |= (KS_SPIOP_WR << SPI_ADDR_SHIFT);
108 txbuf <<= SPI_TURNAROUND_SHIFT;
109 txbuf = cpu_to_be32(txbuf);
111 data[0] = txbuf & 0xFF;
112 data[1] = (txbuf & 0xFF00) >> 8;
113 data[2] = (txbuf & 0xFF0000) >> 16;
114 data[3] = (txbuf & 0xFF000000) >> 24;
115 for (i = 0; i < len; i++)
116 data[i + 4] = val[i];
118 return spi_write(spi, &data, 4 + len);
121 static int ksz_spi_write8(struct ksz_device *dev, u32 reg, u8 value)
123 struct spi_device *spi = dev->priv;
125 return ksz_spi_write_reg(spi, reg, &value, 1);
128 static int ksz_spi_write16(struct ksz_device *dev, u32 reg, u16 value)
130 struct spi_device *spi = dev->priv;
132 value = cpu_to_be16(value);
133 return ksz_spi_write_reg(spi, reg, (u8 *)&value, 2);
136 static int ksz_spi_write24(struct ksz_device *dev, u32 reg, u32 value)
138 struct spi_device *spi = dev->priv;
140 /* make it to big endian 24bit from MSB */
141 value <<= 8;
142 value = cpu_to_be32(value);
143 return ksz_spi_write_reg(spi, reg, (u8 *)&value, 3);
146 static int ksz_spi_write32(struct ksz_device *dev, u32 reg, u32 value)
148 struct spi_device *spi = dev->priv;
150 value = cpu_to_be32(value);
151 return ksz_spi_write_reg(spi, reg, (u8 *)&value, 4);
154 static const struct ksz_io_ops ksz_spi_ops = {
155 .read8 = ksz_spi_read8,
156 .read16 = ksz_spi_read16,
157 .read24 = ksz_spi_read24,
158 .read32 = ksz_spi_read32,
159 .write8 = ksz_spi_write8,
160 .write16 = ksz_spi_write16,
161 .write24 = ksz_spi_write24,
162 .write32 = ksz_spi_write32,
165 static int ksz_spi_probe(struct spi_device *spi)
167 struct ksz_device *dev;
168 int ret;
170 dev = ksz_switch_alloc(&spi->dev, &ksz_spi_ops, spi);
171 if (!dev)
172 return -ENOMEM;
174 if (spi->dev.platform_data)
175 dev->pdata = spi->dev.platform_data;
177 ret = ksz_switch_register(dev);
178 if (ret)
179 return ret;
181 spi_set_drvdata(spi, dev);
183 return 0;
186 static int ksz_spi_remove(struct spi_device *spi)
188 struct ksz_device *dev = spi_get_drvdata(spi);
190 if (dev)
191 ksz_switch_remove(dev);
193 return 0;
196 static const struct of_device_id ksz_dt_ids[] = {
197 { .compatible = "microchip,ksz9477" },
198 { .compatible = "microchip,ksz9897" },
201 MODULE_DEVICE_TABLE(of, ksz_dt_ids);
203 static struct spi_driver ksz_spi_driver = {
204 .driver = {
205 .name = "ksz9477-switch",
206 .owner = THIS_MODULE,
207 .of_match_table = of_match_ptr(ksz_dt_ids),
209 .probe = ksz_spi_probe,
210 .remove = ksz_spi_remove,
213 module_spi_driver(ksz_spi_driver);
215 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
216 MODULE_DESCRIPTION("Microchip KSZ Series Switch SPI access Driver");
217 MODULE_LICENSE("GPL");