wdc_pcmcia.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* $OpenBSD: wdc_pcmcia.c,v 1.31 2014/09/14 14:17:25 jsg Exp $ */
  2. /* $NetBSD: wdc_pcmcia.c,v 1.19 1999/02/19 21:49:43 abs 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 Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
  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/kernel.h>
  34. #include <sys/conf.h>
  35. #include <sys/file.h>
  36. #include <sys/stat.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/buf.h>
  39. #include <sys/uio.h>
  40. #include <sys/malloc.h>
  41. #include <sys/device.h>
  42. #include <sys/disklabel.h>
  43. #include <sys/disk.h>
  44. #include <sys/syslog.h>
  45. #include <machine/cpu.h>
  46. #include <machine/intr.h>
  47. #include <machine/bus.h>
  48. #include <dev/pcmcia/pcmciareg.h>
  49. #include <dev/pcmcia/pcmciavar.h>
  50. #include <dev/pcmcia/pcmciadevs.h>
  51. #include <dev/ata/atavar.h>
  52. #include <dev/ic/wdcvar.h>
  53. #define WDC_PCMCIA_REG_NPORTS 8
  54. #define WDC_PCMCIA_AUXREG_OFFSET (WDC_PCMCIA_REG_NPORTS + 6)
  55. #define WDC_PCMCIA_AUXREG_NPORTS 2
  56. struct wdc_pcmcia_softc {
  57. struct wdc_softc sc_wdcdev;
  58. struct channel_softc *wdc_chanptr;
  59. struct channel_softc wdc_channel;
  60. struct pcmcia_io_handle sc_pioh;
  61. struct pcmcia_io_handle sc_auxpioh;
  62. int sc_iowindow;
  63. int sc_auxiowindow;
  64. void *sc_ih;
  65. struct pcmcia_function *sc_pf;
  66. int sc_flags;
  67. #define WDC_PCMCIA_ATTACH 0x0001
  68. };
  69. static int wdc_pcmcia_match(struct device *, void *, void *);
  70. static void wdc_pcmcia_attach(struct device *, struct device *, void *);
  71. int wdc_pcmcia_detach(struct device *, int);
  72. int wdc_pcmcia_activate(struct device *, int);
  73. struct cfattach wdc_pcmcia_ca = {
  74. sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach,
  75. wdc_pcmcia_detach, wdc_pcmcia_activate
  76. };
  77. struct wdc_pcmcia_product {
  78. u_int16_t wpp_vendor; /* vendor ID */
  79. u_int16_t wpp_product; /* product ID */
  80. int wpp_quirk_flag; /* Quirk flags */
  81. #define WDC_PCMCIA_FORCE_16BIT_IO 0x01 /* Don't use PCMCIA_WIDTH_AUTO */
  82. #define WDC_PCMCIA_NO_EXTRA_RESETS 0x02 /* Only reset ctrl once */
  83. const char *wpp_cis_info[4]; /* XXX necessary? */
  84. } wdc_pcmcia_pr[] = {
  85. { /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
  86. PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
  87. 0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL }, },
  88. { PCMCIA_VENDOR_IBM, PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
  89. 0, { NULL, "Portable CD-ROM Drive", NULL, NULL }, },
  90. { PCMCIA_VENDOR_HAGIWARASYSCOM, PCMCIA_PRODUCT_INVALID, /* XXX */
  91. WDC_PCMCIA_FORCE_16BIT_IO, { NULL, NULL, NULL, NULL }, },
  92. /* The TEAC IDE/Card II is used on the Sony Vaio */
  93. { PCMCIA_VENDOR_TEAC, PCMCIA_PRODUCT_TEAC_IDECARDII,
  94. WDC_PCMCIA_NO_EXTRA_RESETS, PCMCIA_CIS_TEAC_IDECARDII },
  95. /*
  96. * EXP IDE/ATAPI DVD Card use with some DVD players.
  97. * Does not have a vendor ID or product ID.
  98. */
  99. { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
  100. 0, PCMCIA_CIS_EXP_EXPMULTIMEDIA },
  101. /* Mobile Dock 2, which doesn't have vendor ID nor product ID */
  102. { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
  103. 0, PCMCIA_CIS_SHUTTLE_IDE_ATAPI },
  104. /* Archos MiniCD */
  105. { PCMCIA_VENDOR_ARCHOS, PCMCIA_PRODUCT_ARCHOS_ARC_ATAPI,
  106. 0, PCMCIA_CIS_ARCHOS_ARC_ATAPI },
  107. };
  108. struct wdc_pcmcia_disk_device_interface_args {
  109. int ddi_type; /* interface type */
  110. int ddi_reqfn; /* function we are requesting iftype */
  111. int ddi_curfn; /* function we are currently parsing in CIS */
  112. };
  113. int wdc_pcmcia_disk_device_interface_callback(struct pcmcia_tuple *,
  114. void *);
  115. int wdc_pcmcia_disk_device_interface(struct pcmcia_function *);
  116. struct wdc_pcmcia_product *
  117. wdc_pcmcia_lookup(struct pcmcia_attach_args *);
  118. int
  119. wdc_pcmcia_disk_device_interface_callback(tuple, arg)
  120. struct pcmcia_tuple *tuple;
  121. void *arg;
  122. {
  123. struct wdc_pcmcia_disk_device_interface_args *ddi = arg;
  124. switch (tuple->code) {
  125. case PCMCIA_CISTPL_FUNCID:
  126. ddi->ddi_curfn++;
  127. break;
  128. case PCMCIA_CISTPL_FUNCE:
  129. if (ddi->ddi_reqfn != ddi->ddi_curfn)
  130. break;
  131. /* subcode (disk device interface), data (interface type) */
  132. if (tuple->length < 2)
  133. break;
  134. /* check type */
  135. if (pcmcia_tuple_read_1(tuple, 0) !=
  136. PCMCIA_TPLFE_TYPE_DISK_DEVICE_INTERFACE)
  137. break;
  138. ddi->ddi_type = pcmcia_tuple_read_1(tuple, 1);
  139. return (1);
  140. }
  141. return (0);
  142. }
  143. int
  144. wdc_pcmcia_disk_device_interface(pf)
  145. struct pcmcia_function *pf;
  146. {
  147. struct wdc_pcmcia_disk_device_interface_args ddi;
  148. ddi.ddi_reqfn = pf->number;
  149. ddi.ddi_curfn = -1;
  150. if (pcmcia_scan_cis((struct device *)pf->sc,
  151. wdc_pcmcia_disk_device_interface_callback, &ddi) > 0)
  152. return (ddi.ddi_type);
  153. else
  154. return (-1);
  155. }
  156. struct wdc_pcmcia_product *
  157. wdc_pcmcia_lookup(pa)
  158. struct pcmcia_attach_args *pa;
  159. {
  160. struct wdc_pcmcia_product *wpp;
  161. int i, cis_match;
  162. for (wpp = wdc_pcmcia_pr;
  163. wpp < &wdc_pcmcia_pr[nitems(wdc_pcmcia_pr)];
  164. wpp++)
  165. if ((wpp->wpp_vendor == PCMCIA_VENDOR_INVALID ||
  166. pa->manufacturer == wpp->wpp_vendor) &&
  167. (wpp->wpp_product == PCMCIA_PRODUCT_INVALID ||
  168. pa->product == wpp->wpp_product)) {
  169. cis_match = 1;
  170. for (i = 0; i < 4; i++) {
  171. if (!(wpp->wpp_cis_info[i] == NULL ||
  172. (pa->card->cis1_info[i] != NULL &&
  173. strcmp(pa->card->cis1_info[i],
  174. wpp->wpp_cis_info[i]) == 0)))
  175. cis_match = 0;
  176. }
  177. if (cis_match)
  178. return (wpp);
  179. }
  180. return (NULL);
  181. }
  182. static int
  183. wdc_pcmcia_match(parent, match, aux)
  184. struct device *parent;
  185. void *match, *aux;
  186. {
  187. struct pcmcia_attach_args *pa = aux;
  188. struct pcmcia_softc *sc;
  189. int iftype;
  190. if (wdc_pcmcia_lookup(pa) != NULL)
  191. return (1);
  192. if (pa->pf->function == PCMCIA_FUNCTION_DISK) {
  193. sc = pa->pf->sc;
  194. pcmcia_chip_socket_enable(sc->pct, sc->pch);
  195. iftype = wdc_pcmcia_disk_device_interface(pa->pf);
  196. pcmcia_chip_socket_disable(sc->pct, sc->pch);
  197. if (iftype == PCMCIA_TPLFE_DDI_PCCARD_ATA)
  198. return (1);
  199. }
  200. return (0);
  201. }
  202. static void
  203. wdc_pcmcia_attach(parent, self, aux)
  204. struct device *parent;
  205. struct device *self;
  206. void *aux;
  207. {
  208. struct wdc_pcmcia_softc *sc = (void *)self;
  209. struct pcmcia_attach_args *pa = aux;
  210. struct pcmcia_config_entry *cfe;
  211. struct wdc_pcmcia_product *wpp;
  212. const char *intrstr;
  213. int quirks;
  214. sc->sc_pf = pa->pf;
  215. for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
  216. cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
  217. if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
  218. continue;
  219. if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
  220. cfe->iospace[0].length,
  221. cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
  222. &sc->sc_pioh))
  223. continue;
  224. if (cfe->num_iospace == 2) {
  225. if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
  226. cfe->iospace[1].length, 0, &sc->sc_auxpioh))
  227. break;
  228. } else /* num_iospace == 1 */ {
  229. sc->sc_auxpioh.iot = sc->sc_pioh.iot;
  230. if (!bus_space_subregion(sc->sc_pioh.iot,
  231. sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
  232. WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
  233. break;
  234. }
  235. pcmcia_io_free(pa->pf, &sc->sc_pioh);
  236. }
  237. if (cfe == NULL) {
  238. printf(": can't handle card info\n");
  239. goto no_config_entry;
  240. }
  241. /* Enable the card. */
  242. pcmcia_function_init(pa->pf, cfe);
  243. if (pcmcia_function_enable(pa->pf)) {
  244. printf(": function enable failed\n");
  245. goto enable_failed;
  246. }
  247. /*
  248. * XXX DEC Mobile Media CDROM is not yet tested whether it works
  249. * XXX with PCMCIA_WIDTH_IO16. HAGIWARA SYS-COM HPC-CF32 doesn't
  250. * XXX work with PCMCIA_WIDTH_AUTO.
  251. * XXX CANON FC-8M (SANDISK SDCFB 8M) works for both _AUTO and IO16.
  252. * XXX So, here is temporary work around.
  253. */
  254. wpp = wdc_pcmcia_lookup(pa);
  255. if (wpp != NULL)
  256. quirks = wpp->wpp_quirk_flag;
  257. else
  258. quirks = 0;
  259. if (pcmcia_io_map(pa->pf, quirks & WDC_PCMCIA_FORCE_16BIT_IO ?
  260. PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_AUTO, 0,
  261. sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
  262. printf(": can't map first i/o space\n");
  263. goto iomap_failed;
  264. }
  265. /*
  266. * Currently, # of iospace is 1 except DIGITAL Mobile Media CD-ROM.
  267. * So whether the work around like above is necessary or not
  268. * is unknown. XXX.
  269. */
  270. if (cfe->num_iospace <= 1)
  271. sc->sc_auxiowindow = -1;
  272. else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
  273. sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
  274. printf(": can't map second i/o space\n");
  275. goto iomapaux_failed;
  276. }
  277. printf(" port 0x%lx/%lu",
  278. sc->sc_pioh.addr, (u_long)sc->sc_pioh.size);
  279. if (cfe->num_iospace > 1 && sc->sc_auxpioh.size > 0)
  280. printf(",0x%lx/%lu",
  281. sc->sc_auxpioh.addr, (u_long)sc->sc_auxpioh.size);
  282. sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
  283. sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
  284. sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
  285. sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
  286. sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
  287. sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
  288. sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
  289. sc->sc_wdcdev.PIO_cap = 0;
  290. sc->wdc_chanptr = &sc->wdc_channel;
  291. sc->sc_wdcdev.channels = &sc->wdc_chanptr;
  292. sc->sc_wdcdev.nchannels = 1;
  293. sc->wdc_channel.channel = 0;
  294. sc->wdc_channel.wdc = &sc->sc_wdcdev;
  295. sc->wdc_channel.ch_queue = wdc_alloc_queue();
  296. if (sc->wdc_channel.ch_queue == NULL) {
  297. printf("cannot allocate channel queue\n");
  298. goto ch_queue_alloc_failed;
  299. }
  300. if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
  301. sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
  302. /* Establish the interrupt handler. */
  303. sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO, wdcintr,
  304. &sc->wdc_channel, sc->sc_wdcdev.sc_dev.dv_xname);
  305. intrstr = pcmcia_intr_string(sc->sc_pf, sc->sc_ih);
  306. if (*intrstr)
  307. printf(": %s", intrstr);
  308. printf("\n");
  309. sc->sc_flags |= WDC_PCMCIA_ATTACH;
  310. wdcattach(&sc->wdc_channel);
  311. wdc_print_current_modes(&sc->wdc_channel);
  312. sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
  313. return;
  314. ch_queue_alloc_failed:
  315. /* Unmap our aux i/o window. */
  316. if (sc->sc_auxiowindow != -1)
  317. pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
  318. iomapaux_failed:
  319. /* Unmap our i/o window. */
  320. pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
  321. iomap_failed:
  322. /* Disable the function */
  323. pcmcia_function_disable(sc->sc_pf);
  324. enable_failed:
  325. /* Unmap our i/o space. */
  326. pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
  327. if (cfe->num_iospace == 2)
  328. pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
  329. no_config_entry:
  330. sc->sc_iowindow = -1;
  331. }
  332. int
  333. wdc_pcmcia_detach(self, flags)
  334. struct device *self;
  335. int flags;
  336. {
  337. struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
  338. int error;
  339. if (sc->sc_iowindow == -1)
  340. /* Nothing to detach */
  341. return (0);
  342. if ((error = wdcdetach(&sc->wdc_channel, flags)) != 0)
  343. return (error);
  344. if (sc->wdc_channel.ch_queue != NULL)
  345. wdc_free_queue(sc->wdc_channel.ch_queue);
  346. /* Unmap our i/o window and i/o space. */
  347. pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
  348. pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
  349. if (sc->sc_auxiowindow != -1) {
  350. pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
  351. pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
  352. }
  353. return (0);
  354. }
  355. int
  356. wdc_pcmcia_activate(self, act)
  357. struct device *self;
  358. int act;
  359. {
  360. struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
  361. int rv = 0;
  362. if (sc->sc_iowindow == -1)
  363. /* Nothing to activate/deactivate. */
  364. return (0);
  365. switch (act) {
  366. case DVACT_DEACTIVATE:
  367. rv = config_activate_children(self, act);
  368. if (sc->sc_ih)
  369. pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
  370. sc->sc_ih = NULL;
  371. pcmcia_function_disable(sc->sc_pf);
  372. break;
  373. case DVACT_RESUME:
  374. pcmcia_function_enable(sc->sc_pf);
  375. sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
  376. wdcintr, &sc->wdc_channel, sc->sc_wdcdev.sc_dev.dv_xname);
  377. wdcreset(&sc->wdc_channel, VERBOSE);
  378. rv = config_activate_children(self, act);
  379. break;
  380. case DVACT_POWERDOWN:
  381. rv = config_activate_children(self, act);
  382. if (sc->sc_ih)
  383. pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
  384. sc->sc_ih = NULL;
  385. pcmcia_function_disable(sc->sc_pf);
  386. break;
  387. default:
  388. rv = config_activate_children(self, act);
  389. break;
  390. }
  391. return (rv);
  392. }