Home | History | Annotate | Line # | Download | only in ingenic
      1  1.9   thorpej /*	$NetBSD: ingenic_com.c,v 1.9 2018/12/11 06:34:00 thorpej Exp $ */
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.1  macallan  * Copyright (c) 2014 Michael Lorenz
      5  1.1  macallan  * All rights reserved.
      6  1.1  macallan  *
      7  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8  1.1  macallan  * modification, are permitted provided that the following conditions
      9  1.1  macallan  * are met:
     10  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15  1.1  macallan  *
     16  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  macallan  */
     28  1.1  macallan 
     29  1.1  macallan #include <sys/cdefs.h>
     30  1.9   thorpej __KERNEL_RCSID(0, "$NetBSD: ingenic_com.c,v 1.9 2018/12/11 06:34:00 thorpej Exp $");
     31  1.1  macallan 
     32  1.1  macallan #include <sys/param.h>
     33  1.1  macallan #include <sys/systm.h>
     34  1.1  macallan #include <sys/device.h>
     35  1.1  macallan #include <sys/kernel.h>
     36  1.1  macallan #include <sys/termios.h>
     37  1.1  macallan #include <sys/ttydefaults.h>
     38  1.1  macallan #include <sys/types.h>
     39  1.1  macallan 
     40  1.1  macallan #include <sys/bus.h>
     41  1.1  macallan 
     42  1.1  macallan #include <dev/cons.h>
     43  1.1  macallan #include <dev/ic/comreg.h>
     44  1.1  macallan #include <dev/ic/comvar.h>
     45  1.1  macallan 
     46  1.1  macallan #include <mips/cpuregs.h>
     47  1.1  macallan 
     48  1.5  macallan #include <mips/ingenic/ingenic_var.h>
     49  1.1  macallan #include <mips/ingenic/ingenic_regs.h>
     50  1.1  macallan 
     51  1.1  macallan volatile int32_t *com0addr = (int32_t *)MIPS_PHYS_TO_KSEG1(JZ_UART0);
     52  1.1  macallan 
     53  1.1  macallan void	ingenic_putchar_init(void);
     54  1.1  macallan void	ingenic_puts(const char *);
     55  1.1  macallan void	ingenic_putchar(char);
     56  1.1  macallan 
     57  1.1  macallan #ifndef CONMODE
     58  1.4  macallan # define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
     59  1.1  macallan #endif
     60  1.1  macallan 
     61  1.1  macallan void		ingenic_com_cnattach(void);
     62  1.1  macallan 
     63  1.1  macallan static int	ingenic_com_match(device_t, cfdata_t , void *);
     64  1.1  macallan static void	ingenic_com_attach(device_t, device_t, void *);
     65  1.1  macallan 
     66  1.1  macallan struct ingenic_com_softc {
     67  1.1  macallan 	struct com_softc sc_com;
     68  1.5  macallan 	bus_space_tag_t sc_tag;
     69  1.5  macallan 	bus_space_handle_t sc_regh;
     70  1.1  macallan };
     71  1.1  macallan 
     72  1.5  macallan CFATTACH_DECL_NEW(ingenic_com, sizeof(struct ingenic_com_softc),
     73  1.1  macallan     ingenic_com_match, ingenic_com_attach, NULL, NULL);
     74  1.1  macallan 
     75  1.5  macallan static bus_space_handle_t regh = 0;
     76  1.5  macallan static bus_addr_t cons_com = 0;
     77  1.7   thorpej static struct com_regs cons_regs;
     78  1.5  macallan extern bus_space_tag_t apbus_memt;
     79  1.1  macallan 
     80  1.7   thorpej static void
     81  1.7   thorpej ingenic_com_init_regs(struct com_regs *regs, bus_space_tag_t st,
     82  1.7   thorpej 		      bus_space_handle_t sh, bus_addr_t addr)
     83  1.7   thorpej {
     84  1.7   thorpej 
     85  1.9   thorpej 	com_init_regs_stride(regs, st, sh, addr, 2);
     86  1.7   thorpej }
     87  1.7   thorpej 
     88  1.1  macallan void
     89  1.1  macallan ingenic_putchar_init(void)
     90  1.1  macallan {
     91  1.1  macallan 	/*
     92  1.1  macallan 	 * XXX don't screw with the UART's speed until we know what clock
     93  1.1  macallan 	 * we're on
     94  1.1  macallan 	 */
     95  1.1  macallan #if 0
     96  1.1  macallan 	int rate;
     97  1.1  macallan #endif
     98  1.1  macallan 	extern int comspeed(long, long, int);
     99  1.1  macallan 
    100  1.1  macallan 	com0addr = (uint32_t *)MIPS_PHYS_TO_KSEG1(JZ_UART0);
    101  1.1  macallan #if 0
    102  1.1  macallan 	if (comcnfreq != -1) {
    103  1.1  macallan 		rate = comspeed(comcnspeed, comcnfreq, COM_TYPE_INGENIC);
    104  1.1  macallan 		if (rate < 0)
    105  1.1  macallan 			return;					/* XXX */
    106  1.1  macallan #endif
    107  1.1  macallan 		com0addr[com_ier] = 0;
    108  1.1  macallan 		com0addr[com_lctl] = htole32(LCR_DLAB);
    109  1.1  macallan #if 0
    110  1.1  macallan 		com0addr[com_dlbl] = htole32(rate & 0xff);
    111  1.1  macallan 		com0addr[com_dlbh] = htole32(rate >> 8);
    112  1.1  macallan #endif
    113  1.1  macallan 		com0addr[com_lctl] = htole32(LCR_8BITS);	/* XXX */
    114  1.1  macallan 		com0addr[com_mcr]  = htole32(MCR_DTR|MCR_RTS);
    115  1.1  macallan 		com0addr[com_fifo] = htole32(
    116  1.6     skrll 		    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
    117  1.1  macallan 		    FIFO_TRIGGER_1 | FIFO_UART_ON);
    118  1.1  macallan #if 0
    119  1.1  macallan 	}
    120  1.1  macallan #endif
    121  1.1  macallan }
    122  1.1  macallan 
    123  1.1  macallan 
    124  1.1  macallan void
    125  1.1  macallan ingenic_putchar(char c)
    126  1.1  macallan {
    127  1.1  macallan 	int timo = 150000;
    128  1.1  macallan 
    129  1.1  macallan 	while ((le32toh(com0addr[com_lsr]) & LSR_TXRDY) == 0)
    130  1.1  macallan 		if (--timo == 0)
    131  1.1  macallan 			break;
    132  1.1  macallan 
    133  1.1  macallan 	com0addr[com_data] = htole32((uint32_t)c);
    134  1.1  macallan 
    135  1.1  macallan 	while ((le32toh(com0addr[com_lsr]) & LSR_TSRE) == 0)
    136  1.1  macallan 		if (--timo == 0)
    137  1.1  macallan 			break;
    138  1.1  macallan }
    139  1.1  macallan 
    140  1.1  macallan void
    141  1.1  macallan ingenic_puts(const char *restrict s)
    142  1.1  macallan {
    143  1.1  macallan 	char c;
    144  1.1  macallan 
    145  1.1  macallan 	while ((c = *s++) != 0)
    146  1.1  macallan 		ingenic_putchar(c);
    147  1.1  macallan }
    148  1.1  macallan 
    149  1.1  macallan void
    150  1.1  macallan ingenic_com_cnattach(void)
    151  1.1  macallan {
    152  1.6     skrll 
    153  1.5  macallan 	bus_space_map(apbus_memt, JZ_UART0, 0x100, 0, &regh);
    154  1.5  macallan 	cons_com = JZ_UART0;
    155  1.7   thorpej 	ingenic_com_init_regs(&cons_regs, apbus_memt, regh, JZ_UART0);
    156  1.1  macallan 
    157  1.7   thorpej 	comcnattach1(&cons_regs, 115200, 48000000, COM_TYPE_INGENIC, CONMODE);
    158  1.1  macallan }
    159  1.1  macallan 
    160  1.1  macallan static int
    161  1.1  macallan ingenic_com_match(device_t parent, cfdata_t cfdata, void *args)
    162  1.1  macallan {
    163  1.1  macallan 	struct mainbusdev {
    164  1.1  macallan 		const char *md_name;
    165  1.1  macallan 	} *aa = args;
    166  1.1  macallan 	if (strcmp(aa->md_name, "com") == 0) return 1;
    167  1.1  macallan 	return 0;
    168  1.1  macallan }
    169  1.1  macallan 
    170  1.1  macallan 
    171  1.1  macallan static void
    172  1.1  macallan ingenic_com_attach(device_t parent, device_t self, void *args)
    173  1.1  macallan {
    174  1.1  macallan 	struct ingenic_com_softc *isc = device_private(self);
    175  1.1  macallan 	struct com_softc *sc = &isc->sc_com;
    176  1.5  macallan 	struct apbus_attach_args *aa = args;
    177  1.1  macallan 
    178  1.1  macallan 	sc->sc_dev = self;
    179  1.4  macallan 	sc->sc_frequency = 48000000;
    180  1.1  macallan 	sc->sc_type = COM_TYPE_INGENIC;
    181  1.5  macallan 	isc->sc_tag = aa->aa_bst;
    182  1.5  macallan 
    183  1.5  macallan 	if (cons_com == aa->aa_addr) {
    184  1.5  macallan 		isc->sc_regh = regh;
    185  1.5  macallan 	} else {
    186  1.5  macallan 		bus_space_map(apbus_memt, aa->aa_addr, 0x1000, 0, &isc->sc_regh);
    187  1.5  macallan 	}
    188  1.7   thorpej 	ingenic_com_init_regs(&sc->sc_regs, aa->aa_bst, isc->sc_regh,
    189  1.7   thorpej 			      aa->aa_addr);
    190  1.5  macallan 
    191  1.1  macallan 	com_attach_subr(sc);
    192  1.5  macallan 	evbmips_intr_establish(aa->aa_irq, comintr, sc);
    193  1.1  macallan }
    194