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