pata_pdc2027x.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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/blkdev.h>
  29. #include <linux/delay.h>
  30. #include <linux/device.h>
  31. #include <scsi/scsi.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <linux/libata.h>
  35. #define DRV_NAME "pata_pdc2027x"
  36. #define DRV_VERSION "1.0"
  37. #undef PDC_DEBUG
  38. #ifdef PDC_DEBUG
  39. #define PDPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __func__, ## args)
  40. #else
  41. #define PDPRINTK(fmt, args...)
  42. #endif
  43. enum {
  44. PDC_MMIO_BAR = 5,
  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. #ifdef CONFIG_PM_SLEEP
  59. static int pdc2027x_reinit_one(struct pci_dev *pdev);
  60. #endif
  61. static int pdc2027x_prereset(struct ata_link *link, unsigned long deadline);
  62. static void pdc2027x_set_piomode(struct ata_port *ap, struct ata_device *adev);
  63. static void pdc2027x_set_dmamode(struct ata_port *ap, struct ata_device *adev);
  64. static int pdc2027x_check_atapi_dma(struct ata_queued_cmd *qc);
  65. static unsigned long pdc2027x_mode_filter(struct ata_device *adev, unsigned long mask);
  66. static int pdc2027x_cable_detect(struct ata_port *ap);
  67. static int pdc2027x_set_mode(struct ata_link *link, struct ata_device **r_failed);
  68. /*
  69. * ATA Timing Tables based on 133MHz controller clock.
  70. * These tables are only used when the controller is in 133MHz clock.
  71. * If the controller is in 100MHz clock, the ASIC hardware will
  72. * set the timing registers automatically when "set feature" command
  73. * is issued to the device. However, if the controller clock is 133MHz,
  74. * the following tables must be used.
  75. */
  76. static struct pdc2027x_pio_timing {
  77. u8 value0, value1, value2;
  78. } pdc2027x_pio_timing_tbl [] = {
  79. { 0xfb, 0x2b, 0xac }, /* PIO mode 0 */
  80. { 0x46, 0x29, 0xa4 }, /* PIO mode 1 */
  81. { 0x23, 0x26, 0x64 }, /* PIO mode 2 */
  82. { 0x27, 0x0d, 0x35 }, /* PIO mode 3, IORDY on, Prefetch off */
  83. { 0x23, 0x09, 0x25 }, /* PIO mode 4, IORDY on, Prefetch off */
  84. };
  85. static struct pdc2027x_mdma_timing {
  86. u8 value0, value1;
  87. } pdc2027x_mdma_timing_tbl [] = {
  88. { 0xdf, 0x5f }, /* MDMA mode 0 */
  89. { 0x6b, 0x27 }, /* MDMA mode 1 */
  90. { 0x69, 0x25 }, /* MDMA mode 2 */
  91. };
  92. static struct pdc2027x_udma_timing {
  93. u8 value0, value1, value2;
  94. } pdc2027x_udma_timing_tbl [] = {
  95. { 0x4a, 0x0f, 0xd5 }, /* UDMA mode 0 */
  96. { 0x3a, 0x0a, 0xd0 }, /* UDMA mode 1 */
  97. { 0x2a, 0x07, 0xcd }, /* UDMA mode 2 */
  98. { 0x1a, 0x05, 0xcd }, /* UDMA mode 3 */
  99. { 0x1a, 0x03, 0xcd }, /* UDMA mode 4 */
  100. { 0x1a, 0x02, 0xcb }, /* UDMA mode 5 */
  101. { 0x1a, 0x01, 0xcb }, /* UDMA mode 6 */
  102. };
  103. static const struct pci_device_id pdc2027x_pci_tbl[] = {
  104. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20268), PDC_UDMA_100 },
  105. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20269), PDC_UDMA_133 },
  106. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20270), PDC_UDMA_100 },
  107. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20271), PDC_UDMA_133 },
  108. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20275), PDC_UDMA_133 },
  109. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20276), PDC_UDMA_133 },
  110. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20277), PDC_UDMA_133 },
  111. { } /* terminate list */
  112. };
  113. static struct pci_driver pdc2027x_pci_driver = {
  114. .name = DRV_NAME,
  115. .id_table = pdc2027x_pci_tbl,
  116. .probe = pdc2027x_init_one,
  117. .remove = ata_pci_remove_one,
  118. #ifdef CONFIG_PM_SLEEP
  119. .suspend = ata_pci_device_suspend,
  120. .resume = pdc2027x_reinit_one,
  121. #endif
  122. };
  123. static struct scsi_host_template pdc2027x_sht = {
  124. ATA_BMDMA_SHT(DRV_NAME),
  125. };
  126. static struct ata_port_operations pdc2027x_pata100_ops = {
  127. .inherits = &ata_bmdma_port_ops,
  128. .check_atapi_dma = pdc2027x_check_atapi_dma,
  129. .cable_detect = pdc2027x_cable_detect,
  130. .prereset = pdc2027x_prereset,
  131. };
  132. static struct ata_port_operations pdc2027x_pata133_ops = {
  133. .inherits = &pdc2027x_pata100_ops,
  134. .mode_filter = pdc2027x_mode_filter,
  135. .set_piomode = pdc2027x_set_piomode,
  136. .set_dmamode = pdc2027x_set_dmamode,
  137. .set_mode = pdc2027x_set_mode,
  138. };
  139. static struct ata_port_info pdc2027x_port_info[] = {
  140. /* PDC_UDMA_100 */
  141. {
  142. .flags = ATA_FLAG_SLAVE_POSS,
  143. .pio_mask = ATA_PIO4,
  144. .mwdma_mask = ATA_MWDMA2,
  145. .udma_mask = ATA_UDMA5,
  146. .port_ops = &pdc2027x_pata100_ops,
  147. },
  148. /* PDC_UDMA_133 */
  149. {
  150. .flags = ATA_FLAG_SLAVE_POSS,
  151. .pio_mask = ATA_PIO4,
  152. .mwdma_mask = ATA_MWDMA2,
  153. .udma_mask = ATA_UDMA6,
  154. .port_ops = &pdc2027x_pata133_ops,
  155. },
  156. };
  157. MODULE_AUTHOR("Andre Hedrick, Frank Tiernan, Albert Lee");
  158. MODULE_DESCRIPTION("libata driver module for Promise PDC20268 to PDC20277");
  159. MODULE_LICENSE("GPL");
  160. MODULE_VERSION(DRV_VERSION);
  161. MODULE_DEVICE_TABLE(pci, pdc2027x_pci_tbl);
  162. /**
  163. * port_mmio - Get the MMIO address of PDC2027x extended registers
  164. * @ap: Port
  165. * @offset: offset from mmio base
  166. */
  167. static inline void __iomem *port_mmio(struct ata_port *ap, unsigned int offset)
  168. {
  169. return ap->host->iomap[PDC_MMIO_BAR] + ap->port_no * 0x100 + offset;
  170. }
  171. /**
  172. * dev_mmio - Get the MMIO address of PDC2027x extended registers
  173. * @ap: Port
  174. * @adev: device
  175. * @offset: offset from mmio base
  176. */
  177. static inline void __iomem *dev_mmio(struct ata_port *ap, struct ata_device *adev, unsigned int offset)
  178. {
  179. u8 adj = (adev->devno) ? 0x08 : 0x00;
  180. return port_mmio(ap, offset) + adj;
  181. }
  182. /**
  183. * pdc2027x_pata_cable_detect - Probe host controller cable detect info
  184. * @ap: Port for which cable detect info is desired
  185. *
  186. * Read 80c cable indicator from Promise extended register.
  187. * This register is latched when the system is reset.
  188. *
  189. * LOCKING:
  190. * None (inherited from caller).
  191. */
  192. static int pdc2027x_cable_detect(struct ata_port *ap)
  193. {
  194. u32 cgcr;
  195. /* check cable detect results */
  196. cgcr = ioread32(port_mmio(ap, PDC_GLOBAL_CTL));
  197. if (cgcr & (1 << 26))
  198. goto cbl40;
  199. PDPRINTK("No cable or 80-conductor cable on port %d\n", ap->port_no);
  200. return ATA_CBL_PATA80;
  201. cbl40:
  202. printk(KERN_INFO DRV_NAME ": 40-conductor cable detected on port %d\n", ap->port_no);
  203. return ATA_CBL_PATA40;
  204. }
  205. /**
  206. * pdc2027x_port_enabled - Check PDC ATA control register to see whether the port is enabled.
  207. * @ap: Port to check
  208. */
  209. static inline int pdc2027x_port_enabled(struct ata_port *ap)
  210. {
  211. return ioread8(port_mmio(ap, PDC_ATA_CTL)) & 0x02;
  212. }
  213. /**
  214. * pdc2027x_prereset - prereset for PATA host controller
  215. * @link: Target link
  216. * @deadline: deadline jiffies for the operation
  217. *
  218. * Probeinit including cable detection.
  219. *
  220. * LOCKING:
  221. * None (inherited from caller).
  222. */
  223. static int pdc2027x_prereset(struct ata_link *link, unsigned long deadline)
  224. {
  225. /* Check whether port enabled */
  226. if (!pdc2027x_port_enabled(link->ap))
  227. return -ENOENT;
  228. return ata_sff_prereset(link, deadline);
  229. }
  230. /**
  231. * pdc2720x_mode_filter - mode selection filter
  232. * @adev: ATA device
  233. * @mask: list of modes proposed
  234. *
  235. * Block UDMA on devices that cause trouble with this controller.
  236. */
  237. static unsigned long pdc2027x_mode_filter(struct ata_device *adev, unsigned long mask)
  238. {
  239. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  240. struct ata_device *pair = ata_dev_pair(adev);
  241. if (adev->class != ATA_DEV_ATA || adev->devno == 0 || pair == NULL)
  242. return mask;
  243. /* Check for slave of a Maxtor at UDMA6 */
  244. ata_id_c_string(pair->id, model_num, ATA_ID_PROD,
  245. ATA_ID_PROD_LEN + 1);
  246. /* If the master is a maxtor in UDMA6 then the slave should not use UDMA 6 */
  247. if (strstr(model_num, "Maxtor") == NULL && pair->dma_mode == XFER_UDMA_6)
  248. mask &= ~ (1 << (6 + ATA_SHIFT_UDMA));
  249. return mask;
  250. }
  251. /**
  252. * pdc2027x_set_piomode - Initialize host controller PATA PIO timings
  253. * @ap: Port to configure
  254. * @adev: um
  255. *
  256. * Set PIO mode for device.
  257. *
  258. * LOCKING:
  259. * None (inherited from caller).
  260. */
  261. static void pdc2027x_set_piomode(struct ata_port *ap, struct ata_device *adev)
  262. {
  263. unsigned int pio = adev->pio_mode - XFER_PIO_0;
  264. u32 ctcr0, ctcr1;
  265. PDPRINTK("adev->pio_mode[%X]\n", adev->pio_mode);
  266. /* Sanity check */
  267. if (pio > 4) {
  268. printk(KERN_ERR DRV_NAME ": Unknown pio mode [%d] ignored\n", pio);
  269. return;
  270. }
  271. /* Set the PIO timing registers using value table for 133MHz */
  272. PDPRINTK("Set pio regs... \n");
  273. ctcr0 = ioread32(dev_mmio(ap, adev, PDC_CTCR0));
  274. ctcr0 &= 0xffff0000;
  275. ctcr0 |= pdc2027x_pio_timing_tbl[pio].value0 |
  276. (pdc2027x_pio_timing_tbl[pio].value1 << 8);
  277. iowrite32(ctcr0, dev_mmio(ap, adev, PDC_CTCR0));
  278. ctcr1 = ioread32(dev_mmio(ap, adev, PDC_CTCR1));
  279. ctcr1 &= 0x00ffffff;
  280. ctcr1 |= (pdc2027x_pio_timing_tbl[pio].value2 << 24);
  281. iowrite32(ctcr1, dev_mmio(ap, adev, PDC_CTCR1));
  282. PDPRINTK("Set pio regs done\n");
  283. PDPRINTK("Set to pio mode[%u] \n", pio);
  284. }
  285. /**
  286. * pdc2027x_set_dmamode - Initialize host controller PATA UDMA timings
  287. * @ap: Port to configure
  288. * @adev: um
  289. *
  290. * Set UDMA mode for device.
  291. *
  292. * LOCKING:
  293. * None (inherited from caller).
  294. */
  295. static void pdc2027x_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  296. {
  297. unsigned int dma_mode = adev->dma_mode;
  298. u32 ctcr0, ctcr1;
  299. if ((dma_mode >= XFER_UDMA_0) &&
  300. (dma_mode <= XFER_UDMA_6)) {
  301. /* Set the UDMA timing registers with value table for 133MHz */
  302. unsigned int udma_mode = dma_mode & 0x07;
  303. if (dma_mode == XFER_UDMA_2) {
  304. /*
  305. * Turn off tHOLD.
  306. * If tHOLD is '1', the hardware will add half clock for data hold time.
  307. * This code segment seems to be no effect. tHOLD will be overwritten below.
  308. */
  309. ctcr1 = ioread32(dev_mmio(ap, adev, PDC_CTCR1));
  310. iowrite32(ctcr1 & ~(1 << 7), dev_mmio(ap, adev, PDC_CTCR1));
  311. }
  312. PDPRINTK("Set udma regs... \n");
  313. ctcr1 = ioread32(dev_mmio(ap, adev, PDC_CTCR1));
  314. ctcr1 &= 0xff000000;
  315. ctcr1 |= pdc2027x_udma_timing_tbl[udma_mode].value0 |
  316. (pdc2027x_udma_timing_tbl[udma_mode].value1 << 8) |
  317. (pdc2027x_udma_timing_tbl[udma_mode].value2 << 16);
  318. iowrite32(ctcr1, dev_mmio(ap, adev, PDC_CTCR1));
  319. PDPRINTK("Set udma regs done\n");
  320. PDPRINTK("Set to udma mode[%u] \n", udma_mode);
  321. } else if ((dma_mode >= XFER_MW_DMA_0) &&
  322. (dma_mode <= XFER_MW_DMA_2)) {
  323. /* Set the MDMA timing registers with value table for 133MHz */
  324. unsigned int mdma_mode = dma_mode & 0x07;
  325. PDPRINTK("Set mdma regs... \n");
  326. ctcr0 = ioread32(dev_mmio(ap, adev, PDC_CTCR0));
  327. ctcr0 &= 0x0000ffff;
  328. ctcr0 |= (pdc2027x_mdma_timing_tbl[mdma_mode].value0 << 16) |
  329. (pdc2027x_mdma_timing_tbl[mdma_mode].value1 << 24);
  330. iowrite32(ctcr0, dev_mmio(ap, adev, PDC_CTCR0));
  331. PDPRINTK("Set mdma regs done\n");
  332. PDPRINTK("Set to mdma mode[%u] \n", mdma_mode);
  333. } else {
  334. printk(KERN_ERR DRV_NAME ": Unknown dma mode [%u] ignored\n", dma_mode);
  335. }
  336. }
  337. /**
  338. * pdc2027x_set_mode - Set the timing registers back to correct values.
  339. * @link: link to configure
  340. * @r_failed: Returned device for failure
  341. *
  342. * The pdc2027x hardware will look at "SET FEATURES" and change the timing registers
  343. * automatically. The values set by the hardware might be incorrect, under 133Mhz PLL.
  344. * This function overwrites the possibly incorrect values set by the hardware to be correct.
  345. */
  346. static int pdc2027x_set_mode(struct ata_link *link, struct ata_device **r_failed)
  347. {
  348. struct ata_port *ap = link->ap;
  349. struct ata_device *dev;
  350. int rc;
  351. rc = ata_do_set_mode(link, r_failed);
  352. if (rc < 0)
  353. return rc;
  354. ata_for_each_dev(dev, link, ENABLED) {
  355. pdc2027x_set_piomode(ap, dev);
  356. /*
  357. * Enable prefetch if the device support PIO only.
  358. */
  359. if (dev->xfer_shift == ATA_SHIFT_PIO) {
  360. u32 ctcr1 = ioread32(dev_mmio(ap, dev, PDC_CTCR1));
  361. ctcr1 |= (1 << 25);
  362. iowrite32(ctcr1, dev_mmio(ap, dev, PDC_CTCR1));
  363. PDPRINTK("Turn on prefetch\n");
  364. } else {
  365. pdc2027x_set_dmamode(ap, dev);
  366. }
  367. }
  368. return 0;
  369. }
  370. /**
  371. * pdc2027x_check_atapi_dma - Check whether ATAPI DMA can be supported for this command
  372. * @qc: Metadata associated with taskfile to check
  373. *
  374. * LOCKING:
  375. * None (inherited from caller).
  376. *
  377. * RETURNS: 0 when ATAPI DMA can be used
  378. * 1 otherwise
  379. */
  380. static int pdc2027x_check_atapi_dma(struct ata_queued_cmd *qc)
  381. {
  382. struct scsi_cmnd *cmd = qc->scsicmd;
  383. u8 *scsicmd = cmd->cmnd;
  384. int rc = 1; /* atapi dma off by default */
  385. /*
  386. * This workaround is from Promise's GPL driver.
  387. * If ATAPI DMA is used for commands not in the
  388. * following white list, say MODE_SENSE and REQUEST_SENSE,
  389. * pdc2027x might hit the irq lost problem.
  390. */
  391. switch (scsicmd[0]) {
  392. case READ_10:
  393. case WRITE_10:
  394. case READ_12:
  395. case WRITE_12:
  396. case READ_6:
  397. case WRITE_6:
  398. case 0xad: /* READ_DVD_STRUCTURE */
  399. case 0xbe: /* READ_CD */
  400. /* ATAPI DMA is ok */
  401. rc = 0;
  402. break;
  403. default:
  404. ;
  405. }
  406. return rc;
  407. }
  408. /**
  409. * pdc_read_counter - Read the ctr counter
  410. * @host: target ATA host
  411. */
  412. static long pdc_read_counter(struct ata_host *host)
  413. {
  414. void __iomem *mmio_base = host->iomap[PDC_MMIO_BAR];
  415. long counter;
  416. int retry = 1;
  417. u32 bccrl, bccrh, bccrlv, bccrhv;
  418. retry:
  419. bccrl = ioread32(mmio_base + PDC_BYTE_COUNT) & 0x7fff;
  420. bccrh = ioread32(mmio_base + PDC_BYTE_COUNT + 0x100) & 0x7fff;
  421. /* Read the counter values again for verification */
  422. bccrlv = ioread32(mmio_base + PDC_BYTE_COUNT) & 0x7fff;
  423. bccrhv = ioread32(mmio_base + PDC_BYTE_COUNT + 0x100) & 0x7fff;
  424. counter = (bccrh << 15) | bccrl;
  425. PDPRINTK("bccrh [%X] bccrl [%X]\n", bccrh, bccrl);
  426. PDPRINTK("bccrhv[%X] bccrlv[%X]\n", bccrhv, bccrlv);
  427. /*
  428. * The 30-bit decreasing counter are read by 2 pieces.
  429. * Incorrect value may be read when both bccrh and bccrl are changing.
  430. * Ex. When 7900 decrease to 78FF, wrong value 7800 might be read.
  431. */
  432. if (retry && !(bccrh == bccrhv && bccrl >= bccrlv)) {
  433. retry--;
  434. PDPRINTK("rereading counter\n");
  435. goto retry;
  436. }
  437. return counter;
  438. }
  439. /**
  440. * adjust_pll - Adjust the PLL input clock in Hz.
  441. *
  442. * @pdc_controller: controller specific information
  443. * @host: target ATA host
  444. * @pll_clock: The input of PLL in HZ
  445. */
  446. static void pdc_adjust_pll(struct ata_host *host, long pll_clock, unsigned int board_idx)
  447. {
  448. void __iomem *mmio_base = host->iomap[PDC_MMIO_BAR];
  449. u16 pll_ctl;
  450. long pll_clock_khz = pll_clock / 1000;
  451. long pout_required = board_idx? PDC_133_MHZ:PDC_100_MHZ;
  452. long ratio = pout_required / pll_clock_khz;
  453. int F, R;
  454. /* Sanity check */
  455. if (unlikely(pll_clock_khz < 5000L || pll_clock_khz > 70000L)) {
  456. printk(KERN_ERR DRV_NAME ": Invalid PLL input clock %ldkHz, give up!\n", pll_clock_khz);
  457. return;
  458. }
  459. #ifdef PDC_DEBUG
  460. PDPRINTK("pout_required is %ld\n", pout_required);
  461. /* Show the current clock value of PLL control register
  462. * (maybe already configured by the firmware)
  463. */
  464. pll_ctl = ioread16(mmio_base + PDC_PLL_CTL);
  465. PDPRINTK("pll_ctl[%X]\n", pll_ctl);
  466. #endif
  467. /*
  468. * Calculate the ratio of F, R and OD
  469. * POUT = (F + 2) / (( R + 2) * NO)
  470. */
  471. if (ratio < 8600L) { /* 8.6x */
  472. /* Using NO = 0x01, R = 0x0D */
  473. R = 0x0d;
  474. } else if (ratio < 12900L) { /* 12.9x */
  475. /* Using NO = 0x01, R = 0x08 */
  476. R = 0x08;
  477. } else if (ratio < 16100L) { /* 16.1x */
  478. /* Using NO = 0x01, R = 0x06 */
  479. R = 0x06;
  480. } else if (ratio < 64000L) { /* 64x */
  481. R = 0x00;
  482. } else {
  483. /* Invalid ratio */
  484. printk(KERN_ERR DRV_NAME ": Invalid ratio %ld, give up!\n", ratio);
  485. return;
  486. }
  487. F = (ratio * (R+2)) / 1000 - 2;
  488. if (unlikely(F < 0 || F > 127)) {
  489. /* Invalid F */
  490. printk(KERN_ERR DRV_NAME ": F[%d] invalid!\n", F);
  491. return;
  492. }
  493. PDPRINTK("F[%d] R[%d] ratio*1000[%ld]\n", F, R, ratio);
  494. pll_ctl = (R << 8) | F;
  495. PDPRINTK("Writing pll_ctl[%X]\n", pll_ctl);
  496. iowrite16(pll_ctl, mmio_base + PDC_PLL_CTL);
  497. ioread16(mmio_base + PDC_PLL_CTL); /* flush */
  498. /* Wait the PLL circuit to be stable */
  499. mdelay(30);
  500. #ifdef PDC_DEBUG
  501. /*
  502. * Show the current clock value of PLL control register
  503. * (maybe configured by the firmware)
  504. */
  505. pll_ctl = ioread16(mmio_base + PDC_PLL_CTL);
  506. PDPRINTK("pll_ctl[%X]\n", pll_ctl);
  507. #endif
  508. return;
  509. }
  510. /**
  511. * detect_pll_input_clock - Detect the PLL input clock in Hz.
  512. * @host: target ATA host
  513. * Ex. 16949000 on 33MHz PCI bus for pdc20275.
  514. * Half of the PCI clock.
  515. */
  516. static long pdc_detect_pll_input_clock(struct ata_host *host)
  517. {
  518. void __iomem *mmio_base = host->iomap[PDC_MMIO_BAR];
  519. u32 scr;
  520. long start_count, end_count;
  521. struct timeval start_time, end_time;
  522. long pll_clock, usec_elapsed;
  523. /* Start the test mode */
  524. scr = ioread32(mmio_base + PDC_SYS_CTL);
  525. PDPRINTK("scr[%X]\n", scr);
  526. iowrite32(scr | (0x01 << 14), mmio_base + PDC_SYS_CTL);
  527. ioread32(mmio_base + PDC_SYS_CTL); /* flush */
  528. /* Read current counter value */
  529. start_count = pdc_read_counter(host);
  530. do_gettimeofday(&start_time);
  531. /* Let the counter run for 100 ms. */
  532. mdelay(100);
  533. /* Read the counter values again */
  534. end_count = pdc_read_counter(host);
  535. do_gettimeofday(&end_time);
  536. /* Stop the test mode */
  537. scr = ioread32(mmio_base + PDC_SYS_CTL);
  538. PDPRINTK("scr[%X]\n", scr);
  539. iowrite32(scr & ~(0x01 << 14), mmio_base + PDC_SYS_CTL);
  540. ioread32(mmio_base + PDC_SYS_CTL); /* flush */
  541. /* calculate the input clock in Hz */
  542. usec_elapsed = (end_time.tv_sec - start_time.tv_sec) * 1000000 +
  543. (end_time.tv_usec - start_time.tv_usec);
  544. pll_clock = ((start_count - end_count) & 0x3fffffff) / 100 *
  545. (100000000 / usec_elapsed);
  546. PDPRINTK("start[%ld] end[%ld] \n", start_count, end_count);
  547. PDPRINTK("PLL input clock[%ld]Hz\n", pll_clock);
  548. return pll_clock;
  549. }
  550. /**
  551. * pdc_hardware_init - Initialize the hardware.
  552. * @host: target ATA host
  553. * @board_idx: board identifier
  554. */
  555. static int pdc_hardware_init(struct ata_host *host, unsigned int board_idx)
  556. {
  557. long pll_clock;
  558. /*
  559. * Detect PLL input clock rate.
  560. * On some system, where PCI bus is running at non-standard clock rate.
  561. * Ex. 25MHz or 40MHz, we have to adjust the cycle_time.
  562. * The pdc20275 controller employs PLL circuit to help correct timing registers setting.
  563. */
  564. pll_clock = pdc_detect_pll_input_clock(host);
  565. dev_info(host->dev, "PLL input clock %ld kHz\n", pll_clock/1000);
  566. /* Adjust PLL control register */
  567. pdc_adjust_pll(host, pll_clock, board_idx);
  568. return 0;
  569. }
  570. /**
  571. * pdc_ata_setup_port - setup the mmio address
  572. * @port: ata ioports to setup
  573. * @base: base address
  574. */
  575. static void pdc_ata_setup_port(struct ata_ioports *port, void __iomem *base)
  576. {
  577. port->cmd_addr =
  578. port->data_addr = base;
  579. port->feature_addr =
  580. port->error_addr = base + 0x05;
  581. port->nsect_addr = base + 0x0a;
  582. port->lbal_addr = base + 0x0f;
  583. port->lbam_addr = base + 0x10;
  584. port->lbah_addr = base + 0x15;
  585. port->device_addr = base + 0x1a;
  586. port->command_addr =
  587. port->status_addr = base + 0x1f;
  588. port->altstatus_addr =
  589. port->ctl_addr = base + 0x81a;
  590. }
  591. /**
  592. * pdc2027x_init_one - PCI probe function
  593. * Called when an instance of PCI adapter is inserted.
  594. * This function checks whether the hardware is supported,
  595. * initialize hardware and register an instance of ata_host to
  596. * libata. (implements struct pci_driver.probe() )
  597. *
  598. * @pdev: instance of pci_dev found
  599. * @ent: matching entry in the id_tbl[]
  600. */
  601. static int pdc2027x_init_one(struct pci_dev *pdev,
  602. const struct pci_device_id *ent)
  603. {
  604. static const unsigned long cmd_offset[] = { 0x17c0, 0x15c0 };
  605. static const unsigned long bmdma_offset[] = { 0x1000, 0x1008 };
  606. unsigned int board_idx = (unsigned int) ent->driver_data;
  607. const struct ata_port_info *ppi[] =
  608. { &pdc2027x_port_info[board_idx], NULL };
  609. struct ata_host *host;
  610. void __iomem *mmio_base;
  611. int i, rc;
  612. ata_print_version_once(&pdev->dev, DRV_VERSION);
  613. /* alloc host */
  614. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
  615. if (!host)
  616. return -ENOMEM;
  617. /* acquire resources and fill host */
  618. rc = pcim_enable_device(pdev);
  619. if (rc)
  620. return rc;
  621. rc = pcim_iomap_regions(pdev, 1 << PDC_MMIO_BAR, DRV_NAME);
  622. if (rc)
  623. return rc;
  624. host->iomap = pcim_iomap_table(pdev);
  625. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  626. if (rc)
  627. return rc;
  628. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  629. if (rc)
  630. return rc;
  631. mmio_base = host->iomap[PDC_MMIO_BAR];
  632. for (i = 0; i < 2; i++) {
  633. struct ata_port *ap = host->ports[i];
  634. pdc_ata_setup_port(&ap->ioaddr, mmio_base + cmd_offset[i]);
  635. ap->ioaddr.bmdma_addr = mmio_base + bmdma_offset[i];
  636. ata_port_pbar_desc(ap, PDC_MMIO_BAR, -1, "mmio");
  637. ata_port_pbar_desc(ap, PDC_MMIO_BAR, cmd_offset[i], "cmd");
  638. }
  639. //pci_enable_intx(pdev);
  640. /* initialize adapter */
  641. if (pdc_hardware_init(host, board_idx) != 0)
  642. return -EIO;
  643. pci_set_master(pdev);
  644. return ata_host_activate(host, pdev->irq, ata_bmdma_interrupt,
  645. IRQF_SHARED, &pdc2027x_sht);
  646. }
  647. #ifdef CONFIG_PM_SLEEP
  648. static int pdc2027x_reinit_one(struct pci_dev *pdev)
  649. {
  650. struct ata_host *host = pci_get_drvdata(pdev);
  651. unsigned int board_idx;
  652. int rc;
  653. rc = ata_pci_device_do_resume(pdev);
  654. if (rc)
  655. return rc;
  656. if (pdev->device == PCI_DEVICE_ID_PROMISE_20268 ||
  657. pdev->device == PCI_DEVICE_ID_PROMISE_20270)
  658. board_idx = PDC_UDMA_100;
  659. else
  660. board_idx = PDC_UDMA_133;
  661. if (pdc_hardware_init(host, board_idx))
  662. return -EIO;
  663. ata_host_resume(host);
  664. return 0;
  665. }
  666. #endif
  667. module_pci_driver(pdc2027x_pci_driver);