Lines Matching +full:spi +full:- +full:cs +full:- +full:high

2  * Broadcom BCM63XX High Speed SPI Controller driver
4 * Copyright 2000-2010 Broadcom Corporation
5 * Copyright 2012-2013 Jonas Gorski <jogo@openwrt.org>
17 #include <linux/dma-mapping.h>
20 #include <linux/spi/spi.h>
97 #define HSSPI_BUS_NUM 1 /* 0 is legacy SPI */
113 static void bcm63xx_hsspi_set_cs(struct bcm63xx_hsspi *bs, unsigned int cs, in bcm63xx_hsspi_set_cs() argument
118 mutex_lock(&bs->bus_mutex); in bcm63xx_hsspi_set_cs()
119 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG); in bcm63xx_hsspi_set_cs()
121 reg &= ~BIT(cs); in bcm63xx_hsspi_set_cs()
122 if (active == !(bs->cs_polarity & BIT(cs))) in bcm63xx_hsspi_set_cs()
123 reg |= BIT(cs); in bcm63xx_hsspi_set_cs()
125 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG); in bcm63xx_hsspi_set_cs()
126 mutex_unlock(&bs->bus_mutex); in bcm63xx_hsspi_set_cs()
130 struct spi_device *spi, int hz) in bcm63xx_hsspi_set_clk() argument
132 unsigned int profile = spi->chip_select; in bcm63xx_hsspi_set_clk()
135 reg = DIV_ROUND_UP(2048, DIV_ROUND_UP(bs->speed_hz, hz)); in bcm63xx_hsspi_set_clk()
137 bs->regs + HSSPI_PROFILE_CLK_CTRL_REG(profile)); in bcm63xx_hsspi_set_clk()
139 reg = __raw_readl(bs->regs + HSSPI_PROFILE_SIGNAL_CTRL_REG(profile)); in bcm63xx_hsspi_set_clk()
144 __raw_writel(reg, bs->regs + HSSPI_PROFILE_SIGNAL_CTRL_REG(profile)); in bcm63xx_hsspi_set_clk()
146 mutex_lock(&bs->bus_mutex); in bcm63xx_hsspi_set_clk()
148 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG); in bcm63xx_hsspi_set_clk()
150 if (spi->mode & SPI_CPOL) in bcm63xx_hsspi_set_clk()
152 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG); in bcm63xx_hsspi_set_clk()
153 mutex_unlock(&bs->bus_mutex); in bcm63xx_hsspi_set_clk()
156 static int bcm63xx_hsspi_do_txrx(struct spi_device *spi, struct spi_transfer *t) in bcm63xx_hsspi_do_txrx() argument
158 struct bcm63xx_hsspi *bs = spi_master_get_devdata(spi->master); in bcm63xx_hsspi_do_txrx()
159 unsigned int chip_select = spi->chip_select; in bcm63xx_hsspi_do_txrx()
161 int pending = t->len; in bcm63xx_hsspi_do_txrx()
163 const u8 *tx = t->tx_buf; in bcm63xx_hsspi_do_txrx()
164 u8 *rx = t->rx_buf; in bcm63xx_hsspi_do_txrx()
166 bcm63xx_hsspi_set_clk(bs, spi, t->speed_hz); in bcm63xx_hsspi_do_txrx()
167 bcm63xx_hsspi_set_cs(bs, spi->chip_select, true); in bcm63xx_hsspi_do_txrx()
177 step_size -= HSSPI_OPCODE_LEN; in bcm63xx_hsspi_do_txrx()
179 if ((opcode == HSSPI_OP_READ && t->rx_nbits == SPI_NBITS_DUAL) || in bcm63xx_hsspi_do_txrx()
180 (opcode == HSSPI_OP_WRITE && t->tx_nbits == SPI_NBITS_DUAL)) in bcm63xx_hsspi_do_txrx()
185 bs->regs + HSSPI_PROFILE_MODE_CTRL_REG(chip_select)); in bcm63xx_hsspi_do_txrx()
190 reinit_completion(&bs->done); in bcm63xx_hsspi_do_txrx()
192 memcpy_toio(bs->fifo + HSSPI_OPCODE_LEN, tx, curr_step); in bcm63xx_hsspi_do_txrx()
196 __raw_writew(opcode | curr_step, bs->fifo); in bcm63xx_hsspi_do_txrx()
200 bs->regs + HSSPI_INT_MASK_REG); in bcm63xx_hsspi_do_txrx()
206 bs->regs + HSSPI_PINGPONG_COMMAND_REG(0)); in bcm63xx_hsspi_do_txrx()
208 if (wait_for_completion_timeout(&bs->done, HZ) == 0) { in bcm63xx_hsspi_do_txrx()
209 dev_err(&bs->pdev->dev, "transfer timed out!\n"); in bcm63xx_hsspi_do_txrx()
210 return -ETIMEDOUT; in bcm63xx_hsspi_do_txrx()
214 memcpy_fromio(rx, bs->fifo, curr_step); in bcm63xx_hsspi_do_txrx()
218 pending -= curr_step; in bcm63xx_hsspi_do_txrx()
224 static int bcm63xx_hsspi_setup(struct spi_device *spi) in bcm63xx_hsspi_setup() argument
226 struct bcm63xx_hsspi *bs = spi_master_get_devdata(spi->master); in bcm63xx_hsspi_setup()
229 reg = __raw_readl(bs->regs + in bcm63xx_hsspi_setup()
230 HSSPI_PROFILE_SIGNAL_CTRL_REG(spi->chip_select)); in bcm63xx_hsspi_setup()
232 if (spi->mode & SPI_CPHA) in bcm63xx_hsspi_setup()
236 __raw_writel(reg, bs->regs + in bcm63xx_hsspi_setup()
237 HSSPI_PROFILE_SIGNAL_CTRL_REG(spi->chip_select)); in bcm63xx_hsspi_setup()
239 mutex_lock(&bs->bus_mutex); in bcm63xx_hsspi_setup()
240 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG); in bcm63xx_hsspi_setup()
243 if ((reg & GLOBAL_CTRL_CS_POLARITY_MASK) == bs->cs_polarity) { in bcm63xx_hsspi_setup()
244 if (spi->mode & SPI_CS_HIGH) in bcm63xx_hsspi_setup()
245 reg |= BIT(spi->chip_select); in bcm63xx_hsspi_setup()
247 reg &= ~BIT(spi->chip_select); in bcm63xx_hsspi_setup()
248 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG); in bcm63xx_hsspi_setup()
251 if (spi->mode & SPI_CS_HIGH) in bcm63xx_hsspi_setup()
252 bs->cs_polarity |= BIT(spi->chip_select); in bcm63xx_hsspi_setup()
254 bs->cs_polarity &= ~BIT(spi->chip_select); in bcm63xx_hsspi_setup()
256 mutex_unlock(&bs->bus_mutex); in bcm63xx_hsspi_setup()
266 struct spi_device *spi = msg->spi; in bcm63xx_hsspi_transfer_one() local
267 int status = -EINVAL; in bcm63xx_hsspi_transfer_one()
271 /* This controller does not support keeping CS active during idle. in bcm63xx_hsspi_transfer_one()
279 * the multiplexed nature of SPI the actual target will receive in bcm63xx_hsspi_transfer_one()
285 dummy_cs = !spi->chip_select; in bcm63xx_hsspi_transfer_one()
288 list_for_each_entry(t, &msg->transfers, transfer_list) { in bcm63xx_hsspi_transfer_one()
289 status = bcm63xx_hsspi_do_txrx(spi, t); in bcm63xx_hsspi_transfer_one()
293 msg->actual_length += t->len; in bcm63xx_hsspi_transfer_one()
297 if (t->cs_change) in bcm63xx_hsspi_transfer_one()
298 bcm63xx_hsspi_set_cs(bs, spi->chip_select, false); in bcm63xx_hsspi_transfer_one()
301 mutex_lock(&bs->bus_mutex); in bcm63xx_hsspi_transfer_one()
302 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG); in bcm63xx_hsspi_transfer_one()
304 reg |= bs->cs_polarity; in bcm63xx_hsspi_transfer_one()
305 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG); in bcm63xx_hsspi_transfer_one()
306 mutex_unlock(&bs->bus_mutex); in bcm63xx_hsspi_transfer_one()
308 msg->status = status; in bcm63xx_hsspi_transfer_one()
318 if (__raw_readl(bs->regs + HSSPI_INT_STATUS_MASKED_REG) == 0) in bcm63xx_hsspi_interrupt()
321 __raw_writel(HSSPI_INT_CLEAR_ALL, bs->regs + HSSPI_INT_STATUS_REG); in bcm63xx_hsspi_interrupt()
322 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG); in bcm63xx_hsspi_interrupt()
324 complete(&bs->done); in bcm63xx_hsspi_interrupt()
334 struct device *dev = &pdev->dev; in bcm63xx_hsspi_probe()
382 ret = -EINVAL; in bcm63xx_hsspi_probe()
387 master = spi_alloc_master(&pdev->dev, sizeof(*bs)); in bcm63xx_hsspi_probe()
389 ret = -ENOMEM; in bcm63xx_hsspi_probe()
394 bs->pdev = pdev; in bcm63xx_hsspi_probe()
395 bs->clk = clk; in bcm63xx_hsspi_probe()
396 bs->pll_clk = pll_clk; in bcm63xx_hsspi_probe()
397 bs->regs = regs; in bcm63xx_hsspi_probe()
398 bs->speed_hz = rate; in bcm63xx_hsspi_probe()
399 bs->fifo = (u8 __iomem *)(bs->regs + HSSPI_FIFO_REG(0)); in bcm63xx_hsspi_probe()
401 mutex_init(&bs->bus_mutex); in bcm63xx_hsspi_probe()
402 init_completion(&bs->done); in bcm63xx_hsspi_probe()
404 master->dev.of_node = dev->of_node; in bcm63xx_hsspi_probe()
405 if (!dev->of_node) in bcm63xx_hsspi_probe()
406 master->bus_num = HSSPI_BUS_NUM; in bcm63xx_hsspi_probe()
408 of_property_read_u32(dev->of_node, "num-cs", &num_cs); in bcm63xx_hsspi_probe()
410 dev_warn(dev, "unsupported number of cs (%i), reducing to 8\n", in bcm63xx_hsspi_probe()
414 master->num_chipselect = num_cs; in bcm63xx_hsspi_probe()
415 master->setup = bcm63xx_hsspi_setup; in bcm63xx_hsspi_probe()
416 master->transfer_one_message = bcm63xx_hsspi_transfer_one; in bcm63xx_hsspi_probe()
417 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | in bcm63xx_hsspi_probe()
419 master->bits_per_word_mask = SPI_BPW_MASK(8); in bcm63xx_hsspi_probe()
420 master->auto_runtime_pm = true; in bcm63xx_hsspi_probe()
425 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG); in bcm63xx_hsspi_probe()
428 __raw_writel(HSSPI_INT_CLEAR_ALL, bs->regs + HSSPI_INT_STATUS_REG); in bcm63xx_hsspi_probe()
430 /* read out default CS polarities */ in bcm63xx_hsspi_probe()
431 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG); in bcm63xx_hsspi_probe()
432 bs->cs_polarity = reg & GLOBAL_CTRL_CS_POLARITY_MASK; in bcm63xx_hsspi_probe()
434 bs->regs + HSSPI_GLOBAL_CTRL_REG); in bcm63xx_hsspi_probe()
437 pdev->name, bs); in bcm63xx_hsspi_probe()
465 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG); in bcm63xx_hsspi_remove()
466 clk_disable_unprepare(bs->pll_clk); in bcm63xx_hsspi_remove()
467 clk_disable_unprepare(bs->clk); in bcm63xx_hsspi_remove()
479 clk_disable_unprepare(bs->pll_clk); in bcm63xx_hsspi_suspend()
480 clk_disable_unprepare(bs->clk); in bcm63xx_hsspi_suspend()
491 ret = clk_prepare_enable(bs->clk); in bcm63xx_hsspi_resume()
495 if (bs->pll_clk) { in bcm63xx_hsspi_resume()
496 ret = clk_prepare_enable(bs->pll_clk); in bcm63xx_hsspi_resume()
511 { .compatible = "brcm,bcm6328-hsspi", },
518 .name = "bcm63xx-hsspi",
529 MODULE_DESCRIPTION("Broadcom BCM63xx High Speed SPI Controller driver");