Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | /*
* Copyright (c) 2016 Linaro Limited
* 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include <nanokernel.h>
#include <device.h>
#include <init.h>
#include <soc.h>
#include <flash.h>
#include <string.h>
static inline bool is_aligned_32(uint32_t data)
{
return (data & 0x3) ? false : true;
}
static inline bool is_addr_valid(off_t addr, size_t len)
{
if (addr + len > NRF_FICR->CODEPAGESIZE * NRF_FICR->CODESIZE ||
addr < 0) {
return false;
}
return true;
}
static void nvmc_wait_ready(void)
{
while (NRF_NVMC->READY == NVMC_READY_READY_Busy) {
;
}
}
static int flash_nrf5_read(struct device *dev, off_t addr,
void *data, size_t len)
{
if (!is_addr_valid(addr, len)) {
return -EINVAL;
}
memcpy(data, (void *)addr, len);
return 0;
}
static int flash_nrf5_write(struct device *dev, off_t addr,
const void *data, size_t len)
{
uint32_t addr_word;
uint32_t tmp_word;
void *data_word;
uint32_t remaining = len;
uint32_t count = 0;
if (!is_addr_valid(addr, len)) {
return -EINVAL;
}
/* Start with a word-aligned address and handle the offset */
addr_word = addr & ~0x3;
/* If not aligned, read first word, update and write it back */
if (!is_aligned_32(addr)) {
tmp_word = *(uint32_t *)(addr_word);
count = sizeof(uint32_t) - (addr & 0x3);
if (count > len) {
count = len;
}
memcpy((uint8_t *)&tmp_word + (addr & 0x3), data, count);
nvmc_wait_ready();
*(uint32_t *)addr_word = tmp_word;
addr_word = addr + count;
remaining -= count;
}
/* Write all the 4-byte aligned data */
data_word = (void *) data + count;
while (remaining >= sizeof(uint32_t)) {
nvmc_wait_ready();
*(uint32_t *)addr_word = *(uint32_t *)data_word;
addr_word += sizeof(uint32_t);
data_word += sizeof(uint32_t);
remaining -= sizeof(uint32_t);
}
/* Write remaining data */
if (remaining) {
tmp_word = *(uint32_t *)(addr_word);
memcpy((uint8_t *)&tmp_word, data_word, remaining);
nvmc_wait_ready();
*(uint32_t *)addr_word = tmp_word;
}
nvmc_wait_ready();
return 0;
}
static int flash_nrf5_erase(struct device *dev, off_t addr, size_t size)
{
uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
uint32_t n_pages = size / pg_size;
/* Erase can only be done per page */
if (((addr % pg_size) != 0) || ((size % pg_size) != 0) ||
(n_pages == 0)) {
return -EINVAL;
}
if (!is_addr_valid(addr, size)) {
return -EINVAL;
}
/* Erase uses a specific configuration register */
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos;
nvmc_wait_ready();
for (uint32_t i = 0; i < n_pages; i++) {
NRF_NVMC->ERASEPAGE = (uint32_t)addr + (i * pg_size);
nvmc_wait_ready();
}
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
nvmc_wait_ready();
return 0;
}
static int flash_nrf5_write_protection(struct device *dev, bool enable)
{
if (enable) {
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
} else {
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
}
nvmc_wait_ready();
return 0;
}
static const struct flash_driver_api flash_nrf5_api = {
.read = flash_nrf5_read,
.write = flash_nrf5_write,
.erase = flash_nrf5_erase,
.write_protection = flash_nrf5_write_protection,
};
static int nrf5_flash_init(struct device *dev)
{
dev->driver_api = &flash_nrf5_api;
return 0;
}
DEVICE_INIT(nrf5_flash, CONFIG_SOC_FLASH_NRF5_DEV_NAME, nrf5_flash_init,
NULL, NULL, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
|