xref: /rk3399_rockchip-uboot/include/spi.h (revision f5a32af5a3fe92cc0edf80513ab0c6a6fb6cf575)
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 */
34*f5a32af5SJon Lin #define SPI_DMA_PREPARE	BIT(24)			/* dma transfer skip waiting idle */
354e09cc1eSJagannadha Sutradharudu Teki 
36bb786b84SRajeshwari Shinde /* Header byte that marks the start of the message */
37bb786b84SRajeshwari Shinde #define SPI_PREAMBLE_END_BYTE	0xec
38bb786b84SRajeshwari Shinde 
395753d09bSNikita Kiryanov #define SPI_DEFAULT_WORDLEN	8
405753d09bSNikita Kiryanov 
41d7af6a48SSimon Glass #ifdef CONFIG_DM_SPI
42d0cff03eSSimon Glass /* TODO(sjg@chromium.org): Remove this and use max_hz from struct spi_slave */
43d7af6a48SSimon Glass struct dm_spi_bus {
44d7af6a48SSimon Glass 	uint max_hz;
45d7af6a48SSimon Glass };
46d7af6a48SSimon Glass 
47d0cff03eSSimon Glass /**
48d0cff03eSSimon Glass  * struct dm_spi_platdata - platform data for all SPI slaves
49d0cff03eSSimon Glass  *
50d0cff03eSSimon Glass  * This describes a SPI slave, a child device of the SPI bus. To obtain this
51d0cff03eSSimon Glass  * struct from a spi_slave, use dev_get_parent_platdata(dev) or
52d0cff03eSSimon Glass  * dev_get_parent_platdata(slave->dev).
53d0cff03eSSimon Glass  *
54d0cff03eSSimon Glass  * This data is immuatable. Each time the device is probed, @max_hz and @mode
55d0cff03eSSimon Glass  * will be copied to struct spi_slave.
56d0cff03eSSimon Glass  *
57d0cff03eSSimon Glass  * @cs:		Chip select number (0..n-1)
58d0cff03eSSimon Glass  * @max_hz:	Maximum bus speed that this slave can tolerate
59d0cff03eSSimon Glass  * @mode:	SPI mode to use for this device (see SPI mode flags)
60d0cff03eSSimon Glass  */
61d0cff03eSSimon Glass struct dm_spi_slave_platdata {
62d0cff03eSSimon Glass 	unsigned int cs;
63d0cff03eSSimon Glass 	uint max_hz;
64d0cff03eSSimon Glass 	uint mode;
65d0cff03eSSimon Glass };
66d0cff03eSSimon Glass 
67d7af6a48SSimon Glass #endif /* CONFIG_DM_SPI */
68d7af6a48SSimon Glass 
691b1bd9a7SJagannadha Sutradharudu Teki /**
70ce22b922SJagannadha Sutradharudu Teki  * struct spi_slave - Representation of a SPI slave
71d255bb0eSHaavard Skinnemoen  *
72d7af6a48SSimon Glass  * For driver model this is the per-child data used by the SPI bus. It can
73bcbe3d15SSimon Glass  * be accessed using dev_get_parent_priv() on the slave device. The SPI uclass
74d0cff03eSSimon Glass  * sets uip per_child_auto_alloc_size to sizeof(struct spi_slave), and the
75d0cff03eSSimon Glass  * driver should not override it. Two platform data fields (max_hz and mode)
76d0cff03eSSimon Glass  * are copied into this structure to provide an initial value. This allows
77d0cff03eSSimon Glass  * them to be changed, since we should never change platform data in drivers.
78d7af6a48SSimon Glass  *
79d7af6a48SSimon Glass  * If not using driver model, drivers are expected to extend this with
80d7af6a48SSimon Glass  * controller-specific data.
81d7af6a48SSimon Glass  *
82d7af6a48SSimon Glass  * @dev:		SPI slave device
83d7af6a48SSimon Glass  * @max_hz:		Maximum speed for this slave
8460e2809aSSimon Glass  * @speed:		Current bus speed. This is 0 until the bus is first
8560e2809aSSimon Glass  *			claimed.
86d7af6a48SSimon Glass  * @bus:		ID of the bus that the slave is attached to. For
87d7af6a48SSimon Glass  *			driver model this is the sequence number of the SPI
88d7af6a48SSimon Glass  *			bus (bus->seq) so does not need to be stored
89ce22b922SJagannadha Sutradharudu Teki  * @cs:			ID of the chip select connected to the slave.
90f5c3c033SJagan Teki  * @mode:		SPI mode to use for this slave (see SPI mode flags)
915753d09bSNikita Kiryanov  * @wordlen:		Size of SPI word in number of bits
92d68b9b84SÁlvaro Fernández Rojas  * @max_read_size:	If non-zero, the maximum number of bytes which can
93d68b9b84SÁlvaro Fernández Rojas  *			be read at once.
94ce22b922SJagannadha Sutradharudu Teki  * @max_write_size:	If non-zero, the maximum number of bytes which can
95d2a88c91SÁlvaro Fernández Rojas  *			be written at once.
96ce22b922SJagannadha Sutradharudu Teki  * @memory_map:		Address of read-only SPI flash access.
97f77f4691SJagannadha Sutradharudu Teki  * @flags:		Indication of SPI flags.
98d255bb0eSHaavard Skinnemoen  */
99d255bb0eSHaavard Skinnemoen struct spi_slave {
100d7af6a48SSimon Glass #ifdef CONFIG_DM_SPI
101d7af6a48SSimon Glass 	struct udevice *dev;	/* struct spi_slave is dev->parentdata */
102d7af6a48SSimon Glass 	uint max_hz;
10360e2809aSSimon Glass 	uint speed;
104d7af6a48SSimon Glass #else
105d255bb0eSHaavard Skinnemoen 	unsigned int bus;
106d255bb0eSHaavard Skinnemoen 	unsigned int cs;
107d0cff03eSSimon Glass #endif
108f5c3c033SJagan Teki 	uint mode;
1095753d09bSNikita Kiryanov 	unsigned int wordlen;
110d68b9b84SÁlvaro Fernández Rojas 	unsigned int max_read_size;
1110c456ceeSSimon Glass 	unsigned int max_write_size;
112004f15b6SPoddar, Sourav 	void *memory_map;
113f77f4691SJagannadha Sutradharudu Teki 	u8 option;
114c40f6003SJagan Teki 
115f77f4691SJagannadha Sutradharudu Teki 	u8 flags;
11629ee0262SJagan Teki #define SPI_XFER_BEGIN		BIT(0)	/* Assert CS before transfer */
11729ee0262SJagan Teki #define SPI_XFER_END		BIT(1)	/* Deassert CS after transfer */
118c40f6003SJagan Teki #define SPI_XFER_ONCE		(SPI_XFER_BEGIN | SPI_XFER_END)
11929ee0262SJagan Teki #define SPI_XFER_MMAP		BIT(2)	/* Memory Mapped start */
12029ee0262SJagan Teki #define SPI_XFER_MMAP_END	BIT(3)	/* Memory Mapped End */
121*f5a32af5SJon Lin #define SPI_XFER_PREPARE	BIT(7)	/* Transfer skip waiting idle */
122d255bb0eSHaavard Skinnemoen };
12377f85581Swdenk 
1241b1bd9a7SJagannadha Sutradharudu Teki /**
12577f85581Swdenk  * Initialization, must be called once on start up.
126d255bb0eSHaavard Skinnemoen  *
127d255bb0eSHaavard Skinnemoen  * TODO: I don't think we really need this.
12877f85581Swdenk  */
12977f85581Swdenk void spi_init(void);
13077f85581Swdenk 
131ba6c3ce9SSimon Glass /**
132ba6c3ce9SSimon Glass  * spi_do_alloc_slave - Allocate a new SPI slave (internal)
133ba6c3ce9SSimon Glass  *
134ba6c3ce9SSimon Glass  * Allocate and zero all fields in the spi slave, and set the bus/chip
135ba6c3ce9SSimon Glass  * select. Use the helper macro spi_alloc_slave() to call this.
136ba6c3ce9SSimon Glass  *
1371b1bd9a7SJagannadha Sutradharudu Teki  * @offset:	Offset of struct spi_slave within slave structure.
1381b1bd9a7SJagannadha Sutradharudu Teki  * @size:	Size of slave structure.
139ba6c3ce9SSimon Glass  * @bus:	Bus ID of the slave chip.
140ba6c3ce9SSimon Glass  * @cs:		Chip select ID of the slave chip on the specified bus.
141ba6c3ce9SSimon Glass  */
142ba6c3ce9SSimon Glass void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
143ba6c3ce9SSimon Glass 			 unsigned int cs);
144ba6c3ce9SSimon Glass 
145ba6c3ce9SSimon Glass /**
146ba6c3ce9SSimon Glass  * spi_alloc_slave - Allocate a new SPI slave
147ba6c3ce9SSimon Glass  *
148ba6c3ce9SSimon Glass  * Allocate and zero all fields in the spi slave, and set the bus/chip
149ba6c3ce9SSimon Glass  * select.
150ba6c3ce9SSimon Glass  *
1511b1bd9a7SJagannadha Sutradharudu Teki  * @_struct:	Name of structure to allocate (e.g. struct tegra_spi).
1521b1bd9a7SJagannadha Sutradharudu Teki  *		This structure must contain a member 'struct spi_slave *slave'.
153ba6c3ce9SSimon Glass  * @bus:	Bus ID of the slave chip.
154ba6c3ce9SSimon Glass  * @cs:		Chip select ID of the slave chip on the specified bus.
155ba6c3ce9SSimon Glass  */
156ba6c3ce9SSimon Glass #define spi_alloc_slave(_struct, bus, cs) \
157ba6c3ce9SSimon Glass 	spi_do_alloc_slave(offsetof(_struct, slave), \
158ba6c3ce9SSimon Glass 			    sizeof(_struct), bus, cs)
159ba6c3ce9SSimon Glass 
160ba6c3ce9SSimon Glass /**
161ba6c3ce9SSimon Glass  * spi_alloc_slave_base - Allocate a new SPI slave with no private data
162ba6c3ce9SSimon Glass  *
163ba6c3ce9SSimon Glass  * Allocate and zero all fields in the spi slave, and set the bus/chip
164ba6c3ce9SSimon Glass  * select.
165ba6c3ce9SSimon Glass  *
166ba6c3ce9SSimon Glass  * @bus:	Bus ID of the slave chip.
167ba6c3ce9SSimon Glass  * @cs:		Chip select ID of the slave chip on the specified bus.
168ba6c3ce9SSimon Glass  */
169ba6c3ce9SSimon Glass #define spi_alloc_slave_base(bus, cs) \
170ba6c3ce9SSimon Glass 	spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
171ba6c3ce9SSimon Glass 
1721b1bd9a7SJagannadha Sutradharudu Teki /**
173d255bb0eSHaavard Skinnemoen  * Set up communications parameters for a SPI slave.
174d255bb0eSHaavard Skinnemoen  *
175d255bb0eSHaavard Skinnemoen  * This must be called once for each slave. Note that this function
176d255bb0eSHaavard Skinnemoen  * usually doesn't touch any actual hardware, it only initializes the
177d255bb0eSHaavard Skinnemoen  * contents of spi_slave so that the hardware can be easily
178d255bb0eSHaavard Skinnemoen  * initialized later.
179d255bb0eSHaavard Skinnemoen  *
1801b1bd9a7SJagannadha Sutradharudu Teki  * @bus:	Bus ID of the slave chip.
1811b1bd9a7SJagannadha Sutradharudu Teki  * @cs:		Chip select ID of the slave chip on the specified bus.
1821b1bd9a7SJagannadha Sutradharudu Teki  * @max_hz:	Maximum SCK rate in Hz.
1831b1bd9a7SJagannadha Sutradharudu Teki  * @mode:	Clock polarity, clock phase and other parameters.
184d255bb0eSHaavard Skinnemoen  *
185d255bb0eSHaavard Skinnemoen  * Returns: A spi_slave reference that can be used in subsequent SPI
186d255bb0eSHaavard Skinnemoen  * calls, or NULL if one or more of the parameters are not supported.
187d255bb0eSHaavard Skinnemoen  */
188d255bb0eSHaavard Skinnemoen struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
189d255bb0eSHaavard Skinnemoen 		unsigned int max_hz, unsigned int mode);
190d255bb0eSHaavard Skinnemoen 
1911b1bd9a7SJagannadha Sutradharudu Teki /**
192d255bb0eSHaavard Skinnemoen  * Free any memory associated with a SPI slave.
193d255bb0eSHaavard Skinnemoen  *
1941b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
195d255bb0eSHaavard Skinnemoen  */
196d255bb0eSHaavard Skinnemoen void spi_free_slave(struct spi_slave *slave);
197d255bb0eSHaavard Skinnemoen 
1981b1bd9a7SJagannadha Sutradharudu Teki /**
199d255bb0eSHaavard Skinnemoen  * Claim the bus and prepare it for communication with a given slave.
200d255bb0eSHaavard Skinnemoen  *
201d255bb0eSHaavard Skinnemoen  * This must be called before doing any transfers with a SPI slave. It
202d255bb0eSHaavard Skinnemoen  * will enable and initialize any SPI hardware as necessary, and make
203d255bb0eSHaavard Skinnemoen  * sure that the SCK line is in the correct idle state. It is not
204d255bb0eSHaavard Skinnemoen  * allowed to claim the same bus for several slaves without releasing
205d255bb0eSHaavard Skinnemoen  * the bus in between.
206d255bb0eSHaavard Skinnemoen  *
2071b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
208d255bb0eSHaavard Skinnemoen  *
209d255bb0eSHaavard Skinnemoen  * Returns: 0 if the bus was claimed successfully, or a negative value
210d255bb0eSHaavard Skinnemoen  * if it wasn't.
211d255bb0eSHaavard Skinnemoen  */
212d255bb0eSHaavard Skinnemoen int spi_claim_bus(struct spi_slave *slave);
213d255bb0eSHaavard Skinnemoen 
2141b1bd9a7SJagannadha Sutradharudu Teki /**
215d255bb0eSHaavard Skinnemoen  * Release the SPI bus
216d255bb0eSHaavard Skinnemoen  *
217d255bb0eSHaavard Skinnemoen  * This must be called once for every call to spi_claim_bus() after
218d255bb0eSHaavard Skinnemoen  * all transfers have finished. It may disable any SPI hardware as
219d255bb0eSHaavard Skinnemoen  * appropriate.
220d255bb0eSHaavard Skinnemoen  *
2211b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
222d255bb0eSHaavard Skinnemoen  */
223d255bb0eSHaavard Skinnemoen void spi_release_bus(struct spi_slave *slave);
22477f85581Swdenk 
2251b1bd9a7SJagannadha Sutradharudu Teki /**
2265753d09bSNikita Kiryanov  * Set the word length for SPI transactions
2275753d09bSNikita Kiryanov  *
2285753d09bSNikita Kiryanov  * Set the word length (number of bits per word) for SPI transactions.
2295753d09bSNikita Kiryanov  *
2305753d09bSNikita Kiryanov  * @slave:	The SPI slave
2315753d09bSNikita Kiryanov  * @wordlen:	The number of bits in a word
2325753d09bSNikita Kiryanov  *
2335753d09bSNikita Kiryanov  * Returns: 0 on success, -1 on failure.
2345753d09bSNikita Kiryanov  */
2355753d09bSNikita Kiryanov int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
2365753d09bSNikita Kiryanov 
2375753d09bSNikita Kiryanov /**
23877f85581Swdenk  * SPI transfer
23977f85581Swdenk  *
24077f85581Swdenk  * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
24177f85581Swdenk  * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
24277f85581Swdenk  *
24377f85581Swdenk  * The source of the outgoing bits is the "dout" parameter and the
24477f85581Swdenk  * destination of the input bits is the "din" parameter.  Note that "dout"
24577f85581Swdenk  * and "din" can point to the same memory location, in which case the
24677f85581Swdenk  * input data overwrites the output data (since both are buffered by
24777f85581Swdenk  * temporary variables, this is OK).
24877f85581Swdenk  *
24977f85581Swdenk  * spi_xfer() interface:
2501b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave which will be sending/receiving the data.
2511b1bd9a7SJagannadha Sutradharudu Teki  * @bitlen:	How many bits to write and read.
2521b1bd9a7SJagannadha Sutradharudu Teki  * @dout:	Pointer to a string of bits to send out.  The bits are
25377f85581Swdenk  *		held in a byte array and are sent MSB first.
2541b1bd9a7SJagannadha Sutradharudu Teki  * @din:	Pointer to a string of bits that will be filled in.
2551b1bd9a7SJagannadha Sutradharudu Teki  * @flags:	A bitwise combination of SPI_XFER_* flags.
25677f85581Swdenk  *
25777f85581Swdenk  * Returns: 0 on success, not 0 on failure
25877f85581Swdenk  */
259d255bb0eSHaavard Skinnemoen int  spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
260d255bb0eSHaavard Skinnemoen 		void *din, unsigned long flags);
26177f85581Swdenk 
262146bad96STom Rini /* Copy memory mapped data */
263146bad96STom Rini void spi_flash_copy_mmap(void *data, void *offset, size_t len);
264146bad96STom Rini 
2651b1bd9a7SJagannadha Sutradharudu Teki /**
266d255bb0eSHaavard Skinnemoen  * Determine if a SPI chipselect is valid.
267d255bb0eSHaavard Skinnemoen  * This function is provided by the board if the low-level SPI driver
268d255bb0eSHaavard Skinnemoen  * needs it to determine if a given chipselect is actually valid.
269d255bb0eSHaavard Skinnemoen  *
270d255bb0eSHaavard Skinnemoen  * Returns: 1 if bus:cs identifies a valid chip on this board, 0
271d255bb0eSHaavard Skinnemoen  * otherwise.
272d255bb0eSHaavard Skinnemoen  */
273d255bb0eSHaavard Skinnemoen int spi_cs_is_valid(unsigned int bus, unsigned int cs);
274d255bb0eSHaavard Skinnemoen 
275d7af6a48SSimon Glass #ifndef CONFIG_DM_SPI
2761b1bd9a7SJagannadha Sutradharudu Teki /**
277d255bb0eSHaavard Skinnemoen  * Activate a SPI chipselect.
278d255bb0eSHaavard Skinnemoen  * This function is provided by the board code when using a driver
279d255bb0eSHaavard Skinnemoen  * that can't control its chipselects automatically (e.g.
280d255bb0eSHaavard Skinnemoen  * common/soft_spi.c). When called, it should activate the chip select
281d255bb0eSHaavard Skinnemoen  * to the device identified by "slave".
282d255bb0eSHaavard Skinnemoen  */
283d255bb0eSHaavard Skinnemoen void spi_cs_activate(struct spi_slave *slave);
284d255bb0eSHaavard Skinnemoen 
2851b1bd9a7SJagannadha Sutradharudu Teki /**
286d255bb0eSHaavard Skinnemoen  * Deactivate a SPI chipselect.
287d255bb0eSHaavard Skinnemoen  * This function is provided by the board code when using a driver
288d255bb0eSHaavard Skinnemoen  * that can't control its chipselects automatically (e.g.
289d255bb0eSHaavard Skinnemoen  * common/soft_spi.c). When called, it should deactivate the chip
290d255bb0eSHaavard Skinnemoen  * select to the device identified by "slave".
291d255bb0eSHaavard Skinnemoen  */
292d255bb0eSHaavard Skinnemoen void spi_cs_deactivate(struct spi_slave *slave);
293d255bb0eSHaavard Skinnemoen 
2941b1bd9a7SJagannadha Sutradharudu Teki /**
295fa1423e7SThomas Chou  * Set transfer speed.
296fa1423e7SThomas Chou  * This sets a new speed to be applied for next spi_xfer().
2971b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
2981b1bd9a7SJagannadha Sutradharudu Teki  * @hz:		The transfer speed
299fa1423e7SThomas Chou  */
300fa1423e7SThomas Chou void spi_set_speed(struct spi_slave *slave, uint hz);
301d7af6a48SSimon Glass #endif
302fa1423e7SThomas Chou 
3031b1bd9a7SJagannadha Sutradharudu Teki /**
304d255bb0eSHaavard Skinnemoen  * Write 8 bits, then read 8 bits.
3051b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave we're communicating with
3061b1bd9a7SJagannadha Sutradharudu Teki  * @byte:	Byte to be written
307d255bb0eSHaavard Skinnemoen  *
308d255bb0eSHaavard Skinnemoen  * Returns: The value that was read, or a negative value on error.
309d255bb0eSHaavard Skinnemoen  *
310d255bb0eSHaavard Skinnemoen  * TODO: This function probably shouldn't be inlined.
311d255bb0eSHaavard Skinnemoen  */
312d255bb0eSHaavard Skinnemoen static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
313d255bb0eSHaavard Skinnemoen {
314d255bb0eSHaavard Skinnemoen 	unsigned char dout[2];
315d255bb0eSHaavard Skinnemoen 	unsigned char din[2];
316d255bb0eSHaavard Skinnemoen 	int ret;
317d255bb0eSHaavard Skinnemoen 
318d255bb0eSHaavard Skinnemoen 	dout[0] = byte;
319d255bb0eSHaavard Skinnemoen 	dout[1] = 0;
320d255bb0eSHaavard Skinnemoen 
321d255bb0eSHaavard Skinnemoen 	ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
322d255bb0eSHaavard Skinnemoen 	return ret < 0 ? ret : din[1];
323d255bb0eSHaavard Skinnemoen }
32438254f45SGuennadi Liakhovetski 
325d7af6a48SSimon Glass #ifdef CONFIG_DM_SPI
326d7af6a48SSimon Glass 
327d7af6a48SSimon Glass /**
328d7af6a48SSimon Glass  * struct spi_cs_info - Information about a bus chip select
329d7af6a48SSimon Glass  *
330d7af6a48SSimon Glass  * @dev:	Connected device, or NULL if none
331d7af6a48SSimon Glass  */
332d7af6a48SSimon Glass struct spi_cs_info {
333d7af6a48SSimon Glass 	struct udevice *dev;
334d7af6a48SSimon Glass };
335d7af6a48SSimon Glass 
336d7af6a48SSimon Glass /**
337d7af6a48SSimon Glass  * struct struct dm_spi_ops - Driver model SPI operations
338d7af6a48SSimon Glass  *
339d7af6a48SSimon Glass  * The uclass interface is implemented by all SPI devices which use
340d7af6a48SSimon Glass  * driver model.
341d7af6a48SSimon Glass  */
342d7af6a48SSimon Glass struct dm_spi_ops {
343d7af6a48SSimon Glass 	/**
344d7af6a48SSimon Glass 	 * Claim the bus and prepare it for communication.
345d7af6a48SSimon Glass 	 *
346d7af6a48SSimon Glass 	 * The device provided is the slave device. It's parent controller
347d7af6a48SSimon Glass 	 * will be used to provide the communication.
348d7af6a48SSimon Glass 	 *
349d7af6a48SSimon Glass 	 * This must be called before doing any transfers with a SPI slave. It
350d7af6a48SSimon Glass 	 * will enable and initialize any SPI hardware as necessary, and make
351d7af6a48SSimon Glass 	 * sure that the SCK line is in the correct idle state. It is not
352d7af6a48SSimon Glass 	 * allowed to claim the same bus for several slaves without releasing
353d7af6a48SSimon Glass 	 * the bus in between.
354d7af6a48SSimon Glass 	 *
3559694b724SSimon Glass 	 * @dev:	The SPI slave
356d7af6a48SSimon Glass 	 *
357d7af6a48SSimon Glass 	 * Returns: 0 if the bus was claimed successfully, or a negative value
358d7af6a48SSimon Glass 	 * if it wasn't.
359d7af6a48SSimon Glass 	 */
3609694b724SSimon Glass 	int (*claim_bus)(struct udevice *dev);
361d7af6a48SSimon Glass 
362d7af6a48SSimon Glass 	/**
363d7af6a48SSimon Glass 	 * Release the SPI bus
364d7af6a48SSimon Glass 	 *
365d7af6a48SSimon Glass 	 * This must be called once for every call to spi_claim_bus() after
366d7af6a48SSimon Glass 	 * all transfers have finished. It may disable any SPI hardware as
367d7af6a48SSimon Glass 	 * appropriate.
368d7af6a48SSimon Glass 	 *
3699694b724SSimon Glass 	 * @dev:	The SPI slave
370d7af6a48SSimon Glass 	 */
3719694b724SSimon Glass 	int (*release_bus)(struct udevice *dev);
372d7af6a48SSimon Glass 
373d7af6a48SSimon Glass 	/**
374d7af6a48SSimon Glass 	 * Set the word length for SPI transactions
375d7af6a48SSimon Glass 	 *
376d7af6a48SSimon Glass 	 * Set the word length (number of bits per word) for SPI transactions.
377d7af6a48SSimon Glass 	 *
378d7af6a48SSimon Glass 	 * @bus:	The SPI slave
379d7af6a48SSimon Glass 	 * @wordlen:	The number of bits in a word
380d7af6a48SSimon Glass 	 *
381d7af6a48SSimon Glass 	 * Returns: 0 on success, -ve on failure.
382d7af6a48SSimon Glass 	 */
3839694b724SSimon Glass 	int (*set_wordlen)(struct udevice *dev, unsigned int wordlen);
384d7af6a48SSimon Glass 
385d7af6a48SSimon Glass 	/**
386d7af6a48SSimon Glass 	 * SPI transfer
387d7af6a48SSimon Glass 	 *
388d7af6a48SSimon Glass 	 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
389d7af6a48SSimon Glass 	 * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
390d7af6a48SSimon Glass 	 * works.
391d7af6a48SSimon Glass 	 *
392d7af6a48SSimon Glass 	 * The source of the outgoing bits is the "dout" parameter and the
393d7af6a48SSimon Glass 	 * destination of the input bits is the "din" parameter.  Note that
394d7af6a48SSimon Glass 	 * "dout" and "din" can point to the same memory location, in which
395d7af6a48SSimon Glass 	 * case the input data overwrites the output data (since both are
396d7af6a48SSimon Glass 	 * buffered by temporary variables, this is OK).
397d7af6a48SSimon Glass 	 *
398d7af6a48SSimon Glass 	 * spi_xfer() interface:
399d7af6a48SSimon Glass 	 * @dev:	The slave device to communicate with
400d7af6a48SSimon Glass 	 * @bitlen:	How many bits to write and read.
401d7af6a48SSimon Glass 	 * @dout:	Pointer to a string of bits to send out.  The bits are
402d7af6a48SSimon Glass 	 *		held in a byte array and are sent MSB first.
403d7af6a48SSimon Glass 	 * @din:	Pointer to a string of bits that will be filled in.
404d7af6a48SSimon Glass 	 * @flags:	A bitwise combination of SPI_XFER_* flags.
405d7af6a48SSimon Glass 	 *
406d7af6a48SSimon Glass 	 * Returns: 0 on success, not -1 on failure
407d7af6a48SSimon Glass 	 */
408d7af6a48SSimon Glass 	int (*xfer)(struct udevice *dev, unsigned int bitlen, const void *dout,
409d7af6a48SSimon Glass 		    void *din, unsigned long flags);
410d7af6a48SSimon Glass 
411d7af6a48SSimon Glass 	/**
412967efcaeSBoris Brezillon 	 * Optimized handlers for SPI memory-like operations.
413967efcaeSBoris Brezillon 	 *
414967efcaeSBoris Brezillon 	 * Optimized/dedicated operations for interactions with SPI memory. This
415967efcaeSBoris Brezillon 	 * field is optional and should only be implemented if the controller
416967efcaeSBoris Brezillon 	 * has native support for memory like operations.
417967efcaeSBoris Brezillon 	 */
418967efcaeSBoris Brezillon 	const struct spi_controller_mem_ops *mem_ops;
419967efcaeSBoris Brezillon 
420967efcaeSBoris Brezillon 	/**
421d7af6a48SSimon Glass 	 * Set transfer speed.
422d7af6a48SSimon Glass 	 * This sets a new speed to be applied for next spi_xfer().
423d7af6a48SSimon Glass 	 * @bus:	The SPI bus
424d7af6a48SSimon Glass 	 * @hz:		The transfer speed
425d7af6a48SSimon Glass 	 * @return 0 if OK, -ve on error
426d7af6a48SSimon Glass 	 */
427d7af6a48SSimon Glass 	int (*set_speed)(struct udevice *bus, uint hz);
428d7af6a48SSimon Glass 
429d7af6a48SSimon Glass 	/**
430d7af6a48SSimon Glass 	 * Set the SPI mode/flags
431d7af6a48SSimon Glass 	 *
432d7af6a48SSimon Glass 	 * It is unclear if we want to set speed and mode together instead
433d7af6a48SSimon Glass 	 * of separately.
434d7af6a48SSimon Glass 	 *
435d7af6a48SSimon Glass 	 * @bus:	The SPI bus
436d7af6a48SSimon Glass 	 * @mode:	Requested SPI mode (SPI_... flags)
437d7af6a48SSimon Glass 	 * @return 0 if OK, -ve on error
438d7af6a48SSimon Glass 	 */
439d7af6a48SSimon Glass 	int (*set_mode)(struct udevice *bus, uint mode);
440d7af6a48SSimon Glass 
441d7af6a48SSimon Glass 	/**
442d7af6a48SSimon Glass 	 * Get information on a chip select
443d7af6a48SSimon Glass 	 *
444d7af6a48SSimon Glass 	 * This is only called when the SPI uclass does not know about a
445d7af6a48SSimon Glass 	 * chip select, i.e. it has no attached device. It gives the driver
446d7af6a48SSimon Glass 	 * a chance to allow activity on that chip select even so.
447d7af6a48SSimon Glass 	 *
448d7af6a48SSimon Glass 	 * @bus:	The SPI bus
449d7af6a48SSimon Glass 	 * @cs:		The chip select (0..n-1)
450d7af6a48SSimon Glass 	 * @info:	Returns information about the chip select, if valid.
451d7af6a48SSimon Glass 	 *		On entry info->dev is NULL
452d7af6a48SSimon Glass 	 * @return 0 if OK (and @info is set up), -ENODEV if the chip select
453d7af6a48SSimon Glass 	 *	   is invalid, other -ve value on error
454d7af6a48SSimon Glass 	 */
455d7af6a48SSimon Glass 	int (*cs_info)(struct udevice *bus, uint cs, struct spi_cs_info *info);
456d7af6a48SSimon Glass };
457d7af6a48SSimon Glass 
458c60e1f25SSimon Glass struct dm_spi_emul_ops {
459c60e1f25SSimon Glass 	/**
460c60e1f25SSimon Glass 	 * SPI transfer
461c60e1f25SSimon Glass 	 *
462c60e1f25SSimon Glass 	 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
463c60e1f25SSimon Glass 	 * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
464c60e1f25SSimon Glass 	 * works. Here the device is a slave.
465c60e1f25SSimon Glass 	 *
466c60e1f25SSimon Glass 	 * The source of the outgoing bits is the "dout" parameter and the
467c60e1f25SSimon Glass 	 * destination of the input bits is the "din" parameter.  Note that
468c60e1f25SSimon Glass 	 * "dout" and "din" can point to the same memory location, in which
469c60e1f25SSimon Glass 	 * case the input data overwrites the output data (since both are
470c60e1f25SSimon Glass 	 * buffered by temporary variables, this is OK).
471c60e1f25SSimon Glass 	 *
472c60e1f25SSimon Glass 	 * spi_xfer() interface:
473c60e1f25SSimon Glass 	 * @slave:	The SPI slave which will be sending/receiving the data.
474c60e1f25SSimon Glass 	 * @bitlen:	How many bits to write and read.
475c60e1f25SSimon Glass 	 * @dout:	Pointer to a string of bits sent to the device. The
476c60e1f25SSimon Glass 	 *		bits are held in a byte array and are sent MSB first.
477c60e1f25SSimon Glass 	 * @din:	Pointer to a string of bits that will be sent back to
478c60e1f25SSimon Glass 	 *		the master.
479c60e1f25SSimon Glass 	 * @flags:	A bitwise combination of SPI_XFER_* flags.
480c60e1f25SSimon Glass 	 *
481c60e1f25SSimon Glass 	 * Returns: 0 on success, not -1 on failure
482c60e1f25SSimon Glass 	 */
483c60e1f25SSimon Glass 	int (*xfer)(struct udevice *slave, unsigned int bitlen,
484c60e1f25SSimon Glass 		    const void *dout, void *din, unsigned long flags);
485c60e1f25SSimon Glass };
486c60e1f25SSimon Glass 
487d7af6a48SSimon Glass /**
488d7af6a48SSimon Glass  * spi_find_bus_and_cs() - Find bus and slave devices by number
489d7af6a48SSimon Glass  *
490d7af6a48SSimon Glass  * Given a bus number and chip select, this finds the corresponding bus
491d7af6a48SSimon Glass  * device and slave device. Neither device is activated by this function,
492d7af6a48SSimon Glass  * although they may have been activated previously.
493d7af6a48SSimon Glass  *
494d7af6a48SSimon Glass  * @busnum:	SPI bus number
495d7af6a48SSimon Glass  * @cs:		Chip select to look for
496d7af6a48SSimon Glass  * @busp:	Returns bus device
497d7af6a48SSimon Glass  * @devp:	Return slave device
498d7af6a48SSimon Glass  * @return 0 if found, -ENODEV on error
499d7af6a48SSimon Glass  */
500d7af6a48SSimon Glass int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
501d7af6a48SSimon Glass 			struct udevice **devp);
502d7af6a48SSimon Glass 
503d7af6a48SSimon Glass /**
504d7af6a48SSimon Glass  * spi_get_bus_and_cs() - Find and activate bus and slave devices by number
505d7af6a48SSimon Glass  *
506d7af6a48SSimon Glass  * Given a bus number and chip select, this finds the corresponding bus
507d7af6a48SSimon Glass  * device and slave device.
508d7af6a48SSimon Glass  *
509d7af6a48SSimon Glass  * If no such slave exists, and drv_name is not NULL, then a new slave device
510ee9b3572SPatrick Delaunay  * is automatically bound on this chip select with requested speed and mode.
511d7af6a48SSimon Glass  *
512ee9b3572SPatrick Delaunay  * Ths new slave device is probed ready for use with the speed and mode
513ee9b3572SPatrick Delaunay  * from platdata when available or the requested values.
514d7af6a48SSimon Glass  *
515d7af6a48SSimon Glass  * @busnum:	SPI bus number
516d7af6a48SSimon Glass  * @cs:		Chip select to look for
517ee9b3572SPatrick Delaunay  * @speed:	SPI speed to use for this slave when not available in platdata
518ee9b3572SPatrick Delaunay  * @mode:	SPI mode to use for this slave when not available in platdata
519d7af6a48SSimon Glass  * @drv_name:	Name of driver to attach to this chip select
520d7af6a48SSimon Glass  * @dev_name:	Name of the new device thus created
521d7af6a48SSimon Glass  * @busp:	Returns bus device
522d7af6a48SSimon Glass  * @devp:	Return slave device
523d7af6a48SSimon Glass  * @return 0 if found, -ve on error
524d7af6a48SSimon Glass  */
525d7af6a48SSimon Glass int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
526d7af6a48SSimon Glass 			const char *drv_name, const char *dev_name,
527d7af6a48SSimon Glass 			struct udevice **busp, struct spi_slave **devp);
528d7af6a48SSimon Glass 
529d7af6a48SSimon Glass /**
530d7af6a48SSimon Glass  * spi_chip_select() - Get the chip select for a slave
531d7af6a48SSimon Glass  *
532d7af6a48SSimon Glass  * @return the chip select this slave is attached to
533d7af6a48SSimon Glass  */
534d7af6a48SSimon Glass int spi_chip_select(struct udevice *slave);
535d7af6a48SSimon Glass 
536d7af6a48SSimon Glass /**
537ff56bba2SSimon Glass  * spi_find_chip_select() - Find the slave attached to chip select
538ff56bba2SSimon Glass  *
539ff56bba2SSimon Glass  * @bus:	SPI bus to search
540ff56bba2SSimon Glass  * @cs:		Chip select to look for
541ff56bba2SSimon Glass  * @devp:	Returns the slave device if found
542ff56bba2SSimon Glass  * @return 0 if found, -ENODEV on error
543ff56bba2SSimon Glass  */
544ff56bba2SSimon Glass int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp);
545ff56bba2SSimon Glass 
546ff56bba2SSimon Glass /**
547d0cff03eSSimon Glass  * spi_slave_ofdata_to_platdata() - decode standard SPI platform data
548d7af6a48SSimon Glass  *
549d0cff03eSSimon Glass  * This decodes the speed and mode for a slave from a device tree node
550d7af6a48SSimon Glass  *
551d7af6a48SSimon Glass  * @blob:	Device tree blob
552d7af6a48SSimon Glass  * @node:	Node offset to read from
553d0cff03eSSimon Glass  * @plat:	Place to put the decoded information
554d7af6a48SSimon Glass  */
555279e26f5SSimon Glass int spi_slave_ofdata_to_platdata(struct udevice *dev,
556d0cff03eSSimon Glass 				 struct dm_spi_slave_platdata *plat);
557d7af6a48SSimon Glass 
558d7af6a48SSimon Glass /**
559d7af6a48SSimon Glass  * spi_cs_info() - Check information on a chip select
560d7af6a48SSimon Glass  *
561d7af6a48SSimon Glass  * This checks a particular chip select on a bus to see if it has a device
562d7af6a48SSimon Glass  * attached, or is even valid.
563d7af6a48SSimon Glass  *
564d7af6a48SSimon Glass  * @bus:	The SPI bus
565d7af6a48SSimon Glass  * @cs:		The chip select (0..n-1)
566d7af6a48SSimon Glass  * @info:	Returns information about the chip select, if valid
567d7af6a48SSimon Glass  * @return 0 if OK (and @info is set up), -ENODEV if the chip select
568d7af6a48SSimon Glass  *	   is invalid, other -ve value on error
569d7af6a48SSimon Glass  */
570d7af6a48SSimon Glass int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info);
571d7af6a48SSimon Glass 
572d7af6a48SSimon Glass struct sandbox_state;
573c60e1f25SSimon Glass 
574c60e1f25SSimon Glass /**
575c60e1f25SSimon Glass  * sandbox_spi_get_emul() - get an emulator for a SPI slave
576c60e1f25SSimon Glass  *
577c60e1f25SSimon Glass  * This provides a way to attach an emulated SPI device to a particular SPI
578c60e1f25SSimon Glass  * slave, so that xfer() operations on the slave will be handled by the
579c60e1f25SSimon Glass  * emulator. If a emulator already exists on that chip select it is returned.
580c60e1f25SSimon Glass  * Otherwise one is created.
581c60e1f25SSimon Glass  *
582c60e1f25SSimon Glass  * @state:	Sandbox state
583c60e1f25SSimon Glass  * @bus:	SPI bus requesting the emulator
584c60e1f25SSimon Glass  * @slave:	SPI slave device requesting the emulator
585c60e1f25SSimon Glass  * @emuip:	Returns pointer to emulator
586c60e1f25SSimon Glass  * @return 0 if OK, -ve on error
587c60e1f25SSimon Glass  */
588d7af6a48SSimon Glass int sandbox_spi_get_emul(struct sandbox_state *state,
589d7af6a48SSimon Glass 			 struct udevice *bus, struct udevice *slave,
590d7af6a48SSimon Glass 			 struct udevice **emulp);
591d7af6a48SSimon Glass 
5927a3eff4cSPeng Fan /**
5937a3eff4cSPeng Fan  * Claim the bus and prepare it for communication with a given slave.
5947a3eff4cSPeng Fan  *
5957a3eff4cSPeng Fan  * This must be called before doing any transfers with a SPI slave. It
5967a3eff4cSPeng Fan  * will enable and initialize any SPI hardware as necessary, and make
5977a3eff4cSPeng Fan  * sure that the SCK line is in the correct idle state. It is not
5987a3eff4cSPeng Fan  * allowed to claim the same bus for several slaves without releasing
5997a3eff4cSPeng Fan  * the bus in between.
6007a3eff4cSPeng Fan  *
6017a3eff4cSPeng Fan  * @dev:	The SPI slave device
6027a3eff4cSPeng Fan  *
6037a3eff4cSPeng Fan  * Returns: 0 if the bus was claimed successfully, or a negative value
6047a3eff4cSPeng Fan  * if it wasn't.
6057a3eff4cSPeng Fan  */
6067a3eff4cSPeng Fan int dm_spi_claim_bus(struct udevice *dev);
6077a3eff4cSPeng Fan 
6087a3eff4cSPeng Fan /**
6097a3eff4cSPeng Fan  * Release the SPI bus
6107a3eff4cSPeng Fan  *
6117a3eff4cSPeng Fan  * This must be called once for every call to dm_spi_claim_bus() after
6127a3eff4cSPeng Fan  * all transfers have finished. It may disable any SPI hardware as
6137a3eff4cSPeng Fan  * appropriate.
6147a3eff4cSPeng Fan  *
6157a3eff4cSPeng Fan  * @slave:	The SPI slave device
6167a3eff4cSPeng Fan  */
6177a3eff4cSPeng Fan void dm_spi_release_bus(struct udevice *dev);
6187a3eff4cSPeng Fan 
6197a3eff4cSPeng Fan /**
6207a3eff4cSPeng Fan  * SPI transfer
6217a3eff4cSPeng Fan  *
6227a3eff4cSPeng Fan  * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
6237a3eff4cSPeng Fan  * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
6247a3eff4cSPeng Fan  *
6257a3eff4cSPeng Fan  * The source of the outgoing bits is the "dout" parameter and the
6267a3eff4cSPeng Fan  * destination of the input bits is the "din" parameter.  Note that "dout"
6277a3eff4cSPeng Fan  * and "din" can point to the same memory location, in which case the
6287a3eff4cSPeng Fan  * input data overwrites the output data (since both are buffered by
6297a3eff4cSPeng Fan  * temporary variables, this is OK).
6307a3eff4cSPeng Fan  *
6317a3eff4cSPeng Fan  * dm_spi_xfer() interface:
6327a3eff4cSPeng Fan  * @dev:	The SPI slave device which will be sending/receiving the data.
6337a3eff4cSPeng Fan  * @bitlen:	How many bits to write and read.
6347a3eff4cSPeng Fan  * @dout:	Pointer to a string of bits to send out.  The bits are
6357a3eff4cSPeng Fan  *		held in a byte array and are sent MSB first.
6367a3eff4cSPeng Fan  * @din:	Pointer to a string of bits that will be filled in.
6377a3eff4cSPeng Fan  * @flags:	A bitwise combination of SPI_XFER_* flags.
6387a3eff4cSPeng Fan  *
6397a3eff4cSPeng Fan  * Returns: 0 on success, not 0 on failure
6407a3eff4cSPeng Fan  */
6417a3eff4cSPeng Fan int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
6427a3eff4cSPeng Fan 		const void *dout, void *din, unsigned long flags);
6437a3eff4cSPeng Fan 
644bc5701e1SSimon Glass /* Access the operations for a SPI device */
645d7af6a48SSimon Glass #define spi_get_ops(dev)	((struct dm_spi_ops *)(dev)->driver->ops)
646c60e1f25SSimon Glass #define spi_emul_get_ops(dev)	((struct dm_spi_emul_ops *)(dev)->driver->ops)
647d7af6a48SSimon Glass #endif /* CONFIG_DM_SPI */
648d7af6a48SSimon Glass 
64977f85581Swdenk #endif	/* _SPI_H_ */
650