1 /* $NetBSD: if_iee_sbdio.c,v 1.13 2020/07/22 01:24:40 msaitoh Exp $ */
2
3 /*
4 * Copyright (c) 2003 Jochen Kunz.
5 * All rights reserved.
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. 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.
18 *
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.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_iee_sbdio.c,v 1.13 2020/07/22 01:24:40 msaitoh Exp $");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/intr.h>
39 #include <sys/mbuf.h>
40 #include <sys/socket.h>
41 #include <sys/systm.h>
42
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_media.h>
46 #include <net/if_ether.h>
47
48 #include <mips/cache.h>
49 #include <mips/locore.h>
50 #include <machine/sbdvar.h> /* for ether_addr() */
51 #include <machine/sbdiovar.h>
52
53 #include <dev/ic/i82596reg.h>
54 #include <dev/ic/i82596var.h>
55
56 int iee_sbdio_match(device_t, cfdata_t, void *);
57 void iee_sbdio_attach(device_t, device_t, void *);
58 int iee_sbdio_cmd(struct iee_softc *, uint32_t);
59 int iee_sbdio_reset(struct iee_softc *);
60
61 static void iee_sbdio_channel_attention(void *);
62 static void iee_sbdio_chip_reset(void *);
63 static void iee_sbdio_set_scp(void *, uint32_t);
64
65 struct iee_sbdio_softc {
66 struct iee_softc sc_iee;
67 volatile uint32_t *sc_port; /* CPU <-> i82596 interface */
68 };
69
70 CFATTACH_DECL_NEW(iee_sbdio, sizeof(struct iee_sbdio_softc),
71 iee_sbdio_match, iee_sbdio_attach, NULL, NULL);
72
73 int
iee_sbdio_match(device_t parent,cfdata_t cf,void * aux)74 iee_sbdio_match(device_t parent, cfdata_t cf, void *aux)
75 {
76 struct sbdio_attach_args *sa = aux;
77
78 return strcmp(sa->sa_name, "iee") ? 0 : 1;
79 }
80
81 void
iee_sbdio_attach(device_t parent,device_t self,void * aux)82 iee_sbdio_attach(device_t parent, device_t self, void *aux)
83 {
84 struct iee_sbdio_softc *sc_ssc = device_private(self);
85 struct iee_softc *sc = &sc_ssc->sc_iee;
86 struct sbdio_attach_args *sa = aux;
87 uint8_t eaddr[ETHER_ADDR_LEN];
88 int media[2];
89
90 sc->sc_dev = self;
91 sc_ssc->sc_port =
92 (volatile uint32_t *)MIPS_PHYS_TO_KSEG1(sa->sa_addr1);
93 sc->sc_type = I82596_CA;
94 sc->sc_flags = IEE_NEED_SWAP;
95
96 /* bus_dma round the dma size to page size. */
97 sc->sc_cl_align = 1;
98
99 sc->sc_dmat = sa->sa_dmat;
100
101 /* Setup SYSBUS byte. TR2 specific? -uch */
102 sc->sc_sysbus = IEE_SYSBUS_BE | IEE_SYSBUS_INT |
103 IEE_SYSBUS_LIEAR | IEE_SYSBUS_STD;
104
105 sc->sc_iee_reset = iee_sbdio_reset;
106 sc->sc_iee_cmd = iee_sbdio_cmd;
107 sc->sc_mediachange = NULL;
108 sc->sc_mediastatus = NULL;
109
110 media[0] = IFM_ETHER | IFM_MANUAL;
111 media[1] = IFM_ETHER | IFM_10_5;
112
113 intr_establish(sa->sa_irq, iee_intr, sc);
114 (*platform.ether_addr)(eaddr);
115 iee_attach(sc, eaddr, media, 2, IFM_ETHER | IFM_10_5);
116 }
117
118 int
iee_sbdio_cmd(struct iee_softc * sc,uint32_t cmd)119 iee_sbdio_cmd(struct iee_softc *sc, uint32_t cmd)
120 {
121 int retry = 8;
122 int n;
123 uint32_t ack;
124
125 SC_SCB(sc)->scb_cmd = cmd;
126 IEE_SCBSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
127
128 iee_sbdio_channel_attention(sc);
129
130 /* Wait for the cmd to finish */
131 for (n = 0 ; n < retry; n++) {
132 IEE_SCBSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
133 ack = SC_SCB(sc)->scb_cmd;
134 IEE_SCBSYNC(sc, BUS_DMASYNC_PREREAD);
135 if (ack == 0)
136 break;
137 delay(1);
138 }
139 if (n < retry)
140 return 0;
141
142 printf("%s: command timeout. retry=%d\n",
143 device_xname(sc->sc_dev), retry);
144
145 return -1;
146 }
147
148 int
iee_sbdio_reset(struct iee_softc * sc)149 iee_sbdio_reset(struct iee_softc *sc)
150 {
151 #define IEE_ISCP_BUSY 0x1
152 int n, retry = 8;
153 uint32_t cmd, ack;
154
155 /* Make sure the busy byte is set and the cache is flushed. */
156 SC_ISCP(sc)->iscp_busy = IEE_ISCP_BUSY;
157 IEE_ISCPSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
158
159 /* Setup the PORT Command with pointer to SCP. */
160 cmd = IEE_PORT_SCP | IEE_PHYS_SHMEM(sc->sc_scp_off);
161
162 /* Initiate a Hardware reset. */
163 printf("%s: resetting chip... ", device_xname(sc->sc_dev));
164 iee_sbdio_chip_reset(sc);
165
166 /* Set SCP address to CU */
167 iee_sbdio_set_scp(sc, cmd);
168
169 /* Wait for the chip to initialize and read SCP and ISCP. */
170 for (n = 0 ; n < retry; n++) {
171 IEE_ISCPSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
172 ack = SC_ISCP(sc)->iscp_busy;
173 IEE_ISCPSYNC(sc, BUS_DMASYNC_PREREAD);
174 if (ack != IEE_ISCP_BUSY) {
175 break;
176 }
177 delay(100);
178 }
179
180 if (n < retry) {
181 /* ACK interrupts we may have caused */
182 sc->sc_iee_cmd(sc, IEE_SCB_ACK);
183 printf("done.\n");
184
185 return 0;
186 }
187 printf("time out.\n");
188
189 return -1;
190 }
191
192 static void
iee_sbdio_channel_attention(void * cookie)193 iee_sbdio_channel_attention(void *cookie)
194 {
195 struct iee_sbdio_softc *sc = cookie;
196 uint32_t dummy;
197
198 /* Issue a Channel Attention */
199 dummy = *sc->sc_port;
200 dummy = *sc->sc_port;
201 __USE(dummy);
202 }
203
204 static void
iee_sbdio_chip_reset(void * cookie)205 iee_sbdio_chip_reset(void *cookie)
206 {
207 struct iee_sbdio_softc *sc = cookie;
208
209 *sc->sc_port = 0;
210 *sc->sc_port = 0;
211 delay(10);
212 }
213
214 static void
iee_sbdio_set_scp(void * cookie,uint32_t cmd)215 iee_sbdio_set_scp(void *cookie, uint32_t cmd)
216 {
217 struct iee_sbdio_softc *sc = cookie;
218 cmd = (cmd << 16) | (cmd >> 16);
219
220 *sc->sc_port = cmd;
221 *sc->sc_port = cmd;
222
223 /* Issue a Channel Attention to read SCP */
224 iee_sbdio_channel_attention(cookie);
225 }
226