Home | History | Annotate | Line # | Download | only in hd64465
hd64465pcmcia.c revision 1.12.2.4
      1  1.12.2.4    skrll /*	$NetBSD: hd64465pcmcia.c,v 1.12.2.4 2004/09/21 13:16:25 skrll Exp $	*/
      2       1.1      uch 
      3       1.1      uch /*-
      4       1.1      uch  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
      5       1.1      uch  * All rights reserved.
      6       1.1      uch  *
      7       1.1      uch  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1      uch  * by UCHIYAMA Yasushi.
      9       1.1      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.1      uch  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      uch  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      uch  *    documentation and/or other materials provided with the distribution.
     18       1.1      uch  * 3. All advertising materials mentioning features or use of this software
     19       1.1      uch  *    must display the following acknowledgement:
     20       1.1      uch  *        This product includes software developed by the NetBSD
     21       1.1      uch  *        Foundation, Inc. and its contributors.
     22       1.1      uch  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1      uch  *    contributors may be used to endorse or promote products derived
     24       1.1      uch  *    from this software without specific prior written permission.
     25       1.1      uch  *
     26       1.1      uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1      uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1      uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1      uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1      uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1      uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1      uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1      uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1      uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1      uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1      uch  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1      uch  */
     38       1.1      uch 
     39  1.12.2.1    skrll #include <sys/cdefs.h>
     40  1.12.2.4    skrll __KERNEL_RCSID(0, "$NetBSD: hd64465pcmcia.c,v 1.12.2.4 2004/09/21 13:16:25 skrll Exp $");
     41  1.12.2.1    skrll 
     42       1.1      uch #include <sys/param.h>
     43       1.1      uch #include <sys/systm.h>
     44       1.1      uch #include <sys/device.h>
     45       1.1      uch #include <sys/malloc.h>
     46       1.1      uch #include <sys/kthread.h>
     47       1.1      uch #include <sys/boot_flag.h>
     48       1.1      uch 
     49       1.1      uch #include <uvm/uvm_extern.h>
     50       1.1      uch 
     51       1.1      uch #include <machine/bus.h>
     52       1.1      uch #include <machine/intr.h>
     53       1.1      uch 
     54       1.1      uch #include <dev/pcmcia/pcmciareg.h>
     55       1.1      uch #include <dev/pcmcia/pcmciavar.h>
     56       1.1      uch #include <dev/pcmcia/pcmciachip.h>
     57       1.1      uch 
     58       1.1      uch #include <sh3/bscreg.h>
     59       1.5      uch #include <sh3/mmu.h>
     60       1.1      uch 
     61       1.1      uch #include <hpcsh/dev/hd64465/hd64465reg.h>
     62       1.1      uch #include <hpcsh/dev/hd64465/hd64465var.h>
     63       1.4      uch #include <hpcsh/dev/hd64465/hd64465intcreg.h>
     64       1.1      uch #include <hpcsh/dev/hd64461/hd64461pcmciareg.h>
     65       1.1      uch 
     66       1.1      uch #include "locators.h"
     67       1.1      uch 
     68       1.1      uch #ifdef	HD64465PCMCIA_DEBUG
     69       1.5      uch #define	DPRINTF_ENABLE
     70       1.5      uch #define	DPRINTF_DEBUG	hd64465pcmcia_debug
     71       1.1      uch #endif
     72       1.1      uch #include <machine/debug.h>
     73       1.1      uch 
     74       1.1      uch enum memory_window_16 {
     75       1.1      uch 	MEMWIN_16M_COMMON_0,
     76       1.1      uch 	MEMWIN_16M_COMMON_1,
     77       1.1      uch 	MEMWIN_16M_COMMON_2,
     78       1.1      uch 	MEMWIN_16M_COMMON_3,
     79       1.1      uch };
     80       1.5      uch #define	MEMWIN_16M_MAX	4
     81       1.1      uch 
     82       1.1      uch enum hd64465pcmcia_event_type {
     83       1.1      uch 	EVENT_NONE,
     84       1.1      uch 	EVENT_INSERT,
     85       1.1      uch 	EVENT_REMOVE,
     86       1.1      uch };
     87       1.5      uch #define	EVENT_QUEUE_MAX		5
     88       1.1      uch 
     89       1.1      uch struct hd64465pcmcia_softc; /* forward declaration */
     90       1.1      uch 
     91       1.1      uch struct hd64465pcmcia_window_cookie {
     92       1.1      uch 	bus_space_tag_t wc_tag;
     93       1.1      uch 	bus_space_handle_t wc_handle;
     94       1.1      uch 	int wc_size;
     95       1.1      uch 	int wc_window;
     96       1.1      uch };
     97       1.1      uch 
     98       1.1      uch struct hd64465pcmcia_channel {
     99       1.1      uch 	struct hd64465pcmcia_softc *ch_parent;
    100       1.1      uch 	struct device *ch_pcmcia;
    101       1.1      uch 	int ch_channel;
    102       1.1      uch 
    103       1.1      uch 	/* memory space */
    104       1.1      uch 	bus_space_tag_t ch_memt;
    105       1.1      uch 	bus_space_handle_t ch_memh;
    106       1.1      uch 	bus_addr_t ch_membase_addr;
    107       1.1      uch 	bus_size_t ch_memsize;
    108       1.1      uch 	bus_space_tag_t ch_cmemt[MEMWIN_16M_MAX];
    109       1.1      uch 
    110       1.1      uch 	/* I/O space */
    111       1.1      uch 	bus_space_tag_t ch_iot;
    112       1.1      uch 	bus_addr_t ch_iobase;
    113       1.1      uch 	bus_size_t ch_iosize;
    114       1.1      uch 
    115       1.1      uch 	/* card interrupt */
    116       1.1      uch 	int (*ch_ih_card_func)(void *);
    117       1.1      uch 	void *ch_ih_card_arg;
    118       1.1      uch 	int ch_attached;
    119       1.1      uch };
    120       1.1      uch 
    121       1.1      uch struct hd64465pcmcia_event {
    122       1.1      uch 	int __queued;
    123       1.1      uch 	enum hd64465pcmcia_event_type pe_type;
    124       1.1      uch 	struct hd64465pcmcia_channel *pe_ch;
    125       1.1      uch 	SIMPLEQ_ENTRY(hd64465pcmcia_event) pe_link;
    126       1.1      uch };
    127       1.1      uch 
    128       1.1      uch struct hd64465pcmcia_softc {
    129       1.1      uch 	struct device sc_dev;
    130       1.1      uch 	enum hd64465_module_id sc_module_id;
    131       1.1      uch 	int sc_shutdown;
    132       1.1      uch 
    133       1.1      uch 	/* kv mapped Area 5, 6 */
    134       1.1      uch 	vaddr_t sc_area5;
    135       1.1      uch 	vaddr_t sc_area6;
    136       1.1      uch 
    137       1.1      uch 	/* CSC event */
    138       1.1      uch 	struct proc *sc_event_thread;
    139       1.1      uch 	struct hd64465pcmcia_event sc_event_pool[EVENT_QUEUE_MAX];
    140       1.1      uch 	SIMPLEQ_HEAD (, hd64465pcmcia_event) sc_event_head;
    141       1.1      uch 
    142       1.1      uch 	struct hd64465pcmcia_channel sc_ch[2];
    143       1.1      uch };
    144       1.1      uch 
    145       1.1      uch STATIC int hd64465pcmcia_chip_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
    146       1.1      uch     struct pcmcia_mem_handle *);
    147       1.1      uch STATIC void hd64465pcmcia_chip_mem_free(pcmcia_chipset_handle_t,
    148       1.1      uch     struct pcmcia_mem_handle *);
    149       1.1      uch STATIC int hd64465pcmcia_chip_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t,
    150       1.1      uch     bus_size_t, struct pcmcia_mem_handle *, bus_size_t *, int *);
    151       1.1      uch STATIC void hd64465pcmcia_chip_mem_unmap(pcmcia_chipset_handle_t, int);
    152       1.1      uch STATIC int hd64465pcmcia_chip_io_alloc(pcmcia_chipset_handle_t, bus_addr_t,
    153       1.1      uch     bus_size_t, bus_size_t, struct pcmcia_io_handle *);
    154       1.1      uch STATIC void hd64465pcmcia_chip_io_free(pcmcia_chipset_handle_t,
    155       1.1      uch     struct pcmcia_io_handle *);
    156       1.1      uch STATIC int hd64465pcmcia_chip_io_map(pcmcia_chipset_handle_t, int, bus_addr_t,
    157       1.1      uch     bus_size_t, struct pcmcia_io_handle *, int *);
    158       1.1      uch STATIC void hd64465pcmcia_chip_io_unmap(pcmcia_chipset_handle_t, int);
    159       1.1      uch STATIC void hd64465pcmcia_chip_socket_enable(pcmcia_chipset_handle_t);
    160       1.1      uch STATIC void hd64465pcmcia_chip_socket_disable(pcmcia_chipset_handle_t);
    161  1.12.2.2    skrll STATIC void hd64465pcmcia_chip_socket_settype(pcmcia_chipset_handle_t, int);
    162       1.1      uch STATIC void *hd64465pcmcia_chip_intr_establish(pcmcia_chipset_handle_t,
    163       1.1      uch     struct pcmcia_function *, int, int (*)(void *), void *);
    164       1.1      uch STATIC void hd64465pcmcia_chip_intr_disestablish(pcmcia_chipset_handle_t,
    165       1.1      uch     void *);
    166       1.1      uch 
    167       1.1      uch STATIC struct pcmcia_chip_functions hd64465pcmcia_functions = {
    168       1.1      uch 	hd64465pcmcia_chip_mem_alloc,
    169       1.1      uch 	hd64465pcmcia_chip_mem_free,
    170       1.1      uch 	hd64465pcmcia_chip_mem_map,
    171       1.1      uch 	hd64465pcmcia_chip_mem_unmap,
    172       1.1      uch 	hd64465pcmcia_chip_io_alloc,
    173       1.1      uch 	hd64465pcmcia_chip_io_free,
    174       1.1      uch 	hd64465pcmcia_chip_io_map,
    175       1.1      uch 	hd64465pcmcia_chip_io_unmap,
    176       1.1      uch 	hd64465pcmcia_chip_intr_establish,
    177       1.1      uch 	hd64465pcmcia_chip_intr_disestablish,
    178       1.1      uch 	hd64465pcmcia_chip_socket_enable,
    179       1.1      uch 	hd64465pcmcia_chip_socket_disable,
    180  1.12.2.2    skrll 	hd64465pcmcia_chip_socket_settype,
    181       1.1      uch };
    182       1.1      uch 
    183       1.1      uch STATIC int hd64465pcmcia_match(struct device *, struct cfdata *, void *);
    184       1.1      uch STATIC void hd64465pcmcia_attach(struct device *, struct device *, void *);
    185       1.1      uch STATIC int hd64465pcmcia_print(void *, const char *);
    186       1.1      uch STATIC int hd64465pcmcia_submatch(struct device *, struct cfdata *, void *);
    187       1.1      uch 
    188       1.9  thorpej CFATTACH_DECL(hd64465pcmcia, sizeof(struct hd64465pcmcia_softc),
    189      1.10  thorpej     hd64465pcmcia_match, hd64465pcmcia_attach, NULL, NULL);
    190       1.1      uch 
    191       1.1      uch STATIC void hd64465pcmcia_attach_channel(struct hd64465pcmcia_softc *, int);
    192       1.1      uch /* hot plug */
    193       1.1      uch STATIC void hd64465pcmcia_create_event_thread(void *);
    194       1.1      uch STATIC void hd64465pcmcia_event_thread(void *);
    195       1.2      uch STATIC void __queue_event(struct hd64465pcmcia_channel *,
    196       1.1      uch     enum hd64465pcmcia_event_type);
    197       1.1      uch /* interrupt handler */
    198       1.1      uch STATIC int hd64465pcmcia_intr(void *);
    199       1.1      uch /* card status */
    200       1.2      uch STATIC enum hd64465pcmcia_event_type __detect_card(int);
    201       1.1      uch STATIC void hd64465pcmcia_memory_window16_switch(int,  enum memory_window_16);
    202       1.1      uch /* bus width */
    203       1.1      uch STATIC void __sh_set_bus_width(int, int);
    204       1.1      uch /* bus space access */
    205       1.1      uch STATIC int __sh_hd64465_map(vaddr_t, paddr_t, size_t, u_int32_t);
    206       1.1      uch STATIC vaddr_t __sh_hd64465_map_2page(paddr_t);
    207       1.1      uch 
    208       1.5      uch #define	DELAY_MS(x)	delay((x) * 1000)
    209       1.1      uch 
    210       1.1      uch int
    211       1.1      uch hd64465pcmcia_match(struct device *parent, struct cfdata *cf, void *aux)
    212       1.1      uch {
    213       1.1      uch 	struct hd64465_attach_args *ha = aux;
    214       1.1      uch 
    215       1.1      uch 	return (ha->ha_module_id == HD64465_MODULE_PCMCIA);
    216       1.1      uch }
    217       1.1      uch 
    218       1.1      uch void
    219       1.1      uch hd64465pcmcia_attach(struct device *parent, struct device *self, void *aux)
    220       1.1      uch {
    221       1.1      uch 	struct hd64465_attach_args *ha = aux;
    222       1.1      uch 	struct hd64465pcmcia_softc *sc = (struct hd64465pcmcia_softc *)self;
    223       1.1      uch 
    224       1.1      uch 	sc->sc_module_id = ha->ha_module_id;
    225       1.5      uch 
    226       1.1      uch 	printf("\n");
    227       1.1      uch 
    228       1.1      uch 	sc->sc_area5 = __sh_hd64465_map_2page(0x14000000); /* area 5 */
    229       1.1      uch 	sc->sc_area6 = __sh_hd64465_map_2page(0x18000000); /* area 6 */
    230       1.1      uch 
    231  1.12.2.1    skrll 	if (sc->sc_area5 == 0 || sc->sc_area6 == 0) {
    232       1.1      uch 		printf("%s: can't map memory.\n", sc->sc_dev.dv_xname);
    233       1.1      uch 		if (sc->sc_area5)
    234       1.1      uch 			uvm_km_free(kernel_map, sc->sc_area5, 0x03000000);
    235       1.1      uch 		if (sc->sc_area6)
    236       1.1      uch 			uvm_km_free(kernel_map, sc->sc_area6, 0x03000000);
    237       1.1      uch 
    238       1.1      uch 		return;
    239       1.1      uch 	}
    240       1.1      uch 
    241       1.1      uch 	/* Channel 0/1 common CSC event queue */
    242       1.1      uch 	SIMPLEQ_INIT (&sc->sc_event_head);
    243       1.1      uch 	kthread_create(hd64465pcmcia_create_event_thread, sc);
    244       1.1      uch 
    245       1.1      uch 	hd64465pcmcia_attach_channel(sc, 0);
    246       1.1      uch 	hd64465pcmcia_attach_channel(sc, 1);
    247       1.1      uch }
    248       1.1      uch 
    249       1.1      uch void
    250       1.1      uch hd64465pcmcia_create_event_thread(void *arg)
    251       1.1      uch {
    252       1.1      uch 	struct hd64465pcmcia_softc *sc = arg;
    253       1.1      uch 	int error;
    254       1.1      uch 
    255       1.1      uch 	error = kthread_create1(hd64465pcmcia_event_thread, sc,
    256       1.1      uch 	    &sc->sc_event_thread, "%s", sc->sc_dev.dv_xname);
    257       1.1      uch 
    258       1.1      uch 	KASSERT(error == 0);
    259       1.1      uch }
    260       1.1      uch 
    261       1.1      uch void
    262       1.1      uch hd64465pcmcia_event_thread(void *arg)
    263       1.1      uch {
    264       1.1      uch 	struct hd64465pcmcia_softc *sc = arg;
    265       1.1      uch 	struct hd64465pcmcia_event *pe;
    266       1.1      uch 	int s;
    267       1.5      uch 
    268       1.1      uch 	while (!sc->sc_shutdown) {
    269       1.1      uch 		tsleep(sc, PWAIT, "CSC wait", 0);
    270       1.1      uch 		s = splhigh();
    271       1.1      uch 		while ((pe = SIMPLEQ_FIRST(&sc->sc_event_head))) {
    272       1.1      uch 			splx(s);
    273       1.1      uch 			switch (pe->pe_type) {
    274       1.1      uch 			default:
    275       1.1      uch 				printf("%s: unknown event.\n", __FUNCTION__);
    276       1.1      uch 				break;
    277       1.1      uch 			case EVENT_INSERT:
    278       1.1      uch 				DPRINTF("insert event.\n");
    279       1.1      uch 				pcmcia_card_attach(pe->pe_ch->ch_pcmcia);
    280       1.1      uch 				break;
    281       1.1      uch 			case EVENT_REMOVE:
    282       1.1      uch 				DPRINTF("remove event.\n");
    283       1.1      uch 				pcmcia_card_detach(pe->pe_ch->ch_pcmcia,
    284       1.1      uch 				    DETACH_FORCE);
    285       1.1      uch 				break;
    286       1.1      uch 			}
    287       1.1      uch 			s = splhigh();
    288       1.6    lukem 			SIMPLEQ_REMOVE_HEAD(&sc->sc_event_head, pe_link);
    289       1.1      uch 			pe->__queued = 0;
    290       1.1      uch 		}
    291       1.1      uch 		splx(s);
    292       1.1      uch 	}
    293       1.1      uch 	/* NOTREACHED */
    294       1.1      uch }
    295       1.1      uch 
    296       1.1      uch int
    297       1.1      uch hd64465pcmcia_print(void *arg, const char *pnp)
    298       1.1      uch {
    299       1.1      uch 
    300       1.1      uch 	if (pnp)
    301      1.11  thorpej 		aprint_normal("pcmcia at %s", pnp);
    302       1.1      uch 
    303       1.1      uch 	return (UNCONF);
    304       1.1      uch }
    305       1.1      uch 
    306       1.1      uch int
    307       1.1      uch hd64465pcmcia_submatch(struct device *parent, struct cfdata *cf, void *aux)
    308       1.1      uch {
    309       1.1      uch 	struct pcmciabus_attach_args *paa = aux;
    310       1.1      uch 	struct hd64465pcmcia_channel *ch =
    311       1.1      uch 	    (struct hd64465pcmcia_channel *)paa->pch;
    312       1.1      uch 
    313       1.1      uch 	if (ch->ch_channel == 0) {
    314       1.1      uch 		if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] !=
    315       1.1      uch 		    PCMCIABUSCF_CONTROLLER_DEFAULT &&
    316       1.1      uch 		    cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 0)
    317       1.1      uch 			return 0;
    318       1.1      uch 	} else {
    319       1.1      uch 		if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] !=
    320       1.1      uch 		    PCMCIABUSCF_CONTROLLER_DEFAULT &&
    321       1.1      uch 		    cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 1)
    322       1.1      uch 			return 0;
    323       1.1      uch 	}
    324       1.1      uch 	paa->pct = (pcmcia_chipset_tag_t)&hd64465pcmcia_functions;
    325       1.1      uch 
    326       1.7  thorpej 	return (config_match(parent, cf, aux));
    327       1.1      uch }
    328       1.1      uch 
    329       1.1      uch void
    330       1.1      uch hd64465pcmcia_attach_channel(struct hd64465pcmcia_softc *sc, int channel)
    331       1.1      uch {
    332       1.1      uch 	struct device *parent = (struct device *)sc;
    333       1.1      uch 	struct hd64465pcmcia_channel *ch = &sc->sc_ch[channel];
    334       1.5      uch 	struct pcmciabus_attach_args paa;
    335       1.1      uch 	bus_addr_t baseaddr;
    336       1.1      uch 	u_int8_t r;
    337       1.1      uch 	int i;
    338       1.1      uch 
    339       1.1      uch 	ch->ch_parent = sc;
    340       1.1      uch 	ch->ch_channel = channel;
    341       1.1      uch 
    342       1.5      uch 	/*
    343       1.5      uch 	 * Continuous 16-MB Area Mode
    344       1.1      uch 	 */
    345       1.1      uch 	/* set Continuous 16-MB Area Mode */
    346       1.1      uch 	r = hd64465_reg_read_1(HD64461_PCCGCR(channel));
    347       1.1      uch 	r &= ~HD64461_PCCGCR_MMOD;
    348       1.1      uch 	r |= HD64461_PCCGCR_MMOD_16M;
    349       1.1      uch 	hd64465_reg_write_1(HD64461_PCCGCR(channel), r);
    350       1.1      uch 
    351       1.1      uch 	/* Attibute/Common memory extent */
    352       1.1      uch 	baseaddr = (channel == 0) ? sc->sc_area6 : sc->sc_area5;
    353       1.1      uch 
    354       1.1      uch 	ch->ch_memt = bus_space_create(0, "PCMCIA attribute memory",
    355       1.1      uch 	    baseaddr, 0x01000000); /* 16MB */
    356       1.1      uch 	bus_space_alloc(ch->ch_memt, 0, 0x00ffffff, 0x0001000,
    357       1.1      uch 	    0x1000, 0x1000, 0, &ch->ch_membase_addr, &ch->ch_memh);
    358       1.1      uch 
    359       1.1      uch 	/* Common memory space extent */
    360       1.1      uch 	ch->ch_memsize = 0x01000000;
    361       1.1      uch 	for (i = 0; i < MEMWIN_16M_MAX; i++) {
    362       1.1      uch 		ch->ch_cmemt[i] = bus_space_create(0, "PCMCIA common memory",
    363       1.1      uch 		    baseaddr + 0x01000000, ch->ch_memsize);
    364       1.1      uch 	}
    365       1.1      uch 
    366       1.1      uch 	/* I/O port extent */
    367       1.1      uch 	ch->ch_iobase = 0;
    368       1.1      uch 	ch->ch_iosize = 0x01000000;
    369       1.5      uch 	ch->ch_iot = bus_space_create(0, "PCMCIA I/O port",
    370       1.1      uch 	    baseaddr + 0x01000000 * 2, ch->ch_iosize);
    371       1.1      uch 
    372       1.1      uch 	/* Interrupt */
    373       1.4      uch 	hd64465_intr_establish(channel ? HD64465_PCC1 : HD64465_PCC0,
    374       1.4      uch 	    IST_LEVEL, IPL_TTY, hd64465pcmcia_intr, ch);
    375       1.1      uch 
    376       1.1      uch 	paa.paa_busname = "pcmcia";
    377       1.1      uch 	paa.pch = (pcmcia_chipset_handle_t)ch;
    378       1.1      uch 	paa.iobase = ch->ch_iobase;
    379       1.1      uch 	paa.iosize = ch->ch_iosize;
    380       1.1      uch 
    381       1.1      uch 	ch->ch_pcmcia = config_found_sm(parent, &paa, hd64465pcmcia_print,
    382       1.1      uch 	    hd64465pcmcia_submatch);
    383       1.1      uch 
    384       1.2      uch 	if (ch->ch_pcmcia && (__detect_card(ch->ch_channel) == EVENT_INSERT)) {
    385       1.1      uch 		ch->ch_attached = 1;
    386       1.1      uch 		pcmcia_card_attach(ch->ch_pcmcia);
    387       1.1      uch 	}
    388       1.1      uch }
    389       1.1      uch 
    390       1.1      uch int
    391       1.1      uch hd64465pcmcia_intr(void *arg)
    392       1.1      uch {
    393       1.1      uch 	struct hd64465pcmcia_channel *ch = (struct hd64465pcmcia_channel *)arg;
    394       1.1      uch 	u_int32_t cscr;
    395       1.1      uch 	u_int8_t r;
    396       1.1      uch 	int ret = 0;
    397       1.1      uch 
    398       1.1      uch 	cscr = HD64461_PCCCSCR(ch->ch_channel);
    399       1.1      uch 	r = hd64465_reg_read_1(cscr);
    400       1.1      uch 
    401       1.1      uch 	/* clear interrtupt (don't change power switch select) */
    402       1.1      uch 	hd64465_reg_write_1(cscr, r & ~0x40);
    403       1.1      uch 
    404       1.1      uch 	if (r & (0x60 | 0x04/* for memory mapped mode*/)) {
    405       1.1      uch 		if (ch->ch_ih_card_func) {
    406       1.1      uch 			ret = (*ch->ch_ih_card_func)(ch->ch_ih_card_arg);
    407       1.1      uch 		} else {
    408       1.1      uch 			DPRINTF("spurious IREQ interrupt.\n");
    409       1.1      uch 		}
    410       1.1      uch 	}
    411       1.1      uch 
    412       1.1      uch 	if (r & HD64461_PCC0CSCR_P0CDC)
    413       1.2      uch 		__queue_event(ch, __detect_card(ch->ch_channel));
    414       1.1      uch 
    415       1.1      uch 	return (ret);
    416       1.1      uch }
    417       1.1      uch 
    418       1.1      uch void
    419       1.2      uch __queue_event(struct hd64465pcmcia_channel *ch,
    420       1.1      uch     enum hd64465pcmcia_event_type type)
    421       1.1      uch {
    422       1.1      uch 	struct hd64465pcmcia_event *pe, *pool;
    423       1.1      uch 	struct hd64465pcmcia_softc *sc = ch->ch_parent;
    424       1.1      uch 	int i;
    425       1.1      uch 	int s = splhigh();
    426       1.1      uch 
    427       1.1      uch 	if (type == EVENT_NONE)
    428       1.1      uch 		goto out;
    429       1.1      uch 
    430       1.1      uch 	pe = 0;
    431       1.1      uch 	pool = sc->sc_event_pool;
    432       1.1      uch 	for (i = 0; i < EVENT_QUEUE_MAX; i++) {
    433       1.1      uch 		if (!pool[i].__queued) {
    434       1.1      uch 			pe = &pool[i];
    435       1.1      uch 			break;
    436       1.1      uch 		}
    437       1.1      uch 	}
    438       1.1      uch 
    439       1.1      uch 	if (pe == 0) {
    440       1.1      uch 		printf("%s: event FIFO overflow (max %d).\n", __FUNCTION__,
    441       1.1      uch 		    EVENT_QUEUE_MAX);
    442       1.1      uch 		goto out;
    443       1.1      uch 	}
    444       1.1      uch 
    445       1.1      uch 	if ((ch->ch_attached && (type == EVENT_INSERT)) ||
    446       1.1      uch 	    (!ch->ch_attached && (type == EVENT_REMOVE))) {
    447       1.1      uch 		DPRINTF("spurious CSC interrupt.\n");
    448       1.1      uch 		goto out;
    449       1.1      uch 	}
    450       1.1      uch 
    451       1.1      uch 	ch->ch_attached = (type == EVENT_INSERT);
    452       1.1      uch 	pe->__queued = 1;
    453       1.1      uch 	pe->pe_type = type;
    454       1.1      uch 	pe->pe_ch = ch;
    455       1.1      uch 	SIMPLEQ_INSERT_TAIL(&sc->sc_event_head, pe, pe_link);
    456       1.1      uch 	wakeup(sc);
    457       1.1      uch  out:
    458       1.1      uch 	splx(s);
    459       1.1      uch }
    460       1.1      uch 
    461       1.1      uch /*
    462       1.1      uch  * Interface for pcmcia driver.
    463       1.1      uch  */
    464       1.1      uch /*
    465       1.1      uch  * Interrupt.
    466       1.1      uch  */
    467       1.1      uch void *
    468       1.1      uch hd64465pcmcia_chip_intr_establish(pcmcia_chipset_handle_t pch,
    469       1.1      uch     struct pcmcia_function *pf, int ipl, int (*ih_func)(void *), void *ih_arg)
    470       1.1      uch {
    471       1.1      uch 	struct hd64465pcmcia_channel *ch = (struct hd64465pcmcia_channel *)pch;
    472       1.1      uch 	int channel = ch->ch_channel;
    473       1.1      uch 	bus_addr_t cscier = HD64461_PCCCSCIER(channel);
    474       1.1      uch 	u_int8_t r;
    475       1.1      uch 	int s = splhigh();
    476       1.1      uch 
    477       1.4      uch 	hd6446x_intr_priority(ch->ch_channel == 0 ? HD64465_PCC0 : HD64465_PCC1,
    478       1.4      uch 	    ipl);
    479       1.4      uch 
    480       1.1      uch 	ch->ch_ih_card_func = ih_func;
    481       1.1      uch 	ch->ch_ih_card_arg = ih_arg;
    482       1.1      uch 
    483       1.1      uch 	/* Enable card interrupt */
    484       1.1      uch 	r = hd64465_reg_read_1(cscier);
    485       1.1      uch 	/* set level mode */
    486       1.1      uch 	r &= ~HD64461_PCC0CSCIER_P0IREQE_MASK;
    487       1.1      uch 	r |= HD64461_PCC0CSCIER_P0IREQE_LEVEL;
    488       1.1      uch 	hd64465_reg_write_1(cscier, r);
    489       1.1      uch 
    490       1.1      uch 	splx(s);
    491       1.1      uch 
    492       1.1      uch 	return (void *)ih_func;
    493       1.1      uch }
    494       1.1      uch 
    495       1.1      uch void
    496       1.1      uch hd64465pcmcia_chip_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
    497       1.1      uch {
    498       1.1      uch 	struct hd64465pcmcia_channel *ch = (struct hd64465pcmcia_channel *)pch;
    499       1.1      uch 	int channel = ch->ch_channel;
    500       1.1      uch 	bus_addr_t cscier = HD64461_PCCCSCIER(channel);
    501       1.1      uch 	int s = splhigh();
    502       1.1      uch 	u_int8_t r;
    503       1.4      uch 
    504       1.4      uch 	hd6446x_intr_priority(ch->ch_channel == 0 ? HD64465_PCC0 : HD64465_PCC1,
    505       1.4      uch 	    IPL_TTY);
    506       1.1      uch 
    507       1.1      uch 	/* Disable card interrupt */
    508       1.1      uch 	r = hd64465_reg_read_1(cscier);
    509       1.1      uch 	r &= ~HD64461_PCC0CSCIER_P0IREQE_MASK;
    510       1.1      uch 	r |= HD64461_PCC0CSCIER_P0IREQE_NONE;
    511       1.1      uch 	hd64465_reg_write_1(cscier, r);
    512       1.1      uch 
    513       1.1      uch 	ch->ch_ih_card_func = 0;
    514       1.1      uch 
    515       1.1      uch 	splx(s);
    516       1.1      uch }
    517       1.1      uch 
    518       1.1      uch /*
    519       1.1      uch  * Bus resources.
    520       1.1      uch  */
    521       1.1      uch int
    522       1.1      uch hd64465pcmcia_chip_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
    523       1.1      uch     struct pcmcia_mem_handle *pcmhp)
    524       1.1      uch {
    525       1.1      uch 	struct hd64465pcmcia_channel *ch = (struct hd64465pcmcia_channel *)pch;
    526       1.1      uch 
    527       1.1      uch 	pcmhp->memt = ch->ch_memt;
    528       1.1      uch 	pcmhp->addr = ch->ch_membase_addr;
    529       1.1      uch 	pcmhp->memh = ch->ch_memh;
    530       1.1      uch 	pcmhp->size = size;
    531       1.1      uch 	pcmhp->realsize = size;
    532       1.1      uch 
    533       1.1      uch 	DPRINTF("base 0x%08lx size %#lx\n", pcmhp->addr, size);
    534       1.1      uch 
    535       1.1      uch 	return (0);
    536       1.1      uch }
    537       1.1      uch 
    538       1.1      uch void
    539       1.1      uch hd64465pcmcia_chip_mem_free(pcmcia_chipset_handle_t pch,
    540       1.1      uch     struct pcmcia_mem_handle *pcmhp)
    541       1.1      uch {
    542       1.1      uch 	/* NO-OP */
    543       1.1      uch }
    544       1.1      uch 
    545       1.1      uch int
    546       1.1      uch hd64465pcmcia_chip_mem_map(pcmcia_chipset_handle_t pch, int kind,
    547       1.1      uch     bus_addr_t card_addr, bus_size_t size, struct pcmcia_mem_handle *pcmhp,
    548       1.1      uch     bus_size_t *offsetp, int *windowp)
    549       1.1      uch {
    550       1.1      uch 	struct hd64465pcmcia_channel *ch = (struct hd64465pcmcia_channel *)pch;
    551       1.1      uch 	struct hd64465pcmcia_window_cookie *cookie;
    552       1.1      uch 	bus_addr_t ofs;
    553       1.1      uch 
    554       1.1      uch 	cookie = malloc(sizeof(struct hd64465pcmcia_window_cookie),
    555       1.1      uch 	    M_DEVBUF, M_NOWAIT);
    556       1.1      uch 	KASSERT(cookie);
    557       1.1      uch 	memset(cookie, 0, sizeof(struct hd64465pcmcia_window_cookie));
    558       1.1      uch 
    559       1.1      uch 	/* Address */
    560       1.1      uch 	if ((kind & ~PCMCIA_WIDTH_MEM_MASK) == PCMCIA_MEM_ATTR) {
    561       1.1      uch 		cookie->wc_tag = ch->ch_memt;
    562       1.1      uch 		if (bus_space_subregion(ch->ch_memt, ch->ch_memh, card_addr,
    563       1.1      uch 		    size, &cookie->wc_handle) != 0)
    564       1.1      uch 			goto bad;
    565       1.5      uch 
    566       1.1      uch 		*offsetp = card_addr;
    567       1.1      uch 		cookie->wc_window = -1;
    568       1.1      uch 	} else {
    569       1.1      uch 		int window = card_addr / ch->ch_memsize;
    570       1.1      uch 		KASSERT(window < MEMWIN_16M_MAX);
    571       1.1      uch 
    572       1.1      uch 		cookie->wc_tag = ch->ch_cmemt[window];
    573       1.1      uch 		ofs = card_addr - window * ch->ch_memsize;
    574       1.1      uch 		if (bus_space_map(cookie->wc_tag, ofs, size, 0,
    575       1.1      uch 		    &cookie->wc_handle) != 0)
    576       1.1      uch 			goto bad;
    577       1.5      uch 
    578       1.1      uch 		/* XXX bogus. check window per common memory access. */
    579       1.1      uch 		hd64465pcmcia_memory_window16_switch(ch->ch_channel, window);
    580       1.1      uch 		*offsetp = ofs + 0x01000000; /* skip attribute area */
    581       1.1      uch 		cookie->wc_window = window;
    582       1.1      uch 	}
    583       1.1      uch 	cookie->wc_size = size;
    584       1.1      uch 	*windowp = (int)cookie;
    585       1.1      uch 
    586       1.1      uch 	DPRINTF("(%s) %#lx+%#lx-> %#lx+%#lx\n", kind == PCMCIA_MEM_ATTR ?
    587       1.1      uch 	    "attribute" : "common", ch->ch_memh, card_addr, *offsetp, size);
    588       1.1      uch 
    589       1.1      uch 	return (0);
    590       1.1      uch  bad:
    591       1.1      uch 	DPRINTF("%#lx-%#lx map failed.\n", card_addr, size);
    592       1.1      uch 	free(cookie, M_DEVBUF);
    593       1.1      uch 
    594       1.1      uch 	return (1);
    595       1.1      uch }
    596       1.1      uch 
    597       1.1      uch void
    598       1.1      uch hd64465pcmcia_chip_mem_unmap(pcmcia_chipset_handle_t pch, int window)
    599       1.1      uch {
    600       1.1      uch 	struct hd64465pcmcia_window_cookie *cookie = (void *)window;
    601       1.1      uch 
    602       1.1      uch 	if (cookie->wc_window != -1)
    603       1.1      uch 		bus_space_unmap(cookie->wc_tag, cookie->wc_handle,
    604       1.1      uch 		    cookie->wc_size);
    605       1.1      uch 	DPRINTF("%#lx-%#x\n", cookie->wc_handle, cookie->wc_size);
    606       1.1      uch 	free(cookie, M_DEVBUF);
    607       1.1      uch }
    608       1.1      uch 
    609       1.1      uch int
    610       1.1      uch hd64465pcmcia_chip_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start,
    611       1.1      uch     bus_size_t size, bus_size_t align, struct pcmcia_io_handle *pcihp)
    612       1.1      uch {
    613       1.1      uch 	struct hd64465pcmcia_channel *ch = (struct hd64465pcmcia_channel *)pch;
    614       1.1      uch 
    615       1.1      uch 	if (start) {
    616       1.1      uch 		if (bus_space_map(ch->ch_iot, start, size, 0, &pcihp->ioh)) {
    617       1.1      uch 			DPRINTF("couldn't map %#lx+%#lx\n", start, size);
    618       1.1      uch 			return (1);
    619       1.1      uch 		}
    620       1.1      uch 		pcihp->addr = pcihp->ioh;
    621       1.1      uch 		DPRINTF("map %#lx+%#lx\n", start, size);
    622       1.1      uch 	} else {
    623       1.1      uch 		if (bus_space_alloc(ch->ch_iot, ch->ch_iobase,
    624       1.1      uch 		    ch->ch_iobase + ch->ch_iosize - 1,
    625       1.1      uch 		    size, align, 0, 0, &pcihp->addr, &pcihp->ioh)) {
    626       1.1      uch 			DPRINTF("couldn't allocate %#lx\n", size);
    627       1.1      uch 			return (1);
    628       1.1      uch 		}
    629       1.1      uch 		pcihp->flags = PCMCIA_IO_ALLOCATED;
    630       1.1      uch 	}
    631       1.1      uch 	DPRINTF("%#lx from %#lx\n", size, pcihp->addr);
    632       1.1      uch 
    633       1.1      uch 	pcihp->iot = ch->ch_iot;
    634       1.1      uch 	pcihp->size = size;
    635       1.1      uch 
    636       1.1      uch 	return (0);
    637       1.1      uch }
    638       1.1      uch 
    639       1.1      uch int
    640       1.1      uch hd64465pcmcia_chip_io_map(pcmcia_chipset_handle_t pch, int width,
    641       1.1      uch     bus_addr_t offset, bus_size_t size, struct pcmcia_io_handle *pcihp,
    642       1.1      uch     int *windowp)
    643       1.1      uch {
    644       1.1      uch 	struct hd64465pcmcia_channel *ch = (struct hd64465pcmcia_channel *)pch;
    645       1.1      uch #ifdef HD64465PCMCIA_DEBUG
    646       1.1      uch 	static const char *width_names[] = { "auto", "io8", "io16" };
    647       1.1      uch #endif
    648       1.1      uch 
    649       1.1      uch 	__sh_set_bus_width(ch->ch_channel, width);
    650       1.1      uch 
    651       1.1      uch 	DPRINTF("%#lx:%#lx+%#lx %s\n", pcihp->ioh, offset, size,
    652       1.1      uch 	    width_names[width]);
    653       1.1      uch 
    654       1.1      uch 	return (0);
    655       1.1      uch }
    656       1.1      uch 
    657       1.1      uch void
    658       1.1      uch hd64465pcmcia_chip_io_free(pcmcia_chipset_handle_t pch,
    659       1.1      uch     struct pcmcia_io_handle *pcihp)
    660       1.1      uch {
    661       1.1      uch 
    662       1.1      uch 	if (pcihp->flags & PCMCIA_IO_ALLOCATED)
    663       1.1      uch 		bus_space_free(pcihp->iot, pcihp->ioh, pcihp->size);
    664       1.1      uch 	else
    665       1.1      uch 		bus_space_unmap(pcihp->iot, pcihp->ioh, pcihp->size);
    666       1.1      uch 
    667       1.1      uch 	DPRINTF("%#lx+%#lx\n", pcihp->ioh, pcihp->size);
    668       1.1      uch }
    669       1.1      uch 
    670       1.1      uch void
    671       1.1      uch hd64465pcmcia_chip_io_unmap(pcmcia_chipset_handle_t pch, int window)
    672       1.1      uch {
    673       1.1      uch 	/* nothing to do */
    674       1.1      uch }
    675       1.1      uch 
    676       1.1      uch /*
    677       1.1      uch  * Enable/Disable
    678       1.1      uch  */
    679       1.1      uch void
    680       1.1      uch hd64465pcmcia_chip_socket_enable(pcmcia_chipset_handle_t pch)
    681       1.1      uch {
    682       1.1      uch 	struct hd64465pcmcia_channel *ch = (struct hd64465pcmcia_channel *)pch;
    683       1.1      uch 	int channel = ch->ch_channel;
    684  1.12.2.2    skrll 	bus_addr_t gcr;
    685       1.1      uch 	u_int8_t r;
    686       1.1      uch 
    687       1.1      uch 	DPRINTF("enable channel %d\n", channel);
    688       1.1      uch 	gcr = HD64461_PCCGCR(channel);
    689       1.1      uch 
    690  1.12.2.2    skrll 	r = hd64465_reg_read_1(gcr);
    691  1.12.2.2    skrll 	r &= ~HD64461_PCC0GCR_P0PCCT;
    692  1.12.2.2    skrll 	hd64465_reg_write_1(gcr, r);
    693  1.12.2.2    skrll 
    694       1.1      uch 	/* Set Common memory area #0. */
    695       1.1      uch 	hd64465pcmcia_memory_window16_switch(channel, MEMWIN_16M_COMMON_0);
    696       1.1      uch 
    697  1.12.2.2    skrll 	DPRINTF("OK.\n");
    698  1.12.2.2    skrll }
    699       1.1      uch 
    700  1.12.2.2    skrll void
    701  1.12.2.2    skrll hd64465pcmcia_chip_socket_settype(pcmcia_chipset_handle_t pch, int type)
    702  1.12.2.2    skrll {
    703  1.12.2.2    skrll 	struct hd64465pcmcia_channel *ch = (struct hd64465pcmcia_channel *)pch;
    704  1.12.2.2    skrll 	int channel = ch->ch_channel;
    705  1.12.2.2    skrll 	bus_addr_t gcr;
    706  1.12.2.2    skrll 	u_int8_t r;
    707  1.12.2.2    skrll 
    708  1.12.2.2    skrll 	DPRINTF("settype channel %d\n", channel);
    709  1.12.2.2    skrll 	gcr = HD64461_PCCGCR(channel);
    710  1.12.2.2    skrll 
    711  1.12.2.2    skrll 	/* Set the card type */
    712       1.1      uch 	r = hd64465_reg_read_1(gcr);
    713  1.12.2.2    skrll 	if (type == PCMCIA_IFTYPE_IO)
    714       1.1      uch 		r |= HD64461_PCC0GCR_P0PCCT;
    715       1.1      uch 	else
    716       1.1      uch 		r &= ~HD64461_PCC0GCR_P0PCCT;
    717       1.1      uch 	hd64465_reg_write_1(gcr, r);
    718       1.1      uch 
    719       1.1      uch 	DPRINTF("OK.\n");
    720       1.1      uch }
    721       1.1      uch 
    722       1.1      uch void
    723       1.1      uch hd64465pcmcia_chip_socket_disable(pcmcia_chipset_handle_t pch)
    724       1.1      uch {
    725       1.1      uch 	struct hd64465pcmcia_channel *ch = (struct hd64465pcmcia_channel *)pch;
    726       1.1      uch 	int channel = ch->ch_channel;
    727       1.1      uch 
    728       1.1      uch 	/* dont' disable CSC interrupt */
    729       1.1      uch 	hd64465_reg_write_1(HD64461_PCCCSCIER(channel), HD64461_PCCCSCIER_CDE);
    730       1.1      uch 	hd64465_reg_write_1(HD64461_PCCCSCR(channel), 0);
    731       1.1      uch }
    732       1.1      uch 
    733       1.1      uch /*
    734       1.1      uch  * Card detect
    735       1.1      uch  */
    736       1.1      uch enum hd64465pcmcia_event_type
    737       1.2      uch __detect_card(int channel)
    738       1.1      uch {
    739       1.1      uch 	u_int8_t r;
    740       1.1      uch 
    741       1.1      uch 	r = hd64465_reg_read_1(HD64461_PCCISR(channel)) &
    742       1.1      uch 	    (HD64461_PCCISR_CD2 | HD64461_PCCISR_CD1);
    743       1.1      uch 
    744       1.1      uch 	if (r == (HD64461_PCCISR_CD2 | HD64461_PCCISR_CD1)) {
    745       1.1      uch 		DPRINTF("remove\n");
    746       1.1      uch 		return EVENT_REMOVE;
    747       1.1      uch 	}
    748       1.1      uch 	if (r == 0) {
    749       1.5      uch 		DPRINTF("insert\n");
    750       1.1      uch 		return EVENT_INSERT;
    751       1.1      uch 	}
    752       1.1      uch 	DPRINTF("transition\n");
    753       1.1      uch 
    754       1.1      uch 	return (EVENT_NONE);
    755       1.1      uch }
    756       1.1      uch 
    757       1.1      uch /*
    758       1.1      uch  * Memory window access ops.
    759       1.1      uch  */
    760       1.1      uch void
    761       1.1      uch hd64465pcmcia_memory_window16_switch(int channel, enum memory_window_16 window)
    762       1.1      uch {
    763       1.1      uch 	bus_addr_t a = HD64461_PCCGCR(channel);
    764       1.1      uch 	u_int8_t r;
    765       1.1      uch 
    766       1.1      uch 	r = hd64465_reg_read_1(a);
    767       1.1      uch 	r &= ~(HD64461_PCCGCR_PA25 | HD64461_PCCGCR_PA24);
    768       1.1      uch 
    769       1.1      uch 	switch (window) {
    770       1.1      uch 	case MEMWIN_16M_COMMON_0:
    771       1.1      uch 		break;
    772       1.1      uch 	case MEMWIN_16M_COMMON_1:
    773       1.1      uch 		r |= HD64461_PCCGCR_PA24;
    774       1.1      uch 		break;
    775       1.1      uch 	case MEMWIN_16M_COMMON_2:
    776       1.1      uch 		r |= HD64461_PCCGCR_PA25;
    777       1.1      uch 		break;
    778       1.1      uch 	case MEMWIN_16M_COMMON_3:
    779       1.1      uch 		r |= (HD64461_PCCGCR_PA25 | HD64461_PCCGCR_PA24);
    780       1.1      uch 		break;
    781       1.1      uch 	}
    782       1.1      uch 
    783       1.1      uch 	hd64465_reg_write_1(a, r);
    784       1.1      uch }
    785       1.1      uch 
    786       1.1      uch /*
    787       1.1      uch  * SH interface.
    788       1.1      uch  */
    789       1.1      uch void
    790       1.1      uch __sh_set_bus_width(int channel, int width)
    791       1.1      uch {
    792       1.1      uch 	u_int16_t r16;
    793       1.1      uch 
    794       1.2      uch 	r16 = _reg_read_2(SH4_BCR2);
    795       1.5      uch #ifdef HD64465PCMCIA_DEBUG
    796       1.1      uch 	dbg_bit_print_msg(r16, "BCR2");
    797       1.1      uch #endif
    798       1.1      uch 	if (channel == 0) {
    799       1.1      uch 		r16 &= ~((1 << 13)|(1 << 12));
    800       1.1      uch 		r16 |= 1 << (width == PCMCIA_WIDTH_IO8 ? 12 : 13);
    801       1.1      uch 	} else {
    802       1.1      uch 		r16 &= ~((1 << 11)|(1 << 10));
    803       1.1      uch 		r16 |= 1 << (width == PCMCIA_WIDTH_IO8 ? 10 : 11);
    804       1.1      uch 	}
    805       1.2      uch 	_reg_write_2(SH4_BCR2, r16);
    806       1.1      uch }
    807       1.1      uch 
    808       1.1      uch vaddr_t
    809       1.1      uch __sh_hd64465_map_2page(paddr_t pa)
    810       1.1      uch {
    811       1.1      uch 	static const u_int32_t mode[] =
    812       1.1      uch 	{ _PG_PCMCIA_ATTR16, _PG_PCMCIA_MEM16, _PG_PCMCIA_IO };
    813       1.1      uch 	vaddr_t va, v;
    814       1.1      uch 	int i;
    815       1.1      uch 
    816       1.1      uch 	/* allocate kernel virtual */
    817       1.1      uch 	v = va = uvm_km_valloc(kernel_map, 0x03000000);
    818  1.12.2.1    skrll 	if (va == 0) {
    819       1.1      uch 		PRINTF("can't allocate virtual for paddr 0x%08x\n",
    820       1.1      uch 		    (unsigned)pa);
    821       1.1      uch 
    822       1.1      uch 		return (0);
    823       1.1      uch 	}
    824       1.1      uch 
    825       1.1      uch  	/* map to physical addreess with specified memory type. */
    826       1.1      uch 	for (i = 0; i < 3; i++, pa += 0x01000000, va += 0x01000000) {
    827       1.1      uch 		if (__sh_hd64465_map(va, pa, 0x2000, mode[i]) != 0) {
    828       1.1      uch 			uvm_km_free(kernel_map, v, 0x03000000);
    829       1.1      uch 			return (0);
    830       1.1      uch 		}
    831       1.1      uch 	}
    832       1.1      uch 
    833       1.1      uch 	return (v);
    834       1.1      uch }
    835       1.1      uch 
    836       1.1      uch int
    837       1.1      uch __sh_hd64465_map(vaddr_t va, paddr_t pa, size_t sz, u_int32_t flags)
    838       1.1      uch {
    839       1.1      uch 	pt_entry_t *pte;
    840       1.1      uch 	paddr_t epa;
    841       1.1      uch 
    842       1.1      uch 	KDASSERT(((pa & PAGE_MASK) == 0) && ((va & PAGE_MASK) == 0) &&
    843       1.1      uch 	    ((sz & PAGE_MASK) == 0));
    844       1.1      uch 
    845       1.1      uch 	epa = pa + sz;
    846       1.1      uch 	while (pa < epa) {
    847       1.5      uch 		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE);
    848       1.5      uch 		pte = __pmap_kpte_lookup(va);
    849       1.5      uch 		KDASSERT(pte);
    850       1.1      uch 		*pte |= flags;  /* PTEA PCMCIA assistant bit */
    851       1.5      uch 		sh_tlb_update(0, va, *pte);
    852      1.12  thorpej 		pa += PAGE_SIZE;
    853      1.12  thorpej 		va += PAGE_SIZE;
    854       1.1      uch 	}
    855       1.1      uch 
    856       1.1      uch 	return (0);
    857       1.1      uch }
    858