blob: b262e061a8b89866cab536d3e1ace0fadb60a86c [file] [log] [blame]
wdenk77f85582002-09-26 02:01:47 +00001/*
Jagannadha Sutradharudu Teki469146c2013-10-10 22:14:09 +05302 * Common SPI Interface: Controller-specific definitions
3 *
wdenk77f85582002-09-26 02:01:47 +00004 * (C) Copyright 2001
5 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
wdenk77f85582002-09-26 02:01:47 +00008 */
9
10#ifndef _SPI_H_
11#define _SPI_H_
12
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020013/* SPI mode flags */
Jagan Teki465c00d2015-12-29 12:12:30 +053014#define SPI_CPHA BIT(0) /* clock phase */
15#define SPI_CPOL BIT(1) /* clock polarity */
16#define SPI_MODE_0 (0|0) /* (original MicroWire) */
17#define SPI_MODE_1 (0|SPI_CPHA)
18#define SPI_MODE_2 (SPI_CPOL|0)
19#define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
20#define SPI_CS_HIGH BIT(2) /* CS active high */
21#define SPI_LSB_FIRST BIT(3) /* per-word bits-on-wire */
22#define SPI_3WIRE BIT(4) /* SI/SO signals shared */
23#define SPI_LOOP BIT(5) /* loopback mode */
24#define SPI_SLAVE BIT(6) /* slave mode */
25#define SPI_PREAMBLE BIT(7) /* Skip preamble bytes */
Jagan Teki29ee0262015-12-28 22:24:08 +053026#define SPI_TX_BYTE BIT(8) /* transmit with 1 wire byte */
Jagan Teki2b11a412015-12-28 22:55:50 +053027#define SPI_TX_DUAL BIT(9) /* transmit with 2 wires */
28#define SPI_TX_QUAD BIT(10) /* transmit with 4 wires */
Jagan Teki08fe9c22016-08-08 17:12:12 +053029#define SPI_RX_SLOW BIT(11) /* receive with 1 wire slow */
30#define SPI_RX_FAST BIT(12) /* receive with 1 wire fast */
31#define SPI_RX_DUAL BIT(13) /* receive with 2 wires */
32#define SPI_RX_QUAD BIT(14) /* receive with 4 wires */
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053033
Simon Glass248a0482014-09-15 06:33:23 -060034/* SPI bus connection options - see enum spi_dual_flash */
35#define SPI_CONN_DUAL_SHARED (1 << 0)
36#define SPI_CONN_DUAL_SEPARATED (1 << 1)
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +053037
Rajeshwari Shindebb786b82013-05-28 20:10:37 +000038/* Header byte that marks the start of the message */
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +053039#define SPI_PREAMBLE_END_BYTE 0xec
Rajeshwari Shindebb786b82013-05-28 20:10:37 +000040
Jagan Teki5d69df32015-06-27 00:51:30 +053041#define SPI_DEFAULT_WORDLEN 8
Nikita Kiryanov5753d092013-10-16 17:23:25 +030042
Simon Glassd7af6a42014-10-13 23:41:52 -060043#ifdef CONFIG_DM_SPI
Simon Glassd0cff032015-01-25 08:27:12 -070044/* TODO(sjg@chromium.org): Remove this and use max_hz from struct spi_slave */
Simon Glassd7af6a42014-10-13 23:41:52 -060045struct dm_spi_bus {
46 uint max_hz;
47};
48
Simon Glassd0cff032015-01-25 08:27:12 -070049/**
50 * struct dm_spi_platdata - platform data for all SPI slaves
51 *
52 * This describes a SPI slave, a child device of the SPI bus. To obtain this
53 * struct from a spi_slave, use dev_get_parent_platdata(dev) or
54 * dev_get_parent_platdata(slave->dev).
55 *
56 * This data is immuatable. Each time the device is probed, @max_hz and @mode
57 * will be copied to struct spi_slave.
58 *
59 * @cs: Chip select number (0..n-1)
60 * @max_hz: Maximum bus speed that this slave can tolerate
61 * @mode: SPI mode to use for this device (see SPI mode flags)
62 */
63struct dm_spi_slave_platdata {
64 unsigned int cs;
65 uint max_hz;
66 uint mode;
67};
68
Simon Glassd7af6a42014-10-13 23:41:52 -060069#endif /* CONFIG_DM_SPI */
70
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +053071/**
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +053072 * struct spi_slave - Representation of a SPI slave
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020073 *
Simon Glassd7af6a42014-10-13 23:41:52 -060074 * For driver model this is the per-child data used by the SPI bus. It can
Simon Glassbcbe3d12015-09-28 23:32:01 -060075 * be accessed using dev_get_parent_priv() on the slave device. The SPI uclass
Simon Glassd0cff032015-01-25 08:27:12 -070076 * sets uip per_child_auto_alloc_size to sizeof(struct spi_slave), and the
77 * driver should not override it. Two platform data fields (max_hz and mode)
78 * are copied into this structure to provide an initial value. This allows
79 * them to be changed, since we should never change platform data in drivers.
Simon Glassd7af6a42014-10-13 23:41:52 -060080 *
81 * If not using driver model, drivers are expected to extend this with
82 * controller-specific data.
83 *
84 * @dev: SPI slave device
85 * @max_hz: Maximum speed for this slave
Simon Glass60e28092015-02-17 15:29:35 -070086 * @speed: Current bus speed. This is 0 until the bus is first
87 * claimed.
Simon Glassd7af6a42014-10-13 23:41:52 -060088 * @bus: ID of the bus that the slave is attached to. For
89 * driver model this is the sequence number of the SPI
90 * bus (bus->seq) so does not need to be stored
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +053091 * @cs: ID of the chip select connected to the slave.
Jagan Tekif5c3c032015-12-14 12:15:17 +053092 * @mode: SPI mode to use for this slave (see SPI mode flags)
Nikita Kiryanov5753d092013-10-16 17:23:25 +030093 * @wordlen: Size of SPI word in number of bits
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +053094 * @max_write_size: If non-zero, the maximum number of bytes which can
95 * be written at once, excluding command bytes.
96 * @memory_map: Address of read-only SPI flash access.
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +053097 * @option: Varies SPI bus options - separate, shared bus.
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +053098 * @flags: Indication of SPI flags.
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020099 */
100struct spi_slave {
Simon Glassd7af6a42014-10-13 23:41:52 -0600101#ifdef CONFIG_DM_SPI
102 struct udevice *dev; /* struct spi_slave is dev->parentdata */
103 uint max_hz;
Simon Glass60e28092015-02-17 15:29:35 -0700104 uint speed;
Simon Glassd7af6a42014-10-13 23:41:52 -0600105#else
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530106 unsigned int bus;
107 unsigned int cs;
Simon Glassd0cff032015-01-25 08:27:12 -0700108#endif
Jagan Tekif5c3c032015-12-14 12:15:17 +0530109 uint mode;
Nikita Kiryanov5753d092013-10-16 17:23:25 +0300110 unsigned int wordlen;
Simon Glass0c456ce2013-03-11 06:08:05 +0000111 unsigned int max_write_size;
Poddar, Sourav004f15b2013-10-07 15:53:01 +0530112 void *memory_map;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530113 u8 option;
Jagan Tekic40f6002015-12-28 22:23:14 +0530114
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530115 u8 flags;
Jagan Teki29ee0262015-12-28 22:24:08 +0530116#define SPI_XFER_BEGIN BIT(0) /* Assert CS before transfer */
117#define SPI_XFER_END BIT(1) /* Deassert CS after transfer */
Jagan Tekic40f6002015-12-28 22:23:14 +0530118#define SPI_XFER_ONCE (SPI_XFER_BEGIN | SPI_XFER_END)
Jagan Teki29ee0262015-12-28 22:24:08 +0530119#define SPI_XFER_MMAP BIT(2) /* Memory Mapped start */
120#define SPI_XFER_MMAP_END BIT(3) /* Memory Mapped End */
121#define SPI_XFER_U_PAGE BIT(4)
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200122};
wdenk77f85582002-09-26 02:01:47 +0000123
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530124/**
wdenk77f85582002-09-26 02:01:47 +0000125 * Initialization, must be called once on start up.
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200126 *
127 * TODO: I don't think we really need this.
wdenk77f85582002-09-26 02:01:47 +0000128 */
129void spi_init(void);
130
Simon Glassba6c3ce2013-03-11 06:08:00 +0000131/**
132 * spi_do_alloc_slave - Allocate a new SPI slave (internal)
133 *
134 * Allocate and zero all fields in the spi slave, and set the bus/chip
135 * select. Use the helper macro spi_alloc_slave() to call this.
136 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530137 * @offset: Offset of struct spi_slave within slave structure.
138 * @size: Size of slave structure.
139 * @bus: Bus ID of the slave chip.
140 * @cs: Chip select ID of the slave chip on the specified bus.
Simon Glassba6c3ce2013-03-11 06:08:00 +0000141 */
142void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
143 unsigned int cs);
144
145/**
146 * spi_alloc_slave - Allocate a new SPI slave
147 *
148 * Allocate and zero all fields in the spi slave, and set the bus/chip
149 * select.
150 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530151 * @_struct: Name of structure to allocate (e.g. struct tegra_spi).
152 * This structure must contain a member 'struct spi_slave *slave'.
153 * @bus: Bus ID of the slave chip.
154 * @cs: Chip select ID of the slave chip on the specified bus.
Simon Glassba6c3ce2013-03-11 06:08:00 +0000155 */
156#define spi_alloc_slave(_struct, bus, cs) \
157 spi_do_alloc_slave(offsetof(_struct, slave), \
158 sizeof(_struct), bus, cs)
159
160/**
161 * spi_alloc_slave_base - Allocate a new SPI slave with no private data
162 *
163 * Allocate and zero all fields in the spi slave, and set the bus/chip
164 * select.
165 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530166 * @bus: Bus ID of the slave chip.
167 * @cs: Chip select ID of the slave chip on the specified bus.
Simon Glassba6c3ce2013-03-11 06:08:00 +0000168 */
169#define spi_alloc_slave_base(bus, cs) \
170 spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
171
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530172/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200173 * Set up communications parameters for a SPI slave.
174 *
175 * This must be called once for each slave. Note that this function
176 * usually doesn't touch any actual hardware, it only initializes the
177 * contents of spi_slave so that the hardware can be easily
178 * initialized later.
179 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530180 * @bus: Bus ID of the slave chip.
181 * @cs: Chip select ID of the slave chip on the specified bus.
182 * @max_hz: Maximum SCK rate in Hz.
183 * @mode: Clock polarity, clock phase and other parameters.
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200184 *
185 * Returns: A spi_slave reference that can be used in subsequent SPI
186 * calls, or NULL if one or more of the parameters are not supported.
187 */
188struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
189 unsigned int max_hz, unsigned int mode);
190
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530191/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200192 * Free any memory associated with a SPI slave.
193 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530194 * @slave: The SPI slave
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200195 */
196void spi_free_slave(struct spi_slave *slave);
197
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530198/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200199 * Claim the bus and prepare it for communication with a given slave.
200 *
201 * This must be called before doing any transfers with a SPI slave. It
202 * will enable and initialize any SPI hardware as necessary, and make
203 * sure that the SCK line is in the correct idle state. It is not
204 * allowed to claim the same bus for several slaves without releasing
205 * the bus in between.
206 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530207 * @slave: The SPI slave
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200208 *
209 * Returns: 0 if the bus was claimed successfully, or a negative value
210 * if it wasn't.
211 */
212int spi_claim_bus(struct spi_slave *slave);
213
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530214/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200215 * Release the SPI bus
216 *
217 * This must be called once for every call to spi_claim_bus() after
218 * all transfers have finished. It may disable any SPI hardware as
219 * appropriate.
220 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530221 * @slave: The SPI slave
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200222 */
223void spi_release_bus(struct spi_slave *slave);
wdenk77f85582002-09-26 02:01:47 +0000224
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530225/**
Nikita Kiryanov5753d092013-10-16 17:23:25 +0300226 * Set the word length for SPI transactions
227 *
228 * Set the word length (number of bits per word) for SPI transactions.
229 *
230 * @slave: The SPI slave
231 * @wordlen: The number of bits in a word
232 *
233 * Returns: 0 on success, -1 on failure.
234 */
235int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
236
237/**
wdenk77f85582002-09-26 02:01:47 +0000238 * SPI transfer
239 *
240 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
241 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
242 *
243 * The source of the outgoing bits is the "dout" parameter and the
244 * destination of the input bits is the "din" parameter. Note that "dout"
245 * and "din" can point to the same memory location, in which case the
246 * input data overwrites the output data (since both are buffered by
247 * temporary variables, this is OK).
248 *
wdenk77f85582002-09-26 02:01:47 +0000249 * spi_xfer() interface:
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530250 * @slave: The SPI slave which will be sending/receiving the data.
251 * @bitlen: How many bits to write and read.
252 * @dout: Pointer to a string of bits to send out. The bits are
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200253 * held in a byte array and are sent MSB first.
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530254 * @din: Pointer to a string of bits that will be filled in.
255 * @flags: A bitwise combination of SPI_XFER_* flags.
wdenk77f85582002-09-26 02:01:47 +0000256 *
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530257 * Returns: 0 on success, not 0 on failure
wdenk77f85582002-09-26 02:01:47 +0000258 */
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200259int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
260 void *din, unsigned long flags);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200261
Tom Rini146bad92015-08-17 13:29:54 +0530262/* Copy memory mapped data */
263void spi_flash_copy_mmap(void *data, void *offset, size_t len);
264
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530265/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200266 * Determine if a SPI chipselect is valid.
267 * This function is provided by the board if the low-level SPI driver
268 * needs it to determine if a given chipselect is actually valid.
269 *
270 * Returns: 1 if bus:cs identifies a valid chip on this board, 0
271 * otherwise.
272 */
Simon Glassd7af6a42014-10-13 23:41:52 -0600273int spi_cs_is_valid(unsigned int bus, unsigned int cs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200274
Simon Glassd7af6a42014-10-13 23:41:52 -0600275#ifndef CONFIG_DM_SPI
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530276/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200277 * Activate a SPI chipselect.
278 * This function is provided by the board code when using a driver
279 * that can't control its chipselects automatically (e.g.
280 * common/soft_spi.c). When called, it should activate the chip select
281 * to the device identified by "slave".
282 */
283void spi_cs_activate(struct spi_slave *slave);
284
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530285/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200286 * Deactivate a SPI chipselect.
287 * This function is provided by the board code when using a driver
288 * that can't control its chipselects automatically (e.g.
289 * common/soft_spi.c). When called, it should deactivate the chip
290 * select to the device identified by "slave".
291 */
292void spi_cs_deactivate(struct spi_slave *slave);
293
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530294/**
Thomas Choufa1423e2010-12-24 15:16:07 +0800295 * Set transfer speed.
296 * This sets a new speed to be applied for next spi_xfer().
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530297 * @slave: The SPI slave
298 * @hz: The transfer speed
Thomas Choufa1423e2010-12-24 15:16:07 +0800299 */
300void spi_set_speed(struct spi_slave *slave, uint hz);
Simon Glassd7af6a42014-10-13 23:41:52 -0600301#endif
Thomas Choufa1423e2010-12-24 15:16:07 +0800302
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530303/**
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200304 * Write 8 bits, then read 8 bits.
Jagannadha Sutradharudu Teki1b1bd9a2013-09-25 15:47:36 +0530305 * @slave: The SPI slave we're communicating with
306 * @byte: Byte to be written
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200307 *
308 * Returns: The value that was read, or a negative value on error.
309 *
310 * TODO: This function probably shouldn't be inlined.
311 */
312static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
313{
314 unsigned char dout[2];
315 unsigned char din[2];
316 int ret;
317
318 dout[0] = byte;
319 dout[1] = 0;
320
321 ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
322 return ret < 0 ? ret : din[1];
323}
wdenk77f85582002-09-26 02:01:47 +0000324
Hung-ying Tyanf3424c52013-05-15 18:27:30 +0800325/**
326 * Set up a SPI slave for a particular device tree node
327 *
328 * This calls spi_setup_slave() with the correct bus number. Call
329 * spi_free_slave() to free it later.
330 *
Jagannadha Sutradharudu Teki469146c2013-10-10 22:14:09 +0530331 * @param blob: Device tree blob
Simon Glass0efc0242013-12-03 16:43:24 -0700332 * @param slave_node: Slave node to use
333 * @param spi_node: SPI peripheral node to use
Hung-ying Tyanf3424c52013-05-15 18:27:30 +0800334 * @return pointer to new spi_slave structure
335 */
Simon Glass0efc0242013-12-03 16:43:24 -0700336struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
337 int spi_node);
338
339/**
340 * spi_base_setup_slave_fdt() - helper function to set up a SPI slace
341 *
342 * This decodes SPI properties from the slave node to determine the
343 * chip select and SPI parameters.
344 *
345 * @blob: Device tree blob
346 * @busnum: Bus number to use
347 * @node: Device tree node for the SPI bus
348 */
349struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum,
350 int node);
Hung-ying Tyanf3424c52013-05-15 18:27:30 +0800351
Simon Glassd7af6a42014-10-13 23:41:52 -0600352#ifdef CONFIG_DM_SPI
353
354/**
355 * struct spi_cs_info - Information about a bus chip select
356 *
357 * @dev: Connected device, or NULL if none
358 */
359struct spi_cs_info {
360 struct udevice *dev;
361};
362
363/**
364 * struct struct dm_spi_ops - Driver model SPI operations
365 *
366 * The uclass interface is implemented by all SPI devices which use
367 * driver model.
368 */
369struct dm_spi_ops {
370 /**
371 * Claim the bus and prepare it for communication.
372 *
373 * The device provided is the slave device. It's parent controller
374 * will be used to provide the communication.
375 *
376 * This must be called before doing any transfers with a SPI slave. It
377 * will enable and initialize any SPI hardware as necessary, and make
378 * sure that the SCK line is in the correct idle state. It is not
379 * allowed to claim the same bus for several slaves without releasing
380 * the bus in between.
381 *
Simon Glass9694b722015-04-19 09:05:40 -0600382 * @dev: The SPI slave
Simon Glassd7af6a42014-10-13 23:41:52 -0600383 *
384 * Returns: 0 if the bus was claimed successfully, or a negative value
385 * if it wasn't.
386 */
Simon Glass9694b722015-04-19 09:05:40 -0600387 int (*claim_bus)(struct udevice *dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600388
389 /**
390 * Release the SPI bus
391 *
392 * This must be called once for every call to spi_claim_bus() after
393 * all transfers have finished. It may disable any SPI hardware as
394 * appropriate.
395 *
Simon Glass9694b722015-04-19 09:05:40 -0600396 * @dev: The SPI slave
Simon Glassd7af6a42014-10-13 23:41:52 -0600397 */
Simon Glass9694b722015-04-19 09:05:40 -0600398 int (*release_bus)(struct udevice *dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600399
400 /**
401 * Set the word length for SPI transactions
402 *
403 * Set the word length (number of bits per word) for SPI transactions.
404 *
405 * @bus: The SPI slave
406 * @wordlen: The number of bits in a word
407 *
408 * Returns: 0 on success, -ve on failure.
409 */
Simon Glass9694b722015-04-19 09:05:40 -0600410 int (*set_wordlen)(struct udevice *dev, unsigned int wordlen);
Simon Glassd7af6a42014-10-13 23:41:52 -0600411
412 /**
413 * SPI transfer
414 *
415 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
416 * clocks "bitlen" bits in the SPI MISO port. That's just the way SPI
417 * works.
418 *
419 * The source of the outgoing bits is the "dout" parameter and the
420 * destination of the input bits is the "din" parameter. Note that
421 * "dout" and "din" can point to the same memory location, in which
422 * case the input data overwrites the output data (since both are
423 * buffered by temporary variables, this is OK).
424 *
425 * spi_xfer() interface:
426 * @dev: The slave device to communicate with
427 * @bitlen: How many bits to write and read.
428 * @dout: Pointer to a string of bits to send out. The bits are
429 * held in a byte array and are sent MSB first.
430 * @din: Pointer to a string of bits that will be filled in.
431 * @flags: A bitwise combination of SPI_XFER_* flags.
432 *
433 * Returns: 0 on success, not -1 on failure
434 */
435 int (*xfer)(struct udevice *dev, unsigned int bitlen, const void *dout,
436 void *din, unsigned long flags);
437
438 /**
439 * Set transfer speed.
440 * This sets a new speed to be applied for next spi_xfer().
441 * @bus: The SPI bus
442 * @hz: The transfer speed
443 * @return 0 if OK, -ve on error
444 */
445 int (*set_speed)(struct udevice *bus, uint hz);
446
447 /**
448 * Set the SPI mode/flags
449 *
450 * It is unclear if we want to set speed and mode together instead
451 * of separately.
452 *
453 * @bus: The SPI bus
454 * @mode: Requested SPI mode (SPI_... flags)
455 * @return 0 if OK, -ve on error
456 */
457 int (*set_mode)(struct udevice *bus, uint mode);
458
459 /**
460 * Get information on a chip select
461 *
462 * This is only called when the SPI uclass does not know about a
463 * chip select, i.e. it has no attached device. It gives the driver
464 * a chance to allow activity on that chip select even so.
465 *
466 * @bus: The SPI bus
467 * @cs: The chip select (0..n-1)
468 * @info: Returns information about the chip select, if valid.
469 * On entry info->dev is NULL
470 * @return 0 if OK (and @info is set up), -ENODEV if the chip select
471 * is invalid, other -ve value on error
472 */
473 int (*cs_info)(struct udevice *bus, uint cs, struct spi_cs_info *info);
474};
475
Simon Glassc60e1f22014-10-13 23:41:53 -0600476struct dm_spi_emul_ops {
477 /**
478 * SPI transfer
479 *
480 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
481 * clocks "bitlen" bits in the SPI MISO port. That's just the way SPI
482 * works. Here the device is a slave.
483 *
484 * The source of the outgoing bits is the "dout" parameter and the
485 * destination of the input bits is the "din" parameter. Note that
486 * "dout" and "din" can point to the same memory location, in which
487 * case the input data overwrites the output data (since both are
488 * buffered by temporary variables, this is OK).
489 *
490 * spi_xfer() interface:
491 * @slave: The SPI slave which will be sending/receiving the data.
492 * @bitlen: How many bits to write and read.
493 * @dout: Pointer to a string of bits sent to the device. The
494 * bits are held in a byte array and are sent MSB first.
495 * @din: Pointer to a string of bits that will be sent back to
496 * the master.
497 * @flags: A bitwise combination of SPI_XFER_* flags.
498 *
499 * Returns: 0 on success, not -1 on failure
500 */
501 int (*xfer)(struct udevice *slave, unsigned int bitlen,
502 const void *dout, void *din, unsigned long flags);
503};
504
Simon Glassd7af6a42014-10-13 23:41:52 -0600505/**
506 * spi_find_bus_and_cs() - Find bus and slave devices by number
507 *
508 * Given a bus number and chip select, this finds the corresponding bus
509 * device and slave device. Neither device is activated by this function,
510 * although they may have been activated previously.
511 *
512 * @busnum: SPI bus number
513 * @cs: Chip select to look for
514 * @busp: Returns bus device
515 * @devp: Return slave device
516 * @return 0 if found, -ENODEV on error
517 */
518int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
519 struct udevice **devp);
520
521/**
522 * spi_get_bus_and_cs() - Find and activate bus and slave devices by number
523 *
524 * Given a bus number and chip select, this finds the corresponding bus
525 * device and slave device.
526 *
527 * If no such slave exists, and drv_name is not NULL, then a new slave device
528 * is automatically bound on this chip select.
529 *
530 * Ths new slave device is probed ready for use with the given speed and mode.
531 *
532 * @busnum: SPI bus number
533 * @cs: Chip select to look for
534 * @speed: SPI speed to use for this slave
535 * @mode: SPI mode to use for this slave
536 * @drv_name: Name of driver to attach to this chip select
537 * @dev_name: Name of the new device thus created
538 * @busp: Returns bus device
539 * @devp: Return slave device
540 * @return 0 if found, -ve on error
541 */
542int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
543 const char *drv_name, const char *dev_name,
544 struct udevice **busp, struct spi_slave **devp);
545
546/**
547 * spi_chip_select() - Get the chip select for a slave
548 *
549 * @return the chip select this slave is attached to
550 */
551int spi_chip_select(struct udevice *slave);
552
553/**
Simon Glassff56bba2014-11-11 10:46:22 -0700554 * spi_find_chip_select() - Find the slave attached to chip select
555 *
556 * @bus: SPI bus to search
557 * @cs: Chip select to look for
558 * @devp: Returns the slave device if found
559 * @return 0 if found, -ENODEV on error
560 */
561int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp);
562
563/**
Simon Glassd0cff032015-01-25 08:27:12 -0700564 * spi_slave_ofdata_to_platdata() - decode standard SPI platform data
Simon Glassd7af6a42014-10-13 23:41:52 -0600565 *
Simon Glassd0cff032015-01-25 08:27:12 -0700566 * This decodes the speed and mode for a slave from a device tree node
Simon Glassd7af6a42014-10-13 23:41:52 -0600567 *
568 * @blob: Device tree blob
569 * @node: Node offset to read from
Simon Glassd0cff032015-01-25 08:27:12 -0700570 * @plat: Place to put the decoded information
Simon Glassd7af6a42014-10-13 23:41:52 -0600571 */
Simon Glassd0cff032015-01-25 08:27:12 -0700572int spi_slave_ofdata_to_platdata(const void *blob, int node,
573 struct dm_spi_slave_platdata *plat);
Simon Glassd7af6a42014-10-13 23:41:52 -0600574
575/**
576 * spi_cs_info() - Check information on a chip select
577 *
578 * This checks a particular chip select on a bus to see if it has a device
579 * attached, or is even valid.
580 *
581 * @bus: The SPI bus
582 * @cs: The chip select (0..n-1)
583 * @info: Returns information about the chip select, if valid
584 * @return 0 if OK (and @info is set up), -ENODEV if the chip select
585 * is invalid, other -ve value on error
586 */
587int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info);
588
589struct sandbox_state;
Simon Glassc60e1f22014-10-13 23:41:53 -0600590
591/**
592 * sandbox_spi_get_emul() - get an emulator for a SPI slave
593 *
594 * This provides a way to attach an emulated SPI device to a particular SPI
595 * slave, so that xfer() operations on the slave will be handled by the
596 * emulator. If a emulator already exists on that chip select it is returned.
597 * Otherwise one is created.
598 *
599 * @state: Sandbox state
600 * @bus: SPI bus requesting the emulator
601 * @slave: SPI slave device requesting the emulator
602 * @emuip: Returns pointer to emulator
603 * @return 0 if OK, -ve on error
604 */
Simon Glassd7af6a42014-10-13 23:41:52 -0600605int sandbox_spi_get_emul(struct sandbox_state *state,
606 struct udevice *bus, struct udevice *slave,
607 struct udevice **emulp);
608
Peng Fan7a3eff42016-05-03 10:02:22 +0800609/**
610 * Claim the bus and prepare it for communication with a given slave.
611 *
612 * This must be called before doing any transfers with a SPI slave. It
613 * will enable and initialize any SPI hardware as necessary, and make
614 * sure that the SCK line is in the correct idle state. It is not
615 * allowed to claim the same bus for several slaves without releasing
616 * the bus in between.
617 *
618 * @dev: The SPI slave device
619 *
620 * Returns: 0 if the bus was claimed successfully, or a negative value
621 * if it wasn't.
622 */
623int dm_spi_claim_bus(struct udevice *dev);
624
625/**
626 * Release the SPI bus
627 *
628 * This must be called once for every call to dm_spi_claim_bus() after
629 * all transfers have finished. It may disable any SPI hardware as
630 * appropriate.
631 *
632 * @slave: The SPI slave device
633 */
634void dm_spi_release_bus(struct udevice *dev);
635
636/**
637 * SPI transfer
638 *
639 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
640 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
641 *
642 * The source of the outgoing bits is the "dout" parameter and the
643 * destination of the input bits is the "din" parameter. Note that "dout"
644 * and "din" can point to the same memory location, in which case the
645 * input data overwrites the output data (since both are buffered by
646 * temporary variables, this is OK).
647 *
648 * dm_spi_xfer() interface:
649 * @dev: The SPI slave device which will be sending/receiving the data.
650 * @bitlen: How many bits to write and read.
651 * @dout: Pointer to a string of bits to send out. The bits are
652 * held in a byte array and are sent MSB first.
653 * @din: Pointer to a string of bits that will be filled in.
654 * @flags: A bitwise combination of SPI_XFER_* flags.
655 *
656 * Returns: 0 on success, not 0 on failure
657 */
658int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
659 const void *dout, void *din, unsigned long flags);
660
Simon Glassbc5701e2015-04-20 12:37:12 -0600661/* Access the operations for a SPI device */
Simon Glassd7af6a42014-10-13 23:41:52 -0600662#define spi_get_ops(dev) ((struct dm_spi_ops *)(dev)->driver->ops)
Simon Glassc60e1f22014-10-13 23:41:53 -0600663#define spi_emul_get_ops(dev) ((struct dm_spi_emul_ops *)(dev)->driver->ops)
Simon Glassd7af6a42014-10-13 23:41:52 -0600664#endif /* CONFIG_DM_SPI */
665
wdenk77f85582002-09-26 02:01:47 +0000666#endif /* _SPI_H_ */