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