Home | History | Annotate | Line # | Download | only in ingenic
ingenic_com.c revision 1.5
      1  1.5  macallan /*	$NetBSD: ingenic_com.c,v 1.5 2015/07/11 19:00:04 macallan 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.5  macallan __KERNEL_RCSID(0, "$NetBSD: ingenic_com.c,v 1.5 2015/07/11 19:00:04 macallan 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 #include "opt_com.h"
     52  1.1  macallan 
     53  1.5  macallan #ifndef COM_REGMAP
     54  1.5  macallan #error We need COM_REGMAP
     55  1.5  macallan #endif
     56  1.5  macallan 
     57  1.1  macallan volatile int32_t *com0addr = (int32_t *)MIPS_PHYS_TO_KSEG1(JZ_UART0);
     58  1.1  macallan 
     59  1.1  macallan void	ingenic_putchar_init(void);
     60  1.1  macallan void	ingenic_puts(const char *);
     61  1.1  macallan void	ingenic_putchar(char);
     62  1.1  macallan 
     63  1.1  macallan #ifndef CONMODE
     64  1.4  macallan # define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
     65  1.1  macallan #endif
     66  1.1  macallan 
     67  1.1  macallan 
     68  1.1  macallan void		ingenic_com_cnattach(void);
     69  1.1  macallan 
     70  1.1  macallan static int	ingenic_com_match(device_t, cfdata_t , void *);
     71  1.1  macallan static void	ingenic_com_attach(device_t, device_t, void *);
     72  1.1  macallan 
     73  1.1  macallan struct ingenic_com_softc {
     74  1.1  macallan 	struct com_softc sc_com;
     75  1.5  macallan 	bus_space_tag_t sc_tag;
     76  1.5  macallan 	bus_space_handle_t sc_regh;
     77  1.1  macallan };
     78  1.1  macallan 
     79  1.5  macallan CFATTACH_DECL_NEW(ingenic_com, sizeof(struct ingenic_com_softc),
     80  1.1  macallan     ingenic_com_match, ingenic_com_attach, NULL, NULL);
     81  1.1  macallan 
     82  1.5  macallan static bus_space_handle_t regh = 0;
     83  1.5  macallan static bus_addr_t cons_com = 0;
     84  1.5  macallan static struct com_regs regs;
     85  1.5  macallan extern bus_space_tag_t apbus_memt;
     86  1.1  macallan 
     87  1.1  macallan void
     88  1.1  macallan ingenic_putchar_init(void)
     89  1.1  macallan {
     90  1.1  macallan 	/*
     91  1.1  macallan 	 * XXX don't screw with the UART's speed until we know what clock
     92  1.1  macallan 	 * we're on
     93  1.1  macallan 	 */
     94  1.1  macallan #if 0
     95  1.1  macallan 	int rate;
     96  1.1  macallan #endif
     97  1.1  macallan 	extern int comspeed(long, long, int);
     98  1.1  macallan 
     99  1.1  macallan 	com0addr = (uint32_t *)MIPS_PHYS_TO_KSEG1(JZ_UART0);
    100  1.1  macallan #if 0
    101  1.1  macallan 	if (comcnfreq != -1) {
    102  1.1  macallan 		rate = comspeed(comcnspeed, comcnfreq, COM_TYPE_INGENIC);
    103  1.1  macallan 		if (rate < 0)
    104  1.1  macallan 			return;					/* XXX */
    105  1.1  macallan #endif
    106  1.1  macallan 		com0addr[com_ier] = 0;
    107  1.1  macallan 		com0addr[com_lctl] = htole32(LCR_DLAB);
    108  1.1  macallan #if 0
    109  1.1  macallan 		com0addr[com_dlbl] = htole32(rate & 0xff);
    110  1.1  macallan 		com0addr[com_dlbh] = htole32(rate >> 8);
    111  1.1  macallan #endif
    112  1.1  macallan 		com0addr[com_lctl] = htole32(LCR_8BITS);	/* XXX */
    113  1.1  macallan 		com0addr[com_mcr]  = htole32(MCR_DTR|MCR_RTS);
    114  1.1  macallan 		com0addr[com_fifo] = htole32(
    115  1.1  macallan 		    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
    116  1.1  macallan 		    FIFO_TRIGGER_1 | FIFO_UART_ON);
    117  1.1  macallan #if 0
    118  1.1  macallan 	}
    119  1.1  macallan #endif
    120  1.1  macallan }
    121  1.1  macallan 
    122  1.1  macallan 
    123  1.1  macallan void
    124  1.1  macallan ingenic_putchar(char c)
    125  1.1  macallan {
    126  1.1  macallan 	int timo = 150000;
    127  1.1  macallan 
    128  1.1  macallan 	while ((le32toh(com0addr[com_lsr]) & LSR_TXRDY) == 0)
    129  1.1  macallan 		if (--timo == 0)
    130  1.1  macallan 			break;
    131  1.1  macallan 
    132  1.1  macallan 	com0addr[com_data] = htole32((uint32_t)c);
    133  1.1  macallan 
    134  1.1  macallan 	while ((le32toh(com0addr[com_lsr]) & LSR_TSRE) == 0)
    135  1.1  macallan 		if (--timo == 0)
    136  1.1  macallan 			break;
    137  1.1  macallan }
    138  1.1  macallan 
    139  1.1  macallan void
    140  1.1  macallan ingenic_puts(const char *restrict s)
    141  1.1  macallan {
    142  1.1  macallan 	char c;
    143  1.1  macallan 
    144  1.1  macallan 	while ((c = *s++) != 0)
    145  1.1  macallan 		ingenic_putchar(c);
    146  1.1  macallan }
    147  1.1  macallan 
    148  1.1  macallan void
    149  1.1  macallan ingenic_com_cnattach(void)
    150  1.1  macallan {
    151  1.5  macallan 	int i;
    152  1.1  macallan 
    153  1.5  macallan 	bus_space_map(apbus_memt, JZ_UART0, 0x100, 0, &regh);
    154  1.5  macallan 	cons_com = JZ_UART0;
    155  1.1  macallan 	memset(&regs, 0, sizeof(regs));
    156  1.5  macallan 	COM_INIT_REGS(regs, apbus_memt, regh, JZ_UART0);
    157  1.5  macallan 	for (i = 0; i < 16; i++) {
    158  1.5  macallan 		regs.cr_map[i] = regs.cr_map[i] << 2;
    159  1.5  macallan 	}
    160  1.5  macallan 	regs.cr_nports = 32;
    161  1.1  macallan 
    162  1.4  macallan 	comcnattach1(&regs, 115200, 48000000, COM_TYPE_INGENIC, CONMODE);
    163  1.1  macallan }
    164  1.1  macallan 
    165  1.1  macallan static int
    166  1.1  macallan ingenic_com_match(device_t parent, cfdata_t cfdata, void *args)
    167  1.1  macallan {
    168  1.1  macallan 	struct mainbusdev {
    169  1.1  macallan 		const char *md_name;
    170  1.1  macallan 	} *aa = args;
    171  1.1  macallan 	if (strcmp(aa->md_name, "com") == 0) return 1;
    172  1.1  macallan 	return 0;
    173  1.1  macallan }
    174  1.1  macallan 
    175  1.1  macallan 
    176  1.1  macallan static void
    177  1.1  macallan ingenic_com_attach(device_t parent, device_t self, void *args)
    178  1.1  macallan {
    179  1.1  macallan 	struct ingenic_com_softc *isc = device_private(self);
    180  1.1  macallan 	struct com_softc *sc = &isc->sc_com;
    181  1.5  macallan 	struct apbus_attach_args *aa = args;
    182  1.5  macallan 	int i;
    183  1.1  macallan 
    184  1.1  macallan 	sc->sc_dev = self;
    185  1.4  macallan 	sc->sc_frequency = 48000000;
    186  1.1  macallan 	sc->sc_type = COM_TYPE_INGENIC;
    187  1.5  macallan 	isc->sc_tag = aa->aa_bst;
    188  1.5  macallan 
    189  1.5  macallan 	if (cons_com == aa->aa_addr) {
    190  1.5  macallan 		isc->sc_regh = regh;
    191  1.5  macallan 	} else {
    192  1.5  macallan 		bus_space_map(apbus_memt, aa->aa_addr, 0x1000, 0, &isc->sc_regh);
    193  1.5  macallan 	}
    194  1.1  macallan 	memset(&sc->sc_regs, 0, sizeof(sc->sc_regs));
    195  1.5  macallan 	COM_INIT_REGS(sc->sc_regs, aa->aa_bst, isc->sc_regh, aa->aa_addr);
    196  1.5  macallan 	for (i = 0; i < 16; i++)
    197  1.5  macallan 		sc->sc_regs.cr_map[i] = sc->sc_regs.cr_map[i] << 2;
    198  1.5  macallan 	sc->sc_regs.cr_nports = 32;
    199  1.5  macallan 
    200  1.1  macallan 	com_attach_subr(sc);
    201  1.5  macallan 	evbmips_intr_establish(aa->aa_irq, comintr, sc);
    202  1.1  macallan }
    203