if_iee_sbdio.c revision 1.6 1 /* $NetBSD: if_iee_sbdio.c,v 1.6 2009/05/05 16:38:41 tsutsui 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.6 2009/05/05 16:38:41 tsutsui Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38
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>
45
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>
51
52 #include <dev/ic/i82596reg.h>
53 #include <dev/ic/i82596var.h>
54
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 *);
59
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);
63
64 struct iee_sbdio_softc {
65 struct iee_softc sc_iee;
66 volatile uint32_t *sc_port; /* CPU <-> i82596 interface */
67 };
68
69 CFATTACH_DECL_NEW(iee_sbdio, sizeof(struct iee_sbdio_softc),
70 iee_sbdio_match, iee_sbdio_attach, NULL, NULL);
71
72 int
73 iee_sbdio_match(device_t parent, cfdata_t cf, void *aux)
74 {
75 struct sbdio_attach_args *sa = aux;
76
77 return strcmp(sa->sa_name, "iee") ? 0 : 1;
78 }
79
80 void
81 iee_sbdio_attach(device_t parent, device_t self, void *aux)
82 {
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];
88 int rsegs;
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 if (bus_dmamem_alloc(sc->sc_dmat, IEE_SHMEM_MAX, PAGE_SIZE, 0,
102 &sc->sc_dma_segs, 1, &rsegs, BUS_DMA_NOWAIT) != 0) {
103 aprint_error(": can't allocate %d bytes of DMA memory\n",
104 (int)IEE_SHMEM_MAX);
105 return;
106 }
107
108 if (bus_dmamem_map(sc->sc_dmat, &sc->sc_dma_segs, rsegs, IEE_SHMEM_MAX,
109 (void **)sc->sc_shmem_addr, BUS_DMA_NOWAIT) != 0) {
110 aprint_error(": can't map DMA memory\n");
111 bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_segs, rsegs);
112 return;
113 }
114
115 if (bus_dmamap_create(sc->sc_dmat, IEE_SHMEM_MAX, rsegs,
116 IEE_SHMEM_MAX, 0, BUS_DMA_NOWAIT, &sc->sc_shmem_map) != 0) {
117 aprint_error(": can't create DMA map\n");
118 bus_dmamem_unmap(sc->sc_dmat, sc->sc_shmem_addr, IEE_SHMEM_MAX);
119 bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_segs, rsegs);
120 return;
121 }
122
123 if (bus_dmamap_load(sc->sc_dmat, sc->sc_shmem_map, sc->sc_shmem_addr,
124 IEE_SHMEM_MAX, NULL, BUS_DMA_NOWAIT) != 0) {
125 aprint_error(": can't load DMA map\n");
126 bus_dmamap_destroy(sc->sc_dmat, sc->sc_shmem_map);
127 bus_dmamem_unmap(sc->sc_dmat, sc->sc_shmem_addr, IEE_SHMEM_MAX);
128 bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_segs, rsegs);
129 return;
130 }
131 memset(sc->sc_shmem_addr, 0, IEE_SHMEM_MAX);
132
133 mips_dcache_wbinv_range((vaddr_t)sc->sc_shmem_addr, IEE_SHMEM_MAX);
134 sc->sc_shmem_addr = (void *)((vaddr_t)sc->sc_shmem_addr |
135 MIPS_KSEG1_START);
136
137 /* Setup SYSBUS byte. TR2 specific? -uch */
138 SC_SCP->scp_sysbus = IEE_SYSBUS_BE | IEE_SYSBUS_INT |
139 IEE_SYSBUS_LIEAR | IEE_SYSBUS_STD;
140
141 sc->sc_iee_reset = iee_sbdio_reset;
142 sc->sc_iee_cmd = iee_sbdio_cmd;
143 sc->sc_mediachange = NULL;
144 sc->sc_mediastatus = NULL;
145
146 media[0] = IFM_ETHER | IFM_MANUAL;
147 media[1] = IFM_ETHER | IFM_10_5;
148
149 intr_establish(sa->sa_irq, iee_intr, sc);
150 (*platform.ether_addr)(eaddr);
151 iee_attach(sc, eaddr, media, 2, IFM_ETHER | IFM_10_5);
152 }
153
154 int
155 iee_sbdio_cmd(struct iee_softc *sc, uint32_t cmd)
156 {
157 int retry = 8;
158 int n;
159
160 SC_SCB->scb_cmd = cmd;
161 bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCB_OFF, IEE_SCB_SZ,
162 BUS_DMASYNC_PREWRITE);
163
164 iee_sbdio_channel_attention(sc);
165
166 /* Wait for the cmd to finish */
167 for (n = 0 ; n < retry; n++) {
168 bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCB_OFF,
169 IEE_SCB_SZ, BUS_DMASYNC_PREREAD);
170
171 if (SC_SCB->scb_cmd == 0)
172 break;
173 delay(1);
174 }
175 bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCB_OFF, IEE_SCB_SZ,
176 BUS_DMASYNC_PREREAD);
177 if (n < retry)
178 return 0;
179
180 printf("%s: command timeout. retry=%d\n",
181 device_xname(sc->sc_dev), retry);
182
183 return -1;
184 }
185
186 int
187 iee_sbdio_reset(struct iee_softc *sc)
188 {
189 #define IEE_ISCP_BUSSY 0x1
190 int n, retry = 8;
191 uint32_t cmd;
192
193 /* Make sure the busy byte is set and the cache is flushed. */
194 SC_ISCP->iscp_bussy = IEE_ISCP_BUSSY;
195 bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCP_OFF, IEE_SCP_SZ
196 + IEE_ISCP_SZ + IEE_SCB_SZ, BUS_DMASYNC_PREWRITE);
197
198 /* Setup the PORT Command with pointer to SCP. */
199 cmd = IEE_PORT_SCP | IEE_PHYS_SHMEM(IEE_SCP_OFF);
200
201 /* Initiate a Hardware reset. */
202 printf("%s: reseting chip... ", device_xname(sc->sc_dev));
203 iee_sbdio_chip_reset(sc);
204
205 /* Set SCP address to CU */
206 iee_sbdio_set_scp(sc, cmd);
207
208 /* Wait for the chip to initialize and read SCP and ISCP. */
209 for (n = 0 ; n < retry; n++) {
210 bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_ISCP_OFF,
211 IEE_ISCP_SZ, BUS_DMASYNC_PREREAD);
212 if (SC_ISCP->iscp_bussy != IEE_ISCP_BUSSY) {
213 break;
214 }
215 delay(100);
216 }
217 bus_dmamap_sync(sc->sc_dmat, sc->sc_shmem_map, IEE_SCB_OFF, IEE_SCB_SZ,
218 BUS_DMASYNC_PREREAD);
219
220 if (n < retry) {
221 /* ACK interrupts we may have caused */
222 sc->sc_iee_cmd(sc, IEE_SCB_ACK);
223 printf("done.\n");
224
225 return 0;
226 }
227 printf("time out.\n");
228
229 return -1;
230 }
231
232 static void
233 iee_sbdio_channel_attention(void *cookie)
234 {
235 struct iee_sbdio_softc *sc = cookie;
236 uint32_t dummy;
237
238 /* Issue a Channel Attention */
239 dummy = *sc->sc_port;
240 dummy = *sc->sc_port;
241 }
242
243 static void
244 iee_sbdio_chip_reset(void *cookie)
245 {
246 struct iee_sbdio_softc *sc = cookie;
247
248 *sc->sc_port = 0;
249 *sc->sc_port = 0;
250 delay(10);
251 }
252
253 static void
254 iee_sbdio_set_scp(void *cookie, uint32_t cmd)
255 {
256 struct iee_sbdio_softc *sc = cookie;
257 cmd = (cmd << 16) | (cmd >> 16);
258
259 *sc->sc_port = cmd;
260 *sc->sc_port = cmd;
261
262 /* Issue a Channel Attention to read SCP */
263 iee_sbdio_channel_attention(cookie);
264 }
265