2 * Nordic Semiconductor nRF51 non-volatile memory
4 * It provides an interface to erase regions in flash memory.
5 * Furthermore it provides the user and factory information registers.
7 * Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf
9 * See nRF51 reference manual and product sheet sections:
10 * + Non-Volatile Memory Controller (NVMC)
11 * + Factory Information Configuration Registers (FICR)
12 * + User Information Configuration Registers (UICR)
14 * Copyright 2018 Steffen Görtz <contrib@steffen-goertz.de>
16 * This code is licensed under the GPL version 2 or later. See
17 * the COPYING file in the top-level directory.
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
23 #include "qemu/module.h"
24 #include "exec/address-spaces.h"
25 #include "hw/arm/nrf51.h"
26 #include "hw/nvram/nrf51_nvm.h"
29 * FICR Registers Assignments
36 * SIZERAMBLOCK[0] 0x038
37 * SIZERAMBLOCK[1] 0x03C
38 * SIZERAMBLOCK[2] 0x040
39 * SIZERAMBLOCK[3] 0x044
51 * DEVICEADDRTYPE 0x0A0
66 static const uint32_t ficr_content
[64] = {
67 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000400,
68 0x00000100, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000002, 0x00002000,
69 0x00002000, 0x00002000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
70 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
71 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000003,
72 0x12345678, 0x9ABCDEF1, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
73 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
74 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
75 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
76 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
77 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
78 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
79 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
82 static uint64_t ficr_read(void *opaque
, hwaddr offset
, unsigned int size
)
84 assert(offset
< sizeof(ficr_content
));
85 return ficr_content
[offset
/ 4];
88 static void ficr_write(void *opaque
, hwaddr offset
, uint64_t value
,
91 /* Intentionally do nothing */
94 static const MemoryRegionOps ficr_ops
= {
97 .impl
.min_access_size
= 4,
98 .impl
.max_access_size
= 4,
99 .endianness
= DEVICE_LITTLE_ENDIAN
103 * UICR Registers Assignments
108 * BOOTLOADERADDR 0x014
170 static uint64_t uicr_read(void *opaque
, hwaddr offset
, unsigned int size
)
172 NRF51NVMState
*s
= NRF51_NVM(opaque
);
174 assert(offset
< sizeof(s
->uicr_content
));
175 return s
->uicr_content
[offset
/ 4];
178 static void uicr_write(void *opaque
, hwaddr offset
, uint64_t value
,
181 NRF51NVMState
*s
= NRF51_NVM(opaque
);
183 assert(offset
< sizeof(s
->uicr_content
));
184 s
->uicr_content
[offset
/ 4] = value
;
187 static const MemoryRegionOps uicr_ops
= {
190 .impl
.min_access_size
= 4,
191 .impl
.max_access_size
= 4,
192 .endianness
= DEVICE_LITTLE_ENDIAN
196 static uint64_t io_read(void *opaque
, hwaddr offset
, unsigned int size
)
198 NRF51NVMState
*s
= NRF51_NVM(opaque
);
202 case NRF51_NVMC_READY
:
203 r
= NRF51_NVMC_READY_READY
;
205 case NRF51_NVMC_CONFIG
:
209 qemu_log_mask(LOG_GUEST_ERROR
,
210 "%s: bad read offset 0x%" HWADDR_PRIx
"\n", __func__
, offset
);
217 static void io_write(void *opaque
, hwaddr offset
, uint64_t value
,
220 NRF51NVMState
*s
= NRF51_NVM(opaque
);
223 case NRF51_NVMC_CONFIG
:
224 s
->config
= value
& NRF51_NVMC_CONFIG_MASK
;
226 case NRF51_NVMC_ERASEPCR0
:
227 case NRF51_NVMC_ERASEPCR1
:
228 if (s
->config
& NRF51_NVMC_CONFIG_EEN
) {
229 /* Mask in-page sub address */
230 value
&= ~(NRF51_PAGE_SIZE
- 1);
231 if (value
<= (s
->flash_size
- NRF51_PAGE_SIZE
)) {
232 memset(s
->storage
+ value
, 0xFF, NRF51_PAGE_SIZE
);
233 memory_region_flush_rom_device(&s
->flash
, value
,
237 qemu_log_mask(LOG_GUEST_ERROR
,
238 "%s: Flash erase at 0x%" HWADDR_PRIx
" while flash not erasable.\n",
242 case NRF51_NVMC_ERASEALL
:
243 if (value
== NRF51_NVMC_ERASE
) {
244 if (s
->config
& NRF51_NVMC_CONFIG_EEN
) {
245 memset(s
->storage
, 0xFF, s
->flash_size
);
246 memory_region_flush_rom_device(&s
->flash
, 0, s
->flash_size
);
247 memset(s
->uicr_content
, 0xFF, sizeof(s
->uicr_content
));
249 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Flash not erasable.\n",
254 case NRF51_NVMC_ERASEUICR
:
255 if (value
== NRF51_NVMC_ERASE
) {
256 memset(s
->uicr_content
, 0xFF, sizeof(s
->uicr_content
));
261 qemu_log_mask(LOG_GUEST_ERROR
,
262 "%s: bad write offset 0x%" HWADDR_PRIx
"\n", __func__
, offset
);
266 static const MemoryRegionOps io_ops
= {
269 .impl
.min_access_size
= 4,
270 .impl
.max_access_size
= 4,
271 .endianness
= DEVICE_LITTLE_ENDIAN
,
275 static void flash_write(void *opaque
, hwaddr offset
, uint64_t value
,
278 NRF51NVMState
*s
= NRF51_NVM(opaque
);
280 if (s
->config
& NRF51_NVMC_CONFIG_WEN
) {
283 assert(offset
+ size
<= s
->flash_size
);
285 /* NOR Flash only allows bits to be flipped from 1's to 0's on write */
286 oldval
= ldl_le_p(s
->storage
+ offset
);
288 stl_le_p(s
->storage
+ offset
, oldval
);
290 memory_region_flush_rom_device(&s
->flash
, offset
, size
);
292 qemu_log_mask(LOG_GUEST_ERROR
,
293 "%s: Flash write 0x%" HWADDR_PRIx
" while flash not writable.\n",
300 static const MemoryRegionOps flash_ops
= {
301 .write
= flash_write
,
302 .valid
.min_access_size
= 4,
303 .valid
.max_access_size
= 4,
304 .endianness
= DEVICE_LITTLE_ENDIAN
,
307 static void nrf51_nvm_init(Object
*obj
)
309 NRF51NVMState
*s
= NRF51_NVM(obj
);
310 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
312 memory_region_init_io(&s
->mmio
, obj
, &io_ops
, s
, "nrf51_soc.nvmc",
314 sysbus_init_mmio(sbd
, &s
->mmio
);
316 memory_region_init_io(&s
->ficr
, obj
, &ficr_ops
, s
, "nrf51_soc.ficr",
317 sizeof(ficr_content
));
318 sysbus_init_mmio(sbd
, &s
->ficr
);
320 memory_region_init_io(&s
->uicr
, obj
, &uicr_ops
, s
, "nrf51_soc.uicr",
321 sizeof(s
->uicr_content
));
322 sysbus_init_mmio(sbd
, &s
->uicr
);
325 static void nrf51_nvm_realize(DeviceState
*dev
, Error
**errp
)
327 NRF51NVMState
*s
= NRF51_NVM(dev
);
330 memory_region_init_rom_device(&s
->flash
, OBJECT(dev
), &flash_ops
, s
,
331 "nrf51_soc.flash", s
->flash_size
, &err
);
333 error_propagate(errp
, err
);
337 s
->storage
= memory_region_get_ram_ptr(&s
->flash
);
338 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->flash
);
341 static void nrf51_nvm_reset(DeviceState
*dev
)
343 NRF51NVMState
*s
= NRF51_NVM(dev
);
346 memset(s
->uicr_content
, 0xFF, sizeof(s
->uicr_content
));
349 static Property nrf51_nvm_properties
[] = {
350 DEFINE_PROP_UINT32("flash-size", NRF51NVMState
, flash_size
, 0x40000),
351 DEFINE_PROP_END_OF_LIST(),
354 static const VMStateDescription vmstate_nvm
= {
355 .name
= "nrf51_soc.nvm",
357 .minimum_version_id
= 1,
358 .fields
= (VMStateField
[]) {
359 VMSTATE_UINT32_ARRAY(uicr_content
, NRF51NVMState
,
360 NRF51_UICR_FIXTURE_SIZE
),
361 VMSTATE_UINT32(config
, NRF51NVMState
),
362 VMSTATE_END_OF_LIST()
366 static void nrf51_nvm_class_init(ObjectClass
*klass
, void *data
)
368 DeviceClass
*dc
= DEVICE_CLASS(klass
);
370 dc
->props
= nrf51_nvm_properties
;
371 dc
->vmsd
= &vmstate_nvm
;
372 dc
->realize
= nrf51_nvm_realize
;
373 dc
->reset
= nrf51_nvm_reset
;
376 static const TypeInfo nrf51_nvm_info
= {
377 .name
= TYPE_NRF51_NVM
,
378 .parent
= TYPE_SYS_BUS_DEVICE
,
379 .instance_size
= sizeof(NRF51NVMState
),
380 .instance_init
= nrf51_nvm_init
,
381 .class_init
= nrf51_nvm_class_init
384 static void nrf51_nvm_register_types(void)
386 type_register_static(&nrf51_nvm_info
);
389 type_init(nrf51_nvm_register_types
)