/* * The driver only uses one single LUT entry, that is updated on * each call of exec_op(). Index 0 is preset at boot with a basic * read operation, so let's use the last entry (15).
*/ #define SEQID_LUT 15
/* * The PAD definitions for LUT register. * * The pad stands for the number of IO lines [0:3]. * For example, the quad read needs four IO lines, * so you should use LUT_PAD(4).
*/ #define LUT_PAD(x) (fls(x) - 1)
/* * TKT253890, the controller needs the driver to fill the txfifo with * 16 bytes at least to trigger a data transfer, even though the extra * data won't be transferred.
*/ #define QUADSPI_QUIRK_TKT253890 BIT(2)
/* TKT245618, the controller cannot wake up from wait mode */ #define QUADSPI_QUIRK_TKT245618 BIT(3)
/* * Controller adds QSPI_AMBA_BASE (base address of the mapped memory) * internally. No need to add it when setting SFXXAD and SFAR registers
*/ #define QUADSPI_QUIRK_BASE_INTERNAL BIT(4)
/* * Controller uses TDH bits in register QUADSPI_FLSHCR. * They need to be set in accordance with the DDR/SDR mode.
*/ #define QUADSPI_QUIRK_USE_TDH_SETTING BIT(5)
/* * An IC bug makes it necessary to rearrange the 32-bit data. * Later chips, such as IMX6SLX, have fixed this bug.
*/ staticinline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
{ return needs_swap_endian(q) ? __swab32(a) : a;
}
/* * R/W functions for big- or little-endian registers: * The QSPI controller's endianness is independent of * the CPU core's endianness. So far, although the CPU * core is little-endian the QSPI controller can use * big-endian or little-endian.
*/ staticvoid qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr)
{ if (q->devtype_data->little_endian)
iowrite32(val, addr); else
iowrite32be(val, addr);
}
ret = fsl_qspi_check_buswidth(q, op->cmd.buswidth);
if (op->addr.nbytes)
ret |= fsl_qspi_check_buswidth(q, op->addr.buswidth);
if (op->dummy.nbytes)
ret |= fsl_qspi_check_buswidth(q, op->dummy.buswidth);
if (op->data.nbytes)
ret |= fsl_qspi_check_buswidth(q, op->data.buswidth);
if (ret) returnfalse;
/* * The number of instructions needed for the op, needs * to fit into a single LUT entry.
*/ if (op->addr.nbytes +
(op->dummy.nbytes ? 1:0) +
(op->data.nbytes ? 1:0) > 6) returnfalse;
/* Max 64 dummy clock cycles supported */ if (op->dummy.nbytes &&
(op->dummy.nbytes * 8 / op->dummy.buswidth > 64)) returnfalse;
/* Max data length, check controller limits and alignment */ if (op->data.dir == SPI_MEM_DATA_IN &&
(op->data.nbytes > q->devtype_data->ahb_buf_size ||
(op->data.nbytes > q->devtype_data->rxfifo - 4 &&
!IS_ALIGNED(op->data.nbytes, 8)))) returnfalse;
if (op->data.dir == SPI_MEM_DATA_OUT &&
op->data.nbytes > q->devtype_data->txfifo) returnfalse;
/* * For some unknown reason, using LUT_ADDR doesn't work in some * cases (at least with only one byte long addresses), so * let's use LUT_MODE to write the address bytes one by one
*/ for (i = 0; i < op->addr.nbytes; i++) {
u8 addrbyte = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
/* * If we have changed the content of the flash by writing or erasing, or if we * read from flash with a different offset into the page buffer, we need to * invalidate the AHB buffer. If we do not do so, we may read out the wrong * data. The spec tells us reset the AHB domain and Serial Flash domain at * the same time.
*/ staticvoid fsl_qspi_invalidate(struct fsl_qspi *q)
{
u32 reg;
for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) {
memcpy(&val, op->data.buf.out + i, 4);
val = fsl_qspi_endian_xchg(q, val);
qspi_writel(q, val, base + QUADSPI_TBDR);
}
if (i < op->data.nbytes) {
memcpy(&val, op->data.buf.out + i, op->data.nbytes - i);
val = fsl_qspi_endian_xchg(q, val);
qspi_writel(q, val, base + QUADSPI_TBDR);
}
if (needs_fill_txfifo(q)) { for (i = op->data.nbytes; i < 16; i += 4)
qspi_writel(q, 0, base + QUADSPI_TBDR);
}
}
for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) {
val = qspi_readl(q, base + QUADSPI_RBDR(i / 4));
val = fsl_qspi_endian_xchg(q, val);
memcpy(buf + i, &val, 4);
}
if (i < op->data.nbytes) {
val = qspi_readl(q, base + QUADSPI_RBDR(i / 4));
val = fsl_qspi_endian_xchg(q, val);
memcpy(buf + i, &val, op->data.nbytes - i);
}
}
/* * Always start the sequence at the same index since we update * the LUT at each exec_op() call. And also specify the DATA * length, since it's has not been specified in the LUT.
*/
qspi_writel(q, op->data.nbytes | QUADSPI_IPCR_SEQID(SEQID_LUT),
base + QUADSPI_IPCR);
/* Wait for the interrupt. */ if (!wait_for_completion_timeout(&q->c, msecs_to_jiffies(1000)))
err = -ETIMEDOUT;
if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
fsl_qspi_read_rxfifo(q, op);
/* wait for the controller being ready */
fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR, (QUADSPI_SR_IP_ACC_MASK |
QUADSPI_SR_AHB_ACC_MASK), 10, 1000);
fsl_qspi_select_mem(q, mem->spi, op);
if (needs_amba_base_offset(q))
addr_offset = q->memmap_phy;
qspi_writel(q,
q->selected * q->devtype_data->ahb_buf_size + addr_offset,
base + QUADSPI_SFAR);
qspi_writel(q, qspi_readl(q, base + QUADSPI_MCR) |
QUADSPI_MCR_CLR_RXF_MASK | QUADSPI_MCR_CLR_TXF_MASK,
base + QUADSPI_MCR);
qspi_writel(q, QUADSPI_SPTRCLR_BFPTRC | QUADSPI_SPTRCLR_IPPTRC,
base + QUADSPI_SPTRCLR);
qspi_writel(q, invalid_mstrid, base + QUADSPI_BUF0CR);
qspi_writel(q, invalid_mstrid, base + QUADSPI_BUF1CR);
qspi_writel(q, invalid_mstrid, base + QUADSPI_BUF2CR);
fsl_qspi_prepare_lut(q, op);
/* * If we have large chunks of data, we read them through the AHB bus * by accessing the mapped memory. In all other cases we use * IP commands to access the flash.
*/ if (op->data.nbytes > (q->devtype_data->rxfifo - 4) &&
op->data.dir == SPI_MEM_DATA_IN) {
fsl_qspi_read_ahb(q, op);
} else {
qspi_writel(q, QUADSPI_RBCT_WMRK_MASK |
QUADSPI_RBCT_RXBRD_USEIPS, base + QUADSPI_RBCT);
if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
fsl_qspi_fill_txfifo(q, op);
err = fsl_qspi_do_op(q, op);
}
/* Invalidate the data in the AHB buffer. */
fsl_qspi_invalidate(q);
/* disable and unprepare clock to avoid glitch pass to controller */
fsl_qspi_clk_disable_unprep(q);
/* the default frequency, we will change it later if necessary. */
ret = clk_set_rate(q->clk, 66000000); if (ret) return ret;
ret = fsl_qspi_clk_prep_enable(q); if (ret) return ret;
/* Reset the module */
qspi_writel(q, QUADSPI_MCR_SWRSTSD_MASK | QUADSPI_MCR_SWRSTHD_MASK,
base + QUADSPI_MCR);
udelay(1);
/* Disable the module */
qspi_writel(q, QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK,
base + QUADSPI_MCR);
/* * Previous boot stages (BootROM, bootloader) might have used DDR * mode and did not clear the TDH bits. As we currently use SDR mode * only, clear the TDH bits if necessary.
*/ if (needs_tdh_setting(q))
qspi_writel(q, qspi_readl(q, base + QUADSPI_FLSHCR) &
~QUADSPI_FLSHCR_TDH_MASK,
base + QUADSPI_FLSHCR);
reg = qspi_readl(q, base + QUADSPI_SMPR);
qspi_writel(q, reg & ~(QUADSPI_SMPR_FSDLY_MASK
| QUADSPI_SMPR_FSPHS_MASK
| QUADSPI_SMPR_HSENA_MASK
| QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR);
/* We only use the buffer3 for AHB read */
qspi_writel(q, 0, base + QUADSPI_BUF0IND);
qspi_writel(q, 0, base + QUADSPI_BUF1IND);
qspi_writel(q, 0, base + QUADSPI_BUF2IND);
qspi_writel(q, QUADSPI_BFGENCR_SEQID(SEQID_LUT),
q->iobase + QUADSPI_BFGENCR);
qspi_writel(q, QUADSPI_RBCT_WMRK_MASK, base + QUADSPI_RBCT);
qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK |
QUADSPI_BUF3CR_ADATSZ(q->devtype_data->ahb_buf_size / 8),
base + QUADSPI_BUF3CR);
if (needs_amba_base_offset(q))
addr_offset = q->memmap_phy;
/* * In HW there can be a maximum of four chips on two buses with * two chip selects on each bus. We use four chip selects in SW * to differentiate between the four chips. * We use ahb_buf_size for each chip and set SFA1AD, SFA2AD, SFB1AD, * SFB2AD accordingly.
*/
qspi_writel(q, q->devtype_data->ahb_buf_size + addr_offset,
base + QUADSPI_SFA1AD);
qspi_writel(q, q->devtype_data->ahb_buf_size * 2 + addr_offset,
base + QUADSPI_SFA2AD);
qspi_writel(q, q->devtype_data->ahb_buf_size * 3 + addr_offset,
base + QUADSPI_SFB1AD);
qspi_writel(q, q->devtype_data->ahb_buf_size * 4 + addr_offset,
base + QUADSPI_SFB2AD);
q->selected = -1;
/* Enable the module */
qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
base + QUADSPI_MCR);
/* clear all interrupt status */
qspi_writel(q, 0xffffffff, q->iobase + QUADSPI_FR);
/* enable the interrupt */
qspi_writel(q, QUADSPI_RSER_TFIE, q->iobase + QUADSPI_RSER);
/* * In order to keep mtdparts compatible with the old MTD driver at * mtd/spi-nor/fsl-quadspi.c, we set a custom name derived from the * platform_device of the controller.
*/ if (of_get_available_child_count(q->dev->of_node) == 1) return dev_name(q->dev);
name = devm_kasprintf(dev, GFP_KERNEL, "%s-%d", dev_name(q->dev),
spi_get_chipselect(mem->spi, 0));
if (!name) {
dev_err(dev, "failed to get memory for custom flash name\n"); return ERR_PTR(-ENOMEM);
}
/* find the resources */
q->iobase = devm_platform_ioremap_resource_byname(pdev, "QuadSPI"); if (IS_ERR(q->iobase)) return PTR_ERR(q->iobase);
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "QuadSPI-memory"); if (!res) return -EINVAL;
q->memmap_phy = res->start; /* Since there are 4 cs, map size required is 4 times ahb_buf_size */
q->ahb_addr = devm_ioremap(dev, q->memmap_phy,
(q->devtype_data->ahb_buf_size * 4)); if (!q->ahb_addr) return -ENOMEM;
/* find the clocks */
q->clk_en = devm_clk_get(dev, "qspi_en"); if (IS_ERR(q->clk_en)) return PTR_ERR(q->clk_en);
q->clk = devm_clk_get(dev, "qspi"); if (IS_ERR(q->clk)) return PTR_ERR(q->clk);
mutex_init(&q->lock);
ret = fsl_qspi_clk_prep_enable(q); if (ret) {
dev_err(dev, "can not enable the clock\n"); return ret;
}
ret = devm_add_action_or_reset(dev, fsl_qspi_cleanup, q); if (ret) return ret;
/* find the irq */
ret = platform_get_irq(pdev, 0); if (ret < 0) return ret;
ret = devm_request_irq(dev, ret,
fsl_qspi_irq_handler, 0, pdev->name, q); if (ret) {
dev_err(dev, "failed to request irq: %d\n", ret); return ret;
}
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.