xref: /openbsd-src/sys/dev/fdt/com_fdt.c (revision f1dd7b858388b4a23f4f67a4957ec5ff656ebbe8)
1 /* $OpenBSD: com_fdt.c,v 1.5 2021/04/24 10:33:09 kettenis Exp $ */
2 /*
3  * Copyright (c) 2016 Patrick Wildt <patrick@blueri.se>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 #include <sys/tty.h>
22 
23 #include <machine/intr.h>
24 #include <machine/bus.h>
25 #include <machine/fdt.h>
26 
27 #include <dev/ic/comreg.h>
28 #include <dev/ic/comvar.h>
29 #include <dev/cons.h>
30 
31 #include <dev/ofw/fdt.h>
32 #include <dev/ofw/openfirm.h>
33 #include <dev/ofw/ofw_clock.h>
34 #include <dev/ofw/ofw_pinctrl.h>
35 
36 #define com_usr 31	/* Synopsys DesignWare UART */
37 
38 int	com_fdt_match(struct device *, void *, void *);
39 void	com_fdt_attach(struct device *, struct device *, void *);
40 int	com_fdt_intr_designware(void *);
41 
42 struct cfattach com_fdt_ca = {
43 	sizeof (struct com_softc), com_fdt_match, com_fdt_attach
44 };
45 
46 struct consdev com_fdt_cons = {
47 	NULL, NULL, comcngetc, comcnputc, comcnpollc, NULL,
48 	NODEV, CN_LOWPRI
49 };
50 
51 void
52 com_fdt_init_cons(void)
53 {
54 	struct fdt_reg reg;
55 	uint32_t width, shift;
56 	void *node;
57 
58 	if ((node = fdt_find_cons("brcm,bcm2835-aux-uart")) == NULL &&
59 	    (node = fdt_find_cons("marvell,armada-38x-uart")) == NULL &&
60 	    (node = fdt_find_cons("ns16550a")) == NULL &&
61 	    (node = fdt_find_cons("snps,dw-apb-uart")) == NULL &&
62 	    (node = fdt_find_cons("ti,omap3-uart")) == NULL &&
63 	    (node = fdt_find_cons("ti,omap4-uart")) == NULL)
64 			return;
65 	if (fdt_get_reg(node, 0, &reg))
66 		return;
67 
68 	/*
69 	 * Figuring out the clock frequency is rather complicated as
70 	 * on many SoCs this requires traversing a fair amount of the
71 	 * clock tree.  Instead we rely on the firmware to set up the
72 	 * console for us and bypass the cominit() call that
73 	 * comcnattach() does by doing the minimal setup here.
74 	 */
75 
76 	if (OF_is_compatible(stdout_node, "ns16550a")) {
77 		width = 1;
78 		shift = 0;
79 	} else {
80 		width = 4;
81 		shift = 2;
82 	}
83 
84 	comcons_reg_width = OF_getpropint(stdout_node, "reg-io-width", width);
85 	comcons_reg_shift = OF_getpropint(stdout_node, "reg-shift", shift);
86 
87 	comconsiot = fdt_cons_bs_tag;
88 	if (bus_space_map(comconsiot, reg.addr, reg.size, 0, &comconsioh))
89 		return;
90 
91 	cn_tab = &com_fdt_cons;
92 }
93 
94 int
95 com_fdt_match(struct device *parent, void *match, void *aux)
96 {
97 	struct fdt_attach_args *faa = aux;
98 
99 	return (OF_is_compatible(faa->fa_node, "brcm,bcm2835-aux-uart") ||
100 	    OF_is_compatible(faa->fa_node, "marvell,armada-38x-uart") ||
101 	    OF_is_compatible(faa->fa_node, "ns16550a") ||
102 	    OF_is_compatible(faa->fa_node, "snps,dw-apb-uart") ||
103 	    OF_is_compatible(faa->fa_node, "ti,omap3-uart") ||
104 	    OF_is_compatible(faa->fa_node, "ti,omap4-uart"));
105 }
106 
107 void
108 com_fdt_attach(struct device *parent, struct device *self, void *aux)
109 {
110 	struct com_softc *sc = (struct com_softc *)self;
111 	struct fdt_attach_args *faa = aux;
112 	int (*intr)(void *) = comintr;
113 	uint32_t freq, width, shift;
114 
115 	if (faa->fa_nreg < 1)
116 		return;
117 
118 	clock_enable(faa->fa_node, NULL);
119 	reset_deassert_all(faa->fa_node);
120 
121 	/*
122 	 * Determine the clock frequency after enabling the clock.
123 	 * This gives the clock code a chance to configure the
124 	 * appropriate frequency for us.
125 	 */
126 	freq = OF_getpropint(faa->fa_node, "clock-frequency", 0);
127 	if (freq == 0)
128 		freq = clock_get_frequency(faa->fa_node, NULL);
129 
130 	sc->sc_iot = faa->fa_iot;
131 	sc->sc_iobase = faa->fa_reg[0].addr;
132 	sc->sc_uarttype = COM_UART_16550;
133 	sc->sc_frequency = freq ? freq : COM_FREQ;
134 
135 	if (OF_is_compatible(stdout_node, "ns16550a")) {
136 		width = 1;
137 		shift = 0;
138 	} else {
139 		width = 4;
140 		shift = 2;
141 	}
142 
143 	sc->sc_reg_width = OF_getpropint(faa->fa_node, "reg-io-width", width);
144 	sc->sc_reg_shift = OF_getpropint(faa->fa_node, "reg-shift", shift);
145 
146 	if (OF_is_compatible(faa->fa_node, "snps,dw-apb-uart") ||
147 	    OF_is_compatible(faa->fa_node, "marvell,armada-38x-uart"))
148 		intr = com_fdt_intr_designware;
149 
150 	if (OF_is_compatible(faa->fa_node, "ti,omap3-uart") ||
151 	    OF_is_compatible(faa->fa_node, "ti,omap4-uart"))
152 		sc->sc_uarttype = COM_UART_TI16750;
153 
154 	if (stdout_node == faa->fa_node) {
155 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
156 		SET(sc->sc_swflags, COM_SW_SOFTCAR);
157 		comconsfreq = sc->sc_frequency;
158 		comconsrate = stdout_speed ? stdout_speed : B115200;
159 	}
160 
161 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
162 	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
163 		printf("%s: bus_space_map failed\n", __func__);
164 		return;
165 	}
166 
167 	pinctrl_byname(faa->fa_node, "default");
168 
169 	com_attach_subr(sc);
170 
171 	fdt_intr_establish(faa->fa_node, IPL_TTY, intr,
172 	    sc, sc->sc_dev.dv_xname);
173 }
174 
175 int
176 com_fdt_intr_designware(void *cookie)
177 {
178 	struct com_softc *sc = cookie;
179 
180 	com_read_reg(sc, com_usr);
181 
182 	return comintr(sc);
183 }
184