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_ZD25WQ16D 0xBA6015
31#define NILE_FLASH_ID_W25Q16JV_IQ 0xEF4015
32#define NILE_FLASH_ID_W25Q16JV_IM 0xEF7015
33
34#define NILE_FLASH_SR1_BUSY (1 << 0)
35#define NILE_FLASH_SR1_WEL (1 << 1)
36#define NILE_FLASH_SR1_BP0 (1 << 2)
37#define NILE_FLASH_SR1_BP1 (1 << 3)
38#define NILE_FLASH_SR1_BP2 (1 << 4)
39#define NILE_FLASH_SR1_TB (1 << 5)
40#define NILE_FLASH_SR1_SEC (1 << 6)
41#define NILE_FLASH_SR1_CMP (1 << 7)
42
43#define NILE_FLASH_SR2_SRL (1 << 0)
44#define NILE_FLASH_SR2_LB1 (1 << 3)
45#define NILE_FLASH_SR2_LB2 (1 << 4)
46#define NILE_FLASH_SR2_LB3 (1 << 5)
47#define NILE_FLASH_SR2_CMP (1 << 6)
48#define NILE_FLASH_SR2_SUS (1 << 7)
49
50#define NILE_FLASH_SR3_WPS (1 << 2)
51#define NILE_FLASH_SR3_DRV_100 (0)
52#define NILE_FLASH_SR3_DRV_75 (1 << 5)
53#define NILE_FLASH_SR3_DRV_50 (2 << 5)
54#define NILE_FLASH_SR3_DRV_25 (3 << 5)
55#define NILE_FLASH_SR3_DRV_MASK (3 << 5)
56
57#define NILE_FLASH_CMD_WRSR1 0x01
58#define NILE_FLASH_CMD_WRITE 0x02
59#define NILE_FLASH_CMD_READ 0x03
60#define NILE_FLASH_CMD_WRDI 0x04
61#define NILE_FLASH_CMD_RDSR1 0x05
62#define NILE_FLASH_CMD_WREN 0x06
63#define NILE_FLASH_CMD_WRSR3 0x11
64#define NILE_FLASH_CMD_RDSR3 0x15
65#define NILE_FLASH_CMD_ERASE_4K 0x20
66#define NILE_FLASH_CMD_WRSR2 0x31
67#define NILE_FLASH_CMD_RDSR2 0x35
68#define NILE_FLASH_CMD_BLOCK_LOCK 0x36
69#define NILE_FLASH_CMD_BLOCK_UNLOCK 0x39
70#define NILE_FLASH_CMD_BLOCK_RDLOCK 0x3D
71#define NILE_FLASH_CMD_SEC_WRITE 0x42
72#define NILE_FLASH_CMD_SEC_ERASE 0x44
73#define NILE_FLASH_CMD_SEC_READ 0x48
74#define NILE_FLASH_CMD_RDUUID 0x4B
75#define NILE_FLASH_CMD_ERASE_32K 0x52
76#define NILE_FLASH_CMD_RDSFPD 0x5A
77#define NILE_FLASH_CMD_RESET_EN 0x66
78#define NILE_FLASH_CMD_SUSPEND 0x75
79#define NILE_FLASH_CMD_RESUME 0x7A
80#define NILE_FLASH_CMD_LOCK 0x7E
81#define NILE_FLASH_CMD_MFR_ID 0x90
82#define NILE_FLASH_CMD_UNLOCK 0x98
83#define NILE_FLASH_CMD_RESET 0x99
84#define NILE_FLASH_CMD_RDID 0x9F
85#define NILE_FLASH_CMD_WAKE_ID 0xAB
86#define NILE_FLASH_CMD_SLEEP 0xB9
87#define NILE_FLASH_CMD_ERASE_ALL 0xC7
88#define NILE_FLASH_CMD_ERASE_64K 0xD8
89
90#ifndef __ASSEMBLER__
91#include <stdbool.h>
92#include <stdint.h>
93
97static inline bool nile_flash_write_unlock_global(void) {
98 bool __nile_flash_cmd(uint8_t cmd);
99 return __nile_flash_cmd(NILE_FLASH_CMD_WREN) && __nile_flash_cmd(NILE_FLASH_CMD_UNLOCK);
100}
101
105static inline bool nile_flash_write_enable(void) {
106 bool __nile_flash_cmd(uint8_t cmd);
107 return __nile_flash_cmd(NILE_FLASH_CMD_WREN);
108}
109
113static inline bool nile_flash_write_disable(void) {
114 bool __nile_flash_cmd(uint8_t cmd);
115 return __nile_flash_cmd(NILE_FLASH_CMD_WRDI);
116}
117
121static inline bool nile_flash_wake(void) {
122 bool __nile_flash_cmd(uint8_t cmd);
123 return __nile_flash_cmd(NILE_FLASH_CMD_WAKE_ID);
124}
125
129static inline bool nile_flash_sleep(void) {
130 bool __nile_flash_cmd(uint8_t cmd);
131 return __nile_flash_cmd(NILE_FLASH_CMD_SLEEP);
132}
133
137bool nile_flash_read_uuid(uint8_t *buffer);
138
142uint32_t nile_flash_read_id(void);
143
151bool nile_flash_read(void __far* buffer, uint32_t address, uint16_t size);
152
160bool nile_flash_write_page(const void __far* buffer, uint32_t address, uint16_t size);
161
170static inline bool nile_flash_erase_part(uint8_t type, uint32_t address) {
171 bool __nile_flash_erase_address(uint32_t address);
172 return __nile_flash_erase_address((((uint32_t) type) << 24) | address);
173}
174
175static inline uint8_t nile_flash_read_sr1(void) {
176 uint8_t __nile_flash_read_sr(uint8_t value);
177 return __nile_flash_read_sr(NILE_FLASH_CMD_RDSR1);
178}
179
180static inline uint8_t nile_flash_read_sr2(void) {
181 uint8_t __nile_flash_read_sr(uint8_t value);
182 return __nile_flash_read_sr(NILE_FLASH_CMD_RDSR2);
183}
184
185static inline uint8_t nile_flash_read_sr3(void) {
186 uint8_t __nile_flash_read_sr(uint8_t value);
187 return __nile_flash_read_sr(NILE_FLASH_CMD_RDSR3);
188}
189
190static inline bool nile_flash_write_sr1(uint8_t value) {
191 bool __nile_flash_write_sr(uint16_t value);
192 return __nile_flash_write_sr(NILE_FLASH_CMD_WRSR1 | (value << 8));
193}
194
195static inline bool nile_flash_write_sr2(uint8_t value) {
196 bool __nile_flash_write_sr(uint16_t value);
197 return __nile_flash_write_sr(NILE_FLASH_CMD_WRSR2 | (value << 8));
198}
199
200static inline bool nile_flash_write_sr3(uint8_t value) {
201 bool __nile_flash_write_sr(uint16_t value);
202 return __nile_flash_write_sr(NILE_FLASH_CMD_WRSR3 | (value << 8));
203}
204
209
210#endif /* __ASSEMBLER__ */
211
212#endif /* NILE_FLASH_H_ */
static bool nile_flash_write_disable(void)
Disable SPI flash writing.
Definition flash.h:113
#define NILE_FLASH_CMD_WRSR2
Write Status Register 2.
Definition flash.h:66
static bool nile_flash_write_enable(void)
Enable SPI flash writing.
Definition flash.h:105
static uint8_t nile_flash_read_sr3(void)
Definition flash.h:185
static uint8_t nile_flash_read_sr1(void)
Definition flash.h:175
static bool nile_flash_sleep(void)
Put SPI flash to sleep.
Definition flash.h:129
bool nile_flash_wait_ready(void)
Wait until SPI flash is ready.
#define NILE_FLASH_CMD_WREN
Write Enable.
Definition flash.h:62
static bool nile_flash_write_sr2(uint8_t value)
Definition flash.h:195
#define NILE_FLASH_CMD_WAKE_ID
Release Power-down / Device ID.
Definition flash.h:85
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:82
#define NILE_FLASH_CMD_WRSR1
Write Status Register 1.
Definition flash.h:57
#define NILE_FLASH_CMD_SLEEP
Power-down.
Definition flash.h:86
#define NILE_FLASH_CMD_WRDI
Write Disable.
Definition flash.h:60
static bool nile_flash_erase_part(uint8_t type, uint32_t address)
Erase area from SPI flash.
Definition flash.h:170
static uint8_t nile_flash_read_sr2(void)
Definition flash.h:180
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:200
#define NILE_FLASH_CMD_RDSR2
Read Status Register 2.
Definition flash.h:67
static bool nile_flash_write_sr1(uint8_t value)
Definition flash.h:190
#define NILE_FLASH_CMD_RDSR1
Read Status Register 1.
Definition flash.h:61
static bool nile_flash_wake(void)
Wake up SPI flash.
Definition flash.h:121
static bool nile_flash_write_unlock_global(void)
Unlock global SPI flash writing.
Definition flash.h:97
#define NILE_FLASH_CMD_WRSR3
Write Status Register 3.
Definition flash.h:63
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:64