1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2022 Nuvoton Technology Corp.
4 * NPCM Flash Interface Unit(FIU) SPI master controller driver.
11 #include <linux/bitfield.h>
12 #include <linux/log2.h>
13 #include <linux/iopoll.h>
17 #define XFER_TIMEOUT 1000000
19 /* FIU UMA Configuration Register (UMA_CFG) */
20 #define UMA_CFG_RDATSIZ_MASK GENMASK(28, 24)
21 #define UMA_CFG_DBSIZ_MASK GENMASK(23, 21)
22 #define UMA_CFG_WDATSIZ_MASK GENMASK(20, 16)
23 #define UMA_CFG_ADDSIZ_MASK GENMASK(13, 11)
24 #define UMA_CFG_RDBPCK_MASK GENMASK(9, 8)
25 #define UMA_CFG_DBPCK_MASK GENMASK(7, 6)
26 #define UMA_CFG_WDBPCK_MASK GENMASK(5, 4)
27 #define UMA_CFG_ADBPCK_MASK GENMASK(3, 2)
28 #define UMA_CFG_CMBPCK_MASK GENMASK(1, 0)
29 #define UMA_CFG_CMDSIZ_SHIFT 10
31 /* FIU UMA Control and Status Register (UMA_CTS) */
32 #define UMA_CTS_SW_CS BIT(16)
33 #define UMA_CTS_EXEC_DONE BIT(0)
34 #define UMA_CTS_RDYST BIT(24)
35 #define UMA_CTS_DEV_NUM_MASK GENMASK(9, 8)
37 struct npcm_fiu_regs {
43 unsigned int uma_addr;
45 unsigned char res1[4];
54 unsigned int prt_cmd0;
55 unsigned int prt_cmd1;
56 unsigned int prt_cmd2;
57 unsigned int prt_cmd3;
58 unsigned int prt_cmd4;
59 unsigned int prt_cmd5;
60 unsigned int prt_cmd6;
61 unsigned int prt_cmd7;
62 unsigned int prt_cmd8;
63 unsigned int prt_cmd9;
64 unsigned int stuff[4];
68 struct npcm_fiu_priv {
69 struct npcm_fiu_regs *regs;
73 static int npcm_fiu_spi_set_speed(struct udevice *bus, uint speed)
75 struct npcm_fiu_priv *priv = dev_get_priv(bus);
78 debug("%s: set speed %u\n", bus->name, speed);
79 ret = clk_set_rate(&priv->clk, speed);
86 static int npcm_fiu_spi_set_mode(struct udevice *bus, uint mode)
91 static inline void activate_cs(struct npcm_fiu_regs *regs, int cs)
93 writel(FIELD_PREP(UMA_CTS_DEV_NUM_MASK, cs), ®s->uma_cts);
96 static inline void deactivate_cs(struct npcm_fiu_regs *regs, int cs)
98 writel(FIELD_PREP(UMA_CTS_DEV_NUM_MASK, cs) | UMA_CTS_SW_CS, ®s->uma_cts);
101 static int fiu_uma_read(struct udevice *bus, u8 *buf, u32 size)
103 struct npcm_fiu_priv *priv = dev_get_priv(bus);
104 struct npcm_fiu_regs *regs = priv->regs;
110 writel(FIELD_PREP(UMA_CFG_RDATSIZ_MASK, size), ®s->uma_cfg);
112 /* Initiate the read */
113 writel(readl(®s->uma_cts) | UMA_CTS_EXEC_DONE, ®s->uma_cts);
115 /* Wait for completion */
116 ret = readl_poll_timeout(®s->uma_cts, val,
117 !(val & UMA_CTS_EXEC_DONE), XFER_TIMEOUT);
119 printf("npcm_fiu: read timeout\n");
123 /* Copy data from data registers */
125 data_reg[0] = readl(®s->uma_dr0);
127 data_reg[1] = readl(®s->uma_dr1);
128 if (size > DW_SIZE * 2)
129 data_reg[2] = readl(®s->uma_dr2);
130 if (size > DW_SIZE * 3)
131 data_reg[3] = readl(®s->uma_dr3);
132 memcpy(buf, data_reg, size);
137 static int fiu_uma_write(struct udevice *bus, const u8 *buf, u32 size)
139 struct npcm_fiu_priv *priv = dev_get_priv(bus);
140 struct npcm_fiu_regs *regs = priv->regs;
146 writel(FIELD_PREP(UMA_CFG_WDATSIZ_MASK, size), ®s->uma_cfg);
148 /* Write data to data registers */
149 memcpy(data_reg, buf, size);
151 writel(data_reg[0], ®s->uma_dw0);
153 writel(data_reg[1], ®s->uma_dw1);
154 if (size > DW_SIZE * 2)
155 writel(data_reg[2], ®s->uma_dw2);
156 if (size > DW_SIZE * 3)
157 writel(data_reg[3], ®s->uma_dw3);
159 /* Initiate the transaction */
160 writel(readl(®s->uma_cts) | UMA_CTS_EXEC_DONE, ®s->uma_cts);
162 /* Wait for completion */
163 ret = readl_poll_timeout(®s->uma_cts, val,
164 !(val & UMA_CTS_EXEC_DONE), XFER_TIMEOUT);
166 printf("npcm_fiu: write timeout\n");
171 static int npcm_fiu_spi_xfer(struct udevice *dev, unsigned int bitlen,
172 const void *dout, void *din, unsigned long flags)
174 struct udevice *bus = dev->parent;
175 struct npcm_fiu_priv *priv = dev_get_priv(bus);
176 struct npcm_fiu_regs *regs = priv->regs;
177 struct dm_spi_slave_plat *slave_plat =
178 dev_get_parent_plat(dev);
181 int bytes = bitlen / 8;
185 if (flags & SPI_XFER_BEGIN)
186 activate_cs(regs, slave_plat->cs);
189 len = (bytes > CHUNK_SIZE) ? CHUNK_SIZE : bytes;
191 ret = fiu_uma_write(bus, tx, len);
196 ret = fiu_uma_read(bus, rx, len);
204 if (flags & SPI_XFER_END)
205 deactivate_cs(regs, slave_plat->cs);
210 static int npcm_fiu_uma_operation(struct npcm_fiu_priv *priv, const struct spi_mem_op *op,
211 u32 addr, const u8 *tx, u8 *rx, u32 nbytes, bool started)
213 struct npcm_fiu_regs *regs = priv->regs;
214 u32 uma_cfg = 0, val;
218 debug("fiu_uma: opcode 0x%x, dir %d, addr 0x%x, %d bytes\n",
219 op->cmd.opcode, op->data.dir, addr, nbytes);
220 debug(" buswidth cmd:%d, addr:%d, dummy:%d, data:%d\n",
221 op->cmd.buswidth, op->addr.buswidth, op->dummy.buswidth,
223 debug(" size cmd:%d, addr:%d, dummy:%d, data:%d\n",
224 1, op->addr.nbytes, op->dummy.nbytes, op->data.nbytes);
225 debug(" tx %p, rx %p\n", tx, rx);
228 /* Send cmd/addr in the begin of an transaction */
229 writel(op->cmd.opcode, ®s->uma_cmd);
231 uma_cfg |= FIELD_PREP(UMA_CFG_CMBPCK_MASK, ilog2(op->cmd.buswidth)) |
232 (1 << UMA_CFG_CMDSIZ_SHIFT);
233 /* Configure addr bytes */
234 if (op->addr.nbytes) {
235 uma_cfg |= FIELD_PREP(UMA_CFG_ADBPCK_MASK, ilog2(op->addr.buswidth)) |
236 FIELD_PREP(UMA_CFG_ADDSIZ_MASK, op->addr.nbytes);
237 writel(addr, ®s->uma_addr);
239 /* Configure dummy bytes */
240 if (op->dummy.nbytes)
241 uma_cfg |= FIELD_PREP(UMA_CFG_DBPCK_MASK, ilog2(op->dummy.buswidth)) |
242 FIELD_PREP(UMA_CFG_DBSIZ_MASK, op->dummy.nbytes);
244 /* Set data bus width and data size */
245 if (op->data.dir == SPI_MEM_DATA_IN && nbytes)
246 uma_cfg |= FIELD_PREP(UMA_CFG_RDBPCK_MASK, ilog2(op->data.buswidth)) |
247 FIELD_PREP(UMA_CFG_RDATSIZ_MASK, nbytes);
248 else if (op->data.dir == SPI_MEM_DATA_OUT && nbytes)
249 uma_cfg |= FIELD_PREP(UMA_CFG_WDBPCK_MASK, ilog2(op->data.buswidth)) |
250 FIELD_PREP(UMA_CFG_WDATSIZ_MASK, nbytes);
251 writel(uma_cfg, ®s->uma_cfg);
253 if (op->data.dir == SPI_MEM_DATA_OUT && nbytes) {
254 memcpy(data_reg, tx, nbytes);
257 writel(data_reg[0], ®s->uma_dw0);
258 if (nbytes > DW_SIZE)
259 writel(data_reg[1], ®s->uma_dw1);
260 if (nbytes > DW_SIZE * 2)
261 writel(data_reg[2], ®s->uma_dw2);
262 if (nbytes > DW_SIZE * 3)
263 writel(data_reg[3], ®s->uma_dw3);
265 /* Initiate the transaction */
266 writel(readl(®s->uma_cts) | UMA_CTS_EXEC_DONE, ®s->uma_cts);
268 /* Wait for completion */
269 ret = readl_poll_timeout(®s->uma_cts, val,
270 !(val & UMA_CTS_EXEC_DONE), XFER_TIMEOUT);
272 printf("npcm_fiu: UMA op timeout\n");
276 if (op->data.dir == SPI_MEM_DATA_IN && nbytes) {
278 data_reg[0] = readl(®s->uma_dr0);
279 if (nbytes > DW_SIZE)
280 data_reg[1] = readl(®s->uma_dr1);
281 if (nbytes > DW_SIZE * 2)
282 data_reg[2] = readl(®s->uma_dr2);
283 if (nbytes > DW_SIZE * 3)
284 data_reg[3] = readl(®s->uma_dr3);
286 memcpy(rx, data_reg, nbytes);
292 static int npcm_fiu_exec_op(struct spi_slave *slave,
293 const struct spi_mem_op *op)
295 struct udevice *bus = slave->dev->parent;
296 struct npcm_fiu_priv *priv = dev_get_priv(bus);
297 struct npcm_fiu_regs *regs = priv->regs;
298 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(slave->dev);
299 u32 bytes, len, addr;
302 bool started = false;
305 bytes = op->data.nbytes;
306 addr = (u32)op->addr.val;
308 activate_cs(regs, slave_plat->cs);
309 ret = npcm_fiu_uma_operation(priv, op, addr, NULL, NULL, 0, false);
310 deactivate_cs(regs, slave_plat->cs);
314 tx = op->data.buf.out;
315 rx = op->data.buf.in;
317 * Use SW-control CS for write to extend the transaction and
318 * keep the Write Enable state.
319 * Use HW-control CS for read to avoid clock and timing issues.
321 if (op->data.dir == SPI_MEM_DATA_OUT)
322 activate_cs(regs, slave_plat->cs);
324 writel(FIELD_PREP(UMA_CTS_DEV_NUM_MASK, slave_plat->cs) | UMA_CTS_SW_CS,
327 len = (bytes > CHUNK_SIZE) ? CHUNK_SIZE : bytes;
328 ret = npcm_fiu_uma_operation(priv, op, addr, tx, rx, len, started);
332 /* CS is kept low for uma write, extend the transaction */
333 if (op->data.dir == SPI_MEM_DATA_OUT)
343 if (op->data.dir == SPI_MEM_DATA_OUT)
344 deactivate_cs(regs, slave_plat->cs);
349 static int npcm_fiu_spi_probe(struct udevice *bus)
351 struct npcm_fiu_priv *priv = dev_get_priv(bus);
354 priv->regs = (struct npcm_fiu_regs *)dev_read_addr_ptr(bus);
356 ret = clk_get_by_index(bus, 0, &priv->clk);
363 static const struct spi_controller_mem_ops npcm_fiu_mem_ops = {
364 .exec_op = npcm_fiu_exec_op,
367 static const struct dm_spi_ops npcm_fiu_spi_ops = {
368 .xfer = npcm_fiu_spi_xfer,
369 .set_speed = npcm_fiu_spi_set_speed,
370 .set_mode = npcm_fiu_spi_set_mode,
371 .mem_ops = &npcm_fiu_mem_ops,
374 static const struct udevice_id npcm_fiu_spi_ids[] = {
375 { .compatible = "nuvoton,npcm845-fiu" },
376 { .compatible = "nuvoton,npcm750-fiu" },
380 U_BOOT_DRIVER(npcm_fiu_spi) = {
381 .name = "npcm_fiu_spi",
383 .of_match = npcm_fiu_spi_ids,
384 .ops = &npcm_fiu_spi_ops,
385 .priv_auto = sizeof(struct npcm_fiu_priv),
386 .probe = npcm_fiu_spi_probe,