pata_pcmcia.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pata_pcmcia.c - PCMCIA PATA controller driver.
  4. * Copyright 2005-2006 Red Hat Inc, all rights reserved.
  5. * PCMCIA ident update Copyright 2006 Marcin Juszkiewicz
  6. * <openembedded@hrw.one.pl>
  7. *
  8. * Heavily based upon ide-cs.c
  9. * The initial developer of the original code is David A. Hinds
  10. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  11. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/delay.h>
  17. #include <linux/slab.h>
  18. #include <scsi/scsi_host.h>
  19. #include <linux/ata.h>
  20. #include <linux/libata.h>
  21. #include <pcmcia/cistpl.h>
  22. #include <pcmcia/ds.h>
  23. #include <pcmcia/cisreg.h>
  24. #include <pcmcia/ciscode.h>
  25. #define DRV_NAME "pata_pcmcia"
  26. #define DRV_VERSION "0.3.5"
  27. /**
  28. * pcmcia_set_mode - PCMCIA specific mode setup
  29. * @link: link
  30. * @r_failed_dev: Return pointer for failed device
  31. *
  32. * Perform the tuning and setup of the devices and timings, which
  33. * for PCMCIA is the same as any other controller. We wrap it however
  34. * as we need to spot hardware with incorrect or missing master/slave
  35. * decode, which alas is embarrassingly common in the PC world
  36. */
  37. static int pcmcia_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  38. {
  39. struct ata_device *master = &link->device[0];
  40. struct ata_device *slave = &link->device[1];
  41. if (!ata_dev_enabled(master) || !ata_dev_enabled(slave))
  42. return ata_do_set_mode(link, r_failed_dev);
  43. if (memcmp(master->id + ATA_ID_FW_REV, slave->id + ATA_ID_FW_REV,
  44. ATA_ID_FW_REV_LEN + ATA_ID_PROD_LEN) == 0) {
  45. /* Suspicious match, but could be two cards from
  46. the same vendor - check serial */
  47. if (memcmp(master->id + ATA_ID_SERNO, slave->id + ATA_ID_SERNO,
  48. ATA_ID_SERNO_LEN) == 0 && master->id[ATA_ID_SERNO] >> 8) {
  49. ata_dev_warn(slave, "is a ghost device, ignoring\n");
  50. ata_dev_disable(slave);
  51. }
  52. }
  53. return ata_do_set_mode(link, r_failed_dev);
  54. }
  55. /**
  56. * pcmcia_set_mode_8bit - PCMCIA specific mode setup
  57. * @link: link
  58. * @r_failed_dev: Return pointer for failed device
  59. *
  60. * For the simple emulated 8bit stuff the less we do the better.
  61. */
  62. static int pcmcia_set_mode_8bit(struct ata_link *link,
  63. struct ata_device **r_failed_dev)
  64. {
  65. return 0;
  66. }
  67. /**
  68. * ata_data_xfer_8bit - Transfer data by 8bit PIO
  69. * @qc: queued command
  70. * @buf: data buffer
  71. * @buflen: buffer length
  72. * @rw: read/write
  73. *
  74. * Transfer data from/to the device data register by 8 bit PIO.
  75. *
  76. * LOCKING:
  77. * Inherited from caller.
  78. */
  79. static unsigned int ata_data_xfer_8bit(struct ata_queued_cmd *qc,
  80. unsigned char *buf, unsigned int buflen, int rw)
  81. {
  82. struct ata_port *ap = qc->dev->link->ap;
  83. if (rw == READ)
  84. ioread8_rep(ap->ioaddr.data_addr, buf, buflen);
  85. else
  86. iowrite8_rep(ap->ioaddr.data_addr, buf, buflen);
  87. return buflen;
  88. }
  89. /**
  90. * pcmcia_8bit_drain_fifo - Stock FIFO drain logic for SFF controllers
  91. * @qc: command
  92. *
  93. * Drain the FIFO and device of any stuck data following a command
  94. * failing to complete. In some cases this is necessary before a
  95. * reset will recover the device.
  96. *
  97. */
  98. static void pcmcia_8bit_drain_fifo(struct ata_queued_cmd *qc)
  99. {
  100. int count;
  101. struct ata_port *ap;
  102. /* We only need to flush incoming data when a command was running */
  103. if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
  104. return;
  105. ap = qc->ap;
  106. /* Drain up to 64K of data before we give up this recovery method */
  107. for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ)
  108. && count++ < 65536;)
  109. ioread8(ap->ioaddr.data_addr);
  110. if (count)
  111. ata_port_warn(ap, "drained %d bytes to clear DRQ\n", count);
  112. }
  113. static struct scsi_host_template pcmcia_sht = {
  114. ATA_PIO_SHT(DRV_NAME),
  115. };
  116. static struct ata_port_operations pcmcia_port_ops = {
  117. .inherits = &ata_sff_port_ops,
  118. .sff_data_xfer = ata_sff_data_xfer32,
  119. .cable_detect = ata_cable_40wire,
  120. .set_mode = pcmcia_set_mode,
  121. };
  122. static struct ata_port_operations pcmcia_8bit_port_ops = {
  123. .inherits = &ata_sff_port_ops,
  124. .sff_data_xfer = ata_data_xfer_8bit,
  125. .cable_detect = ata_cable_40wire,
  126. .set_mode = pcmcia_set_mode_8bit,
  127. .sff_drain_fifo = pcmcia_8bit_drain_fifo,
  128. };
  129. static int pcmcia_check_one_config(struct pcmcia_device *pdev, void *priv_data)
  130. {
  131. int *is_kme = priv_data;
  132. if ((pdev->resource[0]->flags & IO_DATA_PATH_WIDTH)
  133. != IO_DATA_PATH_WIDTH_8) {
  134. pdev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  135. pdev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
  136. }
  137. pdev->resource[1]->flags &= ~IO_DATA_PATH_WIDTH;
  138. pdev->resource[1]->flags |= IO_DATA_PATH_WIDTH_8;
  139. if (pdev->resource[1]->end) {
  140. pdev->resource[0]->end = 8;
  141. pdev->resource[1]->end = (*is_kme) ? 2 : 1;
  142. } else {
  143. if (pdev->resource[0]->end < 16)
  144. return -ENODEV;
  145. }
  146. return pcmcia_request_io(pdev);
  147. }
  148. /**
  149. * pcmcia_init_one - attach a PCMCIA interface
  150. * @pdev: pcmcia device
  151. *
  152. * Register a PCMCIA IDE interface. Such interfaces are PIO 0 and
  153. * shared IRQ.
  154. */
  155. static int pcmcia_init_one(struct pcmcia_device *pdev)
  156. {
  157. struct ata_host *host;
  158. struct ata_port *ap;
  159. int is_kme = 0, ret = -ENOMEM, p;
  160. unsigned long io_base, ctl_base;
  161. void __iomem *io_addr, *ctl_addr;
  162. int n_ports = 1;
  163. struct ata_port_operations *ops = &pcmcia_port_ops;
  164. /* Set up attributes in order to probe card and get resources */
  165. pdev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO |
  166. CONF_AUTO_SET_VPP | CONF_AUTO_CHECK_VCC;
  167. /* See if we have a manufacturer identifier. Use it to set is_kme for
  168. vendor quirks */
  169. is_kme = ((pdev->manf_id == MANFID_KME) &&
  170. ((pdev->card_id == PRODID_KME_KXLC005_A) ||
  171. (pdev->card_id == PRODID_KME_KXLC005_B)));
  172. if (pcmcia_loop_config(pdev, pcmcia_check_one_config, &is_kme)) {
  173. pdev->config_flags &= ~CONF_AUTO_CHECK_VCC;
  174. if (pcmcia_loop_config(pdev, pcmcia_check_one_config, &is_kme))
  175. goto failed; /* No suitable config found */
  176. }
  177. io_base = pdev->resource[0]->start;
  178. if (pdev->resource[1]->end)
  179. ctl_base = pdev->resource[1]->start;
  180. else
  181. ctl_base = pdev->resource[0]->start + 0x0e;
  182. if (!pdev->irq)
  183. goto failed;
  184. ret = pcmcia_enable_device(pdev);
  185. if (ret)
  186. goto failed;
  187. /* iomap */
  188. ret = -ENOMEM;
  189. io_addr = devm_ioport_map(&pdev->dev, io_base, 8);
  190. ctl_addr = devm_ioport_map(&pdev->dev, ctl_base, 1);
  191. if (!io_addr || !ctl_addr)
  192. goto failed;
  193. /* Success. Disable the IRQ nIEN line, do quirks */
  194. iowrite8(0x02, ctl_addr);
  195. if (is_kme)
  196. iowrite8(0x81, ctl_addr + 0x01);
  197. /* FIXME: Could be more ports at base + 0x10 but we only deal with
  198. one right now */
  199. if (resource_size(pdev->resource[0]) >= 0x20)
  200. n_ports = 2;
  201. if (pdev->manf_id == 0x0097 && pdev->card_id == 0x1620)
  202. ops = &pcmcia_8bit_port_ops;
  203. /*
  204. * Having done the PCMCIA plumbing the ATA side is relatively
  205. * sane.
  206. */
  207. ret = -ENOMEM;
  208. host = ata_host_alloc(&pdev->dev, n_ports);
  209. if (!host)
  210. goto failed;
  211. for (p = 0; p < n_ports; p++) {
  212. ap = host->ports[p];
  213. ap->ops = ops;
  214. ap->pio_mask = ATA_PIO0; /* ISA so PIO 0 cycles */
  215. ap->flags |= ATA_FLAG_SLAVE_POSS;
  216. ap->ioaddr.cmd_addr = io_addr + 0x10 * p;
  217. ap->ioaddr.altstatus_addr = ctl_addr + 0x10 * p;
  218. ap->ioaddr.ctl_addr = ctl_addr + 0x10 * p;
  219. ata_sff_std_ports(&ap->ioaddr);
  220. ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", io_base, ctl_base);
  221. }
  222. /* activate */
  223. ret = ata_host_activate(host, pdev->irq, ata_sff_interrupt,
  224. IRQF_SHARED, &pcmcia_sht);
  225. if (ret)
  226. goto failed;
  227. pdev->priv = host;
  228. return 0;
  229. failed:
  230. pcmcia_disable_device(pdev);
  231. return ret;
  232. }
  233. /**
  234. * pcmcia_remove_one - unplug an pcmcia interface
  235. * @pdev: pcmcia device
  236. *
  237. * A PCMCIA ATA device has been unplugged. Perform the needed
  238. * cleanup. Also called on module unload for any active devices.
  239. */
  240. static void pcmcia_remove_one(struct pcmcia_device *pdev)
  241. {
  242. struct ata_host *host = pdev->priv;
  243. if (host)
  244. ata_host_detach(host);
  245. pcmcia_disable_device(pdev);
  246. }
  247. static const struct pcmcia_device_id pcmcia_devices[] = {
  248. PCMCIA_DEVICE_FUNC_ID(4),
  249. PCMCIA_DEVICE_MANF_CARD(0x0000, 0x0000), /* Corsair */
  250. PCMCIA_DEVICE_MANF_CARD(0x0007, 0x0000), /* Hitachi */
  251. PCMCIA_DEVICE_MANF_CARD(0x000a, 0x0000), /* I-O Data CFA */
  252. PCMCIA_DEVICE_MANF_CARD(0x001c, 0x0001), /* Mitsubishi CFA */
  253. PCMCIA_DEVICE_MANF_CARD(0x0032, 0x0704),
  254. PCMCIA_DEVICE_MANF_CARD(0x0032, 0x2904),
  255. PCMCIA_DEVICE_MANF_CARD(0x0045, 0x0401), /* SanDisk CFA */
  256. PCMCIA_DEVICE_MANF_CARD(0x004f, 0x0000), /* Kingston */
  257. PCMCIA_DEVICE_MANF_CARD(0x0097, 0x1620), /* TI emulated */
  258. PCMCIA_DEVICE_MANF_CARD(0x0098, 0x0000), /* Toshiba */
  259. PCMCIA_DEVICE_MANF_CARD(0x00a4, 0x002d),
  260. PCMCIA_DEVICE_MANF_CARD(0x00ce, 0x0000), /* Samsung */
  261. PCMCIA_DEVICE_MANF_CARD(0x00f1, 0x0101), /* SanDisk High (>8G) CFA */
  262. PCMCIA_DEVICE_MANF_CARD(0x0319, 0x0000), /* Hitachi */
  263. PCMCIA_DEVICE_MANF_CARD(0x2080, 0x0001),
  264. PCMCIA_DEVICE_MANF_CARD(0x4e01, 0x0100), /* Viking CFA */
  265. PCMCIA_DEVICE_MANF_CARD(0x4e01, 0x0200), /* Lexar, Viking CFA */
  266. PCMCIA_DEVICE_PROD_ID123("Caravelle", "PSC-IDE ", "PSC000", 0x8c36137c, 0xd0693ab8, 0x2768a9f0),
  267. PCMCIA_DEVICE_PROD_ID123("CDROM", "IDE", "MCD-601p", 0x1b9179ca, 0xede88951, 0x0d902f74),
  268. PCMCIA_DEVICE_PROD_ID123("PCMCIA", "IDE CARD", "F1", 0x281f1c5d, 0x1907960c, 0xf7fde8b9),
  269. PCMCIA_DEVICE_PROD_ID12("ARGOSY", "CD-ROM", 0x78f308dc, 0x66536591),
  270. PCMCIA_DEVICE_PROD_ID12("ARGOSY", "PnPIDE", 0x78f308dc, 0x0c694728),
  271. PCMCIA_DEVICE_PROD_ID12("CNF ", "CD-ROM", 0x46d7db81, 0x66536591),
  272. PCMCIA_DEVICE_PROD_ID12("CNF CD-M", "CD-ROM", 0x7d93b852, 0x66536591),
  273. PCMCIA_DEVICE_PROD_ID12("Creative Technology Ltd.", "PCMCIA CD-ROM Interface Card", 0xff8c8a45, 0xfe8020c4),
  274. PCMCIA_DEVICE_PROD_ID12("Digital Equipment Corporation.", "Digital Mobile Media CD-ROM", 0x17692a66, 0xef1dcbde),
  275. PCMCIA_DEVICE_PROD_ID12("EXP", "CD+GAME", 0x6f58c983, 0x63c13aaf),
  276. PCMCIA_DEVICE_PROD_ID12("EXP ", "CD-ROM", 0x0a5c52fd, 0x66536591),
  277. PCMCIA_DEVICE_PROD_ID12("EXP ", "PnPIDE", 0x0a5c52fd, 0x0c694728),
  278. PCMCIA_DEVICE_PROD_ID12("FREECOM", "PCCARD-IDE", 0x5714cbf7, 0x48e0ab8e),
  279. PCMCIA_DEVICE_PROD_ID12("HITACHI", "FLASH", 0xf4f43949, 0x9eb86aae),
  280. PCMCIA_DEVICE_PROD_ID12("HITACHI", "microdrive", 0xf4f43949, 0xa6d76178),
  281. PCMCIA_DEVICE_PROD_ID12("Hyperstone", "Model1", 0x3d5b9ef5, 0xca6ab420),
  282. PCMCIA_DEVICE_PROD_ID12("IBM", "microdrive", 0xb569a6e5, 0xa6d76178),
  283. PCMCIA_DEVICE_PROD_ID12("IBM", "IBM17JSSFP20", 0xb569a6e5, 0xf2508753),
  284. PCMCIA_DEVICE_PROD_ID12("KINGSTON", "CF CARD 1GB", 0x2e6d1829, 0x55d5bffb),
  285. PCMCIA_DEVICE_PROD_ID12("KINGSTON", "CF CARD 4GB", 0x2e6d1829, 0x531e7d10),
  286. PCMCIA_DEVICE_PROD_ID12("KINGSTON", "CF8GB", 0x2e6d1829, 0xacbe682e),
  287. PCMCIA_DEVICE_PROD_ID12("IO DATA", "CBIDE2 ", 0x547e66dc, 0x8671043b),
  288. PCMCIA_DEVICE_PROD_ID12("IO DATA", "PCIDE", 0x547e66dc, 0x5c5ab149),
  289. PCMCIA_DEVICE_PROD_ID12("IO DATA", "PCIDEII", 0x547e66dc, 0xb3662674),
  290. PCMCIA_DEVICE_PROD_ID12("LOOKMEET", "CBIDE2 ", 0xe37be2b5, 0x8671043b),
  291. PCMCIA_DEVICE_PROD_ID12("M-Systems", "CF300", 0x7ed2ad87, 0x7e9e78ee),
  292. PCMCIA_DEVICE_PROD_ID12("M-Systems", "CF500", 0x7ed2ad87, 0x7a13045c),
  293. PCMCIA_DEVICE_PROD_ID2("NinjaATA-", 0xebe0bd79),
  294. PCMCIA_DEVICE_PROD_ID12("PCMCIA", "CD-ROM", 0x281f1c5d, 0x66536591),
  295. PCMCIA_DEVICE_PROD_ID12("PCMCIA", "PnPIDE", 0x281f1c5d, 0x0c694728),
  296. PCMCIA_DEVICE_PROD_ID12("SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", 0x4a3f0ba0, 0x322560e1),
  297. PCMCIA_DEVICE_PROD_ID12("SEAGATE", "ST1", 0x87c1b330, 0xe1f30883),
  298. PCMCIA_DEVICE_PROD_ID12("SAMSUNG", "04/05/06", 0x43d74cb4, 0x6a22777d),
  299. PCMCIA_DEVICE_PROD_ID12("SMI VENDOR", "SMI PRODUCT", 0x30896c92, 0x703cc5f6),
  300. PCMCIA_DEVICE_PROD_ID12("TOSHIBA", "MK2001MPL", 0xb4585a1a, 0x3489e003),
  301. PCMCIA_DEVICE_PROD_ID1("TRANSCEND 512M ", 0xd0909443),
  302. PCMCIA_DEVICE_PROD_ID12("TRANSCEND", "TS1GCF45", 0x709b1bf1, 0xf68b6f32),
  303. PCMCIA_DEVICE_PROD_ID12("TRANSCEND", "TS1GCF80", 0x709b1bf1, 0x2a54d4b1),
  304. PCMCIA_DEVICE_PROD_ID12("TRANSCEND", "TS2GCF120", 0x709b1bf1, 0x969aa4f2),
  305. PCMCIA_DEVICE_PROD_ID12("TRANSCEND", "TS4GCF120", 0x709b1bf1, 0xf54a91c8),
  306. PCMCIA_DEVICE_PROD_ID12("TRANSCEND", "TS4GCF133", 0x709b1bf1, 0x7558f133),
  307. PCMCIA_DEVICE_PROD_ID12("TRANSCEND", "TS8GCF133", 0x709b1bf1, 0xb2f89b47),
  308. PCMCIA_DEVICE_PROD_ID12("WIT", "IDE16", 0x244e5994, 0x3e232852),
  309. PCMCIA_DEVICE_PROD_ID12("WEIDA", "TWTTI", 0xcc7cf69c, 0x212bb918),
  310. PCMCIA_DEVICE_PROD_ID1("STI Flash", 0xe4a13209),
  311. PCMCIA_DEVICE_PROD_ID12("STI", "Flash 5.0", 0xbf2df18d, 0x8cb57a0e),
  312. PCMCIA_MFC_DEVICE_PROD_ID12(1, "SanDisk", "ConnectPlus", 0x7a954bd9, 0x74be00c6),
  313. PCMCIA_DEVICE_PROD_ID2("Flash Card", 0x5a362506),
  314. PCMCIA_DEVICE_NULL,
  315. };
  316. MODULE_DEVICE_TABLE(pcmcia, pcmcia_devices);
  317. static struct pcmcia_driver pcmcia_driver = {
  318. .owner = THIS_MODULE,
  319. .name = DRV_NAME,
  320. .id_table = pcmcia_devices,
  321. .probe = pcmcia_init_one,
  322. .remove = pcmcia_remove_one,
  323. };
  324. module_pcmcia_driver(pcmcia_driver);
  325. MODULE_AUTHOR("Alan Cox");
  326. MODULE_DESCRIPTION("low-level driver for PCMCIA ATA");
  327. MODULE_LICENSE("GPL");
  328. MODULE_VERSION(DRV_VERSION);