Home | History | Annotate | Line # | Download | only in g42xxeb
gb225.c revision 1.8.2.1
      1  1.8.2.1     rmind /*	$NetBSD: gb225.c,v 1.8.2.1 2010/07/03 01:19:16 rmind Exp $ */
      2      1.1       bsh 
      3      1.1       bsh /*
      4      1.1       bsh  * Copyright (c) 2002, 2003  Genetec corp.  All rights reserved.
      5      1.1       bsh  * Written by Hiroyuki Bessho for Genetec corp.
      6      1.1       bsh  *
      7      1.1       bsh  * Redistribution and use in source and binary forms, with or without
      8      1.1       bsh  * modification, are permitted provided that the following conditions
      9      1.1       bsh  * are met:
     10      1.1       bsh  * 1. Redistributions of source code must retain the above copyright
     11      1.1       bsh  *    notice, this list of conditions and the following disclaimer.
     12      1.1       bsh  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1       bsh  *    notice, this list of conditions and the following disclaimer in the
     14      1.1       bsh  *    documentation and/or other materials provided with the distribution.
     15      1.1       bsh  * 3. The name of Genetec corp. may not be used to endorse
     16      1.1       bsh  *    or promote products derived from this software without specific prior
     17      1.1       bsh  *    written permission.
     18      1.1       bsh  *
     19      1.1       bsh  * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
     20      1.1       bsh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1       bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1       bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORP.
     23      1.1       bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1       bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1       bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1       bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1       bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1       bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1       bsh  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1       bsh  */
     31      1.1       bsh 
     32      1.1       bsh 
     33      1.1       bsh #include <sys/param.h>
     34      1.1       bsh #include <sys/systm.h>
     35      1.1       bsh #include <sys/device.h>
     36      1.1       bsh #include <sys/kernel.h>
     37      1.1       bsh #include <sys/reboot.h>
     38      1.1       bsh 
     39      1.1       bsh #include <machine/cpu.h>
     40      1.1       bsh #include <machine/bus.h>
     41      1.1       bsh #include <machine/intr.h>
     42      1.1       bsh #include <arm/cpufunc.h>
     43      1.1       bsh 
     44      1.1       bsh #include <arm/mainbus/mainbus.h>
     45      1.1       bsh #include <arm/xscale/pxa2x0reg.h>
     46      1.1       bsh #include <arm/xscale/pxa2x0var.h>
     47      1.1       bsh #include <arm/xscale/pxa2x0_gpio.h>
     48      1.1       bsh #include <arm/sa11x0/sa11x0_var.h>
     49      1.1       bsh #include <evbarm/g42xxeb/g42xxeb_reg.h>
     50      1.1       bsh #include <evbarm/g42xxeb/g42xxeb_var.h>
     51      1.1       bsh #include <evbarm/g42xxeb/gb225reg.h>
     52      1.1       bsh #include <evbarm/g42xxeb/gb225var.h>
     53      1.1       bsh 
     54      1.1       bsh #include "locators.h"
     55      1.1       bsh 
     56      1.1       bsh #define OPIO_INTR	G42XXEB_INT_EXT3
     57      1.1       bsh 
     58      1.1       bsh 
     59      1.1       bsh #define DEBOUNCE_COUNT	2
     60      1.1       bsh #define DEBOUNCE_TICKS	(hz/20)			/* 50ms */
     61      1.1       bsh 
     62      1.1       bsh /* prototypes */
     63      1.8       bsh static int	opio_match(device_t, cfdata_t, void *);
     64      1.8       bsh static void	opio_attach(device_t, device_t, void *);
     65      1.8       bsh static int 	opio_search(device_t, cfdata_t, const int *, void *);
     66      1.1       bsh static int	opio_print(void *, const char *);
     67      1.1       bsh #ifdef OPIO_INTR
     68      1.1       bsh static int 	opio_intr( void *arg );
     69      1.1       bsh static void	opio_debounce(void *arg);
     70      1.1       bsh #endif
     71      1.1       bsh 
     72      1.1       bsh 
     73      1.1       bsh /* attach structures */
     74      1.8       bsh CFATTACH_DECL_NEW(opio, sizeof(struct opio_softc), opio_match, opio_attach,
     75      1.1       bsh     NULL, NULL);
     76      1.1       bsh 
     77      1.1       bsh /*
     78      1.1       bsh  * int opio_print(void *aux, const char *name)
     79      1.1       bsh  * print configuration info for children
     80      1.1       bsh  */
     81      1.1       bsh 
     82      1.1       bsh static int
     83      1.1       bsh opio_print(void *aux, const char *name)
     84      1.1       bsh {
     85      1.1       bsh 	struct obio_attach_args *oba = (struct obio_attach_args*)aux;
     86      1.1       bsh 
     87      1.1       bsh 	if (oba->oba_addr != OPIOCF_ADDR_DEFAULT)
     88      1.1       bsh                 aprint_normal(" addr 0x%lx", oba->oba_addr);
     89      1.1       bsh         return (UNCONF);
     90      1.1       bsh }
     91      1.1       bsh 
     92      1.1       bsh int
     93      1.8       bsh opio_match(device_t parent, cfdata_t match, void *aux)
     94      1.1       bsh {
     95      1.8       bsh 	struct obio_softc *psc = device_private(parent);
     96      1.1       bsh 	uint16_t optid;
     97      1.1       bsh 
     98      1.1       bsh 	optid = bus_space_read_2(psc->sc_iot, psc->sc_obioreg_ioh,
     99      1.1       bsh 	    G42XXEB_OPTBRDID);
    100      1.1       bsh 
    101      1.1       bsh 	return optid == 0x01;
    102      1.1       bsh }
    103      1.1       bsh 
    104      1.1       bsh void
    105      1.8       bsh opio_attach(device_t parent, device_t self, void *aux)
    106      1.1       bsh {
    107      1.8       bsh 	struct opio_softc *sc = device_private(self);
    108      1.8       bsh 	struct obio_softc *bsc = device_private(parent);
    109      1.1       bsh 	struct obio_attach_args *oba = aux;
    110      1.1       bsh 	uint32_t reg;
    111      1.1       bsh 	int i;
    112      1.1       bsh 	bus_space_tag_t iot;
    113      1.1       bsh 	bus_space_handle_t  memctl_ioh = bsc->sc_memctl_ioh;
    114      1.1       bsh 
    115      1.1       bsh 	iot = oba->oba_iot;
    116      1.8       bsh 	sc->sc_dev = self;
    117      1.1       bsh 	sc->sc_iot = iot;
    118      1.1       bsh 	sc->sc_memctl_ioh = memctl_ioh;
    119      1.1       bsh 
    120      1.1       bsh 	/* use 16bit access for CS4, 32bit for CS5 */
    121      1.1       bsh 	reg = bus_space_read_4(iot, memctl_ioh, MEMCTL_MSC2);
    122      1.1       bsh 	reg |= MSC_RBW;
    123      1.1       bsh 	reg &= ~(MSC_RBW<<16);
    124      1.1       bsh 	/* XXX: set access timing */
    125      1.1       bsh 	bus_space_write_4(iot, memctl_ioh, MEMCTL_MSC2, reg);
    126      1.1       bsh 
    127      1.1       bsh 	/* Set alternative function for GPIO pings 48..57 on PXA2X0 */
    128      1.1       bsh #if 0
    129      1.1       bsh 	reg = bus_space_read_4(iot, csc->saip.sc_gpioh, GPIO_GPDR1);
    130      1.1       bsh 	bus_space_write_4(iot, csc->saip.sc_gpioh, GPIO_GPDR1,
    131      1.1       bsh 	    (reg & ~0x03ff0000) | 0x00ff0000);
    132      1.1       bsh 	reg = bus_space_read_4(iot, csc->saip.sc_gpioh, GPIO_GAFR1_U);
    133      1.1       bsh 	bus_space_write_4(iot, csc->saip.sc_gpioh, GPIO_GAFR1_U,
    134      1.1       bsh 	    (reg & ~0x000fffff) | 0x0005aaaa );
    135      1.1       bsh #else
    136      1.1       bsh 	for (i=47; i <= 55; ++i)
    137      1.1       bsh 		pxa2x0_gpio_set_function(i, GPIO_ALT_FN_2_OUT);
    138      1.1       bsh 	pxa2x0_gpio_set_function(56, GPIO_ALT_FN_1_IN);
    139      1.1       bsh 	pxa2x0_gpio_set_function(57, GPIO_ALT_FN_1_IN);
    140      1.1       bsh #endif
    141      1.1       bsh 
    142      1.1       bsh 	/* Enable bus switch for option board */
    143      1.1       bsh 	reg = bus_space_read_2(iot, bsc->sc_obioreg_ioh, G42XXEB_EXTCTRL);
    144      1.1       bsh 	bus_space_write_2(iot, bsc->sc_obioreg_ioh, G42XXEB_EXTCTRL, reg | 3);
    145      1.1       bsh 
    146      1.1       bsh 	/* Map on-board FPGA registers */
    147      1.1       bsh 	if( bus_space_map( iot, GB225_PLDREG_BASE, GB225_PLDREG_SIZE,
    148      1.1       bsh 	    0, &(sc->sc_ioh) ) ){
    149  1.8.2.1     rmind 		aprint_error_dev(self, "can't map FPGA registers\n");
    150      1.1       bsh 	}
    151      1.1       bsh 
    152      1.1       bsh 	aprint_normal("\n");
    153      1.1       bsh 
    154      1.6        ad 	callout_init(&sc->sc_callout, 0);
    155      1.1       bsh 
    156      1.1       bsh 	for (i=0; i < N_OPIO_INTR; ++i) {
    157      1.1       bsh 		sc->sc_intr[i].func = NULL;
    158      1.1       bsh 		sc->sc_intr[i].reported_state = 0xff;
    159      1.1       bsh 		sc->sc_intr[i].last_state = 0xff;
    160      1.1       bsh 	}
    161      1.1       bsh 
    162      1.1       bsh #ifdef OPIO_INTR
    163      1.1       bsh 	sc->sc_ih = obio_intr_establish(bsc, OPIO_INTR, IPL_BIO,
    164      1.1       bsh 	    IST_EDGE_FALLING, opio_intr, sc);
    165      1.1       bsh #endif
    166      1.1       bsh 
    167      1.1       bsh #ifdef DEBUG
    168  1.8.2.1     rmind 	aprint_debug_dev(self, "CF_DET=%x PCMCIA_DET=%x\n",
    169      1.1       bsh 	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, GB225_CFDET),
    170      1.1       bsh 	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, GB225_PCMCIADET));
    171      1.1       bsh #endif
    172      1.1       bsh 
    173      1.1       bsh 	/*
    174      1.1       bsh 	 *  Attach each devices
    175      1.1       bsh 	 */
    176      1.2  drochner 	config_search_ia(opio_search, self, "opio", NULL);
    177      1.1       bsh }
    178      1.1       bsh 
    179      1.1       bsh int
    180      1.8       bsh opio_search(device_t parent, cfdata_t cf,
    181      1.3  drochner 	    const int *ldesc, void *aux)
    182      1.1       bsh {
    183      1.8       bsh 	struct opio_softc *sc = device_private(parent);
    184      1.1       bsh 	struct obio_attach_args oba;
    185      1.1       bsh 
    186      1.1       bsh 	oba.oba_sc = sc;
    187      1.1       bsh         oba.oba_iot = sc->sc_iot;
    188      1.2  drochner         oba.oba_addr = cf->cf_loc[OPIOCF_ADDR];
    189      1.2  drochner         oba.oba_intr = cf->cf_loc[OPIOCF_INTR];
    190      1.1       bsh 
    191      1.1       bsh         if (config_match(parent, cf, &oba) > 0)
    192      1.1       bsh                 config_attach(parent, cf, &oba, opio_print);
    193      1.1       bsh 
    194      1.1       bsh         return 0;
    195      1.1       bsh }
    196      1.1       bsh 
    197      1.1       bsh void *
    198      1.1       bsh opio_intr_establish(struct opio_softc *sc, int intr, int ipl,
    199      1.1       bsh     int (*func)(void *, int), void *arg)
    200      1.1       bsh {
    201      1.1       bsh 	sc->sc_intr[intr].arg = arg;
    202      1.1       bsh 	sc->sc_intr[intr].func = func;
    203      1.1       bsh 
    204      1.1       bsh 	return &sc->sc_intr[intr];
    205      1.1       bsh }
    206      1.1       bsh 
    207      1.1       bsh 
    208      1.1       bsh #ifdef OPIO_INTR
    209      1.1       bsh /*
    210      1.1       bsh  * interrupt handler for option board. interrupt sources are:
    211      1.1       bsh  *   CF card insertion/removal.
    212      1.1       bsh  *   PCMCIA card insertion/removal.
    213      1.1       bsh  *   USB power failure.
    214      1.1       bsh  *   CF/PCMCIA power failure.
    215      1.1       bsh  * We need to debounce for CF/PCMCIA card insertion/removal signal.
    216      1.1       bsh  */
    217      1.1       bsh static int
    218      1.1       bsh opio_intr( void *arg )
    219      1.1       bsh {
    220      1.1       bsh 	struct opio_softc *sc = (struct opio_softc *)arg;
    221      1.5   thorpej 	struct obio_softc *bsc =
    222      1.8       bsh 	    device_private(device_parent(sc->sc_dev));
    223      1.1       bsh 
    224      1.1       bsh 	/* avoid further interrupts while debouncing */
    225      1.1       bsh 	obio_intr_mask(bsc, sc->sc_ih);
    226      1.1       bsh 
    227      1.1       bsh 	printf("OPIO ");
    228      1.1       bsh 
    229      1.1       bsh 	if (sc->sc_debounce == ST_STABLE) {
    230      1.1       bsh 		/* start debounce timer */
    231      1.1       bsh 		callout_reset(&sc->sc_callout, DEBOUNCE_TICKS,
    232      1.1       bsh 		    opio_debounce, sc);
    233      1.1       bsh 		sc->sc_debounce = ST_BOUNCING;
    234      1.1       bsh 	}
    235      1.1       bsh 
    236      1.1       bsh 	return 1;
    237      1.1       bsh }
    238      1.1       bsh 
    239      1.1       bsh 
    240      1.1       bsh static int
    241      1.1       bsh do_debounce(struct opio_softc *sc, int intr, int val, int count)
    242      1.1       bsh {
    243      1.1       bsh 	int s;
    244      1.1       bsh 
    245      1.1       bsh 
    246      1.1       bsh 	struct opio_intr_handler *p = &sc->sc_intr[intr];
    247      1.1       bsh 
    248      1.1       bsh 	if (p->last_state != val) {
    249      1.1       bsh 		p->debounce_count = 0;
    250      1.1       bsh 		p->last_state = val;
    251      1.1       bsh 		return 1;
    252      1.1       bsh 	}
    253      1.1       bsh 	else if (p->debounce_count++ < count)
    254      1.1       bsh 		return 1;
    255      1.1       bsh 	else {
    256      1.1       bsh 		/* debounce done. if status has changed, report it */
    257      1.1       bsh 		if (p->reported_state != val) {
    258      1.1       bsh 			p->reported_state = val;
    259      1.1       bsh 			if (p->func) {
    260      1.1       bsh 				s = _splraise(p->level);
    261      1.1       bsh 				p->func(p->arg, val);
    262      1.1       bsh 				splx(s);
    263      1.1       bsh 			}
    264      1.1       bsh 		}
    265      1.1       bsh 		return 0;
    266      1.1       bsh 	}
    267      1.1       bsh 	/*NOTREACHED*/
    268      1.1       bsh }
    269      1.1       bsh 
    270      1.1       bsh static void
    271      1.1       bsh opio_debounce(void *arg)
    272      1.1       bsh {
    273      1.1       bsh 	struct opio_softc *sc = arg;
    274      1.5   thorpej 	struct obio_softc *osc =
    275      1.8       bsh 	    device_private(device_parent(sc->sc_dev));
    276      1.1       bsh 	bus_space_tag_t iot = sc->sc_iot;
    277      1.1       bsh 	bus_space_handle_t ioh = sc->sc_ioh;
    278      1.1       bsh 	int flag = 0;
    279      1.1       bsh 	uint8_t reg;
    280      1.1       bsh 
    281      1.1       bsh 	reg = bus_space_read_1(iot, ioh, GB225_CFDET) & CARDDET_DET;
    282      1.1       bsh 	flag |= do_debounce(sc, OPIO_INTR_CF_INSERT, reg, DEBOUNCE_COUNT);
    283      1.1       bsh 
    284      1.1       bsh 	reg = bus_space_read_1(iot, ioh, GB225_PCMCIADET) & CARDDET_DET;
    285      1.1       bsh 	flag |= do_debounce(sc, OPIO_INTR_PCMCIA_INSERT, reg, DEBOUNCE_COUNT);
    286      1.1       bsh 
    287      1.1       bsh 	reg = bus_space_read_1(iot, ioh, GB225_DEVERROR);
    288      1.1       bsh 
    289      1.1       bsh 	flag |= do_debounce(sc, OPIO_INTR_USB_POWER, reg & DEVERROR_USB, 1);
    290      1.1       bsh 
    291      1.1       bsh 	flag |= do_debounce(sc, OPIO_INTR_CARD_POWER, reg & DEVERROR_CARD, 1);
    292      1.1       bsh 
    293      1.1       bsh 
    294      1.1       bsh 	if (flag) {
    295      1.1       bsh 		/* start debounce timer */
    296      1.1       bsh 		callout_reset(&sc->sc_callout, DEBOUNCE_TICKS,
    297      1.1       bsh 		    opio_debounce, sc);
    298      1.1       bsh 	}
    299      1.1       bsh 	else {
    300      1.1       bsh 		sc->sc_debounce = ST_STABLE;
    301      1.1       bsh 		obio_intr_unmask(osc, sc->sc_ih);
    302      1.1       bsh 	}
    303      1.1       bsh }
    304      1.1       bsh #endif
    305