1 /*
2  * Copyright (c) 2022 Piotr Dymacz
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <zephyr/drivers/flash.h>
9 #include <zephyr/irq.h>
10 #include <zephyr/kernel.h>
11 #include <string.h>
12 
13 #include <driverlib/flash.h>
14 #include <driverlib/vims.h>
15 
16 #define DT_DRV_COMPAT        ti_cc13xx_cc26xx_flash_controller
17 #define SOC_NV_FLASH_NODE    DT_INST(0, soc_nv_flash)
18 
19 #define FLASH_ADDR           DT_REG_ADDR(SOC_NV_FLASH_NODE)
20 #define FLASH_SIZE           DT_REG_SIZE(SOC_NV_FLASH_NODE)
21 #define FLASH_ERASE_SIZE     DT_PROP(SOC_NV_FLASH_NODE, erase_block_size)
22 #define FLASH_WRITE_SIZE     DT_PROP(SOC_NV_FLASH_NODE, write_block_size)
23 
24 #define WRITE_BUFFER_LEN     (32)
25 
26 struct flash_priv {
27 	struct k_sem mutex;
28 };
29 
30 static const struct flash_parameters flash_cc13xx_cc26xx_parameters = {
31 	.write_block_size = FLASH_WRITE_SIZE,
32 	.erase_value = 0xff,
33 };
34 
35 
flash_cc13xx_cc26xx_init(const struct device * dev)36 static int flash_cc13xx_cc26xx_init(const struct device *dev)
37 {
38 	struct flash_priv *priv = dev->data;
39 
40 	k_sem_init(&priv->mutex, 1, 1);
41 
42 	return 0;
43 }
44 
flash_cc13xx_cc26xx_cache_restore(uint32_t vims_mode)45 static void flash_cc13xx_cc26xx_cache_restore(uint32_t vims_mode)
46 {
47 	while (VIMSModeGet(VIMS_BASE) == VIMS_MODE_CHANGING) {
48 		;
49 	}
50 
51 	/* Restore VIMS mode and line buffers */
52 	if (vims_mode != VIMS_MODE_DISABLED) {
53 		VIMSModeSafeSet(VIMS_BASE, vims_mode, true);
54 	}
55 
56 	VIMSLineBufEnable(VIMS_BASE);
57 }
58 
flash_cc13xx_cc26xx_cache_disable(void)59 static uint32_t flash_cc13xx_cc26xx_cache_disable(void)
60 {
61 	uint32_t vims_mode;
62 
63 	/* VIMS and both line buffers should be off during flash update */
64 	VIMSLineBufDisable(VIMS_BASE);
65 
66 	while (VIMSModeGet(VIMS_BASE) == VIMS_MODE_CHANGING) {
67 		;
68 	}
69 
70 	/* Save current VIMS mode for restoring it later */
71 	vims_mode = VIMSModeGet(VIMS_BASE);
72 	if (vims_mode != VIMS_MODE_DISABLED) {
73 		VIMSModeSafeSet(VIMS_BASE, VIMS_MODE_DISABLED, true);
74 	}
75 
76 	return vims_mode;
77 }
78 
flash_cc13xx_cc26xx_range_protected(off_t offs,size_t size)79 static bool flash_cc13xx_cc26xx_range_protected(off_t offs, size_t size)
80 {
81 	off_t sector, end;
82 
83 	sector = (offs / FLASH_ERASE_SIZE) * FLASH_ERASE_SIZE;
84 	end = offs + size;
85 
86 	/*
87 	 * From TI's HAL 'driverlib/flash.h':
88 	 *
89 	 * After write protecting a sector this sector can only be set back
90 	 * to unprotected by a device reset.
91 	 *
92 	 * Return early if any of sectors from requested range is protected.
93 	 */
94 	do {
95 		if (FlashProtectionGet(sector) == FLASH_WRITE_PROTECT) {
96 			return true;
97 		}
98 
99 		sector += FLASH_ERASE_SIZE;
100 	} while (sector < end);
101 
102 	return false;
103 }
104 
flash_cc13xx_cc26xx_erase(const struct device * dev,off_t offs,size_t size)105 static int flash_cc13xx_cc26xx_erase(const struct device *dev, off_t offs,
106 				     size_t size)
107 {
108 	struct flash_priv *priv = dev->data;
109 	uint32_t vims_mode;
110 	unsigned int key;
111 	int i, rc = 0;
112 	size_t cnt;
113 
114 	if (!size) {
115 		return 0;
116 	}
117 
118 	/* Offset and length should be multiple of erase size */
119 	if (((offs % FLASH_ERASE_SIZE) != 0) ||
120 	    ((size % FLASH_ERASE_SIZE) != 0)) {
121 		return -EINVAL;
122 	}
123 
124 	if (flash_cc13xx_cc26xx_range_protected(offs, size)) {
125 		return -EINVAL;
126 	}
127 
128 	if (k_sem_take(&priv->mutex, K_FOREVER)) {
129 		return -EACCES;
130 	}
131 
132 	vims_mode = flash_cc13xx_cc26xx_cache_disable();
133 
134 	/*
135 	 * Disable all interrupts to prevent flash read, from TI's TRF:
136 	 *
137 	 * During a FLASH memory write or erase operation, the FLASH memory
138 	 * must not be read.
139 	 */
140 	key = irq_lock();
141 
142 	/* Erase sector/page one by one, break out in case of an error */
143 	cnt = size / FLASH_ERASE_SIZE;
144 	for (i = 0; i < cnt; i++, offs += FLASH_ERASE_SIZE) {
145 		while (FlashCheckFsmForReady() != FAPI_STATUS_FSM_READY) {
146 			;
147 		}
148 
149 		rc = FlashSectorErase(offs);
150 		if (rc != FAPI_STATUS_SUCCESS) {
151 			rc = -EIO;
152 			break;
153 		}
154 	}
155 
156 	irq_unlock(key);
157 
158 	flash_cc13xx_cc26xx_cache_restore(vims_mode);
159 
160 	k_sem_give(&priv->mutex);
161 
162 	return rc;
163 }
164 
flash_cc13xx_cc26xx_buffered_write(off_t offs,const void * data,size_t size)165 static int flash_cc13xx_cc26xx_buffered_write(off_t offs, const void *data, size_t size)
166 {
167 	uint8_t write_buffer[WRITE_BUFFER_LEN];
168 	int rc;
169 
170 	for (int i = 0; i < size; i += WRITE_BUFFER_LEN) {
171 		size_t len = MIN(size - i, WRITE_BUFFER_LEN);
172 
173 		memcpy(write_buffer, (uint8_t *)data + i, len);
174 		rc = FlashProgram(write_buffer, offs, len);
175 		if (rc != FAPI_STATUS_SUCCESS) {
176 			rc = -EIO;
177 			break;
178 		}
179 		offs += len;
180 	}
181 
182 	return rc;
183 }
184 
flash_cc13xx_cc26xx_write(const struct device * dev,off_t offs,const void * data,size_t size)185 static int flash_cc13xx_cc26xx_write(const struct device *dev, off_t offs,
186 				     const void *data, size_t size)
187 {
188 	struct flash_priv *priv = dev->data;
189 	uint32_t vims_mode;
190 	unsigned int key;
191 	int rc = 0;
192 
193 	if (!size) {
194 		return 0;
195 	}
196 
197 	if ((offs < 0) || (size < 1)) {
198 		return -EINVAL;
199 	}
200 
201 	if ((offs + size) > FLASH_SIZE) {
202 		return -EINVAL;
203 	}
204 
205 	if (flash_cc13xx_cc26xx_range_protected(offs, size)) {
206 		return -EINVAL;
207 	}
208 
209 	if (k_sem_take(&priv->mutex, K_FOREVER)) {
210 		return -EACCES;
211 	}
212 
213 	vims_mode = flash_cc13xx_cc26xx_cache_disable();
214 
215 	key = irq_lock();
216 
217 	while (FlashCheckFsmForReady() != FAPI_STATUS_FSM_READY) {
218 		;
219 	}
220 
221 	/*
222 	 * From TI's HAL 'driverlib/flash.h':
223 	 *
224 	 * The pui8DataBuffer pointer can not point to flash.
225 	 * Use a buffer in this situation.
226 	 */
227 	if ((data >= (void *)FLASH_ADDR) &&
228 		(data <= (void *)(FLASH_ADDR + FLASH_SIZE))) {
229 		rc = flash_cc13xx_cc26xx_buffered_write(offs, data, size);
230 	} else {
231 		rc = FlashProgram((uint8_t *)data, offs, size);
232 		if (rc != FAPI_STATUS_SUCCESS) {
233 			rc = -EIO;
234 		}
235 	}
236 
237 	irq_unlock(key);
238 
239 	flash_cc13xx_cc26xx_cache_restore(vims_mode);
240 
241 	k_sem_give(&priv->mutex);
242 
243 	return rc;
244 }
245 
flash_cc13xx_cc26xx_read(const struct device * dev,off_t offs,void * data,size_t size)246 static int flash_cc13xx_cc26xx_read(const struct device *dev, off_t offs,
247 				    void *data, size_t size)
248 {
249 	ARG_UNUSED(dev);
250 
251 	if (!size) {
252 		return 0;
253 	}
254 
255 	if ((offs < 0) || (size < 1)) {
256 		return -EINVAL;
257 	}
258 
259 	if ((offs + size) > FLASH_SIZE) {
260 		return -EINVAL;
261 	}
262 
263 	memcpy(data, (void *)offs, size);
264 
265 	return 0;
266 }
267 
268 static const struct flash_parameters *
flash_cc13xx_cc26xx_get_parameters(const struct device * dev)269 flash_cc13xx_cc26xx_get_parameters(const struct device *dev)
270 {
271 	ARG_UNUSED(dev);
272 
273 	return &flash_cc13xx_cc26xx_parameters;
274 }
275 
276 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
277 static const struct flash_pages_layout dev_layout = {
278 	.pages_count = FLASH_SIZE / FLASH_ERASE_SIZE,
279 	.pages_size = FLASH_ERASE_SIZE,
280 };
281 
flash_cc13xx_cc26xx_layout(const struct device * dev,const struct flash_pages_layout ** layout,size_t * layout_size)282 static void flash_cc13xx_cc26xx_layout(const struct device *dev,
283 				       const struct flash_pages_layout **layout,
284 				       size_t *layout_size)
285 {
286 	*layout = &dev_layout;
287 	*layout_size = 1;
288 }
289 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
290 
291 static DEVICE_API(flash, flash_cc13xx_cc26xx_api) = {
292 	.erase = flash_cc13xx_cc26xx_erase,
293 	.write = flash_cc13xx_cc26xx_write,
294 	.read = flash_cc13xx_cc26xx_read,
295 	.get_parameters = flash_cc13xx_cc26xx_get_parameters,
296 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
297 	.page_layout = flash_cc13xx_cc26xx_layout,
298 #endif
299 };
300 
301 static struct flash_priv flash_data;
302 
303 DEVICE_DT_INST_DEFINE(0, flash_cc13xx_cc26xx_init, NULL, &flash_data, NULL,
304 		      POST_KERNEL, CONFIG_FLASH_INIT_PRIORITY,
305 		      &flash_cc13xx_cc26xx_api);
306