pata_pdc2027x.c 22 KB

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