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