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/kernel.h>
22 #include <linux/module.h>
23 #include <linux/spi/spi.h>
24 #include <linux/platform_data/b53.h>
28 #define B53_SPI_DATA 0xf0
30 #define B53_SPI_STATUS 0xfe
31 #define B53_SPI_CMD_SPIF BIT(7)
32 #define B53_SPI_CMD_RACK BIT(5)
34 #define B53_SPI_CMD_READ 0x00
35 #define B53_SPI_CMD_WRITE 0x01
36 #define B53_SPI_CMD_NORMAL 0x60
37 #define B53_SPI_CMD_FAST 0x10
39 #define B53_SPI_PAGE_SELECT 0xff
41 static inline int b53_spi_read_reg(struct spi_device *spi, u8 reg, u8 *val,
46 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_READ;
49 return spi_write_then_read(spi, txbuf, 2, val, len);
52 static inline int b53_spi_clear_status(struct spi_device *spi)
58 for (i = 0; i < 10; i++) {
59 ret = b53_spi_read_reg(spi, B53_SPI_STATUS, &rxbuf, 1);
63 if (!(rxbuf & B53_SPI_CMD_SPIF))
75 static inline int b53_spi_set_page(struct spi_device *spi, u8 page)
79 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
80 txbuf[1] = B53_SPI_PAGE_SELECT;
83 return spi_write(spi, txbuf, sizeof(txbuf));
86 static inline int b53_prepare_reg_access(struct spi_device *spi, u8 page)
88 int ret = b53_spi_clear_status(spi);
92 return b53_spi_set_page(spi, page);
95 static int b53_spi_prepare_reg_read(struct spi_device *spi, u8 reg)
101 ret = b53_spi_read_reg(spi, reg, &rxbuf, 1);
105 for (retry_count = 0; retry_count < 10; retry_count++) {
106 ret = b53_spi_read_reg(spi, B53_SPI_STATUS, &rxbuf, 1);
110 if (rxbuf & B53_SPI_CMD_RACK)
116 if (retry_count == 10)
122 static int b53_spi_read(struct b53_device *dev, u8 page, u8 reg, u8 *data,
125 struct spi_device *spi = dev->priv;
128 ret = b53_prepare_reg_access(spi, page);
132 ret = b53_spi_prepare_reg_read(spi, reg);
136 return b53_spi_read_reg(spi, B53_SPI_DATA, data, len);
139 static int b53_spi_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
141 return b53_spi_read(dev, page, reg, val, 1);
144 static int b53_spi_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
146 int ret = b53_spi_read(dev, page, reg, (u8 *)val, 2);
148 *val = le16_to_cpu(*val);
153 static int b53_spi_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
155 int ret = b53_spi_read(dev, page, reg, (u8 *)val, 4);
157 *val = le32_to_cpu(*val);
162 static int b53_spi_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
167 ret = b53_spi_read(dev, page, reg, (u8 *)val, 6);
169 *val = le64_to_cpu(*val);
174 static int b53_spi_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
176 int ret = b53_spi_read(dev, page, reg, (u8 *)val, 8);
178 *val = le64_to_cpu(*val);
183 static int b53_spi_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
185 struct spi_device *spi = dev->priv;
189 ret = b53_prepare_reg_access(spi, page);
193 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
197 return spi_write(spi, txbuf, sizeof(txbuf));
200 static int b53_spi_write16(struct b53_device *dev, u8 page, u8 reg, u16 value)
202 struct spi_device *spi = dev->priv;
206 ret = b53_prepare_reg_access(spi, page);
210 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
212 put_unaligned_le16(value, &txbuf[2]);
214 return spi_write(spi, txbuf, sizeof(txbuf));
217 static int b53_spi_write32(struct b53_device *dev, u8 page, u8 reg, u32 value)
219 struct spi_device *spi = dev->priv;
223 ret = b53_prepare_reg_access(spi, page);
227 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
229 put_unaligned_le32(value, &txbuf[2]);
231 return spi_write(spi, txbuf, sizeof(txbuf));
234 static int b53_spi_write48(struct b53_device *dev, u8 page, u8 reg, u64 value)
236 struct spi_device *spi = dev->priv;
240 ret = b53_prepare_reg_access(spi, page);
244 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
246 put_unaligned_le64(value, &txbuf[2]);
248 return spi_write(spi, txbuf, sizeof(txbuf) - 2);
251 static int b53_spi_write64(struct b53_device *dev, u8 page, u8 reg, u64 value)
253 struct spi_device *spi = dev->priv;
257 ret = b53_prepare_reg_access(spi, page);
261 txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
263 put_unaligned_le64(value, &txbuf[2]);
265 return spi_write(spi, txbuf, sizeof(txbuf));
268 static struct b53_io_ops b53_spi_ops = {
269 .read8 = b53_spi_read8,
270 .read16 = b53_spi_read16,
271 .read32 = b53_spi_read32,
272 .read48 = b53_spi_read48,
273 .read64 = b53_spi_read64,
274 .write8 = b53_spi_write8,
275 .write16 = b53_spi_write16,
276 .write32 = b53_spi_write32,
277 .write48 = b53_spi_write48,
278 .write64 = b53_spi_write64,
281 static int __devinit b53_spi_probe(struct spi_device *spi)
283 struct b53_device *dev;
286 dev = b53_switch_alloc(&spi->dev, &b53_spi_ops, spi);
290 if (spi->dev.platform_data)
291 dev->pdata = spi->dev.platform_data;
293 ret = b53_switch_register(dev);
297 spi->dev.platform_data = dev;
302 static int __devexit b53_spi_remove(struct spi_device *spi)
304 struct b53_device *dev = spi->dev.platform_data;
307 struct b53_platform_data *pdata = dev->pdata;
308 b53_switch_remove(dev);
309 spi->dev.platform_data = pdata;
315 static struct spi_driver b53_spi_driver = {
317 .name = "b53-switch",
318 .bus = &spi_bus_type,
319 .owner = THIS_MODULE,
321 .probe = b53_spi_probe,
322 .remove = __devexit_p(b53_spi_remove),
325 module_spi_driver(b53_spi_driver);
327 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
328 MODULE_DESCRIPTION("B53 SPI access driver");
329 MODULE_LICENSE("Dual BSD/GPL");