sata_via.c 18 KB

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