/** * struct qspi_platform_data - zynqmp qspi platform data structure * @quirks: Flags is used to identify the platform
*/ struct qspi_platform_data {
u32 quirks;
};
/** * struct zynqmp_qspi - Defines qspi driver instance * @ctlr: Pointer to the spi controller information * @regs: Virtual address of the QSPI controller registers * @refclk: Pointer to the peripheral clock * @pclk: Pointer to the APB clock * @irq: IRQ number * @dev: Pointer to struct device * @txbuf: Pointer to the TX buffer * @rxbuf: Pointer to the RX buffer * @bytes_to_transfer: Number of bytes left to transfer * @bytes_to_receive: Number of bytes left to receive * @genfifocs: Used for chip select * @genfifobus: Used to select the upper or lower bus * @dma_rx_bytes: Remaining bytes to receive by DMA mode * @dma_addr: DMA address after mapping the kernel buffer * @genfifoentry: Used for storing the genfifoentry instruction. * @mode: Defines the mode in which QSPI is operating * @data_completion: completion structure * @op_lock: Operational lock * @speed_hz: Current SPI bus clock speed in hz * @has_tapdelay: Used for tapdelay register available in qspi
*/ struct zynqmp_qspi { struct spi_controller *ctlr; void __iomem *regs; struct clk *refclk; struct clk *pclk; int irq; struct device *dev; constvoid *txbuf; void *rxbuf; int bytes_to_transfer; int bytes_to_receive;
u32 genfifocs;
u32 genfifobus;
u32 dma_rx_bytes;
dma_addr_t dma_addr;
u32 genfifoentry; enum mode_type mode; struct completion data_completion; struct mutex op_lock;
u32 speed_hz; bool has_tapdelay;
};
/** * zynqmp_gqspi_read - For GQSPI controller read operation * @xqspi: Pointer to the zynqmp_qspi structure * @offset: Offset from where to read * Return: Value at the offset
*/ static u32 zynqmp_gqspi_read(struct zynqmp_qspi *xqspi, u32 offset)
{ return readl_relaxed(xqspi->regs + offset);
}
/** * zynqmp_gqspi_write - For GQSPI controller write operation * @xqspi: Pointer to the zynqmp_qspi structure * @offset: Offset where to write * @val: Value to be written
*/ staticinlinevoid zynqmp_gqspi_write(struct zynqmp_qspi *xqspi, u32 offset,
u32 val)
{
writel_relaxed(val, (xqspi->regs + offset));
}
/** * zynqmp_gqspi_selecttarget - For selection of target device * @instanceptr: Pointer to the zynqmp_qspi structure * @targetcs: For chip select * @targetbus: To check which bus is selected- upper or lower
*/ staticvoid zynqmp_gqspi_selecttarget(struct zynqmp_qspi *instanceptr,
u8 targetcs, u8 targetbus)
{ /* * Bus and CS lines selected here will be updated in the instance and * used for subsequent GENFIFO entries during transfer.
*/
/* Choose target select line */ switch (targetcs) { case GQSPI_SELECT_FLASH_CS_BOTH:
instanceptr->genfifocs = GQSPI_GENFIFO_CS_LOWER |
GQSPI_GENFIFO_CS_UPPER; break; case GQSPI_SELECT_FLASH_CS_UPPER:
instanceptr->genfifocs = GQSPI_GENFIFO_CS_UPPER; break; case GQSPI_SELECT_FLASH_CS_LOWER:
instanceptr->genfifocs = GQSPI_GENFIFO_CS_LOWER; break; default:
dev_warn(instanceptr->dev, "Invalid target select\n");
}
/* Choose the bus */ switch (targetbus) { case GQSPI_SELECT_FLASH_BUS_BOTH:
instanceptr->genfifobus = GQSPI_GENFIFO_BUS_LOWER |
GQSPI_GENFIFO_BUS_UPPER; break; case GQSPI_SELECT_FLASH_BUS_UPPER:
instanceptr->genfifobus = GQSPI_GENFIFO_BUS_UPPER; break; case GQSPI_SELECT_FLASH_BUS_LOWER:
instanceptr->genfifobus = GQSPI_GENFIFO_BUS_LOWER; break; default:
dev_warn(instanceptr->dev, "Invalid target bus\n");
}
}
/** * zynqmp_qspi_set_tapdelay: To configure qspi tap delays * @xqspi: Pointer to the zynqmp_qspi structure * @baudrateval: Buadrate to configure
*/ staticvoid zynqmp_qspi_set_tapdelay(struct zynqmp_qspi *xqspi, u32 baudrateval)
{
u32 tapdlybypass = 0, lpbkdlyadj = 0, datadlyadj = 0, clk_rate;
u32 reqhz = 0;
/** * zynqmp_qspi_init_hw - Initialize the hardware * @xqspi: Pointer to the zynqmp_qspi structure * * The default settings of the QSPI controller's configurable parameters on * reset are * - Host mode * - TX threshold set to 1 * - RX threshold set to 1 * - Flash memory interface mode enabled * This function performs the following actions * - Disable and clear all the interrupts * - Enable manual target select * - Enable manual start * - Deselect all the chip select lines * - Set the little endian mode of TX FIFO * - Set clock phase * - Set clock polarity and * - Enable the QSPI controller
*/ staticvoid zynqmp_qspi_init_hw(struct zynqmp_qspi *xqspi)
{
u32 config_reg, baud_rate_val = 0;
ulong clk_rate;
/* Select the GQSPI mode */
zynqmp_gqspi_write(xqspi, GQSPI_SEL_OFST, GQSPI_SEL_MASK); /* Clear and disable interrupts */
zynqmp_gqspi_write(xqspi, GQSPI_ISR_OFST,
zynqmp_gqspi_read(xqspi, GQSPI_ISR_OFST) |
GQSPI_ISR_WR_TO_CLR_MASK); /* Clear the DMA STS */
zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_I_STS_OFST,
zynqmp_gqspi_read(xqspi,
GQSPI_QSPIDMA_DST_I_STS_OFST));
zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_STS_OFST,
zynqmp_gqspi_read(xqspi,
GQSPI_QSPIDMA_DST_STS_OFST) |
GQSPI_QSPIDMA_DST_STS_WTC);
zynqmp_gqspi_write(xqspi, GQSPI_IDR_OFST, GQSPI_IDR_ALL_MASK);
zynqmp_gqspi_write(xqspi,
GQSPI_QSPIDMA_DST_I_DIS_OFST,
GQSPI_QSPIDMA_DST_INTR_ALL_MASK); /* Disable the GQSPI */
zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, 0x0);
config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
config_reg &= ~GQSPI_CFG_MODE_EN_MASK; /* Manual start */
config_reg |= GQSPI_CFG_GEN_FIFO_START_MODE_MASK; /* Little endian by default */
config_reg &= ~GQSPI_CFG_ENDIAN_MASK; /* Disable poll time out */
config_reg &= ~GQSPI_CFG_EN_POLL_TO_MASK; /* Set hold bit */
config_reg |= GQSPI_CFG_WP_HOLD_MASK; /* Clear pre-scalar by default */
config_reg &= ~GQSPI_CFG_BAUD_RATE_DIV_MASK; /* Set CPHA */ if (xqspi->ctlr->mode_bits & SPI_CPHA)
config_reg |= GQSPI_CFG_CLK_PHA_MASK; else
config_reg &= ~GQSPI_CFG_CLK_PHA_MASK; /* Set CPOL */ if (xqspi->ctlr->mode_bits & SPI_CPOL)
config_reg |= GQSPI_CFG_CLK_POL_MASK; else
config_reg &= ~GQSPI_CFG_CLK_POL_MASK;
/* Set the clock frequency */
clk_rate = clk_get_rate(xqspi->refclk); while ((baud_rate_val < GQSPI_BAUD_DIV_MAX) &&
(clk_rate /
(GQSPI_BAUD_DIV_SHIFT << baud_rate_val)) > xqspi->speed_hz)
baud_rate_val++;
/* Enable the GQSPI */
zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, GQSPI_EN_MASK);
}
/** * zynqmp_qspi_copy_read_data - Copy data to RX buffer * @xqspi: Pointer to the zynqmp_qspi structure * @data: The variable where data is stored * @size: Number of bytes to be copied from data to RX buffer
*/ staticvoid zynqmp_qspi_copy_read_data(struct zynqmp_qspi *xqspi,
ulong data, u8 size)
{
memcpy(xqspi->rxbuf, &data, size);
xqspi->rxbuf += size;
xqspi->bytes_to_receive -= size;
}
/** * zynqmp_qspi_chipselect - Select or deselect the chip select line * @qspi: Pointer to the spi_device structure * @is_high: Select(0) or deselect (1) the chip select line
*/ staticvoid zynqmp_qspi_chipselect(struct spi_device *qspi, bool is_high)
{ struct zynqmp_qspi *xqspi = spi_controller_get_devdata(qspi->controller);
ulong timeout;
u32 genfifoentry = 0, statusreg;
/* Wait until the generic FIFO command is empty */ do {
statusreg = zynqmp_gqspi_read(xqspi, GQSPI_ISR_OFST);
if ((statusreg & GQSPI_ISR_GENFIFOEMPTY_MASK) &&
(statusreg & GQSPI_ISR_TXEMPTY_MASK)) break;
cpu_relax();
} while (!time_after_eq(jiffies, timeout));
if (time_after_eq(jiffies, timeout))
dev_err(xqspi->dev, "Chip select timed out\n");
}
/** * zynqmp_qspi_selectspimode - Selects SPI mode - x1 or x2 or x4. * @xqspi: xqspi is a pointer to the GQSPI instance * @spimode: spimode - SPI or DUAL or QUAD. * Return: Mask to set desired SPI mode in GENFIFO entry.
*/ staticinline u32 zynqmp_qspi_selectspimode(struct zynqmp_qspi *xqspi,
u8 spimode)
{
u32 mask = 0;
switch (spimode) { case GQSPI_SELECT_MODE_DUALSPI:
mask = GQSPI_GENFIFO_MODE_DUALSPI; break; case GQSPI_SELECT_MODE_QUADSPI:
mask = GQSPI_GENFIFO_MODE_QUADSPI; break; case GQSPI_SELECT_MODE_SPI:
mask = GQSPI_GENFIFO_MODE_SPI; break; default:
dev_warn(xqspi->dev, "Invalid SPI mode\n");
}
return mask;
}
/** * zynqmp_qspi_config_op - Configure QSPI controller for specified * transfer * @xqspi: Pointer to the zynqmp_qspi structure * @op: The memory operation to execute * * Sets the operational mode of QSPI controller for the next QSPI transfer and * sets the requested clock frequency. * * Return: Always 0 * * Note: * If the requested frequency is not an exact match with what can be * obtained using the pre-scalar value, the driver sets the clock * frequency which is lower than the requested frequency (maximum lower) * for the transfer. * * If the requested frequency is higher or lower than that is supported * by the QSPI controller the driver will set the highest or lowest * frequency supported by controller.
*/ staticint zynqmp_qspi_config_op(struct zynqmp_qspi *xqspi, conststruct spi_mem_op *op)
{
ulong clk_rate;
u32 config_reg, req_speed_hz, baud_rate_val = 0;
req_speed_hz = op->max_freq;
if (xqspi->speed_hz != req_speed_hz) {
xqspi->speed_hz = req_speed_hz;
/* Set the clock frequency */ /* If req_speed_hz == 0, default to lowest speed */
clk_rate = clk_get_rate(xqspi->refclk);
/** * zynqmp_qspi_setup_op - Configure the QSPI controller * @qspi: Pointer to the spi_device structure * * Sets the operational mode of QSPI controller for the next QSPI transfer, * baud rate and divisor value to setup the requested qspi clock. * * Return: 0 on success; error value otherwise.
*/ staticint zynqmp_qspi_setup_op(struct spi_device *qspi)
{ struct spi_controller *ctlr = qspi->controller; struct zynqmp_qspi *xqspi = spi_controller_get_devdata(ctlr);
/** * zynqmp_qspi_filltxfifo - Fills the TX FIFO as long as there is room in * the FIFO or the bytes required to be * transmitted. * @xqspi: Pointer to the zynqmp_qspi structure * @size: Number of bytes to be copied from TX buffer to TX FIFO
*/ staticvoid zynqmp_qspi_filltxfifo(struct zynqmp_qspi *xqspi, int size)
{
u32 count = 0, intermediate;
/** * zynqmp_qspi_readrxfifo - Fills the RX FIFO as long as there is room in * the FIFO. * @xqspi: Pointer to the zynqmp_qspi structure * @size: Number of bytes to be copied from RX buffer to RX FIFO
*/ staticvoid zynqmp_qspi_readrxfifo(struct zynqmp_qspi *xqspi, u32 size)
{
ulong data; int count = 0;
/* Enable the RX interrupts for IO mode */
zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
GQSPI_IER_GENFIFOEMPTY_MASK |
GQSPI_IER_RXNEMPTY_MASK |
GQSPI_IER_RXEMPTY_MASK);
}
}
/** * zynqmp_qspi_irq - Interrupt service routine of the QSPI controller * @irq: IRQ number * @dev_id: Pointer to the xqspi structure * * This function handles TX empty only. * On TX empty interrupt this function reads the received data from RX FIFO * and fills the TX FIFO if there is any data remaining to be transferred. * * Return: IRQ_HANDLED when interrupt is handled * IRQ_NONE otherwise.
*/ static irqreturn_t zynqmp_qspi_irq(int irq, void *dev_id)
{ struct zynqmp_qspi *xqspi = (struct zynqmp_qspi *)dev_id;
u32 status, mask, dma_status = 0;
/** * zynqmp_qspi_setuprxdma - This function sets up the RX DMA operation * @xqspi: xqspi is a pointer to the GQSPI instance. * * Return: 0 on success; error value otherwise.
*/ staticint zynqmp_qspi_setuprxdma(struct zynqmp_qspi *xqspi)
{
u32 rx_bytes, rx_rem;
dma_addr_t addr;
u64 dma_align = (u64)(uintptr_t)xqspi->rxbuf;
/* Write the number of bytes to transfer */
zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_SIZE_OFST, rx_bytes);
return 0;
}
/** * zynqmp_qspi_write_op - This function sets up the GENFIFO entries, * TX FIFO, and fills the TX FIFO with as many * bytes as possible. * @xqspi: Pointer to the GQSPI instance. * @tx_nbits: Transfer buswidth. * @genfifoentry: Variable in which GENFIFO mask is returned * to calling function
*/ staticvoid zynqmp_qspi_write_op(struct zynqmp_qspi *xqspi, u8 tx_nbits,
u32 genfifoentry)
{
zynqmp_qspi_fillgenfifo(xqspi, tx_nbits, genfifoentry);
zynqmp_qspi_filltxfifo(xqspi, GQSPI_TXD_DEPTH); if (xqspi->mode == GQSPI_MODE_DMA)
zynqmp_qspi_disable_dma(xqspi);
}
/** * zynqmp_qspi_read_op - This function sets up the GENFIFO entries and * RX DMA operation. * @xqspi: xqspi is a pointer to the GQSPI instance. * @rx_nbits: Receive buswidth. * @genfifoentry: genfifoentry is pointer to the variable in which * GENFIFO mask is returned to calling function * * Return: 0 on success; error value otherwise.
*/ staticint zynqmp_qspi_read_op(struct zynqmp_qspi *xqspi, u8 rx_nbits,
u32 genfifoentry)
{ int ret;
ret = zynqmp_qspi_setuprxdma(xqspi); if (ret) return ret;
zynqmp_qspi_fillgenfifo(xqspi, rx_nbits, genfifoentry);
return 0;
}
/** * zynqmp_qspi_suspend - Suspend method for the QSPI driver * @dev: Address of the platform_device structure * * This function stops the QSPI driver queue and disables the QSPI controller * * Return: Always 0
*/ staticint __maybe_unused zynqmp_qspi_suspend(struct device *dev)
{ struct zynqmp_qspi *xqspi = dev_get_drvdata(dev); struct spi_controller *ctlr = xqspi->ctlr; int ret;
ret = spi_controller_suspend(ctlr); if (ret) return ret;
zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, 0x0);
return 0;
}
/** * zynqmp_qspi_resume - Resume method for the QSPI driver * @dev: Address of the platform_device structure * * The function starts the QSPI driver queue and initializes the QSPI * controller * * Return: 0 on success; error value otherwise
*/ staticint __maybe_unused zynqmp_qspi_resume(struct device *dev)
{ struct zynqmp_qspi *xqspi = dev_get_drvdata(dev); struct spi_controller *ctlr = xqspi->ctlr;
/** * zynqmp_runtime_resume - Runtime resume method for the SPI driver * @dev: Address of the platform_device structure * * This function enables the clocks * * Return: 0 on success and error value on error
*/ staticint __maybe_unused zynqmp_runtime_resume(struct device *dev)
{ struct zynqmp_qspi *xqspi = dev_get_drvdata(dev); int ret;
ret = clk_prepare_enable(xqspi->pclk); if (ret) {
dev_err(dev, "Cannot enable APB clock.\n"); return ret;
}
ret = clk_prepare_enable(xqspi->refclk); if (ret) {
dev_err(dev, "Cannot enable device clock.\n");
clk_disable_unprepare(xqspi->pclk); return ret;
}
/* Assume we are at most 2x slower than the nominal bus speed */
timeout = mult_frac(bytes, 2 * 8 * MSEC_PER_SEC,
bits * xqspi->speed_hz); /* And add 100 ms for scheduling delays */ return msecs_to_jiffies(timeout + 100);
}
/** * zynqmp_qspi_exec_op() - Initiates the QSPI transfer * @mem: The SPI memory * @op: The memory operation to execute * * Executes a memory operation. * * This function first selects the chip and starts the memory operation. * * Return: 0 in case of success, a negative error code otherwise.
*/ staticint zynqmp_qspi_exec_op(struct spi_mem *mem, conststruct spi_mem_op *op)
{ struct zynqmp_qspi *xqspi =
spi_controller_get_devdata(mem->spi->controller); unsignedlong timeout; int err = 0, i;
u32 genfifoentry = 0;
u16 opcode = op->cmd.opcode;
u64 opaddr;
if (op->dummy.nbytes) {
xqspi->txbuf = NULL;
xqspi->rxbuf = NULL; /* * xqspi->bytes_to_transfer here represents the dummy circles * which need to be sent.
*/
xqspi->bytes_to_transfer = op->dummy.nbytes * 8 / op->dummy.buswidth;
xqspi->bytes_to_receive = 0; /* * Using op->data.buswidth instead of op->dummy.buswidth here because * we need to use it to configure the correct SPI mode.
*/
zynqmp_qspi_write_op(xqspi, op->data.buswidth,
genfifoentry);
zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST) |
GQSPI_CFG_START_GEN_FIFO_MASK);
}
/** * zynqmp_qspi_probe - Probe method for the QSPI driver * @pdev: Pointer to the platform_device structure * * This function initializes the driver data structures and the hardware. * * Return: 0 on success; error value otherwise
*/ staticint zynqmp_qspi_probe(struct platform_device *pdev)
{ int ret = 0; struct spi_controller *ctlr; struct zynqmp_qspi *xqspi; struct device *dev = &pdev->dev; struct device_node *np = dev->of_node;
u32 num_cs; conststruct qspi_platform_data *p_data;
ctlr = devm_spi_alloc_host(&pdev->dev, sizeof(*xqspi)); if (!ctlr) return -ENOMEM;
/** * zynqmp_qspi_remove - Remove method for the QSPI driver * @pdev: Pointer to the platform_device structure * * This function is called if a device is physically removed from the system or * if the driver module is being unloaded. It frees all resources allocated to * the device. * * Return: 0 Always
*/ staticvoid zynqmp_qspi_remove(struct platform_device *pdev)
{ struct zynqmp_qspi *xqspi = platform_get_drvdata(pdev);
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.