Blame view

include/spi.h 22.5 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  /* SPDX-License-Identifier: GPL-2.0+ */
77f855819   wdenk   Initial revision
2
  /*
469146c09   Jagannadha Sutradharudu Teki   sf: Minor cleanups.
3
4
   * Common SPI Interface: Controller-specific definitions
   *
77f855819   wdenk   Initial revision
5
6
   * (C) Copyright 2001
   * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
77f855819   wdenk   Initial revision
7
8
9
10
   */
  
  #ifndef _SPI_H_
  #define _SPI_H_
d13f5b254   Boris Brezillon   spi: Extend the c...
11
  #include <common.h>
38254f45b   Guennadi Liakhovetski   New i.MX31 SPI dr...
12
  /* SPI mode flags */
465c00d78   Jagan Teki   spi: Minor cleanup
13
14
15
16
17
18
19
20
21
22
23
24
  #define SPI_CPHA	BIT(0)			/* clock phase */
  #define SPI_CPOL	BIT(1)			/* clock polarity */
  #define SPI_MODE_0	(0|0)			/* (original MicroWire) */
  #define SPI_MODE_1	(0|SPI_CPHA)
  #define SPI_MODE_2	(SPI_CPOL|0)
  #define SPI_MODE_3	(SPI_CPOL|SPI_CPHA)
  #define SPI_CS_HIGH	BIT(2)			/* CS active high */
  #define SPI_LSB_FIRST	BIT(3)			/* per-word bits-on-wire */
  #define SPI_3WIRE	BIT(4)			/* SI/SO signals shared */
  #define SPI_LOOP	BIT(5)			/* loopback mode */
  #define SPI_SLAVE	BIT(6)			/* slave mode */
  #define SPI_PREAMBLE	BIT(7)			/* Skip preamble bytes */
29ee0262e   Jagan Teki   spi: Use BIT macro
25
  #define SPI_TX_BYTE	BIT(8)			/* transmit with 1 wire byte */
2b11a41ce   Jagan Teki   spi: Add SPI_TX_D...
26
27
  #define SPI_TX_DUAL	BIT(9)			/* transmit with 2 wires */
  #define SPI_TX_QUAD	BIT(10)			/* transmit with 4 wires */
08fe9c294   Jagan Teki   spi: Use mode for...
28
  #define SPI_RX_SLOW	BIT(11)			/* receive with 1 wire slow */
3ac48d0e8   Jagan Teki   spi: Remove SPI_R...
29
30
  #define SPI_RX_DUAL	BIT(12)			/* receive with 2 wires */
  #define SPI_RX_QUAD	BIT(13)			/* receive with 4 wires */
658df8bd9   Vignesh Raghavendra   mtd: spi-nor-core...
31
32
  #define SPI_TX_OCTAL	BIT(14)			/* transmit with 8 wires */
  #define SPI_RX_OCTAL	BIT(15)			/* receive with 8 wires */
4e09cc1e2   Jagannadha Sutradharudu Teki   sf: Add extended ...
33

bb786b84b   Rajeshwari Shinde   spi: Add support ...
34
  /* Header byte that marks the start of the message */
ce22b922d   Jagannadha Sutradharudu Teki   sf: Minor cleanups
35
  #define SPI_PREAMBLE_END_BYTE	0xec
bb786b84b   Rajeshwari Shinde   spi: Add support ...
36

5d69df354   Jagan Teki   spi/sf: Minor cle...
37
  #define SPI_DEFAULT_WORDLEN	8
5753d09b1   Nikita Kiryanov   spi: omap3: add s...
38

d7af6a485   Simon Glass   dm: spi: Add a uc...
39
  #ifdef CONFIG_DM_SPI
d0cff03e1   Simon Glass   dm: spi: Move sla...
40
  /* TODO(sjg@chromium.org): Remove this and use max_hz from struct spi_slave */
d7af6a485   Simon Glass   dm: spi: Add a uc...
41
42
43
  struct dm_spi_bus {
  	uint max_hz;
  };
d0cff03e1   Simon Glass   dm: spi: Move sla...
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  /**
   * struct dm_spi_platdata - platform data for all SPI slaves
   *
   * This describes a SPI slave, a child device of the SPI bus. To obtain this
   * struct from a spi_slave, use dev_get_parent_platdata(dev) or
   * dev_get_parent_platdata(slave->dev).
   *
   * This data is immuatable. Each time the device is probed, @max_hz and @mode
   * will be copied to struct spi_slave.
   *
   * @cs:		Chip select number (0..n-1)
   * @max_hz:	Maximum bus speed that this slave can tolerate
   * @mode:	SPI mode to use for this device (see SPI mode flags)
   */
  struct dm_spi_slave_platdata {
  	unsigned int cs;
  	uint max_hz;
  	uint mode;
  };
