xref: /aosp_15_r20/external/flashrom/spi.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2007, 2008, 2009, 2010, 2011 Carl-Daniel Hailfinger
5  * Copyright (C) 2008 coresystems GmbH
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16 
17 /*
18  * Contains the generic SPI framework
19  */
20 
21 #include <strings.h>
22 #include <string.h>
23 #include "flash.h"
24 #include "flashchips.h"
25 #include "chipdrivers.h"
26 #include "programmer.h"
27 #include "spi.h"
28 
default_spi_send_command(const struct flashctx * flash,unsigned int writecnt,unsigned int readcnt,const unsigned char * writearr,unsigned char * readarr)29 static int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
30 			     unsigned int readcnt,
31 			     const unsigned char *writearr,
32 			     unsigned char *readarr)
33 {
34 	struct spi_command cmd[] = {
35 	{
36 		.writecnt = writecnt,
37 		.readcnt = readcnt,
38 		.writearr = writearr,
39 		.readarr = readarr,
40 	}, {
41 		.writecnt = 0,
42 		.writearr = NULL,
43 		.readcnt = 0,
44 		.readarr = NULL,
45 	}};
46 
47 	return spi_send_multicommand(flash, cmd);
48 }
49 
default_spi_send_multicommand(const struct flashctx * flash,struct spi_command * cmds)50 static int default_spi_send_multicommand(const struct flashctx *flash,
51 				  struct spi_command *cmds)
52 {
53 	int result = 0;
54 	for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
55 		result = spi_send_command(flash, cmds->writecnt, cmds->readcnt,
56 					  cmds->writearr, cmds->readarr);
57 	}
58 	return result;
59 }
60 
spi_send_command(const struct flashctx * flash,unsigned int writecnt,unsigned int readcnt,const unsigned char * writearr,unsigned char * readarr)61 int spi_send_command(const struct flashctx *flash, unsigned int writecnt,
62 		     unsigned int readcnt, const unsigned char *writearr,
63 		     unsigned char *readarr)
64 {
65 	if (flash->mst->spi.command)
66 		return flash->mst->spi.command(flash, writecnt, readcnt, writearr, readarr);
67 	return default_spi_send_command(flash, writecnt, readcnt, writearr, readarr);
68 }
69 
spi_send_multicommand(const struct flashctx * flash,struct spi_command * cmds)70 int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
71 {
72 	if (flash->mst->spi.multicommand)
73 		return flash->mst->spi.multicommand(flash, cmds);
74 	return default_spi_send_multicommand(flash, cmds);
75 }
76 
default_spi_read(struct flashctx * flash,uint8_t * buf,unsigned int start,unsigned int len)77 int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start,
78 		     unsigned int len)
79 {
80 	unsigned int max_data = flash->mst->spi.max_data_read;
81 	if (max_data == MAX_DATA_UNSPECIFIED) {
82 		msg_perr("%s called, but SPI read chunk size not defined "
83 			 "on this hardware. Please report a bug at "
84 			 "[email protected]\n", __func__);
85 		return 1;
86 	}
87 	return spi_read_chunked(flash, buf, start, len, max_data);
88 }
89 
default_spi_write_256(struct flashctx * flash,const uint8_t * buf,unsigned int start,unsigned int len)90 int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
91 {
92 	unsigned int max_data = flash->mst->spi.max_data_write;
93 	if (max_data == MAX_DATA_UNSPECIFIED) {
94 		msg_perr("%s called, but SPI write chunk size not defined "
95 			 "on this hardware. Please report a bug at "
96 			 "[email protected]\n", __func__);
97 		return 1;
98 	}
99 	return spi_write_chunked(flash, buf, start, len, max_data);
100 }
101 
spi_chip_read(struct flashctx * flash,uint8_t * buf,unsigned int start,unsigned int len)102 int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start,
103 		  unsigned int len)
104 {
105 	int ret;
106 	size_t to_read;
107 	size_t start_address = start;
108 	size_t end_address = len - start;
109 	for (; len; len -= to_read, buf += to_read, start += to_read) {
110 		/* Do not cross 16MiB boundaries in a single transfer.
111 		   This helps with
112 		   o multi-die 4-byte-addressing chips,
113 		   o dediprog that has a protocol limit of 32MiB-512B. */
114 		to_read = min(ALIGN_DOWN(start + 16*MiB, 16*MiB) - start, len);
115 		ret = flash->mst->spi.read(flash, buf, start, to_read);
116 		if (ret)
117 			return ret;
118 		update_progress(flash, FLASHROM_PROGRESS_READ, start - start_address + to_read, end_address);
119 	}
120 	return 0;
121 }
122 
123 /*
124  * Program chip using page (256 bytes) programming.
125  * Some SPI masters can't do this, they use single byte programming instead.
126  * The redirect to single byte programming is achieved by setting
127  * .write_256 = spi_chip_write_1
128  */
129 /* real chunksize is up to 256, logical chunksize is 256 */
spi_chip_write_256(struct flashctx * flash,const uint8_t * buf,unsigned int start,unsigned int len)130 int spi_chip_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
131 {
132 	return flash->mst->spi.write_256(flash, buf, start, len);
133 }
134 
spi_aai_write(struct flashctx * flash,const uint8_t * buf,unsigned int start,unsigned int len)135 int spi_aai_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
136 {
137 	if (flash->mst->spi.write_aai)
138 		return flash->mst->spi.write_aai(flash, buf, start, len);
139 	return default_spi_write_aai(flash, buf, start, len);
140 }
141 
spi_probe_opcode(const struct flashctx * flash,uint8_t opcode)142 bool spi_probe_opcode(const struct flashctx *flash, uint8_t opcode)
143 {
144 	if (!flash->mst->spi.probe_opcode)
145 		return true; /* no probe_opcode implies default of supported. */
146 	return flash->mst->spi.probe_opcode(flash, opcode);
147 }
148 
register_spi_master(const struct spi_master * mst,void * data)149 int register_spi_master(const struct spi_master *mst, void *data)
150 {
151 	struct registered_master rmst = {0};
152 
153 	if (mst->shutdown) {
154 		if (register_shutdown(mst->shutdown, data)) {
155 			mst->shutdown(data); /* cleanup */
156 			return 1;
157 		}
158 	}
159 
160 	if (!mst->write_256 || !mst->read || (!mst->command && !mst->multicommand)) {
161 		msg_perr("%s called with incomplete master definition. "
162 			 "Please report a bug at [email protected]\n",
163 			 __func__);
164 		return ERROR_FLASHROM_BUG;
165 	}
166 
167 
168 	rmst.buses_supported = BUS_SPI;
169 	rmst.spi = *mst;
170 	if (data)
171 		rmst.spi.data = data;
172 	return register_master(&rmst);
173 }
174 
175 /*
176  * The following array has erasefn and opcode list pair. The opcode list pair is
177  * 0 termintated and must have size one more than the maximum number of opcodes
178  * used by any erasefn. Also the opcodes must be in increasing order.
179  */
180 static const struct {
181 	enum block_erase_func func;
182 	uint8_t opcode[3];
183 } function_opcode_list[] = {
184 	{SPI_BLOCK_ERASE_20, {0x20}},
185 	{SPI_BLOCK_ERASE_21, {0x21}},
186 	{SPI_BLOCK_ERASE_50, {0x50}},
187 	{SPI_BLOCK_ERASE_52, {0x52}},
188 	{SPI_BLOCK_ERASE_53, {0x53}},
189 	{SPI_BLOCK_ERASE_5C, {0x5c}},
190 	{SPI_BLOCK_ERASE_60, {0x60}},
191 	{SPI_BLOCK_ERASE_62, {0x62}},
192 	{SPI_BLOCK_ERASE_81, {0x81}},
193 	{SPI_BLOCK_ERASE_C4, {0xc4}},
194 	{SPI_BLOCK_ERASE_C7, {0xc7}},
195 	{SPI_BLOCK_ERASE_D7, {0xd7}},
196 	{SPI_BLOCK_ERASE_D8, {0xd8}},
197 	{SPI_BLOCK_ERASE_DB, {0xdb}},
198 	{SPI_BLOCK_ERASE_DC, {0xdc}},
199 	//AT45CS1282
200 	{SPI_ERASE_AT45CS_SECTOR, {0x50, 0x7c, 0}},
201 	//AT45DB**
202 	{SPI_ERASE_AT45DB_PAGE, {0x81}},
203 	{SPI_ERASE_AT45DB_BLOCK, {0x50}},
204 	{SPI_ERASE_AT45DB_SECTOR, {0x7c}},
205 	{SPI_ERASE_AT45DB_CHIP, {0xc7}},
206 	//SF25F**
207 	{S25FL_BLOCK_ERASE, {0xdc}},
208 	{S25FS_BLOCK_ERASE_D8, {0xd8}},
209 };
210 
211 /*
212  * @brief Get erase function pointer from passed opcode list.
213  *
214  * Get the pointer to the erase function which uses passed opcodes and is used
215  * by the passed flashcip. The passed opcode_list must have opcodes in
216  * increasing order.
217  *
218  * @param chip Pointer to the flashchip structure.
219  * @param opcode_list Pointer to the array of opcodes.
220  * @param opcode_count Number of opcodes in 'opcode_list'
221  *
222  * @result Pointer to erase function matching 'chip' and 'opcode_list' or NULL on failure
223  */
spi_get_opcode_from_erasefn(enum block_erase_func func)224 const uint8_t *spi_get_opcode_from_erasefn(enum block_erase_func func)
225 {
226 	size_t i;
227 	for (i = 0; i < ARRAY_SIZE(function_opcode_list); i++) {
228 		if (function_opcode_list[i].func == func)
229 			return function_opcode_list[i].opcode;
230 	}
231 	msg_cinfo("%s: unknown erase function (0x%d). Please report "
232 			"this at [email protected]\n", __func__, func);
233 	return NULL;
234 }
235 
236