ide-cs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*======================================================================
  2. A driver for PCMCIA IDE/ATA disk cards
  3. The contents of this file are subject to the Mozilla Public
  4. License Version 1.1 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of
  6. the License at http://www.mozilla.org/MPL/
  7. Software distributed under the License is distributed on an "AS
  8. IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. implied. See the License for the specific language governing
  10. rights and limitations under the License.
  11. The initial developer of the original code is David A. Hinds
  12. <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  13. are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  14. Alternatively, the contents of this file may be used under the
  15. terms of the GNU General Public License version 2 (the "GPL"), in
  16. which case the provisions of the GPL are applicable instead of the
  17. above. If you wish to allow the use of your version of this file
  18. only under the terms of the GPL and not to allow others to use
  19. your version of this file under the MPL, indicate your decision
  20. by deleting the provisions above and replace them with the notice
  21. and other provisions required by the GPL. If you do not delete
  22. the provisions above, a recipient may use your version of this
  23. file under either the MPL or the GPL.
  24. ======================================================================*/
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/slab.h>
  30. #include <linux/string.h>
  31. #include <linux/timer.h>
  32. #include <linux/ioport.h>
  33. #include <linux/ide.h>
  34. #include <linux/hdreg.h>
  35. #include <linux/major.h>
  36. #include <linux/delay.h>
  37. #include <asm/io.h>
  38. #include <asm/system.h>
  39. #include <pcmcia/cs_types.h>
  40. #include <pcmcia/cs.h>
  41. #include <pcmcia/cistpl.h>
  42. #include <pcmcia/ds.h>
  43. #include <pcmcia/cisreg.h>
  44. #include <pcmcia/ciscode.h>
  45. /*====================================================================*/
  46. /* Module parameters */
  47. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  48. MODULE_DESCRIPTION("PCMCIA ATA/IDE card driver");
  49. MODULE_LICENSE("Dual MPL/GPL");
  50. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
  51. #ifdef PCMCIA_DEBUG
  52. INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
  53. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
  54. static char *version =
  55. "ide-cs.c 1.3 2002/10/26 05:45:31 (David Hinds)";
  56. #else
  57. #define DEBUG(n, args...)
  58. #endif
  59. /*====================================================================*/
  60. static const char ide_major[] = {
  61. IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR,
  62. IDE4_MAJOR, IDE5_MAJOR
  63. };
  64. typedef struct ide_info_t {
  65. struct pcmcia_device *p_dev;
  66. int ndev;
  67. dev_node_t node;
  68. int hd;
  69. } ide_info_t;
  70. static void ide_release(struct pcmcia_device *);
  71. static int ide_config(struct pcmcia_device *);
  72. static void ide_detach(struct pcmcia_device *p_dev);
  73. /*======================================================================
  74. ide_attach() creates an "instance" of the driver, allocating
  75. local data structures for one device. The device is registered
  76. with Card Services.
  77. ======================================================================*/
  78. static int ide_probe(struct pcmcia_device *link)
  79. {
  80. ide_info_t *info;
  81. DEBUG(0, "ide_attach()\n");
  82. /* Create new ide device */
  83. info = kzalloc(sizeof(*info), GFP_KERNEL);
  84. if (!info)
  85. return -ENOMEM;
  86. info->p_dev = link;
  87. link->priv = info;
  88. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  89. link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
  90. link->io.IOAddrLines = 3;
  91. link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
  92. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  93. link->conf.Attributes = CONF_ENABLE_IRQ;
  94. link->conf.IntType = INT_MEMORY_AND_IO;
  95. return ide_config(link);
  96. } /* ide_attach */
  97. /*======================================================================
  98. This deletes a driver "instance". The device is de-registered
  99. with Card Services. If it has been released, all local data
  100. structures are freed. Otherwise, the structures will be freed
  101. when the device is released.
  102. ======================================================================*/
  103. static void ide_detach(struct pcmcia_device *link)
  104. {
  105. DEBUG(0, "ide_detach(0x%p)\n", link);
  106. ide_release(link);
  107. kfree(link->priv);
  108. } /* ide_detach */
  109. static int idecs_register(unsigned long io, unsigned long ctl, unsigned long irq, struct pcmcia_device *handle)
  110. {
  111. ide_hwif_t *hwif;
  112. hw_regs_t hw;
  113. int i;
  114. u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
  115. memset(&hw, 0, sizeof(hw));
  116. ide_std_init_ports(&hw, io, ctl);
  117. hw.irq = irq;
  118. hw.chipset = ide_pci;
  119. hw.dev = &handle->dev;
  120. hwif = ide_find_port();
  121. if (hwif == NULL)
  122. return -1;
  123. i = hwif->index;
  124. if (hwif->present)
  125. ide_unregister(i);
  126. else
  127. ide_init_port_data(hwif, i);
  128. ide_init_port_hw(hwif, &hw);
  129. hwif->quirkproc = &ide_undecoded_slave;
  130. idx[0] = i;
  131. ide_device_add(idx, NULL);
  132. return hwif->present ? i : -1;
  133. }
  134. /*======================================================================
  135. ide_config() is scheduled to run after a CARD_INSERTION event
  136. is received, to configure the PCMCIA socket, and to make the
  137. ide device available to the system.
  138. ======================================================================*/
  139. #define CS_CHECK(fn, ret) \
  140. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  141. static int ide_config(struct pcmcia_device *link)
  142. {
  143. ide_info_t *info = link->priv;
  144. tuple_t tuple;
  145. struct {
  146. u_short buf[128];
  147. cisparse_t parse;
  148. config_info_t conf;
  149. cistpl_cftable_entry_t dflt;
  150. } *stk = NULL;
  151. cistpl_cftable_entry_t *cfg;
  152. int i, pass, last_ret = 0, last_fn = 0, hd, is_kme = 0;
  153. unsigned long io_base, ctl_base;
  154. DEBUG(0, "ide_config(0x%p)\n", link);
  155. stk = kzalloc(sizeof(*stk), GFP_KERNEL);
  156. if (!stk) goto err_mem;
  157. cfg = &stk->parse.cftable_entry;
  158. tuple.TupleData = (cisdata_t *)&stk->buf;
  159. tuple.TupleOffset = 0;
  160. tuple.TupleDataMax = 255;
  161. tuple.Attributes = 0;
  162. is_kme = ((link->manf_id == MANFID_KME) &&
  163. ((link->card_id == PRODID_KME_KXLC005_A) ||
  164. (link->card_id == PRODID_KME_KXLC005_B)));
  165. /* Not sure if this is right... look up the current Vcc */
  166. CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(link, &stk->conf));
  167. pass = io_base = ctl_base = 0;
  168. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  169. tuple.Attributes = 0;
  170. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  171. while (1) {
  172. if (pcmcia_get_tuple_data(link, &tuple) != 0) goto next_entry;
  173. if (pcmcia_parse_tuple(link, &tuple, &stk->parse) != 0) goto next_entry;
  174. /* Check for matching Vcc, unless we're desperate */
  175. if (!pass) {
  176. if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
  177. if (stk->conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000)
  178. goto next_entry;
  179. } else if (stk->dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
  180. if (stk->conf.Vcc != stk->dflt.vcc.param[CISTPL_POWER_VNOM] / 10000)
  181. goto next_entry;
  182. }
  183. }
  184. if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
  185. link->conf.Vpp =
  186. cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
  187. else if (stk->dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
  188. link->conf.Vpp =
  189. stk->dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
  190. if ((cfg->io.nwin > 0) || (stk->dflt.io.nwin > 0)) {
  191. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &stk->dflt.io;
  192. link->conf.ConfigIndex = cfg->index;
  193. link->io.BasePort1 = io->win[0].base;
  194. link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
  195. if (!(io->flags & CISTPL_IO_16BIT))
  196. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  197. if (io->nwin == 2) {
  198. link->io.NumPorts1 = 8;
  199. link->io.BasePort2 = io->win[1].base;
  200. link->io.NumPorts2 = (is_kme) ? 2 : 1;
  201. if (pcmcia_request_io(link, &link->io) != 0)
  202. goto next_entry;
  203. io_base = link->io.BasePort1;
  204. ctl_base = link->io.BasePort2;
  205. } else if ((io->nwin == 1) && (io->win[0].len >= 16)) {
  206. link->io.NumPorts1 = io->win[0].len;
  207. link->io.NumPorts2 = 0;
  208. if (pcmcia_request_io(link, &link->io) != 0)
  209. goto next_entry;
  210. io_base = link->io.BasePort1;
  211. ctl_base = link->io.BasePort1 + 0x0e;
  212. } else goto next_entry;
  213. /* If we've got this far, we're done */
  214. break;
  215. }
  216. next_entry:
  217. if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
  218. memcpy(&stk->dflt, cfg, sizeof(stk->dflt));
  219. if (pass) {
  220. CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(link, &tuple));
  221. } else if (pcmcia_get_next_tuple(link, &tuple) != 0) {
  222. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  223. memset(&stk->dflt, 0, sizeof(stk->dflt));
  224. pass++;
  225. }
  226. }
  227. CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
  228. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
  229. /* disable drive interrupts during IDE probe */
  230. outb(0x02, ctl_base);
  231. /* special setup for KXLC005 card */
  232. if (is_kme)
  233. outb(0x81, ctl_base+1);
  234. /* retry registration in case device is still spinning up */
  235. for (hd = -1, i = 0; i < 10; i++) {
  236. hd = idecs_register(io_base, ctl_base, link->irq.AssignedIRQ, link);
  237. if (hd >= 0) break;
  238. if (link->io.NumPorts1 == 0x20) {
  239. outb(0x02, ctl_base + 0x10);
  240. hd = idecs_register(io_base + 0x10, ctl_base + 0x10,
  241. link->irq.AssignedIRQ, link);
  242. if (hd >= 0) {
  243. io_base += 0x10;
  244. ctl_base += 0x10;
  245. break;
  246. }
  247. }
  248. msleep(100);
  249. }
  250. if (hd < 0) {
  251. printk(KERN_NOTICE "ide-cs: ide_register() at 0x%3lx & 0x%3lx"
  252. ", irq %u failed\n", io_base, ctl_base,
  253. link->irq.AssignedIRQ);
  254. goto failed;
  255. }
  256. info->ndev = 1;
  257. sprintf(info->node.dev_name, "hd%c", 'a' + (hd * 2));
  258. info->node.major = ide_major[hd];
  259. info->node.minor = 0;
  260. info->hd = hd;
  261. link->dev_node = &info->node;
  262. printk(KERN_INFO "ide-cs: %s: Vpp = %d.%d\n",
  263. info->node.dev_name, link->conf.Vpp / 10, link->conf.Vpp % 10);
  264. kfree(stk);
  265. return 0;
  266. err_mem:
  267. printk(KERN_NOTICE "ide-cs: ide_config failed memory allocation\n");
  268. goto failed;
  269. cs_failed:
  270. cs_error(link, last_fn, last_ret);
  271. failed:
  272. kfree(stk);
  273. ide_release(link);
  274. return -ENODEV;
  275. } /* ide_config */
  276. /*======================================================================
  277. After a card is removed, ide_release() will unregister the net
  278. device, and release the PCMCIA configuration. If the device is
  279. still open, this will be postponed until it is closed.
  280. ======================================================================*/
  281. void ide_release(struct pcmcia_device *link)
  282. {
  283. ide_info_t *info = link->priv;
  284. DEBUG(0, "ide_release(0x%p)\n", link);
  285. if (info->ndev) {
  286. /* FIXME: if this fails we need to queue the cleanup somehow
  287. -- need to investigate the required PCMCIA magic */
  288. ide_unregister(info->hd);
  289. }
  290. info->ndev = 0;
  291. pcmcia_disable_device(link);
  292. } /* ide_release */
  293. /*======================================================================
  294. The card status event handler. Mostly, this schedules other
  295. stuff to run after an event is received. A CARD_REMOVAL event
  296. also sets some flags to discourage the ide drivers from
  297. talking to the ports.
  298. ======================================================================*/
  299. static struct pcmcia_device_id ide_ids[] = {
  300. PCMCIA_DEVICE_FUNC_ID(4),
  301. PCMCIA_DEVICE_MANF_CARD(0x0000, 0x0000), /* Corsair */
  302. PCMCIA_DEVICE_MANF_CARD(0x0007, 0x0000), /* Hitachi */
  303. PCMCIA_DEVICE_MANF_CARD(0x000a, 0x0000), /* I-O Data CFA */
  304. PCMCIA_DEVICE_MANF_CARD(0x001c, 0x0001), /* Mitsubishi CFA */
  305. PCMCIA_DEVICE_MANF_CARD(0x0032, 0x0704),
  306. PCMCIA_DEVICE_MANF_CARD(0x0045, 0x0401), /* SanDisk CFA */
  307. PCMCIA_DEVICE_MANF_CARD(0x0098, 0x0000), /* Toshiba */
  308. PCMCIA_DEVICE_MANF_CARD(0x00a4, 0x002d),
  309. PCMCIA_DEVICE_MANF_CARD(0x00ce, 0x0000), /* Samsung */
  310. PCMCIA_DEVICE_MANF_CARD(0x0319, 0x0000), /* Hitachi */
  311. PCMCIA_DEVICE_MANF_CARD(0x2080, 0x0001),
  312. PCMCIA_DEVICE_MANF_CARD(0x4e01, 0x0100), /* Viking CFA */
  313. PCMCIA_DEVICE_MANF_CARD(0x4e01, 0x0200), /* Lexar, Viking CFA */
  314. PCMCIA_DEVICE_PROD_ID123("Caravelle", "PSC-IDE ", "PSC000", 0x8c36137c, 0xd0693ab8, 0x2768a9f0),
  315. PCMCIA_DEVICE_PROD_ID123("CDROM", "IDE", "MCD-601p", 0x1b9179ca, 0xede88951, 0x0d902f74),
  316. PCMCIA_DEVICE_PROD_ID123("PCMCIA", "IDE CARD", "F1", 0x281f1c5d, 0x1907960c, 0xf7fde8b9),
  317. PCMCIA_DEVICE_PROD_ID12("ARGOSY", "CD-ROM", 0x78f308dc, 0x66536591),
  318. PCMCIA_DEVICE_PROD_ID12("ARGOSY", "PnPIDE", 0x78f308dc, 0x0c694728),
  319. PCMCIA_DEVICE_PROD_ID12("CNF CD-M", "CD-ROM", 0x7d93b852, 0x66536591),
  320. PCMCIA_DEVICE_PROD_ID12("Creative Technology Ltd.", "PCMCIA CD-ROM Interface Card", 0xff8c8a45, 0xfe8020c4),
  321. PCMCIA_DEVICE_PROD_ID12("Digital Equipment Corporation.", "Digital Mobile Media CD-ROM", 0x17692a66, 0xef1dcbde),
  322. PCMCIA_DEVICE_PROD_ID12("EXP", "CD+GAME", 0x6f58c983, 0x63c13aaf),
  323. PCMCIA_DEVICE_PROD_ID12("EXP ", "CD-ROM", 0x0a5c52fd, 0x66536591),
  324. PCMCIA_DEVICE_PROD_ID12("EXP ", "PnPIDE", 0x0a5c52fd, 0x0c694728),
  325. PCMCIA_DEVICE_PROD_ID12("FREECOM", "PCCARD-IDE", 0x5714cbf7, 0x48e0ab8e),
  326. PCMCIA_DEVICE_PROD_ID12("HITACHI", "FLASH", 0xf4f43949, 0x9eb86aae),
  327. PCMCIA_DEVICE_PROD_ID12("HITACHI", "microdrive", 0xf4f43949, 0xa6d76178),
  328. PCMCIA_DEVICE_PROD_ID12("Hyperstone", "Model1", 0x3d5b9ef5, 0xca6ab420),
  329. PCMCIA_DEVICE_PROD_ID12("IBM", "microdrive", 0xb569a6e5, 0xa6d76178),
  330. PCMCIA_DEVICE_PROD_ID12("IBM", "IBM17JSSFP20", 0xb569a6e5, 0xf2508753),
  331. PCMCIA_DEVICE_PROD_ID12("KINGSTON", "CF8GB", 0x2e6d1829, 0xacbe682e),
  332. PCMCIA_DEVICE_PROD_ID12("IO DATA", "CBIDE2 ", 0x547e66dc, 0x8671043b),
  333. PCMCIA_DEVICE_PROD_ID12("IO DATA", "PCIDE", 0x547e66dc, 0x5c5ab149),
  334. PCMCIA_DEVICE_PROD_ID12("IO DATA", "PCIDEII", 0x547e66dc, 0xb3662674),
  335. PCMCIA_DEVICE_PROD_ID12("LOOKMEET", "CBIDE2 ", 0xe37be2b5, 0x8671043b),
  336. PCMCIA_DEVICE_PROD_ID12("M-Systems", "CF500", 0x7ed2ad87, 0x7a13045c),
  337. PCMCIA_DEVICE_PROD_ID2("NinjaATA-", 0xebe0bd79),
  338. PCMCIA_DEVICE_PROD_ID12("PCMCIA", "CD-ROM", 0x281f1c5d, 0x66536591),
  339. PCMCIA_DEVICE_PROD_ID12("PCMCIA", "PnPIDE", 0x281f1c5d, 0x0c694728),
  340. PCMCIA_DEVICE_PROD_ID12("SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", 0x4a3f0ba0, 0x322560e1),
  341. PCMCIA_DEVICE_PROD_ID12("SEAGATE", "ST1", 0x87c1b330, 0xe1f30883),
  342. PCMCIA_DEVICE_PROD_ID12("SAMSUNG", "04/05/06", 0x43d74cb4, 0x6a22777d),
  343. PCMCIA_DEVICE_PROD_ID12("SMI VENDOR", "SMI PRODUCT", 0x30896c92, 0x703cc5f6),
  344. PCMCIA_DEVICE_PROD_ID12("TOSHIBA", "MK2001MPL", 0xb4585a1a, 0x3489e003),
  345. PCMCIA_DEVICE_PROD_ID1("TRANSCEND 512M ", 0xd0909443),
  346. PCMCIA_DEVICE_PROD_ID12("TRANSCEND", "TS1GCF80", 0x709b1bf1, 0x2a54d4b1),
  347. PCMCIA_DEVICE_PROD_ID12("TRANSCEND", "TS2GCF120", 0x709b1bf1, 0x969aa4f2),
  348. PCMCIA_DEVICE_PROD_ID12("TRANSCEND", "TS4GCF120", 0x709b1bf1, 0xf54a91c8),
  349. PCMCIA_DEVICE_PROD_ID12("WIT", "IDE16", 0x244e5994, 0x3e232852),
  350. PCMCIA_DEVICE_PROD_ID12("WEIDA", "TWTTI", 0xcc7cf69c, 0x212bb918),
  351. PCMCIA_DEVICE_PROD_ID1("STI Flash", 0xe4a13209),
  352. PCMCIA_DEVICE_PROD_ID12("STI", "Flash 5.0", 0xbf2df18d, 0x8cb57a0e),
  353. PCMCIA_MFC_DEVICE_PROD_ID12(1, "SanDisk", "ConnectPlus", 0x7a954bd9, 0x74be00c6),
  354. PCMCIA_DEVICE_NULL,
  355. };
  356. MODULE_DEVICE_TABLE(pcmcia, ide_ids);
  357. static struct pcmcia_driver ide_cs_driver = {
  358. .owner = THIS_MODULE,
  359. .drv = {
  360. .name = "ide-cs",
  361. },
  362. .probe = ide_probe,
  363. .remove = ide_detach,
  364. .id_table = ide_ids,
  365. };
  366. static int __init init_ide_cs(void)
  367. {
  368. return pcmcia_register_driver(&ide_cs_driver);
  369. }
  370. static void __exit exit_ide_cs(void)
  371. {
  372. pcmcia_unregister_driver(&ide_cs_driver);
  373. }
  374. late_initcall(init_ide_cs);
  375. module_exit(exit_ide_cs);