d7af6a485   Simon Glass   dm: spi: Add a uc...
63
  #endif /* CONFIG_DM_SPI */
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
64
  /**
ce22b922d   Jagannadha Sutradharudu Teki   sf: Minor cleanups
65
   * struct spi_slave - Representation of a SPI slave
d255bb0e7   Haavard Skinnemoen   SPI API improvements
66
   *
d7af6a485   Simon Glass   dm: spi: Add a uc...
67
   * For driver model this is the per-child data used by the SPI bus. It can
bcbe3d157   Simon Glass   dm: Rename dev_ge...
68
   * be accessed using dev_get_parent_priv() on the slave device. The SPI uclass
d0cff03e1   Simon Glass   dm: spi: Move sla...
69
70
71
72
   * sets uip per_child_auto_alloc_size to sizeof(struct spi_slave), and the
   * driver should not override it. Two platform data fields (max_hz and mode)
   * are copied into this structure to provide an initial value. This allows
   * them to be changed, since we should never change platform data in drivers.
d255bb0e7   Haavard Skinnemoen   SPI API improvements
73
   *
d7af6a485   Simon Glass   dm: spi: Add a uc...
74
75
76
77
78
   * If not using driver model, drivers are expected to extend this with
   * controller-specific data.
   *
   * @dev:		SPI slave device
   * @max_hz:		Maximum speed for this slave
60e2809a8   Simon Glass   dm: spi: Avoid se...
79
80
   * @speed:		Current bus speed. This is 0 until the bus is first
   *			claimed.
d7af6a485   Simon Glass   dm: spi: Add a uc...
81
82
83
   * @bus:		ID of the bus that the slave is attached to. For
   *			driver model this is the sequence number of the SPI
   *			bus (bus->seq) so does not need to be stored
ce22b922d   Jagannadha Sutradharudu Teki   sf: Minor cleanups
84
   * @cs:			ID of the chip select connected to the slave.
f5c3c033f   Jagan Teki   spi: make mode vi...
85
   * @mode:		SPI mode to use for this slave (see SPI mode flags)
5753d09b1   Nikita Kiryanov   spi: omap3: add s...
86
   * @wordlen:		Size of SPI word in number of bits
8af74edc3   Álvaro Fernández Rojas   drivers: spi: all...
87
88
   * @max_read_size:	If non-zero, the maximum number of bytes which can
   *			be read at once.
ce22b922d   Jagannadha Sutradharudu Teki   sf: Minor cleanups
89
   * @max_write_size:	If non-zero, the maximum number of bytes which can
6c94bd12c   Álvaro Fernández Rojas   drivers: spi: con...
90
   *			be written at once.
ce22b922d   Jagannadha Sutradharudu Teki   sf: Minor cleanups
91
   * @memory_map:		Address of read-only SPI flash access.
f77f46911   Jagannadha Sutradharudu Teki   sf: Add dual memo...
92
   * @flags:		Indication of SPI flags.
d255bb0e7   Haavard Skinnemoen   SPI API improvements
93
94
   */
  struct spi_slave {
d7af6a485   Simon Glass   dm: spi: Add a uc...
95
96
97
  #ifdef CONFIG_DM_SPI
  	struct udevice *dev;	/* struct spi_slave is dev->parentdata */
  	uint max_hz;
60e2809a8   Simon Glass   dm: spi: Avoid se...
98
  	uint speed;
d7af6a485   Simon Glass   dm: spi: Add a uc...
99
  #else
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
100
101
  	unsigned int bus;
  	unsigned int cs;
d0cff03e1   Simon Glass   dm: spi: Move sla...
102
  #endif
f5c3c033f   Jagan Teki   spi: make mode vi...
103
  	uint mode;
5753d09b1   Nikita Kiryanov   spi: omap3: add s...
104
  	unsigned int wordlen;
8af74edc3   Álvaro Fernández Rojas   drivers: spi: all...
105
  	unsigned int max_read_size;
0c456cee9   Simon Glass   spi: Add paramete...
106
  	unsigned int max_write_size;
004f15b60   Poddar, Sourav   sf: Add memory ma...
107
  	void *memory_map;
c40f60036   Jagan Teki   spi: Move flags m...
108

f77f46911   Jagannadha Sutradharudu Teki   sf: Add dual memo...
109
  	u8 flags;
29ee0262e   Jagan Teki   spi: Use BIT macro
110
111
  #define SPI_XFER_BEGIN		BIT(0)	/* Assert CS before transfer */
  #define SPI_XFER_END		BIT(1)	/* Deassert CS after transfer */
c40f60036   Jagan Teki   spi: Move flags m...
112
  #define SPI_XFER_ONCE		(SPI_XFER_BEGIN | SPI_XFER_END)
29ee0262e   Jagan Teki   spi: Use BIT macro
113
114
  #define SPI_XFER_MMAP		BIT(2)	/* Memory Mapped start */
  #define SPI_XFER_MMAP_END	BIT(3)	/* Memory Mapped End */
d255bb0e7   Haavard Skinnemoen   SPI API improvements
115
  };
