blob: 6b89012a261d2e6eb3791fa0ce0bb308b55d6b68 [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
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070027struct spi_flash {
Furquan Shaikh810e2cd2016-12-05 20:32:24 -080028 struct spi_slave spi;
Furquan Shaikhc28984d2016-11-20 21:04:00 -080029 const char *name;
30 u32 size;
31 u32 sector_size;
32 u8 erase_cmd;
33 u8 status_cmd;
34 /*
35 * Internal functions are expected to be called ONLY by spi flash
36 * driver. External components should only use the public API calls
37 * spi_flash_{read,write,erase,status,volatile_group_begin,
38 * volatile_group_end}.
39 */
40 int (*internal_read)(const struct spi_flash *flash, u32 offset,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070041 size_t len, void *buf);
Furquan Shaikhc28984d2016-11-20 21:04:00 -080042 int (*internal_write)(const struct spi_flash *flash, u32 offset,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070043 size_t len, const void *buf);
Furquan Shaikhc28984d2016-11-20 21:04:00 -080044 int (*internal_erase)(const struct spi_flash *flash, u32 offset,
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070045 size_t len);
Furquan Shaikhc28984d2016-11-20 21:04:00 -080046 int (*internal_status)(const struct spi_flash *flash, u8 *reg);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070047};
48
Furquan Shaikhc28984d2016-11-20 21:04:00 -080049void lb_spi_flash(struct lb_header *header);
50
51/* SPI Flash Driver Public API */
Gabe Black1e187352014-03-27 20:37:03 -070052struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs);
Furquan Shaikhd2fb6ae2016-11-17 20:38:07 -080053/*
54 * Specialized probing performed by platform. This is a weak function which can
55 * be overriden by platform driver.
56 * spi = Pointer to spi_slave structure.
57 * force = Indicates if the platform driver can skip specialized probing.
58 */
59struct spi_flash *spi_flash_programmer_probe(struct spi_slave *spi, int force);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070060
Furquan Shaikhc28984d2016-11-20 21:04:00 -080061/* All the following functions return 0 on success and non-zero on error. */
62int spi_flash_read(const struct spi_flash *flash, u32 offset, size_t len,
63 void *buf);
64int spi_flash_write(const struct spi_flash *flash, u32 offset, size_t len,
65 const void *buf);
66int spi_flash_erase(const struct spi_flash *flash, u32 offset, size_t len);
67int spi_flash_status(const struct spi_flash *flash, u8 *reg);
68/*
69 * Some SPI controllers require exclusive access to SPI flash when volatile
70 * operations like erase or write are being performed. In such cases,
71 * volatile_group_begin will gain exclusive access to SPI flash if not already
72 * acquired and volatile_group_end will end exclusive access if this was the
73 * last request in the group. spi_flash_{write,erase} operations call
74 * volatile_group_begin at the start of function and volatile_group_end after
75 * erase/write operation is performed. These functions can also be used by any
76 * components that wish to club multiple volatile operations into a single
77 * group.
78 */
79int spi_flash_volatile_group_begin(const struct spi_flash *flash);
80int spi_flash_volatile_group_end(const struct spi_flash *flash);
81
82/*
83 * These are callbacks for marking the start and end of volatile group as
84 * handled by the chipset. Not every chipset requires this special handling. So,
85 * these functions are expected to be implemented in Kconfig option for volatile
86 * group is enabled (SPI_FLASH_HAS_VOLATILE_GROUP).
87 */
88int chipset_volatile_group_begin(const struct spi_flash *flash);
89int chipset_volatile_group_end(const struct spi_flash *flash);
Dan Ehrenberga5aac762015-01-08 10:29:19 -080090
Aaron Durbin305c0ca2016-12-03 17:04:06 -060091/* Return spi_flash object reference for the boot device. This is only valid
92 * if CONFIG_BOOT_DEVICE_SPI_FLASH is enabled. */
93const struct spi_flash *boot_device_spi_flash(void);
94
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070095#endif /* _SPI_FLASH_H_ */