1 /*- 2 * Copyright (c) 2009-2010 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Semihalf under sponsorship from 6 * the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_platform.h" 34 35 #include <sys/param.h> 36 #include <sys/bus.h> 37 #include <sys/kernel.h> 38 #include <sys/module.h> 39 #include <sys/systm.h> 40 41 #include <vm/vm.h> 42 #include <vm/pmap.h> 43 44 #include <machine/bus.h> 45 #include <machine/fdt.h> 46 47 #include <dev/fdt/fdt_common.h> 48 #include <dev/ofw/ofw_bus.h> 49 #include <dev/ofw/ofw_bus_subr.h> 50 #include <dev/uart/uart.h> 51 #include <dev/uart/uart_bus.h> 52 #include <dev/uart/uart_cpu.h> 53 #include <dev/uart/uart_cpu_fdt.h> 54 55 /* 56 * UART console routines. 57 */ 58 bus_space_tag_t uart_bus_space_io; 59 bus_space_tag_t uart_bus_space_mem; 60 61 static int 62 uart_fdt_get_clock(phandle_t node, pcell_t *cell) 63 { 64 pcell_t clock; 65 66 /* clock-frequency is a FreeBSD-only extention. */ 67 if ((OF_getprop(node, "clock-frequency", &clock, 68 sizeof(clock))) <= 0) 69 clock = 0; 70 71 if (clock == 0) 72 /* Try to retrieve parent 'bus-frequency' */ 73 /* XXX this should go to simple-bus fixup or so */ 74 if ((OF_getprop(OF_parent(node), "bus-frequency", &clock, 75 sizeof(clock))) <= 0) 76 clock = 0; 77 78 *cell = fdt32_to_cpu(clock); 79 return (0); 80 } 81 82 static int 83 uart_fdt_get_shift(phandle_t node, pcell_t *cell) 84 { 85 pcell_t shift; 86 87 if ((OF_getprop(node, "reg-shift", &shift, sizeof(shift))) <= 0) 88 shift = 0; 89 *cell = fdt32_to_cpu(shift); 90 return (0); 91 } 92 93 int 94 uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2) 95 { 96 97 if (b1->bst != b2->bst) 98 return (0); 99 if (pmap_kextract(b1->bsh) == 0) 100 return (0); 101 if (pmap_kextract(b2->bsh) == 0) 102 return (0); 103 return ((pmap_kextract(b1->bsh) == pmap_kextract(b2->bsh)) ? 1 : 0); 104 } 105 106 static int 107 phandle_chosen_propdev(phandle_t chosen, const char *name, phandle_t *node) 108 { 109 char buf[64]; 110 111 if (OF_getprop(chosen, name, buf, sizeof(buf)) <= 0) 112 return (ENXIO); 113 if ((*node = OF_finddevice(buf)) == -1) 114 return (ENXIO); 115 116 return (0); 117 } 118 119 static const struct ofw_compat_data * 120 uart_fdt_find_compatible(phandle_t node, const struct ofw_compat_data *cd) 121 { 122 const struct ofw_compat_data *ocd; 123 124 for (ocd = cd; ocd->ocd_str != NULL; ocd++) { 125 if (fdt_is_compatible(node, ocd->ocd_str)) 126 return (ocd); 127 } 128 return (NULL); 129 } 130 131 static uintptr_t 132 uart_fdt_find_by_node(phandle_t node, int class_list) 133 { 134 struct ofw_compat_data **cd; 135 const struct ofw_compat_data *ocd; 136 137 if (class_list) { 138 SET_FOREACH(cd, uart_fdt_class_set) { 139 ocd = uart_fdt_find_compatible(node, *cd); 140 if ((ocd != NULL) && (ocd->ocd_data != 0)) 141 return (ocd->ocd_data); 142 } 143 } else { 144 SET_FOREACH(cd, uart_fdt_class_and_device_set) { 145 ocd = uart_fdt_find_compatible(node, *cd); 146 if ((ocd != NULL) && (ocd->ocd_data != 0)) 147 return (ocd->ocd_data); 148 } 149 } 150 return (0); 151 } 152 153 int 154 uart_cpu_getdev(int devtype, struct uart_devinfo *di) 155 { 156 const char *propnames[] = {"stdout-path", "linux,stdout-path", "stdout", 157 "stdin-path", "stdin", NULL}; 158 const char **name; 159 struct uart_class *class; 160 phandle_t node, chosen; 161 pcell_t shift, br, rclk; 162 u_long start, size, pbase, psize; 163 int err; 164 165 uart_bus_space_mem = fdtbus_bs_tag; 166 uart_bus_space_io = NULL; 167 168 /* Allow overriding the FDT using the environment. */ 169 class = &uart_ns8250_class; 170 err = uart_getenv(devtype, di, class); 171 if (!err) 172 return (0); 173 174 if (devtype != UART_DEV_CONSOLE) 175 return (ENXIO); 176 177 /* 178 * Retrieve /chosen/std{in,out}. 179 */ 180 node = -1; 181 if ((chosen = OF_finddevice("/chosen")) != -1) { 182 for (name = propnames; *name != NULL; name++) { 183 if (phandle_chosen_propdev(chosen, *name, &node) == 0) 184 break; 185 } 186 } 187 if (chosen == -1 || *name == NULL) 188 node = OF_finddevice("serial0"); /* Last ditch */ 189 190 if (node == -1) /* Can't find anything */ 191 return (ENXIO); 192 193 /* 194 * Retrieve serial attributes. 195 */ 196 uart_fdt_get_shift(node, &shift); 197 if (OF_getprop(node, "current-speed", &br, sizeof(br)) <= 0) 198 br = 0; 199 else 200 br = fdt32_to_cpu(br); 201 202 /* 203 * Check old style of UART definition first. Unfortunately, the common 204 * FDT processing is not possible if we have clock, power domains and 205 * pinmux stuff. 206 */ 207 class = (struct uart_class *)uart_fdt_find_by_node(node, 0); 208 if (class != NULL) { 209 if ((err = uart_fdt_get_clock(node, &rclk)) != 0) 210 return (err); 211 } else { 212 /* Check class only linker set */ 213 class = 214 (struct uart_class *)uart_fdt_find_by_node(node, 1); 215 if (class == NULL) 216 return (ENXIO); 217 rclk = 0; 218 } 219 220 /* 221 * Finalize configuration. 222 */ 223 di->bas.chan = 0; 224 di->bas.regshft = (u_int)shift; 225 di->baudrate = br; 226 di->bas.rclk = (u_int)rclk; 227 di->ops = uart_getops(class); 228 di->databits = 8; 229 di->stopbits = 1; 230 di->parity = UART_PARITY_NONE; 231 di->bas.bst = uart_bus_space_mem; 232 233 err = fdt_regsize(node, &start, &size); 234 if (err) 235 return (ENXIO); 236 err = fdt_get_range(OF_parent(node), 0, &pbase, &psize); 237 if (err) 238 pbase = 0; 239 240 start += pbase; 241 242 return (bus_space_map(di->bas.bst, start, size, 0, &di->bas.bsh)); 243 } 244