spivar.h revision 1.26 1 1.26 brad /* $NetBSD: spivar.h,v 1.26 2025/10/10 18:36:17 brad Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*-
4 1.1 gdamore * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 1.1 gdamore * Copyright (c) 2006 Garrett D'Amore.
6 1.1 gdamore * All rights reserved.
7 1.1 gdamore *
8 1.1 gdamore * Portions of this code were written by Garrett D'Amore for the
9 1.1 gdamore * Champaign-Urbana Community Wireless Network Project.
10 1.1 gdamore *
11 1.1 gdamore * Redistribution and use in source and binary forms, with or
12 1.1 gdamore * without modification, are permitted provided that the following
13 1.1 gdamore * conditions are met:
14 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
15 1.1 gdamore * notice, this list of conditions and the following disclaimer.
16 1.1 gdamore * 2. Redistributions in binary form must reproduce the above
17 1.1 gdamore * copyright notice, this list of conditions and the following
18 1.1 gdamore * disclaimer in the documentation and/or other materials provided
19 1.1 gdamore * with the distribution.
20 1.1 gdamore * 3. All advertising materials mentioning features or use of this
21 1.1 gdamore * software must display the following acknowledgements:
22 1.1 gdamore * This product includes software developed by the Urbana-Champaign
23 1.1 gdamore * Independent Media Center.
24 1.1 gdamore * This product includes software developed by Garrett D'Amore.
25 1.1 gdamore * 4. Urbana-Champaign Independent Media Center's name and Garrett
26 1.1 gdamore * D'Amore's name may not be used to endorse or promote products
27 1.1 gdamore * derived from this software without specific prior written permission.
28 1.1 gdamore *
29 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30 1.1 gdamore * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31 1.1 gdamore * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 1.1 gdamore * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34 1.1 gdamore * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35 1.1 gdamore * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 1.1 gdamore * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 1.1 gdamore * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1 gdamore * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 1.1 gdamore * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 1.1 gdamore */
43 1.1 gdamore
44 1.1 gdamore #ifndef _DEV_SPI_SPIVAR_H_
45 1.1 gdamore #define _DEV_SPI_SPIVAR_H_
46 1.1 gdamore
47 1.23 thorpej #include <sys/device.h>
48 1.1 gdamore #include <sys/queue.h>
49 1.26 brad #include <sys/uio.h> /* for iovec */
50 1.1 gdamore
51 1.1 gdamore /*
52 1.1 gdamore * Serial Peripheral Interface bus. This is a 4-wire bus common for
53 1.1 gdamore * connecting flash, clocks, sensors, and various other low-speed
54 1.6 dholland * peripherals.
55 1.1 gdamore */
56 1.1 gdamore
57 1.1 gdamore struct spi_handle;
58 1.1 gdamore struct spi_transfer;
59 1.1 gdamore
60 1.22 thorpej typedef struct spi_handle *spi_handle_t;
61 1.22 thorpej
62 1.13 thorpej #define SPI_MODE_CPHA __BIT(0)
63 1.13 thorpej #define SPI_MODE_CPOL __BIT(1)
64 1.13 thorpej
65 1.1 gdamore /*
66 1.1 gdamore * De facto standard latching modes.
67 1.1 gdamore */
68 1.13 thorpej #define SPI_MODE_0 0
69 1.13 thorpej #define SPI_MODE_1 SPI_MODE_CPHA
70 1.13 thorpej #define SPI_MODE_2 SPI_MODE_CPOL
71 1.13 thorpej #define SPI_MODE_3 (SPI_MODE_CPHA | SPI_MODE_CPOL)
72 1.13 thorpej
73 1.1 gdamore /* Philips' Microwire is just Mode 0 */
74 1.1 gdamore #define SPI_MODE_MICROWIRE SPI_MODE_0
75 1.1 gdamore
76 1.13 thorpej /* SPI transfer speed helper macros -- converts to Hz for spi_configure(). */
77 1.13 thorpej #define SPI_FREQ_kHz(x) ((x) * 1000)
78 1.13 thorpej #define SPI_FREQ_MHz(x) ((x) * 1000000)
79 1.13 thorpej
80 1.25 thorpej /* Additional transfer mode flags. */
81 1.25 thorpej #define SPI_F_3WIRE __BIT(0) /* requires 3-wire mode */
82 1.25 thorpej #define SPI_F_CS_HIGH __BIT(1) /* chip select is active-high */
83 1.25 thorpej #define SPI_F_LSB __BIT(2) /* transfer LSB first */
84 1.25 thorpej
85 1.1 gdamore struct spi_controller {
86 1.1 gdamore void *sct_cookie; /* controller private data */
87 1.1 gdamore int sct_nslaves;
88 1.1 gdamore int (*sct_configure)(void *, int, int, int);
89 1.1 gdamore int (*sct_transfer)(void *, struct spi_transfer *);
90 1.1 gdamore };
91 1.1 gdamore
92 1.14 thorpej int spibus_print(void *, const char *);
93 1.1 gdamore
94 1.1 gdamore struct spibus_attach_args {
95 1.18 thorpej const struct spi_controller *sba_controller;
96 1.1 gdamore };
97 1.1 gdamore
98 1.1 gdamore struct spi_attach_args {
99 1.24 thorpej spi_handle_t sa_handle;
100 1.22 thorpej
101 1.8 tnn /* only set if using direct config */
102 1.24 thorpej const char *sa_name; /* name of the device */
103 1.24 thorpej const char *sa_clist; /* compatible strlist */
104 1.24 thorpej size_t sa_clist_size; /* size of compatible strlist */
105 1.23 thorpej devhandle_t sa_devhandle; /* device handle for the device */
106 1.1 gdamore };
107 1.1 gdamore
108 1.1 gdamore /*
109 1.1 gdamore * This is similar in some respects to struct buf, but we cannot use
110 1.1 gdamore * that structure because it was not designed to support full-duplex
111 1.1 gdamore * IO.
112 1.1 gdamore */
113 1.1 gdamore struct spi_chunk {
114 1.1 gdamore struct spi_chunk *chunk_next;
115 1.1 gdamore int chunk_count;
116 1.1 gdamore uint8_t *chunk_read;
117 1.1 gdamore const uint8_t *chunk_write;
118 1.1 gdamore /* for private use by framework and bus driver */
119 1.1 gdamore uint8_t *chunk_rptr;
120 1.1 gdamore const uint8_t *chunk_wptr;
121 1.1 gdamore int chunk_rresid;
122 1.1 gdamore int chunk_wresid;
123 1.1 gdamore };
124 1.1 gdamore
125 1.1 gdamore struct spi_transfer {
126 1.1 gdamore struct spi_chunk *st_chunks; /* chained bufs */
127 1.1 gdamore SIMPLEQ_ENTRY(spi_transfer) st_chain; /* chain of submitted jobs */
128 1.10 kardel volatile int st_flags;
129 1.1 gdamore int st_errno;
130 1.1 gdamore int st_slave;
131 1.1 gdamore void *st_private;
132 1.1 gdamore void (*st_done)(struct spi_transfer *);
133 1.4 rmind kmutex_t st_lock;
134 1.4 rmind kcondvar_t st_cv;
135 1.2 gdamore void *st_busprivate;
136 1.7 mlelstv void *st_spiprivate;
137 1.1 gdamore };
138 1.1 gdamore
139 1.1 gdamore /* declare a list of transfers */
140 1.1 gdamore SIMPLEQ_HEAD(spi_transq, spi_transfer);
141 1.1 gdamore
142 1.2 gdamore #define spi_transq_init(q) \
143 1.2 gdamore SIMPLEQ_INIT(q)
144 1.2 gdamore
145 1.1 gdamore #define spi_transq_enqueue(q, trans) \
146 1.1 gdamore SIMPLEQ_INSERT_TAIL(q, trans, st_chain)
147 1.1 gdamore
148 1.1 gdamore #define spi_transq_dequeue(q) \
149 1.1 gdamore SIMPLEQ_REMOVE_HEAD(q, st_chain)
150 1.1 gdamore
151 1.1 gdamore #define spi_transq_first(q) \
152 1.1 gdamore SIMPLEQ_FIRST(q)
153 1.1 gdamore
154 1.1 gdamore #define SPI_F_DONE 0x0001
155 1.1 gdamore #define SPI_F_ERROR 0x0002
156 1.1 gdamore
157 1.15 thorpej static inline device_t
158 1.18 thorpej spibus_attach(device_t dev, const struct spi_controller *sct)
159 1.15 thorpej {
160 1.15 thorpej struct spibus_attach_args sba = {
161 1.15 thorpej .sba_controller = sct,
162 1.15 thorpej };
163 1.16 thorpej return config_found(dev, &sba, spibus_print,
164 1.17 thorpej CFARGS(.iattr = "spibus",
165 1.17 thorpej .devhandle = device_handle(dev)));
166 1.15 thorpej }
167 1.15 thorpej
168 1.21 thorpej /*
169 1.21 thorpej * Constants to indicate the quality of a match made by a driver's
170 1.21 thorpej * match routine, from lowest to highest:
171 1.21 thorpej *
172 1.21 thorpej * -- Default indirect match; rely on kernel config file.
173 1.21 thorpej *
174 1.21 thorpej * -- Direct-config match by "compatible" string.
175 1.21 thorpej */
176 1.21 thorpej #define SPI_MATCH_DEFAULT 1
177 1.21 thorpej #define SPI_MATCH_DIRECT_COMPATIBLE 10 /* ...and up */
178 1.21 thorpej
179 1.21 thorpej bool spi_use_direct_match(const struct spi_attach_args *,
180 1.21 thorpej const struct device_compatible_entry *, int *);
181 1.24 thorpej int spi_compatible_match(const struct spi_attach_args *,
182 1.14 thorpej const struct device_compatible_entry *);
183 1.12 thorpej const struct device_compatible_entry *
184 1.14 thorpej spi_compatible_lookup(const struct spi_attach_args *,
185 1.14 thorpej const struct device_compatible_entry *);
186 1.22 thorpej
187 1.22 thorpej int spi_configure(device_t, spi_handle_t, int, int);
188 1.22 thorpej int spi_transfer(spi_handle_t, struct spi_transfer *);
189 1.14 thorpej void spi_transfer_init(struct spi_transfer *);
190 1.14 thorpej void spi_chunk_init(struct spi_chunk *, int, const uint8_t *, uint8_t *);
191 1.14 thorpej void spi_transfer_add(struct spi_transfer *, struct spi_chunk *);
192 1.14 thorpej void spi_wait(struct spi_transfer *);
193 1.14 thorpej void spi_done(struct spi_transfer *, int);
194 1.1 gdamore
195 1.1 gdamore /* convenience wrappers */
196 1.26 brad struct spi_chunk_q {
197 1.26 brad struct spi_chunk chunk;
198 1.26 brad SIMPLEQ_ENTRY(spi_chunk_q) chunk_q;
199 1.26 brad };
200 1.26 brad
201 1.22 thorpej int spi_send(spi_handle_t, int, const uint8_t *);
202 1.22 thorpej int spi_recv(spi_handle_t, int, uint8_t *);
203 1.22 thorpej int spi_send_recv(spi_handle_t, int, const uint8_t *, int, uint8_t *);
204 1.26 brad int spi_sendv(spi_handle_t, const struct iovec *, int);
205 1.1 gdamore
206 1.1 gdamore #endif /* _DEV_SPI_SPIVAR_H_ */
207