Home | History | Annotate | Line # | Download | only in sbd
if_iee_sbdio.c revision 1.8
      1  1.8  tsutsui /*	$NetBSD: if_iee_sbdio.c,v 1.8 2009/05/09 03:22:20 tsutsui Exp $	*/
      2  1.1  tsutsui 
      3  1.1  tsutsui /*
      4  1.1  tsutsui  * Copyright (c) 2003 Jochen Kunz.
      5  1.1  tsutsui  * All rights reserved.
      6  1.1  tsutsui  *
      7  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
      8  1.1  tsutsui  * modification, are permitted provided that the following conditions
      9  1.1  tsutsui  * are met:
     10  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     12  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     15  1.1  tsutsui  * 3. The name of Jochen Kunz may not be used to endorse or promote
     16  1.1  tsutsui  *    products derived from this software without specific prior
     17  1.1  tsutsui  *    written permission.
     18  1.1  tsutsui  *
     19  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY JOCHEN KUNZ
     20  1.1  tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JOCHEN KUNZ
     23  1.1  tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  tsutsui  */
     31  1.1  tsutsui 
     32  1.1  tsutsui #include <sys/cdefs.h>
     33  1.8  tsutsui __KERNEL_RCSID(0, "$NetBSD: if_iee_sbdio.c,v 1.8 2009/05/09 03:22:20 tsutsui Exp $");
     34  1.1  tsutsui 
     35  1.1  tsutsui #include <sys/param.h>
     36  1.1  tsutsui #include <sys/systm.h>
     37  1.1  tsutsui #include <sys/device.h>
     38  1.1  tsutsui 
     39  1.1  tsutsui #include <net/if.h>
     40  1.1  tsutsui #include <net/if_dl.h>
     41  1.1  tsutsui #include <net/if_media.h>
     42  1.1  tsutsui #include <net/if_ether.h>
     43  1.1  tsutsui #include <sys/socket.h>
     44  1.1  tsutsui #include <sys/mbuf.h>
     45  1.1  tsutsui 
     46  1.1  tsutsui #include <mips/cache.h>
     47  1.1  tsutsui #include <machine/bus.h>
     48  1.1  tsutsui #include <machine/intr.h>
     49  1.1  tsutsui #include <machine/sbdvar.h>	/* for ether_addr() */
     50  1.1  tsutsui #include <machine/sbdiovar.h>
     51  1.1  tsutsui 
     52  1.1  tsutsui #include <dev/ic/i82596reg.h>
     53  1.1  tsutsui #include <dev/ic/i82596var.h>
     54  1.1  tsutsui 
     55  1.5  tsutsui int iee_sbdio_match(device_t, cfdata_t, void *);
     56  1.5  tsutsui void iee_sbdio_attach(device_t, device_t, void *);
     57  1.1  tsutsui int iee_sbdio_cmd(struct iee_softc *, uint32_t);
     58  1.1  tsutsui int iee_sbdio_reset(struct iee_softc *);
     59  1.1  tsutsui 
     60  1.1  tsutsui static void iee_sbdio_channel_attention(void *);
     61  1.1  tsutsui static void iee_sbdio_chip_reset(void *);
     62  1.1  tsutsui static void iee_sbdio_set_scp(void *, uint32_t);
     63  1.1  tsutsui 
     64  1.1  tsutsui struct iee_sbdio_softc {
     65  1.1  tsutsui 	struct iee_softc sc_iee;
     66  1.6  tsutsui 	volatile uint32_t *sc_port;	/* CPU <-> i82596 interface */
     67  1.1  tsutsui };
     68  1.1  tsutsui 
     69  1.5  tsutsui CFATTACH_DECL_NEW(iee_sbdio, sizeof(struct iee_sbdio_softc),
     70  1.5  tsutsui     iee_sbdio_match, iee_sbdio_attach, NULL, NULL);
     71  1.1  tsutsui 
     72  1.1  tsutsui int
     73  1.5  tsutsui iee_sbdio_match(device_t parent, cfdata_t cf, void *aux)
     74  1.1  tsutsui {
     75  1.1  tsutsui 	struct sbdio_attach_args *sa = aux;
     76  1.1  tsutsui 
     77  1.1  tsutsui 	return strcmp(sa->sa_name, "iee") ? 0 : 1;
     78  1.1  tsutsui }
     79  1.1  tsutsui 
     80  1.1  tsutsui void
     81  1.5  tsutsui iee_sbdio_attach(device_t parent, device_t self, void *aux)
     82  1.1  tsutsui {
     83  1.5  tsutsui 	struct iee_sbdio_softc *sc_ssc = device_private(self);
     84  1.5  tsutsui 	struct iee_softc *sc = &sc_ssc->sc_iee;
     85  1.1  tsutsui 	struct sbdio_attach_args *sa = aux;
     86  1.1  tsutsui 	uint8_t eaddr[ETHER_ADDR_LEN];
     87  1.1  tsutsui 	int media[2];
     88  1.1  tsutsui 
     89  1.5  tsutsui 	sc->sc_dev = self;
     90  1.5  tsutsui 	sc_ssc->sc_port =
     91  1.5  tsutsui 	    (volatile uint32_t *)MIPS_PHYS_TO_KSEG1(sa->sa_addr1);
     92  1.1  tsutsui 	sc->sc_type = I82596_CA;
     93  1.1  tsutsui 	sc->sc_flags = IEE_NEED_SWAP;
     94  1.1  tsutsui 
     95  1.1  tsutsui 	/* bus_dma round the dma size to page size. */
     96  1.1  tsutsui 	sc->sc_cl_align = 1;
     97  1.1  tsutsui 
     98  1.1  tsutsui 	sc->sc_dmat = sa->sa_dmat;
     99  1.1  tsutsui 
    100  1.1  tsutsui 	/* Setup SYSBUS byte. TR2 specific? -uch */
    101  1.8  tsutsui 	sc->sc_sysbus = IEE_SYSBUS_BE | IEE_SYSBUS_INT |
    102  1.1  tsutsui 	    IEE_SYSBUS_LIEAR | IEE_SYSBUS_STD;
    103  1.1  tsutsui 
    104  1.1  tsutsui 	sc->sc_iee_reset = iee_sbdio_reset;
    105  1.1  tsutsui 	sc->sc_iee_cmd = iee_sbdio_cmd;
    106  1.1  tsutsui 	sc->sc_mediachange = NULL;
    107  1.1  tsutsui 	sc->sc_mediastatus = NULL;
    108  1.1  tsutsui 
    109  1.1  tsutsui 	media[0] = IFM_ETHER | IFM_MANUAL;
    110  1.1  tsutsui 	media[1] = IFM_ETHER | IFM_10_5;
    111  1.1  tsutsui 
    112  1.5  tsutsui 	intr_establish(sa->sa_irq, iee_intr, sc);
    113  1.1  tsutsui 	(*platform.ether_addr)(eaddr);
    114  1.1  tsutsui 	iee_attach(sc, eaddr, media, 2, IFM_ETHER | IFM_10_5);
    115  1.1  tsutsui }
    116  1.1  tsutsui 
    117  1.1  tsutsui int
    118  1.1  tsutsui iee_sbdio_cmd(struct iee_softc *sc, uint32_t cmd)
    119  1.1  tsutsui {
    120  1.1  tsutsui 	int retry = 8;
    121  1.1  tsutsui 	int n;
    122  1.7  tsutsui 	uint32_t ack;
    123  1.1  tsutsui 
    124  1.1  tsutsui 	SC_SCB->scb_cmd = cmd;
    125  1.1  tsutsui 	bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCB_OFF, IEE_SCB_SZ,
    126  1.7  tsutsui 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    127  1.1  tsutsui 
    128  1.1  tsutsui 	iee_sbdio_channel_attention(sc);
    129  1.1  tsutsui 
    130  1.1  tsutsui 	/* Wait for the cmd to finish */
    131  1.1  tsutsui 	for (n = 0 ; n < retry; n++) {
    132  1.1  tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCB_OFF,
    133  1.7  tsutsui 		    IEE_SCB_SZ, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    134  1.7  tsutsui 		ack = SC_SCB->scb_cmd;
    135  1.7  tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCB_OFF,
    136  1.1  tsutsui 		    IEE_SCB_SZ, BUS_DMASYNC_PREREAD);
    137  1.7  tsutsui 		if (ack == 0)
    138  1.1  tsutsui 			break;
    139  1.1  tsutsui 		delay(1);
    140  1.1  tsutsui 	}
    141  1.1  tsutsui 	if (n < retry)
    142  1.1  tsutsui 		return 0;
    143  1.1  tsutsui 
    144  1.5  tsutsui 	printf("%s: command timeout. retry=%d\n",
    145  1.5  tsutsui 	    device_xname(sc->sc_dev), retry);
    146  1.1  tsutsui 
    147  1.1  tsutsui 	return -1;
    148  1.1  tsutsui }
    149  1.1  tsutsui 
    150  1.1  tsutsui int
    151  1.1  tsutsui iee_sbdio_reset(struct iee_softc *sc)
    152  1.1  tsutsui {
    153  1.1  tsutsui #define	IEE_ISCP_BUSSY 0x1
    154  1.1  tsutsui 	int n, retry = 8;
    155  1.7  tsutsui 	uint32_t cmd, ack;
    156  1.1  tsutsui 
    157  1.1  tsutsui 	/* Make sure the busy byte is set and the cache is flushed. */
    158  1.1  tsutsui 	SC_ISCP->iscp_bussy = IEE_ISCP_BUSSY;
    159  1.7  tsutsui 	bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map,
    160  1.7  tsutsui 	    IEE_SCP_OFF, IEE_SCP_SZ + IEE_ISCP_SZ + IEE_SCB_SZ,
    161  1.7  tsutsui 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    162  1.1  tsutsui 
    163  1.1  tsutsui 	/* Setup the PORT Command with pointer to SCP. */
    164  1.1  tsutsui 	cmd = IEE_PORT_SCP | IEE_PHYS_SHMEM(IEE_SCP_OFF);
    165  1.1  tsutsui 
    166  1.1  tsutsui 	/* Initiate a Hardware reset. */
    167  1.5  tsutsui 	printf("%s: reseting chip... ", device_xname(sc->sc_dev));
    168  1.1  tsutsui 	iee_sbdio_chip_reset(sc);
    169  1.1  tsutsui 
    170  1.1  tsutsui 	/* Set SCP address to CU */
    171  1.1  tsutsui 	iee_sbdio_set_scp(sc, cmd);
    172  1.1  tsutsui 
    173  1.1  tsutsui 	/* Wait for the chip to initialize and read SCP and ISCP. */
    174  1.1  tsutsui 	for (n = 0 ; n < retry; n++) {
    175  1.1  tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_ISCP_OFF,
    176  1.7  tsutsui 		    IEE_ISCP_SZ, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    177  1.7  tsutsui 		ack = SC_ISCP->iscp_bussy;
    178  1.7  tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_ISCP_OFF,
    179  1.1  tsutsui 		    IEE_ISCP_SZ, BUS_DMASYNC_PREREAD);
    180  1.7  tsutsui 		if (ack != IEE_ISCP_BUSSY) {
    181  1.1  tsutsui 			break;
    182  1.1  tsutsui 		}
    183  1.1  tsutsui 		delay(100);
    184  1.1  tsutsui 	}
    185  1.1  tsutsui 
    186  1.1  tsutsui 	if (n < retry) {
    187  1.1  tsutsui 		/* ACK interrupts we may have caused */
    188  1.1  tsutsui 		sc->sc_iee_cmd(sc, IEE_SCB_ACK);
    189  1.1  tsutsui 		printf("done.\n");
    190  1.1  tsutsui 
    191  1.1  tsutsui 		return 0;
    192  1.1  tsutsui 	}
    193  1.1  tsutsui 	printf("time out.\n");
    194  1.1  tsutsui 
    195  1.1  tsutsui 	return -1;
    196  1.1  tsutsui }
    197  1.1  tsutsui 
    198  1.1  tsutsui static void
    199  1.1  tsutsui iee_sbdio_channel_attention(void *cookie)
    200  1.1  tsutsui {
    201  1.1  tsutsui 	struct iee_sbdio_softc *sc = cookie;
    202  1.1  tsutsui 	uint32_t dummy;
    203  1.1  tsutsui 
    204  1.1  tsutsui 	/* Issue a Channel Attention */
    205  1.1  tsutsui 	dummy = *sc->sc_port;
    206  1.1  tsutsui 	dummy = *sc->sc_port;
    207  1.1  tsutsui }
    208  1.1  tsutsui 
    209  1.1  tsutsui static void
    210  1.1  tsutsui iee_sbdio_chip_reset(void *cookie)
    211  1.1  tsutsui {
    212  1.1  tsutsui 	struct iee_sbdio_softc *sc = cookie;
    213  1.1  tsutsui 
    214  1.1  tsutsui 	*sc->sc_port = 0;
    215  1.1  tsutsui 	*sc->sc_port = 0;
    216  1.1  tsutsui 	delay(10);
    217  1.1  tsutsui }
    218  1.1  tsutsui 
    219  1.1  tsutsui static void
    220  1.1  tsutsui iee_sbdio_set_scp(void *cookie, uint32_t cmd)
    221  1.1  tsutsui {
    222  1.1  tsutsui 	struct iee_sbdio_softc *sc = cookie;
    223  1.1  tsutsui 	cmd = (cmd << 16) | (cmd >> 16);
    224  1.1  tsutsui 
    225  1.1  tsutsui 	*sc->sc_port = cmd;
    226  1.1  tsutsui 	*sc->sc_port = cmd;
    227  1.1  tsutsui 
    228  1.1  tsutsui 	/* Issue a Channel Attention to read SCP */
    229  1.1  tsutsui 	iee_sbdio_channel_attention(cookie);
    230  1.1  tsutsui }
    231