1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009 Free Software Foundation, Inc.
4  * Copyright 2009-2011 Ettus Research LLC
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
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  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef INCLUDED_SPI_FLASH_PRIVATE_H
21 #define INCLUDED_SPI_FLASH_PRIVATE_H
22 
23 #include "spi_flash.h"
24 #include "spi.h"
25 #include "memory_map.h"
26 #include <string.h>
27 
28 
29 /* M25P64 et al. */
30 
31 #define	WREN_CMD	0x06	// write enable
32 #define	WRDI_CMD	0x04	// write disable
33 #define	RDID_CMD	0x9f	// read identification
34 #define	RDSR_CMD	0x05	// read status register
35 #define WRSR_CMD	0x01	// write status register
36 #define	READ_CMD	0x03
37 #define	FAST_READ_CMD	0x0b
38 #define	PP_CMD		0x02	// page program (256 bytes)
39 #define	SE_CMD		0xd8	// sector erase (64KB)
40 #define	BE_CMD		0xc7	// bulk erase (all)
41 #define	RES_CMD		0xab	// read electronic sig (deprecated)
42 
43 /* Status register bits */
44 
45 #define	SR_SRWD		0x80
46 #define	SR_BP2		0x10	// block protect bit 2
47 #define	SR_BP1		0x08	// block protect bit 1
48 #define	SR_BP0		0x04	// block protect bit 0
49 #define	SR_WEL		0x02	// Write Enable Latch
50 #define	SR_WIP		0x01	// Write in Progress.  Set if busy w/ program or erase cycle.
51 
52 
53 #define	FLAGS (SPIF_PUSH_FALL | SPIF_LATCH_RISE)
54 
55 #define LEN(x) ((x) & SPI_CTRL_CHAR_LEN_MASK)
56 
57 
58 static inline uint32_t
min(uint32_t a,uint32_t b)59 min(uint32_t a, uint32_t b)
60 {
61   return a < b ? a : b;
62 }
63 
64 static inline uint32_t
round_down(uint32_t x,uint32_t power_of_2)65 round_down(uint32_t x, uint32_t power_of_2)
66 {
67   return x & -power_of_2;
68 }
69 
70 #endif /* INCLUDED_SPI_FLASH_PRIVATE_H */
71