18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci#include <linux/kernel.h>
38c2ecf20Sopenharmony_ci#include <linux/module.h>
48c2ecf20Sopenharmony_ci#include <linux/init.h>
58c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
68c2ecf20Sopenharmony_ci#include <linux/gfp.h>
78c2ecf20Sopenharmony_ci#include <scsi/scsi_host.h>
88c2ecf20Sopenharmony_ci#include <linux/ata.h>
98c2ecf20Sopenharmony_ci#include <linux/libata.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <asm/dma.h>
128c2ecf20Sopenharmony_ci#include <asm/ecard.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define DRV_NAME	"pata_icside"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define ICS_IDENT_OFFSET		0x2280
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define ICS_ARCIN_V5_INTRSTAT		0x0000
198c2ecf20Sopenharmony_ci#define ICS_ARCIN_V5_INTROFFSET		0x0004
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define ICS_ARCIN_V6_INTROFFSET_1	0x2200
228c2ecf20Sopenharmony_ci#define ICS_ARCIN_V6_INTRSTAT_1		0x2290
238c2ecf20Sopenharmony_ci#define ICS_ARCIN_V6_INTROFFSET_2	0x3200
248c2ecf20Sopenharmony_ci#define ICS_ARCIN_V6_INTRSTAT_2		0x3290
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct portinfo {
278c2ecf20Sopenharmony_ci	unsigned int dataoffset;
288c2ecf20Sopenharmony_ci	unsigned int ctrloffset;
298c2ecf20Sopenharmony_ci	unsigned int stepping;
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic const struct portinfo pata_icside_portinfo_v5 = {
338c2ecf20Sopenharmony_ci	.dataoffset	= 0x2800,
348c2ecf20Sopenharmony_ci	.ctrloffset	= 0x2b80,
358c2ecf20Sopenharmony_ci	.stepping	= 6,
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic const struct portinfo pata_icside_portinfo_v6_1 = {
398c2ecf20Sopenharmony_ci	.dataoffset	= 0x2000,
408c2ecf20Sopenharmony_ci	.ctrloffset	= 0x2380,
418c2ecf20Sopenharmony_ci	.stepping	= 6,
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic const struct portinfo pata_icside_portinfo_v6_2 = {
458c2ecf20Sopenharmony_ci	.dataoffset	= 0x3000,
468c2ecf20Sopenharmony_ci	.ctrloffset	= 0x3380,
478c2ecf20Sopenharmony_ci	.stepping	= 6,
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistruct pata_icside_state {
518c2ecf20Sopenharmony_ci	void __iomem *irq_port;
528c2ecf20Sopenharmony_ci	void __iomem *ioc_base;
538c2ecf20Sopenharmony_ci	unsigned int type;
548c2ecf20Sopenharmony_ci	unsigned int dma;
558c2ecf20Sopenharmony_ci	struct {
568c2ecf20Sopenharmony_ci		u8 port_sel;
578c2ecf20Sopenharmony_ci		u8 disabled;
588c2ecf20Sopenharmony_ci		unsigned int speed[ATA_MAX_DEVICES];
598c2ecf20Sopenharmony_ci	} port[2];
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistruct pata_icside_info {
638c2ecf20Sopenharmony_ci	struct pata_icside_state *state;
648c2ecf20Sopenharmony_ci	struct expansion_card	*ec;
658c2ecf20Sopenharmony_ci	void __iomem		*base;
668c2ecf20Sopenharmony_ci	void __iomem		*irqaddr;
678c2ecf20Sopenharmony_ci	unsigned int		irqmask;
688c2ecf20Sopenharmony_ci	const expansioncard_ops_t *irqops;
698c2ecf20Sopenharmony_ci	unsigned int		mwdma_mask;
708c2ecf20Sopenharmony_ci	unsigned int		nr_ports;
718c2ecf20Sopenharmony_ci	const struct portinfo	*port[2];
728c2ecf20Sopenharmony_ci	unsigned long		raw_base;
738c2ecf20Sopenharmony_ci	unsigned long		raw_ioc_base;
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define ICS_TYPE_A3IN	0
778c2ecf20Sopenharmony_ci#define ICS_TYPE_A3USER	1
788c2ecf20Sopenharmony_ci#define ICS_TYPE_V6	3
798c2ecf20Sopenharmony_ci#define ICS_TYPE_V5	15
808c2ecf20Sopenharmony_ci#define ICS_TYPE_NOTYPE	((unsigned int)-1)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/* ---------------- Version 5 PCB Support Functions --------------------- */
838c2ecf20Sopenharmony_ci/* Prototype: pata_icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
848c2ecf20Sopenharmony_ci * Purpose  : enable interrupts from card
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_cistatic void pata_icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct pata_icside_state *state = ec->irq_data;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	writeb(0, state->irq_port + ICS_ARCIN_V5_INTROFFSET);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/* Prototype: pata_icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
948c2ecf20Sopenharmony_ci * Purpose  : disable interrupts from card
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_cistatic void pata_icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	struct pata_icside_state *state = ec->irq_data;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	readb(state->irq_port + ICS_ARCIN_V5_INTROFFSET);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic const expansioncard_ops_t pata_icside_ops_arcin_v5 = {
1048c2ecf20Sopenharmony_ci	.irqenable	= pata_icside_irqenable_arcin_v5,
1058c2ecf20Sopenharmony_ci	.irqdisable	= pata_icside_irqdisable_arcin_v5,
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* ---------------- Version 6 PCB Support Functions --------------------- */
1108c2ecf20Sopenharmony_ci/* Prototype: pata_icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
1118c2ecf20Sopenharmony_ci * Purpose  : enable interrupts from card
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_cistatic void pata_icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct pata_icside_state *state = ec->irq_data;
1168c2ecf20Sopenharmony_ci	void __iomem *base = state->irq_port;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (!state->port[0].disabled)
1198c2ecf20Sopenharmony_ci		writeb(0, base + ICS_ARCIN_V6_INTROFFSET_1);
1208c2ecf20Sopenharmony_ci	if (!state->port[1].disabled)
1218c2ecf20Sopenharmony_ci		writeb(0, base + ICS_ARCIN_V6_INTROFFSET_2);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci/* Prototype: pata_icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
1258c2ecf20Sopenharmony_ci * Purpose  : disable interrupts from card
1268c2ecf20Sopenharmony_ci */
1278c2ecf20Sopenharmony_cistatic void pata_icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct pata_icside_state *state = ec->irq_data;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
1328c2ecf20Sopenharmony_ci	readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/* Prototype: pata_icside_irqprobe(struct expansion_card *ec)
1368c2ecf20Sopenharmony_ci * Purpose  : detect an active interrupt from card
1378c2ecf20Sopenharmony_ci */
1388c2ecf20Sopenharmony_cistatic int pata_icside_irqpending_arcin_v6(struct expansion_card *ec)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct pata_icside_state *state = ec->irq_data;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return readb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_1) & 1 ||
1438c2ecf20Sopenharmony_ci	       readb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_2) & 1;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic const expansioncard_ops_t pata_icside_ops_arcin_v6 = {
1478c2ecf20Sopenharmony_ci	.irqenable	= pata_icside_irqenable_arcin_v6,
1488c2ecf20Sopenharmony_ci	.irqdisable	= pata_icside_irqdisable_arcin_v6,
1498c2ecf20Sopenharmony_ci	.irqpending	= pata_icside_irqpending_arcin_v6,
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/*
1548c2ecf20Sopenharmony_ci * SG-DMA support.
1558c2ecf20Sopenharmony_ci *
1568c2ecf20Sopenharmony_ci * Similar to the BM-DMA, but we use the RiscPCs IOMD DMA controllers.
1578c2ecf20Sopenharmony_ci * There is only one DMA controller per card, which means that only
1588c2ecf20Sopenharmony_ci * one drive can be accessed at one time.  NOTE! We do not enforce that
1598c2ecf20Sopenharmony_ci * here, but we rely on the main IDE driver spotting that both
1608c2ecf20Sopenharmony_ci * interfaces use the same IRQ, which should guarantee this.
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/*
1648c2ecf20Sopenharmony_ci * Configure the IOMD to give the appropriate timings for the transfer
1658c2ecf20Sopenharmony_ci * mode being requested.  We take the advice of the ATA standards, and
1668c2ecf20Sopenharmony_ci * calculate the cycle time based on the transfer mode, and the EIDE
1678c2ecf20Sopenharmony_ci * MW DMA specs that the drive provides in the IDENTIFY command.
1688c2ecf20Sopenharmony_ci *
1698c2ecf20Sopenharmony_ci * We have the following IOMD DMA modes to choose from:
1708c2ecf20Sopenharmony_ci *
1718c2ecf20Sopenharmony_ci *	Type	Active		Recovery	Cycle
1728c2ecf20Sopenharmony_ci *	A	250 (250)	312 (550)	562 (800)
1738c2ecf20Sopenharmony_ci *	B	187 (200)	250 (550)	437 (750)
1748c2ecf20Sopenharmony_ci *	C	125 (125)	125 (375)	250 (500)
1758c2ecf20Sopenharmony_ci *	D	62  (50)	125 (375)	187 (425)
1768c2ecf20Sopenharmony_ci *
1778c2ecf20Sopenharmony_ci * (figures in brackets are actual measured timings on DIOR/DIOW)
1788c2ecf20Sopenharmony_ci *
1798c2ecf20Sopenharmony_ci * However, we also need to take care of the read/write active and
1808c2ecf20Sopenharmony_ci * recovery timings:
1818c2ecf20Sopenharmony_ci *
1828c2ecf20Sopenharmony_ci *			Read	Write
1838c2ecf20Sopenharmony_ci *  	Mode	Active	-- Recovery --	Cycle	IOMD type
1848c2ecf20Sopenharmony_ci *	MW0	215	50	215	480	A
1858c2ecf20Sopenharmony_ci *	MW1	80	50	50	150	C
1868c2ecf20Sopenharmony_ci *	MW2	70	25	25	120	C
1878c2ecf20Sopenharmony_ci */
1888c2ecf20Sopenharmony_cistatic void pata_icside_set_dmamode(struct ata_port *ap, struct ata_device *adev)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	struct pata_icside_state *state = ap->host->private_data;
1918c2ecf20Sopenharmony_ci	struct ata_timing t;
1928c2ecf20Sopenharmony_ci	unsigned int cycle;
1938c2ecf20Sopenharmony_ci	char iomd_type;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	/*
1968c2ecf20Sopenharmony_ci	 * DMA is based on a 16MHz clock
1978c2ecf20Sopenharmony_ci	 */
1988c2ecf20Sopenharmony_ci	if (ata_timing_compute(adev, adev->dma_mode, &t, 1000, 1))
1998c2ecf20Sopenharmony_ci		return;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	/*
2028c2ecf20Sopenharmony_ci	 * Choose the IOMD cycle timing which ensure that the interface
2038c2ecf20Sopenharmony_ci	 * satisfies the measured active, recovery and cycle times.
2048c2ecf20Sopenharmony_ci	 */
2058c2ecf20Sopenharmony_ci	if (t.active <= 50 && t.recover <= 375 && t.cycle <= 425)
2068c2ecf20Sopenharmony_ci		iomd_type = 'D', cycle = 187;
2078c2ecf20Sopenharmony_ci	else if (t.active <= 125 && t.recover <= 375 && t.cycle <= 500)
2088c2ecf20Sopenharmony_ci		iomd_type = 'C', cycle = 250;
2098c2ecf20Sopenharmony_ci	else if (t.active <= 200 && t.recover <= 550 && t.cycle <= 750)
2108c2ecf20Sopenharmony_ci		iomd_type = 'B', cycle = 437;
2118c2ecf20Sopenharmony_ci	else
2128c2ecf20Sopenharmony_ci		iomd_type = 'A', cycle = 562;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	ata_dev_info(adev, "timings: act %dns rec %dns cyc %dns (%c)\n",
2158c2ecf20Sopenharmony_ci		     t.active, t.recover, t.cycle, iomd_type);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	state->port[ap->port_no].speed[adev->devno] = cycle;
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic void pata_icside_bmdma_setup(struct ata_queued_cmd *qc)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	struct ata_port *ap = qc->ap;
2238c2ecf20Sopenharmony_ci	struct pata_icside_state *state = ap->host->private_data;
2248c2ecf20Sopenharmony_ci	unsigned int write = qc->tf.flags & ATA_TFLAG_WRITE;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/*
2278c2ecf20Sopenharmony_ci	 * We are simplex; BUG if we try to fiddle with DMA
2288c2ecf20Sopenharmony_ci	 * while it's active.
2298c2ecf20Sopenharmony_ci	 */
2308c2ecf20Sopenharmony_ci	BUG_ON(dma_channel_active(state->dma));
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/*
2338c2ecf20Sopenharmony_ci	 * Route the DMA signals to the correct interface
2348c2ecf20Sopenharmony_ci	 */
2358c2ecf20Sopenharmony_ci	writeb(state->port[ap->port_no].port_sel, state->ioc_base);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	set_dma_speed(state->dma, state->port[ap->port_no].speed[qc->dev->devno]);
2388c2ecf20Sopenharmony_ci	set_dma_sg(state->dma, qc->sg, qc->n_elem);
2398c2ecf20Sopenharmony_ci	set_dma_mode(state->dma, write ? DMA_MODE_WRITE : DMA_MODE_READ);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/* issue r/w command */
2428c2ecf20Sopenharmony_ci	ap->ops->sff_exec_command(ap, &qc->tf);
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic void pata_icside_bmdma_start(struct ata_queued_cmd *qc)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	struct ata_port *ap = qc->ap;
2488c2ecf20Sopenharmony_ci	struct pata_icside_state *state = ap->host->private_data;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	BUG_ON(dma_channel_active(state->dma));
2518c2ecf20Sopenharmony_ci	enable_dma(state->dma);
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic void pata_icside_bmdma_stop(struct ata_queued_cmd *qc)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	struct ata_port *ap = qc->ap;
2578c2ecf20Sopenharmony_ci	struct pata_icside_state *state = ap->host->private_data;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	disable_dma(state->dma);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/* see ata_bmdma_stop */
2628c2ecf20Sopenharmony_ci	ata_sff_dma_pause(ap);
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic u8 pata_icside_bmdma_status(struct ata_port *ap)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	struct pata_icside_state *state = ap->host->private_data;
2688c2ecf20Sopenharmony_ci	void __iomem *irq_port;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	irq_port = state->irq_port + (ap->port_no ? ICS_ARCIN_V6_INTRSTAT_2 :
2718c2ecf20Sopenharmony_ci						    ICS_ARCIN_V6_INTRSTAT_1);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	return readb(irq_port) & 1 ? ATA_DMA_INTR : 0;
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic int icside_dma_init(struct pata_icside_info *info)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct pata_icside_state *state = info->state;
2798c2ecf20Sopenharmony_ci	struct expansion_card *ec = info->ec;
2808c2ecf20Sopenharmony_ci	int i;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	for (i = 0; i < ATA_MAX_DEVICES; i++) {
2838c2ecf20Sopenharmony_ci		state->port[0].speed[i] = 480;
2848c2ecf20Sopenharmony_ci		state->port[1].speed[i] = 480;
2858c2ecf20Sopenharmony_ci	}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	if (ec->dma != NO_DMA && !request_dma(ec->dma, DRV_NAME)) {
2888c2ecf20Sopenharmony_ci		state->dma = ec->dma;
2898c2ecf20Sopenharmony_ci		info->mwdma_mask = ATA_MWDMA2;
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	return 0;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic struct scsi_host_template pata_icside_sht = {
2978c2ecf20Sopenharmony_ci	ATA_BASE_SHT(DRV_NAME),
2988c2ecf20Sopenharmony_ci	.sg_tablesize		= SG_MAX_SEGMENTS,
2998c2ecf20Sopenharmony_ci	.dma_boundary		= IOMD_DMA_BOUNDARY,
3008c2ecf20Sopenharmony_ci};
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic void pata_icside_postreset(struct ata_link *link, unsigned int *classes)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	struct ata_port *ap = link->ap;
3058c2ecf20Sopenharmony_ci	struct pata_icside_state *state = ap->host->private_data;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	if (classes[0] != ATA_DEV_NONE || classes[1] != ATA_DEV_NONE)
3088c2ecf20Sopenharmony_ci		return ata_sff_postreset(link, classes);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	state->port[ap->port_no].disabled = 1;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	if (state->type == ICS_TYPE_V6) {
3138c2ecf20Sopenharmony_ci		/*
3148c2ecf20Sopenharmony_ci		 * Disable interrupts from this port, otherwise we
3158c2ecf20Sopenharmony_ci		 * receive spurious interrupts from the floating
3168c2ecf20Sopenharmony_ci		 * interrupt line.
3178c2ecf20Sopenharmony_ci		 */
3188c2ecf20Sopenharmony_ci		void __iomem *irq_port = state->irq_port +
3198c2ecf20Sopenharmony_ci				(ap->port_no ? ICS_ARCIN_V6_INTROFFSET_2 : ICS_ARCIN_V6_INTROFFSET_1);
3208c2ecf20Sopenharmony_ci		readb(irq_port);
3218c2ecf20Sopenharmony_ci	}
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic struct ata_port_operations pata_icside_port_ops = {
3258c2ecf20Sopenharmony_ci	.inherits		= &ata_bmdma_port_ops,
3268c2ecf20Sopenharmony_ci	/* no need to build any PRD tables for DMA */
3278c2ecf20Sopenharmony_ci	.qc_prep		= ata_noop_qc_prep,
3288c2ecf20Sopenharmony_ci	.sff_data_xfer		= ata_sff_data_xfer32,
3298c2ecf20Sopenharmony_ci	.bmdma_setup		= pata_icside_bmdma_setup,
3308c2ecf20Sopenharmony_ci	.bmdma_start		= pata_icside_bmdma_start,
3318c2ecf20Sopenharmony_ci	.bmdma_stop		= pata_icside_bmdma_stop,
3328c2ecf20Sopenharmony_ci	.bmdma_status		= pata_icside_bmdma_status,
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	.cable_detect		= ata_cable_40wire,
3358c2ecf20Sopenharmony_ci	.set_dmamode		= pata_icside_set_dmamode,
3368c2ecf20Sopenharmony_ci	.postreset		= pata_icside_postreset,
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	.port_start		= ATA_OP_NULL,	/* don't need PRD table */
3398c2ecf20Sopenharmony_ci};
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic void pata_icside_setup_ioaddr(struct ata_port *ap, void __iomem *base,
3428c2ecf20Sopenharmony_ci				     struct pata_icside_info *info,
3438c2ecf20Sopenharmony_ci				     const struct portinfo *port)
3448c2ecf20Sopenharmony_ci{
3458c2ecf20Sopenharmony_ci	struct ata_ioports *ioaddr = &ap->ioaddr;
3468c2ecf20Sopenharmony_ci	void __iomem *cmd = base + port->dataoffset;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	ioaddr->cmd_addr	= cmd;
3498c2ecf20Sopenharmony_ci	ioaddr->data_addr	= cmd + (ATA_REG_DATA    << port->stepping);
3508c2ecf20Sopenharmony_ci	ioaddr->error_addr	= cmd + (ATA_REG_ERR     << port->stepping);
3518c2ecf20Sopenharmony_ci	ioaddr->feature_addr	= cmd + (ATA_REG_FEATURE << port->stepping);
3528c2ecf20Sopenharmony_ci	ioaddr->nsect_addr	= cmd + (ATA_REG_NSECT   << port->stepping);
3538c2ecf20Sopenharmony_ci	ioaddr->lbal_addr	= cmd + (ATA_REG_LBAL    << port->stepping);
3548c2ecf20Sopenharmony_ci	ioaddr->lbam_addr	= cmd + (ATA_REG_LBAM    << port->stepping);
3558c2ecf20Sopenharmony_ci	ioaddr->lbah_addr	= cmd + (ATA_REG_LBAH    << port->stepping);
3568c2ecf20Sopenharmony_ci	ioaddr->device_addr	= cmd + (ATA_REG_DEVICE  << port->stepping);
3578c2ecf20Sopenharmony_ci	ioaddr->status_addr	= cmd + (ATA_REG_STATUS  << port->stepping);
3588c2ecf20Sopenharmony_ci	ioaddr->command_addr	= cmd + (ATA_REG_CMD     << port->stepping);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	ioaddr->ctl_addr	= base + port->ctrloffset;
3618c2ecf20Sopenharmony_ci	ioaddr->altstatus_addr	= ioaddr->ctl_addr;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx",
3648c2ecf20Sopenharmony_ci		      info->raw_base + port->dataoffset,
3658c2ecf20Sopenharmony_ci		      info->raw_base + port->ctrloffset);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	if (info->raw_ioc_base)
3688c2ecf20Sopenharmony_ci		ata_port_desc(ap, "iocbase 0x%lx", info->raw_ioc_base);
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic int pata_icside_register_v5(struct pata_icside_info *info)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	struct pata_icside_state *state = info->state;
3748c2ecf20Sopenharmony_ci	void __iomem *base;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	base = ecardm_iomap(info->ec, ECARD_RES_MEMC, 0, 0);
3778c2ecf20Sopenharmony_ci	if (!base)
3788c2ecf20Sopenharmony_ci		return -ENOMEM;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	state->irq_port = base;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	info->base = base;
3838c2ecf20Sopenharmony_ci	info->irqaddr = base + ICS_ARCIN_V5_INTRSTAT;
3848c2ecf20Sopenharmony_ci	info->irqmask = 1;
3858c2ecf20Sopenharmony_ci	info->irqops = &pata_icside_ops_arcin_v5;
3868c2ecf20Sopenharmony_ci	info->nr_ports = 1;
3878c2ecf20Sopenharmony_ci	info->port[0] = &pata_icside_portinfo_v5;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	info->raw_base = ecard_resource_start(info->ec, ECARD_RES_MEMC);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	return 0;
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistatic int pata_icside_register_v6(struct pata_icside_info *info)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	struct pata_icside_state *state = info->state;
3978c2ecf20Sopenharmony_ci	struct expansion_card *ec = info->ec;
3988c2ecf20Sopenharmony_ci	void __iomem *ioc_base, *easi_base;
3998c2ecf20Sopenharmony_ci	unsigned int sel = 0;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	ioc_base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
4028c2ecf20Sopenharmony_ci	if (!ioc_base)
4038c2ecf20Sopenharmony_ci		return -ENOMEM;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	easi_base = ioc_base;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	if (ecard_resource_flags(ec, ECARD_RES_EASI)) {
4088c2ecf20Sopenharmony_ci		easi_base = ecardm_iomap(ec, ECARD_RES_EASI, 0, 0);
4098c2ecf20Sopenharmony_ci		if (!easi_base)
4108c2ecf20Sopenharmony_ci			return -ENOMEM;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci		/*
4138c2ecf20Sopenharmony_ci		 * Enable access to the EASI region.
4148c2ecf20Sopenharmony_ci		 */
4158c2ecf20Sopenharmony_ci		sel = 1 << 5;
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	writeb(sel, ioc_base);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	state->irq_port = easi_base;
4218c2ecf20Sopenharmony_ci	state->ioc_base = ioc_base;
4228c2ecf20Sopenharmony_ci	state->port[0].port_sel = sel;
4238c2ecf20Sopenharmony_ci	state->port[1].port_sel = sel | 1;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	info->base = easi_base;
4268c2ecf20Sopenharmony_ci	info->irqops = &pata_icside_ops_arcin_v6;
4278c2ecf20Sopenharmony_ci	info->nr_ports = 2;
4288c2ecf20Sopenharmony_ci	info->port[0] = &pata_icside_portinfo_v6_1;
4298c2ecf20Sopenharmony_ci	info->port[1] = &pata_icside_portinfo_v6_2;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	info->raw_base = ecard_resource_start(ec, ECARD_RES_EASI);
4328c2ecf20Sopenharmony_ci	info->raw_ioc_base = ecard_resource_start(ec, ECARD_RES_IOCFAST);
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	return icside_dma_init(info);
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_cistatic int pata_icside_add_ports(struct pata_icside_info *info)
4388c2ecf20Sopenharmony_ci{
4398c2ecf20Sopenharmony_ci	struct expansion_card *ec = info->ec;
4408c2ecf20Sopenharmony_ci	struct ata_host *host;
4418c2ecf20Sopenharmony_ci	int i;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	if (info->irqaddr) {
4448c2ecf20Sopenharmony_ci		ec->irqaddr = info->irqaddr;
4458c2ecf20Sopenharmony_ci		ec->irqmask = info->irqmask;
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci	if (info->irqops)
4488c2ecf20Sopenharmony_ci		ecard_setirq(ec, info->irqops, info->state);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	/*
4518c2ecf20Sopenharmony_ci	 * Be on the safe side - disable interrupts
4528c2ecf20Sopenharmony_ci	 */
4538c2ecf20Sopenharmony_ci	ec->ops->irqdisable(ec, ec->irq);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	host = ata_host_alloc(&ec->dev, info->nr_ports);
4568c2ecf20Sopenharmony_ci	if (!host)
4578c2ecf20Sopenharmony_ci		return -ENOMEM;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	host->private_data = info->state;
4608c2ecf20Sopenharmony_ci	host->flags = ATA_HOST_SIMPLEX;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	for (i = 0; i < info->nr_ports; i++) {
4638c2ecf20Sopenharmony_ci		struct ata_port *ap = host->ports[i];
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci		ap->pio_mask = ATA_PIO4;
4668c2ecf20Sopenharmony_ci		ap->mwdma_mask = info->mwdma_mask;
4678c2ecf20Sopenharmony_ci		ap->flags |= ATA_FLAG_SLAVE_POSS;
4688c2ecf20Sopenharmony_ci		ap->ops = &pata_icside_port_ops;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci		pata_icside_setup_ioaddr(ap, info->base, info, info->port[i]);
4718c2ecf20Sopenharmony_ci	}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	return ata_host_activate(host, ec->irq, ata_bmdma_interrupt, 0,
4748c2ecf20Sopenharmony_ci				 &pata_icside_sht);
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_cistatic int pata_icside_probe(struct expansion_card *ec,
4788c2ecf20Sopenharmony_ci			     const struct ecard_id *id)
4798c2ecf20Sopenharmony_ci{
4808c2ecf20Sopenharmony_ci	struct pata_icside_state *state;
4818c2ecf20Sopenharmony_ci	struct pata_icside_info info;
4828c2ecf20Sopenharmony_ci	void __iomem *idmem;
4838c2ecf20Sopenharmony_ci	int ret;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	ret = ecard_request_resources(ec);
4868c2ecf20Sopenharmony_ci	if (ret)
4878c2ecf20Sopenharmony_ci		goto out;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	state = devm_kzalloc(&ec->dev, sizeof(*state), GFP_KERNEL);
4908c2ecf20Sopenharmony_ci	if (!state) {
4918c2ecf20Sopenharmony_ci		ret = -ENOMEM;
4928c2ecf20Sopenharmony_ci		goto release;
4938c2ecf20Sopenharmony_ci	}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	state->type = ICS_TYPE_NOTYPE;
4968c2ecf20Sopenharmony_ci	state->dma = NO_DMA;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	idmem = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
4998c2ecf20Sopenharmony_ci	if (idmem) {
5008c2ecf20Sopenharmony_ci		unsigned int type;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci		type = readb(idmem + ICS_IDENT_OFFSET) & 1;
5038c2ecf20Sopenharmony_ci		type |= (readb(idmem + ICS_IDENT_OFFSET + 4) & 1) << 1;
5048c2ecf20Sopenharmony_ci		type |= (readb(idmem + ICS_IDENT_OFFSET + 8) & 1) << 2;
5058c2ecf20Sopenharmony_ci		type |= (readb(idmem + ICS_IDENT_OFFSET + 12) & 1) << 3;
5068c2ecf20Sopenharmony_ci		ecardm_iounmap(ec, idmem);
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci		state->type = type;
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	memset(&info, 0, sizeof(info));
5128c2ecf20Sopenharmony_ci	info.state = state;
5138c2ecf20Sopenharmony_ci	info.ec = ec;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	switch (state->type) {
5168c2ecf20Sopenharmony_ci	case ICS_TYPE_A3IN:
5178c2ecf20Sopenharmony_ci		dev_warn(&ec->dev, "A3IN unsupported\n");
5188c2ecf20Sopenharmony_ci		ret = -ENODEV;
5198c2ecf20Sopenharmony_ci		break;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	case ICS_TYPE_A3USER:
5228c2ecf20Sopenharmony_ci		dev_warn(&ec->dev, "A3USER unsupported\n");
5238c2ecf20Sopenharmony_ci		ret = -ENODEV;
5248c2ecf20Sopenharmony_ci		break;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	case ICS_TYPE_V5:
5278c2ecf20Sopenharmony_ci		ret = pata_icside_register_v5(&info);
5288c2ecf20Sopenharmony_ci		break;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	case ICS_TYPE_V6:
5318c2ecf20Sopenharmony_ci		ret = pata_icside_register_v6(&info);
5328c2ecf20Sopenharmony_ci		break;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	default:
5358c2ecf20Sopenharmony_ci		dev_warn(&ec->dev, "unknown interface type\n");
5368c2ecf20Sopenharmony_ci		ret = -ENODEV;
5378c2ecf20Sopenharmony_ci		break;
5388c2ecf20Sopenharmony_ci	}
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	if (ret == 0)
5418c2ecf20Sopenharmony_ci		ret = pata_icside_add_ports(&info);
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	if (ret == 0)
5448c2ecf20Sopenharmony_ci		goto out;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci release:
5478c2ecf20Sopenharmony_ci	ecard_release_resources(ec);
5488c2ecf20Sopenharmony_ci out:
5498c2ecf20Sopenharmony_ci	return ret;
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_cistatic void pata_icside_shutdown(struct expansion_card *ec)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	struct ata_host *host = ecard_get_drvdata(ec);
5558c2ecf20Sopenharmony_ci	unsigned long flags;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	/*
5588c2ecf20Sopenharmony_ci	 * Disable interrupts from this card.  We need to do
5598c2ecf20Sopenharmony_ci	 * this before disabling EASI since we may be accessing
5608c2ecf20Sopenharmony_ci	 * this register via that region.
5618c2ecf20Sopenharmony_ci	 */
5628c2ecf20Sopenharmony_ci	local_irq_save(flags);
5638c2ecf20Sopenharmony_ci	ec->ops->irqdisable(ec, ec->irq);
5648c2ecf20Sopenharmony_ci	local_irq_restore(flags);
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	/*
5678c2ecf20Sopenharmony_ci	 * Reset the ROM pointer so that we can read the ROM
5688c2ecf20Sopenharmony_ci	 * after a soft reboot.  This also disables access to
5698c2ecf20Sopenharmony_ci	 * the IDE taskfile via the EASI region.
5708c2ecf20Sopenharmony_ci	 */
5718c2ecf20Sopenharmony_ci	if (host) {
5728c2ecf20Sopenharmony_ci		struct pata_icside_state *state = host->private_data;
5738c2ecf20Sopenharmony_ci		if (state->ioc_base)
5748c2ecf20Sopenharmony_ci			writeb(0, state->ioc_base);
5758c2ecf20Sopenharmony_ci	}
5768c2ecf20Sopenharmony_ci}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cistatic void pata_icside_remove(struct expansion_card *ec)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	struct ata_host *host = ecard_get_drvdata(ec);
5818c2ecf20Sopenharmony_ci	struct pata_icside_state *state = host->private_data;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	ata_host_detach(host);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	pata_icside_shutdown(ec);
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	/*
5888c2ecf20Sopenharmony_ci	 * don't NULL out the drvdata - devres/libata wants it
5898c2ecf20Sopenharmony_ci	 * to free the ata_host structure.
5908c2ecf20Sopenharmony_ci	 */
5918c2ecf20Sopenharmony_ci	if (state->dma != NO_DMA)
5928c2ecf20Sopenharmony_ci		free_dma(state->dma);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	ecard_release_resources(ec);
5958c2ecf20Sopenharmony_ci}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_cistatic const struct ecard_id pata_icside_ids[] = {
5988c2ecf20Sopenharmony_ci	{ MANU_ICS,  PROD_ICS_IDE  },
5998c2ecf20Sopenharmony_ci	{ MANU_ICS2, PROD_ICS2_IDE },
6008c2ecf20Sopenharmony_ci	{ 0xffff, 0xffff }
6018c2ecf20Sopenharmony_ci};
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_cistatic struct ecard_driver pata_icside_driver = {
6048c2ecf20Sopenharmony_ci	.probe		= pata_icside_probe,
6058c2ecf20Sopenharmony_ci	.remove 	= pata_icside_remove,
6068c2ecf20Sopenharmony_ci	.shutdown	= pata_icside_shutdown,
6078c2ecf20Sopenharmony_ci	.id_table	= pata_icside_ids,
6088c2ecf20Sopenharmony_ci	.drv = {
6098c2ecf20Sopenharmony_ci		.name	= DRV_NAME,
6108c2ecf20Sopenharmony_ci	},
6118c2ecf20Sopenharmony_ci};
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_cistatic int __init pata_icside_init(void)
6148c2ecf20Sopenharmony_ci{
6158c2ecf20Sopenharmony_ci	return ecard_register_driver(&pata_icside_driver);
6168c2ecf20Sopenharmony_ci}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_cistatic void __exit pata_icside_exit(void)
6198c2ecf20Sopenharmony_ci{
6208c2ecf20Sopenharmony_ci	ecard_remove_driver(&pata_icside_driver);
6218c2ecf20Sopenharmony_ci}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ciMODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
6248c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
6258c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ICS PATA driver");
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_cimodule_init(pata_icside_init);
6288c2ecf20Sopenharmony_cimodule_exit(pata_icside_exit);
629