pata_pdc2027x.c 23 KB

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