Home | History | Annotate | Line # | Download | only in samsung
exynos_uart.c revision 1.2.4.3
      1  1.2.4.3    martin /* $NetBSD: exynos_uart.c,v 1.2.4.3 2020/04/08 14:07:30 martin Exp $ */
      2  1.2.4.2  christos 
      3  1.2.4.2  christos /*-
      4  1.2.4.2  christos  * Copyright (c) 2013-2018 The NetBSD Foundation, Inc.
      5  1.2.4.2  christos  * All rights reserved.
      6  1.2.4.2  christos  *
      7  1.2.4.2  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.4.2  christos  * by Matt Thomas of 3am Software Foundry and Jared McNeill.
      9  1.2.4.2  christos  *
     10  1.2.4.2  christos  * Redistribution and use in source and binary forms, with or without
     11  1.2.4.2  christos  * modification, are permitted provided that the following conditions
     12  1.2.4.2  christos  * are met:
     13  1.2.4.2  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.2.4.2  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.2.4.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.4.2  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.4.2  christos  *    documentation and/or other materials provided with the distribution.
     18  1.2.4.2  christos  *
     19  1.2.4.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2.4.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2.4.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2.4.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2.4.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2.4.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2.4.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2.4.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2.4.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2.4.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2.4.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2.4.2  christos  */
     31  1.2.4.2  christos 
     32  1.2.4.2  christos #include "locators.h"
     33  1.2.4.2  christos 
     34  1.2.4.2  christos #include <sys/cdefs.h>
     35  1.2.4.2  christos 
     36  1.2.4.3    martin __KERNEL_RCSID(1, "$NetBSD: exynos_uart.c,v 1.2.4.3 2020/04/08 14:07:30 martin Exp $");
     37  1.2.4.2  christos 
     38  1.2.4.2  christos #define cn_trap()			\
     39  1.2.4.2  christos 	do {				\
     40  1.2.4.2  christos 		console_debugger();	\
     41  1.2.4.2  christos 		cn_trapped = 1;		\
     42  1.2.4.2  christos 	} while (/* CONSTCOND */ 0)
     43  1.2.4.2  christos 
     44  1.2.4.2  christos #include <sys/param.h>
     45  1.2.4.2  christos #include <sys/bus.h>
     46  1.2.4.2  christos #include <sys/device.h>
     47  1.2.4.2  christos #include <sys/conf.h>
     48  1.2.4.2  christos #include <sys/intr.h>
     49  1.2.4.2  christos #include <sys/systm.h>
     50  1.2.4.2  christos #include <sys/time.h>
     51  1.2.4.2  christos #include <sys/termios.h>
     52  1.2.4.2  christos #include <sys/kauth.h>
     53  1.2.4.2  christos #include <sys/lwp.h>
     54  1.2.4.2  christos #include <sys/tty.h>
     55  1.2.4.2  christos 
     56  1.2.4.2  christos #include <dev/cons.h>
     57  1.2.4.2  christos 
     58  1.2.4.2  christos #include <dev/fdt/fdtvar.h>
     59  1.2.4.2  christos 
     60  1.2.4.2  christos #include <arm/samsung/sscom_reg.h>
     61  1.2.4.2  christos 
     62  1.2.4.2  christos static int	exynos_uart_match(device_t, cfdata_t, void *);
     63  1.2.4.2  christos static void	exynos_uart_attach(device_t, device_t, void *);
     64  1.2.4.2  christos 
     65  1.2.4.2  christos static int	exynos_uart_intr(void *);
     66  1.2.4.2  christos 
     67  1.2.4.2  christos static int	exynos_uart_cngetc(dev_t);
     68  1.2.4.2  christos static void	exynos_uart_cnputc(dev_t, int);
     69  1.2.4.2  christos static void	exynos_uart_cnpollc(dev_t, int);
     70  1.2.4.2  christos 
     71  1.2.4.2  christos static void	exynos_uart_start(struct tty *);
     72  1.2.4.2  christos static int	exynos_uart_param(struct tty *, struct termios *);
     73  1.2.4.2  christos 
     74  1.2.4.2  christos extern struct cfdriver exuart_cd;
     75  1.2.4.2  christos 
     76  1.2.4.2  christos struct exynos_uart_softc {
     77  1.2.4.2  christos 	device_t sc_dev;
     78  1.2.4.2  christos 	bus_space_tag_t	sc_bst;
     79  1.2.4.2  christos 	bus_space_handle_t sc_bsh;
     80  1.2.4.2  christos 	kmutex_t sc_lock;
     81  1.2.4.2  christos 	u_int sc_freq;
     82  1.2.4.2  christos 	void *sc_ih;
     83  1.2.4.2  christos 
     84  1.2.4.2  christos 	bool sc_console;
     85  1.2.4.2  christos 	struct tty *sc_tty;
     86  1.2.4.2  christos 
     87  1.2.4.2  christos 	int sc_ospeed;
     88  1.2.4.2  christos 	tcflag_t sc_cflag;
     89  1.2.4.2  christos 
     90  1.2.4.2  christos 	u_char sc_buf[1024];
     91  1.2.4.2  christos };
     92  1.2.4.2  christos 
     93  1.2.4.2  christos #define	RD4(sc, reg)			\
     94  1.2.4.2  christos 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     95  1.2.4.2  christos #define	WR4(sc, reg, val)		\
     96  1.2.4.2  christos 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     97  1.2.4.2  christos 
     98  1.2.4.2  christos static bus_addr_t exynos_uart_consaddr;
     99  1.2.4.2  christos 
    100  1.2.4.2  christos static struct exynos_uart_softc exynos_uart_cnsc;
    101  1.2.4.2  christos 
    102  1.2.4.2  christos static struct cnm_state exynos_uart_cnm_state;
    103  1.2.4.2  christos 
    104  1.2.4.2  christos struct consdev exynos_uart_consdev = {
    105  1.2.4.2  christos 	.cn_getc = exynos_uart_cngetc,
    106  1.2.4.2  christos 	.cn_putc = exynos_uart_cnputc,
    107  1.2.4.2  christos 	.cn_pollc = exynos_uart_cnpollc,
    108  1.2.4.2  christos 	.cn_dev = NODEV,
    109  1.2.4.2  christos 	.cn_pri = CN_NORMAL,
    110  1.2.4.2  christos };
    111  1.2.4.2  christos 
    112  1.2.4.2  christos static dev_type_open(exynos_uart_open);
    113  1.2.4.2  christos static dev_type_open(exynos_uart_close);
    114  1.2.4.2  christos static dev_type_read(exynos_uart_read);
    115  1.2.4.2  christos static dev_type_write(exynos_uart_write);
    116  1.2.4.2  christos static dev_type_ioctl(exynos_uart_ioctl);
    117  1.2.4.2  christos static dev_type_tty(exynos_uart_tty);
    118  1.2.4.2  christos static dev_type_poll(exynos_uart_poll);
    119  1.2.4.2  christos static dev_type_stop(exynos_uart_stop);
    120  1.2.4.2  christos 
    121  1.2.4.2  christos const struct cdevsw exuart_cdevsw = {
    122  1.2.4.2  christos 	.d_open = exynos_uart_open,
    123  1.2.4.2  christos 	.d_close = exynos_uart_close,
    124  1.2.4.2  christos 	.d_read = exynos_uart_read,
    125  1.2.4.2  christos 	.d_write = exynos_uart_write,
    126  1.2.4.2  christos 	.d_ioctl = exynos_uart_ioctl,
    127  1.2.4.2  christos 	.d_stop = exynos_uart_stop,
    128  1.2.4.2  christos 	.d_tty = exynos_uart_tty,
    129  1.2.4.2  christos 	.d_poll = exynos_uart_poll,
    130  1.2.4.2  christos 	.d_mmap = nommap,
    131  1.2.4.2  christos 	.d_kqfilter = ttykqfilter,
    132  1.2.4.2  christos 	.d_discard = nodiscard,
    133  1.2.4.2  christos 	.d_flag = D_TTY
    134  1.2.4.2  christos };
    135  1.2.4.2  christos 
    136  1.2.4.2  christos static int exynos_uart_cmajor = -1;
    137  1.2.4.2  christos 
    138  1.2.4.2  christos static const char * const compatible[] = {
    139  1.2.4.2  christos 	"samsung,exynos4210-uart",
    140  1.2.4.2  christos 	NULL
    141  1.2.4.2  christos };
    142  1.2.4.2  christos 
    143  1.2.4.2  christos CFATTACH_DECL_NEW(exynos_uart, sizeof(struct exynos_uart_softc),
    144  1.2.4.2  christos 	exynos_uart_match, exynos_uart_attach, NULL, NULL);
    145  1.2.4.2  christos 
    146  1.2.4.2  christos static int
    147  1.2.4.2  christos exynos_uart_match(device_t parent, cfdata_t cf, void *aux)
    148  1.2.4.2  christos {
    149  1.2.4.2  christos 	struct fdt_attach_args * const faa = aux;
    150  1.2.4.2  christos 
    151  1.2.4.2  christos 	return of_match_compatible(faa->faa_phandle, compatible);
    152  1.2.4.2  christos }
    153  1.2.4.2  christos 
    154  1.2.4.2  christos static void
    155  1.2.4.2  christos exynos_uart_attach(device_t parent, device_t self, void *aux)
    156  1.2.4.2  christos {
    157  1.2.4.2  christos 	struct exynos_uart_softc * const sc = device_private(self);
    158  1.2.4.2  christos 	struct fdt_attach_args * const faa = aux;
    159  1.2.4.2  christos 	const int phandle = faa->faa_phandle;
    160  1.2.4.2  christos 	char intrstr[128];
    161  1.2.4.2  christos 	struct clk *clk_uart, *clk_uart_baud0;
    162  1.2.4.2  christos 	struct tty *tp;
    163  1.2.4.2  christos 	int major, minor;
    164  1.2.4.2  christos 	bus_addr_t addr;
    165  1.2.4.2  christos 	bus_size_t size;
    166  1.2.4.2  christos 
    167  1.2.4.2  christos 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    168  1.2.4.2  christos 		aprint_error(": couldn't get registers\n");
    169  1.2.4.2  christos 		return;
    170  1.2.4.2  christos 	}
    171  1.2.4.2  christos 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    172  1.2.4.2  christos 		aprint_error(": failed to decode interrupt\n");
    173  1.2.4.2  christos 		return;
    174  1.2.4.2  christos 	}
    175  1.2.4.2  christos 	clk_uart = fdtbus_clock_get(phandle, "uart");
    176  1.2.4.2  christos 	if (clk_uart == NULL || clk_enable(clk_uart) != 0) {
    177  1.2.4.2  christos 		aprint_error(": failed to enable uart clock\n");
    178  1.2.4.2  christos 		return;
    179  1.2.4.2  christos 	}
    180  1.2.4.2  christos 	clk_uart_baud0 = fdtbus_clock_get(phandle, "clk_uart_baud0");
    181  1.2.4.2  christos 	if (clk_uart_baud0 == NULL || clk_enable(clk_uart_baud0) != 0) {
    182  1.2.4.2  christos 		aprint_error(": failed to enable clk_uart_baud0 clock\n");
    183  1.2.4.2  christos 		return;
    184  1.2.4.2  christos 	}
    185  1.2.4.2  christos 
    186  1.2.4.2  christos 	const bool is_console = exynos_uart_consaddr == addr;
    187  1.2.4.2  christos 
    188  1.2.4.2  christos 	sc->sc_dev = self;
    189  1.2.4.2  christos 	sc->sc_bst = faa->faa_bst;
    190  1.2.4.2  christos 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
    191  1.2.4.2  christos 	sc->sc_console = is_console;
    192  1.2.4.2  christos 	if (is_console) {
    193  1.2.4.2  christos 		sc->sc_bsh = exynos_uart_cnsc.sc_bsh;
    194  1.2.4.2  christos 	} else {
    195  1.2.4.2  christos 		if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    196  1.2.4.2  christos 			aprint_error(": failed to map registers\n");
    197  1.2.4.2  christos 			return;
    198  1.2.4.2  christos 		}
    199  1.2.4.2  christos 	}
    200  1.2.4.2  christos 	sc->sc_freq = clk_get_rate(clk_uart_baud0);
    201  1.2.4.2  christos 
    202  1.2.4.2  christos 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SERIAL,
    203  1.2.4.2  christos 	    0, exynos_uart_intr, sc);
    204  1.2.4.2  christos 	if (sc->sc_ih == NULL) {
    205  1.2.4.2  christos 		aprint_error(": failed to establish interrupt on %s\n",
    206  1.2.4.2  christos 		    intrstr);
    207  1.2.4.2  christos 		return;
    208  1.2.4.2  christos 	}
    209  1.2.4.2  christos 
    210  1.2.4.2  christos 	if (exynos_uart_cmajor == -1) {
    211  1.2.4.2  christos 		/* allocate a major number */
    212  1.2.4.2  christos 		int bmajor = -1, cmajor = -1;
    213  1.2.4.2  christos 		int error = devsw_attach("exuart", NULL, &bmajor,
    214  1.2.4.2  christos 		    &exuart_cdevsw, &cmajor);
    215  1.2.4.2  christos 		if (error) {
    216  1.2.4.2  christos 			aprint_error(": couldn't allocate major number\n");
    217  1.2.4.2  christos 			return;
    218  1.2.4.2  christos 		}
    219  1.2.4.2  christos 		exynos_uart_cmajor = cmajor;
    220  1.2.4.2  christos 	}
    221  1.2.4.2  christos 
    222  1.2.4.2  christos 	major = cdevsw_lookup_major(&exuart_cdevsw);
    223  1.2.4.2  christos 	minor = device_unit(self);
    224  1.2.4.2  christos 
    225  1.2.4.2  christos 	tp = sc->sc_tty = tty_alloc();
    226  1.2.4.2  christos 	tp->t_oproc = exynos_uart_start;
    227  1.2.4.2  christos 	tp->t_param = exynos_uart_param;
    228  1.2.4.2  christos 	tp->t_dev = makedev(major, minor);
    229  1.2.4.2  christos 	tp->t_sc = sc;
    230  1.2.4.2  christos 	tty_attach(tp);
    231  1.2.4.2  christos 
    232  1.2.4.2  christos 	aprint_naive("\n");
    233  1.2.4.2  christos 	if (is_console) {
    234  1.2.4.2  christos 		cn_tab->cn_dev = tp->t_dev;
    235  1.2.4.2  christos 		aprint_normal(": console");
    236  1.2.4.2  christos 	}
    237  1.2.4.2  christos 	aprint_normal("\n");
    238  1.2.4.2  christos 
    239  1.2.4.2  christos 	if (is_console)
    240  1.2.4.2  christos 		delay(10000);
    241  1.2.4.2  christos 
    242  1.2.4.2  christos 	/* Initialize device */
    243  1.2.4.2  christos 	WR4(sc, SSCOM_UFCON,
    244  1.2.4.2  christos 	    __SHIFTIN(2, UFCON_TXTRIGGER) |
    245  1.2.4.2  christos 	    __SHIFTIN(1, UFCON_RXTRIGGER) |
    246  1.2.4.2  christos 	    UFCON_TXFIFO_RESET | UFCON_RXFIFO_RESET |
    247  1.2.4.2  christos 	    UFCON_FIFO_ENABLE);
    248  1.2.4.2  christos 	/* Configure PIO mode with RX timeout interrupts */
    249  1.2.4.2  christos 	WR4(sc, SSCOM_UCON,
    250  1.2.4.2  christos 	    __SHIFTIN(3, UCON_RXTO) |
    251  1.2.4.2  christos 	    UCON_TOINT | UCON_ERRINT |
    252  1.2.4.2  christos 	    UCON_TXMODE_INT | UCON_RXMODE_INT);
    253  1.2.4.2  christos 
    254  1.2.4.2  christos 	/* Disable interrupts */
    255  1.2.4.2  christos 	WR4(sc, SSCOM_UINTM, ~0u);
    256  1.2.4.2  christos 
    257  1.2.4.2  christos 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    258  1.2.4.2  christos }
    259  1.2.4.2  christos 
    260  1.2.4.2  christos static int
    261  1.2.4.2  christos exynos_uart_cngetc(dev_t dev)
    262  1.2.4.2  christos {
    263  1.2.4.2  christos 	struct exynos_uart_softc * const sc = &exynos_uart_cnsc;
    264  1.2.4.2  christos 	uint32_t ufstat;
    265  1.2.4.2  christos 	int s, c;
    266  1.2.4.2  christos 
    267  1.2.4.2  christos 	s = splserial();
    268  1.2.4.2  christos 
    269  1.2.4.2  christos 	ufstat = RD4(sc, SSCOM_UFSTAT);
    270  1.2.4.2  christos 	if (__SHIFTOUT(ufstat, UFSTAT_RXCOUNT) == 0) {
    271  1.2.4.2  christos 		splx(s);
    272  1.2.4.2  christos 		return -1;
    273  1.2.4.2  christos 	}
    274  1.2.4.2  christos 
    275  1.2.4.2  christos 	c = bus_space_read_1(sc->sc_bst, sc->sc_bsh, SSCOM_URXH);
    276  1.2.4.2  christos #if defined(DDB)
    277  1.2.4.2  christos 	extern int db_active;
    278  1.2.4.2  christos 	if (!db_active)
    279  1.2.4.2  christos #endif
    280  1.2.4.2  christos 	{
    281  1.2.4.2  christos 		int cn_trapped __unused = 0;
    282  1.2.4.2  christos 		cn_check_magic(dev, c, exynos_uart_cnm_state);
    283  1.2.4.2  christos 	}
    284  1.2.4.2  christos 
    285  1.2.4.2  christos 	splx(s);
    286  1.2.4.2  christos 
    287  1.2.4.2  christos 	return c & 0xff;
    288  1.2.4.2  christos }
    289  1.2.4.2  christos 
    290  1.2.4.2  christos static void
    291  1.2.4.2  christos exynos_uart_cnputc(dev_t dev, int c)
    292  1.2.4.2  christos {
    293  1.2.4.2  christos 	struct exynos_uart_softc * const sc = &exynos_uart_cnsc;
    294  1.2.4.2  christos 	int s;
    295  1.2.4.2  christos 
    296  1.2.4.2  christos 	s = splserial();
    297  1.2.4.2  christos 	while ((RD4(sc, SSCOM_UFSTAT) & UFSTAT_TXFULL) != 0)
    298  1.2.4.2  christos 		;
    299  1.2.4.2  christos 
    300  1.2.4.2  christos 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, SSCOM_UTXH, c);
    301  1.2.4.2  christos 
    302  1.2.4.2  christos 	splx(s);
    303  1.2.4.2  christos }
    304  1.2.4.3    martin 
    305  1.2.4.2  christos 
    306  1.2.4.2  christos static void
    307  1.2.4.2  christos exynos_uart_cnpollc(dev_t dev, int on)
    308  1.2.4.2  christos {
    309  1.2.4.2  christos }
    310  1.2.4.2  christos 
    311  1.2.4.2  christos static void
    312  1.2.4.2  christos exynos_uart_cnattach(bus_space_tag_t bst, bus_space_handle_t bsh,
    313  1.2.4.2  christos     int ospeed, tcflag_t cflag)
    314  1.2.4.2  christos {
    315  1.2.4.2  christos 	struct exynos_uart_softc *sc = &exynos_uart_cnsc;
    316  1.2.4.2  christos 
    317  1.2.4.2  christos 	cn_tab = &exynos_uart_consdev;
    318  1.2.4.2  christos 	cn_init_magic(&exynos_uart_cnm_state);
    319  1.2.4.2  christos 	cn_set_magic("\047\001");
    320  1.2.4.2  christos 
    321  1.2.4.2  christos 	sc->sc_bst = bst;
    322  1.2.4.2  christos 	sc->sc_bsh = bsh;
    323  1.2.4.2  christos 	sc->sc_ospeed = ospeed;
    324  1.2.4.2  christos 	sc->sc_cflag = cflag;
    325  1.2.4.2  christos }
    326  1.2.4.2  christos 
    327  1.2.4.2  christos static int
    328  1.2.4.2  christos exynos_uart_open(dev_t dev, int flag, int mode, lwp_t *l)
    329  1.2.4.2  christos {
    330  1.2.4.2  christos 	struct exynos_uart_softc *sc =
    331  1.2.4.2  christos 	    device_lookup_private(&exuart_cd, minor(dev));
    332  1.2.4.2  christos 	struct tty *tp = sc->sc_tty;
    333  1.2.4.2  christos 
    334  1.2.4.2  christos 	if (kauth_authorize_device_tty(l->l_cred,
    335  1.2.4.2  christos 	    KAUTH_DEVICE_TTY_OPEN, tp) != 0) {
    336  1.2.4.2  christos 		return EBUSY;
    337  1.2.4.2  christos 	}
    338  1.2.4.2  christos 
    339  1.2.4.2  christos 	mutex_enter(&sc->sc_lock);
    340  1.2.4.2  christos 
    341  1.2.4.2  christos 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
    342  1.2.4.2  christos 		tp->t_dev = dev;
    343  1.2.4.2  christos 		ttychars(tp);
    344  1.2.4.2  christos 		tp->t_iflag = TTYDEF_IFLAG;
    345  1.2.4.2  christos 		tp->t_oflag = TTYDEF_OFLAG;
    346  1.2.4.2  christos 		tp->t_lflag = TTYDEF_LFLAG;
    347  1.2.4.2  christos 		if (sc->sc_console) {
    348  1.2.4.2  christos 			tp->t_ispeed = tp->t_ospeed = exynos_uart_cnsc.sc_ospeed;
    349  1.2.4.2  christos 			tp->t_cflag = exynos_uart_cnsc.sc_cflag;
    350  1.2.4.2  christos 		} else {
    351  1.2.4.2  christos 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    352  1.2.4.2  christos 			tp->t_cflag = TTYDEF_CFLAG;
    353  1.2.4.2  christos 		}
    354  1.2.4.2  christos 		ttsetwater(tp);
    355  1.2.4.2  christos 	}
    356  1.2.4.2  christos 	tp->t_state |= TS_CARR_ON;
    357  1.2.4.2  christos 
    358  1.2.4.2  christos 	/* Enable RX and error interrupts */
    359  1.2.4.2  christos 	WR4(sc, SSCOM_UINTM, ~0u & ~(UINT_RXD|UINT_ERROR));
    360  1.2.4.2  christos 
    361  1.2.4.2  christos 	mutex_exit(&sc->sc_lock);
    362  1.2.4.2  christos 
    363  1.2.4.2  christos 	return tp->t_linesw->l_open(dev, tp);
    364  1.2.4.2  christos }
    365  1.2.4.2  christos 
    366  1.2.4.2  christos static int
    367  1.2.4.2  christos exynos_uart_close(dev_t dev, int flag, int mode, lwp_t *l)
    368  1.2.4.2  christos {
    369  1.2.4.2  christos 	struct exynos_uart_softc *sc =
    370  1.2.4.2  christos 	    device_lookup_private(&exuart_cd, minor(dev));
    371  1.2.4.2  christos 	struct tty *tp = sc->sc_tty;
    372  1.2.4.2  christos 
    373  1.2.4.2  christos 	mutex_enter(&sc->sc_lock);
    374  1.2.4.2  christos 
    375  1.2.4.2  christos 	tp->t_linesw->l_close(tp, flag);
    376  1.2.4.2  christos 	ttyclose(tp);
    377  1.2.4.2  christos 
    378  1.2.4.2  christos 	/* Disable interrupts */
    379  1.2.4.2  christos 	WR4(sc, SSCOM_UINTM, ~0u);
    380  1.2.4.2  christos 
    381  1.2.4.2  christos 	mutex_exit(&sc->sc_lock);
    382  1.2.4.2  christos 
    383  1.2.4.2  christos 	return 0;
    384  1.2.4.2  christos }
    385  1.2.4.2  christos 
    386  1.2.4.2  christos static int
    387  1.2.4.2  christos exynos_uart_read(dev_t dev, struct uio *uio, int flag)
    388  1.2.4.2  christos {
    389  1.2.4.2  christos 	struct exynos_uart_softc *sc =
    390  1.2.4.2  christos 	    device_lookup_private(&exuart_cd, minor(dev));
    391  1.2.4.2  christos 	struct tty *tp = sc->sc_tty;
    392  1.2.4.2  christos 
    393  1.2.4.2  christos 	return tp->t_linesw->l_read(tp, uio, flag);
    394  1.2.4.2  christos }
    395  1.2.4.2  christos 
    396  1.2.4.2  christos static int
    397  1.2.4.2  christos exynos_uart_write(dev_t dev, struct uio *uio, int flag)
    398  1.2.4.2  christos {
    399  1.2.4.2  christos 	struct exynos_uart_softc *sc =
    400  1.2.4.2  christos 	    device_lookup_private(&exuart_cd, minor(dev));
    401  1.2.4.2  christos 	struct tty *tp = sc->sc_tty;
    402  1.2.4.2  christos 
    403  1.2.4.2  christos 	return tp->t_linesw->l_write(tp, uio, flag);
    404  1.2.4.2  christos }
    405  1.2.4.2  christos 
    406  1.2.4.2  christos static int
    407  1.2.4.2  christos exynos_uart_poll(dev_t dev, int events, lwp_t *l)
    408  1.2.4.2  christos {
    409  1.2.4.2  christos 	struct exynos_uart_softc *sc =
    410  1.2.4.2  christos 	    device_lookup_private(&exuart_cd, minor(dev));
    411  1.2.4.2  christos 	struct tty *tp = sc->sc_tty;
    412  1.2.4.2  christos 
    413  1.2.4.2  christos 	return tp->t_linesw->l_poll(tp, events, l);
    414  1.2.4.2  christos }
    415  1.2.4.2  christos 
    416  1.2.4.2  christos static int
    417  1.2.4.2  christos exynos_uart_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    418  1.2.4.2  christos {
    419  1.2.4.2  christos 	struct exynos_uart_softc *sc =
    420  1.2.4.2  christos 	    device_lookup_private(&exuart_cd, minor(dev));
    421  1.2.4.2  christos 	struct tty *tp = sc->sc_tty;
    422  1.2.4.2  christos 	int error;
    423  1.2.4.2  christos 
    424  1.2.4.2  christos 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
    425  1.2.4.2  christos 	if (error != EPASSTHROUGH)
    426  1.2.4.2  christos 		return error;
    427  1.2.4.2  christos 
    428  1.2.4.2  christos 	return ttioctl(tp, cmd, data, flag, l);
    429  1.2.4.2  christos }
    430  1.2.4.2  christos 
    431  1.2.4.2  christos static struct tty *
    432  1.2.4.2  christos exynos_uart_tty(dev_t dev)
    433  1.2.4.2  christos {
    434  1.2.4.2  christos 	struct exynos_uart_softc *sc =
    435  1.2.4.2  christos 	    device_lookup_private(&exuart_cd, minor(dev));
    436  1.2.4.2  christos 
    437  1.2.4.2  christos 	return sc->sc_tty;
    438  1.2.4.2  christos }
    439  1.2.4.2  christos 
    440  1.2.4.2  christos static void
    441  1.2.4.2  christos exynos_uart_stop(struct tty *tp, int flag)
    442  1.2.4.2  christos {
    443  1.2.4.2  christos }
    444  1.2.4.2  christos 
    445  1.2.4.2  christos static void
    446  1.2.4.2  christos exynos_uart_start(struct tty *tp)
    447  1.2.4.2  christos {
    448  1.2.4.2  christos 	struct exynos_uart_softc *sc = tp->t_sc;
    449  1.2.4.2  christos 	u_char *p = sc->sc_buf;
    450  1.2.4.2  christos 	int s, brem;
    451  1.2.4.2  christos 
    452  1.2.4.2  christos 	s = spltty();
    453  1.2.4.2  christos 
    454  1.2.4.2  christos 	if (tp->t_state & (TS_TTSTOP | TS_BUSY | TS_TIMEOUT)) {
    455  1.2.4.2  christos 		splx(s);
    456  1.2.4.2  christos 		return;
    457  1.2.4.2  christos 	}
    458  1.2.4.2  christos 	tp->t_state |= TS_BUSY;
    459  1.2.4.2  christos 
    460  1.2.4.2  christos 	for (brem = q_to_b(&tp->t_outq, sc->sc_buf, sizeof(sc->sc_buf));
    461  1.2.4.2  christos 	     brem > 0;
    462  1.2.4.2  christos 	     brem--, p++) {
    463  1.2.4.2  christos 		while ((RD4(sc, SSCOM_UFSTAT) & UFSTAT_TXFULL) != 0)
    464  1.2.4.2  christos 			;
    465  1.2.4.2  christos 
    466  1.2.4.2  christos 		bus_space_write_1(sc->sc_bst, sc->sc_bsh,
    467  1.2.4.2  christos 		    SSCOM_UTXH, *p);
    468  1.2.4.2  christos 	}
    469  1.2.4.2  christos 
    470  1.2.4.2  christos 	tp->t_state &= ~TS_BUSY;
    471  1.2.4.2  christos 	if (ttypull(tp)) {
    472  1.2.4.2  christos 		tp->t_state |= TS_TIMEOUT;
    473  1.2.4.2  christos 		callout_schedule(&tp->t_rstrt_ch, 1);
    474  1.2.4.2  christos 	}
    475  1.2.4.2  christos 	splx(s);
    476  1.2.4.2  christos }
    477  1.2.4.2  christos 
    478  1.2.4.2  christos static int
    479  1.2.4.2  christos exynos_uart_param(struct tty *tp, struct termios *t)
    480  1.2.4.2  christos {
    481  1.2.4.2  christos 	struct exynos_uart_softc *sc = tp->t_sc;
    482  1.2.4.2  christos 
    483  1.2.4.2  christos 	mutex_enter(&sc->sc_lock);
    484  1.2.4.2  christos 
    485  1.2.4.2  christos 	if (tp->t_cflag != t->c_cflag) {
    486  1.2.4.2  christos 		uint32_t ulcon = 0;
    487  1.2.4.2  christos 		switch (ISSET(t->c_cflag, CSIZE)) {
    488  1.2.4.2  christos 		case CS5:
    489  1.2.4.2  christos 			ulcon |= ULCON_LENGTH_5;
    490  1.2.4.2  christos 			break;
    491  1.2.4.2  christos 		case CS6:
    492  1.2.4.2  christos 			ulcon |= ULCON_LENGTH_6;
    493  1.2.4.2  christos 			break;
    494  1.2.4.2  christos 		case CS7:
    495  1.2.4.2  christos 			ulcon |= ULCON_LENGTH_7;
    496  1.2.4.2  christos 			break;
    497  1.2.4.2  christos 		case CS8:
    498  1.2.4.2  christos 			ulcon |= ULCON_LENGTH_8;
    499  1.2.4.2  christos 			break;
    500  1.2.4.2  christos 		}
    501  1.2.4.2  christos 		switch (ISSET(t->c_cflag, PARENB|PARODD)) {
    502  1.2.4.2  christos 		case PARENB|PARODD:
    503  1.2.4.2  christos 			ulcon |= ULCON_PARITY_ODD;
    504  1.2.4.2  christos 			break;
    505  1.2.4.2  christos 		case PARENB:
    506  1.2.4.2  christos 			ulcon |= ULCON_PARITY_EVEN;
    507  1.2.4.2  christos 			break;
    508  1.2.4.2  christos 		default:
    509  1.2.4.2  christos 			ulcon |= ULCON_PARITY_NONE;
    510  1.2.4.2  christos 			break;
    511  1.2.4.2  christos 		}
    512  1.2.4.2  christos 		if (ISSET(t->c_cflag, CSTOPB))
    513  1.2.4.2  christos 			ulcon |= ULCON_STOP;
    514  1.2.4.2  christos 		WR4(sc, SSCOM_ULCON, ulcon);
    515  1.2.4.2  christos 	}
    516  1.2.4.2  christos 
    517  1.2.4.2  christos 	if (tp->t_ospeed != t->c_ospeed) {
    518  1.2.4.2  christos 		const uint32_t ubrdiv = (sc->sc_freq / 16) / t->c_ospeed - 1;
    519  1.2.4.2  christos 		WR4(sc, SSCOM_UBRDIV, ubrdiv);
    520  1.2.4.2  christos 	}
    521  1.2.4.2  christos 
    522  1.2.4.2  christos 	tp->t_ispeed = t->c_ispeed;
    523  1.2.4.2  christos 	tp->t_ospeed = t->c_ospeed;
    524  1.2.4.2  christos 	tp->t_cflag = t->c_cflag;
    525  1.2.4.2  christos 
    526  1.2.4.2  christos 	mutex_exit(&sc->sc_lock);
    527  1.2.4.2  christos 
    528  1.2.4.2  christos 	return 0;
    529  1.2.4.2  christos }
    530  1.2.4.2  christos 
    531  1.2.4.2  christos static int
    532  1.2.4.2  christos exynos_uart_intr(void *priv)
    533  1.2.4.2  christos {
    534  1.2.4.2  christos 	struct exynos_uart_softc *sc = priv;
    535  1.2.4.2  christos 	struct tty *tp = sc->sc_tty;
    536  1.2.4.2  christos 	uint32_t uintp, uerstat, ufstat, c;
    537  1.2.4.2  christos 
    538  1.2.4.2  christos 	mutex_enter(&sc->sc_lock);
    539  1.2.4.2  christos 
    540  1.2.4.2  christos 	uintp = RD4(sc, SSCOM_UINTP);
    541  1.2.4.2  christos 
    542  1.2.4.2  christos 	for (;;) {
    543  1.2.4.2  christos 		int cn_trapped = 0;
    544  1.2.4.2  christos 
    545  1.2.4.2  christos 		uerstat = RD4(sc, SSCOM_UERSTAT);
    546  1.2.4.2  christos 		if (uerstat & UERSTAT_BREAK) {
    547  1.2.4.2  christos 			cn_check_magic(tp->t_dev, CNC_BREAK,
    548  1.2.4.2  christos 			    exynos_uart_cnm_state);
    549  1.2.4.2  christos 			if (cn_trapped)
    550  1.2.4.2  christos 				continue;
    551  1.2.4.2  christos 		}
    552  1.2.4.2  christos 
    553  1.2.4.2  christos 		ufstat = RD4(sc, SSCOM_UFSTAT);
    554  1.2.4.2  christos 		if (__SHIFTOUT(ufstat, UFSTAT_RXCOUNT) == 0) {
    555  1.2.4.2  christos 			break;
    556  1.2.4.2  christos 		}
    557  1.2.4.2  christos 
    558  1.2.4.2  christos 		c = bus_space_read_1(sc->sc_bst, sc->sc_bsh, SSCOM_URXH);
    559  1.2.4.2  christos 		cn_check_magic(tp->t_dev, c & 0xff, exynos_uart_cnm_state);
    560  1.2.4.2  christos 		if (cn_trapped)
    561  1.2.4.2  christos 			continue;
    562  1.2.4.2  christos 		tp->t_linesw->l_rint(c & 0xff, tp);
    563  1.2.4.2  christos 	}
    564  1.2.4.2  christos 
    565  1.2.4.2  christos 	WR4(sc, SSCOM_UINTP, uintp);
    566  1.2.4.2  christos 
    567  1.2.4.2  christos 	mutex_exit(&sc->sc_lock);
    568  1.2.4.2  christos 
    569  1.2.4.2  christos 	return 1;
    570  1.2.4.2  christos }
    571  1.2.4.2  christos 
    572  1.2.4.2  christos /*
    573  1.2.4.2  christos  * Console support
    574  1.2.4.2  christos  */
    575  1.2.4.2  christos 
    576  1.2.4.2  christos static int
    577  1.2.4.2  christos exynos_uart_console_match(int phandle)
    578  1.2.4.2  christos {
    579  1.2.4.2  christos 	return of_match_compatible(phandle, compatible);
    580  1.2.4.2  christos }
    581  1.2.4.2  christos 
    582  1.2.4.2  christos static void
    583  1.2.4.2  christos exynos_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
    584  1.2.4.2  christos {
    585  1.2.4.2  christos 	const int phandle = faa->faa_phandle;
    586  1.2.4.2  christos 	bus_space_tag_t bst = faa->faa_bst;
    587  1.2.4.2  christos 	bus_space_handle_t bsh;
    588  1.2.4.2  christos 	bus_addr_t addr;
    589  1.2.4.2  christos 	bus_size_t size;
    590  1.2.4.2  christos 	tcflag_t flags;
    591  1.2.4.2  christos 	int speed;
    592  1.2.4.2  christos 
    593  1.2.4.2  christos 	speed = fdtbus_get_stdout_speed();
    594  1.2.4.2  christos 	if (speed < 0)
    595  1.2.4.2  christos 		speed = 115200; /* default */
    596  1.2.4.2  christos 	flags = fdtbus_get_stdout_flags();
    597  1.2.4.2  christos 
    598  1.2.4.2  christos 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0)
    599  1.2.4.2  christos 		panic("exynos_uart: couldn't get registers");
    600  1.2.4.2  christos 	if (bus_space_map(bst, addr, size, 0, &bsh) != 0)
    601  1.2.4.2  christos 		panic("exynos_uart: couldn't map registers");
    602  1.2.4.2  christos 
    603  1.2.4.2  christos 	exynos_uart_consaddr = addr;
    604  1.2.4.2  christos 
    605  1.2.4.2  christos 	exynos_uart_cnattach(bst, bsh, speed, flags);
    606  1.2.4.2  christos }
    607  1.2.4.2  christos 
    608  1.2.4.2  christos static const struct fdt_console exynos_uart_console = {
    609  1.2.4.2  christos 	.match = exynos_uart_console_match,
    610  1.2.4.2  christos 	.consinit = exynos_uart_console_consinit,
    611  1.2.4.2  christos };
    612  1.2.4.2  christos 
    613  1.2.4.2  christos FDT_CONSOLE(exynos_uart, &exynos_uart_console);
    614