77f855819   wdenk   Initial revision
116

1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
117
  /**
ba6c3ce9b   Simon Glass   spi: Add function...
118
119
120
121
122
   * spi_do_alloc_slave - Allocate a new SPI slave (internal)
   *
   * Allocate and zero all fields in the spi slave, and set the bus/chip
   * select. Use the helper macro spi_alloc_slave() to call this.
   *
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
123
124
125
126
   * @offset:	Offset of struct spi_slave within slave structure.
   * @size:	Size of slave structure.
   * @bus:	Bus ID of the slave chip.
   * @cs:		Chip select ID of the slave chip on the specified bus.
ba6c3ce9b   Simon Glass   spi: Add function...
127
128
129
130
131
132
133
134
135
136
   */
  void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
  			 unsigned int cs);
  
  /**
   * spi_alloc_slave - Allocate a new SPI slave
   *
   * Allocate and zero all fields in the spi slave, and set the bus/chip
   * select.
   *
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
137
138
139
140
   * @_struct:	Name of structure to allocate (e.g. struct tegra_spi).
   *		This structure must contain a member 'struct spi_slave *slave'.
   * @bus:	Bus ID of the slave chip.
   * @cs:		Chip select ID of the slave chip on the specified bus.
ba6c3ce9b   Simon Glass   spi: Add function...
141
142
143
144
145
146
147
148
149
150
151
   */
  #define spi_alloc_slave(_struct, bus, cs) \
  	spi_do_alloc_slave(offsetof(_struct, slave), \
  			    sizeof(_struct), bus, cs)
  
  /**
   * spi_alloc_slave_base - Allocate a new SPI slave with no private data
   *
   * Allocate and zero all fields in the spi slave, and set the bus/chip
   * select.
   *
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
152
153
   * @bus:	Bus ID of the slave chip.
   * @cs:		Chip select ID of the slave chip on the specified bus.
ba6c3ce9b   Simon Glass   spi: Add function...
154
155
156
   */
  #define spi_alloc_slave_base(bus, cs) \
  	spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
157
  /**
d255bb0e7   Haavard Skinnemoen   SPI API improvements
158
159
160
161
162
163
164
   * Set up communications parameters for a SPI slave.
   *
   * This must be called once for each slave. Note that this function
   * usually doesn't touch any actual hardware, it only initializes the
   * contents of spi_slave so that the hardware can be easily
   * initialized later.
   *
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
165
166
167
168
   * @bus:	Bus ID of the slave chip.
   * @cs:		Chip select ID of the slave chip on the specified bus.
   * @max_hz:	Maximum SCK rate in Hz.
   * @mode:	Clock polarity, clock phase and other parameters.
d255bb0e7   Haavard Skinnemoen   SPI API improvements
169
170
171
172
173
174
   *
   * Returns: A spi_slave reference that can be used in subsequent SPI
   * calls, or NULL if one or more of the parameters are not supported.
   */
  struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  		unsigned int max_hz, unsigned int mode);
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
175
  /**
d255bb0e7   Haavard Skinnemoen   SPI API improvements
176
177
   * Free any memory associated with a SPI slave.
   *
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
178
   * @slave:	The SPI slave
d255bb0e7   Haavard Skinnemoen   SPI API improvements
179
180
   */
  void spi_free_slave(struct spi_slave *slave);
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
181
  /**
d255bb0e7   Haavard Skinnemoen   SPI API improvements
182
183
184
185
186
187
188
189
   * Claim the bus and prepare it for communication with a given slave.
   *
   * This must be called before doing any transfers with a SPI slave. It
   * will enable and initialize any SPI hardware as necessary, and make
   * sure that the SCK line is in the correct idle state. It is not
   * allowed to claim the same bus for several slaves without releasing
   * the bus in between.
   *
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
190
   * @slave:	The SPI slave
d255bb0e7   Haavard Skinnemoen   SPI API improvements
191
192
193
194
195
   *
   * Returns: 0 if the bus was claimed successfully, or a negative value
   * if it wasn't.
   */
  int spi_claim_bus(struct spi_slave *slave);
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
196
  /**
d255bb0e7   Haavard Skinnemoen   SPI API improvements
197
198
199
200
201
202
   * Release the SPI bus
   *
   * This must be called once for every call to spi_claim_bus() after
   * all transfers have finished. It may disable any SPI hardware as
   * appropriate.
   *
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
203
   * @slave:	The SPI slave
d255bb0e7   Haavard Skinnemoen   SPI API improvements
204
205
   */
  void spi_release_bus(struct spi_slave *slave);
