blob: d0ce9e721ad0d74ca769c6e19ffd5e167f8e7679 [file] [log] [blame]
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02001/*
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +05302 * Common SPI flash Interface
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02003 *
4 * Copyright (C) 2008 Atmel Corporation
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +05305 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02006 *
Tom Rini5b8031c2016-01-14 22:05:13 -05007 * SPDX-License-Identifier: GPL-2.0
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02008 */
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +05309
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020010#ifndef _SPI_FLASH_H_
11#define _SPI_FLASH_H_
12
Simon Glass4c2dbef2014-10-13 23:42:06 -060013#include <dm.h> /* Because we dereference struct udevice here */
Mike Frysingere06ab652009-11-03 11:36:39 -050014#include <linux/types.h>
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020015
Nikita Kiryanov88e34e52014-08-20 15:08:48 +030016#ifndef CONFIG_SF_DEFAULT_SPEED
17# define CONFIG_SF_DEFAULT_SPEED 1000000
18#endif
19#ifndef CONFIG_SF_DEFAULT_MODE
20# define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
21#endif
22#ifndef CONFIG_SF_DEFAULT_CS
23# define CONFIG_SF_DEFAULT_CS 0
24#endif
25#ifndef CONFIG_SF_DEFAULT_BUS
26# define CONFIG_SF_DEFAULT_BUS 0
27#endif
28
Simon Glassff0960f2014-10-13 23:42:04 -060029struct spi_slave;
Jagannadha Sutradharudu Teki33adfb52013-12-23 23:34:42 +053030
31/**
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053032 * struct spi_flash - SPI flash structure
33 *
34 * @spi: SPI slave
Jagan Teki5d69df32015-06-27 00:51:30 +053035 * @dev: SPI flash device
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053036 * @name: Name of SPI flash
Jagan Teki5d69df32015-06-27 00:51:30 +053037 * @dual_flash: Indicates dual flash memories - dual stacked, parallel
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +053038 * @shift: Flash shift useful in dual parallel
Jagan Teki1fabefd2015-09-29 11:17:02 +053039 * @flags: Indication of spi flash flags
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053040 * @size: Total flash size
41 * @page_size: Write (page) size
42 * @sector_size: Sector size
Jagan Teki5d69df32015-06-27 00:51:30 +053043 * @erase_size: Erase size
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053044 * @bank_read_cmd: Bank read cmd
45 * @bank_write_cmd: Bank write cmd
46 * @bank_curr: Current flash bank
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053047 * @erase_cmd: Erase cmd 4K, 32K, 64K
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +053048 * @read_cmd: Read cmd - Array Fast, Extn read and quad read.
49 * @write_cmd: Write cmd - page and quad program.
Jagan Teki5d69df32015-06-27 00:51:30 +053050 * @dummy_byte: Dummy cycles for read operation.
51 * @memory_map: Address of read-only SPI flash access
Fabio Estevamc3c016c2015-11-05 12:43:42 -020052 * @flash_lock: lock a region of the SPI Flash
53 * @flash_unlock: unlock a region of the SPI Flash
54 * @flash_is_locked: check if a region of the SPI Flash is completely locked
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053055 * @read: Flash read ops: Read len bytes at offset into buf
56 * Supported cmds: Fast Array Read
Vasili Galka801cec52014-03-04 17:24:09 +020057 * @write: Flash write ops: Write len bytes from buf into offset
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053058 * Supported cmds: Page Program
59 * @erase: Flash erase ops: Erase len bytes from offset
60 * Supported cmds: Sector erase 4K, 32K, 64K
Vasili Galka801cec52014-03-04 17:24:09 +020061 * return 0 - Success, 1 - Failure
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053062 */
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020063struct spi_flash {
Pavel Machekd15e74f2015-04-21 10:37:45 +020064 struct spi_slave *spi;
Simon Glass4c2dbef2014-10-13 23:42:06 -060065#ifdef CONFIG_DM_SPI_FLASH
Simon Glass4c2dbef2014-10-13 23:42:06 -060066 struct udevice *dev;
Simon Glass4c2dbef2014-10-13 23:42:06 -060067#endif
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053068 const char *name;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +053069 u8 dual_flash;
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +053070 u8 shift;
Jagan Teki1fabefd2015-09-29 11:17:02 +053071 u16 flags;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020072
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053073 u32 size;
74 u32 page_size;
75 u32 sector_size;
76 u32 erase_size;
Jagannadha Sutradharudu Teki1dcd6d02013-06-19 15:33:58 +053077#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053078 u8 bank_read_cmd;
79 u8 bank_write_cmd;
80 u8 bank_curr;
Jagannadha Sutradharudu Teki1dcd6d02013-06-19 15:33:58 +053081#endif
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053082 u8 erase_cmd;
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053083 u8 read_cmd;
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +053084 u8 write_cmd;
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +053085 u8 dummy_byte;
Jagannadha Sutradharudu Teki615a1562013-06-21 15:56:30 +053086
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053087 void *memory_map;
Fabio Estevamc3c016c2015-11-05 12:43:42 -020088
89 int (*flash_lock)(struct spi_flash *flash, u32 ofs, size_t len);
90 int (*flash_unlock)(struct spi_flash *flash, u32 ofs, size_t len);
91 int (*flash_is_locked)(struct spi_flash *flash, u32 ofs, size_t len);
Simon Glass4c2dbef2014-10-13 23:42:06 -060092#ifndef CONFIG_DM_SPI_FLASH
93 /*
94 * These are not strictly needed for driver model, but keep them here
Pavel Machekd15e74f2015-04-21 10:37:45 +020095 * while the transition is in progress.
Simon Glass4c2dbef2014-10-13 23:42:06 -060096 *
97 * Normally each driver would provide its own operations, but for
98 * SPI flash most chips use the same algorithms. One approach is
99 * to create a 'common' SPI flash device which knows how to talk
100 * to most devices, and then allow other drivers to be used instead
Pavel Machekd15e74f2015-04-21 10:37:45 +0200101 * if required, perhaps with a way of scanning through the list to
Simon Glass4c2dbef2014-10-13 23:42:06 -0600102 * find the driver that matches the device.
103 */
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +0530104 int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
105 int (*write)(struct spi_flash *flash, u32 offset, size_t len,
106 const void *buf);
107 int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
Simon Glass4c2dbef2014-10-13 23:42:06 -0600108#endif
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200109};
110
Simon Glass4c2dbef2014-10-13 23:42:06 -0600111struct dm_spi_flash_ops {
112 int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
113 int (*write)(struct udevice *dev, u32 offset, size_t len,
114 const void *buf);
115 int (*erase)(struct udevice *dev, u32 offset, size_t len);
116};
117
118/* Access the serial operations for a device */
119#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
120
121#ifdef CONFIG_DM_SPI_FLASH
Simon Glass8d987ab2015-03-26 09:29:25 -0600122/**
123 * spi_flash_read_dm() - Read data from SPI flash
124 *
125 * @dev: SPI flash device
126 * @offset: Offset into device in bytes to read from
127 * @len: Number of bytes to read
128 * @buf: Buffer to put the data that is read
129 * @return 0 if OK, -ve on error
130 */
131int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
132
133/**
134 * spi_flash_write_dm() - Write data to SPI flash
135 *
136 * @dev: SPI flash device
137 * @offset: Offset into device in bytes to write to
138 * @len: Number of bytes to write
139 * @buf: Buffer containing bytes to write
140 * @return 0 if OK, -ve on error
141 */
142int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
143 const void *buf);
144
145/**
146 * spi_flash_erase_dm() - Erase blocks of the SPI flash
147 *
148 * Note that @len must be a muiltiple of the flash sector size.
149 *
150 * @dev: SPI flash device
151 * @offset: Offset into device in bytes to start erasing
152 * @len: Number of bytes to erase
153 * @return 0 if OK, -ve on error
154 */
155int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
156
Simon Glass4c2dbef2014-10-13 23:42:06 -0600157int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
158 unsigned int max_hz, unsigned int spi_mode,
159 struct udevice **devp);
160
161/* Compatibility function - this is the old U-Boot API */
162struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
163 unsigned int max_hz, unsigned int spi_mode);
164
165/* Compatibility function - this is the old U-Boot API */
166void spi_flash_free(struct spi_flash *flash);
167
Simon Glass4c2dbef2014-10-13 23:42:06 -0600168static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
Simon Glass8d987ab2015-03-26 09:29:25 -0600169 size_t len, void *buf)
Simon Glass4c2dbef2014-10-13 23:42:06 -0600170{
Simon Glass8d987ab2015-03-26 09:29:25 -0600171 return spi_flash_read_dm(flash->dev, offset, len, buf);
Simon Glass4c2dbef2014-10-13 23:42:06 -0600172}
173
174static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
Simon Glass8d987ab2015-03-26 09:29:25 -0600175 size_t len, const void *buf)
Simon Glass4c2dbef2014-10-13 23:42:06 -0600176{
Simon Glass8d987ab2015-03-26 09:29:25 -0600177 return spi_flash_write_dm(flash->dev, offset, len, buf);
Simon Glass4c2dbef2014-10-13 23:42:06 -0600178}
179
180static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
Simon Glass8d987ab2015-03-26 09:29:25 -0600181 size_t len)
Simon Glass4c2dbef2014-10-13 23:42:06 -0600182{
Simon Glass8d987ab2015-03-26 09:29:25 -0600183 return spi_flash_erase_dm(flash->dev, offset, len);
Simon Glass4c2dbef2014-10-13 23:42:06 -0600184}
185
186struct sandbox_state;
187
188int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
189 struct udevice *bus, int of_offset, const char *spec);
190
191void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
192
193#else
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200194struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
195 unsigned int max_hz, unsigned int spi_mode);
Simon Glass0efc0242013-12-03 16:43:24 -0700196
197/**
198 * Set up a new SPI flash from an fdt node
199 *
200 * @param blob Device tree blob
201 * @param slave_node Pointer to this SPI slave node in the device tree
202 * @param spi_node Cached pointer to the SPI interface this node belongs
203 * to
204 * @return 0 if ok, -1 on error
205 */
206struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
207 int spi_node);
208
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200209void spi_flash_free(struct spi_flash *flash);
210
211static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
212 size_t len, void *buf)
213{
214 return flash->read(flash, offset, len, buf);
215}
216
217static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
218 size_t len, const void *buf)
219{
220 return flash->write(flash, offset, len, buf);
221}
222
223static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
224 size_t len)
225{
226 return flash->erase(flash, offset, len);
227}
Simon Glass4c2dbef2014-10-13 23:42:06 -0600228#endif
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200229
Fabio Estevamc3c016c2015-11-05 12:43:42 -0200230static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
231 bool prot)
232{
Bin Meng439fcb92015-11-13 02:46:26 -0800233 if (!flash->flash_lock || !flash->flash_unlock)
Fabio Estevamc3c016c2015-11-05 12:43:42 -0200234 return -EOPNOTSUPP;
235
236 if (prot)
237 return flash->flash_lock(flash, ofs, len);
238 else
239 return flash->flash_unlock(flash, ofs, len);
240}
241
Christian Riesch32b11272011-12-09 09:47:35 +0000242void spi_boot(void) __noreturn;
Prabhakar Kushwaha1eaa7422014-04-08 19:13:22 +0530243void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);
Christian Riesch32b11272011-12-09 09:47:35 +0000244
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200245#endif /* _SPI_FLASH_H_ */