1 /*
2  * Copyright (c) 2023 Nuvoton Technology Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _NUVOTON_NPCX_SOC_ESPI_TAF_H_
8 #define _NUVOTON_NPCX_SOC_ESPI_TAF_H_
9 
10 #include <zephyr/device.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /* Transmit buffer for eSPI TAF transaction on NPCX              */
17 /* +-------------+--------------+--------------+---------------+ */
18 /* |   Byte 3    |    Byte 2    |    Byte 1    |    Byte 0     | */
19 /* +-------------+--------------+--------------+---------------+ */
20 /* |   Length    |  Tag |Length |     Type     |    PKT_LEN    | */
21 /* |   [7:0]     |      |[11:8] |              |               | */
22 /* +-------------+--------------+--------------+---------------+ */
23 /* |   Data 3    |     Data 2   |    Data 1    |    Data 0     | */
24 /* +-------------+--------------+--------------+---------------+ */
25 /* |   Data 7    |     Data 6   |    Data 5    |    Data 4     | */
26 /* +-------------+--------------+--------------+---------------+ */
27 /* |    ...      |     ...      |    ...       |      ...      | */
28 /* +-------------+--------------+--------------+---------------+ */
29 /* |  Data 63    |    Data 62   |    Data 61   |    Data 60    | */
30 /* +-------------+--------------+--------------+---------------+ */
31 /* PKT_LEN holds the sum of header (Type, Tag and Length) length */
32 /* and data length                                               */
33 
34 /*
35  * NPCX_TAF_CMP_HEADER_LEN is the preamble length of Type, Length
36  * and Tag (i.e. byte 1~byte 3) for flash access completion packet
37  * on NPCX
38  */
39 #define NPCX_TAF_CMP_HEADER_LEN                    3
40 
41 /* Successful Completion Without Data     */
42 #define CYC_SCS_CMP_WITHOUT_DATA                   0x06
43 /* Successful middle Completion With Data */
44 #define CYC_SCS_CMP_WITH_DATA_MIDDLE               0x09
45 /* Successful first Completion With Data  */
46 #define CYC_SCS_CMP_WITH_DATA_FIRST                0x0B
47 /* Successful last Completion With Data   */
48 #define CYC_SCS_CMP_WITH_DATA_LAST                 0x0D
49 /* Successful only Completion With Data   */
50 #define CYC_SCS_CMP_WITH_DATA_ONLY                 0x0F
51 /* Unsuccessful Completion Without Data   */
52 #define CYC_UNSCS_CMP_WITHOUT_DATA                 0x08
53 /* Unsuccessful Last Completion Without Data */
54 #define CYC_UNSCS_CMP_WITHOUT_DATA_LAST            0x0C
55 /* Unsuccessful Only Completion Without Data */
56 #define CYC_UNSCS_CMP_WITHOUT_DATA_ONLY            0x0E
57 
58 /* ESPI TAF RPMC OP1 instruction */
59 #define ESPI_TAF_RPMC_OP1_CMD                      0x9B
60 /* ESPI TAF RPMC OP2 instruction */
61 #define ESPI_TAF_RPMC_OP2_CMD                      0x96
62 
63 /* Timeout for checking transmit buffer available and no completion was sent */
64 #define NPCX_FLASH_CHK_TIMEOUT                     10000
65 
66 /* Clear RSTBUFHEADS, FLASH_ACC_TX_AVAIL, and FLASH_ACC_NP_FREE */
67 #define NPCX_FLASHCTL_ACCESS_MASK     (~(BIT(NPCX_FLASHCTL_RSTBUFHEADS) |   \
68 					 BIT(NPCX_FLASHCTL_FLASH_NP_FREE) | \
69 					 BIT(NPCX_FLASHCTL_FLASH_TX_AVAIL)))
70 
71 /* Flash Sharing Capability Support */
72 #define NPCX_FLASH_SHARING_CAP_SUPP_CAF            0
73 #define NPCX_FLASH_SHARING_CAP_SUPP_TAF            2
74 #define NPCX_FLASH_SHARING_CAP_SUPP_TAF_AND_CAF    3
75 
76 enum NPCX_ESPI_TAF_REQ {
77 	NPCX_ESPI_TAF_REQ_READ,
78 	NPCX_ESPI_TAF_REQ_WRITE,
79 	NPCX_ESPI_TAF_REQ_ERASE,
80 	NPCX_ESPI_TAF_REQ_RPMC_OP1,
81 	NPCX_ESPI_TAF_REQ_RPMC_OP2,
82 	NPCX_ESPI_TAF_REQ_UNKNOWN,
83 };
84 
85 /* NPCX_ESPI_TAF_ERASE_BLOCK_SIZE_4KB is default */
86 enum NPCX_ESPI_TAF_ERASE_BLOCK_SIZE {
87 	NPCX_ESPI_TAF_ERASE_BLOCK_SIZE_1KB,
88 	NPCX_ESPI_TAF_ERASE_BLOCK_SIZE_2KB,
89 	NPCX_ESPI_TAF_ERASE_BLOCK_SIZE_4KB,
90 	NPCX_ESPI_TAF_ERASE_BLOCK_SIZE_8KB,
91 	NPCX_ESPI_TAF_ERASE_BLOCK_SIZE_16KB,
92 	NPCX_ESPI_TAF_ERASE_BLOCK_SIZE_32KB,
93 	NPCX_ESPI_TAF_ERASE_BLOCK_SIZE_64KB,
94 	NPCX_ESPI_TAF_ERASE_BLOCK_SIZE_128KB,
95 };
96 
97 /* NPCX_ESPI_TAF_MAX_READ_REQ_64B is default */
98 enum NPCX_ESPI_TAF_MAX_READ_REQ {
99 	NPCX_ESPI_TAF_MAX_READ_REQ_64B            = 1,
100 	NPCX_ESPI_TAF_MAX_READ_REQ_128B,
101 	NPCX_ESPI_TAF_MAX_READ_REQ_256B,
102 	NPCX_ESPI_TAF_MAX_READ_REQ_512B,
103 	NPCX_ESPI_TAF_MAX_READ_REQ_1024B,
104 	NPCX_ESPI_TAF_MAX_READ_REQ_2048B,
105 	NPCX_ESPI_TAF_MAX_READ_REQ_4096B,
106 };
107 
108 /*
109  * The configurations of SPI flash are set in FIU module.
110  * Thus, eSPI TAF driver of NPCX does not need additional hardware configuarations.
111  * Therefore, define an empty structure here to comply with espi_saf.h
112  */
113 struct espi_saf_hw_cfg {
114 };
115 
116 struct espi_saf_pr {
117 	uint32_t start;
118 	uint32_t end;
119 	uint16_t override_r;
120 	uint16_t override_w;
121 	uint8_t  master_bm_we;
122 	uint8_t  master_bm_rd;
123 	uint8_t  pr_num;
124 	uint8_t  flags;
125 };
126 
127 struct espi_saf_protection {
128 	size_t nregions;
129 	const struct espi_saf_pr *pregions;
130 };
131 
132 struct espi_taf_npcx_pckt {
133 	uint8_t tag;
134 	uint8_t *data;
135 };
136 
137 struct espi_taf_pckt {
138 	uint8_t  type;
139 	uint8_t  tag;
140 	uint32_t addr;
141 	uint16_t len;
142 	uint32_t src[16];
143 };
144 
145 struct npcx_taf_head {
146 	uint8_t pkt_len;
147 	uint8_t type;
148 	uint8_t tag_hlen;
149 	uint8_t llen;
150 };
151 
152 int npcx_init_taf(const struct device *dev, sys_slist_t *callbacks);
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 
158 #endif
159