77f855819   wdenk   Initial revision
206

1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
207
  /**
5753d09b1   Nikita Kiryanov   spi: omap3: add s...
208
209
210
211
212
213
214
215
216
217
218
219
   * Set the word length for SPI transactions
   *
   * Set the word length (number of bits per word) for SPI transactions.
   *
   * @slave:	The SPI slave
   * @wordlen:	The number of bits in a word
   *
   * Returns: 0 on success, -1 on failure.
   */
  int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
  
  /**
ccdabd895   Simon Glass   spi: Correct oper...
220
   * SPI transfer (optional if mem_ops is used)
77f855819   wdenk   Initial revision
221
222
223
224
225
226
227
228
229
230
   *
   * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
   * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
   *
   * The source of the outgoing bits is the "dout" parameter and the
   * destination of the input bits is the "din" parameter.  Note that "dout"
   * and "din" can point to the same memory location, in which case the
   * input data overwrites the output data (since both are buffered by
   * temporary variables, this is OK).
   *
77f855819   wdenk   Initial revision
231
   * spi_xfer() interface:
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
232
233
234
   * @slave:	The SPI slave which will be sending/receiving the data.
   * @bitlen:	How many bits to write and read.
   * @dout:	Pointer to a string of bits to send out.  The bits are
d255bb0e7   Haavard Skinnemoen   SPI API improvements
235
   *		held in a byte array and are sent MSB first.
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
236
237
   * @din:	Pointer to a string of bits that will be filled in.
   * @flags:	A bitwise combination of SPI_XFER_* flags.
77f855819   wdenk   Initial revision
238
   *
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
239
   * Returns: 0 on success, not 0 on failure
77f855819   wdenk   Initial revision
240
   */
d255bb0e7   Haavard Skinnemoen   SPI API improvements
241
242
  int  spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  		void *din, unsigned long flags);
8473b3212   Jagan Teki   spi: Add spi_writ...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  /**
   * spi_write_then_read - SPI synchronous write followed by read
   *
   * This performs a half duplex transaction in which the first transaction
   * is to send the opcode and if the length of buf is non-zero then it start
   * the second transaction as tx or rx based on the need from respective slave.
   *
   * @slave:	The SPI slave device with which opcode/data will be exchanged
   * @opcode:	opcode used for specific transfer
   * @n_opcode:	size of opcode, in bytes
   * @txbuf:	buffer into which data to be written
   * @rxbuf:	buffer into which data will be read
   * @n_buf:	size of buf (whether it's [tx|rx]buf), in bytes
   *
   * Returns: 0 on success, not 0 on failure
   */
  int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
  			size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
  			size_t n_buf);
146bad961   Tom Rini   sf: ops: Add spi_...
262
263
  /* Copy memory mapped data */
  void spi_flash_copy_mmap(void *data, void *offset, size_t len);
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
264
  /**
d255bb0e7   Haavard Skinnemoen   SPI API improvements
265
266
267
268
269
270
271
   * Determine if a SPI chipselect is valid.
   * This function is provided by the board if the low-level SPI driver
   * needs it to determine if a given chipselect is actually valid.
   *
   * Returns: 1 if bus:cs identifies a valid chip on this board, 0
   * otherwise.
   */
d7af6a485   Simon Glass   dm: spi: Add a uc...
272
  int spi_cs_is_valid(unsigned int bus, unsigned int cs);
d255bb0e7   Haavard Skinnemoen   SPI API improvements
273

