2 * B53 register access through SPI
4 * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
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)
151 ret = b53_spi_read(dev, page, reg, (u8 *)&value, 2);
154 *val = le16_to_cpu(value);
159 static int b53_spi_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
164 ret = b53_spi_read(dev, page, reg, (u8 *)&value, 4);
167 *val = le32_to_cpu(value);
172 static int b53_spi_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
178 ret = b53_spi_read(dev, page, reg, (u8 *)&value, 6);
180 *val = le64_to_cpu(value);
185 static int b53_spi_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
190 ret = b53_spi_read(dev, page, reg, (u8 *)&value, 8);
193 *val = le64_to_cpu(value);
198 static int b53_spi_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
200 struct spi_device *spi = dev->priv;
204 ret = b53_prepare_reg_access(spi, page);
208 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
212 return spi_write(spi, txbuf, sizeof(txbuf));
215 static int b53_spi_write16(struct b53_device *dev, u8 page, u8 reg, u16 value)
217 struct spi_device *spi = dev->priv;
221 ret = b53_prepare_reg_access(spi, page);
225 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
227 put_unaligned_le16(value, &txbuf[2]);
229 return spi_write(spi, txbuf, sizeof(txbuf));
232 static int b53_spi_write32(struct b53_device *dev, u8 page, u8 reg, u32 value)
234 struct spi_device *spi = dev->priv;
238 ret = b53_prepare_reg_access(spi, page);
242 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
244 put_unaligned_le32(value, &txbuf[2]);
246 return spi_write(spi, txbuf, sizeof(txbuf));
249 static int b53_spi_write48(struct b53_device *dev, u8 page, u8 reg, u64 value)
251 struct spi_device *spi = dev->priv;
255 ret = b53_prepare_reg_access(spi, page);
259 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
261 put_unaligned_le64(value, &txbuf[2]);
263 return spi_write(spi, txbuf, sizeof(txbuf) - 2);
266 static int b53_spi_write64(struct b53_device *dev, u8 page, u8 reg, u64 value)
268 struct spi_device *spi = dev->priv;
272 ret = b53_prepare_reg_access(spi, page);
276 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
278 put_unaligned_le64(value, &txbuf[2]);
280 return spi_write(spi, txbuf, sizeof(txbuf));
283 static const struct b53_io_ops b53_spi_ops = {
284 .read8 = b53_spi_read8,
285 .read16 = b53_spi_read16,
286 .read32 = b53_spi_read32,
287 .read48 = b53_spi_read48,
288 .read64 = b53_spi_read64,
289 .write8 = b53_spi_write8,
290 .write16 = b53_spi_write16,
291 .write32 = b53_spi_write32,
292 .write48 = b53_spi_write48,
293 .write64 = b53_spi_write64,
296 static int b53_spi_probe(struct spi_device *spi)
298 struct b53_device *dev;
301 dev = b53_switch_alloc(&spi->dev, &b53_spi_ops, spi);
305 if (spi->dev.platform_data)
306 dev->pdata = spi->dev.platform_data;
308 ret = b53_switch_register(dev);
312 spi_set_drvdata(spi, dev);
317 static int b53_spi_remove(struct spi_device *spi)
319 struct b53_device *dev = spi_get_drvdata(spi);
322 b53_switch_remove(dev);
327 static struct spi_driver b53_spi_driver = {
329 .name = "b53-switch",
331 .probe = b53_spi_probe,
332 .remove = b53_spi_remove,
335 module_spi_driver(b53_spi_driver);
337 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
338 MODULE_DESCRIPTION("B53 SPI access driver");
339 MODULE_LICENSE("Dual BSD/GPL");