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 static int 65 snps_uart_attach(struct uart_softc *uart_sc) 66 { 67 struct snps_softc *sc; 68 69 sc = (struct snps_softc *)uart_sc; 70 71 /* UART requires to read USR reg when IIR_BUSY */ 72 sc->ns8250.busy_detect = 1; 73 74 return (ns8250_bus_attach(uart_sc)); 75 } 76 77 static kobj_method_t snps_methods[] = { 78 KOBJMETHOD(uart_probe, ns8250_bus_probe), 79 KOBJMETHOD(uart_attach, snps_uart_attach), 80 KOBJMETHOD(uart_detach, ns8250_bus_detach), 81 KOBJMETHOD(uart_flush, ns8250_bus_flush), 82 KOBJMETHOD(uart_getsig, ns8250_bus_getsig), 83 KOBJMETHOD(uart_ioctl, ns8250_bus_ioctl), 84 KOBJMETHOD(uart_ipend, ns8250_bus_ipend), 85 KOBJMETHOD(uart_param, ns8250_bus_param), 86 KOBJMETHOD(uart_receive, ns8250_bus_receive), 87 KOBJMETHOD(uart_setsig, ns8250_bus_setsig), 88 KOBJMETHOD(uart_transmit, ns8250_bus_transmit), 89 KOBJMETHOD(uart_grab, ns8250_bus_grab), 90 KOBJMETHOD(uart_ungrab, ns8250_bus_ungrab), 91 KOBJMETHOD_END 92 }; 93 94 struct uart_class uart_snps_class = { 95 "snps", 96 snps_methods, 97 sizeof(struct snps_softc), 98 .uc_ops = &uart_ns8250_ops, 99 .uc_range = 8, 100 .uc_rclk = 0, 101 }; 102 103 static struct ofw_compat_data compat_data[] = { 104 { "snps,dw-apb-uart", (uintptr_t)&uart_snps_class }, 105 { NULL, (uintptr_t)NULL } 106 }; 107 UART_FDT_CLASS(compat_data); 108 109 #ifdef EXT_RESOURCES 110 static int 111 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk) 112 { 113 struct snps_softc *sc; 114 115 sc = device_get_softc(dev); 116 *baudclk = NULL; 117 *apb_pclk = NULL; 118 119 /* Baud clock is either named "baudclk", or there is a single 120 * unnamed clock. 121 */ 122 if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 && 123 clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0) 124 return (ENOENT); 125 126 /* APB peripheral clock is optional */ 127 (void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk); 128 129 return (0); 130 } 131 #endif 132 133 static int 134 snps_probe(device_t dev) 135 { 136 struct snps_softc *sc; 137 struct uart_class *uart_class; 138 phandle_t node; 139 uint32_t shift, iowidth, clock; 140 uint64_t freq; 141 int error; 142 #ifdef EXT_RESOURCES 143 clk_t baudclk, apb_pclk; 144 hwreset_t reset; 145 #endif 146 147 if (!ofw_bus_status_okay(dev)) 148 return (ENXIO); 149 150 uart_class = (struct uart_class *)ofw_bus_search_compatible(dev, 151 compat_data)->ocd_data; 152 if (uart_class == NULL) 153 return (ENXIO); 154 155 freq = 0; 156 sc = device_get_softc(dev); 157 sc->ns8250.base.sc_class = uart_class; 158 159 node = ofw_bus_get_node(dev); 160 if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0) 161 shift = 0; 162 if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0) 163 iowidth = 1; 164 if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0) 165 clock = 0; 166 167 #ifdef EXT_RESOURCES 168 if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) { 169 error = hwreset_deassert(reset); 170 if (error != 0) { 171 device_printf(dev, "cannot de-assert reset\n"); 172 return (error); 173 } 174 } 175 176 if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) { 177 error = clk_enable(baudclk); 178 if (error != 0) { 179 device_printf(dev, "cannot enable baud clock\n"); 180 return (error); 181 } 182 if (apb_pclk != NULL) { 183 error = clk_enable(apb_pclk); 184 if (error != 0) { 185 device_printf(dev, 186 "cannot enable peripheral clock\n"); 187 return (error); 188 } 189 } 190 191 if (clock == 0) { 192 error = clk_get_freq(baudclk, &freq); 193 if (error != 0) { 194 device_printf(dev, "cannot get frequency\n"); 195 return (error); 196 } 197 clock = (uint32_t)freq; 198 } 199 } 200 #endif 201 202 if (bootverbose && clock == 0) 203 device_printf(dev, "could not determine frequency\n"); 204 205 error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0); 206 if (error != 0) 207 return (error); 208 209 #ifdef EXT_RESOURCES 210 /* XXX uart_bus_probe has changed the softc, so refresh it */ 211 sc = device_get_softc(dev); 212 213 /* Store clock and reset handles for detach */ 214 sc->baudclk = baudclk; 215 sc->apb_pclk = apb_pclk; 216 sc->reset = reset; 217 #endif 218 219 return (0); 220 } 221 222 static int 223 snps_detach(device_t dev) 224 { 225 #ifdef EXT_RESOURCES 226 struct snps_softc *sc; 227 clk_t baudclk, apb_pclk; 228 hwreset_t reset; 229 #endif 230 int error; 231 232 #ifdef EXT_RESOURCES 233 sc = device_get_softc(dev); 234 baudclk = sc->baudclk; 235 apb_pclk = sc->apb_pclk; 236 reset = sc->reset; 237 #endif 238 239 error = uart_bus_detach(dev); 240 if (error != 0) 241 return (error); 242 243 #ifdef EXT_RESOURCES 244 if (reset != NULL) { 245 error = hwreset_assert(reset); 246 if (error != 0) { 247 device_printf(dev, "cannot assert reset\n"); 248 return (error); 249 } 250 hwreset_release(reset); 251 } 252 if (apb_pclk != NULL) { 253 error = clk_release(apb_pclk); 254 if (error != 0) { 255 device_printf(dev, "cannot release peripheral clock\n"); 256 return (error); 257 } 258 } 259 if (baudclk != NULL) { 260 error = clk_release(baudclk); 261 if (error != 0) { 262 device_printf(dev, "cannot release baud clock\n"); 263 return (error); 264 } 265 } 266 #endif 267 268 return (0); 269 } 270 271 static device_method_t snps_bus_methods[] = { 272 /* Device interface */ 273 DEVMETHOD(device_probe, snps_probe), 274 DEVMETHOD(device_attach, uart_bus_attach), 275 DEVMETHOD(device_detach, snps_detach), 276 DEVMETHOD_END 277 }; 278 279 static driver_t snps_uart_driver = { 280 uart_driver_name, 281 snps_bus_methods, 282 sizeof(struct snps_softc) 283 }; 284 285 DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, uart_devclass, 0, 0); 286