d7af6a485   Simon Glass   dm: spi: Add a uc...
274
  #ifndef CONFIG_DM_SPI
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
275
  /**
d255bb0e7   Haavard Skinnemoen   SPI API improvements
276
277
278
279
280
281
282
   * Activate a SPI chipselect.
   * This function is provided by the board code when using a driver
   * that can't control its chipselects automatically (e.g.
   * common/soft_spi.c). When called, it should activate the chip select
   * to the device identified by "slave".
   */
  void spi_cs_activate(struct spi_slave *slave);
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
283
  /**
d255bb0e7   Haavard Skinnemoen   SPI API improvements
284
285
286
287
288
289
290
   * Deactivate a SPI chipselect.
   * This function is provided by the board code when using a driver
   * that can't control its chipselects automatically (e.g.
   * common/soft_spi.c). When called, it should deactivate the chip
   * select to the device identified by "slave".
   */
  void spi_cs_deactivate(struct spi_slave *slave);
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
291
  /**
fa1423e70   Thomas Chou   spi: add spi_set_...
292
293
   * Set transfer speed.
   * This sets a new speed to be applied for next spi_xfer().
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
294
295
   * @slave:	The SPI slave
   * @hz:		The transfer speed
fa1423e70   Thomas Chou   spi: add spi_set_...
296
297
   */
  void spi_set_speed(struct spi_slave *slave, uint hz);
d7af6a485   Simon Glass   dm: spi: Add a uc...
298
  #endif
fa1423e70   Thomas Chou   spi: add spi_set_...
299

1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
300
  /**
d255bb0e7   Haavard Skinnemoen   SPI API improvements
301
   * Write 8 bits, then read 8 bits.
1b1bd9a7b   Jagannadha Sutradharudu Teki   spi: spi cleanups
302
303
   * @slave:	The SPI slave we're communicating with
   * @byte:	Byte to be written
d255bb0e7   Haavard Skinnemoen   SPI API improvements
304
305
306
307
308
309
310
311
312
313
314
315
316
   *
   * Returns: The value that was read, or a negative value on error.
   *
   * TODO: This function probably shouldn't be inlined.
   */
  static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
  {
  	unsigned char dout[2];
  	unsigned char din[2];
  	int ret;
  
  	dout[0] = byte;
  	dout[1] = 0;
38254f45b   Guennadi Liakhovetski   New i.MX31 SPI dr...
317

d255bb0e7   Haavard Skinnemoen   SPI API improvements
318
319
320
  	ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
  	return ret < 0 ? ret : din[1];
  }
77f855819   wdenk   Initial revision
321

