1 /* $OpenBSD: com_fdt.c,v 1.4 2020/03/23 21:40:01 uaa 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 void *node; 56 57 if ((node = fdt_find_cons("brcm,bcm2835-aux-uart")) == NULL && 58 (node = fdt_find_cons("snps,dw-apb-uart")) == NULL && 59 (node = fdt_find_cons("marvell,armada-38x-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 comcons_reg_width = OF_getpropint(stdout_node, "reg-io-width", 4); 75 comcons_reg_shift = OF_getpropint(stdout_node, "reg-shift", 2); 76 77 comconsiot = fdt_cons_bs_tag; 78 if (bus_space_map(comconsiot, reg.addr, reg.size, 0, &comconsioh)) 79 return; 80 81 cn_tab = &com_fdt_cons; 82 } 83 84 int 85 com_fdt_match(struct device *parent, void *match, void *aux) 86 { 87 struct fdt_attach_args *faa = aux; 88 89 return (OF_is_compatible(faa->fa_node, "brcm,bcm2835-aux-uart") || 90 OF_is_compatible(faa->fa_node, "snps,dw-apb-uart") || 91 OF_is_compatible(faa->fa_node, "marvell,armada-38x-uart") || 92 OF_is_compatible(faa->fa_node, "ti,omap3-uart") || 93 OF_is_compatible(faa->fa_node, "ti,omap4-uart")); 94 } 95 96 void 97 com_fdt_attach(struct device *parent, struct device *self, void *aux) 98 { 99 struct com_softc *sc = (struct com_softc *)self; 100 struct fdt_attach_args *faa = aux; 101 int (*intr)(void *) = comintr; 102 uint32_t freq; 103 104 if (faa->fa_nreg < 1) 105 return; 106 107 clock_enable(faa->fa_node, NULL); 108 reset_deassert_all(faa->fa_node); 109 110 /* 111 * Determine the clock frequency after enabling the clock. 112 * This gives the clock code a chance to configure the 113 * appropriate frequency for us. 114 */ 115 freq = OF_getpropint(faa->fa_node, "clock-frequency", 0); 116 if (freq == 0) 117 freq = clock_get_frequency(faa->fa_node, NULL); 118 119 sc->sc_iot = faa->fa_iot; 120 sc->sc_iobase = faa->fa_reg[0].addr; 121 sc->sc_uarttype = COM_UART_16550; 122 sc->sc_frequency = freq ? freq : COM_FREQ; 123 124 sc->sc_reg_width = OF_getpropint(faa->fa_node, "reg-io-width", 4); 125 sc->sc_reg_shift = OF_getpropint(faa->fa_node, "reg-shift", 2); 126 127 if (OF_is_compatible(faa->fa_node, "snps,dw-apb-uart") || 128 OF_is_compatible(faa->fa_node, "marvell,armada-38x-uart")) 129 intr = com_fdt_intr_designware; 130 131 if (OF_is_compatible(faa->fa_node, "ti,omap3-uart") || 132 OF_is_compatible(faa->fa_node, "ti,omap4-uart")) 133 sc->sc_uarttype = COM_UART_TI16750; 134 135 if (stdout_node == faa->fa_node) { 136 SET(sc->sc_hwflags, COM_HW_CONSOLE); 137 SET(sc->sc_swflags, COM_SW_SOFTCAR); 138 comconsfreq = sc->sc_frequency; 139 comconsrate = stdout_speed ? stdout_speed : B115200; 140 } 141 142 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 143 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 144 printf("%s: bus_space_map failed\n", __func__); 145 return; 146 } 147 148 pinctrl_byname(faa->fa_node, "default"); 149 150 com_attach_subr(sc); 151 152 fdt_intr_establish(faa->fa_node, IPL_TTY, intr, 153 sc, sc->sc_dev.dv_xname); 154 } 155 156 int 157 com_fdt_intr_designware(void *cookie) 158 { 159 struct com_softc *sc = cookie; 160 161 com_read_reg(sc, com_usr); 162 163 return comintr(sc); 164 } 165