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