Home | History | Annotate | Line # | Download | only in gemini
lpc_com.c revision 1.1
      1  1.1  cliff /*	$NetBSD: lpc_com.c,v 1.1 2008/11/09 09:15:42 cliff Exp $	*/
      2  1.1  cliff 
      3  1.1  cliff /* adapted from:
      4  1.1  cliff  *	NetBSD: gemini_com.c,v 1.1 2008/10/24 04:23:18 matt Exp
      5  1.1  cliff  */
      6  1.1  cliff 
      7  1.1  cliff /*
      8  1.1  cliff  * Based on arch/arm/xscale/pxa2x0_com.c
      9  1.1  cliff  *
     10  1.1  cliff  * Copyright 2003 Wasabi Systems, Inc.
     11  1.1  cliff  * All rights reserved.
     12  1.1  cliff  *
     13  1.1  cliff  * Written by Steve C. Woodford for Wasabi Systems, Inc.
     14  1.1  cliff  *
     15  1.1  cliff  * Redistribution and use in source and binary forms, with or without
     16  1.1  cliff  * modification, are permitted provided that the following conditions
     17  1.1  cliff  * are met:
     18  1.1  cliff  * 1. Redistributions of source code must retain the above copyright
     19  1.1  cliff  *    notice, this list of conditions and the following disclaimer.
     20  1.1  cliff  * 2. Redistributions in binary form must reproduce the above copyright
     21  1.1  cliff  *    notice, this list of conditions and the following disclaimer in the
     22  1.1  cliff  *    documentation and/or other materials provided with the distribution.
     23  1.1  cliff  * 3. All advertising materials mentioning features or use of this software
     24  1.1  cliff  *    must display the following acknowledgement:
     25  1.1  cliff  *      This product includes software developed for the NetBSD Project by
     26  1.1  cliff  *      Wasabi Systems, Inc.
     27  1.1  cliff  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     28  1.1  cliff  *    or promote products derived from this software without specific prior
     29  1.1  cliff  *    written permission.
     30  1.1  cliff  *
     31  1.1  cliff  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     32  1.1  cliff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     33  1.1  cliff  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     34  1.1  cliff  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     35  1.1  cliff  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     36  1.1  cliff  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     37  1.1  cliff  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     38  1.1  cliff  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     39  1.1  cliff  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  1.1  cliff  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  1.1  cliff  * POSSIBILITY OF SUCH DAMAGE.
     42  1.1  cliff  */
     43  1.1  cliff 
     44  1.1  cliff #include <sys/cdefs.h>
     45  1.1  cliff __KERNEL_RCSID(0, "$NetBSD: lpc_com.c,v 1.1 2008/11/09 09:15:42 cliff Exp $");
     46  1.1  cliff 
     47  1.1  cliff #include "opt_com.h"
     48  1.1  cliff #include "locators.h"
     49  1.1  cliff 
     50  1.1  cliff #include <sys/param.h>
     51  1.1  cliff #include <sys/systm.h>
     52  1.1  cliff #include <sys/device.h>
     53  1.1  cliff #include <sys/termios.h>
     54  1.1  cliff #include <sys/callout.h>
     55  1.1  cliff #include <sys/kernel.h>
     56  1.1  cliff 
     57  1.1  cliff #include <machine/intr.h>
     58  1.1  cliff #include <machine/bus.h>
     59  1.1  cliff 
     60  1.1  cliff #include <dev/ic/comreg.h>
     61  1.1  cliff #include <dev/ic/comvar.h>
     62  1.1  cliff 
     63  1.1  cliff #include <arm/gemini/gemini_obiovar.h>
     64  1.1  cliff #include <arm/gemini/gemini_lpchcvar.h>
     65  1.1  cliff #include <arm/gemini/gemini_lpcvar.h>
     66  1.1  cliff #include <arm/gemini/gemini_reg.h>
     67  1.1  cliff 
     68  1.1  cliff #include <arm/gemini/lpc_com.h>
     69  1.1  cliff 
     70  1.1  cliff typedef struct lpc_com_softc {
     71  1.1  cliff 	struct com_softc	sc_com;
     72  1.1  cliff 	bus_addr_t		sc_addr;
     73  1.1  cliff 	bus_size_t		sc_size;
     74  1.1  cliff 	int			sc_intr;
     75  1.1  cliff 	bus_space_tag_t		sc_iot;
     76  1.1  cliff 	bus_space_handle_t	sc_ioh;
     77  1.1  cliff 	struct callout		sc_callout;
     78  1.1  cliff } lpc_com_softc_t;
     79  1.1  cliff 
     80  1.1  cliff static int	lpc_com_match(device_t, cfdata_t , void *);
     81  1.1  cliff static void	lpc_com_attach(device_t, device_t, void *);
     82  1.1  cliff static int	lpc_com_intr(void *);
     83  1.1  cliff static void	lpc_com_time(void *);
     84  1.1  cliff 
     85  1.1  cliff CFATTACH_DECL_NEW(lpc_com, sizeof(struct lpc_com_softc),
     86  1.1  cliff     lpc_com_match, lpc_com_attach, NULL, NULL);
     87  1.1  cliff 
     88  1.1  cliff static int
     89  1.1  cliff lpc_com_match(device_t parent, cfdata_t cf, void *aux)
     90  1.1  cliff {
     91  1.1  cliff 	struct gemini_lpc_attach_args *lpc = aux;
     92  1.1  cliff 	gemini_lpc_bus_ops_t *ops;
     93  1.1  cliff 	lpctag_t lpctag;
     94  1.1  cliff 	bus_space_tag_t iot;
     95  1.1  cliff 	bus_space_handle_t ioh;
     96  1.1  cliff 	bus_addr_t iobase;
     97  1.1  cliff 	int rv;
     98  1.1  cliff 
     99  1.1  cliff 	if (lpc->lpc_addr == LPCCF_LDN_DEFAULT
    100  1.1  cliff 	||  lpc->lpc_addr == LPCCF_ADDR_DEFAULT)
    101  1.1  cliff 		panic("lpc_com must have ldn and addr"
    102  1.1  cliff 			" in config.");
    103  1.1  cliff 
    104  1.1  cliff 	if ((lpc->lpc_intr != LPCCF_INTR_DEFAULT) && (lpc->lpc_intr > 0xff))
    105  1.1  cliff 		panic("lpc_com: bad intr %d", lpc->lpc_intr);
    106  1.1  cliff 
    107  1.1  cliff 	if (lpc->lpc_size == LPCCF_SIZE_DEFAULT)
    108  1.1  cliff 		lpc->lpc_size = IT8712F_UART_SIZE;
    109  1.1  cliff 
    110  1.1  cliff 	iobase = lpc->lpc_base + lpc->lpc_addr;
    111  1.1  cliff 	if (com_is_console(lpc->lpc_iot, iobase, NULL))
    112  1.1  cliff 		return 1;
    113  1.1  cliff 
    114  1.1  cliff 	lpctag = lpc->lpc_tag;
    115  1.1  cliff 	ops = lpc->lpc_bus_ops;
    116  1.1  cliff 	(*ops->lpc_pnp_enter)(lpctag);
    117  1.1  cliff 
    118  1.1  cliff 	/* Activate */
    119  1.1  cliff 	(*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0x30, 0x01);
    120  1.1  cliff 
    121  1.1  cliff 	/* Set address */
    122  1.1  cliff 	(*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0x60,
    123  1.1  cliff 		(lpc->lpc_addr % 0xff00) >> 8);
    124  1.1  cliff 	(*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0x61,
    125  1.1  cliff 		(lpc->lpc_addr % 0x00ff) >> 0);
    126  1.1  cliff 
    127  1.1  cliff 	/* Set Interrupt Level */
    128  1.1  cliff 	(*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0x70, lpc->lpc_intr);
    129  1.1  cliff 
    130  1.1  cliff 	/* Set Special Configuration Regs */
    131  1.1  cliff 	(*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0xf0, 0x00);
    132  1.1  cliff #if 0
    133  1.1  cliff 	(*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0xf1, 0x50);
    134  1.1  cliff #else
    135  1.1  cliff 	(*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0xf1, 0x58);	/* LO */
    136  1.1  cliff #endif
    137  1.1  cliff 	(*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0xf2, 0x00);
    138  1.1  cliff 	(*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0xf3, 0x7f);
    139  1.1  cliff 
    140  1.1  cliff 	(*ops->lpc_pnp_exit)(lpctag);
    141  1.1  cliff 
    142  1.1  cliff 	iot = lpc->lpc_iot;
    143  1.1  cliff 	if (bus_space_map(iot, iobase, lpc->lpc_size, 0, &ioh))
    144  1.1  cliff 		return 0;
    145  1.1  cliff 
    146  1.1  cliff 	rv = comprobe1(iot, ioh);
    147  1.1  cliff 
    148  1.1  cliff 	bus_space_unmap(iot, ioh, lpc->lpc_size);
    149  1.1  cliff 
    150  1.1  cliff 	return rv;
    151  1.1  cliff }
    152  1.1  cliff 
    153  1.1  cliff static void
    154  1.1  cliff lpc_com_attach(device_t parent, device_t self, void *aux)
    155  1.1  cliff {
    156  1.1  cliff 	struct lpc_com_softc *sc = device_private(self);
    157  1.1  cliff 	struct gemini_lpc_attach_args *lpc = aux;
    158  1.1  cliff 	gemini_lpc_bus_ops_t *ops = lpc->lpc_bus_ops;
    159  1.1  cliff 	bus_space_tag_t iot;
    160  1.1  cliff 	bus_space_handle_t ioh;
    161  1.1  cliff 	bus_addr_t iobase;
    162  1.1  cliff 
    163  1.1  cliff 	sc->sc_com.sc_dev = self;
    164  1.1  cliff 	iot = lpc->lpc_iot;
    165  1.1  cliff 	iobase = lpc->lpc_base + lpc->lpc_addr;
    166  1.1  cliff 	sc->sc_com.sc_frequency = IT8712F_COM_FREQ;
    167  1.1  cliff 	sc->sc_com.sc_type = COM_TYPE_NORMAL;
    168  1.1  cliff 
    169  1.1  cliff 	if (com_is_console(iot, iobase, &ioh) == 0 &&
    170  1.1  cliff 	    bus_space_map(iot, iobase, lpc->lpc_size, 0, &ioh)) {
    171  1.1  cliff 		panic(": can't map registers\n");
    172  1.1  cliff 		return;
    173  1.1  cliff 	}
    174  1.1  cliff 
    175  1.1  cliff 	COM_INIT_REGS(sc->sc_com.sc_regs, iot, ioh, iobase);
    176  1.1  cliff 
    177  1.1  cliff 	com_attach_subr(&sc->sc_com);
    178  1.1  cliff 	aprint_naive("\n");
    179  1.1  cliff 
    180  1.1  cliff 	if (lpc->lpc_intr == LPCCF_INTR_DEFAULT) {
    181  1.1  cliff 		/* callout based polliung */
    182  1.1  cliff 		callout_init(&sc->sc_callout, 0);
    183  1.1  cliff 		callout_setfunc(&sc->sc_callout, lpc_com_time, sc);
    184  1.1  cliff 		callout_schedule(&sc->sc_callout, hz/8);
    185  1.1  cliff 		aprint_normal("%s: callout polling mode\n", device_xname(self));
    186  1.1  cliff 	} else {
    187  1.1  cliff 		/* interrupting */
    188  1.1  cliff #if 0
    189  1.1  cliff 		(*ops->lpc_intr_establish)(lpc->lpc_intrtag, lpc->lpc_intr,
    190  1.1  cliff 			IPL_SERIAL, IST_LEVEL_HIGH, comintr, &sc->sc_com);
    191  1.1  cliff #else
    192  1.1  cliff 		(*ops->lpc_intr_establish)(lpc->lpc_intrtag, lpc->lpc_intr,
    193  1.1  cliff 			IPL_SERIAL, IST_LEVEL_LOW, lpc_com_intr, &sc->sc_com);
    194  1.1  cliff #endif
    195  1.1  cliff 	}
    196  1.1  cliff }
    197  1.1  cliff 
    198  1.1  cliff int
    199  1.1  cliff lpc_com_intr(void *arg)
    200  1.1  cliff {
    201  1.1  cliff 	printf(".");
    202  1.1  cliff 	return comintr(arg);
    203  1.1  cliff }
    204  1.1  cliff 
    205  1.1  cliff void
    206  1.1  cliff lpc_com_time(void *arg)
    207  1.1  cliff {
    208  1.1  cliff 	lpc_com_softc_t *sc = arg;
    209  1.1  cliff 	int s;
    210  1.1  cliff 
    211  1.1  cliff 	s = splserial();
    212  1.1  cliff 	(void)comintr(&sc->sc_com);
    213  1.1  cliff 	callout_schedule(&sc->sc_callout, hz/8);
    214  1.1  cliff 	splx(s);
    215  1.1  cliff }
    216