esp_pcmcia.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* $OpenBSD: esp_pcmcia.c,v 1.11 2014/01/18 22:33:59 dlg Exp $ */
  2. /* $NetBSD: esp_pcmcia.c,v 1.8 2000/06/05 15:36:45 tsutsui 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 Charles M. Hannum.
  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 <machine/bus.h>
  36. #include <machine/intr.h>
  37. #include <scsi/scsi_all.h>
  38. #include <scsi/scsiconf.h>
  39. #include <dev/pcmcia/pcmciareg.h>
  40. #include <dev/pcmcia/pcmciavar.h>
  41. #include <dev/pcmcia/pcmciadevs.h>
  42. #include <dev/ic/ncr53c9xreg.h>
  43. #include <dev/ic/ncr53c9xvar.h>
  44. struct esp_pcmcia_softc {
  45. struct ncr53c9x_softc sc_ncr53c9x; /* glue to MI code */
  46. int sc_active; /* Pseudo-DMA state vars */
  47. int sc_tc;
  48. int sc_datain;
  49. size_t sc_dmasize;
  50. size_t sc_dmatrans;
  51. char **sc_dmaaddr;
  52. size_t *sc_pdmalen;
  53. /* PCMCIA-specific goo. */
  54. struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
  55. int sc_io_window; /* our i/o window */
  56. struct pcmcia_function *sc_pf; /* our PCMCIA function */
  57. void *sc_ih; /* interrupt handler */
  58. #ifdef ESP_PCMCIA_POLL
  59. struct callout sc_poll_ch;
  60. #endif
  61. int sc_flags;
  62. #define ESP_PCMCIA_ATTACHED 1 /* attach completed */
  63. #define ESP_PCMCIA_ATTACHING 2 /* attach in progress */
  64. };
  65. int esp_pcmcia_match(struct device *, void *, void *);
  66. void esp_pcmcia_attach(struct device *, struct device *, void *);
  67. void esp_pcmcia_init(struct esp_pcmcia_softc *);
  68. int esp_pcmcia_detach(struct device *, int);
  69. int esp_pcmcia_enable(void *, int);
  70. struct cfattach esp_pcmcia_ca = {
  71. sizeof(struct esp_pcmcia_softc), esp_pcmcia_match, esp_pcmcia_attach
  72. };
  73. /*
  74. * Functions and the switch for the MI code.
  75. */
  76. #ifdef ESP_PCMCIA_POLL
  77. void esp_pcmcia_poll(void *);
  78. #endif
  79. u_char esp_pcmcia_read_reg(struct ncr53c9x_softc *, int);
  80. void esp_pcmcia_write_reg(struct ncr53c9x_softc *, int, u_char);
  81. int esp_pcmcia_dma_isintr(struct ncr53c9x_softc *);
  82. void esp_pcmcia_dma_reset(struct ncr53c9x_softc *);
  83. int esp_pcmcia_dma_intr(struct ncr53c9x_softc *);
  84. int esp_pcmcia_dma_setup(struct ncr53c9x_softc *, caddr_t *,
  85. size_t *, int, size_t *);
  86. void esp_pcmcia_dma_go(struct ncr53c9x_softc *);
  87. void esp_pcmcia_dma_stop(struct ncr53c9x_softc *);
  88. int esp_pcmcia_dma_isactive(struct ncr53c9x_softc *);
  89. struct ncr53c9x_glue esp_pcmcia_glue = {
  90. esp_pcmcia_read_reg,
  91. esp_pcmcia_write_reg,
  92. esp_pcmcia_dma_isintr,
  93. esp_pcmcia_dma_reset,
  94. esp_pcmcia_dma_intr,
  95. esp_pcmcia_dma_setup,
  96. esp_pcmcia_dma_go,
  97. esp_pcmcia_dma_stop,
  98. esp_pcmcia_dma_isactive,
  99. NULL, /* gl_clear_latched_intr */
  100. };
  101. struct esp_pcmcia_product {
  102. u_int16_t app_vendor; /* PCMCIA vendor ID */
  103. u_int16_t app_product; /* PCMCIA product ID */
  104. int app_expfunc; /* expected function number */
  105. } esp_pcmcia_prod[] = {
  106. { PCMCIA_VENDOR_PANASONIC, PCMCIA_PRODUCT_PANASONIC_KXLC002, 0 },
  107. { PCMCIA_VENDOR_PANASONIC, PCMCIA_PRODUCT_PANASONIC_KME, 0 },
  108. { PCMCIA_VENDOR_NEWMEDIA2, PCMCIA_PRODUCT_NEWMEDIA2_BUSTOASTER, 0 }
  109. };
  110. int
  111. esp_pcmcia_match(parent, match, aux)
  112. struct device *parent;
  113. void *match, *aux;
  114. {
  115. struct pcmcia_attach_args *pa = aux;
  116. int i;
  117. for (i = 0; i < nitems(esp_pcmcia_prod); i++)
  118. if (pa->manufacturer == esp_pcmcia_prod[i].app_vendor &&
  119. pa->product == esp_pcmcia_prod[i].app_product &&
  120. pa->pf->number == esp_pcmcia_prod[i].app_expfunc)
  121. return (1);
  122. return (0);
  123. }
  124. void
  125. esp_pcmcia_attach(parent, self, aux)
  126. struct device *parent, *self;
  127. void *aux;
  128. {
  129. struct esp_pcmcia_softc *esc = (void *)self;
  130. struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
  131. struct pcmcia_attach_args *pa = aux;
  132. struct pcmcia_config_entry *cfe;
  133. struct pcmcia_function *pf = pa->pf;
  134. const char *intrstr;
  135. esc->sc_pf = pf;
  136. for (cfe = SIMPLEQ_FIRST(&pf->cfe_head); cfe != NULL;
  137. cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
  138. if (cfe->num_memspace != 0 ||
  139. cfe->num_iospace != 1)
  140. continue;
  141. if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
  142. cfe->iospace[0].length, 0, &esc->sc_pcioh) == 0)
  143. break;
  144. }
  145. if (cfe == 0) {
  146. printf(": can't alloc i/o space\n");
  147. goto no_config_entry;
  148. }
  149. /* Enable the card. */
  150. pcmcia_function_init(pf, cfe);
  151. if (pcmcia_function_enable(pf)) {
  152. printf(": function enable failed\n");
  153. goto enable_failed;
  154. }
  155. /* Map in the I/O space */
  156. if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0, esc->sc_pcioh.size,
  157. &esc->sc_pcioh, &esc->sc_io_window)) {
  158. printf(": can't map i/o space\n");
  159. goto iomap_failed;
  160. }
  161. printf(" port 0x%lx/%lu", esc->sc_pcioh.addr,
  162. (u_long)esc->sc_pcioh.size);
  163. esp_pcmcia_init(esc);
  164. esc->sc_ih = pcmcia_intr_establish(esc->sc_pf, IPL_BIO,
  165. ncr53c9x_intr, &esc->sc_ncr53c9x, sc->sc_dev.dv_xname);
  166. intrstr = pcmcia_intr_string(esc->sc_pf, esc->sc_ih);
  167. if (esc->sc_ih == NULL) {
  168. printf(", %s\n", intrstr);
  169. goto iomap_failed;
  170. }
  171. if (*intrstr)
  172. printf(", %s", intrstr);
  173. /*
  174. * Initialize nca board itself.
  175. */
  176. esc->sc_flags |= ESP_PCMCIA_ATTACHING;
  177. ncr53c9x_attach(sc);
  178. esc->sc_flags &= ~ESP_PCMCIA_ATTACHING;
  179. esc->sc_flags |= ESP_PCMCIA_ATTACHED;
  180. return;
  181. iomap_failed:
  182. /* Disable the device. */
  183. pcmcia_function_disable(esc->sc_pf);
  184. enable_failed:
  185. /* Unmap our I/O space. */
  186. pcmcia_io_free(esc->sc_pf, &esc->sc_pcioh);
  187. no_config_entry:
  188. return;
  189. }
  190. void
  191. esp_pcmcia_init(esc)
  192. struct esp_pcmcia_softc *esc;
  193. {
  194. struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
  195. bus_space_tag_t iot = esc->sc_pcioh.iot;
  196. bus_space_handle_t ioh = esc->sc_pcioh.ioh;
  197. /* id 7, clock 40M, parity ON, sync OFF, fast ON, slow ON */
  198. sc->sc_glue = &esp_pcmcia_glue;
  199. #ifdef ESP_PCMCIA_POLL
  200. callout_init(&esc->sc_poll_ch);
  201. #endif
  202. sc->sc_rev = NCR_VARIANT_ESP406;
  203. sc->sc_id = 7;
  204. sc->sc_freq = 40;
  205. /* try -PARENB -SLOW */
  206. sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB | NCRCFG1_SLOW;
  207. /* try +FE */
  208. sc->sc_cfg2 = NCRCFG2_SCSI2;
  209. /* try -IDM -FSCSI -FCLK */
  210. sc->sc_cfg3 = NCRESPCFG3_CDB | NCRESPCFG3_FCLK | NCRESPCFG3_IDM |
  211. NCRESPCFG3_FSCSI;
  212. sc->sc_cfg4 = NCRCFG4_ACTNEG;
  213. /* try +INTP */
  214. sc->sc_cfg5 = NCRCFG5_CRS1 | NCRCFG5_AADDR | NCRCFG5_PTRINC;
  215. sc->sc_minsync = 0;
  216. sc->sc_maxxfer = 64 * 1024;
  217. bus_space_write_1(iot, ioh, NCR_CFG5, sc->sc_cfg5);
  218. bus_space_write_1(iot, ioh, NCR_PIOI, 0);
  219. bus_space_write_1(iot, ioh, NCR_PSTAT, 0);
  220. bus_space_write_1(iot, ioh, 0x09, 0x24);
  221. bus_space_write_1(iot, ioh, NCR_CFG4, sc->sc_cfg4);
  222. }
  223. #ifdef notyet
  224. int
  225. esp_pcmcia_detach(self, flags)
  226. struct device *self;
  227. int flags;
  228. {
  229. struct esp_pcmcia_softc *esc = (void *)self;
  230. int error;
  231. if ((esc->sc_flags & ESP_PCMCIA_ATTACHED) == 0) {
  232. /* Nothing to detach. */
  233. return (0);
  234. }
  235. error = ncr53c9x_detach(&esc->sc_ncr53c9x, flags);
  236. if (error)
  237. return (error);
  238. /* Unmap our i/o window and i/o space. */
  239. pcmcia_io_unmap(esc->sc_pf, esc->sc_io_window);
  240. pcmcia_io_free(esc->sc_pf, &esc->sc_pcioh);
  241. return (0);
  242. }
  243. #endif
  244. int
  245. esp_pcmcia_enable(arg, onoff)
  246. void *arg;
  247. int onoff;
  248. {
  249. struct esp_pcmcia_softc *esc = arg;
  250. if (onoff) {
  251. #ifdef ESP_PCMCIA_POLL
  252. callout_reset(&esc->sc_poll_ch, 1, esp_pcmcia_poll, esc);
  253. #else
  254. /* Establish the interrupt handler. */
  255. esc->sc_ih = pcmcia_intr_establish(esc->sc_pf, IPL_BIO,
  256. ncr53c9x_intr, &esc->sc_ncr53c9x,
  257. esc->sc_ncr53c9x.sc_dev.dv_xname);
  258. if (esc->sc_ih == NULL) {
  259. printf("%s: couldn't establish interrupt handler\n",
  260. esc->sc_ncr53c9x.sc_dev.dv_xname);
  261. return (EIO);
  262. }
  263. #endif
  264. /*
  265. * If attach is in progress, we know that card power is
  266. * enabled and chip will be initialized later.
  267. * Otherwise, enable and reset now.
  268. */
  269. if ((esc->sc_flags & ESP_PCMCIA_ATTACHING) == 0) {
  270. if (pcmcia_function_enable(esc->sc_pf)) {
  271. printf("%s: couldn't enable PCMCIA function\n",
  272. esc->sc_ncr53c9x.sc_dev.dv_xname);
  273. pcmcia_intr_disestablish(esc->sc_pf,
  274. esc->sc_ih);
  275. return (EIO);
  276. }
  277. /* Initialize only chip. */
  278. ncr53c9x_init(&esc->sc_ncr53c9x, 0);
  279. }
  280. } else {
  281. pcmcia_function_disable(esc->sc_pf);
  282. #ifdef ESP_PCMCIA_POLL
  283. callout_stop(&esc->sc_poll_ch);
  284. #else
  285. pcmcia_intr_disestablish(esc->sc_pf, esc->sc_ih);
  286. #endif
  287. }
  288. return (0);
  289. }
  290. #ifdef ESP_PCMCIA_POLL
  291. void
  292. esp_pcmcia_poll(arg)
  293. void *arg;
  294. {
  295. struct esp_pcmcia_softc *esc = arg;
  296. (void) ncr53c9x_intr(&esc->sc_ncr53c9x);
  297. callout_reset(&esc->sc_poll_ch, 1, esp_pcmcia_poll, esc);
  298. }
  299. #endif
  300. /*
  301. * Glue functions.
  302. */
  303. u_char
  304. esp_pcmcia_read_reg(sc, reg)
  305. struct ncr53c9x_softc *sc;
  306. int reg;
  307. {
  308. struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
  309. u_char v;
  310. v = bus_space_read_1(esc->sc_pcioh.iot, esc->sc_pcioh.ioh, reg);
  311. return v;
  312. }
  313. void
  314. esp_pcmcia_write_reg(sc, reg, val)
  315. struct ncr53c9x_softc *sc;
  316. int reg;
  317. u_char val;
  318. {
  319. struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
  320. u_char v = val;
  321. if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA))
  322. v = NCRCMD_TRANS;
  323. bus_space_write_1(esc->sc_pcioh.iot, esc->sc_pcioh.ioh, reg, v);
  324. }
  325. int
  326. esp_pcmcia_dma_isintr(sc)
  327. struct ncr53c9x_softc *sc;
  328. {
  329. return NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT;
  330. }
  331. void
  332. esp_pcmcia_dma_reset(sc)
  333. struct ncr53c9x_softc *sc;
  334. {
  335. struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
  336. esc->sc_active = 0;
  337. esc->sc_tc = 0;
  338. }
  339. int
  340. esp_pcmcia_dma_intr(sc)
  341. struct ncr53c9x_softc *sc;
  342. {
  343. struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
  344. u_char *p;
  345. u_int espphase, espstat, espintr;
  346. int cnt;
  347. if (esc->sc_active == 0) {
  348. printf("%s: dma_intr--inactive DMA\n", sc->sc_dev.dv_xname);
  349. return -1;
  350. }
  351. if ((sc->sc_espintr & NCRINTR_BS) == 0) {
  352. esc->sc_active = 0;
  353. return 0;
  354. }
  355. cnt = *esc->sc_pdmalen;
  356. if (*esc->sc_pdmalen == 0) {
  357. printf("%s: data interrupt, but no count left\n",
  358. sc->sc_dev.dv_xname);
  359. }
  360. p = *esc->sc_dmaaddr;
  361. espphase = sc->sc_phase;
  362. espstat = (u_int) sc->sc_espstat;
  363. espintr = (u_int) sc->sc_espintr;
  364. do {
  365. if (esc->sc_datain) {
  366. *p++ = NCR_READ_REG(sc, NCR_FIFO);
  367. cnt--;
  368. if (espphase == DATA_IN_PHASE)
  369. NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
  370. else
  371. esc->sc_active = 0;
  372. } else {
  373. if (espphase == DATA_OUT_PHASE ||
  374. espphase == MESSAGE_OUT_PHASE) {
  375. NCR_WRITE_REG(sc, NCR_FIFO, *p++);
  376. cnt--;
  377. NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
  378. } else
  379. esc->sc_active = 0;
  380. }
  381. if (esc->sc_active) {
  382. while (!(NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT));
  383. espstat = NCR_READ_REG(sc, NCR_STAT);
  384. espintr = NCR_READ_REG(sc, NCR_INTR);
  385. espphase = (espintr & NCRINTR_DIS)
  386. ? /* Disconnected */ BUSFREE_PHASE
  387. : espstat & PHASE_MASK;
  388. }
  389. } while (esc->sc_active && espintr);
  390. sc->sc_phase = espphase;
  391. sc->sc_espstat = (u_char) espstat;
  392. sc->sc_espintr = (u_char) espintr;
  393. *esc->sc_dmaaddr = p;
  394. *esc->sc_pdmalen = cnt;
  395. if (*esc->sc_pdmalen == 0)
  396. esc->sc_tc = NCRSTAT_TC;
  397. sc->sc_espstat |= esc->sc_tc;
  398. return 0;
  399. }
  400. int
  401. esp_pcmcia_dma_setup(sc, addr, len, datain, dmasize)
  402. struct ncr53c9x_softc *sc;
  403. caddr_t *addr;
  404. size_t *len;
  405. int datain;
  406. size_t *dmasize;
  407. {
  408. struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
  409. esc->sc_dmaaddr = addr;
  410. esc->sc_pdmalen = len;
  411. esc->sc_datain = datain;
  412. esc->sc_dmasize = *dmasize;
  413. esc->sc_tc = 0;
  414. return 0;
  415. }
  416. void
  417. esp_pcmcia_dma_go(sc)
  418. struct ncr53c9x_softc *sc;
  419. {
  420. struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
  421. esc->sc_active = 1;
  422. }
  423. void
  424. esp_pcmcia_dma_stop(sc)
  425. struct ncr53c9x_softc *sc;
  426. {
  427. }
  428. int
  429. esp_pcmcia_dma_isactive(sc)
  430. struct ncr53c9x_softc *sc;
  431. {
  432. struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
  433. return (esc->sc_active);
  434. }