sata_via.c 17 KB

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