1 /*
2  * Copyright (c) 2016 Linaro Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT openisa_rv32m1_ftfe
8 #define SOC_NV_FLASH_NODE DT_INST(0, soc_nv_flash)
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/device.h>
12 #include <string.h>
13 #include <zephyr/drivers/flash.h>
14 #include <errno.h>
15 #include <zephyr/init.h>
16 #include <soc.h>
17 #include "flash_priv.h"
18 
19 #include "fsl_common.h"
20 #include "fsl_flash.h"
21 
22 struct flash_priv {
23 	flash_config_t config;
24 	/*
25 	 * HACK: flash write protection is managed in software.
26 	 */
27 	struct k_sem write_lock;
28 	uint32_t pflash_block_base;
29 };
30 
31 static const struct flash_parameters flash_mcux_parameters = {
32 	.write_block_size = FSL_FEATURE_FLASH_PFLASH_BLOCK_WRITE_UNIT_SIZE,
33 	.erase_value = 0xff,
34 };
35 
36 /*
37  * Interrupt vectors could be executed from flash hence the need for locking.
38  * The underlying MCUX driver takes care of copying the functions to SRAM.
39  *
40  * For more information, see the application note below on Read-While-Write
41  * http://cache.freescale.com/files/32bit/doc/app_note/AN4695.pdf
42  *
43  */
44 
flash_mcux_erase(const struct device * dev,off_t offset,size_t len)45 static int flash_mcux_erase(const struct device *dev, off_t offset,
46 			    size_t len)
47 {
48 	struct flash_priv *priv = dev->data;
49 	uint32_t addr;
50 	status_t rc;
51 	unsigned int key;
52 
53 	if (k_sem_take(&priv->write_lock, K_FOREVER)) {
54 		return -EACCES;
55 	}
56 
57 	addr = offset + priv->pflash_block_base;
58 
59 	key = irq_lock();
60 	rc = FLASH_Erase(&priv->config, addr, len, kFLASH_ApiEraseKey);
61 	irq_unlock(key);
62 
63 	k_sem_give(&priv->write_lock);
64 
65 	return (rc == kStatus_Success) ? 0 : -EINVAL;
66 }
67 
flash_mcux_read(const struct device * dev,off_t offset,void * data,size_t len)68 static int flash_mcux_read(const struct device *dev, off_t offset,
69 				void *data, size_t len)
70 {
71 	struct flash_priv *priv = dev->data;
72 	uint32_t addr;
73 
74 	/*
75 	 * The MCUX supports different flash chips whose valid ranges are
76 	 * hidden below the API: until the API export these ranges, we can not
77 	 * do any generic validation
78 	 */
79 	addr = offset + priv->pflash_block_base;
80 
81 	memcpy(data, (void *) addr, len);
82 
83 	return 0;
84 }
85 
flash_mcux_write(const struct device * dev,off_t offset,const void * data,size_t len)86 static int flash_mcux_write(const struct device *dev, off_t offset,
87 				const void *data, size_t len)
88 {
89 	struct flash_priv *priv = dev->data;
90 	uint32_t addr;
91 	status_t rc;
92 	unsigned int key;
93 
94 	if (k_sem_take(&priv->write_lock, K_FOREVER)) {
95 		return -EACCES;
96 	}
97 
98 	addr = offset + priv->pflash_block_base;
99 
100 	key = irq_lock();
101 	rc = FLASH_Program(&priv->config, addr, (uint32_t *) data, len);
102 	irq_unlock(key);
103 
104 	k_sem_give(&priv->write_lock);
105 
106 	return (rc == kStatus_Success) ? 0 : -EINVAL;
107 }
108 
109 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
110 static const struct flash_pages_layout dev_layout = {
111 	.pages_count = DT_REG_SIZE(SOC_NV_FLASH_NODE) /
112 				DT_PROP(SOC_NV_FLASH_NODE, erase_block_size),
113 	.pages_size = DT_PROP(SOC_NV_FLASH_NODE, erase_block_size),
114 };
115 
flash_mcux_pages_layout(const struct device * dev,const struct flash_pages_layout ** layout,size_t * layout_size)116 static void flash_mcux_pages_layout(const struct device *dev,
117 				    const struct flash_pages_layout **layout,
118 				    size_t *layout_size)
119 {
120 	*layout = &dev_layout;
121 	*layout_size = 1;
122 }
123 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
124 
125 static const struct flash_parameters *
flash_mcux_get_parameters(const struct device * dev)126 flash_mcux_get_parameters(const struct device *dev)
127 {
128 	ARG_UNUSED(dev);
129 
130 	return &flash_mcux_parameters;
131 }
132 
133 static struct flash_priv flash_data;
134 
135 static const struct flash_driver_api flash_mcux_api = {
136 	.erase = flash_mcux_erase,
137 	.write = flash_mcux_write,
138 	.read = flash_mcux_read,
139 	.get_parameters = flash_mcux_get_parameters,
140 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
141 	.page_layout = flash_mcux_pages_layout,
142 #endif
143 };
144 
flash_mcux_init(const struct device * dev)145 static int flash_mcux_init(const struct device *dev)
146 {
147 	struct flash_priv *priv = dev->data;
148 	uint32_t pflash_block_base;
149 	status_t rc;
150 
151 	CLOCK_EnableClock(kCLOCK_Mscm);
152 
153 	k_sem_init(&priv->write_lock, 1, 1);
154 
155 	rc = FLASH_Init(&priv->config);
156 
157 	FLASH_GetProperty(&priv->config, kFLASH_PropertyPflashBlockBaseAddr,
158 			(uint32_t *)&pflash_block_base);
159 	priv->pflash_block_base = (uint32_t) pflash_block_base;
160 
161 	return (rc == kStatus_Success) ? 0 : -EIO;
162 }
163 
164 DEVICE_DT_INST_DEFINE(0, flash_mcux_init, NULL,
165 			&flash_data, NULL, POST_KERNEL,
166 			CONFIG_FLASH_INIT_PRIORITY, &flash_mcux_api);
167