blob: f7f3b3dbdf078af6a189805b84fdb4a7cd78c196 [file] [log] [blame]
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07001/*
2 * Interface to SPI flash
3 *
4 * Copyright (C) 2008 Atmel Corporation
5 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Paul Menzela8ae1c62013-02-20 13:21:20 +010012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070013 * GNU General Public License for more details.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070014 */
15#ifndef _SPI_FLASH_H_
16#define _SPI_FLASH_H_
17
18#include <stdint.h>
19#include <stddef.h>
Furquan Shaikh810e2cd2016-12-05 20:32:24 -080020#include <spi-generic.h>
Dan Ehrenberga5aac762015-01-08 10:29:19 -080021#include <boot/coreboot_tables.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070022
Furquan Shaikh52896c62016-11-22 11:43:58 -080023/* SPI Flash opcodes */
24#define SPI_OPCODE_WREN 0x06
25#define SPI_OPCODE_FAST_READ 0x0b
26
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -070027struct spi_flash;
28
29/*
30 * Representation of SPI flash operations:
31 * read: Flash read operation.
32 * write: Flash write operation.
33 * erase: Flash erase operation.
34 * status: Read flash status register.
35 */
36struct spi_flash_ops {
37 int (*read)(const struct spi_flash *flash, u32 offset, size_t len,
38 void *buf);
39 int (*write)(const struct spi_flash *flash, u32 offset, size_t len,
40 const void *buf);
41 int (*erase)(const struct spi_flash *flash, u32 offset, size_t len);
42 int (*status)(const struct spi_flash *flash, u8 *reg);
43};
44
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070045struct spi_flash {
Furquan Shaikh810e2cd2016-12-05 20:32:24 -080046 struct spi_slave spi;
Furquan Shaikhc28984d2016-11-20 21:04:00 -080047 const char *name;
48 u32 size;
49 u32 sector_size;
Furquan Shaikhfc1a1232017-05-12 00:19:56 -070050 u32 page_size;
Furquan Shaikhc28984d2016-11-20 21:04:00 -080051 u8 erase_cmd;
52 u8 status_cmd;
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -070053 const struct spi_flash_ops *ops;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070054};
55
Furquan Shaikhc28984d2016-11-20 21:04:00 -080056void lb_spi_flash(struct lb_header *header);
57
58/* SPI Flash Driver Public API */
Furquan Shaikh30221b42017-05-15 14:35:15 -070059
60/*
61 * Probe for SPI flash chip on given SPI bus and chip select and fill info in
62 * spi_flash structure.
63 *
64 * Params:
65 * bus = SPI Bus # for the flash chip
66 * cs = Chip select # for the flash chip
67 * flash = Pointer to spi flash structure that needs to be filled
68 *
69 * Return value:
70 * 0 = success
71 * non-zero = error
72 */
73int spi_flash_probe(unsigned int bus, unsigned int cs, struct spi_flash *flash);
Furquan Shaikha1491572017-05-17 19:14:06 -070074
Furquan Shaikhd2fb6ae2016-11-17 20:38:07 -080075/*
Furquan Shaikha1491572017-05-17 19:14:06 -070076 * Generic probing for SPI flash chip based on the different flashes provided.
Furquan Shaikh30221b42017-05-15 14:35:15 -070077 *
78 * Params:
Furquan Shaikha1491572017-05-17 19:14:06 -070079 * spi = Pointer to spi_slave structure
Furquan Shaikh30221b42017-05-15 14:35:15 -070080 * flash = Pointer to spi_flash structure that needs to be filled.
81 *
82 * Return value:
Furquan Shaikha1491572017-05-17 19:14:06 -070083 * 0 = success
Furquan Shaikh30221b42017-05-15 14:35:15 -070084 * non-zero = error
Furquan Shaikhd2fb6ae2016-11-17 20:38:07 -080085 */
Furquan Shaikha1491572017-05-17 19:14:06 -070086int spi_flash_generic_probe(const struct spi_slave *slave,
Furquan Shaikh30221b42017-05-15 14:35:15 -070087 struct spi_flash *flash);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070088
Furquan Shaikhc28984d2016-11-20 21:04:00 -080089/* All the following functions return 0 on success and non-zero on error. */
90int spi_flash_read(const struct spi_flash *flash, u32 offset, size_t len,
91 void *buf);
92int spi_flash_write(const struct spi_flash *flash, u32 offset, size_t len,
93 const void *buf);
94int spi_flash_erase(const struct spi_flash *flash, u32 offset, size_t len);
95int spi_flash_status(const struct spi_flash *flash, u8 *reg);
96/*
97 * Some SPI controllers require exclusive access to SPI flash when volatile
98 * operations like erase or write are being performed. In such cases,
99 * volatile_group_begin will gain exclusive access to SPI flash if not already
100 * acquired and volatile_group_end will end exclusive access if this was the
101 * last request in the group. spi_flash_{write,erase} operations call
102 * volatile_group_begin at the start of function and volatile_group_end after
103 * erase/write operation is performed. These functions can also be used by any
104 * components that wish to club multiple volatile operations into a single
105 * group.
106 */
107int spi_flash_volatile_group_begin(const struct spi_flash *flash);
108int spi_flash_volatile_group_end(const struct spi_flash *flash);
109
110/*
111 * These are callbacks for marking the start and end of volatile group as
112 * handled by the chipset. Not every chipset requires this special handling. So,
113 * these functions are expected to be implemented in Kconfig option for volatile
114 * group is enabled (SPI_FLASH_HAS_VOLATILE_GROUP).
115 */
116int chipset_volatile_group_begin(const struct spi_flash *flash);
117int chipset_volatile_group_end(const struct spi_flash *flash);
Dan Ehrenberga5aac762015-01-08 10:29:19 -0800118
Aaron Durbin305c0ca2016-12-03 17:04:06 -0600119/* Return spi_flash object reference for the boot device. This is only valid
120 * if CONFIG_BOOT_DEVICE_SPI_FLASH is enabled. */
121const struct spi_flash *boot_device_spi_flash(void);
122
Aaron Durbin10d65b02017-12-14 14:34:47 -0700123/* Protect a region of spi flash using its controller, if available. Returns
124 * < 0 on error, else 0 on success. */
125int spi_flash_ctrlr_protect_region(const struct spi_flash *flash,
126 const struct region *region);
127
Aaron Durbin851dde82018-04-19 21:15:25 -0600128/*
129 * This function is provided to support spi flash command-response transactions.
130 * Only 2 vectors are supported and the 'func' is called with appropriate
131 * write and read buffers together. This can be used for chipsets that
132 * have specific spi flash controllers that don't conform to the normal
133 * spi xfer API because they are specialized controllers and not generic.
134 *
135 * Returns 0 on success and non-zero on failure.
136 */
137int spi_flash_vector_helper(const struct spi_slave *slave,
138 struct spi_op vectors[], size_t count,
139 int (*func)(const struct spi_slave *slave, const void *dout,
140 size_t bytesout, void *din, size_t bytesin));
141
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700142#endif /* _SPI_FLASH_H_ */