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