Deleted Added
sdiff udiff text old ( 226381 ) new ( 226947 )
full compact
1/*-
2 * Copyright (c) 2004 Scott Long
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 43 unchanged lines hidden (view full) ---

52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 * POSSIBILITY OF SUCH DAMAGE.
57 */
58
59#include <sys/cdefs.h>
60__FBSDID("$FreeBSD: head/sys/sparc64/sbus/lsi64854.c 226381 2011-10-15 09:29:43Z marius $");
61
62#include <sys/param.h>
63#include <sys/systm.h>
64#include <sys/bus.h>
65#include <sys/kernel.h>
66#include <sys/lock.h>
67#include <sys/mutex.h>
68#include <sys/rman.h>

--- 20 unchanged lines hidden (view full) ---

89 do { \
90 if ((lsi64854debug & (a)) != 0) \
91 printf x; \
92 } while (/* CONSTCOND */0)
93#else
94#define DPRINTF(a,x)
95#endif
96
97#define MAX_DMA_SZ (16*1024*1024)
98
99static void lsi64854_reset(struct lsi64854_softc *);
100static void lsi64854_map_scsi(void *, bus_dma_segment_t *, int, int);
101static int lsi64854_setup(struct lsi64854_softc *, void **, size_t *,
102 int, size_t *);
103static int lsi64854_scsi_intr(void *);
104static int lsi64854_enet_intr(void *);
105static int lsi64854_setup_pp(struct lsi64854_softc *, void **,

--- 14 unchanged lines hidden (view full) ---

120 bus_dma_lock_t *lockfunc;
121 struct ncr53c9x_softc *nsc;
122 void *lockfuncarg;
123 uint32_t csr;
124 int error;
125
126 lockfunc = NULL;
127 lockfuncarg = NULL;
128
129 switch (sc->sc_channel) {
130 case L64854_CHANNEL_SCSI:
131 nsc = sc->sc_client;
132 if (NCR_LOCK_INITIALIZED(nsc) == 0) {
133 device_printf(sc->sc_dev, "mutex not initialized\n");
134 return (ENXIO);
135 }
136 lockfunc = busdma_lock_mutex;
137 lockfuncarg = &nsc->sc_lock;
138 sc->intr = lsi64854_scsi_intr;
139 sc->setup = lsi64854_setup;
140 break;
141 case L64854_CHANNEL_ENET:
142 sc->intr = lsi64854_enet_intr;
143 break;
144 case L64854_CHANNEL_PP:
145 sc->intr = lsi64854_pp_intr;
146 sc->setup = lsi64854_setup_pp;
147 break;
148 default:
149 device_printf(sc->sc_dev, "unknown channel\n");
150 }
151 sc->reset = lsi64854_reset;
152
153 if (sc->setup != NULL) {
154 error = bus_dma_tag_create(
155 sc->sc_parent_dmat, /* parent */
156 1, 0, /* alignment, boundary */
157 BUS_SPACE_MAXADDR, /* lowaddr */
158 BUS_SPACE_MAXADDR, /* highaddr */
159 NULL, NULL, /* filter, filterarg */
160 MAX_DMA_SZ, /* maxsize */
161 1, /* nsegments */
162 MAX_DMA_SZ, /* maxsegsize */
163 BUS_DMA_ALLOCNOW, /* flags */
164 lockfunc, lockfuncarg, /* lockfunc, lockfuncarg */
165 &sc->sc_buffer_dmat);
166 if (error != 0) {
167 device_printf(sc->sc_dev,
168 "cannot allocate buffer DMA tag\n");
169 return (error);
170 }

--- 74 unchanged lines hidden (view full) ---

245 uint32_t csr; \
246 /* \
247 * DMA rev0 & rev1: we are not allowed to touch the DMA "flush" \
248 * and "drain" bits while it is still thinking about a \
249 * request. \
250 * other revs: D_ESC_R_PEND bit reads as 0 \
251 */ \
252 DMAWAIT(sc, L64854_GCSR(sc) & D_ESC_R_PEND, "R_PEND", dontpanic);\
253 if (sc->sc_rev != DMAREV_HME) { \
254 /* \
255 * Select drain bit based on revision \
256 * also clears errors and D_TC flag \
257 */ \
258 csr = L64854_GCSR(sc); \
259 if (sc->sc_rev == DMAREV_1 || sc->sc_rev == DMAREV_0) \
260 csr |= D_ESC_DRAIN; \
261 else \
262 csr |= L64854_INVALIDATE; \
263 \
264 L64854_SCSR(sc,csr); \
265 } \
266 /* \
267 * Wait for draining to finish \
268 * rev0 & rev1 call this PACKCNT \
269 */ \
270 DMAWAIT(sc, L64854_GCSR(sc) & L64854_DRAINING, "DRAINING", dontpanic);\
271} while (/* CONSTCOND */0)
272
273#define DMA_FLUSH(sc, dontpanic) do { \
274 uint32_t csr; \
275 /* \
276 * DMA rev0 & rev1: we are not allowed to touch the DMA "flush" \
277 * and "drain" bits while it is still thinking about a \
278 * request. \
279 * other revs: D_ESC_R_PEND bit reads as 0 \
280 */ \
281 DMAWAIT(sc, L64854_GCSR(sc) & D_ESC_R_PEND, "R_PEND", dontpanic);\
282 csr = L64854_GCSR(sc); \
283 csr &= ~(L64854_WRITE|L64854_EN_DMA); /* no-ops on ENET */ \
284 csr |= L64854_INVALIDATE; /* XXX FAS ? */ \
285 L64854_SCSR(sc,csr); \
286} while (/* CONSTCOND */0)
287
288static void
289lsi64854_reset(struct lsi64854_softc *sc)
290{
291 uint32_t csr;
292
293 DMA_FLUSH(sc, 1);
294 csr = L64854_GCSR(sc);
295
296 DPRINTF(LDB_ANY, ("%s: csr 0x%x\n", __func__, csr));
297
298 if (sc->sc_dmasize != 0) {
299 bus_dmamap_sync(sc->sc_buffer_dmat, sc->sc_dmamap,
300 (csr & D_WRITE) != 0 ? BUS_DMASYNC_PREREAD :
301 BUS_DMASYNC_PREWRITE);
302 bus_dmamap_unload(sc->sc_buffer_dmat, sc->sc_dmamap);
303 }
304
305 if (sc->sc_rev == DMAREV_HME)
306 L64854_SCSR(sc, csr | D_HW_RESET_FAS366);
307
308 csr |= L64854_RESET; /* reset DMA */
309 L64854_SCSR(sc, csr);
310 DELAY(200); /* > 10 Sbus clocks(?) */

--- 48 unchanged lines hidden (view full) ---

359
360static void
361lsi64854_map_scsi(void *arg, bus_dma_segment_t *segs, int nseg, int error)
362{
363 struct lsi64854_softc *sc;
364
365 sc = (struct lsi64854_softc *)arg;
366
367 if (nseg != 1)
368 panic("%s: cannot map %d segments\n", __func__, nseg);
369
370 bus_dmamap_sync(sc->sc_buffer_dmat, sc->sc_dmamap,
371 sc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
372 bus_write_4(sc->sc_res, L64854_REG_ADDR, segs[0].ds_addr);
373}
374
375#define DMAMAX(a) (MAX_DMA_SZ - ((a) & (MAX_DMA_SZ - 1)))
376/*
377 * setup a DMA transfer
378 */
379static int
380lsi64854_setup(struct lsi64854_softc *sc, void **addr, size_t *len,
381 int datain, size_t *dmasize)
382{
383 long bcnt;
384 uint32_t csr;
385
386 DMA_FLUSH(sc, 0);
387
388#if 0
389 DMACSR(sc) &= ~D_INT_EN;
390#endif
391 sc->sc_dmaaddr = addr;
392 sc->sc_dmalen = len;
393 sc->sc_datain = datain;
394
395 /*
396 * The rules say we cannot transfer more than the limit
397 * of this DMA chip (64k for old and 16Mb for new),
398 * and we cannot cross a 16Mb boundary.
399 */
400 *dmasize = sc->sc_dmasize =
401 ulmin(*dmasize, DMAMAX((size_t)*sc->sc_dmaaddr));
402
403 DPRINTF(LDB_ANY, ("%s: dmasize=%ld\n", __func__, (long)sc->sc_dmasize));
404
405 /*
406 * XXX what length?
407 */
408 if (sc->sc_rev == DMAREV_HME) {
409 L64854_SCSR(sc, sc->sc_dmactl | L64854_RESET);
410 L64854_SCSR(sc, sc->sc_dmactl);
411
412 bus_write_4(sc->sc_res, L64854_REG_CNT, *dmasize);
413 }
414
415 /* Program the DMA address */
416 if (sc->sc_dmasize != 0)
417 if (bus_dmamap_load(sc->sc_buffer_dmat, sc->sc_dmamap,
418 *sc->sc_dmaaddr, sc->sc_dmasize, lsi64854_map_scsi, sc, 0))
419 panic("%s: cannot allocate DVMA address", __func__);
420
421 if (sc->sc_rev == DMAREV_ESC) {
422 /* DMA ESC chip bug work-around */
423 bcnt = sc->sc_dmasize;
424 if (((bcnt + (long)*sc->sc_dmaaddr) & PAGE_MASK_8K) != 0)
425 bcnt = roundup(bcnt, PAGE_SIZE_8K);
426 bus_write_4(sc->sc_res, L64854_REG_CNT, bcnt);
427 }
428
429 /* Setup DMA control register */
430 csr = L64854_GCSR(sc);
431
432 if (datain)
433 csr |= L64854_WRITE;
434 else
435 csr &= ~L64854_WRITE;
436 csr |= L64854_INT_EN;
437
438 if (sc->sc_rev == DMAREV_HME)
439 csr |= (D_DSBL_SCSI_DRN | D_EN_DMA);
440

--- 9 unchanged lines hidden (view full) ---

450 *
451 * return 1 if it was a DMA continue.
452 */
453static int
454lsi64854_scsi_intr(void *arg)
455{
456 struct lsi64854_softc *sc = arg;
457 struct ncr53c9x_softc *nsc = sc->sc_client;
458 int trans, resid;
459 uint32_t csr;
460
461 csr = L64854_GCSR(sc);
462
463 DPRINTF(LDB_SCSI, ("%s: addr 0x%x, csr %b\n", __func__,
464 bus_read_4(sc->sc_res, L64854_REG_ADDR), csr, DDMACSR_BITS));
465
466 if (csr & (D_ERR_PEND|D_SLAVE_ERR)) {
467 device_printf(sc->sc_dev, "error: csr=%b\n", csr, DDMACSR_BITS);
468 csr &= ~D_EN_DMA; /* Stop DMA */
469 /* Invalidate the queue; SLAVE_ERR bit is write-to-clear */
470 csr |= D_INVALIDATE|D_SLAVE_ERR;
471 L64854_SCSR(sc, csr);
472 return (-1);
473 }
474
475 /* This is an "assertion" :) */
476 if (sc->sc_active == 0)
477 panic("%s: DMA wasn't active", __func__);
478
479 DMA_DRAIN(sc, 0);
480
481 /* DMA has stopped */
482 csr &= ~D_EN_DMA;
483 L64854_SCSR(sc, csr);
484 sc->sc_active = 0;
485
486 if (sc->sc_dmasize == 0) {
487 /* A "Transfer Pad" operation completed */
488 DPRINTF(LDB_SCSI, ("%s: discarded %d bytes (tcl=%d, tcm=%d)\n",
489 __func__, NCR_READ_REG(nsc, NCR_TCL) |
490 (NCR_READ_REG(nsc, NCR_TCM) << 8),
491 NCR_READ_REG(nsc, NCR_TCL), NCR_READ_REG(nsc, NCR_TCM)));
492 return (0);
493 }
494
495 resid = 0;
496 /*
497 * If a transfer onto the SCSI bus gets interrupted by the device
498 * (e.g. for a SAVEPOINTER message), the data in the FIFO counts
499 * as residual since the NCR53C9X counter registers get decremented
500 * as bytes are clocked into the FIFO.
501 */
502 if (!(csr & D_WRITE) &&
503 (resid = (NCR_READ_REG(nsc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
504 DPRINTF(LDB_SCSI, ("%s: empty esp FIFO of %d ", __func__,
505 resid));
506 if (nsc->sc_rev == NCR_VARIANT_FAS366 &&
507 (NCR_READ_REG(nsc, NCR_CFG3) & NCRFASCFG3_EWIDE))
508 resid <<= 1;
509 }
510
511 if ((nsc->sc_espstat & NCRSTAT_TC) == 0) {
512 /*
513 * `Terminal count' is off, so read the residue
514 * out of the NCR53C9X counter registers.
515 */
516 resid += (NCR_READ_REG(nsc, NCR_TCL) |
517 (NCR_READ_REG(nsc, NCR_TCM) << 8) |
518 ((nsc->sc_cfg2 & NCRCFG2_FE) ?
519 (NCR_READ_REG(nsc, NCR_TCH) << 16) : 0));
520
521 if (resid == 0 && sc->sc_dmasize == 65536 &&
522 (nsc->sc_cfg2 & NCRCFG2_FE) == 0)
523 /* A transfer of 64K is encoded as `TCL=TCM=0' */
524 resid = 65536;
525 }
526
527 trans = sc->sc_dmasize - resid;
528 if (trans < 0) { /* transferred < 0? */
529#if 0
530 /*
531 * This situation can happen in perfectly normal operation
532 * if the ESP is reselected while using DMA to select
533 * another target. As such, don't print the warning.
534 */
535 device_printf(sc->sc_dev, "xfer (%d) > req (%d)\n", trans,
536 sc->sc_dmasize);
537#endif
538 trans = sc->sc_dmasize;
539 }
540
541 DPRINTF(LDB_SCSI, ("%s: tcl=%d, tcm=%d, tch=%d; trans=%d, resid=%d\n",
542 __func__, NCR_READ_REG(nsc, NCR_TCL), NCR_READ_REG(nsc, NCR_TCM),
543 (nsc->sc_cfg2 & NCRCFG2_FE) ? NCR_READ_REG(nsc, NCR_TCH) : 0,
544 trans, resid));
545
546 if (sc->sc_dmasize != 0) {
547 bus_dmamap_sync(sc->sc_buffer_dmat, sc->sc_dmamap,
548 (csr & D_WRITE) != 0 ? BUS_DMASYNC_POSTREAD :
549 BUS_DMASYNC_POSTWRITE);
550 bus_dmamap_unload(sc->sc_buffer_dmat, sc->sc_dmamap);
551 }
552
553 *sc->sc_dmalen -= trans;
554 *sc->sc_dmaaddr = (char *)*sc->sc_dmaaddr + trans;
555
556#if 0 /* this is not normal operation just yet */
557 if (*sc->sc_dmalen == 0 || nsc->sc_phase != nsc->sc_prevphase)
558 return (0);
559
560 /* and again */
561 dma_start(sc, sc->sc_dmaaddr, sc->sc_dmalen, DMACSR(sc) & D_WRITE);
562 return (1);
563#endif
564 return (0);
565}
566
567/*
568 * Pseudo (chained) interrupt to le driver to handle DMA errors.
569 */
570static int
571lsi64854_enet_intr(void *arg)
572{
573 struct lsi64854_softc *sc = arg;
574 uint32_t csr;
575 int i, rv;
576
577 csr = L64854_GCSR(sc);
578
579 /* If the DMA logic shows an interrupt, claim it */
580 rv = ((csr & E_INT_PEND) != 0) ? 1 : 0;
581
582 if (csr & (E_ERR_PEND|E_SLAVE_ERR)) {
583 device_printf(sc->sc_dev, "error: csr=%b\n", csr, EDMACSR_BITS);
584 csr &= ~L64854_EN_DMA; /* Stop DMA */
585 /* Invalidate the queue; SLAVE_ERR bit is write-to-clear */
586 csr |= E_INVALIDATE|E_SLAVE_ERR;
587 L64854_SCSR(sc, csr);
588 /* Will be drained with the LE_C0_IDON interrupt. */
589 sc->sc_dodrain = 1;
590 return (-1);
591 }
592
593 /* XXX - is this necessary with E_DSBL_WR_INVAL on? */
594 if (sc->sc_dodrain) {

--- 10 unchanged lines hidden (view full) ---

605
606static void
607lsi64854_map_pp(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
608{
609 struct lsi64854_softc *sc;
610
611 sc = (struct lsi64854_softc *)arg;
612
613 if (nsegs != 1)
614 panic("%s: cannot map %d segments\n", __func__, nsegs);
615
616 bus_dmamap_sync(sc->sc_buffer_dmat, sc->sc_dmamap, sc->sc_datain ?
617 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
618 bus_write_4(sc->sc_res, L64854_REG_ADDR, segs[0].ds_addr);
619
620 bus_write_4(sc->sc_res, L64854_REG_CNT, sc->sc_dmasize);
621}
622
623/*
624 * setup a DMA transfer
625 */
626static int
627lsi64854_setup_pp(struct lsi64854_softc *sc, void **addr, size_t *len,
628 int datain, size_t *dmasize)
629{
630 uint32_t csr;
631
632 DMA_FLUSH(sc, 0);
633
634 sc->sc_dmaaddr = addr;
635 sc->sc_dmalen = len;
636 sc->sc_datain = datain;
637
638 DPRINTF(LDB_PP, ("%s: pp start %ld@%p,%d\n", __func__,
639 (long)*sc->sc_dmalen, *sc->sc_dmaaddr, datain ? 1 : 0));
640
641 /*
642 * the rules say we cannot transfer more than the limit
643 * of this DMA chip (64k for old and 16Mb for new),
644 * and we cannot cross a 16Mb boundary.
645 */
646 *dmasize = sc->sc_dmasize =
647 ulmin(*dmasize, DMAMAX((size_t)*sc->sc_dmaaddr));
648
649 DPRINTF(LDB_PP, ("%s: dmasize=%ld\n", __func__, (long)sc->sc_dmasize));
650
651 /* Program the DMA address */
652 if (sc->sc_dmasize != 0)
653 if (bus_dmamap_load(sc->sc_buffer_dmat, sc->sc_dmamap,
654 *sc->sc_dmaaddr, sc->sc_dmasize, lsi64854_map_pp, sc, 0))
655 panic("%s: pp cannot allocate DVMA address", __func__);
656
657 /* Setup DMA control register */
658 csr = L64854_GCSR(sc);
659 csr &= ~L64854_BURST_SIZE;
660 if (sc->sc_burst == 32)
661 csr |= L64854_BURST_32;
662 else if (sc->sc_burst == 16)
663 csr |= L64854_BURST_16;
664 else
665 csr |= L64854_BURST_0;
666 csr |= P_EN_DMA|P_INT_EN|P_EN_CNT;
667#if 0
668 /* This bit is read-only in PP csr register */
669 if (datain)
670 csr |= P_WRITE;
671 else
672 csr &= ~P_WRITE;
673#endif
674 L64854_SCSR(sc, csr);
675
676 return (0);
677}
678
679/*
680 * Parallel port DMA interrupt.
681 */
682static int
683lsi64854_pp_intr(void *arg)
684{
685 struct lsi64854_softc *sc = arg;
686 int ret, trans, resid = 0;
687 uint32_t csr;
688
689 csr = L64854_GCSR(sc);
690
691 DPRINTF(LDB_PP, ("%s: addr 0x%x, csr %b\n", __func__,
692 bus_read_4(sc->sc_res, L64854_REG_ADDR), csr, PDMACSR_BITS));
693
694 if (csr & (P_ERR_PEND|P_SLAVE_ERR)) {
695 resid = bus_read_4(sc->sc_res, L64854_REG_CNT);
696 device_printf(sc->sc_dev, "error: resid %d csr=%b\n", resid,
697 csr, PDMACSR_BITS);
698 csr &= ~P_EN_DMA; /* Stop DMA */
699 /* Invalidate the queue; SLAVE_ERR bit is write-to-clear */
700 csr |= P_INVALIDATE|P_SLAVE_ERR;
701 L64854_SCSR(sc, csr);
702 return (-1);
703 }
704
705 ret = (csr & P_INT_PEND) != 0;
706
707 if (sc->sc_active != 0) {
708 DMA_DRAIN(sc, 0);
709 resid = bus_read_4(sc->sc_res, L64854_REG_CNT);
710 }
711
712 /* DMA has stopped */
713 csr &= ~D_EN_DMA;
714 L64854_SCSR(sc, csr);
715 sc->sc_active = 0;
716
717 trans = sc->sc_dmasize - resid;
718 if (trans < 0) /* transferred < 0? */
719 trans = sc->sc_dmasize;
720 *sc->sc_dmalen -= trans;
721 *sc->sc_dmaaddr = (char *)*sc->sc_dmaaddr + trans;
722
723 if (sc->sc_dmasize != 0) {
724 bus_dmamap_sync(sc->sc_buffer_dmat, sc->sc_dmamap,
725 (csr & D_WRITE) != 0 ? BUS_DMASYNC_POSTREAD :
726 BUS_DMASYNC_POSTWRITE);
727 bus_dmamap_unload(sc->sc_buffer_dmat, sc->sc_dmamap);
728 }
729
730 return (ret != 0);
731}