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