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"
27 #include "hw/qdev-properties.h"
28 #include "migration/vmstate.h"
31 * FICR Registers Assignments
38 * SIZERAMBLOCK[0] 0x038
39 * SIZERAMBLOCK[1] 0x03C
40 * SIZERAMBLOCK[2] 0x040
41 * SIZERAMBLOCK[3] 0x044
53 * DEVICEADDRTYPE 0x0A0
68 static const uint32_t ficr_content
[64] = {
69 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000400,
70 0x00000100, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000002, 0x00002000,
71 0x00002000, 0x00002000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
72 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
73 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000003,
74 0x12345678, 0x9ABCDEF1, 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, 0xFFFFFFFF,
80 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
81 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
84 static uint64_t ficr_read(void *opaque
, hwaddr offset
, unsigned int size
)
86 assert(offset
< sizeof(ficr_content
));
87 return ficr_content
[offset
/ 4];
90 static void ficr_write(void *opaque
, hwaddr offset
, uint64_t value
,
93 /* Intentionally do nothing */
96 static const MemoryRegionOps ficr_ops
= {
99 .impl
.min_access_size
= 4,
100 .impl
.max_access_size
= 4,
101 .endianness
= DEVICE_LITTLE_ENDIAN
105 * UICR Registers Assignments
110 * BOOTLOADERADDR 0x014
172 static uint64_t uicr_read(void *opaque
, hwaddr offset
, unsigned int size
)
174 NRF51NVMState
*s
= NRF51_NVM(opaque
);
176 assert(offset
< sizeof(s
->uicr_content
));
177 return s
->uicr_content
[offset
/ 4];
180 static void uicr_write(void *opaque
, hwaddr offset
, uint64_t value
,
183 NRF51NVMState
*s
= NRF51_NVM(opaque
);
185 assert(offset
< sizeof(s
->uicr_content
));
186 s
->uicr_content
[offset
/ 4] = value
;
189 static const MemoryRegionOps uicr_ops
= {
192 .impl
.min_access_size
= 4,
193 .impl
.max_access_size
= 4,
194 .endianness
= DEVICE_LITTLE_ENDIAN
198 static uint64_t io_read(void *opaque
, hwaddr offset
, unsigned int size
)
200 NRF51NVMState
*s
= NRF51_NVM(opaque
);
204 case NRF51_NVMC_READY
:
205 r
= NRF51_NVMC_READY_READY
;
207 case NRF51_NVMC_CONFIG
:
211 qemu_log_mask(LOG_GUEST_ERROR
,
212 "%s: bad read offset 0x%" HWADDR_PRIx
"\n", __func__
, offset
);
219 static void io_write(void *opaque
, hwaddr offset
, uint64_t value
,
222 NRF51NVMState
*s
= NRF51_NVM(opaque
);
225 case NRF51_NVMC_CONFIG
:
226 s
->config
= value
& NRF51_NVMC_CONFIG_MASK
;
228 case NRF51_NVMC_ERASEPCR0
:
229 case NRF51_NVMC_ERASEPCR1
:
230 if (s
->config
& NRF51_NVMC_CONFIG_EEN
) {
231 /* Mask in-page sub address */
232 value
&= ~(NRF51_PAGE_SIZE
- 1);
233 if (value
<= (s
->flash_size
- NRF51_PAGE_SIZE
)) {
234 memset(s
->storage
+ value
, 0xFF, NRF51_PAGE_SIZE
);
235 memory_region_flush_rom_device(&s
->flash
, value
,
239 qemu_log_mask(LOG_GUEST_ERROR
,
240 "%s: Flash erase at 0x%" HWADDR_PRIx
" while flash not erasable.\n",
244 case NRF51_NVMC_ERASEALL
:
245 if (value
== NRF51_NVMC_ERASE
) {
246 if (s
->config
& NRF51_NVMC_CONFIG_EEN
) {
247 memset(s
->storage
, 0xFF, s
->flash_size
);
248 memory_region_flush_rom_device(&s
->flash
, 0, s
->flash_size
);
249 memset(s
->uicr_content
, 0xFF, sizeof(s
->uicr_content
));
251 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Flash not erasable.\n",
256 case NRF51_NVMC_ERASEUICR
:
257 if (value
== NRF51_NVMC_ERASE
) {
258 memset(s
->uicr_content
, 0xFF, sizeof(s
->uicr_content
));
263 qemu_log_mask(LOG_GUEST_ERROR
,
264 "%s: bad write offset 0x%" HWADDR_PRIx
"\n", __func__
, offset
);
268 static const MemoryRegionOps io_ops
= {
271 .impl
.min_access_size
= 4,
272 .impl
.max_access_size
= 4,
273 .endianness
= DEVICE_LITTLE_ENDIAN
,
276 static uint64_t flash_read(void *opaque
, hwaddr offset
, unsigned size
)
279 * This is a rom_device MemoryRegion which is always in
280 * romd_mode (we never put it in MMIO mode), so reads always
281 * go directly to RAM and never come here.
283 g_assert_not_reached();
286 static void flash_write(void *opaque
, hwaddr offset
, uint64_t value
,
289 NRF51NVMState
*s
= NRF51_NVM(opaque
);
291 if (s
->config
& NRF51_NVMC_CONFIG_WEN
) {
294 assert(offset
+ size
<= s
->flash_size
);
296 /* NOR Flash only allows bits to be flipped from 1's to 0's on write */
297 oldval
= ldl_le_p(s
->storage
+ offset
);
299 stl_le_p(s
->storage
+ offset
, oldval
);
301 memory_region_flush_rom_device(&s
->flash
, offset
, size
);
303 qemu_log_mask(LOG_GUEST_ERROR
,
304 "%s: Flash write 0x%" HWADDR_PRIx
" while flash not writable.\n",
311 static const MemoryRegionOps flash_ops
= {
313 .write
= flash_write
,
314 .valid
.min_access_size
= 4,
315 .valid
.max_access_size
= 4,
316 .endianness
= DEVICE_LITTLE_ENDIAN
,
319 static void nrf51_nvm_init(Object
*obj
)
321 NRF51NVMState
*s
= NRF51_NVM(obj
);
322 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
324 memory_region_init_io(&s
->mmio
, obj
, &io_ops
, s
, "nrf51_soc.nvmc",
326 sysbus_init_mmio(sbd
, &s
->mmio
);
328 memory_region_init_io(&s
->ficr
, obj
, &ficr_ops
, s
, "nrf51_soc.ficr",
329 sizeof(ficr_content
));
330 sysbus_init_mmio(sbd
, &s
->ficr
);
332 memory_region_init_io(&s
->uicr
, obj
, &uicr_ops
, s
, "nrf51_soc.uicr",
333 sizeof(s
->uicr_content
));
334 sysbus_init_mmio(sbd
, &s
->uicr
);
337 static void nrf51_nvm_realize(DeviceState
*dev
, Error
**errp
)
339 NRF51NVMState
*s
= NRF51_NVM(dev
);
342 memory_region_init_rom_device(&s
->flash
, OBJECT(dev
), &flash_ops
, s
,
343 "nrf51_soc.flash", s
->flash_size
, &err
);
345 error_propagate(errp
, err
);
349 s
->storage
= memory_region_get_ram_ptr(&s
->flash
);
350 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->flash
);
353 static void nrf51_nvm_reset(DeviceState
*dev
)
355 NRF51NVMState
*s
= NRF51_NVM(dev
);
358 memset(s
->uicr_content
, 0xFF, sizeof(s
->uicr_content
));
361 static Property nrf51_nvm_properties
[] = {
362 DEFINE_PROP_UINT32("flash-size", NRF51NVMState
, flash_size
, 0x40000),
363 DEFINE_PROP_END_OF_LIST(),
366 static const VMStateDescription vmstate_nvm
= {
367 .name
= "nrf51_soc.nvm",
369 .minimum_version_id
= 1,
370 .fields
= (VMStateField
[]) {
371 VMSTATE_UINT32_ARRAY(uicr_content
, NRF51NVMState
,
372 NRF51_UICR_FIXTURE_SIZE
),
373 VMSTATE_UINT32(config
, NRF51NVMState
),
374 VMSTATE_END_OF_LIST()
378 static void nrf51_nvm_class_init(ObjectClass
*klass
, void *data
)
380 DeviceClass
*dc
= DEVICE_CLASS(klass
);
382 device_class_set_props(dc
, nrf51_nvm_properties
);
383 dc
->vmsd
= &vmstate_nvm
;
384 dc
->realize
= nrf51_nvm_realize
;
385 dc
->reset
= nrf51_nvm_reset
;
388 static const TypeInfo nrf51_nvm_info
= {
389 .name
= TYPE_NRF51_NVM
,
390 .parent
= TYPE_SYS_BUS_DEVICE
,
391 .instance_size
= sizeof(NRF51NVMState
),
392 .instance_init
= nrf51_nvm_init
,
393 .class_init
= nrf51_nvm_class_init
396 static void nrf51_nvm_register_types(void)
398 type_register_static(&nrf51_nvm_info
);
401 type_init(nrf51_nvm_register_types
)