blob: 408a5b401cd91e66e88b84eab5c1dcef97681522 [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 *
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 Denk3765b3e2013-10-07 13:07:26 +020012 * version 2 as published by the Free Software Foundation.
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020013 */
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +053014
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020015#ifndef _SPI_FLASH_H_
16#define _SPI_FLASH_H_
17
18#include <spi.h>
Mike Frysingere06ab652009-11-03 11:36:39 -050019#include <linux/types.h>
Christian Riesch32b11272011-12-09 09:47:35 +000020#include <linux/compiler.h>
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020021
Nikita Kiryanov88e34e52014-08-20 15:08:48 +030022#ifndef CONFIG_SF_DEFAULT_SPEED
23# define CONFIG_SF_DEFAULT_SPEED 1000000
24#endif
25#ifndef CONFIG_SF_DEFAULT_MODE
26# define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
27#endif
28#ifndef CONFIG_SF_DEFAULT_CS
29# define CONFIG_SF_DEFAULT_CS 0
30#endif
31#ifndef CONFIG_SF_DEFAULT_BUS
32# define CONFIG_SF_DEFAULT_BUS 0
33#endif
34
Jagannadha Sutradharudu Teki2ba863f2014-01-12 21:38:21 +053035/* sf param flags */
36#define SECT_4K 1 << 1
37#define SECT_32K 1 << 2
38#define E_FSR 1 << 3
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +053039#define WR_QPP 1 << 4
40
41/* Enum list - Full read commands */
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053042enum spi_read_cmds {
43 ARRAY_SLOW = 1 << 0,
44 DUAL_OUTPUT_FAST = 1 << 1,
45 DUAL_IO_FAST = 1 << 2,
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +053046 QUAD_OUTPUT_FAST = 1 << 3,
Jagannadha Sutradharudu Tekic4ba0d82013-12-24 15:24:31 +053047 QUAD_IO_FAST = 1 << 4,
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053048};
49#define RD_EXTN ARRAY_SLOW | DUAL_OUTPUT_FAST | DUAL_IO_FAST
Jagannadha Sutradharudu Tekic4ba0d82013-12-24 15:24:31 +053050#define RD_FULL RD_EXTN | QUAD_OUTPUT_FAST | QUAD_IO_FAST
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053051
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +053052/* Dual SPI flash memories */
53enum spi_dual_flash {
54 SF_SINGLE_FLASH = 0,
55 SF_DUAL_STACKED_FLASH = 1 << 0,
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +053056 SF_DUAL_PARALLEL_FLASH = 1 << 1,
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +053057};
58
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053059/**
Jagannadha Sutradharudu Teki33adfb52013-12-23 23:34:42 +053060 * struct spi_flash_params - SPI/QSPI flash device params structure
61 *
62 * @name: Device name ([MANUFLETTER][DEVTYPE][DENSITY][EXTRAINFO])
63 * @jedec: Device jedec ID (0x[1byte_manuf_id][2byte_dev_id])
64 * @ext_jedec: Device ext_jedec ID
65 * @sector_size: Sector size of this device
66 * @nr_sectors: No.of sectors on this device
67 * @e_rd_cmd: Enum list for read commands
Vasili Galka801cec52014-03-04 17:24:09 +020068 * @flags: Important param, for flash specific behaviour
Jagannadha Sutradharudu Teki33adfb52013-12-23 23:34:42 +053069 */
70struct spi_flash_params {
71 const char *name;
72 u32 jedec;
73 u16 ext_jedec;
74 u32 sector_size;
75 u32 nr_sectors;
76 u8 e_rd_cmd;
77 u16 flags;
78};
79
80extern const struct spi_flash_params spi_flash_params_table[];
81
82/**
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053083 * struct spi_flash - SPI flash structure
84 *
85 * @spi: SPI slave
86 * @name: Name of SPI flash
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +053087 * @dual_flash: Indicates dual flash memories - dual stacked, parallel
88 * @shift: Flash shift useful in dual parallel
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053089 * @size: Total flash size
90 * @page_size: Write (page) size
91 * @sector_size: Sector size
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +053092 * @erase_size: Erase size
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +053093 * @bank_read_cmd: Bank read cmd
94 * @bank_write_cmd: Bank write cmd
95 * @bank_curr: Current flash bank
96 * @poll_cmd: Poll cmd - for flash erase/program
97 * @erase_cmd: Erase cmd 4K, 32K, 64K
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +053098 * @read_cmd: Read cmd - Array Fast, Extn read and quad read.
99 * @write_cmd: Write cmd - page and quad program.
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +0530100 * @dummy_byte: Dummy cycles for read operation.
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +0530101 * @memory_map: Address of read-only SPI flash access
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +0530102 * @read: Flash read ops: Read len bytes at offset into buf
103 * Supported cmds: Fast Array Read
Vasili Galka801cec52014-03-04 17:24:09 +0200104 * @write: Flash write ops: Write len bytes from buf into offset
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +0530105 * Supported cmds: Page Program
106 * @erase: Flash erase ops: Erase len bytes from offset
107 * Supported cmds: Sector erase 4K, 32K, 64K
Vasili Galka801cec52014-03-04 17:24:09 +0200108 * return 0 - Success, 1 - Failure
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +0530109 */
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200110struct spi_flash {
111 struct spi_slave *spi;
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +0530112 const char *name;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530113 u8 dual_flash;
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530114 u8 shift;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200115
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +0530116 u32 size;
117 u32 page_size;
118 u32 sector_size;
119 u32 erase_size;
Jagannadha Sutradharudu Teki1dcd6d02013-06-19 15:33:58 +0530120#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +0530121 u8 bank_read_cmd;
122 u8 bank_write_cmd;
123 u8 bank_curr;
Jagannadha Sutradharudu Teki1dcd6d02013-06-19 15:33:58 +0530124#endif
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +0530125 u8 poll_cmd;
126 u8 erase_cmd;
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +0530127 u8 read_cmd;
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +0530128 u8 write_cmd;
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +0530129 u8 dummy_byte;
Jagannadha Sutradharudu Teki615a1562013-06-21 15:56:30 +0530130
Jagannadha Sutradharudu Teki7ab35d92013-09-28 18:10:43 +0530131 void *memory_map;
132 int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
133 int (*write)(struct spi_flash *flash, u32 offset, size_t len,
134 const void *buf);
135 int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200136};
137
138struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
139 unsigned int max_hz, unsigned int spi_mode);
Simon Glass0efc0242013-12-03 16:43:24 -0700140
141/**
142 * Set up a new SPI flash from an fdt node
143 *
144 * @param blob Device tree blob
145 * @param slave_node Pointer to this SPI slave node in the device tree
146 * @param spi_node Cached pointer to the SPI interface this node belongs
147 * to
148 * @return 0 if ok, -1 on error
149 */
150struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
151 int spi_node);
152
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200153void spi_flash_free(struct spi_flash *flash);
154
155static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
156 size_t len, void *buf)
157{
158 return flash->read(flash, offset, len, buf);
159}
160
161static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
162 size_t len, const void *buf)
163{
164 return flash->write(flash, offset, len, buf);
165}
166
167static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
168 size_t len)
169{
170 return flash->erase(flash, offset, len);
171}
172
Christian Riesch32b11272011-12-09 09:47:35 +0000173void spi_boot(void) __noreturn;
Prabhakar Kushwaha1eaa7422014-04-08 19:13:22 +0530174void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);
Christian Riesch32b11272011-12-09 09:47:35 +0000175
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200176#endif /* _SPI_FLASH_H_ */