if_iee_gsc.c revision 1.1 1 1.1 skrll /* $NetBSD: if_iee_gsc.c,v 1.1 2014/02/24 07:23:43 skrll Exp $ */
2 1.1 skrll
3 1.1 skrll /*
4 1.1 skrll * Copyright (c) 2003 Jochen Kunz.
5 1.1 skrll * All rights reserved.
6 1.1 skrll *
7 1.1 skrll * Redistribution and use in source and binary forms, with or without
8 1.1 skrll * modification, are permitted provided that the following conditions
9 1.1 skrll * are met:
10 1.1 skrll * 1. Redistributions of source code must retain the above copyright
11 1.1 skrll * notice, this list of conditions and the following disclaimer.
12 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 skrll * notice, this list of conditions and the following disclaimer in the
14 1.1 skrll * documentation and/or other materials provided with the distribution.
15 1.1 skrll * 3. The name of Jochen Kunz may not be used to endorse or promote
16 1.1 skrll * products derived from this software without specific prior
17 1.1 skrll * written permission.
18 1.1 skrll *
19 1.1 skrll * THIS SOFTWARE IS PROVIDED BY JOCHEN KUNZ
20 1.1 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JOCHEN KUNZ
23 1.1 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 skrll * POSSIBILITY OF SUCH DAMAGE.
30 1.1 skrll */
31 1.1 skrll
32 1.1 skrll /*
33 1.1 skrll * hppa GSC bus MD frontend for the iee(4) Intel i82596 Ethernet driver.
34 1.1 skrll */
35 1.1 skrll
36 1.1 skrll #include <sys/cdefs.h>
37 1.1 skrll __KERNEL_RCSID(0, "$NetBSD: if_iee_gsc.c,v 1.1 2014/02/24 07:23:43 skrll Exp $");
38 1.1 skrll
39 1.1 skrll /* autoconfig and device stuff */
40 1.1 skrll #include <sys/param.h>
41 1.1 skrll #include <sys/device.h>
42 1.1 skrll #include <sys/conf.h>
43 1.1 skrll #include <machine/iomod.h>
44 1.1 skrll #include <machine/autoconf.h>
45 1.1 skrll #include <hppa/dev/cpudevs.h>
46 1.1 skrll #include <hppa/gsc/gscbusvar.h>
47 1.1 skrll #include "locators.h"
48 1.1 skrll #include "ioconf.h"
49 1.1 skrll
50 1.1 skrll /* bus_space / bus_dma etc. */
51 1.1 skrll #include <sys/bus.h>
52 1.1 skrll #include <machine/intr.h>
53 1.1 skrll
54 1.1 skrll /* general system data and functions */
55 1.1 skrll #include <sys/systm.h>
56 1.1 skrll #include <sys/ioctl.h>
57 1.1 skrll #include <sys/ioccom.h>
58 1.1 skrll #include <sys/types.h>
59 1.1 skrll
60 1.1 skrll /* tsleep / sleep / wakeup */
61 1.1 skrll #include <sys/proc.h>
62 1.1 skrll /* hz for above */
63 1.1 skrll #include <sys/kernel.h>
64 1.1 skrll
65 1.1 skrll /* network stuff */
66 1.1 skrll #include <net/if.h>
67 1.1 skrll #include <net/if_dl.h>
68 1.1 skrll #include <net/if_media.h>
69 1.1 skrll #include <net/if_ether.h>
70 1.1 skrll #include <sys/socket.h>
71 1.1 skrll #include <sys/mbuf.h>
72 1.1 skrll
73 1.1 skrll #include <dev/ic/i82596reg.h>
74 1.1 skrll #include <dev/ic/i82596var.h>
75 1.1 skrll
76 1.1 skrll #define IEE_GSC_IO_SZ 12
77 1.1 skrll #define IEE_GSC_RESET 0
78 1.1 skrll #define IEE_GSC_PORT 4
79 1.1 skrll #define IEE_GSC_CHANATT 8
80 1.1 skrll #define IEE_ISCP_BUSY 0x1
81 1.1 skrll
82 1.1 skrll /* autoconfig stuff */
83 1.1 skrll static int iee_gsc_match(device_t, cfdata_t, void *);
84 1.1 skrll static void iee_gsc_attach(device_t, device_t, void *);
85 1.1 skrll static int iee_gsc_detach(device_t, int);
86 1.1 skrll
87 1.1 skrll struct iee_gsc_softc {
88 1.1 skrll struct iee_softc iee_sc;
89 1.1 skrll bus_space_tag_t sc_iot;
90 1.1 skrll bus_space_handle_t sc_ioh;
91 1.1 skrll void *sc_ih;
92 1.1 skrll };
93 1.1 skrll
94 1.1 skrll CFATTACH_DECL_NEW(
95 1.1 skrll iee_gsc,
96 1.1 skrll sizeof(struct iee_gsc_softc),
97 1.1 skrll iee_gsc_match,
98 1.1 skrll iee_gsc_attach,
99 1.1 skrll iee_gsc_detach,
100 1.1 skrll NULL
101 1.1 skrll );
102 1.1 skrll
103 1.1 skrll int iee_gsc_cmd(struct iee_softc *, uint32_t);
104 1.1 skrll int iee_gsc_reset(struct iee_softc *);
105 1.1 skrll
106 1.1 skrll int
107 1.1 skrll iee_gsc_cmd(struct iee_softc *sc, uint32_t cmd)
108 1.1 skrll {
109 1.1 skrll struct iee_gsc_softc *sc_gsc = (struct iee_gsc_softc *)sc;
110 1.1 skrll int n;
111 1.1 skrll uint16_t ack;
112 1.1 skrll
113 1.1 skrll SC_SCB(sc)->scb_cmd = cmd;
114 1.1 skrll IEE_SCBSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
115 1.1 skrll /* Issue a Channel Attention to force the chip to read the cmd. */
116 1.1 skrll bus_space_write_4(sc_gsc->sc_iot, sc_gsc->sc_ioh, IEE_GSC_CHANATT, 0);
117 1.1 skrll /* Wait for the cmd to finish */
118 1.1 skrll for (n = 0 ; n < 100000; n++) {
119 1.1 skrll DELAY(1);
120 1.1 skrll IEE_SCBSYNC(sc, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
121 1.1 skrll ack = SC_SCB(sc)->scb_cmd;
122 1.1 skrll IEE_SCBSYNC(sc, BUS_DMASYNC_PREREAD);
123 1.1 skrll if (ack == 0)
124 1.1 skrll break;
125 1.1 skrll }
126 1.1 skrll if (n < 100000)
127 1.1 skrll return 0;
128 1.1 skrll printf("%s: iee_gsc_cmd: timeout n=%d\n", device_xname(sc->sc_dev), n);
129 1.1 skrll return -1;
130 1.1 skrll }
131 1.1 skrll
132 1.1 skrll int
133 1.1 skrll iee_gsc_reset(struct iee_softc *sc)
134 1.1 skrll {
135 1.1 skrll struct iee_gsc_softc *sc_gsc = (struct iee_gsc_softc *)sc;
136 1.1 skrll int n;
137 1.1 skrll uint32_t cmd;
138 1.1 skrll uint16_t ack;
139 1.1 skrll
140 1.1 skrll /* Make sure the busy byte is set and the cache is flushed. */
141 1.1 skrll SC_ISCP(sc)->iscp_busy = IEE_ISCP_BUSY;
142 1.1 skrll IEE_ISCPSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
143 1.1 skrll /* Setup the PORT Command with pointer to SCP. */
144 1.1 skrll cmd = IEE_PORT_SCP | IEE_PHYS_SHMEM(sc->sc_scp_off);
145 1.1 skrll /* Write a word to IEE_GSC_RESET to initiate a Hardware reset. */
146 1.1 skrll bus_space_write_4(sc_gsc->sc_iot, sc_gsc->sc_ioh, IEE_GSC_RESET, 0);
147 1.1 skrll DELAY(1000);
148 1.1 skrll /* Write it to the chip, it wants this in two 16 bit parts. */
149 1.1 skrll if (sc->sc_type == I82596_CA) {
150 1.1 skrll bus_space_write_4(sc_gsc->sc_iot, sc_gsc->sc_ioh, IEE_GSC_PORT,
151 1.1 skrll (cmd & 0xffff));
152 1.1 skrll DELAY(1000);
153 1.1 skrll bus_space_write_4(sc_gsc->sc_iot, sc_gsc->sc_ioh, IEE_GSC_PORT,
154 1.1 skrll (cmd >> 16));
155 1.1 skrll } else {
156 1.1 skrll bus_space_write_4(sc_gsc->sc_iot, sc_gsc->sc_ioh, IEE_GSC_PORT,
157 1.1 skrll (cmd >> 16));
158 1.1 skrll DELAY(1000);
159 1.1 skrll bus_space_write_4(sc_gsc->sc_iot, sc_gsc->sc_ioh, IEE_GSC_PORT,
160 1.1 skrll (cmd & 0xffff));
161 1.1 skrll }
162 1.1 skrll DELAY(1000);
163 1.1 skrll /* Issue a Channel Attention to read SCP */
164 1.1 skrll bus_space_write_4(sc_gsc->sc_iot, sc_gsc->sc_ioh, IEE_GSC_CHANATT, 0);
165 1.1 skrll /* Wait for the chip to initialize and read SCP and ISCP. */
166 1.1 skrll for (n = 0 ; n < 1000; n++) {
167 1.1 skrll IEE_ISCPSYNC(sc, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
168 1.1 skrll ack = SC_ISCP(sc)->iscp_busy;
169 1.1 skrll IEE_ISCPSYNC(sc, BUS_DMASYNC_PREREAD);
170 1.1 skrll if (ack != IEE_ISCP_BUSY)
171 1.1 skrll break;
172 1.1 skrll DELAY(100);
173 1.1 skrll }
174 1.1 skrll if (n < 1000) {
175 1.1 skrll /* ACK interrupts we may have caused */
176 1.1 skrll (sc->sc_iee_cmd)(sc, IEE_SCB_ACK);
177 1.1 skrll return 0;
178 1.1 skrll }
179 1.1 skrll printf("%s: iee_gsc_reset timeout busy=0x%x\n",
180 1.1 skrll device_xname(sc->sc_dev), SC_ISCP(sc)->iscp_busy);
181 1.1 skrll return -1;
182 1.1 skrll }
183 1.1 skrll
184 1.1 skrll static int
185 1.1 skrll iee_gsc_match(device_t parent, cfdata_t cf, void *aux)
186 1.1 skrll {
187 1.1 skrll struct gsc_attach_args *ga = aux;
188 1.1 skrll
189 1.1 skrll if (ga->ga_type.iodc_type == HPPA_TYPE_FIO
190 1.1 skrll && (ga->ga_type.iodc_sv_model == HPPA_FIO_LAN
191 1.1 skrll || ga->ga_type.iodc_sv_model == HPPA_FIO_GLAN))
192 1.1 skrll /* beat old ie(4) i82586 driver */
193 1.1 skrll return 10;
194 1.1 skrll return 0;
195 1.1 skrll }
196 1.1 skrll
197 1.1 skrll static void
198 1.1 skrll iee_gsc_attach(device_t parent, device_t self, void *aux)
199 1.1 skrll {
200 1.1 skrll struct iee_gsc_softc *sc_gsc = device_private(self);
201 1.1 skrll struct iee_softc *sc = &sc_gsc->iee_sc;
202 1.1 skrll struct gsc_attach_args *ga = aux;
203 1.1 skrll enum hppa_cpu_type cpu_type;
204 1.1 skrll int media[2];
205 1.1 skrll
206 1.1 skrll sc->sc_dev = self;
207 1.1 skrll
208 1.1 skrll if (ga->ga_type.iodc_sv_model == HPPA_FIO_LAN)
209 1.1 skrll sc->sc_type = I82596_DX; /* ASP(2) based */
210 1.1 skrll else
211 1.1 skrll sc->sc_type = I82596_CA; /* LASI based */
212 1.1 skrll /*
213 1.1 skrll * Pre PA7100LC CPUs don't support uncacheable mappings. So make
214 1.1 skrll * descriptors align to cache lines. Needed to avoid race conditions
215 1.1 skrll * caused by flushing cache lines that overlap multiple descriptors.
216 1.1 skrll */
217 1.1 skrll cpu_type = hppa_cpu_info->hci_cputype;
218 1.1 skrll if (cpu_type == hpcx || cpu_type == hpcxs || cpu_type == hpcxt)
219 1.1 skrll sc->sc_cl_align = 32;
220 1.1 skrll else
221 1.1 skrll sc->sc_cl_align = 1;
222 1.1 skrll
223 1.1 skrll sc_gsc->sc_iot = ga->ga_iot;
224 1.1 skrll if (bus_space_map(sc_gsc->sc_iot, ga->ga_hpa, IEE_GSC_IO_SZ, 0,
225 1.1 skrll &sc_gsc->sc_ioh)) {
226 1.1 skrll aprint_error(": iee_gsc_attach: can't map I/O space\n");
227 1.1 skrll return;
228 1.1 skrll }
229 1.1 skrll
230 1.1 skrll sc->sc_dmat = ga->ga_dmatag;
231 1.1 skrll
232 1.1 skrll /* Setup SYSBUS byte. */
233 1.1 skrll if (ga->ga_type.iodc_sv_model == HPPA_FIO_LAN) {
234 1.1 skrll /*
235 1.1 skrll * Some earlier machines have 82596DX Rev A1 chip
236 1.1 skrll * which doesn't have IEE_SYSBUS_BE for 32-bit BE pointers.
237 1.1 skrll *
238 1.1 skrll * XXX: How can we detect chip revision at runtime?
239 1.1 skrll * Should we check cpu_models instead?
240 1.1 skrll * 715/50, 735/99: Rev A1? (per PR port-hp700/35531)
241 1.1 skrll * 735/125: Rev C
242 1.1 skrll */
243 1.1 skrll sc->sc_sysbus = IEE_SYSBUS_INT |
244 1.1 skrll IEE_SYSBUS_TRG | IEE_SYSBUS_LIEAR | IEE_SYSBUS_STD;
245 1.1 skrll sc->sc_flags = IEE_NEED_SWAP | IEE_REV_A;
246 1.1 skrll } else {
247 1.1 skrll sc->sc_sysbus = IEE_SYSBUS_BE | IEE_SYSBUS_INT |
248 1.1 skrll IEE_SYSBUS_TRG | IEE_SYSBUS_LIEAR | IEE_SYSBUS_STD;
249 1.1 skrll sc->sc_flags = IEE_NEED_SWAP;
250 1.1 skrll }
251 1.1 skrll
252 1.1 skrll sc_gsc->sc_ih = hppa_intr_establish(IPL_NET, iee_intr, sc,
253 1.1 skrll ga->ga_ir, ga->ga_irq);
254 1.1 skrll
255 1.1 skrll sc->sc_iee_reset = iee_gsc_reset;
256 1.1 skrll sc->sc_iee_cmd = iee_gsc_cmd;
257 1.1 skrll sc->sc_mediachange = NULL;
258 1.1 skrll sc->sc_mediastatus = NULL;
259 1.1 skrll
260 1.1 skrll media[0] = IFM_ETHER | IFM_MANUAL;
261 1.1 skrll media[1] = IFM_ETHER | IFM_AUTO;
262 1.1 skrll iee_attach(sc, ga->ga_ether_address, media, 2, IFM_ETHER | IFM_AUTO);
263 1.1 skrll }
264 1.1 skrll
265 1.1 skrll int
266 1.1 skrll iee_gsc_detach(device_t self, int flags)
267 1.1 skrll {
268 1.1 skrll struct iee_gsc_softc *sc_gsc = device_private(self);
269 1.1 skrll struct iee_softc *sc = &sc_gsc->iee_sc;
270 1.1 skrll
271 1.1 skrll iee_detach(sc, flags);
272 1.1 skrll bus_space_unmap(sc_gsc->sc_iot, sc_gsc->sc_ioh, IEE_GSC_IO_SZ);
273 1.1 skrll /* There is no hppa_intr_disestablish()! */
274 1.1 skrll return 0;
275 1.1 skrll }
276