pata_pdc2027x.c 22 KB

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