2 * Common SPI flash Interface
4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
18 #include <dm.h> /* Because we dereference struct udevice here */
19 #include <linux/types.h>
21 #ifndef CONFIG_SF_DEFAULT_SPEED
22 # define CONFIG_SF_DEFAULT_SPEED 1000000
24 #ifndef CONFIG_SF_DEFAULT_MODE
25 # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
27 #ifndef CONFIG_SF_DEFAULT_CS
28 # define CONFIG_SF_DEFAULT_CS 0
30 #ifndef CONFIG_SF_DEFAULT_BUS
31 # define CONFIG_SF_DEFAULT_BUS 0
37 * struct spi_flash - SPI flash structure
40 * @name: Name of SPI flash
41 * @dual_flash: Indicates dual flash memories - dual stacked, parallel
42 * @shift: Flash shift useful in dual parallel
43 * @size: Total flash size
44 * @page_size: Write (page) size
45 * @sector_size: Sector size
46 * @erase_size: Erase size
47 * @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
52 * @read_cmd: Read cmd - Array Fast, Extn read and quad read.
53 * @write_cmd: Write cmd - page and quad program.
54 * @dummy_byte: Dummy cycles for read operation.
55 * @memory_map: Address of read-only SPI flash access
56 * @read: Flash read ops: Read len bytes at offset into buf
57 * Supported cmds: Fast Array Read
58 * @write: Flash write ops: Write len bytes from buf into offset
59 * Supported cmds: Page Program
60 * @erase: Flash erase ops: Erase len bytes from offset
61 * Supported cmds: Sector erase 4K, 32K, 64K
62 * return 0 - Success, 1 - Failure
65 #ifdef CONFIG_DM_SPI_FLASH
66 struct spi_slave *spi;
69 struct spi_slave *spi;
79 #ifdef CONFIG_SPI_FLASH_BAR
91 #ifndef CONFIG_DM_SPI_FLASH
93 * These are not strictly needed for driver model, but keep them here
94 * whilt the transition is in progress.
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
100 * if requird, perhaps with a way of scanning through the list to
101 * find the driver that matches the device.
103 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,
106 int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
110 struct 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,
114 int (*erase)(struct udevice *dev, u32 offset, size_t len);
117 /* Access the serial operations for a device */
118 #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
120 #ifdef CONFIG_DM_SPI_FLASH
121 int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
122 unsigned int max_hz, unsigned int spi_mode,
123 struct udevice **devp);
125 /* Compatibility function - this is the old U-Boot API */
126 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
127 unsigned int max_hz, unsigned int spi_mode);
129 /* Compatibility function - this is the old U-Boot API */
130 void spi_flash_free(struct spi_flash *flash);
132 int spi_flash_remove(struct udevice *flash);
134 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
135 size_t len, void *buf)
137 return sf_get_ops(flash->dev)->read(flash->dev, offset, len, buf);
140 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
141 size_t len, const void *buf)
143 return sf_get_ops(flash->dev)->write(flash->dev, offset, len, buf);
146 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
149 return sf_get_ops(flash->dev)->erase(flash->dev, offset, len);
152 struct sandbox_state;
154 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
155 struct udevice *bus, int of_offset, const char *spec);
157 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
160 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
161 unsigned int max_hz, unsigned int spi_mode);
164 * Set up a new SPI flash from an fdt node
166 * @param blob Device tree blob
167 * @param slave_node Pointer to this SPI slave node in the device tree
168 * @param spi_node Cached pointer to the SPI interface this node belongs
170 * @return 0 if ok, -1 on error
172 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
175 void spi_flash_free(struct spi_flash *flash);
177 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
178 size_t len, void *buf)
180 return flash->read(flash, offset, len, buf);
183 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
184 size_t len, const void *buf)
186 return flash->write(flash, offset, len, buf);
189 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
192 return flash->erase(flash, offset, len);
196 void spi_boot(void) __noreturn;
197 void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);
199 #endif /* _SPI_FLASH_H_ */