Home | History | Annotate | Line # | Download | only in obio
wdc_obio.c revision 1.7
      1 /*	$NetBSD: wdc_obio.c,v 1.7 2003/10/08 11:11:06 bouyer 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/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: wdc_obio.c,v 1.7 2003/10/08 11:11:06 bouyer Exp $");
     36 
     37 #include <sys/types.h>
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 #include <sys/malloc.h>
     42 #include <sys/kernel.h>
     43 #include <sys/callout.h>
     44 
     45 #include <machine/bus.h>
     46 #include <machine/intr.h>
     47 #include <machine/cpu.h>
     48 #include <machine/viareg.h>
     49 
     50 #include <mac68k/obio/obiovar.h>
     51 
     52 #include <dev/ata/atavar.h>
     53 #include <dev/ic/wdcvar.h>
     54 
     55 #define	WDC_OBIO_REG_NPORTS	(8<<3)
     56 #define	WDC_OBIO_AUXREG_OFFSET	0x38
     57 #define	WDC_OBIO_AUXREG_NPORTS	1
     58 #define	WDC_OBIO_ISR_OFFSET	0x101
     59 #define	WDC_OBIO_ISR_NPORTS	1
     60 
     61 static u_long	IDEBase = 0x50f1a000;
     62 
     63 /*
     64  * XXX This code currently doesn't even try to allow 32-bit data port use.
     65  */
     66 
     67 struct wdc_obio_softc {
     68 	struct  wdc_softc sc_wdcdev;
     69 	struct  channel_softc *wdc_chanptr;
     70 	struct  channel_softc wdc_channel;
     71 	void    *sc_ih;
     72 };
     73 
     74 int	wdc_obio_match	__P((struct device *, struct cfdata *, void *));
     75 void	wdc_obio_attach	__P((struct device *, struct device *, void *));
     76 void	wdc_obio_intr __P((void *));
     77 void	mac68k_bsh_wdc_set_stride __P((bus_space_tag_t t,
     78 	bus_space_handle_t *h, int stride));
     79 u_int16_t	mac68k_bsr2_wdc_gen __P((bus_space_tag_t t,
     80 		bus_space_handle_t *bsh, bus_size_t offset));
     81 
     82 CFATTACH_DECL(wdc_obio, sizeof(struct wdc_obio_softc),
     83     wdc_obio_match, wdc_obio_attach, NULL, NULL);
     84 
     85 int
     86 wdc_obio_match(parent, match, aux)
     87 	struct device *parent;
     88 	struct cfdata *match;
     89 	void *aux;
     90 {
     91 	struct obio_attach_args *oa = (struct obio_attach_args *) aux;
     92 	struct channel_softc ch;
     93 	static int wdc_matched = 0;
     94 	int result = 0;
     95 
     96 	memset(&ch, 0, sizeof(ch));
     97 
     98 	switch (current_mac_model->machineid) {
     99 	case MACH_MACPB150:
    100 	case MACH_MACPB190:
    101 	case MACH_MACPB190CS:
    102 	case MACH_MACP580:
    103 	case MACH_MACQ630:
    104 		ch.cmd_iot = ch.ctl_iot = oa->oa_tag;
    105 
    106 		if (bus_space_map(ch.cmd_iot, IDEBase, WDC_OBIO_REG_NPORTS,
    107 				  0, &ch.cmd_ioh))
    108 			return 0;
    109 
    110 		mac68k_bsh_wdc_set_stride(ch.cmd_iot, &ch.cmd_ioh, 4);
    111 
    112 		if (bus_space_subregion(ch.cmd_iot, ch.cmd_ioh,
    113 				        WDC_OBIO_AUXREG_OFFSET,
    114 					WDC_OBIO_AUXREG_NPORTS, &ch.ctl_ioh))
    115 			return 0;
    116 
    117 		result = wdcprobe(&ch);
    118 
    119 		bus_space_unmap(ch.cmd_iot, ch.cmd_ioh, WDC_OBIO_REG_NPORTS);
    120 
    121 		if (result)
    122 			wdc_matched = 1;
    123 		return (result);
    124 	}
    125 	return 0;
    126 }
    127 
    128 static bus_space_tag_t		wdc_obio_isr_tag;
    129 static bus_space_handle_t	wdc_obio_isr_hdl;
    130 static struct channel_softc	*ch_sc = NULL;
    131 
    132 void
    133 wdc_obio_intr(arg)
    134 	void *arg;
    135 {
    136 	unsigned char status;
    137 
    138 	status = bus_space_read_1(wdc_obio_isr_tag,
    139 				  wdc_obio_isr_hdl, 0);
    140 	if (status & 0x20) {
    141 		wdcintr(ch_sc);
    142 		bus_space_write_1(wdc_obio_isr_tag,
    143 				  wdc_obio_isr_hdl, 0, status&~0x20);
    144 	}
    145 }
    146 
    147 void
    148 wdc_obio_attach(parent, self, aux)
    149 	struct device *parent;
    150 	struct device *self;
    151 	void *aux;
    152 {
    153 	struct wdc_obio_softc *sc = (void *)self;
    154 	struct obio_attach_args *oa = aux;
    155 	struct channel_softc *chp = &sc->wdc_channel;
    156 
    157 	oa->oa_addr = IDEBase;
    158 	sc->wdc_channel.cmd_iot = sc->wdc_channel.ctl_iot = oa->oa_tag;
    159 
    160 	if (bus_space_map(sc->wdc_channel.cmd_iot, oa->oa_addr,
    161 			  WDC_OBIO_REG_NPORTS, 0, &sc->wdc_channel.cmd_ioh)) {
    162 		printf("%s: couldn't map registers\n",
    163 			sc->sc_wdcdev.sc_dev.dv_xname);
    164 		return;
    165 	}
    166 
    167 	mac68k_bsh_wdc_set_stride(sc->wdc_channel.cmd_iot,
    168 				  &sc->wdc_channel.cmd_ioh, 4);
    169 
    170 	if (bus_space_subregion(sc->wdc_channel.cmd_iot,
    171 				sc->wdc_channel.cmd_ioh, WDC_OBIO_AUXREG_OFFSET,
    172 				WDC_OBIO_AUXREG_NPORTS,
    173 				&sc->wdc_channel.ctl_ioh))
    174 		return;
    175 
    176     	wdc_obio_isr_tag = oa->oa_tag;
    177 
    178 	if (bus_space_map(wdc_obio_isr_tag,
    179 			  oa->oa_addr+WDC_OBIO_ISR_OFFSET,
    180 			  WDC_OBIO_ISR_NPORTS, 0, &wdc_obio_isr_hdl)) {
    181 		printf("%s: couldn't map intr status register\n",
    182 			sc->sc_wdcdev.sc_dev.dv_xname);
    183 		return;
    184 	}
    185 
    186 	switch (current_mac_model->machineid) {
    187 	case MACH_MACP580:
    188 	case MACH_MACQ630:
    189 		/*
    190 		 * Quadra/Performa IDE generates pseudo Nubus intr at slot F
    191 		 */
    192 		printf(" (Quadra/Performa series IDE interface)");
    193 
    194 		add_nubus_intr(0xf, (void (*)(void*))wdc_obio_intr, (void *)sc);
    195 
    196 		break;
    197 	case MACH_MACPB150:
    198 	case MACH_MACPB190:
    199 	case MACH_MACPB190CS:
    200 		/*
    201 		 * PowerBook IDE generates pseudo NuBus intr at slot C
    202 		 */
    203 		printf(" (PowerBook series IDE interface)");
    204 
    205 		add_nubus_intr(0xc, (void (*)(void*))wdc_obio_intr, (void *)sc);
    206 
    207 		break;
    208 	}
    209 
    210 	ch_sc = chp;
    211 	if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_CAPABILITY_NOIRQ)
    212 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NOIRQ;
    213 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    214 	sc->sc_wdcdev.PIO_cap = 0;
    215 	sc->wdc_chanptr = chp;
    216 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    217 	sc->sc_wdcdev.nchannels = 1;
    218 	chp->channel = 0;
    219 	chp->wdc = &sc->sc_wdcdev;
    220 	chp->ch_queue = malloc(sizeof(struct channel_queue), M_DEVBUF, M_NOWAIT);
    221 
    222 	if (chp->ch_queue == NULL) {
    223 		printf("%s: can't allocate memory for command queue",
    224 			sc->sc_wdcdev.sc_dev.dv_xname);
    225 		return;
    226 	}
    227 
    228 	printf("\n");
    229 
    230 	wdcattach(chp);
    231 }
    232 
    233 u_int16_t
    234 mac68k_bsr2_wdc_gen(bus_space_tag_t t, bus_space_handle_t *bsh, bus_size_t offset)
    235 {
    236 	return (*(volatile u_int16_t *) (bsh->base + offset * bsh->stride));
    237 }
    238 
    239 void
    240 mac68k_bsh_wdc_set_stride(bus_space_tag_t t, bus_space_handle_t *h, int stride)
    241 {
    242 	h->stride = stride;
    243 	h->bsr1 = mac68k_bsr1_gen;
    244 	h->bsr2 = mac68k_bsr2_wdc_gen;
    245 	h->bsr4 = mac68k_bsr4_gen;
    246 	h->bsrs2 = mac68k_bsrs2_gen;
    247 	h->bsrs4 = mac68k_bsrs4_gen;
    248 	h->bsrm1 = mac68k_bsrm1_gen;
    249 	h->bsrm2 = mac68k_bsrm2_swap;
    250 	h->bsrm4 = mac68k_bsrm4_swap;
    251 	h->bsrms2 = mac68k_bsrm2;
    252 	h->bsrms4 = mac68k_bsrm4;
    253 	h->bsrr1 = mac68k_bsrr1_gen;
    254 	h->bsrr2 = mac68k_bsrr2_gen;
    255 	h->bsrr4 = mac68k_bsrr4_gen;
    256 	h->bsrrs2 = mac68k_bsrrs2_gen;
    257 	h->bsrrs4 = mac68k_bsrrs4_gen;
    258 	h->bsw1 = mac68k_bsw1_gen;
    259 	h->bsw2 = mac68k_bsw2_gen;
    260 	h->bsw4 = mac68k_bsw4_gen;
    261 	h->bsws2 = mac68k_bsws2_gen;
    262 	h->bsws4 = mac68k_bsws4_gen;
    263 	h->bswm2 = mac68k_bswm2_swap;
    264 	h->bswm4 = mac68k_bswm4_swap;
    265 	h->bswms2 = mac68k_bswm2;
    266 	h->bswms4 = mac68k_bswm4;
    267 	h->bswr1 = mac68k_bswr1_gen;
    268 	h->bswr2 = mac68k_bswr2_gen;
    269 	h->bswr4 = mac68k_bswr4_gen;
    270 	h->bswrs2 = mac68k_bswrs2_gen;
    271 	h->bswrs4 = mac68k_bswrs4_gen;
    272 	h->bssm1 = mac68k_bssm1_gen;
    273 	h->bssm2 = mac68k_bssm2_gen;
    274 	h->bssm4 = mac68k_bssm4_gen;
    275 	h->bssr1 = mac68k_bssr1_gen;
    276 	h->bssr2 = mac68k_bssr2_gen;
    277 	h->bssr4 = mac68k_bssr4_gen;
    278 }
    279