d7af6a485   Simon Glass   dm: spi: Add a uc...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
  #ifdef CONFIG_DM_SPI
  
  /**
   * struct spi_cs_info - Information about a bus chip select
   *
   * @dev:	Connected device, or NULL if none
   */
  struct spi_cs_info {
  	struct udevice *dev;
  };
  
  /**
   * struct struct dm_spi_ops - Driver model SPI operations
   *
   * The uclass interface is implemented by all SPI devices which use
   * driver model.
   */
  struct dm_spi_ops {
  	/**
  	 * Claim the bus and prepare it for communication.
  	 *
  	 * The device provided is the slave device. It's parent controller
  	 * will be used to provide the communication.
  	 *
  	 * This must be called before doing any transfers with a SPI slave. It
  	 * will enable and initialize any SPI hardware as necessary, and make
  	 * sure that the SCK line is in the correct idle state. It is not
  	 * allowed to claim the same bus for several slaves without releasing
  	 * the bus in between.
  	 *
9694b7244   Simon Glass   dm: spi: Correct ...
352
  	 * @dev:	The SPI slave
d7af6a485   Simon Glass   dm: spi: Add a uc...
353
354
355
356
  	 *
  	 * Returns: 0 if the bus was claimed successfully, or a negative value
  	 * if it wasn't.
  	 */
9694b7244   Simon Glass   dm: spi: Correct ...
357
  	int (*claim_bus)(struct udevice *dev);
d7af6a485   Simon Glass   dm: spi: Add a uc...
358
359
360
361
362
363
364
365
  
  	/**
  	 * Release the SPI bus
  	 *
  	 * This must be called once for every call to spi_claim_bus() after
  	 * all transfers have finished. It may disable any SPI hardware as
  	 * appropriate.
  	 *
9694b7244   Simon Glass   dm: spi: Correct ...
366
  	 * @dev:	The SPI slave
d7af6a485   Simon Glass   dm: spi: Add a uc...
367
  	 */
9694b7244   Simon Glass   dm: spi: Correct ...
368
  	int (*release_bus)(struct udevice *dev);
d7af6a485   Simon Glass   dm: spi: Add a uc...
369
370
371
372
373
374
375
376
377
378
379
  
  	/**
  	 * Set the word length for SPI transactions
  	 *
  	 * Set the word length (number of bits per word) for SPI transactions.
  	 *
  	 * @bus:	The SPI slave
  	 * @wordlen:	The number of bits in a word
  	 *
  	 * Returns: 0 on success, -ve on failure.
  	 */
9694b7244   Simon Glass   dm: spi: Correct ...
380
  	int (*set_wordlen)(struct udevice *dev, unsigned int wordlen);
d7af6a485   Simon Glass   dm: spi: Add a uc...
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
  
  	/**
  	 * SPI transfer
  	 *
  	 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
  	 * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
  	 * works.
  	 *
  	 * The source of the outgoing bits is the "dout" parameter and the
  	 * destination of the input bits is the "din" parameter.  Note that
  	 * "dout" and "din" can point to the same memory location, in which
  	 * case the input data overwrites the output data (since both are
  	 * buffered by temporary variables, this is OK).
  	 *
  	 * spi_xfer() interface:
  	 * @dev:	The slave device to communicate with
  	 * @bitlen:	How many bits to write and read.
  	 * @dout:	Pointer to a string of bits to send out.  The bits are
  	 *		held in a byte array and are sent MSB first.
  	 * @din:	Pointer to a string of bits that will be filled in.
  	 * @flags:	A bitwise combination of SPI_XFER_* flags.
  	 *
  	 * Returns: 0 on success, not -1 on failure
  	 */
  	int (*xfer)(struct udevice *dev, unsigned int bitlen, const void *dout,
  		    void *din, unsigned long flags);
  
  	/**
d13f5b254   Boris Brezillon   spi: Extend the c...
409
410
411
412
413
414
415
416
417
  	 * Optimized handlers for SPI memory-like operations.
  	 *
  	 * Optimized/dedicated operations for interactions with SPI memory. This
  	 * field is optional and should only be implemented if the controller
  	 * has native support for memory like operations.
  	 */
  	const struct spi_controller_mem_ops *mem_ops;
  
  	/**
d7af6a485   Simon Glass   dm: spi: Add a uc...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  	 * Set transfer speed.
  	 * This sets a new speed to be applied for next spi_xfer().
  	 * @bus:	The SPI bus
  	 * @hz:		The transfer speed
  	 * @return 0 if OK, -ve on error
  	 */
  	int (*set_speed)(struct udevice *bus, uint hz);
  
  	/**
  	 * Set the SPI mode/flags
  	 *
  	 * It is unclear if we want to set speed and mode together instead
  	 * of separately.
  	 *
  	 * @bus:	The SPI bus
  	 * @mode:	Requested SPI mode (SPI_... flags)
  	 * @return 0 if OK, -ve on error
  	 */
  	int (*set_mode)(struct udevice *bus, uint mode);
  
  	/**
  	 * Get information on a chip select
  	 *
  	 * This is only called when the SPI uclass does not know about a
  	 * chip select, i.e. it has no attached device. It gives the driver
  	 * a chance to allow activity on that chip select even so.
  	 *
  	 * @bus:	The SPI bus
  	 * @cs:		The chip select (0..n-1)
  	 * @info:	Returns information about the chip select, if valid.
  	 *		On entry info->dev is NULL
4b0600039   Bin Meng   dm: spi: Change c...
449
  	 * @return 0 if OK (and @info is set up), -EINVAL if the chip select
d7af6a485   Simon Glass   dm: spi: Add a uc...
450
451
452
  	 *	   is invalid, other -ve value on error
  	 */
  	int (*cs_info)(struct udevice *bus, uint cs, struct spi_cs_info *info);
c53b318e1   Simon Glass   spi: Add support ...
453
454
455
456
457
458
459
460
461
462
463
464
465
  
  	/**
  	 * get_mmap() - Get memory-mapped SPI
  	 *
  	 * @dev:	The SPI flash slave device
  	 * @map_basep:	Returns base memory address for mapped SPI
  	 * @map_sizep:	Returns size of mapped SPI
  	 * @offsetp:	Returns start offset of SPI flash where the map works
  	 *	correctly (offsets before this are not visible)
  	 * @return 0 if OK, -EFAULT if memory mapping is not available
  	 */
  	int (*get_mmap)(struct udevice *dev, ulong *map_basep,
  			uint *map_sizep, uint *offsetp);
d7af6a485   Simon Glass   dm: spi: Add a uc...
466
  };
