Home | History | Annotate | Line # | Download | only in podulebus
simide.c revision 1.7
      1 /*	$NetBSD: simide.c,v 1.7 2002/10/02 03:31:59 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997-1998 Mark Brinicombe
      5  * Copyright (c) 1997-1998 Causality Limited
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Mark Brinicombe
     18  *	for the NetBSD Project.
     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  * Card driver and probe and attach functions to use generic IDE driver
     34  * for the Simtec IDE podule
     35  */
     36 
     37 /*
     38  * Thanks to Gareth Simpson, Simtec Electronics for providing
     39  * the hardware information
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/conf.h>
     45 #include <sys/device.h>
     46 #include <sys/malloc.h>
     47 
     48 #include <machine/intr.h>
     49 #include <machine/io.h>
     50 #include <machine/bus.h>
     51 #include <acorn32/podulebus/podulebus.h>
     52 #include <acorn32/podulebus/simidereg.h>
     53 
     54 #include <dev/ata/atavar.h>
     55 #include <dev/ic/wdcvar.h>
     56 #include <dev/podulebus/podules.h>
     57 
     58 
     59 /*
     60  * Simtec IDE podule device.
     61  *
     62  * This probes and attaches the top level Simtec IDE device to the podulebus.
     63  * It then configures any children of the Simtec IDE device.
     64  * The attach args specify whether it is configuring the primary or
     65  * secondary channel.
     66  * The children are expected to be wdc devices using simide attachments.
     67  */
     68 
     69 /*
     70  * Simtec IDE card softc structure.
     71  *
     72  * Contains the device node, podule information and global information
     73  * required by the driver such as the card version and the interrupt mask.
     74  */
     75 
     76 struct simide_softc {
     77 	struct wdc_softc	sc_wdcdev;	/* common wdc definitions */
     78 	struct channel_softc	*wdc_chanarray[2]; /* channels definition */
     79 	podule_t 		*sc_podule;		/* Our podule info */
     80 	int 			sc_podule_number;	/* Our podule number */
     81 	int			sc_ctl_reg;		/* Global ctl reg */
     82 	int			sc_version;		/* Card version */
     83 	bus_space_tag_t		sc_ctliot;		/* Bus tag */
     84 	bus_space_handle_t	sc_ctlioh;		/* control handle */
     85 	struct bus_space 	sc_tag;			/* custom tag */
     86 	struct simide_channel {
     87 		struct channel_softc wdc_channel; /* generic part */
     88 		irqhandler_t	sc_ih;			/* interrupt handler */
     89 		int		sc_irqmask;	/* IRQ mask for this channel */
     90 	} simide_channels[2];
     91 };
     92 
     93 int	simide_probe	__P((struct device *, struct cfdata *, void *));
     94 void	simide_attach	__P((struct device *, struct device *, void *));
     95 void	simide_shutdown	__P((void *arg));
     96 int	simide_intr	__P((void *arg));
     97 
     98 CFATTACH_DECL(simide, sizeof(struct simide_softc),
     99     simide_probe, simide_attach, NULL, NULL);
    100 
    101 
    102 /*
    103  * Define prototypes for custom bus space functions.
    104  */
    105 
    106 bs_rm_2_proto(simide);
    107 bs_wm_2_proto(simide);
    108 
    109 /*
    110  * Create an array of address structures. These define the addresses and
    111  * masks needed for the different channels.
    112  *
    113  * index = channel
    114  */
    115 
    116 struct {
    117 	u_int drive_registers;
    118 	u_int aux_register;
    119 	u_int irq_mask;
    120 } simide_info[] = {
    121 	{ PRIMARY_DRIVE_REGISTERS_POFFSET, PRIMARY_AUX_REGISTER_POFFSET,
    122 	  CONTROL_PRIMARY_IRQ },
    123 	{ SECONDARY_DRIVE_REGISTERS_POFFSET, SECONDARY_AUX_REGISTER_POFFSET,
    124 	  CONTROL_SECONDARY_IRQ }
    125 };
    126 
    127 /*
    128  * Card probe function
    129  *
    130  * Just match the manufacturer and podule ID's
    131  */
    132 
    133 int
    134 simide_probe(parent, cf, aux)
    135 	struct device *parent;
    136 	struct cfdata *cf;
    137 	void *aux;
    138 {
    139 	struct podule_attach_args *pa = (void *)aux;
    140 
    141 	return (pa->pa_product == PODULE_SIMTEC_IDE);
    142 }
    143 
    144 /*
    145  * Card attach function
    146  *
    147  * Identify the card version and configure any children.
    148  * Install a shutdown handler to kill interrupts on shutdown
    149  */
    150 
    151 void
    152 simide_attach(parent, self, aux)
    153 	struct device *parent, *self;
    154 	void *aux;
    155 {
    156 	struct simide_softc *sc = (void *)self;
    157 	struct podule_attach_args *pa = (void *)aux;
    158 	int status;
    159 	u_int iobase;
    160 	int channel;
    161 	struct simide_channel *scp;
    162 	struct channel_softc *cp;
    163 	irqhandler_t *ihp;
    164 
    165 	/* Note the podule number and validate */
    166 	if (pa->pa_podule_number == -1)
    167 		panic("Podule has disappeared !");
    168 
    169 	sc->sc_podule_number = pa->pa_podule_number;
    170 	sc->sc_podule = pa->pa_podule;
    171 	podules[sc->sc_podule_number].attached = 1;
    172 
    173 	/*
    174 	 * Ok we need our own bus tag as the register spacing
    175 	 * is not the default.
    176 	 *
    177 	 * For the podulebus the bus tag cookie is the shift
    178 	 * to apply to registers
    179 	 * So duplicate the bus space tag and change the
    180 	 * cookie.
    181 	 *
    182 	 * Also while we are at it replace the default
    183 	 * read/write mulitple short functions with
    184 	 * optimised versions
    185 	 */
    186 
    187 	sc->sc_tag = *pa->pa_iot;
    188 	sc->sc_tag.bs_cookie = (void *) DRIVE_REGISTER_SPACING_SHIFT;
    189 	sc->sc_tag.bs_rm_2 = simide_bs_rm_2;
    190 	sc->sc_tag.bs_wm_2 = simide_bs_wm_2;
    191 	sc->sc_ctliot = pa->pa_iot;
    192 
    193 	/* Obtain bus space handles for all the control registers */
    194 	if (bus_space_map(sc->sc_ctliot, pa->pa_podule->mod_base +
    195 	    CONTROL_REGISTERS_POFFSET, CONTROL_REGISTER_SPACE, 0,
    196 	    &sc->sc_ctlioh))
    197 		panic("%s: Cannot map control registers", self->dv_xname);
    198 
    199 	/* Install a clean up handler to make sure IRQ's are disabled */
    200 	if (shutdownhook_establish(simide_shutdown, (void *)sc) == NULL)
    201 		panic("%s: Cannot install shutdown handler", self->dv_xname);
    202 
    203 	/* Set the interrupt info for this podule */
    204 	sc->sc_podule->irq_addr = pa->pa_podule->mod_base
    205 	    + CONTROL_REGISTERS_POFFSET + (CONTROL_REGISTER_OFFSET << 2);
    206 	sc->sc_podule->irq_mask = STATUS_IRQ;
    207 
    208 	sc->sc_ctl_reg = 0;
    209 
    210 	status = bus_space_read_1(sc->sc_ctliot, sc->sc_ctlioh,
    211 	    STATUS_REGISTER_OFFSET);
    212 
    213 	printf(":");
    214 	/* If any of the bits in STATUS_FAULT are zero then we have a fault. */
    215 	if ((status & STATUS_FAULT) != STATUS_FAULT)
    216 		printf(" card/cable fault (%02x) -", status);
    217 
    218 	if (!(status & STATUS_RESET))
    219 		printf(" (reset)");
    220 	if (!(status & STATUS_ADDR_TEST))
    221 		printf(" (addr)");
    222 	if (!(status & STATUS_CS_TEST))
    223 		printf(" (cs)");
    224 	if (!(status & STATUS_RW_TEST))
    225 		printf(" (rw)");
    226 
    227 	printf("\n");
    228 
    229 	/* Perhaps we should just abort at this point. */
    230 /*	if ((status & STATUS_FAULT) != STATUS_FAULT)
    231 		return;*/
    232 
    233 	/*
    234 	 * Enable IDE, Obey IORDY and disabled slow mode
    235 	 */
    236 	sc->sc_ctl_reg |= CONTROL_IDE_ENABLE | CONTROL_IORDY
    237 			| CONTROL_SLOW_MODE_OFF;
    238 	bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
    239 	    CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
    240 
    241 	/* Fill in wdc and channel infos */
    242 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    243 	sc->sc_wdcdev.PIO_cap = 0;
    244 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
    245 	sc->sc_wdcdev.nchannels = 2;
    246 	for (channel = 0 ; channel < 2; channel++) {
    247 		scp = &sc->simide_channels[channel];
    248 		sc->wdc_chanarray[channel] = &scp->wdc_channel;
    249 		cp = &scp->wdc_channel;
    250 
    251 		cp->channel = channel;
    252 		cp->wdc = &sc->sc_wdcdev;
    253 		cp->ch_queue = malloc(sizeof(struct channel_queue),
    254 		    M_DEVBUF, M_NOWAIT);
    255 		if (cp->ch_queue == NULL) {
    256 			printf("%s %s channel: can't allocate memory for "
    257 			    "command queue", self->dv_xname,
    258 			    (channel == 0) ? "primary" : "secondary");
    259 			continue;
    260 		}
    261 		cp->cmd_iot = cp->ctl_iot = &sc->sc_tag;
    262 		iobase = pa->pa_podule->mod_base;
    263 		if (bus_space_map(cp->cmd_iot, iobase +
    264 		    simide_info[channel].drive_registers,
    265 		    DRIVE_REGISTERS_SPACE, 0, &cp->cmd_ioh))
    266 			continue;
    267 		if (bus_space_map(cp->ctl_iot, iobase +
    268 		    simide_info[channel].aux_register, 4, 0, &cp->ctl_ioh)) {
    269 			bus_space_unmap(cp->cmd_iot, cp->cmd_ioh,
    270 			    DRIVE_REGISTERS_SPACE);
    271 			continue;
    272 		}
    273 		/* Disable interrupts and clear any pending interrupts */
    274 		scp->sc_irqmask = simide_info[channel].irq_mask;
    275 		sc->sc_ctl_reg &= ~scp->sc_irqmask;
    276 		bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
    277 		    CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
    278 		wdcattach(cp);
    279 		ihp = &scp->sc_ih;
    280 		ihp->ih_func = simide_intr;
    281 		ihp->ih_arg = scp;
    282 		ihp->ih_level = IPL_BIO;
    283 		ihp->ih_name = "simide";
    284 		ihp->ih_maskaddr = pa->pa_podule->irq_addr;
    285 		ihp->ih_maskbits = scp->sc_irqmask;
    286 		if (irq_claim(sc->sc_podule->interrupt, ihp))
    287 			panic("%s: Cannot claim interrupt %d",
    288 			    self->dv_xname, sc->sc_podule->interrupt);
    289 		/* clear any pending interrupts and enable interrupts */
    290 		sc->sc_ctl_reg |= scp->sc_irqmask;
    291 		bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
    292 		    CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
    293 	}
    294 
    295 }
    296 
    297 /*
    298  * Card shutdown function
    299  *
    300  * Called via do_shutdown_hooks() during kernel shutdown.
    301  * Clear the cards's interrupt mask to stop any podule interrupts.
    302  */
    303 
    304 void
    305 simide_shutdown(arg)
    306 	void *arg;
    307 {
    308 	struct simide_softc *sc = arg;
    309 
    310 	sc->sc_ctl_reg &= (CONTROL_PRIMARY_IRQ | CONTROL_SECONDARY_IRQ);
    311 
    312 	/* Disable card interrupts */
    313 	bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
    314 	    CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
    315 }
    316 
    317 /*
    318  * Podule interrupt handler
    319  *
    320  * If the interrupt was from our card pass it on to the wdc interrupt handler
    321  */
    322 int
    323 simide_intr(arg)
    324 	void *arg;
    325 {
    326 	struct simide_channel *scp = arg;
    327 	irqhandler_t *ihp = &scp->sc_ih;
    328 	volatile u_char *intraddr = (volatile u_char *)ihp->ih_maskaddr;
    329 
    330 	/* XXX - not bus space yet - should really be handled by podulebus */
    331 	if ((*intraddr) & ihp->ih_maskbits)
    332 		wdcintr(&scp->wdc_channel);
    333 
    334 	return(0);
    335 }
    336