Home | History | Annotate | Line # | Download | only in fdt
dw_apb_uart.c revision 1.2.2.3
      1  1.2.2.3  pgoyette /* $NetBSD: dw_apb_uart.c,v 1.2.2.3 2018/07/28 04:37:44 pgoyette Exp $ */
      2  1.2.2.2  pgoyette 
      3  1.2.2.2  pgoyette /*-
      4  1.2.2.2  pgoyette  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.2.2.2  pgoyette  * All rights reserved.
      6  1.2.2.2  pgoyette  *
      7  1.2.2.2  pgoyette  * Redistribution and use in source and binary forms, with or without
      8  1.2.2.2  pgoyette  * modification, are permitted provided that the following conditions
      9  1.2.2.2  pgoyette  * are met:
     10  1.2.2.2  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11  1.2.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12  1.2.2.2  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.2.2  pgoyette  *    documentation and/or other materials provided with the distribution.
     15  1.2.2.2  pgoyette  *
     16  1.2.2.2  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.2.2.2  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.2.2.2  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.2.2.2  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.2.2.2  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.2.2.2  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.2.2.2  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.2.2.2  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.2.2.2  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.2.2.2  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.2.2.2  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     27  1.2.2.2  pgoyette  */
     28  1.2.2.2  pgoyette 
     29  1.2.2.2  pgoyette #include <sys/cdefs.h>
     30  1.2.2.2  pgoyette 
     31  1.2.2.3  pgoyette __KERNEL_RCSID(1, "$NetBSD: dw_apb_uart.c,v 1.2.2.3 2018/07/28 04:37:44 pgoyette Exp $");
     32  1.2.2.2  pgoyette 
     33  1.2.2.2  pgoyette #include <sys/param.h>
     34  1.2.2.2  pgoyette #include <sys/bus.h>
     35  1.2.2.2  pgoyette #include <sys/device.h>
     36  1.2.2.2  pgoyette #include <sys/intr.h>
     37  1.2.2.2  pgoyette #include <sys/systm.h>
     38  1.2.2.2  pgoyette #include <sys/time.h>
     39  1.2.2.2  pgoyette #include <sys/termios.h>
     40  1.2.2.2  pgoyette 
     41  1.2.2.2  pgoyette #include <dev/ic/comvar.h>
     42  1.2.2.2  pgoyette 
     43  1.2.2.2  pgoyette #include <dev/fdt/fdtvar.h>
     44  1.2.2.2  pgoyette 
     45  1.2.2.2  pgoyette static int dw_apb_uart_match(device_t, cfdata_t, void *);
     46  1.2.2.2  pgoyette static void dw_apb_uart_attach(device_t, device_t, void *);
     47  1.2.2.2  pgoyette 
     48  1.2.2.2  pgoyette static const char * const compatible[] = {
     49  1.2.2.2  pgoyette 	"snps,dw-apb-uart",
     50  1.2.2.2  pgoyette 	NULL
     51  1.2.2.2  pgoyette };
     52  1.2.2.2  pgoyette 
     53  1.2.2.2  pgoyette struct dw_apb_uart_softc {
     54  1.2.2.2  pgoyette 	struct com_softc ssc_sc;
     55  1.2.2.2  pgoyette 	void *ssc_ih;
     56  1.2.2.2  pgoyette 
     57  1.2.2.2  pgoyette 	struct clk *ssc_clk;
     58  1.2.2.2  pgoyette 	struct clk *ssc_pclk;
     59  1.2.2.2  pgoyette 	struct fdtbus_reset *ssc_rst;
     60  1.2.2.2  pgoyette };
     61  1.2.2.2  pgoyette 
     62  1.2.2.2  pgoyette CFATTACH_DECL_NEW(dw_apb_uart, sizeof(struct dw_apb_uart_softc),
     63  1.2.2.2  pgoyette 	dw_apb_uart_match, dw_apb_uart_attach, NULL, NULL);
     64  1.2.2.2  pgoyette 
     65  1.2.2.2  pgoyette static int
     66  1.2.2.2  pgoyette dw_apb_uart_match(device_t parent, cfdata_t cf, void *aux)
     67  1.2.2.2  pgoyette {
     68  1.2.2.2  pgoyette 	struct fdt_attach_args * const faa = aux;
     69  1.2.2.2  pgoyette 
     70  1.2.2.2  pgoyette 	return of_match_compatible(faa->faa_phandle, compatible);
     71  1.2.2.2  pgoyette }
     72  1.2.2.2  pgoyette 
     73  1.2.2.2  pgoyette static void
     74  1.2.2.2  pgoyette dw_apb_uart_attach(device_t parent, device_t self, void *aux)
     75  1.2.2.2  pgoyette {
     76  1.2.2.2  pgoyette 	struct dw_apb_uart_softc * const ssc = device_private(self);
     77  1.2.2.2  pgoyette 	struct com_softc * const sc = &ssc->ssc_sc;
     78  1.2.2.2  pgoyette 	struct fdt_attach_args * const faa = aux;
     79  1.2.2.2  pgoyette 	bus_space_handle_t bsh;
     80  1.2.2.2  pgoyette 	bus_space_tag_t bst;
     81  1.2.2.2  pgoyette 	char intrstr[128];
     82  1.2.2.2  pgoyette 	bus_addr_t addr;
     83  1.2.2.2  pgoyette 	bus_size_t size;
     84  1.2.2.2  pgoyette 	u_int reg_shift;
     85  1.2.2.2  pgoyette 	int error;
     86  1.2.2.2  pgoyette 
     87  1.2.2.2  pgoyette 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
     88  1.2.2.2  pgoyette 		aprint_error(": couldn't get registers\n");
     89  1.2.2.2  pgoyette 		return;
     90  1.2.2.2  pgoyette 	}
     91  1.2.2.2  pgoyette 
     92  1.2.2.2  pgoyette 	if (of_getprop_uint32(faa->faa_phandle, "reg-shift", &reg_shift)) {
     93  1.2.2.2  pgoyette 		/* missing or bad reg-shift property, assume 2 */
     94  1.2.2.2  pgoyette 		bst = faa->faa_a4x_bst;
     95  1.2.2.2  pgoyette 	} else {
     96  1.2.2.2  pgoyette 		if (reg_shift == 2) {
     97  1.2.2.2  pgoyette 			bst = faa->faa_a4x_bst;
     98  1.2.2.2  pgoyette 		} else if (reg_shift == 0) {
     99  1.2.2.2  pgoyette 			bst = faa->faa_bst;
    100  1.2.2.2  pgoyette 		} else {
    101  1.2.2.2  pgoyette 			aprint_error(": unsupported reg-shift value %d\n",
    102  1.2.2.2  pgoyette 			    reg_shift);
    103  1.2.2.2  pgoyette 			return;
    104  1.2.2.2  pgoyette 		}
    105  1.2.2.2  pgoyette 	}
    106  1.2.2.2  pgoyette 
    107  1.2.2.2  pgoyette 	sc->sc_dev = self;
    108  1.2.2.2  pgoyette 
    109  1.2.2.2  pgoyette 	ssc->ssc_clk = fdtbus_clock_get_index(faa->faa_phandle, 0);
    110  1.2.2.2  pgoyette 	if (ssc->ssc_clk == NULL) {
    111  1.2.2.2  pgoyette 		aprint_error(": couldn't get clock\n");
    112  1.2.2.2  pgoyette 		return;
    113  1.2.2.2  pgoyette 	}
    114  1.2.2.2  pgoyette 	if (clk_enable(ssc->ssc_clk) != 0) {
    115  1.2.2.2  pgoyette 		aprint_error(": couldn't enable clock\n");
    116  1.2.2.2  pgoyette 		return;
    117  1.2.2.2  pgoyette 	}
    118  1.2.2.2  pgoyette 
    119  1.2.2.2  pgoyette 	ssc->ssc_pclk = fdtbus_clock_get(faa->faa_phandle, "apb_pclk");
    120  1.2.2.2  pgoyette 	if (ssc->ssc_pclk != NULL && clk_enable(ssc->ssc_pclk) != 0) {
    121  1.2.2.2  pgoyette 		aprint_error(": couldn't enable peripheral clock\n");
    122  1.2.2.2  pgoyette 		return;
    123  1.2.2.2  pgoyette 	}
    124  1.2.2.2  pgoyette 
    125  1.2.2.2  pgoyette 	ssc->ssc_rst = fdtbus_reset_get_index(faa->faa_phandle, 0);
    126  1.2.2.2  pgoyette 	if (ssc->ssc_rst && fdtbus_reset_deassert(ssc->ssc_rst) != 0) {
    127  1.2.2.2  pgoyette 		aprint_error(": couldn't de-assert reset\n");
    128  1.2.2.2  pgoyette 		return;
    129  1.2.2.2  pgoyette 	}
    130  1.2.2.2  pgoyette 
    131  1.2.2.2  pgoyette 	sc->sc_frequency = clk_get_rate(ssc->ssc_clk);
    132  1.2.2.2  pgoyette 	sc->sc_type = COM_TYPE_DW_APB;
    133  1.2.2.2  pgoyette 
    134  1.2.2.2  pgoyette 	error = bus_space_map(bst, addr, size, 0, &bsh);
    135  1.2.2.2  pgoyette 	if (error) {
    136  1.2.2.3  pgoyette 		aprint_error(": couldn't map %#" PRIx64 ": %d", (uint64_t)addr, error);
    137  1.2.2.2  pgoyette 		return;
    138  1.2.2.2  pgoyette 	}
    139  1.2.2.2  pgoyette 
    140  1.2.2.2  pgoyette 	COM_INIT_REGS(sc->sc_regs, bst, bsh, addr);
    141  1.2.2.2  pgoyette 
    142  1.2.2.2  pgoyette 	com_attach_subr(sc);
    143  1.2.2.2  pgoyette 
    144  1.2.2.2  pgoyette 	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
    145  1.2.2.2  pgoyette 		aprint_error_dev(self, "failed to decode interrupt\n");
    146  1.2.2.2  pgoyette 		return;
    147  1.2.2.2  pgoyette 	}
    148  1.2.2.2  pgoyette 
    149  1.2.2.2  pgoyette 	ssc->ssc_ih = fdtbus_intr_establish(faa->faa_phandle, 0, IPL_SERIAL,
    150  1.2.2.2  pgoyette 	    FDT_INTR_MPSAFE, comintr, sc);
    151  1.2.2.2  pgoyette 	if (ssc->ssc_ih == NULL) {
    152  1.2.2.2  pgoyette 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    153  1.2.2.2  pgoyette 		    intrstr);
    154  1.2.2.2  pgoyette 	}
    155  1.2.2.2  pgoyette 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    156  1.2.2.2  pgoyette }
    157  1.2.2.2  pgoyette 
    158  1.2.2.2  pgoyette /*
    159  1.2.2.2  pgoyette  * Console support
    160  1.2.2.2  pgoyette  */
    161  1.2.2.2  pgoyette 
    162  1.2.2.2  pgoyette static int
    163  1.2.2.2  pgoyette dw_apb_uart_console_match(int phandle)
    164  1.2.2.2  pgoyette {
    165  1.2.2.2  pgoyette 	return of_match_compatible(phandle, compatible);
    166  1.2.2.2  pgoyette }
    167  1.2.2.2  pgoyette 
    168  1.2.2.2  pgoyette static void
    169  1.2.2.2  pgoyette dw_apb_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
    170  1.2.2.2  pgoyette {
    171  1.2.2.2  pgoyette 	const int phandle = faa->faa_phandle;
    172  1.2.2.2  pgoyette 	bus_space_tag_t bst = faa->faa_a4x_bst;
    173  1.2.2.2  pgoyette 	bus_addr_t addr;
    174  1.2.2.2  pgoyette 	tcflag_t flags;
    175  1.2.2.2  pgoyette 	int speed;
    176  1.2.2.2  pgoyette 
    177  1.2.2.2  pgoyette 	fdtbus_get_reg(phandle, 0, &addr, NULL);
    178  1.2.2.2  pgoyette 	speed = fdtbus_get_stdout_speed();
    179  1.2.2.2  pgoyette 	if (speed < 0)
    180  1.2.2.2  pgoyette 		speed = 115200;	/* default */
    181  1.2.2.2  pgoyette 	flags = fdtbus_get_stdout_flags();
    182  1.2.2.2  pgoyette 
    183  1.2.2.2  pgoyette 	if (comcnattach(bst, addr, speed, uart_freq, COM_TYPE_DW_APB, flags))
    184  1.2.2.2  pgoyette 		panic("Cannot initialize dw-apb-uart console");
    185  1.2.2.2  pgoyette }
    186  1.2.2.2  pgoyette 
    187  1.2.2.2  pgoyette static const struct fdt_console dw_apb_uart_console = {
    188  1.2.2.2  pgoyette 	.match = dw_apb_uart_console_match,
    189  1.2.2.2  pgoyette 	.consinit = dw_apb_uart_console_consinit,
    190  1.2.2.2  pgoyette };
    191  1.2.2.2  pgoyette 
    192  1.2.2.2  pgoyette FDT_CONSOLE(dw_apb_uart, &dw_apb_uart_console);
    193