177f85581Swdenk /* 2469146c0SJagannadha Sutradharudu Teki * Common SPI Interface: Controller-specific definitions 3469146c0SJagannadha Sutradharudu Teki * 477f85581Swdenk * (C) Copyright 2001 577f85581Swdenk * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. 677f85581Swdenk * 71a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 877f85581Swdenk */ 977f85581Swdenk 1077f85581Swdenk #ifndef _SPI_H_ 1177f85581Swdenk #define _SPI_H_ 1277f85581Swdenk 1338254f45SGuennadi Liakhovetski /* SPI mode flags */ 1429ee0262SJagan Teki #define SPI_CPHA BIT(0) /* clock phase */ 1529ee0262SJagan Teki #define SPI_CPOL BIT(1) /* clock polarity */ 1638254f45SGuennadi Liakhovetski #define SPI_MODE_0 (0|0) /* (original MicroWire) */ 1738254f45SGuennadi Liakhovetski #define SPI_MODE_1 (0|SPI_CPHA) 1838254f45SGuennadi Liakhovetski #define SPI_MODE_2 (SPI_CPOL|0) 1938254f45SGuennadi Liakhovetski #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) 2029ee0262SJagan Teki #define SPI_CS_HIGH BIT(2) /* CS active high */ 2129ee0262SJagan Teki #define SPI_LSB_FIRST BIT(3) /* per-word bits-on-wire */ 2229ee0262SJagan Teki #define SPI_3WIRE BIT(4) /* SI/SO signals shared */ 2329ee0262SJagan Teki #define SPI_LOOP BIT(5) /* loopback mode */ 2429ee0262SJagan Teki #define SPI_SLAVE BIT(6) /* slave mode */ 2529ee0262SJagan Teki #define SPI_PREAMBLE BIT(7) /* Skip preamble bytes */ 2629ee0262SJagan Teki #define SPI_TX_BYTE BIT(8) /* transmit with 1 wire byte */ 272b11a41cSJagan Teki #define SPI_TX_DUAL BIT(9) /* transmit with 2 wires */ 282b11a41cSJagan Teki #define SPI_TX_QUAD BIT(10) /* transmit with 4 wires */ 2938254f45SGuennadi Liakhovetski 3091292e0bSJagan Teki /* SPI mode_rx flags */ 31465c00d7SJagan Teki #define SPI_RX_SLOW BIT(0) /* receive with 1 wire slow */ 32465c00d7SJagan Teki #define SPI_RX_FAST BIT(1) /* receive with 1 wire fast */ 33465c00d7SJagan Teki #define SPI_RX_DUAL BIT(2) /* receive with 2 wires */ 341c17f5ecSJagan Teki #define SPI_RX_QUAD BIT(3) /* receive with 4 wires */ 354e09cc1eSJagannadha Sutradharudu Teki 36248a0488SSimon Glass /* SPI bus connection options - see enum spi_dual_flash */ 37248a0488SSimon Glass #define SPI_CONN_DUAL_SHARED (1 << 0) 38248a0488SSimon Glass #define SPI_CONN_DUAL_SEPARATED (1 << 1) 39f77f4691SJagannadha Sutradharudu Teki 40bb786b84SRajeshwari Shinde /* Header byte that marks the start of the message */ 41bb786b84SRajeshwari Shinde #define SPI_PREAMBLE_END_BYTE 0xec 42bb786b84SRajeshwari Shinde 435753d09bSNikita Kiryanov #define SPI_DEFAULT_WORDLEN 8 445753d09bSNikita Kiryanov 45d7af6a48SSimon Glass #ifdef CONFIG_DM_SPI 46d0cff03eSSimon Glass /* TODO(sjg@chromium.org): Remove this and use max_hz from struct spi_slave */ 47d7af6a48SSimon Glass struct dm_spi_bus { 48d7af6a48SSimon Glass uint max_hz; 49d7af6a48SSimon Glass }; 50d7af6a48SSimon Glass 51d0cff03eSSimon Glass /** 52d0cff03eSSimon Glass * struct dm_spi_platdata - platform data for all SPI slaves 53d0cff03eSSimon Glass * 54d0cff03eSSimon Glass * This describes a SPI slave, a child device of the SPI bus. To obtain this 55d0cff03eSSimon Glass * struct from a spi_slave, use dev_get_parent_platdata(dev) or 56d0cff03eSSimon Glass * dev_get_parent_platdata(slave->dev). 57d0cff03eSSimon Glass * 58d0cff03eSSimon Glass * This data is immuatable. Each time the device is probed, @max_hz and @mode 59d0cff03eSSimon Glass * will be copied to struct spi_slave. 60d0cff03eSSimon Glass * 61d0cff03eSSimon Glass * @cs: Chip select number (0..n-1) 62d0cff03eSSimon Glass * @max_hz: Maximum bus speed that this slave can tolerate 63d0cff03eSSimon Glass * @mode: SPI mode to use for this device (see SPI mode flags) 64f8e2f92dSMugunthan V N * @mode_rx: SPI RX mode to use for this slave (see SPI mode_rx flags) 65d0cff03eSSimon Glass */ 66d0cff03eSSimon Glass struct dm_spi_slave_platdata { 67d0cff03eSSimon Glass unsigned int cs; 68d0cff03eSSimon Glass uint max_hz; 69d0cff03eSSimon Glass uint mode; 70f8e2f92dSMugunthan V N u8 mode_rx; 71d0cff03eSSimon Glass }; 72d0cff03eSSimon Glass 73d7af6a48SSimon Glass #endif /* CONFIG_DM_SPI */ 74d7af6a48SSimon Glass 751b1bd9a7SJagannadha Sutradharudu Teki /** 76ce22b922SJagannadha Sutradharudu Teki * struct spi_slave - Representation of a SPI slave 77d255bb0eSHaavard Skinnemoen * 78d7af6a48SSimon Glass * For driver model this is the per-child data used by the SPI bus. It can 79bcbe3d15SSimon Glass * be accessed using dev_get_parent_priv() on the slave device. The SPI uclass 80d0cff03eSSimon Glass * sets uip per_child_auto_alloc_size to sizeof(struct spi_slave), and the 81d0cff03eSSimon Glass * driver should not override it. Two platform data fields (max_hz and mode) 82d0cff03eSSimon Glass * are copied into this structure to provide an initial value. This allows 83d0cff03eSSimon Glass * them to be changed, since we should never change platform data in drivers. 84d7af6a48SSimon Glass * 85d7af6a48SSimon Glass * If not using driver model, drivers are expected to extend this with 86d7af6a48SSimon Glass * controller-specific data. 87d7af6a48SSimon Glass * 88d7af6a48SSimon Glass * @dev: SPI slave device 89d7af6a48SSimon Glass * @max_hz: Maximum speed for this slave 9060e2809aSSimon Glass * @speed: Current bus speed. This is 0 until the bus is first 9160e2809aSSimon Glass * claimed. 92d7af6a48SSimon Glass * @bus: ID of the bus that the slave is attached to. For 93d7af6a48SSimon Glass * driver model this is the sequence number of the SPI 94d7af6a48SSimon Glass * bus (bus->seq) so does not need to be stored 95ce22b922SJagannadha Sutradharudu Teki * @cs: ID of the chip select connected to the slave. 96f5c3c033SJagan Teki * @mode: SPI mode to use for this slave (see SPI mode flags) 9791292e0bSJagan Teki * @mode_rx: SPI RX mode to use for this slave (see SPI mode_rx flags) 985753d09bSNikita Kiryanov * @wordlen: Size of SPI word in number of bits 99ce22b922SJagannadha Sutradharudu Teki * @max_write_size: If non-zero, the maximum number of bytes which can 1000c456ceeSSimon Glass * be written at once, excluding command bytes. 101ce22b922SJagannadha Sutradharudu Teki * @memory_map: Address of read-only SPI flash access. 102056fbc73SJagannadha Sutradharudu Teki * @option: Varies SPI bus options - separate, shared bus. 103f77f4691SJagannadha Sutradharudu Teki * @flags: Indication of SPI flags. 104d255bb0eSHaavard Skinnemoen */ 105d255bb0eSHaavard Skinnemoen struct spi_slave { 106d7af6a48SSimon Glass #ifdef CONFIG_DM_SPI 107d7af6a48SSimon Glass struct udevice *dev; /* struct spi_slave is dev->parentdata */ 108d7af6a48SSimon Glass uint max_hz; 10960e2809aSSimon Glass uint speed; 110d7af6a48SSimon Glass #else 111d255bb0eSHaavard Skinnemoen unsigned int bus; 112d255bb0eSHaavard Skinnemoen unsigned int cs; 113d0cff03eSSimon Glass #endif 114f5c3c033SJagan Teki uint mode; 11591292e0bSJagan Teki u8 mode_rx; 1165753d09bSNikita Kiryanov unsigned int wordlen; 1170c456ceeSSimon Glass unsigned int max_write_size; 118004f15b6SPoddar, Sourav void *memory_map; 119f77f4691SJagannadha Sutradharudu Teki u8 option; 120c40f6003SJagan Teki 121f77f4691SJagannadha Sutradharudu Teki u8 flags; 12229ee0262SJagan Teki #define SPI_XFER_BEGIN BIT(0) /* Assert CS before transfer */ 12329ee0262SJagan Teki #define SPI_XFER_END BIT(1) /* Deassert CS after transfer */ 124c40f6003SJagan Teki #define SPI_XFER_ONCE (SPI_XFER_BEGIN | SPI_XFER_END) 12529ee0262SJagan Teki #define SPI_XFER_MMAP BIT(2) /* Memory Mapped start */ 12629ee0262SJagan Teki #define SPI_XFER_MMAP_END BIT(3) /* Memory Mapped End */ 12729ee0262SJagan Teki #define SPI_XFER_U_PAGE BIT(4) 128d255bb0eSHaavard Skinnemoen }; 12977f85581Swdenk 1301b1bd9a7SJagannadha Sutradharudu Teki /** 13177f85581Swdenk * Initialization, must be called once on start up. 132d255bb0eSHaavard Skinnemoen * 133d255bb0eSHaavard Skinnemoen * TODO: I don't think we really need this. 13477f85581Swdenk */ 13577f85581Swdenk void spi_init(void); 13677f85581Swdenk 137ba6c3ce9SSimon Glass /** 138ba6c3ce9SSimon Glass * spi_do_alloc_slave - Allocate a new SPI slave (internal) 139ba6c3ce9SSimon Glass * 140ba6c3ce9SSimon Glass * Allocate and zero all fields in the spi slave, and set the bus/chip 141ba6c3ce9SSimon Glass * select. Use the helper macro spi_alloc_slave() to call this. 142ba6c3ce9SSimon Glass * 1431b1bd9a7SJagannadha Sutradharudu Teki * @offset: Offset of struct spi_slave within slave structure. 1441b1bd9a7SJagannadha Sutradharudu Teki * @size: Size of slave structure. 145ba6c3ce9SSimon Glass * @bus: Bus ID of the slave chip. 146ba6c3ce9SSimon Glass * @cs: Chip select ID of the slave chip on the specified bus. 147ba6c3ce9SSimon Glass */ 148ba6c3ce9SSimon Glass void *spi_do_alloc_slave(int offset, int size, unsigned int bus, 149ba6c3ce9SSimon Glass unsigned int cs); 150ba6c3ce9SSimon Glass 151ba6c3ce9SSimon Glass /** 152ba6c3ce9SSimon Glass * spi_alloc_slave - Allocate a new SPI slave 153ba6c3ce9SSimon Glass * 154ba6c3ce9SSimon Glass * Allocate and zero all fields in the spi slave, and set the bus/chip 155ba6c3ce9SSimon Glass * select. 156ba6c3ce9SSimon Glass * 1571b1bd9a7SJagannadha Sutradharudu Teki * @_struct: Name of structure to allocate (e.g. struct tegra_spi). 1581b1bd9a7SJagannadha Sutradharudu Teki * This structure must contain a member 'struct spi_slave *slave'. 159ba6c3ce9SSimon Glass * @bus: Bus ID of the slave chip. 160ba6c3ce9SSimon Glass * @cs: Chip select ID of the slave chip on the specified bus. 161ba6c3ce9SSimon Glass */ 162ba6c3ce9SSimon Glass #define spi_alloc_slave(_struct, bus, cs) \ 163ba6c3ce9SSimon Glass spi_do_alloc_slave(offsetof(_struct, slave), \ 164ba6c3ce9SSimon Glass sizeof(_struct), bus, cs) 165ba6c3ce9SSimon Glass 166ba6c3ce9SSimon Glass /** 167ba6c3ce9SSimon Glass * spi_alloc_slave_base - Allocate a new SPI slave with no private data 168ba6c3ce9SSimon Glass * 169ba6c3ce9SSimon Glass * Allocate and zero all fields in the spi slave, and set the bus/chip 170ba6c3ce9SSimon Glass * select. 171ba6c3ce9SSimon Glass * 172ba6c3ce9SSimon Glass * @bus: Bus ID of the slave chip. 173ba6c3ce9SSimon Glass * @cs: Chip select ID of the slave chip on the specified bus. 174ba6c3ce9SSimon Glass */ 175ba6c3ce9SSimon Glass #define spi_alloc_slave_base(bus, cs) \ 176ba6c3ce9SSimon Glass spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs) 177ba6c3ce9SSimon Glass 1781b1bd9a7SJagannadha Sutradharudu Teki /** 179d255bb0eSHaavard Skinnemoen * Set up communications parameters for a SPI slave. 180d255bb0eSHaavard Skinnemoen * 181d255bb0eSHaavard Skinnemoen * This must be called once for each slave. Note that this function 182d255bb0eSHaavard Skinnemoen * usually doesn't touch any actual hardware, it only initializes the 183d255bb0eSHaavard Skinnemoen * contents of spi_slave so that the hardware can be easily 184d255bb0eSHaavard Skinnemoen * initialized later. 185d255bb0eSHaavard Skinnemoen * 1861b1bd9a7SJagannadha Sutradharudu Teki * @bus: Bus ID of the slave chip. 1871b1bd9a7SJagannadha Sutradharudu Teki * @cs: Chip select ID of the slave chip on the specified bus. 1881b1bd9a7SJagannadha Sutradharudu Teki * @max_hz: Maximum SCK rate in Hz. 1891b1bd9a7SJagannadha Sutradharudu Teki * @mode: Clock polarity, clock phase and other parameters. 190d255bb0eSHaavard Skinnemoen * 191d255bb0eSHaavard Skinnemoen * Returns: A spi_slave reference that can be used in subsequent SPI 192d255bb0eSHaavard Skinnemoen * calls, or NULL if one or more of the parameters are not supported. 193d255bb0eSHaavard Skinnemoen */ 194d255bb0eSHaavard Skinnemoen struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, 195d255bb0eSHaavard Skinnemoen unsigned int max_hz, unsigned int mode); 196d255bb0eSHaavard Skinnemoen 1971b1bd9a7SJagannadha Sutradharudu Teki /** 198d255bb0eSHaavard Skinnemoen * Free any memory associated with a SPI slave. 199d255bb0eSHaavard Skinnemoen * 2001b1bd9a7SJagannadha Sutradharudu Teki * @slave: The SPI slave 201d255bb0eSHaavard Skinnemoen */ 202d255bb0eSHaavard Skinnemoen void spi_free_slave(struct spi_slave *slave); 203d255bb0eSHaavard Skinnemoen 2041b1bd9a7SJagannadha Sutradharudu Teki /** 205d255bb0eSHaavard Skinnemoen * Claim the bus and prepare it for communication with a given slave. 206d255bb0eSHaavard Skinnemoen * 207d255bb0eSHaavard Skinnemoen * This must be called before doing any transfers with a SPI slave. It 208d255bb0eSHaavard Skinnemoen * will enable and initialize any SPI hardware as necessary, and make 209d255bb0eSHaavard Skinnemoen * sure that the SCK line is in the correct idle state. It is not 210d255bb0eSHaavard Skinnemoen * allowed to claim the same bus for several slaves without releasing 211d255bb0eSHaavard Skinnemoen * the bus in between. 212d255bb0eSHaavard Skinnemoen * 2131b1bd9a7SJagannadha Sutradharudu Teki * @slave: The SPI slave 214d255bb0eSHaavard Skinnemoen * 215d255bb0eSHaavard Skinnemoen * Returns: 0 if the bus was claimed successfully, or a negative value 216d255bb0eSHaavard Skinnemoen * if it wasn't. 217d255bb0eSHaavard Skinnemoen */ 218d255bb0eSHaavard Skinnemoen int spi_claim_bus(struct spi_slave *slave); 219d255bb0eSHaavard Skinnemoen 2201b1bd9a7SJagannadha Sutradharudu Teki /** 221d255bb0eSHaavard Skinnemoen * Release the SPI bus 222d255bb0eSHaavard Skinnemoen * 223d255bb0eSHaavard Skinnemoen * This must be called once for every call to spi_claim_bus() after 224d255bb0eSHaavard Skinnemoen * all transfers have finished. It may disable any SPI hardware as 225d255bb0eSHaavard Skinnemoen * appropriate. 226d255bb0eSHaavard Skinnemoen * 2271b1bd9a7SJagannadha Sutradharudu Teki * @slave: The SPI slave 228d255bb0eSHaavard Skinnemoen */ 229d255bb0eSHaavard Skinnemoen void spi_release_bus(struct spi_slave *slave); 23077f85581Swdenk 2311b1bd9a7SJagannadha Sutradharudu Teki /** 2325753d09bSNikita Kiryanov * Set the word length for SPI transactions 2335753d09bSNikita Kiryanov * 2345753d09bSNikita Kiryanov * Set the word length (number of bits per word) for SPI transactions. 2355753d09bSNikita Kiryanov * 2365753d09bSNikita Kiryanov * @slave: The SPI slave 2375753d09bSNikita Kiryanov * @wordlen: The number of bits in a word 2385753d09bSNikita Kiryanov * 2395753d09bSNikita Kiryanov * Returns: 0 on success, -1 on failure. 2405753d09bSNikita Kiryanov */ 2415753d09bSNikita Kiryanov int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen); 2425753d09bSNikita Kiryanov 2435753d09bSNikita Kiryanov /** 24477f85581Swdenk * SPI transfer 24577f85581Swdenk * 24677f85581Swdenk * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks 24777f85581Swdenk * "bitlen" bits in the SPI MISO port. That's just the way SPI works. 24877f85581Swdenk * 24977f85581Swdenk * The source of the outgoing bits is the "dout" parameter and the 25077f85581Swdenk * destination of the input bits is the "din" parameter. Note that "dout" 25177f85581Swdenk * and "din" can point to the same memory location, in which case the 25277f85581Swdenk * input data overwrites the output data (since both are buffered by 25377f85581Swdenk * temporary variables, this is OK). 25477f85581Swdenk * 25577f85581Swdenk * spi_xfer() interface: 2561b1bd9a7SJagannadha Sutradharudu Teki * @slave: The SPI slave which will be sending/receiving the data. 2571b1bd9a7SJagannadha Sutradharudu Teki * @bitlen: How many bits to write and read. 2581b1bd9a7SJagannadha Sutradharudu Teki * @dout: Pointer to a string of bits to send out. The bits are 25977f85581Swdenk * held in a byte array and are sent MSB first. 2601b1bd9a7SJagannadha Sutradharudu Teki * @din: Pointer to a string of bits that will be filled in. 2611b1bd9a7SJagannadha Sutradharudu Teki * @flags: A bitwise combination of SPI_XFER_* flags. 26277f85581Swdenk * 26377f85581Swdenk * Returns: 0 on success, not 0 on failure 26477f85581Swdenk */ 265d255bb0eSHaavard Skinnemoen int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, 266d255bb0eSHaavard Skinnemoen void *din, unsigned long flags); 26777f85581Swdenk 268146bad96STom Rini /* Copy memory mapped data */ 269146bad96STom Rini void spi_flash_copy_mmap(void *data, void *offset, size_t len); 270146bad96STom Rini 2711b1bd9a7SJagannadha Sutradharudu Teki /** 272d255bb0eSHaavard Skinnemoen * Determine if a SPI chipselect is valid. 273d255bb0eSHaavard Skinnemoen * This function is provided by the board if the low-level SPI driver 274d255bb0eSHaavard Skinnemoen * needs it to determine if a given chipselect is actually valid. 275d255bb0eSHaavard Skinnemoen * 276d255bb0eSHaavard Skinnemoen * Returns: 1 if bus:cs identifies a valid chip on this board, 0 277d255bb0eSHaavard Skinnemoen * otherwise. 278d255bb0eSHaavard Skinnemoen */ 279d255bb0eSHaavard Skinnemoen int spi_cs_is_valid(unsigned int bus, unsigned int cs); 280d255bb0eSHaavard Skinnemoen 281d7af6a48SSimon Glass #ifndef CONFIG_DM_SPI 2821b1bd9a7SJagannadha Sutradharudu Teki /** 283d255bb0eSHaavard Skinnemoen * Activate a SPI chipselect. 284d255bb0eSHaavard Skinnemoen * This function is provided by the board code when using a driver 285d255bb0eSHaavard Skinnemoen * that can't control its chipselects automatically (e.g. 286d255bb0eSHaavard Skinnemoen * common/soft_spi.c). When called, it should activate the chip select 287d255bb0eSHaavard Skinnemoen * to the device identified by "slave". 288d255bb0eSHaavard Skinnemoen */ 289d255bb0eSHaavard Skinnemoen void spi_cs_activate(struct spi_slave *slave); 290d255bb0eSHaavard Skinnemoen 2911b1bd9a7SJagannadha Sutradharudu Teki /** 292d255bb0eSHaavard Skinnemoen * Deactivate a SPI chipselect. 293d255bb0eSHaavard Skinnemoen * This function is provided by the board code when using a driver 294d255bb0eSHaavard Skinnemoen * that can't control its chipselects automatically (e.g. 295d255bb0eSHaavard Skinnemoen * common/soft_spi.c). When called, it should deactivate the chip 296d255bb0eSHaavard Skinnemoen * select to the device identified by "slave". 297d255bb0eSHaavard Skinnemoen */ 298d255bb0eSHaavard Skinnemoen void spi_cs_deactivate(struct spi_slave *slave); 299d255bb0eSHaavard Skinnemoen 3001b1bd9a7SJagannadha Sutradharudu Teki /** 301fa1423e7SThomas Chou * Set transfer speed. 302fa1423e7SThomas Chou * This sets a new speed to be applied for next spi_xfer(). 3031b1bd9a7SJagannadha Sutradharudu Teki * @slave: The SPI slave 3041b1bd9a7SJagannadha Sutradharudu Teki * @hz: The transfer speed 305fa1423e7SThomas Chou */ 306fa1423e7SThomas Chou void spi_set_speed(struct spi_slave *slave, uint hz); 307d7af6a48SSimon Glass #endif 308fa1423e7SThomas Chou 3091b1bd9a7SJagannadha Sutradharudu Teki /** 310d255bb0eSHaavard Skinnemoen * Write 8 bits, then read 8 bits. 3111b1bd9a7SJagannadha Sutradharudu Teki * @slave: The SPI slave we're communicating with 3121b1bd9a7SJagannadha Sutradharudu Teki * @byte: Byte to be written 313d255bb0eSHaavard Skinnemoen * 314d255bb0eSHaavard Skinnemoen * Returns: The value that was read, or a negative value on error. 315d255bb0eSHaavard Skinnemoen * 316d255bb0eSHaavard Skinnemoen * TODO: This function probably shouldn't be inlined. 317d255bb0eSHaavard Skinnemoen */ 318d255bb0eSHaavard Skinnemoen static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte) 319d255bb0eSHaavard Skinnemoen { 320d255bb0eSHaavard Skinnemoen unsigned char dout[2]; 321d255bb0eSHaavard Skinnemoen unsigned char din[2]; 322d255bb0eSHaavard Skinnemoen int ret; 323d255bb0eSHaavard Skinnemoen 324d255bb0eSHaavard Skinnemoen dout[0] = byte; 325d255bb0eSHaavard Skinnemoen dout[1] = 0; 326d255bb0eSHaavard Skinnemoen 327d255bb0eSHaavard Skinnemoen ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END); 328d255bb0eSHaavard Skinnemoen return ret < 0 ? ret : din[1]; 329d255bb0eSHaavard Skinnemoen } 33038254f45SGuennadi Liakhovetski 331f3424c55SHung-ying Tyan /** 332f3424c55SHung-ying Tyan * Set up a SPI slave for a particular device tree node 333f3424c55SHung-ying Tyan * 334f3424c55SHung-ying Tyan * This calls spi_setup_slave() with the correct bus number. Call 335f3424c55SHung-ying Tyan * spi_free_slave() to free it later. 336f3424c55SHung-ying Tyan * 337469146c0SJagannadha Sutradharudu Teki * @param blob: Device tree blob 3380efc0249SSimon Glass * @param slave_node: Slave node to use 3390efc0249SSimon Glass * @param spi_node: SPI peripheral node to use 340f3424c55SHung-ying Tyan * @return pointer to new spi_slave structure 341f3424c55SHung-ying Tyan */ 3420efc0249SSimon Glass struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node, 3430efc0249SSimon Glass int spi_node); 3440efc0249SSimon Glass 3450efc0249SSimon Glass /** 3460efc0249SSimon Glass * spi_base_setup_slave_fdt() - helper function to set up a SPI slace 3470efc0249SSimon Glass * 3480efc0249SSimon Glass * This decodes SPI properties from the slave node to determine the 3490efc0249SSimon Glass * chip select and SPI parameters. 3500efc0249SSimon Glass * 3510efc0249SSimon Glass * @blob: Device tree blob 3520efc0249SSimon Glass * @busnum: Bus number to use 3530efc0249SSimon Glass * @node: Device tree node for the SPI bus 3540efc0249SSimon Glass */ 3550efc0249SSimon Glass struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum, 3560efc0249SSimon Glass int node); 357f3424c55SHung-ying Tyan 358d7af6a48SSimon Glass #ifdef CONFIG_DM_SPI 359d7af6a48SSimon Glass 360d7af6a48SSimon Glass /** 361d7af6a48SSimon Glass * struct spi_cs_info - Information about a bus chip select 362d7af6a48SSimon Glass * 363d7af6a48SSimon Glass * @dev: Connected device, or NULL if none 364d7af6a48SSimon Glass */ 365d7af6a48SSimon Glass struct spi_cs_info { 366d7af6a48SSimon Glass struct udevice *dev; 367d7af6a48SSimon Glass }; 368d7af6a48SSimon Glass 369d7af6a48SSimon Glass /** 370d7af6a48SSimon Glass * struct struct dm_spi_ops - Driver model SPI operations 371d7af6a48SSimon Glass * 372d7af6a48SSimon Glass * The uclass interface is implemented by all SPI devices which use 373d7af6a48SSimon Glass * driver model. 374d7af6a48SSimon Glass */ 375d7af6a48SSimon Glass struct dm_spi_ops { 376d7af6a48SSimon Glass /** 377d7af6a48SSimon Glass * Claim the bus and prepare it for communication. 378d7af6a48SSimon Glass * 379d7af6a48SSimon Glass * The device provided is the slave device. It's parent controller 380d7af6a48SSimon Glass * will be used to provide the communication. 381d7af6a48SSimon Glass * 382d7af6a48SSimon Glass * This must be called before doing any transfers with a SPI slave. It 383d7af6a48SSimon Glass * will enable and initialize any SPI hardware as necessary, and make 384d7af6a48SSimon Glass * sure that the SCK line is in the correct idle state. It is not 385d7af6a48SSimon Glass * allowed to claim the same bus for several slaves without releasing 386d7af6a48SSimon Glass * the bus in between. 387d7af6a48SSimon Glass * 3889694b724SSimon Glass * @dev: The SPI slave 389d7af6a48SSimon Glass * 390d7af6a48SSimon Glass * Returns: 0 if the bus was claimed successfully, or a negative value 391d7af6a48SSimon Glass * if it wasn't. 392d7af6a48SSimon Glass */ 3939694b724SSimon Glass int (*claim_bus)(struct udevice *dev); 394d7af6a48SSimon Glass 395d7af6a48SSimon Glass /** 396d7af6a48SSimon Glass * Release the SPI bus 397d7af6a48SSimon Glass * 398d7af6a48SSimon Glass * This must be called once for every call to spi_claim_bus() after 399d7af6a48SSimon Glass * all transfers have finished. It may disable any SPI hardware as 400d7af6a48SSimon Glass * appropriate. 401d7af6a48SSimon Glass * 4029694b724SSimon Glass * @dev: The SPI slave 403d7af6a48SSimon Glass */ 4049694b724SSimon Glass int (*release_bus)(struct udevice *dev); 405d7af6a48SSimon Glass 406d7af6a48SSimon Glass /** 407d7af6a48SSimon Glass * Set the word length for SPI transactions 408d7af6a48SSimon Glass * 409d7af6a48SSimon Glass * Set the word length (number of bits per word) for SPI transactions. 410d7af6a48SSimon Glass * 411d7af6a48SSimon Glass * @bus: The SPI slave 412d7af6a48SSimon Glass * @wordlen: The number of bits in a word 413d7af6a48SSimon Glass * 414d7af6a48SSimon Glass * Returns: 0 on success, -ve on failure. 415d7af6a48SSimon Glass */ 4169694b724SSimon Glass int (*set_wordlen)(struct udevice *dev, unsigned int wordlen); 417d7af6a48SSimon Glass 418d7af6a48SSimon Glass /** 419d7af6a48SSimon Glass * SPI transfer 420d7af6a48SSimon Glass * 421d7af6a48SSimon Glass * This writes "bitlen" bits out the SPI MOSI port and simultaneously 422d7af6a48SSimon Glass * clocks "bitlen" bits in the SPI MISO port. That's just the way SPI 423d7af6a48SSimon Glass * works. 424d7af6a48SSimon Glass * 425d7af6a48SSimon Glass * The source of the outgoing bits is the "dout" parameter and the 426d7af6a48SSimon Glass * destination of the input bits is the "din" parameter. Note that 427d7af6a48SSimon Glass * "dout" and "din" can point to the same memory location, in which 428d7af6a48SSimon Glass * case the input data overwrites the output data (since both are 429d7af6a48SSimon Glass * buffered by temporary variables, this is OK). 430d7af6a48SSimon Glass * 431d7af6a48SSimon Glass * spi_xfer() interface: 432d7af6a48SSimon Glass * @dev: The slave device to communicate with 433d7af6a48SSimon Glass * @bitlen: How many bits to write and read. 434d7af6a48SSimon Glass * @dout: Pointer to a string of bits to send out. The bits are 435d7af6a48SSimon Glass * held in a byte array and are sent MSB first. 436d7af6a48SSimon Glass * @din: Pointer to a string of bits that will be filled in. 437d7af6a48SSimon Glass * @flags: A bitwise combination of SPI_XFER_* flags. 438d7af6a48SSimon Glass * 439d7af6a48SSimon Glass * Returns: 0 on success, not -1 on failure 440d7af6a48SSimon Glass */ 441d7af6a48SSimon Glass int (*xfer)(struct udevice *dev, unsigned int bitlen, const void *dout, 442d7af6a48SSimon Glass void *din, unsigned long flags); 443d7af6a48SSimon Glass 444d7af6a48SSimon Glass /** 445d7af6a48SSimon Glass * Set transfer speed. 446d7af6a48SSimon Glass * This sets a new speed to be applied for next spi_xfer(). 447d7af6a48SSimon Glass * @bus: The SPI bus 448d7af6a48SSimon Glass * @hz: The transfer speed 449d7af6a48SSimon Glass * @return 0 if OK, -ve on error 450d7af6a48SSimon Glass */ 451d7af6a48SSimon Glass int (*set_speed)(struct udevice *bus, uint hz); 452d7af6a48SSimon Glass 453d7af6a48SSimon Glass /** 454d7af6a48SSimon Glass * Set the SPI mode/flags 455d7af6a48SSimon Glass * 456d7af6a48SSimon Glass * It is unclear if we want to set speed and mode together instead 457d7af6a48SSimon Glass * of separately. 458d7af6a48SSimon Glass * 459d7af6a48SSimon Glass * @bus: The SPI bus 460d7af6a48SSimon Glass * @mode: Requested SPI mode (SPI_... flags) 461d7af6a48SSimon Glass * @return 0 if OK, -ve on error 462d7af6a48SSimon Glass */ 463d7af6a48SSimon Glass int (*set_mode)(struct udevice *bus, uint mode); 464d7af6a48SSimon Glass 465d7af6a48SSimon Glass /** 466d7af6a48SSimon Glass * Get information on a chip select 467d7af6a48SSimon Glass * 468d7af6a48SSimon Glass * This is only called when the SPI uclass does not know about a 469d7af6a48SSimon Glass * chip select, i.e. it has no attached device. It gives the driver 470d7af6a48SSimon Glass * a chance to allow activity on that chip select even so. 471d7af6a48SSimon Glass * 472d7af6a48SSimon Glass * @bus: The SPI bus 473d7af6a48SSimon Glass * @cs: The chip select (0..n-1) 474d7af6a48SSimon Glass * @info: Returns information about the chip select, if valid. 475d7af6a48SSimon Glass * On entry info->dev is NULL 476d7af6a48SSimon Glass * @return 0 if OK (and @info is set up), -ENODEV if the chip select 477d7af6a48SSimon Glass * is invalid, other -ve value on error 478d7af6a48SSimon Glass */ 479d7af6a48SSimon Glass int (*cs_info)(struct udevice *bus, uint cs, struct spi_cs_info *info); 480d7af6a48SSimon Glass }; 481d7af6a48SSimon Glass 482c60e1f25SSimon Glass struct dm_spi_emul_ops { 483c60e1f25SSimon Glass /** 484c60e1f25SSimon Glass * SPI transfer 485c60e1f25SSimon Glass * 486c60e1f25SSimon Glass * This writes "bitlen" bits out the SPI MOSI port and simultaneously 487c60e1f25SSimon Glass * clocks "bitlen" bits in the SPI MISO port. That's just the way SPI 488c60e1f25SSimon Glass * works. Here the device is a slave. 489c60e1f25SSimon Glass * 490c60e1f25SSimon Glass * The source of the outgoing bits is the "dout" parameter and the 491c60e1f25SSimon Glass * destination of the input bits is the "din" parameter. Note that 492c60e1f25SSimon Glass * "dout" and "din" can point to the same memory location, in which 493c60e1f25SSimon Glass * case the input data overwrites the output data (since both are 494c60e1f25SSimon Glass * buffered by temporary variables, this is OK). 495c60e1f25SSimon Glass * 496c60e1f25SSimon Glass * spi_xfer() interface: 497c60e1f25SSimon Glass * @slave: The SPI slave which will be sending/receiving the data. 498c60e1f25SSimon Glass * @bitlen: How many bits to write and read. 499c60e1f25SSimon Glass * @dout: Pointer to a string of bits sent to the device. The 500c60e1f25SSimon Glass * bits are held in a byte array and are sent MSB first. 501c60e1f25SSimon Glass * @din: Pointer to a string of bits that will be sent back to 502c60e1f25SSimon Glass * the master. 503c60e1f25SSimon Glass * @flags: A bitwise combination of SPI_XFER_* flags. 504c60e1f25SSimon Glass * 505c60e1f25SSimon Glass * Returns: 0 on success, not -1 on failure 506c60e1f25SSimon Glass */ 507c60e1f25SSimon Glass int (*xfer)(struct udevice *slave, unsigned int bitlen, 508c60e1f25SSimon Glass const void *dout, void *din, unsigned long flags); 509c60e1f25SSimon Glass }; 510c60e1f25SSimon Glass 511d7af6a48SSimon Glass /** 512d7af6a48SSimon Glass * spi_find_bus_and_cs() - Find bus and slave devices by number 513d7af6a48SSimon Glass * 514d7af6a48SSimon Glass * Given a bus number and chip select, this finds the corresponding bus 515d7af6a48SSimon Glass * device and slave device. Neither device is activated by this function, 516d7af6a48SSimon Glass * although they may have been activated previously. 517d7af6a48SSimon Glass * 518d7af6a48SSimon Glass * @busnum: SPI bus number 519d7af6a48SSimon Glass * @cs: Chip select to look for 520d7af6a48SSimon Glass * @busp: Returns bus device 521d7af6a48SSimon Glass * @devp: Return slave device 522d7af6a48SSimon Glass * @return 0 if found, -ENODEV on error 523d7af6a48SSimon Glass */ 524d7af6a48SSimon Glass int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp, 525d7af6a48SSimon Glass struct udevice **devp); 526d7af6a48SSimon Glass 527d7af6a48SSimon Glass /** 528d7af6a48SSimon Glass * spi_get_bus_and_cs() - Find and activate bus and slave devices by number 529d7af6a48SSimon Glass * 530d7af6a48SSimon Glass * Given a bus number and chip select, this finds the corresponding bus 531d7af6a48SSimon Glass * device and slave device. 532d7af6a48SSimon Glass * 533d7af6a48SSimon Glass * If no such slave exists, and drv_name is not NULL, then a new slave device 534d7af6a48SSimon Glass * is automatically bound on this chip select. 535d7af6a48SSimon Glass * 536d7af6a48SSimon Glass * Ths new slave device is probed ready for use with the given speed and mode. 537d7af6a48SSimon Glass * 538d7af6a48SSimon Glass * @busnum: SPI bus number 539d7af6a48SSimon Glass * @cs: Chip select to look for 540d7af6a48SSimon Glass * @speed: SPI speed to use for this slave 541d7af6a48SSimon Glass * @mode: SPI mode to use for this slave 542d7af6a48SSimon Glass * @drv_name: Name of driver to attach to this chip select 543d7af6a48SSimon Glass * @dev_name: Name of the new device thus created 544d7af6a48SSimon Glass * @busp: Returns bus device 545d7af6a48SSimon Glass * @devp: Return slave device 546d7af6a48SSimon Glass * @return 0 if found, -ve on error 547d7af6a48SSimon Glass */ 548d7af6a48SSimon Glass int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode, 549d7af6a48SSimon Glass const char *drv_name, const char *dev_name, 550d7af6a48SSimon Glass struct udevice **busp, struct spi_slave **devp); 551d7af6a48SSimon Glass 552d7af6a48SSimon Glass /** 553d7af6a48SSimon Glass * spi_chip_select() - Get the chip select for a slave 554d7af6a48SSimon Glass * 555d7af6a48SSimon Glass * @return the chip select this slave is attached to 556d7af6a48SSimon Glass */ 557d7af6a48SSimon Glass int spi_chip_select(struct udevice *slave); 558d7af6a48SSimon Glass 559d7af6a48SSimon Glass /** 560ff56bba2SSimon Glass * spi_find_chip_select() - Find the slave attached to chip select 561ff56bba2SSimon Glass * 562ff56bba2SSimon Glass * @bus: SPI bus to search 563ff56bba2SSimon Glass * @cs: Chip select to look for 564ff56bba2SSimon Glass * @devp: Returns the slave device if found 565ff56bba2SSimon Glass * @return 0 if found, -ENODEV on error 566ff56bba2SSimon Glass */ 567ff56bba2SSimon Glass int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp); 568ff56bba2SSimon Glass 569ff56bba2SSimon Glass /** 570d0cff03eSSimon Glass * spi_slave_ofdata_to_platdata() - decode standard SPI platform data 571d7af6a48SSimon Glass * 572d0cff03eSSimon Glass * This decodes the speed and mode for a slave from a device tree node 573d7af6a48SSimon Glass * 574d7af6a48SSimon Glass * @blob: Device tree blob 575d7af6a48SSimon Glass * @node: Node offset to read from 576d0cff03eSSimon Glass * @plat: Place to put the decoded information 577d7af6a48SSimon Glass */ 578d0cff03eSSimon Glass int spi_slave_ofdata_to_platdata(const void *blob, int node, 579d0cff03eSSimon Glass struct dm_spi_slave_platdata *plat); 580d7af6a48SSimon Glass 581d7af6a48SSimon Glass /** 582d7af6a48SSimon Glass * spi_cs_info() - Check information on a chip select 583d7af6a48SSimon Glass * 584d7af6a48SSimon Glass * This checks a particular chip select on a bus to see if it has a device 585d7af6a48SSimon Glass * attached, or is even valid. 586d7af6a48SSimon Glass * 587d7af6a48SSimon Glass * @bus: The SPI bus 588d7af6a48SSimon Glass * @cs: The chip select (0..n-1) 589d7af6a48SSimon Glass * @info: Returns information about the chip select, if valid 590d7af6a48SSimon Glass * @return 0 if OK (and @info is set up), -ENODEV if the chip select 591d7af6a48SSimon Glass * is invalid, other -ve value on error 592d7af6a48SSimon Glass */ 593d7af6a48SSimon Glass int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info); 594d7af6a48SSimon Glass 595d7af6a48SSimon Glass struct sandbox_state; 596c60e1f25SSimon Glass 597c60e1f25SSimon Glass /** 598c60e1f25SSimon Glass * sandbox_spi_get_emul() - get an emulator for a SPI slave 599c60e1f25SSimon Glass * 600c60e1f25SSimon Glass * This provides a way to attach an emulated SPI device to a particular SPI 601c60e1f25SSimon Glass * slave, so that xfer() operations on the slave will be handled by the 602c60e1f25SSimon Glass * emulator. If a emulator already exists on that chip select it is returned. 603c60e1f25SSimon Glass * Otherwise one is created. 604c60e1f25SSimon Glass * 605c60e1f25SSimon Glass * @state: Sandbox state 606c60e1f25SSimon Glass * @bus: SPI bus requesting the emulator 607c60e1f25SSimon Glass * @slave: SPI slave device requesting the emulator 608c60e1f25SSimon Glass * @emuip: Returns pointer to emulator 609c60e1f25SSimon Glass * @return 0 if OK, -ve on error 610c60e1f25SSimon Glass */ 611d7af6a48SSimon Glass int sandbox_spi_get_emul(struct sandbox_state *state, 612d7af6a48SSimon Glass struct udevice *bus, struct udevice *slave, 613d7af6a48SSimon Glass struct udevice **emulp); 614d7af6a48SSimon Glass 615*7a3eff4cSPeng Fan /** 616*7a3eff4cSPeng Fan * Claim the bus and prepare it for communication with a given slave. 617*7a3eff4cSPeng Fan * 618*7a3eff4cSPeng Fan * This must be called before doing any transfers with a SPI slave. It 619*7a3eff4cSPeng Fan * will enable and initialize any SPI hardware as necessary, and make 620*7a3eff4cSPeng Fan * sure that the SCK line is in the correct idle state. It is not 621*7a3eff4cSPeng Fan * allowed to claim the same bus for several slaves without releasing 622*7a3eff4cSPeng Fan * the bus in between. 623*7a3eff4cSPeng Fan * 624*7a3eff4cSPeng Fan * @dev: The SPI slave device 625*7a3eff4cSPeng Fan * 626*7a3eff4cSPeng Fan * Returns: 0 if the bus was claimed successfully, or a negative value 627*7a3eff4cSPeng Fan * if it wasn't. 628*7a3eff4cSPeng Fan */ 629*7a3eff4cSPeng Fan int dm_spi_claim_bus(struct udevice *dev); 630*7a3eff4cSPeng Fan 631*7a3eff4cSPeng Fan /** 632*7a3eff4cSPeng Fan * Release the SPI bus 633*7a3eff4cSPeng Fan * 634*7a3eff4cSPeng Fan * This must be called once for every call to dm_spi_claim_bus() after 635*7a3eff4cSPeng Fan * all transfers have finished. It may disable any SPI hardware as 636*7a3eff4cSPeng Fan * appropriate. 637*7a3eff4cSPeng Fan * 638*7a3eff4cSPeng Fan * @slave: The SPI slave device 639*7a3eff4cSPeng Fan */ 640*7a3eff4cSPeng Fan void dm_spi_release_bus(struct udevice *dev); 641*7a3eff4cSPeng Fan 642*7a3eff4cSPeng Fan /** 643*7a3eff4cSPeng Fan * SPI transfer 644*7a3eff4cSPeng Fan * 645*7a3eff4cSPeng Fan * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks 646*7a3eff4cSPeng Fan * "bitlen" bits in the SPI MISO port. That's just the way SPI works. 647*7a3eff4cSPeng Fan * 648*7a3eff4cSPeng Fan * The source of the outgoing bits is the "dout" parameter and the 649*7a3eff4cSPeng Fan * destination of the input bits is the "din" parameter. Note that "dout" 650*7a3eff4cSPeng Fan * and "din" can point to the same memory location, in which case the 651*7a3eff4cSPeng Fan * input data overwrites the output data (since both are buffered by 652*7a3eff4cSPeng Fan * temporary variables, this is OK). 653*7a3eff4cSPeng Fan * 654*7a3eff4cSPeng Fan * dm_spi_xfer() interface: 655*7a3eff4cSPeng Fan * @dev: The SPI slave device which will be sending/receiving the data. 656*7a3eff4cSPeng Fan * @bitlen: How many bits to write and read. 657*7a3eff4cSPeng Fan * @dout: Pointer to a string of bits to send out. The bits are 658*7a3eff4cSPeng Fan * held in a byte array and are sent MSB first. 659*7a3eff4cSPeng Fan * @din: Pointer to a string of bits that will be filled in. 660*7a3eff4cSPeng Fan * @flags: A bitwise combination of SPI_XFER_* flags. 661*7a3eff4cSPeng Fan * 662*7a3eff4cSPeng Fan * Returns: 0 on success, not 0 on failure 663*7a3eff4cSPeng Fan */ 664*7a3eff4cSPeng Fan int dm_spi_xfer(struct udevice *dev, unsigned int bitlen, 665*7a3eff4cSPeng Fan const void *dout, void *din, unsigned long flags); 666*7a3eff4cSPeng Fan 667bc5701e1SSimon Glass /* Access the operations for a SPI device */ 668d7af6a48SSimon Glass #define spi_get_ops(dev) ((struct dm_spi_ops *)(dev)->driver->ops) 669c60e1f25SSimon Glass #define spi_emul_get_ops(dev) ((struct dm_spi_emul_ops *)(dev)->driver->ops) 670d7af6a48SSimon Glass #endif /* CONFIG_DM_SPI */ 671d7af6a48SSimon Glass 67277f85581Swdenk #endif /* _SPI_H_ */ 673