esp_podule.c revision 1.1 1 1.1 kiyohara /* $NetBSD: esp_podule.c,v 1.1 2013/07/11 13:44:50 kiyohara Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2013 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara #include <sys/cdefs.h>
28 1.1 kiyohara __KERNEL_RCSID(0, "$NetBSD: esp_podule.c,v 1.1 2013/07/11 13:44:50 kiyohara Exp $");
29 1.1 kiyohara
30 1.1 kiyohara #include <sys/param.h>
31 1.1 kiyohara #include <sys/buf.h>
32 1.1 kiyohara #include <sys/bus.h>
33 1.1 kiyohara #include <sys/device.h>
34 1.1 kiyohara
35 1.1 kiyohara #include <dev/scsipi/scsi_all.h>
36 1.1 kiyohara #include <dev/scsipi/scsipi_all.h>
37 1.1 kiyohara #include <dev/scsipi/scsiconf.h>
38 1.1 kiyohara
39 1.1 kiyohara #include <dev/ic/ncr53c9xreg.h>
40 1.1 kiyohara #include <dev/ic/ncr53c9xvar.h>
41 1.1 kiyohara
42 1.1 kiyohara #include <dev/podulebus/podulebus.h>
43 1.1 kiyohara #include <dev/podulebus/podules.h>
44 1.1 kiyohara
45 1.1 kiyohara struct esp_podule_softc {
46 1.1 kiyohara struct ncr53c9x_softc sc_ncr53c9x;
47 1.1 kiyohara
48 1.1 kiyohara bus_space_tag_t sc_iot;
49 1.1 kiyohara bus_space_handle_t sc_ioh;
50 1.1 kiyohara
51 1.1 kiyohara struct ncr53c9x_glue sc_esp_glue;
52 1.1 kiyohara
53 1.1 kiyohara int sc_active; /* Pseudo-DMA state vars */
54 1.1 kiyohara int sc_tc;
55 1.1 kiyohara int sc_datain;
56 1.1 kiyohara size_t sc_dmasize;
57 1.1 kiyohara size_t sc_dmatrans;
58 1.1 kiyohara uint8_t **sc_dmaaddr;
59 1.1 kiyohara size_t *sc_dmalen;
60 1.1 kiyohara };
61 1.1 kiyohara
62 1.1 kiyohara static int esp_podule_match(device_t, cfdata_t, void *);
63 1.1 kiyohara static void esp_podule_attach(device_t, device_t, void *);
64 1.1 kiyohara
65 1.1 kiyohara static int esp_podule_dma_isintr(struct ncr53c9x_softc *);
66 1.1 kiyohara static void esp_podule_dma_reset(struct ncr53c9x_softc *);
67 1.1 kiyohara static int esp_podule_dma_intr(struct ncr53c9x_softc *);
68 1.1 kiyohara static int esp_podule_dma_setup(struct ncr53c9x_softc *, uint8_t **, size_t *,
69 1.1 kiyohara int, size_t *);
70 1.1 kiyohara static void esp_podule_dma_go(struct ncr53c9x_softc *);
71 1.1 kiyohara static void esp_podule_dma_stop(struct ncr53c9x_softc *);
72 1.1 kiyohara static int esp_podule_dma_isactive(struct ncr53c9x_softc *);
73 1.1 kiyohara
74 1.1 kiyohara static uint8_t castle_read_reg(struct ncr53c9x_softc *, int);
75 1.1 kiyohara static void castle_write_reg(struct ncr53c9x_softc *, int, uint8_t);
76 1.1 kiyohara
77 1.1 kiyohara CFATTACH_DECL_NEW(esp_podule, sizeof(struct esp_podule_softc),
78 1.1 kiyohara esp_podule_match, esp_podule_attach, NULL, NULL);
79 1.1 kiyohara
80 1.1 kiyohara static struct ncr53c9x_glue esp_podule_glue = {
81 1.1 kiyohara NULL,
82 1.1 kiyohara NULL,
83 1.1 kiyohara
84 1.1 kiyohara esp_podule_dma_isintr,
85 1.1 kiyohara esp_podule_dma_reset,
86 1.1 kiyohara esp_podule_dma_intr,
87 1.1 kiyohara esp_podule_dma_setup,
88 1.1 kiyohara esp_podule_dma_go,
89 1.1 kiyohara esp_podule_dma_stop,
90 1.1 kiyohara esp_podule_dma_isactive,
91 1.1 kiyohara NULL
92 1.1 kiyohara };
93 1.1 kiyohara
94 1.1 kiyohara #define PODULE_DEVICE(m, p, read, write) \
95 1.1 kiyohara { MANUFACTURER_ ## m, PODULE_ ## m ## _ ## p, read, write }
96 1.1 kiyohara static struct {
97 1.1 kiyohara int manufacturer;
98 1.1 kiyohara int product;
99 1.1 kiyohara uint8_t (*read_reg)(struct ncr53c9x_softc *, int);
100 1.1 kiyohara void (*write_reg)(struct ncr53c9x_softc *, int, uint8_t);
101 1.1 kiyohara } devices[] = {
102 1.1 kiyohara PODULE_DEVICE(CASTLE, SCSI16, castle_read_reg, castle_write_reg),
103 1.1 kiyohara };
104 1.1 kiyohara
105 1.1 kiyohara
106 1.1 kiyohara static int
107 1.1 kiyohara esp_podule_match(device_t parent, cfdata_t cf, void *aux)
108 1.1 kiyohara {
109 1.1 kiyohara struct podulebus_attach_args *pa = aux;
110 1.1 kiyohara int i;
111 1.1 kiyohara
112 1.1 kiyohara for (i = 0; i < __arraycount(devices); i++)
113 1.1 kiyohara if (pa->pa_manufacturer == devices[i].manufacturer &&
114 1.1 kiyohara pa->pa_product == devices[i].product)
115 1.1 kiyohara return 1;
116 1.1 kiyohara
117 1.1 kiyohara return 0;
118 1.1 kiyohara }
119 1.1 kiyohara
120 1.1 kiyohara static void
121 1.1 kiyohara esp_podule_attach(device_t parent, device_t self, void *aux)
122 1.1 kiyohara {
123 1.1 kiyohara struct esp_podule_softc *esc = device_private(self);
124 1.1 kiyohara struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
125 1.1 kiyohara struct podulebus_attach_args *pa = aux;
126 1.1 kiyohara int i;
127 1.1 kiyohara
128 1.1 kiyohara for (i = 0; i < __arraycount(devices); i++)
129 1.1 kiyohara if (pa->pa_manufacturer == devices[i].manufacturer &&
130 1.1 kiyohara pa->pa_product == devices[i].product)
131 1.1 kiyohara break;
132 1.1 kiyohara if (i == __arraycount(devices)) {
133 1.1 kiyohara aprint_error(": lost manufacturer 0x%04x, product 0x%04x\n",
134 1.1 kiyohara pa->pa_manufacturer, pa->pa_product);
135 1.1 kiyohara return;
136 1.1 kiyohara }
137 1.1 kiyohara
138 1.1 kiyohara sc->sc_dev = self;
139 1.1 kiyohara esc->sc_esp_glue = esp_podule_glue;
140 1.1 kiyohara esc->sc_esp_glue.gl_read_reg = devices[i].read_reg;
141 1.1 kiyohara esc->sc_esp_glue.gl_write_reg = devices[i].write_reg;
142 1.1 kiyohara sc->sc_glue = &esc->sc_esp_glue;
143 1.1 kiyohara
144 1.1 kiyohara esc->sc_iot = pa->pa_slow_t;
145 1.1 kiyohara bus_space_map(esc->sc_iot, pa->pa_slow_base, 0x4000, 0, &esc->sc_ioh);
146 1.1 kiyohara sc->sc_freq = 2; /* XXXX ??? */
147 1.1 kiyohara
148 1.1 kiyohara /* Get current ID */
149 1.1 kiyohara sc->sc_id = NCR_READ_REG(sc, NCR_CFG1) & NCRCFG1_BUSID;
150 1.1 kiyohara
151 1.1 kiyohara sc->sc_cfg1 = sc->sc_id;
152 1.1 kiyohara sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_DREQ | NCRCFG2_FE;
153 1.1 kiyohara sc->sc_rev = NCR_VARIANT_NCR53C96; /* XXXX ??? */
154 1.1 kiyohara
155 1.1 kiyohara sc->sc_minsync = 1000 / sc->sc_freq;
156 1.1 kiyohara
157 1.1 kiyohara sc->sc_maxxfer = 64 * 1024;
158 1.1 kiyohara
159 1.1 kiyohara evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
160 1.1 kiyohara device_xname(self), "intr");
161 1.1 kiyohara podulebus_irq_establish(pa->pa_ih, IPL_BIO, ncr53c9x_intr, sc,
162 1.1 kiyohara &sc->sc_intrcnt);
163 1.1 kiyohara
164 1.1 kiyohara sc->sc_adapter.adapt_minphys = minphys;
165 1.1 kiyohara sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
166 1.1 kiyohara ncr53c9x_attach(sc);
167 1.1 kiyohara }
168 1.1 kiyohara
169 1.1 kiyohara
170 1.1 kiyohara static int
171 1.1 kiyohara esp_podule_dma_isintr(struct ncr53c9x_softc *sc)
172 1.1 kiyohara {
173 1.1 kiyohara
174 1.1 kiyohara return NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT;
175 1.1 kiyohara }
176 1.1 kiyohara
177 1.1 kiyohara static void
178 1.1 kiyohara esp_podule_dma_reset(struct ncr53c9x_softc *sc)
179 1.1 kiyohara {
180 1.1 kiyohara struct esp_podule_softc *esc = (struct esp_podule_softc *)sc;
181 1.1 kiyohara
182 1.1 kiyohara esc->sc_active = 0;
183 1.1 kiyohara esc->sc_tc = 0;
184 1.1 kiyohara }
185 1.1 kiyohara
186 1.1 kiyohara static int
187 1.1 kiyohara esp_podule_dma_intr(struct ncr53c9x_softc *sc)
188 1.1 kiyohara {
189 1.1 kiyohara struct esp_podule_softc *esc = (struct esp_podule_softc *)sc;
190 1.1 kiyohara uint8_t *p;
191 1.1 kiyohara u_int espphase, espstat, espintr;
192 1.1 kiyohara int cnt;
193 1.1 kiyohara
194 1.1 kiyohara if (esc->sc_active == 0) {
195 1.1 kiyohara printf("%s: dma_intr--inactive DMA\n",
196 1.1 kiyohara device_xname(sc->sc_dev));
197 1.1 kiyohara return -1;
198 1.1 kiyohara }
199 1.1 kiyohara
200 1.1 kiyohara if ((sc->sc_espintr & NCRINTR_BS) == 0) {
201 1.1 kiyohara esc->sc_active = 0;
202 1.1 kiyohara return 0;
203 1.1 kiyohara }
204 1.1 kiyohara
205 1.1 kiyohara cnt = *esc->sc_dmalen;
206 1.1 kiyohara if (*esc->sc_dmalen == 0) {
207 1.1 kiyohara printf("%s: data interrupt, but no count left\n",
208 1.1 kiyohara device_xname(sc->sc_dev));
209 1.1 kiyohara }
210 1.1 kiyohara
211 1.1 kiyohara p = *esc->sc_dmaaddr;
212 1.1 kiyohara espphase = sc->sc_phase;
213 1.1 kiyohara espstat = (u_int)sc->sc_espstat;
214 1.1 kiyohara espintr = (u_int)sc->sc_espintr;
215 1.1 kiyohara do {
216 1.1 kiyohara if (esc->sc_datain) {
217 1.1 kiyohara *p++ = NCR_READ_REG(sc, NCR_FIFO);
218 1.1 kiyohara cnt--;
219 1.1 kiyohara if (espphase == DATA_IN_PHASE)
220 1.1 kiyohara NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
221 1.1 kiyohara else
222 1.1 kiyohara esc->sc_active = 0;
223 1.1 kiyohara } else {
224 1.1 kiyohara if ((espphase == DATA_OUT_PHASE) ||
225 1.1 kiyohara (espphase == MESSAGE_OUT_PHASE)) {
226 1.1 kiyohara NCR_WRITE_REG(sc, NCR_FIFO, *p++);
227 1.1 kiyohara cnt--;
228 1.1 kiyohara NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
229 1.1 kiyohara } else
230 1.1 kiyohara esc->sc_active = 0;
231 1.1 kiyohara }
232 1.1 kiyohara
233 1.1 kiyohara if (esc->sc_active) {
234 1.1 kiyohara while ((NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT) == 0)
235 1.1 kiyohara ;
236 1.1 kiyohara espstat = NCR_READ_REG(sc, NCR_STAT);
237 1.1 kiyohara espintr = NCR_READ_REG(sc, NCR_INTR);
238 1.1 kiyohara espphase = (espintr & NCRINTR_DIS) ?
239 1.1 kiyohara /* Disconnected */ BUSFREE_PHASE :
240 1.1 kiyohara espstat & PHASE_MASK;
241 1.1 kiyohara }
242 1.1 kiyohara } while (esc->sc_active && espintr);
243 1.1 kiyohara sc->sc_phase = espphase;
244 1.1 kiyohara sc->sc_espstat = (uint8_t)espstat;
245 1.1 kiyohara sc->sc_espintr = (uint8_t)espintr;
246 1.1 kiyohara *esc->sc_dmaaddr = p;
247 1.1 kiyohara *esc->sc_dmalen = cnt;
248 1.1 kiyohara
249 1.1 kiyohara if (*esc->sc_dmalen == 0)
250 1.1 kiyohara esc->sc_tc = NCRSTAT_TC;
251 1.1 kiyohara sc->sc_espstat |= esc->sc_tc;
252 1.1 kiyohara
253 1.1 kiyohara return 0;
254 1.1 kiyohara }
255 1.1 kiyohara
256 1.1 kiyohara static int
257 1.1 kiyohara esp_podule_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
258 1.1 kiyohara int datain, size_t *dmasize)
259 1.1 kiyohara {
260 1.1 kiyohara struct esp_podule_softc *esc = (struct esp_podule_softc *)sc;
261 1.1 kiyohara
262 1.1 kiyohara esc->sc_dmaaddr = addr;
263 1.1 kiyohara esc->sc_dmalen = len;
264 1.1 kiyohara esc->sc_datain = datain;
265 1.1 kiyohara esc->sc_dmasize = *dmasize;
266 1.1 kiyohara esc->sc_tc = 0;
267 1.1 kiyohara
268 1.1 kiyohara return 0;
269 1.1 kiyohara }
270 1.1 kiyohara
271 1.1 kiyohara static void
272 1.1 kiyohara esp_podule_dma_go(struct ncr53c9x_softc *sc)
273 1.1 kiyohara {
274 1.1 kiyohara struct esp_podule_softc *esc = (struct esp_podule_softc *)sc;
275 1.1 kiyohara
276 1.1 kiyohara if (esc->sc_datain == 0) {
277 1.1 kiyohara NCR_WRITE_REG(sc, NCR_FIFO, **esc->sc_dmaaddr);
278 1.1 kiyohara (*esc->sc_dmalen)--;
279 1.1 kiyohara (*esc->sc_dmaaddr)++;
280 1.1 kiyohara }
281 1.1 kiyohara
282 1.1 kiyohara esc->sc_active = 1;
283 1.1 kiyohara }
284 1.1 kiyohara
285 1.1 kiyohara static void
286 1.1 kiyohara esp_podule_dma_stop(struct ncr53c9x_softc *sc)
287 1.1 kiyohara {
288 1.1 kiyohara /* Nothing */
289 1.1 kiyohara }
290 1.1 kiyohara
291 1.1 kiyohara static int
292 1.1 kiyohara esp_podule_dma_isactive(struct ncr53c9x_softc *sc)
293 1.1 kiyohara {
294 1.1 kiyohara struct esp_podule_softc *esc = (struct esp_podule_softc *)sc;
295 1.1 kiyohara
296 1.1 kiyohara return esc->sc_active;
297 1.1 kiyohara }
298 1.1 kiyohara
299 1.1 kiyohara /*
300 1.1 kiyohara * Functions for access to Castle SCSI registers.
301 1.1 kiyohara */
302 1.1 kiyohara #define CASTLE_ESP_OFFSET 0xf00
303 1.1 kiyohara #define CASTLE_ESP_READ(sc, r) \
304 1.1 kiyohara bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, CASTLE_ESP_OFFSET + ((r) << 2))
305 1.1 kiyohara #define CASTLE_ESP_WRITE(sc, r, v) \
306 1.1 kiyohara bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, \
307 1.1 kiyohara CASTLE_ESP_OFFSET + ((r) << 2), (v))
308 1.1 kiyohara
309 1.1 kiyohara static uint8_t
310 1.1 kiyohara castle_read_reg(struct ncr53c9x_softc *sc, int reg)
311 1.1 kiyohara {
312 1.1 kiyohara struct esp_podule_softc *esc = (struct esp_podule_softc *)sc;
313 1.1 kiyohara uint8_t val;
314 1.1 kiyohara
315 1.1 kiyohara val = CASTLE_ESP_READ(esc, reg);
316 1.1 kiyohara return val;
317 1.1 kiyohara }
318 1.1 kiyohara
319 1.1 kiyohara static void
320 1.1 kiyohara castle_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val)
321 1.1 kiyohara {
322 1.1 kiyohara struct esp_podule_softc *esc = (struct esp_podule_softc *)sc;
323 1.1 kiyohara
324 1.1 kiyohara if (reg == NCR_CMD && val == (NCRCMD_TRANS | NCRCMD_DMA))
325 1.1 kiyohara val &= ~NCRCMD_DMA; /* DMA not support */
326 1.1 kiyohara CASTLE_ESP_WRITE(esc, reg, val);
327 1.1 kiyohara }
328