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

1 // SPDX-License-Identifier: GPL-2.0-only
3 * SPI_PPC4XX SPI controller driver.
9 * Based in part on drivers/spi/spi_s3c24xx.c
17 * The PPC4xx SPI controller has no FIFO so each sent/received byte will
18 * generate an interrupt to the CPU. This can cause high CPU utilization.
20 * during SPI transfers by setting max_speed_hz via the device tree.
33 #include <linux/spi/spi.h>
34 #include <linux/spi/spi_bitbang.h>
38 #include <asm/dcr-regs.h>
40 /* bits in mode register - bit 0 is MSb */
53 * SPI_PPC4XX_MODE_RD = 0 means "MSB first" - this is the normal mode
54 * SPI_PPC4XX_MODE_RD = 1 means "LSB first" - this is bit-reversed mode
61 * SPI_PPC4XX_MODE_CI = 1 means "clock idles high"
82 /* clock settings (SCP and CI) for various SPI modes */
102 * CDM = (OPBCLK/4*SCPClkOut) - 1
108 /* SPI Controller driver's private data. */
117 /* need this to set the SPI clock */
137 static int spi_ppc4xx_txrx(struct spi_device *spi, struct spi_transfer *t) in spi_ppc4xx_txrx() argument
142 dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n", in spi_ppc4xx_txrx()
143 t->tx_buf, t->rx_buf, t->len); in spi_ppc4xx_txrx()
145 hw = spi_controller_get_devdata(spi->controller); in spi_ppc4xx_txrx()
147 hw->tx = t->tx_buf; in spi_ppc4xx_txrx()
148 hw->rx = t->rx_buf; in spi_ppc4xx_txrx()
149 hw->len = t->len; in spi_ppc4xx_txrx()
150 hw->count = 0; in spi_ppc4xx_txrx()
153 data = hw->tx ? hw->tx[0] : 0; in spi_ppc4xx_txrx()
154 out_8(&hw->regs->txd, data); in spi_ppc4xx_txrx()
155 out_8(&hw->regs->cr, SPI_PPC4XX_CR_STR); in spi_ppc4xx_txrx()
156 wait_for_completion(&hw->done); in spi_ppc4xx_txrx()
158 return hw->count; in spi_ppc4xx_txrx()
161 static int spi_ppc4xx_setupxfer(struct spi_device *spi, struct spi_transfer *t) in spi_ppc4xx_setupxfer() argument
163 struct ppc4xx_spi *hw = spi_controller_get_devdata(spi->controller); in spi_ppc4xx_setupxfer()
164 struct spi_ppc4xx_cs *cs = spi->controller_state; in spi_ppc4xx_setupxfer() local
170 speed = spi->max_speed_hz; in spi_ppc4xx_setupxfer()
177 if (t->speed_hz) in spi_ppc4xx_setupxfer()
178 speed = min(t->speed_hz, spi->max_speed_hz); in spi_ppc4xx_setupxfer()
181 if (!speed || (speed > spi->max_speed_hz)) { in spi_ppc4xx_setupxfer()
182 dev_err(&spi->dev, "invalid speed_hz (%d)\n", speed); in spi_ppc4xx_setupxfer()
183 return -EINVAL; in spi_ppc4xx_setupxfer()
187 out_8(&hw->regs->mode, cs->mode); in spi_ppc4xx_setupxfer()
191 scr = (hw->opb_freq / speed) - 1; in spi_ppc4xx_setupxfer()
195 dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", cdm, speed); in spi_ppc4xx_setupxfer()
197 if (in_8(&hw->regs->cdm) != cdm) in spi_ppc4xx_setupxfer()
198 out_8(&hw->regs->cdm, cdm); in spi_ppc4xx_setupxfer()
200 mutex_lock(&hw->bitbang.lock); in spi_ppc4xx_setupxfer()
201 if (!hw->bitbang.busy) { in spi_ppc4xx_setupxfer()
202 hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE); in spi_ppc4xx_setupxfer()
205 mutex_unlock(&hw->bitbang.lock); in spi_ppc4xx_setupxfer()
210 static int spi_ppc4xx_setup(struct spi_device *spi) in spi_ppc4xx_setup() argument
212 struct spi_ppc4xx_cs *cs = spi->controller_state; in spi_ppc4xx_setup() local
214 if (!spi->max_speed_hz) { in spi_ppc4xx_setup()
215 dev_err(&spi->dev, "invalid max_speed_hz (must be non-zero)\n"); in spi_ppc4xx_setup()
216 return -EINVAL; in spi_ppc4xx_setup()
219 if (cs == NULL) { in spi_ppc4xx_setup()
220 cs = kzalloc(sizeof(*cs), GFP_KERNEL); in spi_ppc4xx_setup()
221 if (!cs) in spi_ppc4xx_setup()
222 return -ENOMEM; in spi_ppc4xx_setup()
223 spi->controller_state = cs; in spi_ppc4xx_setup()
228 * no need to read-modify-write in spi_ppc4xx_setup()
230 cs->mode = SPI_PPC4XX_MODE_SPE; in spi_ppc4xx_setup()
232 switch (spi->mode & SPI_MODE_X_MASK) { in spi_ppc4xx_setup()
234 cs->mode |= SPI_CLK_MODE0; in spi_ppc4xx_setup()
237 cs->mode |= SPI_CLK_MODE1; in spi_ppc4xx_setup()
240 cs->mode |= SPI_CLK_MODE2; in spi_ppc4xx_setup()
243 cs->mode |= SPI_CLK_MODE3; in spi_ppc4xx_setup()
247 if (spi->mode & SPI_LSB_FIRST) in spi_ppc4xx_setup()
248 cs->mode |= SPI_PPC4XX_MODE_RD; in spi_ppc4xx_setup()
262 status = in_8(&hw->regs->sr); in spi_ppc4xx_int()
267 * BSY de-asserts one cycle after the transfer is complete. The in spi_ppc4xx_int()
276 dev_dbg(hw->dev, "got interrupt but spi still busy?\n"); in spi_ppc4xx_int()
279 lstatus = in_8(&hw->regs->sr); in spi_ppc4xx_int()
283 dev_err(hw->dev, "busywait: too many loops!\n"); in spi_ppc4xx_int()
284 complete(&hw->done); in spi_ppc4xx_int()
288 status = in_8(&hw->regs->sr); in spi_ppc4xx_int()
289 dev_dbg(hw->dev, "loops %d status %x\n", cnt, status); in spi_ppc4xx_int()
293 count = hw->count; in spi_ppc4xx_int()
294 hw->count++; in spi_ppc4xx_int()
297 data = in_8(&hw->regs->rxd); in spi_ppc4xx_int()
298 if (hw->rx) in spi_ppc4xx_int()
299 hw->rx[count] = data; in spi_ppc4xx_int()
303 if (count < hw->len) { in spi_ppc4xx_int()
304 data = hw->tx ? hw->tx[count] : 0; in spi_ppc4xx_int()
305 out_8(&hw->regs->txd, data); in spi_ppc4xx_int()
306 out_8(&hw->regs->cr, SPI_PPC4XX_CR_STR); in spi_ppc4xx_int()
308 complete(&hw->done); in spi_ppc4xx_int()
314 static void spi_ppc4xx_cleanup(struct spi_device *spi) in spi_ppc4xx_cleanup() argument
316 kfree(spi->controller_state); in spi_ppc4xx_cleanup()
322 * On all 4xx PPC's the SPI bus is shared/multiplexed with in spi_ppc4xx_enable()
323 * the 2nd I2C bus. We need to enable the SPI bus before in spi_ppc4xx_enable()
340 struct device_node *np = op->dev.of_node; in spi_ppc4xx_of_probe()
341 struct device *dev = &op->dev; in spi_ppc4xx_of_probe()
348 return -ENOMEM; in spi_ppc4xx_of_probe()
349 host->dev.of_node = np; in spi_ppc4xx_of_probe()
352 hw->host = host; in spi_ppc4xx_of_probe()
353 hw->dev = dev; in spi_ppc4xx_of_probe()
355 init_completion(&hw->done); in spi_ppc4xx_of_probe()
358 bbp = &hw->bitbang; in spi_ppc4xx_of_probe()
359 bbp->master = hw->host; in spi_ppc4xx_of_probe()
360 bbp->setup_transfer = spi_ppc4xx_setupxfer; in spi_ppc4xx_of_probe()
361 bbp->txrx_bufs = spi_ppc4xx_txrx; in spi_ppc4xx_of_probe()
362 bbp->use_dma = 0; in spi_ppc4xx_of_probe()
363 bbp->master->setup = spi_ppc4xx_setup; in spi_ppc4xx_of_probe()
364 bbp->master->cleanup = spi_ppc4xx_cleanup; in spi_ppc4xx_of_probe()
365 bbp->master->bits_per_word_mask = SPI_BPW_MASK(8); in spi_ppc4xx_of_probe()
366 bbp->master->use_gpio_descriptors = true; in spi_ppc4xx_of_probe()
368 * The SPI core will count the number of GPIO descriptors to figure in spi_ppc4xx_of_probe()
371 bbp->master->num_chipselect = 0; in spi_ppc4xx_of_probe()
373 /* the spi->mode bits understood by this driver: */ in spi_ppc4xx_of_probe()
374 bbp->master->mode_bits = in spi_ppc4xx_of_probe()
381 ret = -ENODEV; in spi_ppc4xx_of_probe()
385 clk = of_get_property(opbnp, "clock-frequency", NULL); in spi_ppc4xx_of_probe()
387 dev_err(dev, "OPB: no clock-frequency property set\n"); in spi_ppc4xx_of_probe()
389 ret = -ENODEV; in spi_ppc4xx_of_probe()
392 hw->opb_freq = *clk; in spi_ppc4xx_of_probe()
393 hw->opb_freq >>= 2; in spi_ppc4xx_of_probe()
401 hw->mapbase = resource.start; in spi_ppc4xx_of_probe()
402 hw->mapsize = resource_size(&resource); in spi_ppc4xx_of_probe()
405 if (hw->mapsize < sizeof(struct spi_ppc4xx_regs)) { in spi_ppc4xx_of_probe()
407 ret = -EINVAL; in spi_ppc4xx_of_probe()
415 hw->irqnum = ret; in spi_ppc4xx_of_probe()
417 ret = request_irq(hw->irqnum, spi_ppc4xx_int, in spi_ppc4xx_of_probe()
424 if (!request_mem_region(hw->mapbase, hw->mapsize, DRIVER_NAME)) { in spi_ppc4xx_of_probe()
426 ret = -EBUSY; in spi_ppc4xx_of_probe()
430 hw->regs = ioremap(hw->mapbase, sizeof(struct spi_ppc4xx_regs)); in spi_ppc4xx_of_probe()
432 if (!hw->regs) { in spi_ppc4xx_of_probe()
434 ret = -ENXIO; in spi_ppc4xx_of_probe()
440 /* Finally register our spi controller */ in spi_ppc4xx_of_probe()
441 dev->dma_mask = 0; in spi_ppc4xx_of_probe()
444 dev_err(dev, "failed to register SPI host\n"); in spi_ppc4xx_of_probe()
453 iounmap(hw->regs); in spi_ppc4xx_of_probe()
455 release_mem_region(hw->mapbase, hw->mapsize); in spi_ppc4xx_of_probe()
457 free_irq(hw->irqnum, hw); in spi_ppc4xx_of_probe()
470 spi_bitbang_stop(&hw->bitbang); in spi_ppc4xx_of_remove()
471 release_mem_region(hw->mapbase, hw->mapsize); in spi_ppc4xx_of_remove()
472 free_irq(hw->irqnum, hw); in spi_ppc4xx_of_remove()
473 iounmap(hw->regs); in spi_ppc4xx_of_remove()
478 { .compatible = "ibm,ppc4xx-spi", },
495 MODULE_DESCRIPTION("Simple PPC4xx SPI Driver");