pata_sil680.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 <[email protected]>
  10. * Copyright (C) 2003 Red Hat <[email protected]>
  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/blkdev.h>
  28. #include <linux/delay.h>
  29. #include <scsi/scsi_host.h>
  30. #include <linux/libata.h>
  31. #define DRV_NAME "pata_sil680"
  32. #define DRV_VERSION "0.4.9"
  33. #define SIL680_MMIO_BAR 5
  34. /**
  35. * sil680_selreg - return register base
  36. * @ap: ATA interface
  37. * @r: config offset
  38. *
  39. * Turn a config register offset into the right address in PCI space
  40. * to access the control register in question.
  41. *
  42. * Thankfully this is a configuration operation so isn't performance
  43. * criticial.
  44. */
  45. static int sil680_selreg(struct ata_port *ap, int r)
  46. {
  47. return 0xA0 + (ap->port_no << 4) + r;
  48. }
  49. /**
  50. * sil680_seldev - return register base
  51. * @ap: ATA interface
  52. * @adev: ATA device
  53. * @r: config offset
  54. *
  55. * Turn a config register offset into the right address in PCI space
  56. * to access the control register in question including accounting for
  57. * the unit shift.
  58. */
  59. static int sil680_seldev(struct ata_port *ap, struct ata_device *adev, int r)
  60. {
  61. return 0xA0 + (ap->port_no << 4) + r + (adev->devno << 1);
  62. }
  63. /**
  64. * sil680_cable_detect - cable detection
  65. * @ap: ATA port
  66. *
  67. * Perform cable detection. The SIL680 stores this in PCI config
  68. * space for us.
  69. */
  70. static int sil680_cable_detect(struct ata_port *ap)
  71. {
  72. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  73. int addr = sil680_selreg(ap, 0);
  74. u8 ata66;
  75. pci_read_config_byte(pdev, addr, &ata66);
  76. if (ata66 & 1)
  77. return ATA_CBL_PATA80;
  78. else
  79. return ATA_CBL_PATA40;
  80. }
  81. /**
  82. * sil680_set_piomode - set PIO mode data
  83. * @ap: ATA interface
  84. * @adev: ATA device
  85. *
  86. * Program the SIL680 registers for PIO mode. Note that the task speed
  87. * registers are shared between the devices so we must pick the lowest
  88. * mode for command work.
  89. */
  90. static void sil680_set_piomode(struct ata_port *ap, struct ata_device *adev)
  91. {
  92. static const u16 speed_p[5] = {
  93. 0x328A, 0x2283, 0x1104, 0x10C3, 0x10C1
  94. };
  95. static const u16 speed_t[5] = {
  96. 0x328A, 0x2283, 0x1281, 0x10C3, 0x10C1
  97. };
  98. int tfaddr = sil680_selreg(ap, 0x02);
  99. int addr = sil680_seldev(ap, adev, 0x04);
  100. int 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 DMA mode data
  125. * @ap: ATA interface
  126. * @adev: ATA device
  127. *
  128. * Program the MWDMA/UDMA modes for the sil680 chipset.
  129. *
  130. * The MWDMA mode values are pulled from a lookup table
  131. * while the chipset uses mode number for UDMA.
  132. */
  133. static void sil680_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  134. {
  135. static const u8 ultra_table[2][7] = {
  136. { 0x0C, 0x07, 0x05, 0x04, 0x02, 0x01, 0xFF }, /* 100MHz */
  137. { 0x0F, 0x0B, 0x07, 0x05, 0x03, 0x02, 0x01 }, /* 133Mhz */
  138. };
  139. static const u16 dma_table[3] = { 0x2208, 0x10C2, 0x10C1 };
  140. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  141. int ma = sil680_seldev(ap, adev, 0x08);
  142. int ua = sil680_seldev(ap, adev, 0x0C);
  143. int addr_mask = 0x80 + 4 * ap->port_no;
  144. int port_shift = adev->devno * 4;
  145. u8 scsc, mode;
  146. u16 multi, ultra;
  147. pci_read_config_byte(pdev, 0x8A, &scsc);
  148. pci_read_config_byte(pdev, addr_mask, &mode);
  149. pci_read_config_word(pdev, ma, &multi);
  150. pci_read_config_word(pdev, ua, &ultra);
  151. /* Mask timing bits */
  152. ultra &= ~0x3F;
  153. mode &= ~(0x03 << port_shift);
  154. /* Extract scsc */
  155. scsc = (scsc & 0x30) ? 1 : 0;
  156. if (adev->dma_mode >= XFER_UDMA_0) {
  157. multi = 0x10C1;
  158. ultra |= ultra_table[scsc][adev->dma_mode - XFER_UDMA_0];
  159. mode |= (0x03 << port_shift);
  160. } else {
  161. multi = dma_table[adev->dma_mode - XFER_MW_DMA_0];
  162. mode |= (0x02 << port_shift);
  163. }
  164. pci_write_config_byte(pdev, addr_mask, mode);
  165. pci_write_config_word(pdev, ma, multi);
  166. pci_write_config_word(pdev, ua, ultra);
  167. }
  168. /**
  169. * sil680_sff_exec_command - issue ATA command to host controller
  170. * @ap: port to which command is being issued
  171. * @tf: ATA taskfile register set
  172. *
  173. * Issues ATA command, with proper synchronization with interrupt
  174. * handler / other threads. Use our MMIO space for PCI posting to avoid
  175. * a hideously slow cycle all the way to the device.
  176. *
  177. * LOCKING:
  178. * spin_lock_irqsave(host lock)
  179. */
  180. static void sil680_sff_exec_command(struct ata_port *ap,
  181. const struct ata_taskfile *tf)
  182. {
  183. iowrite8(tf->command, ap->ioaddr.command_addr);
  184. ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  185. }
  186. static bool sil680_sff_irq_check(struct ata_port *ap)
  187. {
  188. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  189. int addr = sil680_selreg(ap, 1);
  190. u8 val;
  191. pci_read_config_byte(pdev, addr, &val);
  192. return val & 0x08;
  193. }
  194. static struct scsi_host_template sil680_sht = {
  195. ATA_BMDMA_SHT(DRV_NAME),
  196. };
  197. static struct ata_port_operations sil680_port_ops = {
  198. .inherits = &ata_bmdma32_port_ops,
  199. .sff_exec_command = sil680_sff_exec_command,
  200. .sff_irq_check = sil680_sff_irq_check,
  201. .cable_detect = sil680_cable_detect,
  202. .set_piomode = sil680_set_piomode,
  203. .set_dmamode = sil680_set_dmamode,
  204. };
  205. /**
  206. * sil680_init_chip - chip setup
  207. * @pdev: PCI device
  208. * @try_mmio: Indicates to caller whether MMIO should be attempted
  209. *
  210. * Perform all the chip setup which must be done both when the device
  211. * is powered up on boot and when we resume in case we resumed from RAM.
  212. * Returns the final clock settings.
  213. */
  214. static u8 sil680_init_chip(struct pci_dev *pdev, int *try_mmio)
  215. {
  216. u8 tmpbyte = 0;
  217. /* FIXME: double check */
  218. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  219. pdev->revision ? 1 : 255);
  220. pci_write_config_byte(pdev, 0x80, 0x00);
  221. pci_write_config_byte(pdev, 0x84, 0x00);
  222. pci_read_config_byte(pdev, 0x8A, &tmpbyte);
  223. dev_dbg(&pdev->dev, "sil680: BA5_EN = %d clock = %02X\n",
  224. tmpbyte & 1, tmpbyte & 0x30);
  225. *try_mmio = 0;
  226. #ifdef CONFIG_PPC
  227. if (machine_is(cell))
  228. *try_mmio = (tmpbyte & 1) || pci_resource_start(pdev, 5);
  229. #endif
  230. switch (tmpbyte & 0x30) {
  231. case 0x00:
  232. /* 133 clock attempt to force it on */
  233. pci_write_config_byte(pdev, 0x8A, tmpbyte|0x10);
  234. break;
  235. case 0x30:
  236. /* if clocking is disabled */
  237. /* 133 clock attempt to force it on */
  238. pci_write_config_byte(pdev, 0x8A, tmpbyte & ~0x20);
  239. break;
  240. case 0x10:
  241. /* 133 already */
  242. break;
  243. case 0x20:
  244. /* BIOS set PCI x2 clocking */
  245. break;
  246. }
  247. pci_read_config_byte(pdev, 0x8A, &tmpbyte);
  248. dev_dbg(&pdev->dev, "sil680: BA5_EN = %d clock = %02X\n",
  249. tmpbyte & 1, tmpbyte & 0x30);
  250. pci_write_config_byte(pdev, 0xA1, 0x72);
  251. pci_write_config_word(pdev, 0xA2, 0x328A);
  252. pci_write_config_dword(pdev, 0xA4, 0x62DD62DD);
  253. pci_write_config_dword(pdev, 0xA8, 0x43924392);
  254. pci_write_config_dword(pdev, 0xAC, 0x40094009);
  255. pci_write_config_byte(pdev, 0xB1, 0x72);
  256. pci_write_config_word(pdev, 0xB2, 0x328A);
  257. pci_write_config_dword(pdev, 0xB4, 0x62DD62DD);
  258. pci_write_config_dword(pdev, 0xB8, 0x43924392);
  259. pci_write_config_dword(pdev, 0xBC, 0x40094009);
  260. switch (tmpbyte & 0x30) {
  261. case 0x00:
  262. dev_info(&pdev->dev, "sil680: 100MHz clock.\n");
  263. break;
  264. case 0x10:
  265. dev_info(&pdev->dev, "sil680: 133MHz clock.\n");
  266. break;
  267. case 0x20:
  268. dev_info(&pdev->dev, "sil680: Using PCI clock.\n");
  269. break;
  270. /* This last case is _NOT_ ok */
  271. case 0x30:
  272. dev_err(&pdev->dev, "sil680: Clock disabled ?\n");
  273. }
  274. return tmpbyte & 0x30;
  275. }
  276. static int sil680_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  277. {
  278. static const struct ata_port_info info = {
  279. .flags = ATA_FLAG_SLAVE_POSS,
  280. .pio_mask = ATA_PIO4,
  281. .mwdma_mask = ATA_MWDMA2,
  282. .udma_mask = ATA_UDMA6,
  283. .port_ops = &sil680_port_ops
  284. };
  285. static const struct ata_port_info info_slow = {
  286. .flags = ATA_FLAG_SLAVE_POSS,
  287. .pio_mask = ATA_PIO4,
  288. .mwdma_mask = ATA_MWDMA2,
  289. .udma_mask = ATA_UDMA5,
  290. .port_ops = &sil680_port_ops
  291. };
  292. const struct ata_port_info *ppi[] = { &info, NULL };
  293. struct ata_host *host;
  294. void __iomem *mmio_base;
  295. int rc, try_mmio;
  296. ata_print_version_once(&pdev->dev, DRV_VERSION);
  297. rc = pcim_enable_device(pdev);
  298. if (rc)
  299. return rc;
  300. switch (sil680_init_chip(pdev, &try_mmio)) {
  301. case 0:
  302. ppi[0] = &info_slow;
  303. break;
  304. case 0x30:
  305. return -ENODEV;
  306. }
  307. if (!try_mmio)
  308. goto use_ioports;
  309. /* Try to acquire MMIO resources and fallback to PIO if
  310. * that fails
  311. */
  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 = dma_set_mask_and_coherent(&pdev->dev, ATA_DMA_MASK);
  322. if (rc)
  323. return rc;
  324. pci_set_master(pdev);
  325. /* Get MMIO base and initialize port addresses */
  326. mmio_base = host->iomap[SIL680_MMIO_BAR];
  327. host->ports[0]->ioaddr.bmdma_addr = mmio_base + 0x00;
  328. host->ports[0]->ioaddr.cmd_addr = mmio_base + 0x80;
  329. host->ports[0]->ioaddr.ctl_addr = mmio_base + 0x8a;
  330. host->ports[0]->ioaddr.altstatus_addr = mmio_base + 0x8a;
  331. ata_sff_std_ports(&host->ports[0]->ioaddr);
  332. host->ports[1]->ioaddr.bmdma_addr = mmio_base + 0x08;
  333. host->ports[1]->ioaddr.cmd_addr = mmio_base + 0xc0;
  334. host->ports[1]->ioaddr.ctl_addr = mmio_base + 0xca;
  335. host->ports[1]->ioaddr.altstatus_addr = mmio_base + 0xca;
  336. ata_sff_std_ports(&host->ports[1]->ioaddr);
  337. /* Register & activate */
  338. return ata_host_activate(host, pdev->irq, ata_bmdma_interrupt,
  339. IRQF_SHARED, &sil680_sht);
  340. use_ioports:
  341. return ata_pci_bmdma_init_one(pdev, ppi, &sil680_sht, NULL, 0);
  342. }
  343. #ifdef CONFIG_PM_SLEEP
  344. static int sil680_reinit_one(struct pci_dev *pdev)
  345. {
  346. struct ata_host *host = pci_get_drvdata(pdev);
  347. int try_mmio, rc;
  348. rc = ata_pci_device_do_resume(pdev);
  349. if (rc)
  350. return rc;
  351. sil680_init_chip(pdev, &try_mmio);
  352. ata_host_resume(host);
  353. return 0;
  354. }
  355. #endif
  356. static const struct pci_device_id sil680[] = {
  357. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_SII_680), },
  358. { },
  359. };
  360. static struct pci_driver sil680_pci_driver = {
  361. .name = DRV_NAME,
  362. .id_table = sil680,
  363. .probe = sil680_init_one,
  364. .remove = ata_pci_remove_one,
  365. #ifdef CONFIG_PM_SLEEP
  366. .suspend = ata_pci_device_suspend,
  367. .resume = sil680_reinit_one,
  368. #endif
  369. };
  370. module_pci_driver(sil680_pci_driver);
  371. MODULE_AUTHOR("Alan Cox");
  372. MODULE_DESCRIPTION("low-level driver for SI680 PATA");
  373. MODULE_LICENSE("GPL");
  374. MODULE_DEVICE_TABLE(pci, sil680);
  375. MODULE_VERSION(DRV_VERSION);