Home | History | Annotate | Line # | Download | only in hd64461
hd64461uart.c revision 1.21.4.2
      1  1.21.4.2     yamt /*	$NetBSD: hd64461uart.c,v 1.21.4.2 2010/08/11 22:52:08 yamt Exp $	*/
      2       1.1      uch 
      3       1.1      uch /*-
      4       1.5      uch  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
      5       1.1      uch  * All rights reserved.
      6       1.1      uch  *
      7       1.1      uch  * Redistribution and use in source and binary forms, with or without
      8       1.1      uch  * modification, are permitted provided that the following conditions
      9       1.1      uch  * are met:
     10       1.1      uch  * 1. Redistributions of source code must retain the above copyright
     11       1.1      uch  *    notice, this list of conditions and the following disclaimer.
     12       1.1      uch  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1      uch  *    notice, this list of conditions and the following disclaimer in the
     14       1.1      uch  *    documentation and/or other materials provided with the distribution.
     15       1.1      uch  *
     16       1.1      uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1      uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1      uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1      uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1      uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1      uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1      uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1      uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1      uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1      uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1      uch  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1      uch  */
     28      1.16    lukem 
     29      1.16    lukem #include <sys/cdefs.h>
     30  1.21.4.2     yamt __KERNEL_RCSID(0, "$NetBSD: hd64461uart.c,v 1.21.4.2 2010/08/11 22:52:08 yamt Exp $");
     31       1.1      uch 
     32       1.7      uch #include "opt_kgdb.h"
     33       1.7      uch 
     34       1.1      uch #include <sys/param.h>
     35       1.1      uch #include <sys/systm.h>
     36       1.1      uch #include <sys/reboot.h>
     37       1.1      uch #include <sys/malloc.h>
     38       1.1      uch #include <sys/device.h>
     39       1.8      uch #include <sys/kgdb.h>
     40       1.1      uch 
     41       1.1      uch #include <sys/termios.h>
     42       1.1      uch #include <dev/cons.h>
     43       1.1      uch #include <sys/conf.h>
     44       1.1      uch 
     45       1.1      uch #include <machine/bus.h>
     46       1.1      uch #include <machine/intr.h>
     47       1.7      uch #include <machine/console.h>
     48  1.21.4.2     yamt #include <machine/platid.h>
     49  1.21.4.2     yamt #include <machine/platid_mask.h>
     50       1.1      uch 
     51       1.1      uch #include <dev/ic/comvar.h>
     52       1.1      uch #include <dev/ic/comreg.h>
     53       1.1      uch 
     54       1.6      uch #include <machine/debug.h>
     55       1.1      uch 
     56       1.1      uch #include <hpcsh/dev/hd64461/hd64461var.h>
     57       1.1      uch #include <hpcsh/dev/hd64461/hd64461reg.h>
     58      1.10      uch #include <hpcsh/dev/hd64461/hd64461intcreg.h>
     59       1.7      uch #include <hpcsh/dev/hd64461/hd64461uartvar.h>
     60       1.7      uch #include <hpcsh/dev/hd64461/hd64461uartreg.h>
     61       1.1      uch 
     62  1.21.4.2     yamt #define	HD64461UART_INIT_REGS(regs, tag, hdl, addr)			\
     63  1.21.4.2     yamt 	do {								\
     64  1.21.4.2     yamt 		int i;							\
     65  1.21.4.2     yamt 									\
     66  1.21.4.2     yamt 		regs.cr_iot = tag;					\
     67  1.21.4.2     yamt 		regs.cr_ioh = hdl;					\
     68  1.21.4.2     yamt 		regs.cr_iobase = addr;					\
     69  1.21.4.2     yamt 		regs.cr_nports = COM_NPORTS;				\
     70  1.21.4.2     yamt 		for (i = 0; i < __arraycount(regs.cr_map); i++)		\
     71  1.21.4.2     yamt 			regs.cr_map[i] = com_std_map[i] << 1;		\
     72  1.21.4.2     yamt 	} while (0)
     73  1.21.4.2     yamt 
     74       1.5      uch STATIC struct hd64461uart_chip {
     75       1.1      uch 	struct hpcsh_bus_space __tag_body;
     76       1.1      uch 	bus_space_tag_t io_tag;
     77       1.1      uch 	int console;
     78       1.1      uch } hd64461uart_chip;
     79       1.1      uch 
     80       1.1      uch struct hd64461uart_softc {
     81       1.1      uch 	struct com_softc sc_com;
     82       1.1      uch 
     83       1.1      uch 	struct hd64461uart_chip *sc_chip;
     84       1.1      uch 	enum hd64461_module_id sc_module_id;
     85       1.1      uch };
     86       1.1      uch 
     87       1.1      uch /* boot console */
     88       1.8      uch void hd64461uartcnprobe(struct consdev *);
     89       1.8      uch void hd64461uartcninit(struct consdev *);
     90       1.1      uch 
     91      1.21     cube STATIC int hd64461uart_match(device_t, cfdata_t , void *);
     92      1.21     cube STATIC void hd64461uart_attach(device_t, device_t, void *);
     93       1.1      uch 
     94      1.21     cube CFATTACH_DECL_NEW(hd64461uart, sizeof(struct hd64461uart_softc),
     95      1.14  thorpej     hd64461uart_match, hd64461uart_attach, NULL, NULL);
     96       1.1      uch 
     97       1.5      uch STATIC void hd64461uart_init(void);
     98       1.1      uch 
     99       1.1      uch #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    100       1.1      uch #ifndef COMCN_SPEED
    101       1.1      uch #define COMCN_SPEED	19200
    102       1.1      uch #endif
    103       1.1      uch 
    104       1.1      uch void
    105       1.8      uch hd64461uartcnprobe(struct consdev *cp)
    106       1.1      uch {
    107      1.11  gehenna 	extern const struct cdevsw com_cdevsw;
    108       1.1      uch 	int maj;
    109       1.1      uch 
    110       1.1      uch 	/* locate the major number */
    111      1.11  gehenna 	maj = cdevsw_lookup_major(&com_cdevsw);
    112       1.1      uch 
    113       1.1      uch 	/* Initialize required fields. */
    114       1.1      uch 	cp->cn_dev = makedev(maj, 0);
    115       1.1      uch 	cp->cn_pri = CN_NORMAL;
    116       1.1      uch }
    117       1.1      uch 
    118       1.1      uch void
    119       1.8      uch hd64461uartcninit(struct consdev *cp)
    120       1.1      uch {
    121  1.21.4.2     yamt 	struct com_regs regs;
    122       1.3      uch 
    123       1.2      uch 	hd64461uart_init();
    124       1.2      uch 
    125  1.21.4.2     yamt 	HD64461UART_INIT_REGS(regs, hd64461uart_chip.io_tag, 0x0, 0x0);
    126  1.21.4.2     yamt 	comcnattach1(&regs, COMCN_SPEED, COM_FREQ, COM_TYPE_NORMAL, CONMODE);
    127       1.2      uch 
    128       1.1      uch 	hd64461uart_chip.console = 1;
    129  1.21.4.2     yamt 	/* Don't stop to suply AFECK */
    130  1.21.4.2     yamt 	if (platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
    131  1.21.4.2     yamt 		use_afeck = 1;
    132       1.1      uch }
    133       1.8      uch 
    134       1.8      uch #ifdef KGDB
    135       1.8      uch int
    136      1.18      uwe hd64461uart_kgdb_init(void)
    137       1.8      uch {
    138  1.21.4.2     yamt 	struct com_regs regs;
    139       1.8      uch 
    140       1.8      uch 	if (strcmp(kgdb_devname, "hd64461uart") != 0)
    141  1.21.4.2     yamt 		return 1;
    142       1.8      uch 
    143       1.9      uch 	if (hd64461uart_chip.console)
    144  1.21.4.2     yamt 		return 1;	/* can't share with console */
    145       1.9      uch 
    146       1.8      uch 	hd64461uart_init();
    147       1.8      uch 
    148  1.21.4.2     yamt 	HD64461UART_INIT_REGS(regs, hd64461uart_chip.io_tag, NULL, 0x0);
    149  1.21.4.2     yamt 	if (com_kgdb_attach1(&regs,
    150  1.21.4.2     yamt 	    kgdb_rate, COM_FREQ, COM_TYPE_NORMAL, CONMODE) != 0) {
    151      1.20    perry 		printf("%s: KGDB console open failed.\n", __func__);
    152  1.21.4.2     yamt 		return 1;
    153       1.8      uch 	}
    154       1.8      uch 
    155  1.21.4.2     yamt 	if (platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
    156  1.21.4.2     yamt 		use_afeck = 1;
    157  1.21.4.2     yamt 	return 0;
    158       1.8      uch }
    159       1.8      uch #endif /* KGDB */
    160       1.1      uch 
    161      1.18      uwe STATIC int
    162      1.21     cube hd64461uart_match(device_t parent, cfdata_t cf, void *aux)
    163       1.1      uch {
    164       1.1      uch 	struct hd64461_attach_args *ha = aux;
    165       1.1      uch 
    166  1.21.4.2     yamt 	return ha->ha_module_id == HD64461_MODULE_UART;
    167       1.1      uch }
    168       1.1      uch 
    169      1.18      uwe STATIC void
    170      1.21     cube hd64461uart_attach(device_t parent, device_t self, void *aux)
    171       1.1      uch {
    172       1.1      uch 	struct hd64461_attach_args *ha = aux;
    173      1.21     cube 	struct hd64461uart_softc *sc = device_private(self);
    174       1.1      uch 	struct com_softc *csc = &sc->sc_com;
    175  1.21.4.2     yamt 	uint16_t r16, or16;
    176      1.19  gdamore 	bus_space_handle_t ioh;
    177       1.1      uch 
    178      1.21     cube 	csc->sc_dev = self;
    179       1.1      uch 	sc->sc_chip = &hd64461uart_chip;
    180       1.1      uch 
    181       1.1      uch 	sc->sc_module_id = ha->ha_module_id;
    182       1.1      uch 
    183       1.9      uch 	if (!sc->sc_chip->console)
    184       1.9      uch 		hd64461uart_init();
    185       1.1      uch 
    186  1.21.4.2     yamt 	bus_space_map(sc->sc_chip->io_tag, 0x0, 8, 0, &ioh);
    187       1.1      uch 	csc->sc_frequency = COM_FREQ;
    188  1.21.4.2     yamt 	HD64461UART_INIT_REGS(csc->sc_regs, sc->sc_chip->io_tag, ioh, 0x0);
    189       1.1      uch 
    190       1.1      uch 	/* switch port to UART */
    191       1.1      uch 
    192       1.1      uch 	/* supply clock */
    193  1.21.4.2     yamt 	r16 = or16 = hd64461_reg_read_2(HD64461_SYSSTBCR_REG16);
    194       1.1      uch 	r16 &= ~HD64461_SYSSTBCR_SURTSD;
    195  1.21.4.2     yamt 	if (platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
    196  1.21.4.2     yamt 		r16 &= ~(HD64461_SYSSTBCR_SAFECKE_IST |
    197  1.21.4.2     yamt 		    HD64461_SYSSTBCR_SAFECKE_OST);
    198      1.18      uwe 	hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
    199       1.1      uch 
    200       1.1      uch 	/* sanity check */
    201      1.19  gdamore 	if (!com_probe_subr(&csc->sc_regs)) {
    202      1.21     cube 		aprint_error(": device problem. don't attach.\n");
    203       1.1      uch 
    204  1.21.4.2     yamt 		/* restore old clock */
    205  1.21.4.2     yamt 		hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, or16);
    206       1.1      uch 		return;
    207       1.1      uch 	}
    208       1.1      uch 
    209       1.1      uch 	com_attach_subr(csc);
    210       1.1      uch 
    211      1.10      uch 	hd6446x_intr_establish(HD64461_INTC_UART, IST_LEVEL, IPL_TTY,
    212  1.21.4.2     yamt 	    comintr, csc);
    213       1.1      uch }
    214       1.1      uch 
    215      1.18      uwe STATIC void
    216      1.18      uwe hd64461uart_init(void)
    217       1.1      uch {
    218       1.3      uch 
    219       1.1      uch 	if (hd64461uart_chip.io_tag)
    220       1.1      uch 		return;
    221       1.1      uch 
    222       1.1      uch 	hd64461uart_chip.io_tag = bus_space_create(
    223       1.1      uch 		&hd64461uart_chip.__tag_body, "HD64461 UART I/O",
    224       1.1      uch 		HD64461_UART_REGBASE, 0); /* no extent */
    225       1.1      uch }
    226