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