sata_via.c 17 KB

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