Home | History | Annotate | Line # | Download | only in ingenic
ingenic_com.c revision 1.1.2.1
      1  1.1.2.1     skrll /*	$NetBSD: ingenic_com.c,v 1.1.2.1 2015/04/06 15:17:59 skrll 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.1.2.1     skrll __KERNEL_RCSID(0, "$NetBSD: ingenic_com.c,v 1.1.2.1 2015/04/06 15:17:59 skrll 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.1  macallan #include <mips/ingenic/ingenic_regs.h>
     49      1.1  macallan 
     50      1.1  macallan #include "opt_com.h"
     51      1.1  macallan 
     52      1.1  macallan volatile int32_t *com0addr = (int32_t *)MIPS_PHYS_TO_KSEG1(JZ_UART0);
     53      1.1  macallan 
     54      1.1  macallan void	ingenic_putchar_init(void);
     55      1.1  macallan void	ingenic_puts(const char *);
     56      1.1  macallan void	ingenic_putchar(char);
     57      1.1  macallan 
     58      1.1  macallan #ifndef CONMODE
     59  1.1.2.1     skrll # define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
     60      1.1  macallan #endif
     61      1.1  macallan 
     62      1.1  macallan 
     63  1.1.2.1     skrll static struct mips_bus_space	ingenic_com_mbst;
     64  1.1.2.1     skrll static int	mbst_valid = 0;
     65      1.1  macallan static void	ingenic_com_bus_mem_init(bus_space_tag_t, void *);
     66      1.1  macallan void		ingenic_com_cnattach(void);
     67      1.1  macallan 
     68      1.1  macallan static int	ingenic_com_match(device_t, cfdata_t , void *);
     69      1.1  macallan static void	ingenic_com_attach(device_t, device_t, void *);
     70      1.1  macallan 
     71      1.1  macallan struct ingenic_com_softc {
     72      1.1  macallan 	struct com_softc sc_com;
     73      1.1  macallan };
     74      1.1  macallan 
     75      1.1  macallan CFATTACH_DECL_NEW(com_mainbus, sizeof(struct ingenic_com_softc),
     76      1.1  macallan     ingenic_com_match, ingenic_com_attach, NULL, NULL);
     77      1.1  macallan 
     78      1.1  macallan bus_space_handle_t regh = 0;
     79      1.1  macallan 
     80      1.1  macallan void
     81      1.1  macallan ingenic_putchar_init(void)
     82      1.1  macallan {
     83      1.1  macallan 	/*
     84      1.1  macallan 	 * XXX don't screw with the UART's speed until we know what clock
     85      1.1  macallan 	 * we're on
     86      1.1  macallan 	 */
     87      1.1  macallan #if 0
     88      1.1  macallan 	int rate;
     89      1.1  macallan #endif
     90      1.1  macallan 	extern int comspeed(long, long, int);
     91      1.1  macallan 
     92      1.1  macallan 	com0addr = (uint32_t *)MIPS_PHYS_TO_KSEG1(JZ_UART0);
     93      1.1  macallan #if 0
     94      1.1  macallan 	if (comcnfreq != -1) {
     95      1.1  macallan 		rate = comspeed(comcnspeed, comcnfreq, COM_TYPE_INGENIC);
     96      1.1  macallan 		if (rate < 0)
     97      1.1  macallan 			return;					/* XXX */
     98      1.1  macallan #endif
     99      1.1  macallan 		com0addr[com_ier] = 0;
    100      1.1  macallan 		com0addr[com_lctl] = htole32(LCR_DLAB);
    101      1.1  macallan #if 0
    102      1.1  macallan 		com0addr[com_dlbl] = htole32(rate & 0xff);
    103      1.1  macallan 		com0addr[com_dlbh] = htole32(rate >> 8);
    104      1.1  macallan #endif
    105      1.1  macallan 		com0addr[com_lctl] = htole32(LCR_8BITS);	/* XXX */
    106      1.1  macallan 		com0addr[com_mcr]  = htole32(MCR_DTR|MCR_RTS);
    107      1.1  macallan 		com0addr[com_fifo] = htole32(
    108      1.1  macallan 		    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
    109      1.1  macallan 		    FIFO_TRIGGER_1 | FIFO_UART_ON);
    110      1.1  macallan #if 0
    111      1.1  macallan 	}
    112      1.1  macallan #endif
    113      1.1  macallan }
    114      1.1  macallan 
    115      1.1  macallan 
    116      1.1  macallan void
    117      1.1  macallan ingenic_putchar(char c)
    118      1.1  macallan {
    119      1.1  macallan 	int timo = 150000;
    120      1.1  macallan 
    121      1.1  macallan 	while ((le32toh(com0addr[com_lsr]) & LSR_TXRDY) == 0)
    122      1.1  macallan 		if (--timo == 0)
    123      1.1  macallan 			break;
    124      1.1  macallan 
    125      1.1  macallan 	com0addr[com_data] = htole32((uint32_t)c);
    126      1.1  macallan 
    127      1.1  macallan 	while ((le32toh(com0addr[com_lsr]) & LSR_TSRE) == 0)
    128      1.1  macallan 		if (--timo == 0)
    129      1.1  macallan 			break;
    130      1.1  macallan }
    131      1.1  macallan 
    132      1.1  macallan void
    133      1.1  macallan ingenic_puts(const char *restrict s)
    134      1.1  macallan {
    135      1.1  macallan 	char c;
    136      1.1  macallan 
    137      1.1  macallan 	while ((c = *s++) != 0)
    138      1.1  macallan 		ingenic_putchar(c);
    139      1.1  macallan }
    140      1.1  macallan 
    141      1.1  macallan static void
    142      1.1  macallan ingenic_com_bus_init(void)
    143      1.1  macallan {
    144      1.1  macallan 	if (mbst_valid) return;
    145      1.1  macallan 	ingenic_com_bus_mem_init(&ingenic_com_mbst, NULL);
    146      1.1  macallan 	mbst_valid = 1;
    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.1  macallan 	struct com_regs	regs;
    153      1.1  macallan 
    154      1.1  macallan 	ingenic_com_bus_init();
    155      1.1  macallan 	bus_space_map(&ingenic_com_mbst, 0, 0x1000, 0, &regh);
    156      1.1  macallan 
    157      1.1  macallan 	memset(&regs, 0, sizeof(regs));
    158      1.1  macallan 	COM_INIT_REGS(regs, &ingenic_com_mbst, regh, 0);
    159      1.1  macallan 
    160      1.1  macallan 	/*
    161      1.1  macallan 	 * XXX
    162      1.1  macallan 	 * UART clock is either 6MHz or 12MHz, the manual is rather unclear
    163      1.1  macallan 	 * so we just leave alone whatever u-boot set up
    164      1.1  macallan 	 * my uplcom is too tolerant to show any difference
    165      1.1  macallan 	 */
    166  1.1.2.1     skrll 	comcnattach1(&regs, 115200, 48000000, COM_TYPE_INGENIC, CONMODE);
    167      1.1  macallan }
    168      1.1  macallan 
    169      1.1  macallan static int
    170      1.1  macallan ingenic_com_match(device_t parent, cfdata_t cfdata, void *args)
    171      1.1  macallan {
    172      1.1  macallan 	struct mainbusdev {
    173      1.1  macallan 		const char *md_name;
    174      1.1  macallan 	} *aa = args;
    175      1.1  macallan 	if (strcmp(aa->md_name, "com") == 0) return 1;
    176      1.1  macallan 	return 0;
    177      1.1  macallan }
    178      1.1  macallan 
    179      1.1  macallan 
    180      1.1  macallan static void
    181      1.1  macallan ingenic_com_attach(device_t parent, device_t self, void *args)
    182      1.1  macallan {
    183      1.1  macallan 	struct ingenic_com_softc *isc = device_private(self);
    184      1.1  macallan 	struct com_softc *sc = &isc->sc_com;
    185      1.1  macallan 
    186      1.1  macallan 	sc->sc_dev = self;
    187  1.1.2.1     skrll 	sc->sc_frequency = 48000000;
    188      1.1  macallan 	sc->sc_type = COM_TYPE_INGENIC;
    189      1.1  macallan 	memset(&sc->sc_regs, 0, sizeof(sc->sc_regs));
    190      1.1  macallan 	COM_INIT_REGS(sc->sc_regs, &ingenic_com_mbst, regh, 0);
    191      1.1  macallan 	com_attach_subr(sc);
    192      1.1  macallan 	printf("\n");
    193  1.1.2.1     skrll 	evbmips_intr_establish(51, comintr, sc);
    194      1.1  macallan }
    195      1.1  macallan 
    196      1.1  macallan #define CHIP	   		ingenic_com
    197      1.1  macallan #define	CHIP_MEM		/* defined */
    198      1.1  macallan #define	CHIP_W1_BUS_START(v)	0x00000000UL
    199      1.1  macallan #define CHIP_W1_BUS_END(v)	0x00010000UL
    200      1.1  macallan #define	CHIP_W1_SYS_START(v)	0x10030000UL
    201      1.1  macallan #define	CHIP_W1_SYS_END(v)	0x10035000UL
    202      1.1  macallan #define	CHIP_ACCESS_SIZE	1
    203      1.1  macallan #define CHIP_ALIGN_STRIDE	2
    204      1.1  macallan 
    205      1.1  macallan #include <mips/mips/bus_space_alignstride_chipdep.c>
    206