sgiwd93.c 8.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*
 * 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.
 *
 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
 * 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)
L
Linus Torvalds 已提交
10 11 12
 * 
 * (In all truth, Jed Schimmel wrote all this code.)
 */
13 14 15

#undef DEBUG

L
Linus Torvalds 已提交
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>
L
Linus Torvalds 已提交
25 26 27 28
#include <linux/spinlock.h>

#include <asm/sgi/hpc3.h>
#include <asm/sgi/ip22.h>
29
#include <asm/sgi/wd.h>
L
Linus Torvalds 已提交
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;
L
Linus Torvalds 已提交
39 40
};

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

L
Linus Torvalds 已提交
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)
L
Linus Torvalds 已提交
54
{
55
	struct Scsi_Host * host = dev_id;
L
Linus Torvalds 已提交
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)
L
Linus Torvalds 已提交
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;
L
Linus Torvalds 已提交
73

74
	physaddr = dma_map_single(hd->dev, addr, len, DMA_DIR(din));
L
Linus Torvalds 已提交
75
	cmd->SCp.dma_handle = physaddr;
76
	hcp = hd->cpu;
L
Linus Torvalds 已提交
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);
L
Linus Torvalds 已提交
101 102
}

103
static int dma_setup(struct scsi_cmnd *cmd, int datainp)
L
Linus Torvalds 已提交
104
{
105
	struct ip22_hostdata *hdata = host_to_hostdata(cmd->device->host);
L
Linus Torvalds 已提交
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);
L
Linus Torvalds 已提交
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);
L
Linus Torvalds 已提交
123

124
	pr_debug(" HPCGO\n");
L
Linus Torvalds 已提交
125 126

	/* Start up the HPC. */
127
	hregs->ndptr = hdata->dma;
L
Linus Torvalds 已提交
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,
L
Linus Torvalds 已提交
137 138
		     int status)
{
139
	struct ip22_hostdata *hdata = host_to_hostdata(instance);
L
Linus Torvalds 已提交
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;

L
Linus Torvalds 已提交
148 149
	hregs = (struct hpc3_scsiregs *) SCpnt->device->host->base;

150
	pr_debug("dma_stop: status<%d> ", status);
L
Linus Torvalds 已提交
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));
L
Linus Torvalds 已提交
162

163
	pr_debug("\n");
L
Linus Torvalds 已提交
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);
L
Linus Torvalds 已提交
175

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

	start = (unsigned long) hcp;
183
	end = start + HPC_DMA_SIZE;
L
Linus Torvalds 已提交
184
	while (start < end) {
185
		hcp->desc.pnext = (u32) (dma + sizeof(struct hpc_chunk));
L
Linus Torvalds 已提交
186
		hcp->desc.cntinfo = HPCDMA_EOX;
187 188
		hcp++;
		dma += sizeof(struct hpc_chunk);
L
Linus Torvalds 已提交
189 190 191
		start += sizeof(struct hpc_chunk);
	};
	hcp--;
192
	hcp->desc.pnext = hdata->dma;
L
Linus Torvalds 已提交
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 229
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,
};

static int __init sgiwd93_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
230
{
231 232 233
	struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
	unsigned char *wdregs = pd->wdregs;
	struct hpc3_scsiregs *hregs = pd->hregs;
L
Linus Torvalds 已提交
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;
	}
L
Linus Torvalds 已提交
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) {
L
Linus Torvalds 已提交
255 256
		printk(KERN_WARNING "sgiwd93: Could not allocate memory for "
		       "host %d buffer.\n", unit);
257 258
		err = -ENOMEM;
		goto out_put;
L
Linus Torvalds 已提交
259
	}
260

261
	init_hpc_chain(hdata);
L
Linus Torvalds 已提交
262 263 264 265

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

266
	wd33c93_init(host, regs, dma_setup, dma_stop, WD33C93_FS_MHZ(20));
L
Linus Torvalds 已提交
267

268 269
	if (hdata->wh.no_sync == 0xff)
		hdata->wh.no_sync = 0;
L
Linus Torvalds 已提交
270

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

278
	platform_set_drvdata(pdev, host);
L
Linus Torvalds 已提交
279

280 281 282
	err = scsi_add_host(host, NULL);
	if (err)
		goto out_irq;
L
Linus Torvalds 已提交
283

284
	scsi_scan_host(host);
L
Linus Torvalds 已提交
285

286
	return 0;
L
Linus Torvalds 已提交
287

288 289 290
out_irq:
	free_irq(irq, host);
out_free:
291
	dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma);
292 293 294
out_put:
	scsi_host_put(host);
out:
L
Linus Torvalds 已提交
295

296
	return err;
L
Linus Torvalds 已提交
297 298
}

299
static void __exit sgiwd93_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
300
{
301 302 303 304 305 306
	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);
307
	dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma);
308 309
	scsi_host_put(host);
}
310

311 312 313 314 315 316 317
static struct platform_driver sgiwd93_driver = {
	.probe  = sgiwd93_probe,
	.remove = __devexit_p(sgiwd93_remove),
	.driver = {
		.name   = "sgiwd93"
	}
};
318

319 320 321 322
static int __init sgiwd93_module_init(void)
{
	return platform_driver_register(&sgiwd93_driver);
}
323

324 325 326
static void __exit sgiwd93_module_exit(void)
{
	return platform_driver_unregister(&sgiwd93_driver);
L
Linus Torvalds 已提交
327 328
}

329 330 331 332 333 334
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");