libnile
Loading...
Searching...
No Matches
flash.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023, 2024 Adrian "asie" Siekierka
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 *
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 *
20 * 3. This notice may not be removed or altered from any source distribution.
21 */
22
23#ifndef NILE_FLASH_H_
24#define NILE_FLASH_H_
25
26#include <wonderful.h>
27#include "hardware.h"
28
29#define NILE_FLASH_ID_BY25Q16BS 0x684015
30#define NILE_FLASH_ID_W25Q16JV_IQ 0xEF4015
31#define NILE_FLASH_ID_W25Q16JV_IM 0xEF7015
32
33#define NILE_FLASH_SR1_BUSY (1 << 0)
34#define NILE_FLASH_SR1_WEL (1 << 1)
35#define NILE_FLASH_SR1_BP0 (1 << 2)
36#define NILE_FLASH_SR1_BP1 (1 << 3)
37#define NILE_FLASH_SR1_BP2 (1 << 4)
38#define NILE_FLASH_SR1_TB (1 << 5)
39#define NILE_FLASH_SR1_SEC (1 << 6)
40#define NILE_FLASH_SR1_CMP (1 << 7)
41
42#define NILE_FLASH_SR2_SRL (1 << 0)
43#define NILE_FLASH_SR2_LB1 (1 << 3)
44#define NILE_FLASH_SR2_LB2 (1 << 4)
45#define NILE_FLASH_SR2_LB3 (1 << 5)
46#define NILE_FLASH_SR2_CMP (1 << 6)
47#define NILE_FLASH_SR2_SUS (1 << 7)
48
49#define NILE_FLASH_SR3_WPS (1 << 2)
50#define NILE_FLASH_SR3_DRV_100 (0)
51#define NILE_FLASH_SR3_DRV_75 (1 << 5)
52#define NILE_FLASH_SR3_DRV_50 (2 << 5)
53#define NILE_FLASH_SR3_DRV_25 (3 << 5)
54#define NILE_FLASH_SR3_DRV_MASK (3 << 5)
55
56#define NILE_FLASH_CMD_WRSR1 0x01
57#define NILE_FLASH_CMD_WRITE 0x02
58#define NILE_FLASH_CMD_READ 0x03
59#define NILE_FLASH_CMD_WRDI 0x04
60#define NILE_FLASH_CMD_RDSR1 0x05
61#define NILE_FLASH_CMD_WREN 0x06
62#define NILE_FLASH_CMD_WRSR3 0x11
63#define NILE_FLASH_CMD_RDSR3 0x15
64#define NILE_FLASH_CMD_ERASE_4K 0x20
65#define NILE_FLASH_CMD_WRSR2 0x31
66#define NILE_FLASH_CMD_RDSR2 0x35
67#define NILE_FLASH_CMD_BLOCK_LOCK 0x36
68#define NILE_FLASH_CMD_BLOCK_UNLOCK 0x39
69#define NILE_FLASH_CMD_BLOCK_RDLOCK 0x3D
70#define NILE_FLASH_CMD_SEC_WRITE 0x42
71#define NILE_FLASH_CMD_SEC_ERASE 0x44
72#define NILE_FLASH_CMD_SEC_READ 0x48
73#define NILE_FLASH_CMD_RDUUID 0x4B
74#define NILE_FLASH_CMD_ERASE_32K 0x52
75#define NILE_FLASH_CMD_RDSFPD 0x5A
76#define NILE_FLASH_CMD_RESET_EN 0x66
77#define NILE_FLASH_CMD_SUSPEND 0x75
78#define NILE_FLASH_CMD_RESUME 0x7A
79#define NILE_FLASH_CMD_LOCK 0x7E
80#define NILE_FLASH_CMD_MFR_ID 0x90
81#define NILE_FLASH_CMD_UNLOCK 0x98
82#define NILE_FLASH_CMD_RESET 0x99
83#define NILE_FLASH_CMD_RDID 0x9F
84#define NILE_FLASH_CMD_WAKE_ID 0xAB
85#define NILE_FLASH_CMD_SLEEP 0xB9
86#define NILE_FLASH_CMD_ERASE_ALL 0xC7
87#define NILE_FLASH_CMD_ERASE_64K 0xD8
88
89#ifndef __ASSEMBLER__
90#include <stdbool.h>
91#include <stdint.h>
92
96static inline bool nile_flash_write_unlock_global(void) {
97 bool __nile_flash_cmd(uint8_t cmd);
98 return __nile_flash_cmd(NILE_FLASH_CMD_WREN) && __nile_flash_cmd(NILE_FLASH_CMD_UNLOCK);
99}
100
104static inline bool nile_flash_write_enable(void) {
105 bool __nile_flash_cmd(uint8_t cmd);
106 return __nile_flash_cmd(NILE_FLASH_CMD_WREN);
107}
108
112static inline bool nile_flash_write_disable(void) {
113 bool __nile_flash_cmd(uint8_t cmd);
114 return __nile_flash_cmd(NILE_FLASH_CMD_WRDI);
115}
116
120static inline bool nile_flash_wake(void) {
121 bool __nile_flash_cmd(uint8_t cmd);
122 return __nile_flash_cmd(NILE_FLASH_CMD_WAKE_ID);
123}
124
128static inline bool nile_flash_sleep(void) {
129 bool __nile_flash_cmd(uint8_t cmd);
130 return __nile_flash_cmd(NILE_FLASH_CMD_SLEEP);
131}
132
136bool nile_flash_read_uuid(uint8_t *buffer);
137
141uint32_t nile_flash_read_id(void);
142
150bool nile_flash_read(void __far* buffer, uint32_t address, uint16_t size);
151
159bool nile_flash_write_page(const void __far* buffer, uint32_t address, uint16_t size);
160
169static inline bool nile_flash_erase_part(uint8_t type, uint32_t address) {
170 bool __nile_flash_erase_address(uint32_t address);
171 return __nile_flash_erase_address((((uint32_t) type) << 24) | address);
172}
173
174static inline uint8_t nile_flash_read_sr1(void) {
175 uint8_t __nile_flash_read_sr(uint8_t value);
176 return __nile_flash_read_sr(NILE_FLASH_CMD_RDSR1);
177}
178
179static inline uint8_t nile_flash_read_sr2(void) {
180 uint8_t __nile_flash_read_sr(uint8_t value);
181 return __nile_flash_read_sr(NILE_FLASH_CMD_RDSR2);
182}
183
184static inline uint8_t nile_flash_read_sr3(void) {
185 uint8_t __nile_flash_read_sr(uint8_t value);
186 return __nile_flash_read_sr(NILE_FLASH_CMD_RDSR3);
187}
188
189static inline bool nile_flash_write_sr1(uint8_t value) {
190 bool __nile_flash_write_sr(uint16_t value);
191 return __nile_flash_write_sr(NILE_FLASH_CMD_WRSR1 | (value << 8));
192}
193
194static inline bool nile_flash_write_sr2(uint8_t value) {
195 bool __nile_flash_write_sr(uint16_t value);
196 return __nile_flash_write_sr(NILE_FLASH_CMD_WRSR2 | (value << 8));
197}
198
199static inline bool nile_flash_write_sr3(uint8_t value) {
200 bool __nile_flash_write_sr(uint16_t value);
201 return __nile_flash_write_sr(NILE_FLASH_CMD_WRSR3 | (value << 8));
202}
203
208
209#endif /* __ASSEMBLER__ */
210
211#endif /* NILE_FLASH_H_ */
static bool nile_flash_write_disable(void)
Disable SPI flash writing.
Definition flash.h:112
#define NILE_FLASH_CMD_WRSR2
Write Status Register 2.
Definition flash.h:65
static bool nile_flash_write_enable(void)
Enable SPI flash writing.
Definition flash.h:104
static uint8_t nile_flash_read_sr3(void)
Definition flash.h:184
static uint8_t nile_flash_read_sr1(void)
Definition flash.h:174
static bool nile_flash_sleep(void)
Put SPI flash to sleep.
Definition flash.h:128
bool nile_flash_wait_ready(void)
Wait until SPI flash is ready.
#define NILE_FLASH_CMD_WREN
Write Enable.
Definition flash.h:61
static bool nile_flash_write_sr2(uint8_t value)
Definition flash.h:194
#define NILE_FLASH_CMD_WAKE_ID
Release Power-down / Device ID.
Definition flash.h:84
bool nile_flash_write_page(const void __far *buffer, uint32_t address, uint16_t size)
Write page to SPI flash.
#define NILE_FLASH_CMD_UNLOCK
Global unlock.
Definition flash.h:81
#define NILE_FLASH_CMD_WRSR1
Write Status Register 1.
Definition flash.h:56
#define NILE_FLASH_CMD_SLEEP
Power-down.
Definition flash.h:85
#define NILE_FLASH_CMD_WRDI
Write Disable.
Definition flash.h:59
static bool nile_flash_erase_part(uint8_t type, uint32_t address)
Erase area from SPI flash.
Definition flash.h:169
static uint8_t nile_flash_read_sr2(void)
Definition flash.h:179
uint32_t nile_flash_read_id(void)
Read JEDEC ID from SPI flash.
static bool nile_flash_write_sr3(uint8_t value)
Definition flash.h:199
#define NILE_FLASH_CMD_RDSR2
Read Status Register 2.
Definition flash.h:66
static bool nile_flash_write_sr1(uint8_t value)
Definition flash.h:189
#define NILE_FLASH_CMD_RDSR1
Read Status Register 1.
Definition flash.h:60
static bool nile_flash_wake(void)
Wake up SPI flash.
Definition flash.h:120
static bool nile_flash_write_unlock_global(void)
Unlock global SPI flash writing.
Definition flash.h:96
#define NILE_FLASH_CMD_WRSR3
Write Status Register 3.
Definition flash.h:62
bool nile_flash_read(void __far *buffer, uint32_t address, uint16_t size)
Read data from SPI flash.
bool nile_flash_read_uuid(uint8_t *buffer)
Read device UUID (8 bytes) from SPI flash.
#define NILE_FLASH_CMD_RDSR3
Read Status Register 3.
Definition flash.h:63