asc_tcds.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* $OpenBSD: asc_tcds.c,v 1.8 2014/01/18 22:33:59 dlg Exp $ */
  2. /* $NetBSD: asc_tcds.c,v 1.5 2001/11/15 09:48:19 lukem Exp $ */
  3. /*-
  4. * Copyright (c) 1998 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
  9. * NASA Ames Research Center.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. * Copyright (c) 1994 Peter Galbavy. All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions
  37. * are met:
  38. * 1. Redistributions of source code must retain the above copyright
  39. * notice, this list of conditions and the following disclaimer.
  40. * 2. Redistributions in binary form must reproduce the above copyright
  41. * notice, this list of conditions and the following disclaimer in the
  42. * documentation and/or other materials provided with the distribution.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  45. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  46. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  47. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  48. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  49. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  53. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include <sys/param.h>
  56. #include <sys/systm.h>
  57. #include <sys/device.h>
  58. #include <sys/buf.h>
  59. #include <scsi/scsi_all.h>
  60. #include <scsi/scsiconf.h>
  61. #include <dev/ic/ncr53c9xreg.h>
  62. #include <dev/ic/ncr53c9xvar.h>
  63. #include <dev/tc/ascvar.h>
  64. #include <machine/bus.h>
  65. #include <dev/tc/tcvar.h>
  66. #include <dev/tc/tcdsreg.h>
  67. #include <dev/tc/tcdsvar.h>
  68. struct asc_tcds_softc {
  69. struct asc_softc asc;
  70. struct tcds_slotconfig *sc_tcds;
  71. };
  72. int asc_tcds_match (struct device *, void *, void *);
  73. void asc_tcds_attach(struct device *, struct device *, void *);
  74. /* Linkup to the rest of the kernel */
  75. struct cfattach asc_tcds_ca = {
  76. sizeof(struct asc_tcds_softc), asc_tcds_match, asc_tcds_attach
  77. };
  78. /*
  79. * Functions and the switch for the MI code.
  80. */
  81. int tcds_dma_isintr(struct ncr53c9x_softc *);
  82. void tcds_dma_reset(struct ncr53c9x_softc *);
  83. int tcds_dma_intr(struct ncr53c9x_softc *);
  84. int tcds_dma_setup(struct ncr53c9x_softc *, caddr_t *,
  85. size_t *, int, size_t *);
  86. void tcds_dma_go(struct ncr53c9x_softc *);
  87. void tcds_dma_stop(struct ncr53c9x_softc *);
  88. int tcds_dma_isactive(struct ncr53c9x_softc *);
  89. void tcds_clear_latched_intr(struct ncr53c9x_softc *);
  90. struct ncr53c9x_glue asc_tcds_glue = {
  91. asc_read_reg,
  92. asc_write_reg,
  93. tcds_dma_isintr,
  94. tcds_dma_reset,
  95. tcds_dma_intr,
  96. tcds_dma_setup,
  97. tcds_dma_go,
  98. tcds_dma_stop,
  99. tcds_dma_isactive,
  100. tcds_clear_latched_intr,
  101. };
  102. int
  103. asc_tcds_match(parent, cf, aux)
  104. struct device *parent;
  105. void *cf, *aux;
  106. {
  107. /* We always exist. */
  108. return 1;
  109. }
  110. #define DMAMAX(a) (NBPG - ((a) & (NBPG - 1)))
  111. /*
  112. * Attach this instance, and then all the sub-devices
  113. */
  114. void
  115. asc_tcds_attach(parent, self, aux)
  116. struct device *parent, *self;
  117. void *aux;
  118. {
  119. struct tcdsdev_attach_args *tcdsdev = aux;
  120. struct asc_tcds_softc *asc = (struct asc_tcds_softc *)self;
  121. struct ncr53c9x_softc *sc = &asc->asc.sc_ncr53c9x;
  122. int error;
  123. /*
  124. * Set up glue for MI code early; we use some of it here.
  125. */
  126. sc->sc_glue = &asc_tcds_glue;
  127. asc->asc.sc_bst = tcdsdev->tcdsda_bst;
  128. asc->asc.sc_bsh = tcdsdev->tcdsda_bsh;
  129. asc->sc_tcds = tcdsdev->tcdsda_sc;
  130. /*
  131. * The TCDS ASIC cannot DMA across 8k boundaries, and this
  132. * driver is written such that each DMA segment gets a new
  133. * call to tcds_dma_setup(). Thus, the DMA map only needs
  134. * to support 8k transfers.
  135. */
  136. asc->asc.sc_dmat = tcdsdev->tcdsda_dmat;
  137. if ((error = bus_dmamap_create(asc->asc.sc_dmat, NBPG, 1, NBPG,
  138. NBPG, BUS_DMA_NOWAIT, &asc->asc.sc_dmamap)) < 0) {
  139. printf("failed to create dma map, error = %d\n", error);
  140. }
  141. sc->sc_id = tcdsdev->tcdsda_id;
  142. sc->sc_freq = tcdsdev->tcdsda_freq;
  143. /* gimme MHz */
  144. sc->sc_freq /= 1000000;
  145. tcds_intr_establish(parent, tcdsdev->tcdsda_chip, ncr53c9x_intr, sc,
  146. self->dv_xname);
  147. /*
  148. * XXX More of this should be in ncr53c9x_attach(), but
  149. * XXX should we really poke around the chip that much in
  150. * XXX the MI code? Think about this more...
  151. */
  152. /*
  153. * Set up static configuration info.
  154. */
  155. sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
  156. sc->sc_cfg2 = NCRCFG2_SCSI2;
  157. sc->sc_cfg3 = NCRCFG3_CDB;
  158. if (sc->sc_freq > 25)
  159. sc->sc_cfg3 |= NCRF9XCFG3_FCLK;
  160. sc->sc_rev = tcdsdev->tcdsda_variant;
  161. if (tcdsdev->tcdsda_fast) {
  162. sc->sc_features |= NCR_F_FASTSCSI;
  163. sc->sc_cfg3_fscsi = NCRF9XCFG3_FSCSI;
  164. }
  165. /*
  166. * XXX minsync and maxxfer _should_ be set up in MI code,
  167. * XXX but it appears to have some dependency on what sort
  168. * XXX of DMA we're hooked up to, etc.
  169. */
  170. /*
  171. * This is the value used to start sync negotiations
  172. * Note that the NCR register "SYNCTP" is programmed
  173. * in "clocks per byte", and has a minimum value of 4.
  174. * The SCSI period used in negotiation is one-fourth
  175. * of the time (in nanoseconds) needed to transfer one byte.
  176. * Since the chip's clock is given in MHz, we have the following
  177. * formula: 4 * period = (1000 / freq) * 4
  178. */
  179. sc->sc_minsync = (1000 / sc->sc_freq) * tcdsdev->tcdsda_period / 4;
  180. sc->sc_maxxfer = 64 * 1024;
  181. /* Do the common parts of attachment. */
  182. ncr53c9x_attach(sc);
  183. }
  184. void
  185. tcds_dma_reset(sc)
  186. struct ncr53c9x_softc *sc;
  187. {
  188. struct asc_tcds_softc *asc = (struct asc_tcds_softc *)sc;
  189. /* TCDS SCSI disable/reset/enable. */
  190. tcds_scsi_reset(asc->sc_tcds); /* XXX */
  191. if (asc->asc.sc_flags & ASC_MAPLOADED)
  192. bus_dmamap_unload(asc->asc.sc_dmat, asc->asc.sc_dmamap);
  193. asc->asc.sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
  194. }
  195. /*
  196. * start a dma transfer or keep it going
  197. */
  198. int
  199. tcds_dma_setup(sc, addr, len, ispullup, dmasize)
  200. struct ncr53c9x_softc *sc;
  201. caddr_t *addr;
  202. size_t *len, *dmasize;
  203. int ispullup; /* DMA into main memory */
  204. {
  205. struct asc_tcds_softc *asc = (struct asc_tcds_softc *)sc;
  206. struct tcds_slotconfig *tcds = asc->sc_tcds;
  207. size_t size;
  208. u_int32_t dic;
  209. NCR_DMA(("tcds_dma %d: start %d@%p,%s\n", tcds->sc_slot,
  210. (int)*asc->asc.sc_dmalen, *asc->asc.sc_dmaaddr,
  211. (ispullup) ? "IN" : "OUT"));
  212. /*
  213. * the rules say we cannot transfer more than the limit
  214. * of this DMA chip (64k) and we cannot cross a 8k boundary.
  215. */
  216. size = min(*dmasize, DMAMAX((size_t)*addr));
  217. asc->asc.sc_dmaaddr = addr;
  218. asc->asc.sc_dmalen = len;
  219. asc->asc.sc_flags = (ispullup) ? ASC_ISPULLUP : 0;
  220. *dmasize = asc->asc.sc_dmasize = size;
  221. NCR_DMA(("dma_start: dmasize = %d\n", (int)size));
  222. if (size == 0)
  223. return 0;
  224. if (bus_dmamap_load(asc->asc.sc_dmat, asc->asc.sc_dmamap, *addr, size,
  225. NULL, BUS_DMA_NOWAIT | (ispullup ? BUS_DMA_READ : BUS_DMA_WRITE))) {
  226. /*
  227. * XXX Should return an error, here, but the upper-layer
  228. * XXX doesn't check the return value!
  229. */
  230. panic("tcds_dma_setup: dmamap load failed");
  231. }
  232. /* synchronize dmamap contents with memory image */
  233. bus_dmamap_sync(asc->asc.sc_dmat, asc->asc.sc_dmamap, 0, size,
  234. (ispullup) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
  235. /* load address, set/clear unaligned transfer and read/write bits. */
  236. bus_space_write_4(tcds->sc_bst, tcds->sc_bsh, tcds->sc_sda,
  237. asc->asc.sc_dmamap->dm_segs[0].ds_addr >> 2);
  238. dic = bus_space_read_4(tcds->sc_bst, tcds->sc_bsh, tcds->sc_dic);
  239. dic &= ~TCDS_DIC_ADDRMASK;
  240. dic |= asc->asc.sc_dmamap->dm_segs[0].ds_addr & TCDS_DIC_ADDRMASK;
  241. if (ispullup)
  242. dic |= TCDS_DIC_WRITE;
  243. else
  244. dic &= ~TCDS_DIC_WRITE;
  245. bus_space_write_4(tcds->sc_bst, tcds->sc_bsh, tcds->sc_dic, dic);
  246. asc->asc.sc_flags |= ASC_MAPLOADED;
  247. return 0;
  248. }
  249. void
  250. tcds_dma_go(sc)
  251. struct ncr53c9x_softc *sc;
  252. {
  253. struct asc_tcds_softc *asc = (struct asc_tcds_softc *)sc;
  254. /* mark unit as DMA-active */
  255. asc->asc.sc_flags |= ASC_DMAACTIVE;
  256. /* start DMA */
  257. tcds_dma_enable(asc->sc_tcds, 1);
  258. }
  259. void
  260. tcds_dma_stop(sc)
  261. struct ncr53c9x_softc *sc;
  262. {
  263. #if 0
  264. struct asc_tcds_softc *asc = (struct asc_tcds_softc *)sc;
  265. #endif
  266. /*
  267. * XXX STOP DMA HERE!
  268. */
  269. }
  270. /*
  271. * Pseudo (chained) interrupt from the asc driver to kick the
  272. * current running DMA transfer. Called from ncr53c9x_intr()
  273. * for now.
  274. *
  275. * return 1 if it was a DMA continue.
  276. */
  277. int
  278. tcds_dma_intr(sc)
  279. struct ncr53c9x_softc *sc;
  280. {
  281. struct asc_tcds_softc *asc = (struct asc_tcds_softc *)sc;
  282. struct tcds_slotconfig *tcds = asc->sc_tcds;
  283. int trans, resid;
  284. u_int32_t tcl, tcm;
  285. u_int32_t dud, dudmask, *addr;
  286. bus_addr_t pa;
  287. NCR_DMA(("tcds_dma %d: intr", tcds->sc_slot));
  288. if (tcds_scsi_iserr(tcds))
  289. return 0;
  290. /* This is an "assertion" :) */
  291. if ((asc->asc.sc_flags & ASC_DMAACTIVE) == 0)
  292. panic("tcds_dma_intr: DMA wasn't active");
  293. /* DMA has stopped */
  294. tcds_dma_enable(tcds, 0);
  295. asc->asc.sc_flags &= ~ASC_DMAACTIVE;
  296. if (asc->asc.sc_dmasize == 0) {
  297. /* A "Transfer Pad" operation completed */
  298. tcl = NCR_READ_REG(sc, NCR_TCL);
  299. tcm = NCR_READ_REG(sc, NCR_TCM);
  300. NCR_DMA(("dma_intr: discarded %d bytes (tcl=%d, tcm=%d)\n",
  301. tcl | (tcm << 8), tcl, tcm));
  302. return 0;
  303. }
  304. resid = 0;
  305. if ((asc->asc.sc_flags & ASC_ISPULLUP) == 0 &&
  306. (resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
  307. NCR_DMA(("dma_intr: empty esp FIFO of %d ", resid));
  308. DELAY(1);
  309. }
  310. resid += (tcl = NCR_READ_REG(sc, NCR_TCL));
  311. resid += (tcm = NCR_READ_REG(sc, NCR_TCM)) << 8;
  312. trans = asc->asc.sc_dmasize - resid;
  313. if (trans < 0) { /* transferred < 0 ? */
  314. printf("tcds_dma %d: xfer (%d) > req (%d)\n",
  315. tcds->sc_slot, trans, (int)asc->asc.sc_dmasize);
  316. trans = asc->asc.sc_dmasize;
  317. }
  318. NCR_DMA(("dma_intr: tcl=%d, tcm=%d; trans=%d, resid=%d\n",
  319. tcl, tcm, trans, resid));
  320. *asc->asc.sc_dmalen -= trans;
  321. *asc->asc.sc_dmaaddr += trans;
  322. bus_dmamap_sync(asc->asc.sc_dmat, asc->asc.sc_dmamap,
  323. 0, asc->asc.sc_dmamap->dm_mapsize,
  324. (asc->asc.sc_flags & ASC_ISPULLUP)
  325. ? BUS_DMASYNC_POSTREAD
  326. : BUS_DMASYNC_POSTWRITE);
  327. /*
  328. * Clean up unaligned DMAs into main memory.
  329. */
  330. if (asc->asc.sc_flags & ASC_ISPULLUP) {
  331. /* Handle unaligned starting address, length. */
  332. dud = bus_space_read_4(tcds->sc_bst,
  333. tcds->sc_bsh, tcds->sc_dud0);
  334. if ((dud & TCDS_DUD0_VALIDBITS) != 0) {
  335. addr = (u_int32_t *)
  336. ((paddr_t)*asc->asc.sc_dmaaddr & ~0x3);
  337. dudmask = 0;
  338. if (dud & TCDS_DUD0_VALID00)
  339. panic("tcds_dma: dud0 byte 0 valid");
  340. if (dud & TCDS_DUD0_VALID01)
  341. dudmask |= TCDS_DUD_BYTE01;
  342. if (dud & TCDS_DUD0_VALID10)
  343. dudmask |= TCDS_DUD_BYTE10;
  344. #ifdef DIAGNOSTIC
  345. if (dud & TCDS_DUD0_VALID11)
  346. dudmask |= TCDS_DUD_BYTE11;
  347. #endif
  348. NCR_DMA(("dud0 at %p dudmask 0x%x\n",
  349. addr, dudmask));
  350. *addr = (*addr & ~dudmask) | (dud & dudmask);
  351. }
  352. dud = bus_space_read_4(tcds->sc_bst,
  353. tcds->sc_bsh, tcds->sc_dud1);
  354. if ((dud & TCDS_DUD1_VALIDBITS) != 0) {
  355. pa = bus_space_read_4(tcds->sc_bst, tcds->sc_bsh,
  356. tcds->sc_sda) << 2;
  357. dudmask = 0;
  358. if (dud & TCDS_DUD1_VALID00)
  359. dudmask |= TCDS_DUD_BYTE00;
  360. if (dud & TCDS_DUD1_VALID01)
  361. dudmask |= TCDS_DUD_BYTE01;
  362. if (dud & TCDS_DUD1_VALID10)
  363. dudmask |= TCDS_DUD_BYTE10;
  364. #ifdef DIAGNOSTIC
  365. if (dud & TCDS_DUD1_VALID11)
  366. panic("tcds_dma: dud1 byte 3 valid");
  367. #endif
  368. NCR_DMA(("dud1 at 0x%lx dudmask 0x%x\n",
  369. pa, dudmask));
  370. /* XXX Fix TC_PHYS_TO_UNCACHED() */
  371. #if defined(__alpha__)
  372. addr = (u_int32_t *)ALPHA_PHYS_TO_K0SEG(pa);
  373. #elif defined(__mips__)
  374. addr = (u_int32_t *)MIPS_PHYS_TO_KSEG1(pa);
  375. #else
  376. #error TURBOchannel only exists on DECs, folks...
  377. #endif
  378. *addr = (*addr & ~dudmask) | (dud & dudmask);
  379. }
  380. /* XXX deal with saved residual byte? */
  381. }
  382. bus_dmamap_unload(asc->asc.sc_dmat, asc->asc.sc_dmamap);
  383. asc->asc.sc_flags &= ~ASC_MAPLOADED;
  384. return 0;
  385. }
  386. /*
  387. * Glue functions.
  388. */
  389. int
  390. tcds_dma_isintr(sc)
  391. struct ncr53c9x_softc *sc;
  392. {
  393. struct asc_tcds_softc *asc = (struct asc_tcds_softc *)sc;
  394. int x;
  395. x = tcds_scsi_isintr(asc->sc_tcds, 1);
  396. /* XXX */
  397. return x;
  398. }
  399. int
  400. tcds_dma_isactive(sc)
  401. struct ncr53c9x_softc *sc;
  402. {
  403. struct asc_tcds_softc *asc = (struct asc_tcds_softc *)sc;
  404. return !!(asc->asc.sc_flags & ASC_DMAACTIVE);
  405. }
  406. void
  407. tcds_clear_latched_intr(sc)
  408. struct ncr53c9x_softc *sc;
  409. {
  410. struct asc_tcds_softc *asc = (struct asc_tcds_softc *)sc;
  411. /* Clear the TCDS interrupt bit. */
  412. (void)tcds_scsi_isintr(asc->sc_tcds, 1);
  413. }