fdtvar.h revision 1.80 1 /* $NetBSD: fdtvar.h,v 1.80 2024/06/30 17:55:28 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_dma_tag_t faa_dmat;
58 int faa_phandle;
59 int faa_quiet;
60 };
61
62
63 struct fdtbus_clock_controller_func {
64 struct clk * (*decode)(device_t, int, const void *, size_t);
65 };
66
67
68 struct fdtbus_dai_controller_func {
69 audio_dai_tag_t (*get_tag)(device_t, const void *, size_t);
70 };
71
72
73 struct fdtbus_dma_controller;
74
75 struct fdtbus_dma {
76 struct fdtbus_dma_controller *dma_dc;
77 void *dma_priv;
78 };
79
80 enum fdtbus_dma_dir {
81 FDT_DMA_READ, /* device -> memory */
82 FDT_DMA_WRITE /* memory -> device */
83 };
84
85 struct fdtbus_dma_opt {
86 int opt_bus_width; /* Bus width */
87 int opt_burst_len; /* Burst length */
88 int opt_swap; /* Enable data swapping */
89 int opt_dblbuf; /* Enable double buffering */
90 int opt_wrap_len; /* Address wrap-around window */
91 };
92
93 struct fdtbus_dma_req {
94 bus_dma_segment_t *dreq_segs; /* Memory */
95 int dreq_nsegs;
96
97 bus_addr_t dreq_dev_phys; /* Device */
98 int dreq_sel; /* Device selector */
99
100 enum fdtbus_dma_dir dreq_dir; /* Transfer direction */
101
102 int dreq_block_irq; /* Enable IRQ at end of block */
103 int dreq_block_multi; /* Enable multiple block transfers */
104 int dreq_flow; /* Enable flow control */
105
106 struct fdtbus_dma_opt dreq_mem_opt; /* Memory options */
107 struct fdtbus_dma_opt dreq_dev_opt; /* Device options */
108 };
109
110 struct fdtbus_dma_controller_func {
111 void * (*acquire)(device_t, const void *, size_t,
112 void (*)(void *), void *);
113 void (*release)(device_t, void *);
114 int (*transfer)(device_t, void *, struct fdtbus_dma_req *);
115 void (*halt)(device_t, void *);
116 };
117
118
119 struct fdtbus_gpio_controller;
120
121 struct fdtbus_gpio_pin {
122 struct fdtbus_gpio_controller *gp_gc;
123 void *gp_priv;
124 };
125
126 struct fdtbus_gpio_controller_func {
127 void * (*acquire)(device_t, const void *, size_t, int);
128 void (*release)(device_t, void *);
129 int (*read)(device_t, void *, bool);
130 void (*write)(device_t, void *, int, bool);
131 };
132
133
134 /* flags for fdtbus_intr_establish */
135 #define FDT_INTR_MPSAFE __BIT(0)
136
137 /* Interrupt trigger types defined by the FDT "interrupts" bindings. */
138 #define FDT_INTR_TYPE_POS_EDGE __BIT(0)
139 #define FDT_INTR_TYPE_NEG_EDGE __BIT(1)
140 #define FDT_INTR_TYPE_DOUBLE_EDGE (FDT_INTR_TYPE_POS_EDGE | \
141 FDT_INTR_TYPE_NEG_EDGE)
142 #define FDT_INTR_TYPE_HIGH_LEVEL __BIT(2)
143 #define FDT_INTR_TYPE_LOW_LEVEL __BIT(3)
144
145 struct fdtbus_interrupt_controller_func {
146 void * (*establish)(device_t, u_int *, int, int,
147 int (*)(void *), void *, const char *);
148 void (*disestablish)(device_t, void *);
149 bool (*intrstr)(device_t, u_int *, char *, size_t);
150 void (*mask)(device_t, void *);
151 void (*unmask)(device_t, void *);
152 };
153
154
155 struct fdtbus_iommu_func {
156 bus_dma_tag_t (*map)(device_t, const u_int *, bus_dma_tag_t);
157 };
158
159
160 struct fdtbus_mbox_channel {
161 struct fdtbus_mbox_controller *mb_ctlr;
162 void *mb_priv;
163 };
164
165 struct fdtbus_mbox_controller_func {
166 void * (*mc_acquire)(device_t, const void *, size_t, void (*)(void *),
167 void *);
168 void (*mc_release)(device_t, void *);
169 int (*mc_recv)(device_t, void *, void *, size_t);
170 int (*mc_send)(device_t, void *, const void *, size_t);
171 };
172
173
174 struct fdtbus_mmc_pwrseq;
175
176 struct fdtbus_mmc_pwrseq_func {
177 void (*pre_power_on)(device_t);
178 void (*post_power_on)(device_t);
179 void (*power_off)(device_t);
180 void (*reset)(device_t);
181 };
182
183
184 struct fdtbus_phy_controller;
185
186 struct fdtbus_phy {
187 struct fdtbus_phy_controller *phy_pc;
188 void *phy_priv;
189 };
190
191 struct fdtbus_phy_controller_func {
192 void * (*acquire)(device_t, const void *, size_t);
193 void (*release)(device_t, void *);
194 int (*enable)(device_t, void *, bool);
195 };
196
197
198 struct fdtbus_pinctrl_controller;
199
200 struct fdtbus_pinctrl_pin {
201 struct fdtbus_pinctrl_controller *pp_pc;
202 void *pp_priv;
203 };
204
205 struct fdtbus_pinctrl_controller_func {
206 int (*set_config)(device_t, const void *, size_t);
207 };
208
209
210 struct fdtbus_power_controller;
211
212 struct fdtbus_power_controller_func {
213 void (*reset)(device_t);
214 void (*poweroff)(device_t);
215 };
216
217
218 struct fdtbus_powerdomain_controller;
219
220 struct fdtbus_powerdomain_controller_func {
221 void (*pdc_enable)(device_t, const uint32_t *, bool);
222 };
223
224
225 struct fdtbus_pwm_controller_func {
226 pwm_tag_t (*get_tag)(device_t, const void *, size_t);
227 };
228
229
230 struct fdtbus_regulator_controller;
231
232 struct fdtbus_regulator {
233 struct fdtbus_regulator_controller *reg_rc;
234 };
235
236 struct fdtbus_regulator_controller_func {
237 int (*acquire)(device_t);
238 void (*release)(device_t);
239 int (*enable)(device_t, bool);
240 int (*set_voltage)(device_t, u_int, u_int);
241 int (*get_voltage)(device_t, u_int *);
242 };
243
244
245 struct fdtbus_reset_controller;
246
247 struct fdtbus_reset {
248 struct fdtbus_reset_controller *rst_rc;
249 void *rst_priv;
250 };
251
252 struct fdtbus_reset_controller_func {
253 void * (*acquire)(device_t, const void *, size_t);
254 void (*release)(device_t, void *);
255 int (*reset_assert)(device_t, void *);
256 int (*reset_deassert)(device_t, void *);
257 };
258
259
260 struct fdtbus_spi_controller_func {
261 struct spi_controller * (*get_controller)(device_t);
262 };
263
264
265 struct syscon;
266
267
268 struct fdt_console {
269 int (*match)(int);
270 void (*consinit)(struct fdt_attach_args *, u_int);
271 };
272
273 struct fdt_console_info {
274 const struct fdt_console *ops;
275 };
276
277 struct fdt_phandle_data {
278 int phandle;
279 int count;
280 const u_int *values;
281 };
282
283 #define _FDT_CONSOLE_REGISTER(name) \
284 __link_set_add_rodata(fdt_consoles, __CONCAT(name,_consinfo));
285
286 #define FDT_CONSOLE(_name, _ops) \
287 static const struct fdt_console_info __CONCAT(_name,_consinfo) = { \
288 .ops = (_ops) \
289 }; \
290 _FDT_CONSOLE_REGISTER(_name)
291
292 struct fdt_opp_info {
293 const char * opp_compat;
294 bool (*opp_supported)(const int, const int);
295 };
296
297 #define _FDT_OPP_REGISTER(name) \
298 __link_set_add_rodata(fdt_opps, __CONCAT(name,_oppinfo));
299
300 #define FDT_OPP(_name, _compat, _suppfn) \
301 static const struct fdt_opp_info __CONCAT(_name,_oppinfo) = { \
302 .opp_compat = (_compat), \
303 .opp_supported = (_suppfn) \
304 }; \
305 _FDT_OPP_REGISTER(_name)
306
307 TAILQ_HEAD(fdt_conslist, fdt_console_info);
308
309 /*
310 * Platform-specific data
311 */
312
313 struct fdt_platform {
314 const struct pmap_devmap *
315 (*fp_devmap)(void);
316 void (*fp_bootstrap)(void);
317 int (*fp_mpstart)(void);
318 void (*fp_startup)(void);
319 void (*fp_init_attach_args)(struct fdt_attach_args *);
320 void (*fp_device_register)(device_t, void *);
321 void (*fp_device_register_post_config)(device_t, void *);
322 void (*fp_reset)(void);
323 void (*fp_delay)(u_int);
324 u_int (*fp_uart_freq)(void);
325 };
326
327 struct fdt_platform_info {
328 const char * fpi_compat;
329 const struct fdt_platform * fpi_ops;
330 };
331
332 #define FDT_PLATFORM_DEFAULT ""
333
334 #define _FDT_PLATFORM_REGISTER(name) \
335 __link_set_add_rodata(fdt_platforms, __CONCAT(name,_platinfo));
336
337 #define FDT_PLATFORM(_name, _compat, _ops) \
338 static const struct fdt_platform_info __CONCAT(_name,_platinfo) = { \
339 .fpi_compat = (_compat), \
340 .fpi_ops = (_ops) \
341 }; \
342 _FDT_PLATFORM_REGISTER(_name)
343
344 const struct fdt_platform *
345 fdt_platform_find(void);
346
347
348 struct fdt_dma_range {
349 paddr_t dr_sysbase;
350 bus_addr_t dr_busbase;
351 bus_size_t dr_len;
352 };
353
354 #define FDT_BUS_SPACE_FLAG_NONPOSTED_MMIO __BIT(0)
355
356 int fdtbus_register_clock_controller(device_t, int,
357 const struct fdtbus_clock_controller_func *);
358 int fdtbus_register_dai_controller(device_t, int,
359 const struct fdtbus_dai_controller_func *);
360 int fdtbus_register_dma_controller(device_t, int,
361 const struct fdtbus_dma_controller_func *);
362 int fdtbus_register_gpio_controller(device_t, int,
363 const struct fdtbus_gpio_controller_func *);
364 int fdtbus_register_i2c_controller(i2c_tag_t, int);
365 int fdtbus_register_interrupt_controller(device_t, int,
366 const struct fdtbus_interrupt_controller_func *);
367 int fdtbus_register_iommu(device_t, int,
368 const struct fdtbus_iommu_func *);
369 int fdtbus_register_mbox_controller(device_t, int,
370 const struct fdtbus_mbox_controller_func *);
371 int fdtbus_register_mmc_pwrseq(device_t, int,
372 const struct fdtbus_mmc_pwrseq_func *);
373 int fdtbus_register_pinctrl_config(device_t, int,
374 const struct fdtbus_pinctrl_controller_func *);
375 int fdtbus_register_power_controller(device_t, int,
376 const struct fdtbus_power_controller_func *);
377 int fdtbus_register_powerdomain_controller(device_t, int,
378 const struct fdtbus_powerdomain_controller_func *);
379 int fdtbus_register_phy_controller(device_t, int,
380 const struct fdtbus_phy_controller_func *);
381 int fdtbus_register_pwm_controller(device_t, int,
382 const struct fdtbus_pwm_controller_func *);
383 int fdtbus_register_regulator_controller(device_t, int,
384 const struct fdtbus_regulator_controller_func *);
385 int fdtbus_register_reset_controller(device_t, int,
386 const struct fdtbus_reset_controller_func *);
387 int fdtbus_register_spi_controller(device_t, int,
388 const struct fdtbus_spi_controller_func *);
389 int fdtbus_register_syscon(device_t, int, struct syscon *);
390
391 void fdtbus_set_decoderegprop(bool);
392
393 int fdtbus_get_reg(int, u_int, bus_addr_t *, bus_size_t *);
394 int fdtbus_get_reg_byname(int, const char *, bus_addr_t *,
395 bus_size_t *);
396 int fdtbus_get_reg64(int, u_int, uint64_t *, uint64_t *);
397 int fdtbus_get_addr_cells(int);
398 int fdtbus_get_size_cells(int);
399 uint64_t fdtbus_get_cells(const uint8_t *, int);
400 int fdtbus_get_phandle(int, const char *);
401 int fdtbus_get_phandle_with_data(int, const char *, const char *,
402 int, struct fdt_phandle_data *);
403 int fdtbus_get_phandle_from_native(int);
404
405
406 struct clk * fdtbus_clock_get(int, const char *);
407 struct clk * fdtbus_clock_get_index(int, u_int);
408 struct clk * fdtbus_clock_byname(const char *);
409 void fdtbus_clock_assign(int);
410 u_int fdtbus_clock_count(int, const char *);
411 int fdtbus_clock_enable(int, const char *, bool);
412 int fdtbus_clock_enable_index(int, u_int, bool);
413
414 audio_dai_tag_t fdtbus_dai_acquire(int, const char *);
415 audio_dai_tag_t fdtbus_dai_acquire_index(int, const char *, int);
416
417 struct fdtbus_dma *
418 fdtbus_dma_get(int, const char *, void (*)(void *), void *);
419 struct fdtbus_dma *
420 fdtbus_dma_get_index(int, u_int, void (*)(void *),
421 void *);
422 void fdtbus_dma_put(struct fdtbus_dma *);
423 int fdtbus_dma_transfer(struct fdtbus_dma *,
424 struct fdtbus_dma_req *);
425 void fdtbus_dma_halt(struct fdtbus_dma *);
426
427 int fdtbus_gpio_count(int, const char *);
428 struct fdtbus_gpio_pin *
429 fdtbus_gpio_acquire(int, const char *, int);
430 struct fdtbus_gpio_pin *
431 fdtbus_gpio_acquire_index(int, const char *, int, int);
432 void fdtbus_gpio_release(struct fdtbus_gpio_pin *);
433 int fdtbus_gpio_read(struct fdtbus_gpio_pin *);
434 void fdtbus_gpio_write(struct fdtbus_gpio_pin *, int);
435 int fdtbus_gpio_read_raw(struct fdtbus_gpio_pin *);
436 void fdtbus_gpio_write_raw(struct fdtbus_gpio_pin *, int);
437
438 i2c_tag_t fdtbus_i2c_get_tag(int);
439 i2c_tag_t fdtbus_i2c_acquire(int, const char *);
440
441 void * fdtbus_intr_establish(int, u_int, int, int,
442 int (*func)(void *), void *arg);
443 void * fdtbus_intr_establish_xname(int, u_int, int, int,
444 int (*func)(void *), void *arg, const char *);
445 void * fdtbus_intr_establish_byname(int, const char *, int, int,
446 int (*func)(void *), void *arg, const char *);
447 void * fdtbus_intr_establish_raw(int, const u_int *, int, int,
448 int (*func)(void *), void *arg, const char *);
449 void fdtbus_intr_mask(int, void *);
450 void fdtbus_intr_unmask(int, void *);
451 void fdtbus_intr_disestablish(int, void *);
452 bool fdtbus_intr_str(int, u_int, char *, size_t);
453 bool fdtbus_intr_str_raw(int, const u_int *, char *, size_t);
454 int fdtbus_intr_parent(int);
455
456 bus_dma_tag_t fdtbus_iommu_map(int, u_int, bus_dma_tag_t);
457 bus_dma_tag_t fdtbus_iommu_map_pci(int, uint32_t, bus_dma_tag_t);
458
459 struct fdtbus_mbox_channel *
460 fdtbus_mbox_get(int, const char *, void (*)(void *), void *);
461 struct fdtbus_mbox_channel *
462 fdtbus_mbox_get_index(int, u_int, void (*)(void *),
463 void *);
464 int fdtbus_mbox_send(struct fdtbus_mbox_channel *, const void *, size_t);
465 int fdtbus_mbox_recv(struct fdtbus_mbox_channel *, void *, size_t);
466 void fdtbus_mbox_put(struct fdtbus_mbox_channel *);
467
468 struct fdtbus_mmc_pwrseq *
469 fdtbus_mmc_pwrseq_get(int);
470 void fdtbus_mmc_pwrseq_pre_power_on(struct fdtbus_mmc_pwrseq *);
471 void fdtbus_mmc_pwrseq_post_power_on(struct fdtbus_mmc_pwrseq *);
472 void fdtbus_mmc_pwrseq_power_off(struct fdtbus_mmc_pwrseq *);
473 void fdtbus_mmc_pwrseq_reset(struct fdtbus_mmc_pwrseq *);
474
475 struct fdtbus_phy *
476 fdtbus_phy_get(int, const char *);
477 struct fdtbus_phy *
478 fdtbus_phy_get_index(int, u_int);
479 void fdtbus_phy_put(struct fdtbus_phy *);
480 device_t fdtbus_phy_device(struct fdtbus_phy *);
481 int fdtbus_phy_enable(struct fdtbus_phy *, bool);
482
483 int fdtbus_pinctrl_set_config_index(int, u_int);
484 int fdtbus_pinctrl_set_config(int, const char *);
485 bool fdtbus_pinctrl_has_config(int, const char *);
486 const char * fdtbus_pinctrl_parse_function(int);
487 const void * fdtbus_pinctrl_parse_pins(int, int *);
488 const char * fdtbus_pinctrl_parse_groups(int, int *);
489 const u_int * fdtbus_pinctrl_parse_pinmux(int, int *);
490 int fdtbus_pinctrl_parse_bias(int, int *);
491 int fdtbus_pinctrl_parse_drive(int);
492 int fdtbus_pinctrl_parse_drive_strength(int);
493 int fdtbus_pinctrl_parse_input_output(int, int *);
494
495 pwm_tag_t fdtbus_pwm_acquire(int, const char *);
496 pwm_tag_t fdtbus_pwm_acquire_index(int, const char *, int);
497
498 struct fdtbus_regulator *
499 fdtbus_regulator_acquire(int, const char *);
500 void fdtbus_regulator_release(struct fdtbus_regulator *);
501 int fdtbus_regulator_enable(struct fdtbus_regulator *);
502 int fdtbus_regulator_disable(struct fdtbus_regulator *);
503 int fdtbus_regulator_set_voltage(struct fdtbus_regulator *,
504 u_int, u_int);
505 int fdtbus_regulator_get_voltage(struct fdtbus_regulator *,
506 u_int *);
507 int fdtbus_regulator_supports_voltage(struct fdtbus_regulator *,
508 u_int, u_int);
509
510 struct fdtbus_reset *
511 fdtbus_reset_get(int, const char *);
512 struct fdtbus_reset *
513 fdtbus_reset_get_index(int, u_int);
514 void fdtbus_reset_put(struct fdtbus_reset *);
515 int fdtbus_reset_assert(struct fdtbus_reset *);
516 int fdtbus_reset_deassert(struct fdtbus_reset *);
517
518 int fdtbus_todr_attach(device_t, int, todr_chip_handle_t);
519
520 void fdtbus_power_reset(void);
521 void fdtbus_power_poweroff(void);
522
523 int fdtbus_powerdomain_enable(int);
524 int fdtbus_powerdomain_enable_index(int, int);
525 int fdtbus_powerdomain_disable(int);
526 int fdtbus_powerdomain_disable_index(int, int);
527
528 struct syscon * fdtbus_syscon_acquire(int, const char *);
529 struct syscon * fdtbus_syscon_lookup(int);
530
531
532 device_t fdtbus_attach_i2cbus(device_t, int, i2c_tag_t, cfprint_t);
533 device_t fdtbus_attach_spibus(device_t, int, cfprint_t);
534
535 bool fdtbus_init(const void *);
536 const void * fdtbus_get_data(void);
537 int fdtbus_phandle2offset(int);
538 int fdtbus_offset2phandle(int);
539 bool fdtbus_get_path(int, char *, size_t);
540
541 const struct fdt_console *
542 fdtbus_get_console(void);
543
544 const char * fdtbus_get_stdout_path(void);
545 int fdtbus_get_stdout_phandle(void);
546 int fdtbus_get_stdout_speed(void);
547 tcflag_t fdtbus_get_stdout_flags(void);
548
549 bool fdtbus_status_okay(int);
550
551 const void * fdtbus_get_prop(int, const char *, int *);
552 const char * fdtbus_get_string(int, const char *);
553 const char * fdtbus_get_string_index(int, const char *, u_int);
554 int fdtbus_get_index(int, const char *, const char *, u_int *);
555
556 void fdtbus_cpus_md_attach(device_t, device_t, void *);
557
558 void fdt_add_bus(device_t, int, struct fdt_attach_args *);
559 void fdt_add_bus_match(device_t, int, struct fdt_attach_args *,
560 bool (*)(void *, int), void *);
561 void fdt_add_child(device_t, int, struct fdt_attach_args *, u_int);
562
563 void fdt_remove_byhandle(int);
564 void fdt_remove_bycompat(const char *[]);
565 int fdt_find_with_property(const char *, int *);
566
567 int fdtbus_print(void *, const char *);
568
569 bus_dma_tag_t fdtbus_dma_tag_create(int, const struct fdt_dma_range *,
570 u_int);
571 bus_space_tag_t fdtbus_bus_tag_create(int, uint32_t);
572
573
574 #endif /* _DEV_FDT_FDTVAR_H_ */
575