1cb34ed44SMarcel Moolenaar /*- 2cb34ed44SMarcel Moolenaar * Copyright (c) 2009-2010 The FreeBSD Foundation 3cb34ed44SMarcel Moolenaar * All rights reserved. 4cb34ed44SMarcel Moolenaar * 5cb34ed44SMarcel Moolenaar * This software was developed by Semihalf under sponsorship from 6cb34ed44SMarcel Moolenaar * the FreeBSD Foundation. 7cb34ed44SMarcel Moolenaar * 8cb34ed44SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 9cb34ed44SMarcel Moolenaar * modification, are permitted provided that the following conditions 10cb34ed44SMarcel Moolenaar * are met: 11cb34ed44SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 12cb34ed44SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 13cb34ed44SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 14cb34ed44SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 15cb34ed44SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 16cb34ed44SMarcel Moolenaar * 17cb34ed44SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18cb34ed44SMarcel Moolenaar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19cb34ed44SMarcel Moolenaar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20cb34ed44SMarcel Moolenaar * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21cb34ed44SMarcel Moolenaar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22cb34ed44SMarcel Moolenaar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23cb34ed44SMarcel Moolenaar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24cb34ed44SMarcel Moolenaar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25cb34ed44SMarcel Moolenaar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26cb34ed44SMarcel Moolenaar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27cb34ed44SMarcel Moolenaar * SUCH DAMAGE. 28cb34ed44SMarcel Moolenaar */ 29cb34ed44SMarcel Moolenaar 30cb34ed44SMarcel Moolenaar #include <sys/cdefs.h> 31cb34ed44SMarcel Moolenaar __FBSDID("$FreeBSD$"); 32cb34ed44SMarcel Moolenaar 33cb34ed44SMarcel Moolenaar #include <sys/param.h> 34cb34ed44SMarcel Moolenaar #include <sys/bus.h> 35cb34ed44SMarcel Moolenaar #include <sys/kernel.h> 36cb34ed44SMarcel Moolenaar #include <sys/module.h> 37*dce533f3SNathan Whitehorn #include <sys/systm.h> 38cb34ed44SMarcel Moolenaar 39c20809e1SNathan Whitehorn #include <vm/vm.h> 40c20809e1SNathan Whitehorn #include <vm/pmap.h> 41c20809e1SNathan Whitehorn 42cb34ed44SMarcel Moolenaar #include <machine/bus.h> 43cb34ed44SMarcel Moolenaar #include <machine/fdt.h> 44cb34ed44SMarcel Moolenaar 45cb34ed44SMarcel Moolenaar #include <dev/fdt/fdt_common.h> 46cb34ed44SMarcel Moolenaar #include <dev/ofw/ofw_bus.h> 47cb34ed44SMarcel Moolenaar #include <dev/ofw/ofw_bus_subr.h> 48cb34ed44SMarcel Moolenaar #include <dev/uart/uart.h> 49cb34ed44SMarcel Moolenaar #include <dev/uart/uart_bus.h> 50cb34ed44SMarcel Moolenaar #include <dev/uart/uart_cpu.h> 51cb34ed44SMarcel Moolenaar 52cb34ed44SMarcel Moolenaar /* 53cb34ed44SMarcel Moolenaar * UART console routines. 54cb34ed44SMarcel Moolenaar */ 55cb34ed44SMarcel Moolenaar bus_space_tag_t uart_bus_space_io; 56cb34ed44SMarcel Moolenaar bus_space_tag_t uart_bus_space_mem; 57cb34ed44SMarcel Moolenaar 58cb34ed44SMarcel Moolenaar static int 59cb34ed44SMarcel Moolenaar uart_fdt_get_clock(phandle_t node, pcell_t *cell) 60cb34ed44SMarcel Moolenaar { 61cb34ed44SMarcel Moolenaar pcell_t clock; 62cb34ed44SMarcel Moolenaar 63cb34ed44SMarcel Moolenaar if ((OF_getprop(node, "clock-frequency", &clock, 64cb34ed44SMarcel Moolenaar sizeof(clock))) <= 0) 65cb34ed44SMarcel Moolenaar return (ENXIO); 66cb34ed44SMarcel Moolenaar 67cb34ed44SMarcel Moolenaar if (clock == 0) 68cb34ed44SMarcel Moolenaar /* Try to retrieve parent 'bus-frequency' */ 69cb34ed44SMarcel Moolenaar /* XXX this should go to simple-bus fixup or so */ 70cb34ed44SMarcel Moolenaar if ((OF_getprop(OF_parent(node), "bus-frequency", &clock, 71cb34ed44SMarcel Moolenaar sizeof(clock))) <= 0) 72cb34ed44SMarcel Moolenaar clock = 0; 73cb34ed44SMarcel Moolenaar 74cb34ed44SMarcel Moolenaar *cell = fdt32_to_cpu(clock); 75cb34ed44SMarcel Moolenaar return (0); 76cb34ed44SMarcel Moolenaar } 77cb34ed44SMarcel Moolenaar 78cb34ed44SMarcel Moolenaar static int 79cb34ed44SMarcel Moolenaar uart_fdt_get_shift(phandle_t node, pcell_t *cell) 80cb34ed44SMarcel Moolenaar { 81cb34ed44SMarcel Moolenaar pcell_t shift; 82cb34ed44SMarcel Moolenaar 83cb34ed44SMarcel Moolenaar if ((OF_getprop(node, "reg-shift", &shift, sizeof(shift))) <= 0) 84cb34ed44SMarcel Moolenaar shift = 0; 85cb34ed44SMarcel Moolenaar *cell = fdt32_to_cpu(shift); 86cb34ed44SMarcel Moolenaar return (0); 87cb34ed44SMarcel Moolenaar } 88cb34ed44SMarcel Moolenaar 89cb34ed44SMarcel Moolenaar int 90cb34ed44SMarcel Moolenaar uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2) 91cb34ed44SMarcel Moolenaar { 92cb34ed44SMarcel Moolenaar 934162dff9SAleksandr Rybalko if (b1->bst != b2->bst) 944162dff9SAleksandr Rybalko return (0); 954162dff9SAleksandr Rybalko if (pmap_kextract(b1->bsh) == 0) 964162dff9SAleksandr Rybalko return (0); 974162dff9SAleksandr Rybalko if (pmap_kextract(b2->bsh) == 0) 984162dff9SAleksandr Rybalko return (0); 994162dff9SAleksandr Rybalko return ((pmap_kextract(b1->bsh) == pmap_kextract(b2->bsh)) ? 1 : 0); 100cb34ed44SMarcel Moolenaar } 101cb34ed44SMarcel Moolenaar 102*dce533f3SNathan Whitehorn static int 103*dce533f3SNathan Whitehorn phandle_chosen_propdev(phandle_t chosen, const char *name, phandle_t *node) 104*dce533f3SNathan Whitehorn { 105*dce533f3SNathan Whitehorn char buf[64]; 106*dce533f3SNathan Whitehorn 107*dce533f3SNathan Whitehorn if (OF_getprop(chosen, name, buf, sizeof(buf)) <= 0) 108*dce533f3SNathan Whitehorn return (ENXIO); 109*dce533f3SNathan Whitehorn if ((*node = OF_finddevice(buf)) == -1) 110*dce533f3SNathan Whitehorn return (ENXIO); 111*dce533f3SNathan Whitehorn 112*dce533f3SNathan Whitehorn return (0); 113*dce533f3SNathan Whitehorn } 114*dce533f3SNathan Whitehorn 115cb34ed44SMarcel Moolenaar int 116cb34ed44SMarcel Moolenaar uart_cpu_getdev(int devtype, struct uart_devinfo *di) 117cb34ed44SMarcel Moolenaar { 118*dce533f3SNathan Whitehorn const char *propnames[] = {"stdout-path", "linux,stdout-path", "stdout", 119*dce533f3SNathan Whitehorn "stdin-path", "stdin", NULL}; 120*dce533f3SNathan Whitehorn const char **name; 121cb34ed44SMarcel Moolenaar struct uart_class *class; 122cb34ed44SMarcel Moolenaar phandle_t node, chosen; 123cb34ed44SMarcel Moolenaar pcell_t shift, br, rclk; 124cb34ed44SMarcel Moolenaar u_long start, size, pbase, psize; 125cb34ed44SMarcel Moolenaar int err; 126cb34ed44SMarcel Moolenaar 127cb34ed44SMarcel Moolenaar uart_bus_space_mem = fdtbus_bs_tag; 128cb34ed44SMarcel Moolenaar uart_bus_space_io = NULL; 129cb34ed44SMarcel Moolenaar 130*dce533f3SNathan Whitehorn /* Allow overriding the FDT using the environment. */ 131cb34ed44SMarcel Moolenaar class = &uart_ns8250_class; 132cb34ed44SMarcel Moolenaar err = uart_getenv(devtype, di, class); 133cb34ed44SMarcel Moolenaar if (!err) 134cb34ed44SMarcel Moolenaar return (0); 135cb34ed44SMarcel Moolenaar 136cb34ed44SMarcel Moolenaar if (devtype != UART_DEV_CONSOLE) 137cb34ed44SMarcel Moolenaar return (ENXIO); 138cb34ed44SMarcel Moolenaar 139cb34ed44SMarcel Moolenaar /* 140cb34ed44SMarcel Moolenaar * Retrieve /chosen/std{in,out}. 141cb34ed44SMarcel Moolenaar */ 142cb34ed44SMarcel Moolenaar if ((chosen = OF_finddevice("/chosen")) == -1) 143cb34ed44SMarcel Moolenaar return (ENXIO); 144*dce533f3SNathan Whitehorn for (name = propnames; *name != NULL; name++) { 145*dce533f3SNathan Whitehorn if (phandle_chosen_propdev(chosen, *name, &node) == 0) 146*dce533f3SNathan Whitehorn break; 147*dce533f3SNathan Whitehorn } 148*dce533f3SNathan Whitehorn if (*name == NULL) 149cb34ed44SMarcel Moolenaar return (ENXIO); 150cb34ed44SMarcel Moolenaar /* 151cb34ed44SMarcel Moolenaar * Retrieve serial attributes. 152cb34ed44SMarcel Moolenaar */ 153cb34ed44SMarcel Moolenaar uart_fdt_get_shift(node, &shift); 154cb34ed44SMarcel Moolenaar 155cb34ed44SMarcel Moolenaar if (OF_getprop(node, "current-speed", &br, sizeof(br)) <= 0) 156cb34ed44SMarcel Moolenaar br = 0; 157cb34ed44SMarcel Moolenaar br = fdt32_to_cpu(br); 158cb34ed44SMarcel Moolenaar 159cb34ed44SMarcel Moolenaar if ((err = uart_fdt_get_clock(node, &rclk)) != 0) 160cb34ed44SMarcel Moolenaar return (err); 161cb34ed44SMarcel Moolenaar /* 162cb34ed44SMarcel Moolenaar * Finalize configuration. 163cb34ed44SMarcel Moolenaar */ 164cb34ed44SMarcel Moolenaar if (fdt_is_compatible(node, "fsl,imx-uart")) 165cb34ed44SMarcel Moolenaar class = &uart_imx_class; 1666fd28cd9SIan Lepore else if (fdt_is_compatible(node, "quicc")) 167cb34ed44SMarcel Moolenaar class = &uart_quicc_class; 1686fd28cd9SIan Lepore else if (fdt_is_compatible(node, "lpc")) 169cb34ed44SMarcel Moolenaar class = &uart_lpc_class; 1706fd28cd9SIan Lepore else if (fdt_is_compatible(node, "arm,pl011")) 171cb34ed44SMarcel Moolenaar class = &uart_pl011_class; 1726fd28cd9SIan Lepore else if (fdt_is_compatible(node, "exynos")) 1735d490515SAleksandr Rybalko class = &uart_s3c2410_class; 1746fd28cd9SIan Lepore else if (fdt_is_compatible(node, "cadence,uart")) 175cb34ed44SMarcel Moolenaar class = &uart_cdnc_class; 176aef60d8cSIan Lepore else if (fdt_is_compatible(node, "ti,ns16550")) 177aef60d8cSIan Lepore class = &uart_ti8250_class; 1786fd28cd9SIan Lepore else if (fdt_is_compatible(node, "ns16550")) 1796fd28cd9SIan Lepore class = &uart_ns8250_class; 180cb34ed44SMarcel Moolenaar 181cb34ed44SMarcel Moolenaar di->bas.chan = 0; 182cb34ed44SMarcel Moolenaar di->bas.regshft = (u_int)shift; 183cb34ed44SMarcel Moolenaar di->baudrate = br; 184cb34ed44SMarcel Moolenaar di->bas.rclk = (u_int)rclk; 185cb34ed44SMarcel Moolenaar di->ops = uart_getops(class); 186cb34ed44SMarcel Moolenaar di->databits = 8; 187cb34ed44SMarcel Moolenaar di->stopbits = 1; 188cb34ed44SMarcel Moolenaar di->parity = UART_PARITY_NONE; 189cb34ed44SMarcel Moolenaar di->bas.bst = uart_bus_space_mem; 190cb34ed44SMarcel Moolenaar 191cb34ed44SMarcel Moolenaar err = fdt_regsize(node, &start, &size); 192cb34ed44SMarcel Moolenaar if (err) 193cb34ed44SMarcel Moolenaar return (ENXIO); 194cb34ed44SMarcel Moolenaar err = fdt_get_range(OF_parent(node), 0, &pbase, &psize); 195cb34ed44SMarcel Moolenaar if (err) 196cb34ed44SMarcel Moolenaar pbase = 0; 197cb34ed44SMarcel Moolenaar 198cb34ed44SMarcel Moolenaar start += pbase; 199cb34ed44SMarcel Moolenaar 200cb34ed44SMarcel Moolenaar return (bus_space_map(di->bas.bst, start, size, 0, &di->bas.bsh)); 201cb34ed44SMarcel Moolenaar } 202