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