1 /*
2  *
3  * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  */
8 
9 #ifndef CFI_DRV_H
10 #define CFI_DRV_H
11 
12 #include <stdint.h>
13 #include <stdbool.h>
14 
15 /* First bus cycle */
16 #define NOR_CMD_READ_ARRAY		0xFF
17 #define NOR_CMD_READ_ID_CODE		0x90
18 #define NOR_CMD_READ_QUERY		0x98
19 #define NOR_CMD_READ_STATUS_REG		0x70
20 #define NOR_CMD_CLEAR_STATUS_REG	0x50
21 #define NOR_CMD_WRITE_TO_BUFFER		0xE8
22 #define NOR_CMD_WORD_PROGRAM		0x40
23 #define NOR_CMD_BLOCK_ERASE		0x20
24 #define NOR_CMD_LOCK_UNLOCK		0x60
25 #define NOR_CMD_BLOCK_ERASE_ACK		0xD0
26 
27 /* Second bus cycle */
28 #define NOR_LOCK_BLOCK			0x01
29 #define NOR_UNLOCK_BLOCK		0xD0
30 
31 /* Status register bits */
32 #define NOR_DWS				(1 << 7)
33 #define NOR_ESS				(1 << 6)
34 #define NOR_ES				(1 << 5)
35 #define NOR_PS				(1 << 4)
36 #define NOR_VPPS			(1 << 3)
37 #define NOR_PSS				(1 << 2)
38 #define NOR_BLS				(1 << 1)
39 #define NOR_BWS				(1 << 0)
40 
41 /* Set 1 to enable debug messages */
42 #define DEBUG_CFI_FLASH         0
43 
44 #include <stdio.h>
45 #if (DEBUG_CFI_FLASH == 1)
46     #define CFI_FLASH_LOG_MSG(f_, ...) printf((f_), ##__VA_ARGS__)
47 #else
48     #define CFI_FLASH_LOG_MSG(f_, ...)
49 #endif
50 
51 enum cfi_error_t {
52     CFI_ERR_NONE,
53     CFI_ERR_WRONG_ARGUMENT,
54     CFI_ERR_NOT_INITIALIZED,
55     CFI_ERR_DEV_BUSY,
56     CFI_ERR_GENERAL_IO,
57     CFI_ERR_DEV_PROTECTED
58 };
59 
60 /**
61  * \brief CFI device configuration structure
62  */
63 struct cfi_dev_cfg_t {
64     const uint32_t base; /*!< CFI base address */
65 };
66 
67 /**
68  * \brief CFI controller device structure
69  */
70 struct cfi_dev_t {
71     const struct cfi_dev_cfg_t* const cfg;
72 };
73 
74 /* Public API */
75 void nor_send_cmd(uintptr_t base_addr, unsigned long cmd);
76 void nor_send_cmd_byte(uintptr_t base_addr, unsigned long cmd);
77 enum cfi_error_t nor_word_program(uintptr_t base_addr, unsigned long data);
78 enum cfi_error_t nor_byte_program(uintptr_t base_addr, uint8_t data);
79 enum cfi_error_t nor_lock(uintptr_t base_addr);
80 enum cfi_error_t nor_unlock(uintptr_t base_addr);
81 enum cfi_error_t nor_erase(uintptr_t base_addr);
82 unsigned int nor_id_check(uintptr_t base_addr);
83 uint8_t nor_cfi_reg_read(uintptr_t addr);
84 void nor_cfi_reg_write(uintptr_t addr, uint32_t value);
85 #endif
86