c60e1f254   Simon Glass   dm: sandbox: Add ...
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
  struct dm_spi_emul_ops {
  	/**
  	 * SPI transfer
  	 *
  	 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
  	 * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
  	 * works. Here the device is a slave.
  	 *
  	 * The source of the outgoing bits is the "dout" parameter and the
  	 * destination of the input bits is the "din" parameter.  Note that
  	 * "dout" and "din" can point to the same memory location, in which
  	 * case the input data overwrites the output data (since both are
  	 * buffered by temporary variables, this is OK).
  	 *
  	 * spi_xfer() interface:
  	 * @slave:	The SPI slave which will be sending/receiving the data.
  	 * @bitlen:	How many bits to write and read.
  	 * @dout:	Pointer to a string of bits sent to the device. The
  	 *		bits are held in a byte array and are sent MSB first.
  	 * @din:	Pointer to a string of bits that will be sent back to
  	 *		the master.
  	 * @flags:	A bitwise combination of SPI_XFER_* flags.
  	 *
  	 * Returns: 0 on success, not -1 on failure
  	 */
  	int (*xfer)(struct udevice *slave, unsigned int bitlen,
  		    const void *dout, void *din, unsigned long flags);
  };
d7af6a485   Simon Glass   dm: spi: Add a uc...
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
  /**
   * spi_find_bus_and_cs() - Find bus and slave devices by number
   *
   * Given a bus number and chip select, this finds the corresponding bus
   * device and slave device. Neither device is activated by this function,
   * although they may have been activated previously.
   *
   * @busnum:	SPI bus number
   * @cs:		Chip select to look for
   * @busp:	Returns bus device
   * @devp:	Return slave device
   * @return 0 if found, -ENODEV on error
   */
  int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
  			struct udevice **devp);
  
  /**
   * spi_get_bus_and_cs() - Find and activate bus and slave devices by number
   *
   * Given a bus number and chip select, this finds the corresponding bus
   * device and slave device.
   *
   * If no such slave exists, and drv_name is not NULL, then a new slave device
b0cc1b846   Patrick Delaunay   dm: spi: Read def...
518
   * is automatically bound on this chip select with requested speed and mode.
d7af6a485   Simon Glass   dm: spi: Add a uc...
519
   *
b0cc1b846   Patrick Delaunay   dm: spi: Read def...
520
521
   * Ths new slave device is probed ready for use with the speed and mode
   * from platdata when available or the requested values.
d7af6a485   Simon Glass   dm: spi: Add a uc...
522
523
524
   *
   * @busnum:	SPI bus number
   * @cs:		Chip select to look for
b0cc1b846   Patrick Delaunay   dm: spi: Read def...
525
526
   * @speed:	SPI speed to use for this slave when not available in platdata
   * @mode:	SPI mode to use for this slave when not available in platdata
d7af6a485   Simon Glass   dm: spi: Add a uc...
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
   * @drv_name:	Name of driver to attach to this chip select
   * @dev_name:	Name of the new device thus created
   * @busp:	Returns bus device
   * @devp:	Return slave device
   * @return 0 if found, -ve on error
   */
  int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
  			const char *drv_name, const char *dev_name,
  			struct udevice **busp, struct spi_slave **devp);
  
  /**
   * spi_chip_select() - Get the chip select for a slave
   *
   * @return the chip select this slave is attached to
   */
  int spi_chip_select(struct udevice *slave);
  
  /**
ff56bba2d   Simon Glass   dm: spi: Correct ...
545
546
547
548
549
   * spi_find_chip_select() - Find the slave attached to chip select
   *
   * @bus:	SPI bus to search
   * @cs:		Chip select to look for
   * @devp:	Returns the slave device if found
7bacce524   Bin Meng   dm: spi: Check cs...
550
551
   * @return 0 if found, -EINVAL if cs is invalid, -ENODEV if no device attached,
   *	   other -ve value on error
ff56bba2d   Simon Glass   dm: spi: Correct ...
552
553
554
555
   */
  int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp);
  
  /**
d0cff03e1   Simon Glass   dm: spi: Move sla...
556
   * spi_slave_ofdata_to_platdata() - decode standard SPI platform data
d7af6a485   Simon Glass   dm: spi: Add a uc...
557
   *
d0cff03e1   Simon Glass   dm: spi: Move sla...
558
   * This decodes the speed and mode for a slave from a device tree node
d7af6a485   Simon Glass   dm: spi: Add a uc...
559
560
561
   *
   * @blob:	Device tree blob
   * @node:	Node offset to read from
d0cff03e1   Simon Glass   dm: spi: Move sla...
562
   * @plat:	Place to put the decoded information
d7af6a485   Simon Glass   dm: spi: Add a uc...
563
   */
279e26f5a   Simon Glass   dm: spi: Convert ...
564
  int spi_slave_ofdata_to_platdata(struct udevice *dev,
d0cff03e1   Simon Glass   dm: spi: Move sla...
565
  				 struct dm_spi_slave_platdata *plat);
