sata_via.c 20 KB

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