Lines Matching +full:bcm6358 +full:- +full:spi
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Broadcom BCM63xx SPI controller support
5 * Copyright (C) 2009-2012 Florian Fainelli <florian@openwrt.org>
16 #include <linux/spi/spi.h>
23 /* BCM 6338/6348 SPI core */
25 #define SPI_6348_CMD 0x00 /* 16-bits register */
34 #define SPI_6348_MSG_CTL 0x40 /* 8-bits register */
41 /* BCM 3368/6358/6262/6368 SPI core */
43 #define SPI_6358_MSG_CTL 0x00 /* 16-bits register */
49 #define SPI_6358_CMD 0x700 /* 16-bits register */
59 /* Shared SPI definitions */
157 return readb(bs->regs + bs->reg_offsets[offset]); in bcm_spi_readb()
163 writeb(value, bs->regs + bs->reg_offsets[offset]); in bcm_spi_writeb()
170 iowrite16be(value, bs->regs + bs->reg_offsets[offset]); in bcm_spi_writew()
172 writew(value, bs->regs + bs->reg_offsets[offset]); in bcm_spi_writew()
186 static void bcm63xx_spi_setup_transfer(struct spi_device *spi, in bcm63xx_spi_setup_transfer() argument
189 struct bcm63xx_spi *bs = spi_controller_get_devdata(spi->controller); in bcm63xx_spi_setup_transfer()
198 if (t->speed_hz >= bcm63xx_spi_freq_table[i][0]) { in bcm63xx_spi_setup_transfer()
210 dev_dbg(&spi->dev, "Setting clock register to %02x (hz %d)\n", in bcm63xx_spi_setup_transfer()
211 clk_cfg, t->speed_hz); in bcm63xx_spi_setup_transfer()
214 /* the spi->mode bits understood by this driver: */
217 static int bcm63xx_txrx_bufs(struct spi_device *spi, struct spi_transfer *first, in bcm63xx_txrx_bufs() argument
220 struct bcm63xx_spi *bs = spi_controller_get_devdata(spi->controller); in bcm63xx_txrx_bufs()
231 dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n", in bcm63xx_txrx_bufs()
232 t->tx_buf, t->rx_buf, t->len); in bcm63xx_txrx_bufs()
234 if (num_transfers > 1 && t->tx_buf && t->len <= BCM63XX_SPI_MAX_PREPEND) in bcm63xx_txrx_bufs()
235 prepend_len = t->len; in bcm63xx_txrx_bufs()
239 if (t->tx_buf) { in bcm63xx_txrx_bufs()
241 memcpy_toio(bs->tx_io + len, t->tx_buf, t->len); in bcm63xx_txrx_bufs()
248 if (t->rx_buf) { in bcm63xx_txrx_bufs()
250 /* prepend is half-duplex write only */ in bcm63xx_txrx_bufs()
255 len += t->len; in bcm63xx_txrx_bufs()
257 t = list_entry(t->transfer_list.next, struct spi_transfer, in bcm63xx_txrx_bufs()
261 reinit_completion(&bs->done); in bcm63xx_txrx_bufs()
267 msg_ctl |= (SPI_FD_RW << bs->msg_type_shift); in bcm63xx_txrx_bufs()
269 msg_ctl |= (SPI_HD_R << bs->msg_type_shift); in bcm63xx_txrx_bufs()
271 msg_ctl |= (SPI_HD_W << bs->msg_type_shift); in bcm63xx_txrx_bufs()
273 switch (bs->msg_ctl_width) { in bcm63xx_txrx_bufs()
285 cmd |= (spi_get_chipselect(spi, 0) << SPI_CMD_DEVICE_ID_SHIFT); in bcm63xx_txrx_bufs()
291 timeout = wait_for_completion_timeout(&bs->done, HZ); in bcm63xx_txrx_bufs()
293 return -ETIMEDOUT; in bcm63xx_txrx_bufs()
302 if (t->rx_buf) in bcm63xx_txrx_bufs()
303 memcpy_fromio(t->rx_buf, bs->rx_io + len, t->len); in bcm63xx_txrx_bufs()
306 len += t->len; in bcm63xx_txrx_bufs()
308 t = list_entry(t->transfer_list.next, struct spi_transfer, in bcm63xx_txrx_bufs()
320 struct spi_device *spi = m->spi; in bcm63xx_spi_transfer_one() local
326 * This SPI controller does not support keeping CS active after a in bcm63xx_spi_transfer_one()
329 * full-duplex transfers. in bcm63xx_spi_transfer_one()
331 list_for_each_entry(t, &m->transfers, transfer_list) { in bcm63xx_spi_transfer_one()
336 total_len += t->len; in bcm63xx_spi_transfer_one()
338 if (n_transfers == 2 && !first->rx_buf && !t->tx_buf && in bcm63xx_spi_transfer_one()
339 first->len <= BCM63XX_SPI_MAX_PREPEND) in bcm63xx_spi_transfer_one()
341 else if (can_use_prepend && t->tx_buf) in bcm63xx_spi_transfer_one()
346 total_len > (bs->fifo_size + BCM63XX_SPI_MAX_PREPEND)) || in bcm63xx_spi_transfer_one()
347 (!can_use_prepend && total_len > bs->fifo_size)) { in bcm63xx_spi_transfer_one()
348 dev_err(&spi->dev, "unable to do transfers larger than FIFO size (%i > %i)\n", in bcm63xx_spi_transfer_one()
349 total_len, bs->fifo_size); in bcm63xx_spi_transfer_one()
350 status = -EINVAL; in bcm63xx_spi_transfer_one()
355 if (t->speed_hz != first->speed_hz) { in bcm63xx_spi_transfer_one()
356 dev_err(&spi->dev, "unable to change speed between transfers\n"); in bcm63xx_spi_transfer_one()
357 status = -EINVAL; in bcm63xx_spi_transfer_one()
362 if (t->delay.value) { in bcm63xx_spi_transfer_one()
363 dev_err(&spi->dev, "unable to keep CS asserted after transfer\n"); in bcm63xx_spi_transfer_one()
364 status = -EINVAL; in bcm63xx_spi_transfer_one()
368 if (t->cs_change || in bcm63xx_spi_transfer_one()
369 list_is_last(&t->transfer_list, &m->transfers)) { in bcm63xx_spi_transfer_one()
371 bcm63xx_spi_setup_transfer(spi, first); in bcm63xx_spi_transfer_one()
374 status = bcm63xx_txrx_bufs(spi, first, n_transfers); in bcm63xx_spi_transfer_one()
378 m->actual_length += total_len; in bcm63xx_spi_transfer_one()
387 m->status = status; in bcm63xx_spi_transfer_one()
409 complete(&bs->done); in bcm63xx_spi_interrupt()
414 static size_t bcm63xx_spi_max_length(struct spi_device *spi) in bcm63xx_spi_max_length() argument
416 struct bcm63xx_spi *bs = spi_controller_get_devdata(spi->controller); in bcm63xx_spi_max_length()
418 return bs->fifo_size; in bcm63xx_spi_max_length()
459 .name = "bcm6348-spi",
463 .name = "bcm6358-spi",
472 { .compatible = "brcm,bcm6348-spi", .data = &bcm6348_spi_reg_offsets },
473 { .compatible = "brcm,bcm6358-spi", .data = &bcm6358_spi_reg_offsets },
482 struct device *dev = &pdev->dev; in bcm63xx_spi_probe()
491 if (dev->of_node) { in bcm63xx_spi_probe()
494 match = of_match_node(bcm63xx_spi_of_match, dev->of_node); in bcm63xx_spi_probe()
496 return -EINVAL; in bcm63xx_spi_probe()
497 bcm63xx_spireg = match->data; in bcm63xx_spi_probe()
499 of_property_read_u32(dev->of_node, "num-cs", &num_cs); in bcm63xx_spi_probe()
506 bus_num = -1; in bcm63xx_spi_probe()
507 } else if (pdev->id_entry->driver_data) { in bcm63xx_spi_probe()
508 const struct platform_device_id *match = pdev->id_entry; in bcm63xx_spi_probe()
510 bcm63xx_spireg = (const unsigned long *)match->driver_data; in bcm63xx_spi_probe()
513 return -EINVAL; in bcm63xx_spi_probe()
520 clk = devm_clk_get(dev, "spi"); in bcm63xx_spi_probe()
533 return -ENOMEM; in bcm63xx_spi_probe()
537 init_completion(&bs->done); in bcm63xx_spi_probe()
540 bs->pdev = pdev; in bcm63xx_spi_probe()
542 bs->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &r); in bcm63xx_spi_probe()
543 if (IS_ERR(bs->regs)) { in bcm63xx_spi_probe()
544 ret = PTR_ERR(bs->regs); in bcm63xx_spi_probe()
548 bs->irq = irq; in bcm63xx_spi_probe()
549 bs->clk = clk; in bcm63xx_spi_probe()
550 bs->reg_offsets = bcm63xx_spireg; in bcm63xx_spi_probe()
551 bs->fifo_size = bs->reg_offsets[SPI_MSG_DATA_SIZE]; in bcm63xx_spi_probe()
553 ret = devm_request_irq(&pdev->dev, irq, bcm63xx_spi_interrupt, 0, in bcm63xx_spi_probe()
554 pdev->name, host); in bcm63xx_spi_probe()
560 host->dev.of_node = dev->of_node; in bcm63xx_spi_probe()
561 host->bus_num = bus_num; in bcm63xx_spi_probe()
562 host->num_chipselect = num_cs; in bcm63xx_spi_probe()
563 host->transfer_one_message = bcm63xx_spi_transfer_one; in bcm63xx_spi_probe()
564 host->mode_bits = MODEBITS; in bcm63xx_spi_probe()
565 host->bits_per_word_mask = SPI_BPW_MASK(8); in bcm63xx_spi_probe()
566 host->max_transfer_size = bcm63xx_spi_max_length; in bcm63xx_spi_probe()
567 host->max_message_size = bcm63xx_spi_max_length; in bcm63xx_spi_probe()
568 host->auto_runtime_pm = true; in bcm63xx_spi_probe()
569 bs->msg_type_shift = bs->reg_offsets[SPI_MSG_TYPE_SHIFT]; in bcm63xx_spi_probe()
570 bs->msg_ctl_width = bs->reg_offsets[SPI_MSG_CTL_WIDTH]; in bcm63xx_spi_probe()
571 bs->tx_io = (u8 *)(bs->regs + bs->reg_offsets[SPI_MSG_DATA]); in bcm63xx_spi_probe()
572 bs->rx_io = (const u8 *)(bs->regs + bs->reg_offsets[SPI_RX_DATA]); in bcm63xx_spi_probe()
575 ret = clk_prepare_enable(bs->clk); in bcm63xx_spi_probe()
587 ret = devm_pm_runtime_enable(&pdev->dev); in bcm63xx_spi_probe()
594 dev_err(dev, "spi register failed\n"); in bcm63xx_spi_probe()
599 r, irq, bs->fifo_size); in bcm63xx_spi_probe()
615 /* reset spi block */ in bcm63xx_spi_remove()
619 clk_disable_unprepare(bs->clk); in bcm63xx_spi_remove()
629 clk_disable_unprepare(bs->clk); in bcm63xx_spi_suspend()
640 ret = clk_prepare_enable(bs->clk); in bcm63xx_spi_resume()
653 .name = "bcm63xx-spi",
667 MODULE_DESCRIPTION("Broadcom BCM63xx SPI Controller driver");