blob: 63b0028a8bd35bdaec2b5791acdaf399ca7e0889 [file] [log] [blame]
Florian Fainellib42dfed2012-02-01 11:14:09 +01001/*
2 * Broadcom BCM63xx SPI controller support
3 *
Florian Fainellicde43842012-04-20 15:37:33 +02004 * Copyright (C) 2009-2012 Florian Fainelli <florian@openwrt.org>
Florian Fainellib42dfed2012-02-01 11:14:09 +01005 * Copyright (C) 2010 Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 */
21
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/clk.h>
25#include <linux/io.h>
26#include <linux/module.h>
27#include <linux/platform_device.h>
28#include <linux/delay.h>
29#include <linux/interrupt.h>
30#include <linux/spi/spi.h>
31#include <linux/completion.h>
32#include <linux/err.h>
Florian Fainellicde43842012-04-20 15:37:33 +020033#include <linux/workqueue.h>
34#include <linux/pm_runtime.h>
Florian Fainellib42dfed2012-02-01 11:14:09 +010035
36#include <bcm63xx_dev_spi.h>
37
38#define PFX KBUILD_MODNAME
39#define DRV_VER "0.1.2"
40
41struct bcm63xx_spi {
42 spinlock_t lock;
43 int stopping;
44 struct completion done;
45
46 void __iomem *regs;
47 int irq;
48
49 /* Platform data */
50 u32 speed_hz;
51 unsigned fifo_size;
52
53 /* Data buffers */
54 const unsigned char *tx_ptr;
55 unsigned char *rx_ptr;
56
57 /* data iomem */
58 u8 __iomem *tx_io;
59 const u8 __iomem *rx_io;
60
61 int remaining_bytes;
62
63 struct clk *clk;
64 struct platform_device *pdev;
65};
66
67static inline u8 bcm_spi_readb(struct bcm63xx_spi *bs,
68 unsigned int offset)
69{
70 return bcm_readb(bs->regs + bcm63xx_spireg(offset));
71}
72
73static inline u16 bcm_spi_readw(struct bcm63xx_spi *bs,
74 unsigned int offset)
75{
76 return bcm_readw(bs->regs + bcm63xx_spireg(offset));
77}
78
79static inline void bcm_spi_writeb(struct bcm63xx_spi *bs,
80 u8 value, unsigned int offset)
81{
82 bcm_writeb(value, bs->regs + bcm63xx_spireg(offset));
83}
84
85static inline void bcm_spi_writew(struct bcm63xx_spi *bs,
86 u16 value, unsigned int offset)
87{
88 bcm_writew(value, bs->regs + bcm63xx_spireg(offset));
89}
90
91static const unsigned bcm63xx_spi_freq_table[SPI_CLK_MASK][2] = {
92 { 20000000, SPI_CLK_20MHZ },
93 { 12500000, SPI_CLK_12_50MHZ },
94 { 6250000, SPI_CLK_6_250MHZ },
95 { 3125000, SPI_CLK_3_125MHZ },
96 { 1563000, SPI_CLK_1_563MHZ },
97 { 781000, SPI_CLK_0_781MHZ },
98 { 391000, SPI_CLK_0_391MHZ }
99};
100
Florian Fainellicde43842012-04-20 15:37:33 +0200101static int bcm63xx_spi_check_transfer(struct spi_device *spi,
102 struct spi_transfer *t)
Florian Fainellib42dfed2012-02-01 11:14:09 +0100103{
Florian Fainellib42dfed2012-02-01 11:14:09 +0100104 u8 bits_per_word;
Florian Fainellib42dfed2012-02-01 11:14:09 +0100105
106 bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word;
Florian Fainellib42dfed2012-02-01 11:14:09 +0100107 if (bits_per_word != 8) {
108 dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
109 __func__, bits_per_word);
110 return -EINVAL;
111 }
112
113 if (spi->chip_select > spi->master->num_chipselect) {
114 dev_err(&spi->dev, "%s, unsupported slave %d\n",
115 __func__, spi->chip_select);
116 return -EINVAL;
117 }
118
Florian Fainellicde43842012-04-20 15:37:33 +0200119 return 0;
120}
121
122static void bcm63xx_spi_setup_transfer(struct spi_device *spi,
123 struct spi_transfer *t)
124{
125 struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
126 u32 hz;
127 u8 clk_cfg, reg;
128 int i;
129
130 hz = (t) ? t->speed_hz : spi->max_speed_hz;
131
Florian Fainellib42dfed2012-02-01 11:14:09 +0100132 /* Find the closest clock configuration */
133 for (i = 0; i < SPI_CLK_MASK; i++) {
134 if (hz <= bcm63xx_spi_freq_table[i][0]) {
135 clk_cfg = bcm63xx_spi_freq_table[i][1];
136 break;
137 }
138 }
139
140 /* No matching configuration found, default to lowest */
141 if (i == SPI_CLK_MASK)
142 clk_cfg = SPI_CLK_0_391MHZ;
143
144 /* clear existing clock configuration bits of the register */
145 reg = bcm_spi_readb(bs, SPI_CLK_CFG);
146 reg &= ~SPI_CLK_MASK;
147 reg |= clk_cfg;
148
149 bcm_spi_writeb(bs, reg, SPI_CLK_CFG);
150 dev_dbg(&spi->dev, "Setting clock register to %02x (hz %d)\n",
151 clk_cfg, hz);
Florian Fainellib42dfed2012-02-01 11:14:09 +0100152}
153
154/* the spi->mode bits understood by this driver: */
155#define MODEBITS (SPI_CPOL | SPI_CPHA)
156
157static int bcm63xx_spi_setup(struct spi_device *spi)
158{
159 struct bcm63xx_spi *bs;
160 int ret;
161
162 bs = spi_master_get_devdata(spi->master);
163
164 if (bs->stopping)
165 return -ESHUTDOWN;
166
167 if (!spi->bits_per_word)
168 spi->bits_per_word = 8;
169
170 if (spi->mode & ~MODEBITS) {
171 dev_err(&spi->dev, "%s, unsupported mode bits %x\n",
172 __func__, spi->mode & ~MODEBITS);
173 return -EINVAL;
174 }
175
Florian Fainellicde43842012-04-20 15:37:33 +0200176 ret = bcm63xx_spi_check_transfer(spi, NULL);
Florian Fainellib42dfed2012-02-01 11:14:09 +0100177 if (ret < 0) {
178 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
179 spi->mode & ~MODEBITS);
180 return ret;
181 }
182
183 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec/bit\n",
184 __func__, spi->mode & MODEBITS, spi->bits_per_word, 0);
185
186 return 0;
187}
188
189/* Fill the TX FIFO with as many bytes as possible */
190static void bcm63xx_spi_fill_tx_fifo(struct bcm63xx_spi *bs)
191{
192 u8 size;
193
194 /* Fill the Tx FIFO with as many bytes as possible */
195 size = bs->remaining_bytes < bs->fifo_size ? bs->remaining_bytes :
196 bs->fifo_size;
197 memcpy_toio(bs->tx_io, bs->tx_ptr, size);
198 bs->remaining_bytes -= size;
199}
200
Florian Fainellicde43842012-04-20 15:37:33 +0200201static unsigned int bcm63xx_txrx_bufs(struct spi_device *spi,
202 struct spi_transfer *t)
Florian Fainellib42dfed2012-02-01 11:14:09 +0100203{
204 struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
205 u16 msg_ctl;
206 u16 cmd;
207
Florian Fainellicde43842012-04-20 15:37:33 +0200208 /* Disable the CMD_DONE interrupt */
209 bcm_spi_writeb(bs, 0, SPI_INT_MASK);
210
Florian Fainellib42dfed2012-02-01 11:14:09 +0100211 dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
212 t->tx_buf, t->rx_buf, t->len);
213
214 /* Transmitter is inhibited */
215 bs->tx_ptr = t->tx_buf;
216 bs->rx_ptr = t->rx_buf;
Florian Fainellib42dfed2012-02-01 11:14:09 +0100217
218 if (t->tx_buf) {
219 bs->remaining_bytes = t->len;
220 bcm63xx_spi_fill_tx_fifo(bs);
221 }
222
Florian Fainellicde43842012-04-20 15:37:33 +0200223 init_completion(&bs->done);
Florian Fainellib42dfed2012-02-01 11:14:09 +0100224
225 /* Fill in the Message control register */
226 msg_ctl = (t->len << SPI_BYTE_CNT_SHIFT);
227
228 if (t->rx_buf && t->tx_buf)
229 msg_ctl |= (SPI_FD_RW << SPI_MSG_TYPE_SHIFT);
230 else if (t->rx_buf)
231 msg_ctl |= (SPI_HD_R << SPI_MSG_TYPE_SHIFT);
232 else if (t->tx_buf)
233 msg_ctl |= (SPI_HD_W << SPI_MSG_TYPE_SHIFT);
234
235 bcm_spi_writew(bs, msg_ctl, SPI_MSG_CTL);
236
237 /* Issue the transfer */
238 cmd = SPI_CMD_START_IMMEDIATE;
239 cmd |= (0 << SPI_CMD_PREPEND_BYTE_CNT_SHIFT);
240 cmd |= (spi->chip_select << SPI_CMD_DEVICE_ID_SHIFT);
241 bcm_spi_writew(bs, cmd, SPI_CMD);
Florian Fainellib42dfed2012-02-01 11:14:09 +0100242
Florian Fainellicde43842012-04-20 15:37:33 +0200243 /* Enable the CMD_DONE interrupt */
244 bcm_spi_writeb(bs, SPI_INTR_CMD_DONE, SPI_INT_MASK);
Florian Fainellib42dfed2012-02-01 11:14:09 +0100245
246 return t->len - bs->remaining_bytes;
247}
248
Florian Fainellicde43842012-04-20 15:37:33 +0200249static int bcm63xx_spi_prepare_transfer(struct spi_master *master)
Florian Fainellib42dfed2012-02-01 11:14:09 +0100250{
Florian Fainellicde43842012-04-20 15:37:33 +0200251 struct bcm63xx_spi *bs = spi_master_get_devdata(master);
252
253 pm_runtime_get_sync(&bs->pdev->dev);
254
255 return 0;
256}
257
258static int bcm63xx_spi_unprepare_transfer(struct spi_master *master)
259{
260 struct bcm63xx_spi *bs = spi_master_get_devdata(master);
261
262 pm_runtime_put(&bs->pdev->dev);
263
264 return 0;
265}
266
267static int bcm63xx_spi_transfer_one(struct spi_master *master,
268 struct spi_message *m)
269{
270 struct bcm63xx_spi *bs = spi_master_get_devdata(master);
Florian Fainellib42dfed2012-02-01 11:14:09 +0100271 struct spi_transfer *t;
Florian Fainellicde43842012-04-20 15:37:33 +0200272 struct spi_device *spi = m->spi;
273 int status = 0;
274 unsigned int timeout = 0;
Florian Fainellib42dfed2012-02-01 11:14:09 +0100275
276 list_for_each_entry(t, &m->transfers, transfer_list) {
Florian Fainellicde43842012-04-20 15:37:33 +0200277 unsigned int len = t->len;
278 u8 rx_tail;
279
280 status = bcm63xx_spi_check_transfer(spi, t);
281 if (status < 0)
282 goto exit;
283
284 /* configure adapter for a new transfer */
285 bcm63xx_spi_setup_transfer(spi, t);
286
287 while (len) {
288 /* send the data */
289 len -= bcm63xx_txrx_bufs(spi, t);
290
291 timeout = wait_for_completion_timeout(&bs->done, HZ);
292 if (!timeout) {
293 status = -ETIMEDOUT;
294 goto exit;
295 }
296
297 /* read out all data */
298 rx_tail = bcm_spi_readb(bs, SPI_RX_TAIL);
299
300 /* Read out all the data */
301 if (rx_tail)
302 memcpy_fromio(bs->rx_ptr, bs->rx_io, rx_tail);
303 }
304
305 m->actual_length += t->len;
Florian Fainellib42dfed2012-02-01 11:14:09 +0100306 }
Florian Fainellicde43842012-04-20 15:37:33 +0200307exit:
308 m->status = status;
309 spi_finalize_current_message(master);
Florian Fainellib42dfed2012-02-01 11:14:09 +0100310
Florian Fainellicde43842012-04-20 15:37:33 +0200311 return 0;
Florian Fainellib42dfed2012-02-01 11:14:09 +0100312}
313
314/* This driver supports single master mode only. Hence
315 * CMD_DONE is the only interrupt we care about
316 */
317static irqreturn_t bcm63xx_spi_interrupt(int irq, void *dev_id)
318{
319 struct spi_master *master = (struct spi_master *)dev_id;
320 struct bcm63xx_spi *bs = spi_master_get_devdata(master);
321 u8 intr;
Florian Fainellib42dfed2012-02-01 11:14:09 +0100322
323 /* Read interupts and clear them immediately */
324 intr = bcm_spi_readb(bs, SPI_INT_STATUS);
325 bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
326 bcm_spi_writeb(bs, 0, SPI_INT_MASK);
327
Florian Fainellicde43842012-04-20 15:37:33 +0200328 /* A transfer completed */
329 if (intr & SPI_INTR_CMD_DONE)
330 complete(&bs->done);
Florian Fainellib42dfed2012-02-01 11:14:09 +0100331
332 return IRQ_HANDLED;
333}
334
335
336static int __devinit bcm63xx_spi_probe(struct platform_device *pdev)
337{
338 struct resource *r;
339 struct device *dev = &pdev->dev;
340 struct bcm63xx_spi_pdata *pdata = pdev->dev.platform_data;
341 int irq;
342 struct spi_master *master;
343 struct clk *clk;
344 struct bcm63xx_spi *bs;
345 int ret;
346
347 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
348 if (!r) {
349 dev_err(dev, "no iomem\n");
350 ret = -ENXIO;
351 goto out;
352 }
353
354 irq = platform_get_irq(pdev, 0);
355 if (irq < 0) {
356 dev_err(dev, "no irq\n");
357 ret = -ENXIO;
358 goto out;
359 }
360
361 clk = clk_get(dev, "spi");
362 if (IS_ERR(clk)) {
363 dev_err(dev, "no clock for device\n");
364 ret = PTR_ERR(clk);
365 goto out;
366 }
367
368 master = spi_alloc_master(dev, sizeof(*bs));
369 if (!master) {
370 dev_err(dev, "out of memory\n");
371 ret = -ENOMEM;
372 goto out_clk;
373 }
374
375 bs = spi_master_get_devdata(master);
Florian Fainellib42dfed2012-02-01 11:14:09 +0100376
377 platform_set_drvdata(pdev, master);
378 bs->pdev = pdev;
379
380 if (!devm_request_mem_region(&pdev->dev, r->start,
381 resource_size(r), PFX)) {
382 dev_err(dev, "iomem request failed\n");
383 ret = -ENXIO;
384 goto out_err;
385 }
386
387 bs->regs = devm_ioremap_nocache(&pdev->dev, r->start,
388 resource_size(r));
389 if (!bs->regs) {
390 dev_err(dev, "unable to ioremap regs\n");
391 ret = -ENOMEM;
392 goto out_err;
393 }
394
395 bs->irq = irq;
396 bs->clk = clk;
397 bs->fifo_size = pdata->fifo_size;
398
399 ret = devm_request_irq(&pdev->dev, irq, bcm63xx_spi_interrupt, 0,
400 pdev->name, master);
401 if (ret) {
402 dev_err(dev, "unable to request irq\n");
403 goto out_err;
404 }
405
406 master->bus_num = pdata->bus_num;
407 master->num_chipselect = pdata->num_chipselect;
408 master->setup = bcm63xx_spi_setup;
Florian Fainellicde43842012-04-20 15:37:33 +0200409 master->prepare_transfer_hardware = bcm63xx_spi_prepare_transfer;
410 master->unprepare_transfer_hardware = bcm63xx_spi_unprepare_transfer;
411 master->transfer_one_message = bcm63xx_spi_transfer_one;
Florian Fainellib42dfed2012-02-01 11:14:09 +0100412 bs->speed_hz = pdata->speed_hz;
413 bs->stopping = 0;
414 bs->tx_io = (u8 *)(bs->regs + bcm63xx_spireg(SPI_MSG_DATA));
415 bs->rx_io = (const u8 *)(bs->regs + bcm63xx_spireg(SPI_RX_DATA));
416 spin_lock_init(&bs->lock);
417
418 /* Initialize hardware */
419 clk_enable(bs->clk);
420 bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
421
422 /* register and we are done */
423 ret = spi_register_master(master);
424 if (ret) {
425 dev_err(dev, "spi register failed\n");
426 goto out_clk_disable;
427 }
428
429 dev_info(dev, "at 0x%08x (irq %d, FIFOs size %d) v%s\n",
430 r->start, irq, bs->fifo_size, DRV_VER);
431
432 return 0;
433
434out_clk_disable:
435 clk_disable(clk);
436out_err:
437 platform_set_drvdata(pdev, NULL);
438 spi_master_put(master);
439out_clk:
440 clk_put(clk);
441out:
442 return ret;
443}
444
445static int __devexit bcm63xx_spi_remove(struct platform_device *pdev)
446{
447 struct spi_master *master = platform_get_drvdata(pdev);
448 struct bcm63xx_spi *bs = spi_master_get_devdata(master);
449
450 /* reset spi block */
451 bcm_spi_writeb(bs, 0, SPI_INT_MASK);
452 spin_lock(&bs->lock);
453 bs->stopping = 1;
454
455 /* HW shutdown */
456 clk_disable(bs->clk);
457 clk_put(bs->clk);
458
459 spin_unlock(&bs->lock);
460 platform_set_drvdata(pdev, 0);
461 spi_unregister_master(master);
462
463 return 0;
464}
465
466#ifdef CONFIG_PM
467static int bcm63xx_spi_suspend(struct device *dev)
468{
469 struct spi_master *master =
470 platform_get_drvdata(to_platform_device(dev));
471 struct bcm63xx_spi *bs = spi_master_get_devdata(master);
472
473 clk_disable(bs->clk);
474
475 return 0;
476}
477
478static int bcm63xx_spi_resume(struct device *dev)
479{
480 struct spi_master *master =
481 platform_get_drvdata(to_platform_device(dev));
482 struct bcm63xx_spi *bs = spi_master_get_devdata(master);
483
484 clk_enable(bs->clk);
485
486 return 0;
487}
488
489static const struct dev_pm_ops bcm63xx_spi_pm_ops = {
490 .suspend = bcm63xx_spi_suspend,
491 .resume = bcm63xx_spi_resume,
492};
493
494#define BCM63XX_SPI_PM_OPS (&bcm63xx_spi_pm_ops)
495#else
496#define BCM63XX_SPI_PM_OPS NULL
497#endif
498
499static struct platform_driver bcm63xx_spi_driver = {
500 .driver = {
501 .name = "bcm63xx-spi",
502 .owner = THIS_MODULE,
503 .pm = BCM63XX_SPI_PM_OPS,
504 },
505 .probe = bcm63xx_spi_probe,
506 .remove = __devexit_p(bcm63xx_spi_remove),
507};
508
509module_platform_driver(bcm63xx_spi_driver);
510
511MODULE_ALIAS("platform:bcm63xx_spi");
512MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
513MODULE_AUTHOR("Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>");
514MODULE_DESCRIPTION("Broadcom BCM63xx SPI Controller driver");
515MODULE_LICENSE("GPL");