xref: /netbsd-src/sys/dev/fdt/fdtvar.h (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /* $NetBSD: fdtvar.h,v 1.60 2020/02/20 01:35:55 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2015 Jared D. McNeill <jmcneill@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 	void	(*mask)(device_t, void *);
80 	void	(*unmask)(device_t, void *);
81 };
82 
83 struct fdtbus_i2c_controller_func {
84 	i2c_tag_t (*get_tag)(device_t);
85 };
86 
87 struct fdtbus_spi_controller_func {
88 	struct spi_controller *	(*get_controller)(device_t);
89 };
90 
91 struct fdtbus_gpio_controller;
92 
93 struct fdtbus_gpio_pin {
94 	struct fdtbus_gpio_controller *gp_gc;
95 	void *gp_priv;
96 };
97 
98 struct fdtbus_gpio_controller_func {
99 	void *	(*acquire)(device_t, const void *, size_t, int);
100 	void	(*release)(device_t, void *);
101 	int	(*read)(device_t, void *, bool);
102 	void	(*write)(device_t, void *, int, bool);
103 };
104 
105 struct fdtbus_pinctrl_controller;
106 
107 struct fdtbus_pinctrl_pin {
108 	struct fdtbus_pinctrl_controller *pp_pc;
109 	void *pp_priv;
110 };
111 
112 struct fdtbus_pinctrl_controller_func {
113 	int (*set_config)(device_t, const void *, size_t);
114 };
115 
116 struct fdtbus_regulator_controller;
117 
118 struct fdtbus_regulator {
119 	struct fdtbus_regulator_controller *reg_rc;
120 };
121 
122 struct fdtbus_regulator_controller_func {
123 	int	(*acquire)(device_t);
124 	void	(*release)(device_t);
125 	int	(*enable)(device_t, bool);
126 	int	(*set_voltage)(device_t, u_int, u_int);
127 	int	(*get_voltage)(device_t, u_int *);
128 };
129 
130 struct fdtbus_clock_controller_func {
131 	struct clk *	(*decode)(device_t, int, const void *, size_t);
132 };
133 
134 struct fdtbus_reset_controller;
135 
136 struct fdtbus_reset {
137 	struct fdtbus_reset_controller *rst_rc;
138 	void *rst_priv;
139 };
140 
141 struct fdtbus_reset_controller_func {
142 	void *	(*acquire)(device_t, const void *, size_t);
143 	void	(*release)(device_t, void *);
144 	int	(*reset_assert)(device_t, void *);
145 	int	(*reset_deassert)(device_t, void *);
146 };
147 
148 struct fdtbus_dai_controller_func {
149 	audio_dai_tag_t (*get_tag)(device_t, const void *, size_t);
150 };
151 
152 struct fdtbus_dma_controller;
153 
154 struct fdtbus_dma {
155 	struct fdtbus_dma_controller *dma_dc;
156 	void *dma_priv;
157 };
158 
159 enum fdtbus_dma_dir {
160 	FDT_DMA_READ,		/* device -> memory */
161 	FDT_DMA_WRITE		/* memory -> device */
162 };
163 
164 struct fdtbus_dma_opt {
165 	int opt_bus_width;		/* Bus width */
166 	int opt_burst_len;		/* Burst length */
167 	int opt_swap;			/* Enable data swapping */
168 	int opt_dblbuf;			/* Enable double buffering */
169 	int opt_wrap_len;		/* Address wrap-around window */
170 };
171 
172 struct fdtbus_dma_req {
173 	bus_dma_segment_t *dreq_segs;	/* Memory */
174 	int dreq_nsegs;
175 
176 	bus_addr_t dreq_dev_phys;	/* Device */
177 	int dreq_sel;			/* Device selector */
178 
179 	enum fdtbus_dma_dir dreq_dir;	/* Transfer direction */
180 
181 	int dreq_block_irq;		/* Enable IRQ at end of block */
182 	int dreq_block_multi;		/* Enable multiple block transfers */
183 	int dreq_flow;			/* Enable flow control */
184 
185 	struct fdtbus_dma_opt dreq_mem_opt;	/* Memory options */
186 	struct fdtbus_dma_opt dreq_dev_opt;	/* Device options */
187 };
188 
189 struct fdtbus_dma_controller_func {
190 	void *	(*acquire)(device_t, const void *, size_t,
191 			   void (*)(void *), void *);
192 	void	(*release)(device_t, void *);
193 	int	(*transfer)(device_t, void *, struct fdtbus_dma_req *);
194 	void	(*halt)(device_t, void *);
195 };
196 
197 struct fdtbus_power_controller;
198 
199 struct fdtbus_power_controller_func {
200 	void 	(*reset)(device_t);
201 	void	(*poweroff)(device_t);
202 };
203 
204 struct fdtbus_phy_controller;
205 
206 struct fdtbus_phy {
207 	struct fdtbus_phy_controller *phy_pc;
208 	void *phy_priv;
209 };
210 
211 struct fdtbus_phy_controller_func {
212 	void *	(*acquire)(device_t, const void *, size_t);
213 	void	(*release)(device_t, void *);
214 	int	(*enable)(device_t, void *, bool);
215 };
216 
217 struct fdtbus_pwm_controller_func {
218 	pwm_tag_t (*get_tag)(device_t, const void *, size_t);
219 };
220 
221 struct fdtbus_mmc_pwrseq;
222 
223 struct fdtbus_mmc_pwrseq_func {
224 	void	(*pre_power_on)(device_t);
225 	void	(*post_power_on)(device_t);
226 	void	(*power_off)(device_t);
227 	void	(*reset)(device_t);
228 };
229 
230 struct syscon;
231 
232 struct fdt_console {
233 	int	(*match)(int);
234 	void	(*consinit)(struct fdt_attach_args *, u_int);
235 };
236 
237 struct fdt_console_info {
238 	const struct fdt_console *ops;
239 };
240 
241 struct fdt_phandle_data {
242 	int phandle;
243 	int count;
244 	const u_int *values;
245 };
246 
247 #define	_FDT_CONSOLE_REGISTER(name)	\
248 	__link_set_add_rodata(fdt_consoles, __CONCAT(name,_consinfo));
249 
250 #define	FDT_CONSOLE(_name, _ops)					\
251 static const struct fdt_console_info __CONCAT(_name,_consinfo) = {	\
252 	.ops = (_ops)							\
253 };									\
254 _FDT_CONSOLE_REGISTER(_name)
255 
256 struct fdt_opp_info {
257 	const char *	opp_compat;
258 	bool		(*opp_supported)(const int, const int);
259 };
260 
261 #define	_FDT_OPP_REGISTER(name)		\
262 	__link_set_add_rodata(fdt_opps, __CONCAT(name,_oppinfo));
263 
264 #define	FDT_OPP(_name, _compat, _suppfn)				\
265 static const struct fdt_opp_info __CONCAT(_name,_oppinfo) = {		\
266 	.opp_compat = (_compat),					\
267 	.opp_supported = (_suppfn)					\
268 };									\
269 _FDT_OPP_REGISTER(_name)
270 
271 TAILQ_HEAD(fdt_conslist, fdt_console_info);
272 
273 struct fdt_dma_range {
274 	paddr_t		dr_sysbase;
275 	bus_addr_t	dr_busbase;
276 	bus_size_t	dr_len;
277 };
278 
279 int		fdtbus_register_interrupt_controller(device_t, int,
280 		    const struct fdtbus_interrupt_controller_func *);
281 int		fdtbus_register_i2c_controller(device_t, int,
282 		    const struct fdtbus_i2c_controller_func *);
283 int		fdtbus_register_spi_controller(device_t, int,
284 		    const struct fdtbus_spi_controller_func *);
285 int		fdtbus_register_gpio_controller(device_t, int,
286 		    const struct fdtbus_gpio_controller_func *);
287 int		fdtbus_register_pinctrl_config(device_t, int,
288 		    const struct fdtbus_pinctrl_controller_func *);
289 int		fdtbus_register_regulator_controller(device_t, int,
290 		    const struct fdtbus_regulator_controller_func *);
291 int		fdtbus_register_clock_controller(device_t, int,
292 		    const struct fdtbus_clock_controller_func *);
293 int		fdtbus_register_reset_controller(device_t, int,
294 		    const struct fdtbus_reset_controller_func *);
295 int		fdtbus_register_dai_controller(device_t, int,
296 		    const struct fdtbus_dai_controller_func *);
297 int		fdtbus_register_dma_controller(device_t, int,
298 		    const struct fdtbus_dma_controller_func *);
299 int		fdtbus_register_power_controller(device_t, int,
300 		    const struct fdtbus_power_controller_func *);
301 int		fdtbus_register_phy_controller(device_t, int,
302 		    const struct fdtbus_phy_controller_func *);
303 int		fdtbus_register_pwm_controller(device_t, int,
304 		    const struct fdtbus_pwm_controller_func *);
305 int		fdtbus_register_mmc_pwrseq(device_t, int,
306 		    const struct fdtbus_mmc_pwrseq_func *);
307 int		fdtbus_register_syscon(device_t, int, struct syscon *);
308 
309 void		fdtbus_set_decoderegprop(bool);
310 
311 int		fdtbus_get_reg(int, u_int, bus_addr_t *, bus_size_t *);
312 int		fdtbus_get_reg_byname(int, const char *, bus_addr_t *,
313 		    bus_size_t *);
314 int		fdtbus_get_reg64(int, u_int, uint64_t *, uint64_t *);
315 int		fdtbus_get_addr_cells(int);
316 int		fdtbus_get_size_cells(int);
317 uint64_t	fdtbus_get_cells(const uint8_t *, int);
318 int		fdtbus_get_phandle(int, const char *);
319 int		fdtbus_get_phandle_with_data(int, const char *, const char *,
320 		    int, struct fdt_phandle_data *);
321 int		fdtbus_get_phandle_from_native(int);
322 i2c_tag_t	fdtbus_get_i2c_tag(int);
323 i2c_tag_t	fdtbus_i2c_acquire(int, const char *);
324 void *		fdtbus_intr_establish(int, u_int, int, int,
325 		    int (*func)(void *), void *arg);
326 void *		fdtbus_intr_establish_byname(int, const char *, int, int,
327 		    int (*func)(void *), void *arg);
328 void *		fdtbus_intr_establish_raw(int, const u_int *, int, int,
329 		    int (*func)(void *), void *arg);
330 void		fdtbus_intr_mask(int, void *);
331 void		fdtbus_intr_unmask(int, void *);
332 void		fdtbus_intr_disestablish(int, void *);
333 bool		fdtbus_intr_str(int, u_int, char *, size_t);
334 bool		fdtbus_intr_str_raw(int, const u_int *, char *, size_t);
335 struct fdtbus_gpio_pin *fdtbus_gpio_acquire(int, const char *, int);
336 struct fdtbus_gpio_pin *fdtbus_gpio_acquire_index(int, const char *, int, int);
337 void		fdtbus_gpio_release(struct fdtbus_gpio_pin *);
338 int		fdtbus_gpio_read(struct fdtbus_gpio_pin *);
339 void		fdtbus_gpio_write(struct fdtbus_gpio_pin *, int);
340 int		fdtbus_gpio_read_raw(struct fdtbus_gpio_pin *);
341 void		fdtbus_gpio_write_raw(struct fdtbus_gpio_pin *, int);
342 audio_dai_tag_t	fdtbus_dai_acquire(int, const char *);
343 audio_dai_tag_t	fdtbus_dai_acquire_index(int, const char *, int);
344 pwm_tag_t	fdtbus_pwm_acquire(int, const char *);
345 pwm_tag_t	fdtbus_pwm_acquire_index(int, const char *, int);
346 int		fdtbus_pinctrl_set_config_index(int, u_int);
347 int		fdtbus_pinctrl_set_config(int, const char *);
348 bool		fdtbus_pinctrl_has_config(int, const char *);
349 const char *	fdtbus_pinctrl_parse_function(int);
350 const void *	fdtbus_pinctrl_parse_pins(int, int *);
351 const char *	fdtbus_pinctrl_parse_groups(int, int *);
352 const u_int *	fdtbus_pinctrl_parse_pinmux(int, int *);
353 int		fdtbus_pinctrl_parse_bias(int, int *);
354 int		fdtbus_pinctrl_parse_drive(int);
355 int		fdtbus_pinctrl_parse_drive_strength(int);
356 int		fdtbus_pinctrl_parse_input_output(int, int *);
357 struct fdtbus_regulator *fdtbus_regulator_acquire(int, const char *);
358 void		fdtbus_regulator_release(struct fdtbus_regulator *);
359 int		fdtbus_regulator_enable(struct fdtbus_regulator *);
360 int		fdtbus_regulator_disable(struct fdtbus_regulator *);
361 int		fdtbus_regulator_set_voltage(struct fdtbus_regulator *,
362 		    u_int, u_int);
363 int		fdtbus_regulator_get_voltage(struct fdtbus_regulator *,
364 		    u_int *);
365 int		fdtbus_regulator_supports_voltage(struct fdtbus_regulator *,
366 		    u_int, u_int);
367 struct syscon *	fdtbus_syscon_acquire(int, const char *);
368 struct syscon *	fdtbus_syscon_lookup(int);
369 
370 struct fdtbus_dma *fdtbus_dma_get(int, const char *, void (*)(void *), void *);
371 struct fdtbus_dma *fdtbus_dma_get_index(int, u_int, void (*)(void *),
372 		    void *);
373 void		fdtbus_dma_put(struct fdtbus_dma *);
374 int		fdtbus_dma_transfer(struct fdtbus_dma *,
375 		    struct fdtbus_dma_req *);
376 void		fdtbus_dma_halt(struct fdtbus_dma *);
377 
378 struct clk *	fdtbus_clock_get(int, const char *);
379 struct clk *	fdtbus_clock_get_index(int, u_int);
380 struct clk *	fdtbus_clock_byname(const char *);
381 void		fdtbus_clock_assign(int);
382 u_int		fdtbus_clock_count(int, const char *);
383 int		fdtbus_clock_enable(int, const char *, bool);
384 int		fdtbus_clock_enable_index(int, u_int, bool);
385 
386 struct fdtbus_reset *fdtbus_reset_get(int, const char *);
387 struct fdtbus_reset *fdtbus_reset_get_index(int, u_int);
388 void		fdtbus_reset_put(struct fdtbus_reset *);
389 int		fdtbus_reset_assert(struct fdtbus_reset *);
390 int		fdtbus_reset_deassert(struct fdtbus_reset *);
391 
392 struct fdtbus_phy *fdtbus_phy_get(int, const char *);
393 struct fdtbus_phy *fdtbus_phy_get_index(int, u_int);
394 void		fdtbus_phy_put(struct fdtbus_phy *);
395 device_t	fdtbus_phy_device(struct fdtbus_phy *);
396 int		fdtbus_phy_enable(struct fdtbus_phy *, bool);
397 
398 struct fdtbus_mmc_pwrseq *fdtbus_mmc_pwrseq_get(int);
399 void		fdtbus_mmc_pwrseq_pre_power_on(struct fdtbus_mmc_pwrseq *);
400 void		fdtbus_mmc_pwrseq_post_power_on(struct fdtbus_mmc_pwrseq *);
401 void		fdtbus_mmc_pwrseq_power_off(struct fdtbus_mmc_pwrseq *);
402 void		fdtbus_mmc_pwrseq_reset(struct fdtbus_mmc_pwrseq *);
403 
404 int		fdtbus_todr_attach(device_t, int, todr_chip_handle_t);
405 
406 void		fdtbus_power_reset(void);
407 void		fdtbus_power_poweroff(void);
408 
409 device_t	fdtbus_attach_i2cbus(device_t, int, i2c_tag_t, cfprint_t);
410 device_t	fdtbus_attach_spibus(device_t, int, cfprint_t);
411 
412 bool		fdtbus_init(const void *);
413 const void *	fdtbus_get_data(void);
414 int		fdtbus_phandle2offset(int);
415 int		fdtbus_offset2phandle(int);
416 bool		fdtbus_get_path(int, char *, size_t);
417 
418 const struct fdt_console *fdtbus_get_console(void);
419 
420 const char *	fdtbus_get_stdout_path(void);
421 int		fdtbus_get_stdout_phandle(void);
422 int		fdtbus_get_stdout_speed(void);
423 tcflag_t	fdtbus_get_stdout_flags(void);
424 
425 bool		fdtbus_status_okay(int);
426 
427 const void *	fdtbus_get_prop(int, const char *, int *);
428 const char *	fdtbus_get_string(int, const char *);
429 const char *	fdtbus_get_string_index(int, const char *, u_int);
430 int		fdtbus_get_index(int, const char *, const char *, u_int *);
431 
432 void		fdt_add_bus(device_t, int, struct fdt_attach_args *);
433 void		fdt_add_bus_match(device_t, int, struct fdt_attach_args *,
434 				  bool (*)(void *, int), void *);
435 void		fdt_add_child(device_t, int, struct fdt_attach_args *, u_int);
436 
437 void		fdt_remove_byhandle(int);
438 void		fdt_remove_bycompat(const char *[]);
439 int		fdt_find_with_property(const char *, int *);
440 int		fdtbus_print(void *, const char *);
441 
442 bus_dma_tag_t	fdtbus_dma_tag_create(int, const struct fdt_dma_range *,
443 		    u_int);
444 
445 #endif /* _DEV_FDT_FDTVAR_H */
446