Linux 4.2.1
[linux/fpc-iii.git] / drivers / spi / spi-bcm63xx-hsspi.c
blobf5ca6dc3a157879895aa0953c4c6681594b3d5ff
1 /*
2 * Broadcom BCM63XX High Speed SPI Controller driver
4 * Copyright 2000-2010 Broadcom Corporation
5 * Copyright 2012-2013 Jonas Gorski <jogo@openwrt.org>
7 * Licensed under the GNU/GPL. See COPYING for details.
8 */
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/io.h>
13 #include <linux/clk.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/err.h>
19 #include <linux/interrupt.h>
20 #include <linux/spi/spi.h>
21 #include <linux/mutex.h>
23 #define HSSPI_GLOBAL_CTRL_REG 0x0
24 #define GLOBAL_CTRL_CS_POLARITY_SHIFT 0
25 #define GLOBAL_CTRL_CS_POLARITY_MASK 0x000000ff
26 #define GLOBAL_CTRL_PLL_CLK_CTRL_SHIFT 8
27 #define GLOBAL_CTRL_PLL_CLK_CTRL_MASK 0x0000ff00
28 #define GLOBAL_CTRL_CLK_GATE_SSOFF BIT(16)
29 #define GLOBAL_CTRL_CLK_POLARITY BIT(17)
30 #define GLOBAL_CTRL_MOSI_IDLE BIT(18)
32 #define HSSPI_GLOBAL_EXT_TRIGGER_REG 0x4
34 #define HSSPI_INT_STATUS_REG 0x8
35 #define HSSPI_INT_STATUS_MASKED_REG 0xc
36 #define HSSPI_INT_MASK_REG 0x10
38 #define HSSPI_PINGx_CMD_DONE(i) BIT((i * 8) + 0)
39 #define HSSPI_PINGx_RX_OVER(i) BIT((i * 8) + 1)
40 #define HSSPI_PINGx_TX_UNDER(i) BIT((i * 8) + 2)
41 #define HSSPI_PINGx_POLL_TIMEOUT(i) BIT((i * 8) + 3)
42 #define HSSPI_PINGx_CTRL_INVAL(i) BIT((i * 8) + 4)
44 #define HSSPI_INT_CLEAR_ALL 0xff001f1f
46 #define HSSPI_PINGPONG_COMMAND_REG(x) (0x80 + (x) * 0x40)
47 #define PINGPONG_CMD_COMMAND_MASK 0xf
48 #define PINGPONG_COMMAND_NOOP 0
49 #define PINGPONG_COMMAND_START_NOW 1
50 #define PINGPONG_COMMAND_START_TRIGGER 2
51 #define PINGPONG_COMMAND_HALT 3
52 #define PINGPONG_COMMAND_FLUSH 4
53 #define PINGPONG_CMD_PROFILE_SHIFT 8
54 #define PINGPONG_CMD_SS_SHIFT 12
56 #define HSSPI_PINGPONG_STATUS_REG(x) (0x84 + (x) * 0x40)
58 #define HSSPI_PROFILE_CLK_CTRL_REG(x) (0x100 + (x) * 0x20)
59 #define CLK_CTRL_FREQ_CTRL_MASK 0x0000ffff
60 #define CLK_CTRL_SPI_CLK_2X_SEL BIT(14)
61 #define CLK_CTRL_ACCUM_RST_ON_LOOP BIT(15)
63 #define HSSPI_PROFILE_SIGNAL_CTRL_REG(x) (0x104 + (x) * 0x20)
64 #define SIGNAL_CTRL_LATCH_RISING BIT(12)
65 #define SIGNAL_CTRL_LAUNCH_RISING BIT(13)
66 #define SIGNAL_CTRL_ASYNC_INPUT_PATH BIT(16)
68 #define HSSPI_PROFILE_MODE_CTRL_REG(x) (0x108 + (x) * 0x20)
69 #define MODE_CTRL_MULTIDATA_RD_STRT_SHIFT 8
70 #define MODE_CTRL_MULTIDATA_WR_STRT_SHIFT 12
71 #define MODE_CTRL_MULTIDATA_RD_SIZE_SHIFT 16
72 #define MODE_CTRL_MULTIDATA_WR_SIZE_SHIFT 18
73 #define MODE_CTRL_MODE_3WIRE BIT(20)
74 #define MODE_CTRL_PREPENDBYTE_CNT_SHIFT 24
76 #define HSSPI_FIFO_REG(x) (0x200 + (x) * 0x200)
79 #define HSSPI_OP_CODE_SHIFT 13
80 #define HSSPI_OP_SLEEP (0 << HSSPI_OP_CODE_SHIFT)
81 #define HSSPI_OP_READ_WRITE (1 << HSSPI_OP_CODE_SHIFT)
82 #define HSSPI_OP_WRITE (2 << HSSPI_OP_CODE_SHIFT)
83 #define HSSPI_OP_READ (3 << HSSPI_OP_CODE_SHIFT)
84 #define HSSPI_OP_SETIRQ (4 << HSSPI_OP_CODE_SHIFT)
86 #define HSSPI_BUFFER_LEN 512
87 #define HSSPI_OPCODE_LEN 2
89 #define HSSPI_MAX_PREPEND_LEN 15
91 #define HSSPI_MAX_SYNC_CLOCK 30000000
93 #define HSSPI_BUS_NUM 1 /* 0 is legacy SPI */
95 struct bcm63xx_hsspi {
96 struct completion done;
97 struct mutex bus_mutex;
99 struct platform_device *pdev;
100 struct clk *clk;
101 void __iomem *regs;
102 u8 __iomem *fifo;
104 u32 speed_hz;
105 u8 cs_polarity;
108 static void bcm63xx_hsspi_set_cs(struct bcm63xx_hsspi *bs, unsigned cs,
109 bool active)
111 u32 reg;
113 mutex_lock(&bs->bus_mutex);
114 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
116 reg &= ~BIT(cs);
117 if (active == !(bs->cs_polarity & BIT(cs)))
118 reg |= BIT(cs);
120 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
121 mutex_unlock(&bs->bus_mutex);
124 static void bcm63xx_hsspi_set_clk(struct bcm63xx_hsspi *bs,
125 struct spi_device *spi, int hz)
127 unsigned profile = spi->chip_select;
128 u32 reg;
130 reg = DIV_ROUND_UP(2048, DIV_ROUND_UP(bs->speed_hz, hz));
131 __raw_writel(CLK_CTRL_ACCUM_RST_ON_LOOP | reg,
132 bs->regs + HSSPI_PROFILE_CLK_CTRL_REG(profile));
134 reg = __raw_readl(bs->regs + HSSPI_PROFILE_SIGNAL_CTRL_REG(profile));
135 if (hz > HSSPI_MAX_SYNC_CLOCK)
136 reg |= SIGNAL_CTRL_ASYNC_INPUT_PATH;
137 else
138 reg &= ~SIGNAL_CTRL_ASYNC_INPUT_PATH;
139 __raw_writel(reg, bs->regs + HSSPI_PROFILE_SIGNAL_CTRL_REG(profile));
141 mutex_lock(&bs->bus_mutex);
142 /* setup clock polarity */
143 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
144 reg &= ~GLOBAL_CTRL_CLK_POLARITY;
145 if (spi->mode & SPI_CPOL)
146 reg |= GLOBAL_CTRL_CLK_POLARITY;
147 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
148 mutex_unlock(&bs->bus_mutex);
151 static int bcm63xx_hsspi_do_txrx(struct spi_device *spi, struct spi_transfer *t)
153 struct bcm63xx_hsspi *bs = spi_master_get_devdata(spi->master);
154 unsigned chip_select = spi->chip_select;
155 u16 opcode = 0;
156 int pending = t->len;
157 int step_size = HSSPI_BUFFER_LEN;
158 const u8 *tx = t->tx_buf;
159 u8 *rx = t->rx_buf;
161 bcm63xx_hsspi_set_clk(bs, spi, t->speed_hz);
162 bcm63xx_hsspi_set_cs(bs, spi->chip_select, true);
164 if (tx && rx)
165 opcode = HSSPI_OP_READ_WRITE;
166 else if (tx)
167 opcode = HSSPI_OP_WRITE;
168 else if (rx)
169 opcode = HSSPI_OP_READ;
171 if (opcode != HSSPI_OP_READ)
172 step_size -= HSSPI_OPCODE_LEN;
174 __raw_writel(0 << MODE_CTRL_PREPENDBYTE_CNT_SHIFT |
175 2 << MODE_CTRL_MULTIDATA_WR_STRT_SHIFT |
176 2 << MODE_CTRL_MULTIDATA_RD_STRT_SHIFT | 0xff,
177 bs->regs + HSSPI_PROFILE_MODE_CTRL_REG(chip_select));
179 while (pending > 0) {
180 int curr_step = min_t(int, step_size, pending);
182 reinit_completion(&bs->done);
183 if (tx) {
184 memcpy_toio(bs->fifo + HSSPI_OPCODE_LEN, tx, curr_step);
185 tx += curr_step;
188 __raw_writew(opcode | curr_step, bs->fifo);
190 /* enable interrupt */
191 __raw_writel(HSSPI_PINGx_CMD_DONE(0),
192 bs->regs + HSSPI_INT_MASK_REG);
194 /* start the transfer */
195 __raw_writel(!chip_select << PINGPONG_CMD_SS_SHIFT |
196 chip_select << PINGPONG_CMD_PROFILE_SHIFT |
197 PINGPONG_COMMAND_START_NOW,
198 bs->regs + HSSPI_PINGPONG_COMMAND_REG(0));
200 if (wait_for_completion_timeout(&bs->done, HZ) == 0) {
201 dev_err(&bs->pdev->dev, "transfer timed out!\n");
202 return -ETIMEDOUT;
205 if (rx) {
206 memcpy_fromio(rx, bs->fifo, curr_step);
207 rx += curr_step;
210 pending -= curr_step;
213 return 0;
216 static int bcm63xx_hsspi_setup(struct spi_device *spi)
218 struct bcm63xx_hsspi *bs = spi_master_get_devdata(spi->master);
219 u32 reg;
221 reg = __raw_readl(bs->regs +
222 HSSPI_PROFILE_SIGNAL_CTRL_REG(spi->chip_select));
223 reg &= ~(SIGNAL_CTRL_LAUNCH_RISING | SIGNAL_CTRL_LATCH_RISING);
224 if (spi->mode & SPI_CPHA)
225 reg |= SIGNAL_CTRL_LAUNCH_RISING;
226 else
227 reg |= SIGNAL_CTRL_LATCH_RISING;
228 __raw_writel(reg, bs->regs +
229 HSSPI_PROFILE_SIGNAL_CTRL_REG(spi->chip_select));
231 mutex_lock(&bs->bus_mutex);
232 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
234 /* only change actual polarities if there is no transfer */
235 if ((reg & GLOBAL_CTRL_CS_POLARITY_MASK) == bs->cs_polarity) {
236 if (spi->mode & SPI_CS_HIGH)
237 reg |= BIT(spi->chip_select);
238 else
239 reg &= ~BIT(spi->chip_select);
240 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
243 if (spi->mode & SPI_CS_HIGH)
244 bs->cs_polarity |= BIT(spi->chip_select);
245 else
246 bs->cs_polarity &= ~BIT(spi->chip_select);
248 mutex_unlock(&bs->bus_mutex);
250 return 0;
253 static int bcm63xx_hsspi_transfer_one(struct spi_master *master,
254 struct spi_message *msg)
256 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
257 struct spi_transfer *t;
258 struct spi_device *spi = msg->spi;
259 int status = -EINVAL;
260 int dummy_cs;
261 u32 reg;
263 /* This controller does not support keeping CS active during idle.
264 * To work around this, we use the following ugly hack:
266 * a. Invert the target chip select's polarity so it will be active.
267 * b. Select a "dummy" chip select to use as the hardware target.
268 * c. Invert the dummy chip select's polarity so it will be inactive
269 * during the actual transfers.
270 * d. Tell the hardware to send to the dummy chip select. Thanks to
271 * the multiplexed nature of SPI the actual target will receive
272 * the transfer and we see its response.
274 * e. At the end restore the polarities again to their default values.
277 dummy_cs = !spi->chip_select;
278 bcm63xx_hsspi_set_cs(bs, dummy_cs, true);
280 list_for_each_entry(t, &msg->transfers, transfer_list) {
281 status = bcm63xx_hsspi_do_txrx(spi, t);
282 if (status)
283 break;
285 msg->actual_length += t->len;
287 if (t->delay_usecs)
288 udelay(t->delay_usecs);
290 if (t->cs_change)
291 bcm63xx_hsspi_set_cs(bs, spi->chip_select, false);
294 mutex_lock(&bs->bus_mutex);
295 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
296 reg &= ~GLOBAL_CTRL_CS_POLARITY_MASK;
297 reg |= bs->cs_polarity;
298 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
299 mutex_unlock(&bs->bus_mutex);
301 msg->status = status;
302 spi_finalize_current_message(master);
304 return 0;
307 static irqreturn_t bcm63xx_hsspi_interrupt(int irq, void *dev_id)
309 struct bcm63xx_hsspi *bs = (struct bcm63xx_hsspi *)dev_id;
311 if (__raw_readl(bs->regs + HSSPI_INT_STATUS_MASKED_REG) == 0)
312 return IRQ_NONE;
314 __raw_writel(HSSPI_INT_CLEAR_ALL, bs->regs + HSSPI_INT_STATUS_REG);
315 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
317 complete(&bs->done);
319 return IRQ_HANDLED;
322 static int bcm63xx_hsspi_probe(struct platform_device *pdev)
324 struct spi_master *master;
325 struct bcm63xx_hsspi *bs;
326 struct resource *res_mem;
327 void __iomem *regs;
328 struct device *dev = &pdev->dev;
329 struct clk *clk;
330 int irq, ret;
331 u32 reg, rate;
333 irq = platform_get_irq(pdev, 0);
334 if (irq < 0) {
335 dev_err(dev, "no irq\n");
336 return -ENXIO;
339 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
340 regs = devm_ioremap_resource(dev, res_mem);
341 if (IS_ERR(regs))
342 return PTR_ERR(regs);
344 clk = devm_clk_get(dev, "hsspi");
346 if (IS_ERR(clk))
347 return PTR_ERR(clk);
349 rate = clk_get_rate(clk);
350 if (!rate)
351 return -EINVAL;
353 ret = clk_prepare_enable(clk);
354 if (ret)
355 return ret;
357 master = spi_alloc_master(&pdev->dev, sizeof(*bs));
358 if (!master) {
359 ret = -ENOMEM;
360 goto out_disable_clk;
363 bs = spi_master_get_devdata(master);
364 bs->pdev = pdev;
365 bs->clk = clk;
366 bs->regs = regs;
367 bs->speed_hz = rate;
368 bs->fifo = (u8 __iomem *)(bs->regs + HSSPI_FIFO_REG(0));
370 mutex_init(&bs->bus_mutex);
371 init_completion(&bs->done);
373 master->bus_num = HSSPI_BUS_NUM;
374 master->num_chipselect = 8;
375 master->setup = bcm63xx_hsspi_setup;
376 master->transfer_one_message = bcm63xx_hsspi_transfer_one;
377 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
378 master->bits_per_word_mask = SPI_BPW_MASK(8);
379 master->auto_runtime_pm = true;
381 platform_set_drvdata(pdev, master);
383 /* Initialize the hardware */
384 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
386 /* clean up any pending interrupts */
387 __raw_writel(HSSPI_INT_CLEAR_ALL, bs->regs + HSSPI_INT_STATUS_REG);
389 /* read out default CS polarities */
390 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
391 bs->cs_polarity = reg & GLOBAL_CTRL_CS_POLARITY_MASK;
392 __raw_writel(reg | GLOBAL_CTRL_CLK_GATE_SSOFF,
393 bs->regs + HSSPI_GLOBAL_CTRL_REG);
395 ret = devm_request_irq(dev, irq, bcm63xx_hsspi_interrupt, IRQF_SHARED,
396 pdev->name, bs);
398 if (ret)
399 goto out_put_master;
401 /* register and we are done */
402 ret = devm_spi_register_master(dev, master);
403 if (ret)
404 goto out_put_master;
406 return 0;
408 out_put_master:
409 spi_master_put(master);
410 out_disable_clk:
411 clk_disable_unprepare(clk);
412 return ret;
416 static int bcm63xx_hsspi_remove(struct platform_device *pdev)
418 struct spi_master *master = platform_get_drvdata(pdev);
419 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
421 /* reset the hardware and block queue progress */
422 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
423 clk_disable_unprepare(bs->clk);
425 return 0;
428 #ifdef CONFIG_PM_SLEEP
429 static int bcm63xx_hsspi_suspend(struct device *dev)
431 struct spi_master *master = dev_get_drvdata(dev);
432 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
434 spi_master_suspend(master);
435 clk_disable_unprepare(bs->clk);
437 return 0;
440 static int bcm63xx_hsspi_resume(struct device *dev)
442 struct spi_master *master = dev_get_drvdata(dev);
443 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
444 int ret;
446 ret = clk_prepare_enable(bs->clk);
447 if (ret)
448 return ret;
450 spi_master_resume(master);
452 return 0;
454 #endif
456 static SIMPLE_DEV_PM_OPS(bcm63xx_hsspi_pm_ops, bcm63xx_hsspi_suspend,
457 bcm63xx_hsspi_resume);
459 static struct platform_driver bcm63xx_hsspi_driver = {
460 .driver = {
461 .name = "bcm63xx-hsspi",
462 .pm = &bcm63xx_hsspi_pm_ops,
464 .probe = bcm63xx_hsspi_probe,
465 .remove = bcm63xx_hsspi_remove,
468 module_platform_driver(bcm63xx_hsspi_driver);
470 MODULE_ALIAS("platform:bcm63xx_hsspi");
471 MODULE_DESCRIPTION("Broadcom BCM63xx High Speed SPI Controller driver");
472 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
473 MODULE_LICENSE("GPL");