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