pata_pdc2027x.c 21 KB

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