1 /*
2  * Copyright 2017-2020 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stdint.h>
8 #include <string.h>
9 
10 #include "mflash_drv.h"
11 #include "fsl_flash.h"
12 #include "fsl_flexspi_nor_flash.h"
13 #include "pin_mux.h"
14 
15 #define FLASH_BASE_ADDR 0x08000000
16 
17 static flexspi_nor_config_t flashConfig;
18 
19 /* API - initialize 'mflash' */
mflash_drv_init(void)20 int32_t mflash_drv_init(void)
21 {
22     status_t result;
23 
24     serial_nor_config_option_t option;
25     option.option0.U = 0xc0000001U;
26     option.option1.U = 0x00000000;
27 
28     result = FLEXSPI_NorFlash_GetConfig(0, &flashConfig, &option);
29     if (result != kStatus_Success)
30         return result;
31 
32     result = FLEXSPI_NorFlash_Init(0, &flashConfig);
33     if (result != kStatus_Success)
34         return result;
35 
36     return result;
37 }
38 
39 /* API - Erase single sector */
mflash_drv_sector_erase(uint32_t sector_addr)40 int32_t mflash_drv_sector_erase(uint32_t sector_addr)
41 {
42     assert(sector_addr >= FLASH_BASE_ADDR);
43     if (0 == mflash_drv_is_sector_aligned(sector_addr))
44         return kStatus_InvalidArgument;
45 
46     return FLEXSPI_NorFlash_Erase(0, (flexspi_nor_config_t *)&flashConfig, (sector_addr - FLASH_BASE_ADDR),
47                                   flashConfig.sectorSize);
48 }
49 
50 /* API - Page program */
mflash_drv_page_program(uint32_t page_addr,uint32_t * data)51 int32_t mflash_drv_page_program(uint32_t page_addr, uint32_t *data)
52 {
53     assert(page_addr >= FLASH_BASE_ADDR);
54     if (0 == mflash_drv_is_page_aligned(page_addr))
55         return kStatus_InvalidArgument;
56 
57     return FLEXSPI_NorFlash_ProgramPage(0, &flashConfig, (page_addr - FLASH_BASE_ADDR), (uint32_t *)data);
58 }
59 
60 /* API - Read data */
mflash_drv_read(uint32_t addr,uint32_t * buffer,uint32_t len)61 int32_t mflash_drv_read(uint32_t addr, uint32_t *buffer, uint32_t len)
62 {
63     memcpy(buffer, (void *)addr, len);
64 
65     return kStatus_Success;
66 }
67 
68 /* API - Get pointer to FLASH region */
mflash_drv_phys2log(uint32_t addr,uint32_t len)69 void *mflash_drv_phys2log(uint32_t addr, uint32_t len)
70 {
71 #if 0
72     if (mflash_drv_is_readable(addr) != 0)
73         return NULL;
74 #endif
75 
76     /* FLASH is directly mapped in the address space */
77     return (void *)(addr);
78 }
79 
80 /* API - Get pointer to FLASH region */
mflash_drv_log2phys(void * ptr,uint32_t len)81 uint32_t mflash_drv_log2phys(void *ptr, uint32_t len)
82 {
83     /* FLASH is directly mapped in the address space */
84     return ((uint32_t)ptr);
85 }
86