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