Home | History | Annotate | Line # | Download | only in podulebus
icside.c revision 1.4
      1 /*	$NetBSD: icside.c,v 1.4 2002/09/14 18:12:16 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  * Probe and attach functions to use generic IDE driver for the ICS IDE podule
     34  */
     35 
     36 /*
     37  * Thanks to David Baildon for loaning an IDE card for the development
     38  * of this driver.
     39  * Thanks to Ian Copestake and David Baildon for providing register mapping
     40  * information
     41  */
     42 
     43 /*
     44  * BUGS:
     45  * Cold boot (hard reset or power-on) booting straight to NetBSD:
     46  * panic: wdcstart: channel waiting for irq
     47  * Booting via desktop avoinds this.
     48  */
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/conf.h>
     53 #include <sys/device.h>
     54 #include <sys/malloc.h>
     55 
     56 #include <machine/intr.h>
     57 #include <machine/io.h>
     58 #include <machine/bus.h>
     59 #include <acorn32/podulebus/podulebus.h>
     60 #include <acorn32/podulebus/icsidereg.h>
     61 
     62 #include <dev/ata/atavar.h>
     63 #include <dev/ic/wdcvar.h>
     64 #include <dev/podulebus/podules.h>
     65 
     66 /*
     67  * ICS IDE podule device.
     68  *
     69  * This probes and attaches the top level ICS IDE device to the podulebus.
     70  * It then configures any children of the ICS IDE device.
     71  * The child is expected to be a wdc device using icside attachments.
     72  */
     73 
     74 /*
     75  * ICS IDE card softc structure.
     76  *
     77  * Contains the device node and podule information.
     78  */
     79 
     80 struct icside_softc {
     81 	struct wdc_softc	sc_wdcdev;	/* common wdc definitions */
     82 	podule_t 		*sc_podule;		/* Our podule */
     83 	int 			sc_podule_number;	/* Our podule number */
     84 	struct bus_space 	sc_tag;			/* custom tag */
     85 	struct podule_attach_args *sc_pa;		/* podule info */
     86 	bus_space_tag_t		sc_latchiot;	/* EEPROM page latch etc */
     87 	bus_space_handle_t	sc_latchioh;
     88 	void			*sc_shutdownhook;
     89 	struct icside_channel {
     90 		struct channel_softc	wdc_channel;	/* generic part */
     91 		void			*ic_ih;		/* interrupt handler */
     92 		struct evcnt		ic_intrcnt;	/* interrupt count */
     93 		u_int			ic_irqaddr;	/* interrupt flag */
     94 		u_int			ic_irqmask;	/*  location */
     95 		bus_space_tag_t		ic_irqiot;	/* Bus space tag */
     96 		bus_space_handle_t	ic_irqioh;	/* handle for IRQ */
     97 	} *icside_channels;
     98 };
     99 
    100 int	icside_probe	__P((struct device *, struct cfdata *, void *));
    101 void	icside_attach	__P((struct device *, struct device *, void *));
    102 int	icside_intr	__P((void *));
    103 void	icside_v6_shutdown(void *);
    104 
    105 struct cfattach icside_ca = {
    106 	sizeof(struct icside_softc), icside_probe, icside_attach
    107 };
    108 
    109 /*
    110  * Define prototypes for custom bus space functions.
    111  */
    112 
    113 bs_rm_2_proto(icside);
    114 bs_wm_2_proto(icside);
    115 
    116 #define MAX_CHANNELS	2
    117 
    118 /*
    119  * Define a structure for describing the different card versions
    120  */
    121 struct ide_version {
    122 	int		id;		/* IDE card ID */
    123 	int		modspace;	/* Type of podule space */
    124 	int		channels;	/* Number of channels */
    125 	const char	*name;		/* name */
    126 	int		latchreg;	/* EEPROM latch register */
    127 	int		ideregs[MAX_CHANNELS];	/* IDE registers */
    128 	int		auxregs[MAX_CHANNELS];	/* AUXSTAT register */
    129 	int		irqregs[MAX_CHANNELS];	/* IRQ register */
    130 	int		irqstatregs[MAX_CHANNELS];
    131 } ide_versions[] = {
    132 	/* A3IN - Unsupported */
    133 /*	{ 0,  0, 0, NULL, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },*/
    134 	/* A3USER - Unsupported */
    135 /*	{ 1,  0, 0, NULL, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },*/
    136 	/* ARCIN V6 - Supported */
    137 	{ 3,  0, 2, "ARCIN V6", V6_ADDRLATCH,
    138 	  { V6_P_IDE_BASE, V6_S_IDE_BASE },
    139 	  { V6_P_AUX_BASE, V6_S_AUX_BASE },
    140 	  { V6_P_IRQ_BASE, V6_S_IRQ_BASE },
    141 	  { V6_P_IRQSTAT_BASE, V6_S_IRQSTAT_BASE }
    142 	},
    143 	/* ARCIN V5 - Supported (ID reg not supported so reads as 15) */
    144 	{ 15,  1, 1, "ARCIN V5", -1,
    145 	  { V5_IDE_BASE, -1 },
    146 	  { V5_AUX_BASE, -1 },
    147 	  { V5_IRQ_BASE, -1 },
    148 	  { V5_IRQSTAT_BASE, -1 }
    149 	}
    150 };
    151 
    152 /*
    153  * Card probe function
    154  *
    155  * Just match the manufacturer and podule ID's
    156  */
    157 
    158 int
    159 icside_probe(parent, cf, aux)
    160 	struct device *parent;
    161 	struct cfdata *cf;
    162 	void *aux;
    163 {
    164 	struct podule_attach_args *pa = (void *)aux;
    165 	return (pa->pa_product == PODULE_ICS_IDE);
    166 }
    167 
    168 /*
    169  * Card attach function
    170  *
    171  * Identify the card version and configure any children.
    172  */
    173 
    174 void
    175 icside_attach(parent, self, aux)
    176 	struct device *parent, *self;
    177 	void *aux;
    178 {
    179 	struct icside_softc *sc = (void *)self;
    180 	struct podule_attach_args *pa = (void *)aux;
    181 	bus_space_tag_t iot;
    182 	bus_space_handle_t ioh;
    183 	struct ide_version *ide = NULL;
    184 	u_int iobase;
    185 	int channel;
    186 	struct icside_channel *icp;
    187 	struct channel_softc *cp;
    188 	int loop;
    189 	int id;
    190 
    191 	/* Note the podule number and validate */
    192 
    193 	if (pa->pa_podule_number == -1)
    194 		panic("Podule has disappeared !");
    195 
    196 	sc->sc_podule_number = pa->pa_podule_number;
    197 	sc->sc_podule = pa->pa_podule;
    198 	podules[sc->sc_podule_number].attached = 1;
    199 
    200 	/* The ID register if present is always in FAST podule space */
    201 	iot = pa->pa_iot;
    202 	if (bus_space_map(iot, pa->pa_podule->fast_base +
    203 	    ID_REGISTER_OFFSET, ID_REGISTER_SPACE, 0, &ioh)) {
    204 		printf("%s: cannot map ID register\n", self->dv_xname);
    205 		return;
    206 	}
    207 
    208 	for (id = 0, loop = 0; loop < 4; ++loop)
    209 		id |= (bus_space_read_1(iot, ioh, loop) & 1) << loop;
    210 
    211 	/* Do we recognise the ID ? */
    212 	for (loop = 0; loop < sizeof(ide_versions) / sizeof(struct ide_version);
    213 	    ++loop) {
    214 		if (ide_versions[loop].id == id) {
    215 			ide = &ide_versions[loop];
    216 			break;
    217 		}
    218 	}
    219 
    220 	/* Report the version and name */
    221 	if (ide == NULL || ide->name == NULL) {
    222 		printf(": rev %d is unsupported\n", id);
    223 		return;
    224 	} else
    225 		printf(": %s\n", ide->name);
    226 
    227 	if (ide->latchreg != -1) {
    228 		sc->sc_latchiot = pa->pa_iot;
    229 		if (bus_space_map(iot, pa->pa_podule->fast_base +
    230 			ide->latchreg, 1, 0, &sc->sc_latchioh)) {
    231 			printf("%s: cannot map latch register\n",
    232 			    self->dv_xname);
    233 			return;
    234 		}
    235 		sc->sc_shutdownhook =
    236 		    shutdownhook_establish(icside_v6_shutdown, sc);
    237 	}
    238 
    239 	/*
    240 	 * Ok we need our own bus tag as the register spacing
    241 	 * is not the default.
    242 	 *
    243 	 * For the podulebus the bus tag cookie is the shift
    244 	 * to apply to registers
    245 	 * So duplicate the bus space tag and change the
    246 	 * cookie.
    247 	 *
    248 	 * Also while we are at it replace the default
    249 	 * read/write mulitple short functions with
    250 	 * optimised versions
    251 	 */
    252 
    253 	sc->sc_tag = *pa->pa_iot;
    254 	sc->sc_tag.bs_cookie = (void *) REGISTER_SPACING_SHIFT;
    255 	sc->sc_tag.bs_rm_2 = icside_bs_rm_2;
    256 	sc->sc_tag.bs_wm_2 = icside_bs_wm_2;
    257 
    258 	/* Initialize wdc struct */
    259 	sc->sc_wdcdev.channels = malloc(
    260 	    sizeof(struct channel_softc *) * ide->channels, M_DEVBUF, M_NOWAIT);
    261 	sc->icside_channels = malloc(
    262 	    sizeof(struct icside_channel) * ide->channels, M_DEVBUF, M_NOWAIT);
    263 	if (sc->sc_wdcdev.channels == NULL || sc->icside_channels == NULL) {
    264 		printf("%s: can't allocate channel infos\n",
    265 		    sc->sc_wdcdev.sc_dev.dv_xname);
    266 		return;
    267 	}
    268 	sc->sc_wdcdev.nchannels = ide->channels;
    269 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    270 	sc->sc_wdcdev.PIO_cap = 0;
    271 	sc->sc_pa = pa;
    272 
    273 	for (channel = 0; channel < ide->channels; ++channel) {
    274 		icp = &sc->icside_channels[channel];
    275 		sc->sc_wdcdev.channels[channel] = &icp->wdc_channel;
    276 		cp = &icp->wdc_channel;
    277 
    278 		cp->channel = channel;
    279 		cp->wdc = &sc->sc_wdcdev;
    280 		cp->ch_queue = malloc(sizeof(struct channel_queue), M_DEVBUF,
    281 		    M_NOWAIT);
    282 		if (cp->ch_queue == NULL) {
    283 			printf("%s %s channel: "
    284 			    "can't allocate memory for command queue",
    285 			    sc->sc_wdcdev.sc_dev.dv_xname,
    286 			    (channel == 0) ? "primary" : "secondary");
    287 			continue;
    288 		}
    289 		cp->cmd_iot = &sc->sc_tag;
    290 		cp->ctl_iot = &sc->sc_tag;
    291 		if (ide->modspace)
    292 			iobase = pa->pa_podule->mod_base;
    293 		else
    294 			iobase = pa->pa_podule->fast_base;
    295 
    296 		if (bus_space_map(iot, iobase + ide->ideregs[channel],
    297 		    IDE_REGISTER_SPACE, 0, &cp->cmd_ioh))
    298 			return;
    299 		if (bus_space_map(iot, iobase + ide->auxregs[channel],
    300 		    AUX_REGISTER_SPACE, 0, &cp->ctl_ioh))
    301 			return;
    302 		icp->ic_irqiot = iot;
    303 		if (bus_space_map(iot, iobase + ide->irqregs[channel],
    304 		    IRQ_REGISTER_SPACE, 0, &icp->ic_irqioh))
    305 			return;
    306 		/* Disable interrupts */
    307 		(void)bus_space_read_1(iot, icp->ic_irqioh, 0);
    308 		/* Call common attach routines */
    309 		wdcattach(cp);
    310 		/* Disable interrupts */
    311 		(void)bus_space_read_1(iot, icp->ic_irqioh, 0);
    312 		pa->pa_podule->irq_addr = iobase + ide->irqstatregs[channel];
    313 		pa->pa_podule->irq_mask = IRQ_STATUS_REGISTER_MASK;
    314 		icp->ic_irqaddr = pa->pa_podule->irq_addr;
    315 		icp->ic_irqmask = pa->pa_podule->irq_mask;
    316 		evcnt_attach_dynamic(&icp->ic_intrcnt, EVCNT_TYPE_INTR, NULL,
    317 		    self->dv_xname, "intr");
    318 		icp->ic_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO,
    319 		    icside_intr, icp, &icp->ic_intrcnt);
    320 		if (icp->ic_ih == NULL) {
    321 			printf("%s: Cannot claim interrupt %d\n",
    322 			    sc->sc_wdcdev.sc_dev.dv_xname,
    323 			    pa->pa_podule->interrupt);
    324 			continue;
    325 		}
    326 		/* Enable interrupts */
    327 		bus_space_write_1(iot, icp->ic_irqioh, 0, 0);
    328 	}
    329 }
    330 
    331 /*
    332  * Shutdown handler -- try to restore the card to a state where
    333  * RISC OS will see it.
    334  */
    335 void
    336 icside_v6_shutdown(void *arg)
    337 {
    338 	struct icside_softc *sc = arg;
    339 
    340 	bus_space_write_1(sc->sc_latchiot, sc->sc_latchioh, 0, 0);
    341 }
    342 
    343 /*
    344  * Podule interrupt handler
    345  *
    346  * If the interrupt was from our card pass it on to the wdc interrupt handler
    347  */
    348 int
    349 icside_intr(arg)
    350 	void *arg;
    351 {
    352 	struct icside_channel *icp = arg;
    353 	volatile u_char *intraddr = (volatile u_char *)icp->ic_irqaddr;
    354 
    355 	/* XXX - not bus space yet - should really be handled by podulebus */
    356 	if ((*intraddr) & icp->ic_irqmask)
    357 		wdcintr(&icp->wdc_channel);
    358 	return(0);
    359 }
    360