sgiwd93.c 8.12 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
6
 * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
Linus Torvalds's avatar
Linus Torvalds committed
7 8
 * Copyright (C) 1999 Andrew R. Baker (andrewb@uab.edu)
 * Copyright (C) 2001 Florian Lohoff (flo@rfc822.org)
9
 * Copyright (C) 2003, 07 Ralf Baechle (ralf@linux-mips.org)
Linus Torvalds's avatar
Linus Torvalds committed
10 11 12
 * 
 * (In all truth, Jed Schimmel wrote all this code.)
 */
13 14 15

#undef DEBUG

Linus Torvalds's avatar
Linus Torvalds committed
16 17
#include <linux/delay.h>
#include <linux/dma-mapping.h>
18 19 20 21 22 23 24
#include <linux/gfp.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/platform_device.h>
Linus Torvalds's avatar
Linus Torvalds committed
25 26 27 28
#include <linux/spinlock.h>

#include <asm/sgi/hpc3.h>
#include <asm/sgi/ip22.h>
29
#include <asm/sgi/wd.h>
Linus Torvalds's avatar
Linus Torvalds committed
30 31 32 33 34 35

#include "scsi.h"
#include "wd33c93.h"

struct ip22_hostdata {
	struct WD33C93_hostdata wh;
36 37 38
	dma_addr_t dma;
	void *cpu;
	struct device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
39 40
};

41 42
#define host_to_hostdata(host) ((struct ip22_hostdata *)((host)->hostdata))

Linus Torvalds's avatar
Linus Torvalds committed
43 44 45 46 47
struct hpc_chunk {
	struct hpc_dma_desc desc;
	u32 _padding;	/* align to quadword boundary */
};

48 49 50 51 52
/* space for hpc dma descriptors */
#define HPC_DMA_SIZE   PAGE_SIZE

#define DMA_DIR(d)   ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)

53
static irqreturn_t sgiwd93_intr(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
54
{
55
	struct Scsi_Host * host = dev_id;
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58 59 60 61 62 63 64 65
	unsigned long flags;

	spin_lock_irqsave(host->host_lock, flags);
	wd33c93_intr(host);
	spin_unlock_irqrestore(host->host_lock, flags);

	return IRQ_HANDLED;
}

static inline
66
void fill_hpc_entries(struct ip22_hostdata *hd, struct scsi_cmnd *cmd, int din)
Linus Torvalds's avatar
Linus Torvalds committed
67 68 69 70 71
{
	unsigned long len = cmd->SCp.this_residual;
	void *addr = cmd->SCp.ptr;
	dma_addr_t physaddr;
	unsigned long count;
72
	struct hpc_chunk *hcp;
Linus Torvalds's avatar
Linus Torvalds committed
73

74
	physaddr = dma_map_single(hd->dev, addr, len, DMA_DIR(din));
Linus Torvalds's avatar
Linus Torvalds committed
75
	cmd->SCp.dma_handle = physaddr;
76
	hcp = hd->cpu;
Linus Torvalds's avatar
Linus Torvalds committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

	while (len) {
		/*
		 * even cntinfo could be up to 16383, without
		 * magic only 8192 works correctly
		 */
		count = len > 8192 ? 8192 : len;
		hcp->desc.pbuf = physaddr;
		hcp->desc.cntinfo = count;
		hcp++;
		len -= count;
		physaddr += count;
	}

	/*
	 * To make sure, if we trip an HPC bug, that we transfer every single
	 * byte, we tag on an extra zero length dma descriptor at the end of
	 * the chain.
	 */
	hcp->desc.pbuf = 0;
	hcp->desc.cntinfo = HPCDMA_EOX;
98 99 100
	dma_cache_sync(hd->dev, hd->cpu,
		       (unsigned long)(hcp + 1) - (unsigned long)hd->cpu,
		       DMA_TO_DEVICE);
Linus Torvalds's avatar
Linus Torvalds committed
101 102
}

103
static int dma_setup(struct scsi_cmnd *cmd, int datainp)
Linus Torvalds's avatar
Linus Torvalds committed
104
{
105
	struct ip22_hostdata *hdata = host_to_hostdata(cmd->device->host);
Linus Torvalds's avatar
Linus Torvalds committed
106 107 108
	struct hpc3_scsiregs *hregs =
		(struct hpc3_scsiregs *) cmd->device->host->base;

109
	pr_debug("dma_setup: datainp<%d> hcp<%p> ", datainp, hdata->cpu);
Linus Torvalds's avatar
Linus Torvalds committed
110 111 112 113 114 115 116 117 118 119 120 121

	hdata->wh.dma_dir = datainp;

	/*
	 * wd33c93 shouldn't pass us bogus dma_setups, but it does:-(  The
	 * other wd33c93 drivers deal with it the same way (which isn't that
	 * obvious).  IMHO a better fix would be, not to do these dma setups
	 * in the first place.
	 */
	if (cmd->SCp.ptr == NULL || cmd->SCp.this_residual == 0)
		return 1;

122
	fill_hpc_entries(hdata, cmd, datainp);
Linus Torvalds's avatar
Linus Torvalds committed
123

124
	pr_debug(" HPCGO\n");
Linus Torvalds's avatar
Linus Torvalds committed
125 126

	/* Start up the HPC. */
127
	hregs->ndptr = hdata->dma;
Linus Torvalds's avatar
Linus Torvalds committed
128 129 130 131 132 133 134 135
	if (datainp)
		hregs->ctrl = HPC3_SCTRL_ACTIVE;
	else
		hregs->ctrl = HPC3_SCTRL_ACTIVE | HPC3_SCTRL_DIR;

	return 0;
}

136
static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
Linus Torvalds's avatar
Linus Torvalds committed
137 138
		     int status)
{
139
	struct ip22_hostdata *hdata = host_to_hostdata(instance);
Linus Torvalds's avatar
Linus Torvalds committed
140 141 142 143 144
	struct hpc3_scsiregs *hregs;

	if (!SCpnt)
		return;

145 146 147
	if (SCpnt->SCp.ptr == NULL || SCpnt->SCp.this_residual == 0)
		return;

Linus Torvalds's avatar
Linus Torvalds committed
148 149
	hregs = (struct hpc3_scsiregs *) SCpnt->device->host->base;

150
	pr_debug("dma_stop: status<%d> ", status);
Linus Torvalds's avatar
Linus Torvalds committed
151 152 153 154 155 156 157 158

	/* First stop the HPC and flush it's FIFO. */
	if (hdata->wh.dma_dir) {
		hregs->ctrl |= HPC3_SCTRL_FLUSH;
		while (hregs->ctrl & HPC3_SCTRL_ACTIVE)
			barrier();
	}
	hregs->ctrl = 0;
159 160 161
	dma_unmap_single(hdata->dev, SCpnt->SCp.dma_handle,
			 SCpnt->SCp.this_residual,
			 DMA_DIR(hdata->wh.dma_dir));
Linus Torvalds's avatar
Linus Torvalds committed
162

163
	pr_debug("\n");
Linus Torvalds's avatar
Linus Torvalds committed
164 165 166 167 168 169 170 171 172 173
}

