1250840Smarcel/*- 2250840Smarcel * Copyright (c) 2009-2010 The FreeBSD Foundation 3250840Smarcel * All rights reserved. 4250840Smarcel * 5250840Smarcel * This software was developed by Semihalf under sponsorship from 6250840Smarcel * the FreeBSD Foundation. 7250840Smarcel * 8250840Smarcel * Redistribution and use in source and binary forms, with or without 9250840Smarcel * modification, are permitted provided that the following conditions 10250840Smarcel * are met: 11250840Smarcel * 1. Redistributions of source code must retain the above copyright 12250840Smarcel * notice, this list of conditions and the following disclaimer. 13250840Smarcel * 2. Redistributions in binary form must reproduce the above copyright 14250840Smarcel * notice, this list of conditions and the following disclaimer in the 15250840Smarcel * documentation and/or other materials provided with the distribution. 16250840Smarcel * 17250840Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18250840Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19250840Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20250840Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21250840Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22250840Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23250840Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24250840Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25250840Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26250840Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27250840Smarcel * SUCH DAMAGE. 28250840Smarcel */ 29250840Smarcel 30250840Smarcel#include <sys/cdefs.h> 31250840Smarcel__FBSDID("$FreeBSD$"); 32250840Smarcel 33259357Sian#include "opt_platform.h" 34259357Sian 35250840Smarcel#include <sys/param.h> 36250840Smarcel#include <sys/bus.h> 37250840Smarcel#include <sys/kernel.h> 38250840Smarcel#include <sys/module.h> 39265973Sian#include <sys/systm.h> 40250840Smarcel 41265973Sian#include <vm/vm.h> 42265973Sian#include <vm/pmap.h> 43265973Sian 44250840Smarcel#include <machine/bus.h> 45250840Smarcel#include <machine/fdt.h> 46250840Smarcel 47250840Smarcel#include <dev/fdt/fdt_common.h> 48250840Smarcel#include <dev/ofw/ofw_bus.h> 49250840Smarcel#include <dev/ofw/ofw_bus_subr.h> 50250840Smarcel#include <dev/uart/uart.h> 51250840Smarcel#include <dev/uart/uart_bus.h> 52250840Smarcel#include <dev/uart/uart_cpu.h> 53283327Sian#include <dev/uart/uart_cpu_fdt.h> 54250840Smarcel 55250840Smarcel/* 56250840Smarcel * UART console routines. 57250840Smarcel */ 58250840Smarcelbus_space_tag_t uart_bus_space_io; 59250840Smarcelbus_space_tag_t uart_bus_space_mem; 60250840Smarcel 61250840Smarcelint 62250840Smarceluart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2) 63250840Smarcel{ 64250840Smarcel 65265973Sian if (b1->bst != b2->bst) 66265973Sian return (0); 67265973Sian if (pmap_kextract(b1->bsh) == 0) 68265973Sian return (0); 69265973Sian if (pmap_kextract(b2->bsh) == 0) 70265973Sian return (0); 71265973Sian return ((pmap_kextract(b1->bsh) == pmap_kextract(b2->bsh)) ? 1 : 0); 72250840Smarcel} 73250840Smarcel 74265973Sianstatic int 75265973Sianphandle_chosen_propdev(phandle_t chosen, const char *name, phandle_t *node) 76265973Sian{ 77265973Sian char buf[64]; 78265973Sian 79265973Sian if (OF_getprop(chosen, name, buf, sizeof(buf)) <= 0) 80265973Sian return (ENXIO); 81265973Sian if ((*node = OF_finddevice(buf)) == -1) 82265973Sian return (ENXIO); 83265973Sian 84265973Sian return (0); 85265973Sian} 86265973Sian 87283327Sianstatic const struct ofw_compat_data * 88283327Sianuart_fdt_find_compatible(phandle_t node, const struct ofw_compat_data *cd) 89283327Sian{ 90283327Sian const struct ofw_compat_data *ocd; 91283327Sian 92283327Sian for (ocd = cd; ocd->ocd_str != NULL; ocd++) { 93283327Sian if (fdt_is_compatible(node, ocd->ocd_str)) 94283327Sian return (ocd); 95283327Sian } 96283327Sian return (NULL); 97283327Sian} 98283327Sian 99283327Sianstatic uintptr_t 100283327Sianuart_fdt_find_by_node(phandle_t node, int class_list) 101283327Sian{ 102283327Sian struct ofw_compat_data **cd; 103283327Sian const struct ofw_compat_data *ocd; 104283327Sian 105283327Sian if (class_list) { 106283327Sian SET_FOREACH(cd, uart_fdt_class_set) { 107283327Sian ocd = uart_fdt_find_compatible(node, *cd); 108283327Sian if ((ocd != NULL) && (ocd->ocd_data != 0)) 109283327Sian return (ocd->ocd_data); 110283327Sian } 111283327Sian } else { 112283327Sian SET_FOREACH(cd, uart_fdt_class_and_device_set) { 113283327Sian ocd = uart_fdt_find_compatible(node, *cd); 114283327Sian if ((ocd != NULL) && (ocd->ocd_data != 0)) 115283327Sian return (ocd->ocd_data); 116283327Sian } 117283327Sian } 118283327Sian return (0); 119283327Sian} 120283327Sian 121250840Smarcelint 122250840Smarceluart_cpu_getdev(int devtype, struct uart_devinfo *di) 123250840Smarcel{ 124265973Sian const char *propnames[] = {"stdout-path", "linux,stdout-path", "stdout", 125265973Sian "stdin-path", "stdin", NULL}; 126265973Sian const char **name; 127250840Smarcel struct uart_class *class; 128250840Smarcel phandle_t node, chosen; 129250840Smarcel pcell_t shift, br, rclk; 130250840Smarcel u_long start, size, pbase, psize; 131250840Smarcel int err; 132250840Smarcel 133250840Smarcel uart_bus_space_mem = fdtbus_bs_tag; 134250840Smarcel uart_bus_space_io = NULL; 135250840Smarcel 136265973Sian /* Allow overriding the FDT using the environment. */ 137250840Smarcel class = &uart_ns8250_class; 138250840Smarcel err = uart_getenv(devtype, di, class); 139250840Smarcel if (!err) 140250840Smarcel return (0); 141250840Smarcel 142250840Smarcel if (devtype != UART_DEV_CONSOLE) 143250840Smarcel return (ENXIO); 144250840Smarcel 145250840Smarcel /* 146250840Smarcel * Retrieve /chosen/std{in,out}. 147250840Smarcel */ 148265998Sian node = -1; 149265998Sian if ((chosen = OF_finddevice("/chosen")) != -1) { 150265998Sian for (name = propnames; *name != NULL; name++) { 151265998Sian if (phandle_chosen_propdev(chosen, *name, &node) == 0) 152265998Sian break; 153265998Sian } 154265973Sian } 155265998Sian if (chosen == -1 || *name == NULL) 156265998Sian node = OF_finddevice("serial0"); /* Last ditch */ 157265998Sian 158265998Sian if (node == -1) /* Can't find anything */ 159250840Smarcel return (ENXIO); 160265998Sian 161250840Smarcel /* 162250840Smarcel * Retrieve serial attributes. 163250840Smarcel */ 164250840Smarcel uart_fdt_get_shift(node, &shift); 165250840Smarcel if (OF_getprop(node, "current-speed", &br, sizeof(br)) <= 0) 166250840Smarcel br = 0; 167283327Sian else 168283327Sian br = fdt32_to_cpu(br); 169250840Smarcel 170250840Smarcel /* 171283327Sian * Check old style of UART definition first. Unfortunately, the common 172283327Sian * FDT processing is not possible if we have clock, power domains and 173283327Sian * pinmux stuff. 174250840Smarcel */ 175283327Sian class = (struct uart_class *)uart_fdt_find_by_node(node, 0); 176283327Sian if (class != NULL) { 177283327Sian if ((err = uart_fdt_get_clock(node, &rclk)) != 0) 178283327Sian return (err); 179283327Sian } else { 180283327Sian /* Check class only linker set */ 181283327Sian class = 182283327Sian (struct uart_class *)uart_fdt_find_by_node(node, 1); 183283327Sian if (class == NULL) 184283327Sian return (ENXIO); 185283327Sian rclk = 0; 186259357Sian } 187250840Smarcel 188283327Sian /* 189283327Sian * Finalize configuration. 190283327Sian */ 191250840Smarcel di->bas.chan = 0; 192250840Smarcel di->bas.regshft = (u_int)shift; 193250840Smarcel di->baudrate = br; 194250840Smarcel di->bas.rclk = (u_int)rclk; 195250840Smarcel di->ops = uart_getops(class); 196250840Smarcel di->databits = 8; 197250840Smarcel di->stopbits = 1; 198250840Smarcel di->parity = UART_PARITY_NONE; 199250840Smarcel di->bas.bst = uart_bus_space_mem; 200250840Smarcel 201250840Smarcel err = fdt_regsize(node, &start, &size); 202250840Smarcel if (err) 203250840Smarcel return (ENXIO); 204250840Smarcel err = fdt_get_range(OF_parent(node), 0, &pbase, &psize); 205250840Smarcel if (err) 206250840Smarcel pbase = 0; 207250840Smarcel 208250840Smarcel start += pbase; 209250840Smarcel 210250840Smarcel return (bus_space_map(di->bas.bst, start, size, 0, &di->bas.bsh)); 211250840Smarcel} 212