18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * BRIEF MODULE DESCRIPTION
48c2ecf20Sopenharmony_ci *      IT8172 IDE controller support
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2000 MontaVista Software Inc.
78c2ecf20Sopenharmony_ci * Copyright (C) 2008 Shane McDonald
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *  This program is free software; you can redistribute  it and/or modify it
108c2ecf20Sopenharmony_ci *  under  the terms of  the GNU General  Public License as published by the
118c2ecf20Sopenharmony_ci *  Free Software Foundation;  either version 2 of the  License, or (at your
128c2ecf20Sopenharmony_ci *  option) any later version.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
158c2ecf20Sopenharmony_ci *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
168c2ecf20Sopenharmony_ci *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
178c2ecf20Sopenharmony_ci *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
188c2ecf20Sopenharmony_ci *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
198c2ecf20Sopenharmony_ci *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
208c2ecf20Sopenharmony_ci *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
218c2ecf20Sopenharmony_ci *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
228c2ecf20Sopenharmony_ci *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
238c2ecf20Sopenharmony_ci *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci *  You should have received a copy of the  GNU General Public License along
268c2ecf20Sopenharmony_ci *  with this program; if not, write  to the Free Software Foundation, Inc.,
278c2ecf20Sopenharmony_ci *  675 Mass Ave, Cambridge, MA 02139, USA.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <linux/module.h>
318c2ecf20Sopenharmony_ci#include <linux/types.h>
328c2ecf20Sopenharmony_ci#include <linux/kernel.h>
338c2ecf20Sopenharmony_ci#include <linux/ioport.h>
348c2ecf20Sopenharmony_ci#include <linux/pci.h>
358c2ecf20Sopenharmony_ci#include <linux/ide.h>
368c2ecf20Sopenharmony_ci#include <linux/init.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define DRV_NAME "IT8172"
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic void it8172_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	struct pci_dev *dev	= to_pci_dev(hwif->dev);
438c2ecf20Sopenharmony_ci	u16 drive_enables;
448c2ecf20Sopenharmony_ci	u32 drive_timing;
458c2ecf20Sopenharmony_ci	const u8 pio = drive->pio_mode - XFER_PIO_0;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	/*
488c2ecf20Sopenharmony_ci	 * The highest value of DIOR/DIOW pulse width and recovery time
498c2ecf20Sopenharmony_ci	 * that can be set in the IT8172 is 8 PCI clock cycles.  As a result,
508c2ecf20Sopenharmony_ci	 * it cannot be configured for PIO mode 0.  This table sets these
518c2ecf20Sopenharmony_ci	 * parameters to the maximum supported by the IT8172.
528c2ecf20Sopenharmony_ci	 */
538c2ecf20Sopenharmony_ci	static const u8 timings[] = { 0x3f, 0x3c, 0x1b, 0x12, 0x0a };
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	pci_read_config_word(dev, 0x40, &drive_enables);
568c2ecf20Sopenharmony_ci	pci_read_config_dword(dev, 0x44, &drive_timing);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	/*
598c2ecf20Sopenharmony_ci	 * Enable port 0x44. The IT8172 spec is confused; it calls
608c2ecf20Sopenharmony_ci	 * this register the "Slave IDE Timing Register", but in fact,
618c2ecf20Sopenharmony_ci	 * it controls timing for both master and slave drives.
628c2ecf20Sopenharmony_ci	 */
638c2ecf20Sopenharmony_ci	drive_enables |= 0x4000;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	drive_enables &= drive->dn ? 0xc006 : 0xc060;
668c2ecf20Sopenharmony_ci	if (drive->media == ide_disk)
678c2ecf20Sopenharmony_ci		/* enable prefetch */
688c2ecf20Sopenharmony_ci		drive_enables |= 0x0004 << (drive->dn * 4);
698c2ecf20Sopenharmony_ci	if (ide_pio_need_iordy(drive, pio))
708c2ecf20Sopenharmony_ci		/* enable IORDY sample-point */
718c2ecf20Sopenharmony_ci		drive_enables |= 0x0002 << (drive->dn * 4);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	drive_timing &= drive->dn ? 0x00003f00 : 0x000fc000;
748c2ecf20Sopenharmony_ci	drive_timing |= timings[pio] << (drive->dn * 6 + 8);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	pci_write_config_word(dev, 0x40, drive_enables);
778c2ecf20Sopenharmony_ci	pci_write_config_dword(dev, 0x44, drive_timing);
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic void it8172_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	struct pci_dev *dev	= to_pci_dev(hwif->dev);
838c2ecf20Sopenharmony_ci	int a_speed		= 3 << (drive->dn * 4);
848c2ecf20Sopenharmony_ci	int u_flag		= 1 << drive->dn;
858c2ecf20Sopenharmony_ci	int u_speed		= 0;
868c2ecf20Sopenharmony_ci	u8 reg48, reg4a;
878c2ecf20Sopenharmony_ci	const u8 speed		= drive->dma_mode;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	pci_read_config_byte(dev, 0x48, &reg48);
908c2ecf20Sopenharmony_ci	pci_read_config_byte(dev, 0x4a, &reg4a);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (speed >= XFER_UDMA_0) {
938c2ecf20Sopenharmony_ci		u8 udma = speed - XFER_UDMA_0;
948c2ecf20Sopenharmony_ci		u_speed = udma << (drive->dn * 4);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, 0x48, reg48 | u_flag);
978c2ecf20Sopenharmony_ci		reg4a &= ~a_speed;
988c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, 0x4a, reg4a | u_speed);
998c2ecf20Sopenharmony_ci	} else {
1008c2ecf20Sopenharmony_ci		const u8 mwdma_to_pio[] = { 0, 3, 4 };
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
1038c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, 0x4a, reg4a & ~a_speed);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci		drive->pio_mode =
1068c2ecf20Sopenharmony_ci			mwdma_to_pio[speed - XFER_MW_DMA_0] + XFER_PIO_0;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci		it8172_set_pio_mode(hwif, drive);
1098c2ecf20Sopenharmony_ci	}
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic const struct ide_port_ops it8172_port_ops = {
1148c2ecf20Sopenharmony_ci	.set_pio_mode	= it8172_set_pio_mode,
1158c2ecf20Sopenharmony_ci	.set_dma_mode	= it8172_set_dma_mode,
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic const struct ide_port_info it8172_port_info = {
1198c2ecf20Sopenharmony_ci	.name		= DRV_NAME,
1208c2ecf20Sopenharmony_ci	.port_ops	= &it8172_port_ops,
1218c2ecf20Sopenharmony_ci	.enablebits	= { {0x41, 0x80, 0x80}, {0x00, 0x00, 0x00} },
1228c2ecf20Sopenharmony_ci	.host_flags	= IDE_HFLAG_SINGLE,
1238c2ecf20Sopenharmony_ci	.pio_mask	= ATA_PIO4 & ~ATA_PIO0,
1248c2ecf20Sopenharmony_ci	.mwdma_mask	= ATA_MWDMA2,
1258c2ecf20Sopenharmony_ci	.udma_mask	= ATA_UDMA2,
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic int it8172_init_one(struct pci_dev *dev, const struct pci_device_id *id)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1318c2ecf20Sopenharmony_ci		return -ENODEV; /* IT8172 is more than an IDE controller */
1328c2ecf20Sopenharmony_ci	return ide_pci_init_one(dev, &it8172_port_info, NULL);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic struct pci_device_id it8172_pci_tbl[] = {
1368c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8172), 0 },
1378c2ecf20Sopenharmony_ci	{ 0, },
1388c2ecf20Sopenharmony_ci};
1398c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, it8172_pci_tbl);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic struct pci_driver it8172_pci_driver = {
1428c2ecf20Sopenharmony_ci	.name		= "IT8172_IDE",
1438c2ecf20Sopenharmony_ci	.id_table	= it8172_pci_tbl,
1448c2ecf20Sopenharmony_ci	.probe		= it8172_init_one,
1458c2ecf20Sopenharmony_ci	.remove		= ide_pci_remove,
1468c2ecf20Sopenharmony_ci	.suspend	= ide_pci_suspend,
1478c2ecf20Sopenharmony_ci	.resume		= ide_pci_resume,
1488c2ecf20Sopenharmony_ci};
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic int __init it8172_ide_init(void)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	return ide_pci_register_driver(&it8172_pci_driver);
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic void __exit it8172_ide_exit(void)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	pci_unregister_driver(&it8172_pci_driver);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cimodule_init(it8172_ide_init);
1618c2ecf20Sopenharmony_cimodule_exit(it8172_ide_exit);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciMODULE_AUTHOR("Steve Longerbeam");
1648c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("PCI driver module for ITE 8172 IDE");
1658c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
166