2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
4 * Copyright (c) 2011 Marius Strobl <marius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 /* $NetBSD: pcscp.c,v 1.45 2010/11/13 13:52:08 uebayasi Exp $ */
32 * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
33 * All rights reserved.
35 * This code is derived from software contributed to The NetBSD Foundation
36 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
37 * NASA Ames Research Center; Izumi Tsutsui.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
48 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
62 * esp_pci.c: device dependent code for AMD Am53c974 (PCscsi-PCI)
63 * written by Izumi Tsutsui <tsutsui@NetBSD.org>
65 * Technical manual available at
66 * http://www.amd.com/files/connectivitysolutions/networking/archivednetworking/19113.pdf
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/endian.h>
76 #include <sys/kernel.h>
78 #include <sys/module.h>
79 #include <sys/mutex.h>
80 #include <sys/resource.h>
83 #include <machine/bus.h>
84 #include <machine/resource.h>
87 #include <cam/cam_ccb.h>
88 #include <cam/scsi/scsi_all.h>
90 #include <dev/pci/pcireg.h>
91 #include <dev/pci/pcivar.h>
93 #include <dev/esp/ncr53c9xreg.h>
94 #include <dev/esp/ncr53c9xvar.h>
96 #include <dev/esp/am53c974reg.h>
98 #define PCI_DEVICE_ID_AMD53C974 0x20201022
100 struct esp_pci_softc {
101 struct ncr53c9x_softc sc_ncr53c9x; /* glue to MI code */
104 struct resource *sc_res[2];
105 #define ESP_PCI_RES_INTR 0
106 #define ESP_PCI_RES_IO 1
108 bus_dma_tag_t sc_pdmat;
110 bus_dma_tag_t sc_xferdmat; /* DMA tag for transfers */
111 bus_dmamap_t sc_xferdmam; /* DMA map for transfers */
113 void *sc_ih; /* interrupt handler */
115 size_t sc_dmasize; /* DMA size */
116 void **sc_dmaaddr; /* DMA address */
117 size_t *sc_dmalen; /* DMA length */
118 int sc_active; /* DMA state */
119 int sc_datain; /* DMA Data Direction */
122 static struct resource_spec esp_pci_res_spec[] = {
123 { SYS_RES_IRQ, 0, RF_SHAREABLE | RF_ACTIVE }, /* ESP_PCI_RES_INTR */
124 { SYS_RES_IOPORT, PCIR_BAR(0), RF_ACTIVE }, /* ESP_PCI_RES_IO */
128 #define READ_DMAREG(sc, reg) \
129 bus_read_4((sc)->sc_res[ESP_PCI_RES_IO], (reg))
130 #define WRITE_DMAREG(sc, reg, var) \
131 bus_write_4((sc)->sc_res[ESP_PCI_RES_IO], (reg), (var))
133 #define READ_ESPREG(sc, reg) \
134 bus_read_1((sc)->sc_res[ESP_PCI_RES_IO], (reg) << 2)
135 #define WRITE_ESPREG(sc, reg, val) \
136 bus_write_1((sc)->sc_res[ESP_PCI_RES_IO], (reg) << 2, (val))
138 static int esp_pci_probe(device_t);
139 static int esp_pci_attach(device_t);
140 static int esp_pci_detach(device_t);
141 static int esp_pci_suspend(device_t);
142 static int esp_pci_resume(device_t);
144 static device_method_t esp_pci_methods[] = {
145 DEVMETHOD(device_probe, esp_pci_probe),
146 DEVMETHOD(device_attach, esp_pci_attach),
147 DEVMETHOD(device_detach, esp_pci_detach),
148 DEVMETHOD(device_suspend, esp_pci_suspend),
149 DEVMETHOD(device_resume, esp_pci_resume),
154 static driver_t esp_pci_driver = {
157 sizeof(struct esp_pci_softc)
160 DRIVER_MODULE(esp, pci, esp_pci_driver, esp_devclass, 0, 0);
161 MODULE_DEPEND(esp, pci, 1, 1, 1);
164 * Functions and the switch for the MI code
166 static void esp_pci_dma_go(struct ncr53c9x_softc *);
167 static int esp_pci_dma_intr(struct ncr53c9x_softc *);
168 static int esp_pci_dma_isactive(struct ncr53c9x_softc *);
170 static int esp_pci_dma_isintr(struct ncr53c9x_softc *);
171 static void esp_pci_dma_reset(struct ncr53c9x_softc *);
172 static int esp_pci_dma_setup(struct ncr53c9x_softc *, void **, size_t *,
174 static void esp_pci_dma_stop(struct ncr53c9x_softc *);
175 static void esp_pci_write_reg(struct ncr53c9x_softc *, int, uint8_t);
176 static uint8_t esp_pci_read_reg(struct ncr53c9x_softc *, int);
177 static void esp_pci_xfermap(void *arg, bus_dma_segment_t *segs, int nseg,
180 static struct ncr53c9x_glue esp_pci_glue = {
189 esp_pci_dma_isactive,
193 esp_pci_probe(device_t dev)
196 if (pci_get_devid(dev) == PCI_DEVICE_ID_AMD53C974) {
197 device_set_desc(dev, "AMD Am53C974 Fast-SCSI");
198 return (BUS_PROBE_DEFAULT);
205 * Attach this instance, and then all the sub-devices
208 esp_pci_attach(device_t dev)
210 struct esp_pci_softc *esc;
211 struct ncr53c9x_softc *sc;
214 esc = device_get_softc(dev);
215 sc = &esc->sc_ncr53c9x;
220 sc->sc_glue = &esp_pci_glue;
222 pci_enable_busmaster(dev);
224 error = bus_alloc_resources(dev, esp_pci_res_spec, esc->sc_res);
226 device_printf(dev, "failed to allocate resources\n");
227 bus_release_resources(dev, esp_pci_res_spec, esc->sc_res);
231 error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
232 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
233 BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED,
234 BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, &esc->sc_pdmat);
236 device_printf(dev, "cannot create parent DMA tag\n");
241 * XXX More of this should be in ncr53c9x_attach(), but
242 * XXX should we really poke around the chip that much in
243 * XXX the MI code? Think about this more...
247 * Set up static configuration info.
249 * XXX we should read the configuration from the EEPROM.
252 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
253 sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
254 sc->sc_cfg3 = NCRAMDCFG3_IDM | NCRAMDCFG3_FCLK;
255 sc->sc_cfg4 = NCRAMDCFG4_GE12NS | NCRAMDCFG4_RADE;
256 sc->sc_rev = NCR_VARIANT_AM53C974;
257 sc->sc_features = NCR_F_FASTSCSI | NCR_F_DMASELECT;
258 sc->sc_cfg3_fscsi = NCRAMDCFG3_FSCSI;
259 sc->sc_freq = 40; /* MHz */
262 * This is the value used to start sync negotiations
263 * Note that the NCR register "SYNCTP" is programmed
264 * in "clocks per byte", and has a minimum value of 4.
265 * The SCSI period used in negotiation is one-fourth
266 * of the time (in nanoseconds) needed to transfer one byte.
267 * Since the chip's clock is given in MHz, we have the following
268 * formula: 4 * period = (1000 / freq) * 4
270 sc->sc_minsync = 1000 / sc->sc_freq;
272 sc->sc_maxxfer = DFLTPHYS; /* see below */
273 sc->sc_maxoffset = 15;
274 sc->sc_extended_geom = 1;
276 #define MDL_SEG_SIZE 0x1000 /* 4kbyte per segment */
279 * Create the DMA tag and map for the data transfers.
281 * Note: given that bus_dma(9) only adheres to the requested alignment
282 * for the first segment (and that also only for bus_dmamem_alloc()ed
283 * DMA maps) we can't use the Memory Descriptor List. However, also
284 * when not using the MDL, the maximum transfer size apparently is
285 * limited to 4k so we have to split transfers up, which plain sucks.
287 error = bus_dma_tag_create(esc->sc_pdmat, PAGE_SIZE, 0,
288 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
289 MDL_SEG_SIZE, 1, MDL_SEG_SIZE, BUS_DMA_ALLOCNOW,
290 busdma_lock_mutex, &sc->sc_lock, &esc->sc_xferdmat);
292 device_printf(dev, "cannot create transfer DMA tag\n");
295 error = bus_dmamap_create(esc->sc_xferdmat, 0, &esc->sc_xferdmam);
297 device_printf(dev, "cannot create transfer DMA map\n");
301 error = bus_setup_intr(dev, esc->sc_res[ESP_PCI_RES_INTR],
302 INTR_MPSAFE | INTR_TYPE_CAM, NULL, ncr53c9x_intr, sc,
305 device_printf(dev, "cannot set up interrupt\n");
309 /* Do the common parts of attachment. */
310 sc->sc_dev = esc->sc_dev;
311 error = ncr53c9x_attach(sc);
313 device_printf(esc->sc_dev, "ncr53c9x_attach failed\n");
320 bus_teardown_intr(esc->sc_dev, esc->sc_res[ESP_PCI_RES_INTR],
323 bus_dmamap_destroy(esc->sc_xferdmat, esc->sc_xferdmam);
325 bus_dma_tag_destroy(esc->sc_xferdmat);
327 bus_dma_tag_destroy(esc->sc_pdmat);
329 bus_release_resources(dev, esp_pci_res_spec, esc->sc_res);
330 NCR_LOCK_DESTROY(sc);
336 esp_pci_detach(device_t dev)
338 struct ncr53c9x_softc *sc;
339 struct esp_pci_softc *esc;
342 esc = device_get_softc(dev);
343 sc = &esc->sc_ncr53c9x;
345 bus_teardown_intr(esc->sc_dev, esc->sc_res[ESP_PCI_RES_INTR],
347 error = ncr53c9x_detach(sc);
350 bus_dmamap_destroy(esc->sc_xferdmat, esc->sc_xferdmam);
351 bus_dma_tag_destroy(esc->sc_xferdmat);
352 bus_dma_tag_destroy(esc->sc_pdmat);
353 bus_release_resources(dev, esp_pci_res_spec, esc->sc_res);
354 NCR_LOCK_DESTROY(sc);
360 esp_pci_suspend(device_t dev)
367 esp_pci_resume(device_t dev)
374 esp_pci_xfermap(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
376 struct esp_pci_softc *esc = (struct esp_pci_softc *)arg;
381 KASSERT(nsegs == 1, ("%s: bad transfer segment count %d", __func__,
383 KASSERT(segs[0].ds_len <= MDL_SEG_SIZE,
384 ("%s: bad transfer segment length %ld", __func__,
385 (long)segs[0].ds_len));
387 /* Program the DMA Starting Physical Address. */
388 WRITE_DMAREG(esc, DMA_SPA, segs[0].ds_addr);
396 esp_pci_read_reg(struct ncr53c9x_softc *sc, int reg)
398 struct esp_pci_softc *esc = (struct esp_pci_softc *)sc;
400 return (READ_ESPREG(esc, reg));
404 esp_pci_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t v)
406 struct esp_pci_softc *esc = (struct esp_pci_softc *)sc;
408 WRITE_ESPREG(esc, reg, v);
412 esp_pci_dma_isintr(struct ncr53c9x_softc *sc)
414 struct esp_pci_softc *esc = (struct esp_pci_softc *)sc;
416 return (READ_ESPREG(esc, NCR_STAT) & NCRSTAT_INT) != 0;
420 esp_pci_dma_reset(struct ncr53c9x_softc *sc)
422 struct esp_pci_softc *esc = (struct esp_pci_softc *)sc;
424 WRITE_DMAREG(esc, DMA_CMD, DMACMD_IDLE);
430 esp_pci_dma_intr(struct ncr53c9x_softc *sc)
432 struct esp_pci_softc *esc = (struct esp_pci_softc *)sc;
433 bus_dma_tag_t xferdmat;
434 bus_dmamap_t xferdmam;
436 int datain, i, resid, trans;
440 xferdmat = esc->sc_xferdmat;
441 xferdmam = esc->sc_xferdmam;
442 datain = esc->sc_datain;
444 dmastat = READ_DMAREG(esc, DMA_STAT);
446 if ((dmastat & DMASTAT_ERR) != 0) {
447 /* XXX not tested... */
448 WRITE_DMAREG(esc, DMA_CMD, DMACMD_ABORT | (datain != 0 ?
451 device_printf(esc->sc_dev, "DMA error detected; Aborting.\n");
452 bus_dmamap_sync(xferdmat, xferdmam, datain != 0 ?
453 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
454 bus_dmamap_unload(xferdmat, xferdmam);
458 if ((dmastat & DMASTAT_ABT) != 0) {
459 /* XXX what should be done? */
460 device_printf(esc->sc_dev, "DMA aborted.\n");
461 WRITE_DMAREG(esc, DMA_CMD, DMACMD_IDLE | (datain != 0 ?
467 KASSERT(esc->sc_active != 0, ("%s: DMA wasn't active", __func__));
469 /* DMA has stopped. */
473 dmasize = esc->sc_dmasize;
475 /* A "Transfer Pad" operation completed. */
476 NCR_DMA(("%s: discarded %d bytes (tcl=%d, tcm=%d)\n",
477 __func__, READ_ESPREG(esc, NCR_TCL) |
478 (READ_ESPREG(esc, NCR_TCM) << 8),
479 READ_ESPREG(esc, NCR_TCL), READ_ESPREG(esc, NCR_TCM)));
485 * If a transfer onto the SCSI bus gets interrupted by the device
486 * (e.g. for a SAVEPOINTER message), the data in the FIFO counts
487 * as residual since the ESP counter registers get decremented as
488 * bytes are clocked into the FIFO.
491 (resid = (READ_ESPREG(esc, NCR_FFLAG) & NCRFIFO_FF)) != 0)
492 NCR_DMA(("%s: empty esp FIFO of %d ", __func__, resid));
494 if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
496 * "Terminal count" is off, so read the residue
497 * out of the ESP counter registers.
500 resid = READ_ESPREG(esc, NCR_FFLAG) & NCRFIFO_FF;
503 READ_ESPREG(esc, NCR_FFLAG) & NCRFIFO_FF;
504 WRITE_DMAREG(esc, DMA_CMD, DMACMD_BLAST | DMACMD_DIR);
506 for (i = 0; i < 0x8000; i++) /* XXX 0x8000 ? */
507 if ((READ_DMAREG(esc, DMA_STAT) &
511 /* See the below comments... */
513 p = *esc->sc_dmaaddr;
516 resid += READ_ESPREG(esc, NCR_TCL) |
517 (READ_ESPREG(esc, NCR_TCM) << 8) |
518 (READ_ESPREG(esc, NCR_TCH) << 16);
520 while ((dmastat & DMASTAT_DONE) == 0)
521 dmastat = READ_DMAREG(esc, DMA_STAT);
523 WRITE_DMAREG(esc, DMA_CMD, DMACMD_IDLE | (datain != 0 ?
526 /* Sync the transfer buffer. */
527 bus_dmamap_sync(xferdmat, xferdmam, datain != 0 ?
528 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
529 bus_dmamap_unload(xferdmat, xferdmam);
531 trans = dmasize - resid;
534 * From the technical manual notes:
536 * "In some odd byte conditions, one residual byte will be left
537 * in the SCSI FIFO, and the FIFO flags will never count to 0.
538 * When this happens, the residual byte should be retrieved
539 * via PIO following completion of the BLAST operation."
543 *p = READ_ESPREG(esc, NCR_FIFO);
547 if (trans < 0) { /* transferred < 0 ? */
550 * This situation can happen in perfectly normal operation
551 * if the ESP is reselected while using DMA to select
552 * another target. As such, don't print the warning.
554 device_printf(dev, "xfer (%d) > req (%d)\n", trans, dmasize);
559 NCR_DMA(("%s: tcl=%d, tcm=%d, tch=%d; trans=%d, resid=%d\n", __func__,
560 READ_ESPREG(esc, NCR_TCL), READ_ESPREG(esc, NCR_TCM),
561 READ_ESPREG(esc, NCR_TCH), trans, resid));
563 *esc->sc_dmalen -= trans;
564 *esc->sc_dmaaddr = (char *)*esc->sc_dmaaddr + trans;
570 esp_pci_dma_setup(struct ncr53c9x_softc *sc, void **addr, size_t *len,
571 int datain, size_t *dmasize)
573 struct esp_pci_softc *esc = (struct esp_pci_softc *)sc;
576 WRITE_DMAREG(esc, DMA_CMD, DMACMD_IDLE | (datain != 0 ? DMACMD_DIR :
579 *dmasize = esc->sc_dmasize = ulmin(*dmasize, MDL_SEG_SIZE);
580 esc->sc_dmaaddr = addr;
581 esc->sc_dmalen = len;
582 esc->sc_datain = datain;
585 * There's no need to set up DMA for a "Transfer Pad" operation.
590 /* Set the transfer length. */
591 WRITE_DMAREG(esc, DMA_STC, *dmasize);
594 * Load the transfer buffer and program the DMA address.
595 * Note that the NCR53C9x core can't handle EINPROGRESS so we set
598 error = bus_dmamap_load(esc->sc_xferdmat, esc->sc_xferdmam,
599 *esc->sc_dmaaddr, *dmasize, esp_pci_xfermap, sc, BUS_DMA_NOWAIT);
605 esp_pci_dma_go(struct ncr53c9x_softc *sc)
607 struct esp_pci_softc *esc = (struct esp_pci_softc *)sc;
610 datain = esc->sc_datain;
612 /* No DMA transfer for a "Transfer Pad" operation */
613 if (esc->sc_dmasize == 0)
616 /* Sync the transfer buffer. */
617 bus_dmamap_sync(esc->sc_xferdmat, esc->sc_xferdmam, datain != 0 ?
618 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
620 /* Set the DMA engine to the IDLE state. */
621 /* XXX DMA Transfer Interrupt Enable bit is broken? */
622 WRITE_DMAREG(esc, DMA_CMD, DMACMD_IDLE | /* DMACMD_INTE | */
623 (datain != 0 ? DMACMD_DIR : 0));
625 /* Issue a DMA start command. */
626 WRITE_DMAREG(esc, DMA_CMD, DMACMD_START | /* DMACMD_INTE | */
627 (datain != 0 ? DMACMD_DIR : 0));
633 esp_pci_dma_stop(struct ncr53c9x_softc *sc)
635 struct esp_pci_softc *esc = (struct esp_pci_softc *)sc;
638 /* XXX what should we do here ? */
639 WRITE_DMAREG(esc, DMA_CMD,
640 DMACMD_ABORT | (esc->sc_datain != 0 ? DMACMD_DIR : 0));
641 bus_dmamap_unload(esc->sc_xferdmat, esc->sc_xferdmam);
647 esp_pci_dma_isactive(struct ncr53c9x_softc *sc)
649 struct esp_pci_softc *esc = (struct esp_pci_softc *)sc;
651 /* XXX should we check esc->sc_active? */
652 if ((READ_DMAREG(esc, DMA_CMD) & DMACMD_CMD) != DMACMD_IDLE)