pata_pdc2027x.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. * Promise PATA TX2/TX4/TX2000/133 IDE driver for pdc20268 to pdc20277.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Ported to libata by:
  10. * Albert Lee <albertcc@tw.ibm.com> IBM Corporation
  11. *
  12. * Copyright (C) 1998-2002 Andre Hedrick <andre@linux-ide.org>
  13. * Portions Copyright (C) 1999 Promise Technology, Inc.
  14. *
  15. * Author: Frank Tiernan (frankt@promise.com)
  16. * Released under terms of General Public License
  17. *
  18. *
  19. * libata documentation is available via 'make {ps|pdf}docs',
  20. * as Documentation/DocBook/libata.*
  21. *
  22. * Hardware information only available under NDA.
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/init.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/delay.h>
  31. #include <linux/device.h>
  32. #include <scsi/scsi.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi_cmnd.h>
  35. #include <linux/libata.h>
  36. #include <asm/io.h>
  37. #define DRV_NAME "pata_pdc2027x"
  38. #define DRV_VERSION "0.74-ac5"
  39. #undef PDC_DEBUG
  40. #ifdef PDC_DEBUG
  41. #define PDPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ## args)
  42. #else
  43. #define PDPRINTK(fmt, args...)
  44. #endif
  45. enum {
  46. PDC_UDMA_100 = 0,
  47. PDC_UDMA_133 = 1,
  48. PDC_100_MHZ = 100000000,
  49. PDC_133_MHZ = 133333333,
  50. PDC_SYS_CTL = 0x1100,
  51. PDC_ATA_CTL = 0x1104,
  52. PDC_GLOBAL_CTL = 0x1108,
  53. PDC_CTCR0 = 0x110C,
  54. PDC_CTCR1 = 0x1110,
  55. PDC_BYTE_COUNT = 0x1120,
  56. PDC_PLL_CTL = 0x1202,
  57. };
  58. static int pdc2027x_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  59. static void pdc2027x_remove_one(struct pci_dev *pdev);
  60. static void pdc2027x_error_handler(struct ata_port *ap);
  61. static void pdc2027x_set_piomode(struct ata_port *ap, struct ata_device *adev);
  62. static void pdc2027x_set_dmamode(struct ata_port *ap, struct ata_device *adev);
  63. static void pdc2027x_post_set_mode(struct ata_port *ap);
  64. static int pdc2027x_check_atapi_dma(struct ata_queued_cmd *qc);
  65. /*
  66. * ATA Timing Tables based on 133MHz controller clock.
  67. * These tables are only used when the controller is in 133MHz clock.
  68. * If the controller is in 100MHz clock, the ASIC hardware will
  69. * set the timing registers automatically when "set feature" command
  70. * is issued to the device. However, if the controller clock is 133MHz,
  71. * the following tables must be used.
  72. */
  73. static struct pdc2027x_pio_timing {
  74. u8 value0, value1, value2;
  75. } pdc2027x_pio_timing_tbl [] = {
  76. { 0xfb, 0x2b, 0xac }, /* PIO mode 0 */
  77. { 0x46, 0x29, 0xa4 }, /* PIO mode 1 */
  78. { 0x23, 0x26, 0x64 }, /* PIO mode 2 */
  79. { 0x27, 0x0d, 0x35 }, /* PIO mode 3, IORDY on, Prefetch off */
  80. { 0x23, 0x09, 0x25 }, /* PIO mode 4, IORDY on, Prefetch off */
  81. };
  82. static struct pdc2027x_mdma_timing {
  83. u8 value0, value1;
  84. } pdc2027x_mdma_timing_tbl [] = {
  85. { 0xdf, 0x5f }, /* MDMA mode 0 */
  86. { 0x6b, 0x27 }, /* MDMA mode 1 */
  87. { 0x69, 0x25 }, /* MDMA mode 2 */
  88. };
  89. static struct pdc2027x_udma_timing {
  90. u8 value0, value1, value2;
  91. } pdc2027x_udma_timing_tbl [] = {
  92. { 0x4a, 0x0f, 0xd5 }, /* UDMA mode 0 */
  93. { 0x3a, 0x0a, 0xd0 }, /* UDMA mode 1 */
  94. { 0x2a, 0x07, 0xcd }, /* UDMA mode 2 */
  95. { 0x1a, 0x05, 0xcd }, /* UDMA mode 3 */
  96. { 0x1a, 0x03, 0xcd }, /* UDMA mode 4 */
  97. { 0x1a, 0x02, 0xcb }, /* UDMA mode 5 */
  98. { 0x1a, 0x01, 0xcb }, /* UDMA mode 6 */
  99. };
  100. static const struct pci_device_id pdc2027x_pci_tbl[] = {
  101. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20268, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PDC_UDMA_100 },
  102. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20269, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PDC_UDMA_133 },
  103. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20270, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PDC_UDMA_100 },
  104. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20271, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PDC_UDMA_133 },
  105. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20275, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PDC_UDMA_133 },
  106. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20276, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PDC_UDMA_133 },
  107. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20277, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PDC_UDMA_133 },
  108. { } /* terminate list */
  109. };
  110. static struct pci_driver pdc2027x_pci_driver = {
  111. .name = DRV_NAME,
  112. .id_table = pdc2027x_pci_tbl,
  113. .probe = pdc2027x_init_one,
  114. .remove = __devexit_p(pdc2027x_remove_one),
  115. };
  116. static struct scsi_host_template pdc2027x_sht = {
  117. .module = THIS_MODULE,
  118. .name = DRV_NAME,
  119. .ioctl = ata_scsi_ioctl,
  120. .queuecommand = ata_scsi_queuecmd,
  121. .can_queue = ATA_DEF_QUEUE,
  122. .this_id = ATA_SHT_THIS_ID,
  123. .sg_tablesize = LIBATA_MAX_PRD,
  124. .max_sectors = ATA_MAX_SECTORS,
  125. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  126. .emulated = ATA_SHT_EMULATED,
  127. .use_clustering = ATA_SHT_USE_CLUSTERING,
  128. .proc_name = DRV_NAME,
  129. .dma_boundary = ATA_DMA_BOUNDARY,
  130. .slave_configure = ata_scsi_slave_config,
  131. .bios_param = ata_std_bios_param,
  132. };
  133. static struct ata_port_operations pdc2027x_pata100_ops = {
  134. .port_disable = ata_port_disable,
  135. .tf_load = ata_tf_load,
  136. .tf_read = ata_tf_read,
  137. .check_status = ata_check_status,
  138. .exec_command = ata_exec_command,
  139. .dev_select = ata_std_dev_select,
  140. .check_atapi_dma = pdc2027x_check_atapi_dma,
  141. .bmdma_setup = ata_bmdma_setup,
  142. .bmdma_start = ata_bmdma_start,
  143. .bmdma_stop = ata_bmdma_stop,
  144. .bmdma_status = ata_bmdma_status,
  145. .qc_prep = ata_qc_prep,
  146. .qc_issue = ata_qc_issue_prot,
  147. .data_xfer = ata_mmio_data_xfer,
  148. .freeze = ata_bmdma_freeze,
  149. .thaw = ata_bmdma_thaw,
  150. .error_handler = pdc2027x_error_handler,
  151. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  152. .irq_handler = ata_interrupt,
  153. .irq_clear = ata_bmdma_irq_clear,
  154. .port_start = ata_port_start,
  155. .port_stop = ata_port_stop,
  156. .host_stop = ata_pci_host_stop,
  157. };
  158. static struct ata_port_operations pdc2027x_pata133_ops = {
  159. .port_disable = ata_port_disable,
  160. .set_piomode = pdc2027x_set_piomode,
  161. .set_dmamode = pdc2027x_set_dmamode,
  162. .post_set_mode = pdc2027x_post_set_mode,
  163. .tf_load = ata_tf_load,
  164. .tf_read = ata_tf_read,
  165. .check_status = ata_check_status,
  166. .exec_command = ata_exec_command,
  167. .dev_select = ata_std_dev_select,
  168. .check_atapi_dma = pdc2027x_check_atapi_dma,
  169. .bmdma_setup = ata_bmdma_setup,
  170. .bmdma_start = ata_bmdma_start,
  171. .bmdma_stop = ata_bmdma_stop,
  172. .bmdma_status = ata_bmdma_status,
  173. .qc_prep = ata_qc_prep,
  174. .qc_issue = ata_qc_issue_prot,
  175. .data_xfer = ata_mmio_data_xfer,
  176. .freeze = ata_bmdma_freeze,
  177. .thaw = ata_bmdma_thaw,
  178. .error_handler = pdc2027x_error_handler,
  179. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  180. .irq_handler = ata_interrupt,
  181. .irq_clear = ata_bmdma_irq_clear,
  182. .port_start = ata_port_start,
  183. .port_stop = ata_port_stop,
  184. .host_stop = ata_pci_host_stop,
  185. };
  186. static struct ata_port_info pdc2027x_port_info[] = {
  187. /* PDC_UDMA_100 */
  188. {
  189. .sht = &pdc2027x_sht,
  190. .flags = ATA_FLAG_NO_LEGACY | ATA_FLAG_SLAVE_POSS |
  191. ATA_FLAG_MMIO,
  192. .pio_mask = 0x1f, /* pio0-4 */
  193. .mwdma_mask = 0x07, /* mwdma0-2 */
  194. .udma_mask = ATA_UDMA5, /* udma0-5 */
  195. .port_ops = &pdc2027x_pata100_ops,
  196. },
  197. /* PDC_UDMA_133 */
  198. {
  199. .sht = &pdc2027x_sht,
  200. .flags = ATA_FLAG_NO_LEGACY | ATA_FLAG_SLAVE_POSS |
  201. ATA_FLAG_MMIO,
  202. .pio_mask = 0x1f, /* pio0-4 */
  203. .mwdma_mask = 0x07, /* mwdma0-2 */
  204. .udma_mask = ATA_UDMA6, /* udma0-6 */
  205. .port_ops = &pdc2027x_pata133_ops,
  206. },
  207. };
  208. MODULE_AUTHOR("Andre Hedrick, Frank Tiernan, Albert Lee");
  209. MODULE_DESCRIPTION("libata driver module for Promise PDC20268 to PDC20277");
  210. MODULE_LICENSE("GPL");
  211. MODULE_VERSION(DRV_VERSION);
  212. MODULE_DEVICE_TABLE(pci, pdc2027x_pci_tbl);
  213. /**
  214. * port_mmio - Get the MMIO address of PDC2027x extended registers
  215. * @ap: Port
  216. * @offset: offset from mmio base
  217. */
  218. static inline void __iomem *port_mmio(struct ata_port *ap, unsigned int offset)
  219. {
  220. return ap->host->mmio_base + ap->port_no * 0x100 + offset;
  221. }
  222. /**
  223. * dev_mmio - Get the MMIO address of PDC2027x extended registers
  224. * @ap: Port
  225. * @adev: device
  226. * @offset: offset from mmio base
  227. */
  228. static inline void __iomem *dev_mmio(struct ata_port *ap, struct ata_device *adev, unsigned int offset)
  229. {
  230. u8 adj = (adev->devno) ? 0x08 : 0x00;
  231. return port_mmio(ap, offset) + adj;
  232. }
  233. /**
  234. * pdc2027x_pata_cbl_detect - Probe host controller cable detect info
  235. * @ap: Port for which cable detect info is desired
  236. *
  237. * Read 80c cable indicator from Promise extended register.
  238. * This register is latched when the system is reset.
  239. *
  240. * LOCKING:
  241. * None (inherited from caller).
  242. */
  243. static void pdc2027x_cbl_detect(struct ata_port *ap)
  244. {
  245. u32 cgcr;
  246. /* check cable detect results */
  247. cgcr = readl(port_mmio(ap, PDC_GLOBAL_CTL));
  248. if (cgcr & (1 << 26))
  249. goto cbl40;
  250. PDPRINTK("No cable or 80-conductor cable on port %d\n", ap->port_no);
  251. ap->cbl = ATA_CBL_PATA80;
  252. return;
  253. cbl40:
  254. printk(KERN_INFO DRV_NAME ": 40-conductor cable detected on port %d\n", ap->port_no);
  255. ap->cbl = ATA_CBL_PATA40;
  256. ap->udma_mask &= ATA_UDMA_MASK_40C;
  257. }
  258. /**
  259. * pdc2027x_port_enabled - Check PDC ATA control register to see whether the port is enabled.
  260. * @ap: Port to check
  261. */
  262. static inline int pdc2027x_port_enabled(struct ata_port *ap)
  263. {
  264. return readb(port_mmio(ap, PDC_ATA_CTL)) & 0x02;
  265. }
  266. /**
  267. * pdc2027x_prereset - prereset for PATA host controller
  268. * @ap: Target port
  269. *
  270. * Probeinit including cable detection.
  271. *
  272. * LOCKING:
  273. * None (inherited from caller).
  274. */
  275. static int pdc2027x_prereset(struct ata_port *ap)
  276. {
  277. /* Check whether port enabled */
  278. if (!pdc2027x_port_enabled(ap))
  279. return -ENOENT;
  280. pdc2027x_cbl_detect(ap);
  281. return ata_std_prereset(ap);
  282. }
  283. /**
  284. * pdc2027x_error_handler - Perform reset on PATA port and classify
  285. * @ap: Port to reset
  286. *
  287. * Reset PATA phy and classify attached devices.
  288. *
  289. * LOCKING:
  290. * None (inherited from caller).
  291. */
  292. static void pdc2027x_error_handler(struct ata_port *ap)
  293. {
  294. ata_bmdma_drive_eh(ap, pdc2027x_prereset, ata_std_softreset, NULL, ata_std_postreset);
  295. }
  296. /**
  297. * pdc2027x_set_piomode - Initialize host controller PATA PIO timings
  298. * @ap: Port to configure
  299. * @adev: um
  300. * @pio: PIO mode, 0 - 4
  301. *
  302. * Set PIO mode for device.
  303. *
  304. * LOCKING:
  305. * None (inherited from caller).
  306. */
  307. static void pdc2027x_set_piomode(struct ata_port *ap, struct ata_device *adev)
  308. {
  309. unsigned int pio = adev->pio_mode - XFER_PIO_0;
  310. u32 ctcr0, ctcr1;
  311. PDPRINTK("adev->pio_mode[%X]\n", adev->pio_mode);
  312. /* Sanity check */
  313. if (pio > 4) {
  314. printk(KERN_ERR DRV_NAME ": Unknown pio mode [%d] ignored\n", pio);
  315. return;
  316. }
  317. /* Set the PIO timing registers using value table for 133MHz */
  318. PDPRINTK("Set pio regs... \n");
  319. ctcr0 = readl(dev_mmio(ap, adev, PDC_CTCR0));
  320. ctcr0 &= 0xffff0000;
  321. ctcr0 |= pdc2027x_pio_timing_tbl[pio].value0 |
  322. (pdc2027x_pio_timing_tbl[pio].value1 << 8);
  323. writel(ctcr0, dev_mmio(ap, adev, PDC_CTCR0));
  324. ctcr1 = readl(dev_mmio(ap, adev, PDC_CTCR1));
  325. ctcr1 &= 0x00ffffff;
  326. ctcr1 |= (pdc2027x_pio_timing_tbl[pio].value2 << 24);
  327. writel(ctcr1, dev_mmio(ap, adev, PDC_CTCR1));
  328. PDPRINTK("Set pio regs done\n");
  329. PDPRINTK("Set to pio mode[%u] \n", pio);
  330. }
  331. /**
  332. * pdc2027x_set_dmamode - Initialize host controller PATA UDMA timings
  333. * @ap: Port to configure
  334. * @adev: um
  335. * @udma: udma mode, XFER_UDMA_0 to XFER_UDMA_6
  336. *
  337. * Set UDMA mode for device.
  338. *
  339. * LOCKING:
  340. * None (inherited from caller).
  341. */
  342. static void pdc2027x_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  343. {
  344. unsigned int dma_mode = adev->dma_mode;
  345. u32 ctcr0, ctcr1;
  346. if ((dma_mode >= XFER_UDMA_0) &&
  347. (dma_mode <= XFER_UDMA_6)) {
  348. /* Set the UDMA timing registers with value table for 133MHz */
  349. unsigned int udma_mode = dma_mode & 0x07;
  350. if (dma_mode == XFER_UDMA_2) {
  351. /*
  352. * Turn off tHOLD.
  353. * If tHOLD is '1', the hardware will add half clock for data hold time.
  354. * This code segment seems to be no effect. tHOLD will be overwritten below.
  355. */
  356. ctcr1 = readl(dev_mmio(ap, adev, PDC_CTCR1));
  357. writel(ctcr1 & ~(1 << 7), dev_mmio(ap, adev, PDC_CTCR1));
  358. }
  359. PDPRINTK("Set udma regs... \n");
  360. ctcr1 = readl(dev_mmio(ap, adev, PDC_CTCR1));
  361. ctcr1 &= 0xff000000;
  362. ctcr1 |= pdc2027x_udma_timing_tbl[udma_mode].value0 |
  363. (pdc2027x_udma_timing_tbl[udma_mode].value1 << 8) |
  364. (pdc2027x_udma_timing_tbl[udma_mode].value2 << 16);
  365. writel(ctcr1, dev_mmio(ap, adev, PDC_CTCR1));
  366. PDPRINTK("Set udma regs done\n");
  367. PDPRINTK("Set to udma mode[%u] \n", udma_mode);
  368. } else if ((dma_mode >= XFER_MW_DMA_0) &&
  369. (dma_mode <= XFER_MW_DMA_2)) {
  370. /* Set the MDMA timing registers with value table for 133MHz */
  371. unsigned int mdma_mode = dma_mode & 0x07;
  372. PDPRINTK("Set mdma regs... \n");
  373. ctcr0 = readl(dev_mmio(ap, adev, PDC_CTCR0));
  374. ctcr0 &= 0x0000ffff;
  375. ctcr0 |= (pdc2027x_mdma_timing_tbl[mdma_mode].value0 << 16) |
  376. (pdc2027x_mdma_timing_tbl[mdma_mode].value1 << 24);
  377. writel(ctcr0, dev_mmio(ap, adev, PDC_CTCR0));
  378. PDPRINTK("Set mdma regs done\n");
  379. PDPRINTK("Set to mdma mode[%u] \n", mdma_mode);
  380. } else {
  381. printk(KERN_ERR DRV_NAME ": Unknown dma mode [%u] ignored\n", dma_mode);
  382. }
  383. }
  384. /**
  385. * pdc2027x_post_set_mode - Set the timing registers back to correct values.
  386. * @ap: Port to configure
  387. *
  388. * The pdc2027x hardware will look at "SET FEATURES" and change the timing registers
  389. * automatically. The values set by the hardware might be incorrect, under 133Mhz PLL.
  390. * This function overwrites the possibly incorrect values set by the hardware to be correct.
  391. */
  392. static void pdc2027x_post_set_mode(struct ata_port *ap)
  393. {
  394. int i;
  395. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  396. struct ata_device *dev = &ap->device[i];
  397. if (ata_dev_enabled(dev)) {
  398. pdc2027x_set_piomode(ap, dev);
  399. /*
  400. * Enable prefetch if the device support PIO only.
  401. */
  402. if (dev->xfer_shift == ATA_SHIFT_PIO) {
  403. u32 ctcr1 = readl(dev_mmio(ap, dev, PDC_CTCR1));
  404. ctcr1 |= (1 << 25);
  405. writel(ctcr1, dev_mmio(ap, dev, PDC_CTCR1));
  406. PDPRINTK("Turn on prefetch\n");
  407. } else {
  408. pdc2027x_set_dmamode(ap, dev);
  409. }
  410. }
  411. }
  412. }
  413. /**
  414. * pdc2027x_check_atapi_dma - Check whether ATAPI DMA can be supported for this command
  415. * @qc: Metadata associated with taskfile to check
  416. *
  417. * LOCKING:
  418. * None (inherited from caller).
  419. *
  420. * RETURNS: 0 when ATAPI DMA can be used
  421. * 1 otherwise
  422. */
  423. static int pdc2027x_check_atapi_dma(struct ata_queued_cmd *qc)
  424. {
  425. struct scsi_cmnd *cmd = qc->scsicmd;
  426. u8 *scsicmd = cmd->cmnd;
  427. int rc = 1; /* atapi dma off by default */
  428. /*
  429. * This workaround is from Promise's GPL driver.
  430. * If ATAPI DMA is used for commands not in the
  431. * following white list, say MODE_SENSE and REQUEST_SENSE,
  432. * pdc2027x might hit the irq lost problem.
  433. */
  434. switch (scsicmd[0]) {
  435. case READ_10:
  436. case WRITE_10:
  437. case READ_12:
  438. case WRITE_12:
  439. case READ_6:
  440. case WRITE_6:
  441. case 0xad: /* READ_DVD_STRUCTURE */
  442. case 0xbe: /* READ_CD */
  443. /* ATAPI DMA is ok */
  444. rc = 0;
  445. break;
  446. default:
  447. ;
  448. }
  449. return rc;
  450. }
  451. /**
  452. * pdc_read_counter - Read the ctr counter
  453. * @probe_ent: for the port address
  454. */
  455. static long pdc_read_counter(struct ata_probe_ent *probe_ent)
  456. {
  457. long counter;
  458. int retry = 1;
  459. u32 bccrl, bccrh, bccrlv, bccrhv;
  460. retry:
  461. bccrl = readl(probe_ent->mmio_base + PDC_BYTE_COUNT) & 0xffff;
  462. bccrh = readl(probe_ent->mmio_base + PDC_BYTE_COUNT + 0x100) & 0xffff;
  463. rmb();
  464. /* Read the counter values again for verification */
  465. bccrlv = readl(probe_ent->mmio_base + PDC_BYTE_COUNT) & 0xffff;
  466. bccrhv = readl(probe_ent->mmio_base + PDC_BYTE_COUNT + 0x100) & 0xffff;
  467. rmb();
  468. counter = (bccrh << 15) | bccrl;
  469. PDPRINTK("bccrh [%X] bccrl [%X]\n", bccrh, bccrl);
  470. PDPRINTK("bccrhv[%X] bccrlv[%X]\n", bccrhv, bccrlv);
  471. /*
  472. * The 30-bit decreasing counter are read by 2 pieces.
  473. * Incorrect value may be read when both bccrh and bccrl are changing.
  474. * Ex. When 7900 decrease to 78FF, wrong value 7800 might be read.
  475. */
  476. if (retry && !(bccrh == bccrhv && bccrl >= bccrlv)) {
  477. retry--;
  478. PDPRINTK("rereading counter\n");
  479. goto retry;
  480. }
  481. return counter;
  482. }
  483. /**
  484. * adjust_pll - Adjust the PLL input clock in Hz.
  485. *
  486. * @pdc_controller: controller specific information
  487. * @probe_ent: For the port address
  488. * @pll_clock: The input of PLL in HZ
  489. */
  490. static void pdc_adjust_pll(struct ata_probe_ent *probe_ent, long pll_clock, unsigned int board_idx)
  491. {
  492. u16 pll_ctl;
  493. long pll_clock_khz = pll_clock / 1000;
  494. long pout_required = board_idx? PDC_133_MHZ:PDC_100_MHZ;
  495. long ratio = pout_required / pll_clock_khz;
  496. int F, R;
  497. /* Sanity check */
  498. if (unlikely(pll_clock_khz < 5000L || pll_clock_khz > 70000L)) {
  499. printk(KERN_ERR DRV_NAME ": Invalid PLL input clock %ldkHz, give up!\n", pll_clock_khz);
  500. return;
  501. }
  502. #ifdef PDC_DEBUG
  503. PDPRINTK("pout_required is %ld\n", pout_required);
  504. /* Show the current clock value of PLL control register
  505. * (maybe already configured by the firmware)
  506. */
  507. pll_ctl = readw(probe_ent->mmio_base + PDC_PLL_CTL);
  508. PDPRINTK("pll_ctl[%X]\n", pll_ctl);
  509. #endif
  510. /*
  511. * Calculate the ratio of F, R and OD
  512. * POUT = (F + 2) / (( R + 2) * NO)
  513. */
  514. if (ratio < 8600L) { /* 8.6x */
  515. /* Using NO = 0x01, R = 0x0D */
  516. R = 0x0d;
  517. } else if (ratio < 12900L) { /* 12.9x */
  518. /* Using NO = 0x01, R = 0x08 */
  519. R = 0x08;
  520. } else if (ratio < 16100L) { /* 16.1x */
  521. /* Using NO = 0x01, R = 0x06 */
  522. R = 0x06;
  523. } else if (ratio < 64000L) { /* 64x */
  524. R = 0x00;
  525. } else {
  526. /* Invalid ratio */
  527. printk(KERN_ERR DRV_NAME ": Invalid ratio %ld, give up!\n", ratio);
  528. return;
  529. }
  530. F = (ratio * (R+2)) / 1000 - 2;
  531. if (unlikely(F < 0 || F > 127)) {
  532. /* Invalid F */
  533. printk(KERN_ERR DRV_NAME ": F[%d] invalid!\n", F);
  534. return;
  535. }
  536. PDPRINTK("F[%d] R[%d] ratio*1000[%ld]\n", F, R, ratio);
  537. pll_ctl = (R << 8) | F;
  538. PDPRINTK("Writing pll_ctl[%X]\n", pll_ctl);
  539. writew(pll_ctl, probe_ent->mmio_base + PDC_PLL_CTL);
  540. readw(probe_ent->mmio_base + PDC_PLL_CTL); /* flush */
  541. /* Wait the PLL circuit to be stable */
  542. mdelay(30);
  543. #ifdef PDC_DEBUG
  544. /*
  545. * Show the current clock value of PLL control register
  546. * (maybe configured by the firmware)
  547. */
  548. pll_ctl = readw(probe_ent->mmio_base + PDC_PLL_CTL);
  549. PDPRINTK("pll_ctl[%X]\n", pll_ctl);
  550. #endif
  551. return;
  552. }
  553. /**
  554. * detect_pll_input_clock - Detect the PLL input clock in Hz.
  555. * @probe_ent: for the port address
  556. * Ex. 16949000 on 33MHz PCI bus for pdc20275.
  557. * Half of the PCI clock.
  558. */
  559. static long pdc_detect_pll_input_clock(struct ata_probe_ent *probe_ent)
  560. {
  561. u32 scr;
  562. long start_count, end_count;
  563. long pll_clock;
  564. /* Read current counter value */
  565. start_count = pdc_read_counter(probe_ent);
  566. /* Start the test mode */
  567. scr = readl(probe_ent->mmio_base + PDC_SYS_CTL);
  568. PDPRINTK("scr[%X]\n", scr);
  569. writel(scr | (0x01 << 14), probe_ent->mmio_base + PDC_SYS_CTL);
  570. readl(probe_ent->mmio_base + PDC_SYS_CTL); /* flush */
  571. /* Let the counter run for 100 ms. */
  572. mdelay(100);
  573. /* Read the counter values again */
  574. end_count = pdc_read_counter(probe_ent);
  575. /* Stop the test mode */
  576. scr = readl(probe_ent->mmio_base + PDC_SYS_CTL);
  577. PDPRINTK("scr[%X]\n", scr);
  578. writel(scr & ~(0x01 << 14), probe_ent->mmio_base + PDC_SYS_CTL);
  579. readl(probe_ent->mmio_base + PDC_SYS_CTL); /* flush */
  580. /* calculate the input clock in Hz */
  581. pll_clock = (start_count - end_count) * 10;
  582. PDPRINTK("start[%ld] end[%ld] \n", start_count, end_count);
  583. PDPRINTK("PLL input clock[%ld]Hz\n", pll_clock);
  584. return pll_clock;
  585. }
  586. /**
  587. * pdc_hardware_init - Initialize the hardware.
  588. * @pdev: instance of pci_dev found
  589. * @pdc_controller: controller specific information
  590. * @pe: for the port address
  591. */
  592. static int pdc_hardware_init(struct pci_dev *pdev, struct ata_probe_ent *pe, unsigned int board_idx)
  593. {
  594. long pll_clock;
  595. /*
  596. * Detect PLL input clock rate.
  597. * On some system, where PCI bus is running at non-standard clock rate.
  598. * Ex. 25MHz or 40MHz, we have to adjust the cycle_time.
  599. * The pdc20275 controller employs PLL circuit to help correct timing registers setting.
  600. */
  601. pll_clock = pdc_detect_pll_input_clock(pe);
  602. if (pll_clock < 0) /* counter overflow? Try again. */
  603. pll_clock = pdc_detect_pll_input_clock(pe);
  604. dev_printk(KERN_INFO, &pdev->dev, "PLL input clock %ld kHz\n", pll_clock/1000);
  605. /* Adjust PLL control register */
  606. pdc_adjust_pll(pe, pll_clock, board_idx);
  607. return 0;
  608. }
  609. /**
  610. * pdc_ata_setup_port - setup the mmio address
  611. * @port: ata ioports to setup
  612. * @base: base address
  613. */
  614. static void pdc_ata_setup_port(struct ata_ioports *port, unsigned long base)
  615. {
  616. port->cmd_addr =
  617. port->data_addr = base;
  618. port->feature_addr =
  619. port->error_addr = base + 0x05;
  620. port->nsect_addr = base + 0x0a;
  621. port->lbal_addr = base + 0x0f;
  622. port->lbam_addr = base + 0x10;
  623. port->lbah_addr = base + 0x15;
  624. port->device_addr = base + 0x1a;
  625. port->command_addr =
  626. port->status_addr = base + 0x1f;
  627. port->altstatus_addr =
  628. port->ctl_addr = base + 0x81a;
  629. }
  630. /**
  631. * pdc2027x_init_one - PCI probe function
  632. * Called when an instance of PCI adapter is inserted.
  633. * This function checks whether the hardware is supported,
  634. * initialize hardware and register an instance of ata_host to
  635. * libata by providing struct ata_probe_ent and ata_device_add().
  636. * (implements struct pci_driver.probe() )
  637. *
  638. * @pdev: instance of pci_dev found
  639. * @ent: matching entry in the id_tbl[]
  640. */
  641. static int __devinit pdc2027x_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  642. {
  643. static int printed_version;
  644. unsigned int board_idx = (unsigned int) ent->driver_data;
  645. struct ata_probe_ent *probe_ent = NULL;
  646. unsigned long base;
  647. void __iomem *mmio_base;
  648. int rc;
  649. if (!printed_version++)
  650. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  651. rc = pci_enable_device(pdev);
  652. if (rc)
  653. return rc;
  654. rc = pci_request_regions(pdev, DRV_NAME);
  655. if (rc)
  656. goto err_out;
  657. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  658. if (rc)
  659. goto err_out_regions;
  660. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  661. if (rc)
  662. goto err_out_regions;
  663. /* Prepare the probe entry */
  664. probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
  665. if (probe_ent == NULL) {
  666. rc = -ENOMEM;
  667. goto err_out_regions;
  668. }
  669. probe_ent->dev = pci_dev_to_dev(pdev);
  670. INIT_LIST_HEAD(&probe_ent->node);
  671. mmio_base = pci_iomap(pdev, 5, 0);
  672. if (!mmio_base) {
  673. rc = -ENOMEM;
  674. goto err_out_free_ent;
  675. }
  676. base = (unsigned long) mmio_base;
  677. probe_ent->sht = pdc2027x_port_info[board_idx].sht;
  678. probe_ent->port_flags = pdc2027x_port_info[board_idx].flags;
  679. probe_ent->pio_mask = pdc2027x_port_info[board_idx].pio_mask;
  680. probe_ent->mwdma_mask = pdc2027x_port_info[board_idx].mwdma_mask;
  681. probe_ent->udma_mask = pdc2027x_port_info[board_idx].udma_mask;
  682. probe_ent->port_ops = pdc2027x_port_info[board_idx].port_ops;
  683. probe_ent->irq = pdev->irq;
  684. probe_ent->irq_flags = SA_SHIRQ;
  685. probe_ent->mmio_base = mmio_base;
  686. pdc_ata_setup_port(&probe_ent->port[0], base + 0x17c0);
  687. probe_ent->port[0].bmdma_addr = base + 0x1000;
  688. pdc_ata_setup_port(&probe_ent->port[1], base + 0x15c0);
  689. probe_ent->port[1].bmdma_addr = base + 0x1008;
  690. probe_ent->n_ports = 2;
  691. pci_set_master(pdev);
  692. //pci_enable_intx(pdev);
  693. /* initialize adapter */
  694. if (pdc_hardware_init(pdev, probe_ent, board_idx) != 0)
  695. goto err_out_free_ent;
  696. ata_device_add(probe_ent);
  697. kfree(probe_ent);
  698. return 0;
  699. err_out_free_ent:
  700. kfree(probe_ent);
  701. err_out_regions:
  702. pci_release_regions(pdev);
  703. err_out:
  704. pci_disable_device(pdev);
  705. return rc;
  706. }
  707. /**
  708. * pdc2027x_remove_one - Called to remove a single instance of the
  709. * adapter.
  710. *
  711. * @dev: The PCI device to remove.
  712. * FIXME: module load/unload not working yet
  713. */
  714. static void __devexit pdc2027x_remove_one(struct pci_dev *pdev)
  715. {
  716. ata_pci_remove_one(pdev);
  717. }
  718. /**
  719. * pdc2027x_init - Called after this module is loaded into the kernel.
  720. */
  721. static int __init pdc2027x_init(void)
  722. {
  723. return pci_module_init(&pdc2027x_pci_driver);
  724. }
  725. /**
  726. * pdc2027x_exit - Called before this module unloaded from the kernel
  727. */
  728. static void __exit pdc2027x_exit(void)
  729. {
  730. pci_unregister_driver(&pdc2027x_pci_driver);
  731. }
  732. module_init(pdc2027x_init);
  733. module_exit(pdc2027x_exit);