1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Nuvoton Technology corporation.
4 #include <linux/bits.h>
5 #include <linux/init.h>
6 #include <linux/kernel.h>
7 #include <linux/device.h>
8 #include <linux/module.h>
9 #include <linux/ioport.h>
10 #include <linux/clk.h>
11 #include <linux/platform_device.h>
13 #include <linux/vmalloc.h>
14 #include <linux/regmap.h>
15 #include <linux/of_device.h>
16 #include <linux/spi/spi-mem.h>
17 #include <linux/mfd/syscon.h>
19 /* NPCM7xx GCR module */
20 #define NPCM7XX_INTCR3_OFFSET 0x9C
21 #define NPCM7XX_INTCR3_FIU_FIX BIT(6)
23 /* Flash Interface Unit (FIU) Registers */
24 #define NPCM_FIU_DRD_CFG 0x00
25 #define NPCM_FIU_DWR_CFG 0x04
26 #define NPCM_FIU_UMA_CFG 0x08
27 #define NPCM_FIU_UMA_CTS 0x0C
28 #define NPCM_FIU_UMA_CMD 0x10
29 #define NPCM_FIU_UMA_ADDR 0x14
30 #define NPCM_FIU_PRT_CFG 0x18
31 #define NPCM_FIU_UMA_DW0 0x20
32 #define NPCM_FIU_UMA_DW1 0x24
33 #define NPCM_FIU_UMA_DW2 0x28
34 #define NPCM_FIU_UMA_DW3 0x2C
35 #define NPCM_FIU_UMA_DR0 0x30
36 #define NPCM_FIU_UMA_DR1 0x34
37 #define NPCM_FIU_UMA_DR2 0x38
38 #define NPCM_FIU_UMA_DR3 0x3C
39 #define NPCM_FIU_CFG 0x78
40 #define NPCM_FIU_MAX_REG_LIMIT 0x80
42 /* FIU Direct Read Configuration Register */
43 #define NPCM_FIU_DRD_CFG_LCK BIT(31)
44 #define NPCM_FIU_DRD_CFG_R_BURST GENMASK(25, 24)
45 #define NPCM_FIU_DRD_CFG_ADDSIZ GENMASK(17, 16)
46 #define NPCM_FIU_DRD_CFG_DBW GENMASK(13, 12)
47 #define NPCM_FIU_DRD_CFG_ACCTYPE GENMASK(9, 8)
48 #define NPCM_FIU_DRD_CFG_RDCMD GENMASK(7, 0)
49 #define NPCM_FIU_DRD_ADDSIZ_SHIFT 16
50 #define NPCM_FIU_DRD_DBW_SHIFT 12
51 #define NPCM_FIU_DRD_ACCTYPE_SHIFT 8
53 /* FIU Direct Write Configuration Register */
54 #define NPCM_FIU_DWR_CFG_LCK BIT(31)
55 #define NPCM_FIU_DWR_CFG_W_BURST GENMASK(25, 24)
56 #define NPCM_FIU_DWR_CFG_ADDSIZ GENMASK(17, 16)
57 #define NPCM_FIU_DWR_CFG_ABPCK GENMASK(11, 10)
58 #define NPCM_FIU_DWR_CFG_DBPCK GENMASK(9, 8)
59 #define NPCM_FIU_DWR_CFG_WRCMD GENMASK(7, 0)
60 #define NPCM_FIU_DWR_ADDSIZ_SHIFT 16
61 #define NPCM_FIU_DWR_ABPCK_SHIFT 10
62 #define NPCM_FIU_DWR_DBPCK_SHIFT 8
64 /* FIU UMA Configuration Register */
65 #define NPCM_FIU_UMA_CFG_LCK BIT(31)
66 #define NPCM_FIU_UMA_CFG_CMMLCK BIT(30)
67 #define NPCM_FIU_UMA_CFG_RDATSIZ GENMASK(28, 24)
68 #define NPCM_FIU_UMA_CFG_DBSIZ GENMASK(23, 21)
69 #define NPCM_FIU_UMA_CFG_WDATSIZ GENMASK(20, 16)
70 #define NPCM_FIU_UMA_CFG_ADDSIZ GENMASK(13, 11)
71 #define NPCM_FIU_UMA_CFG_CMDSIZ BIT(10)
72 #define NPCM_FIU_UMA_CFG_RDBPCK GENMASK(9, 8)
73 #define NPCM_FIU_UMA_CFG_DBPCK GENMASK(7, 6)
74 #define NPCM_FIU_UMA_CFG_WDBPCK GENMASK(5, 4)
75 #define NPCM_FIU_UMA_CFG_ADBPCK GENMASK(3, 2)
76 #define NPCM_FIU_UMA_CFG_CMBPCK GENMASK(1, 0)
77 #define NPCM_FIU_UMA_CFG_ADBPCK_SHIFT 2
78 #define NPCM_FIU_UMA_CFG_WDBPCK_SHIFT 4
79 #define NPCM_FIU_UMA_CFG_DBPCK_SHIFT 6
80 #define NPCM_FIU_UMA_CFG_RDBPCK_SHIFT 8
81 #define NPCM_FIU_UMA_CFG_ADDSIZ_SHIFT 11
82 #define NPCM_FIU_UMA_CFG_WDATSIZ_SHIFT 16
83 #define NPCM_FIU_UMA_CFG_DBSIZ_SHIFT 21
84 #define NPCM_FIU_UMA_CFG_RDATSIZ_SHIFT 24
86 /* FIU UMA Control and Status Register */
87 #define NPCM_FIU_UMA_CTS_RDYIE BIT(25)
88 #define NPCM_FIU_UMA_CTS_RDYST BIT(24)
89 #define NPCM_FIU_UMA_CTS_SW_CS BIT(16)
90 #define NPCM_FIU_UMA_CTS_DEV_NUM GENMASK(9, 8)
91 #define NPCM_FIU_UMA_CTS_EXEC_DONE BIT(0)
92 #define NPCM_FIU_UMA_CTS_DEV_NUM_SHIFT 8
94 /* FIU UMA Command Register */
95 #define NPCM_FIU_UMA_CMD_DUM3 GENMASK(31, 24)
96 #define NPCM_FIU_UMA_CMD_DUM2 GENMASK(23, 16)
97 #define NPCM_FIU_UMA_CMD_DUM1 GENMASK(15, 8)
98 #define NPCM_FIU_UMA_CMD_CMD GENMASK(7, 0)
100 /* FIU UMA Address Register */
101 #define NPCM_FIU_UMA_ADDR_UMA_ADDR GENMASK(31, 0)
102 #define NPCM_FIU_UMA_ADDR_AB3 GENMASK(31, 24)
103 #define NPCM_FIU_UMA_ADDR_AB2 GENMASK(23, 16)
104 #define NPCM_FIU_UMA_ADDR_AB1 GENMASK(15, 8)
105 #define NPCM_FIU_UMA_ADDR_AB0 GENMASK(7, 0)
107 /* FIU UMA Write Data Bytes 0-3 Register */
108 #define NPCM_FIU_UMA_DW0_WB3 GENMASK(31, 24)
109 #define NPCM_FIU_UMA_DW0_WB2 GENMASK(23, 16)
110 #define NPCM_FIU_UMA_DW0_WB1 GENMASK(15, 8)
111 #define NPCM_FIU_UMA_DW0_WB0 GENMASK(7, 0)
113 /* FIU UMA Write Data Bytes 4-7 Register */
114 #define NPCM_FIU_UMA_DW1_WB7 GENMASK(31, 24)
115 #define NPCM_FIU_UMA_DW1_WB6 GENMASK(23, 16)
116 #define NPCM_FIU_UMA_DW1_WB5 GENMASK(15, 8)
117 #define NPCM_FIU_UMA_DW1_WB4 GENMASK(7, 0)
119 /* FIU UMA Write Data Bytes 8-11 Register */
120 #define NPCM_FIU_UMA_DW2_WB11 GENMASK(31, 24)
121 #define NPCM_FIU_UMA_DW2_WB10 GENMASK(23, 16)
122 #define NPCM_FIU_UMA_DW2_WB9 GENMASK(15, 8)
123 #define NPCM_FIU_UMA_DW2_WB8 GENMASK(7, 0)
125 /* FIU UMA Write Data Bytes 12-15 Register */
126 #define NPCM_FIU_UMA_DW3_WB15 GENMASK(31, 24)
127 #define NPCM_FIU_UMA_DW3_WB14 GENMASK(23, 16)
128 #define NPCM_FIU_UMA_DW3_WB13 GENMASK(15, 8)
129 #define NPCM_FIU_UMA_DW3_WB12 GENMASK(7, 0)
131 /* FIU UMA Read Data Bytes 0-3 Register */
132 #define NPCM_FIU_UMA_DR0_RB3 GENMASK(31, 24)
133 #define NPCM_FIU_UMA_DR0_RB2 GENMASK(23, 16)
134 #define NPCM_FIU_UMA_DR0_RB1 GENMASK(15, 8)
135 #define NPCM_FIU_UMA_DR0_RB0 GENMASK(7, 0)
137 /* FIU UMA Read Data Bytes 4-7 Register */
138 #define NPCM_FIU_UMA_DR1_RB15 GENMASK(31, 24)
139 #define NPCM_FIU_UMA_DR1_RB14 GENMASK(23, 16)
140 #define NPCM_FIU_UMA_DR1_RB13 GENMASK(15, 8)
141 #define NPCM_FIU_UMA_DR1_RB12 GENMASK(7, 0)
143 /* FIU UMA Read Data Bytes 8-11 Register */
144 #define NPCM_FIU_UMA_DR2_RB15 GENMASK(31, 24)
145 #define NPCM_FIU_UMA_DR2_RB14 GENMASK(23, 16)
146 #define NPCM_FIU_UMA_DR2_RB13 GENMASK(15, 8)
147 #define NPCM_FIU_UMA_DR2_RB12 GENMASK(7, 0)
149 /* FIU UMA Read Data Bytes 12-15 Register */
150 #define NPCM_FIU_UMA_DR3_RB15 GENMASK(31, 24)
151 #define NPCM_FIU_UMA_DR3_RB14 GENMASK(23, 16)
152 #define NPCM_FIU_UMA_DR3_RB13 GENMASK(15, 8)
153 #define NPCM_FIU_UMA_DR3_RB12 GENMASK(7, 0)
155 /* FIU Configuration Register */
156 #define NPCM_FIU_CFG_FIU_FIX BIT(31)
160 DRD_SINGLE_WIRE_MODE = 0,
161 DRD_DUAL_IO_MODE = 1,
162 DRD_QUAD_IO_MODE = 2,
167 DWR_ABPCK_BIT_PER_CLK = 0,
168 DWR_ABPCK_2_BIT_PER_CLK = 1,
169 DWR_ABPCK_4_BIT_PER_CLK = 2,
173 DWR_DBPCK_BIT_PER_CLK = 0,
174 DWR_DBPCK_2_BIT_PER_CLK = 1,
175 DWR_DBPCK_4_BIT_PER_CLK = 2,
178 #define NPCM_FIU_DRD_16_BYTE_BURST 0x3000000
179 #define NPCM_FIU_DWR_16_BYTE_BURST 0x3000000
181 #define MAP_SIZE_128MB 0x8000000
182 #define MAP_SIZE_16MB 0x1000000
183 #define MAP_SIZE_8MB 0x800000
185 #define FIU_DRD_MAX_DUMMY_NUMBER 3
186 #define NPCM_MAX_CHIP_NUM 4
187 #define CHUNK_SIZE 16
188 #define UMA_MICRO_SEC_TIMEOUT 150
197 struct npcm_fiu_info {
205 const struct npcm_fiu_info *npcm_fiu_data_info;
209 static const struct npcm_fiu_info npcm7xx_fiu_info[] = {
210 {.name = "FIU0", .fiu_id = FIU0,
211 .max_map_size = MAP_SIZE_128MB, .max_cs = 2},
212 {.name = "FIU3", .fiu_id = FIU3,
213 .max_map_size = MAP_SIZE_128MB, .max_cs = 4},
214 {.name = "FIUX", .fiu_id = FIUX,
215 .max_map_size = MAP_SIZE_16MB, .max_cs = 2} };
217 static const struct fiu_data npcm7xx_fiu_data = {
218 .npcm_fiu_data_info = npcm7xx_fiu_info,
222 static const struct npcm_fiu_info npxm8xx_fiu_info[] = {
223 {.name = "FIU0", .fiu_id = FIU0,
224 .max_map_size = MAP_SIZE_128MB, .max_cs = 2},
225 {.name = "FIU3", .fiu_id = FIU3,
226 .max_map_size = MAP_SIZE_128MB, .max_cs = 4},
227 {.name = "FIUX", .fiu_id = FIUX,
228 .max_map_size = MAP_SIZE_16MB, .max_cs = 2},
229 {.name = "FIU1", .fiu_id = FIU1,
230 .max_map_size = MAP_SIZE_16MB, .max_cs = 4} };
232 static const struct fiu_data npxm8xx_fiu_data = {
233 .npcm_fiu_data_info = npxm8xx_fiu_info,
239 struct npcm_fiu_chip {
240 void __iomem *flash_region_mapped_ptr;
241 struct npcm_fiu_spi *fiu;
242 unsigned long clkrate;
246 struct npcm_fiu_spi {
247 struct npcm_fiu_chip chip[NPCM_MAX_CHIP_NUM];
248 const struct npcm_fiu_info *info;
249 struct spi_mem_op drd_op;
250 struct resource *res_mem;
251 struct regmap *regmap;
252 unsigned long clkrate;
258 static const struct regmap_config npcm_mtd_regmap_config = {
262 .max_register = NPCM_FIU_MAX_REG_LIMIT,
265 static void npcm_fiu_set_drd(struct npcm_fiu_spi *fiu,
266 const struct spi_mem_op *op)
268 regmap_update_bits(fiu->regmap, NPCM_FIU_DRD_CFG,
269 NPCM_FIU_DRD_CFG_ACCTYPE,
270 ilog2(op->addr.buswidth) <<
271 NPCM_FIU_DRD_ACCTYPE_SHIFT);
272 fiu->drd_op.addr.buswidth = op->addr.buswidth;
273 regmap_update_bits(fiu->regmap, NPCM_FIU_DRD_CFG,
274 NPCM_FIU_DRD_CFG_DBW,
275 op->dummy.nbytes << NPCM_FIU_DRD_DBW_SHIFT);
276 fiu->drd_op.dummy.nbytes = op->dummy.nbytes;
277 regmap_update_bits(fiu->regmap, NPCM_FIU_DRD_CFG,
278 NPCM_FIU_DRD_CFG_RDCMD, op->cmd.opcode);
279 fiu->drd_op.cmd.opcode = op->cmd.opcode;
280 regmap_update_bits(fiu->regmap, NPCM_FIU_DRD_CFG,
281 NPCM_FIU_DRD_CFG_ADDSIZ,
282 (op->addr.nbytes - 3) << NPCM_FIU_DRD_ADDSIZ_SHIFT);
283 fiu->drd_op.addr.nbytes = op->addr.nbytes;
286 static ssize_t npcm_fiu_direct_read(struct spi_mem_dirmap_desc *desc,
287 u64 offs, size_t len, void *buf)
289 struct npcm_fiu_spi *fiu =
290 spi_controller_get_devdata(desc->mem->spi->master);
291 struct npcm_fiu_chip *chip = &fiu->chip[spi_get_chipselect(desc->mem->spi, 0)];
292 void __iomem *src = (void __iomem *)(chip->flash_region_mapped_ptr +
297 if (fiu->spix_mode) {
298 for (i = 0 ; i < len ; i++)
299 *(buf_rx + i) = ioread8(src + i);
301 if (desc->info.op_tmpl.addr.buswidth != fiu->drd_op.addr.buswidth ||
302 desc->info.op_tmpl.dummy.nbytes != fiu->drd_op.dummy.nbytes ||
303 desc->info.op_tmpl.cmd.opcode != fiu->drd_op.cmd.opcode ||
304 desc->info.op_tmpl.addr.nbytes != fiu->drd_op.addr.nbytes)
305 npcm_fiu_set_drd(fiu, &desc->info.op_tmpl);
307 memcpy_fromio(buf_rx, src, len);
313 static ssize_t npcm_fiu_direct_write(struct spi_mem_dirmap_desc *desc,
314 u64 offs, size_t len, const void *buf)
316 struct npcm_fiu_spi *fiu =
317 spi_controller_get_devdata(desc->mem->spi->master);
318 struct npcm_fiu_chip *chip = &fiu->chip[spi_get_chipselect(desc->mem->spi, 0)];
319 void __iomem *dst = (void __iomem *)(chip->flash_region_mapped_ptr +
321 const u8 *buf_tx = buf;
325 for (i = 0 ; i < len ; i++)
326 iowrite8(*(buf_tx + i), dst + i);
328 memcpy_toio(dst, buf_tx, len);
333 static int npcm_fiu_uma_read(struct spi_mem *mem,
334 const struct spi_mem_op *op, u32 addr,
335 bool is_address_size, u8 *data, u32 data_size)
337 struct npcm_fiu_spi *fiu =
338 spi_controller_get_devdata(mem->spi->master);
339 u32 uma_cfg = BIT(10);
345 regmap_update_bits(fiu->regmap, NPCM_FIU_UMA_CTS,
346 NPCM_FIU_UMA_CTS_DEV_NUM,
347 (spi_get_chipselect(mem->spi, 0) <<
348 NPCM_FIU_UMA_CTS_DEV_NUM_SHIFT));
349 regmap_update_bits(fiu->regmap, NPCM_FIU_UMA_CMD,
350 NPCM_FIU_UMA_CMD_CMD, op->cmd.opcode);
352 if (is_address_size) {
353 uma_cfg |= ilog2(op->cmd.buswidth);
354 uma_cfg |= ilog2(op->addr.buswidth)
355 << NPCM_FIU_UMA_CFG_ADBPCK_SHIFT;
356 uma_cfg |= ilog2(op->dummy.buswidth)
357 << NPCM_FIU_UMA_CFG_DBPCK_SHIFT;
358 uma_cfg |= ilog2(op->data.buswidth)
359 << NPCM_FIU_UMA_CFG_RDBPCK_SHIFT;
360 uma_cfg |= op->dummy.nbytes << NPCM_FIU_UMA_CFG_DBSIZ_SHIFT;
361 uma_cfg |= op->addr.nbytes << NPCM_FIU_UMA_CFG_ADDSIZ_SHIFT;
362 regmap_write(fiu->regmap, NPCM_FIU_UMA_ADDR, addr);
364 regmap_write(fiu->regmap, NPCM_FIU_UMA_ADDR, 0x0);
367 uma_cfg |= data_size << NPCM_FIU_UMA_CFG_RDATSIZ_SHIFT;
368 regmap_write(fiu->regmap, NPCM_FIU_UMA_CFG, uma_cfg);
369 regmap_write_bits(fiu->regmap, NPCM_FIU_UMA_CTS,
370 NPCM_FIU_UMA_CTS_EXEC_DONE,
371 NPCM_FIU_UMA_CTS_EXEC_DONE);
372 ret = regmap_read_poll_timeout(fiu->regmap, NPCM_FIU_UMA_CTS, val,
373 (!(val & NPCM_FIU_UMA_CTS_EXEC_DONE)), 0,
374 UMA_MICRO_SEC_TIMEOUT);
379 for (i = 0; i < DIV_ROUND_UP(data_size, 4); i++)
380 regmap_read(fiu->regmap, NPCM_FIU_UMA_DR0 + (i * 4),
382 memcpy(data, data_reg, data_size);
388 static int npcm_fiu_uma_write(struct spi_mem *mem,
389 const struct spi_mem_op *op, u8 cmd,
390 bool is_address_size, u8 *data, u32 data_size)
392 struct npcm_fiu_spi *fiu =
393 spi_controller_get_devdata(mem->spi->master);
394 u32 uma_cfg = BIT(10);
395 u32 data_reg[4] = {0};
399 regmap_update_bits(fiu->regmap, NPCM_FIU_UMA_CTS,
400 NPCM_FIU_UMA_CTS_DEV_NUM,
401 (spi_get_chipselect(mem->spi, 0) <<
402 NPCM_FIU_UMA_CTS_DEV_NUM_SHIFT));
404 regmap_update_bits(fiu->regmap, NPCM_FIU_UMA_CMD,
405 NPCM_FIU_UMA_CMD_CMD, cmd);
408 memcpy(data_reg, data, data_size);
409 for (i = 0; i < DIV_ROUND_UP(data_size, 4); i++)
410 regmap_write(fiu->regmap, NPCM_FIU_UMA_DW0 + (i * 4),
414 if (is_address_size) {
415 uma_cfg |= ilog2(op->cmd.buswidth);
416 uma_cfg |= ilog2(op->addr.buswidth) <<
417 NPCM_FIU_UMA_CFG_ADBPCK_SHIFT;
418 uma_cfg |= ilog2(op->data.buswidth) <<
419 NPCM_FIU_UMA_CFG_WDBPCK_SHIFT;
420 uma_cfg |= op->addr.nbytes << NPCM_FIU_UMA_CFG_ADDSIZ_SHIFT;
421 regmap_write(fiu->regmap, NPCM_FIU_UMA_ADDR, op->addr.val);
423 regmap_write(fiu->regmap, NPCM_FIU_UMA_ADDR, 0x0);
426 uma_cfg |= (data_size << NPCM_FIU_UMA_CFG_WDATSIZ_SHIFT);
427 regmap_write(fiu->regmap, NPCM_FIU_UMA_CFG, uma_cfg);
429 regmap_write_bits(fiu->regmap, NPCM_FIU_UMA_CTS,
430 NPCM_FIU_UMA_CTS_EXEC_DONE,
431 NPCM_FIU_UMA_CTS_EXEC_DONE);
433 return regmap_read_poll_timeout(fiu->regmap, NPCM_FIU_UMA_CTS, val,
434 (!(val & NPCM_FIU_UMA_CTS_EXEC_DONE)), 0,
435 UMA_MICRO_SEC_TIMEOUT);
438 static int npcm_fiu_manualwrite(struct spi_mem *mem,
439 const struct spi_mem_op *op)
441 struct npcm_fiu_spi *fiu =
442 spi_controller_get_devdata(mem->spi->master);
443 u8 *data = (u8 *)op->data.buf.out;
449 num_data_chunks = op->data.nbytes / CHUNK_SIZE;
450 remain_data = op->data.nbytes % CHUNK_SIZE;
452 regmap_update_bits(fiu->regmap, NPCM_FIU_UMA_CTS,
453 NPCM_FIU_UMA_CTS_DEV_NUM,
454 (spi_get_chipselect(mem->spi, 0) <<
455 NPCM_FIU_UMA_CTS_DEV_NUM_SHIFT));
456 regmap_update_bits(fiu->regmap, NPCM_FIU_UMA_CTS,
457 NPCM_FIU_UMA_CTS_SW_CS, 0);
459 ret = npcm_fiu_uma_write(mem, op, op->cmd.opcode, true, NULL, 0);
463 /* Starting the data writing loop in multiples of 8 */
464 for (idx = 0; idx < num_data_chunks; ++idx) {
465 ret = npcm_fiu_uma_write(mem, op, data[0], false,
466 &data[1], CHUNK_SIZE - 1);
473 /* Handling chunk remains */
474 if (remain_data > 0) {
475 ret = npcm_fiu_uma_write(mem, op, data[0], false,
476 &data[1], remain_data - 1);
481 regmap_update_bits(fiu->regmap, NPCM_FIU_UMA_CTS,
482 NPCM_FIU_UMA_CTS_SW_CS, NPCM_FIU_UMA_CTS_SW_CS);
487 static int npcm_fiu_read(struct spi_mem *mem, const struct spi_mem_op *op)
489 u8 *data = op->data.buf.in;
490 int i, readlen, currlen;
496 currlen = op->data.nbytes;
499 addr = ((u32)op->addr.val + i);
506 ret = npcm_fiu_uma_read(mem, op, addr, true, buf_ptr,
513 } while (currlen > 0);
518 static void npcm_fiux_set_direct_wr(struct npcm_fiu_spi *fiu)
520 regmap_write(fiu->regmap, NPCM_FIU_DWR_CFG,
521 NPCM_FIU_DWR_16_BYTE_BURST);
522 regmap_update_bits(fiu->regmap, NPCM_FIU_DWR_CFG,
523 NPCM_FIU_DWR_CFG_ABPCK,
524 DWR_ABPCK_4_BIT_PER_CLK << NPCM_FIU_DWR_ABPCK_SHIFT);
525 regmap_update_bits(fiu->regmap, NPCM_FIU_DWR_CFG,
526 NPCM_FIU_DWR_CFG_DBPCK,
527 DWR_DBPCK_4_BIT_PER_CLK << NPCM_FIU_DWR_DBPCK_SHIFT);
530 static void npcm_fiux_set_direct_rd(struct npcm_fiu_spi *fiu)
534 regmap_write(fiu->regmap, NPCM_FIU_DRD_CFG,
535 NPCM_FIU_DRD_16_BYTE_BURST);
536 regmap_update_bits(fiu->regmap, NPCM_FIU_DRD_CFG,
537 NPCM_FIU_DRD_CFG_ACCTYPE,
538 DRD_SPI_X_MODE << NPCM_FIU_DRD_ACCTYPE_SHIFT);
539 regmap_update_bits(fiu->regmap, NPCM_FIU_DRD_CFG,
540 NPCM_FIU_DRD_CFG_DBW,
541 rx_dummy << NPCM_FIU_DRD_DBW_SHIFT);
544 static int npcm_fiu_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
546 struct npcm_fiu_spi *fiu =
547 spi_controller_get_devdata(mem->spi->master);
548 struct npcm_fiu_chip *chip = &fiu->chip[spi_get_chipselect(mem->spi, 0)];
552 dev_dbg(fiu->dev, "cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n",
553 op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
554 op->dummy.buswidth, op->data.buswidth, op->addr.val,
557 if (fiu->spix_mode || op->addr.nbytes > 4)
560 if (fiu->clkrate != chip->clkrate) {
561 ret = clk_set_rate(fiu->clk, chip->clkrate);
563 dev_warn(fiu->dev, "Failed setting %lu frequency, stay at %lu frequency\n",
564 chip->clkrate, fiu->clkrate);
566 fiu->clkrate = chip->clkrate;
569 if (op->data.dir == SPI_MEM_DATA_IN) {
570 if (!op->addr.nbytes) {
571 buf = op->data.buf.in;
572 ret = npcm_fiu_uma_read(mem, op, op->addr.val, false,
573 buf, op->data.nbytes);
575 ret = npcm_fiu_read(mem, op);
578 if (!op->addr.nbytes && !op->data.nbytes)
579 ret = npcm_fiu_uma_write(mem, op, op->cmd.opcode, false,
581 if (op->addr.nbytes && !op->data.nbytes) {
584 u32 addr = op->addr.val;
586 for (i = op->addr.nbytes - 1; i >= 0; i--) {
587 buf_addr[i] = addr & 0xff;
590 ret = npcm_fiu_uma_write(mem, op, op->cmd.opcode, false,
591 buf_addr, op->addr.nbytes);
593 if (!op->addr.nbytes && op->data.nbytes)
594 ret = npcm_fiu_uma_write(mem, op, op->cmd.opcode, false,
595 (u8 *)op->data.buf.out,
597 if (op->addr.nbytes && op->data.nbytes)
598 ret = npcm_fiu_manualwrite(mem, op);
604 static int npcm_fiu_dirmap_create(struct spi_mem_dirmap_desc *desc)
606 struct npcm_fiu_spi *fiu =
607 spi_controller_get_devdata(desc->mem->spi->master);
608 struct npcm_fiu_chip *chip = &fiu->chip[spi_get_chipselect(desc->mem->spi, 0)];
609 struct regmap *gcr_regmap;
612 dev_warn(fiu->dev, "Reserved memory not defined, direct read disabled\n");
613 desc->nodirmap = true;
617 if (!fiu->spix_mode &&
618 desc->info.op_tmpl.data.dir == SPI_MEM_DATA_OUT) {
619 desc->nodirmap = true;
623 if (!chip->flash_region_mapped_ptr) {
624 chip->flash_region_mapped_ptr =
625 devm_ioremap(fiu->dev, (fiu->res_mem->start +
626 (fiu->info->max_map_size *
627 spi_get_chipselect(desc->mem->spi, 0))),
628 (u32)desc->info.length);
629 if (!chip->flash_region_mapped_ptr) {
630 dev_warn(fiu->dev, "Error mapping memory region, direct read disabled\n");
631 desc->nodirmap = true;
636 if (of_device_is_compatible(fiu->dev->of_node, "nuvoton,npcm750-fiu")) {
638 syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr");
639 if (IS_ERR(gcr_regmap)) {
640 dev_warn(fiu->dev, "Didn't find nuvoton,npcm750-gcr, direct read disabled\n");
641 desc->nodirmap = true;
644 regmap_update_bits(gcr_regmap, NPCM7XX_INTCR3_OFFSET,
645 NPCM7XX_INTCR3_FIU_FIX,
646 NPCM7XX_INTCR3_FIU_FIX);
648 regmap_update_bits(fiu->regmap, NPCM_FIU_CFG,
649 NPCM_FIU_CFG_FIU_FIX,
650 NPCM_FIU_CFG_FIU_FIX);
653 if (desc->info.op_tmpl.data.dir == SPI_MEM_DATA_IN) {
655 npcm_fiu_set_drd(fiu, &desc->info.op_tmpl);
657 npcm_fiux_set_direct_rd(fiu);
660 npcm_fiux_set_direct_wr(fiu);
666 static int npcm_fiu_setup(struct spi_device *spi)
668 struct spi_controller *ctrl = spi->master;
669 struct npcm_fiu_spi *fiu = spi_controller_get_devdata(ctrl);
670 struct npcm_fiu_chip *chip;
672 chip = &fiu->chip[spi_get_chipselect(spi, 0)];
674 chip->chipselect = spi_get_chipselect(spi, 0);
675 chip->clkrate = spi->max_speed_hz;
677 fiu->clkrate = clk_get_rate(fiu->clk);
682 static const struct spi_controller_mem_ops npcm_fiu_mem_ops = {
683 .exec_op = npcm_fiu_exec_op,
684 .dirmap_create = npcm_fiu_dirmap_create,
685 .dirmap_read = npcm_fiu_direct_read,
686 .dirmap_write = npcm_fiu_direct_write,
689 static const struct of_device_id npcm_fiu_dt_ids[] = {
690 { .compatible = "nuvoton,npcm750-fiu", .data = &npcm7xx_fiu_data },
691 { .compatible = "nuvoton,npcm845-fiu", .data = &npxm8xx_fiu_data },
695 static int npcm_fiu_probe(struct platform_device *pdev)
697 const struct fiu_data *fiu_data_match;
698 struct device *dev = &pdev->dev;
699 struct spi_controller *ctrl;
700 struct npcm_fiu_spi *fiu;
701 void __iomem *regbase;
704 ctrl = devm_spi_alloc_master(dev, sizeof(*fiu));
708 fiu = spi_controller_get_devdata(ctrl);
710 fiu_data_match = of_device_get_match_data(dev);
711 if (!fiu_data_match) {
712 dev_err(dev, "No compatible OF match\n");
716 id = of_alias_get_id(dev->of_node, "fiu");
717 if (id < 0 || id >= fiu_data_match->fiu_max) {
718 dev_err(dev, "Invalid platform device id: %d\n", id);
722 fiu->info = &fiu_data_match->npcm_fiu_data_info[id];
724 platform_set_drvdata(pdev, fiu);
727 regbase = devm_platform_ioremap_resource_byname(pdev, "control");
729 return PTR_ERR(regbase);
731 fiu->regmap = devm_regmap_init_mmio(dev, regbase,
732 &npcm_mtd_regmap_config);
733 if (IS_ERR(fiu->regmap)) {
734 dev_err(dev, "Failed to create regmap\n");
735 return PTR_ERR(fiu->regmap);
738 fiu->res_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
740 fiu->clk = devm_clk_get(dev, NULL);
741 if (IS_ERR(fiu->clk))
742 return PTR_ERR(fiu->clk);
744 fiu->spix_mode = of_property_read_bool(dev->of_node,
745 "nuvoton,spix-mode");
747 platform_set_drvdata(pdev, fiu);
748 clk_prepare_enable(fiu->clk);
750 ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD
751 | SPI_TX_DUAL | SPI_TX_QUAD;
752 ctrl->setup = npcm_fiu_setup;
754 ctrl->mem_ops = &npcm_fiu_mem_ops;
755 ctrl->num_chipselect = fiu->info->max_cs;
756 ctrl->dev.of_node = dev->of_node;
758 ret = devm_spi_register_master(dev, ctrl);
760 clk_disable_unprepare(fiu->clk);
765 static void npcm_fiu_remove(struct platform_device *pdev)
767 struct npcm_fiu_spi *fiu = platform_get_drvdata(pdev);
769 clk_disable_unprepare(fiu->clk);
772 MODULE_DEVICE_TABLE(of, npcm_fiu_dt_ids);
774 static struct platform_driver npcm_fiu_driver = {
777 .bus = &platform_bus_type,
778 .of_match_table = npcm_fiu_dt_ids,
780 .probe = npcm_fiu_probe,
781 .remove_new = npcm_fiu_remove,
783 module_platform_driver(npcm_fiu_driver);
785 MODULE_DESCRIPTION("Nuvoton FLASH Interface Unit SPI Controller Driver");
786 MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>");
787 MODULE_LICENSE("GPL v2");