uboot/include/spi.h
<<
>>
Prefs
   1/*
   2 * Common SPI Interface: Controller-specific definitions
   3 *
   4 * (C) Copyright 2001
   5 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
   6 *
   7 * SPDX-License-Identifier:     GPL-2.0+
   8 */
   9
  10#ifndef _SPI_H_
  11#define _SPI_H_
  12
  13/* SPI mode flags */
  14#define SPI_CPHA        BIT(0)                  /* clock phase */
  15#define SPI_CPOL        BIT(1)                  /* clock polarity */
  16#define SPI_MODE_0      (0|0)                   /* (original MicroWire) */
  17#define SPI_MODE_1      (0|SPI_CPHA)
  18#define SPI_MODE_2      (SPI_CPOL|0)
  19#define SPI_MODE_3      (SPI_CPOL|SPI_CPHA)
  20#define SPI_CS_HIGH     BIT(2)                  /* CS active high */
  21#define SPI_LSB_FIRST   BIT(3)                  /* per-word bits-on-wire */
  22#define SPI_3WIRE       BIT(4)                  /* SI/SO signals shared */
  23#define SPI_LOOP        BIT(5)                  /* loopback mode */
  24#define SPI_SLAVE       BIT(6)                  /* slave mode */
  25#define SPI_PREAMBLE    BIT(7)                  /* Skip preamble bytes */
  26#define SPI_TX_BYTE     BIT(8)                  /* transmit with 1 wire byte */
  27#define SPI_TX_DUAL     BIT(9)                  /* transmit with 2 wires */
  28#define SPI_TX_QUAD     BIT(10)                 /* transmit with 4 wires */
  29
  30#define SPI_3BYTE_MODE  0x0
  31#define SPI_4BYTE_MODE  0x1
  32
  33/* SPI transfer flags */
  34#define SPI_XFER_STRIPE (1 << 6)
  35#define SPI_XFER_MASK   (3 << 8)
  36#define SPI_XFER_LOWER  (1 << 8)
  37#define SPI_XFER_UPPER  (2 << 8)
  38
  39/* SPI TX operation modes */
  40#define SPI_OPM_TX_QPP          (1 << 0)
  41#define SPI_OPM_TX_BP           (1 << 1)
  42
  43/* SPI RX operation modes */
  44#define SPI_OPM_RX_AS           (1 << 0)
  45#define SPI_OPM_RX_AF           (1 << 1)
  46#define SPI_OPM_RX_DOUT         (1 << 2)
  47#define SPI_OPM_RX_DIO          (1 << 3)
  48#define SPI_OPM_RX_QOF          (1 << 4)
  49#define SPI_OPM_RX_QIOF         (1 << 5)
  50#define SPI_OPM_RX_EXTN (SPI_OPM_RX_AS | SPI_OPM_RX_AF | SPI_OPM_RX_DOUT | \
  51                                SPI_OPM_RX_DIO | SPI_OPM_RX_QOF | \
  52                                SPI_OPM_RX_QIOF)
  53
  54/* SPI mode_rx flags */
  55#define SPI_RX_SLOW     BIT(0)                  /* receive with 1 wire slow */
  56#define SPI_RX_FAST     BIT(1)                  /* receive with 1 wire fast */
  57#define SPI_RX_DUAL     BIT(2)                  /* receive with 2 wires */
  58#define SPI_RX_QUAD     BIT(3)                  /* receive with 4 wires */
  59
  60/* Header byte that marks the start of the message */
  61#define SPI_PREAMBLE_END_BYTE   0xec
  62
  63#define SPI_DEFAULT_WORDLEN     8
  64
  65#ifdef CONFIG_DM_SPI
  66/* TODO(sjg@chromium.org): Remove this and use max_hz from struct spi_slave */
  67struct dm_spi_bus {
  68        uint max_hz;
  69};
  70
  71/**
  72 * struct dm_spi_platdata - platform data for all SPI slaves
  73 *
  74 * This describes a SPI slave, a child device of the SPI bus. To obtain this
  75 * struct from a spi_slave, use dev_get_parent_platdata(dev) or
  76 * dev_get_parent_platdata(slave->dev).
  77 *
  78 * This data is immuatable. Each time the device is probed, @max_hz and @mode
  79 * will be copied to struct spi_slave.
  80 *
  81 * @cs:         Chip select number (0..n-1)
  82 * @max_hz:     Maximum bus speed that this slave can tolerate
  83 * @mode:       SPI mode to use for this device (see SPI mode flags)
  84 */
  85struct dm_spi_slave_platdata {
  86        unsigned int cs;
  87        uint max_hz;
  88        uint mode;
  89};
  90
  91#endif /* CONFIG_DM_SPI */
  92
  93/**
  94 * struct spi_slave - Representation of a SPI slave
  95 *
  96 * For driver model this is the per-child data used by the SPI bus. It can
  97 * be accessed using dev_get_parent_priv() on the slave device. The SPI uclass
  98 * sets uip per_child_auto_alloc_size to sizeof(struct spi_slave), and the
  99 * driver should not override it. Two platform data fields (max_hz and mode)
 100 * are copied into this structure to provide an initial value. This allows
 101 * them to be changed, since we should never change platform data in drivers.
 102 *
 103 * If not using driver model, drivers are expected to extend this with
 104 * controller-specific data.
 105 *
 106 * @dev:                SPI slave device
 107 * @max_hz:             Maximum speed for this slave
 108 * @speed:              Current bus speed. This is 0 until the bus is first
 109 *                      claimed.
 110 * @bus:                ID of the bus that the slave is attached to. For
 111 *                      driver model this is the sequence number of the SPI
 112 *                      bus (bus->seq) so does not need to be stored
 113 * @cs:                 ID of the chip select connected to the slave.
 114 * @mode:               SPI mode to use for this slave (see SPI mode flags)
 115 * @wordlen:            Size of SPI word in number of bits
 116 * @max_write_size:     If non-zero, the maximum number of bytes which can
 117 *                      be written at once, excluding command bytes.
 118 * @memory_map:         Address of read-only SPI flash access.
 119 * @flags:              Indication of SPI flags.
 120 */
 121struct spi_slave {
 122#ifdef CONFIG_DM_SPI
 123        struct udevice *dev;    /* struct spi_slave is dev->parentdata */
 124        uint max_hz;
 125        uint speed;
 126#else
 127        unsigned int bus;
 128        unsigned int cs;
 129#endif
 130        uint mode;
 131        unsigned int wordlen;
 132        unsigned int max_write_size;
 133        void *memory_map;
 134        u8 option;
 135        u8 dio;
 136        u32 bytemode;
 137        u32 flags;
 138        u8 dummy_bytes;
 139#define SPI_XFER_BEGIN          BIT(0)  /* Assert CS before transfer */
 140#define SPI_XFER_END            BIT(1)  /* Deassert CS after transfer */
 141#define SPI_XFER_ONCE           (SPI_XFER_BEGIN | SPI_XFER_END)
 142#define SPI_XFER_MMAP           BIT(2)  /* Memory Mapped start */
 143#define SPI_XFER_MMAP_END       BIT(3)  /* Memory Mapped End */
 144#define SPI_XFER_U_PAGE         BIT(4)
 145};
 146
 147/**
 148 * Initialization, must be called once on start up.
 149 *
 150 * TODO: I don't think we really need this.
 151 */
 152void spi_init(void);
 153
 154/**
 155 * spi_do_alloc_slave - Allocate a new SPI slave (internal)
 156 *
 157 * Allocate and zero all fields in the spi slave, and set the bus/chip
 158 * select. Use the helper macro spi_alloc_slave() to call this.
 159 *
 160 * @offset:     Offset of struct spi_slave within slave structure.
 161 * @size:       Size of slave structure.
 162 * @bus:        Bus ID of the slave chip.
 163 * @cs:         Chip select ID of the slave chip on the specified bus.
 164 */
 165void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
 166                         unsigned int cs);
 167
 168/**
 169 * spi_alloc_slave - Allocate a new SPI slave
 170 *
 171 * Allocate and zero all fields in the spi slave, and set the bus/chip
 172 * select.
 173 *
 174 * @_struct:    Name of structure to allocate (e.g. struct tegra_spi).
 175 *              This structure must contain a member 'struct spi_slave *slave'.
 176 * @bus:        Bus ID of the slave chip.
 177 * @cs:         Chip select ID of the slave chip on the specified bus.
 178 */
 179#define spi_alloc_slave(_struct, bus, cs) \
 180        spi_do_alloc_slave(offsetof(_struct, slave), \
 181                            sizeof(_struct), bus, cs)
 182
 183/**
 184 * spi_alloc_slave_base - Allocate a new SPI slave with no private data
 185 *
 186 * Allocate and zero all fields in the spi slave, and set the bus/chip
 187 * select.
 188 *
 189 * @bus:        Bus ID of the slave chip.
 190 * @cs:         Chip select ID of the slave chip on the specified bus.
 191 */
 192#define spi_alloc_slave_base(bus, cs) \
 193        spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
 194
 195/**
 196 * Set up communications parameters for a SPI slave.
 197 *
 198 * This must be called once for each slave. Note that this function
 199 * usually doesn't touch any actual hardware, it only initializes the
 200 * contents of spi_slave so that the hardware can be easily
 201 * initialized later.
 202 *
 203 * @bus:        Bus ID of the slave chip.
 204 * @cs:         Chip select ID of the slave chip on the specified bus.
 205 * @max_hz:     Maximum SCK rate in Hz.
 206 * @mode:       Clock polarity, clock phase and other parameters.
 207 *
 208 * Returns: A spi_slave reference that can be used in subsequent SPI
 209 * calls, or NULL if one or more of the parameters are not supported.
 210 */
 211struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
 212                unsigned int max_hz, unsigned int mode);
 213
 214/**
 215 * Free any memory associated with a SPI slave.
 216 *
 217 * @slave:      The SPI slave
 218 */
 219void spi_free_slave(struct spi_slave *slave);
 220
 221/**
 222 * Claim the bus and prepare it for communication with a given slave.
 223 *
 224 * This must be called before doing any transfers with a SPI slave. It
 225 * will enable and initialize any SPI hardware as necessary, and make
 226 * sure that the SCK line is in the correct idle state. It is not
 227 * allowed to claim the same bus for several slaves without releasing
 228 * the bus in between.
 229 *
 230 * @slave:      The SPI slave
 231 *
 232 * Returns: 0 if the bus was claimed successfully, or a negative value
 233 * if it wasn't.
 234 */
 235int spi_claim_bus(struct spi_slave *slave);
 236
 237/**
 238 * Release the SPI bus
 239 *
 240 * This must be called once for every call to spi_claim_bus() after
 241 * all transfers have finished. It may disable any SPI hardware as
 242 * appropriate.
 243 *
 244 * @slave:      The SPI slave
 245 */
 246void spi_release_bus(struct spi_slave *slave);
 247
 248/**
 249 * Set the word length for SPI transactions
 250 *
 251 * Set the word length (number of bits per word) for SPI transactions.
 252 *
 253 * @slave:      The SPI slave
 254 * @wordlen:    The number of bits in a word
 255 *
 256 * Returns: 0 on success, -1 on failure.
 257 */
 258int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
 259
 260/**
 261 * SPI transfer
 262 *
 263 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
 264 * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
 265 *
 266 * The source of the outgoing bits is the "dout" parameter and the
 267 * destination of the input bits is the "din" parameter.  Note that "dout"
 268 * and "din" can point to the same memory location, in which case the
 269 * input data overwrites the output data (since both are buffered by
 270 * temporary variables, this is OK).
 271 *
 272 * spi_xfer() interface:
 273 * @slave:      The SPI slave which will be sending/receiving the data.
 274 * @bitlen:     How many bits to write and read.
 275 * @dout:       Pointer to a string of bits to send out.  The bits are
 276 *              held in a byte array and are sent MSB first.
 277 * @din:        Pointer to a string of bits that will be filled in.
 278 * @flags:      A bitwise combination of SPI_XFER_* flags.
 279 *
 280 * Returns: 0 on success, not 0 on failure
 281 */
 282int  spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 283                void *din, unsigned long flags);
 284
 285/* Copy memory mapped data */
 286void spi_flash_copy_mmap(void *data, void *offset, size_t len);
 287
 288/**
 289 * Determine if a SPI chipselect is valid.
 290 * This function is provided by the board if the low-level SPI driver
 291 * needs it to determine if a given chipselect is actually valid.
 292 *
 293 * Returns: 1 if bus:cs identifies a valid chip on this board, 0
 294 * otherwise.
 295 */
 296int spi_cs_is_valid(unsigned int bus, unsigned int cs);
 297
 298#ifndef CONFIG_DM_SPI
 299/**
 300 * Activate a SPI chipselect.
 301 * This function is provided by the board code when using a driver
 302 * that can't control its chipselects automatically (e.g.
 303 * common/soft_spi.c). When called, it should activate the chip select
 304 * to the device identified by "slave".
 305 */
 306void spi_cs_activate(struct spi_slave *slave);
 307
 308/**
 309 * Deactivate a SPI chipselect.
 310 * This function is provided by the board code when using a driver
 311 * that can't control its chipselects automatically (e.g.
 312 * common/soft_spi.c). When called, it should deactivate the chip
 313 * select to the device identified by "slave".
 314 */
 315void spi_cs_deactivate(struct spi_slave *slave);
 316
 317/**
 318 * Set transfer speed.
 319 * This sets a new speed to be applied for next spi_xfer().
 320 * @slave:      The SPI slave
 321 * @hz:         The transfer speed
 322 */
 323void spi_set_speed(struct spi_slave *slave, uint hz);
 324#endif
 325
 326/**
 327 * Write 8 bits, then read 8 bits.
 328 * @slave:      The SPI slave we're communicating with
 329 * @byte:       Byte to be written
 330 *
 331 * Returns: The value that was read, or a negative value on error.
 332 *
 333 * TODO: This function probably shouldn't be inlined.
 334 */
 335static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
 336{
 337        unsigned char dout[2];
 338        unsigned char din[2];
 339        int ret;
 340
 341        dout[0] = byte;
 342        dout[1] = 0;
 343
 344        ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
 345        return ret < 0 ? ret : din[1];
 346}
 347
 348/**
 349 * Set up a SPI slave for a particular device tree node
 350 *
 351 * This calls spi_setup_slave() with the correct bus number. Call
 352 * spi_free_slave() to free it later.
 353 *
 354 * @param blob:         Device tree blob
 355 * @param slave_node:   Slave node to use
 356 * @param spi_node:     SPI peripheral node to use
 357 * @return pointer to new spi_slave structure
 358 */
 359struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
 360                                      int spi_node);
 361
 362/**
 363 * spi_base_setup_slave_fdt() - helper function to set up a SPI slace
 364 *
 365 * This decodes SPI properties from the slave node to determine the
 366 * chip select and SPI parameters.
 367 *
 368 * @blob:       Device tree blob
 369 * @busnum:     Bus number to use
 370 * @node:       Device tree node for the SPI bus
 371 */
 372struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum,
 373                                           int node);
 374
 375#ifdef CONFIG_DM_SPI
 376
 377/**
 378 * struct spi_cs_info - Information about a bus chip select
 379 *
 380 * @dev:        Connected device, or NULL if none
 381 */
 382struct spi_cs_info {
 383        struct udevice *dev;
 384};
 385
 386/**
 387 * struct struct dm_spi_ops - Driver model SPI operations
 388 *
 389 * The uclass interface is implemented by all SPI devices which use
 390 * driver model.
 391 */
 392struct dm_spi_ops {
 393        /**
 394         * Claim the bus and prepare it for communication.
 395         *
 396         * The device provided is the slave device. It's parent controller
 397         * will be used to provide the communication.
 398         *
 399         * This must be called before doing any transfers with a SPI slave. It
 400         * will enable and initialize any SPI hardware as necessary, and make
 401         * sure that the SCK line is in the correct idle state. It is not
 402         * allowed to claim the same bus for several slaves without releasing
 403         * the bus in between.
 404         *
 405         * @dev:        The SPI slave
 406         *
 407         * Returns: 0 if the bus was claimed successfully, or a negative value
 408         * if it wasn't.
 409         */
 410        int (*claim_bus)(struct udevice *dev);
 411
 412        /**
 413         * Release the SPI bus
 414         *
 415         * This must be called once for every call to spi_claim_bus() after
 416         * all transfers have finished. It may disable any SPI hardware as
 417         * appropriate.
 418         *
 419         * @dev:        The SPI slave
 420         */
 421        int (*release_bus)(struct udevice *dev);
 422
 423        /**
 424         * Set the word length for SPI transactions
 425         *
 426         * Set the word length (number of bits per word) for SPI transactions.
 427         *
 428         * @bus:        The SPI slave
 429         * @wordlen:    The number of bits in a word
 430         *
 431         * Returns: 0 on success, -ve on failure.
 432         */
 433        int (*set_wordlen)(struct udevice *dev, unsigned int wordlen);
 434
 435        /**
 436         * SPI transfer
 437         *
 438         * This writes "bitlen" bits out the SPI MOSI port and simultaneously
 439         * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
 440         * works.
 441         *
 442         * The source of the outgoing bits is the "dout" parameter and the
 443         * destination of the input bits is the "din" parameter.  Note that
 444         * "dout" and "din" can point to the same memory location, in which
 445         * case the input data overwrites the output data (since both are
 446         * buffered by temporary variables, this is OK).
 447         *
 448         * spi_xfer() interface:
 449         * @dev:        The slave device to communicate with
 450         * @bitlen:     How many bits to write and read.
 451         * @dout:       Pointer to a string of bits to send out.  The bits are
 452         *              held in a byte array and are sent MSB first.
 453         * @din:        Pointer to a string of bits that will be filled in.
 454         * @flags:      A bitwise combination of SPI_XFER_* flags.
 455         *
 456         * Returns: 0 on success, not -1 on failure
 457         */
 458        int (*xfer)(struct udevice *dev, unsigned int bitlen, const void *dout,
 459                    void *din, unsigned long flags);
 460
 461        /**
 462         * Set transfer speed.
 463         * This sets a new speed to be applied for next spi_xfer().
 464         * @bus:        The SPI bus
 465         * @hz:         The transfer speed
 466         * @return 0 if OK, -ve on error
 467         */
 468        int (*set_speed)(struct udevice *bus, uint hz);
 469
 470        /**
 471         * Set the SPI mode/flags
 472         *
 473         * It is unclear if we want to set speed and mode together instead
 474         * of separately.
 475         *
 476         * @bus:        The SPI bus
 477         * @mode:       Requested SPI mode (SPI_... flags)
 478         * @return 0 if OK, -ve on error
 479         */
 480        int (*set_mode)(struct udevice *bus, uint mode);
 481
 482        /**
 483         * Get information on a chip select
 484         *
 485         * This is only called when the SPI uclass does not know about a
 486         * chip select, i.e. it has no attached device. It gives the driver
 487         * a chance to allow activity on that chip select even so.
 488         *
 489         * @bus:        The SPI bus
 490         * @cs:         The chip select (0..n-1)
 491         * @info:       Returns information about the chip select, if valid.
 492         *              On entry info->dev is NULL
 493         * @return 0 if OK (and @info is set up), -ENODEV if the chip select
 494         *         is invalid, other -ve value on error
 495         */
 496        int (*cs_info)(struct udevice *bus, uint cs, struct spi_cs_info *info);
 497};
 498
 499struct dm_spi_emul_ops {
 500        /**
 501         * SPI transfer
 502         *
 503         * This writes "bitlen" bits out the SPI MOSI port and simultaneously
 504         * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
 505         * works. Here the device is a slave.
 506         *
 507         * The source of the outgoing bits is the "dout" parameter and the
 508         * destination of the input bits is the "din" parameter.  Note that
 509         * "dout" and "din" can point to the same memory location, in which
 510         * case the input data overwrites the output data (since both are
 511         * buffered by temporary variables, this is OK).
 512         *
 513         * spi_xfer() interface:
 514         * @slave:      The SPI slave which will be sending/receiving the data.
 515         * @bitlen:     How many bits to write and read.
 516         * @dout:       Pointer to a string of bits sent to the device. The
 517         *              bits are held in a byte array and are sent MSB first.
 518         * @din:        Pointer to a string of bits that will be sent back to
 519         *              the master.
 520         * @flags:      A bitwise combination of SPI_XFER_* flags.
 521         *
 522         * Returns: 0 on success, not -1 on failure
 523         */
 524        int (*xfer)(struct udevice *slave, unsigned int bitlen,
 525                    const void *dout, void *din, unsigned long flags);
 526};
 527
 528/**
 529 * spi_find_bus_and_cs() - Find bus and slave devices by number
 530 *
 531 * Given a bus number and chip select, this finds the corresponding bus
 532 * device and slave device. Neither device is activated by this function,
 533 * although they may have been activated previously.
 534 *
 535 * @busnum:     SPI bus number
 536 * @cs:         Chip select to look for
 537 * @busp:       Returns bus device
 538 * @devp:       Return slave device
 539 * @return 0 if found, -ENODEV on error
 540 */
 541int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
 542                        struct udevice **devp);
 543
 544/**
 545 * spi_get_bus_and_cs() - Find and activate bus and slave devices by number
 546 *
 547 * Given a bus number and chip select, this finds the corresponding bus
 548 * device and slave device.
 549 *
 550 * If no such slave exists, and drv_name is not NULL, then a new slave device
 551 * is automatically bound on this chip select.
 552 *
 553 * Ths new slave device is probed ready for use with the given speed and mode.
 554 *
 555 * @busnum:     SPI bus number
 556 * @cs:         Chip select to look for
 557 * @speed:      SPI speed to use for this slave
 558 * @mode:       SPI mode to use for this slave
 559 * @drv_name:   Name of driver to attach to this chip select
 560 * @dev_name:   Name of the new device thus created
 561 * @busp:       Returns bus device
 562 * @devp:       Return slave device
 563 * @return 0 if found, -ve on error
 564 */
 565int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
 566                        const char *drv_name, const char *dev_name,
 567                        struct udevice **busp, struct spi_slave **devp);
 568
 569/**
 570 * spi_chip_select() - Get the chip select for a slave
 571 *
 572 * @return the chip select this slave is attached to
 573 */
 574int spi_chip_select(struct udevice *slave);
 575
 576/**
 577 * spi_find_chip_select() - Find the slave attached to chip select
 578 *
 579 * @bus:        SPI bus to search
 580 * @cs:         Chip select to look for
 581 * @devp:       Returns the slave device if found
 582 * @return 0 if found, -ENODEV on error
 583 */
 584int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp);
 585
 586/**
 587 * spi_slave_ofdata_to_platdata() - decode standard SPI platform data
 588 *
 589 * This decodes the speed and mode for a slave from a device tree node
 590 *
 591 * @blob:       Device tree blob
 592 * @node:       Node offset to read from
 593 * @plat:       Place to put the decoded information
 594 */
 595int spi_slave_ofdata_to_platdata(struct udevice *dev,
 596                                 struct dm_spi_slave_platdata *plat);
 597
 598/**
 599 * spi_cs_info() - Check information on a chip select
 600 *
 601 * This checks a particular chip select on a bus to see if it has a device
 602 * attached, or is even valid.
 603 *
 604 * @bus:        The SPI bus
 605 * @cs:         The chip select (0..n-1)
 606 * @info:       Returns information about the chip select, if valid
 607 * @return 0 if OK (and @info is set up), -ENODEV if the chip select
 608 *         is invalid, other -ve value on error
 609 */
 610int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info);
 611
 612struct sandbox_state;
 613
 614/**
 615 * sandbox_spi_get_emul() - get an emulator for a SPI slave
 616 *
 617 * This provides a way to attach an emulated SPI device to a particular SPI
 618 * slave, so that xfer() operations on the slave will be handled by the
 619 * emulator. If a emulator already exists on that chip select it is returned.
 620 * Otherwise one is created.
 621 *
 622 * @state:      Sandbox state
 623 * @bus:        SPI bus requesting the emulator
 624 * @slave:      SPI slave device requesting the emulator
 625 * @emuip:      Returns pointer to emulator
 626 * @return 0 if OK, -ve on error
 627 */
 628int sandbox_spi_get_emul(struct sandbox_state *state,
 629                         struct udevice *bus, struct udevice *slave,
 630                         struct udevice **emulp);
 631
 632/**
 633 * Claim the bus and prepare it for communication with a given slave.
 634 *
 635 * This must be called before doing any transfers with a SPI slave. It
 636 * will enable and initialize any SPI hardware as necessary, and make
 637 * sure that the SCK line is in the correct idle state. It is not
 638 * allowed to claim the same bus for several slaves without releasing
 639 * the bus in between.
 640 *
 641 * @dev:        The SPI slave device
 642 *
 643 * Returns: 0 if the bus was claimed successfully, or a negative value
 644 * if it wasn't.
 645 */
 646int dm_spi_claim_bus(struct udevice *dev);
 647
 648/**
 649 * Release the SPI bus
 650 *
 651 * This must be called once for every call to dm_spi_claim_bus() after
 652 * all transfers have finished. It may disable any SPI hardware as
 653 * appropriate.
 654 *
 655 * @slave:      The SPI slave device
 656 */
 657void dm_spi_release_bus(struct udevice *dev);
 658
 659/**
 660 * SPI transfer
 661 *
 662 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
 663 * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
 664 *
 665 * The source of the outgoing bits is the "dout" parameter and the
 666 * destination of the input bits is the "din" parameter.  Note that "dout"
 667 * and "din" can point to the same memory location, in which case the
 668 * input data overwrites the output data (since both are buffered by
 669 * temporary variables, this is OK).
 670 *
 671 * dm_spi_xfer() interface:
 672 * @dev:        The SPI slave device which will be sending/receiving the data.
 673 * @bitlen:     How many bits to write and read.
 674 * @dout:       Pointer to a string of bits to send out.  The bits are
 675 *              held in a byte array and are sent MSB first.
 676 * @din:        Pointer to a string of bits that will be filled in.
 677 * @flags:      A bitwise combination of SPI_XFER_* flags.
 678 *
 679 * Returns: 0 on success, not 0 on failure
 680 */
 681int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
 682                const void *dout, void *din, unsigned long flags);
 683
 684/* Access the operations for a SPI device */
 685#define spi_get_ops(dev)        ((struct dm_spi_ops *)(dev)->driver->ops)
 686#define spi_emul_get_ops(dev)   ((struct dm_spi_emul_ops *)(dev)->driver->ops)
 687#endif /* CONFIG_DM_SPI */
 688
 689#endif  /* _SPI_H_ */
 690