sata_via.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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_port *ap, unsigned int sc_reg, u32 *val);
  64. static int svia_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val);
  65. static void svia_noop_freeze(struct ata_port *ap);
  66. static void vt6420_error_handler(struct ata_port *ap);
  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. .error_handler = vt6420_error_handler,
  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_port *ap, unsigned int sc_reg, u32 *val)
  136. {
  137. if (sc_reg > SCR_CONTROL)
  138. return -EINVAL;
  139. *val = ioread32(ap->ioaddr.scr_addr + (4 * sc_reg));
  140. return 0;
  141. }
  142. static int svia_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val)
  143. {
  144. if (sc_reg > SCR_CONTROL)
  145. return -EINVAL;
  146. iowrite32(val, 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. ata_chk_status(ap);
  155. ata_bmdma_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(ap, SCR_CONTROL, 0x300);
  189. svia_scr_read(ap, SCR_CONTROL, &scontrol); /* flush */
  190. /* wait for phy to become ready, if necessary */
  191. do {
  192. msleep(200);
  193. svia_scr_read(ap, 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(ap, SCR_STATUS, &sstatus);
  199. svia_scr_read(ap, 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(ap, 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_wait_ready(ap, deadline);
  214. return 0;
  215. }
  216. static void vt6420_error_handler(struct ata_port *ap)
  217. {
  218. ata_bmdma_drive_eh(ap, vt6420_prereset, ata_std_softreset, NULL,
  219. ata_std_postreset);
  220. }
  221. static int vt6421_pata_cable_detect(struct ata_port *ap)
  222. {
  223. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  224. u8 tmp;
  225. pci_read_config_byte(pdev, PATA_UDMA_TIMING, &tmp);
  226. if (tmp & 0x10)
  227. return ATA_CBL_PATA40;
  228. return ATA_CBL_PATA80;
  229. }
  230. static void vt6421_set_pio_mode(struct ata_port *ap, struct ata_device *adev)
  231. {
  232. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  233. static const u8 pio_bits[] = { 0xA8, 0x65, 0x65, 0x31, 0x20 };
  234. pci_write_config_byte(pdev, PATA_PIO_TIMING, pio_bits[adev->pio_mode - XFER_PIO_0]);
  235. }
  236. static void vt6421_set_dma_mode(struct ata_port *ap, struct ata_device *adev)
  237. {
  238. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  239. static const u8 udma_bits[] = { 0xEE, 0xE8, 0xE6, 0xE4, 0xE2, 0xE1, 0xE0, 0xE0 };
  240. pci_write_config_byte(pdev, PATA_UDMA_TIMING, udma_bits[adev->dma_mode - XFER_UDMA_0]);
  241. }
  242. static const unsigned int svia_bar_sizes[] = {
  243. 8, 4, 8, 4, 16, 256
  244. };
  245. static const unsigned int vt6421_bar_sizes[] = {
  246. 16, 16, 16, 16, 32, 128
  247. };
  248. static void __iomem *svia_scr_addr(void __iomem *addr, unsigned int port)
  249. {
  250. return addr + (port * 128);
  251. }
  252. static void __iomem *vt6421_scr_addr(void __iomem *addr, unsigned int port)
  253. {
  254. return addr + (port * 64);
  255. }
  256. static void vt6421_init_addrs(struct ata_port *ap)
  257. {
  258. void __iomem * const * iomap = ap->host->iomap;
  259. void __iomem *reg_addr = iomap[ap->port_no];
  260. void __iomem *bmdma_addr = iomap[4] + (ap->port_no * 8);
  261. struct ata_ioports *ioaddr = &ap->ioaddr;
  262. ioaddr->cmd_addr = reg_addr;
  263. ioaddr->altstatus_addr =
  264. ioaddr->ctl_addr = (void __iomem *)
  265. ((unsigned long)(reg_addr + 8) | ATA_PCI_CTL_OFS);
  266. ioaddr->bmdma_addr = bmdma_addr;
  267. ioaddr->scr_addr = vt6421_scr_addr(iomap[5], ap->port_no);
  268. ata_std_ports(ioaddr);
  269. ata_port_pbar_desc(ap, ap->port_no, -1, "port");
  270. ata_port_pbar_desc(ap, 4, ap->port_no * 8, "bmdma");
  271. }
  272. static int vt6420_prepare_host(struct pci_dev *pdev, struct ata_host **r_host)
  273. {
  274. const struct ata_port_info *ppi[] = { &vt6420_port_info, NULL };
  275. struct ata_host *host;
  276. int rc;
  277. rc = ata_pci_prepare_sff_host(pdev, ppi, &host);
  278. if (rc)
  279. return rc;
  280. *r_host = host;
  281. rc = pcim_iomap_regions(pdev, 1 << 5, DRV_NAME);
  282. if (rc) {
  283. dev_printk(KERN_ERR, &pdev->dev, "failed to iomap PCI BAR 5\n");
  284. return rc;
  285. }
  286. host->ports[0]->ioaddr.scr_addr = svia_scr_addr(host->iomap[5], 0);
  287. host->ports[1]->ioaddr.scr_addr = svia_scr_addr(host->iomap[5], 1);
  288. return 0;
  289. }
  290. static int vt6421_prepare_host(struct pci_dev *pdev, struct ata_host **r_host)
  291. {
  292. const struct ata_port_info *ppi[] =
  293. { &vt6421_sport_info, &vt6421_sport_info, &vt6421_pport_info };
  294. struct ata_host *host;
  295. int i, rc;
  296. *r_host = host = ata_host_alloc_pinfo(&pdev->dev, ppi, ARRAY_SIZE(ppi));
  297. if (!host) {
  298. dev_printk(KERN_ERR, &pdev->dev, "failed to allocate host\n");
  299. return -ENOMEM;
  300. }
  301. rc = pcim_iomap_regions(pdev, 0x3f, DRV_NAME);
  302. if (rc) {
  303. dev_printk(KERN_ERR, &pdev->dev, "failed to request/iomap "
  304. "PCI BARs (errno=%d)\n", rc);
  305. return rc;
  306. }
  307. host->iomap = pcim_iomap_table(pdev);
  308. for (i = 0; i < host->n_ports; i++)
  309. vt6421_init_addrs(host->ports[i]);
  310. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  311. if (rc)
  312. return rc;
  313. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  314. if (rc)
  315. return rc;
  316. return 0;
  317. }
  318. static void svia_configure(struct pci_dev *pdev)
  319. {
  320. u8 tmp8;
  321. pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, &tmp8);
  322. dev_printk(KERN_INFO, &pdev->dev, "routed to hard irq line %d\n",
  323. (int) (tmp8 & 0xf0) == 0xf0 ? 0 : tmp8 & 0x0f);
  324. /* make sure SATA channels are enabled */
  325. pci_read_config_byte(pdev, SATA_CHAN_ENAB, &tmp8);
  326. if ((tmp8 & ALL_PORTS) != ALL_PORTS) {
  327. dev_printk(KERN_DEBUG, &pdev->dev,
  328. "enabling SATA channels (0x%x)\n",
  329. (int) tmp8);
  330. tmp8 |= ALL_PORTS;
  331. pci_write_config_byte(pdev, SATA_CHAN_ENAB, tmp8);
  332. }
  333. /* make sure interrupts for each channel sent to us */
  334. pci_read_config_byte(pdev, SATA_INT_GATE, &tmp8);
  335. if ((tmp8 & ALL_PORTS) != ALL_PORTS) {
  336. dev_printk(KERN_DEBUG, &pdev->dev,
  337. "enabling SATA channel interrupts (0x%x)\n",
  338. (int) tmp8);
  339. tmp8 |= ALL_PORTS;
  340. pci_write_config_byte(pdev, SATA_INT_GATE, tmp8);
  341. }
  342. /* make sure native mode is enabled */
  343. pci_read_config_byte(pdev, SATA_NATIVE_MODE, &tmp8);
  344. if ((tmp8 & NATIVE_MODE_ALL) != NATIVE_MODE_ALL) {
  345. dev_printk(KERN_DEBUG, &pdev->dev,
  346. "enabling SATA channel native mode (0x%x)\n",
  347. (int) tmp8);
  348. tmp8 |= NATIVE_MODE_ALL;
  349. pci_write_config_byte(pdev, SATA_NATIVE_MODE, tmp8);
  350. }
  351. }
  352. static int svia_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  353. {
  354. static int printed_version;
  355. unsigned int i;
  356. int rc;
  357. struct ata_host *host;
  358. int board_id = (int) ent->driver_data;
  359. const unsigned *bar_sizes;
  360. if (!printed_version++)
  361. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  362. rc = pcim_enable_device(pdev);
  363. if (rc)
  364. return rc;
  365. if (board_id == vt6420)
  366. bar_sizes = &svia_bar_sizes[0];
  367. else
  368. bar_sizes = &vt6421_bar_sizes[0];
  369. for (i = 0; i < ARRAY_SIZE(svia_bar_sizes); i++)
  370. if ((pci_resource_start(pdev, i) == 0) ||
  371. (pci_resource_len(pdev, i) < bar_sizes[i])) {
  372. dev_printk(KERN_ERR, &pdev->dev,
  373. "invalid PCI BAR %u (sz 0x%llx, val 0x%llx)\n",
  374. i,
  375. (unsigned long long)pci_resource_start(pdev, i),
  376. (unsigned long long)pci_resource_len(pdev, i));
  377. return -ENODEV;
  378. }
  379. if (board_id == vt6420)
  380. rc = vt6420_prepare_host(pdev, &host);
  381. else
  382. rc = vt6421_prepare_host(pdev, &host);
  383. if (rc)
  384. return rc;
  385. svia_configure(pdev);
  386. pci_set_master(pdev);
  387. return ata_host_activate(host, pdev->irq, ata_interrupt, IRQF_SHARED,
  388. &svia_sht);
  389. }
  390. static int __init svia_init(void)
  391. {
  392. return pci_register_driver(&svia_pci_driver);
  393. }
  394. static void __exit svia_exit(void)
  395. {
  396. pci_unregister_driver(&svia_pci_driver);
  397. }
  398. module_init(svia_init);
  399. module_exit(svia_exit);