1 /*- 2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bus.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <machine/bus.h> 35 36 #include <dev/uart/uart.h> 37 #include <dev/uart/uart_bus.h> 38 #include <dev/uart/uart_cpu_fdt.h> 39 #include <dev/uart/uart_dev_ns8250.h> 40 41 #include <dev/ofw/ofw_bus.h> 42 #include <dev/ofw/ofw_bus_subr.h> 43 44 #include <dev/extres/clk/clk.h> 45 #include <dev/extres/hwreset/hwreset.h> 46 47 #include "uart_if.h" 48 49 struct snps_softc { 50 struct ns8250_softc ns8250; 51 52 clk_t baudclk; 53 clk_t apb_pclk; 54 hwreset_t reset; 55 }; 56 57 /* 58 * To use early printf on 64 bits Allwinner SoC, add to kernel config 59 * options SOCDEV_PA=0x0 60 * options SOCDEV_VA=0x40000000 61 * options EARLY_PRINTF 62 * 63 * To use early printf on 32 bits Allwinner SoC, add to kernel config 64 * options SOCDEV_PA=0x01C00000 65 * options SOCDEV_VA=0x10000000 66 * options EARLY_PRINTF 67 * 68 * remove the if 0 69 */ 70 #if 0 71 #ifdef EARLY_PRINTF 72 static void 73 uart_snps_early_putc(int c) 74 { 75 volatile uint32_t *stat; 76 volatile uint32_t *tx; 77 78 #ifdef ALLWINNER_64 79 stat = (uint32_t *) (SOCDEV_VA + 0x1C2807C); 80 tx = (uint32_t *) (SOCDEV_VA + 0x1C28000); 81 #endif 82 #ifdef ALLWINNER_32 83 stat = (uint32_t *) (SOCDEV_VA + 0x2807C); 84 tx = (uint32_t *) (SOCDEV_VA + 0x28000); 85 #endif 86 87 while ((*stat & (1 << 2)) == 0) 88 continue; 89 *tx = c; 90 } 91 early_putc_t *early_putc = uart_snps_early_putc; 92 #endif /* EARLY_PRINTF */ 93 #endif 94 95 static kobj_method_t snps_methods[] = { 96 KOBJMETHOD(uart_probe, ns8250_bus_probe), 97 KOBJMETHOD(uart_attach, ns8250_bus_attach), 98 KOBJMETHOD(uart_detach, ns8250_bus_detach), 99 KOBJMETHOD(uart_flush, ns8250_bus_flush), 100 KOBJMETHOD(uart_getsig, ns8250_bus_getsig), 101 KOBJMETHOD(uart_ioctl, ns8250_bus_ioctl), 102 KOBJMETHOD(uart_ipend, ns8250_bus_ipend), 103 KOBJMETHOD(uart_param, ns8250_bus_param), 104 KOBJMETHOD(uart_receive, ns8250_bus_receive), 105 KOBJMETHOD(uart_setsig, ns8250_bus_setsig), 106 KOBJMETHOD(uart_transmit, ns8250_bus_transmit), 107 KOBJMETHOD(uart_grab, ns8250_bus_grab), 108 KOBJMETHOD(uart_ungrab, ns8250_bus_ungrab), 109 KOBJMETHOD_END 110 }; 111 112 struct uart_class uart_snps_class = { 113 "snps", 114 snps_methods, 115 sizeof(struct snps_softc), 116 .uc_ops = &uart_ns8250_ops, 117 .uc_range = 8, 118 .uc_rclk = 0, 119 }; 120 121 static struct ofw_compat_data compat_data[] = { 122 { "snps,dw-apb-uart", (uintptr_t)&uart_snps_class }, 123 { "marvell,armada-38x-uart", (uintptr_t)&uart_snps_class }, 124 { NULL, (uintptr_t)NULL } 125 }; 126 UART_FDT_CLASS(compat_data); 127 128 static int 129 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk) 130 { 131 132 *baudclk = NULL; 133 *apb_pclk = NULL; 134 135 /* Baud clock is either named "baudclk", or there is a single 136 * unnamed clock. 137 */ 138 if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 && 139 clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0) 140 return (ENOENT); 141 142 /* APB peripheral clock is optional */ 143 (void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk); 144 145 return (0); 146 } 147 148 static int 149 snps_probe(device_t dev) 150 { 151 struct snps_softc *sc; 152 struct uart_class *uart_class; 153 phandle_t node; 154 uint32_t shift, iowidth, clock; 155 uint64_t freq; 156 int error; 157 clk_t baudclk, apb_pclk; 158 hwreset_t reset; 159 160 if (!ofw_bus_status_okay(dev)) 161 return (ENXIO); 162 163 uart_class = (struct uart_class *)ofw_bus_search_compatible(dev, 164 compat_data)->ocd_data; 165 if (uart_class == NULL) 166 return (ENXIO); 167 168 freq = 0; 169 sc = device_get_softc(dev); 170 sc->ns8250.base.sc_class = uart_class; 171 172 node = ofw_bus_get_node(dev); 173 if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0) 174 shift = 0; 175 if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0) 176 iowidth = 1; 177 if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0) 178 clock = 0; 179 180 if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) { 181 error = hwreset_deassert(reset); 182 if (error != 0) { 183 device_printf(dev, "cannot de-assert reset\n"); 184 return (error); 185 } 186 } 187 188 if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) { 189 error = clk_enable(baudclk); 190 if (error != 0) { 191 device_printf(dev, "cannot enable baud clock\n"); 192 return (error); 193 } 194 if (apb_pclk != NULL) { 195 error = clk_enable(apb_pclk); 196 if (error != 0) { 197 device_printf(dev, 198 "cannot enable peripheral clock\n"); 199 return (error); 200 } 201 } 202 203 if (clock == 0) { 204 error = clk_get_freq(baudclk, &freq); 205 if (error != 0) { 206 device_printf(dev, "cannot get frequency\n"); 207 return (error); 208 } 209 clock = (uint32_t)freq; 210 } 211 } 212 213 if (bootverbose && clock == 0) 214 device_printf(dev, "could not determine frequency\n"); 215 216 error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, UART_F_BUSY_DETECT); 217 if (error > 0) 218 return (error); 219 220 /* XXX uart_bus_probe has changed the softc, so refresh it */ 221 sc = device_get_softc(dev); 222 223 /* Store clock and reset handles for detach */ 224 sc->baudclk = baudclk; 225 sc->apb_pclk = apb_pclk; 226 sc->reset = reset; 227 228 return (BUS_PROBE_VENDOR); 229 } 230 231 static int 232 snps_detach(device_t dev) 233 { 234 struct snps_softc *sc; 235 clk_t baudclk, apb_pclk; 236 hwreset_t reset; 237 int error; 238 239 sc = device_get_softc(dev); 240 baudclk = sc->baudclk; 241 apb_pclk = sc->apb_pclk; 242 reset = sc->reset; 243 244 error = uart_bus_detach(dev); 245 if (error != 0) 246 return (error); 247 248 if (reset != NULL) { 249 error = hwreset_assert(reset); 250 if (error != 0) { 251 device_printf(dev, "cannot assert reset\n"); 252 return (error); 253 } 254 hwreset_release(reset); 255 } 256 if (apb_pclk != NULL) { 257 error = clk_release(apb_pclk); 258 if (error != 0) { 259 device_printf(dev, "cannot release peripheral clock\n"); 260 return (error); 261 } 262 } 263 if (baudclk != NULL) { 264 error = clk_release(baudclk); 265 if (error != 0) { 266 device_printf(dev, "cannot release baud clock\n"); 267 return (error); 268 } 269 } 270 271 return (0); 272 } 273 274 static device_method_t snps_bus_methods[] = { 275 /* Device interface */ 276 DEVMETHOD(device_probe, snps_probe), 277 DEVMETHOD(device_attach, uart_bus_attach), 278 DEVMETHOD(device_detach, snps_detach), 279 DEVMETHOD_END 280 }; 281 282 static driver_t snps_uart_driver = { 283 uart_driver_name, 284 snps_bus_methods, 285 sizeof(struct snps_softc) 286 }; 287 288 DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, 0, 0); 289