xref: /openbsd/sys/dev/fdt/pluart_fdt.c (revision c1f87e31)
1 /*	$OpenBSD: pluart_fdt.c,v 1.8 2022/06/27 13:03:32 anton Exp $	*/
2 /*
3  * Copyright (c) 2014 Patrick Wildt <patrick@blueri.se>
4  * Copyright (c) 2005 Dale Rahn <drahn@dalerahn.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/tty.h>
22 
23 #include <machine/bus.h>
24 #include <machine/fdt.h>
25 
26 #include <dev/ic/pluartvar.h>
27 
28 #include <dev/ofw/fdt.h>
29 #include <dev/ofw/openfirm.h>
30 #include <dev/ofw/ofw_clock.h>
31 #include <dev/ofw/ofw_pinctrl.h>
32 
33 int	pluart_fdt_match(struct device *, void *, void *);
34 void	pluart_fdt_attach(struct device *, struct device *, void *);
35 
36 const struct cfattach pluart_fdt_ca = {
37 	sizeof(struct pluart_softc), pluart_fdt_match, pluart_fdt_attach
38 };
39 
40 void
pluart_init_cons(void)41 pluart_init_cons(void)
42 {
43 	struct fdt_reg reg;
44 	void *node;
45 
46 	if ((node = fdt_find_cons("arm,pl011")) == NULL)
47 		return;
48 	if (fdt_get_reg(node, 0, &reg))
49 		return;
50 
51 	pluartcnattach(fdt_cons_bs_tag, reg.addr, B115200, TTYDEF_CFLAG);
52 }
53 
54 int
pluart_fdt_match(struct device * parent,void * self,void * aux)55 pluart_fdt_match(struct device *parent, void *self, void *aux)
56 {
57 	struct fdt_attach_args *faa = aux;
58 
59 	return OF_is_compatible(faa->fa_node, "arm,pl011");
60 }
61 
62 void
pluart_fdt_attach(struct device * parent,struct device * self,void * aux)63 pluart_fdt_attach(struct device *parent, struct device *self, void *aux)
64 {
65 	struct fdt_attach_args *faa = aux;
66 	struct pluart_softc *sc = (struct pluart_softc *) self;
67 	uint32_t periphid;
68 
69 	if (faa->fa_nreg < 1) {
70 		printf(": no registers\n");
71 		return;
72 	}
73 
74 	if (OF_is_compatible(faa->fa_node, "arm,sbsa-uart")) {
75 		sc->sc_hwflags |= COM_HW_SBSA;
76 	} else {
77 		clock_enable_all(faa->fa_node);
78 		sc->sc_clkfreq = clock_get_frequency(faa->fa_node, "uartclk");
79 	}
80 
81 	periphid = OF_getpropint(faa->fa_node, "arm,primecell-periphid", 0);
82 	if (periphid != 0)
83 		sc->sc_hwrev = (periphid >> 20) & 0x0f;
84 
85 	sc->sc_irq = fdt_intr_establish(faa->fa_node, IPL_TTY, pluart_intr,
86 	    sc, sc->sc_dev.dv_xname);
87 
88 	sc->sc_iot = faa->fa_iot;
89 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, faa->fa_reg[0].size,
90 	    0, &sc->sc_ioh))
91 		panic("pluartattach: bus_space_map failed!");
92 
93 	pinctrl_byname(faa->fa_node, "default");
94 
95 	pluart_attach_common(sc, stdout_node == faa->fa_node);
96 }
97