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 "sysemu/sysemu.h"
29 #include "include/qemu/error-report.h"
30 #include "exec/address-spaces.h"
32 #include "hw/ssi/aspeed_smc.h"
34 /* CE Type Setting Register */
35 #define R_CONF (0x00 / 4)
36 #define CONF_LEGACY_DISABLE (1 << 31)
37 #define CONF_ENABLE_W4 20
38 #define CONF_ENABLE_W3 19
39 #define CONF_ENABLE_W2 18
40 #define CONF_ENABLE_W1 17
41 #define CONF_ENABLE_W0 16
42 #define CONF_FLASH_TYPE4 8
43 #define CONF_FLASH_TYPE3 6
44 #define CONF_FLASH_TYPE2 4
45 #define CONF_FLASH_TYPE1 2
46 #define CONF_FLASH_TYPE0 0
47 #define CONF_FLASH_TYPE_NOR 0x0
48 #define CONF_FLASH_TYPE_NAND 0x1
49 #define CONF_FLASH_TYPE_SPI 0x2
51 /* CE Control Register */
52 #define R_CE_CTRL (0x04 / 4)
53 #define CTRL_EXTENDED4 4 /* 32 bit addressing for SPI */
54 #define CTRL_EXTENDED3 3 /* 32 bit addressing for SPI */
55 #define CTRL_EXTENDED2 2 /* 32 bit addressing for SPI */
56 #define CTRL_EXTENDED1 1 /* 32 bit addressing for SPI */
57 #define CTRL_EXTENDED0 0 /* 32 bit addressing for SPI */
59 /* Interrupt Control and Status Register */
60 #define R_INTR_CTRL (0x08 / 4)
61 #define INTR_CTRL_DMA_STATUS (1 << 11)
62 #define INTR_CTRL_CMD_ABORT_STATUS (1 << 10)
63 #define INTR_CTRL_WRITE_PROTECT_STATUS (1 << 9)
64 #define INTR_CTRL_DMA_EN (1 << 3)
65 #define INTR_CTRL_CMD_ABORT_EN (1 << 2)
66 #define INTR_CTRL_WRITE_PROTECT_EN (1 << 1)
68 /* CEx Control Register */
69 #define R_CTRL0 (0x10 / 4)
70 #define CTRL_CMD_SHIFT 16
71 #define CTRL_CMD_MASK 0xff
72 #define CTRL_AST2400_SPI_4BYTE (1 << 13)
73 #define CTRL_CE_STOP_ACTIVE (1 << 2)
74 #define CTRL_CMD_MODE_MASK 0x3
75 #define CTRL_READMODE 0x0
76 #define CTRL_FREADMODE 0x1
77 #define CTRL_WRITEMODE 0x2
78 #define CTRL_USERMODE 0x3
79 #define R_CTRL1 (0x14 / 4)
80 #define R_CTRL2 (0x18 / 4)
81 #define R_CTRL3 (0x1C / 4)
82 #define R_CTRL4 (0x20 / 4)
84 /* CEx Segment Address Register */
85 #define R_SEG_ADDR0 (0x30 / 4)
86 #define SEG_END_SHIFT 24 /* 8MB units */
87 #define SEG_END_MASK 0xff
88 #define SEG_START_SHIFT 16 /* address bit [A29-A23] */
89 #define SEG_START_MASK 0xff
90 #define R_SEG_ADDR1 (0x34 / 4)
91 #define R_SEG_ADDR2 (0x38 / 4)
92 #define R_SEG_ADDR3 (0x3C / 4)
93 #define R_SEG_ADDR4 (0x40 / 4)
95 /* Misc Control Register #1 */
96 #define R_MISC_CTRL1 (0x50 / 4)
98 /* Misc Control Register #2 */
99 #define R_MISC_CTRL2 (0x54 / 4)
101 /* DMA Control/Status Register */
102 #define R_DMA_CTRL (0x80 / 4)
103 #define DMA_CTRL_DELAY_MASK 0xf
104 #define DMA_CTRL_DELAY_SHIFT 8
105 #define DMA_CTRL_FREQ_MASK 0xf
106 #define DMA_CTRL_FREQ_SHIFT 4
107 #define DMA_CTRL_MODE (1 << 3)
108 #define DMA_CTRL_CKSUM (1 << 2)
109 #define DMA_CTRL_DIR (1 << 1)
110 #define DMA_CTRL_EN (1 << 0)
112 /* DMA Flash Side Address */
113 #define R_DMA_FLASH_ADDR (0x84 / 4)
115 /* DMA DRAM Side Address */
116 #define R_DMA_DRAM_ADDR (0x88 / 4)
118 /* DMA Length Register */
119 #define R_DMA_LEN (0x8C / 4)
121 /* Checksum Calculation Result */
122 #define R_DMA_CHECKSUM (0x90 / 4)
124 /* Misc Control Register #2 */
125 #define R_TIMINGS (0x94 / 4)
127 /* SPI controller registers and bits */
128 #define R_SPI_CONF (0x00 / 4)
129 #define SPI_CONF_ENABLE_W0 0
130 #define R_SPI_CTRL0 (0x4 / 4)
131 #define R_SPI_MISC_CTRL (0x10 / 4)
132 #define R_SPI_TIMINGS (0x14 / 4)
134 #define ASPEED_SMC_R_SPI_MAX (0x20 / 4)
135 #define ASPEED_SMC_R_SMC_MAX (0x20 / 4)
137 #define ASPEED_SOC_SMC_FLASH_BASE 0x10000000
138 #define ASPEED_SOC_FMC_FLASH_BASE 0x20000000
139 #define ASPEED_SOC_SPI_FLASH_BASE 0x30000000
140 #define ASPEED_SOC_SPI2_FLASH_BASE 0x38000000
143 #define SPI_OP_READ 0x03 /* Read data bytes (low frequency) */
146 * Default segments mapping addresses and size for each slave per
147 * controller. These can be changed when board is initialized with the
148 * Segment Address Registers.
150 static const AspeedSegments aspeed_segments_legacy
[] = {
151 { 0x10000000, 32 * 1024 * 1024 },
154 static const AspeedSegments aspeed_segments_fmc
[] = {
155 { 0x20000000, 64 * 1024 * 1024 }, /* start address is readonly */
156 { 0x24000000, 32 * 1024 * 1024 },
157 { 0x26000000, 32 * 1024 * 1024 },
158 { 0x28000000, 32 * 1024 * 1024 },
159 { 0x2A000000, 32 * 1024 * 1024 }
162 static const AspeedSegments aspeed_segments_spi
[] = {
163 { 0x30000000, 64 * 1024 * 1024 },
166 static const AspeedSegments aspeed_segments_ast2500_fmc
[] = {
167 { 0x20000000, 128 * 1024 * 1024 }, /* start address is readonly */
168 { 0x28000000, 32 * 1024 * 1024 },
169 { 0x2A000000, 32 * 1024 * 1024 },
172 static const AspeedSegments aspeed_segments_ast2500_spi1
[] = {
173 { 0x30000000, 32 * 1024 * 1024 }, /* start address is readonly */
174 { 0x32000000, 96 * 1024 * 1024 }, /* end address is readonly */
177 static const AspeedSegments aspeed_segments_ast2500_spi2
[] = {
178 { 0x38000000, 32 * 1024 * 1024 }, /* start address is readonly */
179 { 0x3A000000, 96 * 1024 * 1024 }, /* end address is readonly */
182 static const AspeedSMCController controllers
[] = {
184 .name
= "aspeed.smc.smc",
186 .r_ce_ctrl
= R_CE_CTRL
,
188 .r_timings
= R_TIMINGS
,
189 .conf_enable_w0
= CONF_ENABLE_W0
,
191 .segments
= aspeed_segments_legacy
,
192 .flash_window_base
= ASPEED_SOC_SMC_FLASH_BASE
,
193 .flash_window_size
= 0x6000000,
195 .nregs
= ASPEED_SMC_R_SMC_MAX
,
197 .name
= "aspeed.smc.fmc",
199 .r_ce_ctrl
= R_CE_CTRL
,
201 .r_timings
= R_TIMINGS
,
202 .conf_enable_w0
= CONF_ENABLE_W0
,
204 .segments
= aspeed_segments_fmc
,
205 .flash_window_base
= ASPEED_SOC_FMC_FLASH_BASE
,
206 .flash_window_size
= 0x10000000,
208 .nregs
= ASPEED_SMC_R_MAX
,
210 .name
= "aspeed.smc.spi",
211 .r_conf
= R_SPI_CONF
,
213 .r_ctrl0
= R_SPI_CTRL0
,
214 .r_timings
= R_SPI_TIMINGS
,
215 .conf_enable_w0
= SPI_CONF_ENABLE_W0
,
217 .segments
= aspeed_segments_spi
,
218 .flash_window_base
= ASPEED_SOC_SPI_FLASH_BASE
,
219 .flash_window_size
= 0x10000000,
221 .nregs
= ASPEED_SMC_R_SPI_MAX
,
223 .name
= "aspeed.smc.ast2500-fmc",
225 .r_ce_ctrl
= R_CE_CTRL
,
227 .r_timings
= R_TIMINGS
,
228 .conf_enable_w0
= CONF_ENABLE_W0
,
230 .segments
= aspeed_segments_ast2500_fmc
,
231 .flash_window_base
= ASPEED_SOC_FMC_FLASH_BASE
,
232 .flash_window_size
= 0x10000000,
234 .nregs
= ASPEED_SMC_R_MAX
,
236 .name
= "aspeed.smc.ast2500-spi1",
238 .r_ce_ctrl
= R_CE_CTRL
,
240 .r_timings
= R_TIMINGS
,
241 .conf_enable_w0
= CONF_ENABLE_W0
,
243 .segments
= aspeed_segments_ast2500_spi1
,
244 .flash_window_base
= ASPEED_SOC_SPI_FLASH_BASE
,
245 .flash_window_size
= 0x8000000,
247 .nregs
= ASPEED_SMC_R_MAX
,
249 .name
= "aspeed.smc.ast2500-spi2",
251 .r_ce_ctrl
= R_CE_CTRL
,
253 .r_timings
= R_TIMINGS
,
254 .conf_enable_w0
= CONF_ENABLE_W0
,
256 .segments
= aspeed_segments_ast2500_spi2
,
257 .flash_window_base
= ASPEED_SOC_SPI2_FLASH_BASE
,
258 .flash_window_size
= 0x8000000,
260 .nregs
= ASPEED_SMC_R_MAX
,
265 * The Segment Register uses a 8MB unit to encode the start address
266 * and the end address of the mapping window of a flash SPI slave :
268 * | byte 1 | byte 2 | byte 3 | byte 4 |
269 * +--------+--------+--------+--------+
270 * | end | start | 0 | 0 |
273 static inline uint32_t aspeed_smc_segment_to_reg(const AspeedSegments
*seg
)
276 reg
|= ((seg
->addr
>> 23) & SEG_START_MASK
) << SEG_START_SHIFT
;
277 reg
|= (((seg
->addr
+ seg
->size
) >> 23) & SEG_END_MASK
) << SEG_END_SHIFT
;
281 static inline void aspeed_smc_reg_to_segment(uint32_t reg
, AspeedSegments
*seg
)
283 seg
->addr
= ((reg
>> SEG_START_SHIFT
) & SEG_START_MASK
) << 23;
284 seg
->size
= (((reg
>> SEG_END_SHIFT
) & SEG_END_MASK
) << 23) - seg
->addr
;
287 static bool aspeed_smc_flash_overlap(const AspeedSMCState
*s
,
288 const AspeedSegments
*new,
294 for (i
= 0; i
< s
->ctrl
->max_slaves
; i
++) {
299 aspeed_smc_reg_to_segment(s
->regs
[R_SEG_ADDR0
+ i
], &seg
);
301 if (new->addr
+ new->size
> seg
.addr
&&
302 new->addr
< seg
.addr
+ seg
.size
) {
303 qemu_log_mask(LOG_GUEST_ERROR
, "%s: new segment CS%d [ 0x%"
304 HWADDR_PRIx
" - 0x%"HWADDR_PRIx
" ] overlaps with "
305 "CS%d [ 0x%"HWADDR_PRIx
" - 0x%"HWADDR_PRIx
" ]\n",
306 s
->ctrl
->name
, cs
, new->addr
, new->addr
+ new->size
,
307 i
, seg
.addr
, seg
.addr
+ seg
.size
);
314 static void aspeed_smc_flash_set_segment(AspeedSMCState
*s
, int cs
,
317 AspeedSMCFlash
*fl
= &s
->flashes
[cs
];
320 aspeed_smc_reg_to_segment(new, &seg
);
322 /* The start address of CS0 is read-only */
323 if (cs
== 0 && seg
.addr
!= s
->ctrl
->flash_window_base
) {
324 qemu_log_mask(LOG_GUEST_ERROR
,
325 "%s: Tried to change CS0 start address to 0x%"
326 HWADDR_PRIx
"\n", s
->ctrl
->name
, seg
.addr
);
327 seg
.addr
= s
->ctrl
->flash_window_base
;
328 new = aspeed_smc_segment_to_reg(&seg
);
332 * The end address of the AST2500 spi controllers is also
335 if ((s
->ctrl
->segments
== aspeed_segments_ast2500_spi1
||
336 s
->ctrl
->segments
== aspeed_segments_ast2500_spi2
) &&
337 cs
== s
->ctrl
->max_slaves
&&
338 seg
.addr
+ seg
.size
!= s
->ctrl
->segments
[cs
].addr
+
339 s
->ctrl
->segments
[cs
].size
) {
340 qemu_log_mask(LOG_GUEST_ERROR
,
341 "%s: Tried to change CS%d end address to 0x%"
342 HWADDR_PRIx
"\n", s
->ctrl
->name
, cs
, seg
.addr
+ seg
.size
);
343 seg
.size
= s
->ctrl
->segments
[cs
].addr
+ s
->ctrl
->segments
[cs
].size
-
345 new = aspeed_smc_segment_to_reg(&seg
);
348 /* Keep the segment in the overall flash window */
349 if (seg
.addr
+ seg
.size
<= s
->ctrl
->flash_window_base
||
350 seg
.addr
> s
->ctrl
->flash_window_base
+ s
->ctrl
->flash_window_size
) {
351 qemu_log_mask(LOG_GUEST_ERROR
, "%s: new segment for CS%d is invalid : "
352 "[ 0x%"HWADDR_PRIx
" - 0x%"HWADDR_PRIx
" ]\n",
353 s
->ctrl
->name
, cs
, seg
.addr
, seg
.addr
+ seg
.size
);
357 /* Check start address vs. alignment */
358 if (seg
.size
&& !QEMU_IS_ALIGNED(seg
.addr
, seg
.size
)) {
359 qemu_log_mask(LOG_GUEST_ERROR
, "%s: new segment for CS%d is not "
360 "aligned : [ 0x%"HWADDR_PRIx
" - 0x%"HWADDR_PRIx
" ]\n",
361 s
->ctrl
->name
, cs
, seg
.addr
, seg
.addr
+ seg
.size
);
364 /* And segments should not overlap (in the specs) */
365 aspeed_smc_flash_overlap(s
, &seg
, cs
);
367 /* All should be fine now to move the region */
368 memory_region_transaction_begin();
369 memory_region_set_size(&fl
->mmio
, seg
.size
);
370 memory_region_set_address(&fl
->mmio
, seg
.addr
- s
->ctrl
->flash_window_base
);
371 memory_region_set_enabled(&fl
->mmio
, true);
372 memory_region_transaction_commit();
374 s
->regs
[R_SEG_ADDR0
+ cs
] = new;
377 static uint64_t aspeed_smc_flash_default_read(void *opaque
, hwaddr addr
,
380 qemu_log_mask(LOG_GUEST_ERROR
, "%s: To 0x%" HWADDR_PRIx
" of size %u"
381 PRIx64
"\n", __func__
, addr
, size
);
385 static void aspeed_smc_flash_default_write(void *opaque
, hwaddr addr
,
386 uint64_t data
, unsigned size
)
388 qemu_log_mask(LOG_GUEST_ERROR
, "%s: To 0x%" HWADDR_PRIx
" of size %u: 0x%"
389 PRIx64
"\n", __func__
, addr
, size
, data
);
392 static const MemoryRegionOps aspeed_smc_flash_default_ops
= {
393 .read
= aspeed_smc_flash_default_read
,
394 .write
= aspeed_smc_flash_default_write
,
395 .endianness
= DEVICE_LITTLE_ENDIAN
,
397 .min_access_size
= 1,
398 .max_access_size
= 4,
402 static inline int aspeed_smc_flash_mode(const AspeedSMCFlash
*fl
)
404 const AspeedSMCState
*s
= fl
->controller
;
406 return s
->regs
[s
->r_ctrl0
+ fl
->id
] & CTRL_CMD_MODE_MASK
;
409 static inline bool aspeed_smc_is_writable(const AspeedSMCFlash
*fl
)
411 const AspeedSMCState
*s
= fl
->controller
;
413 return s
->regs
[s
->r_conf
] & (1 << (s
->conf_enable_w0
+ fl
->id
));
416 static inline int aspeed_smc_flash_cmd(const AspeedSMCFlash
*fl
)
418 const AspeedSMCState
*s
= fl
->controller
;
419 int cmd
= (s
->regs
[s
->r_ctrl0
+ fl
->id
] >> CTRL_CMD_SHIFT
) & CTRL_CMD_MASK
;
421 /* In read mode, the default SPI command is READ (0x3). In other
422 * modes, the command should necessarily be defined */
423 if (aspeed_smc_flash_mode(fl
) == CTRL_READMODE
) {
428 qemu_log_mask(LOG_GUEST_ERROR
, "%s: no command defined for mode %d\n",
429 __func__
, aspeed_smc_flash_mode(fl
));
435 static inline int aspeed_smc_flash_is_4byte(const AspeedSMCFlash
*fl
)
437 const AspeedSMCState
*s
= fl
->controller
;
439 if (s
->ctrl
->segments
== aspeed_segments_spi
) {
440 return s
->regs
[s
->r_ctrl0
] & CTRL_AST2400_SPI_4BYTE
;
442 return s
->regs
[s
->r_ce_ctrl
] & (1 << (CTRL_EXTENDED0
+ fl
->id
));
446 static inline bool aspeed_smc_is_ce_stop_active(const AspeedSMCFlash
*fl
)
448 const AspeedSMCState
*s
= fl
->controller
;
450 return s
->regs
[s
->r_ctrl0
+ fl
->id
] & CTRL_CE_STOP_ACTIVE
;
453 static void aspeed_smc_flash_select(AspeedSMCFlash
*fl
)
455 AspeedSMCState
*s
= fl
->controller
;
457 s
->regs
[s
->r_ctrl0
+ fl
->id
] &= ~CTRL_CE_STOP_ACTIVE
;
458 qemu_set_irq(s
->cs_lines
[fl
->id
], aspeed_smc_is_ce_stop_active(fl
));
461 static void aspeed_smc_flash_unselect(AspeedSMCFlash
*fl
)
463 AspeedSMCState
*s
= fl
->controller
;
465 s
->regs
[s
->r_ctrl0
+ fl
->id
] |= CTRL_CE_STOP_ACTIVE
;
466 qemu_set_irq(s
->cs_lines
[fl
->id
], aspeed_smc_is_ce_stop_active(fl
));
469 static uint32_t aspeed_smc_check_segment_addr(const AspeedSMCFlash
*fl
,
472 const AspeedSMCState
*s
= fl
->controller
;
475 aspeed_smc_reg_to_segment(s
->regs
[R_SEG_ADDR0
+ fl
->id
], &seg
);
476 if ((addr
& (seg
.size
- 1)) != addr
) {
477 qemu_log_mask(LOG_GUEST_ERROR
,
478 "%s: invalid address 0x%08x for CS%d segment : "
479 "[ 0x%"HWADDR_PRIx
" - 0x%"HWADDR_PRIx
" ]\n",
480 s
->ctrl
->name
, addr
, fl
->id
, seg
.addr
,
481 seg
.addr
+ seg
.size
);
484 addr
&= seg
.size
- 1;
488 static void aspeed_smc_flash_send_addr(AspeedSMCFlash
*fl
, uint32_t addr
)
490 const AspeedSMCState
*s
= fl
->controller
;
491 uint8_t cmd
= aspeed_smc_flash_cmd(fl
);
493 /* Flash access can not exceed CS segment */
494 addr
= aspeed_smc_check_segment_addr(fl
, addr
);
496 ssi_transfer(s
->spi
, cmd
);
498 if (aspeed_smc_flash_is_4byte(fl
)) {
499 ssi_transfer(s
->spi
, (addr
>> 24) & 0xff);
501 ssi_transfer(s
->spi
, (addr
>> 16) & 0xff);
502 ssi_transfer(s
->spi
, (addr
>> 8) & 0xff);
503 ssi_transfer(s
->spi
, (addr
& 0xff));
506 static uint64_t aspeed_smc_flash_read(void *opaque
, hwaddr addr
, unsigned size
)
508 AspeedSMCFlash
*fl
= opaque
;
509 AspeedSMCState
*s
= fl
->controller
;
513 switch (aspeed_smc_flash_mode(fl
)) {
515 for (i
= 0; i
< size
; i
++) {
516 ret
|= ssi_transfer(s
->spi
, 0x0) << (8 * i
);
521 aspeed_smc_flash_select(fl
);
522 aspeed_smc_flash_send_addr(fl
, addr
);
524 for (i
= 0; i
< size
; i
++) {
525 ret
|= ssi_transfer(s
->spi
, 0x0) << (8 * i
);
528 aspeed_smc_flash_unselect(fl
);
531 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid flash mode %d\n",
532 __func__
, aspeed_smc_flash_mode(fl
));
538 static void aspeed_smc_flash_write(void *opaque
, hwaddr addr
, uint64_t data
,
541 AspeedSMCFlash
*fl
= opaque
;
542 AspeedSMCState
*s
= fl
->controller
;
545 if (!aspeed_smc_is_writable(fl
)) {
546 qemu_log_mask(LOG_GUEST_ERROR
, "%s: flash is not writable at 0x%"
547 HWADDR_PRIx
"\n", __func__
, addr
);
551 switch (aspeed_smc_flash_mode(fl
)) {
553 for (i
= 0; i
< size
; i
++) {
554 ssi_transfer(s
->spi
, (data
>> (8 * i
)) & 0xff);
558 aspeed_smc_flash_select(fl
);
559 aspeed_smc_flash_send_addr(fl
, addr
);
561 for (i
= 0; i
< size
; i
++) {
562 ssi_transfer(s
->spi
, (data
>> (8 * i
)) & 0xff);
565 aspeed_smc_flash_unselect(fl
);
568 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid flash mode %d\n",
569 __func__
, aspeed_smc_flash_mode(fl
));
573 static const MemoryRegionOps aspeed_smc_flash_ops
= {
574 .read
= aspeed_smc_flash_read
,
575 .write
= aspeed_smc_flash_write
,
576 .endianness
= DEVICE_LITTLE_ENDIAN
,
578 .min_access_size
= 1,
579 .max_access_size
= 4,
583 static void aspeed_smc_flash_update_cs(AspeedSMCFlash
*fl
)
585 const AspeedSMCState
*s
= fl
->controller
;
587 qemu_set_irq(s
->cs_lines
[fl
->id
], aspeed_smc_is_ce_stop_active(fl
));
590 static void aspeed_smc_reset(DeviceState
*d
)
592 AspeedSMCState
*s
= ASPEED_SMC(d
);
595 memset(s
->regs
, 0, sizeof s
->regs
);
597 /* Pretend DMA is done (u-boot initialization) */
598 s
->regs
[R_INTR_CTRL
] = INTR_CTRL_DMA_STATUS
;
600 /* Unselect all slaves */
601 for (i
= 0; i
< s
->num_cs
; ++i
) {
602 s
->regs
[s
->r_ctrl0
+ i
] |= CTRL_CE_STOP_ACTIVE
;
603 qemu_set_irq(s
->cs_lines
[i
], true);
606 /* setup default segment register values for all */
607 for (i
= 0; i
< s
->ctrl
->max_slaves
; ++i
) {
608 s
->regs
[R_SEG_ADDR0
+ i
] =
609 aspeed_smc_segment_to_reg(&s
->ctrl
->segments
[i
]);
612 /* HW strapping for AST2500 FMC controllers */
613 if (s
->ctrl
->segments
== aspeed_segments_ast2500_fmc
) {
614 /* flash type is fixed to SPI for CE0 and CE1 */
615 s
->regs
[s
->r_conf
] |= (CONF_FLASH_TYPE_SPI
<< CONF_FLASH_TYPE0
);
616 s
->regs
[s
->r_conf
] |= (CONF_FLASH_TYPE_SPI
<< CONF_FLASH_TYPE1
);
618 /* 4BYTE mode is autodetected for CE0. Let's force it to 1 for
620 s
->regs
[s
->r_ce_ctrl
] |= (1 << (CTRL_EXTENDED0
));
623 /* HW strapping for AST2400 FMC controllers (SCU70). Let's use the
624 * configuration of the palmetto-bmc machine */
625 if (s
->ctrl
->segments
== aspeed_segments_fmc
) {
626 s
->regs
[s
->r_conf
] |= (CONF_FLASH_TYPE_SPI
<< CONF_FLASH_TYPE0
);
628 s
->regs
[s
->r_ce_ctrl
] |= (1 << (CTRL_EXTENDED0
));
632 static uint64_t aspeed_smc_read(void *opaque
, hwaddr addr
, unsigned int size
)
634 AspeedSMCState
*s
= ASPEED_SMC(opaque
);
638 if (addr
== s
->r_conf
||
639 addr
== s
->r_timings
||
640 addr
== s
->r_ce_ctrl
||
641 addr
== R_INTR_CTRL
||
642 (addr
>= R_SEG_ADDR0
&& addr
< R_SEG_ADDR0
+ s
->ctrl
->max_slaves
) ||
643 (addr
>= s
->r_ctrl0
&& addr
< s
->r_ctrl0
+ s
->num_cs
)) {
644 return s
->regs
[addr
];
646 qemu_log_mask(LOG_UNIMP
, "%s: not implemented: 0x%" HWADDR_PRIx
"\n",
652 static void aspeed_smc_write(void *opaque
, hwaddr addr
, uint64_t data
,
655 AspeedSMCState
*s
= ASPEED_SMC(opaque
);
656 uint32_t value
= data
;
660 if (addr
== s
->r_conf
||
661 addr
== s
->r_timings
||
662 addr
== s
->r_ce_ctrl
) {
663 s
->regs
[addr
] = value
;
664 } else if (addr
>= s
->r_ctrl0
&& addr
< s
->r_ctrl0
+ s
->num_cs
) {
665 int cs
= addr
- s
->r_ctrl0
;
666 s
->regs
[addr
] = value
;
667 aspeed_smc_flash_update_cs(&s
->flashes
[cs
]);
668 } else if (addr
>= R_SEG_ADDR0
&&
669 addr
< R_SEG_ADDR0
+ s
->ctrl
->max_slaves
) {
670 int cs
= addr
- R_SEG_ADDR0
;
672 if (value
!= s
->regs
[R_SEG_ADDR0
+ cs
]) {
673 aspeed_smc_flash_set_segment(s
, cs
, value
);
676 qemu_log_mask(LOG_UNIMP
, "%s: not implemented: 0x%" HWADDR_PRIx
"\n",
682 static const MemoryRegionOps aspeed_smc_ops
= {
683 .read
= aspeed_smc_read
,
684 .write
= aspeed_smc_write
,
685 .endianness
= DEVICE_LITTLE_ENDIAN
,
686 .valid
.unaligned
= true,
689 static void aspeed_smc_realize(DeviceState
*dev
, Error
**errp
)
691 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
692 AspeedSMCState
*s
= ASPEED_SMC(dev
);
693 AspeedSMCClass
*mc
= ASPEED_SMC_GET_CLASS(s
);
700 /* keep a copy under AspeedSMCState to speed up accesses */
701 s
->r_conf
= s
->ctrl
->r_conf
;
702 s
->r_ce_ctrl
= s
->ctrl
->r_ce_ctrl
;
703 s
->r_ctrl0
= s
->ctrl
->r_ctrl0
;
704 s
->r_timings
= s
->ctrl
->r_timings
;
705 s
->conf_enable_w0
= s
->ctrl
->conf_enable_w0
;
707 /* Enforce some real HW limits */
708 if (s
->num_cs
> s
->ctrl
->max_slaves
) {
709 qemu_log_mask(LOG_GUEST_ERROR
, "%s: num_cs cannot exceed: %d\n",
710 __func__
, s
->ctrl
->max_slaves
);
711 s
->num_cs
= s
->ctrl
->max_slaves
;
714 s
->spi
= ssi_create_bus(dev
, "spi");
716 /* Setup cs_lines for slaves */
717 sysbus_init_irq(sbd
, &s
->irq
);
718 s
->cs_lines
= g_new0(qemu_irq
, s
->num_cs
);
719 ssi_auto_connect_slaves(dev
, s
->cs_lines
, s
->spi
);
721 for (i
= 0; i
< s
->num_cs
; ++i
) {
722 sysbus_init_irq(sbd
, &s
->cs_lines
[i
]);
725 /* The memory region for the controller registers */
726 memory_region_init_io(&s
->mmio
, OBJECT(s
), &aspeed_smc_ops
, s
,
727 s
->ctrl
->name
, s
->ctrl
->nregs
* 4);
728 sysbus_init_mmio(sbd
, &s
->mmio
);
731 * The container memory region representing the address space
732 * window in which the flash modules are mapped. The size and
733 * address depends on the SoC model and controller type.
735 snprintf(name
, sizeof(name
), "%s.flash", s
->ctrl
->name
);
737 memory_region_init_io(&s
->mmio_flash
, OBJECT(s
),
738 &aspeed_smc_flash_default_ops
, s
, name
,
739 s
->ctrl
->flash_window_size
);
740 sysbus_init_mmio(sbd
, &s
->mmio_flash
);
742 s
->flashes
= g_new0(AspeedSMCFlash
, s
->ctrl
->max_slaves
);
745 * Let's create a sub memory region for each possible slave. All
746 * have a configurable memory segment in the overall flash mapping
747 * window of the controller but, there is not necessarily a flash
748 * module behind to handle the memory accesses. This depends on
749 * the board configuration.
751 for (i
= 0; i
< s
->ctrl
->max_slaves
; ++i
) {
752 AspeedSMCFlash
*fl
= &s
->flashes
[i
];
754 snprintf(name
, sizeof(name
), "%s.%d", s
->ctrl
->name
, i
);
758 fl
->size
= s
->ctrl
->segments
[i
].size
;
759 memory_region_init_io(&fl
->mmio
, OBJECT(s
), &aspeed_smc_flash_ops
,
761 memory_region_add_subregion(&s
->mmio_flash
, offset
, &fl
->mmio
);
766 static const VMStateDescription vmstate_aspeed_smc
= {
767 .name
= "aspeed.smc",
769 .minimum_version_id
= 1,
770 .fields
= (VMStateField
[]) {
771 VMSTATE_UINT32_ARRAY(regs
, AspeedSMCState
, ASPEED_SMC_R_MAX
),
772 VMSTATE_END_OF_LIST()
776 static Property aspeed_smc_properties
[] = {
777 DEFINE_PROP_UINT32("num-cs", AspeedSMCState
, num_cs
, 1),
778 DEFINE_PROP_END_OF_LIST(),
781 static void aspeed_smc_class_init(ObjectClass
*klass
, void *data
)
783 DeviceClass
*dc
= DEVICE_CLASS(klass
);
784 AspeedSMCClass
*mc
= ASPEED_SMC_CLASS(klass
);
786 dc
->realize
= aspeed_smc_realize
;
787 dc
->reset
= aspeed_smc_reset
;
788 dc
->props
= aspeed_smc_properties
;
789 dc
->vmsd
= &vmstate_aspeed_smc
;
793 static const TypeInfo aspeed_smc_info
= {
794 .name
= TYPE_ASPEED_SMC
,
795 .parent
= TYPE_SYS_BUS_DEVICE
,
796 .instance_size
= sizeof(AspeedSMCState
),
797 .class_size
= sizeof(AspeedSMCClass
),
801 static void aspeed_smc_register_types(void)
805 type_register_static(&aspeed_smc_info
);
806 for (i
= 0; i
< ARRAY_SIZE(controllers
); ++i
) {
808 .name
= controllers
[i
].name
,
809 .parent
= TYPE_ASPEED_SMC
,
810 .class_init
= aspeed_smc_class_init
,
811 .class_data
= (void *)&controllers
[i
],
817 type_init(aspeed_smc_register_types
)