2 * B53 register access through SPI
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>
25 #include <linux/platform_data/b53.h>
29 #define B53_SPI_DATA 0xf0
31 #define B53_SPI_STATUS 0xfe
32 #define B53_SPI_CMD_SPIF BIT(7)
33 #define B53_SPI_CMD_RACK BIT(5)
35 #define B53_SPI_CMD_READ 0x00
36 #define B53_SPI_CMD_WRITE 0x01
37 #define B53_SPI_CMD_NORMAL 0x60
38 #define B53_SPI_CMD_FAST 0x10
40 #define B53_SPI_PAGE_SELECT 0xff
42 static inline int b53_spi_read_reg(struct spi_device *spi, u8 reg, u8 *val,
47 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_READ;
50 return spi_write_then_read(spi, txbuf, 2, val, len);
53 static inline int b53_spi_clear_status(struct spi_device *spi)
59 for (i = 0; i < 10; i++) {
60 ret = b53_spi_read_reg(spi, B53_SPI_STATUS, &rxbuf, 1);
64 if (!(rxbuf & B53_SPI_CMD_SPIF))
76 static inline int b53_spi_set_page(struct spi_device *spi, u8 page)
80 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
81 txbuf[1] = B53_SPI_PAGE_SELECT;
84 return spi_write(spi, txbuf, sizeof(txbuf));
87 static inline int b53_prepare_reg_access(struct spi_device *spi, u8 page)
89 int ret = b53_spi_clear_status(spi);
94 return b53_spi_set_page(spi, page);
97 static int b53_spi_prepare_reg_read(struct spi_device *spi, u8 reg)
103 ret = b53_spi_read_reg(spi, reg, &rxbuf, 1);
107 for (retry_count = 0; retry_count < 10; retry_count++) {
108 ret = b53_spi_read_reg(spi, B53_SPI_STATUS, &rxbuf, 1);
112 if (rxbuf & B53_SPI_CMD_RACK)
118 if (retry_count == 10)
124 static int b53_spi_read(struct b53_device *dev, u8 page, u8 reg, u8 *data,
127 struct spi_device *spi = dev->priv;
130 ret = b53_prepare_reg_access(spi, page);
134 ret = b53_spi_prepare_reg_read(spi, reg);
138 return b53_spi_read_reg(spi, B53_SPI_DATA, data, len);
141 static int b53_spi_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
143 return b53_spi_read(dev, page, reg, val, 1);
146 static int b53_spi_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
148 int ret = b53_spi_read(dev, page, reg, (u8 *)val, 2);
151 *val = le16_to_cpu(*val);
156 static int b53_spi_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
158 int ret = b53_spi_read(dev, page, reg, (u8 *)val, 4);
161 *val = le32_to_cpu(*val);
166 static int b53_spi_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
171 ret = b53_spi_read(dev, page, reg, (u8 *)val, 6);
173 *val = le64_to_cpu(*val);
178 static int b53_spi_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
180 int ret = b53_spi_read(dev, page, reg, (u8 *)val, 8);
183 *val = le64_to_cpu(*val);
188 static int b53_spi_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
190 struct spi_device *spi = dev->priv;
194 ret = b53_prepare_reg_access(spi, page);
198 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
202 return spi_write(spi, txbuf, sizeof(txbuf));
205 static int b53_spi_write16(struct b53_device *dev, u8 page, u8 reg, u16 value)
207 struct spi_device *spi = dev->priv;
211 ret = b53_prepare_reg_access(spi, page);
215 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
217 put_unaligned_le16(value, &txbuf[2]);
219 return spi_write(spi, txbuf, sizeof(txbuf));
222 static int b53_spi_write32(struct b53_device *dev, u8 page, u8 reg, u32 value)
224 struct spi_device *spi = dev->priv;
228 ret = b53_prepare_reg_access(spi, page);
232 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
234 put_unaligned_le32(value, &txbuf[2]);
236 return spi_write(spi, txbuf, sizeof(txbuf));
239 static int b53_spi_write48(struct b53_device *dev, u8 page, u8 reg, u64 value)
241 struct spi_device *spi = dev->priv;
245 ret = b53_prepare_reg_access(spi, page);
249 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
251 put_unaligned_le64(value, &txbuf[2]);
253 return spi_write(spi, txbuf, sizeof(txbuf) - 2);
256 static int b53_spi_write64(struct b53_device *dev, u8 page, u8 reg, u64 value)
258 struct spi_device *spi = dev->priv;
262 ret = b53_prepare_reg_access(spi, page);
266 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
268 put_unaligned_le64(value, &txbuf[2]);
270 return spi_write(spi, txbuf, sizeof(txbuf));
273 static const struct b53_io_ops b53_spi_ops = {
274 .read8 = b53_spi_read8,
275 .read16 = b53_spi_read16,
276 .read32 = b53_spi_read32,
277 .read48 = b53_spi_read48,
278 .read64 = b53_spi_read64,
279 .write8 = b53_spi_write8,
280 .write16 = b53_spi_write16,
281 .write32 = b53_spi_write32,
282 .write48 = b53_spi_write48,
283 .write64 = b53_spi_write64,
286 static int b53_spi_probe(struct spi_device *spi)
288 struct b53_device *dev;
291 dev = b53_switch_alloc(&spi->dev, &b53_spi_ops, spi);
295 if (spi->dev.platform_data)
296 dev->pdata = spi->dev.platform_data;
298 ret = b53_switch_register(dev);
302 spi_set_drvdata(spi, dev);
307 static int b53_spi_remove(struct spi_device *spi)
309 struct b53_device *dev = spi_get_drvdata(spi);
312 b53_switch_remove(dev);
317 static struct spi_driver b53_spi_driver = {
319 .name = "b53-switch",
321 .probe = b53_spi_probe,
322 .remove = b53_spi_remove,
325 module_spi_driver(b53_spi_driver);
328 MODULE_DESCRIPTION("B53 SPI access driver");
329 MODULE_LICENSE("Dual BSD/GPL");