sata_via.c 21 KB

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