pata_pdc2027x.c 24 KB

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