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