pata_via.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * pata_via.c - VIA PATA for new ATA layer
  3. * (C) 2005-2006 Red Hat Inc
  4. *
  5. * Documentation
  6. * Most chipset documentation available under NDA only
  7. *
  8. * VIA version guide
  9. * VIA VT82C561 - early design, uses ata_generic currently
  10. * VIA VT82C576 - MWDMA, 33Mhz
  11. * VIA VT82C586 - MWDMA, 33Mhz
  12. * VIA VT82C586a - Added UDMA to 33Mhz
  13. * VIA VT82C586b - UDMA33
  14. * VIA VT82C596a - Nonfunctional UDMA66
  15. * VIA VT82C596b - Working UDMA66
  16. * VIA VT82C686 - Nonfunctional UDMA66
  17. * VIA VT82C686a - Working UDMA66
  18. * VIA VT82C686b - Updated to UDMA100
  19. * VIA VT8231 - UDMA100
  20. * VIA VT8233 - UDMA100
  21. * VIA VT8233a - UDMA133
  22. * VIA VT8233c - UDMA100
  23. * VIA VT8235 - UDMA133
  24. * VIA VT8237 - UDMA133
  25. * VIA VT8237S - UDMA133
  26. * VIA VT8251 - UDMA133
  27. *
  28. * Most registers remain compatible across chips. Others start reserved
  29. * and acquire sensible semantics if set to 1 (eg cable detect). A few
  30. * exceptions exist, notably around the FIFO settings.
  31. *
  32. * One additional quirk of the VIA design is that like ALi they use few
  33. * PCI IDs for a lot of chips.
  34. *
  35. * Based heavily on:
  36. *
  37. * Version 3.38
  38. *
  39. * VIA IDE driver for Linux. Supported southbridges:
  40. *
  41. * vt82c576, vt82c586, vt82c586a, vt82c586b, vt82c596a, vt82c596b,
  42. * vt82c686, vt82c686a, vt82c686b, vt8231, vt8233, vt8233c, vt8233a,
  43. * vt8235, vt8237
  44. *
  45. * Copyright (c) 2000-2002 Vojtech Pavlik
  46. *
  47. * Based on the work of:
  48. * Michel Aubry
  49. * Jeff Garzik
  50. * Andre Hedrick
  51. */
  52. #include <linux/kernel.h>
  53. #include <linux/module.h>
  54. #include <linux/pci.h>
  55. #include <linux/init.h>
  56. #include <linux/blkdev.h>
  57. #include <linux/delay.h>
  58. #include <scsi/scsi_host.h>
  59. #include <linux/libata.h>
  60. #include <linux/dmi.h>
  61. #define DRV_NAME "pata_via"
  62. #define DRV_VERSION "0.3.4"
  63. /*
  64. * The following comes directly from Vojtech Pavlik's ide/pci/via82cxxx
  65. * driver.
  66. */
  67. enum {
  68. VIA_UDMA = 0x007,
  69. VIA_UDMA_NONE = 0x000,
  70. VIA_UDMA_33 = 0x001,
  71. VIA_UDMA_66 = 0x002,
  72. VIA_UDMA_100 = 0x003,
  73. VIA_UDMA_133 = 0x004,
  74. VIA_BAD_PREQ = 0x010, /* Crashes if PREQ# till DDACK# set */
  75. VIA_BAD_CLK66 = 0x020, /* 66 MHz clock doesn't work correctly */
  76. VIA_SET_FIFO = 0x040, /* Needs to have FIFO split set */
  77. VIA_NO_UNMASK = 0x080, /* Doesn't work with IRQ unmasking on */
  78. VIA_BAD_ID = 0x100, /* Has wrong vendor ID (0x1107) */
  79. VIA_BAD_AST = 0x200, /* Don't touch Address Setup Timing */
  80. VIA_NO_ENABLES = 0x400, /* Has no enablebits */
  81. VIA_SATA_PATA = 0x800, /* SATA/PATA combined configuration */
  82. };
  83. enum {
  84. VIA_IDFLAG_SINGLE = (1 << 0), /* single channel controller) */
  85. };
  86. /*
  87. * VIA SouthBridge chips.
  88. */
  89. static const struct via_isa_bridge {
  90. const char *name;
  91. u16 id;
  92. u8 rev_min;
  93. u8 rev_max;
  94. u16 flags;
  95. } via_isa_bridges[] = {
  96. { "vx855", PCI_DEVICE_ID_VIA_VX855, 0x00, 0x2f,
  97. VIA_UDMA_133 | VIA_BAD_AST | VIA_SATA_PATA },
  98. { "vx800", PCI_DEVICE_ID_VIA_VX800, 0x00, 0x2f, VIA_UDMA_133 |
  99. VIA_BAD_AST | VIA_SATA_PATA },
  100. { "vt8261", PCI_DEVICE_ID_VIA_8261, 0x00, 0x2f,
  101. VIA_UDMA_133 | VIA_BAD_AST },
  102. { "vt8237s", PCI_DEVICE_ID_VIA_8237S, 0x00, 0x2f, VIA_UDMA_133 | VIA_BAD_AST },
  103. { "vt8251", PCI_DEVICE_ID_VIA_8251, 0x00, 0x2f, VIA_UDMA_133 | VIA_BAD_AST },
  104. { "cx700", PCI_DEVICE_ID_VIA_CX700, 0x00, 0x2f, VIA_UDMA_133 | VIA_BAD_AST | VIA_SATA_PATA },
  105. { "vt6410", PCI_DEVICE_ID_VIA_6410, 0x00, 0x2f, VIA_UDMA_133 | VIA_BAD_AST | VIA_NO_ENABLES },
  106. { "vt6415", PCI_DEVICE_ID_VIA_6415, 0x00, 0xff, VIA_UDMA_133 | VIA_BAD_AST | VIA_NO_ENABLES },
  107. { "vt8237a", PCI_DEVICE_ID_VIA_8237A, 0x00, 0x2f, VIA_UDMA_133 | VIA_BAD_AST },
  108. { "vt8237", PCI_DEVICE_ID_VIA_8237, 0x00, 0x2f, VIA_UDMA_133 | VIA_BAD_AST },
  109. { "vt8235", PCI_DEVICE_ID_VIA_8235, 0x00, 0x2f, VIA_UDMA_133 | VIA_BAD_AST },
  110. { "vt8233a", PCI_DEVICE_ID_VIA_8233A, 0x00, 0x2f, VIA_UDMA_133 | VIA_BAD_AST },
  111. { "vt8233c", PCI_DEVICE_ID_VIA_8233C_0, 0x00, 0x2f, VIA_UDMA_100 },
  112. { "vt8233", PCI_DEVICE_ID_VIA_8233_0, 0x00, 0x2f, VIA_UDMA_100 },
  113. { "vt8231", PCI_DEVICE_ID_VIA_8231, 0x00, 0x2f, VIA_UDMA_100 },
  114. { "vt82c686b", PCI_DEVICE_ID_VIA_82C686, 0x40, 0x4f, VIA_UDMA_100 },
  115. { "vt82c686a", PCI_DEVICE_ID_VIA_82C686, 0x10, 0x2f, VIA_UDMA_66 },
  116. { "vt82c686", PCI_DEVICE_ID_VIA_82C686, 0x00, 0x0f, VIA_UDMA_33 | VIA_BAD_CLK66 },
  117. { "vt82c596b", PCI_DEVICE_ID_VIA_82C596, 0x10, 0x2f, VIA_UDMA_66 },
  118. { "vt82c596a", PCI_DEVICE_ID_VIA_82C596, 0x00, 0x0f, VIA_UDMA_33 | VIA_BAD_CLK66 },
  119. { "vt82c586b", PCI_DEVICE_ID_VIA_82C586_0, 0x47, 0x4f, VIA_UDMA_33 | VIA_SET_FIFO },
  120. { "vt82c586b", PCI_DEVICE_ID_VIA_82C586_0, 0x40, 0x46, VIA_UDMA_33 | VIA_SET_FIFO | VIA_BAD_PREQ },
  121. { "vt82c586b", PCI_DEVICE_ID_VIA_82C586_0, 0x30, 0x3f, VIA_UDMA_33 | VIA_SET_FIFO },
  122. { "vt82c586a", PCI_DEVICE_ID_VIA_82C586_0, 0x20, 0x2f, VIA_UDMA_33 | VIA_SET_FIFO },
  123. { "vt82c586", PCI_DEVICE_ID_VIA_82C586_0, 0x00, 0x0f, VIA_UDMA_NONE | VIA_SET_FIFO },
  124. { "vt82c576", PCI_DEVICE_ID_VIA_82C576, 0x00, 0x2f, VIA_UDMA_NONE | VIA_SET_FIFO | VIA_NO_UNMASK },
  125. { "vt82c576", PCI_DEVICE_ID_VIA_82C576, 0x00, 0x2f, VIA_UDMA_NONE | VIA_SET_FIFO | VIA_NO_UNMASK | VIA_BAD_ID },
  126. { "vtxxxx", PCI_DEVICE_ID_VIA_ANON, 0x00, 0x2f,
  127. VIA_UDMA_133 | VIA_BAD_AST },
  128. { NULL }
  129. };
  130. struct via_port {
  131. u8 cached_device;
  132. };
  133. /*
  134. * Cable special cases
  135. */
  136. static const struct dmi_system_id cable_dmi_table[] = {
  137. {
  138. .ident = "Acer Ferrari 3400",
  139. .matches = {
  140. DMI_MATCH(DMI_BOARD_VENDOR, "Acer,Inc."),
  141. DMI_MATCH(DMI_BOARD_NAME, "Ferrari 3400"),
  142. },
  143. },
  144. { }
  145. };
  146. static int via_cable_override(struct pci_dev *pdev)
  147. {
  148. /* Systems by DMI */
  149. if (dmi_check_system(cable_dmi_table))
  150. return 1;
  151. /* Arima W730-K8/Targa Visionary 811/... */
  152. if (pdev->subsystem_vendor == 0x161F && pdev->subsystem_device == 0x2032)
  153. return 1;
  154. return 0;
  155. }
  156. /**
  157. * via_cable_detect - cable detection
  158. * @ap: ATA port
  159. *
  160. * Perform cable detection. Actually for the VIA case the BIOS
  161. * already did this for us. We read the values provided by the
  162. * BIOS. If you are using an 8235 in a non-PC configuration you
  163. * may need to update this code.
  164. *
  165. * Hotplug also impacts on this.
  166. */
  167. static int via_cable_detect(struct ata_port *ap) {
  168. const struct via_isa_bridge *config = ap->host->private_data;
  169. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  170. u32 ata66;
  171. if (via_cable_override(pdev))
  172. return ATA_CBL_PATA40_SHORT;
  173. if ((config->flags & VIA_SATA_PATA) && ap->port_no == 0)
  174. return ATA_CBL_SATA;
  175. /* Early chips are 40 wire */
  176. if ((config->flags & VIA_UDMA) < VIA_UDMA_66)
  177. return ATA_CBL_PATA40;
  178. /* UDMA 66 chips have only drive side logic */
  179. else if ((config->flags & VIA_UDMA) < VIA_UDMA_100)
  180. return ATA_CBL_PATA_UNK;
  181. /* UDMA 100 or later */
  182. pci_read_config_dword(pdev, 0x50, &ata66);
  183. /* Check both the drive cable reporting bits, we might not have
  184. two drives */
  185. if (ata66 & (0x10100000 >> (16 * ap->port_no)))
  186. return ATA_CBL_PATA80;
  187. /* Check with ACPI so we can spot BIOS reported SATA bridges */
  188. if (ata_acpi_init_gtm(ap) &&
  189. ata_acpi_cbl_80wire(ap, ata_acpi_init_gtm(ap)))
  190. return ATA_CBL_PATA80;
  191. return ATA_CBL_PATA40;
  192. }
  193. static int via_pre_reset(struct ata_link *link, unsigned long deadline)
  194. {
  195. struct ata_port *ap = link->ap;
  196. const struct via_isa_bridge *config = ap->host->private_data;
  197. if (!(config->flags & VIA_NO_ENABLES)) {
  198. static const struct pci_bits via_enable_bits[] = {
  199. { 0x40, 1, 0x02, 0x02 },
  200. { 0x40, 1, 0x01, 0x01 }
  201. };
  202. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  203. if (!pci_test_config_bits(pdev, &via_enable_bits[ap->port_no]))
  204. return -ENOENT;
  205. }
  206. return ata_sff_prereset(link, deadline);
  207. }
  208. /**
  209. * via_do_set_mode - set initial PIO mode data
  210. * @ap: ATA interface
  211. * @adev: ATA device
  212. * @mode: ATA mode being programmed
  213. * @tdiv: Clocks per PCI clock
  214. * @set_ast: Set to program address setup
  215. * @udma_type: UDMA mode/format of registers
  216. *
  217. * Program the VIA registers for DMA and PIO modes. Uses the ata timing
  218. * support in order to compute modes.
  219. *
  220. * FIXME: Hotplug will require we serialize multiple mode changes
  221. * on the two channels.
  222. */
  223. static void via_do_set_mode(struct ata_port *ap, struct ata_device *adev, int mode, int tdiv, int set_ast, int udma_type)
  224. {
  225. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  226. struct ata_device *peer = ata_dev_pair(adev);
  227. struct ata_timing t, p;
  228. static int via_clock = 33333; /* Bus clock in kHZ - ought to be tunable one day */
  229. unsigned long T = 1000000000 / via_clock;
  230. unsigned long UT = T/tdiv;
  231. int ut;
  232. int offset = 3 - (2*ap->port_no) - adev->devno;
  233. /* Calculate the timing values we require */
  234. ata_timing_compute(adev, mode, &t, T, UT);
  235. /* We share 8bit timing so we must merge the constraints */
  236. if (peer) {
  237. if (peer->pio_mode) {
  238. ata_timing_compute(peer, peer->pio_mode, &p, T, UT);
  239. ata_timing_merge(&p, &t, &t, ATA_TIMING_8BIT);
  240. }
  241. }
  242. /* Address setup is programmable but breaks on UDMA133 setups */
  243. if (set_ast) {
  244. u8 setup; /* 2 bits per drive */
  245. int shift = 2 * offset;
  246. pci_read_config_byte(pdev, 0x4C, &setup);
  247. setup &= ~(3 << shift);
  248. setup |= clamp_val(t.setup, 1, 4) << shift; /* 1,4 or 1,4 - 1 FIXME */
  249. pci_write_config_byte(pdev, 0x4C, setup);
  250. }
  251. /* Load the PIO mode bits */
  252. pci_write_config_byte(pdev, 0x4F - ap->port_no,
  253. ((clamp_val(t.act8b, 1, 16) - 1) << 4) | (clamp_val(t.rec8b, 1, 16) - 1));
  254. pci_write_config_byte(pdev, 0x48 + offset,
  255. ((clamp_val(t.active, 1, 16) - 1) << 4) | (clamp_val(t.recover, 1, 16) - 1));
  256. /* Load the UDMA bits according to type */
  257. switch(udma_type) {
  258. default:
  259. /* BUG() ? */
  260. /* fall through */
  261. case 33:
  262. ut = t.udma ? (0xe0 | (clamp_val(t.udma, 2, 5) - 2)) : 0x03;
  263. break;
  264. case 66:
  265. ut = t.udma ? (0xe8 | (clamp_val(t.udma, 2, 9) - 2)) : 0x0f;
  266. break;
  267. case 100:
  268. ut = t.udma ? (0xe0 | (clamp_val(t.udma, 2, 9) - 2)) : 0x07;
  269. break;
  270. case 133:
  271. ut = t.udma ? (0xe0 | (clamp_val(t.udma, 2, 9) - 2)) : 0x07;
  272. break;
  273. }
  274. /* Set UDMA unless device is not UDMA capable */
  275. if (udma_type) {
  276. u8 udma_etc;
  277. pci_read_config_byte(pdev, 0x50 + offset, &udma_etc);
  278. /* clear transfer mode bit */
  279. udma_etc &= ~0x20;
  280. if (t.udma) {
  281. /* preserve 80-wire cable detection bit */
  282. udma_etc &= 0x10;
  283. udma_etc |= ut;
  284. }
  285. pci_write_config_byte(pdev, 0x50 + offset, udma_etc);
  286. }
  287. }
  288. static void via_set_piomode(struct ata_port *ap, struct ata_device *adev)
  289. {
  290. const struct via_isa_bridge *config = ap->host->private_data;
  291. int set_ast = (config->flags & VIA_BAD_AST) ? 0 : 1;
  292. int mode = config->flags & VIA_UDMA;
  293. static u8 tclock[5] = { 1, 1, 2, 3, 4 };
  294. static u8 udma[5] = { 0, 33, 66, 100, 133 };
  295. via_do_set_mode(ap, adev, adev->pio_mode, tclock[mode], set_ast, udma[mode]);
  296. }
  297. static void via_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  298. {
  299. const struct via_isa_bridge *config = ap->host->private_data;
  300. int set_ast = (config->flags & VIA_BAD_AST) ? 0 : 1;
  301. int mode = config->flags & VIA_UDMA;
  302. static u8 tclock[5] = { 1, 1, 2, 3, 4 };
  303. static u8 udma[5] = { 0, 33, 66, 100, 133 };
  304. via_do_set_mode(ap, adev, adev->dma_mode, tclock[mode], set_ast, udma[mode]);
  305. }
  306. /**
  307. * via_mode_filter - filter buggy device/mode pairs
  308. * @dev: ATA device
  309. * @mask: Mode bitmask
  310. *
  311. * We need to apply some minimal filtering for old controllers and at least
  312. * one breed of Transcend SSD. Return the updated mask.
  313. */
  314. static unsigned long via_mode_filter(struct ata_device *dev, unsigned long mask)
  315. {
  316. struct ata_host *host = dev->link->ap->host;
  317. const struct via_isa_bridge *config = host->private_data;
  318. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  319. if (config->id == PCI_DEVICE_ID_VIA_82C586_0) {
  320. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  321. if (strcmp(model_num, "TS64GSSD25-M") == 0) {
  322. ata_dev_printk(dev, KERN_WARNING,
  323. "disabling UDMA mode due to reported lockups with this device.\n");
  324. mask &= ~ ATA_MASK_UDMA;
  325. }
  326. }
  327. return ata_bmdma_mode_filter(dev, mask);
  328. }
  329. /**
  330. * via_tf_load - send taskfile registers to host controller
  331. * @ap: Port to which output is sent
  332. * @tf: ATA taskfile register set
  333. *
  334. * Outputs ATA taskfile to standard ATA host controller.
  335. *
  336. * Note: This is to fix the internal bug of via chipsets, which
  337. * will reset the device register after changing the IEN bit on
  338. * ctl register
  339. */
  340. static void via_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  341. {
  342. struct ata_ioports *ioaddr = &ap->ioaddr;
  343. struct via_port *vp = ap->private_data;
  344. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  345. int newctl = 0;
  346. if (tf->ctl != ap->last_ctl) {
  347. iowrite8(tf->ctl, ioaddr->ctl_addr);
  348. ap->last_ctl = tf->ctl;
  349. ata_wait_idle(ap);
  350. newctl = 1;
  351. }
  352. if (tf->flags & ATA_TFLAG_DEVICE) {
  353. iowrite8(tf->device, ioaddr->device_addr);
  354. vp->cached_device = tf->device;
  355. } else if (newctl)
  356. iowrite8(vp->cached_device, ioaddr->device_addr);
  357. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  358. WARN_ON_ONCE(!ioaddr->ctl_addr);
  359. iowrite8(tf->hob_feature, ioaddr->feature_addr);
  360. iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
  361. iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
  362. iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
  363. iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
  364. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  365. tf->hob_feature,
  366. tf->hob_nsect,
  367. tf->hob_lbal,
  368. tf->hob_lbam,
  369. tf->hob_lbah);
  370. }
  371. if (is_addr) {
  372. iowrite8(tf->feature, ioaddr->feature_addr);
  373. iowrite8(tf->nsect, ioaddr->nsect_addr);
  374. iowrite8(tf->lbal, ioaddr->lbal_addr);
  375. iowrite8(tf->lbam, ioaddr->lbam_addr);
  376. iowrite8(tf->lbah, ioaddr->lbah_addr);
  377. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  378. tf->feature,
  379. tf->nsect,
  380. tf->lbal,
  381. tf->lbam,
  382. tf->lbah);
  383. }
  384. ata_wait_idle(ap);
  385. }
  386. static int via_port_start(struct ata_port *ap)
  387. {
  388. struct via_port *vp;
  389. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  390. int ret = ata_sff_port_start(ap);
  391. if (ret < 0)
  392. return ret;
  393. vp = devm_kzalloc(&pdev->dev, sizeof(struct via_port), GFP_KERNEL);
  394. if (vp == NULL)
  395. return -ENOMEM;
  396. ap->private_data = vp;
  397. return 0;
  398. }
  399. static struct scsi_host_template via_sht = {
  400. ATA_BMDMA_SHT(DRV_NAME),
  401. };
  402. static struct ata_port_operations via_port_ops = {
  403. .inherits = &ata_bmdma_port_ops,
  404. .cable_detect = via_cable_detect,
  405. .set_piomode = via_set_piomode,
  406. .set_dmamode = via_set_dmamode,
  407. .prereset = via_pre_reset,
  408. .sff_tf_load = via_tf_load,
  409. .port_start = via_port_start,
  410. .mode_filter = via_mode_filter,
  411. };
  412. static struct ata_port_operations via_port_ops_noirq = {
  413. .inherits = &via_port_ops,
  414. .sff_data_xfer = ata_sff_data_xfer_noirq,
  415. };
  416. /**
  417. * via_config_fifo - set up the FIFO
  418. * @pdev: PCI device
  419. * @flags: configuration flags
  420. *
  421. * Set the FIFO properties for this device if necessary. Used both on
  422. * set up and on and the resume path
  423. */
  424. static void via_config_fifo(struct pci_dev *pdev, unsigned int flags)
  425. {
  426. u8 enable;
  427. /* 0x40 low bits indicate enabled channels */
  428. pci_read_config_byte(pdev, 0x40 , &enable);
  429. enable &= 3;
  430. if (flags & VIA_SET_FIFO) {
  431. static const u8 fifo_setting[4] = {0x00, 0x60, 0x00, 0x20};
  432. u8 fifo;
  433. pci_read_config_byte(pdev, 0x43, &fifo);
  434. /* Clear PREQ# until DDACK# for errata */
  435. if (flags & VIA_BAD_PREQ)
  436. fifo &= 0x7F;
  437. else
  438. fifo &= 0x9f;
  439. /* Turn on FIFO for enabled channels */
  440. fifo |= fifo_setting[enable];
  441. pci_write_config_byte(pdev, 0x43, fifo);
  442. }
  443. }
  444. /**
  445. * via_init_one - discovery callback
  446. * @pdev: PCI device
  447. * @id: PCI table info
  448. *
  449. * A VIA IDE interface has been discovered. Figure out what revision
  450. * and perform configuration work before handing it to the ATA layer
  451. */
  452. static int via_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  453. {
  454. /* Early VIA without UDMA support */
  455. static const struct ata_port_info via_mwdma_info = {
  456. .flags = ATA_FLAG_SLAVE_POSS,
  457. .pio_mask = ATA_PIO4,
  458. .mwdma_mask = ATA_MWDMA2,
  459. .port_ops = &via_port_ops
  460. };
  461. /* Ditto with IRQ masking required */
  462. static const struct ata_port_info via_mwdma_info_borked = {
  463. .flags = ATA_FLAG_SLAVE_POSS,
  464. .pio_mask = ATA_PIO4,
  465. .mwdma_mask = ATA_MWDMA2,
  466. .port_ops = &via_port_ops_noirq,
  467. };
  468. /* VIA UDMA 33 devices (and borked 66) */
  469. static const struct ata_port_info via_udma33_info = {
  470. .flags = ATA_FLAG_SLAVE_POSS,
  471. .pio_mask = ATA_PIO4,
  472. .mwdma_mask = ATA_MWDMA2,
  473. .udma_mask = ATA_UDMA2,
  474. .port_ops = &via_port_ops
  475. };
  476. /* VIA UDMA 66 devices */
  477. static const struct ata_port_info via_udma66_info = {
  478. .flags = ATA_FLAG_SLAVE_POSS,
  479. .pio_mask = ATA_PIO4,
  480. .mwdma_mask = ATA_MWDMA2,
  481. .udma_mask = ATA_UDMA4,
  482. .port_ops = &via_port_ops
  483. };
  484. /* VIA UDMA 100 devices */
  485. static const struct ata_port_info via_udma100_info = {
  486. .flags = ATA_FLAG_SLAVE_POSS,
  487. .pio_mask = ATA_PIO4,
  488. .mwdma_mask = ATA_MWDMA2,
  489. .udma_mask = ATA_UDMA5,
  490. .port_ops = &via_port_ops
  491. };
  492. /* UDMA133 with bad AST (All current 133) */
  493. static const struct ata_port_info via_udma133_info = {
  494. .flags = ATA_FLAG_SLAVE_POSS,
  495. .pio_mask = ATA_PIO4,
  496. .mwdma_mask = ATA_MWDMA2,
  497. .udma_mask = ATA_UDMA6, /* FIXME: should check north bridge */
  498. .port_ops = &via_port_ops
  499. };
  500. const struct ata_port_info *ppi[] = { NULL, NULL };
  501. struct pci_dev *isa;
  502. const struct via_isa_bridge *config;
  503. static int printed_version;
  504. u8 enable;
  505. u32 timing;
  506. unsigned long flags = id->driver_data;
  507. int rc;
  508. if (!printed_version++)
  509. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  510. rc = pcim_enable_device(pdev);
  511. if (rc)
  512. return rc;
  513. if (flags & VIA_IDFLAG_SINGLE)
  514. ppi[1] = &ata_dummy_port_info;
  515. /* To find out how the IDE will behave and what features we
  516. actually have to look at the bridge not the IDE controller */
  517. for (config = via_isa_bridges; config->id != PCI_DEVICE_ID_VIA_ANON;
  518. config++)
  519. if ((isa = pci_get_device(PCI_VENDOR_ID_VIA +
  520. !!(config->flags & VIA_BAD_ID),
  521. config->id, NULL))) {
  522. u8 rev = isa->revision;
  523. pci_dev_put(isa);
  524. if (rev >= config->rev_min && rev <= config->rev_max)
  525. break;
  526. }
  527. if (!(config->flags & VIA_NO_ENABLES)) {
  528. /* 0x40 low bits indicate enabled channels */
  529. pci_read_config_byte(pdev, 0x40 , &enable);
  530. enable &= 3;
  531. if (enable == 0)
  532. return -ENODEV;
  533. }
  534. /* Initialise the FIFO for the enabled channels. */
  535. via_config_fifo(pdev, config->flags);
  536. /* Clock set up */
  537. switch(config->flags & VIA_UDMA) {
  538. case VIA_UDMA_NONE:
  539. if (config->flags & VIA_NO_UNMASK)
  540. ppi[0] = &via_mwdma_info_borked;
  541. else
  542. ppi[0] = &via_mwdma_info;
  543. break;
  544. case VIA_UDMA_33:
  545. ppi[0] = &via_udma33_info;
  546. break;
  547. case VIA_UDMA_66:
  548. ppi[0] = &via_udma66_info;
  549. /* The 66 MHz devices require we enable the clock */
  550. pci_read_config_dword(pdev, 0x50, &timing);
  551. timing |= 0x80008;
  552. pci_write_config_dword(pdev, 0x50, timing);
  553. break;
  554. case VIA_UDMA_100:
  555. ppi[0] = &via_udma100_info;
  556. break;
  557. case VIA_UDMA_133:
  558. ppi[0] = &via_udma133_info;
  559. break;
  560. default:
  561. WARN_ON(1);
  562. return -ENODEV;
  563. }
  564. if (config->flags & VIA_BAD_CLK66) {
  565. /* Disable the 66MHz clock on problem devices */
  566. pci_read_config_dword(pdev, 0x50, &timing);
  567. timing &= ~0x80008;
  568. pci_write_config_dword(pdev, 0x50, timing);
  569. }
  570. /* We have established the device type, now fire it up */
  571. return ata_pci_sff_init_one(pdev, ppi, &via_sht, (void *)config);
  572. }
  573. #ifdef CONFIG_PM
  574. /**
  575. * via_reinit_one - reinit after resume
  576. * @pdev; PCI device
  577. *
  578. * Called when the VIA PATA device is resumed. We must then
  579. * reconfigure the fifo and other setup we may have altered. In
  580. * addition the kernel needs to have the resume methods on PCI
  581. * quirk supported.
  582. */
  583. static int via_reinit_one(struct pci_dev *pdev)
  584. {
  585. u32 timing;
  586. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  587. const struct via_isa_bridge *config = host->private_data;
  588. int rc;
  589. rc = ata_pci_device_do_resume(pdev);
  590. if (rc)
  591. return rc;
  592. via_config_fifo(pdev, config->flags);
  593. if ((config->flags & VIA_UDMA) == VIA_UDMA_66) {
  594. /* The 66 MHz devices require we enable the clock */
  595. pci_read_config_dword(pdev, 0x50, &timing);
  596. timing |= 0x80008;
  597. pci_write_config_dword(pdev, 0x50, timing);
  598. }
  599. if (config->flags & VIA_BAD_CLK66) {
  600. /* Disable the 66MHz clock on problem devices */
  601. pci_read_config_dword(pdev, 0x50, &timing);
  602. timing &= ~0x80008;
  603. pci_write_config_dword(pdev, 0x50, timing);
  604. }
  605. ata_host_resume(host);
  606. return 0;
  607. }
  608. #endif
  609. static const struct pci_device_id via[] = {
  610. { PCI_VDEVICE(VIA, 0x0415), },
  611. { PCI_VDEVICE(VIA, 0x0571), },
  612. { PCI_VDEVICE(VIA, 0x0581), },
  613. { PCI_VDEVICE(VIA, 0x1571), },
  614. { PCI_VDEVICE(VIA, 0x3164), },
  615. { PCI_VDEVICE(VIA, 0x5324), },
  616. { PCI_VDEVICE(VIA, 0xC409), VIA_IDFLAG_SINGLE },
  617. { },
  618. };
  619. static struct pci_driver via_pci_driver = {
  620. .name = DRV_NAME,
  621. .id_table = via,
  622. .probe = via_init_one,
  623. .remove = ata_pci_remove_one,
  624. #ifdef CONFIG_PM
  625. .suspend = ata_pci_device_suspend,
  626. .resume = via_reinit_one,
  627. #endif
  628. };
  629. static int __init via_init(void)
  630. {
  631. return pci_register_driver(&via_pci_driver);
  632. }
  633. static void __exit via_exit(void)
  634. {
  635. pci_unregister_driver(&via_pci_driver);
  636. }
  637. MODULE_AUTHOR("Alan Cox");
  638. MODULE_DESCRIPTION("low-level driver for VIA PATA");
  639. MODULE_LICENSE("GPL");
  640. MODULE_DEVICE_TABLE(pci, via);
  641. MODULE_VERSION(DRV_VERSION);
  642. module_init(via_init);
  643. module_exit(via_exit);