blob: f2814ef41a0b3c27b2e82352190de831559422c9 [file] [log] [blame]
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +02001/*
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +05302 * Common SPI flash Interface
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +02003 *
4 * Copyright (C) 2008 Atmel Corporation
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +05305 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +02006 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +020012 * version 2 as published by the Free Software Foundation.
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020013 */
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +053014
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020015#ifndef _SPI_FLASH_H_
16#define _SPI_FLASH_H_
17
Simon Glassd6eedf72014-10-13 23:42:06 -060018#include <dm.h> /* Because we dereference struct udevice here */
Mike Frysingere4c698b2009-11-03 11:36:39 -050019#include <linux/types.h>
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020020
Nikita Kiryanov6c6ccdf2014-08-20 15:08:48 +030021#ifndef CONFIG_SF_DEFAULT_SPEED
22# define CONFIG_SF_DEFAULT_SPEED 1000000
23#endif
24#ifndef CONFIG_SF_DEFAULT_MODE
25# define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
26#endif
27#ifndef CONFIG_SF_DEFAULT_CS
28# define CONFIG_SF_DEFAULT_CS 0
29#endif
30#ifndef CONFIG_SF_DEFAULT_BUS
31# define CONFIG_SF_DEFAULT_BUS 0
32#endif
33
Simon Glassd34b4562014-10-13 23:42:04 -060034struct spi_slave;
Jagannadha Sutradharudu Teki20e45bc2013-12-23 23:34:42 +053035
36/**
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +053037 * struct spi_flash - SPI flash structure
38 *
39 * @spi: SPI slave
40 * @name: Name of SPI flash
Simon Glassd34b4562014-10-13 23:42:04 -060041 * @dual_flash: Indicates dual flash memories - dual stacked, parallel
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +053042 * @shift: Flash shift useful in dual parallel
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +053043 * @size: Total flash size
44 * @page_size: Write (page) size
45 * @sector_size: Sector size
Simon Glassd34b4562014-10-13 23:42:04 -060046 * @erase_size: Erase size
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +053047 * @bank_read_cmd: Bank read cmd
48 * @bank_write_cmd: Bank write cmd
49 * @bank_curr: Current flash bank
50 * @poll_cmd: Poll cmd - for flash erase/program
51 * @erase_cmd: Erase cmd 4K, 32K, 64K
Jagannadha Sutradharudu Tekie0ebabc2014-01-11 15:13:11 +053052 * @read_cmd: Read cmd - Array Fast, Extn read and quad read.
53 * @write_cmd: Write cmd - page and quad program.
Simon Glassd34b4562014-10-13 23:42:04 -060054 * @dummy_byte: Dummy cycles for read operation.
55 * @memory_map: Address of read-only SPI flash access
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +053056 * @read: Flash read ops: Read len bytes at offset into buf
57 * Supported cmds: Fast Array Read
Vasili Galkac0a19fd2014-03-04 17:24:09 +020058 * @write: Flash write ops: Write len bytes from buf into offset
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +053059 * Supported cmds: Page Program
60 * @erase: Flash erase ops: Erase len bytes from offset
61 * Supported cmds: Sector erase 4K, 32K, 64K
Vasili Galkac0a19fd2014-03-04 17:24:09 +020062 * return 0 - Success, 1 - Failure
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +053063 */
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020064struct spi_flash {
65 struct spi_slave *spi;
Pavel Machek026f1fd2015-04-21 10:37:45 +020066#ifdef CONFIG_DM_SPI_FLASH
Simon Glassd6eedf72014-10-13 23:42:06 -060067 struct udevice *dev;
Bin Mengc4801ad2015-04-28 13:29:54 +053068 u16 flags;
Simon Glassd6eedf72014-10-13 23:42:06 -060069#endif
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +053070 const char *name;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +053071 u8 dual_flash;
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +053072 u8 shift;
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020073
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +053074 u32 size;
75 u32 page_size;
76 u32 sector_size;
77 u32 erase_size;
Jagannadha Sutradharudu Tekic6d173d2013-06-19 15:33:58 +053078#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +053079 u8 bank_read_cmd;
80 u8 bank_write_cmd;
81 u8 bank_curr;
Jagannadha Sutradharudu Tekic6d173d2013-06-19 15:33:58 +053082#endif
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +053083 u8 poll_cmd;
84 u8 erase_cmd;
Jagannadha Sutradharudu Teki02eee9a2014-01-11 15:10:28 +053085 u8 read_cmd;
Jagannadha Sutradharudu Tekie0ebabc2014-01-11 15:13:11 +053086 u8 write_cmd;
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +053087 u8 dummy_byte;
Jagannadha Sutradharudu Teki750f3ac2013-06-21 15:56:30 +053088
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +053089 void *memory_map;
Simon Glassd6eedf72014-10-13 23:42:06 -060090#ifndef CONFIG_DM_SPI_FLASH
91 /*
92 * These are not strictly needed for driver model, but keep them here
Pavel Machek026f1fd2015-04-21 10:37:45 +020093 * while the transition is in progress.
Simon Glassd6eedf72014-10-13 23:42:06 -060094 *
95 * Normally each driver would provide its own operations, but for
96 * SPI flash most chips use the same algorithms. One approach is
97 * to create a 'common' SPI flash device which knows how to talk
98 * to most devices, and then allow other drivers to be used instead
Pavel Machek026f1fd2015-04-21 10:37:45 +020099 * if required, perhaps with a way of scanning through the list to
Simon Glassd6eedf72014-10-13 23:42:06 -0600100 * find the driver that matches the device.
101 */
Jagannadha Sutradharudu Tekif000ad02013-09-28 18:10:43 +0530102 int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
103 int (*write)(struct spi_flash *flash, u32 offset, size_t len,
104 const void *buf);
105 int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
Simon Glassd6eedf72014-10-13 23:42:06 -0600106#endif
107};
108
109struct dm_spi_flash_ops {
110 int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
111 int (*write)(struct udevice *dev, u32 offset, size_t len,
112 const void *buf);
113 int (*erase)(struct udevice *dev, u32 offset, size_t len);
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200114};
115
Simon Glassd6eedf72014-10-13 23:42:06 -0600116/* Access the serial operations for a device */
117#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
118
119#ifdef CONFIG_DM_SPI_FLASH
Simon Glass1c369232015-03-26 09:29:25 -0600120/**
121 * spi_flash_read_dm() - Read data from SPI flash
122 *
123 * @dev: SPI flash device
124 * @offset: Offset into device in bytes to read from
125 * @len: Number of bytes to read
126 * @buf: Buffer to put the data that is read
127 * @return 0 if OK, -ve on error
128 */
129int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
130
131/**
132 * spi_flash_write_dm() - Write data to SPI flash
133 *
134 * @dev: SPI flash device
135 * @offset: Offset into device in bytes to write to
136 * @len: Number of bytes to write
137 * @buf: Buffer containing bytes to write
138 * @return 0 if OK, -ve on error
139 */
140int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
141 const void *buf);
142
143/**
144 * spi_flash_erase_dm() - Erase blocks of the SPI flash
145 *
146 * Note that @len must be a muiltiple of the flash sector size.
147 *
148 * @dev: SPI flash device
149 * @offset: Offset into device in bytes to start erasing
150 * @len: Number of bytes to erase
151 * @return 0 if OK, -ve on error
152 */
153int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
154
Simon Glassd6eedf72014-10-13 23:42:06 -0600155int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
156 unsigned int max_hz, unsigned int spi_mode,
157 struct udevice **devp);
158
159/* Compatibility function - this is the old U-Boot API */
160struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
161 unsigned int max_hz, unsigned int spi_mode);
162
163/* Compatibility function - this is the old U-Boot API */
164void spi_flash_free(struct spi_flash *flash);
165
166int spi_flash_remove(struct udevice *flash);
167
168static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
Simon Glass1c369232015-03-26 09:29:25 -0600169 size_t len, void *buf)
Simon Glassd6eedf72014-10-13 23:42:06 -0600170{
Simon Glass1c369232015-03-26 09:29:25 -0600171 return spi_flash_read_dm(flash->dev, offset, len, buf);
Simon Glassd6eedf72014-10-13 23:42:06 -0600172}
173
174static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
Simon Glass1c369232015-03-26 09:29:25 -0600175 size_t len, const void *buf)
Simon Glassd6eedf72014-10-13 23:42:06 -0600176{
Simon Glass1c369232015-03-26 09:29:25 -0600177 return spi_flash_write_dm(flash->dev, offset, len, buf);
Simon Glassd6eedf72014-10-13 23:42:06 -0600178}
179
180static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
Simon Glass1c369232015-03-26 09:29:25 -0600181 size_t len)
Simon Glassd6eedf72014-10-13 23:42:06 -0600182{
Simon Glass1c369232015-03-26 09:29:25 -0600183 return spi_flash_erase_dm(flash->dev, offset, len);
Simon Glassd6eedf72014-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 Skinnemoen2f5bfb72008-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 Glass49e9d2c2013-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 Skinnemoen2f5bfb72008-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 Glassd6eedf72014-10-13 23:42:06 -0600228#endif
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200229
Christian Riesch23f16a82011-12-09 09:47:35 +0000230void spi_boot(void) __noreturn;
Prabhakar Kushwaha9ea255a2014-04-08 19:13:22 +0530231void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);
Christian Riesch23f16a82011-12-09 09:47:35 +0000232
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200233#endif /* _SPI_FLASH_H_ */