Add basic support for mini2440 board to barebox.
[barebox-mini2440.git] / include / spi / spi.h
blob8dce8dbd2b4397616ced53a671aebb8b079f13c3
1 #ifndef __INCLUDE_SPI_H
2 #define __INCLUDE_SPI_H
4 #ifndef DOXYGEN_SHOULD_SKIP_THIS
6 #include <driver.h>
8 struct spi_board_info {
9 char *name;
10 int max_speed_hz;
11 int bus_num;
12 int chip_select;
14 /* mode becomes spi_device.mode, and is essential for chips
15 * where the default of SPI_CS_HIGH = 0 is wrong.
17 u8 mode;
21 /**
22 * struct spi_device - Master side proxy for an SPI slave device
23 * @dev: Driver model representation of the device.
24 * @master: SPI controller used with the device.
25 * @max_speed_hz: Maximum clock rate to be used with this chip
26 * (on this board); may be changed by the device's driver.
27 * The spi_transfer.speed_hz can override this for each transfer.
28 * @chip_select: Chipselect, distinguishing chips handled by @master.
29 * @mode: The spi mode defines how data is clocked out and in.
30 * This may be changed by the device's driver.
31 * The "active low" default for chipselect mode can be overridden
32 * (by specifying SPI_CS_HIGH) as can the "MSB first" default for
33 * each word in a transfer (by specifying SPI_LSB_FIRST).
34 * @bits_per_word: Data transfers involve one or more words; word sizes
35 * like eight or 12 bits are common. In-memory wordsizes are
36 * powers of two bytes (e.g. 20 bit samples use 32 bits).
37 * This may be changed by the device's driver, or left at the
38 * default (0) indicating protocol words are eight bit bytes.
39 * The spi_transfer.bits_per_word can override this for each transfer.
40 * @irq: Negative, or the number passed to request_irq() to receive
41 * interrupts from this device.
42 * @controller_state: Controller's runtime state
43 * @controller_data: Board-specific definitions for controller, such as
44 * FIFO initialization parameters; from board_info.controller_data
45 * @modalias: Name of the driver to use with this device, or an alias
46 * for that name. This appears in the sysfs "modalias" attribute
47 * for driver coldplugging, and in uevents used for hotplugging
49 * A @spi_device is used to interchange data between an SPI slave
50 * (usually a discrete chip) and CPU memory.
52 * In @dev, the platform_data is used to hold information about this
53 * device that's meaningful to the device's protocol driver, but not
54 * to its controller. One example might be an identifier for a chip
55 * variant with slightly different functionality; another might be
56 * information about how this particular board wires the chip's pins.
58 struct spi_device {
59 struct device_d dev;
60 struct spi_master *master;
61 u32 max_speed_hz;
62 u8 chip_select;
63 u8 mode;
64 #define SPI_CPHA 0x01 /* clock phase */
65 #define SPI_CPOL 0x02 /* clock polarity */
66 #define SPI_MODE_0 (0|0) /* (original MicroWire) */
67 #define SPI_MODE_1 (0|SPI_CPHA)
68 #define SPI_MODE_2 (SPI_CPOL|0)
69 #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
70 #define SPI_CS_HIGH 0x04 /* chipselect active high? */
71 #define SPI_LSB_FIRST 0x08 /* per-word bits-on-wire */
72 #define SPI_3WIRE 0x10 /* SI/SO signals shared */
73 #define SPI_LOOP 0x20 /* loopback mode */
74 u8 bits_per_word;
75 int irq;
76 void *controller_state;
77 void *controller_data;
78 const char *modalias;
81 * likely need more hooks for more protocol options affecting how
82 * the controller talks to each chip, like:
83 * - memory packing (12 bit samples into low bits, others zeroed)
84 * - priority
85 * - drop chipselect after each word
86 * - chipselect delays
87 * - ...
91 struct spi_message;
93 /**
94 * struct spi_master - interface to SPI master controller
95 * @dev: device interface to this driver
96 * @bus_num: board-specific (and often SOC-specific) identifier for a
97 * given SPI controller.
98 * @num_chipselect: chipselects are used to distinguish individual
99 * SPI slaves, and are numbered from zero to num_chipselects.
100 * each slave has a chipselect signal, but it's common that not
101 * every chipselect is connected to a slave.
102 * @setup: updates the device mode and clocking records used by a
103 * device's SPI controller; protocol code may call this. This
104 * must fail if an unrecognized or unsupported mode is requested.
105 * It's always safe to call this unless transfers are pending on
106 * the device whose settings are being modified.
107 * @transfer: adds a message to the controller's transfer queue.
108 * @cleanup: frees controller-specific state
110 * Each SPI master controller can communicate with one or more @spi_device
111 * children. These make a small bus, sharing MOSI, MISO and SCK signals
112 * but not chip select signals. Each device may be configured to use a
113 * different clock rate, since those shared signals are ignored unless
114 * the chip is selected.
116 * The driver for an SPI controller manages access to those devices through
117 * a queue of spi_message transactions, copying data between CPU memory and
118 * an SPI slave device. For each such message it queues, it calls the
119 * message's completion function when the transaction completes.
121 struct spi_master {
122 struct device_d *dev;
124 /* other than negative (== assign one dynamically), bus_num is fully
125 * board-specific. usually that simplifies to being SOC-specific.
126 * example: one SOC has three SPI controllers, numbered 0..2,
127 * and one board's schematics might show it using SPI-2. software
128 * would normally use bus_num=2 for that controller.
130 s16 bus_num;
132 /* chipselects will be integral to many controllers; some others
133 * might use board-specific GPIOs.
135 u16 num_chipselect;
137 /* setup mode and clock, etc (spi driver may call many times) */
138 int (*setup)(struct spi_device *spi);
140 /* bidirectional bulk transfers
142 * + The transfer() method may not sleep; its main role is
143 * just to add the message to the queue.
144 * + For now there's no remove-from-queue operation, or
145 * any other request management
146 * + To a given spi_device, message queueing is pure fifo
148 * + The master's main job is to process its message queue,
149 * selecting a chip then transferring data
150 * + If there are multiple spi_device children, the i/o queue
151 * arbitration algorithm is unspecified (round robin, fifo,
152 * priority, reservations, preemption, etc)
154 * + Chipselect stays active during the entire message
155 * (unless modified by spi_transfer.cs_change != 0).
156 * + The message transfers use clock and SPI mode parameters
157 * previously established by setup() for this device
159 int (*transfer)(struct spi_device *spi,
160 struct spi_message *mesg);
162 /* called on release() to free memory provided by spi_master */
163 void (*cleanup)(struct spi_device *spi);
166 /*---------------------------------------------------------------------------*/
169 * I/O INTERFACE between SPI controller and protocol drivers
171 * Protocol drivers use a queue of spi_messages, each transferring data
172 * between the controller and memory buffers.
174 * The spi_messages themselves consist of a series of read+write transfer
175 * segments. Those segments always read the same number of bits as they
176 * write; but one or the other is easily ignored by passing a null buffer
177 * pointer. (This is unlike most types of I/O API, because SPI hardware
178 * is full duplex.)
180 * NOTE: Allocation of spi_transfer and spi_message memory is entirely
181 * up to the protocol driver, which guarantees the integrity of both (as
182 * well as the data buffers) for as long as the message is queued.
186 * struct spi_transfer - a read/write buffer pair
187 * @tx_buf: data to be written (dma-safe memory), or NULL
188 * @rx_buf: data to be read (dma-safe memory), or NULL
189 * @len: size of rx and tx buffers (in bytes)
190 * @speed_hz: Select a speed other then the device default for this
191 * transfer. If 0 the default (from @spi_device) is used.
192 * @bits_per_word: select a bits_per_word other then the device default
193 * for this transfer. If 0 the default (from @spi_device) is used.
194 * @cs_change: affects chipselect after this transfer completes
195 * @delay_usecs: microseconds to delay after this transfer before
196 * (optionally) changing the chipselect status, then starting
197 * the next transfer or completing this @spi_message.
198 * @transfer_list: transfers are sequenced through @spi_message.transfers
200 * SPI transfers always write the same number of bytes as they read.
201 * Protocol drivers should always provide @rx_buf and/or @tx_buf.
203 * If the transmit buffer is null, zeroes will be shifted out
204 * while filling @rx_buf. If the receive buffer is null, the data
205 * shifted in will be discarded. Only "len" bytes shift out (or in).
206 * It's an error to try to shift out a partial word. (For example, by
207 * shifting out three bytes with word size of sixteen or twenty bits;
208 * the former uses two bytes per word, the latter uses four bytes.)
210 * In-memory data values are always in native CPU byte order, translated
211 * from the wire byte order (big-endian except with SPI_LSB_FIRST). So
212 * for example when bits_per_word is sixteen, buffers are 2N bytes long
213 * (@len = 2N) and hold N sixteen bit words in CPU byte order.
215 * When the word size of the SPI transfer is not a power-of-two multiple
216 * of eight bits, those in-memory words include extra bits. In-memory
217 * words are always seen by protocol drivers as right-justified, so the
218 * undefined (rx) or unused (tx) bits are always the most significant bits.
220 * All SPI transfers start with the relevant chipselect active. Normally
221 * it stays selected until after the last transfer in a message. Drivers
222 * can affect the chipselect signal using cs_change.
224 * (i) If the transfer isn't the last one in the message, this flag is
225 * used to make the chipselect briefly go inactive in the middle of the
226 * message. Toggling chipselect in this way may be needed to terminate
227 * a chip command, letting a single spi_message perform all of group of
228 * chip transactions together.
230 * (ii) When the transfer is the last one in the message, the chip may
231 * stay selected until the next transfer. On multi-device SPI busses
232 * with nothing blocking messages going to other devices, this is just
233 * a performance hint; starting a message to another device deselects
234 * this one. But in other cases, this can be used to ensure correctness.
235 * Some devices need protocol transactions to be built from a series of
236 * spi_message submissions, where the content of one message is determined
237 * by the results of previous messages and where the whole transaction
238 * ends when the chipselect goes intactive.
240 * The code that submits an spi_message (and its spi_transfers)
241 * to the lower layers is responsible for managing its memory.
242 * Zero-initialize every field you don't set up explicitly, to
243 * insulate against future API updates. After you submit a message
244 * and its transfers, ignore them until its completion callback.
246 struct spi_transfer {
247 /* it's ok if tx_buf == rx_buf (right?)
248 * for MicroWire, one buffer must be null
249 * buffers must work with dma_*map_single() calls, unless
250 * spi_message.is_dma_mapped reports a pre-existing mapping
252 const void *tx_buf;
253 void *rx_buf;
254 unsigned len;
256 unsigned cs_change:1;
257 u8 bits_per_word;
258 u16 delay_usecs;
259 u32 speed_hz;
261 struct list_head transfer_list;
265 * struct spi_message - one multi-segment SPI transaction
266 * @transfers: list of transfer segments in this transaction
267 * @spi: SPI device to which the transaction is queued
268 * @actual_length: the total number of bytes that were transferred in all
269 * successful segments
270 * @status: zero for success, else negative errno
271 * @queue: for use by whichever driver currently owns the message
272 * @state: for use by whichever driver currently owns the message
274 * A @spi_message is used to execute an atomic sequence of data transfers,
275 * each represented by a struct spi_transfer. The sequence is "atomic"
276 * in the sense that no other spi_message may use that SPI bus until that
277 * sequence completes. On some systems, many such sequences can execute as
278 * as single programmed DMA transfer. On all systems, these messages are
279 * queued, and might complete after transactions to other devices. Messages
280 * sent to a given spi_device are alway executed in FIFO order.
282 * The code that submits an spi_message (and its spi_transfers)
283 * to the lower layers is responsible for managing its memory.
284 * Zero-initialize every field you don't set up explicitly, to
285 * insulate against future API updates. After you submit a message
286 * and its transfers, ignore them until its completion callback.
288 struct spi_message {
289 struct list_head transfers;
291 struct spi_device *spi;
293 /* REVISIT: we might want a flag affecting the behavior of the
294 * last transfer ... allowing things like "read 16 bit length L"
295 * immediately followed by "read L bytes". Basically imposing
296 * a specific message scheduling algorithm.
298 * Some controller drivers (message-at-a-time queue processing)
299 * could provide that as their default scheduling algorithm. But
300 * others (with multi-message pipelines) could need a flag to
301 * tell them about such special cases.
304 unsigned actual_length;
305 int status;
307 /* for optional use by whatever driver currently owns the
308 * spi_message ... between calls to spi_async and then later
309 * complete(), that's the spi_master controller driver.
311 struct list_head queue;
312 void *state;
315 static inline void spi_message_init(struct spi_message *m)
317 memset(m, 0, sizeof *m);
318 INIT_LIST_HEAD(&m->transfers);
321 static inline void
322 spi_message_add_tail(struct spi_transfer *t, struct spi_message *m)
324 list_add_tail(&t->transfer_list, &m->transfers);
327 static inline void
328 spi_transfer_del(struct spi_transfer *t)
330 list_del(&t->transfer_list);
333 /* All these synchronous SPI transfer routines are utilities layered
334 * over the core async transfer primitive. Here, "synchronous" means
335 * they will sleep uninterruptibly until the async transfer completes.
338 int spi_sync(struct spi_device *spi, struct spi_message *message);
340 int spi_register_master(struct spi_master *master);
342 #ifdef CONFIG_SPI
343 int spi_register_board_info(struct spi_board_info const *info, int num);
344 #else
345 static inline int spi_register_board_info(struct spi_board_info const *info,
346 int num)
348 return 0;
350 #endif
352 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
354 #endif /* __INCLUDE_SPI_H */