ata_piix.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * ata_piix.c - Intel PATA/SATA controllers
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. *
  9. * Copyright 2003-2005 Red Hat Inc
  10. * Copyright 2003-2005 Jeff Garzik
  11. *
  12. *
  13. * Copyright header from piix.c:
  14. *
  15. * Copyright (C) 1998-1999 Andrzej Krzysztofowicz, Author and Maintainer
  16. * Copyright (C) 1998-2000 Andre Hedrick <andre@linux-ide.org>
  17. * Copyright (C) 2003 Red Hat Inc <alan@redhat.com>
  18. *
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2, or (at your option)
  23. * any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; see the file COPYING. If not, write to
  32. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  33. *
  34. *
  35. * libata documentation is available via 'make {ps|pdf}docs',
  36. * as Documentation/DocBook/libata.*
  37. *
  38. * Hardware documentation available at http://developer.intel.com/
  39. *
  40. */
  41. #include <linux/kernel.h>
  42. #include <linux/module.h>
  43. #include <linux/pci.h>
  44. #include <linux/init.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/delay.h>
  47. #include "scsi.h"
  48. #include <scsi/scsi_host.h>
  49. #include <linux/libata.h>
  50. #define DRV_NAME "ata_piix"
  51. #define DRV_VERSION "1.04"
  52. enum {
  53. PIIX_IOCFG = 0x54, /* IDE I/O configuration register */
  54. ICH5_PMR = 0x90, /* port mapping register */
  55. ICH5_PCS = 0x92, /* port control and status */
  56. PIIX_SCC = 0x0A, /* sub-class code register */
  57. PIIX_FLAG_AHCI = (1 << 28), /* AHCI possible */
  58. PIIX_FLAG_CHECKINTR = (1 << 29), /* make sure PCI INTx enabled */
  59. PIIX_FLAG_COMBINED = (1 << 30), /* combined mode possible */
  60. /* combined mode. if set, PATA is channel 0.
  61. * if clear, PATA is channel 1.
  62. */
  63. PIIX_COMB_PATA_P0 = (1 << 1),
  64. PIIX_COMB = (1 << 2), /* combined mode enabled? */
  65. PIIX_PORT_ENABLED = (1 << 0),
  66. PIIX_PORT_PRESENT = (1 << 4),
  67. PIIX_80C_PRI = (1 << 5) | (1 << 4),
  68. PIIX_80C_SEC = (1 << 7) | (1 << 6),
  69. ich5_pata = 0,
  70. ich5_sata = 1,
  71. piix4_pata = 2,
  72. ich6_sata = 3,
  73. ich6_sata_rm = 4,
  74. ich7_sata = 5,
  75. esb2_sata = 6,
  76. PIIX_AHCI_DEVICE = 6,
  77. };
  78. static int piix_init_one (struct pci_dev *pdev,
  79. const struct pci_device_id *ent);
  80. static void piix_pata_phy_reset(struct ata_port *ap);
  81. static void piix_sata_phy_reset(struct ata_port *ap);
  82. static void piix_set_piomode (struct ata_port *ap, struct ata_device *adev);
  83. static void piix_set_dmamode (struct ata_port *ap, struct ata_device *adev);
  84. static unsigned int in_module_init = 1;
  85. static struct pci_device_id piix_pci_tbl[] = {
  86. #ifdef ATA_ENABLE_PATA
  87. { 0x8086, 0x7111, PCI_ANY_ID, PCI_ANY_ID, 0, 0, piix4_pata },
  88. { 0x8086, 0x24db, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_pata },
  89. { 0x8086, 0x25a2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_pata },
  90. #endif
  91. /* NOTE: The following PCI ids must be kept in sync with the
  92. * list in drivers/pci/quirks.c.
  93. */
  94. { 0x8086, 0x24d1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
  95. { 0x8086, 0x24df, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
  96. { 0x8086, 0x25a3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
  97. { 0x8086, 0x25b0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
  98. { 0x8086, 0x2651, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata },
  99. { 0x8086, 0x2652, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata_rm },
  100. { 0x8086, 0x2653, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata_rm },
  101. { 0x8086, 0x27c0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich7_sata },
  102. { 0x8086, 0x27c4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich7_sata },
  103. { 0x8086, 0x2680, PCI_ANY_ID, PCI_ANY_ID, 0, 0, esb2_sata },
  104. { } /* terminate list */
  105. };
  106. static struct pci_driver piix_pci_driver = {
  107. .name = DRV_NAME,
  108. .id_table = piix_pci_tbl,
  109. .probe = piix_init_one,
  110. .remove = ata_pci_remove_one,
  111. };
  112. static Scsi_Host_Template piix_sht = {
  113. .module = THIS_MODULE,
  114. .name = DRV_NAME,
  115. .ioctl = ata_scsi_ioctl,
  116. .queuecommand = ata_scsi_queuecmd,
  117. .eh_strategy_handler = ata_scsi_error,
  118. .can_queue = ATA_DEF_QUEUE,
  119. .this_id = ATA_SHT_THIS_ID,
  120. .sg_tablesize = LIBATA_MAX_PRD,
  121. .max_sectors = ATA_MAX_SECTORS,
  122. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  123. .emulated = ATA_SHT_EMULATED,
  124. .use_clustering = ATA_SHT_USE_CLUSTERING,
  125. .proc_name = DRV_NAME,
  126. .dma_boundary = ATA_DMA_BOUNDARY,
  127. .slave_configure = ata_scsi_slave_config,
  128. .bios_param = ata_std_bios_param,
  129. .ordered_flush = 1,
  130. };
  131. static struct ata_port_operations piix_pata_ops = {
  132. .port_disable = ata_port_disable,
  133. .set_piomode = piix_set_piomode,
  134. .set_dmamode = piix_set_dmamode,
  135. .tf_load = ata_tf_load,
  136. .tf_read = ata_tf_read,
  137. .check_status = ata_check_status,
  138. .exec_command = ata_exec_command,
  139. .dev_select = ata_std_dev_select,
  140. .phy_reset = piix_pata_phy_reset,
  141. .bmdma_setup = ata_bmdma_setup,
  142. .bmdma_start = ata_bmdma_start,
  143. .bmdma_stop = ata_bmdma_stop,
  144. .bmdma_status = ata_bmdma_status,
  145. .qc_prep = ata_qc_prep,
  146. .qc_issue = ata_qc_issue_prot,
  147. .eng_timeout = ata_eng_timeout,
  148. .irq_handler = ata_interrupt,
  149. .irq_clear = ata_bmdma_irq_clear,
  150. .port_start = ata_port_start,
  151. .port_stop = ata_port_stop,
  152. .host_stop = ata_host_stop,
  153. };
  154. static struct ata_port_operations piix_sata_ops = {
  155. .port_disable = ata_port_disable,
  156. .tf_load = ata_tf_load,
  157. .tf_read = ata_tf_read,
  158. .check_status = ata_check_status,
  159. .exec_command = ata_exec_command,
  160. .dev_select = ata_std_dev_select,
  161. .phy_reset = piix_sata_phy_reset,
  162. .bmdma_setup = ata_bmdma_setup,
  163. .bmdma_start = ata_bmdma_start,
  164. .bmdma_stop = ata_bmdma_stop,
  165. .bmdma_status = ata_bmdma_status,
  166. .qc_prep = ata_qc_prep,
  167. .qc_issue = ata_qc_issue_prot,
  168. .eng_timeout = ata_eng_timeout,
  169. .irq_handler = ata_interrupt,
  170. .irq_clear = ata_bmdma_irq_clear,
  171. .port_start = ata_port_start,
  172. .port_stop = ata_port_stop,
  173. .host_stop = ata_host_stop,
  174. };
  175. static struct ata_port_info piix_port_info[] = {
  176. /* ich5_pata */
  177. {
  178. .sht = &piix_sht,
  179. .host_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST |
  180. PIIX_FLAG_CHECKINTR,
  181. .pio_mask = 0x1f, /* pio0-4 */
  182. #if 0
  183. .mwdma_mask = 0x06, /* mwdma1-2 */
  184. #else
  185. .mwdma_mask = 0x00, /* mwdma broken */
  186. #endif
  187. .udma_mask = 0x3f, /* udma0-5 */
  188. .port_ops = &piix_pata_ops,
  189. },
  190. /* ich5_sata */
  191. {
  192. .sht = &piix_sht,
  193. .host_flags = ATA_FLAG_SATA | ATA_FLAG_SRST |
  194. PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR,
  195. .pio_mask = 0x1f, /* pio0-4 */
  196. .mwdma_mask = 0x07, /* mwdma0-2 */
  197. .udma_mask = 0x7f, /* udma0-6 */
  198. .port_ops = &piix_sata_ops,
  199. },
  200. /* piix4_pata */
  201. {
  202. .sht = &piix_sht,
  203. .host_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  204. .pio_mask = 0x1f, /* pio0-4 */
  205. #if 0
  206. .mwdma_mask = 0x06, /* mwdma1-2 */
  207. #else
  208. .mwdma_mask = 0x00, /* mwdma broken */
  209. #endif
  210. .udma_mask = ATA_UDMA_MASK_40C,
  211. .port_ops = &piix_pata_ops,
  212. },
  213. /* ich6_sata */
  214. {
  215. .sht = &piix_sht,
  216. .host_flags = ATA_FLAG_SATA | ATA_FLAG_SRST |
  217. PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR |
  218. ATA_FLAG_SLAVE_POSS,
  219. .pio_mask = 0x1f, /* pio0-4 */
  220. .mwdma_mask = 0x07, /* mwdma0-2 */
  221. .udma_mask = 0x7f, /* udma0-6 */
  222. .port_ops = &piix_sata_ops,
  223. },
  224. /* ich6_sata_rm */
  225. {
  226. .sht = &piix_sht,
  227. .host_flags = ATA_FLAG_SATA | ATA_FLAG_SRST |
  228. PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR |
  229. ATA_FLAG_SLAVE_POSS | PIIX_FLAG_AHCI,
  230. .pio_mask = 0x1f, /* pio0-4 */
  231. .mwdma_mask = 0x07, /* mwdma0-2 */
  232. .udma_mask = 0x7f, /* udma0-6 */
  233. .port_ops = &piix_sata_ops,
  234. },
  235. /* ich7_sata */
  236. {
  237. .sht = &piix_sht,
  238. .host_flags = ATA_FLAG_SATA | ATA_FLAG_SRST |
  239. PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR |
  240. ATA_FLAG_SLAVE_POSS | PIIX_FLAG_AHCI,
  241. .pio_mask = 0x1f, /* pio0-4 */
  242. .mwdma_mask = 0x07, /* mwdma0-2 */
  243. .udma_mask = 0x7f, /* udma0-6 */
  244. .port_ops = &piix_sata_ops,
  245. },
  246. /* esb2_sata */
  247. {
  248. .sht = &piix_sht,
  249. .host_flags = ATA_FLAG_SATA | ATA_FLAG_SRST |
  250. PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR |
  251. ATA_FLAG_SLAVE_POSS | PIIX_FLAG_AHCI,
  252. .pio_mask = 0x1f, /* pio0-4 */
  253. .mwdma_mask = 0x07, /* mwdma0-2 */
  254. .udma_mask = 0x7f, /* udma0-6 */
  255. .port_ops = &piix_sata_ops,
  256. },
  257. };
  258. static struct pci_bits piix_enable_bits[] = {
  259. { 0x41U, 1U, 0x80UL, 0x80UL }, /* port 0 */
  260. { 0x43U, 1U, 0x80UL, 0x80UL }, /* port 1 */
  261. };
  262. MODULE_AUTHOR("Andre Hedrick, Alan Cox, Andrzej Krzysztofowicz, Jeff Garzik");
  263. MODULE_DESCRIPTION("SCSI low-level driver for Intel PIIX/ICH ATA controllers");
  264. MODULE_LICENSE("GPL");
  265. MODULE_DEVICE_TABLE(pci, piix_pci_tbl);
  266. MODULE_VERSION(DRV_VERSION);
  267. /**
  268. * piix_pata_cbl_detect - Probe host controller cable detect info
  269. * @ap: Port for which cable detect info is desired
  270. *
  271. * Read 80c cable indicator from ATA PCI device's PCI config
  272. * register. This register is normally set by firmware (BIOS).
  273. *
  274. * LOCKING:
  275. * None (inherited from caller).
  276. */
  277. static void piix_pata_cbl_detect(struct ata_port *ap)
  278. {
  279. struct pci_dev *pdev = to_pci_dev(ap->host_set->dev);
  280. u8 tmp, mask;
  281. /* no 80c support in host controller? */
  282. if ((ap->udma_mask & ~ATA_UDMA_MASK_40C) == 0)
  283. goto cbl40;
  284. /* check BIOS cable detect results */
  285. mask = ap->hard_port_no == 0 ? PIIX_80C_PRI : PIIX_80C_SEC;
  286. pci_read_config_byte(pdev, PIIX_IOCFG, &tmp);
  287. if ((tmp & mask) == 0)
  288. goto cbl40;
  289. ap->cbl = ATA_CBL_PATA80;
  290. return;
  291. cbl40:
  292. ap->cbl = ATA_CBL_PATA40;
  293. ap->udma_mask &= ATA_UDMA_MASK_40C;
  294. }
  295. /**
  296. * piix_pata_phy_reset - Probe specified port on PATA host controller
  297. * @ap: Port to probe
  298. *
  299. * Probe PATA phy.
  300. *
  301. * LOCKING:
  302. * None (inherited from caller).
  303. */
  304. static void piix_pata_phy_reset(struct ata_port *ap)
  305. {
  306. struct pci_dev *pdev = to_pci_dev(ap->host_set->dev);
  307. if (!pci_test_config_bits(pdev, &piix_enable_bits[ap->hard_port_no])) {
  308. ata_port_disable(ap);
  309. printk(KERN_INFO "ata%u: port disabled. ignoring.\n", ap->id);
  310. return;
  311. }
  312. piix_pata_cbl_detect(ap);
  313. ata_port_probe(ap);
  314. ata_bus_reset(ap);
  315. }
  316. /**
  317. * piix_sata_probe - Probe PCI device for present SATA devices
  318. * @ap: Port associated with the PCI device we wish to probe
  319. *
  320. * Reads SATA PCI device's PCI config register Port Configuration
  321. * and Status (PCS) to determine port and device availability.
  322. *
  323. * LOCKING:
  324. * None (inherited from caller).
  325. *
  326. * RETURNS:
  327. * Non-zero if port is enabled, it may or may not have a device
  328. * attached in that case (PRESENT bit would only be set if BIOS probe
  329. * was done). Zero is returned if port is disabled.
  330. */
  331. static int piix_sata_probe (struct ata_port *ap)
  332. {
  333. struct pci_dev *pdev = to_pci_dev(ap->host_set->dev);
  334. int combined = (ap->flags & ATA_FLAG_SLAVE_POSS);
  335. int orig_mask, mask, i;
  336. u8 pcs;
  337. mask = (PIIX_PORT_PRESENT << ap->hard_port_no) |
  338. (PIIX_PORT_ENABLED << ap->hard_port_no);
  339. pci_read_config_byte(pdev, ICH5_PCS, &pcs);
  340. orig_mask = (int) pcs & 0xff;
  341. /* TODO: this is vaguely wrong for ICH6 combined mode,
  342. * where only two of the four SATA ports are mapped
  343. * onto a single ATA channel. It is also vaguely inaccurate
  344. * for ICH5, which has only two ports. However, this is ok,
  345. * as further device presence detection code will handle
  346. * any false positives produced here.
  347. */
  348. for (i = 0; i < 4; i++) {
  349. mask = (PIIX_PORT_ENABLED << i);
  350. if ((orig_mask & mask) == mask)
  351. if (combined || (i == ap->hard_port_no))
  352. return 1;
  353. }
  354. return 0;
  355. }
  356. /**
  357. * piix_sata_phy_reset - Probe specified port on SATA host controller
  358. * @ap: Port to probe
  359. *
  360. * Probe SATA phy.
  361. *
  362. * LOCKING:
  363. * None (inherited from caller).
  364. */
  365. static void piix_sata_phy_reset(struct ata_port *ap)
  366. {
  367. if (!piix_sata_probe(ap)) {
  368. ata_port_disable(ap);
  369. printk(KERN_INFO "ata%u: SATA port has no device.\n", ap->id);
  370. return;
  371. }
  372. ap->cbl = ATA_CBL_SATA;
  373. ata_port_probe(ap);
  374. ata_bus_reset(ap);
  375. }
  376. /**
  377. * piix_set_piomode - Initialize host controller PATA PIO timings
  378. * @ap: Port whose timings we are configuring
  379. * @adev: um
  380. * @pio: PIO mode, 0 - 4
  381. *
  382. * Set PIO mode for device, in host controller PCI config space.
  383. *
  384. * LOCKING:
  385. * None (inherited from caller).
  386. */
  387. static void piix_set_piomode (struct ata_port *ap, struct ata_device *adev)
  388. {
  389. unsigned int pio = adev->pio_mode - XFER_PIO_0;
  390. struct pci_dev *dev = to_pci_dev(ap->host_set->dev);
  391. unsigned int is_slave = (adev->devno != 0);
  392. unsigned int master_port= ap->hard_port_no ? 0x42 : 0x40;
  393. unsigned int slave_port = 0x44;
  394. u16 master_data;
  395. u8 slave_data;
  396. static const /* ISP RTC */
  397. u8 timings[][2] = { { 0, 0 },
  398. { 0, 0 },
  399. { 1, 0 },
  400. { 2, 1 },
  401. { 2, 3 }, };
  402. pci_read_config_word(dev, master_port, &master_data);
  403. if (is_slave) {
  404. master_data |= 0x4000;
  405. /* enable PPE, IE and TIME */
  406. master_data |= 0x0070;
  407. pci_read_config_byte(dev, slave_port, &slave_data);
  408. slave_data &= (ap->hard_port_no ? 0x0f : 0xf0);
  409. slave_data |=
  410. (timings[pio][0] << 2) |
  411. (timings[pio][1] << (ap->hard_port_no ? 4 : 0));
  412. } else {
  413. master_data &= 0xccf8;
  414. /* enable PPE, IE and TIME */
  415. master_data |= 0x0007;
  416. master_data |=
  417. (timings[pio][0] << 12) |
  418. (timings[pio][1] << 8);
  419. }
  420. pci_write_config_word(dev, master_port, master_data);
  421. if (is_slave)
  422. pci_write_config_byte(dev, slave_port, slave_data);
  423. }
  424. /**
  425. * piix_set_dmamode - Initialize host controller PATA PIO timings
  426. * @ap: Port whose timings we are configuring
  427. * @adev: um
  428. * @udma: udma mode, 0 - 6
  429. *
  430. * Set UDMA mode for device, in host controller PCI config space.
  431. *
  432. * LOCKING:
  433. * None (inherited from caller).
  434. */
  435. static void piix_set_dmamode (struct ata_port *ap, struct ata_device *adev)
  436. {
  437. unsigned int udma = adev->dma_mode; /* FIXME: MWDMA too */
  438. struct pci_dev *dev = to_pci_dev(ap->host_set->dev);
  439. u8 maslave = ap->hard_port_no ? 0x42 : 0x40;
  440. u8 speed = udma;
  441. unsigned int drive_dn = (ap->hard_port_no ? 2 : 0) + adev->devno;
  442. int a_speed = 3 << (drive_dn * 4);
  443. int u_flag = 1 << drive_dn;
  444. int v_flag = 0x01 << drive_dn;
  445. int w_flag = 0x10 << drive_dn;
  446. int u_speed = 0;
  447. int sitre;
  448. u16 reg4042, reg4a;
  449. u8 reg48, reg54, reg55;
  450. pci_read_config_word(dev, maslave, &reg4042);
  451. DPRINTK("reg4042 = 0x%04x\n", reg4042);
  452. sitre = (reg4042 & 0x4000) ? 1 : 0;
  453. pci_read_config_byte(dev, 0x48, &reg48);
  454. pci_read_config_word(dev, 0x4a, &reg4a);
  455. pci_read_config_byte(dev, 0x54, &reg54);
  456. pci_read_config_byte(dev, 0x55, &reg55);
  457. switch(speed) {
  458. case XFER_UDMA_4:
  459. case XFER_UDMA_2: u_speed = 2 << (drive_dn * 4); break;
  460. case XFER_UDMA_6:
  461. case XFER_UDMA_5:
  462. case XFER_UDMA_3:
  463. case XFER_UDMA_1: u_speed = 1 << (drive_dn * 4); break;
  464. case XFER_UDMA_0: u_speed = 0 << (drive_dn * 4); break;
  465. case XFER_MW_DMA_2:
  466. case XFER_MW_DMA_1: break;
  467. default:
  468. BUG();
  469. return;
  470. }
  471. if (speed >= XFER_UDMA_0) {
  472. if (!(reg48 & u_flag))
  473. pci_write_config_byte(dev, 0x48, reg48 | u_flag);
  474. if (speed == XFER_UDMA_5) {
  475. pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
  476. } else {
  477. pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
  478. }
  479. if ((reg4a & a_speed) != u_speed)
  480. pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed);
  481. if (speed > XFER_UDMA_2) {
  482. if (!(reg54 & v_flag))
  483. pci_write_config_byte(dev, 0x54, reg54 | v_flag);
  484. } else
  485. pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
  486. } else {
  487. if (reg48 & u_flag)
  488. pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
  489. if (reg4a & a_speed)
  490. pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
  491. if (reg54 & v_flag)
  492. pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
  493. if (reg55 & w_flag)
  494. pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
  495. }
  496. }
  497. #define AHCI_PCI_BAR 5
  498. #define AHCI_GLOBAL_CTL 0x04
  499. #define AHCI_ENABLE (1 << 31)
  500. static int piix_disable_ahci(struct pci_dev *pdev)
  501. {
  502. void __iomem *mmio;
  503. u32 tmp;
  504. int rc = 0;
  505. /* BUG: pci_enable_device has not yet been called. This
  506. * works because this device is usually set up by BIOS.
  507. */
  508. if (!pci_resource_start(pdev, AHCI_PCI_BAR) ||
  509. !pci_resource_len(pdev, AHCI_PCI_BAR))
  510. return 0;
  511. mmio = pci_iomap(pdev, AHCI_PCI_BAR, 64);
  512. if (!mmio)
  513. return -ENOMEM;
  514. tmp = readl(mmio + AHCI_GLOBAL_CTL);
  515. if (tmp & AHCI_ENABLE) {
  516. tmp &= ~AHCI_ENABLE;
  517. writel(tmp, mmio + AHCI_GLOBAL_CTL);
  518. tmp = readl(mmio + AHCI_GLOBAL_CTL);
  519. if (tmp & AHCI_ENABLE)
  520. rc = -EIO;
  521. }
  522. pci_iounmap(pdev, mmio);
  523. return rc;
  524. }
  525. /**
  526. * piix_init_one - Register PIIX ATA PCI device with kernel services
  527. * @pdev: PCI device to register
  528. * @ent: Entry in piix_pci_tbl matching with @pdev
  529. *
  530. * Called from kernel PCI layer. We probe for combined mode (sigh),
  531. * and then hand over control to libata, for it to do the rest.
  532. *
  533. * LOCKING:
  534. * Inherited from PCI layer (may sleep).
  535. *
  536. * RETURNS:
  537. * Zero on success, or -ERRNO value.
  538. */
  539. static int piix_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  540. {
  541. static int printed_version;
  542. struct ata_port_info *port_info[2];
  543. unsigned int combined = 0, n_ports = 1;
  544. unsigned int pata_chan = 0, sata_chan = 0;
  545. if (!printed_version++)
  546. printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
  547. /* no hotplugging support (FIXME) */
  548. if (!in_module_init)
  549. return -ENODEV;
  550. port_info[0] = &piix_port_info[ent->driver_data];
  551. port_info[1] = NULL;
  552. if (port_info[0]->host_flags & PIIX_FLAG_AHCI) {
  553. u8 tmp;
  554. pci_read_config_byte(pdev, PIIX_SCC, &tmp);
  555. if (tmp == PIIX_AHCI_DEVICE) {
  556. int rc = piix_disable_ahci(pdev);
  557. if (rc)
  558. return rc;
  559. }
  560. }
  561. if (port_info[0]->host_flags & PIIX_FLAG_COMBINED) {
  562. u8 tmp;
  563. pci_read_config_byte(pdev, ICH5_PMR, &tmp);
  564. if (tmp & PIIX_COMB) {
  565. combined = 1;
  566. if (tmp & PIIX_COMB_PATA_P0)
  567. sata_chan = 1;
  568. else
  569. pata_chan = 1;
  570. }
  571. }
  572. /* On ICH5, some BIOSen disable the interrupt using the
  573. * PCI_COMMAND_INTX_DISABLE bit added in PCI 2.3.
  574. * On ICH6, this bit has the same effect, but only when
  575. * MSI is disabled (and it is disabled, as we don't use
  576. * message-signalled interrupts currently).
  577. */
  578. if (port_info[0]->host_flags & PIIX_FLAG_CHECKINTR)
  579. pci_intx(pdev, 1);
  580. if (combined) {
  581. port_info[sata_chan] = &piix_port_info[ent->driver_data];
  582. port_info[sata_chan]->host_flags |= ATA_FLAG_SLAVE_POSS;
  583. port_info[pata_chan] = &piix_port_info[ich5_pata];
  584. n_ports++;
  585. printk(KERN_WARNING DRV_NAME ": combined mode detected\n");
  586. }
  587. return ata_pci_init_one(pdev, port_info, n_ports);
  588. }
  589. static int __init piix_init(void)
  590. {
  591. int rc;
  592. DPRINTK("pci_module_init\n");
  593. rc = pci_module_init(&piix_pci_driver);
  594. if (rc)
  595. return rc;
  596. in_module_init = 0;
  597. DPRINTK("done\n");
  598. return 0;
  599. }
  600. static void __exit piix_exit(void)
  601. {
  602. pci_unregister_driver(&piix_pci_driver);
  603. }
  604. module_init(piix_init);
  605. module_exit(piix_exit);