blob: 0ec98fb55df5b63300f5941ad831840400211a7c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0 */
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02002/*
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +05303 * Common SPI flash Interface
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02004 *
5 * Copyright (C) 2008 Atmel Corporation
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +05306 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02007 */
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +05308
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02009#ifndef _SPI_FLASH_H_
10#define _SPI_FLASH_H_
11
Simon Glass4c2dbef2014-10-13 23:42:06 -060012#include <dm.h> /* Because we dereference struct udevice here */
Mike Frysingere06ab652009-11-03 11:36:39 -050013#include <linux/types.h>
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020014
Nikita Kiryanov88e34e52014-08-20 15:08:48 +030015#ifndef CONFIG_SF_DEFAULT_SPEED
16# define CONFIG_SF_DEFAULT_SPEED 1000000
17#endif
18#ifndef CONFIG_SF_DEFAULT_MODE
19# define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
20#endif
21#ifndef CONFIG_SF_DEFAULT_CS
22# define CONFIG_SF_DEFAULT_CS 0
23#endif
24#ifndef CONFIG_SF_DEFAULT_BUS
25# define CONFIG_SF_DEFAULT_BUS 0
26#endif
27
Simon Glassff0960f2014-10-13 23:42:04 -060028struct spi_slave;
Jagannadha Sutradharudu Teki33adfb52013-12-23 23:34:42 +053029
30/**
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053031 * struct spi_flash - SPI flash structure
32 *
33 * @spi: SPI slave
Jagan Teki5d69df32015-06-27 00:51:30 +053034 * @dev: SPI flash device
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053035 * @name: Name of SPI flash
Jagan Teki5d69df32015-06-27 00:51:30 +053036 * @dual_flash: Indicates dual flash memories - dual stacked, parallel
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +053037 * @shift: Flash shift useful in dual parallel
Jagan Teki1fabefd2015-09-29 11:17:02 +053038 * @flags: Indication of spi flash flags
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053039 * @size: Total flash size
40 * @page_size: Write (page) size
41 * @sector_size: Sector size
Jagan Teki5d69df32015-06-27 00:51:30 +053042 * @erase_size: Erase size
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053043 * @bank_read_cmd: Bank read cmd
44 * @bank_write_cmd: Bank write cmd
45 * @bank_curr: Current flash bank
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053046 * @erase_cmd: Erase cmd 4K, 32K, 64K
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +053047 * @read_cmd: Read cmd - Array Fast, Extn read and quad read.
48 * @write_cmd: Write cmd - page and quad program.
Jagan Teki5d69df32015-06-27 00:51:30 +053049 * @dummy_byte: Dummy cycles for read operation.
50 * @memory_map: Address of read-only SPI flash access
Fabio Estevamc3c016c2015-11-05 12:43:42 -020051 * @flash_lock: lock a region of the SPI Flash
52 * @flash_unlock: unlock a region of the SPI Flash
53 * @flash_is_locked: check if a region of the SPI Flash is completely locked
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053054 * @read: Flash read ops: Read len bytes at offset into buf
55 * Supported cmds: Fast Array Read
Vasili Galka801cec52014-03-04 17:24:09 +020056 * @write: Flash write ops: Write len bytes from buf into offset
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053057 * Supported cmds: Page Program
58 * @erase: Flash erase ops: Erase len bytes from offset
59 * Supported cmds: Sector erase 4K, 32K, 64K
Vasili Galka801cec52014-03-04 17:24:09 +020060 * return 0 - Success, 1 - Failure
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053061 */
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020062struct spi_flash {
Pavel Machekd15e74f2015-04-21 10:37:45 +020063 struct spi_slave *spi;
Simon Glass4c2dbef2014-10-13 23:42:06 -060064#ifdef CONFIG_DM_SPI_FLASH
Simon Glass4c2dbef2014-10-13 23:42:06 -060065 struct udevice *dev;
Simon Glass4c2dbef2014-10-13 23:42:06 -060066#endif
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053067 const char *name;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +053068 u8 dual_flash;
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +053069 u8 shift;
Jagan Teki1fabefd2015-09-29 11:17:02 +053070 u16 flags;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020071
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053072 u32 size;
73 u32 page_size;
74 u32 sector_size;
75 u32 erase_size;
Jagannadha Sutradharudu Teki1dcd6d02013-06-19 15:33:58 +053076#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053077 u8 bank_read_cmd;
78 u8 bank_write_cmd;
79 u8 bank_curr;
Jagannadha Sutradharudu Teki1dcd6d02013-06-19 15:33:58 +053080#endif
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053081 u8 erase_cmd;
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053082 u8 read_cmd;
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +053083 u8 write_cmd;
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +053084 u8 dummy_byte;
Jagannadha Sutradharudu Teki615a1562013-06-21 15:56:30 +053085
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053086 void *memory_map;
Fabio Estevamc3c016c2015-11-05 12:43:42 -020087
88 int (*flash_lock)(struct spi_flash *flash, u32 ofs, size_t len);
89 int (*flash_unlock)(struct spi_flash *flash, u32 ofs, size_t len);
90 int (*flash_is_locked)(struct spi_flash *flash, u32 ofs, size_t len);
Simon Glass4c2dbef2014-10-13 23:42:06 -060091#ifndef CONFIG_DM_SPI_FLASH
92 /*
93 * These are not strictly needed for driver model, but keep them here
Pavel Machekd15e74f2015-04-21 10:37:45 +020094 * while the transition is in progress.
Simon Glass4c2dbef2014-10-13 23:42:06 -060095 *
96 * Normally each driver would provide its own operations, but for
97 * SPI flash most chips use the same algorithms. One approach is
98 * to create a 'common' SPI flash device which knows how to talk
99 * to most devices, and then allow other drivers to be used instead
Pavel Machekd15e74f2015-04-21 10:37:45 +0200100 * if required, perhaps with a way of scanning through the list to
Simon Glass4c2dbef2014-10-13 23:42:06 -0600101 * find the driver that matches the device.
102 */
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +0530103 int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
104 int (*write)(struct spi_flash *flash, u32 offset, size_t len,
105 const void *buf);
106 int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
Simon Glass4c2dbef2014-10-13 23:42:06 -0600107#endif
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200108};
109
Simon Glass4c2dbef2014-10-13 23:42:06 -0600110struct dm_spi_flash_ops {
111 int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
112 int (*write)(struct udevice *dev, u32 offset, size_t len,
113 const void *buf);
114 int (*erase)(struct udevice *dev, u32 offset, size_t len);
115};
116
117/* Access the serial operations for a device */
118#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
119
120#ifdef CONFIG_DM_SPI_FLASH
Simon Glass8d987ab2015-03-26 09:29:25 -0600121/**
122 * spi_flash_read_dm() - Read data from SPI flash
123 *
124 * @dev: SPI flash device
125 * @offset: Offset into device in bytes to read from
126 * @len: Number of bytes to read
127 * @buf: Buffer to put the data that is read
128 * @return 0 if OK, -ve on error
129 */
130int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
131
132/**
133 * spi_flash_write_dm() - Write data to SPI flash
134 *
135 * @dev: SPI flash device
136 * @offset: Offset into device in bytes to write to
137 * @len: Number of bytes to write
138 * @buf: Buffer containing bytes to write
139 * @return 0 if OK, -ve on error
140 */
141int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
142 const void *buf);
143
144/**
145 * spi_flash_erase_dm() - Erase blocks of the SPI flash
146 *
147 * Note that @len must be a muiltiple of the flash sector size.
148 *
149 * @dev: SPI flash device
150 * @offset: Offset into device in bytes to start erasing
151 * @len: Number of bytes to erase
152 * @return 0 if OK, -ve on error
153 */
154int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
155
Simon Glass4c2dbef2014-10-13 23:42:06 -0600156int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
157 unsigned int max_hz, unsigned int spi_mode,
158 struct udevice **devp);
159
160/* Compatibility function - this is the old U-Boot API */
161struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
162 unsigned int max_hz, unsigned int spi_mode);
163
164/* Compatibility function - this is the old U-Boot API */
165void spi_flash_free(struct spi_flash *flash);
166
Simon Glass4c2dbef2014-10-13 23:42:06 -0600167static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
Simon Glass8d987ab2015-03-26 09:29:25 -0600168 size_t len, void *buf)
Simon Glass4c2dbef2014-10-13 23:42:06 -0600169{
Simon Glass8d987ab2015-03-26 09:29:25 -0600170 return spi_flash_read_dm(flash->dev, offset, len, buf);
Simon Glass4c2dbef2014-10-13 23:42:06 -0600171}
172
173static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
Simon Glass8d987ab2015-03-26 09:29:25 -0600174 size_t len, const void *buf)
Simon Glass4c2dbef2014-10-13 23:42:06 -0600175{
Simon Glass8d987ab2015-03-26 09:29:25 -0600176 return spi_flash_write_dm(flash->dev, offset, len, buf);
Simon Glass4c2dbef2014-10-13 23:42:06 -0600177}
178
179static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
Simon Glass8d987ab2015-03-26 09:29:25 -0600180 size_t len)
Simon Glass4c2dbef2014-10-13 23:42:06 -0600181{
Simon Glass8d987ab2015-03-26 09:29:25 -0600182 return spi_flash_erase_dm(flash->dev, offset, len);
Simon Glass4c2dbef2014-10-13 23:42:06 -0600183}
184
185struct sandbox_state;
186
187int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
Simon Glass008dcdd2018-06-11 13:07:16 -0600188 struct udevice *bus, ofnode node, const char *spec);
Simon Glass4c2dbef2014-10-13 23:42:06 -0600189
190void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
191
192#else
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200193struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
194 unsigned int max_hz, unsigned int spi_mode);
Simon Glass0efc0242013-12-03 16:43:24 -0700195
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200196void spi_flash_free(struct spi_flash *flash);
197
198static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
199 size_t len, void *buf)
200{
201 return flash->read(flash, offset, len, buf);
202}
203
204static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
205 size_t len, const void *buf)
206{
207 return flash->write(flash, offset, len, buf);
208}
209
210static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
211 size_t len)
212{
213 return flash->erase(flash, offset, len);
214}
Simon Glass4c2dbef2014-10-13 23:42:06 -0600215#endif
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200216
Fabio Estevamc3c016c2015-11-05 12:43:42 -0200217static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
218 bool prot)
219{
Bin Meng439fcb92015-11-13 02:46:26 -0800220 if (!flash->flash_lock || !flash->flash_unlock)
Fabio Estevamc3c016c2015-11-05 12:43:42 -0200221 return -EOPNOTSUPP;
222
223 if (prot)
224 return flash->flash_lock(flash, ofs, len);
225 else
226 return flash->flash_unlock(flash, ofs, len);
227}
228
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200229#endif /* _SPI_FLASH_H_ */