18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  Q40 I/O port IDE Driver
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *     (c) Richard Zidlicky
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  This file is subject to the terms and conditions of the GNU General Public
78c2ecf20Sopenharmony_ci *  License.  See the file COPYING in the main directory of this archive for
88c2ecf20Sopenharmony_ci *  more details.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci#include <linux/mm.h>
158c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
168c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
178c2ecf20Sopenharmony_ci#include <linux/ide.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <asm/ide.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci    /*
238c2ecf20Sopenharmony_ci     *  Bases of the IDE interfaces
248c2ecf20Sopenharmony_ci     */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define Q40IDE_NUM_HWIFS	2
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define PCIDE_BASE1	0x1f0
298c2ecf20Sopenharmony_ci#define PCIDE_BASE2	0x170
308c2ecf20Sopenharmony_ci#define PCIDE_BASE3	0x1e8
318c2ecf20Sopenharmony_ci#define PCIDE_BASE4	0x168
328c2ecf20Sopenharmony_ci#define PCIDE_BASE5	0x1e0
338c2ecf20Sopenharmony_ci#define PCIDE_BASE6	0x160
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic const unsigned long pcide_bases[Q40IDE_NUM_HWIFS] = {
368c2ecf20Sopenharmony_ci    PCIDE_BASE1, PCIDE_BASE2, /* PCIDE_BASE3, PCIDE_BASE4  , PCIDE_BASE5,
378c2ecf20Sopenharmony_ci    PCIDE_BASE6 */
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int q40ide_default_irq(unsigned long base)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci           switch (base) {
438c2ecf20Sopenharmony_ci	            case 0x1f0: return 14;
448c2ecf20Sopenharmony_ci		    case 0x170: return 15;
458c2ecf20Sopenharmony_ci		    case 0x1e8: return 11;
468c2ecf20Sopenharmony_ci		    default:
478c2ecf20Sopenharmony_ci			return 0;
488c2ecf20Sopenharmony_ci	   }
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/*
538c2ecf20Sopenharmony_ci * Addresses are pretranslated for Q40 ISA access.
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_cistatic void q40_ide_setup_ports(struct ide_hw *hw, unsigned long base, int irq)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	memset(hw, 0, sizeof(*hw));
588c2ecf20Sopenharmony_ci	/* BIG FAT WARNING:
598c2ecf20Sopenharmony_ci	   assumption: only DATA port is ever used in 16 bit mode */
608c2ecf20Sopenharmony_ci	hw->io_ports.data_addr = Q40_ISA_IO_W(base);
618c2ecf20Sopenharmony_ci	hw->io_ports.error_addr = Q40_ISA_IO_B(base + 1);
628c2ecf20Sopenharmony_ci	hw->io_ports.nsect_addr = Q40_ISA_IO_B(base + 2);
638c2ecf20Sopenharmony_ci	hw->io_ports.lbal_addr = Q40_ISA_IO_B(base + 3);
648c2ecf20Sopenharmony_ci	hw->io_ports.lbam_addr = Q40_ISA_IO_B(base + 4);
658c2ecf20Sopenharmony_ci	hw->io_ports.lbah_addr = Q40_ISA_IO_B(base + 5);
668c2ecf20Sopenharmony_ci	hw->io_ports.device_addr = Q40_ISA_IO_B(base + 6);
678c2ecf20Sopenharmony_ci	hw->io_ports.status_addr = Q40_ISA_IO_B(base + 7);
688c2ecf20Sopenharmony_ci	hw->io_ports.ctl_addr = Q40_ISA_IO_B(base + 0x206);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	hw->irq = irq;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic void q40ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd,
748c2ecf20Sopenharmony_ci			      void *buf, unsigned int len)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	unsigned long data_addr = drive->hwif->io_ports.data_addr;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (drive->media == ide_disk && cmd && (cmd->tf_flags & IDE_TFLAG_FS)) {
798c2ecf20Sopenharmony_ci		__ide_mm_insw(data_addr, buf, (len + 1) / 2);
808c2ecf20Sopenharmony_ci		return;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	raw_insw_swapw((u16 *)data_addr, buf, (len + 1) / 2);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic void q40ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd,
878c2ecf20Sopenharmony_ci			       void *buf, unsigned int len)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	unsigned long data_addr = drive->hwif->io_ports.data_addr;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (drive->media == ide_disk && cmd && (cmd->tf_flags & IDE_TFLAG_FS)) {
928c2ecf20Sopenharmony_ci		__ide_mm_outsw(data_addr, buf, (len + 1) / 2);
938c2ecf20Sopenharmony_ci		return;
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	raw_outsw_swapw((u16 *)data_addr, buf, (len + 1) / 2);
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/* Q40 has a byte-swapped IDE interface */
1008c2ecf20Sopenharmony_cistatic const struct ide_tp_ops q40ide_tp_ops = {
1018c2ecf20Sopenharmony_ci	.exec_command		= ide_exec_command,
1028c2ecf20Sopenharmony_ci	.read_status		= ide_read_status,
1038c2ecf20Sopenharmony_ci	.read_altstatus		= ide_read_altstatus,
1048c2ecf20Sopenharmony_ci	.write_devctl		= ide_write_devctl,
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	.dev_select		= ide_dev_select,
1078c2ecf20Sopenharmony_ci	.tf_load		= ide_tf_load,
1088c2ecf20Sopenharmony_ci	.tf_read		= ide_tf_read,
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	.input_data		= q40ide_input_data,
1118c2ecf20Sopenharmony_ci	.output_data		= q40ide_output_data,
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic const struct ide_port_info q40ide_port_info = {
1158c2ecf20Sopenharmony_ci	.tp_ops			= &q40ide_tp_ops,
1168c2ecf20Sopenharmony_ci	.host_flags		= IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
1178c2ecf20Sopenharmony_ci	.irq_flags		= IRQF_SHARED,
1188c2ecf20Sopenharmony_ci	.chipset		= ide_generic,
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/*
1228c2ecf20Sopenharmony_ci * the static array is needed to have the name reported in /proc/ioports,
1238c2ecf20Sopenharmony_ci * hwif->name unfortunately isn't available yet
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_cistatic const char *q40_ide_names[Q40IDE_NUM_HWIFS]={
1268c2ecf20Sopenharmony_ci	"ide0", "ide1"
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/*
1308c2ecf20Sopenharmony_ci *  Probe for Q40 IDE interfaces
1318c2ecf20Sopenharmony_ci */
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic int __init q40ide_init(void)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci    int i;
1368c2ecf20Sopenharmony_ci    struct ide_hw hw[Q40IDE_NUM_HWIFS], *hws[] = { NULL, NULL };
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci    if (!MACH_IS_Q40)
1398c2ecf20Sopenharmony_ci      return -ENODEV;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci    printk(KERN_INFO "ide: Q40 IDE controller\n");
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci    for (i = 0; i < Q40IDE_NUM_HWIFS; i++) {
1448c2ecf20Sopenharmony_ci	const char *name = q40_ide_names[i];
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	if (!request_region(pcide_bases[i], 8, name)) {
1478c2ecf20Sopenharmony_ci		printk("could not reserve ports %lx-%lx for %s\n",
1488c2ecf20Sopenharmony_ci		       pcide_bases[i],pcide_bases[i]+8,name);
1498c2ecf20Sopenharmony_ci		continue;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci	if (!request_region(pcide_bases[i]+0x206, 1, name)) {
1528c2ecf20Sopenharmony_ci		printk("could not reserve port %lx for %s\n",
1538c2ecf20Sopenharmony_ci		       pcide_bases[i]+0x206,name);
1548c2ecf20Sopenharmony_ci		release_region(pcide_bases[i], 8);
1558c2ecf20Sopenharmony_ci		continue;
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci	q40_ide_setup_ports(&hw[i], pcide_bases[i],
1588c2ecf20Sopenharmony_ci			q40ide_default_irq(pcide_bases[i]));
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	hws[i] = &hw[i];
1618c2ecf20Sopenharmony_ci    }
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci    return ide_host_add(&q40ide_port_info, hws, Q40IDE_NUM_HWIFS, NULL);
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cimodule_init(q40ide_init);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
169