sata_via.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * sata_via.c - VIA Serial ATA controllers
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2004 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available under NDA.
  31. *
  32. *
  33. *
  34. */
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/pci.h>
  38. #include <linux/init.h>
  39. #include <linux/blkdev.h>
  40. #include <linux/delay.h>
  41. #include <linux/device.h>
  42. #include <scsi/scsi_host.h>
  43. #include <linux/libata.h>
  44. #define DRV_NAME "sata_via"
  45. #define DRV_VERSION "2.3"
  46. enum board_ids_enum {
  47. vt6420,
  48. vt6421,
  49. };
  50. enum {
  51. SATA_CHAN_ENAB = 0x40, /* SATA channel enable */
  52. SATA_INT_GATE = 0x41, /* SATA interrupt gating */
  53. SATA_NATIVE_MODE = 0x42, /* Native mode enable */
  54. PATA_UDMA_TIMING = 0xB3, /* PATA timing for DMA/ cable detect */
  55. PATA_PIO_TIMING = 0xAB, /* PATA timing register */
  56. PORT0 = (1 << 1),
  57. PORT1 = (1 << 0),
  58. ALL_PORTS = PORT0 | PORT1,
  59. NATIVE_MODE_ALL = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4),
  60. SATA_EXT_PHY = (1 << 6), /* 0==use PATA, 1==ext phy */
  61. };
  62. static int svia_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  63. static int svia_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
  64. static int svia_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
  65. static void svia_noop_freeze(struct ata_port *ap);
  66. static int vt6420_prereset(struct ata_link *link, unsigned long deadline);
  67. static int vt6421_pata_cable_detect(struct ata_port *ap);
  68. static void vt6421_set_pio_mode(struct ata_port *ap, struct ata_device *adev);
  69. static void vt6421_set_dma_mode(struct ata_port *ap, struct ata_device *adev);
  70. static const struct pci_device_id svia_pci_tbl[] = {
  71. { PCI_VDEVICE(VIA, 0x5337), vt6420 },
  72. { PCI_VDEVICE(VIA, 0x0591), vt6420 },
  73. { PCI_VDEVICE(VIA, 0x3149), vt6420 },
  74. { PCI_VDEVICE(VIA, 0x3249), vt6421 },
  75. { PCI_VDEVICE(VIA, 0x5287), vt6420 },
  76. { PCI_VDEVICE(VIA, 0x5372), vt6420 },
  77. { PCI_VDEVICE(VIA, 0x7372), vt6420 },
  78. { } /* terminate list */
  79. };
  80. static struct pci_driver svia_pci_driver = {
  81. .name = DRV_NAME,
  82. .id_table = svia_pci_tbl,
  83. .probe = svia_init_one,
  84. #ifdef CONFIG_PM
  85. .suspend = ata_pci_device_suspend,
  86. .resume = ata_pci_device_resume,
  87. #endif
  88. .remove = ata_pci_remove_one,
  89. };
  90. static struct scsi_host_template svia_sht = {
  91. ATA_BMDMA_SHT(DRV_NAME),
  92. };
  93. static struct ata_port_operations vt6420_sata_ops = {
  94. .inherits = &ata_bmdma_port_ops,
  95. .freeze = svia_noop_freeze,
  96. .prereset = vt6420_prereset,
  97. };
  98. static struct ata_port_operations vt6421_pata_ops = {
  99. .inherits = &ata_bmdma_port_ops,
  100. .cable_detect = vt6421_pata_cable_detect,
  101. .set_piomode = vt6421_set_pio_mode,
  102. .set_dmamode = vt6421_set_dma_mode,
  103. };
  104. static struct ata_port_operations vt6421_sata_ops = {
  105. .inherits = &ata_bmdma_port_ops,
  106. .scr_read = svia_scr_read,
  107. .scr_write = svia_scr_write,
  108. };
  109. static const struct ata_port_info vt6420_port_info = {
  110. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY,
  111. .pio_mask = 0x1f,
  112. .mwdma_mask = 0x07,
  113. .udma_mask = ATA_UDMA6,
  114. .port_ops = &vt6420_sata_ops,
  115. };
  116. static struct ata_port_info vt6421_sport_info = {
  117. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY,
  118. .pio_mask = 0x1f,
  119. .mwdma_mask = 0x07,
  120. .udma_mask = ATA_UDMA6,
  121. .port_ops = &vt6421_sata_ops,
  122. };
  123. static struct ata_port_info vt6421_pport_info = {
  124. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_LEGACY,
  125. .pio_mask = 0x1f,
  126. .mwdma_mask = 0,
  127. .udma_mask = ATA_UDMA6,
  128. .port_ops = &vt6421_pata_ops,
  129. };
  130. MODULE_AUTHOR("Jeff Garzik");
  131. MODULE_DESCRIPTION("SCSI low-level driver for VIA SATA controllers");
  132. MODULE_LICENSE("GPL");
  133. MODULE_DEVICE_TABLE(pci, svia_pci_tbl);
  134. MODULE_VERSION(DRV_VERSION);
  135. static int svia_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
  136. {
  137. if (sc_reg > SCR_CONTROL)
  138. return -EINVAL;
  139. *val = ioread32(link->ap->ioaddr.scr_addr + (4 * sc_reg));
  140. return 0;
  141. }
  142. static int svia_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
  143. {
  144. if (sc_reg > SCR_CONTROL)
  145. return -EINVAL;
  146. iowrite32(val, link->ap->ioaddr.scr_addr + (4 * sc_reg));
  147. return 0;
  148. }
  149. static void svia_noop_freeze(struct ata_port *ap)
  150. {
  151. /* Some VIA controllers choke if ATA_NIEN is manipulated in
  152. * certain way. Leave it alone and just clear pending IRQ.
  153. */
  154. ap->ops->sff_check_status(ap);
  155. ata_sff_irq_clear(ap);
  156. }
  157. /**
  158. * vt6420_prereset - prereset for vt6420
  159. * @link: target ATA link
  160. * @deadline: deadline jiffies for the operation
  161. *
  162. * SCR registers on vt6420 are pieces of shit and may hang the
  163. * whole machine completely if accessed with the wrong timing.
  164. * To avoid such catastrophe, vt6420 doesn't provide generic SCR
  165. * access operations, but uses SStatus and SControl only during
  166. * boot probing in controlled way.
  167. *
  168. * As the old (pre EH update) probing code is proven to work, we
  169. * strictly follow the access pattern.
  170. *
  171. * LOCKING:
  172. * Kernel thread context (may sleep)
  173. *
  174. * RETURNS:
  175. * 0 on success, -errno otherwise.
  176. */
  177. static int vt6420_prereset(struct ata_link *link, unsigned long deadline)
  178. {
  179. struct ata_port *ap = link->ap;
  180. struct ata_eh_context *ehc = &ap->link.eh_context;
  181. unsigned long timeout = jiffies + (HZ * 5);
  182. u32 sstatus, scontrol;
  183. int online;
  184. /* don't do any SCR stuff if we're not loading */
  185. if (!(ap->pflags & ATA_PFLAG_LOADING))
  186. goto skip_scr;
  187. /* Resume phy. This is the old SATA resume sequence */
  188. svia_scr_write(link, SCR_CONTROL, 0x300);
  189. svia_scr_read(link, SCR_CONTROL, &scontrol); /* flush */
  190. /* wait for phy to become ready, if necessary */
  191. do {
  192. msleep(200);
  193. svia_scr_read(link, SCR_STATUS, &sstatus);
  194. if ((sstatus & 0xf) != 1)
  195. break;
  196. } while (time_before(jiffies, timeout));
  197. /* open code sata_print_link_status() */
  198. svia_scr_read(link, SCR_STATUS, &sstatus);
  199. svia_scr_read(link, SCR_CONTROL, &scontrol);
  200. online = (sstatus & 0xf) == 0x3;
  201. ata_port_printk(ap, KERN_INFO,
  202. "SATA link %s 1.5 Gbps (SStatus %X SControl %X)\n",
  203. online ? "up" : "down", sstatus, scontrol);
  204. /* SStatus is read one more time */
  205. svia_scr_read(link, SCR_STATUS, &sstatus);
  206. if (!online) {
  207. /* tell EH to bail */
  208. ehc->i.action &= ~ATA_EH_RESET;
  209. return 0;
  210. }
  211. skip_scr:
  212. /* wait for !BSY */
  213. ata_sff_wait_ready(link, deadline);
  214. return 0;
  215. }
  216. static int vt6421_pata_cable_detect(struct ata_port *ap)
  217. {
  218. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  219. u8 tmp;
  220. pci_read_config_byte(pdev, PATA_UDMA_TIMING, &tmp);
  221. if (tmp & 0x10)
  222. return ATA_CBL_PATA40;
  223. return ATA_CBL_PATA80;
  224. }
  225. static void vt6421_set_pio_mode(struct ata_port *ap, struct ata_device *adev)
  226. {
  227. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  228. static const u8 pio_bits[] = { 0xA8, 0x65, 0x65, 0x31, 0x20 };
  229. pci_write_config_byte(pdev, PATA_PIO_TIMING, pio_bits[adev->pio_mode - XFER_PIO_0]);
  230. }
  231. static void vt6421_set_dma_mode(struct ata_port *ap, struct ata_device *adev)
  232. {
  233. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  234. static const u8 udma_bits[] = { 0xEE, 0xE8, 0xE6, 0xE4, 0xE2, 0xE1, 0xE0, 0xE0 };
  235. pci_write_config_byte(pdev, PATA_UDMA_TIMING, udma_bits[adev->dma_mode - XFER_UDMA_0]);
  236. }
  237. static const unsigned int svia_bar_sizes[] = {
  238. 8, 4, 8, 4, 16, 256
  239. };
  240. static const unsigned int vt6421_bar_sizes[] = {
  241. 16, 16, 16, 16, 32, 128
  242. };
  243. static void __iomem *svia_scr_addr(void __iomem *addr, unsigned int port)
  244. {
  245. return addr + (port * 128);
  246. }
  247. static void __iomem *vt6421_scr_addr(void __iomem *addr, unsigned int port)
  248. {
  249. return addr + (port * 64);
  250. }
  251. static void vt6421_init_addrs(struct ata_port *ap)
  252. {
  253. void __iomem * const * iomap = ap->host->iomap;
  254. void __iomem *reg_addr = iomap[ap->port_no];
  255. void __iomem *bmdma_addr = iomap[4] + (ap->port_no * 8);
  256. struct ata_ioports *ioaddr = &ap->ioaddr;
  257. ioaddr->cmd_addr = reg_addr;
  258. ioaddr->altstatus_addr =
  259. ioaddr->ctl_addr = (void __iomem *)
  260. ((unsigned long)(reg_addr + 8) | ATA_PCI_CTL_OFS);
  261. ioaddr->bmdma_addr = bmdma_addr;
  262. ioaddr->scr_addr = vt6421_scr_addr(iomap[5], ap->port_no);
  263. ata_sff_std_ports(ioaddr);
  264. ata_port_pbar_desc(ap, ap->port_no, -1, "port");
  265. ata_port_pbar_desc(ap, 4, ap->port_no * 8, "bmdma");
  266. }
  267. static int vt6420_prepare_host(struct pci_dev *pdev, struct ata_host **r_host)
  268. {
  269. const struct ata_port_info *ppi[] = { &vt6420_port_info, NULL };
  270. struct ata_host *host;
  271. int rc;
  272. rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
  273. if (rc)
  274. return rc;
  275. *r_host = host;
  276. rc = pcim_iomap_regions(pdev, 1 << 5, DRV_NAME);
  277. if (rc) {
  278. dev_printk(KERN_ERR, &pdev->dev, "failed to iomap PCI BAR 5\n");
  279. return rc;
  280. }
  281. host->ports[0]->ioaddr.scr_addr = svia_scr_addr(host->iomap[5], 0);
  282. host->ports[1]->ioaddr.scr_addr = svia_scr_addr(host->iomap[5], 1);
  283. return 0;
  284. }
  285. static int vt6421_prepare_host(struct pci_dev *pdev, struct ata_host **r_host)
  286. {
  287. const struct ata_port_info *ppi[] =
  288. { &vt6421_sport_info, &vt6421_sport_info, &vt6421_pport_info };
  289. struct ata_host *host;
  290. int i, rc;
  291. *r_host = host = ata_host_alloc_pinfo(&pdev->dev, ppi, ARRAY_SIZE(ppi));
  292. if (!host) {
  293. dev_printk(KERN_ERR, &pdev->dev, "failed to allocate host\n");
  294. return -ENOMEM;
  295. }
  296. rc = pcim_iomap_regions(pdev, 0x3f, DRV_NAME);
  297. if (rc) {
  298. dev_printk(KERN_ERR, &pdev->dev, "failed to request/iomap "
  299. "PCI BARs (errno=%d)\n", rc);
  300. return rc;
  301. }
  302. host->iomap = pcim_iomap_table(pdev);
  303. for (i = 0; i < host->n_ports; i++)
  304. vt6421_init_addrs(host->ports[i]);
  305. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  306. if (rc)
  307. return rc;
  308. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  309. if (rc)
  310. return rc;
  311. return 0;
  312. }
  313. static void svia_configure(struct pci_dev *pdev)
  314. {
  315. u8 tmp8;
  316. pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, &tmp8);
  317. dev_printk(KERN_INFO, &pdev->dev, "routed to hard irq line %d\n",
  318. (int) (tmp8 & 0xf0) == 0xf0 ? 0 : tmp8 & 0x0f);
  319. /* make sure SATA channels are enabled */
  320. pci_read_config_byte(pdev, SATA_CHAN_ENAB, &tmp8);
  321. if ((tmp8 & ALL_PORTS) != ALL_PORTS) {
  322. dev_printk(KERN_DEBUG, &pdev->dev,
  323. "enabling SATA channels (0x%x)\n",
  324. (int) tmp8);
  325. tmp8 |= ALL_PORTS;
  326. pci_write_config_byte(pdev, SATA_CHAN_ENAB, tmp8);
  327. }
  328. /* make sure interrupts for each channel sent to us */
  329. pci_read_config_byte(pdev, SATA_INT_GATE, &tmp8);
  330. if ((tmp8 & ALL_PORTS) != ALL_PORTS) {
  331. dev_printk(KERN_DEBUG, &pdev->dev,
  332. "enabling SATA channel interrupts (0x%x)\n",
  333. (int) tmp8);
  334. tmp8 |= ALL_PORTS;
  335. pci_write_config_byte(pdev, SATA_INT_GATE, tmp8);
  336. }
  337. /* make sure native mode is enabled */
  338. pci_read_config_byte(pdev, SATA_NATIVE_MODE, &tmp8);
  339. if ((tmp8 & NATIVE_MODE_ALL) != NATIVE_MODE_ALL) {
  340. dev_printk(KERN_DEBUG, &pdev->dev,
  341. "enabling SATA channel native mode (0x%x)\n",
  342. (int) tmp8);
  343. tmp8 |= NATIVE_MODE_ALL;
  344. pci_write_config_byte(pdev, SATA_NATIVE_MODE, tmp8);
  345. }
  346. }
  347. static int svia_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  348. {
  349. static int printed_version;
  350. unsigned int i;
  351. int rc;
  352. struct ata_host *host;
  353. int board_id = (int) ent->driver_data;
  354. const unsigned *bar_sizes;
  355. if (!printed_version++)
  356. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  357. rc = pcim_enable_device(pdev);
  358. if (rc)
  359. return rc;
  360. if (board_id == vt6420)
  361. bar_sizes = &svia_bar_sizes[0];
  362. else
  363. bar_sizes = &vt6421_bar_sizes[0];
  364. for (i = 0; i < ARRAY_SIZE(svia_bar_sizes); i++)
  365. if ((pci_resource_start(pdev, i) == 0) ||
  366. (pci_resource_len(pdev, i) < bar_sizes[i])) {
  367. dev_printk(KERN_ERR, &pdev->dev,
  368. "invalid PCI BAR %u (sz 0x%llx, val 0x%llx)\n",
  369. i,
  370. (unsigned long long)pci_resource_start(pdev, i),
  371. (unsigned long long)pci_resource_len(pdev, i));
  372. return -ENODEV;
  373. }
  374. if (board_id == vt6420)
  375. rc = vt6420_prepare_host(pdev, &host);
  376. else
  377. rc = vt6421_prepare_host(pdev, &host);
  378. if (rc)
  379. return rc;
  380. svia_configure(pdev);
  381. pci_set_master(pdev);
  382. return ata_host_activate(host, pdev->irq, ata_sff_interrupt,
  383. IRQF_SHARED, &svia_sht);
  384. }
  385. static int __init svia_init(void)
  386. {
  387. return pci_register_driver(&svia_pci_driver);
  388. }
  389. static void __exit svia_exit(void)
  390. {
  391. pci_unregister_driver(&svia_pci_driver);
  392. }
  393. module_init(svia_init);
  394. module_exit(svia_exit);