void sgiwd93_reset(unsigned long base)
{
	struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) base;

	hregs->ctrl = HPC3_SCTRL_CRESET;
	udelay(50);
	hregs->ctrl = 0;
}
174
EXPORT_SYMBOL_GPL(sgiwd93_reset);
Linus Torvalds's avatar
Linus Torvalds committed
175

176
static inline void init_hpc_chain(struct ip22_hostdata *hdata)
Linus Torvalds's avatar
Linus Torvalds committed
177
{
178 179
	struct hpc_chunk *hcp = (struct hpc_chunk *)hdata->cpu;
	dma_addr_t dma = hdata->dma;
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182
	unsigned long start, end;

	start = (unsigned long) hcp;
183
	end = start + HPC_DMA_SIZE;
Linus Torvalds's avatar
Linus Torvalds committed
184
	while (start < end) {
185
		hcp->desc.pnext = (u32) (dma + sizeof(struct hpc_chunk));
Linus Torvalds's avatar
Linus Torvalds committed
186
		hcp->desc.cntinfo = HPCDMA_EOX;
187 188
		hcp++;
		dma += sizeof(struct hpc_chunk);
Linus Torvalds's avatar
Linus Torvalds committed
189 190 191
		start += sizeof(struct hpc_chunk);
	};
	hcp--;
192
	hcp->desc.pnext = hdata->dma;
Linus Torvalds's avatar
Linus Torvalds committed
193 194
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
static int sgiwd93_bus_reset(struct scsi_cmnd *cmd)
{
	/* FIXME perform bus-specific reset */

	/* FIXME 2: kill this function, and let midlayer fallback
	   to the same result, calling wd33c93_host_reset() */

	spin_lock_irq(cmd->device->host->host_lock);
	wd33c93_host_reset(cmd);
	spin_unlock_irq(cmd->device->host->host_lock);

	return SUCCESS;
}

/*
 * Kludge alert - the SCSI code calls the abort and reset method with int
 * arguments not with pointers.  So this is going to blow up beautyfully
 * on 64-bit systems with memory outside the compat address spaces.
 */
static struct scsi_host_template sgiwd93_template = {
	.module			= THIS_MODULE,
	.proc_name		= "SGIWD93",
	.name			= "SGI WD93",
	.queuecommand		= wd33c93_queuecommand,
	.eh_abort_handler	= wd33c93_abort,
	.eh_bus_reset_handler	= sgiwd93_bus_reset,
	.eh_host_reset_handler	= wd33c93_host_reset,
	.can_queue		= 16,
	.this_id		= 7,
	.sg_tablesize		= SG_ALL,
	.cmd_per_lun		= 8,
	.use_clustering		= DISABLE_CLUSTERING,
};

229
static int sgiwd93_probe(struct platform_device *pdev)
Linus Torvalds's avatar
Linus Torvalds committed
230
{
231 232 233
	struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
	unsigned char *wdregs = pd->wdregs;
	struct hpc3_scsiregs *hregs = pd->hregs;
Linus Torvalds's avatar
Linus Torvalds committed
234 235 236
	struct ip22_hostdata *hdata;
	struct Scsi_Host *host;
	wd33c93_regs regs;
237 238 239 240 241 242 243 244 245
	unsigned int unit = pd->unit;
	unsigned int irq = pd->irq;
	int err;

	host = scsi_host_alloc(&sgiwd93_template, sizeof(struct ip22_hostdata));
	if (!host) {
		err = -ENOMEM;
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
246 247 248 249

	host->base = (unsigned long) hregs;
	host->irq = irq;

250
	hdata = host_to_hostdata(host);
251 252 253 254
	hdata->dev = &pdev->dev;
	hdata->cpu = dma_alloc_noncoherent(&pdev->dev, HPC_DMA_SIZE,
					   &hdata->dma, GFP_KERNEL);
	if (!hdata->cpu) {
Linus Torvalds's avatar
Linus Torvalds committed
255 256
		printk(KERN_WARNING "sgiwd93: Could not allocate memory for "
		       "host %d buffer.\n", unit);
257 258
		err = -ENOMEM;
		goto out_put;
Linus Torvalds's avatar
Linus Torvalds committed
259
	}
260

261
	init_hpc_chain(hdata);
Linus Torvalds's avatar
Linus Torvalds committed
262 263 264 265

	regs.SASR = wdregs + 3;
	regs.SCMD = wdregs + 7;

266 267 268
	hdata->wh.no_sync = 0;
	hdata->wh.fast = 1;
	hdata->wh.dma_mode = CTRL_BURST;
Linus Torvalds's avatar
Linus Torvalds committed
269

270
	wd33c93_init(host, regs, dma_setup, dma_stop, WD33C93_FS_MHZ(20));
Linus Torvalds's avatar
Linus Torvalds committed
271

272 273
	err = request_irq(irq, sgiwd93_intr, 0, "SGI WD93", host);
	if (err) {
Linus Torvalds's avatar
Linus Torvalds committed
274 275 276 277 278
		printk(KERN_WARNING "sgiwd93: Could not register irq %d "
		       "for host %d.\n", irq, unit);
		goto out_free;
	}

279
	platform_set_drvdata(pdev, host);
Linus Torvalds's avatar
Linus Torvalds committed
280

281 282 283
	err = scsi_add_host(host, NULL);
	if (err)
		goto out_irq;
Linus Torvalds's avatar
Linus Torvalds committed
284

285
	scsi_scan_host(host);
Linus Torvalds's avatar
Linus Torvalds committed
286

287
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
288

289 290 291
out_irq:
	free_irq(irq, host);
out_free:
292
	dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma);
293 294 295
out_put:
	scsi_host_put(host);
out:
Linus Torvalds's avatar
Linus Torvalds committed
296

297
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
298 299
}

300
static int __exit sgiwd93_remove(struct platform_device *pdev)
Linus Torvalds's avatar
Linus Torvalds committed
301
{
302 303 304 305 306 307
	struct Scsi_Host *host = platform_get_drvdata(pdev);
	struct ip22_hostdata *hdata = (struct ip22_hostdata *) host->hostdata;
	struct sgiwd93_platform_data *pd = pdev->dev.platform_data;

	scsi_remove_host(host);
	free_irq(pd->irq, host);
308
	dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma);
309
	scsi_host_put(host);
310
	return 0;
311
}
312

313 314
static struct platform_driver sgiwd93_driver = {
	.probe  = sgiwd93_probe,
315
	.remove = sgiwd93_remove,
316
	.driver = {
317 318
		.name   = "sgiwd93",
		.owner	= THIS_MODULE,
319 320
	}
};
321

322 323 324 325
static int __init sgiwd93_module_init(void)
{
	return platform_driver_register(&sgiwd93_driver);
}
326

327 328 329
static void __exit sgiwd93_module_exit(void)
{
	return platform_driver_unregister(&sgiwd93_driver);
Linus Torvalds's avatar
Linus Torvalds committed
330 331
}

332 333 334 335 336 337
module_init(sgiwd93_module_init);
module_exit(sgiwd93_module_exit);

MODULE_DESCRIPTION("SGI WD33C93 driver");
MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
MODULE_LICENSE("GPL");
338
MODULE_ALIAS("platform:sgiwd93");