QSPI Flash驱动代码分析(等待QSPI空闲)
等待QSPI空闲的含义是,在一个时间间隔内连续N次判断出QSPI处于空闲状态。
1. 函数cqspi_is_idle()
通过QSPI配置寄存器(偏移量0x00)的状态只读bit[31]位,该位为1表示串行接口和QSPI流水管道处于空闲状态。
2. 函数cqspi_wait_idle()
该函数是为了等待QSPI空闲,要求在CQSPI_TIMEOUT_MS(500ms)时间内有连续poll_idle_retry(3)次判断出QSPI空闲;否则就退出超时。
注意:每次判断后需要暂时放弃当前CPU执行,即调用cpu_relax()。
static bool cqspi_is_idle(struct cqspi_st *cqspi)
{
u32 reg = readl(cqspi->iobase + CQSPI_REG_CONFIG);
return reg & (1UL << CQSPI_REG_CONFIG_IDLE_LSB);
}
static int cqspi_wait_idle(struct cqspi_st *cqspi)
{
const unsigned int poll_idle_retry = 3;
unsigned int count = 0;
unsigned long timeout;
timeout = jiffies + msecs_to_jiffies(CQSPI_TIMEOUT_MS);
while (1) {
/*
* Read few times in succession to ensure the controller
* is indeed idle, that is, the bit does not transition
* low again.
*/
if (cqspi_is_idle(cqspi))
count++;
else
count = 0;
if (count >= poll_idle_retry)
return 0;
if (time_after(jiffies, timeout)) {
/* Timeout, in busy mode. */
dev_err(&cqspi->pdev->dev,
"QSPI is still busy after %dms timeout.\n",
CQSPI_TIMEOUT_MS);
return -ETIMEDOUT;
}
cpu_relax();
}
}