sata_via.c 18 KB

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