Home | History | Annotate | Line # | Download | only in podulebus
simide.c revision 1.2
      1 /*	$NetBSD: simide.c,v 1.2 2001/11/27 00:53:12 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 struct cfattach simide_ca = {
     99 	sizeof(struct simide_softc), simide_probe, simide_attach
    100 };
    101 
    102 
    103 /*
    104  * Define prototypes for custom bus space functions.
    105  */
    106 
    107 bs_rm_2_proto(simide);
    108 bs_wm_2_proto(simide);
    109 
    110 /*
    111  * Create an array of address structures. These define the addresses and
    112  * masks needed for the different channels.
    113  *
    114  * index = channel
    115  */
    116 
    117 struct {
    118 	u_int drive_registers;
    119 	u_int aux_register;
    120 	u_int irq_mask;
    121 } simide_info[] = {
    122 	{ PRIMARY_DRIVE_REGISTERS_POFFSET, PRIMARY_AUX_REGISTER_POFFSET,
    123 	  CONTROL_PRIMARY_IRQ },
    124 	{ SECONDARY_DRIVE_REGISTERS_POFFSET, SECONDARY_AUX_REGISTER_POFFSET,
    125 	  CONTROL_SECONDARY_IRQ }
    126 };
    127 
    128 /*
    129  * Card probe function
    130  *
    131  * Just match the manufacturer and podule ID's
    132  */
    133 
    134 int
    135 simide_probe(parent, cf, aux)
    136 	struct device *parent;
    137 	struct cfdata *cf;
    138 	void *aux;
    139 {
    140 	struct podule_attach_args *pa = (void *)aux;
    141 
    142 	if (matchpodule(pa, MANUFACTURER_SIMTEC, PODULE_SIMTEC_IDE, -1) == 0)
    143 		return(0);
    144 	return(1);
    145 }
    146 
    147 /*
    148  * Card attach function
    149  *
    150  * Identify the card version and configure any children.
    151  * Install a shutdown handler to kill interrupts on shutdown
    152  */
    153 
    154 void
    155 simide_attach(parent, self, aux)
    156 	struct device *parent, *self;
    157 	void *aux;
    158 {
    159 	struct simide_softc *sc = (void *)self;
    160 	struct podule_attach_args *pa = (void *)aux;
    161 	int status;
    162 	u_int iobase;
    163 	int channel;
    164 	struct simide_channel *scp;
    165 	struct channel_softc *cp;
    166 	irqhandler_t *ihp;
    167 
    168 	/* Note the podule number and validate */
    169 	if (pa->pa_podule_number == -1)
    170 		panic("Podule has disappeared !");
    171 
    172 	sc->sc_podule_number = pa->pa_podule_number;
    173 	sc->sc_podule = pa->pa_podule;
    174 	podules[sc->sc_podule_number].attached = 1;
    175 
    176 	/*
    177 	 * Ok we need our own bus tag as the register spacing
    178 	 * is not the default.
    179 	 *
    180 	 * For the podulebus the bus tag cookie is the shift
    181 	 * to apply to registers
    182 	 * So duplicate the bus space tag and change the
    183 	 * cookie.
    184 	 *
    185 	 * Also while we are at it replace the default
    186 	 * read/write mulitple short functions with
    187 	 * optimised versions
    188 	 */
    189 
    190 	sc->sc_tag = *pa->pa_iot;
    191 	sc->sc_tag.bs_cookie = (void *) DRIVE_REGISTER_SPACING_SHIFT;
    192 	sc->sc_tag.bs_rm_2 = simide_bs_rm_2;
    193 	sc->sc_tag.bs_wm_2 = simide_bs_wm_2;
    194 	sc->sc_ctliot = pa->pa_iot;
    195 
    196 	/* Obtain bus space handles for all the control registers */
    197 	if (bus_space_map(sc->sc_ctliot, pa->pa_podule->mod_base +
    198 	    CONTROL_REGISTERS_POFFSET, CONTROL_REGISTER_SPACE, 0,
    199 	    &sc->sc_ctlioh))
    200 		panic("%s: Cannot map control registers\n", self->dv_xname);
    201 
    202 	/* Install a clean up handler to make sure IRQ's are disabled */
    203 	if (shutdownhook_establish(simide_shutdown, (void *)sc) == NULL)
    204 		panic("%s: Cannot install shutdown handler", self->dv_xname);
    205 
    206 	/* Set the interrupt info for this podule */
    207 	sc->sc_podule->irq_addr = pa->pa_podule->mod_base
    208 	    + CONTROL_REGISTERS_POFFSET + (CONTROL_REGISTER_OFFSET << 2);
    209 	sc->sc_podule->irq_mask = STATUS_IRQ;
    210 
    211 	sc->sc_ctl_reg = 0;
    212 
    213 	status = bus_space_read_1(sc->sc_ctliot, sc->sc_ctlioh,
    214 	    STATUS_REGISTER_OFFSET);
    215 
    216 	printf(":");
    217 	/* If any of the bits in STATUS_FAULT are zero then we have a fault. */
    218 	if ((status & STATUS_FAULT) != STATUS_FAULT)
    219 		printf(" card/cable fault (%02x) -", status);
    220 
    221 	if (!(status & STATUS_RESET))
    222 		printf(" (reset)");
    223 	if (!(status & STATUS_ADDR_TEST))
    224 		printf(" (addr)");
    225 	if (!(status & STATUS_CS_TEST))
    226 		printf(" (cs)");
    227 	if (!(status & STATUS_RW_TEST))
    228 		printf(" (rw)");
    229 
    230 	printf("\n");
    231 
    232 	/* Perhaps we should just abort at this point. */
    233 /*	if ((status & STATUS_FAULT) != STATUS_FAULT)
    234 		return;*/
    235 
    236 	/*
    237 	 * Enable IDE, Obey IORDY and disabled slow mode
    238 	 */
    239 	sc->sc_ctl_reg |= CONTROL_IDE_ENABLE | CONTROL_IORDY
    240 			| CONTROL_SLOW_MODE_OFF;
    241 	bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
    242 	    CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
    243 
    244 	/* Fill in wdc and channel infos */
    245 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    246 	sc->sc_wdcdev.PIO_cap = 0;
    247 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
    248 	sc->sc_wdcdev.nchannels = 2;
    249 	for (channel = 0 ; channel < 2; channel++) {
    250 		scp = &sc->simide_channels[channel];
    251 		sc->wdc_chanarray[channel] = &scp->wdc_channel;
    252 		cp = &scp->wdc_channel;
    253 
    254 		cp->channel = channel;
    255 		cp->wdc = &sc->sc_wdcdev;
    256 		cp->ch_queue = malloc(sizeof(struct channel_queue),
    257 		    M_DEVBUF, M_NOWAIT);
    258 		if (cp->ch_queue == NULL) {
    259 			printf("%s %s channel: can't allocate memory for "
    260 			    "command queue", self->dv_xname,
    261 			    (channel == 0) ? "primary" : "secondary");
    262 			continue;
    263 		}
    264 		cp->cmd_iot = cp->ctl_iot = &sc->sc_tag;
    265 		iobase = pa->pa_podule->mod_base;
    266 		if (bus_space_map(cp->cmd_iot, iobase +
    267 		    simide_info[channel].drive_registers,
    268 		    DRIVE_REGISTERS_SPACE, 0, &cp->cmd_ioh))
    269 			continue;
    270 		if (bus_space_map(cp->ctl_iot, iobase +
    271 		    simide_info[channel].aux_register, 4, 0, &cp->ctl_ioh)) {
    272 			bus_space_unmap(cp->cmd_iot, cp->cmd_ioh,
    273 			    DRIVE_REGISTERS_SPACE);
    274 			continue;
    275 		}
    276 		/* Disable interrupts and clear any pending interrupts */
    277 		scp->sc_irqmask = simide_info[channel].irq_mask;
    278 		sc->sc_ctl_reg &= ~scp->sc_irqmask;
    279 		bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
    280 		    CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
    281 		wdcattach(cp);
    282 		ihp = &scp->sc_ih;
    283 		ihp->ih_func = simide_intr;
    284 		ihp->ih_arg = scp;
    285 		ihp->ih_level = IPL_BIO;
    286 		ihp->ih_name = "simide";
    287 		ihp->ih_maskaddr = pa->pa_podule->irq_addr;
    288 		ihp->ih_maskbits = scp->sc_irqmask;
    289 		if (irq_claim(sc->sc_podule->interrupt, ihp))
    290 			panic("%s: Cannot claim interrupt %d\n",
    291 			    self->dv_xname, sc->sc_podule->interrupt);
    292 		/* clear any pending interrupts and enable interrupts */
    293 		sc->sc_ctl_reg |= scp->sc_irqmask;
    294 		bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
    295 		    CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
    296 	}
    297 
    298 }
    299 
    300 /*
    301  * Card shutdown function
    302  *
    303  * Called via do_shutdown_hooks() during kernel shutdown.
    304  * Clear the cards's interrupt mask to stop any podule interrupts.
    305  */
    306 
    307 void
    308 simide_shutdown(arg)
    309 	void *arg;
    310 {
    311 	struct simide_softc *sc = arg;
    312 
    313 	sc->sc_ctl_reg &= (CONTROL_PRIMARY_IRQ | CONTROL_SECONDARY_IRQ);
    314 
    315 	/* Disable card interrupts */
    316 	bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
    317 	    CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
    318 }
    319 
    320 /*
    321  * Podule interrupt handler
    322  *
    323  * If the interrupt was from our card pass it on to the wdc interrupt handler
    324  */
    325 int
    326 simide_intr(arg)
    327 	void *arg;
    328 {
    329 	struct simide_channel *scp = arg;
    330 	irqhandler_t *ihp = &scp->sc_ih;
    331 	volatile u_char *intraddr = (volatile u_char *)ihp->ih_maskaddr;
    332 
    333 	/* XXX - not bus space yet - should really be handled by podulebus */
    334 	if ((*intraddr) & ihp->ih_maskbits)
    335 		wdcintr(&scp->wdc_channel);
    336 
    337 	return(0);
    338 }
    339