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 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * version 2 as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | #ifndef _SPI_FLASH_H_ |
| 24 | #define _SPI_FLASH_H_ |
| 25 | |
| 26 | #include <stdint.h> |
| 27 | #include <stddef.h> |
| 28 | #include <console/console.h> |
| 29 | #include <spi.h> |
| 30 | |
| 31 | /** |
| 32 | * container_of - cast a member of a structure out to the containing structure |
| 33 | * @ptr: the pointer to the member. |
| 34 | * @type: the type of the container struct this is embedded in. |
| 35 | * @member: the name of the member within the struct. |
| 36 | * |
| 37 | */ |
| 38 | #define container_of(ptr, type, member) ({ \ |
| 39 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 40 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 41 | |
| 42 | #define min(a, b) ((a)<(b)?(a):(b)) |
| 43 | |
| 44 | #define CONFIG_ICH_SPI |
| 45 | #ifdef CONFIG_ICH_SPI |
| 46 | #define CONTROLLER_PAGE_LIMIT 64 |
| 47 | #else |
| 48 | /* any number larger than 4K would do, actually */ |
| 49 | #define CONTROLLER_PAGE_LIMIT ((int)(~0U>>1)) |
| 50 | #endif |
| 51 | |
| 52 | struct spi_flash { |
| 53 | struct spi_slave *spi; |
| 54 | |
| 55 | const char *name; |
| 56 | |
| 57 | u32 size; |
| 58 | |
| 59 | u32 sector_size; |
| 60 | |
| 61 | int (*read)(struct spi_flash *flash, u32 offset, |
| 62 | size_t len, void *buf); |
| 63 | int (*write)(struct spi_flash *flash, u32 offset, |
| 64 | size_t len, const void *buf); |
| 65 | int (*erase)(struct spi_flash *flash, u32 offset, |
| 66 | size_t len); |
| 67 | }; |
| 68 | |
| 69 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 70 | unsigned int max_hz, unsigned int spi_mode); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 71 | |
| 72 | static inline int spi_flash_read(struct spi_flash *flash, u32 offset, |
| 73 | size_t len, void *buf) |
| 74 | { |
| 75 | return flash->read(flash, offset, len, buf); |
| 76 | } |
| 77 | |
| 78 | static inline int spi_flash_write(struct spi_flash *flash, u32 offset, |
| 79 | size_t len, const void *buf) |
| 80 | { |
| 81 | return flash->write(flash, offset, len, buf); |
| 82 | } |
| 83 | |
| 84 | static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, |
| 85 | size_t len) |
| 86 | { |
| 87 | return flash->erase(flash, offset, len); |
| 88 | } |
| 89 | |
| 90 | #endif /* _SPI_FLASH_H_ */ |