2 * Broadcom BCM63xx SPI controller support
4 * Copyright (C) 2009 Florian Fainelli <florian@openwrt.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/clk.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/spi/spi.h>
29 #include <linux/spi/spi_bitbang.h>
30 #include <linux/gpio.h>
31 #include <linux/completion.h>
32 #include <linux/err.h>
34 #include <bcm63xx_io.h>
35 #include <bcm63xx_regs.h>
36 #include <bcm63xx_dev_spi.h>
38 #define PFX KBUILD_MODNAME
39 #define DRV_VER "0.1.0"
42 /* bitbang has to be first */
43 struct spi_bitbang bitbang
;
44 struct completion done
;
51 unsigned msg_fifo_size
;
52 unsigned rx_fifo_size
;
55 const unsigned char *tx_ptr
;
56 unsigned char *rx_ptr
;
60 struct resource
*ioarea
;
61 struct platform_device
*pdev
;
64 static void bcm63xx_spi_chipselect(struct spi_device
*spi
, int is_on
)
68 val
= bcm_spi_readw(SPI_CMD
);
69 if (is_on
== BITBANG_CS_INACTIVE
)
71 else if (is_on
== BITBANG_CS_ACTIVE
)
72 val
|= (1 << spi
->chip_select
<< SPI_CMD_DEVICE_ID_SHIFT
);
73 bcm_spi_writew(val
, SPI_CMD
);
76 static int bcm63xx_spi_setup_transfer(struct spi_device
*spi
,
77 struct spi_transfer
*t
)
84 struct bcm63xx_spi
*bs
= spi_master_get_devdata(spi
->master
);
86 bits_per_word
= (t
) ? t
->bits_per_word
: spi
->bits_per_word
;
87 hz
= (t
) ? t
->speed_hz
: spi
->max_speed_hz
;
88 if (bits_per_word
!= 8) {
89 dev_err(&spi
->dev
, "%s, unsupported bits_per_word=%d\n",
90 __func__
, bits_per_word
);
94 if (spi
->chip_select
> spi
->master
->num_chipselect
) {
95 dev_err(&spi
->dev
, "%s, unsupported slave %d\n",
96 __func__
, spi
->chip_select
);
100 /* Check clock setting */
101 div
= (bs
->speed_hz
/ hz
);
104 clk_cfg
= SPI_CLK_25MHZ
;
107 clk_cfg
= SPI_CLK_12_50MHZ
;
110 clk_cfg
= SPI_CLK_6_250MHZ
;
113 clk_cfg
= SPI_CLK_3_125MHZ
;
116 clk_cfg
= SPI_CLK_1_563MHZ
;
119 clk_cfg
= SPI_CLK_0_781MHZ
;
123 /* Set to slowest mode for compatibility */
124 clk_cfg
= SPI_CLK_0_781MHZ
;
128 bcm_spi_writeb(clk_cfg
, SPI_CLK_CFG
);
129 dev_dbg(&spi
->dev
, "Setting clock register to %d (hz %d, cmd %02x)\n",
135 /* the spi->mode bits understood by this driver: */
136 #define MODEBITS (SPI_CPOL | SPI_CPHA)
138 static int bcm63xx_spi_setup(struct spi_device
*spi
)
140 struct spi_bitbang
*bitbang
;
141 struct bcm63xx_spi
*bs
;
144 bs
= spi_master_get_devdata(spi
->master
);
145 bitbang
= &bs
->bitbang
;
147 if (!spi
->bits_per_word
)
148 spi
->bits_per_word
= 8;
150 if (spi
->mode
& ~MODEBITS
) {
151 dev_err(&spi
->dev
, "%s, unsupported mode bits %x\n",
152 __func__
, spi
->mode
& ~MODEBITS
);
156 retval
= bcm63xx_spi_setup_transfer(spi
, NULL
);
158 dev_err(&spi
->dev
, "setup: unsupported mode bits %x\n",
159 spi
->mode
& ~MODEBITS
);
163 dev_dbg(&spi
->dev
, "%s, mode %d, %u bits/w, %u nsec/bit\n",
164 __func__
, spi
->mode
& MODEBITS
, spi
->bits_per_word
, 0);
169 /* Fill the TX FIFO with as many bytes as possible */
170 static void bcm63xx_spi_fill_tx_fifo(struct bcm63xx_spi
*bs
)
174 /* Fill the Tx FIFO with as many bytes as possible */
175 tail
= bcm_spi_readb(SPI_MSG_TAIL
);
176 while ((tail
< bs
->msg_fifo_size
) && (bs
->remaining_bytes
> 0)) {
178 bcm_spi_writeb(*bs
->tx_ptr
++, SPI_MSG_DATA
);
180 bcm_spi_writeb(0, SPI_MSG_DATA
);
181 bs
->remaining_bytes
--;
182 tail
= bcm_spi_readb(SPI_MSG_TAIL
);
186 static int bcm63xx_txrx_bufs(struct spi_device
*spi
, struct spi_transfer
*t
)
188 struct bcm63xx_spi
*bs
= spi_master_get_devdata(spi
->master
);
192 dev_dbg(&spi
->dev
, "txrx: tx %p, rx %p, len %d\n",
193 t
->tx_buf
, t
->rx_buf
, t
->len
);
195 /* Transmitter is inhibited */
196 bs
->tx_ptr
= t
->tx_buf
;
197 bs
->rx_ptr
= t
->rx_buf
;
198 bs
->remaining_bytes
= t
->len
;
199 init_completion(&bs
->done
);
201 bcm63xx_spi_fill_tx_fifo(bs
);
203 /* Enable the command done interrupt which
204 * we use to determine completion of a command */
205 bcm_writeb(SPI_INTR_CMD_DONE
, SPI_INT_MASK
);
207 /* Fill in the Message control register */
208 msg_ctl
= bcm_spi_readb(SPI_MSG_CTL
);
209 msg_ctl
|= (t
->len
<< SPI_BYTE_CNT_SHIFT
);
210 msg_ctl
|= (SPI_HD_R
<< SPI_MSG_TYPE_SHIFT
);
211 bcm_spi_writeb(msg_ctl
, SPI_MSG_CTL
);
213 /* Issue the transfer */
214 cmd
= bcm_spi_readb(SPI_CMD
);
215 cmd
|= SPI_CMD_START_IMMEDIATE
;
216 cmd
|= (0 << SPI_CMD_PREPEND_BYTE_CNT_SHIFT
);
217 bcm_spi_writeb(cmd
, SPI_CMD
);
219 wait_for_completion(&bs
->done
);
221 /* Disable the CMD_DONE interrupt */
222 bcm_spi_writeb(~(SPI_INTR_CMD_DONE
), SPI_INT_MASK
);
224 return t
->len
- bs
->remaining_bytes
;
227 /* This driver supports single master mode only. Hence
228 * CMD_DONE is the only interrupt we care about
230 static irqreturn_t
bcm63xx_spi_interrupt(int irq
, void *dev_id
)
232 struct spi_master
*master
= (struct spi_master
*)dev_id
;
233 struct bcm63xx_spi
*bs
= spi_master_get_devdata(master
);
237 /* Read interupts and clear them immediately */
238 intr
= bcm_spi_readb(SPI_INT_STATUS
);
239 bcm_writeb(SPI_INTR_CLEAR_ALL
, SPI_INT_STATUS
);
241 /* A tansfer completed */
242 if (intr
& SPI_INTR_CMD_DONE
) {
245 rx_empty
= bcm_spi_readb(SPI_ST
);
246 /* Read out all the data */
247 while ((rx_empty
& SPI_RX_EMPTY
) == 0) {
250 data
= bcm_spi_readb(SPI_RX_DATA
);
252 *bs
->rx_ptr
++ = data
;
254 rx_empty
= bcm_spi_readb(SPI_RX_EMPTY
);
257 /* See if there is more data to send */
258 if (bs
->remaining_bytes
> 0) {
259 bcm63xx_spi_fill_tx_fifo(bs
);
261 /* Start the transfer */
262 cmd
= bcm_spi_readb(SPI_CMD
);
263 cmd
|= SPI_CMD_START_IMMEDIATE
;
264 cmd
|= (0 << SPI_CMD_PREPEND_BYTE_CNT_SHIFT
);
265 bcm_spi_writeb(cmd
, SPI_CMD
);
274 static int __init
bcm63xx_spi_probe(struct platform_device
*pdev
)
277 struct bcm63xx_spi_pdata
*pdata
= pdev
->dev
.platform_data
;
279 struct spi_master
*master
;
281 struct bcm63xx_spi
*bs
;
284 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
290 irq
= platform_get_irq(pdev
, 0);
296 clk
= clk_get(&pdev
->dev
, "spi");
298 dev_err(&pdev
->dev
, "No clock for device\n");
303 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct bcm63xx_spi
));
309 bs
= spi_master_get_devdata(master
);
310 bs
->bitbang
.master
= spi_master_get(master
);
311 bs
->bitbang
.chipselect
= bcm63xx_spi_chipselect
;
312 bs
->bitbang
.setup_transfer
= bcm63xx_spi_setup_transfer
;
313 bs
->bitbang
.txrx_bufs
= bcm63xx_txrx_bufs
;
314 bs
->bitbang
.master
->setup
= bcm63xx_spi_setup
;
315 init_completion(&bs
->done
);
317 platform_set_drvdata(pdev
, master
);
320 bs
->regs
= ioremap_nocache(r
->start
, r
->end
- r
->start
);
322 printk(KERN_ERR PFX
" unable to ioremap regs\n");
328 bs
->msg_fifo_size
= pdata
->msg_fifo_size
;
329 bs
->rx_fifo_size
= pdata
->rx_fifo_size
;
331 ret
= request_irq(irq
, bcm63xx_spi_interrupt
, 0,
332 pdev
->dev
.bus_id
, master
);
334 printk(KERN_ERR PFX
" unable to request irq\n");
338 master
->bus_num
= pdata
->bus_num
;
339 master
->num_chipselect
= pdata
->num_chipselect
;
340 bs
->speed_hz
= pdata
->speed_hz
;
342 /* Initialize hardware */
344 bcm_spi_writew(SPI_CMD_HARD_RESET
, SPI_CMD
);
345 bcm_spi_writeb(SPI_INTR_CLEAR_ALL
, SPI_INT_MASK
);
347 dev_info(&pdev
->dev
, PFX
" at 0x%08x (irq %d) %s\n",
348 r
->start
, irq
, DRV_VER
);
350 ret
= spi_bitbang_start(&bs
->bitbang
);
352 dev_err(&pdev
->dev
, "spi_bitbang_start FAILED\n");
360 free_irq(irq
, master
);
365 spi_master_put(master
);
370 static int __exit
bcm63xx_spi_remove(struct platform_device
*pdev
)
372 struct spi_master
*master
= platform_get_drvdata(pdev
);
373 struct bcm63xx_spi
*bs
= spi_master_get_devdata(master
);
375 spi_bitbang_stop(&bs
->bitbang
);
376 clk_disable(bs
->clk
);
378 free_irq(bs
->irq
, master
);
380 platform_set_drvdata(pdev
, 0);
381 spi_master_put(bs
->bitbang
.master
);
387 static int bcm63xx_spi_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
389 struct spi_master
*master
= platform_get_drvdata(pdev
);
390 struct bcm63xx_spi
*bs
= spi_master_get_devdata(master
);
392 clk_disable(bs
->clk
);
397 static int bcm63xx_spi_resume(struct platform_device
*pdev
)
399 struct bcm63xx_spi
*bs
= spi_master_get_devdata(master
);
400 struct bcm63xx_spi
*bs
= spi_master_get_devdata(master
);
407 #define bcm63xx_spi_suspend NULL
408 #define bcm63xx_spi_resume NULL
411 static struct platform_driver bcm63xx_spi_driver
= {
413 .name
= "bcm63xx-spi",
414 .owner
= THIS_MODULE
,
416 .probe
= bcm63xx_spi_probe
,
417 .remove
= bcm63xx_spi_remove
,
418 .suspend
= bcm63xx_spi_suspend
,
419 .resume
= bcm63xx_spi_resume
,
423 static int __init
bcm63xx_spi_init(void)
425 return platform_driver_register(&bcm63xx_spi_driver
);
428 static void __exit
bcm63xx_spi_exit(void)
430 platform_driver_unregister(&bcm63xx_spi_driver
);
433 module_init(bcm63xx_spi_init
);
434 module_exit(bcm63xx_spi_exit
);
436 MODULE_ALIAS("platform:bcm63xx_spi");
437 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
438 MODULE_DESCRIPTION("Broadcom BCM63xx SPI Controller driver");
439 MODULE_LICENSE("GPL");