Home | History | Annotate | Line # | Download | only in dev
it8368.c revision 1.22
      1  1.22    martin /*	$NetBSD: it8368.c,v 1.22 2008/04/28 20:23:21 martin Exp $ */
      2   1.1       uch 
      3  1.10       uch /*-
      4  1.10       uch  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
      5   1.1       uch  * All rights reserved.
      6   1.1       uch  *
      7  1.10       uch  * This code is derived from software contributed to The NetBSD Foundation
      8  1.10       uch  * by UCHIYAMA Yasushi.
      9  1.10       uch  *
     10   1.1       uch  * Redistribution and use in source and binary forms, with or without
     11   1.1       uch  * modification, are permitted provided that the following conditions
     12   1.1       uch  * are met:
     13   1.1       uch  * 1. Redistributions of source code must retain the above copyright
     14   1.1       uch  *    notice, this list of conditions and the following disclaimer.
     15  1.10       uch  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.10       uch  *    notice, this list of conditions and the following disclaimer in the
     17  1.10       uch  *    documentation and/or other materials provided with the distribution.
     18   1.1       uch  *
     19  1.10       uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.10       uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.10       uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.10       uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.10       uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.10       uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.10       uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.10       uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.10       uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.10       uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.10       uch  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       uch  */
     31  1.18     lukem 
     32  1.18     lukem #include <sys/cdefs.h>
     33  1.22    martin __KERNEL_RCSID(0, "$NetBSD: it8368.c,v 1.22 2008/04/28 20:23:21 martin Exp $");
     34  1.10       uch 
     35   1.8       uch #undef WINCE_DEFAULT_SETTING /* for debug */
     36   1.8       uch #undef IT8368DEBUG
     37   1.1       uch 
     38   1.1       uch #include <sys/param.h>
     39   1.1       uch #include <sys/systm.h>
     40   1.1       uch #include <sys/device.h>
     41   1.1       uch 
     42   1.1       uch #include <machine/bus.h>
     43   1.1       uch 
     44   1.1       uch #include <dev/pcmcia/pcmciareg.h>
     45   1.1       uch #include <dev/pcmcia/pcmciavar.h>
     46   1.1       uch #include <dev/pcmcia/pcmciachip.h>
     47   1.1       uch 
     48   1.1       uch #include <hpcmips/tx/tx39var.h>
     49   1.1       uch #include <hpcmips/tx/txcsbusvar.h>
     50   1.6       uch #include <hpcmips/tx/tx39biureg.h> /* legacy mode requires BIU access */
     51   1.6       uch #include <hpcmips/dev/it8368var.h>
     52   1.1       uch #include <hpcmips/dev/it8368reg.h>
     53   1.1       uch 
     54   1.1       uch #ifdef IT8368DEBUG
     55   1.8       uch int	it8368debug = 1;
     56   1.8       uch #define	DPRINTF(arg) if (it8368debug) printf arg;
     57   1.8       uch #define	DPRINTFN(n, arg) if (it8368debug > (n)) printf arg;
     58   1.1       uch #else
     59   1.1       uch #define	DPRINTF(arg)
     60   1.8       uch #define DPRINTFN(n, arg)
     61   1.1       uch #endif
     62   1.1       uch 
     63  1.10       uch int it8368e_match(struct device *, struct cfdata *, void *);
     64  1.10       uch void it8368e_attach(struct device *, struct device *, void *);
     65  1.10       uch int it8368_print(void *, const char *);
     66   1.1       uch 
     67   1.4       uch #define IT8368_LASTSTATE_PRESENT	0x0002
     68   1.4       uch #define IT8368_LASTSTATE_HALF		0x0001
     69   1.7       uch #define IT8368_LASTSTATE_EMPTY		0x0000
     70   1.4       uch 
     71   1.1       uch struct it8368e_softc {
     72   1.1       uch 	struct device	sc_dev;
     73   1.1       uch 	struct device	*sc_pcmcia;
     74   1.1       uch 	tx_chipset_tag_t sc_tc;
     75   1.1       uch 
     76   1.1       uch 	/* Register space */
     77   1.4       uch 	bus_space_tag_t		sc_csregt;
     78   1.4       uch 	bus_space_handle_t	sc_csregh;
     79   1.1       uch 	/* I/O, attribute space */
     80   1.4       uch 	bus_space_tag_t		sc_csiot;
     81   1.4       uch 	bus_addr_t		sc_csiobase;
     82   1.4       uch 	bus_size_t		sc_csiosize;
     83   1.3       uch 	/*
     84   1.3       uch 	 *  XXX theses means attribute memory. not memory space.
     85   1.3       uch 	 *	memory space is 0x64000000.
     86   1.3       uch 	 */
     87   1.4       uch 	bus_space_tag_t		sc_csmemt;
     88   1.4       uch 	bus_addr_t		sc_csmembase;
     89   1.4       uch 	bus_size_t		sc_csmemsize;
     90   1.1       uch 
     91   1.1       uch 	/* Separate I/O and attribute space mode */
     92   1.1       uch 	int sc_fixattr;
     93   1.1       uch 
     94   1.1       uch 	/* Card interrupt handler */
     95  1.10       uch 	int	(*sc_card_fun)(void *);
     96   1.4       uch 	void	*sc_card_arg;
     97   1.4       uch 	void	*sc_card_ih;
     98   1.4       uch 	int	sc_card_irq;
     99   1.4       uch 
    100   1.4       uch 	/* Card status change */
    101   1.4       uch 	int	sc_irq;
    102   1.4       uch 	void	*sc_ih;
    103   1.4       uch 	int	sc_laststate;
    104   1.1       uch };
    105   1.1       uch 
    106  1.10       uch void it8368_init_socket(struct it8368e_softc*);
    107  1.10       uch void it8368_attach_socket(struct it8368e_softc *);
    108  1.10       uch int it8368_intr(void *);
    109  1.10       uch int it8368_chip_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
    110  1.10       uch     struct pcmcia_mem_handle *);
    111  1.10       uch void it8368_chip_mem_free(pcmcia_chipset_handle_t, struct pcmcia_mem_handle *);
    112  1.11     soren int it8368_chip_mem_map(pcmcia_chipset_handle_t, int, bus_size_t, bus_size_t,
    113  1.10       uch     struct pcmcia_mem_handle *, bus_addr_t *, int *);
    114  1.10       uch void it8368_chip_mem_unmap(pcmcia_chipset_handle_t, int);
    115  1.10       uch int it8368_chip_io_alloc(pcmcia_chipset_handle_t, bus_addr_t, bus_size_t,
    116  1.10       uch     bus_size_t, struct pcmcia_io_handle *);
    117  1.10       uch void it8368_chip_io_free(pcmcia_chipset_handle_t, struct pcmcia_io_handle *);
    118  1.10       uch int it8368_chip_io_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
    119  1.10       uch     struct pcmcia_io_handle *, int *);
    120  1.10       uch void it8368_chip_io_unmap(pcmcia_chipset_handle_t, int);
    121  1.10       uch void it8368_chip_socket_enable(pcmcia_chipset_handle_t);
    122  1.10       uch void it8368_chip_socket_disable(pcmcia_chipset_handle_t);
    123  1.10       uch void *it8368_chip_intr_establish(pcmcia_chipset_handle_t,
    124  1.10       uch     struct pcmcia_function *, int, int (*) (void *), void *);
    125  1.10       uch void it8368_chip_intr_disestablish(pcmcia_chipset_handle_t, void *);
    126   1.1       uch 
    127   1.8       uch #ifdef IT8368DEBUG
    128  1.10       uch void it8368_dump(struct it8368e_softc *);
    129   1.8       uch #endif
    130   1.8       uch 
    131   1.1       uch static struct pcmcia_chip_functions it8368_functions = {
    132   1.1       uch 	it8368_chip_mem_alloc,
    133   1.1       uch 	it8368_chip_mem_free,
    134   1.1       uch 	it8368_chip_mem_map,
    135   1.1       uch 	it8368_chip_mem_unmap,
    136   1.1       uch 	it8368_chip_io_alloc,
    137   1.1       uch 	it8368_chip_io_free,
    138   1.1       uch 	it8368_chip_io_map,
    139   1.1       uch 	it8368_chip_io_unmap,
    140   1.1       uch 	it8368_chip_intr_establish,
    141   1.1       uch 	it8368_chip_intr_disestablish,
    142   1.1       uch 	it8368_chip_socket_enable,
    143   1.1       uch 	it8368_chip_socket_disable
    144   1.1       uch };
    145   1.1       uch 
    146  1.16   thorpej CFATTACH_DECL(it8368e, sizeof(struct it8368e_softc),
    147  1.16   thorpej     it8368e_match, it8368e_attach, NULL, NULL);
    148   1.1       uch 
    149   1.1       uch /*
    150   1.1       uch  *	IT8368 configuration register is big-endian.
    151   1.1       uch  */
    152  1.21     perry static inline u_int16_t it8368_reg_read(bus_space_tag_t,
    153  1.10       uch     bus_space_handle_t, int);
    154  1.21     perry static inline void it8368_reg_write(bus_space_tag_t, bus_space_handle_t,
    155  1.10       uch     int, u_int16_t);
    156   1.1       uch 
    157   1.8       uch #ifdef IT8368E_DESTRUCTIVE_CHECK
    158  1.10       uch int	it8368e_id_check(void *);
    159   1.8       uch 
    160   1.8       uch /*
    161   1.8       uch  *	IT8368E don't have identification method. this is destructive check.
    162   1.8       uch  */
    163   1.8       uch int
    164  1.10       uch it8368e_id_check(void *aux)
    165   1.8       uch {
    166   1.8       uch 	struct cs_attach_args *ca = aux;
    167   1.8       uch 	tx_chipset_tag_t tc;
    168   1.8       uch 	bus_space_tag_t csregt;
    169   1.8       uch 	bus_space_handle_t csregh;
    170   1.8       uch 	u_int16_t oreg, reg;
    171   1.8       uch 	int match = 0;
    172   1.8       uch 
    173   1.8       uch 	tc = ca->ca_tc;
    174   1.8       uch 	csregt = ca->ca_csreg.cstag;
    175   1.8       uch 
    176   1.8       uch 	bus_space_map(csregt, ca->ca_csreg.csbase, ca->ca_csreg.cssize,
    177  1.10       uch 	    0, &csregh);
    178   1.8       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_CTRL_REG);
    179   1.8       uch 	oreg = reg;
    180  1.12       uch 	dbg_bit_print(reg);
    181   1.8       uch 
    182   1.8       uch 	reg &= ~IT8368_CTRL_BYTESWAP;
    183   1.8       uch 	it8368_reg_write(csregt, csregh, IT8368_CTRL_REG, reg);
    184   1.8       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_CTRL_REG);
    185   1.8       uch 	if (reg & IT8368_CTRL_BYTESWAP)
    186   1.8       uch 		goto nomatch;
    187   1.8       uch 
    188   1.8       uch 	reg |= IT8368_CTRL_BYTESWAP;
    189   1.8       uch 	it8368_reg_write(csregt, csregh, IT8368_CTRL_REG, reg);
    190   1.8       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_CTRL_REG);
    191   1.8       uch 	if (!(reg & IT8368_CTRL_BYTESWAP))
    192   1.8       uch 		goto nomatch;
    193   1.8       uch 
    194   1.8       uch 	match = 1;
    195   1.8       uch  nomatch:
    196   1.8       uch 	it8368_reg_write(csregt, csregh, IT8368_CTRL_REG, oreg);
    197   1.8       uch 	bus_space_unmap(csregt, csregh, ca->ca_csreg.cssize);
    198   1.8       uch 
    199   1.8       uch 	return (match);
    200   1.8       uch }
    201   1.8       uch #endif /* IT8368E_DESTRUCTIVE_CHECK */
    202   1.4       uch 
    203   1.1       uch int
    204  1.10       uch it8368e_match(struct device *parent, struct cfdata *cf, void *aux)
    205   1.1       uch {
    206   1.8       uch #ifdef IT8368E_DESTRUCTIVE_CHECK
    207   1.8       uch 	return (it8368e_id_check(aux));
    208   1.8       uch #else
    209   1.8       uch 	return (1);
    210   1.8       uch #endif
    211   1.1       uch }
    212   1.1       uch 
    213   1.1       uch void
    214  1.10       uch it8368e_attach(struct device *parent, struct device *self, void *aux)
    215   1.1       uch {
    216   1.1       uch 	struct cs_attach_args *ca = aux;
    217   1.1       uch 	struct it8368e_softc *sc = (void*)self;
    218   1.1       uch 	tx_chipset_tag_t tc;
    219   1.1       uch 	bus_space_tag_t csregt;
    220   1.1       uch 	bus_space_handle_t csregh;
    221   1.1       uch 	u_int16_t reg;
    222   1.1       uch 
    223   1.1       uch 	sc->sc_tc = tc = ca->ca_tc;
    224   1.1       uch 	sc->sc_csregt = csregt = ca->ca_csreg.cstag;
    225   1.1       uch 
    226   1.1       uch 	bus_space_map(csregt, ca->ca_csreg.csbase, ca->ca_csreg.cssize,
    227  1.10       uch 	    0, &sc->sc_csregh);
    228   1.1       uch 	csregh = sc->sc_csregh;
    229   1.1       uch 	sc->sc_csiot = ca->ca_csio.cstag;
    230   1.1       uch 	sc->sc_csiobase = ca->ca_csio.csbase;
    231   1.1       uch 	sc->sc_csiosize = ca->ca_csio.cssize;
    232   1.1       uch 
    233   1.3       uch #ifdef IT8368DEBUG
    234   1.4       uch 	printf("\n\t[Windows CE setting]\n");
    235   1.1       uch 	it8368_dump(sc); /* print WindowsCE setting */
    236   1.3       uch #endif
    237   1.1       uch 	/* LHA[14:13] <= HA[14:13]	*/
    238   1.1       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_CTRL_REG);
    239   1.1       uch 	reg &= ~IT8368_CTRL_ADDRSEL;
    240   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_CTRL_REG, reg);
    241   1.1       uch 
    242   1.1       uch 	/* Set all MFIO direction as LHA[23:13] output pins */
    243   1.1       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_MFIODIR_REG);
    244   1.1       uch 	reg |= IT8368_MFIODIR_MASK;
    245   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_MFIODIR_REG, reg);
    246   1.1       uch 
    247   1.1       uch 	/* Set all MFIO functions as LHA */
    248   1.1       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_MFIOSEL_REG);
    249   1.1       uch 	reg &= ~IT8368_MFIOSEL_MASK;
    250   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_MFIOSEL_REG, reg);
    251   1.1       uch 
    252   1.1       uch 	/* Disable MFIO interrupt */
    253   1.1       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_MFIOPOSINTEN_REG);
    254   1.1       uch 	reg &= ~IT8368_MFIOPOSINTEN_MASK;
    255   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_MFIOPOSINTEN_REG, reg);
    256   1.1       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_MFIONEGINTEN_REG);
    257   1.1       uch 	reg &= ~IT8368_MFIONEGINTEN_MASK;
    258   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_MFIONEGINTEN_REG, reg);
    259   1.1       uch 
    260   1.1       uch 	/* Port direction */
    261   1.1       uch 	reg = IT8368_PIN_CRDVCCON1 | IT8368_PIN_CRDVCCON0 |
    262  1.10       uch 	    IT8368_PIN_CRDVPPON1 | IT8368_PIN_CRDVPPON0 |
    263  1.10       uch 	    IT8368_PIN_BCRDRST;
    264   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_GPIODIR_REG, reg);
    265   1.5       uch 	printf("\n");
    266   1.5       uch 
    267   1.1       uch 	/*
    268   1.1       uch 	 *	Separate I/O and attribute memory region
    269   1.1       uch 	 */
    270   1.1       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_CTRL_REG);
    271   1.8       uch 
    272   1.1       uch 	reg |= IT8368_CTRL_FIXATTRIO;
    273   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_CTRL_REG, reg);
    274   1.8       uch 
    275   1.6       uch 	if (IT8368_CTRL_FIXATTRIO &
    276   1.6       uch 	    it8368_reg_read(csregt, csregh, IT8368_CTRL_REG)) {
    277   1.1       uch 		sc->sc_fixattr = 1;
    278   1.5       uch 		printf("%s: fix attr mode\n", sc->sc_dev.dv_xname);
    279   1.1       uch 	} else {
    280   1.1       uch 		sc->sc_fixattr = 0;
    281   1.6       uch 		printf("%s: legacy attr mode\n", sc->sc_dev.dv_xname);
    282   1.1       uch 	}
    283   1.8       uch 
    284   1.6       uch 	sc->sc_csmemt = sc->sc_csiot;
    285   1.6       uch 	sc->sc_csiosize /= 2;
    286   1.6       uch 	sc->sc_csmemsize = sc->sc_csiosize;
    287   1.6       uch 	sc->sc_csmembase = sc->sc_csiosize;
    288   1.6       uch 
    289   1.7       uch #ifdef IT8368DEBUG
    290   1.1       uch 	it8368_dump(sc);
    291   1.7       uch #endif
    292   1.4       uch 	/* Enable card and interrupt driving. */
    293   1.4       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_CTRL_REG);
    294   1.4       uch 	reg |= (IT8368_CTRL_GLOBALEN | IT8368_CTRL_CARDEN);
    295   1.4       uch 	if (sc->sc_fixattr)
    296   1.4       uch 		reg |= IT8368_CTRL_FIXATTRIO;
    297   1.4       uch 	it8368_reg_write(csregt, csregh, IT8368_CTRL_REG, reg);
    298   1.4       uch 
    299   1.4       uch 	sc->sc_irq = ca->ca_irq1;
    300   1.1       uch 	sc->sc_card_irq = ca->ca_irq3;
    301   1.1       uch 
    302   1.1       uch 	it8368_attach_socket(sc);
    303   1.1       uch }
    304   1.1       uch 
    305  1.21     perry inline u_int16_t
    306  1.10       uch it8368_reg_read(bus_space_tag_t t, bus_space_handle_t h, int ofs)
    307   1.1       uch {
    308   1.1       uch 	u_int16_t val;
    309   1.1       uch 
    310   1.1       uch 	val = bus_space_read_2(t, h, ofs);
    311  1.10       uch 	return (0xffff & (((val >> 8) & 0xff)|((val << 8) & 0xff00)));
    312   1.1       uch }
    313   1.1       uch 
    314  1.21     perry inline void
    315  1.10       uch it8368_reg_write(bus_space_tag_t t, bus_space_handle_t h, int ofs, u_int16_t v)
    316   1.1       uch {
    317   1.1       uch 	u_int16_t val;
    318   1.1       uch 
    319   1.1       uch 	val = 0xffff & (((v >> 8) & 0xff)|((v << 8) & 0xff00));
    320   1.1       uch 	bus_space_write_2(t, h, ofs, val);
    321   1.1       uch }
    322   1.1       uch 
    323   1.1       uch int
    324  1.10       uch it8368_intr(void *arg)
    325   1.1       uch {
    326   1.1       uch 	struct it8368e_softc *sc = arg;
    327   1.4       uch 	bus_space_tag_t csregt = sc->sc_csregt;
    328   1.4       uch 	bus_space_handle_t csregh = sc->sc_csregh;
    329   1.4       uch 	u_int16_t reg;
    330   1.3       uch 
    331   1.4       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_GPIONEGINTSTAT_REG);
    332   1.3       uch 
    333   1.4       uch 	if (reg & IT8368_PIN_BCRDRDY) {
    334   1.4       uch 		if (sc->sc_card_fun) {
    335   1.4       uch 			/* clear interrupt */
    336   1.4       uch 			it8368_reg_write(csregt, csregh,
    337  1.10       uch 			    IT8368_GPIONEGINTSTAT_REG,
    338  1.10       uch 			    IT8368_PIN_BCRDRDY);
    339   1.4       uch 
    340   1.4       uch 			/* Dispatch card interrupt handler */
    341   1.4       uch 			(*sc->sc_card_fun)(sc->sc_card_arg);
    342   1.4       uch 		}
    343   1.4       uch 	} else if (reg & IT8368_PIN_CRDDET2) {
    344   1.4       uch 		it8368_reg_write(csregt, csregh, IT8368_GPIONEGINTSTAT_REG,
    345  1.10       uch 		    IT8368_PIN_CRDDET2);
    346   1.4       uch 		printf("[CSC]\n");
    347   1.7       uch #ifdef IT8368DEBUG
    348   1.4       uch 		it8368_dump(sc);
    349   1.7       uch #endif
    350   1.4       uch 		it8368_chip_socket_disable(sc);
    351   1.4       uch 	} else {
    352   1.7       uch #ifdef IT8368DEBUG
    353   1.8       uch 		u_int16_t reg2;
    354   1.8       uch 		reg2 = reg & ~(IT8368_PIN_BCRDRDY|IT8368_PIN_CRDDET2);
    355   1.8       uch 		printf("unknown it8368 interrupt: ");
    356  1.12       uch 		dbg_bit_print(reg2);
    357   1.8       uch 		it8368_reg_write(csregt, csregh, IT8368_GPIONEGINTSTAT_REG,
    358  1.10       uch 		    reg);
    359   1.7       uch #endif
    360   1.1       uch 	}
    361   1.4       uch 
    362  1.10       uch 	return (0);
    363   1.1       uch }
    364   1.1       uch 
    365   1.1       uch int
    366  1.10       uch it8368_print(void *arg, const char *pnp)
    367   1.1       uch {
    368   1.3       uch 	if (pnp)
    369  1.17   thorpej 		aprint_normal("pcmcia at %s", pnp);
    370   1.1       uch 
    371  1.10       uch 	return (UNCONF);
    372   1.1       uch }
    373   1.1       uch 
    374   1.1       uch void
    375  1.10       uch it8368_attach_socket(struct it8368e_softc *sc)
    376   1.1       uch {
    377   1.1       uch 	struct pcmciabus_attach_args paa;
    378   1.1       uch 
    379   1.1       uch 	paa.paa_busname = "pcmcia";
    380   1.1       uch 	paa.pct = (pcmcia_chipset_tag_t)&it8368_functions;
    381   1.1       uch 	paa.pch = (pcmcia_chipset_handle_t)sc;
    382   1.9       uch 	paa.iobase = 0;
    383   1.9       uch 	paa.iosize = sc->sc_csiosize;
    384   1.1       uch 
    385  1.19  drochner 	if ((sc->sc_pcmcia = config_found_ia((void*)sc, "pcmciabus", &paa,
    386  1.19  drochner 					     it8368_print))) {
    387   1.4       uch 		it8368_init_socket(sc);
    388   1.4       uch 	}
    389   1.4       uch }
    390   1.4       uch 
    391   1.4       uch void
    392  1.10       uch it8368_init_socket(struct it8368e_softc *sc)
    393   1.4       uch {
    394   1.4       uch 	bus_space_tag_t csregt = sc->sc_csregt;
    395   1.4       uch 	bus_space_handle_t csregh = sc->sc_csregh;
    396   1.4       uch 	u_int16_t reg;
    397   1.4       uch 
    398   1.4       uch 	/*
    399   1.4       uch 	 *  set up the card to interrupt on card detect
    400   1.4       uch 	 */
    401   1.4       uch 	reg = IT8368_PIN_CRDDET2; /* CSC */
    402   1.4       uch 	/* enable negative edge */
    403   1.4       uch 	it8368_reg_write(csregt, csregh, IT8368_GPIONEGINTEN_REG, reg);
    404   1.4       uch 	/* disable positive edge */
    405   1.4       uch 	it8368_reg_write(csregt, csregh, IT8368_GPIOPOSINTEN_REG, 0);
    406   1.4       uch 
    407   1.4       uch 	sc->sc_ih = tx_intr_establish(sc->sc_tc, sc->sc_irq,
    408  1.10       uch 	    IST_EDGE, IPL_BIO, it8368_intr, sc);
    409   1.4       uch 	if (sc->sc_ih == NULL) {
    410   1.4       uch 		printf("%s: can't establish interrupt\n",
    411  1.10       uch 		    sc->sc_dev.dv_xname);
    412   1.4       uch 		return;
    413   1.4       uch 	}
    414   1.4       uch 
    415   1.4       uch 	/*
    416   1.4       uch 	 *  if there's a card there, then attach it.
    417   1.4       uch 	 */
    418   1.4       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_GPIODATAIN_REG);
    419   1.4       uch 
    420   1.4       uch 	if (reg & (IT8368_PIN_CRDDET2|IT8368_PIN_CRDDET1)) {
    421   1.4       uch 		sc->sc_laststate = IT8368_LASTSTATE_EMPTY;
    422   1.4       uch 	} else {
    423   1.4       uch 		pcmcia_card_attach(sc->sc_pcmcia);
    424   1.4       uch 		sc->sc_laststate = IT8368_LASTSTATE_PRESENT;
    425   1.1       uch 	}
    426   1.1       uch }
    427   1.1       uch 
    428   1.1       uch void *
    429  1.10       uch it8368_chip_intr_establish(pcmcia_chipset_handle_t pch,
    430  1.10       uch     struct pcmcia_function *pf, int ipl, int (*ih_fun)(void *), void *ih_arg)
    431   1.1       uch {
    432   1.1       uch 	struct it8368e_softc *sc = (struct it8368e_softc*) pch;
    433   1.4       uch 	bus_space_tag_t csregt = sc->sc_csregt;
    434   1.4       uch 	bus_space_handle_t csregh = sc->sc_csregh;
    435   1.4       uch 	u_int16_t reg;
    436   1.1       uch 
    437   1.4       uch 	if (sc->sc_card_fun)
    438   1.3       uch 		panic("it8368_chip_intr_establish: "
    439  1.10       uch 		    "duplicate card interrupt handler.");
    440   1.4       uch 
    441   1.1       uch 	sc->sc_card_fun = ih_fun;
    442   1.1       uch 	sc->sc_card_arg = ih_arg;
    443   1.1       uch 
    444   1.4       uch 	sc->sc_card_ih = tx_intr_establish(sc->sc_tc, sc->sc_card_irq,
    445  1.10       uch 	    IST_EDGE, IPL_BIO, it8368_intr,
    446  1.10       uch 	    sc);
    447   1.4       uch 
    448   1.4       uch 	/* enable card interrupt */
    449   1.4       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_GPIONEGINTEN_REG);
    450   1.4       uch 	reg |= IT8368_PIN_BCRDRDY;
    451   1.4       uch 	it8368_reg_write(csregt, csregh, IT8368_GPIONEGINTEN_REG, reg);
    452   1.4       uch 
    453  1.10       uch 	return (sc->sc_card_ih);
    454   1.1       uch }
    455   1.1       uch 
    456   1.1       uch void
    457  1.10       uch it8368_chip_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
    458   1.1       uch {
    459   1.1       uch 	struct it8368e_softc *sc = (struct it8368e_softc*) pch;
    460   1.4       uch 	bus_space_tag_t csregt = sc->sc_csregt;
    461   1.4       uch 	bus_space_handle_t csregh = sc->sc_csregh;
    462   1.4       uch 	u_int16_t reg;
    463   1.1       uch 
    464   1.4       uch 	if (!sc->sc_card_fun)
    465   1.3       uch 		panic("it8368_chip_intr_disestablish:"
    466  1.10       uch 		    "no handler established.");
    467   1.4       uch 	assert(ih == sc->sc_card_ih);
    468   1.4       uch 
    469   1.1       uch 	sc->sc_card_fun = 0;
    470   1.1       uch 	sc->sc_card_arg = 0;
    471   1.1       uch 
    472   1.4       uch 	/* disable card interrupt */
    473   1.4       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_GPIONEGINTEN_REG);
    474   1.4       uch 	reg &= ~IT8368_PIN_BCRDRDY;
    475   1.4       uch 	it8368_reg_write(csregt, csregh, IT8368_GPIONEGINTEN_REG, reg);
    476   1.4       uch 
    477   1.1       uch 	tx_intr_disestablish(sc->sc_tc, ih);
    478   1.1       uch }
    479   1.1       uch 
    480   1.1       uch int
    481  1.10       uch it8368_chip_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
    482  1.10       uch     struct pcmcia_mem_handle *pcmhp)
    483   1.1       uch {
    484   1.1       uch 	struct it8368e_softc *sc = (struct it8368e_softc*) pch;
    485   1.1       uch 
    486   1.6       uch 	if (bus_space_alloc(sc->sc_csmemt, sc->sc_csmembase,
    487  1.10       uch 	    sc->sc_csmembase + sc->sc_csmemsize, size,
    488  1.10       uch 	    size, 0, 0, 0, &pcmhp->memh)) {
    489   1.6       uch 		DPRINTF(("it8368_chip_mem_alloc: failed\n"));
    490  1.10       uch 		return (1);
    491   1.1       uch 	}
    492   1.3       uch 
    493   1.6       uch 	if (!sc->sc_fixattr) /* XXX IT8368 brain damaged spec */
    494   1.6       uch 		pcmhp->memh -= sc->sc_csmembase;
    495   1.6       uch 
    496   1.6       uch 	pcmhp->memt = sc->sc_csmemt;
    497   1.1       uch 	pcmhp->addr = pcmhp->memh;
    498   1.1       uch 	pcmhp->size = size;
    499   1.1       uch 	pcmhp->realsize = size;
    500   1.3       uch 
    501   1.8       uch 	DPRINTF(("it8368_chip_mem_alloc: %#x+%#x\n",
    502  1.10       uch 	    (unsigned)pcmhp->memh, (unsigned)size));
    503   1.1       uch 
    504  1.10       uch 	return (0);
    505   1.1       uch }
    506   1.1       uch 
    507   1.1       uch void
    508  1.10       uch it8368_chip_mem_free(pcmcia_chipset_handle_t pch,
    509  1.10       uch     struct pcmcia_mem_handle *pcmhp)
    510   1.1       uch {
    511   1.6       uch 	struct it8368e_softc *sc = (struct it8368e_softc*) pch;
    512   1.6       uch 
    513   1.8       uch 	DPRINTF(("it8368_chip_mem_free: %#x+%#x\n",
    514  1.10       uch 	    (unsigned)pcmhp->memh, (unsigned)pcmhp->size));
    515   1.8       uch 
    516   1.6       uch 	if (!sc->sc_fixattr) /* XXX IT8368 brain damaged spec */
    517   1.6       uch 		pcmhp->memh += sc->sc_csmembase;
    518   1.6       uch 
    519   1.1       uch 	bus_space_unmap(pcmhp->memt, pcmhp->memh, pcmhp->size);
    520   1.1       uch }
    521   1.1       uch 
    522   1.1       uch int
    523  1.10       uch it8368_chip_mem_map(pcmcia_chipset_handle_t pch, int kind,
    524  1.10       uch     bus_addr_t card_addr, bus_size_t size, struct pcmcia_mem_handle *pcmhp,
    525  1.11     soren     bus_size_t *offsetp, int *windowp)
    526   1.1       uch {
    527   1.6       uch 	/* attribute mode */
    528   1.6       uch 	it8368_mode(pch, IT8368_ATTR_MODE, IT8368_WIDTH_16);
    529   1.1       uch 
    530   1.3       uch 	*offsetp = card_addr;
    531   1.8       uch 	DPRINTF(("it8368_chip_mem_map %#x+%#x\n",
    532  1.10       uch 	    (unsigned)pcmhp->memh, (unsigned)size));
    533   1.3       uch 
    534  1.10       uch 	return (0);
    535   1.1       uch }
    536   1.1       uch 
    537   1.1       uch void
    538  1.10       uch it8368_chip_mem_unmap(pcmcia_chipset_handle_t pch, int window)
    539   1.1       uch {
    540   1.6       uch 	/* return to I/O mode */
    541   1.6       uch 	it8368_mode(pch, IT8368_IO_MODE, IT8368_WIDTH_16);
    542   1.1       uch }
    543   1.1       uch 
    544   1.1       uch void
    545  1.10       uch it8368_mode(pcmcia_chipset_handle_t pch, int io, int width)
    546   1.1       uch {
    547   1.6       uch 	struct it8368e_softc *sc = (struct it8368e_softc*) pch;
    548   1.1       uch 	txreg_t reg32;
    549   1.1       uch 
    550   1.6       uch 	DPRINTF(("it8368_mode: change access space to "));
    551   1.8       uch 	DPRINTF((io ? "I/O (%dbit)\n" : "attribute (%dbit)...\n",
    552  1.10       uch 	    width == IT8368_WIDTH_8 ? 8 : 16));
    553   1.6       uch 
    554   1.1       uch 	reg32 = tx_conf_read(sc->sc_tc, TX39_MEMCONFIG3_REG);
    555   1.8       uch 
    556   1.6       uch 	if (io) {
    557   1.8       uch 		if (width == IT8368_WIDTH_8)
    558   1.6       uch 			reg32 |= TX39_MEMCONFIG3_PORT8SEL;
    559   1.6       uch 		else
    560   1.6       uch 			reg32 &= ~TX39_MEMCONFIG3_PORT8SEL;
    561   1.1       uch 	}
    562   1.6       uch 
    563   1.1       uch 	if (!sc->sc_fixattr) {
    564   1.6       uch 		if (io)
    565   1.1       uch 			reg32 |= TX39_MEMCONFIG3_CARD1IOEN;
    566   1.6       uch 		else
    567   1.1       uch 			reg32 &= ~TX39_MEMCONFIG3_CARD1IOEN;
    568   1.1       uch 	}
    569   1.1       uch 	tx_conf_write(sc->sc_tc, TX39_MEMCONFIG3_REG, reg32);
    570   1.1       uch 
    571   1.8       uch #ifdef IT8368DEBUG
    572   1.8       uch 	if (sc->sc_fixattr)
    573   1.8       uch 		return; /* No need to report BIU status */
    574   1.8       uch 
    575   1.8       uch 	/* check BIU status */
    576   1.1       uch 	reg32 = tx_conf_read(sc->sc_tc, TX39_MEMCONFIG3_REG);
    577   1.8       uch 	if (reg32 & TX39_MEMCONFIG3_CARD1IOEN) {
    578   1.8       uch 		DPRINTF(("it8368_mode: I/O space (%dbit) enabled\n",
    579  1.10       uch 		    reg32 & TX39_MEMCONFIG3_PORT8SEL ? 8 : 16));
    580   1.8       uch 	} else {
    581   1.6       uch 		DPRINTF(("it8368_mode: atttribute space enabled\n"));
    582   1.8       uch 	}
    583   1.8       uch #endif /* IT8368DEBUG */
    584   1.1       uch }
    585   1.1       uch 
    586   1.1       uch int
    587  1.10       uch it8368_chip_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start,
    588  1.10       uch     bus_size_t size, bus_size_t align, struct pcmcia_io_handle *pcihp)
    589   1.1       uch {
    590   1.1       uch 	struct it8368e_softc *sc = (struct it8368e_softc*) pch;
    591   1.1       uch 
    592   1.1       uch 	if (start) {
    593   1.3       uch 		if (bus_space_map(sc->sc_csiot, start, size, 0,
    594  1.10       uch 		    &pcihp->ioh)) {
    595  1.10       uch 			return (1);
    596   1.1       uch 		}
    597   1.1       uch 		DPRINTF(("it8368_chip_io_alloc map port %#x+%#x\n",
    598  1.10       uch 		    (unsigned)start, (unsigned)size));
    599   1.1       uch 	} else {
    600   1.1       uch 		if (bus_space_alloc(sc->sc_csiot, sc->sc_csiobase,
    601  1.10       uch 		    sc->sc_csiobase + sc->sc_csiosize,
    602  1.10       uch 		    size, align, 0, 0, &pcihp->addr,
    603  1.10       uch 		    &pcihp->ioh)) {
    604   1.3       uch 
    605  1.10       uch 			return (1);
    606   1.1       uch 		}
    607   1.1       uch 		pcihp->flags = PCMCIA_IO_ALLOCATED;
    608   1.1       uch 		DPRINTF(("it8368_chip_io_alloc alloc %#x from %#x\n",
    609  1.10       uch 		    (unsigned)size, (unsigned)pcihp->addr));
    610   1.2       uch 	}
    611   1.1       uch 
    612   1.1       uch 	pcihp->iot = sc->sc_csiot;
    613   1.1       uch 	pcihp->size = size;
    614   1.1       uch 
    615  1.10       uch 	return (0);
    616   1.1       uch }
    617   1.1       uch 
    618   1.1       uch int
    619  1.10       uch it8368_chip_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
    620  1.10       uch     bus_size_t size, struct pcmcia_io_handle *pcihp, int *windowp)
    621   1.1       uch {
    622   1.6       uch 	/* I/O mode */
    623   1.6       uch 	it8368_mode(pch, IT8368_IO_MODE, IT8368_WIDTH_16);
    624   1.1       uch 
    625   1.8       uch 	DPRINTF(("it8368_chip_io_map %#x:%#x+%#x\n",
    626  1.10       uch 	    (unsigned)pcihp->ioh, (unsigned)offset, (unsigned)size));
    627   1.1       uch 
    628  1.10       uch 	return (0);
    629   1.1       uch }
    630   1.1       uch 
    631   1.1       uch void
    632  1.10       uch it8368_chip_io_free(pcmcia_chipset_handle_t pch,
    633  1.10       uch     struct pcmcia_io_handle *pcihp)
    634   1.1       uch {
    635   1.6       uch 	if (pcihp->flags & PCMCIA_IO_ALLOCATED)
    636   1.1       uch 		bus_space_free(pcihp->iot, pcihp->ioh, pcihp->size);
    637   1.6       uch 	else
    638   1.1       uch 		bus_space_unmap(pcihp->iot, pcihp->ioh, pcihp->size);
    639   1.6       uch 
    640   1.8       uch 	DPRINTF(("it8368_chip_io_free %#x+%#x\n",
    641  1.10       uch 	    (unsigned)pcihp->ioh, (unsigned)pcihp->size));
    642   1.1       uch }
    643   1.1       uch 
    644   1.1       uch void
    645  1.10       uch it8368_chip_io_unmap(pcmcia_chipset_handle_t pch, int window)
    646   1.1       uch {
    647  1.10       uch 
    648   1.1       uch }
    649   1.1       uch 
    650   1.1       uch void
    651  1.10       uch it8368_chip_socket_enable(pcmcia_chipset_handle_t pch)
    652   1.1       uch {
    653   1.8       uch #ifndef WINCE_DEFAULT_SETTING
    654   1.1       uch 	struct it8368e_softc *sc = (struct it8368e_softc*)pch;
    655   1.1       uch 	bus_space_tag_t csregt = sc->sc_csregt;
    656   1.1       uch 	bus_space_handle_t csregh = sc->sc_csregh;
    657   1.1       uch 	volatile u_int16_t reg;
    658   1.3       uch 
    659   1.1       uch 	/* Power off */
    660   1.1       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_GPIODATAOUT_REG);
    661   1.1       uch 	reg &= ~(IT8368_PIN_CRDVCCMASK | IT8368_PIN_CRDVPPMASK);
    662   1.1       uch 	reg |= (IT8368_PIN_CRDVCC_0V | IT8368_PIN_CRDVPP_0V);
    663   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_GPIODATAOUT_REG, reg);
    664   1.1       uch 	delay(20000);
    665   1.1       uch 
    666   1.1       uch 	/*
    667   1.1       uch 	 * wait 300ms until power fails (Tpf).  Then, wait 100ms since
    668   1.1       uch 	 * we are changing Vcc (Toff).
    669   1.1       uch 	 */
    670   1.1       uch 	delay((300 + 100) * 1000);
    671   1.1       uch 
    672   1.1       uch 	/* Supply Vcc */
    673   1.1       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_GPIODATAOUT_REG);
    674   1.1       uch 	reg &= ~(IT8368_PIN_CRDVCCMASK | IT8368_PIN_CRDVPPMASK);
    675   1.1       uch 	reg |= IT8368_PIN_CRDVCC_5V; /* XXX */
    676   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_GPIODATAOUT_REG, reg);
    677   1.1       uch 
    678   1.1       uch 	/*
    679   1.1       uch 	 * wait 100ms until power raise (Tpr) and 20ms to become
    680   1.1       uch 	 * stable (Tsu(Vcc)).
    681   1.1       uch 	 *
    682   1.1       uch 	 * some machines require some more time to be settled
    683   1.1       uch 	 * (300ms is added here).
    684   1.1       uch 	 */
    685   1.1       uch 	delay((100 + 20 + 300) * 1000);
    686   1.1       uch 
    687   1.1       uch 	/* Assert reset signal */
    688   1.1       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_GPIODATAOUT_REG);
    689   1.1       uch 	reg |= IT8368_PIN_BCRDRST;
    690   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_GPIODATAOUT_REG, reg);
    691   1.4       uch 
    692   1.1       uch 	/*
    693   1.1       uch 	 * hold RESET at least 10us.
    694   1.1       uch 	 */
    695   1.1       uch 	delay(10);
    696   1.4       uch 
    697   1.8       uch 	/* deassert reset signal */
    698   1.1       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_GPIODATAOUT_REG);
    699   1.1       uch 	reg &= ~IT8368_PIN_BCRDRST;
    700   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_GPIODATAOUT_REG, reg);
    701   1.1       uch 	delay(20000);
    702   1.1       uch 
    703   1.6       uch 	DPRINTF(("it8368_chip_socket_enable: socket enabled\n"));
    704   1.8       uch #endif /* !WINCE_DEFAULT_SETTING */
    705   1.1       uch }
    706   1.1       uch 
    707   1.1       uch void
    708  1.10       uch it8368_chip_socket_disable(pcmcia_chipset_handle_t pch)
    709   1.1       uch {
    710   1.8       uch #ifndef WINCE_DEFAULT_SETTING
    711   1.1       uch 	struct it8368e_softc *sc = (struct it8368e_softc*) pch;
    712   1.1       uch 	bus_space_tag_t csregt = sc->sc_csregt;
    713   1.1       uch 	bus_space_handle_t csregh = sc->sc_csregh;
    714   1.1       uch 	u_int16_t reg;
    715   1.1       uch 
    716   1.1       uch 	/* Power down */
    717   1.1       uch 	reg = it8368_reg_read(csregt, csregh, IT8368_GPIODATAOUT_REG);
    718   1.1       uch 	reg &= ~(IT8368_PIN_CRDVCCMASK | IT8368_PIN_CRDVPPMASK);
    719   1.1       uch 	reg |= (IT8368_PIN_CRDVCC_0V | IT8368_PIN_CRDVPP_0V);
    720   1.1       uch 	it8368_reg_write(csregt, csregh, IT8368_GPIODATAOUT_REG, reg);
    721   1.1       uch 	delay(20000);
    722   1.1       uch 
    723   1.1       uch 	/*
    724   1.1       uch 	 * wait 300ms until power fails (Tpf).
    725   1.1       uch 	 */
    726   1.1       uch 	delay(300 * 1000);
    727   1.4       uch 
    728   1.6       uch 	DPRINTF(("it8368_chip_socket_disable: socket disabled\n"));
    729   1.8       uch #endif /* !WINCE_DEFAULT_SETTING */
    730   1.1       uch }
    731   1.1       uch 
    732   1.7       uch #ifdef IT8368DEBUG
    733  1.12       uch #define PRINTGPIO(m) __dbg_bit_print(it8368_reg_read(csregt, csregh,		\
    734  1.13  takemura 	IT8368_GPIO##m##_REG), 0, IT8368_GPIO_MAX, #m, DBG_BIT_PRINT_COUNT)
    735  1.12       uch #define PRINTMFIO(m) __dbg_bit_print(it8368_reg_read(csregt, csregh,		\
    736  1.13  takemura 	IT8368_MFIO##m##_REG), 0, IT8368_MFIO_MAX, #m, DBG_BIT_PRINT_COUNT)
    737   1.1       uch void
    738  1.10       uch it8368_dump(struct it8368e_softc *sc)
    739   1.1       uch {
    740   1.1       uch 	bus_space_tag_t csregt = sc->sc_csregt;
    741   1.1       uch 	bus_space_handle_t csregh = sc->sc_csregh;
    742   1.1       uch 
    743   1.1       uch 	printf("[GPIO]\n");
    744   1.1       uch 	PRINTGPIO(DIR);
    745   1.1       uch 	PRINTGPIO(DATAIN);
    746   1.1       uch 	PRINTGPIO(DATAOUT);
    747   1.1       uch 	PRINTGPIO(POSINTEN);
    748   1.1       uch 	PRINTGPIO(NEGINTEN);
    749   1.1       uch 	PRINTGPIO(POSINTSTAT);
    750   1.1       uch 	PRINTGPIO(NEGINTSTAT);
    751   1.1       uch 	printf("[MFIO]\n");
    752   1.1       uch 	PRINTMFIO(SEL);
    753   1.1       uch 	PRINTMFIO(DIR);
    754   1.1       uch 	PRINTMFIO(DATAIN);
    755   1.1       uch 	PRINTMFIO(DATAOUT);
    756   1.1       uch 	PRINTMFIO(POSINTEN);
    757   1.1       uch 	PRINTMFIO(NEGINTEN);
    758   1.1       uch 	PRINTMFIO(POSINTSTAT);
    759   1.1       uch 	PRINTMFIO(NEGINTSTAT);
    760  1.12       uch 	__dbg_bit_print(it8368_reg_read(csregt, csregh, IT8368_CTRL_REG), 0, 15,
    761  1.13  takemura 	    "CTRL", DBG_BIT_PRINT_COUNT);
    762  1.12       uch 	__dbg_bit_print(it8368_reg_read(csregt, csregh, IT8368_GPIODATAIN_REG),
    763  1.13  takemura 	    8, 11, "]CRDDET/SENSE[", DBG_BIT_PRINT_COUNT);
    764   1.1       uch }
    765   1.7       uch #endif /* IT8368DEBUG */
    766