Re: [PATCH v2 1/2] spi: Add Renesas R-Car Gen3 RPC SPI controller driver

From: Marek Vasut
Date: Wed Dec 05 2018 - 07:58:01 EST


On 12/05/2018 08:44 AM, masonccyang@xxxxxxxxxxx wrote:
> Hi Marek,

Hi,

> thanks for your review.
>
>> "Marek Vasut" <marek.vasut@xxxxxxxxx>
>> 2018/12/05 äå 10:04
>>
>> To
>>
>> "Mason Yang" <masonccyang@xxxxxxxxxxx>, broonie@xxxxxxxxxx, linux-
>> kernel@xxxxxxxxxxxxxxx, linux-spi@xxxxxxxxxxxxxxx,
>> boris.brezillon@xxxxxxxxxxx, linux-renesas-soc@xxxxxxxxxxxxxxx,
>> "Geert Uytterhoeven" <geert+renesas@xxxxxxxxx>,
>>
>> cc
>>
>> juliensu@xxxxxxxxxxx, "Simon Horman" <horms@xxxxxxxxxxxx>,
>> zhengxunli@xxxxxxxxxxx
>>
>> Subject
>>
>> Re: [PATCH v2 1/2] spi: Add Renesas R-Car Gen3 RPC SPI controller driver
>>
>> On 12/03/2018 10:18 AM, Mason Yang wrote:
>> > Add a driver for Renesas R-Car Gen3 RPC SPI controller.
>> >
>> > Signed-off-by: Mason Yang <masonccyang@xxxxxxxxxxx>
>>
>> What changed in this V2 ?
>>
>> [...]
>
> see some description in [PATH v2 0/2]

I don't see any V2: list there.

>> > +struct rpc_spi {
>> > + Â struct clk *clk_rpc;
>> > + Â void __iomem *base;
>> > + Â struct {
>> > + Â Â Âvoid __iomem *map;
>> > + Â Â Âdma_addr_t dma;
>> > + Â Â Âsize_t size;
>> > + Â } linear;
>>
>> This is still here, see my review comments on the previous version.
>>
>> > + Â struct regmap *regmap;
>> > + Â u32 cur_speed_hz;
>> > + Â u32 cmd;
>> > + Â u32 addr;
>> > + Â u32 dummy;
>> > + Â u32 smcr;
>> > + Â u32 smenr;
>> > + Â u32 xferlen;
>> > + Â u32 totalxferlen;
>> > + Â enum spi_mem_data_dir xfer_dir;
>> > +#ifdef CONFIG_RESET_CONTROLLER
>> > + Â struct reset_control *rstc;
>> > +#endif
>> > +};
>> > +
>> > +static int rpc_spi_set_freq(struct rpc_spi *rpc, unsigned long freq)
>> > +{
>> > + Â int ret;
>> > +
>> > + Â if (rpc->cur_speed_hz == freq)
>> > + Â Â Âreturn 0;
>> > +
>> > + Â ret = clk_set_rate(rpc->clk_rpc, freq);
>> > + Â if (ret)
>> > + Â Â Âreturn ret;
>> > +
>> > + Â rpc->cur_speed_hz = freq;
>> > + Â return ret;
>> > +}
>> > +
>> > +static void rpc_spi_hw_init(struct rpc_spi *rpc)
>> > +{
>> > + Â /*
>> > + Â Â* NOTE: The 0x260 are undocumented bits, but they must be set.
>> > + Â Â* Â RPC_PHYCNT_STRTIM is strobe timing adjustment bit,
>> > + Â Â* Â 0x0 : the delay is biggest,
>> > + Â Â* Â 0x1 : the delay is 2nd biggest,
>> > + Â Â* Â 0x3 or 0x6 is a recommended value.
>> > + Â Â*/
>>
>> Doesn't this vary by SoC ? I think H3 ES1.0 had different value here,
>> but I might be wrong.
>
> I check the Renesas bare-metal code, mini_monitor v4.01.
> It set 0x03 or 0x0 and I test them w/ 0x0, 0x3 and 0x6 are all OK.

Shouldn't this somehow use the soc_device_match() then and configure it
accordingly for various chips ? Like eg. the r8a7795-cpg-mssr driver does.

>> > + Â regmap_write(rpc->regmap, RPC_PHYCNT, RPC_PHYCNT_CAL |
>> > + Â Â Â Â Â Â ÂRPC_PHYCNT_STRTIM(0x6) | 0x260);
>> > +
>> > + Â /*
>> > + Â Â* NOTE: The 0x31511144 and 0x431 are undocumented bits,
>> > + Â Â* Â Âbut they must be set for RPC_PHYOFFSET1 & RPC_PHYOFFSET2.
>> > + Â Â*/
>> > + Â regmap_write(rpc->regmap, RPC_PHYOFFSET1, 0x31511144);
>> > + Â regmap_write(rpc->regmap, RPC_PHYOFFSET2, 0x431);
>> > +
>> > + Â regmap_write(rpc->regmap, RPC_SSLDR, RPC_SSLDR_SPNDL(7) |
>> > + Â Â Â Â Â Â ÂRPC_SSLDR_SLNDL(7) | RPC_SSLDR_SCKDL(7));
>> > +}
>> > +
>> > +#ifdef CONFIG_RESET_CONTROLLER
>>
>> Just make the driver depend on reset controller.
>
> ?
> please refer to
> https://github.com/torvalds/linux/blob/master/drivers/clk/renesas/renesas-cpg-mssr.c
>
> line 124 ~ 126

