Home | History | Annotate | Line # | Download | only in hd64461
hd64461uart.c revision 1.10.6.2
      1  1.10.6.2  nathanw /*	$NetBSD: hd64461uart.c,v 1.10.6.2 2002/09/17 21:14:52 nathanw Exp $	*/
      2  1.10.6.2  nathanw 
      3  1.10.6.2  nathanw /*-
      4  1.10.6.2  nathanw  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
      5  1.10.6.2  nathanw  * All rights reserved.
      6  1.10.6.2  nathanw  *
      7  1.10.6.2  nathanw  * Redistribution and use in source and binary forms, with or without
      8  1.10.6.2  nathanw  * modification, are permitted provided that the following conditions
      9  1.10.6.2  nathanw  * are met:
     10  1.10.6.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     11  1.10.6.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     12  1.10.6.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.10.6.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     14  1.10.6.2  nathanw  *    documentation and/or other materials provided with the distribution.
     15  1.10.6.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     16  1.10.6.2  nathanw  *    must display the following acknowledgement:
     17  1.10.6.2  nathanw  *        This product includes software developed by the NetBSD
     18  1.10.6.2  nathanw  *        Foundation, Inc. and its contributors.
     19  1.10.6.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  1.10.6.2  nathanw  *    contributors may be used to endorse or promote products derived
     21  1.10.6.2  nathanw  *    from this software without specific prior written permission.
     22  1.10.6.2  nathanw  *
     23  1.10.6.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.10.6.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.10.6.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.10.6.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.10.6.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.10.6.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.10.6.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.10.6.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.10.6.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.10.6.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.10.6.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     34  1.10.6.2  nathanw  */
     35  1.10.6.2  nathanw 
     36  1.10.6.2  nathanw #include "opt_kgdb.h"
     37  1.10.6.2  nathanw 
     38  1.10.6.2  nathanw #include <sys/param.h>
     39  1.10.6.2  nathanw #include <sys/systm.h>
     40  1.10.6.2  nathanw #include <sys/reboot.h>
     41  1.10.6.2  nathanw #include <sys/malloc.h>
     42  1.10.6.2  nathanw #include <sys/device.h>
     43  1.10.6.2  nathanw #include <sys/kgdb.h>
     44  1.10.6.2  nathanw 
     45  1.10.6.2  nathanw #include <sys/termios.h>
     46  1.10.6.2  nathanw #include <dev/cons.h>
     47  1.10.6.2  nathanw #include <sys/conf.h>
     48  1.10.6.2  nathanw 
     49  1.10.6.2  nathanw #include <machine/bus.h>
     50  1.10.6.2  nathanw #include <machine/intr.h>
     51  1.10.6.2  nathanw #include <machine/console.h>
     52  1.10.6.2  nathanw 
     53  1.10.6.2  nathanw #include <dev/ic/comvar.h>
     54  1.10.6.2  nathanw #include <dev/ic/comreg.h>
     55  1.10.6.2  nathanw 
     56  1.10.6.2  nathanw #include <machine/debug.h>
     57  1.10.6.2  nathanw 
     58  1.10.6.2  nathanw #include <hpcsh/dev/hd64461/hd64461var.h>
     59  1.10.6.2  nathanw #include <hpcsh/dev/hd64461/hd64461reg.h>
     60  1.10.6.2  nathanw #include <hpcsh/dev/hd64461/hd64461intcreg.h>
     61  1.10.6.2  nathanw #include <hpcsh/dev/hd64461/hd64461uartvar.h>
     62  1.10.6.2  nathanw #include <hpcsh/dev/hd64461/hd64461uartreg.h>
     63  1.10.6.2  nathanw 
     64  1.10.6.2  nathanw STATIC struct hd64461uart_chip {
     65  1.10.6.2  nathanw 	struct hpcsh_bus_space __tag_body;
     66  1.10.6.2  nathanw 	bus_space_tag_t io_tag;
     67  1.10.6.2  nathanw 	int console;
     68  1.10.6.2  nathanw } hd64461uart_chip;
     69  1.10.6.2  nathanw 
     70  1.10.6.2  nathanw struct hd64461uart_softc {
     71  1.10.6.2  nathanw 	struct com_softc sc_com;
     72  1.10.6.2  nathanw 
     73  1.10.6.2  nathanw 	struct hd64461uart_chip *sc_chip;
     74  1.10.6.2  nathanw 	enum hd64461_module_id sc_module_id;
     75  1.10.6.2  nathanw };
     76  1.10.6.2  nathanw 
     77  1.10.6.2  nathanw /* boot console */
     78  1.10.6.2  nathanw void hd64461uartcnprobe(struct consdev *);
     79  1.10.6.2  nathanw void hd64461uartcninit(struct consdev *);
     80  1.10.6.2  nathanw 
     81  1.10.6.2  nathanw STATIC int hd64461uart_match(struct device *, struct cfdata *, void *);
     82  1.10.6.2  nathanw STATIC void hd64461uart_attach(struct device *, struct device *, void *);
     83  1.10.6.2  nathanw 
     84  1.10.6.2  nathanw struct cfattach hd64461uart_ca = {
     85  1.10.6.2  nathanw 	sizeof(struct hd64461uart_softc), hd64461uart_match,
     86  1.10.6.2  nathanw 	hd64461uart_attach
     87  1.10.6.2  nathanw };
     88  1.10.6.2  nathanw 
     89  1.10.6.2  nathanw STATIC void hd64461uart_init(void);
     90  1.10.6.2  nathanw STATIC u_int8_t hd64461uart_read_1(void *, bus_space_handle_t, bus_size_t);
     91  1.10.6.2  nathanw STATIC void hd64461uart_write_1(void *, bus_space_handle_t, bus_size_t,
     92  1.10.6.2  nathanw     u_int8_t);
     93  1.10.6.2  nathanw 
     94  1.10.6.2  nathanw #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
     95  1.10.6.2  nathanw #ifndef COMCN_SPEED
     96  1.10.6.2  nathanw #define COMCN_SPEED	19200
     97  1.10.6.2  nathanw #endif
     98  1.10.6.2  nathanw 
     99  1.10.6.2  nathanw void
    100  1.10.6.2  nathanw hd64461uartcnprobe(struct consdev *cp)
    101  1.10.6.2  nathanw {
    102  1.10.6.2  nathanw 	extern const struct cdevsw com_cdevsw;
    103  1.10.6.2  nathanw 	int maj;
    104  1.10.6.2  nathanw 
    105  1.10.6.2  nathanw 	/* locate the major number */
    106  1.10.6.2  nathanw 	maj = cdevsw_lookup_major(&com_cdevsw);
    107  1.10.6.2  nathanw 
    108  1.10.6.2  nathanw 	/* Initialize required fields. */
    109  1.10.6.2  nathanw 	cp->cn_dev = makedev(maj, 0);
    110  1.10.6.2  nathanw 	cp->cn_pri = CN_NORMAL;
    111  1.10.6.2  nathanw }
    112  1.10.6.2  nathanw 
    113  1.10.6.2  nathanw void
    114  1.10.6.2  nathanw hd64461uartcninit(struct consdev *cp)
    115  1.10.6.2  nathanw {
    116  1.10.6.2  nathanw 
    117  1.10.6.2  nathanw 	hd64461uart_init();
    118  1.10.6.2  nathanw 
    119  1.10.6.2  nathanw 	comcnattach(hd64461uart_chip.io_tag, 0x0, COMCN_SPEED, COM_FREQ,
    120  1.10.6.2  nathanw 	    CONMODE);
    121  1.10.6.2  nathanw 
    122  1.10.6.2  nathanw 	hd64461uart_chip.console = 1;
    123  1.10.6.2  nathanw }
    124  1.10.6.2  nathanw 
    125  1.10.6.2  nathanw #ifdef KGDB
    126  1.10.6.2  nathanw int
    127  1.10.6.2  nathanw hd64461uart_kgdb_init()
    128  1.10.6.2  nathanw {
    129  1.10.6.2  nathanw 
    130  1.10.6.2  nathanw 	if (strcmp(kgdb_devname, "hd64461uart") != 0)
    131  1.10.6.2  nathanw 		return (1);
    132  1.10.6.2  nathanw 
    133  1.10.6.2  nathanw 	if (hd64461uart_chip.console)
    134  1.10.6.2  nathanw 		return (1);	/* can't share with console */
    135  1.10.6.2  nathanw 
    136  1.10.6.2  nathanw 	hd64461uart_init();
    137  1.10.6.2  nathanw 
    138  1.10.6.2  nathanw 	if (com_kgdb_attach(hd64461uart_chip.io_tag, 0x0, kgdb_rate,
    139  1.10.6.2  nathanw 	    COM_FREQ, CONMODE) != 0) {
    140  1.10.6.2  nathanw 		printf("%s: KGDB console open failed.\n", __FUNCTION__);
    141  1.10.6.2  nathanw 		return (1);
    142  1.10.6.2  nathanw 	}
    143  1.10.6.2  nathanw 
    144  1.10.6.2  nathanw 	return (0);
    145  1.10.6.2  nathanw }
    146  1.10.6.2  nathanw #endif /* KGDB */
    147  1.10.6.2  nathanw 
    148  1.10.6.2  nathanw int
    149  1.10.6.2  nathanw hd64461uart_match(struct device *parent, struct cfdata *cf, void *aux)
    150  1.10.6.2  nathanw {
    151  1.10.6.2  nathanw 	struct hd64461_attach_args *ha = aux;
    152  1.10.6.2  nathanw 
    153  1.10.6.2  nathanw 	return (ha->ha_module_id == HD64461_MODULE_UART);
    154  1.10.6.2  nathanw }
    155  1.10.6.2  nathanw 
    156  1.10.6.2  nathanw void
    157  1.10.6.2  nathanw hd64461uart_attach(struct device *parent, struct device *self, void *aux)
    158  1.10.6.2  nathanw {
    159  1.10.6.2  nathanw 	struct hd64461_attach_args *ha = aux;
    160  1.10.6.2  nathanw 	struct hd64461uart_softc *sc = (struct hd64461uart_softc *)self;
    161  1.10.6.2  nathanw 	struct com_softc *csc = &sc->sc_com;
    162  1.10.6.2  nathanw 	u_int16_t r16;
    163  1.10.6.2  nathanw 
    164  1.10.6.2  nathanw 	sc->sc_chip = &hd64461uart_chip;
    165  1.10.6.2  nathanw 
    166  1.10.6.2  nathanw 	sc->sc_module_id = ha->ha_module_id;
    167  1.10.6.2  nathanw 
    168  1.10.6.2  nathanw 	if (!sc->sc_chip->console)
    169  1.10.6.2  nathanw 		hd64461uart_init();
    170  1.10.6.2  nathanw 
    171  1.10.6.2  nathanw 	csc->sc_iot = sc->sc_chip->io_tag;
    172  1.10.6.2  nathanw 	bus_space_map(csc->sc_iot, 0, 8, 0, &csc->sc_ioh);
    173  1.10.6.2  nathanw 	csc->sc_iobase = 0;
    174  1.10.6.2  nathanw 	csc->sc_frequency = COM_FREQ;
    175  1.10.6.2  nathanw 
    176  1.10.6.2  nathanw 	/* switch port to UART */
    177  1.10.6.2  nathanw 
    178  1.10.6.2  nathanw 	/* supply clock */
    179  1.10.6.2  nathanw 	r16 = hd64461_reg_read_2(HD64461_SYSSTBCR_REG16);
    180  1.10.6.2  nathanw 	r16 &= ~HD64461_SYSSTBCR_SURTSD;
    181  1.10.6.2  nathanw 	hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
    182  1.10.6.2  nathanw 
    183  1.10.6.2  nathanw 	/* sanity check */
    184  1.10.6.2  nathanw 	if (!comprobe1(csc->sc_iot, csc->sc_ioh)) {
    185  1.10.6.2  nathanw 		printf(": device problem. don't attach.\n");
    186  1.10.6.2  nathanw 
    187  1.10.6.2  nathanw 		/* stop clock */
    188  1.10.6.2  nathanw 		r16 |= HD64461_SYSSTBCR_SURTSD;
    189  1.10.6.2  nathanw 		hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
    190  1.10.6.2  nathanw 		return;
    191  1.10.6.2  nathanw 	}
    192  1.10.6.2  nathanw 
    193  1.10.6.2  nathanw 	com_attach_subr(csc);
    194  1.10.6.2  nathanw 
    195  1.10.6.2  nathanw 	hd6446x_intr_establish(HD64461_INTC_UART, IST_LEVEL, IPL_TTY,
    196  1.10.6.2  nathanw 	    comintr, self);
    197  1.10.6.2  nathanw }
    198  1.10.6.2  nathanw 
    199  1.10.6.2  nathanw void
    200  1.10.6.2  nathanw hd64461uart_init()
    201  1.10.6.2  nathanw {
    202  1.10.6.2  nathanw 
    203  1.10.6.2  nathanw 	if (hd64461uart_chip.io_tag)
    204  1.10.6.2  nathanw 		return;
    205  1.10.6.2  nathanw 
    206  1.10.6.2  nathanw 	hd64461uart_chip.io_tag = bus_space_create(
    207  1.10.6.2  nathanw 		&hd64461uart_chip.__tag_body, "HD64461 UART I/O",
    208  1.10.6.2  nathanw 		HD64461_UART_REGBASE, 0); /* no extent */
    209  1.10.6.2  nathanw 
    210  1.10.6.2  nathanw 	/* override bus_space_read_1, bus_space_write_1 */
    211  1.10.6.2  nathanw 	hd64461uart_chip.io_tag->hbs_r_1 = hd64461uart_read_1;
    212  1.10.6.2  nathanw 	hd64461uart_chip.io_tag->hbs_w_1 = hd64461uart_write_1;
    213  1.10.6.2  nathanw }
    214  1.10.6.2  nathanw 
    215  1.10.6.2  nathanw u_int8_t
    216  1.10.6.2  nathanw hd64461uart_read_1(void *t, bus_space_handle_t h, bus_size_t ofs)
    217  1.10.6.2  nathanw {
    218  1.10.6.2  nathanw 
    219  1.10.6.2  nathanw 	return *(__volatile__ u_int8_t *)(h + (ofs << 1));
    220  1.10.6.2  nathanw }
    221  1.10.6.2  nathanw 
    222  1.10.6.2  nathanw void
    223  1.10.6.2  nathanw hd64461uart_write_1(void *t, bus_space_handle_t h, bus_size_t ofs,
    224  1.10.6.2  nathanw     u_int8_t val)
    225  1.10.6.2  nathanw {
    226  1.10.6.2  nathanw 
    227  1.10.6.2  nathanw 	*(__volatile__ u_int8_t *)(h + (ofs << 1)) = val;
    228  1.10.6.2  nathanw }
    229