pata_legacy.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /*
  2. * pata-legacy.c - Legacy port PATA/SATA controller driver.
  3. * Copyright 2005/2006 Red Hat <alan@redhat.com>, all rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; see the file COPYING. If not, write to
  17. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * An ATA driver for the legacy ATA ports.
  20. *
  21. * Data Sources:
  22. * Opti 82C465/82C611 support: Data sheets at opti-inc.com
  23. * HT6560 series:
  24. * Promise 20230/20620:
  25. * http://www.ryston.cz/petr/vlb/pdc20230b.html
  26. * http://www.ryston.cz/petr/vlb/pdc20230c.html
  27. * http://www.ryston.cz/petr/vlb/pdc20630.html
  28. *
  29. * Unsupported but docs exist:
  30. * Appian/Adaptec AIC25VL01/Cirrus Logic PD7220
  31. * Winbond W83759A
  32. *
  33. * This driver handles legacy (that is "ISA/VLB side") IDE ports found
  34. * on PC class systems. There are three hybrid devices that are exceptions
  35. * The Cyrix 5510/5520 where a pre SFF ATA device is on the bridge and
  36. * the MPIIX where the tuning is PCI side but the IDE is "ISA side".
  37. *
  38. * Specific support is included for the ht6560a/ht6560b/opti82c611a/
  39. * opti82c465mv/promise 20230c/20630
  40. *
  41. * Use the autospeed and pio_mask options with:
  42. * Appian ADI/2 aka CLPD7220 or AIC25VL01.
  43. * Use the jumpers, autospeed and set pio_mask to the mode on the jumpers with
  44. * Goldstar GM82C711, PIC-1288A-125, UMC 82C871F, Winbond W83759,
  45. * Winbond W83759A, Promise PDC20230-B
  46. *
  47. * For now use autospeed and pio_mask as above with the W83759A. This may
  48. * change.
  49. *
  50. * TODO
  51. * Merge existing pata_qdi driver
  52. *
  53. */
  54. #include <linux/kernel.h>
  55. #include <linux/module.h>
  56. #include <linux/pci.h>
  57. #include <linux/init.h>
  58. #include <linux/blkdev.h>
  59. #include <linux/delay.h>
  60. #include <scsi/scsi_host.h>
  61. #include <linux/ata.h>
  62. #include <linux/libata.h>
  63. #include <linux/platform_device.h>
  64. #define DRV_NAME "pata_legacy"
  65. #define DRV_VERSION "0.5.4"
  66. #define NR_HOST 6
  67. static int legacy_port[NR_HOST] = { 0x1f0, 0x170, 0x1e8, 0x168, 0x1e0, 0x160 };
  68. static int legacy_irq[NR_HOST] = { 14, 15, 11, 10, 8, 12 };
  69. struct legacy_data {
  70. unsigned long timing;
  71. u8 clock[2];
  72. u8 last;
  73. int fast;
  74. struct platform_device *platform_dev;
  75. };
  76. static struct legacy_data legacy_data[NR_HOST];
  77. static struct ata_host *legacy_host[NR_HOST];
  78. static int nr_legacy_host;
  79. static int probe_all; /* Set to check all ISA port ranges */
  80. static int ht6560a; /* HT 6560A on primary 1, secondary 2, both 3 */
  81. static int ht6560b; /* HT 6560A on primary 1, secondary 2, both 3 */
  82. static int opti82c611a; /* Opti82c611A on primary 1, secondary 2, both 3 */
  83. static int opti82c46x; /* Opti 82c465MV present (pri/sec autodetect) */
  84. static int autospeed; /* Chip present which snoops speed changes */
  85. static int pio_mask = 0x1F; /* PIO range for autospeed devices */
  86. static int iordy_mask = 0xFFFFFFFF; /* Use iordy if available */
  87. /**
  88. * legacy_set_mode - mode setting
  89. * @ap: IDE interface
  90. * @unused: Device that failed when error is returned
  91. *
  92. * Use a non standard set_mode function. We don't want to be tuned.
  93. *
  94. * The BIOS configured everything. Our job is not to fiddle. Just use
  95. * whatever PIO the hardware is using and leave it at that. When we
  96. * get some kind of nice user driven API for control then we can
  97. * expand on this as per hdparm in the base kernel.
  98. */
  99. static int legacy_set_mode(struct ata_port *ap, struct ata_device **unused)
  100. {
  101. int i;
  102. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  103. struct ata_device *dev = &ap->device[i];
  104. if (ata_dev_enabled(dev)) {
  105. ata_dev_printk(dev, KERN_INFO, "configured for PIO\n");
  106. dev->pio_mode = XFER_PIO_0;
  107. dev->xfer_mode = XFER_PIO_0;
  108. dev->xfer_shift = ATA_SHIFT_PIO;
  109. dev->flags |= ATA_DFLAG_PIO;
  110. }
  111. }
  112. return 0;
  113. }
  114. static struct scsi_host_template legacy_sht = {
  115. .module = THIS_MODULE,
  116. .name = DRV_NAME,
  117. .ioctl = ata_scsi_ioctl,
  118. .queuecommand = ata_scsi_queuecmd,
  119. .can_queue = ATA_DEF_QUEUE,
  120. .this_id = ATA_SHT_THIS_ID,
  121. .sg_tablesize = LIBATA_MAX_PRD,
  122. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  123. .emulated = ATA_SHT_EMULATED,
  124. .use_clustering = ATA_SHT_USE_CLUSTERING,
  125. .proc_name = DRV_NAME,
  126. .dma_boundary = ATA_DMA_BOUNDARY,
  127. .slave_configure = ata_scsi_slave_config,
  128. .slave_destroy = ata_scsi_slave_destroy,
  129. .bios_param = ata_std_bios_param,
  130. };
  131. /*
  132. * These ops are used if the user indicates the hardware
  133. * snoops the commands to decide on the mode and handles the
  134. * mode selection "magically" itself. Several legacy controllers
  135. * do this. The mode range can be set if it is not 0x1F by setting
  136. * pio_mask as well.
  137. */
  138. static struct ata_port_operations simple_port_ops = {
  139. .port_disable = ata_port_disable,
  140. .tf_load = ata_tf_load,
  141. .tf_read = ata_tf_read,
  142. .check_status = ata_check_status,
  143. .exec_command = ata_exec_command,
  144. .dev_select = ata_std_dev_select,
  145. .freeze = ata_bmdma_freeze,
  146. .thaw = ata_bmdma_thaw,
  147. .error_handler = ata_bmdma_error_handler,
  148. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  149. .cable_detect = ata_cable_40wire,
  150. .qc_prep = ata_qc_prep,
  151. .qc_issue = ata_qc_issue_prot,
  152. .data_xfer = ata_data_xfer_noirq,
  153. .irq_handler = ata_interrupt,
  154. .irq_clear = ata_bmdma_irq_clear,
  155. .irq_on = ata_irq_on,
  156. .irq_ack = ata_irq_ack,
  157. .port_start = ata_port_start,
  158. };
  159. static struct ata_port_operations legacy_port_ops = {
  160. .set_mode = legacy_set_mode,
  161. .port_disable = ata_port_disable,
  162. .tf_load = ata_tf_load,
  163. .tf_read = ata_tf_read,
  164. .check_status = ata_check_status,
  165. .exec_command = ata_exec_command,
  166. .dev_select = ata_std_dev_select,
  167. .cable_detect = ata_cable_40wire,
  168. .freeze = ata_bmdma_freeze,
  169. .thaw = ata_bmdma_thaw,
  170. .error_handler = ata_bmdma_error_handler,
  171. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  172. .qc_prep = ata_qc_prep,
  173. .qc_issue = ata_qc_issue_prot,
  174. .data_xfer = ata_data_xfer_noirq,
  175. .irq_handler = ata_interrupt,
  176. .irq_clear = ata_bmdma_irq_clear,
  177. .irq_on = ata_irq_on,
  178. .irq_ack = ata_irq_ack,
  179. .port_start = ata_port_start,
  180. };
  181. /*
  182. * Promise 20230C and 20620 support
  183. *
  184. * This controller supports PIO0 to PIO2. We set PIO timings conservatively to
  185. * allow for 50MHz Vesa Local Bus. The 20620 DMA support is weird being DMA to
  186. * controller and PIO'd to the host and not supported.
  187. */
  188. static void pdc20230_set_piomode(struct ata_port *ap, struct ata_device *adev)
  189. {
  190. int tries = 5;
  191. int pio = adev->pio_mode - XFER_PIO_0;
  192. u8 rt;
  193. unsigned long flags;
  194. /* Safe as UP only. Force I/Os to occur together */
  195. local_irq_save(flags);
  196. /* Unlock the control interface */
  197. do
  198. {
  199. inb(0x1F5);
  200. outb(inb(0x1F2) | 0x80, 0x1F2);
  201. inb(0x1F2);
  202. inb(0x3F6);
  203. inb(0x3F6);
  204. inb(0x1F2);
  205. inb(0x1F2);
  206. }
  207. while((inb(0x1F2) & 0x80) && --tries);
  208. local_irq_restore(flags);
  209. outb(inb(0x1F4) & 0x07, 0x1F4);
  210. rt = inb(0x1F3);
  211. rt &= 0x07 << (3 * adev->devno);
  212. if (pio)
  213. rt |= (1 + 3 * pio) << (3 * adev->devno);
  214. udelay(100);
  215. outb(inb(0x1F2) | 0x01, 0x1F2);
  216. udelay(100);
  217. inb(0x1F5);
  218. }
  219. static void pdc_data_xfer_vlb(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data)
  220. {
  221. struct ata_port *ap = adev->ap;
  222. int slop = buflen & 3;
  223. unsigned long flags;
  224. if (ata_id_has_dword_io(adev->id)) {
  225. local_irq_save(flags);
  226. /* Perform the 32bit I/O synchronization sequence */
  227. ioread8(ap->ioaddr.nsect_addr);
  228. ioread8(ap->ioaddr.nsect_addr);
  229. ioread8(ap->ioaddr.nsect_addr);
  230. /* Now the data */
  231. if (write_data)
  232. iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
  233. else
  234. ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
  235. if (unlikely(slop)) {
  236. u32 pad;
  237. if (write_data) {
  238. memcpy(&pad, buf + buflen - slop, slop);
  239. pad = le32_to_cpu(pad);
  240. iowrite32(pad, ap->ioaddr.data_addr);
  241. } else {
  242. pad = ioread32(ap->ioaddr.data_addr);
  243. pad = cpu_to_le16(pad);
  244. memcpy(buf + buflen - slop, &pad, slop);
  245. }
  246. }
  247. local_irq_restore(flags);
  248. }
  249. else
  250. ata_data_xfer_noirq(adev, buf, buflen, write_data);
  251. }
  252. static struct ata_port_operations pdc20230_port_ops = {
  253. .set_piomode = pdc20230_set_piomode,
  254. .port_disable = ata_port_disable,
  255. .tf_load = ata_tf_load,
  256. .tf_read = ata_tf_read,
  257. .check_status = ata_check_status,
  258. .exec_command = ata_exec_command,
  259. .dev_select = ata_std_dev_select,
  260. .freeze = ata_bmdma_freeze,
  261. .thaw = ata_bmdma_thaw,
  262. .error_handler = ata_bmdma_error_handler,
  263. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  264. .cable_detect = ata_cable_40wire,
  265. .qc_prep = ata_qc_prep,
  266. .qc_issue = ata_qc_issue_prot,
  267. .data_xfer = pdc_data_xfer_vlb,
  268. .irq_handler = ata_interrupt,
  269. .irq_clear = ata_bmdma_irq_clear,
  270. .irq_on = ata_irq_on,
  271. .irq_ack = ata_irq_ack,
  272. .port_start = ata_port_start,
  273. };
  274. /*
  275. * Holtek 6560A support
  276. *
  277. * This controller supports PIO0 to PIO2 (no IORDY even though higher timings
  278. * can be loaded).
  279. */
  280. static void ht6560a_set_piomode(struct ata_port *ap, struct ata_device *adev)
  281. {
  282. u8 active, recover;
  283. struct ata_timing t;
  284. /* Get the timing data in cycles. For now play safe at 50Mhz */
  285. ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
  286. active = FIT(t.active, 2, 15);
  287. recover = FIT(t.recover, 4, 15);
  288. inb(0x3E6);
  289. inb(0x3E6);
  290. inb(0x3E6);
  291. inb(0x3E6);
  292. iowrite8(recover << 4 | active, ap->ioaddr.device_addr);
  293. ioread8(ap->ioaddr.status_addr);
  294. }
  295. static struct ata_port_operations ht6560a_port_ops = {
  296. .set_piomode = ht6560a_set_piomode,
  297. .port_disable = ata_port_disable,
  298. .tf_load = ata_tf_load,
  299. .tf_read = ata_tf_read,
  300. .check_status = ata_check_status,
  301. .exec_command = ata_exec_command,
  302. .dev_select = ata_std_dev_select,
  303. .freeze = ata_bmdma_freeze,
  304. .thaw = ata_bmdma_thaw,
  305. .error_handler = ata_bmdma_error_handler,
  306. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  307. .cable_detect = ata_cable_40wire,
  308. .qc_prep = ata_qc_prep,
  309. .qc_issue = ata_qc_issue_prot,
  310. .data_xfer = ata_data_xfer, /* Check vlb/noirq */
  311. .irq_handler = ata_interrupt,
  312. .irq_clear = ata_bmdma_irq_clear,
  313. .irq_on = ata_irq_on,
  314. .irq_ack = ata_irq_ack,
  315. .port_start = ata_port_start,
  316. };
  317. /*
  318. * Holtek 6560B support
  319. *
  320. * This controller supports PIO0 to PIO4. We honour the BIOS/jumper FIFO setting
  321. * unless we see an ATAPI device in which case we force it off.
  322. *
  323. * FIXME: need to implement 2nd channel support.
  324. */
  325. static void ht6560b_set_piomode(struct ata_port *ap, struct ata_device *adev)
  326. {
  327. u8 active, recover;
  328. struct ata_timing t;
  329. /* Get the timing data in cycles. For now play safe at 50Mhz */
  330. ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
  331. active = FIT(t.active, 2, 15);
  332. recover = FIT(t.recover, 2, 16);
  333. recover &= 0x15;
  334. inb(0x3E6);
  335. inb(0x3E6);
  336. inb(0x3E6);
  337. inb(0x3E6);
  338. iowrite8(recover << 4 | active, ap->ioaddr.device_addr);
  339. if (adev->class != ATA_DEV_ATA) {
  340. u8 rconf = inb(0x3E6);
  341. if (rconf & 0x24) {
  342. rconf &= ~ 0x24;
  343. outb(rconf, 0x3E6);
  344. }
  345. }
  346. ioread8(ap->ioaddr.status_addr);
  347. }
  348. static struct ata_port_operations ht6560b_port_ops = {
  349. .set_piomode = ht6560b_set_piomode,
  350. .port_disable = ata_port_disable,
  351. .tf_load = ata_tf_load,
  352. .tf_read = ata_tf_read,
  353. .check_status = ata_check_status,
  354. .exec_command = ata_exec_command,
  355. .dev_select = ata_std_dev_select,
  356. .freeze = ata_bmdma_freeze,
  357. .thaw = ata_bmdma_thaw,
  358. .error_handler = ata_bmdma_error_handler,
  359. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  360. .cable_detect = ata_cable_40wire,
  361. .qc_prep = ata_qc_prep,
  362. .qc_issue = ata_qc_issue_prot,
  363. .data_xfer = ata_data_xfer, /* FIXME: Check 32bit and noirq */
  364. .irq_handler = ata_interrupt,
  365. .irq_clear = ata_bmdma_irq_clear,
  366. .irq_on = ata_irq_on,
  367. .irq_ack = ata_irq_ack,
  368. .port_start = ata_port_start,
  369. };
  370. /*
  371. * Opti core chipset helpers
  372. */
  373. /**
  374. * opti_syscfg - read OPTI chipset configuration
  375. * @reg: Configuration register to read
  376. *
  377. * Returns the value of an OPTI system board configuration register.
  378. */
  379. static u8 opti_syscfg(u8 reg)
  380. {
  381. unsigned long flags;
  382. u8 r;
  383. /* Uniprocessor chipset and must force cycles adjancent */
  384. local_irq_save(flags);
  385. outb(reg, 0x22);
  386. r = inb(0x24);
  387. local_irq_restore(flags);
  388. return r;
  389. }
  390. /*
  391. * Opti 82C611A
  392. *
  393. * This controller supports PIO0 to PIO3.
  394. */
  395. static void opti82c611a_set_piomode(struct ata_port *ap, struct ata_device *adev)
  396. {
  397. u8 active, recover, setup;
  398. struct ata_timing t;
  399. struct ata_device *pair = ata_dev_pair(adev);
  400. int clock;
  401. int khz[4] = { 50000, 40000, 33000, 25000 };
  402. u8 rc;
  403. /* Enter configuration mode */
  404. ioread16(ap->ioaddr.error_addr);
  405. ioread16(ap->ioaddr.error_addr);
  406. iowrite8(3, ap->ioaddr.nsect_addr);
  407. /* Read VLB clock strapping */
  408. clock = 1000000000 / khz[ioread8(ap->ioaddr.lbah_addr) & 0x03];
  409. /* Get the timing data in cycles */
  410. ata_timing_compute(adev, adev->pio_mode, &t, clock, 1000);
  411. /* Setup timing is shared */
  412. if (pair) {
  413. struct ata_timing tp;
  414. ata_timing_compute(pair, pair->pio_mode, &tp, clock, 1000);
  415. ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
  416. }
  417. active = FIT(t.active, 2, 17) - 2;
  418. recover = FIT(t.recover, 1, 16) - 1;
  419. setup = FIT(t.setup, 1, 4) - 1;
  420. /* Select the right timing bank for write timing */
  421. rc = ioread8(ap->ioaddr.lbal_addr);
  422. rc &= 0x7F;
  423. rc |= (adev->devno << 7);
  424. iowrite8(rc, ap->ioaddr.lbal_addr);
  425. /* Write the timings */
  426. iowrite8(active << 4 | recover, ap->ioaddr.error_addr);
  427. /* Select the right bank for read timings, also
  428. load the shared timings for address */
  429. rc = ioread8(ap->ioaddr.device_addr);
  430. rc &= 0xC0;
  431. rc |= adev->devno; /* Index select */
  432. rc |= (setup << 4) | 0x04;
  433. iowrite8(rc, ap->ioaddr.device_addr);
  434. /* Load the read timings */
  435. iowrite8(active << 4 | recover, ap->ioaddr.data_addr);
  436. /* Ensure the timing register mode is right */
  437. rc = ioread8(ap->ioaddr.lbal_addr);
  438. rc &= 0x73;
  439. rc |= 0x84;
  440. iowrite8(rc, ap->ioaddr.lbal_addr);
  441. /* Exit command mode */
  442. iowrite8(0x83, ap->ioaddr.nsect_addr);
  443. }
  444. static struct ata_port_operations opti82c611a_port_ops = {
  445. .set_piomode = opti82c611a_set_piomode,
  446. .port_disable = ata_port_disable,
  447. .tf_load = ata_tf_load,
  448. .tf_read = ata_tf_read,
  449. .check_status = ata_check_status,
  450. .exec_command = ata_exec_command,
  451. .dev_select = ata_std_dev_select,
  452. .freeze = ata_bmdma_freeze,
  453. .thaw = ata_bmdma_thaw,
  454. .error_handler = ata_bmdma_error_handler,
  455. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  456. .cable_detect = ata_cable_40wire,
  457. .qc_prep = ata_qc_prep,
  458. .qc_issue = ata_qc_issue_prot,
  459. .data_xfer = ata_data_xfer,
  460. .irq_handler = ata_interrupt,
  461. .irq_clear = ata_bmdma_irq_clear,
  462. .irq_on = ata_irq_on,
  463. .irq_ack = ata_irq_ack,
  464. .port_start = ata_port_start,
  465. };
  466. /*
  467. * Opti 82C465MV
  468. *
  469. * This controller supports PIO0 to PIO3. Unlike the 611A the MVB
  470. * version is dual channel but doesn't have a lot of unique registers.
  471. */
  472. static void opti82c46x_set_piomode(struct ata_port *ap, struct ata_device *adev)
  473. {
  474. u8 active, recover, setup;
  475. struct ata_timing t;
  476. struct ata_device *pair = ata_dev_pair(adev);
  477. int clock;
  478. int khz[4] = { 50000, 40000, 33000, 25000 };
  479. u8 rc;
  480. u8 sysclk;
  481. /* Get the clock */
  482. sysclk = opti_syscfg(0xAC) & 0xC0; /* BIOS set */
  483. /* Enter configuration mode */
  484. ioread16(ap->ioaddr.error_addr);
  485. ioread16(ap->ioaddr.error_addr);
  486. iowrite8(3, ap->ioaddr.nsect_addr);
  487. /* Read VLB clock strapping */
  488. clock = 1000000000 / khz[sysclk];
  489. /* Get the timing data in cycles */
  490. ata_timing_compute(adev, adev->pio_mode, &t, clock, 1000);
  491. /* Setup timing is shared */
  492. if (pair) {
  493. struct ata_timing tp;
  494. ata_timing_compute(pair, pair->pio_mode, &tp, clock, 1000);
  495. ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
  496. }
  497. active = FIT(t.active, 2, 17) - 2;
  498. recover = FIT(t.recover, 1, 16) - 1;
  499. setup = FIT(t.setup, 1, 4) - 1;
  500. /* Select the right timing bank for write timing */
  501. rc = ioread8(ap->ioaddr.lbal_addr);
  502. rc &= 0x7F;
  503. rc |= (adev->devno << 7);
  504. iowrite8(rc, ap->ioaddr.lbal_addr);
  505. /* Write the timings */
  506. iowrite8(active << 4 | recover, ap->ioaddr.error_addr);
  507. /* Select the right bank for read timings, also
  508. load the shared timings for address */
  509. rc = ioread8(ap->ioaddr.device_addr);
  510. rc &= 0xC0;
  511. rc |= adev->devno; /* Index select */
  512. rc |= (setup << 4) | 0x04;
  513. iowrite8(rc, ap->ioaddr.device_addr);
  514. /* Load the read timings */
  515. iowrite8(active << 4 | recover, ap->ioaddr.data_addr);
  516. /* Ensure the timing register mode is right */
  517. rc = ioread8(ap->ioaddr.lbal_addr);
  518. rc &= 0x73;
  519. rc |= 0x84;
  520. iowrite8(rc, ap->ioaddr.lbal_addr);
  521. /* Exit command mode */
  522. iowrite8(0x83, ap->ioaddr.nsect_addr);
  523. /* We need to know this for quad device on the MVB */
  524. ap->host->private_data = ap;
  525. }
  526. /**
  527. * opt82c465mv_qc_issue_prot - command issue
  528. * @qc: command pending
  529. *
  530. * Called when the libata layer is about to issue a command. We wrap
  531. * this interface so that we can load the correct ATA timings. The
  532. * MVB has a single set of timing registers and these are shared
  533. * across channels. As there are two registers we really ought to
  534. * track the last two used values as a sort of register window. For
  535. * now we just reload on a channel switch. On the single channel
  536. * setup this condition never fires so we do nothing extra.
  537. *
  538. * FIXME: dual channel needs ->serialize support
  539. */
  540. static unsigned int opti82c46x_qc_issue_prot(struct ata_queued_cmd *qc)
  541. {
  542. struct ata_port *ap = qc->ap;
  543. struct ata_device *adev = qc->dev;
  544. /* If timings are set and for the wrong channel (2nd test is
  545. due to a libata shortcoming and will eventually go I hope) */
  546. if (ap->host->private_data != ap->host
  547. && ap->host->private_data != NULL)
  548. opti82c46x_set_piomode(ap, adev);
  549. return ata_qc_issue_prot(qc);
  550. }
  551. static struct ata_port_operations opti82c46x_port_ops = {
  552. .set_piomode = opti82c46x_set_piomode,
  553. .port_disable = ata_port_disable,
  554. .tf_load = ata_tf_load,
  555. .tf_read = ata_tf_read,
  556. .check_status = ata_check_status,
  557. .exec_command = ata_exec_command,
  558. .dev_select = ata_std_dev_select,
  559. .freeze = ata_bmdma_freeze,
  560. .thaw = ata_bmdma_thaw,
  561. .error_handler = ata_bmdma_error_handler,
  562. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  563. .cable_detect = ata_cable_40wire,
  564. .qc_prep = ata_qc_prep,
  565. .qc_issue = opti82c46x_qc_issue_prot,
  566. .data_xfer = ata_data_xfer,
  567. .irq_handler = ata_interrupt,
  568. .irq_clear = ata_bmdma_irq_clear,
  569. .irq_on = ata_irq_on,
  570. .irq_ack = ata_irq_ack,
  571. .port_start = ata_port_start,
  572. };
  573. /**
  574. * legacy_init_one - attach a legacy interface
  575. * @port: port number
  576. * @io: I/O port start
  577. * @ctrl: control port
  578. * @irq: interrupt line
  579. *
  580. * Register an ISA bus IDE interface. Such interfaces are PIO and we
  581. * assume do not support IRQ sharing.
  582. */
  583. static __init int legacy_init_one(int port, unsigned long io, unsigned long ctrl, int irq)
  584. {
  585. struct legacy_data *ld = &legacy_data[nr_legacy_host];
  586. struct ata_host *host;
  587. struct ata_port *ap;
  588. struct platform_device *pdev;
  589. struct ata_port_operations *ops = &legacy_port_ops;
  590. void __iomem *io_addr, *ctrl_addr;
  591. int pio_modes = pio_mask;
  592. u32 mask = (1 << port);
  593. u32 iordy = (iordy_mask & mask) ? 0: ATA_FLAG_NO_IORDY;
  594. int ret;
  595. pdev = platform_device_register_simple(DRV_NAME, nr_legacy_host, NULL, 0);
  596. if (IS_ERR(pdev))
  597. return PTR_ERR(pdev);
  598. ret = -EBUSY;
  599. if (devm_request_region(&pdev->dev, io, 8, "pata_legacy") == NULL ||
  600. devm_request_region(&pdev->dev, ctrl, 1, "pata_legacy") == NULL)
  601. goto fail;
  602. ret = -ENOMEM;
  603. io_addr = devm_ioport_map(&pdev->dev, io, 8);
  604. ctrl_addr = devm_ioport_map(&pdev->dev, ctrl, 1);
  605. if (!io_addr || !ctrl_addr)
  606. goto fail;
  607. if (ht6560a & mask) {
  608. ops = &ht6560a_port_ops;
  609. pio_modes = 0x07;
  610. iordy = ATA_FLAG_NO_IORDY;
  611. }
  612. if (ht6560b & mask) {
  613. ops = &ht6560b_port_ops;
  614. pio_modes = 0x1F;
  615. }
  616. if (opti82c611a & mask) {
  617. ops = &opti82c611a_port_ops;
  618. pio_modes = 0x0F;
  619. }
  620. if (opti82c46x & mask) {
  621. ops = &opti82c46x_port_ops;
  622. pio_modes = 0x0F;
  623. }
  624. /* Probe for automatically detectable controllers */
  625. if (io == 0x1F0 && ops == &legacy_port_ops) {
  626. unsigned long flags;
  627. local_irq_save(flags);
  628. /* Probes */
  629. inb(0x1F5);
  630. outb(inb(0x1F2) | 0x80, 0x1F2);
  631. inb(0x1F2);
  632. inb(0x3F6);
  633. inb(0x3F6);
  634. inb(0x1F2);
  635. inb(0x1F2);
  636. if ((inb(0x1F2) & 0x80) == 0) {
  637. /* PDC20230c or 20630 ? */
  638. printk(KERN_INFO "PDC20230-C/20630 VLB ATA controller detected.\n");
  639. pio_modes = 0x07;
  640. ops = &pdc20230_port_ops;
  641. iordy = ATA_FLAG_NO_IORDY;
  642. udelay(100);
  643. inb(0x1F5);
  644. } else {
  645. outb(0x55, 0x1F2);
  646. inb(0x1F2);
  647. inb(0x1F2);
  648. if (inb(0x1F2) == 0x00) {
  649. printk(KERN_INFO "PDC20230-B VLB ATA controller detected.\n");
  650. }
  651. }
  652. local_irq_restore(flags);
  653. }
  654. /* Chip does mode setting by command snooping */
  655. if (ops == &legacy_port_ops && (autospeed & mask))
  656. ops = &simple_port_ops;
  657. ret = -ENOMEM;
  658. host = ata_host_alloc(&pdev->dev, 1);
  659. if (!host)
  660. goto fail;
  661. ap = host->ports[0];
  662. ap->ops = ops;
  663. ap->pio_mask = pio_modes;
  664. ap->flags |= ATA_FLAG_SLAVE_POSS | iordy;
  665. ap->ioaddr.cmd_addr = io_addr;
  666. ap->ioaddr.altstatus_addr = ctrl_addr;
  667. ap->ioaddr.ctl_addr = ctrl_addr;
  668. ata_std_ports(&ap->ioaddr);
  669. ap->private_data = ld;
  670. ret = ata_host_activate(host, irq, ata_interrupt, 0, &legacy_sht);
  671. if (ret)
  672. goto fail;
  673. legacy_host[nr_legacy_host++] = dev_get_drvdata(&pdev->dev);
  674. ld->platform_dev = pdev;
  675. return 0;
  676. fail:
  677. platform_device_unregister(pdev);
  678. return ret;
  679. }
  680. /**
  681. * legacy_check_special_cases - ATA special cases
  682. * @p: PCI device to check
  683. * @master: set this if we find an ATA master
  684. * @master: set this if we find an ATA secondary
  685. *
  686. * A small number of vendors implemented early PCI ATA interfaces on bridge logic
  687. * without the ATA interface being PCI visible. Where we have a matching PCI driver
  688. * we must skip the relevant device here. If we don't know about it then the legacy
  689. * driver is the right driver anyway.
  690. */
  691. static void legacy_check_special_cases(struct pci_dev *p, int *primary, int *secondary)
  692. {
  693. /* Cyrix CS5510 pre SFF MWDMA ATA on the bridge */
  694. if (p->vendor == 0x1078 && p->device == 0x0000) {
  695. *primary = *secondary = 1;
  696. return;
  697. }
  698. /* Cyrix CS5520 pre SFF MWDMA ATA on the bridge */
  699. if (p->vendor == 0x1078 && p->device == 0x0002) {
  700. *primary = *secondary = 1;
  701. return;
  702. }
  703. /* Intel MPIIX - PIO ATA on non PCI side of bridge */
  704. if (p->vendor == 0x8086 && p->device == 0x1234) {
  705. u16 r;
  706. pci_read_config_word(p, 0x6C, &r);
  707. if (r & 0x8000) { /* ATA port enabled */
  708. if (r & 0x4000)
  709. *secondary = 1;
  710. else
  711. *primary = 1;
  712. }
  713. return;
  714. }
  715. }
  716. /**
  717. * legacy_init - attach legacy interfaces
  718. *
  719. * Attach legacy IDE interfaces by scanning the usual IRQ/port suspects.
  720. * Right now we do not scan the ide0 and ide1 address but should do so
  721. * for non PCI systems or systems with no PCI IDE legacy mode devices.
  722. * If you fix that note there are special cases to consider like VLB
  723. * drivers and CS5510/20.
  724. */
  725. static __init int legacy_init(void)
  726. {
  727. int i;
  728. int ct = 0;
  729. int primary = 0;
  730. int secondary = 0;
  731. int last_port = NR_HOST;
  732. struct pci_dev *p = NULL;
  733. for_each_pci_dev(p) {
  734. int r;
  735. /* Check for any overlap of the system ATA mappings. Native mode controllers
  736. stuck on these addresses or some devices in 'raid' mode won't be found by
  737. the storage class test */
  738. for (r = 0; r < 6; r++) {
  739. if (pci_resource_start(p, r) == 0x1f0)
  740. primary = 1;
  741. if (pci_resource_start(p, r) == 0x170)
  742. secondary = 1;
  743. }
  744. /* Check for special cases */
  745. legacy_check_special_cases(p, &primary, &secondary);
  746. /* If PCI bus is present then don't probe for tertiary legacy ports */
  747. if (probe_all == 0)
  748. last_port = 2;
  749. }
  750. /* If an OPTI 82C46X is present find out where the channels are */
  751. if (opti82c46x) {
  752. static const char *optis[4] = {
  753. "3/463MV", "5MV",
  754. "5MVA", "5MVB"
  755. };
  756. u8 chans = 1;
  757. u8 ctrl = (opti_syscfg(0x30) & 0xC0) >> 6;
  758. opti82c46x = 3; /* Assume master and slave first */
  759. printk(KERN_INFO DRV_NAME ": Opti 82C46%s chipset support.\n", optis[ctrl]);
  760. if (ctrl == 3)
  761. chans = (opti_syscfg(0x3F) & 0x20) ? 2 : 1;
  762. ctrl = opti_syscfg(0xAC);
  763. /* Check enabled and this port is the 465MV port. On the
  764. MVB we may have two channels */
  765. if (ctrl & 8) {
  766. if (ctrl & 4)
  767. opti82c46x = 2; /* Slave */
  768. else
  769. opti82c46x = 1; /* Master */
  770. if (chans == 2)
  771. opti82c46x = 3; /* Master and Slave */
  772. } /* Slave only */
  773. else if (chans == 1)
  774. opti82c46x = 1;
  775. }
  776. for (i = 0; i < last_port; i++) {
  777. /* Skip primary if we have seen a PCI one */
  778. if (i == 0 && primary == 1)
  779. continue;
  780. /* Skip secondary if we have seen a PCI one */
  781. if (i == 1 && secondary == 1)
  782. continue;
  783. if (legacy_init_one(i, legacy_port[i],
  784. legacy_port[i] + 0x0206,
  785. legacy_irq[i]) == 0)
  786. ct++;
  787. }
  788. if (ct != 0)
  789. return 0;
  790. return -ENODEV;
  791. }
  792. static __exit void legacy_exit(void)
  793. {
  794. int i;
  795. for (i = 0; i < nr_legacy_host; i++) {
  796. struct legacy_data *ld = &legacy_data[i];
  797. ata_host_detach(legacy_host[i]);
  798. platform_device_unregister(ld->platform_dev);
  799. if (ld->timing)
  800. release_region(ld->timing, 2);
  801. }
  802. }
  803. MODULE_AUTHOR("Alan Cox");
  804. MODULE_DESCRIPTION("low-level driver for legacy ATA");
  805. MODULE_LICENSE("GPL");
  806. MODULE_VERSION(DRV_VERSION);
  807. module_param(probe_all, int, 0);
  808. module_param(autospeed, int, 0);
  809. module_param(ht6560a, int, 0);
  810. module_param(ht6560b, int, 0);
  811. module_param(opti82c611a, int, 0);
  812. module_param(opti82c46x, int, 0);
  813. module_param(pio_mask, int, 0);
  814. module_param(iordy_mask, int, 0);
  815. module_init(legacy_init);
  816. module_exit(legacy_exit);