|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Philippe Retornaz <[email protected]> |
| 3 | + * Copyright (c) 2017 Linaro Limited |
| 4 | + * Copyright (c) 2017 BayLibre, SAS |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +#define LOG_DOMAIN flash_stm32g0 |
| 10 | +#define LOG_LEVEL CONFIG_FLASH_LOG_LEVEL |
| 11 | +#include <logging/log.h> |
| 12 | +LOG_MODULE_REGISTER(LOG_DOMAIN); |
| 13 | + |
| 14 | +#include <kernel.h> |
| 15 | +#include <device.h> |
| 16 | +#include <string.h> |
| 17 | +#include <flash.h> |
| 18 | +#include <init.h> |
| 19 | +#include <soc.h> |
| 20 | + |
| 21 | +#include "flash_stm32.h" |
| 22 | + |
| 23 | + |
| 24 | +#define STM32G0X_PAGE_SHIFT 11 |
| 25 | + |
| 26 | + |
| 27 | +/* |
| 28 | + * offset and len must be aligned on 8 for write, |
| 29 | + * positive and not beyond end of flash |
| 30 | + */ |
| 31 | +bool flash_stm32_valid_range(struct device *dev, off_t offset, u32_t len, |
| 32 | + bool write) |
| 33 | +{ |
| 34 | + return (!write || (offset % 8 == 0 && len % 8 == 0)) && |
| 35 | + flash_stm32_range_exists(dev, offset, len); |
| 36 | +} |
| 37 | + |
| 38 | +/* |
| 39 | + * STM32G0xx devices can have up to 64 2K pages |
| 40 | + */ |
| 41 | +static unsigned int get_page(off_t offset) |
| 42 | +{ |
| 43 | + return offset >> STM32G0X_PAGE_SHIFT; |
| 44 | +} |
| 45 | + |
| 46 | +static int write_dword(struct device *dev, off_t offset, u64_t val) |
| 47 | +{ |
| 48 | + volatile u32_t *flash = (u32_t *)(offset + CONFIG_FLASH_BASE_ADDRESS); |
| 49 | + struct stm32g0x_flash *regs = FLASH_STM32_REGS(dev); |
| 50 | + u32_t tmp; |
| 51 | + int rc; |
| 52 | + |
| 53 | + /* if the control register is locked, do not fail silently */ |
| 54 | + if (regs->cr & FLASH_CR_LOCK) { |
| 55 | + return -EIO; |
| 56 | + } |
| 57 | + |
| 58 | + /* Check that no Flash main memory operation is ongoing */ |
| 59 | + rc = flash_stm32_wait_flash_idle(dev); |
| 60 | + if (rc < 0) { |
| 61 | + return rc; |
| 62 | + } |
| 63 | + |
| 64 | + /* Check if this double word is erased */ |
| 65 | + if (flash[0] != 0xFFFFFFFFUL || |
| 66 | + flash[1] != 0xFFFFFFFFUL) { |
| 67 | + return -EIO; |
| 68 | + } |
| 69 | + |
| 70 | + /* Set the PG bit */ |
| 71 | + regs->cr |= FLASH_CR_PG; |
| 72 | + |
| 73 | + /* Flush the register write */ |
| 74 | + tmp = regs->cr; |
| 75 | + |
| 76 | + /* Perform the data write operation at the desired memory address */ |
| 77 | + flash[0] = (u32_t)val; |
| 78 | + flash[1] = (u32_t)(val >> 32); |
| 79 | + |
| 80 | + /* Wait until the BSY bit is cleared */ |
| 81 | + rc = flash_stm32_wait_flash_idle(dev); |
| 82 | + |
| 83 | + /* Clear the PG bit */ |
| 84 | + regs->cr &= (~FLASH_CR_PG); |
| 85 | + |
| 86 | + return rc; |
| 87 | +} |
| 88 | + |
| 89 | +static int erase_page(struct device *dev, unsigned int page) |
| 90 | +{ |
| 91 | + struct stm32g0x_flash *regs = FLASH_STM32_REGS(dev); |
| 92 | + u32_t tmp; |
| 93 | + int rc; |
| 94 | + |
| 95 | + /* if the control register is locked, do not fail silently */ |
| 96 | + if (regs->cr & FLASH_CR_LOCK) { |
| 97 | + return -EIO; |
| 98 | + } |
| 99 | + |
| 100 | + /* Check that no Flash memory operation is ongoing */ |
| 101 | + rc = flash_stm32_wait_flash_idle(dev); |
| 102 | + if (rc < 0) { |
| 103 | + return rc; |
| 104 | + } |
| 105 | + |
| 106 | + /* Set the PER bit and select the page you wish to erase */ |
| 107 | + regs->cr |= FLASH_CR_PER; |
| 108 | + regs->cr &= ~FLASH_CR_PNB_Msk; |
| 109 | + regs->cr |= ((page % 256) << 3); |
| 110 | + |
| 111 | + /* Set the STRT bit */ |
| 112 | + regs->cr |= FLASH_CR_STRT; |
| 113 | + |
| 114 | + /* flush the register write */ |
| 115 | + tmp = regs->cr; |
| 116 | + |
| 117 | + /* Wait for the BSY bit */ |
| 118 | + rc = flash_stm32_wait_flash_idle(dev); |
| 119 | + |
| 120 | + regs->cr &= ~FLASH_CR_PER; |
| 121 | + |
| 122 | + return rc; |
| 123 | +} |
| 124 | + |
| 125 | +int flash_stm32_block_erase_loop(struct device *dev, unsigned int offset, |
| 126 | + unsigned int len) |
| 127 | +{ |
| 128 | + int i, rc = 0; |
| 129 | + |
| 130 | + i = get_page(offset); |
| 131 | + for (; i <= get_page(offset + len - 1) ; ++i) { |
| 132 | + rc = erase_page(dev, i); |
| 133 | + if (rc < 0) { |
| 134 | + break; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return rc; |
| 139 | +} |
| 140 | + |
| 141 | +int flash_stm32_write_range(struct device *dev, unsigned int offset, |
| 142 | + const void *data, unsigned int len) |
| 143 | +{ |
| 144 | + int i, rc = 0; |
| 145 | + |
| 146 | + for (i = 0; i < len; i += 8, offset += 8) { |
| 147 | + rc = write_dword(dev, offset, ((const u64_t *) data)[i>>3]); |
| 148 | + if (rc < 0) { |
| 149 | + return rc; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return rc; |
| 154 | +} |
| 155 | + |
| 156 | +void flash_stm32_page_layout(struct device *dev, |
| 157 | + const struct flash_pages_layout **layout, |
| 158 | + size_t *layout_size) |
| 159 | +{ |
| 160 | + static struct flash_pages_layout stm32g0_flash_layout = { |
| 161 | + .pages_count = 0, |
| 162 | + .pages_size = 0, |
| 163 | + }; |
| 164 | + |
| 165 | + ARG_UNUSED(dev); |
| 166 | + |
| 167 | + if (stm32g0_flash_layout.pages_count == 0) { |
| 168 | + stm32g0_flash_layout.pages_count = FLASH_SIZE / FLASH_PAGE_SIZE; |
| 169 | + stm32g0_flash_layout.pages_size = FLASH_PAGE_SIZE; |
| 170 | + } |
| 171 | + |
| 172 | + *layout = &stm32g0_flash_layout; |
| 173 | + *layout_size = 1; |
| 174 | +} |
0 commit comments