Home | History | Annotate | Line # | Download | only in obio
wdc_obio.c revision 1.3
      1 /*	$NetBSD: wdc_obio.c,v 1.3 2002/10/02 05:36:39 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002 Takeshi Shibagaki  All rights reserved.
      5  *
      6  * mac68k OBIO-IDE attachment created by Takeshi Shibagaki
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Charles M. Hannum.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/types.h>
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 #include <sys/kernel.h>
     40 #include <sys/callout.h>
     41 
     42 #include <machine/bus.h>
     43 #include <machine/intr.h>
     44 #include <machine/cpu.h>
     45 #include <machine/viareg.h>
     46 
     47 #include <mac68k/obio/obiovar.h>
     48 
     49 #include <dev/ata/atavar.h>
     50 #include <dev/ic/wdcvar.h>
     51 
     52 #define	WDC_OBIO_REG_NPORTS	(8<<3)
     53 #define	WDC_OBIO_AUXREG_OFFSET	0x38
     54 #define	WDC_OBIO_AUXREG_NPORTS	1
     55 #define	WDC_OBIO_ISR_OFFSET	0x101
     56 #define	WDC_OBIO_ISR_NPORTS	1
     57 
     58 static u_long	IDEBase = 0x50f1a000;
     59 
     60 /*
     61  * XXX This code currently doesn't even try to allow 32-bit data port use.
     62  */
     63 
     64 struct wdc_obio_softc {
     65 	struct  wdc_softc sc_wdcdev;
     66 	struct  channel_softc *wdc_chanptr;
     67 	struct  channel_softc wdc_channel;
     68 	void    *sc_ih;
     69 };
     70 
     71 int	wdc_obio_match	__P((struct device *, struct cfdata *, void *));
     72 void	wdc_obio_attach	__P((struct device *, struct device *, void *));
     73 void	wdc_obio_intr __P((void *));
     74 void	mac68k_bsh_wdc_set_stride __P((bus_space_tag_t t,
     75 	bus_space_handle_t *h, int stride));
     76 u_int16_t	mac68k_bsr2_wdc_gen __P((bus_space_tag_t t,
     77 		bus_space_handle_t *bsh, bus_size_t offset));
     78 
     79 CFATTACH_DECL(wdc_obio, sizeof(struct wdc_obio_softc),
     80     wdc_obio_match, wdc_obio_attach, NULL, NULL);
     81 
     82 int
     83 wdc_obio_match(parent, match, aux)
     84 	struct device *parent;
     85 	struct cfdata *match;
     86 	void *aux;
     87 {
     88 	struct obio_attach_args *oa = (struct obio_attach_args *) aux;
     89 	struct channel_softc ch;
     90 	static int wdc_matched = 0;
     91 	int result = 0;
     92 
     93 	memset(&ch, 0, sizeof(ch));
     94 
     95 	switch (current_mac_model->machineid) {
     96 	case MACH_MACPB150:
     97 	case MACH_MACPB190:
     98 	case MACH_MACPB190CS:
     99 	case MACH_MACP580:
    100 	case MACH_MACQ630:
    101 		ch.cmd_iot = ch.ctl_iot = oa->oa_tag;
    102 
    103 		if (bus_space_map(ch.cmd_iot, IDEBase, WDC_OBIO_REG_NPORTS,
    104 				  0, &ch.cmd_ioh))
    105 			return 0;
    106 
    107 		mac68k_bsh_wdc_set_stride(ch.cmd_iot, &ch.cmd_ioh, 4);
    108 
    109 		if (bus_space_subregion(ch.cmd_iot, ch.cmd_ioh,
    110 				        WDC_OBIO_AUXREG_OFFSET,
    111 					WDC_OBIO_AUXREG_NPORTS, &ch.ctl_ioh))
    112 			return 0;
    113 
    114 		result = wdcprobe(&ch);
    115 
    116 		bus_space_unmap(ch.cmd_iot, ch.cmd_ioh, WDC_OBIO_REG_NPORTS);
    117 
    118 		if (result)
    119 			wdc_matched = 1;
    120 		return (result);
    121 	}
    122 	return 0;
    123 }
    124 
    125 static bus_space_tag_t		wdc_obio_isr_tag;
    126 static bus_space_handle_t	wdc_obio_isr_hdl;
    127 static struct channel_softc	*ch_sc = NULL;
    128 
    129 void
    130 wdc_obio_intr(arg)
    131 	void *arg;
    132 {
    133 	unsigned char status;
    134 
    135 	status = bus_space_read_1(wdc_obio_isr_tag,
    136 				  wdc_obio_isr_hdl, 0);
    137 	if (status & 0x20) {
    138 		wdcintr(ch_sc);
    139 		bus_space_write_1(wdc_obio_isr_tag,
    140 				  wdc_obio_isr_hdl, 0, status&~0x20);
    141 	}
    142 }
    143 
    144 void
    145 wdc_obio_attach(parent, self, aux)
    146 	struct device *parent;
    147 	struct device *self;
    148 	void *aux;
    149 {
    150 	struct wdc_obio_softc *sc = (void *)self;
    151 	struct obio_attach_args *oa = aux;
    152 	struct channel_softc *chp = &sc->wdc_channel;
    153 
    154 	oa->oa_addr = IDEBase;
    155 	sc->wdc_channel.cmd_iot = sc->wdc_channel.ctl_iot = oa->oa_tag;
    156 
    157 	if (bus_space_map(sc->wdc_channel.cmd_iot, oa->oa_addr,
    158 			  WDC_OBIO_REG_NPORTS, 0, &sc->wdc_channel.cmd_ioh)) {
    159 		printf("%s: couldn't map registers\n",
    160 			sc->sc_wdcdev.sc_dev.dv_xname);
    161 		return;
    162 	}
    163 
    164 	mac68k_bsh_wdc_set_stride(sc->wdc_channel.cmd_iot,
    165 				  &sc->wdc_channel.cmd_ioh, 4);
    166 
    167 	if (bus_space_subregion(sc->wdc_channel.cmd_iot,
    168 				sc->wdc_channel.cmd_ioh, WDC_OBIO_AUXREG_OFFSET,
    169 				WDC_OBIO_AUXREG_NPORTS,
    170 				&sc->wdc_channel.ctl_ioh))
    171 		return;
    172 
    173     	wdc_obio_isr_tag = oa->oa_tag;
    174 
    175 	if (bus_space_map(wdc_obio_isr_tag,
    176 			  oa->oa_addr+WDC_OBIO_ISR_OFFSET,
    177 			  WDC_OBIO_ISR_NPORTS, 0, &wdc_obio_isr_hdl)) {
    178 		printf("%s: couldn't map intr status register\n",
    179 			sc->sc_wdcdev.sc_dev.dv_xname);
    180 		return;
    181 	}
    182 
    183 	switch (current_mac_model->machineid) {
    184 	case MACH_MACP580:
    185 	case MACH_MACQ630:
    186 		/*
    187 		 * Quadra/Performa IDE generates pseudo Nubus intr at slot F
    188 		 */
    189 		printf(" (Quadra/Performa series IDE interface)");
    190 
    191 		add_nubus_intr(0xf, (void (*)(void*))wdc_obio_intr, (void *)sc);
    192 
    193 		break;
    194 	case MACH_MACPB150:
    195 	case MACH_MACPB190:
    196 	case MACH_MACPB190CS:
    197 		/*
    198 		 * PowerBook IDE generates pseudo NuBus intr at slot C
    199 		 */
    200 		printf(" (PowerBook series IDE interface)");
    201 
    202 		add_nubus_intr(0xc, (void (*)(void*))wdc_obio_intr, (void *)sc);
    203 
    204 		break;
    205 	}
    206 
    207 	ch_sc = chp;
    208 	if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_CAPABILITY_NOIRQ)
    209 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NOIRQ;
    210 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    211 	sc->sc_wdcdev.PIO_cap = 0;
    212 	sc->wdc_chanptr = chp;
    213 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    214 	sc->sc_wdcdev.nchannels = 1;
    215 	chp->channel = 0;
    216 	chp->wdc = &sc->sc_wdcdev;
    217 	chp->ch_queue = malloc(sizeof(struct channel_queue), M_DEVBUF, M_NOWAIT);
    218 
    219 	if (chp->ch_queue == NULL) {
    220 		printf("%s: can't allocate memory for command queue",
    221 			sc->sc_wdcdev.sc_dev.dv_xname);
    222 		return;
    223 	}
    224 
    225 	printf("\n");
    226 
    227 	wdcattach(chp);
    228 }
    229 
    230 u_int16_t
    231 mac68k_bsr2_wdc_gen(bus_space_tag_t t, bus_space_handle_t *bsh, bus_size_t offset)
    232 {
    233 	return (*(volatile u_int16_t *) (bsh->base + offset * bsh->stride));
    234 }
    235 
    236 void
    237 mac68k_bsh_wdc_set_stride(bus_space_tag_t t, bus_space_handle_t *h, int stride)
    238 {
    239 	h->stride = stride;
    240 	h->bsr1 = mac68k_bsr1_gen;
    241 	h->bsr2 = mac68k_bsr2_wdc_gen;
    242 	h->bsr4 = mac68k_bsr4_gen;
    243 	h->bsrs2 = mac68k_bsrs2_gen;
    244 	h->bsrs4 = mac68k_bsrs4_gen;
    245 	h->bsrm1 = mac68k_bsrm1_gen;
    246 	h->bsrm2 = mac68k_bsrm2_swap;
    247 	h->bsrm4 = mac68k_bsrm4_swap;
    248 	h->bsrms2 = mac68k_bsrm2;
    249 	h->bsrms4 = mac68k_bsrm4;
    250 	h->bsrr1 = mac68k_bsrr1_gen;
    251 	h->bsrr2 = mac68k_bsrr2_gen;
    252 	h->bsrr4 = mac68k_bsrr4_gen;
    253 	h->bsrrs2 = mac68k_bsrrs2_gen;
    254 	h->bsrrs4 = mac68k_bsrrs4_gen;
    255 	h->bsw1 = mac68k_bsw1_gen;
    256 	h->bsw2 = mac68k_bsw2_gen;
    257 	h->bsw4 = mac68k_bsw4_gen;
    258 	h->bsws2 = mac68k_bsws2_gen;
    259 	h->bsws4 = mac68k_bsws4_gen;
    260 	h->bswm2 = mac68k_bswm2_swap;
    261 	h->bswm4 = mac68k_bswm4_swap;
    262 	h->bswms2 = mac68k_bswm2;
    263 	h->bswms4 = mac68k_bswm4;
    264 	h->bswr1 = mac68k_bswr1_gen;
    265 	h->bswr2 = mac68k_bswr2_gen;
    266 	h->bswr4 = mac68k_bswr4_gen;
    267 	h->bswrs2 = mac68k_bswrs2_gen;
    268 	h->bswrs4 = mac68k_bswrs4_gen;
    269 	h->bssm1 = mac68k_bssm1_gen;
    270 	h->bssm2 = mac68k_bssm2_gen;
    271 	h->bssm4 = mac68k_bssm4_gen;
    272 	h->bssr1 = mac68k_bssr1_gen;
    273 	h->bssr2 = mac68k_bssr2_gen;
    274 	h->bssr4 = mac68k_bssr4_gen;
    275 }
    276