Home | History | Annotate | Line # | Download | only in obio
wdc_obio.c revision 1.17
      1 /*	$NetBSD: wdc_obio.c,v 1.17 2004/08/20 06:39:38 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/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: wdc_obio.c,v 1.17 2004/08/20 06:39:38 thorpej 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	0x40
     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  ata_channel *sc_chanlist[1];
     70 	struct  ata_channel sc_channel;
     71 	struct	ata_queue sc_chqueue;
     72 	struct	wdc_regs sc_wdc_regs;
     73 	void    *sc_ih;
     74 };
     75 
     76 int	wdc_obio_match	(struct device *, struct cfdata *, void *);
     77 void	wdc_obio_attach	(struct device *, struct device *, void *);
     78 void	wdc_obio_intr (void *);
     79 
     80 CFATTACH_DECL(wdc_obio, sizeof(struct wdc_obio_softc),
     81     wdc_obio_match, wdc_obio_attach, NULL, NULL);
     82 
     83 int
     84 wdc_obio_match(parent, match, aux)
     85 	struct device *parent;
     86 	struct cfdata *match;
     87 	void *aux;
     88 {
     89 	struct obio_attach_args *oa = (struct obio_attach_args *) aux;
     90 	struct ata_channel ch;
     91 	struct wdc_softc wdc;
     92 	struct wdc_regs wdr;
     93 	static int wdc_matched = 0;
     94 	int i, result = 0;
     95 
     96 	memset(&wdc, 0, sizeof(wdc));
     97 	memset(&ch, 0, sizeof(ch));
     98 	ch.ch_atac = &wdc.sc_atac;
     99 	wdc.regs = &wdr;
    100 
    101 	switch (current_mac_model->machineid) {
    102 	case MACH_MACPB150:
    103 	case MACH_MACPB190:
    104 	case MACH_MACPB190CS:
    105 	case MACH_MACP580:
    106 	case MACH_MACQ630:
    107 		wdr.cmd_iot = wdr.ctl_iot = oa->oa_tag;
    108 
    109 		if (bus_space_map(wdr.cmd_iot, IDEBase, WDC_OBIO_REG_NPORTS,
    110 				0, &wdr.cmd_baseioh))
    111 			return 0;
    112 
    113 		mac68k_bus_space_handle_swapped(wdr.cmd_iot, &wdr.cmd_baseioh);
    114 
    115 		for (i = 0; i < WDC_NREG; i++) {
    116 			if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh,
    117 					    4 * i, 4, &wdr.cmd_iohs[i]) != 0) {
    118 				return 0;
    119 			}
    120 		}
    121 		wdc_init_shadow_regs(&ch);
    122 
    123 
    124 		if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh,
    125 				        WDC_OBIO_AUXREG_OFFSET,
    126 					WDC_OBIO_AUXREG_NPORTS,
    127 					&wdr.ctl_ioh))
    128 			return 0;
    129 
    130 		result = wdcprobe(&ch);
    131 
    132 		bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_OBIO_REG_NPORTS);
    133 
    134 		if (result)
    135 			wdc_matched = 1;
    136 		return (result);
    137 	}
    138 	return 0;
    139 }
    140 
    141 static bus_space_tag_t		wdc_obio_isr_tag;
    142 static bus_space_handle_t	wdc_obio_isr_hdl;
    143 static struct ata_channel	*ch_sc = NULL;
    144 
    145 void
    146 wdc_obio_intr(arg)
    147 	void *arg;
    148 {
    149 	unsigned char status;
    150 
    151 	status = bus_space_read_1(wdc_obio_isr_tag,
    152 				  wdc_obio_isr_hdl, 0);
    153 	if (status & 0x20) {
    154 		wdcintr(ch_sc);
    155 		bus_space_write_1(wdc_obio_isr_tag,
    156 				  wdc_obio_isr_hdl, 0, status&~0x20);
    157 	}
    158 }
    159 
    160 void
    161 wdc_obio_attach(parent, self, aux)
    162 	struct device *parent;
    163 	struct device *self;
    164 	void *aux;
    165 {
    166 	struct wdc_obio_softc *sc = (void *)self;
    167 	struct wdc_regs *wdr;
    168 	struct obio_attach_args *oa = aux;
    169 	struct ata_channel *chp = &sc->sc_channel;
    170 	int i;
    171 
    172 	sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
    173 
    174 	oa->oa_addr = IDEBase;
    175 	wdr->cmd_iot = wdr->ctl_iot = oa->oa_tag;
    176 
    177 	if (bus_space_map(wdr->cmd_iot, oa->oa_addr,
    178 		      WDC_OBIO_REG_NPORTS, 0, &wdr->cmd_baseioh)) {
    179 		printf("%s: couldn't map registers\n",
    180 			sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    181 		return;
    182 	}
    183 
    184 	mac68k_bus_space_handle_swapped(wdr->cmd_iot,
    185 				  &wdr->cmd_baseioh);
    186 
    187 	for (i = 0; i < WDC_NREG; i++) {
    188 		if (bus_space_subregion(wdr->cmd_iot,
    189 				    wdr->cmd_baseioh, 4 * i, 4,
    190 				    &wdr->cmd_iohs[i]) != 0) {
    191 			printf("%s: unable to subregion control register\n",
    192 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    193 			return;
    194 		}
    195 	}
    196 	wdc_init_shadow_regs(&sc->sc_channel);
    197 
    198 	if (bus_space_subregion(wdr->cmd_iot,
    199 				wdr->cmd_baseioh,
    200 				WDC_OBIO_AUXREG_OFFSET, WDC_OBIO_AUXREG_NPORTS,
    201 				&wdr->ctl_ioh)) {
    202 		printf("%s: unable to subregion aux register\n",
    203 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    204 		return;
    205 	}
    206 
    207     	wdc_obio_isr_tag = oa->oa_tag;
    208 
    209 	if (bus_space_map(wdc_obio_isr_tag,
    210 			  oa->oa_addr+WDC_OBIO_ISR_OFFSET,
    211 			  WDC_OBIO_ISR_NPORTS, 0, &wdc_obio_isr_hdl)) {
    212 		printf("%s: couldn't map intr status register\n",
    213 			sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    214 		return;
    215 	}
    216 
    217 	switch (current_mac_model->machineid) {
    218 	case MACH_MACP580:
    219 	case MACH_MACQ630:
    220 		/*
    221 		 * Quadra/Performa IDE generates pseudo Nubus intr at slot F
    222 		 */
    223 		printf(" (Quadra/Performa series IDE interface)");
    224 
    225 		add_nubus_intr(0xf, (void (*)(void*))wdc_obio_intr, (void *)sc);
    226 
    227 		break;
    228 	case MACH_MACPB150:
    229 	case MACH_MACPB190:
    230 	case MACH_MACPB190CS:
    231 		/*
    232 		 * PowerBook IDE generates pseudo NuBus intr at slot C
    233 		 */
    234 		printf(" (PowerBook series IDE interface)");
    235 
    236 		add_nubus_intr(0xc, (void (*)(void*))wdc_obio_intr, (void *)sc);
    237 
    238 		break;
    239 	}
    240 
    241 	ch_sc = chp;
    242 	if (sc->sc_wdcdev.sc_atac.atac_dev.dv_cfdata->cf_flags & ATAC_CAP_NOIRQ)
    243 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ;
    244 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
    245 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
    246 	sc->sc_chanlist[0] = chp;
    247 	sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
    248 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
    249 	chp->ch_channel = 0;
    250 	chp->ch_atac = &sc->sc_wdcdev.sc_atac;
    251 	chp->ch_queue = &sc->sc_chqueue;
    252 
    253 	printf("\n");
    254 
    255 	wdcattach(chp);
    256 }
    257