2 * ASPEED AST2400 SMC Controller (SPI Flash Only)
4 * Copyright (C) 2016 IBM Corp.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "hw/sysbus.h"
27 #include "migration/vmstate.h"
29 #include "qemu/module.h"
30 #include "qemu/error-report.h"
31 #include "qapi/error.h"
32 #include "exec/address-spaces.h"
33 #include "qemu/units.h"
37 #include "hw/qdev-properties.h"
38 #include "hw/ssi/aspeed_smc.h"
40 /* CE Type Setting Register */
41 #define R_CONF (0x00 / 4)
42 #define CONF_LEGACY_DISABLE (1 << 31)
43 #define CONF_ENABLE_W4 20
44 #define CONF_ENABLE_W3 19
45 #define CONF_ENABLE_W2 18
46 #define CONF_ENABLE_W1 17
47 #define CONF_ENABLE_W0 16
48 #define CONF_FLASH_TYPE4 8
49 #define CONF_FLASH_TYPE3 6
50 #define CONF_FLASH_TYPE2 4
51 #define CONF_FLASH_TYPE1 2
52 #define CONF_FLASH_TYPE0 0
53 #define CONF_FLASH_TYPE_NOR 0x0
54 #define CONF_FLASH_TYPE_NAND 0x1
55 #define CONF_FLASH_TYPE_SPI 0x2 /* AST2600 is SPI only */
57 /* CE Control Register */
58 #define R_CE_CTRL (0x04 / 4)
59 #define CTRL_EXTENDED4 4 /* 32 bit addressing for SPI */
60 #define CTRL_EXTENDED3 3 /* 32 bit addressing for SPI */
61 #define CTRL_EXTENDED2 2 /* 32 bit addressing for SPI */
62 #define CTRL_EXTENDED1 1 /* 32 bit addressing for SPI */
63 #define CTRL_EXTENDED0 0 /* 32 bit addressing for SPI */
65 /* Interrupt Control and Status Register */
66 #define R_INTR_CTRL (0x08 / 4)
67 #define INTR_CTRL_DMA_STATUS (1 << 11)
68 #define INTR_CTRL_CMD_ABORT_STATUS (1 << 10)
69 #define INTR_CTRL_WRITE_PROTECT_STATUS (1 << 9)
70 #define INTR_CTRL_DMA_EN (1 << 3)
71 #define INTR_CTRL_CMD_ABORT_EN (1 << 2)
72 #define INTR_CTRL_WRITE_PROTECT_EN (1 << 1)
74 /* CEx Control Register */
75 #define R_CTRL0 (0x10 / 4)
76 #define CTRL_IO_QPI (1 << 31)
77 #define CTRL_IO_QUAD_DATA (1 << 30)
78 #define CTRL_IO_DUAL_DATA (1 << 29)
79 #define CTRL_IO_DUAL_ADDR_DATA (1 << 28) /* Includes dummies */
80 #define CTRL_IO_QUAD_ADDR_DATA (1 << 28) /* Includes dummies */
81 #define CTRL_CMD_SHIFT 16
82 #define CTRL_CMD_MASK 0xff
83 #define CTRL_DUMMY_HIGH_SHIFT 14
84 #define CTRL_AST2400_SPI_4BYTE (1 << 13)
85 #define CE_CTRL_CLOCK_FREQ_SHIFT 8
86 #define CE_CTRL_CLOCK_FREQ_MASK 0xf
87 #define CE_CTRL_CLOCK_FREQ(div) \
88 (((div) & CE_CTRL_CLOCK_FREQ_MASK) << CE_CTRL_CLOCK_FREQ_SHIFT)
89 #define CTRL_DUMMY_LOW_SHIFT 6 /* 2 bits [7:6] */
90 #define CTRL_CE_STOP_ACTIVE (1 << 2)
91 #define CTRL_CMD_MODE_MASK 0x3
92 #define CTRL_READMODE 0x0
93 #define CTRL_FREADMODE 0x1
94 #define CTRL_WRITEMODE 0x2
95 #define CTRL_USERMODE 0x3
96 #define R_CTRL1 (0x14 / 4)
97 #define R_CTRL2 (0x18 / 4)
98 #define R_CTRL3 (0x1C / 4)
99 #define R_CTRL4 (0x20 / 4)
101 /* CEx Segment Address Register */
102 #define R_SEG_ADDR0 (0x30 / 4)
103 #define SEG_END_SHIFT 24 /* 8MB units */
104 #define SEG_END_MASK 0xff
105 #define SEG_START_SHIFT 16 /* address bit [A29-A23] */
106 #define SEG_START_MASK 0xff
107 #define R_SEG_ADDR1 (0x34 / 4)
108 #define R_SEG_ADDR2 (0x38 / 4)
109 #define R_SEG_ADDR3 (0x3C / 4)
110 #define R_SEG_ADDR4 (0x40 / 4)
112 /* Misc Control Register #1 */
113 #define R_MISC_CTRL1 (0x50 / 4)
115 /* SPI dummy cycle data */
116 #define R_DUMMY_DATA (0x54 / 4)
118 /* DMA Control/Status Register */
119 #define R_DMA_CTRL (0x80 / 4)
120 #define DMA_CTRL_DELAY_MASK 0xf
121 #define DMA_CTRL_DELAY_SHIFT 8
122 #define DMA_CTRL_FREQ_MASK 0xf
123 #define DMA_CTRL_FREQ_SHIFT 4
124 #define DMA_CTRL_CALIB (1 << 3)
125 #define DMA_CTRL_CKSUM (1 << 2)
126 #define DMA_CTRL_WRITE (1 << 1)
127 #define DMA_CTRL_ENABLE (1 << 0)
129 /* DMA Flash Side Address */
130 #define R_DMA_FLASH_ADDR (0x84 / 4)
132 /* DMA DRAM Side Address */
133 #define R_DMA_DRAM_ADDR (0x88 / 4)
135 /* DMA Length Register */
136 #define R_DMA_LEN (0x8C / 4)
138 /* Checksum Calculation Result */
139 #define R_DMA_CHECKSUM (0x90 / 4)
141 /* Read Timing Compensation Register */
142 #define R_TIMINGS (0x94 / 4)
144 /* SPI controller registers and bits (AST2400) */
145 #define R_SPI_CONF (0x00 / 4)
146 #define SPI_CONF_ENABLE_W0 0
147 #define R_SPI_CTRL0 (0x4 / 4)
148 #define R_SPI_MISC_CTRL (0x10 / 4)
149 #define R_SPI_TIMINGS (0x14 / 4)
151 #define ASPEED_SMC_R_SPI_MAX (0x20 / 4)
152 #define ASPEED_SMC_R_SMC_MAX (0x20 / 4)
154 #define ASPEED_SOC_SMC_FLASH_BASE 0x10000000
155 #define ASPEED_SOC_FMC_FLASH_BASE 0x20000000
156 #define ASPEED_SOC_SPI_FLASH_BASE 0x30000000
157 #define ASPEED_SOC_SPI2_FLASH_BASE 0x38000000
160 * DMA DRAM addresses should be 4 bytes aligned and the valid address
161 * range is 0x40000000 - 0x5FFFFFFF (AST2400)
162 * 0x80000000 - 0xBFFFFFFF (AST2500)
164 * DMA flash addresses should be 4 bytes aligned and the valid address
165 * range is 0x20000000 - 0x2FFFFFFF.
167 * DMA length is from 4 bytes to 32MB
169 * 0x7FFFFF: 32M bytes
171 #define DMA_DRAM_ADDR(s, val) ((s)->sdram_base | \
172 ((val) & (s)->ctrl->dma_dram_mask))
173 #define DMA_FLASH_ADDR(s, val) ((s)->ctrl->flash_window_base | \
174 ((val) & (s)->ctrl->dma_flash_mask))
175 #define DMA_LENGTH(val) ((val) & 0x01FFFFFC)
178 #define SPI_OP_READ 0x03 /* Read data bytes (low frequency) */
180 #define SNOOP_OFF 0xFF
181 #define SNOOP_START 0x0
184 * Default segments mapping addresses and size for each slave per
185 * controller. These can be changed when board is initialized with the
186 * Segment Address Registers.
188 static const AspeedSegments aspeed_segments_legacy
[] = {
189 { 0x10000000, 32 * 1024 * 1024 },
192 static const AspeedSegments aspeed_segments_fmc
[] = {
193 { 0x20000000, 64 * 1024 * 1024 }, /* start address is readonly */
194 { 0x24000000, 32 * 1024 * 1024 },
195 { 0x26000000, 32 * 1024 * 1024 },
196 { 0x28000000, 32 * 1024 * 1024 },
197 { 0x2A000000, 32 * 1024 * 1024 }
200 static const AspeedSegments aspeed_segments_spi
[] = {
201 { 0x30000000, 64 * 1024 * 1024 },
204 static const AspeedSegments aspeed_segments_ast2500_fmc
[] = {
205 { 0x20000000, 128 * 1024 * 1024 }, /* start address is readonly */
206 { 0x28000000, 32 * 1024 * 1024 },
207 { 0x2A000000, 32 * 1024 * 1024 },
210 static const AspeedSegments aspeed_segments_ast2500_spi1
[] = {
211 { 0x30000000, 32 * 1024 * 1024 }, /* start address is readonly */
212 { 0x32000000, 96 * 1024 * 1024 }, /* end address is readonly */
215 static const AspeedSegments aspeed_segments_ast2500_spi2
[] = {
216 { 0x38000000, 32 * 1024 * 1024 }, /* start address is readonly */
217 { 0x3A000000, 96 * 1024 * 1024 }, /* end address is readonly */
219 static uint32_t aspeed_smc_segment_to_reg(const AspeedSMCState
*s
,
220 const AspeedSegments
*seg
);
221 static void aspeed_smc_reg_to_segment(const AspeedSMCState
*s
, uint32_t reg
,
222 AspeedSegments
*seg
);
225 * AST2600 definitions
227 #define ASPEED26_SOC_FMC_FLASH_BASE 0x20000000
228 #define ASPEED26_SOC_SPI_FLASH_BASE 0x30000000
229 #define ASPEED26_SOC_SPI2_FLASH_BASE 0x50000000
231 static const AspeedSegments aspeed_segments_ast2600_fmc
[] = {
232 { 0x0, 128 * MiB
}, /* start address is readonly */
233 { 0x0, 0 }, /* disabled */
234 { 0x0, 0 }, /* disabled */
237 static const AspeedSegments aspeed_segments_ast2600_spi1
[] = {
238 { 0x0, 128 * MiB
}, /* start address is readonly */
239 { 0x0, 0 }, /* disabled */
242 static const AspeedSegments aspeed_segments_ast2600_spi2
[] = {
243 { 0x0, 128 * MiB
}, /* start address is readonly */
244 { 0x0, 0 }, /* disabled */
245 { 0x0, 0 }, /* disabled */
248 static uint32_t aspeed_2600_smc_segment_to_reg(const AspeedSMCState
*s
,
249 const AspeedSegments
*seg
);
250 static void aspeed_2600_smc_reg_to_segment(const AspeedSMCState
*s
,
251 uint32_t reg
, AspeedSegments
*seg
);
253 static const AspeedSMCController controllers
[] = {
255 .name
= "aspeed.smc-ast2400",
257 .r_ce_ctrl
= R_CE_CTRL
,
259 .r_timings
= R_TIMINGS
,
261 .conf_enable_w0
= CONF_ENABLE_W0
,
263 .segments
= aspeed_segments_legacy
,
264 .flash_window_base
= ASPEED_SOC_SMC_FLASH_BASE
,
265 .flash_window_size
= 0x6000000,
267 .nregs
= ASPEED_SMC_R_SMC_MAX
,
268 .segment_to_reg
= aspeed_smc_segment_to_reg
,
269 .reg_to_segment
= aspeed_smc_reg_to_segment
,
271 .name
= "aspeed.fmc-ast2400",
273 .r_ce_ctrl
= R_CE_CTRL
,
275 .r_timings
= R_TIMINGS
,
277 .conf_enable_w0
= CONF_ENABLE_W0
,
279 .segments
= aspeed_segments_fmc
,
280 .flash_window_base
= ASPEED_SOC_FMC_FLASH_BASE
,
281 .flash_window_size
= 0x10000000,
283 .dma_flash_mask
= 0x0FFFFFFC,
284 .dma_dram_mask
= 0x1FFFFFFC,
285 .nregs
= ASPEED_SMC_R_MAX
,
286 .segment_to_reg
= aspeed_smc_segment_to_reg
,
287 .reg_to_segment
= aspeed_smc_reg_to_segment
,
289 .name
= "aspeed.spi1-ast2400",
290 .r_conf
= R_SPI_CONF
,
292 .r_ctrl0
= R_SPI_CTRL0
,
293 .r_timings
= R_SPI_TIMINGS
,
295 .conf_enable_w0
= SPI_CONF_ENABLE_W0
,
297 .segments
= aspeed_segments_spi
,
298 .flash_window_base
= ASPEED_SOC_SPI_FLASH_BASE
,
299 .flash_window_size
= 0x10000000,
301 .nregs
= ASPEED_SMC_R_SPI_MAX
,
302 .segment_to_reg
= aspeed_smc_segment_to_reg
,
303 .reg_to_segment
= aspeed_smc_reg_to_segment
,
305 .name
= "aspeed.fmc-ast2500",
307 .r_ce_ctrl
= R_CE_CTRL
,
309 .r_timings
= R_TIMINGS
,
311 .conf_enable_w0
= CONF_ENABLE_W0
,
313 .segments
= aspeed_segments_ast2500_fmc
,
314 .flash_window_base
= ASPEED_SOC_FMC_FLASH_BASE
,
315 .flash_window_size
= 0x10000000,
317 .dma_flash_mask
= 0x0FFFFFFC,
318 .dma_dram_mask
= 0x3FFFFFFC,
319 .nregs
= ASPEED_SMC_R_MAX
,
320 .segment_to_reg
= aspeed_smc_segment_to_reg
,
321 .reg_to_segment
= aspeed_smc_reg_to_segment
,
323 .name
= "aspeed.spi1-ast2500",
325 .r_ce_ctrl
= R_CE_CTRL
,
327 .r_timings
= R_TIMINGS
,
329 .conf_enable_w0
= CONF_ENABLE_W0
,
331 .segments
= aspeed_segments_ast2500_spi1
,
332 .flash_window_base
= ASPEED_SOC_SPI_FLASH_BASE
,
333 .flash_window_size
= 0x8000000,
335 .nregs
= ASPEED_SMC_R_MAX
,
336 .segment_to_reg
= aspeed_smc_segment_to_reg
,
337 .reg_to_segment
= aspeed_smc_reg_to_segment
,
339 .name
= "aspeed.spi2-ast2500",
341 .r_ce_ctrl
= R_CE_CTRL
,
343 .r_timings
= R_TIMINGS
,
345 .conf_enable_w0
= CONF_ENABLE_W0
,
347 .segments
= aspeed_segments_ast2500_spi2
,
348 .flash_window_base
= ASPEED_SOC_SPI2_FLASH_BASE
,
349 .flash_window_size
= 0x8000000,
351 .nregs
= ASPEED_SMC_R_MAX
,
352 .segment_to_reg
= aspeed_smc_segment_to_reg
,
353 .reg_to_segment
= aspeed_smc_reg_to_segment
,
355 .name
= "aspeed.fmc-ast2600",
357 .r_ce_ctrl
= R_CE_CTRL
,
359 .r_timings
= R_TIMINGS
,
361 .conf_enable_w0
= CONF_ENABLE_W0
,
363 .segments
= aspeed_segments_ast2600_fmc
,
364 .flash_window_base
= ASPEED26_SOC_FMC_FLASH_BASE
,
365 .flash_window_size
= 0x10000000,
367 .nregs
= ASPEED_SMC_R_MAX
,
368 .segment_to_reg
= aspeed_2600_smc_segment_to_reg
,
369 .reg_to_segment
= aspeed_2600_smc_reg_to_segment
,
371 .name
= "aspeed.spi1-ast2600",
373 .r_ce_ctrl
= R_CE_CTRL
,
375 .r_timings
= R_TIMINGS
,
377 .conf_enable_w0
= CONF_ENABLE_W0
,
379 .segments
= aspeed_segments_ast2600_spi1
,
380 .flash_window_base
= ASPEED26_SOC_SPI_FLASH_BASE
,
381 .flash_window_size
= 0x10000000,
383 .nregs
= ASPEED_SMC_R_MAX
,
384 .segment_to_reg
= aspeed_2600_smc_segment_to_reg
,
385 .reg_to_segment
= aspeed_2600_smc_reg_to_segment
,
387 .name
= "aspeed.spi2-ast2600",
389 .r_ce_ctrl
= R_CE_CTRL
,
391 .r_timings
= R_TIMINGS
,
393 .conf_enable_w0
= CONF_ENABLE_W0
,
395 .segments
= aspeed_segments_ast2600_spi2
,
396 .flash_window_base
= ASPEED26_SOC_SPI2_FLASH_BASE
,
397 .flash_window_size
= 0x10000000,
399 .nregs
= ASPEED_SMC_R_MAX
,
400 .segment_to_reg
= aspeed_2600_smc_segment_to_reg
,
401 .reg_to_segment
= aspeed_2600_smc_reg_to_segment
,
406 * The Segment Registers of the AST2400 and AST2500 have a 8MB
407 * unit. The address range of a flash SPI slave is encoded with
408 * absolute addresses which should be part of the overall controller
411 static uint32_t aspeed_smc_segment_to_reg(const AspeedSMCState
*s
,
412 const AspeedSegments
*seg
)
415 reg
|= ((seg
->addr
>> 23) & SEG_START_MASK
) << SEG_START_SHIFT
;
416 reg
|= (((seg
->addr
+ seg
->size
) >> 23) & SEG_END_MASK
) << SEG_END_SHIFT
;
420 static void aspeed_smc_reg_to_segment(const AspeedSMCState
*s
,
421 uint32_t reg
, AspeedSegments
*seg
)
423 seg
->addr
= ((reg
>> SEG_START_SHIFT
) & SEG_START_MASK
) << 23;
424 seg
->size
= (((reg
>> SEG_END_SHIFT
) & SEG_END_MASK
) << 23) - seg
->addr
;
428 * The Segment Registers of the AST2600 have a 1MB unit. The address
429 * range of a flash SPI slave is encoded with offsets in the overall
430 * controller window. The previous SoC AST2400 and AST2500 used
431 * absolute addresses. Only bits [27:20] are relevant and the end
432 * address is an upper bound limit.
434 #define AST2600_SEG_ADDR_MASK 0x0ff00000
436 static uint32_t aspeed_2600_smc_segment_to_reg(const AspeedSMCState
*s
,
437 const AspeedSegments
*seg
)
441 /* Disabled segments have a nil register */
446 reg
|= (seg
->addr
& AST2600_SEG_ADDR_MASK
) >> 16; /* start offset */
447 reg
|= (seg
->addr
+ seg
->size
- 1) & AST2600_SEG_ADDR_MASK
; /* end offset */
451 static void aspeed_2600_smc_reg_to_segment(const AspeedSMCState
*s
,
452 uint32_t reg
, AspeedSegments
*seg
)
454 uint32_t start_offset
= (reg
<< 16) & AST2600_SEG_ADDR_MASK
;
455 uint32_t end_offset
= reg
& AST2600_SEG_ADDR_MASK
;
458 seg
->addr
= s
->ctrl
->flash_window_base
+ start_offset
;
459 seg
->size
= end_offset
+ MiB
- start_offset
;
461 seg
->addr
= s
->ctrl
->flash_window_base
;
466 static bool aspeed_smc_flash_overlap(const AspeedSMCState
*s
,
467 const AspeedSegments
*new,
473 for (i
= 0; i
< s
->ctrl
->max_slaves
; i
++) {
478 s
->ctrl
->reg_to_segment(s
, s
->regs
[R_SEG_ADDR0
+ i
], &seg
);
480 if (new->addr
+ new->size
> seg
.addr
&&
481 new->addr
< seg
.addr
+ seg
.size
) {
482 qemu_log_mask(LOG_GUEST_ERROR
, "%s: new segment CS%d [ 0x%"
483 HWADDR_PRIx
" - 0x%"HWADDR_PRIx
" ] overlaps with "
484 "CS%d [ 0x%"HWADDR_PRIx
" - 0x%"HWADDR_PRIx
" ]\n",
485 s
->ctrl
->name
, cs
, new->addr
, new->addr
+ new->size
,
486 i
, seg
.addr
, seg
.addr
+ seg
.size
);
493 static void aspeed_smc_flash_set_segment_region(AspeedSMCState
*s
, int cs
,
496 AspeedSMCFlash
*fl
= &s
->flashes
[cs
];
499 s
->ctrl
->reg_to_segment(s
, regval
, &seg
);
501 memory_region_transaction_begin();
502 memory_region_set_size(&fl
->mmio
, seg
.size
);
503 memory_region_set_address(&fl
->mmio
, seg
.addr
- s
->ctrl
->flash_window_base
);
504 memory_region_set_enabled(&fl
->mmio
, !!seg
.size
);
505 memory_region_transaction_commit();
507 s
->regs
[R_SEG_ADDR0
+ cs
] = regval
;
510 static void aspeed_smc_flash_set_segment(AspeedSMCState
*s
, int cs
,
515 s
->ctrl
->reg_to_segment(s
, new, &seg
);
517 trace_aspeed_smc_flash_set_segment(cs
, new, seg
.addr
, seg
.addr
+ seg
.size
);
519 /* The start address of CS0 is read-only */
520 if (cs
== 0 && seg
.addr
!= s
->ctrl
->flash_window_base
) {
521 qemu_log_mask(LOG_GUEST_ERROR
,
522 "%s: Tried to change CS0 start address to 0x%"
523 HWADDR_PRIx
"\n", s
->ctrl
->name
, seg
.addr
);
524 seg
.addr
= s
->ctrl
->flash_window_base
;
525 new = s
->ctrl
->segment_to_reg(s
, &seg
);
529 * The end address of the AST2500 spi controllers is also
532 if ((s
->ctrl
->segments
== aspeed_segments_ast2500_spi1
||
533 s
->ctrl
->segments
== aspeed_segments_ast2500_spi2
) &&
534 cs
== s
->ctrl
->max_slaves
&&
535 seg
.addr
+ seg
.size
!= s
->ctrl
->segments
[cs
].addr
+
536 s
->ctrl
->segments
[cs
].size
) {
537 qemu_log_mask(LOG_GUEST_ERROR
,
538 "%s: Tried to change CS%d end address to 0x%"
539 HWADDR_PRIx
"\n", s
->ctrl
->name
, cs
, seg
.addr
+ seg
.size
);
540 seg
.size
= s
->ctrl
->segments
[cs
].addr
+ s
->ctrl
->segments
[cs
].size
-
542 new = s
->ctrl
->segment_to_reg(s
, &seg
);
545 /* Keep the segment in the overall flash window */
547 (seg
.addr
+ seg
.size
<= s
->ctrl
->flash_window_base
||
548 seg
.addr
> s
->ctrl
->flash_window_base
+ s
->ctrl
->flash_window_size
)) {
549 qemu_log_mask(LOG_GUEST_ERROR
, "%s: new segment for CS%d is invalid : "
550 "[ 0x%"HWADDR_PRIx
" - 0x%"HWADDR_PRIx
" ]\n",
551 s
->ctrl
->name
, cs
, seg
.addr
, seg
.addr
+ seg
.size
);
555 /* Check start address vs. alignment */
556 if (seg
.size
&& !QEMU_IS_ALIGNED(seg
.addr
, seg
.size
)) {
557 qemu_log_mask(LOG_GUEST_ERROR
, "%s: new segment for CS%d is not "
558 "aligned : [ 0x%"HWADDR_PRIx
" - 0x%"HWADDR_PRIx
" ]\n",
559 s
->ctrl
->name
, cs
, seg
.addr
, seg
.addr
+ seg
.size
);
562 /* And segments should not overlap (in the specs) */
563 aspeed_smc_flash_overlap(s
, &seg
, cs
);
565 /* All should be fine now to move the region */
566 aspeed_smc_flash_set_segment_region(s
, cs
, new);
569 static uint64_t aspeed_smc_flash_default_read(void *opaque
, hwaddr addr
,
572 qemu_log_mask(LOG_GUEST_ERROR
, "%s: To 0x%" HWADDR_PRIx
" of size %u"
573 PRIx64
"\n", __func__
, addr
, size
);
577 static void aspeed_smc_flash_default_write(void *opaque
, hwaddr addr
,
578 uint64_t data
, unsigned size
)
580 qemu_log_mask(LOG_GUEST_ERROR
, "%s: To 0x%" HWADDR_PRIx
" of size %u: 0x%"
581 PRIx64
"\n", __func__
, addr
, size
, data
);
584 static const MemoryRegionOps aspeed_smc_flash_default_ops
= {
585 .read
= aspeed_smc_flash_default_read
,
586 .write
= aspeed_smc_flash_default_write
,
587 .endianness
= DEVICE_LITTLE_ENDIAN
,
589 .min_access_size
= 1,
590 .max_access_size
= 4,
594 static inline int aspeed_smc_flash_mode(const AspeedSMCFlash
*fl
)
596 const AspeedSMCState
*s
= fl
->controller
;
598 return s
->regs
[s
->r_ctrl0
+ fl
->id
] & CTRL_CMD_MODE_MASK
;
601 static inline bool aspeed_smc_is_writable(const AspeedSMCFlash
*fl
)
603 const AspeedSMCState
*s
= fl
->controller
;
605 return s
->regs
[s
->r_conf
] & (1 << (s
->conf_enable_w0
+ fl
->id
));
608 static inline int aspeed_smc_flash_cmd(const AspeedSMCFlash
*fl
)
610 const AspeedSMCState
*s
= fl
->controller
;
611 int cmd
= (s
->regs
[s
->r_ctrl0
+ fl
->id
] >> CTRL_CMD_SHIFT
) & CTRL_CMD_MASK
;
614 * In read mode, the default SPI command is READ (0x3). In other
615 * modes, the command should necessarily be defined
617 * TODO: add support for READ4 (0x13) on AST2600
619 if (aspeed_smc_flash_mode(fl
) == CTRL_READMODE
) {
624 qemu_log_mask(LOG_GUEST_ERROR
, "%s: no command defined for mode %d\n",
625 __func__
, aspeed_smc_flash_mode(fl
));
631 static inline int aspeed_smc_flash_is_4byte(const AspeedSMCFlash
*fl
)
633 const AspeedSMCState
*s
= fl
->controller
;
635 if (s
->ctrl
->segments
== aspeed_segments_spi
) {
636 return s
->regs
[s
->r_ctrl0
] & CTRL_AST2400_SPI_4BYTE
;
638 return s
->regs
[s
->r_ce_ctrl
] & (1 << (CTRL_EXTENDED0
+ fl
->id
));
642 static void aspeed_smc_flash_do_select(AspeedSMCFlash
*fl
, bool unselect
)
644 AspeedSMCState
*s
= fl
->controller
;
646 trace_aspeed_smc_flash_select(fl
->id
, unselect
? "un" : "");
648 qemu_set_irq(s
->cs_lines
[fl
->id
], unselect
);
651 static void aspeed_smc_flash_select(AspeedSMCFlash
*fl
)
653 aspeed_smc_flash_do_select(fl
, false);
656 static void aspeed_smc_flash_unselect(AspeedSMCFlash
*fl
)
658 aspeed_smc_flash_do_select(fl
, true);
661 static uint32_t aspeed_smc_check_segment_addr(const AspeedSMCFlash
*fl
,
664 const AspeedSMCState
*s
= fl
->controller
;
667 s
->ctrl
->reg_to_segment(s
, s
->regs
[R_SEG_ADDR0
+ fl
->id
], &seg
);
668 if ((addr
% seg
.size
) != addr
) {
669 qemu_log_mask(LOG_GUEST_ERROR
,
670 "%s: invalid address 0x%08x for CS%d segment : "
671 "[ 0x%"HWADDR_PRIx
" - 0x%"HWADDR_PRIx
" ]\n",
672 s
->ctrl
->name
, addr
, fl
->id
, seg
.addr
,
673 seg
.addr
+ seg
.size
);
680 static int aspeed_smc_flash_dummies(const AspeedSMCFlash
*fl
)
682 const AspeedSMCState
*s
= fl
->controller
;
683 uint32_t r_ctrl0
= s
->regs
[s
->r_ctrl0
+ fl
->id
];
684 uint32_t dummy_high
= (r_ctrl0
>> CTRL_DUMMY_HIGH_SHIFT
) & 0x1;
685 uint32_t dummy_low
= (r_ctrl0
>> CTRL_DUMMY_LOW_SHIFT
) & 0x3;
686 uint32_t dummies
= ((dummy_high
<< 2) | dummy_low
) * 8;
688 if (r_ctrl0
& CTRL_IO_DUAL_ADDR_DATA
) {
695 static void aspeed_smc_flash_setup(AspeedSMCFlash
*fl
, uint32_t addr
)
697 const AspeedSMCState
*s
= fl
->controller
;
698 uint8_t cmd
= aspeed_smc_flash_cmd(fl
);
701 /* Flash access can not exceed CS segment */
702 addr
= aspeed_smc_check_segment_addr(fl
, addr
);
704 ssi_transfer(s
->spi
, cmd
);
706 if (aspeed_smc_flash_is_4byte(fl
)) {
707 ssi_transfer(s
->spi
, (addr
>> 24) & 0xff);
709 ssi_transfer(s
->spi
, (addr
>> 16) & 0xff);
710 ssi_transfer(s
->spi
, (addr
>> 8) & 0xff);
711 ssi_transfer(s
->spi
, (addr
& 0xff));
714 * Use fake transfers to model dummy bytes. The value should
715 * be configured to some non-zero value in fast read mode and
716 * zero in read mode. But, as the HW allows inconsistent
717 * settings, let's check for fast read mode.
719 if (aspeed_smc_flash_mode(fl
) == CTRL_FREADMODE
) {
720 for (i
= 0; i
< aspeed_smc_flash_dummies(fl
); i
++) {
721 ssi_transfer(fl
->controller
->spi
, s
->regs
[R_DUMMY_DATA
] & 0xff);
726 static uint64_t aspeed_smc_flash_read(void *opaque
, hwaddr addr
, unsigned size
)
728 AspeedSMCFlash
*fl
= opaque
;
729 AspeedSMCState
*s
= fl
->controller
;
733 switch (aspeed_smc_flash_mode(fl
)) {
735 for (i
= 0; i
< size
; i
++) {
736 ret
|= ssi_transfer(s
->spi
, 0x0) << (8 * i
);
741 aspeed_smc_flash_select(fl
);
742 aspeed_smc_flash_setup(fl
, addr
);
744 for (i
= 0; i
< size
; i
++) {
745 ret
|= ssi_transfer(s
->spi
, 0x0) << (8 * i
);
748 aspeed_smc_flash_unselect(fl
);
751 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid flash mode %d\n",
752 __func__
, aspeed_smc_flash_mode(fl
));
755 trace_aspeed_smc_flash_read(fl
->id
, addr
, size
, ret
,
756 aspeed_smc_flash_mode(fl
));
761 * TODO (clg@kaod.org): stolen from xilinx_spips.c. Should move to a
762 * common include header.
765 READ
= 0x3, READ_4
= 0x13,
766 FAST_READ
= 0xb, FAST_READ_4
= 0x0c,
767 DOR
= 0x3b, DOR_4
= 0x3c,
768 QOR
= 0x6b, QOR_4
= 0x6c,
769 DIOR
= 0xbb, DIOR_4
= 0xbc,
770 QIOR
= 0xeb, QIOR_4
= 0xec,
772 PP
= 0x2, PP_4
= 0x12,
774 QPP
= 0x32, QPP_4
= 0x34,
777 static int aspeed_smc_num_dummies(uint8_t command
)
779 switch (command
) { /* check for dummies */
780 case READ
: /* no dummy bytes/cycles */
806 static bool aspeed_smc_do_snoop(AspeedSMCFlash
*fl
, uint64_t data
,
809 AspeedSMCState
*s
= fl
->controller
;
810 uint8_t addr_width
= aspeed_smc_flash_is_4byte(fl
) ? 4 : 3;
812 trace_aspeed_smc_do_snoop(fl
->id
, s
->snoop_index
, s
->snoop_dummies
,
813 (uint8_t) data
& 0xff);
815 if (s
->snoop_index
== SNOOP_OFF
) {
816 return false; /* Do nothing */
818 } else if (s
->snoop_index
== SNOOP_START
) {
819 uint8_t cmd
= data
& 0xff;
820 int ndummies
= aspeed_smc_num_dummies(cmd
);
823 * No dummy cycles are expected with the current command. Turn
824 * off snooping and let the transfer proceed normally.
827 s
->snoop_index
= SNOOP_OFF
;
831 s
->snoop_dummies
= ndummies
* 8;
833 } else if (s
->snoop_index
>= addr_width
+ 1) {
835 /* The SPI transfer has reached the dummy cycles sequence */
836 for (; s
->snoop_dummies
; s
->snoop_dummies
--) {
837 ssi_transfer(s
->spi
, s
->regs
[R_DUMMY_DATA
] & 0xff);
840 /* If no more dummy cycles are expected, turn off snooping */
841 if (!s
->snoop_dummies
) {
842 s
->snoop_index
= SNOOP_OFF
;
844 s
->snoop_index
+= size
;
848 * Dummy cycles have been faked already. Ignore the current
854 s
->snoop_index
+= size
;
858 static void aspeed_smc_flash_write(void *opaque
, hwaddr addr
, uint64_t data
,
861 AspeedSMCFlash
*fl
= opaque
;
862 AspeedSMCState
*s
= fl
->controller
;
865 trace_aspeed_smc_flash_write(fl
->id
, addr
, size
, data
,
866 aspeed_smc_flash_mode(fl
));
868 if (!aspeed_smc_is_writable(fl
)) {
869 qemu_log_mask(LOG_GUEST_ERROR
, "%s: flash is not writable at 0x%"
870 HWADDR_PRIx
"\n", __func__
, addr
);
874 switch (aspeed_smc_flash_mode(fl
)) {
876 if (aspeed_smc_do_snoop(fl
, data
, size
)) {
880 for (i
= 0; i
< size
; i
++) {
881 ssi_transfer(s
->spi
, (data
>> (8 * i
)) & 0xff);
885 aspeed_smc_flash_select(fl
);
886 aspeed_smc_flash_setup(fl
, addr
);
888 for (i
= 0; i
< size
; i
++) {
889 ssi_transfer(s
->spi
, (data
>> (8 * i
)) & 0xff);
892 aspeed_smc_flash_unselect(fl
);
895 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid flash mode %d\n",
896 __func__
, aspeed_smc_flash_mode(fl
));
900 static const MemoryRegionOps aspeed_smc_flash_ops
= {
901 .read
= aspeed_smc_flash_read
,
902 .write
= aspeed_smc_flash_write
,
903 .endianness
= DEVICE_LITTLE_ENDIAN
,
905 .min_access_size
= 1,
906 .max_access_size
= 4,
910 static void aspeed_smc_flash_update_ctrl(AspeedSMCFlash
*fl
, uint32_t value
)
912 AspeedSMCState
*s
= fl
->controller
;
915 /* User mode selects the CS, other modes unselect */
916 unselect
= (value
& CTRL_CMD_MODE_MASK
) != CTRL_USERMODE
;
918 /* A change of CTRL_CE_STOP_ACTIVE from 0 to 1, unselects the CS */
919 if (!(s
->regs
[s
->r_ctrl0
+ fl
->id
] & CTRL_CE_STOP_ACTIVE
) &&
920 value
& CTRL_CE_STOP_ACTIVE
) {
924 s
->regs
[s
->r_ctrl0
+ fl
->id
] = value
;
926 s
->snoop_index
= unselect
? SNOOP_OFF
: SNOOP_START
;
928 aspeed_smc_flash_do_select(fl
, unselect
);
931 static void aspeed_smc_reset(DeviceState
*d
)
933 AspeedSMCState
*s
= ASPEED_SMC(d
);
936 memset(s
->regs
, 0, sizeof s
->regs
);
938 /* Unselect all slaves */
939 for (i
= 0; i
< s
->num_cs
; ++i
) {
940 s
->regs
[s
->r_ctrl0
+ i
] |= CTRL_CE_STOP_ACTIVE
;
941 qemu_set_irq(s
->cs_lines
[i
], true);
944 /* setup the default segment register values and regions for all */
945 for (i
= 0; i
< s
->ctrl
->max_slaves
; ++i
) {
946 aspeed_smc_flash_set_segment_region(s
, i
,
947 s
->ctrl
->segment_to_reg(s
, &s
->ctrl
->segments
[i
]));
950 /* HW strapping flash type for the AST2600 controllers */
951 if (s
->ctrl
->segments
== aspeed_segments_ast2600_fmc
) {
952 /* flash type is fixed to SPI for all */
953 s
->regs
[s
->r_conf
] |= (CONF_FLASH_TYPE_SPI
<< CONF_FLASH_TYPE0
);
954 s
->regs
[s
->r_conf
] |= (CONF_FLASH_TYPE_SPI
<< CONF_FLASH_TYPE1
);
955 s
->regs
[s
->r_conf
] |= (CONF_FLASH_TYPE_SPI
<< CONF_FLASH_TYPE2
);
958 /* HW strapping flash type for FMC controllers */
959 if (s
->ctrl
->segments
== aspeed_segments_ast2500_fmc
) {
960 /* flash type is fixed to SPI for CE0 and CE1 */
961 s
->regs
[s
->r_conf
] |= (CONF_FLASH_TYPE_SPI
<< CONF_FLASH_TYPE0
);
962 s
->regs
[s
->r_conf
] |= (CONF_FLASH_TYPE_SPI
<< CONF_FLASH_TYPE1
);
965 /* HW strapping for AST2400 FMC controllers (SCU70). Let's use the
966 * configuration of the palmetto-bmc machine */
967 if (s
->ctrl
->segments
== aspeed_segments_fmc
) {
968 s
->regs
[s
->r_conf
] |= (CONF_FLASH_TYPE_SPI
<< CONF_FLASH_TYPE0
);
971 s
->snoop_index
= SNOOP_OFF
;
972 s
->snoop_dummies
= 0;
975 static uint64_t aspeed_smc_read(void *opaque
, hwaddr addr
, unsigned int size
)
977 AspeedSMCState
*s
= ASPEED_SMC(opaque
);
981 if (addr
== s
->r_conf
||
982 (addr
>= s
->r_timings
&&
983 addr
< s
->r_timings
+ s
->ctrl
->nregs_timings
) ||
984 addr
== s
->r_ce_ctrl
||
985 addr
== R_INTR_CTRL
||
986 addr
== R_DUMMY_DATA
||
987 (s
->ctrl
->has_dma
&& addr
== R_DMA_CTRL
) ||
988 (s
->ctrl
->has_dma
&& addr
== R_DMA_FLASH_ADDR
) ||
989 (s
->ctrl
->has_dma
&& addr
== R_DMA_DRAM_ADDR
) ||
990 (s
->ctrl
->has_dma
&& addr
== R_DMA_LEN
) ||
991 (s
->ctrl
->has_dma
&& addr
== R_DMA_CHECKSUM
) ||
992 (addr
>= R_SEG_ADDR0
&& addr
< R_SEG_ADDR0
+ s
->ctrl
->max_slaves
) ||
993 (addr
>= s
->r_ctrl0
&& addr
< s
->r_ctrl0
+ s
->ctrl
->max_slaves
)) {
995 trace_aspeed_smc_read(addr
, size
, s
->regs
[addr
]);
997 return s
->regs
[addr
];
999 qemu_log_mask(LOG_UNIMP
, "%s: not implemented: 0x%" HWADDR_PRIx
"\n",
1005 static uint8_t aspeed_smc_hclk_divisor(uint8_t hclk_mask
)
1007 /* HCLK/1 .. HCLK/16 */
1008 const uint8_t hclk_divisors
[] = {
1009 15, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 0
1013 for (i
= 0; i
< ARRAY_SIZE(hclk_divisors
); i
++) {
1014 if (hclk_mask
== hclk_divisors
[i
]) {
1019 qemu_log_mask(LOG_GUEST_ERROR
, "invalid HCLK mask %x", hclk_mask
);
1024 * When doing calibration, the SPI clock rate in the CE0 Control
1025 * Register and the read delay cycles in the Read Timing Compensation
1026 * Register are set using bit[11:4] of the DMA Control Register.
1028 static void aspeed_smc_dma_calibration(AspeedSMCState
*s
)
1031 (s
->regs
[R_DMA_CTRL
] >> DMA_CTRL_DELAY_SHIFT
) & DMA_CTRL_DELAY_MASK
;
1033 (s
->regs
[R_DMA_CTRL
] >> DMA_CTRL_FREQ_SHIFT
) & DMA_CTRL_FREQ_MASK
;
1034 uint8_t hclk_div
= aspeed_smc_hclk_divisor(hclk_mask
);
1035 uint32_t hclk_shift
= (hclk_div
- 1) << 2;
1039 * The Read Timing Compensation Register values apply to all CS on
1040 * the SPI bus and only HCLK/1 - HCLK/5 can have tunable delays
1042 if (hclk_div
&& hclk_div
< 6) {
1043 s
->regs
[s
->r_timings
] &= ~(0xf << hclk_shift
);
1044 s
->regs
[s
->r_timings
] |= delay
<< hclk_shift
;
1048 * TODO: compute the CS from the DMA address and the segment
1049 * registers. This is not really a problem for now because the
1050 * Timing Register values apply to all CS and software uses CS0 to
1054 s
->regs
[s
->r_ctrl0
+ cs
] &=
1055 ~(CE_CTRL_CLOCK_FREQ_MASK
<< CE_CTRL_CLOCK_FREQ_SHIFT
);
1056 s
->regs
[s
->r_ctrl0
+ cs
] |= CE_CTRL_CLOCK_FREQ(hclk_div
);
1060 * Emulate read errors in the DMA Checksum Register for high
1061 * frequencies and optimistic settings of the Read Timing Compensation
1062 * Register. This will help in tuning the SPI timing calibration
1065 static bool aspeed_smc_inject_read_failure(AspeedSMCState
*s
)
1068 (s
->regs
[R_DMA_CTRL
] >> DMA_CTRL_DELAY_SHIFT
) & DMA_CTRL_DELAY_MASK
;
1070 (s
->regs
[R_DMA_CTRL
] >> DMA_CTRL_FREQ_SHIFT
) & DMA_CTRL_FREQ_MASK
;
1073 * Typical values of a palmetto-bmc machine.
1075 switch (aspeed_smc_hclk_divisor(hclk_mask
)) {
1078 case 3: /* at least one HCLK cycle delay */
1079 return (delay
& 0x7) < 1;
1080 case 2: /* at least two HCLK cycle delay */
1081 return (delay
& 0x7) < 2;
1082 case 1: /* (> 100MHz) is above the max freq of the controller */
1085 g_assert_not_reached();
1090 * Accumulate the result of the reads to provide a checksum that will
1091 * be used to validate the read timing settings.
1093 static void aspeed_smc_dma_checksum(AspeedSMCState
*s
)
1098 if (s
->regs
[R_DMA_CTRL
] & DMA_CTRL_WRITE
) {
1099 qemu_log_mask(LOG_GUEST_ERROR
,
1100 "%s: invalid direction for DMA checksum\n", __func__
);
1104 if (s
->regs
[R_DMA_CTRL
] & DMA_CTRL_CALIB
) {
1105 aspeed_smc_dma_calibration(s
);
1108 while (s
->regs
[R_DMA_LEN
]) {
1109 data
= address_space_ldl_le(&s
->flash_as
, s
->regs
[R_DMA_FLASH_ADDR
],
1110 MEMTXATTRS_UNSPECIFIED
, &result
);
1111 if (result
!= MEMTX_OK
) {
1112 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Flash read failed @%08x\n",
1113 __func__
, s
->regs
[R_DMA_FLASH_ADDR
]);
1116 trace_aspeed_smc_dma_checksum(s
->regs
[R_DMA_FLASH_ADDR
], data
);
1119 * When the DMA is on-going, the DMA registers are updated
1120 * with the current working addresses and length.
1122 s
->regs
[R_DMA_CHECKSUM
] += data
;
1123 s
->regs
[R_DMA_FLASH_ADDR
] += 4;
1124 s
->regs
[R_DMA_LEN
] -= 4;
1127 if (s
->inject_failure
&& aspeed_smc_inject_read_failure(s
)) {
1128 s
->regs
[R_DMA_CHECKSUM
] = 0xbadc0de;
1133 static void aspeed_smc_dma_rw(AspeedSMCState
*s
)
1138 while (s
->regs
[R_DMA_LEN
]) {
1139 if (s
->regs
[R_DMA_CTRL
] & DMA_CTRL_WRITE
) {
1140 data
= address_space_ldl_le(&s
->dram_as
, s
->regs
[R_DMA_DRAM_ADDR
],
1141 MEMTXATTRS_UNSPECIFIED
, &result
);
1142 if (result
!= MEMTX_OK
) {
1143 qemu_log_mask(LOG_GUEST_ERROR
, "%s: DRAM read failed @%08x\n",
1144 __func__
, s
->regs
[R_DMA_DRAM_ADDR
]);
1148 address_space_stl_le(&s
->flash_as
, s
->regs
[R_DMA_FLASH_ADDR
],
1149 data
, MEMTXATTRS_UNSPECIFIED
, &result
);
1150 if (result
!= MEMTX_OK
) {
1151 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Flash write failed @%08x\n",
1152 __func__
, s
->regs
[R_DMA_FLASH_ADDR
]);
1156 data
= address_space_ldl_le(&s
->flash_as
, s
->regs
[R_DMA_FLASH_ADDR
],
1157 MEMTXATTRS_UNSPECIFIED
, &result
);
1158 if (result
!= MEMTX_OK
) {
1159 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Flash read failed @%08x\n",
1160 __func__
, s
->regs
[R_DMA_FLASH_ADDR
]);
1164 address_space_stl_le(&s
->dram_as
, s
->regs
[R_DMA_DRAM_ADDR
],
1165 data
, MEMTXATTRS_UNSPECIFIED
, &result
);
1166 if (result
!= MEMTX_OK
) {
1167 qemu_log_mask(LOG_GUEST_ERROR
, "%s: DRAM write failed @%08x\n",
1168 __func__
, s
->regs
[R_DMA_DRAM_ADDR
]);
1174 * When the DMA is on-going, the DMA registers are updated
1175 * with the current working addresses and length.
1177 s
->regs
[R_DMA_FLASH_ADDR
] += 4;
1178 s
->regs
[R_DMA_DRAM_ADDR
] += 4;
1179 s
->regs
[R_DMA_LEN
] -= 4;
1180 s
->regs
[R_DMA_CHECKSUM
] += data
;
1184 static void aspeed_smc_dma_stop(AspeedSMCState
*s
)
1187 * When the DMA is disabled, INTR_CTRL_DMA_STATUS=0 means the
1190 s
->regs
[R_INTR_CTRL
] &= ~INTR_CTRL_DMA_STATUS
;
1191 s
->regs
[R_DMA_CHECKSUM
] = 0;
1194 * Lower the DMA irq in any case. The IRQ control register could
1195 * have been cleared before disabling the DMA.
1197 qemu_irq_lower(s
->irq
);
1201 * When INTR_CTRL_DMA_STATUS=1, the DMA has completed and a new DMA
1202 * can start even if the result of the previous was not collected.
1204 static bool aspeed_smc_dma_in_progress(AspeedSMCState
*s
)
1206 return s
->regs
[R_DMA_CTRL
] & DMA_CTRL_ENABLE
&&
1207 !(s
->regs
[R_INTR_CTRL
] & INTR_CTRL_DMA_STATUS
);
1210 static void aspeed_smc_dma_done(AspeedSMCState
*s
)
1212 s
->regs
[R_INTR_CTRL
] |= INTR_CTRL_DMA_STATUS
;
1213 if (s
->regs
[R_INTR_CTRL
] & INTR_CTRL_DMA_EN
) {
1214 qemu_irq_raise(s
->irq
);
1218 static void aspeed_smc_dma_ctrl(AspeedSMCState
*s
, uint64_t dma_ctrl
)
1220 if (!(dma_ctrl
& DMA_CTRL_ENABLE
)) {
1221 s
->regs
[R_DMA_CTRL
] = dma_ctrl
;
1223 aspeed_smc_dma_stop(s
);
1227 if (aspeed_smc_dma_in_progress(s
)) {
1228 qemu_log_mask(LOG_GUEST_ERROR
, "%s: DMA in progress\n", __func__
);
1232 s
->regs
[R_DMA_CTRL
] = dma_ctrl
;
1234 if (s
->regs
[R_DMA_CTRL
] & DMA_CTRL_CKSUM
) {
1235 aspeed_smc_dma_checksum(s
);
1237 aspeed_smc_dma_rw(s
);
1240 aspeed_smc_dma_done(s
);
1243 static void aspeed_smc_write(void *opaque
, hwaddr addr
, uint64_t data
,
1246 AspeedSMCState
*s
= ASPEED_SMC(opaque
);
1247 uint32_t value
= data
;
1251 trace_aspeed_smc_write(addr
, size
, data
);
1253 if (addr
== s
->r_conf
||
1254 (addr
>= s
->r_timings
&&
1255 addr
< s
->r_timings
+ s
->ctrl
->nregs_timings
) ||
1256 addr
== s
->r_ce_ctrl
) {
1257 s
->regs
[addr
] = value
;
1258 } else if (addr
>= s
->r_ctrl0
&& addr
< s
->r_ctrl0
+ s
->num_cs
) {
1259 int cs
= addr
- s
->r_ctrl0
;
1260 aspeed_smc_flash_update_ctrl(&s
->flashes
[cs
], value
);
1261 } else if (addr
>= R_SEG_ADDR0
&&
1262 addr
< R_SEG_ADDR0
+ s
->ctrl
->max_slaves
) {
1263 int cs
= addr
- R_SEG_ADDR0
;
1265 if (value
!= s
->regs
[R_SEG_ADDR0
+ cs
]) {
1266 aspeed_smc_flash_set_segment(s
, cs
, value
);
1268 } else if (addr
== R_DUMMY_DATA
) {
1269 s
->regs
[addr
] = value
& 0xff;
1270 } else if (addr
== R_INTR_CTRL
) {
1271 s
->regs
[addr
] = value
;
1272 } else if (s
->ctrl
->has_dma
&& addr
== R_DMA_CTRL
) {
1273 aspeed_smc_dma_ctrl(s
, value
);
1274 } else if (s
->ctrl
->has_dma
&& addr
== R_DMA_DRAM_ADDR
) {
1275 s
->regs
[addr
] = DMA_DRAM_ADDR(s
, value
);
1276 } else if (s
->ctrl
->has_dma
&& addr
== R_DMA_FLASH_ADDR
) {
1277 s
->regs
[addr
] = DMA_FLASH_ADDR(s
, value
);
1278 } else if (s
->ctrl
->has_dma
&& addr
== R_DMA_LEN
) {
1279 s
->regs
[addr
] = DMA_LENGTH(value
);
1281 qemu_log_mask(LOG_UNIMP
, "%s: not implemented: 0x%" HWADDR_PRIx
"\n",
1287 static const MemoryRegionOps aspeed_smc_ops
= {
1288 .read
= aspeed_smc_read
,
1289 .write
= aspeed_smc_write
,
1290 .endianness
= DEVICE_LITTLE_ENDIAN
,
1291 .valid
.unaligned
= true,
1296 * Initialize the custom address spaces for DMAs
1298 static void aspeed_smc_dma_setup(AspeedSMCState
*s
, Error
**errp
)
1303 error_setg(errp
, TYPE_ASPEED_SMC
": 'dram' link not set");
1307 name
= g_strdup_printf("%s-dma-flash", s
->ctrl
->name
);
1308 address_space_init(&s
->flash_as
, &s
->mmio_flash
, name
);
1311 name
= g_strdup_printf("%s-dma-dram", s
->ctrl
->name
);
1312 address_space_init(&s
->dram_as
, s
->dram_mr
, name
);
1316 static void aspeed_smc_realize(DeviceState
*dev
, Error
**errp
)
1318 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1319 AspeedSMCState
*s
= ASPEED_SMC(dev
);
1320 AspeedSMCClass
*mc
= ASPEED_SMC_GET_CLASS(s
);
1327 /* keep a copy under AspeedSMCState to speed up accesses */
1328 s
->r_conf
= s
->ctrl
->r_conf
;
1329 s
->r_ce_ctrl
= s
->ctrl
->r_ce_ctrl
;
1330 s
->r_ctrl0
= s
->ctrl
->r_ctrl0
;
1331 s
->r_timings
= s
->ctrl
->r_timings
;
1332 s
->conf_enable_w0
= s
->ctrl
->conf_enable_w0
;
1334 /* Enforce some real HW limits */
1335 if (s
->num_cs
> s
->ctrl
->max_slaves
) {
1336 qemu_log_mask(LOG_GUEST_ERROR
, "%s: num_cs cannot exceed: %d\n",
1337 __func__
, s
->ctrl
->max_slaves
);
1338 s
->num_cs
= s
->ctrl
->max_slaves
;
1341 /* DMA irq. Keep it first for the initialization in the SoC */
1342 sysbus_init_irq(sbd
, &s
->irq
);
1344 s
->spi
= ssi_create_bus(dev
, "spi");
1346 /* Setup cs_lines for slaves */
1347 s
->cs_lines
= g_new0(qemu_irq
, s
->num_cs
);
1348 ssi_auto_connect_slaves(dev
, s
->cs_lines
, s
->spi
);
1350 for (i
= 0; i
< s
->num_cs
; ++i
) {
1351 sysbus_init_irq(sbd
, &s
->cs_lines
[i
]);
1354 /* The memory region for the controller registers */
1355 memory_region_init_io(&s
->mmio
, OBJECT(s
), &aspeed_smc_ops
, s
,
1356 s
->ctrl
->name
, s
->ctrl
->nregs
* 4);
1357 sysbus_init_mmio(sbd
, &s
->mmio
);
1360 * The container memory region representing the address space
1361 * window in which the flash modules are mapped. The size and
1362 * address depends on the SoC model and controller type.
1364 snprintf(name
, sizeof(name
), "%s.flash", s
->ctrl
->name
);
1366 memory_region_init_io(&s
->mmio_flash
, OBJECT(s
),
1367 &aspeed_smc_flash_default_ops
, s
, name
,
1368 s
->ctrl
->flash_window_size
);
1369 sysbus_init_mmio(sbd
, &s
->mmio_flash
);
1371 s
->flashes
= g_new0(AspeedSMCFlash
, s
->ctrl
->max_slaves
);
1374 * Let's create a sub memory region for each possible slave. All
1375 * have a configurable memory segment in the overall flash mapping
1376 * window of the controller but, there is not necessarily a flash
1377 * module behind to handle the memory accesses. This depends on
1378 * the board configuration.
1380 for (i
= 0; i
< s
->ctrl
->max_slaves
; ++i
) {
1381 AspeedSMCFlash
*fl
= &s
->flashes
[i
];
1383 snprintf(name
, sizeof(name
), "%s.%d", s
->ctrl
->name
, i
);
1387 fl
->size
= s
->ctrl
->segments
[i
].size
;
1388 memory_region_init_io(&fl
->mmio
, OBJECT(s
), &aspeed_smc_flash_ops
,
1389 fl
, name
, fl
->size
);
1390 memory_region_add_subregion(&s
->mmio_flash
, offset
, &fl
->mmio
);
1395 if (s
->ctrl
->has_dma
) {
1396 aspeed_smc_dma_setup(s
, errp
);
1400 static const VMStateDescription vmstate_aspeed_smc
= {
1401 .name
= "aspeed.smc",
1403 .minimum_version_id
= 2,
1404 .fields
= (VMStateField
[]) {
1405 VMSTATE_UINT32_ARRAY(regs
, AspeedSMCState
, ASPEED_SMC_R_MAX
),
1406 VMSTATE_UINT8(snoop_index
, AspeedSMCState
),
1407 VMSTATE_UINT8(snoop_dummies
, AspeedSMCState
),
1408 VMSTATE_END_OF_LIST()
1412 static Property aspeed_smc_properties
[] = {
1413 DEFINE_PROP_UINT32("num-cs", AspeedSMCState
, num_cs
, 1),
1414 DEFINE_PROP_BOOL("inject-failure", AspeedSMCState
, inject_failure
, false),
1415 DEFINE_PROP_UINT64("sdram-base", AspeedSMCState
, sdram_base
, 0),
1416 DEFINE_PROP_LINK("dram", AspeedSMCState
, dram_mr
,
1417 TYPE_MEMORY_REGION
, MemoryRegion
*),
1418 DEFINE_PROP_END_OF_LIST(),
1421 static void aspeed_smc_class_init(ObjectClass
*klass
, void *data
)
1423 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1424 AspeedSMCClass
*mc
= ASPEED_SMC_CLASS(klass
);
1426 dc
->realize
= aspeed_smc_realize
;
1427 dc
->reset
= aspeed_smc_reset
;
1428 device_class_set_props(dc
, aspeed_smc_properties
);
1429 dc
->vmsd
= &vmstate_aspeed_smc
;
1433 static const TypeInfo aspeed_smc_info
= {
1434 .name
= TYPE_ASPEED_SMC
,
1435 .parent
= TYPE_SYS_BUS_DEVICE
,
1436 .instance_size
= sizeof(AspeedSMCState
),
1437 .class_size
= sizeof(AspeedSMCClass
),
1441 static void aspeed_smc_register_types(void)
1445 type_register_static(&aspeed_smc_info
);
1446 for (i
= 0; i
< ARRAY_SIZE(controllers
); ++i
) {
1448 .name
= controllers
[i
].name
,
1449 .parent
= TYPE_ASPEED_SMC
,
1450 .class_init
= aspeed_smc_class_init
,
1451 .class_data
= (void *)&controllers
[i
],
1457 type_init(aspeed_smc_register_types
)