xref: /rk3399_rockchip-uboot/include/spi.h (revision 9689b7dd4f457171bf295756bb0fe433cbc03d7f)
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 
13967efcaeSBoris Brezillon #include <common.h>
14967efcaeSBoris Brezillon 
1538254f45SGuennadi Liakhovetski /* SPI mode flags */
1629ee0262SJagan Teki #define SPI_CPHA	BIT(0)			/* clock phase */
1729ee0262SJagan Teki #define SPI_CPOL	BIT(1)			/* clock polarity */
1838254f45SGuennadi Liakhovetski #define SPI_MODE_0	(0|0)			/* (original MicroWire) */
1938254f45SGuennadi Liakhovetski #define SPI_MODE_1	(0|SPI_CPHA)
2038254f45SGuennadi Liakhovetski #define SPI_MODE_2	(SPI_CPOL|0)
2138254f45SGuennadi Liakhovetski #define SPI_MODE_3	(SPI_CPOL|SPI_CPHA)
2229ee0262SJagan Teki #define SPI_CS_HIGH	BIT(2)			/* CS active high */
2329ee0262SJagan Teki #define SPI_LSB_FIRST	BIT(3)			/* per-word bits-on-wire */
2429ee0262SJagan Teki #define SPI_3WIRE	BIT(4)			/* SI/SO signals shared */
2529ee0262SJagan Teki #define SPI_LOOP	BIT(5)			/* loopback mode */
2629ee0262SJagan Teki #define SPI_SLAVE	BIT(6)			/* slave mode */
2729ee0262SJagan Teki #define SPI_PREAMBLE	BIT(7)			/* Skip preamble bytes */
2829ee0262SJagan Teki #define SPI_TX_BYTE	BIT(8)			/* transmit with 1 wire byte */
292b11a41cSJagan Teki #define SPI_TX_DUAL	BIT(9)			/* transmit with 2 wires */
302b11a41cSJagan Teki #define SPI_TX_QUAD	BIT(10)			/* transmit with 4 wires */
3108fe9c29SJagan Teki #define SPI_RX_SLOW	BIT(11)			/* receive with 1 wire slow */
323ac48d0eSJagan Teki #define SPI_RX_DUAL	BIT(12)			/* receive with 2 wires */
333ac48d0eSJagan Teki #define SPI_RX_QUAD	BIT(13)			/* receive with 4 wires */
34305d7e6eSVignesh Raghavendra #define SPI_TX_OCTAL	BIT(14)			/* transmit with 8 wires */
35305d7e6eSVignesh Raghavendra #define SPI_RX_OCTAL	BIT(15)			/* receive with 8 wires */
36f5a32af5SJon Lin #define SPI_DMA_PREPARE	BIT(24)			/* dma transfer skip waiting idle */
374e09cc1eSJagannadha Sutradharudu Teki 
38bb786b84SRajeshwari Shinde /* Header byte that marks the start of the message */
39bb786b84SRajeshwari Shinde #define SPI_PREAMBLE_END_BYTE	0xec
40bb786b84SRajeshwari Shinde 
415753d09bSNikita Kiryanov #define SPI_DEFAULT_WORDLEN	8
425753d09bSNikita Kiryanov 
43*9689b7ddSOvidiu Panait /**
44*9689b7ddSOvidiu Panait  * struct dm_spi_bus - SPI bus info
45*9689b7ddSOvidiu Panait  *
46*9689b7ddSOvidiu Panait  * This contains information about a SPI bus. To obtain this structure, use
47*9689b7ddSOvidiu Panait  * dev_get_uclass_priv(bus) where bus is the SPI bus udevice.
48*9689b7ddSOvidiu Panait  *
49*9689b7ddSOvidiu Panait  * @max_hz:	Maximum speed that the bus can tolerate.
50*9689b7ddSOvidiu Panait  * @speed:	Current bus speed. This is 0 until the bus is first claimed.
51*9689b7ddSOvidiu Panait  * @mode:	Current bus mode. This is 0 until the bus is first claimed.
52*9689b7ddSOvidiu Panait  *
53*9689b7ddSOvidiu Panait  * TODO(sjg@chromium.org): Remove this and use max_hz from struct spi_slave.
54*9689b7ddSOvidiu Panait  */
55d7af6a48SSimon Glass struct dm_spi_bus {
56d7af6a48SSimon Glass 	uint max_hz;
57*9689b7ddSOvidiu Panait 	uint speed;
58*9689b7ddSOvidiu Panait 	uint mode;
59d7af6a48SSimon Glass };
60d7af6a48SSimon Glass 
61d0cff03eSSimon Glass /**
62d0cff03eSSimon Glass  * struct dm_spi_platdata - platform data for all SPI slaves
63d0cff03eSSimon Glass  *
64d0cff03eSSimon Glass  * This describes a SPI slave, a child device of the SPI bus. To obtain this
65d0cff03eSSimon Glass  * struct from a spi_slave, use dev_get_parent_platdata(dev) or
66d0cff03eSSimon Glass  * dev_get_parent_platdata(slave->dev).
67d0cff03eSSimon Glass  *
68d0cff03eSSimon Glass  * This data is immuatable. Each time the device is probed, @max_hz and @mode
69d0cff03eSSimon Glass  * will be copied to struct spi_slave.
70d0cff03eSSimon Glass  *
71d0cff03eSSimon Glass  * @cs:		Chip select number (0..n-1)
72d0cff03eSSimon Glass  * @max_hz:	Maximum bus speed that this slave can tolerate
73d0cff03eSSimon Glass  * @mode:	SPI mode to use for this device (see SPI mode flags)
74d0cff03eSSimon Glass  */
75d0cff03eSSimon Glass struct dm_spi_slave_platdata {
76d0cff03eSSimon Glass 	unsigned int cs;
77d0cff03eSSimon Glass 	uint max_hz;
78d0cff03eSSimon Glass 	uint mode;
79d0cff03eSSimon Glass };
80d0cff03eSSimon Glass 
811b1bd9a7SJagannadha Sutradharudu Teki /**
82ce22b922SJagannadha Sutradharudu Teki  * struct spi_slave - Representation of a SPI slave
83d255bb0eSHaavard Skinnemoen  *
84d7af6a48SSimon Glass  * For driver model this is the per-child data used by the SPI bus. It can
85bcbe3d15SSimon Glass  * be accessed using dev_get_parent_priv() on the slave device. The SPI uclass
86d0cff03eSSimon Glass  * sets uip per_child_auto_alloc_size to sizeof(struct spi_slave), and the
87d0cff03eSSimon Glass  * driver should not override it. Two platform data fields (max_hz and mode)
88d0cff03eSSimon Glass  * are copied into this structure to provide an initial value. This allows
89d0cff03eSSimon Glass  * them to be changed, since we should never change platform data in drivers.
90d7af6a48SSimon Glass  *
91d7af6a48SSimon Glass  * If not using driver model, drivers are expected to extend this with
92d7af6a48SSimon Glass  * controller-specific data.
93d7af6a48SSimon Glass  *
94d7af6a48SSimon Glass  * @dev:		SPI slave device
95d7af6a48SSimon Glass  * @max_hz:		Maximum speed for this slave
96d7af6a48SSimon Glass  * @bus:		ID of the bus that the slave is attached to. For
97d7af6a48SSimon Glass  *			driver model this is the sequence number of the SPI
98d7af6a48SSimon Glass  *			bus (bus->seq) so does not need to be stored
99ce22b922SJagannadha Sutradharudu Teki  * @cs:			ID of the chip select connected to the slave.
100f5c3c033SJagan Teki  * @mode:		SPI mode to use for this slave (see SPI mode flags)
1015753d09bSNikita Kiryanov  * @wordlen:		Size of SPI word in number of bits
102d68b9b84SÁlvaro Fernández Rojas  * @max_read_size:	If non-zero, the maximum number of bytes which can
103d68b9b84SÁlvaro Fernández Rojas  *			be read at once.
104ce22b922SJagannadha Sutradharudu Teki  * @max_write_size:	If non-zero, the maximum number of bytes which can
105d2a88c91SÁlvaro Fernández Rojas  *			be written at once.
106ce22b922SJagannadha Sutradharudu Teki  * @memory_map:		Address of read-only SPI flash access.
107f77f4691SJagannadha Sutradharudu Teki  * @flags:		Indication of SPI flags.
108d255bb0eSHaavard Skinnemoen  */
109d255bb0eSHaavard Skinnemoen struct spi_slave {
110d7af6a48SSimon Glass #ifdef CONFIG_DM_SPI
111d7af6a48SSimon Glass 	struct udevice *dev;	/* struct spi_slave is dev->parentdata */
112d7af6a48SSimon Glass 	uint max_hz;
113d7af6a48SSimon Glass #else
114d255bb0eSHaavard Skinnemoen 	unsigned int bus;
115d255bb0eSHaavard Skinnemoen 	unsigned int cs;
116d0cff03eSSimon Glass #endif
117f5c3c033SJagan Teki 	uint mode;
1185753d09bSNikita Kiryanov 	unsigned int wordlen;
119d68b9b84SÁlvaro Fernández Rojas 	unsigned int max_read_size;
1200c456ceeSSimon Glass 	unsigned int max_write_size;
121004f15b6SPoddar, Sourav 	void *memory_map;
122f77f4691SJagannadha Sutradharudu Teki 	u8 option;
123c40f6003SJagan Teki 
124f77f4691SJagannadha Sutradharudu Teki 	u8 flags;
12529ee0262SJagan Teki #define SPI_XFER_BEGIN		BIT(0)	/* Assert CS before transfer */
12629ee0262SJagan Teki #define SPI_XFER_END		BIT(1)	/* Deassert CS after transfer */
127c40f6003SJagan Teki #define SPI_XFER_ONCE		(SPI_XFER_BEGIN | SPI_XFER_END)
12829ee0262SJagan Teki #define SPI_XFER_MMAP		BIT(2)	/* Memory Mapped start */
12929ee0262SJagan Teki #define SPI_XFER_MMAP_END	BIT(3)	/* Memory Mapped End */
130f5a32af5SJon Lin #define SPI_XFER_PREPARE	BIT(7)	/* Transfer skip waiting idle */
131d255bb0eSHaavard Skinnemoen };
13277f85581Swdenk 
1331b1bd9a7SJagannadha Sutradharudu Teki /**
13477f85581Swdenk  * Initialization, must be called once on start up.
135d255bb0eSHaavard Skinnemoen  *
136d255bb0eSHaavard Skinnemoen  * TODO: I don't think we really need this.
13777f85581Swdenk  */
13877f85581Swdenk void spi_init(void);
13977f85581Swdenk 
140ba6c3ce9SSimon Glass /**
141ba6c3ce9SSimon Glass  * spi_do_alloc_slave - Allocate a new SPI slave (internal)
142ba6c3ce9SSimon Glass  *
143ba6c3ce9SSimon Glass  * Allocate and zero all fields in the spi slave, and set the bus/chip
144ba6c3ce9SSimon Glass  * select. Use the helper macro spi_alloc_slave() to call this.
145ba6c3ce9SSimon Glass  *
1461b1bd9a7SJagannadha Sutradharudu Teki  * @offset:	Offset of struct spi_slave within slave structure.
1471b1bd9a7SJagannadha Sutradharudu Teki  * @size:	Size of slave structure.
148ba6c3ce9SSimon Glass  * @bus:	Bus ID of the slave chip.
149ba6c3ce9SSimon Glass  * @cs:		Chip select ID of the slave chip on the specified bus.
150ba6c3ce9SSimon Glass  */
151ba6c3ce9SSimon Glass void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
152ba6c3ce9SSimon Glass 			 unsigned int cs);
153ba6c3ce9SSimon Glass 
154ba6c3ce9SSimon Glass /**
155ba6c3ce9SSimon Glass  * spi_alloc_slave - Allocate a new SPI slave
156ba6c3ce9SSimon Glass  *
157ba6c3ce9SSimon Glass  * Allocate and zero all fields in the spi slave, and set the bus/chip
158ba6c3ce9SSimon Glass  * select.
159ba6c3ce9SSimon Glass  *
1601b1bd9a7SJagannadha Sutradharudu Teki  * @_struct:	Name of structure to allocate (e.g. struct tegra_spi).
1611b1bd9a7SJagannadha Sutradharudu Teki  *		This structure must contain a member 'struct spi_slave *slave'.
162ba6c3ce9SSimon Glass  * @bus:	Bus ID of the slave chip.
163ba6c3ce9SSimon Glass  * @cs:		Chip select ID of the slave chip on the specified bus.
164ba6c3ce9SSimon Glass  */
165ba6c3ce9SSimon Glass #define spi_alloc_slave(_struct, bus, cs) \
166ba6c3ce9SSimon Glass 	spi_do_alloc_slave(offsetof(_struct, slave), \
167ba6c3ce9SSimon Glass 			    sizeof(_struct), bus, cs)
168ba6c3ce9SSimon Glass 
169ba6c3ce9SSimon Glass /**
170ba6c3ce9SSimon Glass  * spi_alloc_slave_base - Allocate a new SPI slave with no private data
171ba6c3ce9SSimon Glass  *
172ba6c3ce9SSimon Glass  * Allocate and zero all fields in the spi slave, and set the bus/chip
173ba6c3ce9SSimon Glass  * select.
174ba6c3ce9SSimon Glass  *
175ba6c3ce9SSimon Glass  * @bus:	Bus ID of the slave chip.
176ba6c3ce9SSimon Glass  * @cs:		Chip select ID of the slave chip on the specified bus.
177ba6c3ce9SSimon Glass  */
178ba6c3ce9SSimon Glass #define spi_alloc_slave_base(bus, cs) \
179ba6c3ce9SSimon Glass 	spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
180ba6c3ce9SSimon Glass 
1811b1bd9a7SJagannadha Sutradharudu Teki /**
182d255bb0eSHaavard Skinnemoen  * Set up communications parameters for a SPI slave.
183d255bb0eSHaavard Skinnemoen  *
184d255bb0eSHaavard Skinnemoen  * This must be called once for each slave. Note that this function
185d255bb0eSHaavard Skinnemoen  * usually doesn't touch any actual hardware, it only initializes the
186d255bb0eSHaavard Skinnemoen  * contents of spi_slave so that the hardware can be easily
187d255bb0eSHaavard Skinnemoen  * initialized later.
188d255bb0eSHaavard Skinnemoen  *
1891b1bd9a7SJagannadha Sutradharudu Teki  * @bus:	Bus ID of the slave chip.
1901b1bd9a7SJagannadha Sutradharudu Teki  * @cs:		Chip select ID of the slave chip on the specified bus.
1911b1bd9a7SJagannadha Sutradharudu Teki  * @max_hz:	Maximum SCK rate in Hz.
1921b1bd9a7SJagannadha Sutradharudu Teki  * @mode:	Clock polarity, clock phase and other parameters.
193d255bb0eSHaavard Skinnemoen  *
194d255bb0eSHaavard Skinnemoen  * Returns: A spi_slave reference that can be used in subsequent SPI
195d255bb0eSHaavard Skinnemoen  * calls, or NULL if one or more of the parameters are not supported.
196d255bb0eSHaavard Skinnemoen  */
197d255bb0eSHaavard Skinnemoen struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
198d255bb0eSHaavard Skinnemoen 		unsigned int max_hz, unsigned int mode);
199d255bb0eSHaavard Skinnemoen 
2001b1bd9a7SJagannadha Sutradharudu Teki /**
201d255bb0eSHaavard Skinnemoen  * Free any memory associated with a SPI slave.
202d255bb0eSHaavard Skinnemoen  *
2031b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
204d255bb0eSHaavard Skinnemoen  */
205d255bb0eSHaavard Skinnemoen void spi_free_slave(struct spi_slave *slave);
206d255bb0eSHaavard Skinnemoen 
2071b1bd9a7SJagannadha Sutradharudu Teki /**
208d255bb0eSHaavard Skinnemoen  * Claim the bus and prepare it for communication with a given slave.
209d255bb0eSHaavard Skinnemoen  *
210d255bb0eSHaavard Skinnemoen  * This must be called before doing any transfers with a SPI slave. It
211d255bb0eSHaavard Skinnemoen  * will enable and initialize any SPI hardware as necessary, and make
212d255bb0eSHaavard Skinnemoen  * sure that the SCK line is in the correct idle state. It is not
213d255bb0eSHaavard Skinnemoen  * allowed to claim the same bus for several slaves without releasing
214d255bb0eSHaavard Skinnemoen  * the bus in between.
215d255bb0eSHaavard Skinnemoen  *
2161b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
217d255bb0eSHaavard Skinnemoen  *
218d255bb0eSHaavard Skinnemoen  * Returns: 0 if the bus was claimed successfully, or a negative value
219d255bb0eSHaavard Skinnemoen  * if it wasn't.
220d255bb0eSHaavard Skinnemoen  */
221d255bb0eSHaavard Skinnemoen int spi_claim_bus(struct spi_slave *slave);
222d255bb0eSHaavard Skinnemoen 
2231b1bd9a7SJagannadha Sutradharudu Teki /**
224d255bb0eSHaavard Skinnemoen  * Release the SPI bus
225d255bb0eSHaavard Skinnemoen  *
226d255bb0eSHaavard Skinnemoen  * This must be called once for every call to spi_claim_bus() after
227d255bb0eSHaavard Skinnemoen  * all transfers have finished. It may disable any SPI hardware as
228d255bb0eSHaavard Skinnemoen  * appropriate.
229d255bb0eSHaavard Skinnemoen  *
2301b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
231d255bb0eSHaavard Skinnemoen  */
232d255bb0eSHaavard Skinnemoen void spi_release_bus(struct spi_slave *slave);
23377f85581Swdenk 
2341b1bd9a7SJagannadha Sutradharudu Teki /**
2355753d09bSNikita Kiryanov  * Set the word length for SPI transactions
2365753d09bSNikita Kiryanov  *
2375753d09bSNikita Kiryanov  * Set the word length (number of bits per word) for SPI transactions.
2385753d09bSNikita Kiryanov  *
2395753d09bSNikita Kiryanov  * @slave:	The SPI slave
2405753d09bSNikita Kiryanov  * @wordlen:	The number of bits in a word
2415753d09bSNikita Kiryanov  *
2425753d09bSNikita Kiryanov  * Returns: 0 on success, -1 on failure.
2435753d09bSNikita Kiryanov  */
2445753d09bSNikita Kiryanov int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
2455753d09bSNikita Kiryanov 
2465753d09bSNikita Kiryanov /**
247f75b9e50SSimon Glass  * SPI transfer (optional if mem_ops is used)
24877f85581Swdenk  *
24977f85581Swdenk  * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
25077f85581Swdenk  * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
25177f85581Swdenk  *
25277f85581Swdenk  * The source of the outgoing bits is the "dout" parameter and the
25377f85581Swdenk  * destination of the input bits is the "din" parameter.  Note that "dout"
25477f85581Swdenk  * and "din" can point to the same memory location, in which case the
25577f85581Swdenk  * input data overwrites the output data (since both are buffered by
25677f85581Swdenk  * temporary variables, this is OK).
25777f85581Swdenk  *
25877f85581Swdenk  * spi_xfer() interface:
2591b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave which will be sending/receiving the data.
2601b1bd9a7SJagannadha Sutradharudu Teki  * @bitlen:	How many bits to write and read.
2611b1bd9a7SJagannadha Sutradharudu Teki  * @dout:	Pointer to a string of bits to send out.  The bits are
26277f85581Swdenk  *		held in a byte array and are sent MSB first.
2631b1bd9a7SJagannadha Sutradharudu Teki  * @din:	Pointer to a string of bits that will be filled in.
2641b1bd9a7SJagannadha Sutradharudu Teki  * @flags:	A bitwise combination of SPI_XFER_* flags.
26577f85581Swdenk  *
26677f85581Swdenk  * Returns: 0 on success, not 0 on failure
26777f85581Swdenk  */
268d255bb0eSHaavard Skinnemoen int  spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
269d255bb0eSHaavard Skinnemoen 		void *din, unsigned long flags);
27077f85581Swdenk 
271a3171fa3SJagan Teki /**
272a3171fa3SJagan Teki  * spi_write_then_read - SPI synchronous write followed by read
273a3171fa3SJagan Teki  *
274a3171fa3SJagan Teki  * This performs a half duplex transaction in which the first transaction
275a3171fa3SJagan Teki  * is to send the opcode and if the length of buf is non-zero then it start
276a3171fa3SJagan Teki  * the second transaction as tx or rx based on the need from respective slave.
277a3171fa3SJagan Teki  *
278a3171fa3SJagan Teki  * @slave:	The SPI slave device with which opcode/data will be exchanged
279a3171fa3SJagan Teki  * @opcode:	opcode used for specific transfer
280a3171fa3SJagan Teki  * @n_opcode:	size of opcode, in bytes
281a3171fa3SJagan Teki  * @txbuf:	buffer into which data to be written
282a3171fa3SJagan Teki  * @rxbuf:	buffer into which data will be read
283a3171fa3SJagan Teki  * @n_buf:	size of buf (whether it's [tx|rx]buf), in bytes
284a3171fa3SJagan Teki  *
285a3171fa3SJagan Teki  * Returns: 0 on success, not 0 on failure
286a3171fa3SJagan Teki  */
287a3171fa3SJagan Teki int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
288a3171fa3SJagan Teki 			size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
289a3171fa3SJagan Teki 			size_t n_buf);
290a3171fa3SJagan Teki 
291146bad96STom Rini /* Copy memory mapped data */
292146bad96STom Rini void spi_flash_copy_mmap(void *data, void *offset, size_t len);
293146bad96STom Rini 
2941b1bd9a7SJagannadha Sutradharudu Teki /**
295d255bb0eSHaavard Skinnemoen  * Determine if a SPI chipselect is valid.
296d255bb0eSHaavard Skinnemoen  * This function is provided by the board if the low-level SPI driver
297d255bb0eSHaavard Skinnemoen  * needs it to determine if a given chipselect is actually valid.
298d255bb0eSHaavard Skinnemoen  *
299d255bb0eSHaavard Skinnemoen  * Returns: 1 if bus:cs identifies a valid chip on this board, 0
300d255bb0eSHaavard Skinnemoen  * otherwise.
301d255bb0eSHaavard Skinnemoen  */
302d255bb0eSHaavard Skinnemoen int spi_cs_is_valid(unsigned int bus, unsigned int cs);
303d255bb0eSHaavard Skinnemoen 
304d7af6a48SSimon Glass #ifndef CONFIG_DM_SPI
3051b1bd9a7SJagannadha Sutradharudu Teki /**
306d255bb0eSHaavard Skinnemoen  * Activate a SPI chipselect.
307d255bb0eSHaavard Skinnemoen  * This function is provided by the board code when using a driver
308d255bb0eSHaavard Skinnemoen  * that can't control its chipselects automatically (e.g.
309d255bb0eSHaavard Skinnemoen  * common/soft_spi.c). When called, it should activate the chip select
310d255bb0eSHaavard Skinnemoen  * to the device identified by "slave".
311d255bb0eSHaavard Skinnemoen  */
312d255bb0eSHaavard Skinnemoen void spi_cs_activate(struct spi_slave *slave);
313d255bb0eSHaavard Skinnemoen 
3141b1bd9a7SJagannadha Sutradharudu Teki /**
315d255bb0eSHaavard Skinnemoen  * Deactivate a SPI chipselect.
316d255bb0eSHaavard Skinnemoen  * This function is provided by the board code when using a driver
317d255bb0eSHaavard Skinnemoen  * that can't control its chipselects automatically (e.g.
318d255bb0eSHaavard Skinnemoen  * common/soft_spi.c). When called, it should deactivate the chip
319d255bb0eSHaavard Skinnemoen  * select to the device identified by "slave".
320d255bb0eSHaavard Skinnemoen  */
321d255bb0eSHaavard Skinnemoen void spi_cs_deactivate(struct spi_slave *slave);
322d255bb0eSHaavard Skinnemoen 
3231b1bd9a7SJagannadha Sutradharudu Teki /**
324fa1423e7SThomas Chou  * Set transfer speed.
325fa1423e7SThomas Chou  * This sets a new speed to be applied for next spi_xfer().
3261b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
3271b1bd9a7SJagannadha Sutradharudu Teki  * @hz:		The transfer speed
328fa1423e7SThomas Chou  */
329fa1423e7SThomas Chou void spi_set_speed(struct spi_slave *slave, uint hz);
330d7af6a48SSimon Glass #endif
331fa1423e7SThomas Chou 
3321b1bd9a7SJagannadha Sutradharudu Teki /**
333d255bb0eSHaavard Skinnemoen  * Write 8 bits, then read 8 bits.
3341b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave we're communicating with
3351b1bd9a7SJagannadha Sutradharudu Teki  * @byte:	Byte to be written
336d255bb0eSHaavard Skinnemoen  *
337d255bb0eSHaavard Skinnemoen  * Returns: The value that was read, or a negative value on error.
338d255bb0eSHaavard Skinnemoen  *
339d255bb0eSHaavard Skinnemoen  * TODO: This function probably shouldn't be inlined.
340d255bb0eSHaavard Skinnemoen  */
341d255bb0eSHaavard Skinnemoen static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
342d255bb0eSHaavard Skinnemoen {
343d255bb0eSHaavard Skinnemoen 	unsigned char dout[2];
344d255bb0eSHaavard Skinnemoen 	unsigned char din[2];
345d255bb0eSHaavard Skinnemoen 	int ret;
346d255bb0eSHaavard Skinnemoen 
347d255bb0eSHaavard Skinnemoen 	dout[0] = byte;
348d255bb0eSHaavard Skinnemoen 	dout[1] = 0;
349d255bb0eSHaavard Skinnemoen 
350d255bb0eSHaavard Skinnemoen 	ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
351d255bb0eSHaavard Skinnemoen 	return ret < 0 ? ret : din[1];
352d255bb0eSHaavard Skinnemoen }
35338254f45SGuennadi Liakhovetski 
354d7af6a48SSimon Glass #ifdef CONFIG_DM_SPI
355d7af6a48SSimon Glass 
356d7af6a48SSimon Glass /**
357d7af6a48SSimon Glass  * struct spi_cs_info - Information about a bus chip select
358d7af6a48SSimon Glass  *
359d7af6a48SSimon Glass  * @dev:	Connected device, or NULL if none
360d7af6a48SSimon Glass  */
361d7af6a48SSimon Glass struct spi_cs_info {
362d7af6a48SSimon Glass 	struct udevice *dev;
363d7af6a48SSimon Glass };
364d7af6a48SSimon Glass 
365d7af6a48SSimon Glass /**
366d7af6a48SSimon Glass  * struct struct dm_spi_ops - Driver model SPI operations
367d7af6a48SSimon Glass  *
368d7af6a48SSimon Glass  * The uclass interface is implemented by all SPI devices which use
369d7af6a48SSimon Glass  * driver model.
370d7af6a48SSimon Glass  */
371d7af6a48SSimon Glass struct dm_spi_ops {
372d7af6a48SSimon Glass 	/**
373d7af6a48SSimon Glass 	 * Claim the bus and prepare it for communication.
374d7af6a48SSimon Glass 	 *
375d7af6a48SSimon Glass 	 * The device provided is the slave device. It's parent controller
376d7af6a48SSimon Glass 	 * will be used to provide the communication.
377d7af6a48SSimon Glass 	 *
378d7af6a48SSimon Glass 	 * This must be called before doing any transfers with a SPI slave. It
379d7af6a48SSimon Glass 	 * will enable and initialize any SPI hardware as necessary, and make
380d7af6a48SSimon Glass 	 * sure that the SCK line is in the correct idle state. It is not
381d7af6a48SSimon Glass 	 * allowed to claim the same bus for several slaves without releasing
382d7af6a48SSimon Glass 	 * the bus in between.
383d7af6a48SSimon Glass 	 *
3849694b724SSimon Glass 	 * @dev:	The SPI slave
385d7af6a48SSimon Glass 	 *
386d7af6a48SSimon Glass 	 * Returns: 0 if the bus was claimed successfully, or a negative value
387d7af6a48SSimon Glass 	 * if it wasn't.
388d7af6a48SSimon Glass 	 */
3899694b724SSimon Glass 	int (*claim_bus)(struct udevice *dev);
390d7af6a48SSimon Glass 
391d7af6a48SSimon Glass 	/**
392d7af6a48SSimon Glass 	 * Release the SPI bus
393d7af6a48SSimon Glass 	 *
394d7af6a48SSimon Glass 	 * This must be called once for every call to spi_claim_bus() after
395d7af6a48SSimon Glass 	 * all transfers have finished. It may disable any SPI hardware as
396d7af6a48SSimon Glass 	 * appropriate.
397d7af6a48SSimon Glass 	 *
3989694b724SSimon Glass 	 * @dev:	The SPI slave
399d7af6a48SSimon Glass 	 */
4009694b724SSimon Glass 	int (*release_bus)(struct udevice *dev);
401d7af6a48SSimon Glass 
402d7af6a48SSimon Glass 	/**
403d7af6a48SSimon Glass 	 * Set the word length for SPI transactions
404d7af6a48SSimon Glass 	 *
405d7af6a48SSimon Glass 	 * Set the word length (number of bits per word) for SPI transactions.
406d7af6a48SSimon Glass 	 *
407d7af6a48SSimon Glass 	 * @bus:	The SPI slave
408d7af6a48SSimon Glass 	 * @wordlen:	The number of bits in a word
409d7af6a48SSimon Glass 	 *
410d7af6a48SSimon Glass 	 * Returns: 0 on success, -ve on failure.
411d7af6a48SSimon Glass 	 */
4129694b724SSimon Glass 	int (*set_wordlen)(struct udevice *dev, unsigned int wordlen);
413d7af6a48SSimon Glass 
414d7af6a48SSimon Glass 	/**
415d7af6a48SSimon Glass 	 * SPI transfer
416d7af6a48SSimon Glass 	 *
417d7af6a48SSimon Glass 	 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
418d7af6a48SSimon Glass 	 * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
419d7af6a48SSimon Glass 	 * works.
420d7af6a48SSimon Glass 	 *
421d7af6a48SSimon Glass 	 * The source of the outgoing bits is the "dout" parameter and the
422d7af6a48SSimon Glass 	 * destination of the input bits is the "din" parameter.  Note that
423d7af6a48SSimon Glass 	 * "dout" and "din" can point to the same memory location, in which
424d7af6a48SSimon Glass 	 * case the input data overwrites the output data (since both are
425d7af6a48SSimon Glass 	 * buffered by temporary variables, this is OK).
426d7af6a48SSimon Glass 	 *
427d7af6a48SSimon Glass 	 * spi_xfer() interface:
428d7af6a48SSimon Glass 	 * @dev:	The slave device to communicate with
429d7af6a48SSimon Glass 	 * @bitlen:	How many bits to write and read.
430d7af6a48SSimon Glass 	 * @dout:	Pointer to a string of bits to send out.  The bits are
431d7af6a48SSimon Glass 	 *		held in a byte array and are sent MSB first.
432d7af6a48SSimon Glass 	 * @din:	Pointer to a string of bits that will be filled in.
433d7af6a48SSimon Glass 	 * @flags:	A bitwise combination of SPI_XFER_* flags.
434d7af6a48SSimon Glass 	 *
435d7af6a48SSimon Glass 	 * Returns: 0 on success, not -1 on failure
436d7af6a48SSimon Glass 	 */
437d7af6a48SSimon Glass 	int (*xfer)(struct udevice *dev, unsigned int bitlen, const void *dout,
438d7af6a48SSimon Glass 		    void *din, unsigned long flags);
439d7af6a48SSimon Glass 
440d7af6a48SSimon Glass 	/**
441967efcaeSBoris Brezillon 	 * Optimized handlers for SPI memory-like operations.
442967efcaeSBoris Brezillon 	 *
443967efcaeSBoris Brezillon 	 * Optimized/dedicated operations for interactions with SPI memory. This
444967efcaeSBoris Brezillon 	 * field is optional and should only be implemented if the controller
445967efcaeSBoris Brezillon 	 * has native support for memory like operations.
446967efcaeSBoris Brezillon 	 */
447967efcaeSBoris Brezillon 	const struct spi_controller_mem_ops *mem_ops;
448967efcaeSBoris Brezillon 
449967efcaeSBoris Brezillon 	/**
450d7af6a48SSimon Glass 	 * Set transfer speed.
451d7af6a48SSimon Glass 	 * This sets a new speed to be applied for next spi_xfer().
452d7af6a48SSimon Glass 	 * @bus:	The SPI bus
453d7af6a48SSimon Glass 	 * @hz:		The transfer speed
454d7af6a48SSimon Glass 	 * @return 0 if OK, -ve on error
455d7af6a48SSimon Glass 	 */
456d7af6a48SSimon Glass 	int (*set_speed)(struct udevice *bus, uint hz);
457d7af6a48SSimon Glass 
458d7af6a48SSimon Glass 	/**
459d7af6a48SSimon Glass 	 * Set the SPI mode/flags
460d7af6a48SSimon Glass 	 *
461d7af6a48SSimon Glass 	 * It is unclear if we want to set speed and mode together instead
462d7af6a48SSimon Glass 	 * of separately.
463d7af6a48SSimon Glass 	 *
464d7af6a48SSimon Glass 	 * @bus:	The SPI bus
465d7af6a48SSimon Glass 	 * @mode:	Requested SPI mode (SPI_... flags)
466d7af6a48SSimon Glass 	 * @return 0 if OK, -ve on error
467d7af6a48SSimon Glass 	 */
468d7af6a48SSimon Glass 	int (*set_mode)(struct udevice *bus, uint mode);
469d7af6a48SSimon Glass 
470d7af6a48SSimon Glass 	/**
471d7af6a48SSimon Glass 	 * Get information on a chip select
472d7af6a48SSimon Glass 	 *
473d7af6a48SSimon Glass 	 * This is only called when the SPI uclass does not know about a
474d7af6a48SSimon Glass 	 * chip select, i.e. it has no attached device. It gives the driver
475d7af6a48SSimon Glass 	 * a chance to allow activity on that chip select even so.
476d7af6a48SSimon Glass 	 *
477d7af6a48SSimon Glass 	 * @bus:	The SPI bus
478d7af6a48SSimon Glass 	 * @cs:		The chip select (0..n-1)
479d7af6a48SSimon Glass 	 * @info:	Returns information about the chip select, if valid.
480d7af6a48SSimon Glass 	 *		On entry info->dev is NULL
481d7af6a48SSimon Glass 	 * @return 0 if OK (and @info is set up), -ENODEV if the chip select
482d7af6a48SSimon Glass 	 *	   is invalid, other -ve value on error
483d7af6a48SSimon Glass 	 */
484d7af6a48SSimon Glass 	int (*cs_info)(struct udevice *bus, uint cs, struct spi_cs_info *info);
4857ac3b0edSSimon Glass 
4867ac3b0edSSimon Glass 	/**
4877ac3b0edSSimon Glass 	 * get_mmap() - Get memory-mapped SPI
4887ac3b0edSSimon Glass 	 *
4897ac3b0edSSimon Glass 	 * @dev:	The SPI flash slave device
4907ac3b0edSSimon Glass 	 * @map_basep:	Returns base memory address for mapped SPI
4917ac3b0edSSimon Glass 	 * @map_sizep:	Returns size of mapped SPI
4927ac3b0edSSimon Glass 	 * @offsetp:	Returns start offset of SPI flash where the map works
4937ac3b0edSSimon Glass 	 *	correctly (offsets before this are not visible)
4947ac3b0edSSimon Glass 	 * @return 0 if OK, -EFAULT if memory mapping is not available
4957ac3b0edSSimon Glass 	 */
4967ac3b0edSSimon Glass 	int (*get_mmap)(struct udevice *dev, ulong *map_basep,
4977ac3b0edSSimon Glass 			uint *map_sizep, uint *offsetp);
498d7af6a48SSimon Glass };
499d7af6a48SSimon Glass 
500c60e1f25SSimon Glass struct dm_spi_emul_ops {
501c60e1f25SSimon Glass 	/**
502c60e1f25SSimon Glass 	 * SPI transfer
503c60e1f25SSimon Glass 	 *
504c60e1f25SSimon Glass 	 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
505c60e1f25SSimon Glass 	 * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
506c60e1f25SSimon Glass 	 * works. Here the device is a slave.
507c60e1f25SSimon Glass 	 *
508c60e1f25SSimon Glass 	 * The source of the outgoing bits is the "dout" parameter and the
509c60e1f25SSimon Glass 	 * destination of the input bits is the "din" parameter.  Note that
510c60e1f25SSimon Glass 	 * "dout" and "din" can point to the same memory location, in which
511c60e1f25SSimon Glass 	 * case the input data overwrites the output data (since both are
512c60e1f25SSimon Glass 	 * buffered by temporary variables, this is OK).
513c60e1f25SSimon Glass 	 *
514c60e1f25SSimon Glass 	 * spi_xfer() interface:
515c60e1f25SSimon Glass 	 * @slave:	The SPI slave which will be sending/receiving the data.
516c60e1f25SSimon Glass 	 * @bitlen:	How many bits to write and read.
517c60e1f25SSimon Glass 	 * @dout:	Pointer to a string of bits sent to the device. The
518c60e1f25SSimon Glass 	 *		bits are held in a byte array and are sent MSB first.
519c60e1f25SSimon Glass 	 * @din:	Pointer to a string of bits that will be sent back to
520c60e1f25SSimon Glass 	 *		the master.
521c60e1f25SSimon Glass 	 * @flags:	A bitwise combination of SPI_XFER_* flags.
522c60e1f25SSimon Glass 	 *
523c60e1f25SSimon Glass 	 * Returns: 0 on success, not -1 on failure
524c60e1f25SSimon Glass 	 */
525c60e1f25SSimon Glass 	int (*xfer)(struct udevice *slave, unsigned int bitlen,
526c60e1f25SSimon Glass 		    const void *dout, void *din, unsigned long flags);
527c60e1f25SSimon Glass };
528c60e1f25SSimon Glass 
529d7af6a48SSimon Glass /**
530d7af6a48SSimon Glass  * spi_find_bus_and_cs() - Find bus and slave devices by number
531d7af6a48SSimon Glass  *
532d7af6a48SSimon Glass  * Given a bus number and chip select, this finds the corresponding bus
533d7af6a48SSimon Glass  * device and slave device. Neither device is activated by this function,
534d7af6a48SSimon Glass  * although they may have been activated previously.
535d7af6a48SSimon Glass  *
536d7af6a48SSimon Glass  * @busnum:	SPI bus number
537d7af6a48SSimon Glass  * @cs:		Chip select to look for
538d7af6a48SSimon Glass  * @busp:	Returns bus device
539d7af6a48SSimon Glass  * @devp:	Return slave device
540d7af6a48SSimon Glass  * @return 0 if found, -ENODEV on error
541d7af6a48SSimon Glass  */
542d7af6a48SSimon Glass int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
543d7af6a48SSimon Glass 			struct udevice **devp);
544d7af6a48SSimon Glass 
545d7af6a48SSimon Glass /**
546d7af6a48SSimon Glass  * spi_get_bus_and_cs() - Find and activate bus and slave devices by number
547d7af6a48SSimon Glass  *
548d7af6a48SSimon Glass  * Given a bus number and chip select, this finds the corresponding bus
549d7af6a48SSimon Glass  * device and slave device.
550d7af6a48SSimon Glass  *
551d7af6a48SSimon Glass  * If no such slave exists, and drv_name is not NULL, then a new slave device
552ee9b3572SPatrick Delaunay  * is automatically bound on this chip select with requested speed and mode.
553d7af6a48SSimon Glass  *
554ee9b3572SPatrick Delaunay  * Ths new slave device is probed ready for use with the speed and mode
555ee9b3572SPatrick Delaunay  * from platdata when available or the requested values.
556d7af6a48SSimon Glass  *
557d7af6a48SSimon Glass  * @busnum:	SPI bus number
558d7af6a48SSimon Glass  * @cs:		Chip select to look for
559ee9b3572SPatrick Delaunay  * @speed:	SPI speed to use for this slave when not available in platdata
560ee9b3572SPatrick Delaunay  * @mode:	SPI mode to use for this slave when not available in platdata
561d7af6a48SSimon Glass  * @drv_name:	Name of driver to attach to this chip select
562d7af6a48SSimon Glass  * @dev_name:	Name of the new device thus created
563d7af6a48SSimon Glass  * @busp:	Returns bus device
564d7af6a48SSimon Glass  * @devp:	Return slave device
565d7af6a48SSimon Glass  * @return 0 if found, -ve on error
566d7af6a48SSimon Glass  */
567d7af6a48SSimon Glass int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
568d7af6a48SSimon Glass 			const char *drv_name, const char *dev_name,
569d7af6a48SSimon Glass 			struct udevice **busp, struct spi_slave **devp);
570d7af6a48SSimon Glass 
571d7af6a48SSimon Glass /**
572d7af6a48SSimon Glass  * spi_chip_select() - Get the chip select for a slave
573d7af6a48SSimon Glass  *
574d7af6a48SSimon Glass  * @return the chip select this slave is attached to
575d7af6a48SSimon Glass  */
576d7af6a48SSimon Glass int spi_chip_select(struct udevice *slave);
577d7af6a48SSimon Glass 
578d7af6a48SSimon Glass /**
579ff56bba2SSimon Glass  * spi_find_chip_select() - Find the slave attached to chip select
580ff56bba2SSimon Glass  *
581ff56bba2SSimon Glass  * @bus:	SPI bus to search
582ff56bba2SSimon Glass  * @cs:		Chip select to look for
583ff56bba2SSimon Glass  * @devp:	Returns the slave device if found
5845a1c4376SBin Meng  * @return 0 if found, -EINVAL if cs is invalid, -ENODEV if no device attached,
5855a1c4376SBin Meng  *	   other -ve value on error
586ff56bba2SSimon Glass  */
587ff56bba2SSimon Glass int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp);
588ff56bba2SSimon Glass 
589ff56bba2SSimon Glass /**
590d0cff03eSSimon Glass  * spi_slave_ofdata_to_platdata() - decode standard SPI platform data
591d7af6a48SSimon Glass  *
592d0cff03eSSimon Glass  * This decodes the speed and mode for a slave from a device tree node
593d7af6a48SSimon Glass  *
594d7af6a48SSimon Glass  * @blob:	Device tree blob
595d7af6a48SSimon Glass  * @node:	Node offset to read from
596d0cff03eSSimon Glass  * @plat:	Place to put the decoded information
597d7af6a48SSimon Glass  */
598279e26f5SSimon Glass int spi_slave_ofdata_to_platdata(struct udevice *dev,
599d0cff03eSSimon Glass 				 struct dm_spi_slave_platdata *plat);
600d7af6a48SSimon Glass 
601d7af6a48SSimon Glass /**
602d7af6a48SSimon Glass  * spi_cs_info() - Check information on a chip select
603d7af6a48SSimon Glass  *
604d7af6a48SSimon Glass  * This checks a particular chip select on a bus to see if it has a device
605d7af6a48SSimon Glass  * attached, or is even valid.
606d7af6a48SSimon Glass  *
607d7af6a48SSimon Glass  * @bus:	The SPI bus
608d7af6a48SSimon Glass  * @cs:		The chip select (0..n-1)
609d7af6a48SSimon Glass  * @info:	Returns information about the chip select, if valid
610d7af6a48SSimon Glass  * @return 0 if OK (and @info is set up), -ENODEV if the chip select
611d7af6a48SSimon Glass  *	   is invalid, other -ve value on error
612d7af6a48SSimon Glass  */
613d7af6a48SSimon Glass int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info);
614d7af6a48SSimon Glass 
615d7af6a48SSimon Glass struct sandbox_state;
616c60e1f25SSimon Glass 
617c60e1f25SSimon Glass /**
618c60e1f25SSimon Glass  * sandbox_spi_get_emul() - get an emulator for a SPI slave
619c60e1f25SSimon Glass  *
620c60e1f25SSimon Glass  * This provides a way to attach an emulated SPI device to a particular SPI
621c60e1f25SSimon Glass  * slave, so that xfer() operations on the slave will be handled by the
622c60e1f25SSimon Glass  * emulator. If a emulator already exists on that chip select it is returned.
623c60e1f25SSimon Glass  * Otherwise one is created.
624c60e1f25SSimon Glass  *
625c60e1f25SSimon Glass  * @state:	Sandbox state
626c60e1f25SSimon Glass  * @bus:	SPI bus requesting the emulator
627c60e1f25SSimon Glass  * @slave:	SPI slave device requesting the emulator
628c60e1f25SSimon Glass  * @emuip:	Returns pointer to emulator
629c60e1f25SSimon Glass  * @return 0 if OK, -ve on error
630c60e1f25SSimon Glass  */
631d7af6a48SSimon Glass int sandbox_spi_get_emul(struct sandbox_state *state,
632d7af6a48SSimon Glass 			 struct udevice *bus, struct udevice *slave,
633d7af6a48SSimon Glass 			 struct udevice **emulp);
634d7af6a48SSimon Glass 
6357a3eff4cSPeng Fan /**
6367a3eff4cSPeng Fan  * Claim the bus and prepare it for communication with a given slave.
6377a3eff4cSPeng Fan  *
6387a3eff4cSPeng Fan  * This must be called before doing any transfers with a SPI slave. It
6397a3eff4cSPeng Fan  * will enable and initialize any SPI hardware as necessary, and make
6407a3eff4cSPeng Fan  * sure that the SCK line is in the correct idle state. It is not
6417a3eff4cSPeng Fan  * allowed to claim the same bus for several slaves without releasing
6427a3eff4cSPeng Fan  * the bus in between.
6437a3eff4cSPeng Fan  *
6447a3eff4cSPeng Fan  * @dev:	The SPI slave device
6457a3eff4cSPeng Fan  *
6467a3eff4cSPeng Fan  * Returns: 0 if the bus was claimed successfully, or a negative value
6477a3eff4cSPeng Fan  * if it wasn't.
6487a3eff4cSPeng Fan  */
6497a3eff4cSPeng Fan int dm_spi_claim_bus(struct udevice *dev);
6507a3eff4cSPeng Fan 
6517a3eff4cSPeng Fan /**
6527a3eff4cSPeng Fan  * Release the SPI bus
6537a3eff4cSPeng Fan  *
6547a3eff4cSPeng Fan  * This must be called once for every call to dm_spi_claim_bus() after
6557a3eff4cSPeng Fan  * all transfers have finished. It may disable any SPI hardware as
6567a3eff4cSPeng Fan  * appropriate.
6577a3eff4cSPeng Fan  *
6587a3eff4cSPeng Fan  * @slave:	The SPI slave device
6597a3eff4cSPeng Fan  */
6607a3eff4cSPeng Fan void dm_spi_release_bus(struct udevice *dev);
6617a3eff4cSPeng Fan 
6627a3eff4cSPeng Fan /**
6637a3eff4cSPeng Fan  * SPI transfer
6647a3eff4cSPeng Fan  *
6657a3eff4cSPeng Fan  * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
6667a3eff4cSPeng Fan  * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
6677a3eff4cSPeng Fan  *
6687a3eff4cSPeng Fan  * The source of the outgoing bits is the "dout" parameter and the
6697a3eff4cSPeng Fan  * destination of the input bits is the "din" parameter.  Note that "dout"
6707a3eff4cSPeng Fan  * and "din" can point to the same memory location, in which case the
6717a3eff4cSPeng Fan  * input data overwrites the output data (since both are buffered by
6727a3eff4cSPeng Fan  * temporary variables, this is OK).
6737a3eff4cSPeng Fan  *
6747a3eff4cSPeng Fan  * dm_spi_xfer() interface:
6757a3eff4cSPeng Fan  * @dev:	The SPI slave device which will be sending/receiving the data.
6767a3eff4cSPeng Fan  * @bitlen:	How many bits to write and read.
6777a3eff4cSPeng Fan  * @dout:	Pointer to a string of bits to send out.  The bits are
6787a3eff4cSPeng Fan  *		held in a byte array and are sent MSB first.
6797a3eff4cSPeng Fan  * @din:	Pointer to a string of bits that will be filled in.
6807a3eff4cSPeng Fan  * @flags:	A bitwise combination of SPI_XFER_* flags.
6817a3eff4cSPeng Fan  *
6827a3eff4cSPeng Fan  * Returns: 0 on success, not 0 on failure
6837a3eff4cSPeng Fan  */
6847a3eff4cSPeng Fan int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
6857a3eff4cSPeng Fan 		const void *dout, void *din, unsigned long flags);
6867a3eff4cSPeng Fan 
6877ac3b0edSSimon Glass /**
6887ac3b0edSSimon Glass  * spi_get_mmap() - Get memory-mapped SPI
6897ac3b0edSSimon Glass  *
6907ac3b0edSSimon Glass  * @dev:	SPI slave device to check
6917ac3b0edSSimon Glass  * @map_basep:	Returns base memory address for mapped SPI
6927ac3b0edSSimon Glass  * @map_sizep:	Returns size of mapped SPI
6937ac3b0edSSimon Glass  * @offsetp:	Returns start offset of SPI flash where the map works
6947ac3b0edSSimon Glass  *	correctly (offsets before this are not visible)
6957ac3b0edSSimon Glass  * @return 0 if OK, -ENOSYS if no operation, -EFAULT if memory mapping is not
6967ac3b0edSSimon Glass  *	available
6977ac3b0edSSimon Glass  */
6987ac3b0edSSimon Glass int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
6997ac3b0edSSimon Glass 		    uint *offsetp);
7007ac3b0edSSimon Glass 
701bc5701e1SSimon Glass /* Access the operations for a SPI device */
702d7af6a48SSimon Glass #define spi_get_ops(dev)	((struct dm_spi_ops *)(dev)->driver->ops)
703c60e1f25SSimon Glass #define spi_emul_get_ops(dev)	((struct dm_spi_emul_ops *)(dev)->driver->ops)
704d7af6a48SSimon Glass #endif /* CONFIG_DM_SPI */
705d7af6a48SSimon Glass 
70677f85581Swdenk #endif	/* _SPI_H_ */
707