d7af6a485   Simon Glass   dm: spi: Add a uc...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
  
  /**
   * spi_cs_info() - Check information on a chip select
   *
   * This checks a particular chip select on a bus to see if it has a device
   * attached, or is even valid.
   *
   * @bus:	The SPI bus
   * @cs:		The chip select (0..n-1)
   * @info:	Returns information about the chip select, if valid
   * @return 0 if OK (and @info is set up), -ENODEV if the chip select
   *	   is invalid, other -ve value on error
   */
  int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info);
  
  struct sandbox_state;
c60e1f254   Simon Glass   dm: sandbox: Add ...
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
  
  /**
   * sandbox_spi_get_emul() - get an emulator for a SPI slave
   *
   * This provides a way to attach an emulated SPI device to a particular SPI
   * slave, so that xfer() operations on the slave will be handled by the
   * emulator. If a emulator already exists on that chip select it is returned.
   * Otherwise one is created.
   *
   * @state:	Sandbox state
   * @bus:	SPI bus requesting the emulator
   * @slave:	SPI slave device requesting the emulator
   * @emuip:	Returns pointer to emulator
   * @return 0 if OK, -ve on error
   */
d7af6a485   Simon Glass   dm: spi: Add a uc...
597
598
599
  int sandbox_spi_get_emul(struct sandbox_state *state,
  			 struct udevice *bus, struct udevice *slave,
  			 struct udevice **emulp);
7a3eff4ce   Peng Fan   dm: spi: introduc...
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
  /**
   * Claim the bus and prepare it for communication with a given slave.
   *
   * This must be called before doing any transfers with a SPI slave. It
   * will enable and initialize any SPI hardware as necessary, and make
   * sure that the SCK line is in the correct idle state. It is not
   * allowed to claim the same bus for several slaves without releasing
   * the bus in between.
   *
   * @dev:	The SPI slave device
   *
   * Returns: 0 if the bus was claimed successfully, or a negative value
   * if it wasn't.
   */
  int dm_spi_claim_bus(struct udevice *dev);
  
  /**
   * Release the SPI bus
   *
   * This must be called once for every call to dm_spi_claim_bus() after
   * all transfers have finished. It may disable any SPI hardware as
   * appropriate.
   *
   * @slave:	The SPI slave device
   */
  void dm_spi_release_bus(struct udevice *dev);
  
  /**
   * SPI transfer
   *
   * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
   * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
   *
   * The source of the outgoing bits is the "dout" parameter and the
   * destination of the input bits is the "din" parameter.  Note that "dout"
   * and "din" can point to the same memory location, in which case the
   * input data overwrites the output data (since both are buffered by
   * temporary variables, this is OK).
   *
   * dm_spi_xfer() interface:
   * @dev:	The SPI slave device which will be sending/receiving the data.
   * @bitlen:	How many bits to write and read.
   * @dout:	Pointer to a string of bits to send out.  The bits are
   *		held in a byte array and are sent MSB first.
   * @din:	Pointer to a string of bits that will be filled in.
   * @flags:	A bitwise combination of SPI_XFER_* flags.
   *
   * Returns: 0 on success, not 0 on failure
   */
  int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
  		const void *dout, void *din, unsigned long flags);
c53b318e1   Simon Glass   spi: Add support ...
651
652
653
654
655
656
657
658
659
660
661
662
663
  /**
   * spi_get_mmap() - Get memory-mapped SPI
   *
   * @dev:	SPI slave device to check
   * @map_basep:	Returns base memory address for mapped SPI
   * @map_sizep:	Returns size of mapped SPI
   * @offsetp:	Returns start offset of SPI flash where the map works
   *	correctly (offsets before this are not visible)
   * @return 0 if OK, -ENOSYS if no operation, -EFAULT if memory mapping is not
   *	available
   */
  int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
  		    uint *offsetp);
bc5701e1b   Simon Glass   dm: spi: Correct ...
664
  /* Access the operations for a SPI device */
d7af6a485   Simon Glass   dm: spi: Add a uc...
665
  #define spi_get_ops(dev)	((struct dm_spi_ops *)(dev)->driver->ops)
c60e1f254   Simon Glass   dm: sandbox: Add ...
666
  #define spi_emul_get_ops(dev)	((struct dm_spi_emul_ops *)(dev)->driver->ops)
d7af6a485   Simon Glass   dm: spi: Add a uc...
667
  #endif /* CONFIG_DM_SPI */
77f855819   wdenk   Initial revision
668
  #endif	/* _SPI_H_ */