bcm2835_com.c revision 1.5
1/* $NetBSD: bcm2835_com.c,v 1.5 2018/12/08 17:46:09 thorpej Exp $ */ 2 3/*- 4 * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__KERNEL_RCSID(0, "$NetBSD: bcm2835_com.c,v 1.5 2018/12/08 17:46:09 thorpej Exp $"); 31 32#include <sys/param.h> 33#include <sys/bus.h> 34#include <sys/device.h> 35#include <sys/intr.h> 36#include <sys/systm.h> 37#include <sys/termios.h> 38 39#include <arm/broadcom/bcm2835reg.h> 40#include <arm/broadcom/bcm2835var.h> 41#include <arm/broadcom/bcm2835_intr.h> 42 43#include <dev/fdt/fdtvar.h> 44 45#include <dev/ic/comvar.h> 46 47static int bcm_com_match(device_t, cfdata_t, void *); 48static void bcm_com_attach(device_t, device_t, void *); 49 50CFATTACH_DECL_NEW(bcmcom, sizeof(struct com_softc), 51 bcm_com_match, bcm_com_attach, NULL, NULL); 52 53static const char * const compatible[] = { 54 "brcm,bcm2835-aux-uart", 55 NULL 56}; 57 58static int 59bcm_com_match(device_t parent, cfdata_t cf, void *aux) 60{ 61 struct fdt_attach_args * const faa = aux; 62 63 return of_match_compatible(faa->faa_phandle, compatible); 64} 65 66static void 67bcm_com_attach(device_t parent, device_t self, void *aux) 68{ 69 struct com_softc * const sc = device_private(self); 70 struct fdt_attach_args * const faa = aux; 71 const int phandle = faa->faa_phandle; 72 73 bus_space_tag_t bst = faa->faa_a4x_bst; 74 bus_space_handle_t bsh; 75 bus_addr_t addr; 76 bus_size_t size; 77 void *ih; 78 79 sc->sc_dev = self; 80 sc->sc_type = COM_TYPE_BCMAUXUART; 81 82 int error = fdtbus_get_reg(phandle, 0, &addr, &size); 83 if (error) { 84 aprint_error_dev(sc->sc_dev, "unable to map device\n"); 85 return; 86 } 87 88 if (com_is_console(bst, addr, &bsh) == 0 && 89 bus_space_map(bst, addr, size, 0, &bsh) != 0) { 90 aprint_error(": can't map device\n"); 91 return; 92 } 93 94 /* Enable clocks */ 95 struct clk *clk; 96 for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++) { 97 if (clk_enable(clk) != 0) { 98 aprint_error(": failed to enable clock #%d\n", i); 99 return; 100 } 101 /* First clock is UARTCLK */ 102 if (i == 0) 103 sc->sc_frequency = clk_get_rate(clk); 104 } 105 106 sc->sc_frequency *= 2; 107 108 com_init_regs(&sc->sc_regs, bst, bsh, addr); 109 110 com_attach_subr(sc); 111 aprint_naive("\n"); 112 113 char intrstr[128]; 114 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 115 aprint_error(": failed to decode interrupt\n"); 116 return; 117 } 118 119 ih = fdtbus_intr_establish(phandle, 0, IPL_SERIAL, FDT_INTR_MPSAFE, 120 comintr, sc); 121 if (ih == NULL) { 122 aprint_error_dev(self, "failed to establish interrupt %s\n", 123 intrstr); 124 return; 125 } 126 aprint_normal_dev(self, "interrupting on %s\n", intrstr); 127} 128 129static int 130bcmaux_com_console_match(int phandle) 131{ 132 133 return of_match_compatible(phandle, compatible); 134} 135 136static void 137bcmaux_com_console_consinit(struct fdt_attach_args *faa, u_int uart_freq) 138{ 139 const int phandle = faa->faa_phandle; 140 bus_space_tag_t bst = faa->faa_a4x_bst; 141 bus_addr_t addr; 142 tcflag_t flags; 143 int speed; 144 145 fdtbus_get_reg(phandle, 0, &addr, NULL); 146 speed = fdtbus_get_stdout_speed(); 147 if (speed < 0) 148 speed = 115200; /* default */ 149 flags = fdtbus_get_stdout_flags(); 150 151 if (comcnattach(bst, addr, speed, uart_freq, COM_TYPE_BCMAUXUART, 152 flags)) 153 panic("Cannot initialize bcm com console"); 154 155 cn_set_magic("+++++"); 156} 157 158static const struct fdt_console bcmaux_com_console = { 159 .match = bcmaux_com_console_match, 160 .consinit = bcmaux_com_console_consinit, 161}; 162 163FDT_CONSOLE(bcmcom, &bcmaux_com_console); 164