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
12 #include "fsl_flash.h"
13 #include "fsl_cache.h"
14 #include "pin_mux.h"
15
16 /*! @brief Flash driver Structure */
17 static flash_config_t s_flashDriver;
18
19 /* API - initialize 'mflash' */
mflash_drv_init(void)20 int32_t mflash_drv_init(void)
21 {
22 int32_t result = 0;
23
24 /* Clean up Flash driver Structure*/
25 memset(&s_flashDriver, 0, sizeof(flash_config_t));
26
27 /* Setup flash driver structure for device and initialize variables. */
28 result = FLASH_Init(&s_flashDriver);
29 return result;
30 }
31
32 /* API - Erase single sector */
mflash_drv_sector_erase(uint32_t sector_addr)33 int32_t mflash_drv_sector_erase(uint32_t sector_addr)
34 {
35 int32_t result = 0;
36 if (0 == mflash_drv_is_sector_aligned(sector_addr))
37 return kStatus_InvalidArgument;
38
39 result = FLASH_Erase(&s_flashDriver, sector_addr, MFLASH_SECTOR_SIZE, kFLASH_ApiEraseKey);
40 DCACHE_InvalidateByRange(sector_addr, MFLASH_SECTOR_SIZE);
41
42 return result;
43 }
44
45 /* Internal - write single sector */
mflash_drv_sector_program(uint32_t sector_addr,uint32_t * sector_data,uint32_t sector_data_length)46 static int32_t mflash_drv_sector_program(uint32_t sector_addr, uint32_t *sector_data, uint32_t sector_data_length)
47 {
48 int32_t result = 0;
49 if (0 == mflash_drv_is_sector_aligned(sector_addr))
50 return kStatus_InvalidArgument;
51
52 result = FLASH_Program(&s_flashDriver, (uint32_t)sector_addr, (uint8_t *)sector_data, sector_data_length);
53 DCACHE_InvalidateByRange(sector_addr, sector_data_length);
54
55 return result;
56 }
57
58 /* API - Calling wrapper, page is the same as sector on this platform */
mflash_drv_page_program(uint32_t page_addr,uint32_t * data)59 int32_t mflash_drv_page_program(uint32_t page_addr, uint32_t *data)
60 {
61 return mflash_drv_sector_program(page_addr, data, MFLASH_PAGE_SIZE);
62 }
63
64 /* API - Read data */
mflash_drv_read(uint32_t addr,uint32_t * buffer,uint32_t len)65 int32_t mflash_drv_read(uint32_t addr, uint32_t *buffer, uint32_t len)
66 {
67 memcpy(buffer, (void *)addr, len);
68 return kStatus_Success;
69 }
70
71 /* API - Get pointer to FLASH region */
mflash_drv_phys2log(uint32_t addr,uint32_t len)72 void *mflash_drv_phys2log(uint32_t addr, uint32_t len)
73 {
74 /* FLASH is directly mapped in the address space */
75 return (void *)(addr);
76 }
77
78 /* API - Get pointer to FLASH region */
mflash_drv_log2phys(void * ptr,uint32_t len)79 uint32_t mflash_drv_log2phys(void *ptr, uint32_t len)
80 {
81 /* FLASH is directly mapped in the address space */
82 return ((uint32_t)ptr);
83 }
84