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