xref: /openbmc/u-boot/include/spi_flash.h (revision b46694df)
1 /*
2  * Interface to SPI flash
3  *
4  * Copyright (C) 2008 Atmel Corporation
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 #ifndef _SPI_FLASH_H_
24 #define _SPI_FLASH_H_
25 
26 #include <spi.h>
27 #include <linux/types.h>
28 #include <linux/compiler.h>
29 
30 struct spi_flash {
31 	struct spi_slave *spi;
32 
33 	const char	*name;
34 
35 	/* Total flash size */
36 	u32		size;
37 	/* Write (page) size */
38 	u32		page_size;
39 	/* Erase (sector) size */
40 	u32		sector_size;
41 
42 	void *memory_map;	/* Address of read-only SPI flash access */
43 	int		(*read)(struct spi_flash *flash, u32 offset,
44 				size_t len, void *buf);
45 	int		(*write)(struct spi_flash *flash, u32 offset,
46 				size_t len, const void *buf);
47 	int		(*erase)(struct spi_flash *flash, u32 offset,
48 				size_t len);
49 };
50 
51 /**
52  * spi_flash_do_alloc - Allocate a new spi flash structure
53  *
54  * The structure is allocated and cleared with default values for
55  * read, write and erase, which the caller can modify. The caller must set
56  * up size, page_size and sector_size.
57  *
58  * Use the helper macro spi_flash_alloc() to call this.
59  *
60  * @offset: Offset of struct spi_slave within slave structure
61  * @size: Size of slave structure
62  * @spi: SPI slave
63  * @name: Name of SPI flash device
64  */
65 void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
66 			 const char *name);
67 
68 /**
69  * spi_flash_alloc - Allocate a new SPI flash structure
70  *
71  * @_struct: Name of structure to allocate (e.g. struct ramtron_spi_fram). This
72  *	structure must contain a member 'struct spi_flash *flash'.
73  * @spi: SPI slave
74  * @name: Name of SPI flash device
75  */
76 #define spi_flash_alloc(_struct, spi, name) \
77 	spi_flash_do_alloc(offsetof(_struct, flash), sizeof(_struct), \
78 				spi, name)
79 
80 /**
81  * spi_flash_alloc_base - Allocate a new SPI flash structure with no private data
82  *
83  * @spi: SPI slave
84  * @name: Name of SPI flash device
85  */
86 #define spi_flash_alloc_base(spi, name) \
87 	spi_flash_do_alloc(0, sizeof(struct spi_flash), spi, name)
88 
89 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
90 		unsigned int max_hz, unsigned int spi_mode);
91 void spi_flash_free(struct spi_flash *flash);
92 
93 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
94 		size_t len, void *buf)
95 {
96 	return flash->read(flash, offset, len, buf);
97 }
98 
99 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
100 		size_t len, const void *buf)
101 {
102 	return flash->write(flash, offset, len, buf);
103 }
104 
105 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
106 		size_t len)
107 {
108 	return flash->erase(flash, offset, len);
109 }
110 
111 void spi_boot(void) __noreturn;
112 
113 #endif /* _SPI_FLASH_H_ */
114