xref: /openbmc/linux/drivers/ata/pata_ninja32.c (revision 11b7becca9425aab50807503c8102b2db9e5ecf0)
151dbd490SAlan Cox /*
251dbd490SAlan Cox  * pata_ninja32.c 	- Ninja32 PATA for new ATA layer
351dbd490SAlan Cox  *			  (C) 2007 Red Hat Inc
451dbd490SAlan Cox  *			  Alan Cox <alan@redhat.com>
551dbd490SAlan Cox  *
651dbd490SAlan Cox  * Note: The controller like many controllers has shared timings for
751dbd490SAlan Cox  * PIO and DMA. We thus flip to the DMA timings in dma_start and flip back
851dbd490SAlan Cox  * in the dma_stop function. Thus we actually don't need a set_dmamode
951dbd490SAlan Cox  * method as the PIO method is always called and will set the right PIO
1051dbd490SAlan Cox  * timing parameters.
1151dbd490SAlan Cox  *
1251dbd490SAlan Cox  * The Ninja32 Cardbus is not a generic SFF controller. Instead it is
1351dbd490SAlan Cox  * laid out as follows off BAR 0. This is based upon Mark Lord's delkin
1451dbd490SAlan Cox  * driver and the extensive analysis done by the BSD developers, notably
1551dbd490SAlan Cox  * ITOH Yasufumi.
1651dbd490SAlan Cox  *
1751dbd490SAlan Cox  *	Base + 0x00 IRQ Status
1851dbd490SAlan Cox  *	Base + 0x01 IRQ control
1951dbd490SAlan Cox  *	Base + 0x02 Chipset control
2051dbd490SAlan Cox  *	Base + 0x04 VDMA and reset control + wait bits
2151dbd490SAlan Cox  *	Base + 0x08 BMIMBA
2251dbd490SAlan Cox  *	Base + 0x0C DMA Length
2351dbd490SAlan Cox  *	Base + 0x10 Taskfile
2451dbd490SAlan Cox  *	Base + 0x18 BMDMA Status ?
2551dbd490SAlan Cox  *	Base + 0x1C
2651dbd490SAlan Cox  *	Base + 0x1D Bus master control
2751dbd490SAlan Cox  *		bit 0 = enable
2851dbd490SAlan Cox  *		bit 1 = 0 write/1 read
2951dbd490SAlan Cox  *		bit 2 = 1 sgtable
3051dbd490SAlan Cox  *		bit 3 = go
3151dbd490SAlan Cox  *		bit 4-6 wait bits
3251dbd490SAlan Cox  *		bit 7 = done
3351dbd490SAlan Cox  *	Base + 0x1E AltStatus
3451dbd490SAlan Cox  *	Base + 0x1F timing register
3551dbd490SAlan Cox  */
3651dbd490SAlan Cox 
3751dbd490SAlan Cox #include <linux/kernel.h>
3851dbd490SAlan Cox #include <linux/module.h>
3951dbd490SAlan Cox #include <linux/pci.h>
4051dbd490SAlan Cox #include <linux/init.h>
4151dbd490SAlan Cox #include <linux/blkdev.h>
4251dbd490SAlan Cox #include <linux/delay.h>
4351dbd490SAlan Cox #include <scsi/scsi_host.h>
4451dbd490SAlan Cox #include <linux/libata.h>
4551dbd490SAlan Cox 
4651dbd490SAlan Cox #define DRV_NAME "pata_ninja32"
4751dbd490SAlan Cox #define DRV_VERSION "0.0.1"
4851dbd490SAlan Cox 
4951dbd490SAlan Cox 
5051dbd490SAlan Cox /**
5151dbd490SAlan Cox  *	ninja32_set_piomode	-	set initial PIO mode data
5251dbd490SAlan Cox  *	@ap: ATA interface
5351dbd490SAlan Cox  *	@adev: ATA device
5451dbd490SAlan Cox  *
5551dbd490SAlan Cox  *	Called to do the PIO mode setup. Our timing registers are shared
5651dbd490SAlan Cox  *	but we want to set the PIO timing by default.
5751dbd490SAlan Cox  */
5851dbd490SAlan Cox 
5951dbd490SAlan Cox static void ninja32_set_piomode(struct ata_port *ap, struct ata_device *adev)
6051dbd490SAlan Cox {
6151dbd490SAlan Cox 	static u16 pio_timing[5] = {
6251dbd490SAlan Cox 		0xd6, 0x85, 0x44, 0x33, 0x13
6351dbd490SAlan Cox 	};
64*11b7beccSJeff Garzik 	iowrite8(pio_timing[adev->pio_mode - XFER_PIO_0],
65*11b7beccSJeff Garzik 		 ap->ioaddr.bmdma_addr + 0x1f);
6651dbd490SAlan Cox 	ap->private_data = adev;
6751dbd490SAlan Cox }
6851dbd490SAlan Cox 
6951dbd490SAlan Cox 
7051dbd490SAlan Cox static void ninja32_dev_select(struct ata_port *ap, unsigned int device)
7151dbd490SAlan Cox {
7251dbd490SAlan Cox 	struct ata_device *adev = &ap->link.device[device];
7351dbd490SAlan Cox 	if (ap->private_data != adev) {
7451dbd490SAlan Cox 		iowrite8(0xd6, ap->ioaddr.bmdma_addr + 0x1f);
7551dbd490SAlan Cox 		ata_std_dev_select(ap, device);
7651dbd490SAlan Cox 		ninja32_set_piomode(ap, adev);
7751dbd490SAlan Cox 	}
7851dbd490SAlan Cox }
7951dbd490SAlan Cox 
8051dbd490SAlan Cox static struct scsi_host_template ninja32_sht = {
8151dbd490SAlan Cox 	.module			= THIS_MODULE,
8251dbd490SAlan Cox 	.name			= DRV_NAME,
8351dbd490SAlan Cox 	.ioctl			= ata_scsi_ioctl,
8451dbd490SAlan Cox 	.queuecommand		= ata_scsi_queuecmd,
8551dbd490SAlan Cox 	.can_queue		= ATA_DEF_QUEUE,
8651dbd490SAlan Cox 	.this_id		= ATA_SHT_THIS_ID,
8751dbd490SAlan Cox 	.sg_tablesize		= LIBATA_MAX_PRD,
8851dbd490SAlan Cox 	.cmd_per_lun		= ATA_SHT_CMD_PER_LUN,
8951dbd490SAlan Cox 	.emulated		= ATA_SHT_EMULATED,
9051dbd490SAlan Cox 	.use_clustering		= ATA_SHT_USE_CLUSTERING,
9151dbd490SAlan Cox 	.proc_name		= DRV_NAME,
9251dbd490SAlan Cox 	.dma_boundary		= ATA_DMA_BOUNDARY,
9351dbd490SAlan Cox 	.slave_configure	= ata_scsi_slave_config,
9451dbd490SAlan Cox 	.slave_destroy		= ata_scsi_slave_destroy,
9551dbd490SAlan Cox 	.bios_param		= ata_std_bios_param,
9651dbd490SAlan Cox };
9751dbd490SAlan Cox 
9851dbd490SAlan Cox static struct ata_port_operations ninja32_port_ops = {
9951dbd490SAlan Cox 	.set_piomode	= ninja32_set_piomode,
10051dbd490SAlan Cox 	.mode_filter	= ata_pci_default_filter,
10151dbd490SAlan Cox 
10251dbd490SAlan Cox 	.tf_load	= ata_tf_load,
10351dbd490SAlan Cox 	.tf_read	= ata_tf_read,
10451dbd490SAlan Cox 	.check_status 	= ata_check_status,
10551dbd490SAlan Cox 	.exec_command	= ata_exec_command,
10651dbd490SAlan Cox 	.dev_select 	= ninja32_dev_select,
10751dbd490SAlan Cox 
10851dbd490SAlan Cox 	.freeze		= ata_bmdma_freeze,
10951dbd490SAlan Cox 	.thaw		= ata_bmdma_thaw,
11051dbd490SAlan Cox 	.error_handler	= ata_bmdma_error_handler,
11151dbd490SAlan Cox 	.post_internal_cmd = ata_bmdma_post_internal_cmd,
11251dbd490SAlan Cox 	.cable_detect	= ata_cable_40wire,
11351dbd490SAlan Cox 
11451dbd490SAlan Cox 	.bmdma_setup 	= ata_bmdma_setup,
11551dbd490SAlan Cox 	.bmdma_start 	= ata_bmdma_start,
11651dbd490SAlan Cox 	.bmdma_stop	= ata_bmdma_stop,
11751dbd490SAlan Cox 	.bmdma_status 	= ata_bmdma_status,
11851dbd490SAlan Cox 
11951dbd490SAlan Cox 	.qc_prep 	= ata_qc_prep,
12051dbd490SAlan Cox 	.qc_issue	= ata_qc_issue_prot,
12151dbd490SAlan Cox 
12251dbd490SAlan Cox 	.data_xfer	= ata_data_xfer,
12351dbd490SAlan Cox 
12451dbd490SAlan Cox 	.irq_handler	= ata_interrupt,
12551dbd490SAlan Cox 	.irq_clear	= ata_bmdma_irq_clear,
12651dbd490SAlan Cox 	.irq_on		= ata_irq_on,
12751dbd490SAlan Cox 
12851dbd490SAlan Cox 	.port_start	= ata_sff_port_start,
12951dbd490SAlan Cox };
13051dbd490SAlan Cox 
13151dbd490SAlan Cox static int ninja32_init_one(struct pci_dev *dev, const struct pci_device_id *id)
13251dbd490SAlan Cox {
13351dbd490SAlan Cox 	struct ata_host *host;
13451dbd490SAlan Cox 	struct ata_port *ap;
13551dbd490SAlan Cox 	void __iomem *base;
13651dbd490SAlan Cox 	int rc;
13751dbd490SAlan Cox 
13851dbd490SAlan Cox 	host = ata_host_alloc(&dev->dev, 1);
13951dbd490SAlan Cox 	if (!host)
14051dbd490SAlan Cox 		return -ENOMEM;
14151dbd490SAlan Cox 	ap = host->ports[0];
14251dbd490SAlan Cox 
14351dbd490SAlan Cox 	/* Set up the PCI device */
14451dbd490SAlan Cox 	rc = pcim_enable_device(dev);
14551dbd490SAlan Cox 	if (rc)
14651dbd490SAlan Cox 		return rc;
14751dbd490SAlan Cox 	rc = pcim_iomap_regions(dev, 1 << 0, DRV_NAME);
14851dbd490SAlan Cox 	if (rc == -EBUSY)
14951dbd490SAlan Cox 		pcim_pin_device(dev);
15051dbd490SAlan Cox 	if (rc)
15151dbd490SAlan Cox 		return rc;
15251dbd490SAlan Cox 
15351dbd490SAlan Cox 	host->iomap = pcim_iomap_table(dev);
15451dbd490SAlan Cox 	rc = pci_set_dma_mask(dev, ATA_DMA_MASK);
15551dbd490SAlan Cox 	if (rc)
15651dbd490SAlan Cox 		return rc;
15751dbd490SAlan Cox 	rc = pci_set_consistent_dma_mask(dev, ATA_DMA_MASK);
15851dbd490SAlan Cox 	if (rc)
15951dbd490SAlan Cox 		return rc;
16051dbd490SAlan Cox 	pci_set_master(dev);
16151dbd490SAlan Cox 
16251dbd490SAlan Cox 	/* Set up the register mappings */
16351dbd490SAlan Cox 	base = host->iomap[0];
16451dbd490SAlan Cox 	if (!base)
16551dbd490SAlan Cox 		return -ENOMEM;
16651dbd490SAlan Cox 	ap->ops = &ninja32_port_ops;
16751dbd490SAlan Cox 	ap->pio_mask = 0x1F;
16851dbd490SAlan Cox 	ap->flags |= ATA_FLAG_SLAVE_POSS;
16951dbd490SAlan Cox 
17051dbd490SAlan Cox 	ap->ioaddr.cmd_addr = base + 0x10;
17151dbd490SAlan Cox 	ap->ioaddr.ctl_addr = base + 0x1E;
17251dbd490SAlan Cox 	ap->ioaddr.altstatus_addr = base + 0x1E;
17351dbd490SAlan Cox 	ap->ioaddr.bmdma_addr = base;
17451dbd490SAlan Cox 	ata_std_ports(&ap->ioaddr);
17551dbd490SAlan Cox 
17651dbd490SAlan Cox 	iowrite8(0x05, base + 0x01);	/* Enable interrupt lines */
17751dbd490SAlan Cox 	iowrite8(0xB3, base + 0x02);	/* Burst, ?? setup */
17851dbd490SAlan Cox 	iowrite8(0x00, base + 0x04);	/* WAIT0 ? */
17951dbd490SAlan Cox 	/* FIXME: Should we disable them at remove ? */
180*11b7beccSJeff Garzik 	return ata_host_activate(host, dev->irq, ata_interrupt,
181*11b7beccSJeff Garzik 				 IRQF_SHARED, &ninja32_sht);
18251dbd490SAlan Cox }
18351dbd490SAlan Cox 
18451dbd490SAlan Cox static const struct pci_device_id ninja32[] = {
18551dbd490SAlan Cox 	{ 0x1145, 0xf021, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
18651dbd490SAlan Cox 	{ 0x1145, 0xf024, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
18751dbd490SAlan Cox 	{ },
18851dbd490SAlan Cox };
18951dbd490SAlan Cox 
19051dbd490SAlan Cox static struct pci_driver ninja32_pci_driver = {
19151dbd490SAlan Cox 	.name 		= DRV_NAME,
19251dbd490SAlan Cox 	.id_table	= ninja32,
19351dbd490SAlan Cox 	.probe 		= ninja32_init_one,
19451dbd490SAlan Cox 	.remove		= ata_pci_remove_one
19551dbd490SAlan Cox };
19651dbd490SAlan Cox 
19751dbd490SAlan Cox static int __init ninja32_init(void)
19851dbd490SAlan Cox {
19951dbd490SAlan Cox 	return pci_register_driver(&ninja32_pci_driver);
20051dbd490SAlan Cox }
20151dbd490SAlan Cox 
20251dbd490SAlan Cox static void __exit ninja32_exit(void)
20351dbd490SAlan Cox {
20451dbd490SAlan Cox 	pci_unregister_driver(&ninja32_pci_driver);
20551dbd490SAlan Cox }
20651dbd490SAlan Cox 
20751dbd490SAlan Cox MODULE_AUTHOR("Alan Cox");
20851dbd490SAlan Cox MODULE_DESCRIPTION("low-level driver for Ninja32 ATA");
20951dbd490SAlan Cox MODULE_LICENSE("GPL");
21051dbd490SAlan Cox MODULE_DEVICE_TABLE(pci, ninja32);
21151dbd490SAlan Cox MODULE_VERSION(DRV_VERSION);
21251dbd490SAlan Cox 
21351dbd490SAlan Cox module_init(ninja32_init);
21451dbd490SAlan Cox module_exit(ninja32_exit);
215