fdtvar.h revision 1.55 1 /* $NetBSD: fdtvar.h,v 1.55 2019/10/28 21:14:58 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #ifndef _DEV_FDT_FDTVAR_H
30 #define _DEV_FDT_FDTVAR_H
31
32 #include <sys/types.h>
33 #include <sys/bus.h>
34 #include <sys/gpio.h>
35 #include <sys/termios.h>
36
37 #include <dev/i2c/i2cvar.h>
38 #include <dev/pwm/pwmvar.h>
39 #include <dev/clk/clk.h>
40
41 #ifdef _KERNEL_OPT
42 #include "audio.h"
43 #endif
44 #if NAUDIO > 0
45 #include <dev/audio/audio_dai.h>
46 #else
47 typedef void *audio_dai_tag_t;
48 #endif
49
50 #include <dev/clock_subr.h>
51
52 #include <dev/ofw/openfirm.h>
53
54 struct fdt_attach_args {
55 const char *faa_name;
56 bus_space_tag_t faa_bst;
57 bus_space_tag_t faa_a4x_bst;
58 bus_dma_tag_t faa_dmat;
59 int faa_phandle;
60 int faa_quiet;
61 };
62
63 /* flags for fdtbus_intr_establish */
64 #define FDT_INTR_MPSAFE __BIT(0)
65
66 /* Interrupt trigger types defined by the FDT "interrupts" bindings. */
67 #define FDT_INTR_TYPE_POS_EDGE __BIT(0)
68 #define FDT_INTR_TYPE_NEG_EDGE __BIT(1)
69 #define FDT_INTR_TYPE_DOUBLE_EDGE (FDT_INTR_TYPE_POS_EDGE | \
70 FDT_INTR_TYPE_NEG_EDGE)
71 #define FDT_INTR_TYPE_HIGH_LEVEL __BIT(2)
72 #define FDT_INTR_TYPE_LOW_LEVEL __BIT(3)
73
74 struct fdtbus_interrupt_controller_func {
75 void * (*establish)(device_t, u_int *, int, int,
76 int (*)(void *), void *);
77 void (*disestablish)(device_t, void *);
78 bool (*intrstr)(device_t, u_int *, char *, size_t);
79 };
80
81 struct fdtbus_i2c_controller_func {
82 i2c_tag_t (*get_tag)(device_t);
83 };
84
85 struct fdtbus_spi_controller_func {
86 struct spi_controller * (*get_controller)(device_t);
87 };
88
89 struct fdtbus_gpio_controller;
90
91 struct fdtbus_gpio_pin {
92 struct fdtbus_gpio_controller *gp_gc;
93 void *gp_priv;
94 };
95
96 struct fdtbus_gpio_controller_func {
97 void * (*acquire)(device_t, const void *, size_t, int);
98 void (*release)(device_t, void *);
99 int (*read)(device_t, void *, bool);
100 void (*write)(device_t, void *, int, bool);
101 };
102
103 struct fdtbus_pinctrl_controller;
104
105 struct fdtbus_pinctrl_pin {
106 struct fdtbus_pinctrl_controller *pp_pc;
107 void *pp_priv;
108 };
109
110 struct fdtbus_pinctrl_controller_func {
111 int (*set_config)(device_t, const void *, size_t);
112 };
113
114 struct fdtbus_regulator_controller;
115
116 struct fdtbus_regulator {
117 struct fdtbus_regulator_controller *reg_rc;
118 };
119
120 struct fdtbus_regulator_controller_func {
121 int (*acquire)(device_t);
122 void (*release)(device_t);
123 int (*enable)(device_t, bool);
124 int (*set_voltage)(device_t, u_int, u_int);
125 int (*get_voltage)(device_t, u_int *);
126 };
127
128 struct fdtbus_clock_controller_func {
129 struct clk * (*decode)(device_t, int, const void *, size_t);
130 };
131
132 struct fdtbus_reset_controller;
133
134 struct fdtbus_reset {
135 struct fdtbus_reset_controller *rst_rc;
136 void *rst_priv;
137 };
138
139 struct fdtbus_reset_controller_func {
140 void * (*acquire)(device_t, const void *, size_t);
141 void (*release)(device_t, void *);
142 int (*reset_assert)(device_t, void *);
143 int (*reset_deassert)(device_t, void *);
144 };
145
146 struct fdtbus_dai_controller_func {
147 audio_dai_tag_t (*get_tag)(device_t, const void *, size_t);
148 };
149
150 struct fdtbus_dma_controller;
151
152 struct fdtbus_dma {
153 struct fdtbus_dma_controller *dma_dc;
154 void *dma_priv;
155 };
156
157 enum fdtbus_dma_dir {
158 FDT_DMA_READ, /* device -> memory */
159 FDT_DMA_WRITE /* memory -> device */
160 };
161
162 struct fdtbus_dma_opt {
163 int opt_bus_width; /* Bus width */
164 int opt_burst_len; /* Burst length */
165 int opt_swap; /* Enable data swapping */
166 int opt_dblbuf; /* Enable double buffering */
167 int opt_wrap_len; /* Address wrap-around window */
168 };
169
170 struct fdtbus_dma_req {
171 bus_dma_segment_t *dreq_segs; /* Memory */
172 int dreq_nsegs;
173
174 bus_addr_t dreq_dev_phys; /* Device */
175 int dreq_sel; /* Device selector */
176
177 enum fdtbus_dma_dir dreq_dir; /* Transfer direction */
178
179 int dreq_block_irq; /* Enable IRQ at end of block */
180 int dreq_block_multi; /* Enable multiple block transfers */
181 int dreq_flow; /* Enable flow control */
182
183 struct fdtbus_dma_opt dreq_mem_opt; /* Memory options */
184 struct fdtbus_dma_opt dreq_dev_opt; /* Device options */
185 };
186
187 struct fdtbus_dma_controller_func {
188 void * (*acquire)(device_t, const void *, size_t,
189 void (*)(void *), void *);
190 void (*release)(device_t, void *);
191 int (*transfer)(device_t, void *, struct fdtbus_dma_req *);
192 void (*halt)(device_t, void *);
193 };
194
195 struct fdtbus_power_controller;
196
197 struct fdtbus_power_controller_func {
198 void (*reset)(device_t);
199 void (*poweroff)(device_t);
200 };
201
202 struct fdtbus_phy_controller;
203
204 struct fdtbus_phy {
205 struct fdtbus_phy_controller *phy_pc;
206 void *phy_priv;
207 };
208
209 struct fdtbus_phy_controller_func {
210 void * (*acquire)(device_t, const void *, size_t);
211 void (*release)(device_t, void *);
212 int (*enable)(device_t, void *, bool);
213 };
214
215 struct fdtbus_pwm_controller_func {
216 pwm_tag_t (*get_tag)(device_t, const void *, size_t);
217 };
218
219 struct fdtbus_mmc_pwrseq;
220
221 struct fdtbus_mmc_pwrseq_func {
222 void (*pre_power_on)(device_t);
223 void (*post_power_on)(device_t);
224 void (*power_off)(device_t);
225 void (*reset)(device_t);
226 };
227
228 struct syscon;
229
230 struct fdt_console {
231 int (*match)(int);
232 void (*consinit)(struct fdt_attach_args *, u_int);
233 };
234
235 struct fdt_console_info {
236 const struct fdt_console *ops;
237 };
238
239 struct fdt_phandle_data {
240 int phandle;
241 int count;
242 const u_int *values;
243 };
244
245 #define _FDT_CONSOLE_REGISTER(name) \
246 __link_set_add_rodata(fdt_consoles, __CONCAT(name,_consinfo));
247
248 #define FDT_CONSOLE(_name, _ops) \
249 static const struct fdt_console_info __CONCAT(_name,_consinfo) = { \
250 .ops = (_ops) \
251 }; \
252 _FDT_CONSOLE_REGISTER(_name)
253
254 struct fdt_opp_info {
255 const char * opp_compat;
256 bool (*opp_supported)(const int, const int);
257 };
258
259 #define _FDT_OPP_REGISTER(name) \
260 __link_set_add_rodata(fdt_opps, __CONCAT(name,_oppinfo));
261
262 #define FDT_OPP(_name, _compat, _suppfn) \
263 static const struct fdt_opp_info __CONCAT(_name,_oppinfo) = { \
264 .opp_compat = (_compat), \
265 .opp_supported = (_suppfn) \
266 }; \
267 _FDT_OPP_REGISTER(_name)
268
269 TAILQ_HEAD(fdt_conslist, fdt_console_info);
270
271 int fdtbus_register_interrupt_controller(device_t, int,
272 const struct fdtbus_interrupt_controller_func *);
273 int fdtbus_register_i2c_controller(device_t, int,
274 const struct fdtbus_i2c_controller_func *);
275 int fdtbus_register_spi_controller(device_t, int,
276 const struct fdtbus_spi_controller_func *);
277 int fdtbus_register_gpio_controller(device_t, int,
278 const struct fdtbus_gpio_controller_func *);
279 int fdtbus_register_pinctrl_config(device_t, int,
280 const struct fdtbus_pinctrl_controller_func *);
281 int fdtbus_register_regulator_controller(device_t, int,
282 const struct fdtbus_regulator_controller_func *);
283 int fdtbus_register_clock_controller(device_t, int,
284 const struct fdtbus_clock_controller_func *);
285 int fdtbus_register_reset_controller(device_t, int,
286 const struct fdtbus_reset_controller_func *);
287 int fdtbus_register_dai_controller(device_t, int,
288 const struct fdtbus_dai_controller_func *);
289 int fdtbus_register_dma_controller(device_t, int,
290 const struct fdtbus_dma_controller_func *);
291 int fdtbus_register_power_controller(device_t, int,
292 const struct fdtbus_power_controller_func *);
293 int fdtbus_register_phy_controller(device_t, int,
294 const struct fdtbus_phy_controller_func *);
295 int fdtbus_register_pwm_controller(device_t, int,
296 const struct fdtbus_pwm_controller_func *);
297 int fdtbus_register_mmc_pwrseq(device_t, int,
298 const struct fdtbus_mmc_pwrseq_func *);
299 int fdtbus_register_syscon(device_t, int, struct syscon *);
300
301 void fdtbus_set_decoderegprop(bool);
302
303 int fdtbus_get_reg(int, u_int, bus_addr_t *, bus_size_t *);
304 int fdtbus_get_reg_byname(int, const char *, bus_addr_t *,
305 bus_size_t *);
306 int fdtbus_get_reg64(int, u_int, uint64_t *, uint64_t *);
307 int fdtbus_get_phandle(int, const char *);
308 int fdtbus_get_phandle_with_data(int, const char *, const char *,
309 int, struct fdt_phandle_data *);
310 int fdtbus_get_phandle_from_native(int);
311 i2c_tag_t fdtbus_get_i2c_tag(int);
312 i2c_tag_t fdtbus_i2c_acquire(int, const char *);
313 void * fdtbus_intr_establish(int, u_int, int, int,
314 int (*func)(void *), void *arg);
315 void * fdtbus_intr_establish_byname(int, const char *, int, int,
316 int (*func)(void *), void *arg);
317 void * fdtbus_intr_establish_raw(int, const u_int *, int, int,
318 int (*func)(void *), void *arg);
319 void fdtbus_intr_disestablish(int, void *);
320 bool fdtbus_intr_str(int, u_int, char *, size_t);
321 bool fdtbus_intr_str_raw(int, const u_int *, char *, size_t);
322 struct fdtbus_gpio_pin *fdtbus_gpio_acquire(int, const char *, int);
323 struct fdtbus_gpio_pin *fdtbus_gpio_acquire_index(int, const char *, int, int);
324 void fdtbus_gpio_release(struct fdtbus_gpio_pin *);
325 int fdtbus_gpio_read(struct fdtbus_gpio_pin *);
326 void fdtbus_gpio_write(struct fdtbus_gpio_pin *, int);
327 int fdtbus_gpio_read_raw(struct fdtbus_gpio_pin *);
328 void fdtbus_gpio_write_raw(struct fdtbus_gpio_pin *, int);
329 audio_dai_tag_t fdtbus_dai_acquire(int, const char *);
330 audio_dai_tag_t fdtbus_dai_acquire_index(int, const char *, int);
331 pwm_tag_t fdtbus_pwm_acquire(int, const char *);
332 pwm_tag_t fdtbus_pwm_acquire_index(int, const char *, int);
333 int fdtbus_pinctrl_set_config_index(int, u_int);
334 int fdtbus_pinctrl_set_config(int, const char *);
335 bool fdtbus_pinctrl_has_config(int, const char *);
336 const char * fdtbus_pinctrl_parse_function(int);
337 const void * fdtbus_pinctrl_parse_pins(int, int *);
338 const char * fdtbus_pinctrl_parse_groups(int, int *);
339 const u_int * fdtbus_pinctrl_parse_pinmux(int, int *);
340 int fdtbus_pinctrl_parse_bias(int, int *);
341 int fdtbus_pinctrl_parse_drive(int);
342 int fdtbus_pinctrl_parse_drive_strength(int);
343 int fdtbus_pinctrl_parse_input_output(int, int *);
344 struct fdtbus_regulator *fdtbus_regulator_acquire(int, const char *);
345 void fdtbus_regulator_release(struct fdtbus_regulator *);
346 int fdtbus_regulator_enable(struct fdtbus_regulator *);
347 int fdtbus_regulator_disable(struct fdtbus_regulator *);
348 int fdtbus_regulator_set_voltage(struct fdtbus_regulator *,
349 u_int, u_int);
350 int fdtbus_regulator_get_voltage(struct fdtbus_regulator *,
351 u_int *);
352 int fdtbus_regulator_supports_voltage(struct fdtbus_regulator *,
353 u_int, u_int);
354 struct syscon * fdtbus_syscon_acquire(int, const char *);
355 struct syscon * fdtbus_syscon_lookup(int);
356
357 struct fdtbus_dma *fdtbus_dma_get(int, const char *, void (*)(void *), void *);
358 struct fdtbus_dma *fdtbus_dma_get_index(int, u_int, void (*)(void *),
359 void *);
360 void fdtbus_dma_put(struct fdtbus_dma *);
361 int fdtbus_dma_transfer(struct fdtbus_dma *,
362 struct fdtbus_dma_req *);
363 void fdtbus_dma_halt(struct fdtbus_dma *);
364
365 struct clk * fdtbus_clock_get(int, const char *);
366 struct clk * fdtbus_clock_get_index(int, u_int);
367 struct clk * fdtbus_clock_byname(const char *);
368 void fdtbus_clock_assign(int);
369
370 struct fdtbus_reset *fdtbus_reset_get(int, const char *);
371 struct fdtbus_reset *fdtbus_reset_get_index(int, u_int);
372 void fdtbus_reset_put(struct fdtbus_reset *);
373 int fdtbus_reset_assert(struct fdtbus_reset *);
374 int fdtbus_reset_deassert(struct fdtbus_reset *);
375
376 struct fdtbus_phy *fdtbus_phy_get(int, const char *);
377 struct fdtbus_phy *fdtbus_phy_get_index(int, u_int);
378 void fdtbus_phy_put(struct fdtbus_phy *);
379 device_t fdtbus_phy_device(struct fdtbus_phy *);
380 int fdtbus_phy_enable(struct fdtbus_phy *, bool);
381
382 struct fdtbus_mmc_pwrseq *fdtbus_mmc_pwrseq_get(int);
383 void fdtbus_mmc_pwrseq_pre_power_on(struct fdtbus_mmc_pwrseq *);
384 void fdtbus_mmc_pwrseq_post_power_on(struct fdtbus_mmc_pwrseq *);
385 void fdtbus_mmc_pwrseq_power_off(struct fdtbus_mmc_pwrseq *);
386 void fdtbus_mmc_pwrseq_reset(struct fdtbus_mmc_pwrseq *);
387
388 int fdtbus_todr_attach(device_t, int, todr_chip_handle_t);
389
390 void fdtbus_power_reset(void);
391 void fdtbus_power_poweroff(void);
392
393 device_t fdtbus_attach_i2cbus(device_t, int, i2c_tag_t, cfprint_t);
394 device_t fdtbus_attach_spibus(device_t, int, cfprint_t);
395
396 bool fdtbus_set_data(const void *);
397 const void * fdtbus_get_data(void);
398 int fdtbus_phandle2offset(int);
399 int fdtbus_offset2phandle(int);
400 bool fdtbus_get_path(int, char *, size_t);
401
402 const struct fdt_console *fdtbus_get_console(void);
403
404 const char * fdtbus_get_stdout_path(void);
405 int fdtbus_get_stdout_phandle(void);
406 int fdtbus_get_stdout_speed(void);
407 tcflag_t fdtbus_get_stdout_flags(void);
408
409 bool fdtbus_status_okay(int);
410
411 const void * fdtbus_get_prop(int, const char *, int *);
412 const char * fdtbus_get_string(int, const char *);
413 const char * fdtbus_get_string_index(int, const char *, u_int);
414 int fdtbus_get_index(int, const char *, const char *, u_int *);
415
416 void fdt_add_bus(device_t, int, struct fdt_attach_args *);
417 void fdt_add_bus_match(device_t, int, struct fdt_attach_args *,
418 bool (*)(void *, int), void *);
419 void fdt_add_child(device_t, int, struct fdt_attach_args *, u_int);
420
421 void fdt_remove_byhandle(int);
422 void fdt_remove_bycompat(const char *[]);
423 int fdt_find_with_property(const char *, int *);
424 int fdtbus_print(void *, const char *);
425
426 #endif /* _DEV_FDT_FDTVAR_H */
427