1 /* $OpenBSD: com_fdt.c,v 1.8 2023/08/15 07:56:27 miod 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 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("ns16550a")) == NULL && 59 (node = fdt_find_cons("snps,dw-apb-uart")) == NULL && 60 (node = fdt_find_cons("ti,omap3-uart")) == NULL && 61 (node = fdt_find_cons("ti,omap4-uart")) == NULL) 62 return; 63 if (fdt_get_reg(node, 0, ®)) 64 return; 65 66 /* 67 * Figuring out the clock frequency is rather complicated as 68 * on many SoCs this requires traversing a fair amount of the 69 * clock tree. Instead we rely on the firmware to set up the 70 * console for us and bypass the cominit() call that 71 * comcnattach() does by doing the minimal setup here. 72 */ 73 74 if (OF_is_compatible(stdout_node, "ns16550a")) { 75 width = 1; 76 shift = 0; 77 } else { 78 width = 4; 79 shift = 2; 80 } 81 82 comcons_reg_width = OF_getpropint(stdout_node, "reg-io-width", width); 83 comcons_reg_shift = OF_getpropint(stdout_node, "reg-shift", shift); 84 85 comconsiot = fdt_cons_bs_tag; 86 if (bus_space_map(comconsiot, reg.addr, reg.size, 0, &comconsioh)) 87 return; 88 89 cn_tab = &com_fdt_cons; 90 } 91 92 int 93 com_fdt_match(struct device *parent, void *match, void *aux) 94 { 95 struct fdt_attach_args *faa = aux; 96 97 return (OF_is_compatible(faa->fa_node, "brcm,bcm2835-aux-uart") || 98 OF_is_compatible(faa->fa_node, "marvell,armada-38x-uart") || 99 OF_is_compatible(faa->fa_node, "ns16550a") || 100 OF_is_compatible(faa->fa_node, "snps,dw-apb-uart") || 101 OF_is_compatible(faa->fa_node, "ti,omap3-uart") || 102 OF_is_compatible(faa->fa_node, "ti,omap4-uart")); 103 } 104 105 void 106 com_fdt_attach(struct device *parent, struct device *self, void *aux) 107 { 108 struct com_softc *sc = (struct com_softc *)self; 109 struct fdt_attach_args *faa = aux; 110 int (*intr)(void *) = comintr; 111 uint32_t freq, width, shift; 112 113 if (faa->fa_nreg < 1) 114 return; 115 116 clock_enable(faa->fa_node, NULL); 117 reset_deassert_all(faa->fa_node); 118 119 /* 120 * Determine the clock frequency after enabling the clock. 121 * This gives the clock code a chance to configure the 122 * appropriate frequency for us. 123 */ 124 freq = OF_getpropint(faa->fa_node, "clock-frequency", 0); 125 if (freq == 0) 126 freq = clock_get_frequency(faa->fa_node, NULL); 127 128 sc->sc_iot = faa->fa_iot; 129 sc->sc_iobase = faa->fa_reg[0].addr; 130 sc->sc_uarttype = COM_UART_16550; 131 sc->sc_frequency = freq ? freq : COM_FREQ; 132 133 if (OF_is_compatible(faa->fa_node, "ns16550a")) { 134 width = 1; 135 shift = 0; 136 } else { 137 width = 4; 138 shift = 2; 139 } 140 141 sc->sc_reg_width = OF_getpropint(faa->fa_node, "reg-io-width", width); 142 sc->sc_reg_shift = OF_getpropint(faa->fa_node, "reg-shift", shift); 143 144 if (OF_is_compatible(faa->fa_node, "snps,dw-apb-uart") || 145 OF_is_compatible(faa->fa_node, "marvell,armada-38x-uart")) { 146 sc->sc_uarttype = COM_UART_DW_APB; 147 intr = com_fdt_intr_designware; 148 } 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