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>
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
->controller
);
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
->controller
);
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
->controller
);
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 if (op
->dummy
.nbytes
)
357 uma_cfg
|= ilog2(op
->dummy
.buswidth
)
358 << NPCM_FIU_UMA_CFG_DBPCK_SHIFT
;
359 uma_cfg
|= ilog2(op
->data
.buswidth
)
360 << NPCM_FIU_UMA_CFG_RDBPCK_SHIFT
;
361 uma_cfg
|= op
->dummy
.nbytes
<< NPCM_FIU_UMA_CFG_DBSIZ_SHIFT
;
362 uma_cfg
|= op
->addr
.nbytes
<< NPCM_FIU_UMA_CFG_ADDSIZ_SHIFT
;
363 regmap_write(fiu
->regmap
, NPCM_FIU_UMA_ADDR
, addr
);
365 regmap_write(fiu
->regmap
, NPCM_FIU_UMA_ADDR
, 0x0);
368 uma_cfg
|= data_size
<< NPCM_FIU_UMA_CFG_RDATSIZ_SHIFT
;
369 regmap_write(fiu
->regmap
, NPCM_FIU_UMA_CFG
, uma_cfg
);
370 regmap_write_bits(fiu
->regmap
, NPCM_FIU_UMA_CTS
,
371 NPCM_FIU_UMA_CTS_EXEC_DONE
,
372 NPCM_FIU_UMA_CTS_EXEC_DONE
);
373 ret
= regmap_read_poll_timeout(fiu
->regmap
, NPCM_FIU_UMA_CTS
, val
,
374 (!(val
& NPCM_FIU_UMA_CTS_EXEC_DONE
)), 0,
375 UMA_MICRO_SEC_TIMEOUT
);
380 for (i
= 0; i
< DIV_ROUND_UP(data_size
, 4); i
++)
381 regmap_read(fiu
->regmap
, NPCM_FIU_UMA_DR0
+ (i
* 4),
383 memcpy(data
, data_reg
, data_size
);
389 static int npcm_fiu_uma_write(struct spi_mem
*mem
,
390 const struct spi_mem_op
*op
, u8 cmd
,
391 bool is_address_size
, u8
*data
, u32 data_size
)
393 struct npcm_fiu_spi
*fiu
=
394 spi_controller_get_devdata(mem
->spi
->controller
);
395 u32 uma_cfg
= BIT(10);
396 u32 data_reg
[4] = {0};
400 regmap_update_bits(fiu
->regmap
, NPCM_FIU_UMA_CTS
,
401 NPCM_FIU_UMA_CTS_DEV_NUM
,
402 (spi_get_chipselect(mem
->spi
, 0) <<
403 NPCM_FIU_UMA_CTS_DEV_NUM_SHIFT
));
405 regmap_update_bits(fiu
->regmap
, NPCM_FIU_UMA_CMD
,
406 NPCM_FIU_UMA_CMD_CMD
, cmd
);
409 memcpy(data_reg
, data
, data_size
);
410 for (i
= 0; i
< DIV_ROUND_UP(data_size
, 4); i
++)
411 regmap_write(fiu
->regmap
, NPCM_FIU_UMA_DW0
+ (i
* 4),
415 if (is_address_size
) {
416 uma_cfg
|= ilog2(op
->cmd
.buswidth
);
417 uma_cfg
|= ilog2(op
->addr
.buswidth
) <<
418 NPCM_FIU_UMA_CFG_ADBPCK_SHIFT
;
419 uma_cfg
|= ilog2(op
->data
.buswidth
) <<
420 NPCM_FIU_UMA_CFG_WDBPCK_SHIFT
;
421 uma_cfg
|= op
->addr
.nbytes
<< NPCM_FIU_UMA_CFG_ADDSIZ_SHIFT
;
422 regmap_write(fiu
->regmap
, NPCM_FIU_UMA_ADDR
, op
->addr
.val
);
424 regmap_write(fiu
->regmap
, NPCM_FIU_UMA_ADDR
, 0x0);
427 uma_cfg
|= (data_size
<< NPCM_FIU_UMA_CFG_WDATSIZ_SHIFT
);
428 regmap_write(fiu
->regmap
, NPCM_FIU_UMA_CFG
, uma_cfg
);
430 regmap_write_bits(fiu
->regmap
, NPCM_FIU_UMA_CTS
,
431 NPCM_FIU_UMA_CTS_EXEC_DONE
,
432 NPCM_FIU_UMA_CTS_EXEC_DONE
);
434 return regmap_read_poll_timeout(fiu
->regmap
, NPCM_FIU_UMA_CTS
, val
,
435 (!(val
& NPCM_FIU_UMA_CTS_EXEC_DONE
)), 0,
436 UMA_MICRO_SEC_TIMEOUT
);
439 static int npcm_fiu_manualwrite(struct spi_mem
*mem
,
440 const struct spi_mem_op
*op
)
442 struct npcm_fiu_spi
*fiu
=
443 spi_controller_get_devdata(mem
->spi
->controller
);
444 u8
*data
= (u8
*)op
->data
.buf
.out
;
450 num_data_chunks
= op
->data
.nbytes
/ CHUNK_SIZE
;
451 remain_data
= op
->data
.nbytes
% CHUNK_SIZE
;
453 regmap_update_bits(fiu
->regmap
, NPCM_FIU_UMA_CTS
,
454 NPCM_FIU_UMA_CTS_DEV_NUM
,
455 (spi_get_chipselect(mem
->spi
, 0) <<
456 NPCM_FIU_UMA_CTS_DEV_NUM_SHIFT
));
457 regmap_update_bits(fiu
->regmap
, NPCM_FIU_UMA_CTS
,
458 NPCM_FIU_UMA_CTS_SW_CS
, 0);
460 ret
= npcm_fiu_uma_write(mem
, op
, op
->cmd
.opcode
, true, NULL
, 0);
464 /* Starting the data writing loop in multiples of 8 */
465 for (idx
= 0; idx
< num_data_chunks
; ++idx
) {
466 ret
= npcm_fiu_uma_write(mem
, op
, data
[0], false,
467 &data
[1], CHUNK_SIZE
- 1);
474 /* Handling chunk remains */
475 if (remain_data
> 0) {
476 ret
= npcm_fiu_uma_write(mem
, op
, data
[0], false,
477 &data
[1], remain_data
- 1);
482 regmap_update_bits(fiu
->regmap
, NPCM_FIU_UMA_CTS
,
483 NPCM_FIU_UMA_CTS_SW_CS
, NPCM_FIU_UMA_CTS_SW_CS
);
488 static int npcm_fiu_read(struct spi_mem
*mem
, const struct spi_mem_op
*op
)
490 u8
*data
= op
->data
.buf
.in
;
491 int i
, readlen
, currlen
;
497 currlen
= op
->data
.nbytes
;
500 addr
= ((u32
)op
->addr
.val
+ i
);
507 ret
= npcm_fiu_uma_read(mem
, op
, addr
, true, buf_ptr
,
514 } while (currlen
> 0);
519 static void npcm_fiux_set_direct_wr(struct npcm_fiu_spi
*fiu
)
521 regmap_write(fiu
->regmap
, NPCM_FIU_DWR_CFG
,
522 NPCM_FIU_DWR_16_BYTE_BURST
);
523 regmap_update_bits(fiu
->regmap
, NPCM_FIU_DWR_CFG
,
524 NPCM_FIU_DWR_CFG_ABPCK
,
525 DWR_ABPCK_4_BIT_PER_CLK
<< NPCM_FIU_DWR_ABPCK_SHIFT
);
526 regmap_update_bits(fiu
->regmap
, NPCM_FIU_DWR_CFG
,
527 NPCM_FIU_DWR_CFG_DBPCK
,
528 DWR_DBPCK_4_BIT_PER_CLK
<< NPCM_FIU_DWR_DBPCK_SHIFT
);
531 static void npcm_fiux_set_direct_rd(struct npcm_fiu_spi
*fiu
)
535 regmap_write(fiu
->regmap
, NPCM_FIU_DRD_CFG
,
536 NPCM_FIU_DRD_16_BYTE_BURST
);
537 regmap_update_bits(fiu
->regmap
, NPCM_FIU_DRD_CFG
,
538 NPCM_FIU_DRD_CFG_ACCTYPE
,
539 DRD_SPI_X_MODE
<< NPCM_FIU_DRD_ACCTYPE_SHIFT
);
540 regmap_update_bits(fiu
->regmap
, NPCM_FIU_DRD_CFG
,
541 NPCM_FIU_DRD_CFG_DBW
,
542 rx_dummy
<< NPCM_FIU_DRD_DBW_SHIFT
);
545 static int npcm_fiu_exec_op(struct spi_mem
*mem
, const struct spi_mem_op
*op
)
547 struct npcm_fiu_spi
*fiu
=
548 spi_controller_get_devdata(mem
->spi
->controller
);
549 struct npcm_fiu_chip
*chip
= &fiu
->chip
[spi_get_chipselect(mem
->spi
, 0)];
553 dev_dbg(fiu
->dev
, "cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n",
554 op
->cmd
.opcode
, op
->cmd
.buswidth
, op
->addr
.buswidth
,
555 op
->dummy
.buswidth
, op
->data
.buswidth
, op
->addr
.val
,
558 if (fiu
->spix_mode
|| op
->addr
.nbytes
> 4)
561 if (fiu
->clkrate
!= chip
->clkrate
) {
562 ret
= clk_set_rate(fiu
->clk
, chip
->clkrate
);
564 dev_warn(fiu
->dev
, "Failed setting %lu frequency, stay at %lu frequency\n",
565 chip
->clkrate
, fiu
->clkrate
);
567 fiu
->clkrate
= chip
->clkrate
;
570 if (op
->data
.dir
== SPI_MEM_DATA_IN
) {
571 if (!op
->addr
.nbytes
) {
572 buf
= op
->data
.buf
.in
;
573 ret
= npcm_fiu_uma_read(mem
, op
, op
->addr
.val
, false,
574 buf
, op
->data
.nbytes
);
576 ret
= npcm_fiu_read(mem
, op
);
579 if (!op
->addr
.nbytes
&& !op
->data
.nbytes
)
580 ret
= npcm_fiu_uma_write(mem
, op
, op
->cmd
.opcode
, false,
582 if (op
->addr
.nbytes
&& !op
->data
.nbytes
) {
585 u32 addr
= op
->addr
.val
;
587 for (i
= op
->addr
.nbytes
- 1; i
>= 0; i
--) {
588 buf_addr
[i
] = addr
& 0xff;
591 ret
= npcm_fiu_uma_write(mem
, op
, op
->cmd
.opcode
, false,
592 buf_addr
, op
->addr
.nbytes
);
594 if (!op
->addr
.nbytes
&& op
->data
.nbytes
)
595 ret
= npcm_fiu_uma_write(mem
, op
, op
->cmd
.opcode
, false,
596 (u8
*)op
->data
.buf
.out
,
598 if (op
->addr
.nbytes
&& op
->data
.nbytes
)
599 ret
= npcm_fiu_manualwrite(mem
, op
);
605 static int npcm_fiu_dirmap_create(struct spi_mem_dirmap_desc
*desc
)
607 struct npcm_fiu_spi
*fiu
=
608 spi_controller_get_devdata(desc
->mem
->spi
->controller
);
609 struct npcm_fiu_chip
*chip
= &fiu
->chip
[spi_get_chipselect(desc
->mem
->spi
, 0)];
610 struct regmap
*gcr_regmap
;
613 dev_warn(fiu
->dev
, "Reserved memory not defined, direct read disabled\n");
614 desc
->nodirmap
= true;
618 if (!fiu
->spix_mode
&&
619 desc
->info
.op_tmpl
.data
.dir
== SPI_MEM_DATA_OUT
) {
620 desc
->nodirmap
= true;
624 if (!chip
->flash_region_mapped_ptr
) {
625 chip
->flash_region_mapped_ptr
=
626 devm_ioremap(fiu
->dev
, (fiu
->res_mem
->start
+
627 (fiu
->info
->max_map_size
*
628 spi_get_chipselect(desc
->mem
->spi
, 0))),
629 (u32
)desc
->info
.length
);
630 if (!chip
->flash_region_mapped_ptr
) {
631 dev_warn(fiu
->dev
, "Error mapping memory region, direct read disabled\n");
632 desc
->nodirmap
= true;
637 if (of_device_is_compatible(fiu
->dev
->of_node
, "nuvoton,npcm750-fiu")) {
639 syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr");
640 if (IS_ERR(gcr_regmap
)) {
641 dev_warn(fiu
->dev
, "Didn't find nuvoton,npcm750-gcr, direct read disabled\n");
642 desc
->nodirmap
= true;
645 regmap_update_bits(gcr_regmap
, NPCM7XX_INTCR3_OFFSET
,
646 NPCM7XX_INTCR3_FIU_FIX
,
647 NPCM7XX_INTCR3_FIU_FIX
);
649 regmap_update_bits(fiu
->regmap
, NPCM_FIU_CFG
,
650 NPCM_FIU_CFG_FIU_FIX
,
651 NPCM_FIU_CFG_FIU_FIX
);
654 if (desc
->info
.op_tmpl
.data
.dir
== SPI_MEM_DATA_IN
) {
656 npcm_fiu_set_drd(fiu
, &desc
->info
.op_tmpl
);
658 npcm_fiux_set_direct_rd(fiu
);
661 npcm_fiux_set_direct_wr(fiu
);
667 static int npcm_fiu_setup(struct spi_device
*spi
)
669 struct spi_controller
*ctrl
= spi
->controller
;
670 struct npcm_fiu_spi
*fiu
= spi_controller_get_devdata(ctrl
);
671 struct npcm_fiu_chip
*chip
;
673 chip
= &fiu
->chip
[spi_get_chipselect(spi
, 0)];
675 chip
->chipselect
= spi_get_chipselect(spi
, 0);
676 chip
->clkrate
= spi
->max_speed_hz
;
678 fiu
->clkrate
= clk_get_rate(fiu
->clk
);
683 static const struct spi_controller_mem_ops npcm_fiu_mem_ops
= {
684 .exec_op
= npcm_fiu_exec_op
,
685 .dirmap_create
= npcm_fiu_dirmap_create
,
686 .dirmap_read
= npcm_fiu_direct_read
,
687 .dirmap_write
= npcm_fiu_direct_write
,
690 static const struct of_device_id npcm_fiu_dt_ids
[] = {
691 { .compatible
= "nuvoton,npcm750-fiu", .data
= &npcm7xx_fiu_data
},
692 { .compatible
= "nuvoton,npcm845-fiu", .data
= &npxm8xx_fiu_data
},
696 static int npcm_fiu_probe(struct platform_device
*pdev
)
698 const struct fiu_data
*fiu_data_match
;
699 struct device
*dev
= &pdev
->dev
;
700 struct spi_controller
*ctrl
;
701 struct npcm_fiu_spi
*fiu
;
702 void __iomem
*regbase
;
705 ctrl
= devm_spi_alloc_host(dev
, sizeof(*fiu
));
709 fiu
= spi_controller_get_devdata(ctrl
);
711 fiu_data_match
= of_device_get_match_data(dev
);
712 if (!fiu_data_match
) {
713 dev_err(dev
, "No compatible OF match\n");
717 id
= of_alias_get_id(dev
->of_node
, "fiu");
718 if (id
< 0 || id
>= fiu_data_match
->fiu_max
) {
719 dev_err(dev
, "Invalid platform device id: %d\n", id
);
723 fiu
->info
= &fiu_data_match
->npcm_fiu_data_info
[id
];
725 platform_set_drvdata(pdev
, fiu
);
728 regbase
= devm_platform_ioremap_resource_byname(pdev
, "control");
730 return PTR_ERR(regbase
);
732 fiu
->regmap
= devm_regmap_init_mmio(dev
, regbase
,
733 &npcm_mtd_regmap_config
);
734 if (IS_ERR(fiu
->regmap
)) {
735 dev_err(dev
, "Failed to create regmap\n");
736 return PTR_ERR(fiu
->regmap
);
739 fiu
->res_mem
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
741 fiu
->clk
= devm_clk_get_enabled(dev
, NULL
);
742 if (IS_ERR(fiu
->clk
))
743 return PTR_ERR(fiu
->clk
);
745 fiu
->spix_mode
= of_property_read_bool(dev
->of_node
,
746 "nuvoton,spix-mode");
748 platform_set_drvdata(pdev
, fiu
);
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 return devm_spi_register_controller(dev
, ctrl
);
761 static void npcm_fiu_remove(struct platform_device
*pdev
)
765 MODULE_DEVICE_TABLE(of
, npcm_fiu_dt_ids
);
767 static struct platform_driver npcm_fiu_driver
= {
770 .bus
= &platform_bus_type
,
771 .of_match_table
= npcm_fiu_dt_ids
,
773 .probe
= npcm_fiu_probe
,
774 .remove
= npcm_fiu_remove
,
776 module_platform_driver(npcm_fiu_driver
);
778 MODULE_DESCRIPTION("Nuvoton FLASH Interface Unit SPI Controller Driver");
779 MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>");
780 MODULE_LICENSE("GPL v2");