xref: /rk3399_rockchip-uboot/include/spi.h (revision f5c3c033f7ebd86e6a6e90f4a97b54d3e0f41d9a)
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 */
1438254f45SGuennadi Liakhovetski #define	SPI_CPHA	0x01			/* clock phase */
1538254f45SGuennadi Liakhovetski #define	SPI_CPOL	0x02			/* 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)
20d255bb0eSHaavard Skinnemoen #define	SPI_CS_HIGH	0x04			/* CS active high */
2138254f45SGuennadi Liakhovetski #define	SPI_LSB_FIRST	0x08			/* per-word bits-on-wire */
2238254f45SGuennadi Liakhovetski #define	SPI_3WIRE	0x10			/* SI/SO signals shared */
2338254f45SGuennadi Liakhovetski #define	SPI_LOOP	0x20			/* loopback mode */
24bb786b84SRajeshwari Shinde #define	SPI_SLAVE	0x40			/* slave mode */
25bb786b84SRajeshwari Shinde #define	SPI_PREAMBLE	0x80			/* Skip preamble bytes */
2638254f45SGuennadi Liakhovetski 
27d255bb0eSHaavard Skinnemoen /* SPI transfer flags */
28d255bb0eSHaavard Skinnemoen #define SPI_XFER_BEGIN		0x01	/* Assert CS before transfer */
29d255bb0eSHaavard Skinnemoen #define SPI_XFER_END		0x02	/* Deassert CS after transfer */
30004f15b6SPoddar, Sourav #define SPI_XFER_MMAP		0x08	/* Memory Mapped start */
31004f15b6SPoddar, Sourav #define SPI_XFER_MMAP_END	0x10	/* Memory Mapped End */
324700219dSNikita Kiryanov #define SPI_XFER_ONCE		(SPI_XFER_BEGIN | SPI_XFER_END)
33f77f4691SJagannadha Sutradharudu Teki #define SPI_XFER_U_PAGE	(1 << 5)
3477f85581Swdenk 
353163aaa6SJagannadha Sutradharudu Teki /* SPI TX operation modes */
36248a0488SSimon Glass #define SPI_OPM_TX_QPP		(1 << 0)
3754ba653aSJagannadha Sutradharudu Teki #define SPI_OPM_TX_BP		(1 << 1)
383163aaa6SJagannadha Sutradharudu Teki 
394e09cc1eSJagannadha Sutradharudu Teki /* SPI RX operation modes */
40248a0488SSimon Glass #define SPI_OPM_RX_AS		(1 << 0)
41d2f76316SSiva Durga Prasad Paladugu #define SPI_OPM_RX_AF		(1 << 1)
42d2f76316SSiva Durga Prasad Paladugu #define SPI_OPM_RX_DOUT		(1 << 2)
43d2f76316SSiva Durga Prasad Paladugu #define SPI_OPM_RX_DIO		(1 << 3)
44d2f76316SSiva Durga Prasad Paladugu #define SPI_OPM_RX_QOF		(1 << 4)
45d2f76316SSiva Durga Prasad Paladugu #define SPI_OPM_RX_QIOF		(1 << 5)
46d2f76316SSiva Durga Prasad Paladugu #define SPI_OPM_RX_EXTN	(SPI_OPM_RX_AS | SPI_OPM_RX_AF | SPI_OPM_RX_DOUT | \
47c4ba0d82SJagannadha Sutradharudu Teki 				SPI_OPM_RX_DIO | SPI_OPM_RX_QOF | \
48248a0488SSimon Glass 				SPI_OPM_RX_QIOF)
494e09cc1eSJagannadha Sutradharudu Teki 
50248a0488SSimon Glass /* SPI bus connection options - see enum spi_dual_flash */
51248a0488SSimon Glass #define SPI_CONN_DUAL_SHARED		(1 << 0)
52248a0488SSimon Glass #define SPI_CONN_DUAL_SEPARATED	(1 << 1)
53f77f4691SJagannadha Sutradharudu Teki 
54bb786b84SRajeshwari Shinde /* Header byte that marks the start of the message */
55bb786b84SRajeshwari Shinde #define SPI_PREAMBLE_END_BYTE	0xec
56bb786b84SRajeshwari Shinde 
575753d09bSNikita Kiryanov #define SPI_DEFAULT_WORDLEN	8
585753d09bSNikita Kiryanov 
59d7af6a48SSimon Glass #ifdef CONFIG_DM_SPI
60d0cff03eSSimon Glass /* TODO(sjg@chromium.org): Remove this and use max_hz from struct spi_slave */
61d7af6a48SSimon Glass struct dm_spi_bus {
62d7af6a48SSimon Glass 	uint max_hz;
63d7af6a48SSimon Glass };
64d7af6a48SSimon Glass 
65d0cff03eSSimon Glass /**
66d0cff03eSSimon Glass  * struct dm_spi_platdata - platform data for all SPI slaves
67d0cff03eSSimon Glass  *
68d0cff03eSSimon Glass  * This describes a SPI slave, a child device of the SPI bus. To obtain this
69d0cff03eSSimon Glass  * struct from a spi_slave, use dev_get_parent_platdata(dev) or
70d0cff03eSSimon Glass  * dev_get_parent_platdata(slave->dev).
71d0cff03eSSimon Glass  *
72d0cff03eSSimon Glass  * This data is immuatable. Each time the device is probed, @max_hz and @mode
73d0cff03eSSimon Glass  * will be copied to struct spi_slave.
74d0cff03eSSimon Glass  *
75d0cff03eSSimon Glass  * @cs:		Chip select number (0..n-1)
76d0cff03eSSimon Glass  * @max_hz:	Maximum bus speed that this slave can tolerate
77d0cff03eSSimon Glass  * @mode:	SPI mode to use for this device (see SPI mode flags)
78d0cff03eSSimon Glass  */
79d0cff03eSSimon Glass struct dm_spi_slave_platdata {
80d0cff03eSSimon Glass 	unsigned int cs;
81d0cff03eSSimon Glass 	uint max_hz;
82d0cff03eSSimon Glass 	uint mode;
83d0cff03eSSimon Glass };
84d0cff03eSSimon Glass 
85d7af6a48SSimon Glass #endif /* CONFIG_DM_SPI */
86d7af6a48SSimon Glass 
871b1bd9a7SJagannadha Sutradharudu Teki /**
88ce22b922SJagannadha Sutradharudu Teki  * struct spi_slave - Representation of a SPI slave
89d255bb0eSHaavard Skinnemoen  *
90d7af6a48SSimon Glass  * For driver model this is the per-child data used by the SPI bus. It can
91bcbe3d15SSimon Glass  * be accessed using dev_get_parent_priv() on the slave device. The SPI uclass
92d0cff03eSSimon Glass  * sets uip per_child_auto_alloc_size to sizeof(struct spi_slave), and the
93d0cff03eSSimon Glass  * driver should not override it. Two platform data fields (max_hz and mode)
94d0cff03eSSimon Glass  * are copied into this structure to provide an initial value. This allows
95d0cff03eSSimon Glass  * them to be changed, since we should never change platform data in drivers.
96d7af6a48SSimon Glass  *
97d7af6a48SSimon Glass  * If not using driver model, drivers are expected to extend this with
98d7af6a48SSimon Glass  * controller-specific data.
99d7af6a48SSimon Glass  *
100d7af6a48SSimon Glass  * @dev:		SPI slave device
101d7af6a48SSimon Glass  * @max_hz:		Maximum speed for this slave
10260e2809aSSimon Glass  * @speed:		Current bus speed. This is 0 until the bus is first
10360e2809aSSimon Glass  *			claimed.
104d7af6a48SSimon Glass  * @bus:		ID of the bus that the slave is attached to. For
105d7af6a48SSimon Glass  *			driver model this is the sequence number of the SPI
106d7af6a48SSimon Glass  *			bus (bus->seq) so does not need to be stored
107ce22b922SJagannadha Sutradharudu Teki  * @cs:			ID of the chip select connected to the slave.
108*f5c3c033SJagan Teki  * @mode:		SPI mode to use for this slave (see SPI mode flags)
1094e09cc1eSJagannadha Sutradharudu Teki  * @op_mode_rx:		SPI RX operation mode.
1103163aaa6SJagannadha Sutradharudu Teki  * @op_mode_tx:		SPI TX operation mode.
1115753d09bSNikita Kiryanov  * @wordlen:		Size of SPI word in number of bits
112ce22b922SJagannadha Sutradharudu Teki  * @max_write_size:	If non-zero, the maximum number of bytes which can
1130c456ceeSSimon Glass  *			be written at once, excluding command bytes.
114ce22b922SJagannadha Sutradharudu Teki  * @memory_map:		Address of read-only SPI flash access.
115056fbc73SJagannadha Sutradharudu Teki  * @option:		Varies SPI bus options - separate, shared bus.
116f77f4691SJagannadha Sutradharudu Teki  * @flags:		Indication of SPI flags.
117d255bb0eSHaavard Skinnemoen  */
118d255bb0eSHaavard Skinnemoen struct spi_slave {
119d7af6a48SSimon Glass #ifdef CONFIG_DM_SPI
120d7af6a48SSimon Glass 	struct udevice *dev;	/* struct spi_slave is dev->parentdata */
121d7af6a48SSimon Glass 	uint max_hz;
12260e2809aSSimon Glass 	uint speed;
123d7af6a48SSimon Glass #else
124d255bb0eSHaavard Skinnemoen 	unsigned int bus;
125d255bb0eSHaavard Skinnemoen 	unsigned int cs;
126d0cff03eSSimon Glass #endif
127*f5c3c033SJagan Teki 	uint mode;
1284e09cc1eSJagannadha Sutradharudu Teki 	u8 op_mode_rx;
1293163aaa6SJagannadha Sutradharudu Teki 	u8 op_mode_tx;
1305753d09bSNikita Kiryanov 	unsigned int wordlen;
1310c456ceeSSimon Glass 	unsigned int max_write_size;
132004f15b6SPoddar, Sourav 	void *memory_map;
133f77f4691SJagannadha Sutradharudu Teki 	u8 option;
134f77f4691SJagannadha Sutradharudu Teki 	u8 flags;
135d255bb0eSHaavard Skinnemoen };
13677f85581Swdenk 
1371b1bd9a7SJagannadha Sutradharudu Teki /**
13877f85581Swdenk  * Initialization, must be called once on start up.
139d255bb0eSHaavard Skinnemoen  *
140d255bb0eSHaavard Skinnemoen  * TODO: I don't think we really need this.
14177f85581Swdenk  */
14277f85581Swdenk void spi_init(void);
14377f85581Swdenk 
144ba6c3ce9SSimon Glass /**
145ba6c3ce9SSimon Glass  * spi_do_alloc_slave - Allocate a new SPI slave (internal)
146ba6c3ce9SSimon Glass  *
147ba6c3ce9SSimon Glass  * Allocate and zero all fields in the spi slave, and set the bus/chip
148ba6c3ce9SSimon Glass  * select. Use the helper macro spi_alloc_slave() to call this.
149ba6c3ce9SSimon Glass  *
1501b1bd9a7SJagannadha Sutradharudu Teki  * @offset:	Offset of struct spi_slave within slave structure.
1511b1bd9a7SJagannadha Sutradharudu Teki  * @size:	Size of slave structure.
152ba6c3ce9SSimon Glass  * @bus:	Bus ID of the slave chip.
153ba6c3ce9SSimon Glass  * @cs:		Chip select ID of the slave chip on the specified bus.
154ba6c3ce9SSimon Glass  */
155ba6c3ce9SSimon Glass void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
156ba6c3ce9SSimon Glass 			 unsigned int cs);
157ba6c3ce9SSimon Glass 
158ba6c3ce9SSimon Glass /**
159ba6c3ce9SSimon Glass  * spi_alloc_slave - Allocate a new SPI slave
160ba6c3ce9SSimon Glass  *
161ba6c3ce9SSimon Glass  * Allocate and zero all fields in the spi slave, and set the bus/chip
162ba6c3ce9SSimon Glass  * select.
163ba6c3ce9SSimon Glass  *
1641b1bd9a7SJagannadha Sutradharudu Teki  * @_struct:	Name of structure to allocate (e.g. struct tegra_spi).
1651b1bd9a7SJagannadha Sutradharudu Teki  *		This structure must contain a member 'struct spi_slave *slave'.
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(_struct, bus, cs) \
170ba6c3ce9SSimon Glass 	spi_do_alloc_slave(offsetof(_struct, slave), \
171ba6c3ce9SSimon Glass 			    sizeof(_struct), bus, cs)
172ba6c3ce9SSimon Glass 
173ba6c3ce9SSimon Glass /**
174ba6c3ce9SSimon Glass  * spi_alloc_slave_base - Allocate a new SPI slave with no private data
175ba6c3ce9SSimon Glass  *
176ba6c3ce9SSimon Glass  * Allocate and zero all fields in the spi slave, and set the bus/chip
177ba6c3ce9SSimon Glass  * select.
178ba6c3ce9SSimon Glass  *
179ba6c3ce9SSimon Glass  * @bus:	Bus ID of the slave chip.
180ba6c3ce9SSimon Glass  * @cs:		Chip select ID of the slave chip on the specified bus.
181ba6c3ce9SSimon Glass  */
182ba6c3ce9SSimon Glass #define spi_alloc_slave_base(bus, cs) \
183ba6c3ce9SSimon Glass 	spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
184ba6c3ce9SSimon Glass 
1851b1bd9a7SJagannadha Sutradharudu Teki /**
186d255bb0eSHaavard Skinnemoen  * Set up communications parameters for a SPI slave.
187d255bb0eSHaavard Skinnemoen  *
188d255bb0eSHaavard Skinnemoen  * This must be called once for each slave. Note that this function
189d255bb0eSHaavard Skinnemoen  * usually doesn't touch any actual hardware, it only initializes the
190d255bb0eSHaavard Skinnemoen  * contents of spi_slave so that the hardware can be easily
191d255bb0eSHaavard Skinnemoen  * initialized later.
192d255bb0eSHaavard Skinnemoen  *
1931b1bd9a7SJagannadha Sutradharudu Teki  * @bus:	Bus ID of the slave chip.
1941b1bd9a7SJagannadha Sutradharudu Teki  * @cs:		Chip select ID of the slave chip on the specified bus.
1951b1bd9a7SJagannadha Sutradharudu Teki  * @max_hz:	Maximum SCK rate in Hz.
1961b1bd9a7SJagannadha Sutradharudu Teki  * @mode:	Clock polarity, clock phase and other parameters.
197d255bb0eSHaavard Skinnemoen  *
198d255bb0eSHaavard Skinnemoen  * Returns: A spi_slave reference that can be used in subsequent SPI
199d255bb0eSHaavard Skinnemoen  * calls, or NULL if one or more of the parameters are not supported.
200d255bb0eSHaavard Skinnemoen  */
201d255bb0eSHaavard Skinnemoen struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
202d255bb0eSHaavard Skinnemoen 		unsigned int max_hz, unsigned int mode);
203d255bb0eSHaavard Skinnemoen 
2041b1bd9a7SJagannadha Sutradharudu Teki /**
205d255bb0eSHaavard Skinnemoen  * Free any memory associated with a SPI slave.
206d255bb0eSHaavard Skinnemoen  *
2071b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
208d255bb0eSHaavard Skinnemoen  */
209d255bb0eSHaavard Skinnemoen void spi_free_slave(struct spi_slave *slave);
210d255bb0eSHaavard Skinnemoen 
2111b1bd9a7SJagannadha Sutradharudu Teki /**
212d255bb0eSHaavard Skinnemoen  * Claim the bus and prepare it for communication with a given slave.
213d255bb0eSHaavard Skinnemoen  *
214d255bb0eSHaavard Skinnemoen  * This must be called before doing any transfers with a SPI slave. It
215d255bb0eSHaavard Skinnemoen  * will enable and initialize any SPI hardware as necessary, and make
216d255bb0eSHaavard Skinnemoen  * sure that the SCK line is in the correct idle state. It is not
217d255bb0eSHaavard Skinnemoen  * allowed to claim the same bus for several slaves without releasing
218d255bb0eSHaavard Skinnemoen  * the bus in between.
219d255bb0eSHaavard Skinnemoen  *
2201b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
221d255bb0eSHaavard Skinnemoen  *
222d255bb0eSHaavard Skinnemoen  * Returns: 0 if the bus was claimed successfully, or a negative value
223d255bb0eSHaavard Skinnemoen  * if it wasn't.
224d255bb0eSHaavard Skinnemoen  */
225d255bb0eSHaavard Skinnemoen int spi_claim_bus(struct spi_slave *slave);
226d255bb0eSHaavard Skinnemoen 
2271b1bd9a7SJagannadha Sutradharudu Teki /**
228d255bb0eSHaavard Skinnemoen  * Release the SPI bus
229d255bb0eSHaavard Skinnemoen  *
230d255bb0eSHaavard Skinnemoen  * This must be called once for every call to spi_claim_bus() after
231d255bb0eSHaavard Skinnemoen  * all transfers have finished. It may disable any SPI hardware as
232d255bb0eSHaavard Skinnemoen  * appropriate.
233d255bb0eSHaavard Skinnemoen  *
2341b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
235d255bb0eSHaavard Skinnemoen  */
236d255bb0eSHaavard Skinnemoen void spi_release_bus(struct spi_slave *slave);
23777f85581Swdenk 
2381b1bd9a7SJagannadha Sutradharudu Teki /**
2395753d09bSNikita Kiryanov  * Set the word length for SPI transactions
2405753d09bSNikita Kiryanov  *
2415753d09bSNikita Kiryanov  * Set the word length (number of bits per word) for SPI transactions.
2425753d09bSNikita Kiryanov  *
2435753d09bSNikita Kiryanov  * @slave:	The SPI slave
2445753d09bSNikita Kiryanov  * @wordlen:	The number of bits in a word
2455753d09bSNikita Kiryanov  *
2465753d09bSNikita Kiryanov  * Returns: 0 on success, -1 on failure.
2475753d09bSNikita Kiryanov  */
2485753d09bSNikita Kiryanov int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
2495753d09bSNikita Kiryanov 
2505753d09bSNikita Kiryanov /**
25177f85581Swdenk  * SPI transfer
25277f85581Swdenk  *
25377f85581Swdenk  * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
25477f85581Swdenk  * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
25577f85581Swdenk  *
25677f85581Swdenk  * The source of the outgoing bits is the "dout" parameter and the
25777f85581Swdenk  * destination of the input bits is the "din" parameter.  Note that "dout"
25877f85581Swdenk  * and "din" can point to the same memory location, in which case the
25977f85581Swdenk  * input data overwrites the output data (since both are buffered by
26077f85581Swdenk  * temporary variables, this is OK).
26177f85581Swdenk  *
26277f85581Swdenk  * spi_xfer() interface:
2631b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave which will be sending/receiving the data.
2641b1bd9a7SJagannadha Sutradharudu Teki  * @bitlen:	How many bits to write and read.
2651b1bd9a7SJagannadha Sutradharudu Teki  * @dout:	Pointer to a string of bits to send out.  The bits are
26677f85581Swdenk  *		held in a byte array and are sent MSB first.
2671b1bd9a7SJagannadha Sutradharudu Teki  * @din:	Pointer to a string of bits that will be filled in.
2681b1bd9a7SJagannadha Sutradharudu Teki  * @flags:	A bitwise combination of SPI_XFER_* flags.
26977f85581Swdenk  *
27077f85581Swdenk  * Returns: 0 on success, not 0 on failure
27177f85581Swdenk  */
272d255bb0eSHaavard Skinnemoen int  spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
273d255bb0eSHaavard Skinnemoen 		void *din, unsigned long flags);
27477f85581Swdenk 
275146bad96STom Rini /* Copy memory mapped data */
276146bad96STom Rini void spi_flash_copy_mmap(void *data, void *offset, size_t len);
277146bad96STom Rini 
2781b1bd9a7SJagannadha Sutradharudu Teki /**
279d255bb0eSHaavard Skinnemoen  * Determine if a SPI chipselect is valid.
280d255bb0eSHaavard Skinnemoen  * This function is provided by the board if the low-level SPI driver
281d255bb0eSHaavard Skinnemoen  * needs it to determine if a given chipselect is actually valid.
282d255bb0eSHaavard Skinnemoen  *
283d255bb0eSHaavard Skinnemoen  * Returns: 1 if bus:cs identifies a valid chip on this board, 0
284d255bb0eSHaavard Skinnemoen  * otherwise.
285d255bb0eSHaavard Skinnemoen  */
286d255bb0eSHaavard Skinnemoen int spi_cs_is_valid(unsigned int bus, unsigned int cs);
287d255bb0eSHaavard Skinnemoen 
288d7af6a48SSimon Glass #ifndef CONFIG_DM_SPI
2891b1bd9a7SJagannadha Sutradharudu Teki /**
290d255bb0eSHaavard Skinnemoen  * Activate a SPI chipselect.
291d255bb0eSHaavard Skinnemoen  * This function is provided by the board code when using a driver
292d255bb0eSHaavard Skinnemoen  * that can't control its chipselects automatically (e.g.
293d255bb0eSHaavard Skinnemoen  * common/soft_spi.c). When called, it should activate the chip select
294d255bb0eSHaavard Skinnemoen  * to the device identified by "slave".
295d255bb0eSHaavard Skinnemoen  */
296d255bb0eSHaavard Skinnemoen void spi_cs_activate(struct spi_slave *slave);
297d255bb0eSHaavard Skinnemoen 
2981b1bd9a7SJagannadha Sutradharudu Teki /**
299d255bb0eSHaavard Skinnemoen  * Deactivate a SPI chipselect.
300d255bb0eSHaavard Skinnemoen  * This function is provided by the board code when using a driver
301d255bb0eSHaavard Skinnemoen  * that can't control its chipselects automatically (e.g.
302d255bb0eSHaavard Skinnemoen  * common/soft_spi.c). When called, it should deactivate the chip
303d255bb0eSHaavard Skinnemoen  * select to the device identified by "slave".
304d255bb0eSHaavard Skinnemoen  */
305d255bb0eSHaavard Skinnemoen void spi_cs_deactivate(struct spi_slave *slave);
306d255bb0eSHaavard Skinnemoen 
3071b1bd9a7SJagannadha Sutradharudu Teki /**
308fa1423e7SThomas Chou  * Set transfer speed.
309fa1423e7SThomas Chou  * This sets a new speed to be applied for next spi_xfer().
3101b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave
3111b1bd9a7SJagannadha Sutradharudu Teki  * @hz:		The transfer speed
312fa1423e7SThomas Chou  */
313fa1423e7SThomas Chou void spi_set_speed(struct spi_slave *slave, uint hz);
314d7af6a48SSimon Glass #endif
315fa1423e7SThomas Chou 
3161b1bd9a7SJagannadha Sutradharudu Teki /**
317d255bb0eSHaavard Skinnemoen  * Write 8 bits, then read 8 bits.
3181b1bd9a7SJagannadha Sutradharudu Teki  * @slave:	The SPI slave we're communicating with
3191b1bd9a7SJagannadha Sutradharudu Teki  * @byte:	Byte to be written
320d255bb0eSHaavard Skinnemoen  *
321d255bb0eSHaavard Skinnemoen  * Returns: The value that was read, or a negative value on error.
322d255bb0eSHaavard Skinnemoen  *
323d255bb0eSHaavard Skinnemoen  * TODO: This function probably shouldn't be inlined.
324d255bb0eSHaavard Skinnemoen  */
325d255bb0eSHaavard Skinnemoen static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
326d255bb0eSHaavard Skinnemoen {
327d255bb0eSHaavard Skinnemoen 	unsigned char dout[2];
328d255bb0eSHaavard Skinnemoen 	unsigned char din[2];
329d255bb0eSHaavard Skinnemoen 	int ret;
330d255bb0eSHaavard Skinnemoen 
331d255bb0eSHaavard Skinnemoen 	dout[0] = byte;
332d255bb0eSHaavard Skinnemoen 	dout[1] = 0;
333d255bb0eSHaavard Skinnemoen 
334d255bb0eSHaavard Skinnemoen 	ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
335d255bb0eSHaavard Skinnemoen 	return ret < 0 ? ret : din[1];
336d255bb0eSHaavard Skinnemoen }
33738254f45SGuennadi Liakhovetski 
338f3424c55SHung-ying Tyan /**
339f3424c55SHung-ying Tyan  * Set up a SPI slave for a particular device tree node
340f3424c55SHung-ying Tyan  *
341f3424c55SHung-ying Tyan  * This calls spi_setup_slave() with the correct bus number. Call
342f3424c55SHung-ying Tyan  * spi_free_slave() to free it later.
343f3424c55SHung-ying Tyan  *
344469146c0SJagannadha Sutradharudu Teki  * @param blob:		Device tree blob
3450efc0249SSimon Glass  * @param slave_node:	Slave node to use
3460efc0249SSimon Glass  * @param spi_node:	SPI peripheral node to use
347f3424c55SHung-ying Tyan  * @return pointer to new spi_slave structure
348f3424c55SHung-ying Tyan  */
3490efc0249SSimon Glass struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
3500efc0249SSimon Glass 				      int spi_node);
3510efc0249SSimon Glass 
3520efc0249SSimon Glass /**
3530efc0249SSimon Glass  * spi_base_setup_slave_fdt() - helper function to set up a SPI slace
3540efc0249SSimon Glass  *
3550efc0249SSimon Glass  * This decodes SPI properties from the slave node to determine the
3560efc0249SSimon Glass  * chip select and SPI parameters.
3570efc0249SSimon Glass  *
3580efc0249SSimon Glass  * @blob:	Device tree blob
3590efc0249SSimon Glass  * @busnum:	Bus number to use
3600efc0249SSimon Glass  * @node:	Device tree node for the SPI bus
3610efc0249SSimon Glass  */
3620efc0249SSimon Glass struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum,
3630efc0249SSimon Glass 					   int node);
364f3424c55SHung-ying Tyan 
365d7af6a48SSimon Glass #ifdef CONFIG_DM_SPI
366d7af6a48SSimon Glass 
367d7af6a48SSimon Glass /**
368d7af6a48SSimon Glass  * struct spi_cs_info - Information about a bus chip select
369d7af6a48SSimon Glass  *
370d7af6a48SSimon Glass  * @dev:	Connected device, or NULL if none
371d7af6a48SSimon Glass  */
372d7af6a48SSimon Glass struct spi_cs_info {
373d7af6a48SSimon Glass 	struct udevice *dev;
374d7af6a48SSimon Glass };
375d7af6a48SSimon Glass 
376d7af6a48SSimon Glass /**
377d7af6a48SSimon Glass  * struct struct dm_spi_ops - Driver model SPI operations
378d7af6a48SSimon Glass  *
379d7af6a48SSimon Glass  * The uclass interface is implemented by all SPI devices which use
380d7af6a48SSimon Glass  * driver model.
381d7af6a48SSimon Glass  */
382d7af6a48SSimon Glass struct dm_spi_ops {
383d7af6a48SSimon Glass 	/**
384d7af6a48SSimon Glass 	 * Claim the bus and prepare it for communication.
385d7af6a48SSimon Glass 	 *
386d7af6a48SSimon Glass 	 * The device provided is the slave device. It's parent controller
387d7af6a48SSimon Glass 	 * will be used to provide the communication.
388d7af6a48SSimon Glass 	 *
389d7af6a48SSimon Glass 	 * This must be called before doing any transfers with a SPI slave. It
390d7af6a48SSimon Glass 	 * will enable and initialize any SPI hardware as necessary, and make
391d7af6a48SSimon Glass 	 * sure that the SCK line is in the correct idle state. It is not
392d7af6a48SSimon Glass 	 * allowed to claim the same bus for several slaves without releasing
393d7af6a48SSimon Glass 	 * the bus in between.
394d7af6a48SSimon Glass 	 *
3959694b724SSimon Glass 	 * @dev:	The SPI slave
396d7af6a48SSimon Glass 	 *
397d7af6a48SSimon Glass 	 * Returns: 0 if the bus was claimed successfully, or a negative value
398d7af6a48SSimon Glass 	 * if it wasn't.
399d7af6a48SSimon Glass 	 */
4009694b724SSimon Glass 	int (*claim_bus)(struct udevice *dev);
401d7af6a48SSimon Glass 
402d7af6a48SSimon Glass 	/**
403d7af6a48SSimon Glass 	 * Release the SPI bus
404d7af6a48SSimon Glass 	 *
405d7af6a48SSimon Glass 	 * This must be called once for every call to spi_claim_bus() after
406d7af6a48SSimon Glass 	 * all transfers have finished. It may disable any SPI hardware as
407d7af6a48SSimon Glass 	 * appropriate.
408d7af6a48SSimon Glass 	 *
4099694b724SSimon Glass 	 * @dev:	The SPI slave
410d7af6a48SSimon Glass 	 */
4119694b724SSimon Glass 	int (*release_bus)(struct udevice *dev);
412d7af6a48SSimon Glass 
413d7af6a48SSimon Glass 	/**
414d7af6a48SSimon Glass 	 * Set the word length for SPI transactions
415d7af6a48SSimon Glass 	 *
416d7af6a48SSimon Glass 	 * Set the word length (number of bits per word) for SPI transactions.
417d7af6a48SSimon Glass 	 *
418d7af6a48SSimon Glass 	 * @bus:	The SPI slave
419d7af6a48SSimon Glass 	 * @wordlen:	The number of bits in a word
420d7af6a48SSimon Glass 	 *
421d7af6a48SSimon Glass 	 * Returns: 0 on success, -ve on failure.
422d7af6a48SSimon Glass 	 */
4239694b724SSimon Glass 	int (*set_wordlen)(struct udevice *dev, unsigned int wordlen);
424d7af6a48SSimon Glass 
425d7af6a48SSimon Glass 	/**
426d7af6a48SSimon Glass 	 * SPI transfer
427d7af6a48SSimon Glass 	 *
428d7af6a48SSimon Glass 	 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
429d7af6a48SSimon Glass 	 * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
430d7af6a48SSimon Glass 	 * works.
431d7af6a48SSimon Glass 	 *
432d7af6a48SSimon Glass 	 * The source of the outgoing bits is the "dout" parameter and the
433d7af6a48SSimon Glass 	 * destination of the input bits is the "din" parameter.  Note that
434d7af6a48SSimon Glass 	 * "dout" and "din" can point to the same memory location, in which
435d7af6a48SSimon Glass 	 * case the input data overwrites the output data (since both are
436d7af6a48SSimon Glass 	 * buffered by temporary variables, this is OK).
437d7af6a48SSimon Glass 	 *
438d7af6a48SSimon Glass 	 * spi_xfer() interface:
439d7af6a48SSimon Glass 	 * @dev:	The slave device to communicate with
440d7af6a48SSimon Glass 	 * @bitlen:	How many bits to write and read.
441d7af6a48SSimon Glass 	 * @dout:	Pointer to a string of bits to send out.  The bits are
442d7af6a48SSimon Glass 	 *		held in a byte array and are sent MSB first.
443d7af6a48SSimon Glass 	 * @din:	Pointer to a string of bits that will be filled in.
444d7af6a48SSimon Glass 	 * @flags:	A bitwise combination of SPI_XFER_* flags.
445d7af6a48SSimon Glass 	 *
446d7af6a48SSimon Glass 	 * Returns: 0 on success, not -1 on failure
447d7af6a48SSimon Glass 	 */
448d7af6a48SSimon Glass 	int (*xfer)(struct udevice *dev, unsigned int bitlen, const void *dout,
449d7af6a48SSimon Glass 		    void *din, unsigned long flags);
450d7af6a48SSimon Glass 
451d7af6a48SSimon Glass 	/**
452d7af6a48SSimon Glass 	 * Set transfer speed.
453d7af6a48SSimon Glass 	 * This sets a new speed to be applied for next spi_xfer().
454d7af6a48SSimon Glass 	 * @bus:	The SPI bus
455d7af6a48SSimon Glass 	 * @hz:		The transfer speed
456d7af6a48SSimon Glass 	 * @return 0 if OK, -ve on error
457d7af6a48SSimon Glass 	 */
458d7af6a48SSimon Glass 	int (*set_speed)(struct udevice *bus, uint hz);
459d7af6a48SSimon Glass 
460d7af6a48SSimon Glass 	/**
461d7af6a48SSimon Glass 	 * Set the SPI mode/flags
462d7af6a48SSimon Glass 	 *
463d7af6a48SSimon Glass 	 * It is unclear if we want to set speed and mode together instead
464d7af6a48SSimon Glass 	 * of separately.
465d7af6a48SSimon Glass 	 *
466d7af6a48SSimon Glass 	 * @bus:	The SPI bus
467d7af6a48SSimon Glass 	 * @mode:	Requested SPI mode (SPI_... flags)
468d7af6a48SSimon Glass 	 * @return 0 if OK, -ve on error
469d7af6a48SSimon Glass 	 */
470d7af6a48SSimon Glass 	int (*set_mode)(struct udevice *bus, uint mode);
471d7af6a48SSimon Glass 
472d7af6a48SSimon Glass 	/**
473d7af6a48SSimon Glass 	 * Get information on a chip select
474d7af6a48SSimon Glass 	 *
475d7af6a48SSimon Glass 	 * This is only called when the SPI uclass does not know about a
476d7af6a48SSimon Glass 	 * chip select, i.e. it has no attached device. It gives the driver
477d7af6a48SSimon Glass 	 * a chance to allow activity on that chip select even so.
478d7af6a48SSimon Glass 	 *
479d7af6a48SSimon Glass 	 * @bus:	The SPI bus
480d7af6a48SSimon Glass 	 * @cs:		The chip select (0..n-1)
481d7af6a48SSimon Glass 	 * @info:	Returns information about the chip select, if valid.
482d7af6a48SSimon Glass 	 *		On entry info->dev is NULL
483d7af6a48SSimon Glass 	 * @return 0 if OK (and @info is set up), -ENODEV if the chip select
484d7af6a48SSimon Glass 	 *	   is invalid, other -ve value on error
485d7af6a48SSimon Glass 	 */
486d7af6a48SSimon Glass 	int (*cs_info)(struct udevice *bus, uint cs, struct spi_cs_info *info);
487d7af6a48SSimon Glass };
488d7af6a48SSimon Glass 
489c60e1f25SSimon Glass struct dm_spi_emul_ops {
490c60e1f25SSimon Glass 	/**
491c60e1f25SSimon Glass 	 * SPI transfer
492c60e1f25SSimon Glass 	 *
493c60e1f25SSimon Glass 	 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
494c60e1f25SSimon Glass 	 * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
495c60e1f25SSimon Glass 	 * works. Here the device is a slave.
496c60e1f25SSimon Glass 	 *
497c60e1f25SSimon Glass 	 * The source of the outgoing bits is the "dout" parameter and the
498c60e1f25SSimon Glass 	 * destination of the input bits is the "din" parameter.  Note that
499c60e1f25SSimon Glass 	 * "dout" and "din" can point to the same memory location, in which
500c60e1f25SSimon Glass 	 * case the input data overwrites the output data (since both are
501c60e1f25SSimon Glass 	 * buffered by temporary variables, this is OK).
502c60e1f25SSimon Glass 	 *
503c60e1f25SSimon Glass 	 * spi_xfer() interface:
504c60e1f25SSimon Glass 	 * @slave:	The SPI slave which will be sending/receiving the data.
505c60e1f25SSimon Glass 	 * @bitlen:	How many bits to write and read.
506c60e1f25SSimon Glass 	 * @dout:	Pointer to a string of bits sent to the device. The
507c60e1f25SSimon Glass 	 *		bits are held in a byte array and are sent MSB first.
508c60e1f25SSimon Glass 	 * @din:	Pointer to a string of bits that will be sent back to
509c60e1f25SSimon Glass 	 *		the master.
510c60e1f25SSimon Glass 	 * @flags:	A bitwise combination of SPI_XFER_* flags.
511c60e1f25SSimon Glass 	 *
512c60e1f25SSimon Glass 	 * Returns: 0 on success, not -1 on failure
513c60e1f25SSimon Glass 	 */
514c60e1f25SSimon Glass 	int (*xfer)(struct udevice *slave, unsigned int bitlen,
515c60e1f25SSimon Glass 		    const void *dout, void *din, unsigned long flags);
516c60e1f25SSimon Glass };
517c60e1f25SSimon Glass 
518d7af6a48SSimon Glass /**
519d7af6a48SSimon Glass  * spi_find_bus_and_cs() - Find bus and slave devices by number
520d7af6a48SSimon Glass  *
521d7af6a48SSimon Glass  * Given a bus number and chip select, this finds the corresponding bus
522d7af6a48SSimon Glass  * device and slave device. Neither device is activated by this function,
523d7af6a48SSimon Glass  * although they may have been activated previously.
524d7af6a48SSimon Glass  *
525d7af6a48SSimon Glass  * @busnum:	SPI bus number
526d7af6a48SSimon Glass  * @cs:		Chip select to look for
527d7af6a48SSimon Glass  * @busp:	Returns bus device
528d7af6a48SSimon Glass  * @devp:	Return slave device
529d7af6a48SSimon Glass  * @return 0 if found, -ENODEV on error
530d7af6a48SSimon Glass  */
531d7af6a48SSimon Glass int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
532d7af6a48SSimon Glass 			struct udevice **devp);
533d7af6a48SSimon Glass 
534d7af6a48SSimon Glass /**
535d7af6a48SSimon Glass  * spi_get_bus_and_cs() - Find and activate bus and slave devices by number
536d7af6a48SSimon Glass  *
537d7af6a48SSimon Glass  * Given a bus number and chip select, this finds the corresponding bus
538d7af6a48SSimon Glass  * device and slave device.
539d7af6a48SSimon Glass  *
540d7af6a48SSimon Glass  * If no such slave exists, and drv_name is not NULL, then a new slave device
541d7af6a48SSimon Glass  * is automatically bound on this chip select.
542d7af6a48SSimon Glass  *
543d7af6a48SSimon Glass  * Ths new slave device is probed ready for use with the given speed and mode.
544d7af6a48SSimon Glass  *
545d7af6a48SSimon Glass  * @busnum:	SPI bus number
546d7af6a48SSimon Glass  * @cs:		Chip select to look for
547d7af6a48SSimon Glass  * @speed:	SPI speed to use for this slave
548d7af6a48SSimon Glass  * @mode:	SPI mode to use for this slave
549d7af6a48SSimon Glass  * @drv_name:	Name of driver to attach to this chip select
550d7af6a48SSimon Glass  * @dev_name:	Name of the new device thus created
551d7af6a48SSimon Glass  * @busp:	Returns bus device
552d7af6a48SSimon Glass  * @devp:	Return slave device
553d7af6a48SSimon Glass  * @return 0 if found, -ve on error
554d7af6a48SSimon Glass  */
555d7af6a48SSimon Glass int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
556d7af6a48SSimon Glass 			const char *drv_name, const char *dev_name,
557d7af6a48SSimon Glass 			struct udevice **busp, struct spi_slave **devp);
558d7af6a48SSimon Glass 
559d7af6a48SSimon Glass /**
560d7af6a48SSimon Glass  * spi_chip_select() - Get the chip select for a slave
561d7af6a48SSimon Glass  *
562d7af6a48SSimon Glass  * @return the chip select this slave is attached to
563d7af6a48SSimon Glass  */
564d7af6a48SSimon Glass int spi_chip_select(struct udevice *slave);
565d7af6a48SSimon Glass 
566d7af6a48SSimon Glass /**
567ff56bba2SSimon Glass  * spi_find_chip_select() - Find the slave attached to chip select
568ff56bba2SSimon Glass  *
569ff56bba2SSimon Glass  * @bus:	SPI bus to search
570ff56bba2SSimon Glass  * @cs:		Chip select to look for
571ff56bba2SSimon Glass  * @devp:	Returns the slave device if found
572ff56bba2SSimon Glass  * @return 0 if found, -ENODEV on error
573ff56bba2SSimon Glass  */
574ff56bba2SSimon Glass int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp);
575ff56bba2SSimon Glass 
576ff56bba2SSimon Glass /**
577d0cff03eSSimon Glass  * spi_slave_ofdata_to_platdata() - decode standard SPI platform data
578d7af6a48SSimon Glass  *
579d0cff03eSSimon Glass  * This decodes the speed and mode for a slave from a device tree node
580d7af6a48SSimon Glass  *
581d7af6a48SSimon Glass  * @blob:	Device tree blob
582d7af6a48SSimon Glass  * @node:	Node offset to read from
583d0cff03eSSimon Glass  * @plat:	Place to put the decoded information
584d7af6a48SSimon Glass  */
585d0cff03eSSimon Glass int spi_slave_ofdata_to_platdata(const void *blob, int node,
586d0cff03eSSimon Glass 				 struct dm_spi_slave_platdata *plat);
587d7af6a48SSimon Glass 
588d7af6a48SSimon Glass /**
589d7af6a48SSimon Glass  * spi_cs_info() - Check information on a chip select
590d7af6a48SSimon Glass  *
591d7af6a48SSimon Glass  * This checks a particular chip select on a bus to see if it has a device
592d7af6a48SSimon Glass  * attached, or is even valid.
593d7af6a48SSimon Glass  *
594d7af6a48SSimon Glass  * @bus:	The SPI bus
595d7af6a48SSimon Glass  * @cs:		The chip select (0..n-1)
596d7af6a48SSimon Glass  * @info:	Returns information about the chip select, if valid
597d7af6a48SSimon Glass  * @return 0 if OK (and @info is set up), -ENODEV if the chip select
598d7af6a48SSimon Glass  *	   is invalid, other -ve value on error
599d7af6a48SSimon Glass  */
600d7af6a48SSimon Glass int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info);
601d7af6a48SSimon Glass 
602d7af6a48SSimon Glass struct sandbox_state;
603c60e1f25SSimon Glass 
604c60e1f25SSimon Glass /**
605c60e1f25SSimon Glass  * sandbox_spi_get_emul() - get an emulator for a SPI slave
606c60e1f25SSimon Glass  *
607c60e1f25SSimon Glass  * This provides a way to attach an emulated SPI device to a particular SPI
608c60e1f25SSimon Glass  * slave, so that xfer() operations on the slave will be handled by the
609c60e1f25SSimon Glass  * emulator. If a emulator already exists on that chip select it is returned.
610c60e1f25SSimon Glass  * Otherwise one is created.
611c60e1f25SSimon Glass  *
612c60e1f25SSimon Glass  * @state:	Sandbox state
613c60e1f25SSimon Glass  * @bus:	SPI bus requesting the emulator
614c60e1f25SSimon Glass  * @slave:	SPI slave device requesting the emulator
615c60e1f25SSimon Glass  * @emuip:	Returns pointer to emulator
616c60e1f25SSimon Glass  * @return 0 if OK, -ve on error
617c60e1f25SSimon Glass  */
618d7af6a48SSimon Glass int sandbox_spi_get_emul(struct sandbox_state *state,
619d7af6a48SSimon Glass 			 struct udevice *bus, struct udevice *slave,
620d7af6a48SSimon Glass 			 struct udevice **emulp);
621d7af6a48SSimon Glass 
622bc5701e1SSimon Glass /* Access the operations for a SPI device */
623d7af6a48SSimon Glass #define spi_get_ops(dev)	((struct dm_spi_ops *)(dev)->driver->ops)
624c60e1f25SSimon Glass #define spi_emul_get_ops(dev)	((struct dm_spi_emul_ops *)(dev)->driver->ops)
625d7af6a48SSimon Glass #endif /* CONFIG_DM_SPI */
626d7af6a48SSimon Glass 
62777f85581Swdenk #endif	/* _SPI_H_ */
628