Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / ews4800mips / sbd / if_iee_sbdio.c
blob880b87e270bbc893a67136b56e533efed7ddaf9f
1 /* $NetBSD: if_iee_sbdio.c,v 1.9 2009/05/10 04:26:19 tsutsui Exp $ */
3 /*
4 * Copyright (c) 2003 Jochen Kunz.
5 * All rights reserved.
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. The name of Jochen Kunz may not be used to endorse or promote
16 * products derived from this software without specific prior
17 * written permission.
19 * THIS SOFTWARE IS PROVIDED BY JOCHEN KUNZ
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JOCHEN KUNZ
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_iee_sbdio.c,v 1.9 2009/05/10 04:26:19 tsutsui Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 #include <net/if_ether.h>
43 #include <sys/socket.h>
44 #include <sys/mbuf.h>
46 #include <mips/cache.h>
47 #include <machine/bus.h>
48 #include <machine/intr.h>
49 #include <machine/sbdvar.h> /* for ether_addr() */
50 #include <machine/sbdiovar.h>
52 #include <dev/ic/i82596reg.h>
53 #include <dev/ic/i82596var.h>
55 int iee_sbdio_match(device_t, cfdata_t, void *);
56 void iee_sbdio_attach(device_t, device_t, void *);
57 int iee_sbdio_cmd(struct iee_softc *, uint32_t);
58 int iee_sbdio_reset(struct iee_softc *);
60 static void iee_sbdio_channel_attention(void *);
61 static void iee_sbdio_chip_reset(void *);
62 static void iee_sbdio_set_scp(void *, uint32_t);
64 struct iee_sbdio_softc {
65 struct iee_softc sc_iee;
66 volatile uint32_t *sc_port; /* CPU <-> i82596 interface */
69 CFATTACH_DECL_NEW(iee_sbdio, sizeof(struct iee_sbdio_softc),
70 iee_sbdio_match, iee_sbdio_attach, NULL, NULL);
72 int
73 iee_sbdio_match(device_t parent, cfdata_t cf, void *aux)
75 struct sbdio_attach_args *sa = aux;
77 return strcmp(sa->sa_name, "iee") ? 0 : 1;
80 void
81 iee_sbdio_attach(device_t parent, device_t self, void *aux)
83 struct iee_sbdio_softc *sc_ssc = device_private(self);
84 struct iee_softc *sc = &sc_ssc->sc_iee;
85 struct sbdio_attach_args *sa = aux;
86 uint8_t eaddr[ETHER_ADDR_LEN];
87 int media[2];
89 sc->sc_dev = self;
90 sc_ssc->sc_port =
91 (volatile uint32_t *)MIPS_PHYS_TO_KSEG1(sa->sa_addr1);
92 sc->sc_type = I82596_CA;
93 sc->sc_flags = IEE_NEED_SWAP;
95 /* bus_dma round the dma size to page size. */
96 sc->sc_cl_align = 1;
98 sc->sc_dmat = sa->sa_dmat;
100 /* Setup SYSBUS byte. TR2 specific? -uch */
101 sc->sc_sysbus = IEE_SYSBUS_BE | IEE_SYSBUS_INT |
102 IEE_SYSBUS_LIEAR | IEE_SYSBUS_STD;
104 sc->sc_iee_reset = iee_sbdio_reset;
105 sc->sc_iee_cmd = iee_sbdio_cmd;
106 sc->sc_mediachange = NULL;
107 sc->sc_mediastatus = NULL;
109 media[0] = IFM_ETHER | IFM_MANUAL;
110 media[1] = IFM_ETHER | IFM_10_5;
112 intr_establish(sa->sa_irq, iee_intr, sc);
113 (*platform.ether_addr)(eaddr);
114 iee_attach(sc, eaddr, media, 2, IFM_ETHER | IFM_10_5);
118 iee_sbdio_cmd(struct iee_softc *sc, uint32_t cmd)
120 int retry = 8;
121 int n;
122 uint32_t ack;
124 SC_SCB(sc)->scb_cmd = cmd;
125 IEE_SCBSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
127 iee_sbdio_channel_attention(sc);
129 /* Wait for the cmd to finish */
130 for (n = 0 ; n < retry; n++) {
131 IEE_SCBSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
132 ack = SC_SCB(sc)->scb_cmd;
133 IEE_SCBSYNC(sc, BUS_DMASYNC_PREREAD);
134 if (ack == 0)
135 break;
136 delay(1);
138 if (n < retry)
139 return 0;
141 printf("%s: command timeout. retry=%d\n",
142 device_xname(sc->sc_dev), retry);
144 return -1;
148 iee_sbdio_reset(struct iee_softc *sc)
150 #define IEE_ISCP_BUSY 0x1
151 int n, retry = 8;
152 uint32_t cmd, ack;
154 /* Make sure the busy byte is set and the cache is flushed. */
155 SC_ISCP(sc)->iscp_busy = IEE_ISCP_BUSY;
156 IEE_ISCPSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
158 /* Setup the PORT Command with pointer to SCP. */
159 cmd = IEE_PORT_SCP | IEE_PHYS_SHMEM(sc->sc_scp_off);
161 /* Initiate a Hardware reset. */
162 printf("%s: reseting chip... ", device_xname(sc->sc_dev));
163 iee_sbdio_chip_reset(sc);
165 /* Set SCP address to CU */
166 iee_sbdio_set_scp(sc, cmd);
168 /* Wait for the chip to initialize and read SCP and ISCP. */
169 for (n = 0 ; n < retry; n++) {
170 IEE_ISCPSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
171 ack = SC_ISCP(sc)->iscp_busy;
172 IEE_ISCPSYNC(sc, BUS_DMASYNC_PREREAD);
173 if (ack != IEE_ISCP_BUSY) {
174 break;
176 delay(100);
179 if (n < retry) {
180 /* ACK interrupts we may have caused */
181 sc->sc_iee_cmd(sc, IEE_SCB_ACK);
182 printf("done.\n");
184 return 0;
186 printf("time out.\n");
188 return -1;
191 static void
192 iee_sbdio_channel_attention(void *cookie)
194 struct iee_sbdio_softc *sc = cookie;
195 uint32_t dummy;
197 /* Issue a Channel Attention */
198 dummy = *sc->sc_port;
199 dummy = *sc->sc_port;
202 static void
203 iee_sbdio_chip_reset(void *cookie)
205 struct iee_sbdio_softc *sc = cookie;
207 *sc->sc_port = 0;
208 *sc->sc_port = 0;
209 delay(10);
212 static void
213 iee_sbdio_set_scp(void *cookie, uint32_t cmd)
215 struct iee_sbdio_softc *sc = cookie;
216 cmd = (cmd << 16) | (cmd >> 16);
218 *sc->sc_port = cmd;
219 *sc->sc_port = cmd;
221 /* Issue a Channel Attention to read SCP */
222 iee_sbdio_channel_attention(cookie);