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