@@ -36,17 +36,6 @@ struct spi_nor_data {
3636 struct k_sem sem ;
3737};
3838
39- #if defined(CONFIG_MULTITHREADING )
40- #define SYNC_INIT () k_sem_init( \
41- &((struct spi_nor_data *)dev->driver_data)->sem, 1, UINT_MAX)
42- #define SYNC_LOCK () k_sem_take(&driver_data->sem, K_FOREVER)
43- #define SYNC_UNLOCK () k_sem_give(&driver_data->sem)
44- #else
45- #define SYNC_INIT ()
46- #define SYNC_LOCK ()
47- #define SYNC_UNLOCK ()
48- #endif
49-
5039/*
5140 * @brief Send an SPI command
5241 *
@@ -110,6 +99,32 @@ static int spi_nor_access(const struct device *const dev,
11099#define spi_nor_cmd_addr_write (dev , opcode , addr , src , length ) \
111100 spi_nor_access(dev, opcode, true, addr, (void *)src, length, true)
112101
102+ /* Everything necessary to acquire owning access to the device.
103+ *
104+ * For now this means taking the lock.
105+ */
106+ static void acquire_device (struct device * dev )
107+ {
108+ if (IS_ENABLED (CONFIG_MULTITHREADING )) {
109+ struct spi_nor_data * const driver_data = dev -> driver_data ;
110+
111+ k_sem_take (& driver_data -> sem , K_FOREVER );
112+ }
113+ }
114+
115+ /* Everything necessary to release access to the device.
116+ *
117+ * For now, this means releasing the lock.
118+ */
119+ static void release_device (struct device * dev )
120+ {
121+ if (IS_ENABLED (CONFIG_MULTITHREADING )) {
122+ struct spi_nor_data * const driver_data = dev -> driver_data ;
123+
124+ k_sem_give (& driver_data -> sem );
125+ }
126+ }
127+
113128/**
114129 * @brief Retrieve the Flash JEDEC ID and compare it with the one expected
115130 *
@@ -155,7 +170,6 @@ static int spi_nor_wait_until_ready(struct device *dev)
155170static int spi_nor_read (struct device * dev , off_t addr , void * dest ,
156171 size_t size )
157172{
158- struct spi_nor_data * const driver_data = dev -> driver_data ;
159173 const struct spi_nor_config * params = dev -> config -> config_info ;
160174 int ret ;
161175
@@ -164,29 +178,28 @@ static int spi_nor_read(struct device *dev, off_t addr, void *dest,
164178 return - EINVAL ;
165179 }
166180
167- SYNC_LOCK ( );
181+ acquire_device ( dev );
168182
169183 spi_nor_wait_until_ready (dev );
170184
171185 ret = spi_nor_cmd_addr_read (dev , SPI_NOR_CMD_READ , addr , dest , size );
172186
173- SYNC_UNLOCK ( );
187+ release_device ( dev );
174188 return ret ;
175189}
176190
177191static int spi_nor_write (struct device * dev , off_t addr , const void * src ,
178192 size_t size )
179193{
180- struct spi_nor_data * const driver_data = dev -> driver_data ;
181194 const struct spi_nor_config * params = dev -> config -> config_info ;
182- int ret ;
195+ int ret = 0 ;
183196
184197 /* should be between 0 and flash size */
185198 if ((addr < 0 ) || ((size + addr ) > params -> size )) {
186199 return - EINVAL ;
187200 }
188201
189- SYNC_LOCK ( );
202+ acquire_device ( dev );
190203
191204 while (size > 0 ) {
192205 size_t to_write = size ;
@@ -206,8 +219,7 @@ static int spi_nor_write(struct device *dev, off_t addr, const void *src,
206219 ret = spi_nor_cmd_addr_write (dev , SPI_NOR_CMD_PP , addr ,
207220 src , to_write );
208221 if (ret != 0 ) {
209- SYNC_UNLOCK ();
210- return ret ;
222+ goto out ;
211223 }
212224
213225 size -= to_write ;
@@ -217,21 +229,22 @@ static int spi_nor_write(struct device *dev, off_t addr, const void *src,
217229 spi_nor_wait_until_ready (dev );
218230 }
219231
220- SYNC_UNLOCK ();
221- return 0 ;
232+ out :
233+ release_device (dev );
234+ return ret ;
222235}
223236
224237static int spi_nor_erase (struct device * dev , off_t addr , size_t size )
225238{
226- struct spi_nor_data * const driver_data = dev -> driver_data ;
227239 const struct spi_nor_config * params = dev -> config -> config_info ;
240+ int ret = 0 ;
228241
229242 /* should be between 0 and flash size */
230243 if ((addr < 0 ) || ((size + addr ) > params -> size )) {
231244 return - ENODEV ;
232245 }
233246
234- SYNC_LOCK ( );
247+ acquire_device ( dev );
235248
236249 while (size ) {
237250 /* write enable */
@@ -264,26 +277,26 @@ static int spi_nor_erase(struct device *dev, off_t addr, size_t size)
264277 size -= SPI_NOR_SECTOR_SIZE ;
265278 } else {
266279 /* minimal erase size is at least a sector size */
267- SYNC_UNLOCK ();
268280 LOG_DBG ("unsupported at 0x%lx size %zu" , (long )addr ,
269281 size );
270- return - EINVAL ;
282+ ret = - EINVAL ;
283+ goto out ;
271284 }
272285
273286 spi_nor_wait_until_ready (dev );
274287 }
275288
276- SYNC_UNLOCK ();
289+ out :
290+ release_device (dev );
277291
278- return 0 ;
292+ return ret ;
279293}
280294
281295static int spi_nor_write_protection_set (struct device * dev , bool write_protect )
282296{
283- struct spi_nor_data * const driver_data = dev -> driver_data ;
284297 int ret ;
285298
286- SYNC_LOCK ( );
299+ acquire_device ( dev );
287300
288301 spi_nor_wait_until_ready (dev );
289302
@@ -296,7 +309,7 @@ static int spi_nor_write_protection_set(struct device *dev, bool write_protect)
296309 ret = spi_nor_cmd_write (dev , SPI_NOR_CMD_ULBPR );
297310 }
298311
299- SYNC_UNLOCK ( );
312+ release_device ( dev );
300313
301314 return ret ;
302315}
@@ -340,7 +353,6 @@ static int spi_nor_configure(struct device *dev)
340353 return - ENODEV ;
341354 }
342355
343-
344356 return 0 ;
345357}
346358
@@ -352,7 +364,11 @@ static int spi_nor_configure(struct device *dev)
352364 */
353365static int spi_nor_init (struct device * dev )
354366{
355- SYNC_INIT ();
367+ if (IS_ENABLED (CONFIG_MULTITHREADING )) {
368+ struct spi_nor_data * const driver_data = dev -> driver_data ;
369+
370+ k_sem_init (& driver_data -> sem , 1 , UINT_MAX );
371+ }
356372
357373 return spi_nor_configure (dev );
358374}
0 commit comments