1 /* $OpenBSD: com_fdt.c,v 1.3 2018/08/06 10:52:30 patrick 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 109 /* 110 * Determine the clock frequency after enabling the clock. 111 * This gives the clock code a chance to configure the 112 * appropriate frequency for us. 113 */ 114 freq = OF_getpropint(faa->fa_node, "clock-frequency", 0); 115 if (freq == 0) 116 freq = clock_get_frequency(faa->fa_node, NULL); 117 118 sc->sc_iot = faa->fa_iot; 119 sc->sc_iobase = faa->fa_reg[0].addr; 120 sc->sc_uarttype = COM_UART_16550; 121 sc->sc_frequency = freq ? freq : COM_FREQ; 122 123 sc->sc_reg_width = OF_getpropint(faa->fa_node, "reg-io-width", 4); 124 sc->sc_reg_shift = OF_getpropint(faa->fa_node, "reg-shift", 2); 125 126 if (OF_is_compatible(faa->fa_node, "snps,dw-apb-uart") || 127 OF_is_compatible(faa->fa_node, "marvell,armada-38x-uart")) 128 intr = com_fdt_intr_designware; 129 130 if (OF_is_compatible(faa->fa_node, "ti,omap3-uart") || 131 OF_is_compatible(faa->fa_node, "ti,omap4-uart")) 132 sc->sc_uarttype = COM_UART_TI16750; 133 134 if (stdout_node == faa->fa_node) { 135 SET(sc->sc_hwflags, COM_HW_CONSOLE); 136 SET(sc->sc_swflags, COM_SW_SOFTCAR); 137 comconsfreq = sc->sc_frequency; 138 comconsrate = stdout_speed ? stdout_speed : B115200; 139 } 140 141 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 142 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 143 printf("%s: bus_space_map failed\n", __func__); 144 return; 145 } 146 147 pinctrl_byname(faa->fa_node, "default"); 148 149 com_attach_subr(sc); 150 151 fdt_intr_establish(faa->fa_node, IPL_TTY, intr, 152 sc, sc->sc_dev.dv_xname); 153 } 154 155 int 156 com_fdt_intr_designware(void *cookie) 157 { 158 struct com_softc *sc = cookie; 159 160 com_read_reg(sc, com_usr); 161 162 return comintr(sc); 163 } 164