asc_tc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* $OpenBSD: asc_tc.c,v 1.12 2014/01/18 22:33:59 dlg Exp $ */
  2. /* $NetBSD: asc_tc.c,v 1.19 2001/11/15 09:48:19 lukem Exp $ */
  3. /*-
  4. * Copyright (c) 2000 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Tohru Nishimura.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  21. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  23. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/device.h>
  34. #include <sys/buf.h>
  35. #include <scsi/scsi_all.h>
  36. #include <scsi/scsiconf.h>
  37. #include <scsi/scsi_message.h>
  38. #include <machine/bus.h>
  39. #include <dev/ic/ncr53c9xreg.h>
  40. #include <dev/ic/ncr53c9xvar.h>
  41. #include <dev/tc/ascvar.h>
  42. #include <dev/tc/tcvar.h>
  43. struct asc_tc_softc {
  44. struct asc_softc asc;
  45. /* XXX XXX XXX */
  46. caddr_t sc_base, sc_bounce, sc_target;
  47. };
  48. int asc_tc_match(struct device *, void *, void *);
  49. void asc_tc_attach(struct device *, struct device *, void *);
  50. struct cfattach asc_tc_ca = {
  51. sizeof(struct asc_tc_softc), asc_tc_match, asc_tc_attach
  52. };
  53. int asc_dma_isintr(struct ncr53c9x_softc *);
  54. void asc_tc_reset(struct ncr53c9x_softc *);
  55. int asc_tc_intr(struct ncr53c9x_softc *);
  56. int asc_tc_setup(struct ncr53c9x_softc *, caddr_t *,
  57. size_t *, int, size_t *);
  58. void asc_tc_go(struct ncr53c9x_softc *);
  59. void asc_tc_stop(struct ncr53c9x_softc *);
  60. int asc_dma_isactive(struct ncr53c9x_softc *);
  61. void asc_clear_latched_intr(struct ncr53c9x_softc *);
  62. struct ncr53c9x_glue asc_tc_glue = {
  63. asc_read_reg,
  64. asc_write_reg,
  65. asc_dma_isintr,
  66. asc_tc_reset,
  67. asc_tc_intr,
  68. asc_tc_setup,
  69. asc_tc_go,
  70. asc_tc_stop,
  71. asc_dma_isactive,
  72. asc_clear_latched_intr,
  73. };
  74. /*
  75. * Parameters specific to PMAZ-A TC option card.
  76. */
  77. #define PMAZ_OFFSET_53C94 0x0 /* from module base */
  78. #define PMAZ_OFFSET_DMAR 0x40000 /* DMA Address Register */
  79. #define PMAZ_OFFSET_RAM 0x80000 /* 128KB SRAM buffer */
  80. #define PMAZ_OFFSET_ROM 0xc0000 /* diagnostic ROM */
  81. #define PMAZ_RAM_SIZE 0x20000 /* 128k (32k*32) */
  82. #define PER_TGT_DMA_SIZE ((PMAZ_RAM_SIZE/7) & ~(sizeof(int)-1))
  83. #define PMAZ_DMAR_WRITE 0x80000000 /* DMA direction bit */
  84. #define PMAZ_DMAR_MASK 0x1ffff /* 17 bits, 128k */
  85. #define PMAZ_DMA_ADDR(x) ((unsigned long)(x) & PMAZ_DMAR_MASK)
  86. int
  87. asc_tc_match(parent, cfdata, aux)
  88. struct device *parent;
  89. void *cfdata, *aux;
  90. {
  91. struct tc_attach_args *d = aux;
  92. if (strncmp("PMAZ-AA ", d->ta_modname, TC_ROM_LLEN))
  93. return (0);
  94. return (1);
  95. }
  96. void
  97. asc_tc_attach(parent, self, aux)
  98. struct device *parent, *self;
  99. void *aux;
  100. {
  101. struct tc_attach_args *ta = aux;
  102. struct asc_tc_softc *asc = (struct asc_tc_softc *)self;
  103. struct ncr53c9x_softc *sc = &asc->asc.sc_ncr53c9x;
  104. /*
  105. * Set up glue for MI code early; we use some of it here.
  106. */
  107. sc->sc_glue = &asc_tc_glue;
  108. asc->asc.sc_bst = ta->ta_memt;
  109. asc->asc.sc_dmat = ta->ta_dmat;
  110. if (bus_space_map(asc->asc.sc_bst, ta->ta_addr,
  111. PMAZ_OFFSET_RAM + PMAZ_RAM_SIZE, 0, &asc->asc.sc_bsh)) {
  112. printf("%s: unable to map device\n", sc->sc_dev.dv_xname);
  113. return;
  114. }
  115. asc->sc_base = (caddr_t)ta->ta_addr; /* XXX XXX XXX */
  116. tc_intr_establish(parent, ta->ta_cookie, IPL_BIO, ncr53c9x_intr, sc,
  117. self->dv_xname);
  118. sc->sc_id = 7;
  119. sc->sc_freq = TC_SPEED_TO_KHZ(ta->ta_busspeed); /* in kHz so far */
  120. /*
  121. * XXX More of this should be in ncr53c9x_attach(), but
  122. * XXX should we really poke around the chip that much in
  123. * XXX the MI code? Think about this more...
  124. */
  125. /*
  126. * Set up static configuration info.
  127. */
  128. sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
  129. sc->sc_cfg2 = NCRCFG2_SCSI2;
  130. sc->sc_cfg3 = 0;
  131. sc->sc_rev = NCR_VARIANT_NCR53C94;
  132. /*
  133. * XXX minsync and maxxfer _should_ be set up in MI code,
  134. * XXX but it appears to have some dependency on what sort
  135. * XXX of DMA we're hooked up to, etc.
  136. */
  137. /*
  138. * This is the value used to start sync negotiations
  139. * Note that the NCR register "SYNCTP" is programmed
  140. * in "clocks per byte", and has a minimum value of 4.
  141. * The SCSI period used in negotiation is one-fourth
  142. * of the time (in nanoseconds) needed to transfer one byte.
  143. * Since the chip's clock is given in kHz, we have the following
  144. * formula: 4 * period = (1000000 / freq) * 4
  145. */
  146. sc->sc_minsync = (1000000 / sc->sc_freq) * 5 / 4;
  147. sc->sc_maxxfer = 64 * 1024;
  148. /* convert sc_freq to MHz */
  149. sc->sc_freq /= 1000;
  150. /* Do the common parts of attachment. */
  151. ncr53c9x_attach(sc);
  152. }
  153. void
  154. asc_tc_reset(sc)
  155. struct ncr53c9x_softc *sc;
  156. {
  157. struct asc_tc_softc *asc = (struct asc_tc_softc *)sc;
  158. asc->asc.sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
  159. }
  160. int
  161. asc_tc_intr(sc)
  162. struct ncr53c9x_softc *sc;
  163. {
  164. struct asc_tc_softc *asc = (struct asc_tc_softc *)sc;
  165. int trans, resid;
  166. resid = 0;
  167. if ((asc->asc.sc_flags & ASC_ISPULLUP) == 0 &&
  168. (resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
  169. NCR_DMA(("asc_tc_intr: empty FIFO of %d ", resid));
  170. DELAY(1);
  171. }
  172. resid += NCR_READ_REG(sc, NCR_TCL);
  173. resid += NCR_READ_REG(sc, NCR_TCM) << 8;
  174. trans = asc->asc.sc_dmasize - resid;
  175. if (asc->asc.sc_flags & ASC_ISPULLUP)
  176. memcpy(asc->sc_target, asc->sc_bounce, trans);
  177. *asc->asc.sc_dmalen -= trans;
  178. *asc->asc.sc_dmaaddr += trans;
  179. asc->asc.sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
  180. return (0);
  181. }
  182. int
  183. asc_tc_setup(sc, addr, len, datain, dmasize)
  184. struct ncr53c9x_softc *sc;
  185. caddr_t *addr;
  186. size_t *len;
  187. int datain;
  188. size_t *dmasize;
  189. {
  190. struct asc_tc_softc *asc = (struct asc_tc_softc *)sc;
  191. u_int32_t tc_dmar;
  192. size_t size;
  193. asc->asc.sc_dmaaddr = addr;
  194. asc->asc.sc_dmalen = len;
  195. asc->asc.sc_flags = (datain) ? ASC_ISPULLUP : 0;
  196. NCR_DMA(("asc_tc_setup: start %ld@%p, %s\n", (long)*asc->asc.sc_dmalen,
  197. *asc->asc.sc_dmaaddr, datain ? "IN" : "OUT"));
  198. size = *dmasize;
  199. if (size > PER_TGT_DMA_SIZE)
  200. size = PER_TGT_DMA_SIZE;
  201. *dmasize = asc->asc.sc_dmasize = size;
  202. NCR_DMA(("asc_tc_setup: dmasize = %ld\n", (long)asc->asc.sc_dmasize));
  203. asc->sc_bounce = asc->sc_base + PMAZ_OFFSET_RAM;
  204. asc->sc_bounce += PER_TGT_DMA_SIZE *
  205. sc->sc_nexus->xs->sc_link->target;
  206. asc->sc_target = *addr;
  207. if ((asc->asc.sc_flags & ASC_ISPULLUP) == 0)
  208. memcpy(asc->sc_bounce, asc->sc_target, size);
  209. #if 1
  210. if (asc->asc.sc_flags & ASC_ISPULLUP)
  211. tc_dmar = PMAZ_DMA_ADDR(asc->sc_bounce);
  212. else
  213. tc_dmar = PMAZ_DMAR_WRITE | PMAZ_DMA_ADDR(asc->sc_bounce);
  214. bus_space_write_4(asc->asc.sc_bst, asc->asc.sc_bsh, PMAZ_OFFSET_DMAR,
  215. tc_dmar);
  216. asc->asc.sc_flags |= ASC_MAPLOADED|ASC_DMAACTIVE;
  217. #endif
  218. return (0);
  219. }
  220. void
  221. asc_tc_go(sc)
  222. struct ncr53c9x_softc *sc;
  223. {
  224. #if 0
  225. struct asc_tc_softc *asc = (struct asc_tc_softc *)sc;
  226. u_int32_t tc_dmar;
  227. if (asc->asc.sc_flags & ASC_ISPULLUP)
  228. tc_dmar = PMAZ_DMA_ADDR(asc->sc_bounce);
  229. else
  230. tc_dmar = PMAZ_DMAR_WRITE | PMAZ_DMA_ADDR(asc->sc_bounce);
  231. bus_space_write_4(asc->asc.sc_bst, asc->asc.sc_bsh, PMAZ_OFFSET_DMAR,
  232. tc_dmar);
  233. asc->asc.sc_flags |= ASC_DMAACTIVE;
  234. #endif
  235. }
  236. /* NEVER CALLED BY MI 53C9x ENGINE INDEED */
  237. void
  238. asc_tc_stop(sc)
  239. struct ncr53c9x_softc *sc;
  240. {
  241. #if 0
  242. struct asc_tc_softc *asc = (struct asc_tc_softc *)sc;
  243. if (asc->asc.sc_flags & ASC_ISPULLUP)
  244. memcpy(asc->sc_target, asc->sc_bounce, asc->sc_dmasize);
  245. asc->asc.sc_flags &= ~ASC_DMAACTIVE;
  246. #endif
  247. }
  248. /*
  249. * Glue functions.
  250. */
  251. int
  252. asc_dma_isintr(sc)
  253. struct ncr53c9x_softc *sc;
  254. {
  255. return !!(NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT);
  256. }
  257. int
  258. asc_dma_isactive(sc)
  259. struct ncr53c9x_softc *sc;
  260. {
  261. struct asc_tc_softc *asc = (struct asc_tc_softc *)sc;
  262. return !!(asc->asc.sc_flags & ASC_DMAACTIVE);
  263. }
  264. void
  265. asc_clear_latched_intr(sc)
  266. struct ncr53c9x_softc *sc;
  267. {
  268. }