1 /**
2  * \file
3  *
4  * \brief SAM SPI Flash Driver for SAMB11
5  *
6  * Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 #ifndef SPI_FLASH_H_INCLUDED
47 #define SPI_FLASH_H_INCLUDED
48 
49 /**
50  * \defgroup asfdoc_samb_spi_flash_group SAM SPI Flash Driver (SPI Flash)
51  *
52  * This driver for Atmel&reg; | SMART SAM devices provides an interface for the
53  * configuration and management of the device's SPI Flash functionality. The
54  * following driver API modes are covered by this manual:
55  *
56  * The following peripherals are used by this module:
57  *  - SPI Flash (SPI Flash Management)
58  *
59  * The following devices can use this module:
60  *  - Atmel | SMART SAM B11
61  *
62  * The outline of this documentation is as follows:
63  *  - \ref asfdoc_samb_spi_flash_prerequisites
64  *  - \ref asfdoc_samb_spi_flash_module_overview
65  *  - \ref asfdoc_samb_spi_flash_special_considerations
66  *  - \ref asfdoc_samb_spi_flash_extra_info
67  *  - \ref asfdoc_samb_spi_flash_examples
68  *  - \ref asfdoc_samb_spi_flash_api_overview
69  *
70  *
71  * \section asfdoc_samb_spi_flash_prerequisites Prerequisites
72  *
73  * There are no prerequisites for this module.
74  *
75  *
76  * \section asfdoc_samb_spi_flash_module_overview Module Overview
77  * The AHB SPI-Flash Controller is used to access the internal stacked FLASH
78  * memory to access various instruction/data code needed for storing
79  * application code, code patches, and OTA images.
80  *
81  * The table above describes the stacked SPI Flash memory organization and layout.
82  * Boot up, the ROM boot loader starts loading different sections of from the
83  * flash by first reading the flash header to figure out the locations and sizes
84  * for different sections, then copying the sections one by one into their
85  * respective memory regions. The header and all sections have CRC calculated
86  * for integrity check. If CRC fails, section will not be loaded into RAM.
87  *
88  * <table>
89  *  <tr>
90  *    <th>SPI flash memory address</th>
91  *    <th>Function description</th>
92  *  </tr>
93  *  <tr>
94  *    <td>0x0000 ~ 0x0FFF</td>
95  *    <td>Flash Header</td>
96  *  </tr>
97  *  <tr>
98  *    <td>0x1000 ~ End of patch section (according to patch size)</td>
99  *    <td>Patch image section</td>
100  *  </tr>
101  *  <tr>
102  *    <td>User App Start Section ~ User App End Section <=254KB</td>
103  *    <td>USER APP</td>
104  *  </tr>
105  *  <tr>
106  *    <td>NVDS 4KB</td>
107  *    <td>NVDS</td>
108  *  </tr>
109  * </table>
110  *
111  * \section asfdoc_samb_spi_flash_special_considerations Special Considerations
112  * There are no special considerations for this module.
113  *
114  * \section asfdoc_samb_spi_flash_extra_info Extra Information
115  *
116  * For extra information, see \ref asfdoc_samb_spi_flash_extra. This includes:
117  *  - \ref asfdoc_samb_spi_flash_extra_acronyms
118  *  - \ref asfdoc_samb_spi_flash_extra_dependencies
119  *  - \ref asfdoc_samb_spi_flash_extra_errata
120  *  - \ref asfdoc_samb_spi_flash_extra_history
121  *
122  *
123  * \section asfdoc_samb_spi_flash_examples Examples
124  *
125  * For a list of examples related to this driver, see
126  * \ref asfdoc_samb_spi_flash_exqsg.
127  *
128  *
129  * \section asfdoc_samb_spi_flash_api_overview API Overview
130  * @{
131  */
132 
133 #include <stdint.h>
134 #include <string.h>
135 #include <compiler.h>
136 #include <gpio.h>
137 
138 #ifdef __cplusplus
139 extern "C" {
140 #endif
141 
142 /** Flash memory size: 256 KBytes */
143 #define FLASH_MEMORY_SIZE       0x40000
144 
145 /** Accessible NVDS area start spi flash address */
146 #define FLASH_NVDS_START_ADDRESS  0x3F000
147 
148 /** Accessible NVDS area spi flash size */
149 #define FLASH_NVDS_SIZE         0x1000
150 
151 /** Flash memory sector size 4 KBytes */
152 #define FLASH_SECTOR_SIZE       0x1000
153 
154 /** Flash memory page size: 256 Bytes */
155 #define FLASH_PAGE_SIZE         0x100
156 
157 /** Flash sector Mask */
158 #define FLASH_SECT_MASK         0xFFF000
159 
160 /** Flash page mask */
161 #define FLASH_PAGE_MASK         0xFFFF00
162 
163 /** SPI flash write enable */
164 #define SPI_FLASH_CMD_WRITE_ENABLE      0x06
165 /** SPI flash write disable */
166 #define SPI_FLASH_CMD_WRITE_DISABLE     0x04
167 /** SPI flash read identification */
168 #define SPI_FLASH_CMD_READ_ID           0x9F
169 /** SPI flash read status register */
170 #define SPI_FLASH_CMD_READ_STATUS       0x05
171 /** SPI flash write status register */
172 #define SPI_FLASH_CMD_WRITE_STATUS      0x01
173 /** SPI flash read data bytes */
174 #define SPI_FLASH_CMD_READ_DATA         0x03
175 /** SPI flash read data bytes at higher speed */
176 #define SPI_FLASH_CMD_READ_HIGH_SPEED   0x0B
177 /** SPI flash page program */
178 #define SPI_FLASH_CMD_PAGE_PROGRAM      0x02
179 /** SPI flash sector erase */
180 #define SPI_FLASH_CMD_SECTOR_ERASE      0x20
181 /** SPI flash bulk erase */
182 #define SPI_FLASH_CMD_BULK_ERASE        0xC7
183 /** SPI flash deep power-down */
184 #define SPI_FLASH_CMD_ENTER_LOW_POWER   0xB9
185 /** SPI flash release from deep power down */
186 #define SPI_FLASH_CMD_LEAVE_LOW_POWER   0xAB
187 
188 /** SPI flash direction: send command */
189 #define SPI_FLASH_DIRECTION_CMD         0x01
190 /** SPI flash direction: program or erase */
191 #define SPI_FLASH_DIRECTION_PRO         0x0F
192 /** SPI flash direction: read data */
193 #define SPI_FLASH_DIRECTION_READ        0x1F
194 
195 /** \name SPI flash callback config
196  * @{
197  */
198 void spi_flash_init(void) ;
199 /** @}*/
200 
201 /** \name SPI flash read/write/erase operation
202  * @{
203  */
204 uint32_t spi_flash_rdid(void);
205 void spi_flash_read(uint8_t *read_buf, uint32_t flash_addr, uint32_t size);
206 int8_t spi_flash_write(void *write_buf, uint32_t flash_addr, uint32_t size);
207 void spi_flash_sector_erase(uint32_t flash_addr);
208 uint8_t spi_flash_erase(uint32_t start_offset, uint32_t size);
209 void spi_flash_enter_low_power_mode(void);
210 void spi_flash_leave_low_power_mode(void);
211 void spi_flash_clock_init(void);
212 void spi_flash_turn_off(void);
213 void spi_flash_turn_on(void);
214 /** @}*/
215 
216 /** @}*/
217 
218 #ifdef __cplusplus
219 }
220 #endif
221 
222 
223 /**
224  * \page asfdoc_samb_spi_flash_extra Extra Information for SPI Flash Driver
225  *
226  * \section asfdoc_samb_spi_flash_extra_acronyms Acronyms
227  * Below is a table listing the acronyms used in this module, along with their
228  * intended meanings.
229  *
230  * <table>
231  *	<tr>
232  *		<th>Acronym</th>
233  *		<th>Description</th>
234  *	</tr>
235  *	<tr>
236  *		<td>SPI Flash</td>
237  *		<td>Serial Peripheral Interface Flash</td>
238  *	</tr>
239  * </table>
240  *
241  *
242  * \section asfdoc_samb_spi_flash_extra_dependencies Dependencies
243  * There are no dependencies related to this driver.
244  *
245  *
246  * \section asfdoc_samb_spi_flash_extra_errata Errata
247  * There are no errata related to this driver.
248  *
249  *
250  * \section asfdoc_samb_spi_flash_extra_history Module History
251  * An overview of the module history is presented in the table below, with
252  * details on the enhancements and fixes made to the module since its first
253  * release. The current version of this corresponds to the newest version in
254  * the table.
255  *
256  * <table>
257  *	<tr>
258  *		<th>Changelog</th>
259  *	</tr>
260  *	<tr>
261  *		<td>Initial Release</td>
262  *	</tr>
263  * </table>
264  */
265 
266 /**
267  * \page asfdoc_samb_spi_flash_exqsg Examples for SPI Flash Driver
268  *
269  * This is a list of the available Quick Start guides (QSGs) and example
270  * applications for \ref asfdoc_samb_spi_flash_group. QSGs are simple examples with
271  * step-by-step instructions to configure and use this driver in a selection of
272  * use cases. Note that QSGs can be compiled as a standalone application or be
273  * added to the user application.
274  *
275  *  - \subpage asfdoc_samb_spi_flash_basic_use_case
276  *
277  * \page asfdoc_samb_spi_flash_document_revision_history Document Revision History
278  *
279  * <table>
280  *	<tr>
281  *		<th>Doc. Rev.</td>
282  *		<th>Date</td>
283  *		<th>Comments</td>
284  *	</tr>
285  *	<tr>
286  *		<td>A</td>
287  *		<td>09/2015</td>
288  *		<td>Initial release</td>
289  *	</tr>
290  * </table>
291  */
292 
293 #endif
294