1// SPDX-License-Identifier: GPL-2.0
2#include <linux/types.h>
3#include <linux/mm.h>
4#include <linux/blkdev.h>
5#include <linux/interrupt.h>
6#include <linux/init.h>
7#include <linux/kernel.h>
8#include <linux/module.h>
9
10#include <asm/page.h>
11#include <asm/mvme147hw.h>
12#include <asm/irq.h>
13
14#include <scsi/scsi.h>
15#include <scsi/scsi_cmnd.h>
16#include <scsi/scsi_device.h>
17#include <scsi/scsi_eh.h>
18#include <scsi/scsi_host.h>
19#include <scsi/scsi_tcq.h>
20#include "wd33c93.h"
21#include "mvme147.h"
22
23static irqreturn_t mvme147_intr(int irq, void *data)
24{
25	struct Scsi_Host *instance = data;
26
27	if (irq == MVME147_IRQ_SCSI_PORT)
28		wd33c93_intr(instance);
29	else
30		m147_pcc->dma_intr = 0x89;	/* Ack and enable ints */
31	return IRQ_HANDLED;
32}
33
34static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
35{
36	struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
37	struct Scsi_Host *instance = cmd->device->host;
38	struct WD33C93_hostdata *hdata = shost_priv(instance);
39	unsigned char flags = 0x01;
40	unsigned long addr = virt_to_bus(scsi_pointer->ptr);
41
42	/* setup dma direction */
43	if (!dir_in)
44		flags |= 0x04;
45
46	/* remember direction */
47	hdata->dma_dir = dir_in;
48
49	if (dir_in) {
50		/* invalidate any cache */
51		cache_clear(addr, scsi_pointer->this_residual);
52	} else {
53		/* push any dirty cache */
54		cache_push(addr, scsi_pointer->this_residual);
55	}
56
57	/* start DMA */
58	m147_pcc->dma_bcr = scsi_pointer->this_residual | (1 << 24);
59	m147_pcc->dma_dadr = addr;
60	m147_pcc->dma_cntrl = flags;
61
62	/* return success */
63	return 0;
64}
65
66static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
67		     int status)
68{
69	m147_pcc->dma_cntrl = 0;
70}
71
72static const struct scsi_host_template mvme147_host_template = {
73	.module			= THIS_MODULE,
74	.proc_name		= "MVME147",
75	.name			= "MVME147 built-in SCSI",
76	.queuecommand		= wd33c93_queuecommand,
77	.eh_abort_handler	= wd33c93_abort,
78	.eh_host_reset_handler	= wd33c93_host_reset,
79	.show_info		= wd33c93_show_info,
80	.write_info		= wd33c93_write_info,
81	.can_queue		= CAN_QUEUE,
82	.this_id		= 7,
83	.sg_tablesize		= SG_ALL,
84	.cmd_per_lun		= CMD_PER_LUN,
85	.cmd_size		= sizeof(struct scsi_pointer),
86};
87
88static struct Scsi_Host *mvme147_shost;
89
90static int __init mvme147_init(void)
91{
92	wd33c93_regs regs;
93	struct WD33C93_hostdata *hdata;
94	int error = -ENOMEM;
95
96	if (!MACH_IS_MVME147)
97		return 0;
98
99	mvme147_shost = scsi_host_alloc(&mvme147_host_template,
100			sizeof(struct WD33C93_hostdata));
101	if (!mvme147_shost)
102		goto err_out;
103	mvme147_shost->base = 0xfffe4000;
104	mvme147_shost->irq = MVME147_IRQ_SCSI_PORT;
105
106	regs.SASR = (volatile unsigned char *)0xfffe4000;
107	regs.SCMD = (volatile unsigned char *)0xfffe4001;
108
109	hdata = shost_priv(mvme147_shost);
110	hdata->no_sync = 0xff;
111	hdata->fast = 0;
112	hdata->dma_mode = CTRL_DMA;
113
114	wd33c93_init(mvme147_shost, regs, dma_setup, dma_stop, WD33C93_FS_8_10);
115
116	error = request_irq(MVME147_IRQ_SCSI_PORT, mvme147_intr, 0,
117			"MVME147 SCSI PORT", mvme147_shost);
118	if (error)
119		goto err_unregister;
120	error = request_irq(MVME147_IRQ_SCSI_DMA, mvme147_intr, 0,
121			"MVME147 SCSI DMA", mvme147_shost);
122	if (error)
123		goto err_free_irq;
124#if 0	/* Disabled; causes problems booting */
125	m147_pcc->scsi_interrupt = 0x10;	/* Assert SCSI bus reset */
126	udelay(100);
127	m147_pcc->scsi_interrupt = 0x00;	/* Negate SCSI bus reset */
128	udelay(2000);
129	m147_pcc->scsi_interrupt = 0x40;	/* Clear bus reset interrupt */
130#endif
131	m147_pcc->scsi_interrupt = 0x09;	/* Enable interrupt */
132
133	m147_pcc->dma_cntrl = 0x00;	/* ensure DMA is stopped */
134	m147_pcc->dma_intr = 0x89;	/* Ack and enable ints */
135
136	error = scsi_add_host(mvme147_shost, NULL);
137	if (error)
138		goto err_free_irq;
139	scsi_scan_host(mvme147_shost);
140	return 0;
141
142err_free_irq:
143	free_irq(MVME147_IRQ_SCSI_PORT, mvme147_shost);
144err_unregister:
145	scsi_host_put(mvme147_shost);
146err_out:
147	return error;
148}
149
150static void __exit mvme147_exit(void)
151{
152	scsi_remove_host(mvme147_shost);
153
154	/* XXX Make sure DMA is stopped! */
155	free_irq(MVME147_IRQ_SCSI_PORT, mvme147_shost);
156	free_irq(MVME147_IRQ_SCSI_DMA, mvme147_shost);
157
158	scsi_host_put(mvme147_shost);
159}
160
161module_init(mvme147_init);
162module_exit(mvme147_exit);
163