spivar.h revision 1.2.4.2 1 1.2.4.2 ad /* $NetBSD: spivar.h,v 1.2.4.2 2006/11/18 21:34:50 ad Exp $ */
2 1.2.4.2 ad
3 1.2.4.2 ad /*-
4 1.2.4.2 ad * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 1.2.4.2 ad * Copyright (c) 2006 Garrett D'Amore.
6 1.2.4.2 ad * All rights reserved.
7 1.2.4.2 ad *
8 1.2.4.2 ad * Portions of this code were written by Garrett D'Amore for the
9 1.2.4.2 ad * Champaign-Urbana Community Wireless Network Project.
10 1.2.4.2 ad *
11 1.2.4.2 ad * Redistribution and use in source and binary forms, with or
12 1.2.4.2 ad * without modification, are permitted provided that the following
13 1.2.4.2 ad * conditions are met:
14 1.2.4.2 ad * 1. Redistributions of source code must retain the above copyright
15 1.2.4.2 ad * notice, this list of conditions and the following disclaimer.
16 1.2.4.2 ad * 2. Redistributions in binary form must reproduce the above
17 1.2.4.2 ad * copyright notice, this list of conditions and the following
18 1.2.4.2 ad * disclaimer in the documentation and/or other materials provided
19 1.2.4.2 ad * with the distribution.
20 1.2.4.2 ad * 3. All advertising materials mentioning features or use of this
21 1.2.4.2 ad * software must display the following acknowledgements:
22 1.2.4.2 ad * This product includes software developed by the Urbana-Champaign
23 1.2.4.2 ad * Independent Media Center.
24 1.2.4.2 ad * This product includes software developed by Garrett D'Amore.
25 1.2.4.2 ad * 4. Urbana-Champaign Independent Media Center's name and Garrett
26 1.2.4.2 ad * D'Amore's name may not be used to endorse or promote products
27 1.2.4.2 ad * derived from this software without specific prior written permission.
28 1.2.4.2 ad *
29 1.2.4.2 ad * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30 1.2.4.2 ad * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31 1.2.4.2 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 1.2.4.2 ad * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.2.4.2 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34 1.2.4.2 ad * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35 1.2.4.2 ad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 1.2.4.2 ad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 1.2.4.2 ad * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 1.2.4.2 ad * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.2.4.2 ad * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 1.2.4.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 1.2.4.2 ad * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 1.2.4.2 ad */
43 1.2.4.2 ad
44 1.2.4.2 ad #ifndef _DEV_SPI_SPIVAR_H_
45 1.2.4.2 ad #define _DEV_SPI_SPIVAR_H_
46 1.2.4.2 ad
47 1.2.4.2 ad #include <sys/queue.h>
48 1.2.4.2 ad
49 1.2.4.2 ad /*
50 1.2.4.2 ad * Serial Peripheral Interface bus. This is a 4-wire bus common for
51 1.2.4.2 ad * connecting flash, clocks, sensors, and various other low-speed
52 1.2.4.2 ad * peripherials.
53 1.2.4.2 ad */
54 1.2.4.2 ad
55 1.2.4.2 ad struct spi_handle;
56 1.2.4.2 ad struct spi_transfer;
57 1.2.4.2 ad
58 1.2.4.2 ad /*
59 1.2.4.2 ad * De facto standard latching modes.
60 1.2.4.2 ad */
61 1.2.4.2 ad #define SPI_MODE_0 0 /* CPOL = 0, CPHA = 0 */
62 1.2.4.2 ad #define SPI_MODE_1 1 /* CPOL = 0, CPHA = 1 */
63 1.2.4.2 ad #define SPI_MODE_2 2 /* CPOL = 1, CPHA = 0 */
64 1.2.4.2 ad #define SPI_MODE_3 3 /* CPOL = 1, CPHA = 1 */
65 1.2.4.2 ad /* Philips' Microwire is just Mode 0 */
66 1.2.4.2 ad #define SPI_MODE_MICROWIRE SPI_MODE_0
67 1.2.4.2 ad
68 1.2.4.2 ad struct spi_controller {
69 1.2.4.2 ad void *sct_cookie; /* controller private data */
70 1.2.4.2 ad int sct_nslaves;
71 1.2.4.2 ad int (*sct_configure)(void *, int, int, int);
72 1.2.4.2 ad int (*sct_transfer)(void *, struct spi_transfer *);
73 1.2.4.2 ad };
74 1.2.4.2 ad
75 1.2.4.2 ad int spibus_print(void *, const char *);
76 1.2.4.2 ad
77 1.2.4.2 ad /* one per chip select */
78 1.2.4.2 ad struct spibus_attach_args {
79 1.2.4.2 ad struct spi_controller *sba_controller;
80 1.2.4.2 ad };
81 1.2.4.2 ad
82 1.2.4.2 ad struct spi_attach_args {
83 1.2.4.2 ad struct spi_handle *sa_handle;
84 1.2.4.2 ad };
85 1.2.4.2 ad
86 1.2.4.2 ad /*
87 1.2.4.2 ad * This is similar in some respects to struct buf, but we cannot use
88 1.2.4.2 ad * that structure because it was not designed to support full-duplex
89 1.2.4.2 ad * IO.
90 1.2.4.2 ad */
91 1.2.4.2 ad struct spi_chunk {
92 1.2.4.2 ad struct spi_chunk *chunk_next;
93 1.2.4.2 ad int chunk_count;
94 1.2.4.2 ad uint8_t *chunk_read;
95 1.2.4.2 ad const uint8_t *chunk_write;
96 1.2.4.2 ad /* for private use by framework and bus driver */
97 1.2.4.2 ad uint8_t *chunk_rptr;
98 1.2.4.2 ad const uint8_t *chunk_wptr;
99 1.2.4.2 ad int chunk_rresid;
100 1.2.4.2 ad int chunk_wresid;
101 1.2.4.2 ad };
102 1.2.4.2 ad
103 1.2.4.2 ad struct spi_transfer {
104 1.2.4.2 ad struct spi_chunk *st_chunks; /* chained bufs */
105 1.2.4.2 ad SIMPLEQ_ENTRY(spi_transfer) st_chain; /* chain of submitted jobs */
106 1.2.4.2 ad int st_flags;
107 1.2.4.2 ad int st_errno;
108 1.2.4.2 ad int st_slave;
109 1.2.4.2 ad void *st_private;
110 1.2.4.2 ad void (*st_done)(struct spi_transfer *);
111 1.2.4.2 ad struct simplelock st_lock;
112 1.2.4.2 ad void *st_busprivate;
113 1.2.4.2 ad };
114 1.2.4.2 ad
115 1.2.4.2 ad /* declare a list of transfers */
116 1.2.4.2 ad SIMPLEQ_HEAD(spi_transq, spi_transfer);
117 1.2.4.2 ad
118 1.2.4.2 ad #define spi_transq_init(q) \
119 1.2.4.2 ad SIMPLEQ_INIT(q)
120 1.2.4.2 ad
121 1.2.4.2 ad #define spi_transq_enqueue(q, trans) \
122 1.2.4.2 ad SIMPLEQ_INSERT_TAIL(q, trans, st_chain)
123 1.2.4.2 ad
124 1.2.4.2 ad #define spi_transq_dequeue(q) \
125 1.2.4.2 ad SIMPLEQ_REMOVE_HEAD(q, st_chain)
126 1.2.4.2 ad
127 1.2.4.2 ad #define spi_transq_first(q) \
128 1.2.4.2 ad SIMPLEQ_FIRST(q)
129 1.2.4.2 ad
130 1.2.4.2 ad #define SPI_F_DONE 0x0001
131 1.2.4.2 ad #define SPI_F_ERROR 0x0002
132 1.2.4.2 ad
133 1.2.4.2 ad int spi_configure(struct spi_handle *, int mode, int speed);
134 1.2.4.2 ad int spi_transfer(struct spi_handle *, struct spi_transfer *);
135 1.2.4.2 ad void spi_transfer_init(struct spi_transfer *);
136 1.2.4.2 ad void spi_chunk_init(struct spi_chunk *, int, const uint8_t *, uint8_t *);
137 1.2.4.2 ad void spi_transfer_add(struct spi_transfer *, struct spi_chunk *);
138 1.2.4.2 ad void spi_wait(struct spi_transfer *);
139 1.2.4.2 ad void spi_done(struct spi_transfer *, int);
140 1.2.4.2 ad
141 1.2.4.2 ad /* convenience wrappers */
142 1.2.4.2 ad int spi_send(struct spi_handle *, int, const uint8_t *);
143 1.2.4.2 ad int spi_recv(struct spi_handle *, int, uint8_t *);
144 1.2.4.2 ad int spi_send_recv(struct spi_handle *, int, const uint8_t *, int, uint8_t *);
145 1.2.4.2 ad
146 1.2.4.2 ad #endif /* _DEV_SPI_SPIVAR_H_ */
147