uboot/include/spi_flash.h
<<
>>
Prefs
   1/*
   2 * Common SPI flash Interface
   3 *
   4 * Copyright (C) 2008 Atmel Corporation
   5 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
   6 *
   7 * SPDX-License-Identifier:     GPL-2.0
   8 */
   9
  10#ifndef _SPI_FLASH_H_
  11#define _SPI_FLASH_H_
  12
  13#include <dm.h> /* Because we dereference struct udevice here */
  14#include <linux/types.h>
  15
  16#define SF_DUALIO_FLASH 1
  17
  18#ifndef CONFIG_SF_DEFAULT_SPEED
  19# define CONFIG_SF_DEFAULT_SPEED        1000000
  20#endif
  21#ifndef CONFIG_SF_DEFAULT_MODE
  22# define CONFIG_SF_DEFAULT_MODE         SPI_MODE_3
  23#endif
  24#ifndef CONFIG_SF_DEFAULT_CS
  25# define CONFIG_SF_DEFAULT_CS           0
  26#endif
  27#ifndef CONFIG_SF_DEFAULT_BUS
  28# define CONFIG_SF_DEFAULT_BUS          0
  29#endif
  30
  31struct spi_slave;
  32
  33/**
  34 * struct spi_flash - SPI flash structure
  35 *
  36 * @spi:                SPI slave
  37 * @dev:                SPI flash device
  38 * @name:               Name of SPI flash
  39 * @dual_flash:         Indicates dual flash memories - dual stacked, parallel
  40 * @shift:              Flash shift useful in dual parallel
  41 * @flags:              Indication of spi flash flags
  42 * @size:               Total flash size
  43 * @page_size:          Write (page) size
  44 * @sector_size:        Sector size
  45 * @erase_size:         Erase size
  46 * @bank_read_cmd:      Bank read cmd
  47 * @bank_write_cmd:     Bank write cmd
  48 * @bank_curr:          Current flash bank
  49 * @erase_cmd:          Erase cmd 4K, 32K, 64K
  50 * @read_cmd:           Read cmd - Array Fast, Extn read and quad read.
  51 * @write_cmd:          Write cmd - page and quad program.
  52 * @dummy_byte:         Dummy cycles for read operation.
  53 * @memory_map:         Address of read-only SPI flash access
  54 * @flash_lock:         lock a region of the SPI Flash
  55 * @flash_unlock:       unlock a region of the SPI Flash
  56 * @flash_is_locked:    check if a region of the SPI Flash is completely locked
  57 * @read:               Flash read ops: Read len bytes at offset into buf
  58 *                      Supported cmds: Fast Array Read
  59 * @write:              Flash write ops: Write len bytes from buf into offset
  60 *                      Supported cmds: Page Program
  61 * @erase:              Flash erase ops: Erase len bytes from offset
  62 *                      Supported cmds: Sector erase 4K, 32K, 64K
  63 * return 0 - Success, 1 - Failure
  64 */
  65struct spi_flash {
  66        struct spi_slave *spi;
  67#ifdef CONFIG_DM_SPI_FLASH
  68        struct udevice *dev;
  69#endif
  70        const char *name;
  71        u8 dual_flash;
  72        u8 shift;
  73        u16 flags;
  74
  75        u32 size;
  76        u32 page_size;
  77        u32 sector_size;
  78        u32 erase_size;
  79#ifdef CONFIG_SPI_FLASH_BAR
  80        u8 bank_read_cmd;
  81        u8 bank_write_cmd;
  82        u8 bank_curr;
  83        u8 upage_prev;
  84#endif
  85        u8 erase_cmd;
  86        u8 read_cmd;
  87        u8 write_cmd;
  88        u8 dummy_byte;
  89
  90        void *memory_map;
  91
  92        int (*flash_lock)(struct spi_flash *flash, u32 ofs, size_t len);
  93        int (*flash_unlock)(struct spi_flash *flash, u32 ofs, size_t len);
  94        int (*flash_is_locked)(struct spi_flash *flash, u32 ofs, size_t len);
  95#ifndef CONFIG_DM_SPI_FLASH
  96        /*
  97         * These are not strictly needed for driver model, but keep them here
  98         * while the transition is in progress.
  99         *
 100         * Normally each driver would provide its own operations, but for
 101         * SPI flash most chips use the same algorithms. One approach is
 102         * to create a 'common' SPI flash device which knows how to talk
 103         * to most devices, and then allow other drivers to be used instead
 104         * if required, perhaps with a way of scanning through the list to
 105         * find the driver that matches the device.
 106         */
 107        int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
 108        int (*write)(struct spi_flash *flash, u32 offset, size_t len,
 109                        const void *buf);
 110        int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
 111#endif
 112};
 113
 114struct dm_spi_flash_ops {
 115        int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
 116        int (*write)(struct udevice *dev, u32 offset, size_t len,
 117                     const void *buf);
 118        int (*erase)(struct udevice *dev, u32 offset, size_t len);
 119};
 120
 121/* Access the serial operations for a device */
 122#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
 123
 124#ifdef CONFIG_DM_SPI_FLASH
 125/**
 126 * spi_flash_read_dm() - Read data from SPI flash
 127 *
 128 * @dev:        SPI flash device
 129 * @offset:     Offset into device in bytes to read from
 130 * @len:        Number of bytes to read
 131 * @buf:        Buffer to put the data that is read
 132 * @return 0 if OK, -ve on error
 133 */
 134int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
 135
 136/**
 137 * spi_flash_write_dm() - Write data to SPI flash
 138 *
 139 * @dev:        SPI flash device
 140 * @offset:     Offset into device in bytes to write to
 141 * @len:        Number of bytes to write
 142 * @buf:        Buffer containing bytes to write
 143 * @return 0 if OK, -ve on error
 144 */
 145int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
 146                       const void *buf);
 147
 148/**
 149 * spi_flash_erase_dm() - Erase blocks of the SPI flash
 150 *
 151 * Note that @len must be a muiltiple of the flash sector size.
 152 *
 153 * @dev:        SPI flash device
 154 * @offset:     Offset into device in bytes to start erasing
 155 * @len:        Number of bytes to erase
 156 * @return 0 if OK, -ve on error
 157 */
 158int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
 159
 160int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
 161                           unsigned int max_hz, unsigned int spi_mode,
 162                           struct udevice **devp);
 163
 164/* Compatibility function - this is the old U-Boot API */
 165struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
 166                                  unsigned int max_hz, unsigned int spi_mode);
 167
 168/* Compatibility function - this is the old U-Boot API */
 169void spi_flash_free(struct spi_flash *flash);
 170
 171static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
 172                                 size_t len, void *buf)
 173{
 174        return spi_flash_read_dm(flash->dev, offset, len, buf);
 175}
 176
 177static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
 178                                  size_t len, const void *buf)
 179{
 180        return spi_flash_write_dm(flash->dev, offset, len, buf);
 181}
 182
 183static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
 184                                  size_t len)
 185{
 186        return spi_flash_erase_dm(flash->dev, offset, len);
 187}
 188
 189struct sandbox_state;
 190
 191int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
 192                         struct udevice *bus, int of_offset, const char *spec);
 193
 194void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
 195
 196#else
 197struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
 198                unsigned int max_hz, unsigned int spi_mode);
 199
 200/**
 201 * Set up a new SPI flash from an fdt node
 202 *
 203 * @param blob          Device tree blob
 204 * @param slave_node    Pointer to this SPI slave node in the device tree
 205 * @param spi_node      Cached pointer to the SPI interface this node belongs
 206 *                      to
 207 * @return 0 if ok, -1 on error
 208 */
 209struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
 210                                      int spi_node);
 211
 212void spi_flash_free(struct spi_flash *flash);
 213
 214static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
 215                size_t len, void *buf)
 216{
 217        return flash->read(flash, offset, len, buf);
 218}
 219
 220static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
 221                size_t len, const void *buf)
 222{
 223        return flash->write(flash, offset, len, buf);
 224}
 225
 226static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
 227                size_t len)
 228{
 229        return flash->erase(flash, offset, len);
 230}
 231#endif
 232
 233static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
 234                                        bool prot)
 235{
 236        if (!flash->flash_lock || !flash->flash_unlock)
 237                return -EOPNOTSUPP;
 238
 239        if (prot)
 240                return flash->flash_lock(flash, ofs, len);
 241        else
 242                return flash->flash_unlock(flash, ofs, len);
 243}
 244
 245#endif /* _SPI_FLASH_H_ */
 246