1 /* $OpenBSD: com_fdt.c,v 1.9 2024/01/31 01:01:10 hastings 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 int com_fdt_match(struct device *, void *, void *);
37 void com_fdt_attach(struct device *, struct device *, void *);
38 int com_fdt_intr_designware(void *);
39
40 const struct cfattach com_fdt_ca = {
41 sizeof (struct com_softc), com_fdt_match, com_fdt_attach
42 };
43
44 struct consdev com_fdt_cons = {
45 NULL, NULL, comcngetc, comcnputc, comcnpollc, NULL,
46 NODEV, CN_LOWPRI
47 };
48
49 void
com_fdt_init_cons(void)50 com_fdt_init_cons(void)
51 {
52 struct fdt_reg reg;
53 uint32_t width, shift;
54 void *node;
55
56 if ((node = fdt_find_cons("brcm,bcm2835-aux-uart")) == NULL &&
57 (node = fdt_find_cons("marvell,armada-38x-uart")) == NULL &&
58 (node = fdt_find_cons("mediatek,mt6577-uart")) == NULL &&
59 (node = fdt_find_cons("ns16550a")) == NULL &&
60 (node = fdt_find_cons("snps,dw-apb-uart")) == NULL &&
61 (node = fdt_find_cons("ti,omap3-uart")) == NULL &&
62 (node = fdt_find_cons("ti,omap4-uart")) == NULL)
63 return;
64 if (fdt_get_reg(node, 0, ®))
65 return;
66
67 /*
68 * Figuring out the clock frequency is rather complicated as
69 * on many SoCs this requires traversing a fair amount of the
70 * clock tree. Instead we rely on the firmware to set up the
71 * console for us and bypass the cominit() call that
72 * comcnattach() does by doing the minimal setup here.
73 */
74
75 if (OF_is_compatible(stdout_node, "ns16550a")) {
76 width = 1;
77 shift = 0;
78 } else {
79 width = 4;
80 shift = 2;
81 }
82
83 comcons_reg_width = OF_getpropint(stdout_node, "reg-io-width", width);
84 comcons_reg_shift = OF_getpropint(stdout_node, "reg-shift", shift);
85
86 comconsiot = fdt_cons_bs_tag;
87 if (bus_space_map(comconsiot, reg.addr, reg.size, 0, &comconsioh))
88 return;
89
90 cn_tab = &com_fdt_cons;
91 }
92
93 int
com_fdt_match(struct device * parent,void * match,void * aux)94 com_fdt_match(struct device *parent, void *match, void *aux)
95 {
96 struct fdt_attach_args *faa = aux;
97
98 return (OF_is_compatible(faa->fa_node, "brcm,bcm2835-aux-uart") ||
99 OF_is_compatible(faa->fa_node, "marvell,armada-38x-uart") ||
100 OF_is_compatible(faa->fa_node, "mediatek,mt6577-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
com_fdt_attach(struct device * parent,struct device * self,void * aux)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(faa->fa_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, "mediatek,mt6577-uart"))
147 sc->sc_uarttype = COM_UART_16550A;
148
149 if (OF_is_compatible(faa->fa_node, "snps,dw-apb-uart") ||
150 OF_is_compatible(faa->fa_node, "marvell,armada-38x-uart")) {
151 sc->sc_uarttype = COM_UART_DW_APB;
152 intr = com_fdt_intr_designware;
153 }
154
155 if (OF_is_compatible(faa->fa_node, "ti,omap3-uart") ||
156 OF_is_compatible(faa->fa_node, "ti,omap4-uart"))
157 sc->sc_uarttype = COM_UART_TI16750;
158
159 if (stdout_node == faa->fa_node) {
160 SET(sc->sc_hwflags, COM_HW_CONSOLE);
161 SET(sc->sc_swflags, COM_SW_SOFTCAR);
162 comconsfreq = sc->sc_frequency;
163 comconsrate = stdout_speed ? stdout_speed : B115200;
164 }
165
166 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
167 faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
168 printf("%s: bus_space_map failed\n", __func__);
169 return;
170 }
171
172 pinctrl_byname(faa->fa_node, "default");
173
174 com_attach_subr(sc);
175
176 fdt_intr_establish(faa->fa_node, IPL_TTY, intr,
177 sc, sc->sc_dev.dv_xname);
178 }
179
180 int
com_fdt_intr_designware(void * cookie)181 com_fdt_intr_designware(void *cookie)
182 {
183 struct com_softc *sc = cookie;
184
185 com_read_reg(sc, com_usr);
186
187 return comintr(sc);
188 }
189