pata_via.c 20 KB

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