/* program delay transfers if tx_delay is non zero */ if (spicfg && spicfg->wdelay)
spidat1 |= SPIDAT1_WDEL;
/* * Board specific chip select logic decides the polarity and cs * line for the controller
*/ if (spi_get_csgpiod(spi, 0)) { if (value == BITBANG_CS_ACTIVE)
gpiod_set_value(spi_get_csgpiod(spi, 0), 1); else
gpiod_set_value(spi_get_csgpiod(spi, 0), 0);
} else { if (value == BITBANG_CS_ACTIVE) { if (!(spi->mode & SPI_CS_WORD))
spidat1 |= SPIDAT1_CSHOLD_MASK;
spidat1 &= ~(0x1 << chip_sel);
}
}
iowrite16(spidat1, dspi->base + SPIDAT1 + 2);
}
/** * davinci_spi_get_prescale - Calculates the correct prescale value * @dspi: the controller data * @max_speed_hz: the maximum rate the SPI clock can run at * * This function calculates the prescale value that generates a clock rate * less than or equal to the specified maximum. * * Returns: calculated prescale value for easy programming into SPI registers * or negative error number if valid prescalar cannot be updated.
*/ staticinlineint davinci_spi_get_prescale(struct davinci_spi *dspi,
u32 max_speed_hz)
{ int ret;
/* Subtract 1 to match what will be programmed into SPI register. */
ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz) - 1;
if (ret < dspi->prescaler_limit || ret > 255) return -EINVAL;
return ret;
}
/** * davinci_spi_setup_transfer - This functions will determine transfer method * @spi: spi device on which data transfer to be done * @t: spi transfer in which transfer info is filled * * This function determines data transfer method (8/16/32 bit transfer). * It will also set the SPI Clock Control register according to * SPI slave device freq.
*/ staticint davinci_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
{
if (spi->mode & SPI_LSB_FIRST)
spifmt |= SPIFMT_SHIFTDIR_MASK;
if (spi->mode & SPI_CPOL)
spifmt |= SPIFMT_POLARITY_MASK;
if (!(spi->mode & SPI_CPHA))
spifmt |= SPIFMT_PHASE_MASK;
/* * Assume wdelay is used only on SPI peripherals that has this field * in SPIFMTn register and when it's configured from board file or DT.
*/ if (spicfg->wdelay)
spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
& SPIFMT_WDELAY_MASK);
/* * Version 1 hardware supports two basic SPI modes: * - Standard SPI mode uses 4 pins, with chipselect * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS) * (distinct from SPI_3WIRE, with just one data wire; * or similar variants without MOSI or without MISO) * * Version 2 hardware supports an optional handshaking signal, * so it can support two more modes: * - 5 pin SPI variant is standard SPI plus SPI_READY * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
*/
if (dspi->version == SPI_VERSION_2) {
u32 delay = 0;
if (spicfg->odd_parity)
spifmt |= SPIFMT_ODD_PARITY_MASK;
if (spicfg->parity_enable)
spifmt |= SPIFMT_PARITYENA_MASK;
if (spicfg == NULL && np) {
spicfg = kzalloc(sizeof(*spicfg), GFP_KERNEL); if (!spicfg) return -ENOMEM;
*spicfg = davinci_spi_default_cfg; /* override with dt configured values */ if (!of_property_read_u32(np, "ti,spi-wdelay", &prop))
spicfg->wdelay = (u8)prop;
spi->controller_data = spicfg;
if (dspi->dma_rx && dspi->dma_tx)
spicfg->io_type = SPI_IO_TYPE_DMA;
}
return 0;
}
/** * davinci_spi_setup - This functions will set default transfer method * @spi: spi device on which data transfer to be done * * This functions sets the default transfer method.
*/ staticint davinci_spi_setup(struct spi_device *spi)
{ struct davinci_spi *dspi; struct device_node *np = spi->dev.of_node; bool internal_cs = true;
if (int_status & SPIFLG_TIMEOUT_MASK) {
dev_err(sdev, "SPI Time-out Error\n"); return -ETIMEDOUT;
} if (int_status & SPIFLG_DESYNC_MASK) {
dev_err(sdev, "SPI Desynchronization Error\n"); return -EIO;
} if (int_status & SPIFLG_BITERR_MASK) {
dev_err(sdev, "SPI Bit error\n"); return -EIO;
}
if (dspi->version == SPI_VERSION_2) { if (int_status & SPIFLG_DLEN_ERR_MASK) {
dev_err(sdev, "SPI Data Length Error\n"); return -EIO;
} if (int_status & SPIFLG_PARERR_MASK) {
dev_err(sdev, "SPI Parity Error\n"); return -EIO;
} if (int_status & SPIFLG_OVRRUN_MASK) {
dev_err(sdev, "SPI Data Overrun error\n"); return -EIO;
} if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
dev_err(sdev, "SPI Buffer Init Active\n"); return -EBUSY;
}
}
return 0;
}
/** * davinci_spi_process_events - check for and handle any SPI controller events * @dspi: the controller data * * This function will check the SPIFLG register and handle any events that are * detected there
*/ staticint davinci_spi_process_events(struct davinci_spi *dspi)
{
u32 buf, status, errors = 0, spidat1;
if (!dspi->wcount && !dspi->rcount)
complete(&dspi->done);
}
/** * davinci_spi_bufs - functions which will handle transfer data * @spi: spi device on which data transfer to be done * @t: spi transfer in which transfer info is filled * * This function will put data to be transferred into data register * of SPI controller and then wait until the completion will be marked * by the IRQ Handler.
*/ staticint davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
{ struct davinci_spi *dspi; int data_type, ret = -ENOMEM;
u32 tx_data, spidat1;
u32 errors = 0; struct davinci_spi_config *spicfg; struct davinci_spi_platform_data *pdata; unsignedlong timeout;
if (!t->tx_buf) { /* To avoid errors when doing rx-only transfers with * many SG entries (> 20), use the rx buffer as the * dummy tx buffer so that dma reloads are done at the * same time for rx and tx.
*/
t->tx_sg.sgl = t->rx_sg.sgl;
t->tx_sg.nents = t->rx_sg.nents;
}
/* Wait for the transfer to complete */ if (spicfg->io_type != SPI_IO_TYPE_POLL) {
timeout = DIV_ROUND_UP(t->speed_hz, MSEC_PER_SEC);
timeout = DIV_ROUND_UP(t->len * 8, timeout); /* Assume we are at most 2x slower than the nominal bus speed */
timeout = 2 * msecs_to_jiffies(timeout);
if (wait_for_completion_timeout(&dspi->done, timeout) == 0)
errors = SPIFLG_TIMEOUT_MASK;
} else { while (dspi->rcount > 0 || dspi->wcount > 0) {
errors = davinci_spi_process_events(dspi); if (errors) break;
cpu_relax();
}
}
/* * Check for bit error, desync error,parity error,timeout error and * receive overflow errors
*/ if (errors) {
ret = davinci_spi_check_error(dspi, errors);
WARN(!ret, "%s: error reported but no error found!\n",
dev_name(&spi->dev)); return ret;
}
if (dspi->rcount != 0 || dspi->wcount != 0) {
dev_err(&spi->dev, "SPI data transfer error\n"); return -EIO;
}
return t->len;
err_desc: return ret;
}
/** * dummy_thread_fn - dummy thread function * @irq: IRQ number for this SPI Master * @data: structure for SPI Master controller davinci_spi * * This is to satisfy the request_threaded_irq() API so that the irq * handler is called in interrupt context.
*/ static irqreturn_t dummy_thread_fn(s32 irq, void *data)
{ return IRQ_HANDLED;
}
/** * davinci_spi_irq - Interrupt handler for SPI Master Controller * @irq: IRQ number for this SPI Master * @data: structure for SPI Master controller davinci_spi * * ISR will determine that interrupt arrives either for READ or WRITE command. * According to command it will do the appropriate action. It will check * transfer length and if it is not zero then dispatch transfer command again. * If transfer length is zero then it will indicate the COMPLETION so that * davinci_spi_bufs function can go ahead.
*/ static irqreturn_t davinci_spi_irq(s32 irq, void *data)
{ struct davinci_spi *dspi = data; int status;
status = davinci_spi_process_events(dspi); if (unlikely(status != 0))
clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
if ((!dspi->rcount && !dspi->wcount) || status)
complete(&dspi->done);
/** * spi_davinci_get_pdata - Get platform data from DTS binding * @pdev: ptr to platform data * @dspi: ptr to driver data * * Parses and populates pdata in dspi from device tree bindings. * * NOTE: Not all platform data params are supported currently.
*/ staticint spi_davinci_get_pdata(struct platform_device *pdev, struct davinci_spi *dspi)
{ struct device_node *node = pdev->dev.of_node; conststruct davinci_spi_of_data *spi_data; struct davinci_spi_platform_data *pdata; unsignedint num_cs, intr_line = 0;
pdata = &dspi->pdata;
spi_data = device_get_match_data(&pdev->dev);
pdata->version = spi_data->version;
pdata->prescaler_limit = spi_data->prescaler_limit; /* * default num_cs is 1 and all chipsel are internal to the chip * indicated by chip_sel being NULL or cs_gpios being NULL or * set to -ENOENT. num-cs includes internal as well as gpios. * indicated by chip_sel being NULL. GPIO based CS is not * supported yet in DT bindings.
*/
num_cs = 1;
of_property_read_u32(node, "num-cs", &num_cs);
pdata->num_chipselect = num_cs;
of_property_read_u32(node, "ti,davinci-spi-intr-line", &intr_line);
pdata->intr_line = intr_line; return 0;
} #else staticint spi_davinci_get_pdata(struct platform_device *pdev, struct davinci_spi *dspi)
{ return -ENODEV;
} #endif
/** * davinci_spi_probe - probe function for SPI Master Controller * @pdev: platform_device structure which contains plateform specific data * * According to Linux Device Model this function will be invoked by Linux * with platform_device struct which contains the device specific info. * This function will map the SPI controller's memory, register IRQ, * Reset SPI controller and setting its registers to default value. * It will invoke spi_bitbang_start to create work queue so that client driver * can register transfer method to work queue.
*/ staticint davinci_spi_probe(struct platform_device *pdev)
{ struct spi_controller *host; struct davinci_spi *dspi; struct davinci_spi_platform_data *pdata; struct resource *r; int ret = 0;
u32 spipc0;
host = spi_alloc_host(&pdev->dev, sizeof(struct davinci_spi)); if (host == NULL) {
ret = -ENOMEM; goto err;
}
platform_set_drvdata(pdev, host);
dspi = spi_controller_get_devdata(host);
if (dev_get_platdata(&pdev->dev)) {
pdata = dev_get_platdata(&pdev->dev);
dspi->pdata = *pdata;
} else { /* update dspi pdata with that from the DT */
ret = spi_davinci_get_pdata(pdev, dspi); if (ret < 0) goto free_host;
}
/* pdata in dspi is now updated and point pdata to that */
pdata = &dspi->pdata;
dspi->bytes_per_word = devm_kcalloc(&pdev->dev,
pdata->num_chipselect, sizeof(*dspi->bytes_per_word),
GFP_KERNEL); if (dspi->bytes_per_word == NULL) {
ret = -ENOMEM; goto free_host;
}
dspi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &r); if (IS_ERR(dspi->base)) {
ret = PTR_ERR(dspi->base); goto free_host;
}
dspi->pbase = r->start;
init_completion(&dspi->done);
ret = platform_get_irq(pdev, 0); if (ret < 0) goto free_host;
dspi->irq = ret;
ret = devm_request_threaded_irq(&pdev->dev, dspi->irq, davinci_spi_irq,
dummy_thread_fn, 0, dev_name(&pdev->dev), dspi); if (ret) goto free_host;
dspi->bitbang.ctlr = host;
dspi->clk = devm_clk_get_enabled(&pdev->dev, NULL); if (IS_ERR(dspi->clk)) {
ret = -ENODEV; goto free_host;
}
/* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
iowrite32(spipc0, dspi->base + SPIPC0);
/** * davinci_spi_remove - remove function for SPI Master Controller * @pdev: platform_device structure which contains plateform specific data * * This function will do the reverse action of davinci_spi_probe function * It will free the IRQ and SPI controller's memory region. * It will also call spi_bitbang_stop to destroy the work queue which was * created by spi_bitbang_start.
*/ staticvoid davinci_spi_remove(struct platform_device *pdev)
{ struct davinci_spi *dspi; struct spi_controller *host;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.