Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Interface to SPI flash |
| 3 | * |
| 4 | * Copyright (C) 2008 Atmel Corporation |
| 5 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 6 | * 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 Menzel | a8ae1c6 | 2013-02-20 13:21:20 +0100 | [diff] [blame] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 13 | * GNU General Public License for more details. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 14 | */ |
| 15 | #ifndef _SPI_FLASH_H_ |
| 16 | #define _SPI_FLASH_H_ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <stddef.h> |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 20 | #include <spi-generic.h> |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 21 | #include <boot/coreboot_tables.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 22 | |
Furquan Shaikh | 52896c6 | 2016-11-22 11:43:58 -0800 | [diff] [blame] | 23 | /* SPI Flash opcodes */ |
| 24 | #define SPI_OPCODE_WREN 0x06 |
| 25 | #define SPI_OPCODE_FAST_READ 0x0b |
| 26 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 27 | struct spi_flash { |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 28 | struct spi_slave spi; |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 29 | 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 Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 41 | size_t len, void *buf); |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 42 | int (*internal_write)(const struct spi_flash *flash, u32 offset, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 43 | size_t len, const void *buf); |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 44 | int (*internal_erase)(const struct spi_flash *flash, u32 offset, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 45 | size_t len); |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 46 | int (*internal_status)(const struct spi_flash *flash, u8 *reg); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 49 | void lb_spi_flash(struct lb_header *header); |
| 50 | |
| 51 | /* SPI Flash Driver Public API */ |
Gabe Black | 1e18735 | 2014-03-27 20:37:03 -0700 | [diff] [blame] | 52 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs); |
Furquan Shaikh | d2fb6ae | 2016-11-17 20:38:07 -0800 | [diff] [blame] | 53 | /* |
| 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 | */ |
| 59 | struct spi_flash *spi_flash_programmer_probe(struct spi_slave *spi, int force); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 60 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 61 | /* All the following functions return 0 on success and non-zero on error. */ |
| 62 | int spi_flash_read(const struct spi_flash *flash, u32 offset, size_t len, |
| 63 | void *buf); |
| 64 | int spi_flash_write(const struct spi_flash *flash, u32 offset, size_t len, |
| 65 | const void *buf); |
| 66 | int spi_flash_erase(const struct spi_flash *flash, u32 offset, size_t len); |
| 67 | int 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 | */ |
| 79 | int spi_flash_volatile_group_begin(const struct spi_flash *flash); |
| 80 | int 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 | */ |
| 88 | int chipset_volatile_group_begin(const struct spi_flash *flash); |
| 89 | int chipset_volatile_group_end(const struct spi_flash *flash); |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 90 | |
Aaron Durbin | 305c0ca | 2016-12-03 17:04:06 -0600 | [diff] [blame] | 91 | /* Return spi_flash object reference for the boot device. This is only valid |
| 92 | * if CONFIG_BOOT_DEVICE_SPI_FLASH is enabled. */ |
| 93 | const struct spi_flash *boot_device_spi_flash(void); |
| 94 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 95 | #endif /* _SPI_FLASH_H_ */ |