sata_via.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * sata_via.c - VIA Serial ATA controllers
  3. *
  4. * Maintained by: Tejun Heo <tj@kernel.org>
  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/driver-api/libata.rst
  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/blkdev.h>
  39. #include <linux/delay.h>
  40. #include <linux/device.h>
  41. #include <scsi/scsi.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_host.h>
  44. #include <linux/libata.h>
  45. #define DRV_NAME "sata_via"
  46. #define DRV_VERSION "2.6"
  47. /*
  48. * vt8251 is different from other sata controllers of VIA. It has two
  49. * channels, each channel has both Master and Slave slot.
  50. */
  51. enum board_ids_enum {
  52. vt6420,
  53. vt6421,
  54. vt8251,
  55. };
  56. enum {
  57. SATA_CHAN_ENAB = 0x40, /* SATA channel enable */
  58. SATA_INT_GATE = 0x41, /* SATA interrupt gating */
  59. SATA_NATIVE_MODE = 0x42, /* Native mode enable */
  60. SVIA_MISC_3 = 0x46, /* Miscellaneous Control III */
  61. PATA_UDMA_TIMING = 0xB3, /* PATA timing for DMA/ cable detect */
  62. PATA_PIO_TIMING = 0xAB, /* PATA timing register */
  63. PORT0 = (1 << 1),
  64. PORT1 = (1 << 0),
  65. ALL_PORTS = PORT0 | PORT1,
  66. NATIVE_MODE_ALL = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4),
  67. SATA_EXT_PHY = (1 << 6), /* 0==use PATA, 1==ext phy */
  68. SATA_HOTPLUG = (1 << 5), /* enable IRQ on hotplug */
  69. };
  70. struct svia_priv {
  71. bool wd_workaround;
  72. };
  73. static int vt6420_hotplug;
  74. module_param_named(vt6420_hotplug, vt6420_hotplug, int, 0644);
  75. MODULE_PARM_DESC(vt6420_hotplug, "Enable hot-plug support for VT6420 (0=Don't support, 1=support)");
  76. static int svia_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  77. #ifdef CONFIG_PM_SLEEP
  78. static int svia_pci_device_resume(struct pci_dev *pdev);
  79. #endif
  80. static int svia_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
  81. static int svia_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
  82. static int vt8251_scr_read(struct ata_link *link, unsigned int scr, u32 *val);
  83. static int vt8251_scr_write(struct ata_link *link, unsigned int scr, u32 val);
  84. static void svia_tf_load(struct ata_port *ap, const struct ata_taskfile *tf);
  85. static void svia_noop_freeze(struct ata_port *ap);
  86. static int vt6420_prereset(struct ata_link *link, unsigned long deadline);
  87. static void vt6420_bmdma_start(struct ata_queued_cmd *qc);
  88. static int vt6421_pata_cable_detect(struct ata_port *ap);
  89. static void vt6421_set_pio_mode(struct ata_port *ap, struct ata_device *adev);
  90. static void vt6421_set_dma_mode(struct ata_port *ap, struct ata_device *adev);
  91. static void vt6421_error_handler(struct ata_port *ap);
  92. static const struct pci_device_id svia_pci_tbl[] = {
  93. { PCI_VDEVICE(VIA, 0x5337), vt6420 },
  94. { PCI_VDEVICE(VIA, 0x0591), vt6420 }, /* 2 sata chnls (Master) */
  95. { PCI_VDEVICE(VIA, 0x3149), vt6420 }, /* 2 sata chnls (Master) */
  96. { PCI_VDEVICE(VIA, 0x3249), vt6421 }, /* 2 sata chnls, 1 pata chnl */
  97. { PCI_VDEVICE(VIA, 0x5372), vt6420 },
  98. { PCI_VDEVICE(VIA, 0x7372), vt6420 },
  99. { PCI_VDEVICE(VIA, 0x5287), vt8251 }, /* 2 sata chnls (Master/Slave) */
  100. { PCI_VDEVICE(VIA, 0x9000), vt8251 },
  101. { } /* terminate list */
  102. };
  103. static struct pci_driver svia_pci_driver = {
  104. .name = DRV_NAME,
  105. .id_table = svia_pci_tbl,
  106. .probe = svia_init_one,
  107. #ifdef CONFIG_PM_SLEEP
  108. .suspend = ata_pci_device_suspend,
  109. .resume = svia_pci_device_resume,
  110. #endif
  111. .remove = ata_pci_remove_one,
  112. };
  113. static struct scsi_host_template svia_sht = {
  114. ATA_BMDMA_SHT(DRV_NAME),
  115. };
  116. static struct ata_port_operations svia_base_ops = {
  117. .inherits = &ata_bmdma_port_ops,
  118. .sff_tf_load = svia_tf_load,
  119. };
  120. static struct ata_port_operations vt6420_sata_ops = {
  121. .inherits = &svia_base_ops,
  122. .freeze = svia_noop_freeze,
  123. .prereset = vt6420_prereset,
  124. .bmdma_start = vt6420_bmdma_start,
  125. };
  126. static struct ata_port_operations vt6421_pata_ops = {
  127. .inherits = &svia_base_ops,
  128. .cable_detect = vt6421_pata_cable_detect,
  129. .set_piomode = vt6421_set_pio_mode,
  130. .set_dmamode = vt6421_set_dma_mode,
  131. };
  132. static struct ata_port_operations vt6421_sata_ops = {
  133. .inherits = &svia_base_ops,
  134. .scr_read = svia_scr_read,
  135. .scr_write = svia_scr_write,
  136. .error_handler = vt6421_error_handler,
  137. };
  138. static struct ata_port_operations vt8251_ops = {
  139. .inherits = &svia_base_ops,
  140. .hardreset = sata_std_hardreset,
  141. .scr_read = vt8251_scr_read,
  142. .scr_write = vt8251_scr_write,
  143. };
  144. static const struct ata_port_info vt6420_port_info = {
  145. .flags = ATA_FLAG_SATA,
  146. .pio_mask = ATA_PIO4,
  147. .mwdma_mask = ATA_MWDMA2,
  148. .udma_mask = ATA_UDMA6,
  149. .port_ops = &vt6420_sata_ops,
  150. };
  151. static const struct ata_port_info vt6421_sport_info = {
  152. .flags = ATA_FLAG_SATA,
  153. .pio_mask = ATA_PIO4,
  154. .mwdma_mask = ATA_MWDMA2,
  155. .udma_mask = ATA_UDMA6,
  156. .port_ops = &vt6421_sata_ops,
  157. };
  158. static const struct ata_port_info vt6421_pport_info = {
  159. .flags = ATA_FLAG_SLAVE_POSS,
  160. .pio_mask = ATA_PIO4,
  161. /* No MWDMA */
  162. .udma_mask = ATA_UDMA6,
  163. .port_ops = &vt6421_pata_ops,
  164. };
  165. static const struct ata_port_info vt8251_port_info = {
  166. .flags = ATA_FLAG_SATA | ATA_FLAG_SLAVE_POSS,
  167. .pio_mask = ATA_PIO4,
  168. .mwdma_mask = ATA_MWDMA2,
  169. .udma_mask = ATA_UDMA6,
  170. .port_ops = &vt8251_ops,
  171. };
  172. MODULE_AUTHOR("Jeff Garzik");
  173. MODULE_DESCRIPTION("SCSI low-level driver for VIA SATA controllers");
  174. MODULE_LICENSE("GPL");
  175. MODULE_DEVICE_TABLE(pci, svia_pci_tbl);
  176. MODULE_VERSION(DRV_VERSION);
  177. static int svia_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
  178. {
  179. if (sc_reg > SCR_CONTROL)
  180. return -EINVAL;
  181. *val = ioread32(link->ap->ioaddr.scr_addr + (4 * sc_reg));
  182. return 0;
  183. }
  184. static int svia_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
  185. {
  186. if (sc_reg > SCR_CONTROL)
  187. return -EINVAL;
  188. iowrite32(val, link->ap->ioaddr.scr_addr + (4 * sc_reg));
  189. return 0;
  190. }
  191. static int vt8251_scr_read(struct ata_link *link, unsigned int scr, u32 *val)
  192. {
  193. static const u8 ipm_tbl[] = { 1, 2, 6, 0 };
  194. struct pci_dev *pdev = to_pci_dev(link->ap->host->dev);
  195. int slot = 2 * link->ap->port_no + link->pmp;
  196. u32 v = 0;
  197. u8 raw;
  198. switch (scr) {
  199. case SCR_STATUS:
  200. pci_read_config_byte(pdev, 0xA0 + slot, &raw);
  201. /* read the DET field, bit0 and 1 of the config byte */
  202. v |= raw & 0x03;
  203. /* read the SPD field, bit4 of the configure byte */
  204. if (raw & (1 << 4))
  205. v |= 0x02 << 4;
  206. else
  207. v |= 0x01 << 4;
  208. /* read the IPM field, bit2 and 3 of the config byte */
  209. v |= ipm_tbl[(raw >> 2) & 0x3];
  210. break;
  211. case SCR_ERROR:
  212. /* devices other than 5287 uses 0xA8 as base */
  213. WARN_ON(pdev->device != 0x5287);
  214. pci_read_config_dword(pdev, 0xB0 + slot * 4, &v);
  215. break;
  216. case SCR_CONTROL:
  217. pci_read_config_byte(pdev, 0xA4 + slot, &raw);
  218. /* read the DET field, bit0 and bit1 */
  219. v |= ((raw & 0x02) << 1) | (raw & 0x01);
  220. /* read the IPM field, bit2 and bit3 */
  221. v |= ((raw >> 2) & 0x03) << 8;
  222. break;
  223. default:
  224. return -EINVAL;
  225. }
  226. *val = v;
  227. return 0;
  228. }
  229. static int vt8251_scr_write(struct ata_link *link, unsigned int scr, u32 val)
  230. {
  231. struct pci_dev *pdev = to_pci_dev(link->ap->host->dev);
  232. int slot = 2 * link->ap->port_no + link->pmp;
  233. u32 v = 0;
  234. switch (scr) {
  235. case SCR_ERROR:
  236. /* devices other than 5287 uses 0xA8 as base */
  237. WARN_ON(pdev->device != 0x5287);
  238. pci_write_config_dword(pdev, 0xB0 + slot * 4, val);
  239. return 0;
  240. case SCR_CONTROL:
  241. /* set the DET field */
  242. v |= ((val & 0x4) >> 1) | (val & 0x1);
  243. /* set the IPM field */
  244. v |= ((val >> 8) & 0x3) << 2;
  245. pci_write_config_byte(pdev, 0xA4 + slot, v);
  246. return 0;
  247. default:
  248. return -EINVAL;
  249. }
  250. }
  251. /**
  252. * svia_tf_load - send taskfile registers to host controller
  253. * @ap: Port to which output is sent
  254. * @tf: ATA taskfile register set
  255. *
  256. * Outputs ATA taskfile to standard ATA host controller.
  257. *
  258. * This is to fix the internal bug of via chipsets, which will
  259. * reset the device register after changing the IEN bit on ctl
  260. * register.
  261. */
  262. static void svia_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  263. {
  264. struct ata_taskfile ttf;
  265. if (tf->ctl != ap->last_ctl) {
  266. ttf = *tf;
  267. ttf.flags |= ATA_TFLAG_DEVICE;
  268. tf = &ttf;
  269. }
  270. ata_sff_tf_load(ap, tf);
  271. }
  272. static void svia_noop_freeze(struct ata_port *ap)
  273. {
  274. /* Some VIA controllers choke if ATA_NIEN is manipulated in
  275. * certain way. Leave it alone and just clear pending IRQ.
  276. */
  277. ap->ops->sff_check_status(ap);
  278. ata_bmdma_irq_clear(ap);
  279. }
  280. /**
  281. * vt6420_prereset - prereset for vt6420
  282. * @link: target ATA link
  283. * @deadline: deadline jiffies for the operation
  284. *
  285. * SCR registers on vt6420 are pieces of shit and may hang the
  286. * whole machine completely if accessed with the wrong timing.
  287. * To avoid such catastrophe, vt6420 doesn't provide generic SCR
  288. * access operations, but uses SStatus and SControl only during
  289. * boot probing in controlled way.
  290. *
  291. * As the old (pre EH update) probing code is proven to work, we
  292. * strictly follow the access pattern.
  293. *
  294. * LOCKING:
  295. * Kernel thread context (may sleep)
  296. *
  297. * RETURNS:
  298. * 0 on success, -errno otherwise.
  299. */
  300. static int vt6420_prereset(struct ata_link *link, unsigned long deadline)
  301. {
  302. struct ata_port *ap = link->ap;
  303. struct ata_eh_context *ehc = &ap->link.eh_context;
  304. unsigned long timeout = jiffies + (HZ * 5);
  305. u32 sstatus, scontrol;
  306. int online;
  307. /* don't do any SCR stuff if we're not loading */
  308. if (!(ap->pflags & ATA_PFLAG_LOADING))
  309. goto skip_scr;
  310. /* Resume phy. This is the old SATA resume sequence */
  311. svia_scr_write(link, SCR_CONTROL, 0x300);
  312. svia_scr_read(link, SCR_CONTROL, &scontrol); /* flush */
  313. /* wait for phy to become ready, if necessary */
  314. do {
  315. ata_msleep(link->ap, 200);
  316. svia_scr_read(link, SCR_STATUS, &sstatus);
  317. if ((sstatus & 0xf) != 1)
  318. break;
  319. } while (time_before(jiffies, timeout));
  320. /* open code sata_print_link_status() */
  321. svia_scr_read(link, SCR_STATUS, &sstatus);
  322. svia_scr_read(link, SCR_CONTROL, &scontrol);
  323. online = (sstatus & 0xf) == 0x3;
  324. ata_port_info(ap,
  325. "SATA link %s 1.5 Gbps (SStatus %X SControl %X)\n",
  326. online ? "up" : "down", sstatus, scontrol);
  327. /* SStatus is read one more time */
  328. svia_scr_read(link, SCR_STATUS, &sstatus);
  329. if (!online) {
  330. /* tell EH to bail */
  331. ehc->i.action &= ~ATA_EH_RESET;
  332. return 0;
  333. }
  334. skip_scr:
  335. /* wait for !BSY */
  336. ata_sff_wait_ready(link, deadline);
  337. return 0;
  338. }
  339. static void vt6420_bmdma_start(struct ata_queued_cmd *qc)
  340. {
  341. struct ata_port *ap = qc->ap;
  342. if ((qc->tf.command == ATA_CMD_PACKET) &&
  343. (qc->scsicmd->sc_data_direction == DMA_TO_DEVICE)) {
  344. /* Prevents corruption on some ATAPI burners */
  345. ata_sff_pause(ap);
  346. }
  347. ata_bmdma_start(qc);
  348. }
  349. static int vt6421_pata_cable_detect(struct ata_port *ap)
  350. {
  351. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  352. u8 tmp;
  353. pci_read_config_byte(pdev, PATA_UDMA_TIMING, &tmp);
  354. if (tmp & 0x10)
  355. return ATA_CBL_PATA40;
  356. return ATA_CBL_PATA80;
  357. }
  358. static void vt6421_set_pio_mode(struct ata_port *ap, struct ata_device *adev)
  359. {
  360. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  361. static const u8 pio_bits[] = { 0xA8, 0x65, 0x65, 0x31, 0x20 };
  362. pci_write_config_byte(pdev, PATA_PIO_TIMING - adev->devno,
  363. pio_bits[adev->pio_mode - XFER_PIO_0]);
  364. }
  365. static void vt6421_set_dma_mode(struct ata_port *ap, struct ata_device *adev)
  366. {
  367. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  368. static const u8 udma_bits[] = { 0xEE, 0xE8, 0xE6, 0xE4, 0xE2, 0xE1, 0xE0, 0xE0 };
  369. pci_write_config_byte(pdev, PATA_UDMA_TIMING - adev->devno,
  370. udma_bits[adev->dma_mode - XFER_UDMA_0]);
  371. }
  372. static const unsigned int svia_bar_sizes[] = {
  373. 8, 4, 8, 4, 16, 256
  374. };
  375. static const unsigned int vt6421_bar_sizes[] = {
  376. 16, 16, 16, 16, 32, 128
  377. };
  378. static void __iomem *svia_scr_addr(void __iomem *addr, unsigned int port)
  379. {
  380. return addr + (port * 128);
  381. }
  382. static void __iomem *vt6421_scr_addr(void __iomem *addr, unsigned int port)
  383. {
  384. return addr + (port * 64);
  385. }
  386. static void vt6421_init_addrs(struct ata_port *ap)
  387. {
  388. void __iomem * const * iomap = ap->host->iomap;
  389. void __iomem *reg_addr = iomap[ap->port_no];
  390. void __iomem *bmdma_addr = iomap[4] + (ap->port_no * 8);
  391. struct ata_ioports *ioaddr = &ap->ioaddr;
  392. ioaddr->cmd_addr = reg_addr;
  393. ioaddr->altstatus_addr =
  394. ioaddr->ctl_addr = (void __iomem *)
  395. ((unsigned long)(reg_addr + 8) | ATA_PCI_CTL_OFS);
  396. ioaddr->bmdma_addr = bmdma_addr;
  397. ioaddr->scr_addr = vt6421_scr_addr(iomap[5], ap->port_no);
  398. ata_sff_std_ports(ioaddr);
  399. ata_port_pbar_desc(ap, ap->port_no, -1, "port");
  400. ata_port_pbar_desc(ap, 4, ap->port_no * 8, "bmdma");
  401. }
  402. static int vt6420_prepare_host(struct pci_dev *pdev, struct ata_host **r_host)
  403. {
  404. const struct ata_port_info *ppi[] = { &vt6420_port_info, NULL };
  405. struct ata_host *host;
  406. int rc;
  407. if (vt6420_hotplug) {
  408. ppi[0]->port_ops->scr_read = svia_scr_read;
  409. ppi[0]->port_ops->scr_write = svia_scr_write;
  410. }
  411. rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
  412. if (rc)
  413. return rc;
  414. *r_host = host;
  415. rc = pcim_iomap_regions(pdev, 1 << 5, DRV_NAME);
  416. if (rc) {
  417. dev_err(&pdev->dev, "failed to iomap PCI BAR 5\n");
  418. return rc;
  419. }
  420. host->ports[0]->ioaddr.scr_addr = svia_scr_addr(host->iomap[5], 0);
  421. host->ports[1]->ioaddr.scr_addr = svia_scr_addr(host->iomap[5], 1);
  422. return 0;
  423. }
  424. static int vt6421_prepare_host(struct pci_dev *pdev, struct ata_host **r_host)
  425. {
  426. const struct ata_port_info *ppi[] =
  427. { &vt6421_sport_info, &vt6421_sport_info, &vt6421_pport_info };
  428. struct ata_host *host;
  429. int i, rc;
  430. *r_host = host = ata_host_alloc_pinfo(&pdev->dev, ppi, ARRAY_SIZE(ppi));
  431. if (!host) {
  432. dev_err(&pdev->dev, "failed to allocate host\n");
  433. return -ENOMEM;
  434. }
  435. rc = pcim_iomap_regions(pdev, 0x3f, DRV_NAME);
  436. if (rc) {
  437. dev_err(&pdev->dev, "failed to request/iomap PCI BARs (errno=%d)\n",
  438. rc);
  439. return rc;
  440. }
  441. host->iomap = pcim_iomap_table(pdev);
  442. for (i = 0; i < host->n_ports; i++)
  443. vt6421_init_addrs(host->ports[i]);
  444. rc = dma_set_mask(&pdev->dev, ATA_DMA_MASK);
  445. if (rc)
  446. return rc;
  447. rc = dma_set_coherent_mask(&pdev->dev, ATA_DMA_MASK);
  448. if (rc)
  449. return rc;
  450. return 0;
  451. }
  452. static int vt8251_prepare_host(struct pci_dev *pdev, struct ata_host **r_host)
  453. {
  454. const struct ata_port_info *ppi[] = { &vt8251_port_info, NULL };
  455. struct ata_host *host;
  456. int i, rc;
  457. rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
  458. if (rc)
  459. return rc;
  460. *r_host = host;
  461. rc = pcim_iomap_regions(pdev, 1 << 5, DRV_NAME);
  462. if (rc) {
  463. dev_err(&pdev->dev, "failed to iomap PCI BAR 5\n");
  464. return rc;
  465. }
  466. /* 8251 hosts four sata ports as M/S of the two channels */
  467. for (i = 0; i < host->n_ports; i++)
  468. ata_slave_link_init(host->ports[i]);
  469. return 0;
  470. }
  471. static void svia_wd_fix(struct pci_dev *pdev)
  472. {
  473. u8 tmp8;
  474. pci_read_config_byte(pdev, 0x52, &tmp8);
  475. pci_write_config_byte(pdev, 0x52, tmp8 | BIT(2));
  476. }
  477. static irqreturn_t vt642x_interrupt(int irq, void *dev_instance)
  478. {
  479. struct ata_host *host = dev_instance;
  480. irqreturn_t rc = ata_bmdma_interrupt(irq, dev_instance);
  481. /* if the IRQ was not handled, it might be a hotplug IRQ */
  482. if (rc != IRQ_HANDLED) {
  483. u32 serror;
  484. unsigned long flags;
  485. spin_lock_irqsave(&host->lock, flags);
  486. /* check for hotplug on port 0 */
  487. svia_scr_read(&host->ports[0]->link, SCR_ERROR, &serror);
  488. if (serror & SERR_PHYRDY_CHG) {
  489. ata_ehi_hotplugged(&host->ports[0]->link.eh_info);
  490. ata_port_freeze(host->ports[0]);
  491. rc = IRQ_HANDLED;
  492. }
  493. /* check for hotplug on port 1 */
  494. svia_scr_read(&host->ports[1]->link, SCR_ERROR, &serror);
  495. if (serror & SERR_PHYRDY_CHG) {
  496. ata_ehi_hotplugged(&host->ports[1]->link.eh_info);
  497. ata_port_freeze(host->ports[1]);
  498. rc = IRQ_HANDLED;
  499. }
  500. spin_unlock_irqrestore(&host->lock, flags);
  501. }
  502. return rc;
  503. }
  504. static void vt6421_error_handler(struct ata_port *ap)
  505. {
  506. struct svia_priv *hpriv = ap->host->private_data;
  507. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  508. u32 serror;
  509. /* see svia_configure() for description */
  510. if (!hpriv->wd_workaround) {
  511. svia_scr_read(&ap->link, SCR_ERROR, &serror);
  512. if (serror == 0x1000500) {
  513. ata_port_warn(ap, "Incompatible drive: enabling workaround. This slows down transfer rate to ~60 MB/s");
  514. svia_wd_fix(pdev);
  515. hpriv->wd_workaround = true;
  516. ap->link.eh_context.i.flags |= ATA_EHI_QUIET;
  517. }
  518. }
  519. ata_sff_error_handler(ap);
  520. }
  521. static void svia_configure(struct pci_dev *pdev, int board_id,
  522. struct svia_priv *hpriv)
  523. {
  524. u8 tmp8;
  525. pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, &tmp8);
  526. dev_info(&pdev->dev, "routed to hard irq line %d\n",
  527. (int) (tmp8 & 0xf0) == 0xf0 ? 0 : tmp8 & 0x0f);
  528. /* make sure SATA channels are enabled */
  529. pci_read_config_byte(pdev, SATA_CHAN_ENAB, &tmp8);
  530. if ((tmp8 & ALL_PORTS) != ALL_PORTS) {
  531. dev_dbg(&pdev->dev, "enabling SATA channels (0x%x)\n",
  532. (int)tmp8);
  533. tmp8 |= ALL_PORTS;
  534. pci_write_config_byte(pdev, SATA_CHAN_ENAB, tmp8);
  535. }
  536. /* make sure interrupts for each channel sent to us */
  537. pci_read_config_byte(pdev, SATA_INT_GATE, &tmp8);
  538. if ((tmp8 & ALL_PORTS) != ALL_PORTS) {
  539. dev_dbg(&pdev->dev, "enabling SATA channel interrupts (0x%x)\n",
  540. (int) tmp8);
  541. tmp8 |= ALL_PORTS;
  542. pci_write_config_byte(pdev, SATA_INT_GATE, tmp8);
  543. }
  544. /* make sure native mode is enabled */
  545. pci_read_config_byte(pdev, SATA_NATIVE_MODE, &tmp8);
  546. if ((tmp8 & NATIVE_MODE_ALL) != NATIVE_MODE_ALL) {
  547. dev_dbg(&pdev->dev,
  548. "enabling SATA channel native mode (0x%x)\n",
  549. (int) tmp8);
  550. tmp8 |= NATIVE_MODE_ALL;
  551. pci_write_config_byte(pdev, SATA_NATIVE_MODE, tmp8);
  552. }
  553. if ((board_id == vt6420 && vt6420_hotplug) || board_id == vt6421) {
  554. /* enable IRQ on hotplug */
  555. pci_read_config_byte(pdev, SVIA_MISC_3, &tmp8);
  556. if ((tmp8 & SATA_HOTPLUG) != SATA_HOTPLUG) {
  557. dev_dbg(&pdev->dev,
  558. "enabling SATA hotplug (0x%x)\n",
  559. (int) tmp8);
  560. tmp8 |= SATA_HOTPLUG;
  561. pci_write_config_byte(pdev, SVIA_MISC_3, tmp8);
  562. }
  563. }
  564. /*
  565. * vt6420/1 has problems talking to some drives. The following
  566. * is the fix from Joseph Chan <JosephChan@via.com.tw>.
  567. *
  568. * When host issues HOLD, device may send up to 20DW of data
  569. * before acknowledging it with HOLDA and the host should be
  570. * able to buffer them in FIFO. Unfortunately, some WD drives
  571. * send up to 40DW before acknowledging HOLD and, in the
  572. * default configuration, this ends up overflowing vt6421's
  573. * FIFO, making the controller abort the transaction with
  574. * R_ERR.
  575. *
  576. * Rx52[2] is the internal 128DW FIFO Flow control watermark
  577. * adjusting mechanism enable bit and the default value 0
  578. * means host will issue HOLD to device when the left FIFO
  579. * size goes below 32DW. Setting it to 1 makes the watermark
  580. * 64DW.
  581. *
  582. * https://bugzilla.kernel.org/show_bug.cgi?id=15173
  583. * http://article.gmane.org/gmane.linux.ide/46352
  584. * http://thread.gmane.org/gmane.linux.kernel/1062139
  585. *
  586. * As the fix slows down data transfer, apply it only if the error
  587. * actually appears - see vt6421_error_handler()
  588. * Apply the fix always on vt6420 as we don't know if SCR_ERROR can be
  589. * read safely.
  590. */
  591. if (board_id == vt6420) {
  592. svia_wd_fix(pdev);
  593. hpriv->wd_workaround = true;
  594. }
  595. }
  596. static int svia_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  597. {
  598. unsigned int i;
  599. int rc;
  600. struct ata_host *host = NULL;
  601. int board_id = (int) ent->driver_data;
  602. const unsigned *bar_sizes;
  603. struct svia_priv *hpriv;
  604. ata_print_version_once(&pdev->dev, DRV_VERSION);
  605. rc = pcim_enable_device(pdev);
  606. if (rc)
  607. return rc;
  608. if (board_id == vt6421)
  609. bar_sizes = &vt6421_bar_sizes[0];
  610. else
  611. bar_sizes = &svia_bar_sizes[0];
  612. for (i = 0; i < ARRAY_SIZE(svia_bar_sizes); i++)
  613. if ((pci_resource_start(pdev, i) == 0) ||
  614. (pci_resource_len(pdev, i) < bar_sizes[i])) {
  615. dev_err(&pdev->dev,
  616. "invalid PCI BAR %u (sz 0x%llx, val 0x%llx)\n",
  617. i,
  618. (unsigned long long)pci_resource_start(pdev, i),
  619. (unsigned long long)pci_resource_len(pdev, i));
  620. return -ENODEV;
  621. }
  622. switch (board_id) {
  623. case vt6420:
  624. rc = vt6420_prepare_host(pdev, &host);
  625. break;
  626. case vt6421:
  627. rc = vt6421_prepare_host(pdev, &host);
  628. break;
  629. case vt8251:
  630. rc = vt8251_prepare_host(pdev, &host);
  631. break;
  632. default:
  633. rc = -EINVAL;
  634. }
  635. if (rc)
  636. return rc;
  637. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  638. if (!hpriv)
  639. return -ENOMEM;
  640. host->private_data = hpriv;
  641. svia_configure(pdev, board_id, hpriv);
  642. pci_set_master(pdev);
  643. if ((board_id == vt6420 && vt6420_hotplug) || board_id == vt6421)
  644. return ata_host_activate(host, pdev->irq, vt642x_interrupt,
  645. IRQF_SHARED, &svia_sht);
  646. else
  647. return ata_host_activate(host, pdev->irq, ata_bmdma_interrupt,
  648. IRQF_SHARED, &svia_sht);
  649. }
  650. #ifdef CONFIG_PM_SLEEP
  651. static int svia_pci_device_resume(struct pci_dev *pdev)
  652. {
  653. struct ata_host *host = pci_get_drvdata(pdev);
  654. struct svia_priv *hpriv = host->private_data;
  655. int rc;
  656. rc = ata_pci_device_do_resume(pdev);
  657. if (rc)
  658. return rc;
  659. if (hpriv->wd_workaround)
  660. svia_wd_fix(pdev);
  661. ata_host_resume(host);
  662. return 0;
  663. }
  664. #endif
  665. module_pci_driver(svia_pci_driver);