pata_sil680.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * pata_sil680.c - SIL680 PATA for new ATA layer
  3. * (C) 2005 Red Hat Inc
  4. * Alan Cox <alan@redhat.com>
  5. *
  6. * based upon
  7. *
  8. * linux/drivers/ide/pci/siimage.c Version 1.07 Nov 30, 2003
  9. *
  10. * Copyright (C) 2001-2002 Andre Hedrick <andre@linux-ide.org>
  11. * Copyright (C) 2003 Red Hat <alan@redhat.com>
  12. *
  13. * May be copied or modified under the terms of the GNU General Public License
  14. *
  15. * Documentation publically available.
  16. *
  17. * If you have strange problems with nVidia chipset systems please
  18. * see the SI support documentation and update your system BIOS
  19. * if necessary
  20. *
  21. * TODO
  22. * If we know all our devices are LBA28 (or LBA28 sized) we could use
  23. * the command fifo mode.
  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 <scsi/scsi_host.h>
  32. #include <linux/libata.h>
  33. #define DRV_NAME "pata_sil680"
  34. #define DRV_VERSION "0.4.8"
  35. #define SIL680_MMIO_BAR 5
  36. /**
  37. * sil680_selreg - return register base
  38. * @hwif: interface
  39. * @r: config offset
  40. *
  41. * Turn a config register offset into the right address in either
  42. * PCI space or MMIO space to access the control register in question
  43. * Thankfully this is a configuration operation so isnt performance
  44. * criticial.
  45. */
  46. static unsigned long sil680_selreg(struct ata_port *ap, int r)
  47. {
  48. unsigned long base = 0xA0 + r;
  49. base += (ap->port_no << 4);
  50. return base;
  51. }
  52. /**
  53. * sil680_seldev - return register base
  54. * @hwif: interface
  55. * @r: config offset
  56. *
  57. * Turn a config register offset into the right address in either
  58. * PCI space or MMIO space to access the control register in question
  59. * including accounting for the unit shift.
  60. */
  61. static unsigned long sil680_seldev(struct ata_port *ap, struct ata_device *adev, int r)
  62. {
  63. unsigned long base = 0xA0 + r;
  64. base += (ap->port_no << 4);
  65. base |= adev->devno ? 2 : 0;
  66. return base;
  67. }
  68. /**
  69. * sil680_cable_detect - cable detection
  70. * @ap: ATA port
  71. *
  72. * Perform cable detection. The SIL680 stores this in PCI config
  73. * space for us.
  74. */
  75. static int sil680_cable_detect(struct ata_port *ap) {
  76. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  77. unsigned long addr = sil680_selreg(ap, 0);
  78. u8 ata66;
  79. pci_read_config_byte(pdev, addr, &ata66);
  80. if (ata66 & 1)
  81. return ATA_CBL_PATA80;
  82. else
  83. return ATA_CBL_PATA40;
  84. }
  85. /**
  86. * sil680_set_piomode - set initial PIO mode data
  87. * @ap: ATA interface
  88. * @adev: ATA device
  89. *
  90. * Program the SIL680 registers for PIO mode. Note that the task speed
  91. * registers are shared between the devices so we must pick the lowest
  92. * mode for command work.
  93. */
  94. static void sil680_set_piomode(struct ata_port *ap, struct ata_device *adev)
  95. {
  96. static u16 speed_p[5] = { 0x328A, 0x2283, 0x1104, 0x10C3, 0x10C1 };
  97. static u16 speed_t[5] = { 0x328A, 0x2283, 0x1281, 0x10C3, 0x10C1 };
  98. unsigned long tfaddr = sil680_selreg(ap, 0x02);
  99. unsigned long addr = sil680_seldev(ap, adev, 0x04);
  100. unsigned long addr_mask = 0x80 + 4 * ap->port_no;
  101. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  102. int pio = adev->pio_mode - XFER_PIO_0;
  103. int lowest_pio = pio;
  104. int port_shift = 4 * adev->devno;
  105. u16 reg;
  106. u8 mode;
  107. struct ata_device *pair = ata_dev_pair(adev);
  108. if (pair != NULL && adev->pio_mode > pair->pio_mode)
  109. lowest_pio = pair->pio_mode - XFER_PIO_0;
  110. pci_write_config_word(pdev, addr, speed_p[pio]);
  111. pci_write_config_word(pdev, tfaddr, speed_t[lowest_pio]);
  112. pci_read_config_word(pdev, tfaddr-2, &reg);
  113. pci_read_config_byte(pdev, addr_mask, &mode);
  114. reg &= ~0x0200; /* Clear IORDY */
  115. mode &= ~(3 << port_shift); /* Clear IORDY and DMA bits */
  116. if (ata_pio_need_iordy(adev)) {
  117. reg |= 0x0200; /* Enable IORDY */
  118. mode |= 1 << port_shift;
  119. }
  120. pci_write_config_word(pdev, tfaddr-2, reg);
  121. pci_write_config_byte(pdev, addr_mask, mode);
  122. }
  123. /**
  124. * sil680_set_dmamode - set initial DMA mode data
  125. * @ap: ATA interface
  126. * @adev: ATA device
  127. *
  128. * Program the MWDMA/UDMA modes for the sil680 k
  129. * chipset. The MWDMA mode values are pulled from a lookup table
  130. * while the chipset uses mode number for UDMA.
  131. */
  132. static void sil680_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  133. {
  134. static u8 ultra_table[2][7] = {
  135. { 0x0C, 0x07, 0x05, 0x04, 0x02, 0x01, 0xFF }, /* 100MHz */
  136. { 0x0F, 0x0B, 0x07, 0x05, 0x03, 0x02, 0x01 }, /* 133Mhz */
  137. };
  138. static u16 dma_table[3] = { 0x2208, 0x10C2, 0x10C1 };
  139. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  140. unsigned long ma = sil680_seldev(ap, adev, 0x08);
  141. unsigned long ua = sil680_seldev(ap, adev, 0x0C);
  142. unsigned long addr_mask = 0x80 + 4 * ap->port_no;
  143. int port_shift = adev->devno * 4;
  144. u8 scsc, mode;
  145. u16 multi, ultra;
  146. pci_read_config_byte(pdev, 0x8A, &scsc);
  147. pci_read_config_byte(pdev, addr_mask, &mode);
  148. pci_read_config_word(pdev, ma, &multi);
  149. pci_read_config_word(pdev, ua, &ultra);
  150. /* Mask timing bits */
  151. ultra &= ~0x3F;
  152. mode &= ~(0x03 << port_shift);
  153. /* Extract scsc */
  154. scsc = (scsc & 0x30) ? 1: 0;
  155. if (adev->dma_mode >= XFER_UDMA_0) {
  156. multi = 0x10C1;
  157. ultra |= ultra_table[scsc][adev->dma_mode - XFER_UDMA_0];
  158. mode |= (0x03 << port_shift);
  159. } else {
  160. multi = dma_table[adev->dma_mode - XFER_MW_DMA_0];
  161. mode |= (0x02 << port_shift);
  162. }
  163. pci_write_config_byte(pdev, addr_mask, mode);
  164. pci_write_config_word(pdev, ma, multi);
  165. pci_write_config_word(pdev, ua, ultra);
  166. }
  167. static struct scsi_host_template sil680_sht = {
  168. .module = THIS_MODULE,
  169. .name = DRV_NAME,
  170. .ioctl = ata_scsi_ioctl,
  171. .queuecommand = ata_scsi_queuecmd,
  172. .can_queue = ATA_DEF_QUEUE,
  173. .this_id = ATA_SHT_THIS_ID,
  174. .sg_tablesize = LIBATA_MAX_PRD,
  175. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  176. .emulated = ATA_SHT_EMULATED,
  177. .use_clustering = ATA_SHT_USE_CLUSTERING,
  178. .proc_name = DRV_NAME,
  179. .dma_boundary = ATA_DMA_BOUNDARY,
  180. .slave_configure = ata_scsi_slave_config,
  181. .slave_destroy = ata_scsi_slave_destroy,
  182. .bios_param = ata_std_bios_param,
  183. };
  184. static struct ata_port_operations sil680_port_ops = {
  185. .set_piomode = sil680_set_piomode,
  186. .set_dmamode = sil680_set_dmamode,
  187. .mode_filter = ata_pci_default_filter,
  188. .tf_load = ata_tf_load,
  189. .tf_read = ata_tf_read,
  190. .check_status = ata_check_status,
  191. .exec_command = ata_exec_command,
  192. .dev_select = ata_std_dev_select,
  193. .freeze = ata_bmdma_freeze,
  194. .thaw = ata_bmdma_thaw,
  195. .error_handler = ata_bmdma_error_handler,
  196. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  197. .cable_detect = sil680_cable_detect,
  198. .bmdma_setup = ata_bmdma_setup,
  199. .bmdma_start = ata_bmdma_start,
  200. .bmdma_stop = ata_bmdma_stop,
  201. .bmdma_status = ata_bmdma_status,
  202. .qc_prep = ata_qc_prep,
  203. .qc_issue = ata_qc_issue_prot,
  204. .data_xfer = ata_data_xfer,
  205. .irq_handler = ata_interrupt,
  206. .irq_clear = ata_bmdma_irq_clear,
  207. .irq_on = ata_irq_on,
  208. .port_start = ata_sff_port_start,
  209. };
  210. /**
  211. * sil680_init_chip - chip setup
  212. * @pdev: PCI device
  213. *
  214. * Perform all the chip setup which must be done both when the device
  215. * is powered up on boot and when we resume in case we resumed from RAM.
  216. * Returns the final clock settings.
  217. */
  218. static u8 sil680_init_chip(struct pci_dev *pdev, int *try_mmio)
  219. {
  220. u32 class_rev = 0;
  221. u8 tmpbyte = 0;
  222. pci_read_config_dword(pdev, PCI_CLASS_REVISION, &class_rev);
  223. class_rev &= 0xff;
  224. /* FIXME: double check */
  225. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, (class_rev) ? 1 : 255);
  226. pci_write_config_byte(pdev, 0x80, 0x00);
  227. pci_write_config_byte(pdev, 0x84, 0x00);
  228. pci_read_config_byte(pdev, 0x8A, &tmpbyte);
  229. dev_dbg(&pdev->dev, "sil680: BA5_EN = %d clock = %02X\n",
  230. tmpbyte & 1, tmpbyte & 0x30);
  231. *try_mmio = (tmpbyte & 1) || pci_resource_start(pdev, 5);
  232. switch(tmpbyte & 0x30) {
  233. case 0x00:
  234. /* 133 clock attempt to force it on */
  235. pci_write_config_byte(pdev, 0x8A, tmpbyte|0x10);
  236. break;
  237. case 0x30:
  238. /* if clocking is disabled */
  239. /* 133 clock attempt to force it on */
  240. pci_write_config_byte(pdev, 0x8A, tmpbyte & ~0x20);
  241. break;
  242. case 0x10:
  243. /* 133 already */
  244. break;
  245. case 0x20:
  246. /* BIOS set PCI x2 clocking */
  247. break;
  248. }
  249. pci_read_config_byte(pdev, 0x8A, &tmpbyte);
  250. dev_dbg(&pdev->dev, "sil680: BA5_EN = %d clock = %02X\n",
  251. tmpbyte & 1, tmpbyte & 0x30);
  252. pci_write_config_byte(pdev, 0xA1, 0x72);
  253. pci_write_config_word(pdev, 0xA2, 0x328A);
  254. pci_write_config_dword(pdev, 0xA4, 0x62DD62DD);
  255. pci_write_config_dword(pdev, 0xA8, 0x43924392);
  256. pci_write_config_dword(pdev, 0xAC, 0x40094009);
  257. pci_write_config_byte(pdev, 0xB1, 0x72);
  258. pci_write_config_word(pdev, 0xB2, 0x328A);
  259. pci_write_config_dword(pdev, 0xB4, 0x62DD62DD);
  260. pci_write_config_dword(pdev, 0xB8, 0x43924392);
  261. pci_write_config_dword(pdev, 0xBC, 0x40094009);
  262. switch(tmpbyte & 0x30) {
  263. case 0x00: printk(KERN_INFO "sil680: 100MHz clock.\n");break;
  264. case 0x10: printk(KERN_INFO "sil680: 133MHz clock.\n");break;
  265. case 0x20: printk(KERN_INFO "sil680: Using PCI clock.\n");break;
  266. /* This last case is _NOT_ ok */
  267. case 0x30: printk(KERN_ERR "sil680: Clock disabled ?\n");
  268. }
  269. return tmpbyte & 0x30;
  270. }
  271. static int __devinit sil680_init_one(struct pci_dev *pdev,
  272. const struct pci_device_id *id)
  273. {
  274. static const struct ata_port_info info = {
  275. .sht = &sil680_sht,
  276. .flags = ATA_FLAG_SLAVE_POSS,
  277. .pio_mask = 0x1f,
  278. .mwdma_mask = 0x07,
  279. .udma_mask = ATA_UDMA6,
  280. .port_ops = &sil680_port_ops
  281. };
  282. static const struct ata_port_info info_slow = {
  283. .sht = &sil680_sht,
  284. .flags = ATA_FLAG_SLAVE_POSS,
  285. .pio_mask = 0x1f,
  286. .mwdma_mask = 0x07,
  287. .udma_mask = ATA_UDMA5,
  288. .port_ops = &sil680_port_ops
  289. };
  290. const struct ata_port_info *ppi[] = { &info, NULL };
  291. static int printed_version;
  292. struct ata_host *host;
  293. void __iomem *mmio_base;
  294. int rc, try_mmio;
  295. if (!printed_version++)
  296. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  297. switch (sil680_init_chip(pdev, &try_mmio)) {
  298. case 0:
  299. ppi[0] = &info_slow;
  300. break;
  301. case 0x30:
  302. return -ENODEV;
  303. }
  304. if (!try_mmio)
  305. goto use_ioports;
  306. /* Try to acquire MMIO resources and fallback to PIO if
  307. * that fails
  308. */
  309. rc = pcim_enable_device(pdev);
  310. if (rc)
  311. return rc;
  312. rc = pcim_iomap_regions(pdev, 1 << SIL680_MMIO_BAR, DRV_NAME);
  313. if (rc)
  314. goto use_ioports;
  315. /* Allocate host and set it up */
  316. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
  317. if (!host)
  318. return -ENOMEM;
  319. host->iomap = pcim_iomap_table(pdev);
  320. /* Setup DMA masks */
  321. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  322. if (rc)
  323. return rc;
  324. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  325. if (rc)
  326. return rc;
  327. pci_set_master(pdev);
  328. /* Get MMIO base and initialize port addresses */
  329. mmio_base = host->iomap[SIL680_MMIO_BAR];
  330. host->ports[0]->ioaddr.bmdma_addr = mmio_base + 0x00;
  331. host->ports[0]->ioaddr.cmd_addr = mmio_base + 0x80;
  332. host->ports[0]->ioaddr.ctl_addr = mmio_base + 0x8a;
  333. host->ports[0]->ioaddr.altstatus_addr = mmio_base + 0x8a;
  334. ata_std_ports(&host->ports[0]->ioaddr);
  335. host->ports[1]->ioaddr.bmdma_addr = mmio_base + 0x08;
  336. host->ports[1]->ioaddr.cmd_addr = mmio_base + 0xc0;
  337. host->ports[1]->ioaddr.ctl_addr = mmio_base + 0xca;
  338. host->ports[1]->ioaddr.altstatus_addr = mmio_base + 0xca;
  339. ata_std_ports(&host->ports[1]->ioaddr);
  340. /* Register & activate */
  341. return ata_host_activate(host, pdev->irq, ata_interrupt, IRQF_SHARED,
  342. &sil680_sht);
  343. use_ioports:
  344. return ata_pci_init_one(pdev, ppi);
  345. }
  346. #ifdef CONFIG_PM
  347. static int sil680_reinit_one(struct pci_dev *pdev)
  348. {
  349. int try_mmio;
  350. sil680_init_chip(pdev, &try_mmio);
  351. return ata_pci_device_resume(pdev);
  352. }
  353. #endif
  354. static const struct pci_device_id sil680[] = {
  355. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_SII_680), },
  356. { },
  357. };
  358. static struct pci_driver sil680_pci_driver = {
  359. .name = DRV_NAME,
  360. .id_table = sil680,
  361. .probe = sil680_init_one,
  362. .remove = ata_pci_remove_one,
  363. #ifdef CONFIG_PM
  364. .suspend = ata_pci_device_suspend,
  365. .resume = sil680_reinit_one,
  366. #endif
  367. };
  368. static int __init sil680_init(void)
  369. {
  370. return pci_register_driver(&sil680_pci_driver);
  371. }
  372. static void __exit sil680_exit(void)
  373. {
  374. pci_unregister_driver(&sil680_pci_driver);
  375. }
  376. MODULE_AUTHOR("Alan Cox");
  377. MODULE_DESCRIPTION("low-level driver for SI680 PATA");
  378. MODULE_LICENSE("GPL");
  379. MODULE_DEVICE_TABLE(pci, sil680);
  380. MODULE_VERSION(DRV_VERSION);
  381. module_init(sil680_init);
  382. module_exit(sil680_exit);