| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 1 | /* | 
 | 2 |  *    pata_oldpiix.c - Intel PATA/SATA controllers | 
 | 3 |  * | 
| Alan Cox | ab77163 | 2008-10-27 15:09:10 +0000 | [diff] [blame] | 4 |  *	(C) 2005 Red Hat | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 5 |  * | 
 | 6 |  *    Some parts based on ata_piix.c by Jeff Garzik and others. | 
 | 7 |  * | 
 | 8 |  *    Early PIIX differs significantly from the later PIIX as it lacks | 
 | 9 |  *    SITRE and the slave timing registers. This means that you have to | 
 | 10 |  *    set timing per channel, or be clever. Libata tells us whenever it | 
 | 11 |  *    does drive selection and we use this to reload the timings. | 
 | 12 |  * | 
 | 13 |  *    Because of these behaviour differences PIIX gets its own driver module. | 
 | 14 |  */ | 
 | 15 |  | 
 | 16 | #include <linux/kernel.h> | 
 | 17 | #include <linux/module.h> | 
 | 18 | #include <linux/pci.h> | 
 | 19 | #include <linux/init.h> | 
 | 20 | #include <linux/blkdev.h> | 
 | 21 | #include <linux/delay.h> | 
 | 22 | #include <linux/device.h> | 
 | 23 | #include <scsi/scsi_host.h> | 
 | 24 | #include <linux/libata.h> | 
 | 25 | #include <linux/ata.h> | 
 | 26 |  | 
 | 27 | #define DRV_NAME	"pata_oldpiix" | 
| Jeff Garzik | a0fcdc0 | 2007-03-09 07:24:15 -0500 | [diff] [blame] | 28 | #define DRV_VERSION	"0.5.5" | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 29 |  | 
 | 30 | /** | 
 | 31 |  *	oldpiix_pre_reset		-	probe begin | 
| Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 32 |  *	@link: ATA link | 
| Tejun Heo | d4b2bab | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 33 |  *	@deadline: deadline jiffies for the operation | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 34 |  * | 
 | 35 |  *	Set up cable type and use generic probe init | 
 | 36 |  */ | 
 | 37 |  | 
| Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 38 | static int oldpiix_pre_reset(struct ata_link *link, unsigned long deadline) | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 39 | { | 
| Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 40 | 	struct ata_port *ap = link->ap; | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 41 | 	struct pci_dev *pdev = to_pci_dev(ap->host->dev); | 
 | 42 | 	static const struct pci_bits oldpiix_enable_bits[] = { | 
 | 43 | 		{ 0x41U, 1U, 0x80UL, 0x80UL },	/* port 0 */ | 
 | 44 | 		{ 0x43U, 1U, 0x80UL, 0x80UL },	/* port 1 */ | 
 | 45 | 	}; | 
 | 46 |  | 
| Alan Cox | c961922 | 2006-09-26 17:53:38 +0100 | [diff] [blame] | 47 | 	if (!pci_test_config_bits(pdev, &oldpiix_enable_bits[ap->port_no])) | 
 | 48 | 		return -ENOENT; | 
| Tejun Heo | d4b2bab | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 49 |  | 
| Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 50 | 	return ata_sff_prereset(link, deadline); | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 51 | } | 
 | 52 |  | 
 | 53 | /** | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 54 |  *	oldpiix_set_piomode - Initialize host controller PATA PIO timings | 
 | 55 |  *	@ap: Port whose timings we are configuring | 
| Jeff Garzik | a0fcdc0 | 2007-03-09 07:24:15 -0500 | [diff] [blame] | 56 |  *	@adev: Device whose timings we are configuring | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 57 |  * | 
 | 58 |  *	Set PIO mode for device, in host controller PCI config space. | 
 | 59 |  * | 
 | 60 |  *	LOCKING: | 
 | 61 |  *	None (inherited from caller). | 
 | 62 |  */ | 
 | 63 |  | 
 | 64 | static void oldpiix_set_piomode (struct ata_port *ap, struct ata_device *adev) | 
 | 65 | { | 
 | 66 | 	unsigned int pio	= adev->pio_mode - XFER_PIO_0; | 
 | 67 | 	struct pci_dev *dev	= to_pci_dev(ap->host->dev); | 
 | 68 | 	unsigned int idetm_port= ap->port_no ? 0x42 : 0x40; | 
 | 69 | 	u16 idetm_data; | 
 | 70 | 	int control = 0; | 
 | 71 |  | 
 | 72 | 	/* | 
 | 73 | 	 *	See Intel Document 298600-004 for the timing programing rules | 
 | 74 | 	 *	for PIIX/ICH. Note that the early PIIX does not have the slave | 
 | 75 | 	 *	timing port at 0x44. | 
 | 76 | 	 */ | 
 | 77 |  | 
 | 78 | 	static const	 /* ISP  RTC */ | 
 | 79 | 	u8 timings[][2]	= { { 0, 0 }, | 
 | 80 | 			    { 0, 0 }, | 
 | 81 | 			    { 1, 0 }, | 
 | 82 | 			    { 2, 1 }, | 
 | 83 | 			    { 2, 3 }, }; | 
 | 84 |  | 
| Sergei Shtylyov | 409ba47 | 2007-02-05 19:45:38 +0300 | [diff] [blame] | 85 | 	if (pio > 1) | 
 | 86 | 		control |= 1;	/* TIME */ | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 87 | 	if (ata_pio_need_iordy(adev)) | 
| Sergei Shtylyov | 409ba47 | 2007-02-05 19:45:38 +0300 | [diff] [blame] | 88 | 		control |= 2;	/* IE */ | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 89 |  | 
| Sergei Shtylyov | 409ba47 | 2007-02-05 19:45:38 +0300 | [diff] [blame] | 90 | 	/* Intel specifies that the prefetch/posting is for disk only */ | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 91 | 	if (adev->class == ATA_DEV_ATA) | 
| Sergei Shtylyov | 409ba47 | 2007-02-05 19:45:38 +0300 | [diff] [blame] | 92 | 		control |= 4;	/* PPE */ | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 93 |  | 
 | 94 | 	pci_read_config_word(dev, idetm_port, &idetm_data); | 
 | 95 |  | 
| Sergei Shtylyov | 409ba47 | 2007-02-05 19:45:38 +0300 | [diff] [blame] | 96 | 	/* | 
 | 97 | 	 * Set PPE, IE and TIME as appropriate. | 
 | 98 | 	 * Clear the other drive's timing bits. | 
 | 99 | 	 */ | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 100 | 	if (adev->devno == 0) { | 
 | 101 | 		idetm_data &= 0xCCE0; | 
 | 102 | 		idetm_data |= control; | 
 | 103 | 	} else { | 
 | 104 | 		idetm_data &= 0xCC0E; | 
 | 105 | 		idetm_data |= (control << 4); | 
 | 106 | 	} | 
 | 107 | 	idetm_data |= (timings[pio][0] << 12) | | 
 | 108 | 			(timings[pio][1] << 8); | 
 | 109 | 	pci_write_config_word(dev, idetm_port, idetm_data); | 
 | 110 |  | 
 | 111 | 	/* Track which port is configured */ | 
 | 112 | 	ap->private_data = adev; | 
 | 113 | } | 
 | 114 |  | 
 | 115 | /** | 
 | 116 |  *	oldpiix_set_dmamode - Initialize host controller PATA DMA timings | 
 | 117 |  *	@ap: Port whose timings we are configuring | 
 | 118 |  *	@adev: Device to program | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 119 |  * | 
 | 120 |  *	Set MWDMA mode for device, in host controller PCI config space. | 
 | 121 |  * | 
 | 122 |  *	LOCKING: | 
 | 123 |  *	None (inherited from caller). | 
 | 124 |  */ | 
 | 125 |  | 
 | 126 | static void oldpiix_set_dmamode (struct ata_port *ap, struct ata_device *adev) | 
 | 127 | { | 
 | 128 | 	struct pci_dev *dev	= to_pci_dev(ap->host->dev); | 
 | 129 | 	u8 idetm_port		= ap->port_no ? 0x42 : 0x40; | 
 | 130 | 	u16 idetm_data; | 
 | 131 |  | 
 | 132 | 	static const	 /* ISP  RTC */ | 
 | 133 | 	u8 timings[][2]	= { { 0, 0 }, | 
 | 134 | 			    { 0, 0 }, | 
 | 135 | 			    { 1, 0 }, | 
 | 136 | 			    { 2, 1 }, | 
 | 137 | 			    { 2, 3 }, }; | 
 | 138 |  | 
 | 139 | 	/* | 
 | 140 | 	 * MWDMA is driven by the PIO timings. We must also enable | 
 | 141 | 	 * IORDY unconditionally along with TIME1. PPE has already | 
 | 142 | 	 * been set when the PIO timing was set. | 
 | 143 | 	 */ | 
 | 144 |  | 
 | 145 | 	unsigned int mwdma	= adev->dma_mode - XFER_MW_DMA_0; | 
 | 146 | 	unsigned int control; | 
 | 147 | 	const unsigned int needed_pio[3] = { | 
 | 148 | 		XFER_PIO_0, XFER_PIO_3, XFER_PIO_4 | 
 | 149 | 	}; | 
 | 150 | 	int pio = needed_pio[mwdma] - XFER_PIO_0; | 
 | 151 |  | 
 | 152 | 	pci_read_config_word(dev, idetm_port, &idetm_data); | 
 | 153 |  | 
 | 154 | 	control = 3;	/* IORDY|TIME0 */ | 
 | 155 | 	/* Intel specifies that the PPE functionality is for disk only */ | 
 | 156 | 	if (adev->class == ATA_DEV_ATA) | 
 | 157 | 		control |= 4;	/* PPE enable */ | 
 | 158 |  | 
 | 159 | 	/* If the drive MWDMA is faster than it can do PIO then | 
 | 160 | 	   we must force PIO into PIO0 */ | 
 | 161 |  | 
 | 162 | 	if (adev->pio_mode < needed_pio[mwdma]) | 
 | 163 | 		/* Enable DMA timing only */ | 
 | 164 | 		control |= 8;	/* PIO cycles in PIO0 */ | 
 | 165 |  | 
 | 166 | 	/* Mask out the relevant control and timing bits we will load. Also | 
 | 167 | 	   clear the other drive TIME register as a precaution */ | 
 | 168 | 	if (adev->devno == 0) { | 
 | 169 | 		idetm_data &= 0xCCE0; | 
 | 170 | 		idetm_data |= control; | 
 | 171 | 	} else { | 
 | 172 | 		idetm_data &= 0xCC0E; | 
 | 173 | 		idetm_data |= (control << 4); | 
 | 174 | 	} | 
 | 175 | 	idetm_data |= (timings[pio][0] << 12) | (timings[pio][1] << 8); | 
 | 176 | 	pci_write_config_word(dev, idetm_port, idetm_data); | 
 | 177 |  | 
 | 178 | 	/* Track which port is configured */ | 
 | 179 | 	ap->private_data = adev; | 
 | 180 | } | 
 | 181 |  | 
 | 182 | /** | 
| Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 183 |  *	oldpiix_qc_issue	-	command issue | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 184 |  *	@qc: command pending | 
 | 185 |  * | 
 | 186 |  *	Called when the libata layer is about to issue a command. We wrap | 
 | 187 |  *	this interface so that we can load the correct ATA timings if | 
| Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 188 |  *	necessary. Our logic also clears TIME0/TIME1 for the other device so | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 189 |  *	that, even if we get this wrong, cycles to the other device will | 
 | 190 |  *	be made PIO0. | 
 | 191 |  */ | 
 | 192 |  | 
| Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 193 | static unsigned int oldpiix_qc_issue(struct ata_queued_cmd *qc) | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 194 | { | 
 | 195 | 	struct ata_port *ap = qc->ap; | 
 | 196 | 	struct ata_device *adev = qc->dev; | 
 | 197 |  | 
 | 198 | 	if (adev != ap->private_data) { | 
| Alan | b7939b1 | 2007-02-20 17:47:37 +0000 | [diff] [blame] | 199 | 		oldpiix_set_piomode(ap, adev); | 
| Alan Cox | b15b3eb | 2008-08-01 09:18:34 +0100 | [diff] [blame] | 200 | 		if (ata_dma_enabled(adev)) | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 201 | 			oldpiix_set_dmamode(ap, adev); | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 202 | 	} | 
| Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 203 | 	return ata_sff_qc_issue(qc); | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 204 | } | 
 | 205 |  | 
 | 206 |  | 
 | 207 | static struct scsi_host_template oldpiix_sht = { | 
| Tejun Heo | 68d1d07 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 208 | 	ATA_BMDMA_SHT(DRV_NAME), | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 209 | }; | 
 | 210 |  | 
| Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 211 | static struct ata_port_operations oldpiix_pata_ops = { | 
 | 212 | 	.inherits		= &ata_bmdma_port_ops, | 
| Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 213 | 	.qc_issue		= oldpiix_qc_issue, | 
| Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 214 | 	.cable_detect		= ata_cable_40wire, | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 215 | 	.set_piomode		= oldpiix_set_piomode, | 
 | 216 | 	.set_dmamode		= oldpiix_set_dmamode, | 
| Tejun Heo | a1efdab | 2008-03-25 12:22:50 +0900 | [diff] [blame] | 217 | 	.prereset		= oldpiix_pre_reset, | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 218 | }; | 
 | 219 |  | 
 | 220 |  | 
 | 221 | /** | 
 | 222 |  *	oldpiix_init_one - Register PIIX ATA PCI device with kernel services | 
 | 223 |  *	@pdev: PCI device to register | 
 | 224 |  *	@ent: Entry in oldpiix_pci_tbl matching with @pdev | 
 | 225 |  * | 
 | 226 |  *	Called from kernel PCI layer.  We probe for combined mode (sigh), | 
 | 227 |  *	and then hand over control to libata, for it to do the rest. | 
 | 228 |  * | 
 | 229 |  *	LOCKING: | 
 | 230 |  *	Inherited from PCI layer (may sleep). | 
 | 231 |  * | 
 | 232 |  *	RETURNS: | 
 | 233 |  *	Zero on success, or -ERRNO value. | 
 | 234 |  */ | 
 | 235 |  | 
 | 236 | static int oldpiix_init_one (struct pci_dev *pdev, const struct pci_device_id *ent) | 
 | 237 | { | 
 | 238 | 	static int printed_version; | 
| Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 239 | 	static const struct ata_port_info info = { | 
| Jeff Garzik | 1d2808f | 2007-05-28 06:59:48 -0400 | [diff] [blame] | 240 | 		.flags		= ATA_FLAG_SLAVE_POSS, | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 241 | 		.pio_mask	= 0x1f,	/* pio0-4 */ | 
 | 242 | 		.mwdma_mask	= 0x07, /* mwdma1-2 */ | 
 | 243 | 		.port_ops	= &oldpiix_pata_ops, | 
 | 244 | 	}; | 
| Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 245 | 	const struct ata_port_info *ppi[] = { &info, NULL }; | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 246 |  | 
 | 247 | 	if (!printed_version++) | 
 | 248 | 		dev_printk(KERN_DEBUG, &pdev->dev, | 
 | 249 | 			   "version " DRV_VERSION "\n"); | 
 | 250 |  | 
| Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 251 | 	return ata_pci_sff_init_one(pdev, ppi, &oldpiix_sht, NULL); | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 252 | } | 
 | 253 |  | 
 | 254 | static const struct pci_device_id oldpiix_pci_tbl[] = { | 
| Jeff Garzik | 2d2744f | 2006-09-28 20:21:59 -0400 | [diff] [blame] | 255 | 	{ PCI_VDEVICE(INTEL, 0x1230), }, | 
 | 256 |  | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 257 | 	{ }	/* terminate list */ | 
 | 258 | }; | 
 | 259 |  | 
 | 260 | static struct pci_driver oldpiix_pci_driver = { | 
 | 261 | 	.name			= DRV_NAME, | 
 | 262 | 	.id_table		= oldpiix_pci_tbl, | 
 | 263 | 	.probe			= oldpiix_init_one, | 
 | 264 | 	.remove			= ata_pci_remove_one, | 
| Tejun Heo | 438ac6d | 2007-03-02 17:31:26 +0900 | [diff] [blame] | 265 | #ifdef CONFIG_PM | 
| Alan | 30ced0f | 2006-11-22 16:57:36 +0000 | [diff] [blame] | 266 | 	.suspend		= ata_pci_device_suspend, | 
 | 267 | 	.resume			= ata_pci_device_resume, | 
| Tejun Heo | 438ac6d | 2007-03-02 17:31:26 +0900 | [diff] [blame] | 268 | #endif | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 269 | }; | 
 | 270 |  | 
 | 271 | static int __init oldpiix_init(void) | 
 | 272 | { | 
 | 273 | 	return pci_register_driver(&oldpiix_pci_driver); | 
 | 274 | } | 
 | 275 |  | 
 | 276 | static void __exit oldpiix_exit(void) | 
 | 277 | { | 
 | 278 | 	pci_unregister_driver(&oldpiix_pci_driver); | 
 | 279 | } | 
 | 280 |  | 
| Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 281 | module_init(oldpiix_init); | 
 | 282 | module_exit(oldpiix_exit); | 
 | 283 |  | 
 | 284 | MODULE_AUTHOR("Alan Cox"); | 
 | 285 | MODULE_DESCRIPTION("SCSI low-level driver for early PIIX series controllers"); | 
 | 286 | MODULE_LICENSE("GPL"); | 
 | 287 | MODULE_DEVICE_TABLE(pci, oldpiix_pci_tbl); | 
 | 288 | MODULE_VERSION(DRV_VERSION); | 
 | 289 |  |