This seems like a stopgap measure for systems which do not have a reset
api compatible controller. Geert ?

>> > +static int rpc_spi_do_reset(struct rpc_spi *rpc)
>> > +{
>> > + Â int i, ret;
>> > +
>> > + Â ret = reset_control_reset(rpc->rstc);
>> > + Â if (ret)
>> > + Â Â Âreturn ret;
>> > +
>> > + Â for (i = 0; i < LOOP_TIMEOUT; i++) {
>> > + Â Â Âret = reset_control_status(rpc->rstc);
>> > + Â Â Âif (ret == 0)
>> > + Â Â Â Â return 0;
>> > + Â Â Âusleep_range(0, 1);
>> > + Â }
>> > +
>> > + Â return -ETIMEDOUT;
>> > +}
>> > +#else
>> > +static int rpc_spi_do_reset(struct rpc_spi *rpc)
>> > +{
>> > + Â return -ETIMEDOUT;
>> > +}
>> > +#endif
>> > +
>> > +static int wait_msg_xfer_end(struct rpc_spi *rpc)
>> > +{
>> > + Â u32 sts;
>> > +
>> > + Â return regmap_read_poll_timeout(rpc->regmap, RPC_CMNSR, sts,
>> > + Â Â Â Â Â Â Â sts & RPC_CMNSR_TEND, 0, USEC_PER_SEC);
>> > +}
>> > +
>> > +static u8 rpc_bits_xfer(u32 nbytes)
>> > +{
>> > + Â if (nbytes > 4)
>> > + Â Â Ânbytes = 4;
>>
>> Use clamp() ?
>>
> Â
> nbytes = clamp(nbytes, 1, 4);
>
> got many warnings, something like,
> ./include/linux/kernel.h:845:29: warning: comparison of distinct pointer
> types lacks a cast

Geert already replied.

>> > +
>> > + Â return GENMASK(3, 4 - nbytes);
>>
>> Nice ;-)
>>
>> > +}
>> > +
>> > +static int rpc_spi_io_xfer(struct rpc_spi *rpc,
>> > + Â Â Â Â Â Âconst void *tx_buf, void *rx_buf)
>> > +{
>> > + Â u32 smenr, smcr, data, pos = 0;
>> > + Â int ret = 0;
>> > +
>> > + Â regmap_write(rpc->regmap, RPC_CMNCR, RPC_CMNCR_MD | RPC_CMNCR_SFDE |
>> > + Â Â Â Â Â Â ÂRPC_CMNCR_MOIIO_HIZ | RPC_CMNCR_IOFV_HIZ |
>> > + Â Â Â Â Â Â ÂRPC_CMNCR_BSZ(0));
>> > + Â regmap_write(rpc->regmap, RPC_SMDRENR, 0x0);
>> > + Â regmap_write(rpc->regmap, RPC_SMCMR, rpc->cmd);
>> > + Â regmap_write(rpc->regmap, RPC_SMDMCR, rpc->dummy);
>> > + Â regmap_write(rpc->regmap, RPC_SMADR, rpc->addr);
>> > +
>> > + Â if (tx_buf) {
>> > + Â Â Âsmenr = rpc->smenr;
>> > +
>> > + Â Â Âwhile (pos < rpc->xferlen) {
>> > + Â Â Â Â u32 nbytes = rpc->xferlen Â- pos;
>> > +
>> > + Â Â Â Â regmap_write(rpc->regmap, RPC_SMWDR0,
>> > + Â Â Â Â Â Â Â Â *(u32 *)(tx_buf + pos));
>>
>> *(u32 *) cast is probably not needed , fix casts globally.
>
> It must have it!

Why ?

>> > + Â Â Â Â if (nbytes > 4) {
>> > + Â Â Â Â Â Ânbytes = 4;
>> > + Â Â Â Â Â Âsmcr = rpc->smcr |
>> > + Â Â Â Â Â Â Â Â Â RPC_SMCR_SPIE | RPC_SMCR_SSLKP;
>> > + Â Â Â Â } else {
>> > + Â Â Â Â Â Âsmcr = rpc->smcr | RPC_SMCR_SPIE;
>> > + Â Â Â Â }
>> > +
>> > + Â Â Â Â regmap_write(rpc->regmap, RPC_SMENR, smenr);
>> > + Â Â Â Â regmap_write(rpc->regmap, RPC_SMCR, smcr);
>> > + Â Â Â Â ret = wait_msg_xfer_end(rpc);
>> > + Â Â Â Â if (ret)
>> > + Â Â Â Â Â Âgoto out;
>> > +
>> > + Â Â Â Â pos += nbytes;
>> > + Â Â Â Â smenr = rpc->smenr & ~RPC_SMENR_CDE &
>> > + Â Â Â Â Â Â Â Â Â Â~RPC_SMENR_ADE(0xf);
>> > + Â Â Â}
>> > + Â } else if (rx_buf) {
>> > + Â Â Âwhile (pos < rpc->xferlen) {
>> > + Â Â Â Â u32 nbytes = rpc->xferlen Â- pos;
>> > +
>> > + Â Â Â Â if (nbytes > 4)
>> > + Â Â Â Â Â Ânbytes = 4;
>> > +
>> > + Â Â Â Â regmap_write(rpc->regmap, RPC_SMENR, rpc->smenr);
>> > + Â Â Â Â regmap_write(rpc->regmap, RPC_SMCR,
>> > + Â Â Â Â Â Â Â Â rpc->smcr | RPC_SMCR_SPIE);
>> > + Â Â Â Â ret = wait_msg_xfer_end(rpc);
>> > + Â Â Â Â if (ret)
>> > + Â Â Â Â Â Âgoto out;
>> > +
>> > + Â Â Â Â regmap_read(rpc->regmap, RPC_SMRDR0, &data);
>> > + Â Â Â Â memcpy_fromio(rx_buf + pos, (void *)&data, nbytes);
>> > + Â Â Â Â pos += nbytes;
>> > + Â Â Â Â regmap_write(rpc->regmap, RPC_SMCMR, rpc->cmd);
>> > + Â Â Â Â regmap_write(rpc->regmap, RPC_SMDMCR, rpc->dummy);
>> > + Â Â Â Â regmap_write(rpc->regmap, RPC_SMADR, rpc->addr + pos);
>> > + Â Â Â}
>> > + Â } else {
>> > + Â Â Âregmap_write(rpc->regmap, RPC_SMENR, rpc->smenr);
>> > + Â Â Âregmap_write(rpc->regmap, RPC_SMCR, rpc->smcr | RPC_SMCR_SPIE);
>> > + Â Â Âret = wait_msg_xfer_end(rpc);
>> > + Â Â Âif (ret)
>> > + Â Â Â Â goto out;
>> > + Â }
>> > +
>> > + Â return ret;
>> > +out:
>> > + Â return rpc_spi_do_reset(rpc);
>> > +}
>> > +
>> > +static void rpc_spi_mem_set_prep_op_cfg(struct spi_device *spi,
>> > + Â Â Â Â Â Â Â const struct spi_mem_op *op,
>> > + Â Â Â Â Â Â Â u64 *offs, size_t *len)
>> > +{
>> > + Â struct rpc_spi *rpc = spi_master_get_devdata(spi->master);
>> > +
>> > + Â rpc->cmd = RPC_SMCMR_CMD(op->cmd.opcode);
>> > + Â rpc->smenr = RPC_SMENR_CDE |
>> > + Â Â Â Â Â RPC_SMENR_CDB(fls(op->cmd.buswidth >> 1));
>> > + Â rpc->totalxferlen = 1;
>> > + Â rpc->xfer_dir = SPI_MEM_NO_DATA;
>> > + Â rpc->xferlen = 0;
>> > + Â rpc->addr = 0;
>> > +
>> > + Â if (op->addr.nbytes) {
>> > + Â Â Ârpc->smenr |= RPC_SMENR_ADB(fls(op->addr.buswidth >> 1));
>> > + Â Â Âif (op->addr.nbytes == 4)
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_ADE(0xf);
>> > + Â Â Âelse
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_ADE(0x7);
>> > +
>> > + Â Â Âif (offs && len)
>> > + Â Â Â Â rpc->addr = *(u32 *)offs;
>> > + Â Â Âelse
>> > + Â Â Â Â rpc->addr = op->addr.val;
>> > + Â Â Ârpc->totalxferlen += op->addr.nbytes;
>> > + Â }
>> > +
>> > + Â if (op->dummy.nbytes) {
>> > + Â Â Ârpc->smenr |= RPC_SMENR_DME;
>> > + Â Â Ârpc->dummy = RPC_SMDMCR_DMCYC(op->dummy.nbytes);
>> > + Â Â Ârpc->totalxferlen += op->dummy.nbytes;
>> > + Â }
>> > +
>> > + Â if (op->data.nbytes || (offs && len)) {
>> > + Â Â Âif (op->data.dir == SPI_MEM_DATA_IN) {
>> > + Â Â Â Â rpc->smcr = RPC_SMCR_SPIRE;
>> > + Â Â Â Â rpc->xfer_dir = SPI_MEM_DATA_IN;
>> > + Â Â Â} else if (op->data.dir == SPI_MEM_DATA_OUT) {
>> > + Â Â Â Â rpc->smcr = RPC_SMCR_SPIWE;
>> > + Â Â Â Â rpc->xfer_dir = SPI_MEM_DATA_OUT;
>> > + Â Â Â}
>> > +
>> > + Â Â Âif (offs && len) {
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_SPIDE(rpc_bits_xfer
>> > + Â Â Â Â Â Â Â (*(u32 *)len)) | RPC_SMENR_SPIDB
>> > + Â Â Â Â Â Â Â (fls(op->data.buswidth >> 1));
>>
>> Fix the *(u32 *)
>>
>> > + Â Â Â Â rpc->xferlen = *(u32 *)len;
>> > + Â Â Â Â rpc->totalxferlen += *(u32 *)len;
>> > + Â Â Â} else {
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_SPIDE(rpc_bits_xfer
>> > + Â Â Â Â Â Â Â (op->data.nbytes)) | RPC_SMENR_SPIDB
>> > + Â Â Â Â Â Â Â (fls(op->data.buswidth >> 1));
>>
>> Drop parenthesis around fls()
>
> ?
> no way.

I would really appreciate it if you could explain things instead.

Geert already did so, by pointing out this is a confusing code
formatting problem and how it should be fixed, so no need to repeat
that. But I hope you understand how that sort of explanation is far more
valuable than "no way" kind of reply.

>> > +
>> > + Â Â Â Â rpc->xferlen = op->data.nbytes;
>> > + Â Â Â Â rpc->totalxferlen += op->data.nbytes;
>> > + Â Â Â}
>> > + Â }
>> > +}
>> > +
>> > +static bool rpc_spi_mem_supports_op(struct spi_mem *mem,
>> > + Â Â Â Â Â Â Â Âconst struct spi_mem_op *op)
>> > +{
>> > + Â if (op->data.buswidth > 4 || op->addr.buswidth > 4 ||
>> > + Â Â Â op->dummy.buswidth > 4 || op->cmd.buswidth > 4)
>> > + Â Â Âreturn false;
>> > +
>> > + Â if (op->addr.nbytes > 4)
>>
>> Merge this into previous conditional statement.
>>
>> > + Â Â Âreturn false;
>> > +
>> > + Â return true;
>> > +}
>> > +
>> > +static ssize_t rpc_spi_mem_dirmap_read(struct spi_mem_dirmap_desc
> *desc,
>> > + Â Â Â Â Â Â Â Â Â u64 offs, size_t len, void *buf)
>> > +{
>> > + Â struct rpc_spi *rpc =
> spi_master_get_devdata(desc->mem->spi->master);
>> > + Â int ret;
>> > +
>> > + Â if (WARN_ON(offs + desc->info.offset + len > U32_MAX))
>> > + Â Â Âreturn -EINVAL;
>> > +
>> > + Â ret = rpc_spi_set_freq(rpc, desc->mem->spi->max_speed_hz);
>> > + Â if (ret)
>> > + Â Â Âreturn ret;
>> > +
>> > + Â rpc_spi_mem_set_prep_op_cfg(desc->mem->spi,
>> > + Â Â Â Â Â Â Â Â&desc->info.op_tmpl, &offs, &len);
>> > +
>> > + Â regmap_write(rpc->regmap, RPC_CMNCR, RPC_CMNCR_SFDE |
>> > + Â Â Â Â Â RPC_CMNCR_MOIIO_HIZ | RPC_CMNCR_IOFV_HIZ |
>> > + Â Â Â Â Â RPC_CMNCR_BSZ(0));
>> > + Â regmap_write(rpc->regmap, RPC_DRCR, RPC_DRCR_RBURST(0x1f) |
>> > + Â Â Â Â Â RPC_DRCR_RBE);
>> > + Â regmap_write(rpc->regmap, RPC_DRCMR, rpc->cmd);
>> > + Â regmap_write(rpc->regmap, RPC_DREAR, RPC_DREAR_EAC);
>> > + Â regmap_write(rpc->regmap, RPC_DROPR, 0);
>> > + Â regmap_write(rpc->regmap, RPC_DRENR, rpc->smenr);
>> > + Â regmap_write(rpc->regmap, RPC_DRDMCR, rpc->dummy);
>> > + Â regmap_write(rpc->regmap, RPC_DRDRENR, 0);
>> > +
>> > + Â memcpy_fromio(buf, rpc->linear.map + desc->info.offset + offs, len);
>> > +
>> > + Â return len;
>> > +}
>> > +
>> > +static ssize_t rpc_spi_mem_dirmap_write(struct spi_mem_dirmap_desc
> *desc,
>> > + Â Â Â Â Â Â Â u64 offs, size_t len, const void *buf)
>> > +{
>> > + Â struct rpc_spi *rpc =
> spi_master_get_devdata(desc->mem->spi->master);
>> > + Â int ret;
>> > +
>> > + Â if (WARN_ON(offs + desc->info.offset + len > U32_MAX))
>> > + Â Â Âreturn -EINVAL;
>> > +
>> > + Â if (WARN_ON(len > RPC_WBUF_SIZE))
>> > + Â Â Âreturn -EIO;
>> > +
>> > + Â ret = rpc_spi_set_freq(rpc, desc->mem->spi->max_speed_hz);
>> > + Â if (ret)
>> > + Â Â Âreturn ret;
>> > +
>> > + Â rpc_spi_mem_set_prep_op_cfg(desc->mem->spi,
>> > + Â Â Â Â Â Â Â Â&desc->info.op_tmpl, &offs, &len);
>> > +
>> > + Â regmap_write(rpc->regmap, RPC_CMNCR, RPC_CMNCR_MD | RPC_CMNCR_SFDE |
>> > + Â Â Â Â Â Â ÂRPC_CMNCR_MOIIO_HIZ | RPC_CMNCR_IOFV_HIZ |
>> > + Â Â Â Â Â Â ÂRPC_CMNCR_BSZ(0));
>> > + Â regmap_write(rpc->regmap, RPC_SMDRENR, 0);
>> > + Â regmap_write(rpc->regmap, RPC_PHYCNT, RPC_PHYCNT_CAL | 0x260 |
>> > + Â Â Â Â Â Â ÂRPC_PHYCNT_WBUF2 | RPC_PHYCNT_WBUF);
>> > +
>> > + Â memcpy_toio(rpc->base + RPC_WBUF, buf, len);
>> > +
>> > + Â regmap_write(rpc->regmap, RPC_SMCMR, rpc->cmd);
>> > + Â regmap_write(rpc->regmap, RPC_SMADR, offs);
>> > + Â regmap_write(rpc->regmap, RPC_SMENR, rpc->smenr);
>> > + Â regmap_write(rpc->regmap, RPC_SMCR, rpc->smcr | RPC_SMCR_SPIE);
>> > + Â ret = wait_msg_xfer_end(rpc);
>> > + Â if (ret)
>> > + Â Â Âgoto out;
>> > +
>> > + Â regmap_write(rpc->regmap, RPC_DRCR, RPC_DRCR_RCF);
>> > + Â regmap_write(rpc->regmap, RPC_PHYCNT, RPC_PHYCNT_CAL |
>> > + Â Â Â Â Â Â ÂRPC_PHYCNT_STRTIM(6) | 0x260);
>> > +
>> > + Â return len;
>> > +out:
>> > + Â return rpc_spi_do_reset(rpc);
>> > +}
>> > +
>> > +static int rpc_spi_mem_dirmap_create(struct spi_mem_dirmap_desc *desc)
>> > +{
>> > + Â struct rpc_spi *rpc =
> spi_master_get_devdata(desc->mem->spi->master);
>> > +
>> > + Â if (desc->info.offset + desc->info.length > U32_MAX)
>> > + Â Â Âreturn -ENOTSUPP;
>> > +
>> > + Â if (!rpc_spi_mem_supports_op(desc->mem, &desc->info.op_tmpl))
>> > + Â Â Âreturn -ENOTSUPP;
>> > +
>> > + Â if (!rpc->linear.map &&
>> > + Â Â Â desc->info.op_tmpl.data.dir == SPI_MEM_DATA_IN)
>> > + Â Â Âreturn -ENOTSUPP;
>> > +
>> > + Â return 0;
>> > +}
>> > +
>> > +static int rpc_spi_mem_exec_op(struct spi_mem *mem,
>> > + Â Â Â Â Â Â Â Âconst struct spi_mem_op *op)
>> > +{
>> > + Â struct rpc_spi *rpc = spi_master_get_devdata(mem->spi->master);
>> > + Â int ret;
>> > +
>> > + Â ret = rpc_spi_set_freq(rpc, mem->spi->max_speed_hz);
>> > + Â if (ret)
>> > + Â Â Âreturn ret;
>> > +
>> > + Â rpc_spi_mem_set_prep_op_cfg(mem->spi, op, NULL, NULL);
>> > +
>> > + Â ret = rpc_spi_io_xfer(rpc,
>> > + Â Â Â Â Â Â Â op->data.dir == SPI_MEM_DATA_OUT ?
>> > + Â Â Â Â Â Â Â op->data.buf.out : NULL,
>> > + Â Â Â Â Â Â Â op->data.dir == SPI_MEM_DATA_IN ?
>> > + Â Â Â Â Â Â Â op->data.buf.in : NULL);
>> > +
>> > + Â return ret;
>> > +}
>> > +
>> > +static const struct spi_controller_mem_ops rpc_spi_mem_ops = {
>> > + Â .supports_op = rpc_spi_mem_supports_op,
>> > + Â .exec_op = rpc_spi_mem_exec_op,
>> > + Â .dirmap_create = rpc_spi_mem_dirmap_create,
>> > + Â .dirmap_read = rpc_spi_mem_dirmap_read,
>> > + Â .dirmap_write = rpc_spi_mem_dirmap_write,
>> > +};
>> > +
>> > +static void rpc_spi_transfer_setup(struct rpc_spi *rpc,
>> > + Â Â Â Â Â Â Â struct spi_message *msg)
>> > +{
>> > + Â struct spi_transfer *t, xfer[4] = { };
>> > + Â u32 i, xfercnt, xferpos = 0;
>> > +
>> > + Â rpc->totalxferlen = 0;
>> > + Â rpc->xfer_dir = SPI_MEM_NO_DATA;
>> > +
>> > + Â list_for_each_entry(t, &msg->transfers, transfer_list) {
>> > + Â Â Âif (t->tx_buf) {
>> > + Â Â Â Â xfer[xferpos].tx_buf = t->tx_buf;
>> > + Â Â Â Â xfer[xferpos].tx_nbits = t->tx_nbits;
>> > + Â Â Â}
>> > +
>> > + Â Â Âif (t->rx_buf) {
>> > + Â Â Â Â xfer[xferpos].rx_buf = t->rx_buf;
>> > + Â Â Â Â xfer[xferpos].rx_nbits = t->rx_nbits;
>> > + Â Â Â}
>> > +
>> > + Â Â Âif (t->len) {
>> > + Â Â Â Â xfer[xferpos++].len = t->len;
>> > + Â Â Â Â rpc->totalxferlen += t->len;
>> > + Â Â Â}
>> > +
>> > + Â Â Âif (list_is_last(&t->transfer_list, &msg->transfers)) {
>> > + Â Â Â Â if (xferpos > 1 && t->rx_buf) {
>> > + Â Â Â Â Â Ârpc->xfer_dir = SPI_MEM_DATA_IN;
>> > + Â Â Â Â Â Ârpc->smcr = RPC_SMCR_SPIRE;
>> > + Â Â Â Â } else if (xferpos > 1 && t->tx_buf) {
>> > + Â Â Â Â Â Ârpc->xfer_dir = SPI_MEM_DATA_OUT;
>> > + Â Â Â Â Â Ârpc->smcr = RPC_SMCR_SPIWE;
>> > + Â Â Â Â }
>> > + Â Â Â}
>> > + Â }
>> > +
>> > + Â xfercnt = xferpos;
>> > + Â rpc->xferlen = xfer[--xferpos].len;
>> > + Â rpc->cmd = RPC_SMCMR_CMD(((u8 *)xfer[0].tx_buf)[0]);
>>
>> Is the cast needed ?
>
> yes!

Why ?

>> > + Â rpc->smenr = RPC_SMENR_CDE | RPC_SMENR_CDB(fls(xfer[0].tx_nbits
>>> 1));
>> > + Â rpc->addr = 0;
>> > +
>> > + Â if (xfercnt > 2 && xfer[1].len && xfer[1].tx_buf) {
>> > + Â Â Ârpc->smenr |= RPC_SMENR_ADB(fls(xfer[1].tx_nbits >> 1));
>> > + Â Â Âfor (i = 0; i < xfer[1].len; i++)
>> > + Â Â Â Â rpc->addr |= (u32)((u8 *)xfer[1].tx_buf)[i]
>> > + Â Â Â Â Â Â Â << (8 * (xfer[1].len - i - 1));
>> > +
>> > + Â Â Âif (xfer[1].len == 4)
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_ADE(0xf);
>> > + Â Â Âelse
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_ADE(0x7);
>> > + Â }
>> > +
>> > + Â switch (xfercnt) {
>> > + Â case 2:
>> > + Â Â Âif (xfer[1].rx_buf) {
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_SPIDE(rpc_bits_xfer
>> > + Â Â Â Â Â Â Â Â Â(xfer[1].len)) | RPC_SMENR_SPIDB(fls
>> > + Â Â Â Â Â Â Â Â Â(xfer[1].rx_nbits >> 1));
>> > + Â Â Â} else if (xfer[1].tx_buf) {
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_SPIDE(rpc_bits_xfer
>> > + Â Â Â Â Â Â Â Â Â(xfer[1].len)) | RPC_SMENR_SPIDB(fls
>> > + Â Â Â Â Â Â Â Â Â(xfer[1].tx_nbits >> 1));
>> > + Â Â Â}
>> > + Â Â Âbreak;
>> > +
>> > + Â case 3:
>> > + Â Â Âif (xfer[2].len && xfer[2].rx_buf && !xfer[2].tx_buf) {
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_SPIDE(rpc_bits_xfer
>> > + Â Â Â Â Â Â Â Â Â(xfer[2].len)) | RPC_SMENR_SPIDB(fls
>> > + Â Â Â Â Â Â Â Â Â(xfer[2].rx_nbits >> 1));
>>
>> It seems this SMENR pattern repeats itself throughout the driver, can
>> this be improved / deduplicated ?
>
> It seems no way!

Why ?

>> > + Â Â Â} else if (xfer[2].len && xfer[2].tx_buf && !xfer[2].rx_buf) {
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_SPIDE(rpc_bits_xfer
>> > + Â Â Â Â Â Â Â Â Â(xfer[2].len)) | RPC_SMENR_SPIDB(fls
>> > + Â Â Â Â Â Â Â Â Â(xfer[2].tx_nbits >> 1));
>> > + Â Â Â}
>> > + Â Â Âbreak;
>> > +
>> > + Â case 4:
>> > + Â Â Âif (xfer[2].len && xfer[2].tx_buf) {
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_DME;
>> > + Â Â Â Â rpc->dummy = RPC_SMDMCR_DMCYC(xfer[2].len);
>> > + Â Â Â}
>> > +
>> > + Â Â Âif (xfer[3].len && xfer[3].rx_buf) {
>> > + Â Â Â Â rpc->smenr |= RPC_SMENR_SPIDE(rpc_bits_xfer
>> > + Â Â Â Â Â Â Â Â Â(xfer[3].len)) | RPC_SMENR_SPIDB(fls
>> > + Â Â Â Â Â Â Â Â Â(xfer[3].rx_nbits >> 1));
>> > + Â Â Â}
>> > + Â Â Âbreak;
>> > +
>> > + Â default:
>> > + Â Â Âbreak;
>> > + Â }
>> > +}
>> > +
>> > +static int rpc_spi_xfer_message(struct rpc_spi *rpc, struct
>> spi_transfer *t)
>> > +{
>> > + Â int ret;
>> > +
>> > + Â ret = rpc_spi_set_freq(rpc, t->speed_hz);
>> > + Â if (ret)
>> > + Â Â Âreturn ret;
>> > +
>> > + Â ret = rpc_spi_io_xfer(rpc,
>> > + Â Â Â Â Â Â Â rpc->xfer_dir == SPI_MEM_DATA_OUT ?
>> > + Â Â Â Â Â Â Â t->tx_buf : NULL,
>> > + Â Â Â Â Â Â Â rpc->xfer_dir == SPI_MEM_DATA_IN ?
>> > + Â Â Â Â Â Â Â t->rx_buf : NULL);
>> > +
>> > + Â return ret;
>> > +}
>> > +
>> > +static int rpc_spi_transfer_one_message(struct spi_master *master,
>> > + Â Â Â Â Â Â Â struct spi_message *msg)
>> > +{
>> > + Â struct rpc_spi *rpc = spi_master_get_devdata(master);
>> > + Â struct spi_transfer *t;
>> > + Â int ret;
>> > +
>> > + Â rpc_spi_transfer_setup(rpc, msg);
>> > +
>> > + Â list_for_each_entry(t, &msg->transfers, transfer_list) {
>> > + Â Â Âif (!list_is_last(&t->transfer_list, &msg->transfers))
>> > + Â Â Â Â continue;
>> > + Â Â Âret = rpc_spi_xfer_message(rpc, t);
>> > + Â Â Âif (ret)
>> > + Â Â Â Â goto out;
>> > + Â }
>> > +
>> > + Â msg->status = 0;
>> > + Â msg->actual_length = rpc->totalxferlen;
>> > +out:
>> > + Â spi_finalize_current_message(master);
>> > + Â return 0;
>> > +}
>> > +
>> > +static const struct regmap_range rpc_spi_volatile_ranges[] = {
>> > + Â regmap_reg_range(RPC_SMRDR0, RPC_SMRDR0),
>> > + Â regmap_reg_range(RPC_SMWDR0, RPC_SMWDR0),
>>
>> Why is SMWDR volatile ?
>
> No matter is write-back or write-through.

Oh, do you want to assure the SMWDR value is always written directly to
the hardware ?

btw. I think SMRDR should be precious ?

>> > + Â regmap_reg_range(RPC_CMNSR, RPC_CMNSR),
>> > +};
>> > +
>> > +static const struct regmap_access_table rpc_spi_volatile_table = {
>> > +  .yes_ranges  = rpc_spi_volatile_ranges,
>> > +  .n_yes_ranges  = ARRAY_SIZE(rpc_spi_volatile_ranges),
>> > +};
>> > +
>> > +static const struct regmap_config rpc_spi_regmap_config = {
>> > + Â .reg_bits = 32,
>> > + Â .val_bits = 32,
>> > + Â .reg_stride = 4,
>> > + Â .fast_io = true,
>> > + Â .max_register = RPC_WBUF + RPC_WBUF_SIZE,
>> > + Â .volatile_table = &rpc_spi_volatile_table,
>> > +};
>> > +
>> > +static int rpc_spi_probe(struct platform_device *pdev)
>> > +{
>> > + Â struct spi_master *master;
>> > + Â struct resource *res;
>> > + Â struct rpc_spi *rpc;
>> > + Â const struct regmap_config *regmap_config;
>> > + Â int ret;
>> > +
>> > + Â master = spi_alloc_master(&pdev->dev, sizeof(struct rpc_spi));
>>
>> sizeof(*rpc)
>>
>> > + Â if (!master)
>> > + Â Â Âreturn -ENOMEM;
>> > +
>> > + Â platform_set_drvdata(pdev, master);
>> > +
>> > + Â rpc = spi_master_get_devdata(master);
>> > +
>> > + Â master->dev.of_node = pdev->dev.of_node;
>> > +
>> > + Â rpc->clk_rpc = devm_clk_get(&pdev->dev, "clk_rpc");
>> > + Â if (IS_ERR(rpc->clk_rpc))
>> > + Â Â Âreturn PTR_ERR(rpc->clk_rpc);
>> > +
>> > + Â res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
> "rpc_regs");
>> > + Â rpc->base = devm_ioremap_resource(&pdev->dev, res);
>> > + Â if (IS_ERR(rpc->base))
>> > + Â Â Âreturn PTR_ERR(rpc->base);
>> > +
>> > + Â regmap_config = &rpc_spi_regmap_config;
>> > + Â rpc->regmap = devm_regmap_init_mmio(&pdev->dev, rpc->base,
>> > + Â Â Â Â Â Â Â Â Â regmap_config);
>> > + Â if (IS_ERR(rpc->regmap)) {
>> > + Â Â Âdev_err(&pdev->dev, "failed to init regmap %ld for rpc-spi\n",
>> > + Â Â Â Â PTR_ERR(rpc->regmap));
>> > + Â Â Âreturn PTR_ERR(rpc->regmap);
>> > + Â }
>> > +
>> > + Â res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dirmap");
>> > + Â rpc->linear.map = devm_ioremap_resource(&pdev->dev, res);
>> > + Â if (!IS_ERR(rpc->linear.map)) {
>> > + Â Â Ârpc->linear.dma = res->start;
>> > + Â Â Ârpc->linear.size = resource_size(res);
>> > + Â } else {
>> > + Â Â Ârpc->linear.map = NULL;
>> > + Â }
>> > +
>> > + Â rpc->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
>> > + Â if (IS_ERR(rpc->rstc))
>> > + Â Â Âreturn PTR_ERR(rpc->rstc);
>> > +
>> > + Â pm_runtime_enable(&pdev->dev);
>> > + Â master->auto_runtime_pm = true;
>> > +
>> > + Â master->num_chipselect = 1;
>> > + Â master->mem_ops = &rpc_spi_mem_ops;
>> > + Â master->transfer_one_message = rpc_spi_transfer_one_message;
>> > +
>> > + Â master->bits_per_word_mask = SPI_BPW_MASK(8);
>> > + Â master->mode_bits = SPI_CPOL | SPI_CPHA |
>> > + Â Â Â Â SPI_RX_DUAL | SPI_TX_DUAL |
>> > + Â Â Â Â SPI_RX_QUAD | SPI_TX_QUAD;
>> > +
>> > + Â rpc_spi_hw_init(rpc);
>> > +
>> > + Â ret = spi_register_master(master);
>> > + Â if (ret) {
>> > + Â Â Âdev_err(&pdev->dev, "spi_register_master failed\n");
>>
>> Knowing the return value would be useful.
>>
>> > + Â Â Âgoto err_put_master;
>> > + Â }
>> > + Â return 0;
>> > +
>> > +err_put_master:
>> > + Â spi_master_put(master);
>> > + Â pm_runtime_disable(&pdev->dev);
>> > +
>> > + Â return ret;
>> > +}
>> > +
>> > +static int rpc_spi_remove(struct platform_device *pdev)
>> > +{
>> > + Â struct spi_master *master = platform_get_drvdata(pdev);
>> > +
>> > + Â pm_runtime_disable(&pdev->dev);
>> > + Â spi_unregister_master(master);
>> > +
>> > + Â return 0;
>> > +}
>> > +
>> > +static const struct of_device_id rpc_spi_of_ids[] = {
>> > + Â { .compatible = "renesas,r8a77995-rpc", },
>> > + Â { /* sentinel */ }
>> > +};
>> > +MODULE_DEVICE_TABLE(of, rpc_spi_of_ids);
>> > +
>> > +#ifdef CONFIG_PM_SLEEP
>> > +static int rpc_spi_suspend(struct device *dev)
>> > +{
>> > + Â struct platform_device *pdev = to_platform_device(dev);
>> > + Â struct spi_master *master = platform_get_drvdata(pdev);
>> > +
>> > + Â return spi_master_suspend(master);
>>
>> Won't the SPI NOR lose state across suspend ? Is that a problem ?
>
> I don't think so.
> Because when the device is not in operation and CS# is high,
> it is put in stand-by mode.

Is the power to the SPI NOR retained ?

> best regards,
> Mason
>
> CONFIDENTIALITY NOTE:
>
> This e-mail and any attachments may contain confidential information
> and/or personal data, which is protected by applicable laws. Please be
> reminded that duplication, disclosure, distribution, or use of this
> e-mail (and/or its attachments) or any part thereof is prohibited. If
> you receive this e-mail in error, please notify us immediately and
> delete this mail as well as its attachment(s) from your system. In
> addition, please be informed that collection, processing, and/or use of
> personal data is prohibited unless expressly permitted by personal data
> protection laws. Thank you for your attention and cooperation.
>
> Macronix International Co., Ltd.
>
> =====================================================================


--
Best regards,
Marek Vasut