blob: 14e3efb21c8db954b1de874817dbd0c4e53abd57 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenk77f85582002-09-26 02:01:47 +00002/*
Jagannadha Sutradharudu Teki469146c2013-10-10 22:14:09 +05303 * Common SPI Interface: Controller-specific definitions
4 *
wdenk77f85582002-09-26 02:01:47 +00005 * (C) Copyright 2001
6 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
wdenk77f85582002-09-26 02:01:47 +00007 */
8
9#ifndef _SPI_H_
10#define _SPI_H_
11
Boris Brezillond13f5b22018-08-16 17:30:11 +020012#include <common.h>
Simon Glasscd93d622020-05-10 11:40:13 -060013#include <linux/bitops.h>
Boris Brezillond13f5b22018-08-16 17:30:11 +020014
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020015/* SPI mode flags */
Simon Glass70e5e672020-07-07 13:11:49 -060016#define SPI_CPHA BIT(0) /* clock phase (1 = SPI_CLOCK_PHASE_SECOND) */
17#define SPI_CPOL BIT(1) /* clock polarity (1 = SPI_POLARITY_HIGH) */
Jagan Teki465c00d2015-12-29 12:12:30 +053018#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 */
Jagan Teki29ee0262015-12-28 22:24:08 +053028#define SPI_TX_BYTE BIT(8) /* transmit with 1 wire byte */
Jagan Teki2b11a412015-12-28 22:55:50 +053029#define SPI_TX_DUAL BIT(9) /* transmit with 2 wires */
30#define SPI_TX_QUAD BIT(10) /* transmit with 4 wires */
Jagan Teki08fe9c22016-08-08 17:12:12 +053031#define SPI_RX_SLOW BIT(11) /* receive with 1 wire slow */
Jagan Teki3ac48d02016-08-10 20:48:14 +053032#define SPI_RX_DUAL BIT(12) /* receive with 2 wires */
33#define SPI_RX_QUAD BIT(13) /* receive with 4 wires */
Vignesh Raghavendra658df8b2019-12-05 15:46:05 +053034#define SPI_TX_OCTAL BIT(14) /* transmit with 8 wires */
35#define SPI_RX_OCTAL BIT(15) /* receive with 8 wires */
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053036
Rajeshwari Shindebb786b82013-05-28 20:10:37 +000037/* Header byte that marks the start of the message */
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +053038#define SPI_PREAMBLE_END_BYTE 0xec
Rajeshwari Shindebb786b82013-05-28 20:10:37 +000039
Jagan Teki5d69df32015-06-27 00:51:30 +053040#define SPI_DEFAULT_WORDLEN 8
Nikita Kiryanov5753d092013-10-16 17:23:25 +030041
Ovidiu Panait741280e2020-12-14 19:06:50 +020042/**
43 * struct dm_spi_bus - SPI bus info
44 *
45 * This contains information about a SPI bus. To obtain this structure, use
46 * dev_get_uclass_priv(bus) where bus is the SPI bus udevice.
47 *
48 * @max_hz: Maximum speed that the bus can tolerate.
49 * @speed: Current bus speed. This is 0 until the bus is first claimed.
50 * @mode: Current bus mode. This is 0 until the bus is first claimed.
51 *
52 * TODO(sjg@chromium.org): Remove this and use max_hz from struct spi_slave.
53 */
Simon Glassd7af6a42014-10-13 23:41:52 -060054struct dm_spi_bus {
55 uint max_hz;
Ovidiu Panait741280e2020-12-14 19:06:50 +020056 uint speed;
57 uint mode;
Simon Glassd7af6a42014-10-13 23:41:52 -060058};
59
Simon Glassd0cff032015-01-25 08:27:12 -070060/**
Simon Glass8a8d24b2020-12-03 16:55:23 -070061 * struct dm_spi_plat - platform data for all SPI slaves
Simon Glassd0cff032015-01-25 08:27:12 -070062 *
63 * This describes a SPI slave, a child device of the SPI bus. To obtain this
Simon Glasscaa4daa2020-12-03 16:55:18 -070064 * struct from a spi_slave, use dev_get_parent_plat(dev) or
65 * dev_get_parent_plat(slave->dev).
Simon Glassd0cff032015-01-25 08:27:12 -070066 *
Sean Anderson40fc33f2020-08-07 13:13:34 -040067 * This data is immutable. Each time the device is probed, @max_hz and @mode
Simon Glassd0cff032015-01-25 08:27:12 -070068 * will be copied to struct spi_slave.
69 *
70 * @cs: Chip select number (0..n-1)
71 * @max_hz: Maximum bus speed that this slave can tolerate
72 * @mode: SPI mode to use for this device (see SPI mode flags)
73 */
Simon Glass8a8d24b2020-12-03 16:55:23 -070074struct dm_spi_slave_plat {
Simon Glassd0cff032015-01-25 08:27:12 -070075 unsigned int cs;
76 uint max_hz;
77 uint mode;
78};
79
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +053080/**
Simon Glassb14ccfc2020-04-08 16:57:21 -060081 * enum spi_clock_phase - indicates the clock phase to use for SPI (CPHA)
82 *
83 * @SPI_CLOCK_PHASE_FIRST: Data sampled on the first phase
84 * @SPI_CLOCK_PHASE_SECOND: Data sampled on the second phase
85 */
86enum spi_clock_phase {
87 SPI_CLOCK_PHASE_FIRST,
88 SPI_CLOCK_PHASE_SECOND,
89};
90
91/**
92 * enum spi_wire_mode - indicates the number of wires used for SPI
93 *
94 * @SPI_4_WIRE_MODE: Normal bidirectional mode with MOSI and MISO
95 * @SPI_3_WIRE_MODE: Unidirectional version with a single data line SISO
96 */
97enum spi_wire_mode {
98 SPI_4_WIRE_MODE,
99 SPI_3_WIRE_MODE,
100};
101
102/**
103 * enum spi_polarity - indicates the polarity of the SPI bus (CPOL)
104 *
105 * @SPI_POLARITY_LOW: Clock is low in idle state
106 * @SPI_POLARITY_HIGH: Clock is high in idle state
107 */
108enum spi_polarity {
109 SPI_POLARITY_LOW,
110 SPI_POLARITY_HIGH,
111};
112
113/**
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +0530114 * struct spi_slave - Representation of a SPI slave
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200115 *
Simon Glassd7af6a42014-10-13 23:41:52 -0600116 * For driver model this is the per-child data used by the SPI bus. It can
Simon Glassbcbe3d12015-09-28 23:32:01 -0600117 * be accessed using dev_get_parent_priv() on the slave device. The SPI uclass
Simon Glass41575d82020-12-03 16:55:17 -0700118 * sets up per_child_auto to sizeof(struct spi_slave), and the
Simon Glassd0cff032015-01-25 08:27:12 -0700119 * driver should not override it. Two platform data fields (max_hz and mode)
120 * are copied into this structure to provide an initial value. This allows
121 * them to be changed, since we should never change platform data in drivers.
Simon Glassd7af6a42014-10-13 23:41:52 -0600122 *
123 * If not using driver model, drivers are expected to extend this with
124 * controller-specific data.
125 *
126 * @dev: SPI slave device
127 * @max_hz: Maximum speed for this slave
Simon Glassd7af6a42014-10-13 23:41:52 -0600128 * @bus: ID of the bus that the slave is attached to. For
129 * driver model this is the sequence number of the SPI
Simon Glass8b85dfc2020-12-16 21:20:07 -0700130 * bus (dev_seq(bus)) so does not need to be stored
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +0530131 * @cs: ID of the chip select connected to the slave.
Jagan Tekif5c3c032015-12-14 12:15:17 +0530132 * @mode: SPI mode to use for this slave (see SPI mode flags)
Nikita Kiryanov5753d092013-10-16 17:23:25 +0300133 * @wordlen: Size of SPI word in number of bits
Álvaro Fernández Rojas8af74ed2018-01-23 17:14:56 +0100134 * @max_read_size: If non-zero, the maximum number of bytes which can
135 * be read at once.
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +0530136 * @max_write_size: If non-zero, the maximum number of bytes which can
Álvaro Fernández Rojas6c94bd12018-01-23 17:14:57 +0100137 * be written at once.
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +0530138 * @memory_map: Address of read-only SPI flash access.
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530139 * @flags: Indication of SPI flags.
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200140 */
141struct spi_slave {
Lukasz Majewski56c40462020-06-04 23:11:53 +0800142#if CONFIG_IS_ENABLED(DM_SPI)
Simon Glassd7af6a42014-10-13 23:41:52 -0600143 struct udevice *dev; /* struct spi_slave is dev->parentdata */
144 uint max_hz;
Simon Glassd7af6a42014-10-13 23:41:52 -0600145#else
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530146 unsigned int bus;
147 unsigned int cs;
Simon Glassd0cff032015-01-25 08:27:12 -0700148#endif
Jagan Tekif5c3c032015-12-14 12:15:17 +0530149 uint mode;
Nikita Kiryanov5753d092013-10-16 17:23:25 +0300150 unsigned int wordlen;
Álvaro Fernández Rojas8af74ed2018-01-23 17:14:56 +0100151 unsigned int max_read_size;
Simon Glass0c456ce2013-03-11 06:08:05 +0000152 unsigned int max_write_size;
Poddar, Sourav004f15b2013-10-07 15:53:01 +0530153 void *memory_map;
Jagan Tekic40f6002015-12-28 22:23:14 +0530154
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530155 u8 flags;
Jagan Teki29ee0262015-12-28 22:24:08 +0530156#define SPI_XFER_BEGIN BIT(0) /* Assert CS before transfer */
157#define SPI_XFER_END BIT(1) /* Deassert CS after transfer */
Jagan Tekic40f6002015-12-28 22:23:14 +0530158#define SPI_XFER_ONCE (SPI_XFER_BEGIN | SPI_XFER_END)
Bichao Zheng8951e442023-07-13 13:21:29 +0800159#ifdef CONFIG_AMLOGIC_MODIFY
160/* spi nfc needed */
161#define SPI_XFER_OOB BIT(4)
162#define SPI_XFER_RAW BIT(5)
163#define SPI_XFER_AUTO_OOB BIT(6)
164#define SPI_XFER_OOB_ONLY BIT(7)
165#define SPI_XFER_NFC_MASK_FLAG \
166 (SPI_XFER_OOB | SPI_XFER_RAW | \
167 SPI_XFER_AUTO_OOB | SPI_XFER_OOB_ONLY)
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200168};
Bichao Zheng8951e442023-07-13 13:21:29 +0800169#endif
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530170/**
Simon Glassba6c3ce2013-03-11 06:08:00 +0000171 * spi_do_alloc_slave - Allocate a new SPI slave (internal)
172 *
173 * Allocate and zero all fields in the spi slave, and set the bus/chip
174 * select. Use the helper macro spi_alloc_slave() to call this.
175 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530176 * @offset: Offset of struct spi_slave within slave structure.
177 * @size: Size of slave structure.
178 * @bus: Bus ID of the slave chip.
179 * @cs: Chip select ID of the slave chip on the specified bus.
Simon Glassba6c3ce2013-03-11 06:08:00 +0000180 */
181void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
182 unsigned int cs);
183
184/**
185 * spi_alloc_slave - Allocate a new SPI slave
186 *
187 * Allocate and zero all fields in the spi slave, and set the bus/chip
188 * select.
189 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530190 * @_struct: Name of structure to allocate (e.g. struct tegra_spi).
191 * This structure must contain a member 'struct spi_slave *slave'.
192 * @bus: Bus ID of the slave chip.
193 * @cs: Chip select ID of the slave chip on the specified bus.
Simon Glassba6c3ce2013-03-11 06:08:00 +0000194 */
195#define spi_alloc_slave(_struct, bus, cs) \
196 spi_do_alloc_slave(offsetof(_struct, slave), \
197 sizeof(_struct), bus, cs)
198
199/**
200 * spi_alloc_slave_base - Allocate a new SPI slave with no private data
201 *
202 * Allocate and zero all fields in the spi slave, and set the bus/chip
203 * select.
204 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530205 * @bus: Bus ID of the slave chip.
206 * @cs: Chip select ID of the slave chip on the specified bus.
Simon Glassba6c3ce2013-03-11 06:08:00 +0000207 */
208#define spi_alloc_slave_base(bus, cs) \
209 spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
210
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530211/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200212 * Set up communications parameters for a SPI slave.
213 *
214 * This must be called once for each slave. Note that this function
215 * usually doesn't touch any actual hardware, it only initializes the
216 * contents of spi_slave so that the hardware can be easily
217 * initialized later.
218 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530219 * @bus: Bus ID of the slave chip.
220 * @cs: Chip select ID of the slave chip on the specified bus.
221 * @max_hz: Maximum SCK rate in Hz.
222 * @mode: Clock polarity, clock phase and other parameters.
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200223 *
224 * Returns: A spi_slave reference that can be used in subsequent SPI
225 * calls, or NULL if one or more of the parameters are not supported.
226 */
227struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
228 unsigned int max_hz, unsigned int mode);
229
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530230/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200231 * Free any memory associated with a SPI slave.
232 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530233 * @slave: The SPI slave
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200234 */
235void spi_free_slave(struct spi_slave *slave);
236
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530237/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200238 * Claim the bus and prepare it for communication with a given slave.
239 *
240 * This must be called before doing any transfers with a SPI slave. It
241 * will enable and initialize any SPI hardware as necessary, and make
242 * sure that the SCK line is in the correct idle state. It is not
243 * allowed to claim the same bus for several slaves without releasing
244 * the bus in between.
245 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530246 * @slave: The SPI slave
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200247 *
248 * Returns: 0 if the bus was claimed successfully, or a negative value
249 * if it wasn't.
250 */
251int spi_claim_bus(struct spi_slave *slave);
252
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530253/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200254 * Release the SPI bus
255 *
256 * This must be called once for every call to spi_claim_bus() after
257 * all transfers have finished. It may disable any SPI hardware as
258 * appropriate.
259 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530260 * @slave: The SPI slave
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200261 */
262void spi_release_bus(struct spi_slave *slave);
wdenk77f85582002-09-26 02:01:47 +0000263
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530264/**
Nikita Kiryanov5753d092013-10-16 17:23:25 +0300265 * Set the word length for SPI transactions
266 *
267 * Set the word length (number of bits per word) for SPI transactions.
268 *
269 * @slave: The SPI slave
270 * @wordlen: The number of bits in a word
271 *
272 * Returns: 0 on success, -1 on failure.
273 */
274int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
275
276/**
Simon Glassccdabd82019-12-06 21:42:35 -0700277 * SPI transfer (optional if mem_ops is used)
wdenk77f85582002-09-26 02:01:47 +0000278 *
279 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
280 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
281 *
282 * The source of the outgoing bits is the "dout" parameter and the
283 * destination of the input bits is the "din" parameter. Note that "dout"
284 * and "din" can point to the same memory location, in which case the
285 * input data overwrites the output data (since both are buffered by
286 * temporary variables, this is OK).
287 *
wdenk77f85582002-09-26 02:01:47 +0000288 * spi_xfer() interface:
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530289 * @slave: The SPI slave which will be sending/receiving the data.
290 * @bitlen: How many bits to write and read.
291 * @dout: Pointer to a string of bits to send out. The bits are
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200292 * held in a byte array and are sent MSB first.
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530293 * @din: Pointer to a string of bits that will be filled in.
294 * @flags: A bitwise combination of SPI_XFER_* flags.
wdenk77f85582002-09-26 02:01:47 +0000295 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530296 * Returns: 0 on success, not 0 on failure
wdenk77f85582002-09-26 02:01:47 +0000297 */
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200298int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
299 void *din, unsigned long flags);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200300
Jagan Teki8473b322019-07-22 17:22:56 +0530301/**
302 * spi_write_then_read - SPI synchronous write followed by read
303 *
304 * This performs a half duplex transaction in which the first transaction
305 * is to send the opcode and if the length of buf is non-zero then it start
306 * the second transaction as tx or rx based on the need from respective slave.
307 *
308 * @slave: The SPI slave device with which opcode/data will be exchanged
309 * @opcode: opcode used for specific transfer
310 * @n_opcode: size of opcode, in bytes
311 * @txbuf: buffer into which data to be written
312 * @rxbuf: buffer into which data will be read
313 * @n_buf: size of buf (whether it's [tx|rx]buf), in bytes
314 *
315 * Returns: 0 on success, not 0 on failure
316 */
317int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
318 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
319 size_t n_buf);
320
Tom Rini146bad92015-08-17 13:29:54 +0530321/* Copy memory mapped data */
322void spi_flash_copy_mmap(void *data, void *offset, size_t len);
323
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530324/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200325 * Determine if a SPI chipselect is valid.
326 * This function is provided by the board if the low-level SPI driver
327 * needs it to determine if a given chipselect is actually valid.
328 *
329 * Returns: 1 if bus:cs identifies a valid chip on this board, 0
330 * otherwise.
331 */
Simon Glassd7af6a42014-10-13 23:41:52 -0600332int spi_cs_is_valid(unsigned int bus, unsigned int cs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200333
Simon Glass324ec5d2020-07-08 09:02:14 -0600334/*
335 * These names are used in several drivers and these declarations will be
336 * removed soon as part of the SPI DM migration. Drop them if driver model is
337 * enabled for SPI.
338 */
Lukasz Majewski56c40462020-06-04 23:11:53 +0800339#if !CONFIG_IS_ENABLED(DM_SPI)
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530340/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200341 * Activate a SPI chipselect.
342 * This function is provided by the board code when using a driver
343 * that can't control its chipselects automatically (e.g.
344 * common/soft_spi.c). When called, it should activate the chip select
345 * to the device identified by "slave".
346 */
347void spi_cs_activate(struct spi_slave *slave);
348
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530349/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200350 * Deactivate a SPI chipselect.
351 * This function is provided by the board code when using a driver
352 * that can't control its chipselects automatically (e.g.
353 * common/soft_spi.c). When called, it should deactivate the chip
354 * select to the device identified by "slave".
355 */
356void spi_cs_deactivate(struct spi_slave *slave);
Simon Glass324ec5d2020-07-08 09:02:14 -0600357#endif
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200358
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530359/**
Thomas Choufa1423e2010-12-24 15:16:07 +0800360 * Set transfer speed.
361 * This sets a new speed to be applied for next spi_xfer().
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530362 * @slave: The SPI slave
363 * @hz: The transfer speed
Paul Barkerc98f6fe2022-10-05 13:18:34 +0100364 *
365 * Returns: 0 on success, or a negative value on error.
Thomas Choufa1423e2010-12-24 15:16:07 +0800366 */
Paul Barkerc98f6fe2022-10-05 13:18:34 +0100367int spi_set_speed(struct spi_slave *slave, uint hz);
Thomas Choufa1423e2010-12-24 15:16:07 +0800368
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530369/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200370 * Write 8 bits, then read 8 bits.
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530371 * @slave: The SPI slave we're communicating with
372 * @byte: Byte to be written
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200373 *
374 * Returns: The value that was read, or a negative value on error.
375 *
376 * TODO: This function probably shouldn't be inlined.
377 */
378static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
379{
380 unsigned char dout[2];
381 unsigned char din[2];
382 int ret;
383
384 dout[0] = byte;
385 dout[1] = 0;
386
387 ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
388 return ret < 0 ? ret : din[1];
389}
wdenk77f85582002-09-26 02:01:47 +0000390
Simon Glassd7af6a42014-10-13 23:41:52 -0600391/**
392 * struct spi_cs_info - Information about a bus chip select
393 *
394 * @dev: Connected device, or NULL if none
395 */
396struct spi_cs_info {
397 struct udevice *dev;
398};
399
400/**
401 * struct struct dm_spi_ops - Driver model SPI operations
402 *
403 * The uclass interface is implemented by all SPI devices which use
404 * driver model.
405 */
406struct dm_spi_ops {
407 /**
408 * Claim the bus and prepare it for communication.
409 *
410 * The device provided is the slave device. It's parent controller
411 * will be used to provide the communication.
412 *
413 * This must be called before doing any transfers with a SPI slave. It
414 * will enable and initialize any SPI hardware as necessary, and make
415 * sure that the SCK line is in the correct idle state. It is not
416 * allowed to claim the same bus for several slaves without releasing
417 * the bus in between.
418 *
Simon Glass9694b722015-04-19 09:05:40 -0600419 * @dev: The SPI slave
Simon Glassd7af6a42014-10-13 23:41:52 -0600420 *
421 * Returns: 0 if the bus was claimed successfully, or a negative value
422 * if it wasn't.
423 */
Simon Glass9694b722015-04-19 09:05:40 -0600424 int (*claim_bus)(struct udevice *dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600425
426 /**
427 * Release the SPI bus
428 *
429 * This must be called once for every call to spi_claim_bus() after
430 * all transfers have finished. It may disable any SPI hardware as
431 * appropriate.
432 *
Simon Glass9694b722015-04-19 09:05:40 -0600433 * @dev: The SPI slave
Simon Glassd7af6a42014-10-13 23:41:52 -0600434 */
Simon Glass9694b722015-04-19 09:05:40 -0600435 int (*release_bus)(struct udevice *dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600436
437 /**
438 * Set the word length for SPI transactions
439 *
440 * Set the word length (number of bits per word) for SPI transactions.
441 *
442 * @bus: The SPI slave
443 * @wordlen: The number of bits in a word
444 *
445 * Returns: 0 on success, -ve on failure.
446 */
Simon Glass9694b722015-04-19 09:05:40 -0600447 int (*set_wordlen)(struct udevice *dev, unsigned int wordlen);
Simon Glassd7af6a42014-10-13 23:41:52 -0600448
449 /**
450 * SPI transfer
451 *
452 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
453 * clocks "bitlen" bits in the SPI MISO port. That's just the way SPI
454 * works.
455 *
456 * The source of the outgoing bits is the "dout" parameter and the
457 * destination of the input bits is the "din" parameter. Note that
458 * "dout" and "din" can point to the same memory location, in which
459 * case the input data overwrites the output data (since both are
460 * buffered by temporary variables, this is OK).
461 *
462 * spi_xfer() interface:
463 * @dev: The slave device to communicate with
464 * @bitlen: How many bits to write and read.
465 * @dout: Pointer to a string of bits to send out. The bits are
466 * held in a byte array and are sent MSB first.
467 * @din: Pointer to a string of bits that will be filled in.
468 * @flags: A bitwise combination of SPI_XFER_* flags.
469 *
470 * Returns: 0 on success, not -1 on failure
471 */
472 int (*xfer)(struct udevice *dev, unsigned int bitlen, const void *dout,
473 void *din, unsigned long flags);
474
475 /**
Boris Brezillond13f5b22018-08-16 17:30:11 +0200476 * Optimized handlers for SPI memory-like operations.
477 *
478 * Optimized/dedicated operations for interactions with SPI memory. This
479 * field is optional and should only be implemented if the controller
480 * has native support for memory like operations.
481 */
482 const struct spi_controller_mem_ops *mem_ops;
483
484 /**
Simon Glassd7af6a42014-10-13 23:41:52 -0600485 * Set transfer speed.
486 * This sets a new speed to be applied for next spi_xfer().
487 * @bus: The SPI bus
488 * @hz: The transfer speed
489 * @return 0 if OK, -ve on error
490 */
491 int (*set_speed)(struct udevice *bus, uint hz);
492
493 /**
494 * Set the SPI mode/flags
495 *
496 * It is unclear if we want to set speed and mode together instead
497 * of separately.
498 *
499 * @bus: The SPI bus
500 * @mode: Requested SPI mode (SPI_... flags)
501 * @return 0 if OK, -ve on error
502 */
503 int (*set_mode)(struct udevice *bus, uint mode);
504
505 /**
506 * Get information on a chip select
507 *
508 * This is only called when the SPI uclass does not know about a
509 * chip select, i.e. it has no attached device. It gives the driver
510 * a chance to allow activity on that chip select even so.
511 *
512 * @bus: The SPI bus
513 * @cs: The chip select (0..n-1)
514 * @info: Returns information about the chip select, if valid.
515 * On entry info->dev is NULL
Bin Meng4b060002019-09-09 06:00:01 -0700516 * @return 0 if OK (and @info is set up), -EINVAL if the chip select
Simon Glassd7af6a42014-10-13 23:41:52 -0600517 * is invalid, other -ve value on error
518 */
519 int (*cs_info)(struct udevice *bus, uint cs, struct spi_cs_info *info);
Simon Glassc53b3182019-10-20 21:31:47 -0600520
521 /**
522 * get_mmap() - Get memory-mapped SPI
523 *
524 * @dev: The SPI flash slave device
525 * @map_basep: Returns base memory address for mapped SPI
526 * @map_sizep: Returns size of mapped SPI
527 * @offsetp: Returns start offset of SPI flash where the map works
528 * correctly (offsets before this are not visible)
529 * @return 0 if OK, -EFAULT if memory mapping is not available
530 */
531 int (*get_mmap)(struct udevice *dev, ulong *map_basep,
532 uint *map_sizep, uint *offsetp);
Simon Glassd7af6a42014-10-13 23:41:52 -0600533};
534
Simon Glassc60e1f22014-10-13 23:41:53 -0600535struct dm_spi_emul_ops {
536 /**
537 * SPI transfer
538 *
539 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
540 * clocks "bitlen" bits in the SPI MISO port. That's just the way SPI
541 * works. Here the device is a slave.
542 *
543 * The source of the outgoing bits is the "dout" parameter and the
544 * destination of the input bits is the "din" parameter. Note that
545 * "dout" and "din" can point to the same memory location, in which
546 * case the input data overwrites the output data (since both are
547 * buffered by temporary variables, this is OK).
548 *
549 * spi_xfer() interface:
550 * @slave: The SPI slave which will be sending/receiving the data.
551 * @bitlen: How many bits to write and read.
552 * @dout: Pointer to a string of bits sent to the device. The
553 * bits are held in a byte array and are sent MSB first.
554 * @din: Pointer to a string of bits that will be sent back to
555 * the master.
556 * @flags: A bitwise combination of SPI_XFER_* flags.
557 *
558 * Returns: 0 on success, not -1 on failure
559 */
560 int (*xfer)(struct udevice *slave, unsigned int bitlen,
561 const void *dout, void *din, unsigned long flags);
562};
563
Simon Glassd7af6a42014-10-13 23:41:52 -0600564/**
565 * spi_find_bus_and_cs() - Find bus and slave devices by number
566 *
567 * Given a bus number and chip select, this finds the corresponding bus
568 * device and slave device. Neither device is activated by this function,
569 * although they may have been activated previously.
570 *
571 * @busnum: SPI bus number
572 * @cs: Chip select to look for
573 * @busp: Returns bus device
574 * @devp: Return slave device
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100575 * Return: 0 if found, -ENODEV on error
Simon Glassd7af6a42014-10-13 23:41:52 -0600576 */
577int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
578 struct udevice **devp);
579
580/**
581 * spi_get_bus_and_cs() - Find and activate bus and slave devices by number
582 *
583 * Given a bus number and chip select, this finds the corresponding bus
584 * device and slave device.
585 *
Patrice Chotard61708bb2022-03-30 09:33:13 +0200586 * @busnum: SPI bus number
587 * @cs: Chip select to look for
588 * @busp: Returns bus device
589 * @devp: Return slave device
590 * @return 0 if found, -ve on error
591 */
592int spi_get_bus_and_cs(int busnum, int cs,
593 struct udevice **busp, struct spi_slave **devp);
594
595/**
596 * _spi_get_bus_and_cs() - Find and activate bus and slave devices by number
597 * As spi_flash_probe(), This is an old-style function. We should remove
598 * it when all SPI flash drivers use dm
599 *
600 * Given a bus number and chip select, this finds the corresponding bus
601 * device and slave device.
602 *
Simon Glassd7af6a42014-10-13 23:41:52 -0600603 * If no such slave exists, and drv_name is not NULL, then a new slave device
Patrick Delaunayb0cc1b82019-02-27 15:36:44 +0100604 * is automatically bound on this chip select with requested speed and mode.
Simon Glassd7af6a42014-10-13 23:41:52 -0600605 *
Patrick Delaunayb0cc1b82019-02-27 15:36:44 +0100606 * Ths new slave device is probed ready for use with the speed and mode
Simon Glasscaa4daa2020-12-03 16:55:18 -0700607 * from plat when available or the requested values.
Simon Glassd7af6a42014-10-13 23:41:52 -0600608 *
609 * @busnum: SPI bus number
610 * @cs: Chip select to look for
Simon Glasscaa4daa2020-12-03 16:55:18 -0700611 * @speed: SPI speed to use for this slave when not available in plat
612 * @mode: SPI mode to use for this slave when not available in plat
Simon Glassd7af6a42014-10-13 23:41:52 -0600613 * @drv_name: Name of driver to attach to this chip select
614 * @dev_name: Name of the new device thus created
615 * @busp: Returns bus device
616 * @devp: Return slave device
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100617 * Return: 0 if found, -ve on error
Simon Glassd7af6a42014-10-13 23:41:52 -0600618 */
Patrice Chotard61708bb2022-03-30 09:33:13 +0200619int _spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
Simon Glassd7af6a42014-10-13 23:41:52 -0600620 const char *drv_name, const char *dev_name,
621 struct udevice **busp, struct spi_slave **devp);
622
623/**
624 * spi_chip_select() - Get the chip select for a slave
625 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100626 * Return: the chip select this slave is attached to
Simon Glassd7af6a42014-10-13 23:41:52 -0600627 */
628int spi_chip_select(struct udevice *slave);
629
630/**
Simon Glassff56bba2014-11-11 10:46:22 -0700631 * spi_find_chip_select() - Find the slave attached to chip select
632 *
633 * @bus: SPI bus to search
634 * @cs: Chip select to look for
635 * @devp: Returns the slave device if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100636 * Return: 0 if found, -EINVAL if cs is invalid, -ENODEV if no device attached,
Bin Meng7bacce52019-09-09 06:00:02 -0700637 * other -ve value on error
Simon Glassff56bba2014-11-11 10:46:22 -0700638 */
639int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp);
640
641/**
Simon Glassd1998a92020-12-03 16:55:21 -0700642 * spi_slave_of_to_plat() - decode standard SPI platform data
Simon Glassd7af6a42014-10-13 23:41:52 -0600643 *
Simon Glassd0cff032015-01-25 08:27:12 -0700644 * This decodes the speed and mode for a slave from a device tree node
Simon Glassd7af6a42014-10-13 23:41:52 -0600645 *
646 * @blob: Device tree blob
647 * @node: Node offset to read from
Simon Glassd0cff032015-01-25 08:27:12 -0700648 * @plat: Place to put the decoded information
Simon Glassd7af6a42014-10-13 23:41:52 -0600649 */
Simon Glass8a8d24b2020-12-03 16:55:23 -0700650int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat);
Simon Glassd7af6a42014-10-13 23:41:52 -0600651
652/**
653 * spi_cs_info() - Check information on a chip select
654 *
655 * This checks a particular chip select on a bus to see if it has a device
656 * attached, or is even valid.
657 *
658 * @bus: The SPI bus
659 * @cs: The chip select (0..n-1)
660 * @info: Returns information about the chip select, if valid
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100661 * Return: 0 if OK (and @info is set up), -ENODEV if the chip select
Simon Glassd7af6a42014-10-13 23:41:52 -0600662 * is invalid, other -ve value on error
663 */
664int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info);
665
666struct sandbox_state;
Simon Glassc60e1f22014-10-13 23:41:53 -0600667
668/**
669 * sandbox_spi_get_emul() - get an emulator for a SPI slave
670 *
671 * This provides a way to attach an emulated SPI device to a particular SPI
672 * slave, so that xfer() operations on the slave will be handled by the
673 * emulator. If a emulator already exists on that chip select it is returned.
674 * Otherwise one is created.
675 *
676 * @state: Sandbox state
677 * @bus: SPI bus requesting the emulator
678 * @slave: SPI slave device requesting the emulator
679 * @emuip: Returns pointer to emulator
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100680 * Return: 0 if OK, -ve on error
Simon Glassc60e1f22014-10-13 23:41:53 -0600681 */
Simon Glassd7af6a42014-10-13 23:41:52 -0600682int sandbox_spi_get_emul(struct sandbox_state *state,
683 struct udevice *bus, struct udevice *slave,
684 struct udevice **emulp);
685
Peng Fan7a3eff42016-05-03 10:02:22 +0800686/**
687 * Claim the bus and prepare it for communication with a given slave.
688 *
689 * This must be called before doing any transfers with a SPI slave. It
690 * will enable and initialize any SPI hardware as necessary, and make
691 * sure that the SCK line is in the correct idle state. It is not
692 * allowed to claim the same bus for several slaves without releasing
693 * the bus in between.
694 *
695 * @dev: The SPI slave device
696 *
697 * Returns: 0 if the bus was claimed successfully, or a negative value
698 * if it wasn't.
699 */
700int dm_spi_claim_bus(struct udevice *dev);
701
702/**
703 * Release the SPI bus
704 *
705 * This must be called once for every call to dm_spi_claim_bus() after
706 * all transfers have finished. It may disable any SPI hardware as
707 * appropriate.
708 *
709 * @slave: The SPI slave device
710 */
711void dm_spi_release_bus(struct udevice *dev);
712
713/**
714 * SPI transfer
715 *
716 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
717 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
718 *
719 * The source of the outgoing bits is the "dout" parameter and the
720 * destination of the input bits is the "din" parameter. Note that "dout"
721 * and "din" can point to the same memory location, in which case the
722 * input data overwrites the output data (since both are buffered by
723 * temporary variables, this is OK).
724 *
725 * dm_spi_xfer() interface:
726 * @dev: The SPI slave device which will be sending/receiving the data.
727 * @bitlen: How many bits to write and read.
728 * @dout: Pointer to a string of bits to send out. The bits are
729 * held in a byte array and are sent MSB first.
730 * @din: Pointer to a string of bits that will be filled in.
731 * @flags: A bitwise combination of SPI_XFER_* flags.
732 *
733 * Returns: 0 on success, not 0 on failure
734 */
735int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
736 const void *dout, void *din, unsigned long flags);
737
Simon Glassc53b3182019-10-20 21:31:47 -0600738/**
739 * spi_get_mmap() - Get memory-mapped SPI
740 *
741 * @dev: SPI slave device to check
742 * @map_basep: Returns base memory address for mapped SPI
743 * @map_sizep: Returns size of mapped SPI
744 * @offsetp: Returns start offset of SPI flash where the map works
745 * correctly (offsets before this are not visible)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100746 * Return: 0 if OK, -ENOSYS if no operation, -EFAULT if memory mapping is not
Simon Glassc53b3182019-10-20 21:31:47 -0600747 * available
748 */
749int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
750 uint *offsetp);
751
Simon Glassbc5701e2015-04-20 12:37:12 -0600752/* Access the operations for a SPI device */
Simon Glassd7af6a42014-10-13 23:41:52 -0600753#define spi_get_ops(dev) ((struct dm_spi_ops *)(dev)->driver->ops)
Simon Glassc60e1f22014-10-13 23:41:53 -0600754#define spi_emul_get_ops(dev) ((struct dm_spi_emul_ops *)(dev)->driver->ops)
Simon Glassd7af6a42014-10-13 23:41:52 -0600755
wdenk77f85582002-09-26 02:01:47 +0000756#endif /* _SPI_H_ */