23
23
#include <zephyr/logging/log.h>
24
24
LOG_MODULE_REGISTER (flash_npcx_fiu_nor , CONFIG_FLASH_LOG_LEVEL );
25
25
26
+ #if defined(CONFIG_ESPI_TAF )
27
+ static const struct device * const espi_dev = DEVICE_DT_GET (DT_NODELABEL (espi0 ));
28
+ #endif
29
+
26
30
#define BLOCK_64K_SIZE KB(64)
27
31
#define BLOCK_4K_SIZE KB(4)
28
32
@@ -47,6 +51,8 @@ struct flash_npcx_nor_config {
47
51
48
52
/* Device data */
49
53
struct flash_npcx_nor_data {
54
+ /* mutex for flash write and erase operation */
55
+ struct k_sem nor_flash_sem ;
50
56
/* Specific control operation for Quad-SPI Nor Flash */
51
57
uint32_t operation ;
52
58
};
@@ -192,16 +198,32 @@ static int flash_npcx_nor_write_status_regs(const struct device *dev, uint8_t *s
192
198
#if defined(CONFIG_FLASH_JESD216_API )
193
199
static int flash_npcx_nor_read_jedec_id (const struct device * dev , uint8_t * id )
194
200
{
201
+ int ret ;
202
+
195
203
if (id == NULL ) {
196
204
return - EINVAL ;
197
205
}
198
206
199
- return flash_npcx_uma_read (dev , SPI_NOR_CMD_RDID , id , SPI_NOR_MAX_ID_LEN );
207
+ #if defined(CONFIG_ESPI_TAF )
208
+ if (espi_taf_npcx_block (espi_dev , true) != 0 ) {
209
+ LOG_ERR ("TAF LOCK TIMEOUT" );
210
+ return - ETIMEDOUT ;
211
+ }
212
+ #endif
213
+
214
+ ret = flash_npcx_uma_read (dev , SPI_NOR_CMD_RDID , id , SPI_NOR_MAX_ID_LEN );
215
+
216
+ #if defined(CONFIG_ESPI_TAF )
217
+ espi_taf_npcx_block (espi_dev , false);
218
+ #endif
219
+
220
+ return ret ;
200
221
}
201
222
202
223
static int flash_npcx_nor_read_sfdp (const struct device * dev , off_t addr ,
203
224
void * data , size_t size )
204
225
{
226
+ int ret ;
205
227
uint8_t sfdp_addr [4 ];
206
228
struct npcx_uma_cfg cfg = { .opcode = JESD216_CMD_READ_SFDP ,
207
229
.tx_buf = sfdp_addr ,
@@ -217,8 +239,22 @@ static int flash_npcx_nor_read_sfdp(const struct device *dev, off_t addr,
217
239
sfdp_addr [0 ] = (addr >> 16 ) & 0xff ;
218
240
sfdp_addr [1 ] = (addr >> 8 ) & 0xff ;
219
241
sfdp_addr [2 ] = addr & 0xff ;
220
- return flash_npcx_uma_transceive (dev , & cfg , NPCX_UMA_ACCESS_WRITE |
242
+
243
+ #if defined(CONFIG_ESPI_TAF )
244
+ if (espi_taf_npcx_block (espi_dev , true) != 0 ) {
245
+ LOG_ERR ("TAF LOCK TIMEOUT" );
246
+ return - ETIMEDOUT ;
247
+ }
248
+ #endif
249
+
250
+ ret = flash_npcx_uma_transceive (dev , & cfg , NPCX_UMA_ACCESS_WRITE |
221
251
NPCX_UMA_ACCESS_READ );
252
+
253
+ #if defined(CONFIG_ESPI_TAF )
254
+ espi_taf_npcx_block (espi_dev , false);
255
+ #endif
256
+
257
+ return ret ;
222
258
}
223
259
#endif /* CONFIG_FLASH_JESD216_API */
224
260
@@ -260,32 +296,47 @@ static int flash_npcx_nor_read(const struct device *dev, off_t addr,
260
296
static int flash_npcx_nor_erase (const struct device * dev , off_t addr , size_t size )
261
297
{
262
298
const struct flash_npcx_nor_config * config = dev -> config ;
299
+ struct flash_npcx_nor_data * dev_data = dev -> data ;
263
300
int ret = 0 ;
264
301
302
+ k_sem_take (& dev_data -> nor_flash_sem , K_FOREVER );
303
+
265
304
/* Out of the region of nor flash device? */
266
305
if (!is_within_region (addr , size , 0 , config -> flash_size )) {
267
306
LOG_ERR ("Addr %ld, size %d are out of range" , addr , size );
268
- return - EINVAL ;
307
+ ret = - EINVAL ;
308
+ goto out_nor_erase ;
269
309
}
270
310
271
311
/* address must be sector-aligned */
272
312
if (!SPI_NOR_IS_SECTOR_ALIGNED (addr )) {
273
313
LOG_ERR ("Addr %ld is not sector-aligned" , addr );
274
- return - EINVAL ;
314
+ ret = - EINVAL ;
315
+ goto out_nor_erase ;
275
316
}
276
317
277
318
/* size must be a multiple of sectors */
278
319
if ((size % BLOCK_4K_SIZE ) != 0 ) {
279
320
LOG_ERR ("Size %d is not a multiple of sectors" , size );
280
- return - EINVAL ;
321
+ ret = - EINVAL ;
322
+ goto out_nor_erase ;
281
323
}
282
324
325
+ #if defined(CONFIG_ESPI_TAF )
326
+ if (espi_taf_npcx_block (espi_dev , true) != 0 ) {
327
+ LOG_ERR ("TAF LOCK TIMEOUT" );
328
+ ret = - ETIMEDOUT ;
329
+ goto out_nor_erase ;
330
+ }
331
+ #endif
332
+
283
333
/* Select erase opcode by size */
284
334
if (size == config -> flash_size ) {
285
335
flash_npcx_uma_cmd_only (dev , SPI_NOR_CMD_WREN );
286
336
/* Send chip erase command */
287
337
flash_npcx_uma_cmd_only (dev , SPI_NOR_CMD_CE );
288
- return flash_npcx_nor_wait_until_ready (dev );
338
+ ret = flash_npcx_nor_wait_until_ready (dev );
339
+ goto out_nor_erase_unblock ;
289
340
}
290
341
291
342
while (size > 0 ) {
@@ -305,6 +356,12 @@ static int flash_npcx_nor_erase(const struct device *dev, off_t addr, size_t siz
305
356
break ;
306
357
}
307
358
}
359
+ out_nor_erase_unblock :
360
+ #if defined(CONFIG_ESPI_TAF )
361
+ espi_taf_npcx_block (espi_dev , false);
362
+ #endif
363
+ out_nor_erase :
364
+ k_sem_give (& dev_data -> nor_flash_sem );
308
365
309
366
return ret ;
310
367
}
@@ -313,12 +370,16 @@ static int flash_npcx_nor_write(const struct device *dev, off_t addr,
313
370
const void * data , size_t size )
314
371
{
315
372
const struct flash_npcx_nor_config * config = dev -> config ;
373
+ struct flash_npcx_nor_data * dev_data = dev -> data ;
316
374
uint8_t * tx_buf = (uint8_t * )data ;
317
375
int ret = 0 ;
318
376
size_t sz_write ;
319
377
378
+ k_sem_take (& dev_data -> nor_flash_sem , K_FOREVER );
379
+
320
380
/* Out of the region of nor flash device? */
321
381
if (!is_within_region (addr , size , 0 , config -> flash_size )) {
382
+ k_sem_give (& dev_data -> nor_flash_sem );
322
383
return - EINVAL ;
323
384
}
324
385
@@ -338,20 +399,38 @@ static int flash_npcx_nor_write(const struct device *dev, off_t addr,
338
399
}
339
400
340
401
while (size > 0 ) {
402
+ #if defined(CONFIG_ESPI_TAF )
403
+ if (espi_taf_npcx_block (espi_dev , true) != 0 ) {
404
+ LOG_ERR ("TAF LOCK TIMEOUT" );
405
+ k_sem_give (& dev_data -> nor_flash_sem );
406
+
407
+ return - ETIMEDOUT ;
408
+ }
409
+ #endif
341
410
/* Start to write */
342
411
flash_npcx_uma_cmd_only (dev , SPI_NOR_CMD_WREN );
343
412
ret = flash_npcx_uma_write_by_addr (dev , SPI_NOR_CMD_PP , tx_buf ,
344
413
sz_write , addr );
345
414
if (ret != 0 ) {
415
+ #if defined(CONFIG_ESPI_TAF )
416
+ espi_taf_npcx_block (espi_dev , false);
417
+ #endif
346
418
break ;
347
419
}
348
420
349
421
/* Wait for writing completed */
350
422
ret = flash_npcx_nor_wait_until_ready (dev );
351
423
if (ret != 0 ) {
424
+ #if defined(CONFIG_ESPI_TAF )
425
+ espi_taf_npcx_block (espi_dev , false);
426
+ #endif
352
427
break ;
353
428
}
354
429
430
+ #if defined(CONFIG_ESPI_TAF )
431
+ espi_taf_npcx_block (espi_dev , false);
432
+ #endif
433
+
355
434
size -= sz_write ;
356
435
tx_buf += sz_write ;
357
436
addr += sz_write ;
@@ -363,6 +442,8 @@ static int flash_npcx_nor_write(const struct device *dev, off_t addr,
363
442
}
364
443
}
365
444
445
+ k_sem_give (& dev_data -> nor_flash_sem );
446
+
366
447
return ret ;
367
448
}
368
449
@@ -462,7 +543,16 @@ static int flash_npcx_nor_ex_op(const struct device *dev, uint16_t code,
462
543
}
463
544
#endif
464
545
546
+ #if defined(CONFIG_ESPI_TAF )
547
+ if (espi_taf_npcx_block (espi_dev , true) != 0 ) {
548
+ LOG_ERR ("TAF LOCK TIMEOUT" );
549
+ return - ETIMEDOUT ;
550
+ }
551
+ #endif
465
552
ret = flash_npcx_nor_ex_exec_uma (dev , op_in , op_out );
553
+ #if defined(CONFIG_ESPI_TAF )
554
+ espi_taf_npcx_block (espi_dev , false);
555
+ #endif
466
556
#ifdef CONFIG_USERSPACE
467
557
if (ret == 0 && syscall_trap ) {
468
558
K_OOPS (k_usermode_to_copy (out , op_out , sizeof (out_copy )));
@@ -532,6 +622,7 @@ static DEVICE_API(flash, flash_npcx_nor_driver_api) = {
532
622
static int flash_npcx_nor_init (const struct device * dev )
533
623
{
534
624
const struct flash_npcx_nor_config * config = dev -> config ;
625
+ struct flash_npcx_nor_data * dev_data = dev -> data ;
535
626
int ret ;
536
627
537
628
if (!IS_ENABLED (CONFIG_FLASH_NPCX_FIU_NOR_INIT )) {
@@ -594,6 +685,8 @@ static int flash_npcx_nor_init(const struct device *dev)
594
685
qspi_npcx_fiu_set_spi_size (config -> qspi_bus , & config -> qspi_cfg );
595
686
}
596
687
688
+ k_sem_init (& dev_data -> nor_flash_sem , 1 , 1 );
689
+
597
690
return 0 ;
598
691
}
599
692
0 commit comments