blob: f1fe1a6659a37d1072bd7d6bb39cdffb40a01b5b [file] [log] [blame]
Huang Shijie8eabdd12014-04-10 16:27:28 +08001/*
2 * Copyright (C) 2014 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
Huang Shijief39d2fa2014-02-24 18:37:35 +080010#ifndef __LINUX_MTD_SPI_NOR_H
11#define __LINUX_MTD_SPI_NOR_H
12
13/* Flash opcodes. */
Brian Norrisb02e7f32014-04-08 18:15:31 -070014#define SPINOR_OP_WREN 0x06 /* Write enable */
15#define SPINOR_OP_RDSR 0x05 /* Read status register */
16#define SPINOR_OP_WRSR 0x01 /* Write status register 1 byte */
17#define SPINOR_OP_NORM_READ 0x03 /* Read data bytes (low frequency) */
18#define SPINOR_OP_FAST_READ 0x0b /* Read data bytes (high frequency) */
19#define SPINOR_OP_DUAL_READ 0x3b /* Read data bytes (Dual SPI) */
20#define SPINOR_OP_QUAD_READ 0x6b /* Read data bytes (Quad SPI) */
21#define SPINOR_OP_PP 0x02 /* Page program (up to 256 bytes) */
22#define SPINOR_OP_BE_4K 0x20 /* Erase 4KiB block */
23#define SPINOR_OP_BE_4K_PMC 0xd7 /* Erase 4KiB block on PMC chips */
24#define SPINOR_OP_BE_32K 0x52 /* Erase 32KiB block */
25#define SPINOR_OP_CHIP_ERASE 0xc7 /* Erase whole flash chip */
26#define SPINOR_OP_SE 0xd8 /* Sector erase (usually 64KiB) */
27#define SPINOR_OP_RDID 0x9f /* Read JEDEC ID */
28#define SPINOR_OP_RDCR 0x35 /* Read configuration register */
Huang Shijief39d2fa2014-02-24 18:37:35 +080029
30/* 4-byte address opcodes - used on Spansion and some Macronix flashes. */
Brian Norrisb02e7f32014-04-08 18:15:31 -070031#define SPINOR_OP_NORM_READ_4B 0x13 /* Read data bytes (low frequency) */
32#define SPINOR_OP_FAST_READ_4B 0x0c /* Read data bytes (high frequency) */
33#define SPINOR_OP_DUAL_READ_4B 0x3c /* Read data bytes (Dual SPI) */
34#define SPINOR_OP_QUAD_READ_4B 0x6c /* Read data bytes (Quad SPI) */
35#define SPINOR_OP_PP_4B 0x12 /* Page program (up to 256 bytes) */
36#define SPINOR_OP_SE_4B 0xdc /* Sector erase (usually 64KiB) */
Huang Shijief39d2fa2014-02-24 18:37:35 +080037
38/* Used for SST flashes only. */
Brian Norrisb02e7f32014-04-08 18:15:31 -070039#define SPINOR_OP_BP 0x02 /* Byte program */
40#define SPINOR_OP_WRDI 0x04 /* Write disable */
41#define SPINOR_OP_AAI_WP 0xad /* Auto address increment word program */
Huang Shijief39d2fa2014-02-24 18:37:35 +080042
43/* Used for Macronix and Winbond flashes. */
Brian Norrisb02e7f32014-04-08 18:15:31 -070044#define SPINOR_OP_EN4B 0xb7 /* Enter 4-byte mode */
45#define SPINOR_OP_EX4B 0xe9 /* Exit 4-byte mode */
Huang Shijief39d2fa2014-02-24 18:37:35 +080046
47/* Used for Spansion flashes only. */
Brian Norrisb02e7f32014-04-08 18:15:31 -070048#define SPINOR_OP_BRWR 0x17 /* Bank register write */
Huang Shijief39d2fa2014-02-24 18:37:35 +080049
50/* Status Register bits. */
Brian Norrisbecd0cb2014-04-08 18:10:23 -070051#define SR_WIP 1 /* Write in progress */
52#define SR_WEL 2 /* Write enable latch */
Huang Shijief39d2fa2014-02-24 18:37:35 +080053/* meaning of other SR_* bits may differ between vendors */
Brian Norrisbecd0cb2014-04-08 18:10:23 -070054#define SR_BP0 4 /* Block protect 0 */
55#define SR_BP1 8 /* Block protect 1 */
56#define SR_BP2 0x10 /* Block protect 2 */
57#define SR_SRWD 0x80 /* SR write protect */
Huang Shijief39d2fa2014-02-24 18:37:35 +080058
Brian Norrisbecd0cb2014-04-08 18:10:23 -070059#define SR_QUAD_EN_MX 0x40 /* Macronix Quad I/O */
Huang Shijief39d2fa2014-02-24 18:37:35 +080060
61/* Configuration Register bits. */
Brian Norrisbecd0cb2014-04-08 18:10:23 -070062#define CR_QUAD_EN_SPAN 0x2 /* Spansion Quad I/O */
Huang Shijief39d2fa2014-02-24 18:37:35 +080063
Huang Shijie6e602ef2014-02-24 18:37:36 +080064enum read_mode {
65 SPI_NOR_NORMAL = 0,
66 SPI_NOR_FAST,
67 SPI_NOR_DUAL,
68 SPI_NOR_QUAD,
69};
70
71/**
72 * struct spi_nor_xfer_cfg - Structure for defining a Serial Flash transfer
73 * @wren: command for "Write Enable", or 0x00 for not required
74 * @cmd: command for operation
75 * @cmd_pins: number of pins to send @cmd (1, 2, 4)
76 * @addr: address for operation
77 * @addr_pins: number of pins to send @addr (1, 2, 4)
78 * @addr_width: number of address bytes
79 * (3,4, or 0 for address not required)
80 * @mode: mode data
81 * @mode_pins: number of pins to send @mode (1, 2, 4)
82 * @mode_cycles: number of mode cycles (0 for mode not required)
83 * @dummy_cycles: number of dummy cycles (0 for dummy not required)
84 */
85struct spi_nor_xfer_cfg {
86 u8 wren;
87 u8 cmd;
88 u8 cmd_pins;
89 u32 addr;
90 u8 addr_pins;
91 u8 addr_width;
92 u8 mode;
93 u8 mode_pins;
94 u8 mode_cycles;
95 u8 dummy_cycles;
96};
97
Brian Norrisbecd0cb2014-04-08 18:10:23 -070098#define SPI_NOR_MAX_CMD_SIZE 8
Huang Shijie6e602ef2014-02-24 18:37:36 +080099enum spi_nor_ops {
100 SPI_NOR_OPS_READ = 0,
101 SPI_NOR_OPS_WRITE,
102 SPI_NOR_OPS_ERASE,
103 SPI_NOR_OPS_LOCK,
104 SPI_NOR_OPS_UNLOCK,
105};
106
107/**
108 * struct spi_nor - Structure for defining a the SPI NOR layer
109 * @mtd: point to a mtd_info structure
110 * @lock: the lock for the read/write/erase/lock/unlock operations
111 * @dev: point to a spi device, or a spi nor controller device.
112 * @page_size: the page size of the SPI NOR
113 * @addr_width: number of address bytes
114 * @erase_opcode: the opcode for erasing a sector
115 * @read_opcode: the read opcode
116 * @read_dummy: the dummy needed by the read operation
117 * @program_opcode: the program opcode
118 * @flash_read: the mode of the read
119 * @sst_write_second: used by the SST write operation
120 * @cfg: used by the read_xfer/write_xfer
121 * @cmd_buf: used by the write_reg
122 * @prepare: [OPTIONAL] do some preparations for the
123 * read/write/erase/lock/unlock operations
124 * @unprepare: [OPTIONAL] do some post work after the
125 * read/write/erase/lock/unlock operations
126 * @read_xfer: [OPTIONAL] the read fundamental primitive
127 * @write_xfer: [OPTIONAL] the writefundamental primitive
128 * @read_reg: [DRIVER-SPECIFIC] read out the register
129 * @write_reg: [DRIVER-SPECIFIC] write data to the register
130 * @read_id: [REPLACEABLE] read out the ID data, and find
131 * the proper spi_device_id
132 * @wait_till_ready: [REPLACEABLE] wait till the NOR becomes ready
133 * @read: [DRIVER-SPECIFIC] read data from the SPI NOR
134 * @write: [DRIVER-SPECIFIC] write data to the SPI NOR
135 * @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
136 * at the offset @offs
137 * @priv: the private data
138 */
139struct spi_nor {
140 struct mtd_info *mtd;
141 struct mutex lock;
142 struct device *dev;
143 u32 page_size;
144 u8 addr_width;
145 u8 erase_opcode;
146 u8 read_opcode;
147 u8 read_dummy;
148 u8 program_opcode;
149 enum read_mode flash_read;
150 bool sst_write_second;
151 struct spi_nor_xfer_cfg cfg;
152 u8 cmd_buf[SPI_NOR_MAX_CMD_SIZE];
153
154 int (*prepare)(struct spi_nor *nor, enum spi_nor_ops ops);
155 void (*unprepare)(struct spi_nor *nor, enum spi_nor_ops ops);
156 int (*read_xfer)(struct spi_nor *nor, struct spi_nor_xfer_cfg *cfg,
157 u8 *buf, size_t len);
158 int (*write_xfer)(struct spi_nor *nor, struct spi_nor_xfer_cfg *cfg,
159 u8 *buf, size_t len);
160 int (*read_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len);
161 int (*write_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len,
162 int write_enable);
163 const struct spi_device_id *(*read_id)(struct spi_nor *nor);
164 int (*wait_till_ready)(struct spi_nor *nor);
165
166 int (*read)(struct spi_nor *nor, loff_t from,
167 size_t len, size_t *retlen, u_char *read_buf);
168 void (*write)(struct spi_nor *nor, loff_t to,
169 size_t len, size_t *retlen, const u_char *write_buf);
170 int (*erase)(struct spi_nor *nor, loff_t offs);
171
172 void *priv;
173};
Huang Shijieb1994892014-02-24 18:37:37 +0800174
175/**
176 * spi_nor_scan() - scan the SPI NOR
177 * @nor: the spi_nor structure
178 * @id: the spi_device_id provided by the driver
179 * @mode: the read mode supported by the driver
180 *
181 * The drivers can use this fuction to scan the SPI NOR.
182 * In the scanning, it will try to get all the necessary information to
183 * fill the mtd_info{} and the spi_nor{}.
184 *
185 * The board may assigns a spi_device_id with @id which be used to compared with
186 * the spi_device_id detected by the scanning.
187 *
188 * Return: 0 for success, others for failure.
189 */
190int spi_nor_scan(struct spi_nor *nor, const struct spi_device_id *id,
191 enum read_mode mode);
192extern const struct spi_device_id spi_nor_ids[];
193
Huang Shijie0d8c11c2014-02-24 18:37:40 +0800194/**
195 * spi_nor_match_id() - find the spi_device_id by the name
196 * @name: the name of the spi_device_id
197 *
198 * The drivers use this function to find the spi_device_id
199 * specified by the @name.
200 *
201 * Return: returns the right spi_device_id pointer on success,
202 * and returns NULL on failure.
203 */
204const struct spi_device_id *spi_nor_match_id(char *name);
205
Huang Shijief39d2fa2014-02-24 18:37:35 +0800206#endif