pata_sil680.c 12 KB

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