1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/slab.h>
6 #include <linux/delay.h>
7 #include <linux/bcma/bcma.h>
8 #include <linux/spi/spi.h>
10 #include "spi-bcm53xx.h"
12 #define BCM53XXSPI_MAX_SPI_BAUD 13500000 /* 216 MHz? */
13 #define BCM53XXSPI_FLASH_WINDOW SZ_32M
15 /* The longest observed required wait was 19 ms */
16 #define BCM53XXSPI_SPE_TIMEOUT_MS 80
19 struct bcma_device
*core
;
20 struct spi_master
*master
;
21 void __iomem
*mmio_base
;
24 bool bspi
; /* Boot SPI mode with memory mapping */
27 static inline u32
bcm53xxspi_read(struct bcm53xxspi
*b53spi
, u16 offset
)
29 return bcma_read32(b53spi
->core
, offset
);
32 static inline void bcm53xxspi_write(struct bcm53xxspi
*b53spi
, u16 offset
,
35 bcma_write32(b53spi
->core
, offset
, value
);
38 static void bcm53xxspi_disable_bspi(struct bcm53xxspi
*b53spi
)
40 struct device
*dev
= &b53spi
->core
->dev
;
41 unsigned long deadline
;
47 tmp
= bcm53xxspi_read(b53spi
, B53SPI_BSPI_MAST_N_BOOT_CTRL
);
51 deadline
= jiffies
+ usecs_to_jiffies(200);
53 tmp
= bcm53xxspi_read(b53spi
, B53SPI_BSPI_BUSY_STATUS
);
55 bcm53xxspi_write(b53spi
, B53SPI_BSPI_MAST_N_BOOT_CTRL
,
62 } while (!time_after_eq(jiffies
, deadline
));
64 dev_warn(dev
, "Timeout disabling BSPI\n");
67 static void bcm53xxspi_enable_bspi(struct bcm53xxspi
*b53spi
)
74 tmp
= bcm53xxspi_read(b53spi
, B53SPI_BSPI_MAST_N_BOOT_CTRL
);
78 bcm53xxspi_write(b53spi
, B53SPI_BSPI_MAST_N_BOOT_CTRL
, 0x0);
82 static inline unsigned int bcm53xxspi_calc_timeout(size_t len
)
84 /* Do some magic calculation based on length and buad. Add 10% and 1. */
85 return (len
* 9000 / BCM53XXSPI_MAX_SPI_BAUD
* 110 / 100) + 1;
88 static int bcm53xxspi_wait(struct bcm53xxspi
*b53spi
, unsigned int timeout_ms
)
90 unsigned long deadline
;
93 /* SPE bit has to be 0 before we read MSPI STATUS */
94 deadline
= jiffies
+ msecs_to_jiffies(BCM53XXSPI_SPE_TIMEOUT_MS
);
96 tmp
= bcm53xxspi_read(b53spi
, B53SPI_MSPI_SPCR2
);
97 if (!(tmp
& B53SPI_MSPI_SPCR2_SPE
))
100 } while (!time_after_eq(jiffies
, deadline
));
102 if (tmp
& B53SPI_MSPI_SPCR2_SPE
)
106 deadline
= jiffies
+ msecs_to_jiffies(timeout_ms
);
108 tmp
= bcm53xxspi_read(b53spi
, B53SPI_MSPI_MSPI_STATUS
);
109 if (tmp
& B53SPI_MSPI_MSPI_STATUS_SPIF
) {
110 bcm53xxspi_write(b53spi
, B53SPI_MSPI_MSPI_STATUS
, 0);
116 } while (!time_after_eq(jiffies
, deadline
));
119 bcm53xxspi_write(b53spi
, B53SPI_MSPI_MSPI_STATUS
, 0);
121 pr_err("Timeout waiting for SPI to be ready!\n");
126 static void bcm53xxspi_buf_write(struct bcm53xxspi
*b53spi
, u8
*w_buf
,
127 size_t len
, bool cont
)
132 for (i
= 0; i
< len
; i
++) {
133 /* Transmit Register File MSB */
134 bcm53xxspi_write(b53spi
, B53SPI_MSPI_TXRAM
+ 4 * (i
* 2),
135 (unsigned int)w_buf
[i
]);
138 for (i
= 0; i
< len
; i
++) {
139 tmp
= B53SPI_CDRAM_CONT
| B53SPI_CDRAM_PCS_DISABLE_ALL
|
140 B53SPI_CDRAM_PCS_DSCK
;
141 if (!cont
&& i
== len
- 1)
142 tmp
&= ~B53SPI_CDRAM_CONT
;
144 /* Command Register File */
145 bcm53xxspi_write(b53spi
, B53SPI_MSPI_CDRAM
+ 4 * i
, tmp
);
148 /* Set queue pointers */
149 bcm53xxspi_write(b53spi
, B53SPI_MSPI_NEWQP
, 0);
150 bcm53xxspi_write(b53spi
, B53SPI_MSPI_ENDQP
, len
- 1);
153 bcm53xxspi_write(b53spi
, B53SPI_MSPI_WRITE_LOCK
, 1);
155 /* Start SPI transfer */
156 tmp
= bcm53xxspi_read(b53spi
, B53SPI_MSPI_SPCR2
);
157 tmp
|= B53SPI_MSPI_SPCR2_SPE
;
159 tmp
|= B53SPI_MSPI_SPCR2_CONT_AFTER_CMD
;
160 bcm53xxspi_write(b53spi
, B53SPI_MSPI_SPCR2
, tmp
);
162 /* Wait for SPI to finish */
163 bcm53xxspi_wait(b53spi
, bcm53xxspi_calc_timeout(len
));
166 bcm53xxspi_write(b53spi
, B53SPI_MSPI_WRITE_LOCK
, 0);
168 b53spi
->read_offset
= len
;
171 static void bcm53xxspi_buf_read(struct bcm53xxspi
*b53spi
, u8
*r_buf
,
172 size_t len
, bool cont
)
177 for (i
= 0; i
< b53spi
->read_offset
+ len
; i
++) {
178 tmp
= B53SPI_CDRAM_CONT
| B53SPI_CDRAM_PCS_DISABLE_ALL
|
179 B53SPI_CDRAM_PCS_DSCK
;
180 if (!cont
&& i
== b53spi
->read_offset
+ len
- 1)
181 tmp
&= ~B53SPI_CDRAM_CONT
;
183 /* Command Register File */
184 bcm53xxspi_write(b53spi
, B53SPI_MSPI_CDRAM
+ 4 * i
, tmp
);
187 /* Set queue pointers */
188 bcm53xxspi_write(b53spi
, B53SPI_MSPI_NEWQP
, 0);
189 bcm53xxspi_write(b53spi
, B53SPI_MSPI_ENDQP
,
190 b53spi
->read_offset
+ len
- 1);
193 bcm53xxspi_write(b53spi
, B53SPI_MSPI_WRITE_LOCK
, 1);
195 /* Start SPI transfer */
196 tmp
= bcm53xxspi_read(b53spi
, B53SPI_MSPI_SPCR2
);
197 tmp
|= B53SPI_MSPI_SPCR2_SPE
;
199 tmp
|= B53SPI_MSPI_SPCR2_CONT_AFTER_CMD
;
200 bcm53xxspi_write(b53spi
, B53SPI_MSPI_SPCR2
, tmp
);
202 /* Wait for SPI to finish */
203 bcm53xxspi_wait(b53spi
, bcm53xxspi_calc_timeout(len
));
206 bcm53xxspi_write(b53spi
, B53SPI_MSPI_WRITE_LOCK
, 0);
208 for (i
= 0; i
< len
; ++i
) {
209 int offset
= b53spi
->read_offset
+ i
;
211 /* Data stored in the transmit register file LSB */
212 r_buf
[i
] = (u8
)bcm53xxspi_read(b53spi
, B53SPI_MSPI_RXRAM
+ 4 * (1 + offset
* 2));
215 b53spi
->read_offset
= 0;
218 static int bcm53xxspi_transfer_one(struct spi_master
*master
,
219 struct spi_device
*spi
,
220 struct spi_transfer
*t
)
222 struct bcm53xxspi
*b53spi
= spi_master_get_devdata(master
);
226 bcm53xxspi_disable_bspi(b53spi
);
229 buf
= (u8
*)t
->tx_buf
;
232 size_t to_write
= min_t(size_t, 16, left
);
233 bool cont
= left
- to_write
> 0;
235 bcm53xxspi_buf_write(b53spi
, buf
, to_write
, cont
);
242 buf
= (u8
*)t
->rx_buf
;
245 size_t to_read
= min_t(size_t, 16 - b53spi
->read_offset
,
247 bool cont
= left
- to_read
> 0;
249 bcm53xxspi_buf_read(b53spi
, buf
, to_read
, cont
);
258 static int bcm53xxspi_flash_read(struct spi_device
*spi
,
259 struct spi_flash_read_message
*msg
)
261 struct bcm53xxspi
*b53spi
= spi_master_get_devdata(spi
->master
);
264 if (msg
->from
+ msg
->len
> BCM53XXSPI_FLASH_WINDOW
)
267 bcm53xxspi_enable_bspi(b53spi
);
268 memcpy_fromio(msg
->buf
, b53spi
->mmio_base
+ msg
->from
, msg
->len
);
269 msg
->retlen
= msg
->len
;
274 /**************************************************
276 **************************************************/
278 static struct spi_board_info bcm53xx_info
= {
279 .modalias
= "bcm53xxspiflash",
282 static const struct bcma_device_id bcm53xxspi_bcma_tbl
[] = {
283 BCMA_CORE(BCMA_MANUF_BCM
, BCMA_CORE_NS_QSPI
, BCMA_ANY_REV
, BCMA_ANY_CLASS
),
286 MODULE_DEVICE_TABLE(bcma
, bcm53xxspi_bcma_tbl
);
288 static int bcm53xxspi_bcma_probe(struct bcma_device
*core
)
290 struct device
*dev
= &core
->dev
;
291 struct bcm53xxspi
*b53spi
;
292 struct spi_master
*master
;
295 if (core
->bus
->drv_cc
.core
->id
.rev
!= 42) {
296 pr_err("SPI on SoC with unsupported ChipCommon rev\n");
300 master
= spi_alloc_master(dev
, sizeof(*b53spi
));
304 b53spi
= spi_master_get_devdata(master
);
305 b53spi
->master
= master
;
309 b53spi
->mmio_base
= devm_ioremap(dev
, core
->addr_s
[0],
310 BCM53XXSPI_FLASH_WINDOW
);
312 bcm53xxspi_disable_bspi(b53spi
);
314 master
->transfer_one
= bcm53xxspi_transfer_one
;
315 if (b53spi
->mmio_base
)
316 master
->spi_flash_read
= bcm53xxspi_flash_read
;
318 bcma_set_drvdata(core
, b53spi
);
320 err
= devm_spi_register_master(dev
, master
);
322 spi_master_put(master
);
323 bcma_set_drvdata(core
, NULL
);
327 /* Broadcom SoCs (at least with the CC rev 42) use SPI for flash only */
328 spi_new_device(master
, &bcm53xx_info
);
333 static struct bcma_driver bcm53xxspi_bcma_driver
= {
334 .name
= KBUILD_MODNAME
,
335 .id_table
= bcm53xxspi_bcma_tbl
,
336 .probe
= bcm53xxspi_bcma_probe
,
339 /**************************************************
341 **************************************************/
343 static int __init
bcm53xxspi_module_init(void)
347 err
= bcma_driver_register(&bcm53xxspi_bcma_driver
);
349 pr_err("Failed to register bcma driver: %d\n", err
);
354 static void __exit
bcm53xxspi_module_exit(void)
356 bcma_driver_unregister(&bcm53xxspi_bcma_driver
);
359 module_init(bcm53xxspi_module_init
);
360 module_exit(bcm53xxspi_module_exit
);
362 MODULE_DESCRIPTION("Broadcom BCM53xx SPI Controller driver");
363 MODULE_AUTHOR("Rafał Miłecki <zajec5@gmail.com>");
364 MODULE_LICENSE("GPL");