pata_via.c 20 KB

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