Lines Matching +full:tx +full:- +full:clk +full:- +full:10 +full:- +full:inverted
1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/clk.h>
34 #define SIFIVE_SPI_REG_TXDATA 0x48 /* Tx FIFO data */
36 #define SIFIVE_SPI_REG_TXMARK 0x50 /* Tx FIFO watermark */
93 struct clk *clk; /* bus clock */ member
96 struct completion done; /* wake-up from interrupt */
101 iowrite32(value, spi->regs + offset); in sifive_spi_write()
106 return ioread32(spi->regs + offset); in sifive_spi_read()
126 /* Exit specialized memory-mapped SPI flash mode */ in sifive_spi_init()
134 struct spi_device *device = msg->spi; in sifive_spi_prepare_message()
137 if (device->mode & SPI_CS_HIGH) in sifive_spi_prepare_message()
138 spi->cs_inactive &= ~BIT(spi_get_chipselect(device, 0)); in sifive_spi_prepare_message()
140 spi->cs_inactive |= BIT(spi_get_chipselect(device, 0)); in sifive_spi_prepare_message()
141 sifive_spi_write(spi, SIFIVE_SPI_REG_CSDEF, spi->cs_inactive); in sifive_spi_prepare_message()
148 device->mode & SIFIVE_SPI_SCKMODE_MODE_MASK); in sifive_spi_prepare_message()
155 struct sifive_spi *spi = spi_controller_get_devdata(device->controller); in sifive_spi_set_cs()
157 /* Reverse polarity is handled by SCMR/CPOL. Not inverted CS. */ in sifive_spi_set_cs()
158 if (device->mode & SPI_CS_HIGH) in sifive_spi_set_cs()
174 cr = DIV_ROUND_UP(clk_get_rate(spi->clk) >> 1, t->speed_hz) - 1; in sifive_spi_prep_transfer()
178 mode = max_t(unsigned int, t->rx_nbits, t->tx_nbits); in sifive_spi_prep_transfer()
181 cr = SIFIVE_SPI_FMT_LEN(t->bits_per_word); in sifive_spi_prep_transfer()
193 if (device->mode & SPI_LSB_FIRST) in sifive_spi_prep_transfer()
195 if (!t->rx_buf) in sifive_spi_prep_transfer()
202 * (8/mode) * fifo_depth / hz <= 5 * 10^-6 in sifive_spi_prep_transfer()
205 return 1600000 * spi->fifo_depth <= t->speed_hz * mode; in sifive_spi_prep_transfer()
216 complete(&spi->done); in sifive_spi_irq()
232 reinit_completion(&spi->done); in sifive_spi_wait()
234 wait_for_completion(&spi->done); in sifive_spi_wait()
260 const u8 *tx_ptr = t->tx_buf; in sifive_spi_transfer_one()
261 u8 *rx_ptr = t->rx_buf; in sifive_spi_transfer_one()
262 unsigned int remaining_words = t->len; in sifive_spi_transfer_one()
265 unsigned int n_words = min(remaining_words, spi->fifo_depth); in sifive_spi_transfer_one()
275 n_words - 1); in sifive_spi_transfer_one()
286 remaining_words -= n_words; in sifive_spi_transfer_one()
299 host = spi_alloc_host(&pdev->dev, sizeof(struct sifive_spi)); in sifive_spi_probe()
301 dev_err(&pdev->dev, "out of memory\n"); in sifive_spi_probe()
302 return -ENOMEM; in sifive_spi_probe()
306 init_completion(&spi->done); in sifive_spi_probe()
309 spi->regs = devm_platform_ioremap_resource(pdev, 0); in sifive_spi_probe()
310 if (IS_ERR(spi->regs)) { in sifive_spi_probe()
311 ret = PTR_ERR(spi->regs); in sifive_spi_probe()
315 spi->clk = devm_clk_get(&pdev->dev, NULL); in sifive_spi_probe()
316 if (IS_ERR(spi->clk)) { in sifive_spi_probe()
317 dev_err(&pdev->dev, "Unable to find bus clock\n"); in sifive_spi_probe()
318 ret = PTR_ERR(spi->clk); in sifive_spi_probe()
330 of_property_read_u32(pdev->dev.of_node, "sifive,fifo-depth", in sifive_spi_probe()
331 &spi->fifo_depth); in sifive_spi_probe()
333 spi->fifo_depth = SIFIVE_SPI_DEFAULT_DEPTH; in sifive_spi_probe()
336 of_property_read_u32(pdev->dev.of_node, "sifive,max-bits-per-word", in sifive_spi_probe()
340 dev_err(&pdev->dev, "Only 8bit SPI words supported by the driver\n"); in sifive_spi_probe()
341 ret = -EINVAL; in sifive_spi_probe()
346 ret = clk_prepare_enable(spi->clk); in sifive_spi_probe()
348 dev_err(&pdev->dev, "Unable to enable bus clock\n"); in sifive_spi_probe()
353 spi->cs_inactive = sifive_spi_read(spi, SIFIVE_SPI_REG_CSDEF); in sifive_spi_probe()
356 sifive_spi_write(spi, SIFIVE_SPI_REG_CSDEF, spi->cs_inactive); in sifive_spi_probe()
358 dev_err(&pdev->dev, "Could not auto probe CS lines\n"); in sifive_spi_probe()
359 ret = -EINVAL; in sifive_spi_probe()
365 dev_err(&pdev->dev, "Invalid number of spi targets\n"); in sifive_spi_probe()
366 ret = -EINVAL; in sifive_spi_probe()
371 host->dev.of_node = pdev->dev.of_node; in sifive_spi_probe()
372 host->bus_num = pdev->id; in sifive_spi_probe()
373 host->num_chipselect = num_cs; in sifive_spi_probe()
374 host->mode_bits = SPI_CPHA | SPI_CPOL in sifive_spi_probe()
379 * we need to "left-align" the bits (unless SPI_LSB_FIRST) in sifive_spi_probe()
381 host->bits_per_word_mask = SPI_BPW_MASK(8); in sifive_spi_probe()
382 host->flags = SPI_CONTROLLER_MUST_TX | SPI_CONTROLLER_GPIO_SS; in sifive_spi_probe()
383 host->prepare_message = sifive_spi_prepare_message; in sifive_spi_probe()
384 host->set_cs = sifive_spi_set_cs; in sifive_spi_probe()
385 host->transfer_one = sifive_spi_transfer_one; in sifive_spi_probe()
387 pdev->dev.dma_mask = NULL; in sifive_spi_probe()
392 ret = devm_request_irq(&pdev->dev, irq, sifive_spi_irq, 0, in sifive_spi_probe()
393 dev_name(&pdev->dev), spi); in sifive_spi_probe()
395 dev_err(&pdev->dev, "Unable to bind to interrupt\n"); in sifive_spi_probe()
399 dev_info(&pdev->dev, "mapped; irq=%d, cs=%d\n", in sifive_spi_probe()
400 irq, host->num_chipselect); in sifive_spi_probe()
402 ret = devm_spi_register_controller(&pdev->dev, host); in sifive_spi_probe()
404 dev_err(&pdev->dev, "spi_register_host failed\n"); in sifive_spi_probe()
411 clk_disable_unprepare(spi->clk); in sifive_spi_probe()
425 clk_disable_unprepare(spi->clk); in sifive_spi_remove()
441 clk_disable_unprepare(spi->clk); in sifive_spi_suspend()
452 ret = clk_prepare_enable(spi->clk); in sifive_spi_resume()
457 clk_disable_unprepare(spi->clk); in sifive_spi_resume()