2 * Copyright (C) 2014-2016 Rafał Miłecki <rafal@milecki.pl>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 #include <linux/bcma/bcma.h>
16 #include <linux/spi/spi.h>
18 #include "spi-bcm53xx.h"
20 #define BCM53XXSPI_MAX_SPI_BAUD 13500000 /* 216 MHz? */
21 #define BCM53XXSPI_FLASH_WINDOW SZ_32M
23 /* The longest observed required wait was 19 ms */
24 #define BCM53XXSPI_SPE_TIMEOUT_MS 80
27 struct bcma_device
*core
;
28 struct spi_master
*master
;
29 void __iomem
*mmio_base
;
30 bool bspi
; /* Boot SPI mode with memory mapping */
33 static inline u32
bcm53xxspi_read(struct bcm53xxspi
*b53spi
, u16 offset
)
35 return bcma_read32(b53spi
->core
, offset
);
38 static inline void bcm53xxspi_write(struct bcm53xxspi
*b53spi
, u16 offset
,
41 bcma_write32(b53spi
->core
, offset
, value
);
44 static void bcm53xxspi_disable_bspi(struct bcm53xxspi
*b53spi
)
46 struct device
*dev
= &b53spi
->core
->dev
;
47 unsigned long deadline
;
53 tmp
= bcm53xxspi_read(b53spi
, B53SPI_BSPI_MAST_N_BOOT_CTRL
);
57 deadline
= jiffies
+ usecs_to_jiffies(200);
59 tmp
= bcm53xxspi_read(b53spi
, B53SPI_BSPI_BUSY_STATUS
);
61 bcm53xxspi_write(b53spi
, B53SPI_BSPI_MAST_N_BOOT_CTRL
,
68 } while (!time_after_eq(jiffies
, deadline
));
70 dev_warn(dev
, "Timeout disabling BSPI\n");
73 static void bcm53xxspi_enable_bspi(struct bcm53xxspi
*b53spi
)
80 tmp
= bcm53xxspi_read(b53spi
, B53SPI_BSPI_MAST_N_BOOT_CTRL
);
84 bcm53xxspi_write(b53spi
, B53SPI_BSPI_MAST_N_BOOT_CTRL
, 0x0);
88 static inline unsigned int bcm53xxspi_calc_timeout(size_t len
)
90 /* Do some magic calculation based on length and buad. Add 10% and 1. */
91 return (len
* 9000 / BCM53XXSPI_MAX_SPI_BAUD
* 110 / 100) + 1;
94 static int bcm53xxspi_wait(struct bcm53xxspi
*b53spi
, unsigned int timeout_ms
)
96 unsigned long deadline
;
99 /* SPE bit has to be 0 before we read MSPI STATUS */
100 deadline
= jiffies
+ msecs_to_jiffies(BCM53XXSPI_SPE_TIMEOUT_MS
);
102 tmp
= bcm53xxspi_read(b53spi
, B53SPI_MSPI_SPCR2
);
103 if (!(tmp
& B53SPI_MSPI_SPCR2_SPE
))
106 } while (!time_after_eq(jiffies
, deadline
));
108 if (tmp
& B53SPI_MSPI_SPCR2_SPE
)
112 deadline
= jiffies
+ msecs_to_jiffies(timeout_ms
);
114 tmp
= bcm53xxspi_read(b53spi
, B53SPI_MSPI_MSPI_STATUS
);
115 if (tmp
& B53SPI_MSPI_MSPI_STATUS_SPIF
) {
116 bcm53xxspi_write(b53spi
, B53SPI_MSPI_MSPI_STATUS
, 0);
122 } while (!time_after_eq(jiffies
, deadline
));
125 bcm53xxspi_write(b53spi
, B53SPI_MSPI_MSPI_STATUS
, 0);
127 pr_err("Timeout waiting for SPI to be ready!\n");
132 static void bcm53xxspi_buf_write(struct bcm53xxspi
*b53spi
, u8
*w_buf
,
133 size_t len
, bool cont
)
138 for (i
= 0; i
< len
; i
++) {
139 /* Transmit Register File MSB */
140 bcm53xxspi_write(b53spi
, B53SPI_MSPI_TXRAM
+ 4 * (i
* 2),
141 (unsigned int)w_buf
[i
]);
144 for (i
= 0; i
< len
; i
++) {
145 tmp
= B53SPI_CDRAM_CONT
| B53SPI_CDRAM_PCS_DISABLE_ALL
|
146 B53SPI_CDRAM_PCS_DSCK
;
147 if (!cont
&& i
== len
- 1)
148 tmp
&= ~B53SPI_CDRAM_CONT
;
150 /* Command Register File */
151 bcm53xxspi_write(b53spi
, B53SPI_MSPI_CDRAM
+ 4 * i
, tmp
);
154 /* Set queue pointers */
155 bcm53xxspi_write(b53spi
, B53SPI_MSPI_NEWQP
, 0);
156 bcm53xxspi_write(b53spi
, B53SPI_MSPI_ENDQP
, len
- 1);
159 bcm53xxspi_write(b53spi
, B53SPI_MSPI_WRITE_LOCK
, 1);
161 /* Start SPI transfer */
162 tmp
= bcm53xxspi_read(b53spi
, B53SPI_MSPI_SPCR2
);
163 tmp
|= B53SPI_MSPI_SPCR2_SPE
;
165 tmp
|= B53SPI_MSPI_SPCR2_CONT_AFTER_CMD
;
166 bcm53xxspi_write(b53spi
, B53SPI_MSPI_SPCR2
, tmp
);
168 /* Wait for SPI to finish */
169 bcm53xxspi_wait(b53spi
, bcm53xxspi_calc_timeout(len
));
172 bcm53xxspi_write(b53spi
, B53SPI_MSPI_WRITE_LOCK
, 0);
175 static void bcm53xxspi_buf_read(struct bcm53xxspi
*b53spi
, u8
*r_buf
,
176 size_t len
, bool cont
)
181 for (i
= 0; i
< len
; i
++) {
182 tmp
= B53SPI_CDRAM_CONT
| B53SPI_CDRAM_PCS_DISABLE_ALL
|
183 B53SPI_CDRAM_PCS_DSCK
;
184 if (!cont
&& i
== len
- 1)
185 tmp
&= ~B53SPI_CDRAM_CONT
;
187 /* Command Register File */
188 bcm53xxspi_write(b53spi
, B53SPI_MSPI_CDRAM
+ 4 * i
, tmp
);
191 /* Set queue pointers */
192 bcm53xxspi_write(b53spi
, B53SPI_MSPI_NEWQP
, 0);
193 bcm53xxspi_write(b53spi
, B53SPI_MSPI_ENDQP
, len
- 1);
196 bcm53xxspi_write(b53spi
, B53SPI_MSPI_WRITE_LOCK
, 1);
198 /* Start SPI transfer */
199 tmp
= bcm53xxspi_read(b53spi
, B53SPI_MSPI_SPCR2
);
200 tmp
|= B53SPI_MSPI_SPCR2_SPE
;
202 tmp
|= B53SPI_MSPI_SPCR2_CONT_AFTER_CMD
;
203 bcm53xxspi_write(b53spi
, B53SPI_MSPI_SPCR2
, tmp
);
205 /* Wait for SPI to finish */
206 bcm53xxspi_wait(b53spi
, bcm53xxspi_calc_timeout(len
));
209 bcm53xxspi_write(b53spi
, B53SPI_MSPI_WRITE_LOCK
, 0);
211 for (i
= 0; i
< len
; ++i
) {
212 u16 reg
= B53SPI_MSPI_RXRAM
+ 4 * (1 + i
* 2);
214 /* Data stored in the transmit register file LSB */
215 r_buf
[i
] = (u8
)bcm53xxspi_read(b53spi
, reg
);
219 static int bcm53xxspi_transfer_one(struct spi_master
*master
,
220 struct spi_device
*spi
,
221 struct spi_transfer
*t
)
223 struct bcm53xxspi
*b53spi
= spi_master_get_devdata(master
);
227 bcm53xxspi_disable_bspi(b53spi
);
230 buf
= (u8
*)t
->tx_buf
;
233 size_t to_write
= min_t(size_t, 16, left
);
234 bool cont
= !spi_transfer_is_last(master
, t
) ||
237 bcm53xxspi_buf_write(b53spi
, buf
, to_write
, cont
);
244 buf
= (u8
*)t
->rx_buf
;
247 size_t to_read
= min_t(size_t, 16, left
);
248 bool cont
= !spi_transfer_is_last(master
, t
) ||
251 bcm53xxspi_buf_read(b53spi
, buf
, to_read
, cont
);
260 static int bcm53xxspi_flash_read(struct spi_device
*spi
,
261 struct spi_flash_read_message
*msg
)
263 struct bcm53xxspi
*b53spi
= spi_master_get_devdata(spi
->master
);
266 if (msg
->from
+ msg
->len
> BCM53XXSPI_FLASH_WINDOW
)
269 bcm53xxspi_enable_bspi(b53spi
);
270 memcpy_fromio(msg
->buf
, b53spi
->mmio_base
+ msg
->from
, msg
->len
);
271 msg
->retlen
= msg
->len
;
276 /**************************************************
278 **************************************************/
280 static const struct bcma_device_id bcm53xxspi_bcma_tbl
[] = {
281 BCMA_CORE(BCMA_MANUF_BCM
, BCMA_CORE_NS_QSPI
, BCMA_ANY_REV
, BCMA_ANY_CLASS
),
284 MODULE_DEVICE_TABLE(bcma
, bcm53xxspi_bcma_tbl
);
286 static int bcm53xxspi_bcma_probe(struct bcma_device
*core
)
288 struct device
*dev
= &core
->dev
;
289 struct bcm53xxspi
*b53spi
;
290 struct spi_master
*master
;
293 if (core
->bus
->drv_cc
.core
->id
.rev
!= 42) {
294 pr_err("SPI on SoC with unsupported ChipCommon rev\n");
298 master
= spi_alloc_master(dev
, sizeof(*b53spi
));
302 b53spi
= spi_master_get_devdata(master
);
303 b53spi
->master
= master
;
307 b53spi
->mmio_base
= devm_ioremap(dev
, core
->addr_s
[0],
308 BCM53XXSPI_FLASH_WINDOW
);
310 bcm53xxspi_disable_bspi(b53spi
);
312 master
->dev
.of_node
= dev
->of_node
;
313 master
->transfer_one
= bcm53xxspi_transfer_one
;
314 if (b53spi
->mmio_base
)
315 master
->spi_flash_read
= bcm53xxspi_flash_read
;
317 bcma_set_drvdata(core
, b53spi
);
319 err
= devm_spi_register_master(dev
, master
);
321 spi_master_put(master
);
322 bcma_set_drvdata(core
, NULL
);
329 static struct bcma_driver bcm53xxspi_bcma_driver
= {
330 .name
= KBUILD_MODNAME
,
331 .id_table
= bcm53xxspi_bcma_tbl
,
332 .probe
= bcm53xxspi_bcma_probe
,
335 /**************************************************
337 **************************************************/
339 static int __init
bcm53xxspi_module_init(void)
343 err
= bcma_driver_register(&bcm53xxspi_bcma_driver
);
345 pr_err("Failed to register bcma driver: %d\n", err
);
350 static void __exit
bcm53xxspi_module_exit(void)
352 bcma_driver_unregister(&bcm53xxspi_bcma_driver
);
355 module_init(bcm53xxspi_module_init
);
356 module_exit(bcm53xxspi_module_exit
);
358 MODULE_DESCRIPTION("Broadcom BCM53xx SPI Controller driver");
359 MODULE_AUTHOR("Rafał Miłecki <zajec5@gmail.com>");
360 MODULE_LICENSE("GPL v2");