Home | History | Annotate | Line # | Download | only in sbd
if_iee_sbdio.c revision 1.1
      1  1.1  tsutsui /*	$NetBSD: if_iee_sbdio.c,v 1.1 2005/12/29 15:20:09 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.1  tsutsui __KERNEL_RCSID(0, "$NetBSD: if_iee_sbdio.c,v 1.1 2005/12/29 15:20:09 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.1  tsutsui int iee_sbdio_match(struct device *, struct cfdata *, void *);
     56  1.1  tsutsui void iee_sbdio_attach(struct device *, struct device *, 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.1  tsutsui 	/* CPU <-> i82589 interface */
     67  1.1  tsutsui 	volatile uint32_t *sc_port;
     68  1.1  tsutsui };
     69  1.1  tsutsui 
     70  1.1  tsutsui CFATTACH_DECL(iee_sbdio, sizeof(struct iee_sbdio_softc),
     71  1.1  tsutsui     iee_sbdio_match, iee_sbdio_attach, 0, 0);
     72  1.1  tsutsui 
     73  1.1  tsutsui int
     74  1.1  tsutsui iee_sbdio_match(struct device *parent, struct cfdata *match, void *aux)
     75  1.1  tsutsui {
     76  1.1  tsutsui 	struct sbdio_attach_args *sa = aux;
     77  1.1  tsutsui 
     78  1.1  tsutsui 	return strcmp(sa->sa_name, "iee") ? 0 : 1;
     79  1.1  tsutsui }
     80  1.1  tsutsui 
     81  1.1  tsutsui void
     82  1.1  tsutsui iee_sbdio_attach(struct device *parent, struct device *self, void *aux)
     83  1.1  tsutsui {
     84  1.1  tsutsui 	struct sbdio_attach_args *sa = aux;
     85  1.1  tsutsui 	struct iee_sbdio_softc *sc_ssc = (void *)self;
     86  1.1  tsutsui 	struct iee_softc *sc = &sc_ssc->sc_iee;
     87  1.1  tsutsui 	uint8_t eaddr[ETHER_ADDR_LEN];
     88  1.1  tsutsui 	int media[2];
     89  1.1  tsutsui 	int rsegs;
     90  1.1  tsutsui 
     91  1.1  tsutsui 	printf(" at %p irq %d ", (void *)sa->sa_addr1, sa->sa_irq);
     92  1.1  tsutsui 
     93  1.1  tsutsui 	sc_ssc->sc_port = (volatile uint32_t *)
     94  1.1  tsutsui 	    MIPS_PHYS_TO_KSEG1(sa->sa_addr1);
     95  1.1  tsutsui 	sc->sc_type = I82596_CA;
     96  1.1  tsutsui 	sc->sc_flags = IEE_NEED_SWAP;
     97  1.1  tsutsui 
     98  1.1  tsutsui 
     99  1.1  tsutsui 	/* bus_dma round the dma size to page size. */
    100  1.1  tsutsui 	sc->sc_cl_align = 1;
    101  1.1  tsutsui 
    102  1.1  tsutsui 	sc->sc_dmat = sa->sa_dmat;
    103  1.1  tsutsui 
    104  1.1  tsutsui 	if (bus_dmamem_alloc(sc->sc_dmat, IEE_SHMEM_MAX, PAGE_SIZE, 0,
    105  1.1  tsutsui 		&sc->sc_dma_segs, 1, &rsegs, BUS_DMA_NOWAIT) != 0) {
    106  1.1  tsutsui 		aprint_normal(": iee_sbdio_attach: can't allocate %d bytes of "
    107  1.1  tsutsui 		    "DMA memory\n", (int)IEE_SHMEM_MAX);
    108  1.1  tsutsui 		return;
    109  1.1  tsutsui 	}
    110  1.1  tsutsui 
    111  1.1  tsutsui 	if (bus_dmamem_map(sc->sc_dmat, &sc->sc_dma_segs, rsegs, IEE_SHMEM_MAX,
    112  1.1  tsutsui 		&sc->sc_shmem_addr, BUS_DMA_NOWAIT) != 0) {
    113  1.1  tsutsui 		aprint_normal(": iee_sbdio_attach: can't map DMA memory\n");
    114  1.1  tsutsui 		bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_segs, rsegs);
    115  1.1  tsutsui 		return;
    116  1.1  tsutsui 	}
    117  1.1  tsutsui 
    118  1.1  tsutsui 	if (bus_dmamap_create(sc->sc_dmat, IEE_SHMEM_MAX, rsegs,
    119  1.1  tsutsui 		IEE_SHMEM_MAX, 0, BUS_DMA_NOWAIT, &sc->sc_shmem_map) != 0) {
    120  1.1  tsutsui 		aprint_normal(": iee_sbdio_attach: can't create DMA map\n");
    121  1.1  tsutsui 		bus_dmamem_unmap(sc->sc_dmat, sc->sc_shmem_addr, IEE_SHMEM_MAX);
    122  1.1  tsutsui 		bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_segs, rsegs);
    123  1.1  tsutsui 		return;
    124  1.1  tsutsui 	}
    125  1.1  tsutsui 
    126  1.1  tsutsui 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_shmem_map, sc->sc_shmem_addr,
    127  1.1  tsutsui 		IEE_SHMEM_MAX, NULL, BUS_DMA_NOWAIT) != 0) {
    128  1.1  tsutsui 		aprint_normal(": iee_sbdio_attach: can't load DMA map\n");
    129  1.1  tsutsui 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_shmem_map);
    130  1.1  tsutsui 		bus_dmamem_unmap(sc->sc_dmat, sc->sc_shmem_addr, IEE_SHMEM_MAX);
    131  1.1  tsutsui 		bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_segs, rsegs);
    132  1.1  tsutsui 		return;
    133  1.1  tsutsui 	}
    134  1.1  tsutsui 	memset(sc->sc_shmem_addr, 0, IEE_SHMEM_MAX);
    135  1.1  tsutsui 
    136  1.1  tsutsui 	mips_dcache_wbinv_range((vaddr_t)sc->sc_shmem_addr, IEE_SHMEM_MAX);
    137  1.1  tsutsui 	sc->sc_shmem_addr = (caddr_t)((vaddr_t)sc->sc_shmem_addr |
    138  1.1  tsutsui 	    MIPS_KSEG1_START);
    139  1.1  tsutsui 
    140  1.1  tsutsui 	/* Setup SYSBUS byte. TR2 specific? -uch */
    141  1.1  tsutsui 	SC_SCP->scp_sysbus = IEE_SYSBUS_BE | IEE_SYSBUS_INT |
    142  1.1  tsutsui 	    IEE_SYSBUS_LIEAR | IEE_SYSBUS_STD;
    143  1.1  tsutsui 
    144  1.1  tsutsui 	sc->sc_iee_reset = iee_sbdio_reset;
    145  1.1  tsutsui 	sc->sc_iee_cmd = iee_sbdio_cmd;
    146  1.1  tsutsui 	sc->sc_mediachange = NULL;
    147  1.1  tsutsui 	sc->sc_mediastatus = NULL;
    148  1.1  tsutsui 
    149  1.1  tsutsui 	media[0] = IFM_ETHER | IFM_MANUAL;
    150  1.1  tsutsui 	media[1] = IFM_ETHER | IFM_10_5;
    151  1.1  tsutsui 
    152  1.1  tsutsui 	intr_establish(sa->sa_irq, iee_intr, self);
    153  1.1  tsutsui 	(*platform.ether_addr)(eaddr);
    154  1.1  tsutsui 	iee_attach(sc, eaddr, media, 2, IFM_ETHER | IFM_10_5);
    155  1.1  tsutsui }
    156  1.1  tsutsui 
    157  1.1  tsutsui int
    158  1.1  tsutsui iee_sbdio_cmd(struct iee_softc *sc, uint32_t cmd)
    159  1.1  tsutsui {
    160  1.1  tsutsui 	int retry = 8;
    161  1.1  tsutsui 	int n;
    162  1.1  tsutsui 
    163  1.1  tsutsui 	SC_SCB->scb_cmd = cmd;
    164  1.1  tsutsui 	bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCB_OFF, IEE_SCB_SZ,
    165  1.1  tsutsui 	    BUS_DMASYNC_PREWRITE);
    166  1.1  tsutsui 
    167  1.1  tsutsui 	iee_sbdio_channel_attention(sc);
    168  1.1  tsutsui 
    169  1.1  tsutsui 	/* Wait for the cmd to finish */
    170  1.1  tsutsui 	for (n = 0 ; n < retry; n++) {
    171  1.1  tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCB_OFF,
    172  1.1  tsutsui 		    IEE_SCB_SZ, BUS_DMASYNC_PREREAD);
    173  1.1  tsutsui 
    174  1.1  tsutsui 		if (SC_SCB->scb_cmd == 0)
    175  1.1  tsutsui 			break;
    176  1.1  tsutsui 		delay(1);
    177  1.1  tsutsui 	}
    178  1.1  tsutsui 	bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCB_OFF, IEE_SCB_SZ,
    179  1.1  tsutsui 	    BUS_DMASYNC_PREREAD);
    180  1.1  tsutsui 	if (n < retry)
    181  1.1  tsutsui 		return 0;
    182  1.1  tsutsui 
    183  1.1  tsutsui 	printf("%s: command timeout. retry=%d\n", sc->sc_dev.dv_xname, retry);
    184  1.1  tsutsui 
    185  1.1  tsutsui 	return -1;
    186  1.1  tsutsui }
    187  1.1  tsutsui 
    188  1.1  tsutsui int
    189  1.1  tsutsui iee_sbdio_reset(struct iee_softc *sc)
    190  1.1  tsutsui {
    191  1.1  tsutsui #define	IEE_ISCP_BUSSY 0x1
    192  1.1  tsutsui 	int n, retry = 8;
    193  1.1  tsutsui 	uint32_t cmd;
    194  1.1  tsutsui 
    195  1.1  tsutsui 	/* Make sure the busy byte is set and the cache is flushed. */
    196  1.1  tsutsui 	SC_ISCP->iscp_bussy = IEE_ISCP_BUSSY;
    197  1.1  tsutsui 	bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCP_OFF, IEE_SCP_SZ
    198  1.1  tsutsui 	    + IEE_ISCP_SZ + IEE_SCB_SZ, BUS_DMASYNC_PREWRITE);
    199  1.1  tsutsui 
    200  1.1  tsutsui 	/* Setup the PORT Command with pointer to SCP. */
    201  1.1  tsutsui 	cmd = IEE_PORT_SCP | IEE_PHYS_SHMEM(IEE_SCP_OFF);
    202  1.1  tsutsui 
    203  1.1  tsutsui 	/* Initiate a Hardware reset. */
    204  1.1  tsutsui 	printf("%s: reseting chip... ", sc->sc_dev.dv_xname);
    205  1.1  tsutsui 	iee_sbdio_chip_reset(sc);
    206  1.1  tsutsui 
    207  1.1  tsutsui 	/* Set SCP address to CU */
    208  1.1  tsutsui 	iee_sbdio_set_scp(sc, cmd);
    209  1.1  tsutsui 
    210  1.1  tsutsui 	/* Wait for the chip to initialize and read SCP and ISCP. */
    211  1.1  tsutsui 	for (n = 0 ; n < retry; n++) {
    212  1.1  tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_ISCP_OFF,
    213  1.1  tsutsui 		    IEE_ISCP_SZ, BUS_DMASYNC_PREREAD);
    214  1.1  tsutsui 		if (SC_ISCP->iscp_bussy != IEE_ISCP_BUSSY) {
    215  1.1  tsutsui 			break;
    216  1.1  tsutsui 		}
    217  1.1  tsutsui 		delay(100);
    218  1.1  tsutsui 	}
    219  1.1  tsutsui 	bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCB_OFF, IEE_SCB_SZ,
    220  1.1  tsutsui 	    BUS_DMASYNC_PREREAD);
    221  1.1  tsutsui 
    222  1.1  tsutsui 	if (n < retry) {
    223  1.1  tsutsui 		/* ACK interrupts we may have caused */
    224  1.1  tsutsui 		sc->sc_iee_cmd(sc, IEE_SCB_ACK);
    225  1.1  tsutsui 		printf("done.\n");
    226  1.1  tsutsui 
    227  1.1  tsutsui 		return 0;
    228  1.1  tsutsui 	}
    229  1.1  tsutsui 	printf("time out.\n");
    230  1.1  tsutsui 
    231  1.1  tsutsui 	return -1;
    232  1.1  tsutsui }
    233  1.1  tsutsui 
    234  1.1  tsutsui static void
    235  1.1  tsutsui iee_sbdio_channel_attention(void *cookie)
    236  1.1  tsutsui {
    237  1.1  tsutsui 	struct iee_sbdio_softc *sc = cookie;
    238  1.1  tsutsui 	uint32_t dummy;
    239  1.1  tsutsui 
    240  1.1  tsutsui 	/* Issue a Channel Attention */
    241  1.1  tsutsui 	dummy = *sc->sc_port;
    242  1.1  tsutsui 	dummy = *sc->sc_port;
    243  1.1  tsutsui }
    244  1.1  tsutsui 
    245  1.1  tsutsui static void
    246  1.1  tsutsui iee_sbdio_chip_reset(void *cookie)
    247  1.1  tsutsui {
    248  1.1  tsutsui 	struct iee_sbdio_softc *sc = cookie;
    249  1.1  tsutsui 
    250  1.1  tsutsui 	*sc->sc_port = 0;
    251  1.1  tsutsui 	*sc->sc_port = 0;
    252  1.1  tsutsui 	delay(10);
    253  1.1  tsutsui }
    254  1.1  tsutsui 
    255  1.1  tsutsui static void
    256  1.1  tsutsui iee_sbdio_set_scp(void *cookie, uint32_t cmd)
    257  1.1  tsutsui {
    258  1.1  tsutsui 	struct iee_sbdio_softc *sc = cookie;
    259  1.1  tsutsui 	cmd = (cmd << 16) | (cmd >> 16);
    260  1.1  tsutsui 
    261  1.1  tsutsui 	*sc->sc_port = cmd;
    262  1.1  tsutsui 	*sc->sc_port = cmd;
    263  1.1  tsutsui 
    264  1.1  tsutsui 	/* Issue a Channel Attention to read SCP */
    265  1.1  tsutsui 	iee_sbdio_channel_attention(cookie);
    266  1.1  tsutsui }
    267