• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/scsi/
1/* sun3x_esp.c: ESP front-end for Sun3x systems.
2 *
3 * Copyright (C) 2007,2008 Thomas Bogendoerfer (tsbogend@alpha.franken.de)
4 */
5
6#include <linux/kernel.h>
7#include <linux/gfp.h>
8#include <linux/types.h>
9#include <linux/delay.h>
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/platform_device.h>
13#include <linux/dma-mapping.h>
14#include <linux/interrupt.h>
15
16#include <asm/sun3x.h>
17#include <asm/io.h>
18#include <asm/dma.h>
19#include <asm/dvma.h>
20
21/* DMA controller reg offsets */
22#define DMA_CSR		0x00UL	/* rw  DMA control/status register    0x00   */
23#define DMA_ADDR        0x04UL	/* rw  DMA transfer address register  0x04   */
24#define DMA_COUNT       0x08UL	/* rw  DMA transfer count register    0x08   */
25#define DMA_TEST        0x0cUL	/* rw  DMA test/debug register        0x0c   */
26
27#include <scsi/scsi_host.h>
28
29#include "esp_scsi.h"
30
31#define DRV_MODULE_NAME		"sun3x_esp"
32#define PFX DRV_MODULE_NAME	": "
33#define DRV_VERSION		"1.000"
34#define DRV_MODULE_RELDATE	"Nov 1, 2007"
35
36#define dma_read32(REG) \
37	*(volatile u32 *)(esp->dma_regs + (REG))
38#define dma_write32(VAL, REG) \
39	do { *(volatile u32 *)(esp->dma_regs + (REG)) = (VAL); } while (0)
40
41static void sun3x_esp_write8(struct esp *esp, u8 val, unsigned long reg)
42{
43	writeb(val, esp->regs + (reg * 4UL));
44}
45
46static u8 sun3x_esp_read8(struct esp *esp, unsigned long reg)
47{
48	return readb(esp->regs + (reg * 4UL));
49}
50
51static dma_addr_t sun3x_esp_map_single(struct esp *esp, void *buf,
52				      size_t sz, int dir)
53{
54	return dma_map_single(esp->dev, buf, sz, dir);
55}
56
57static int sun3x_esp_map_sg(struct esp *esp, struct scatterlist *sg,
58				  int num_sg, int dir)
59{
60	return dma_map_sg(esp->dev, sg, num_sg, dir);
61}
62
63static void sun3x_esp_unmap_single(struct esp *esp, dma_addr_t addr,
64				  size_t sz, int dir)
65{
66	dma_unmap_single(esp->dev, addr, sz, dir);
67}
68
69static void sun3x_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
70			      int num_sg, int dir)
71{
72	dma_unmap_sg(esp->dev, sg, num_sg, dir);
73}
74
75static int sun3x_esp_irq_pending(struct esp *esp)
76{
77	if (dma_read32(DMA_CSR) & (DMA_HNDL_INTR | DMA_HNDL_ERROR))
78		return 1;
79	return 0;
80}
81
82static void sun3x_esp_reset_dma(struct esp *esp)
83{
84	u32 val;
85
86	val = dma_read32(DMA_CSR);
87	dma_write32(val | DMA_RST_SCSI, DMA_CSR);
88	dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
89
90	/* Enable interrupts.  */
91	val = dma_read32(DMA_CSR);
92	dma_write32(val | DMA_INT_ENAB, DMA_CSR);
93}
94
95static void sun3x_esp_dma_drain(struct esp *esp)
96{
97	u32 csr;
98	int lim;
99
100	csr = dma_read32(DMA_CSR);
101	if (!(csr & DMA_FIFO_ISDRAIN))
102		return;
103
104	dma_write32(csr | DMA_FIFO_STDRAIN, DMA_CSR);
105
106	lim = 1000;
107	while (dma_read32(DMA_CSR) & DMA_FIFO_ISDRAIN) {
108		if (--lim == 0) {
109			printk(KERN_ALERT PFX "esp%d: DMA will not drain!\n",
110			       esp->host->unique_id);
111			break;
112		}
113		udelay(1);
114	}
115}
116
117static void sun3x_esp_dma_invalidate(struct esp *esp)
118{
119	u32 val;
120	int lim;
121
122	lim = 1000;
123	while ((val = dma_read32(DMA_CSR)) & DMA_PEND_READ) {
124		if (--lim == 0) {
125			printk(KERN_ALERT PFX "esp%d: DMA will not "
126			       "invalidate!\n", esp->host->unique_id);
127			break;
128		}
129		udelay(1);
130	}
131
132	val &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
133	val |= DMA_FIFO_INV;
134	dma_write32(val, DMA_CSR);
135	val &= ~DMA_FIFO_INV;
136	dma_write32(val, DMA_CSR);
137}
138
139static void sun3x_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
140				  u32 dma_count, int write, u8 cmd)
141{
142	u32 csr;
143
144	BUG_ON(!(cmd & ESP_CMD_DMA));
145
146	sun3x_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
147	sun3x_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
148	csr = dma_read32(DMA_CSR);
149	csr |= DMA_ENABLE;
150	if (write)
151		csr |= DMA_ST_WRITE;
152	else
153		csr &= ~DMA_ST_WRITE;
154	dma_write32(csr, DMA_CSR);
155	dma_write32(addr, DMA_ADDR);
156
157	scsi_esp_cmd(esp, cmd);
158}
159
160static int sun3x_esp_dma_error(struct esp *esp)
161{
162	u32 csr = dma_read32(DMA_CSR);
163
164	if (csr & DMA_HNDL_ERROR)
165		return 1;
166
167	return 0;
168}
169
170static const struct esp_driver_ops sun3x_esp_ops = {
171	.esp_write8	=	sun3x_esp_write8,
172	.esp_read8	=	sun3x_esp_read8,
173	.map_single	=	sun3x_esp_map_single,
174	.map_sg		=	sun3x_esp_map_sg,
175	.unmap_single	=	sun3x_esp_unmap_single,
176	.unmap_sg	=	sun3x_esp_unmap_sg,
177	.irq_pending	=	sun3x_esp_irq_pending,
178	.reset_dma	=	sun3x_esp_reset_dma,
179	.dma_drain	=	sun3x_esp_dma_drain,
180	.dma_invalidate	=	sun3x_esp_dma_invalidate,
181	.send_dma_cmd	=	sun3x_esp_send_dma_cmd,
182	.dma_error	=	sun3x_esp_dma_error,
183};
184
185static int __devinit esp_sun3x_probe(struct platform_device *dev)
186{
187	struct scsi_host_template *tpnt = &scsi_esp_template;
188	struct Scsi_Host *host;
189	struct esp *esp;
190	struct resource *res;
191	int err = -ENOMEM;
192
193	host = scsi_host_alloc(tpnt, sizeof(struct esp));
194	if (!host)
195		goto fail;
196
197	host->max_id = 8;
198	esp = shost_priv(host);
199
200	esp->host = host;
201	esp->dev = dev;
202	esp->ops = &sun3x_esp_ops;
203
204	res = platform_get_resource(dev, IORESOURCE_MEM, 0);
205	if (!res || !res->start)
206		goto fail_unlink;
207
208	esp->regs = ioremap_nocache(res->start, 0x20);
209	if (!esp->regs)
210		goto fail_unmap_regs;
211
212	res = platform_get_resource(dev, IORESOURCE_MEM, 1);
213	if (!res || !res->start)
214		goto fail_unmap_regs;
215
216	esp->dma_regs = ioremap_nocache(res->start, 0x10);
217
218	esp->command_block = dma_alloc_coherent(esp->dev, 16,
219						&esp->command_block_dma,
220						GFP_KERNEL);
221	if (!esp->command_block)
222		goto fail_unmap_regs_dma;
223
224	host->irq = platform_get_irq(dev, 0);
225	err = request_irq(host->irq, scsi_esp_intr, IRQF_SHARED,
226			  "SUN3X ESP", esp);
227	if (err < 0)
228		goto fail_unmap_command_block;
229
230	esp->scsi_id = 7;
231	esp->host->this_id = esp->scsi_id;
232	esp->scsi_id_mask = (1 << esp->scsi_id);
233	esp->cfreq = 20000000;
234
235	dev_set_drvdata(&dev->dev, esp);
236
237	err = scsi_esp_register(esp, &dev->dev);
238	if (err)
239		goto fail_free_irq;
240
241	return 0;
242
243fail_free_irq:
244	free_irq(host->irq, esp);
245fail_unmap_command_block:
246	dma_free_coherent(esp->dev, 16,
247			  esp->command_block,
248			  esp->command_block_dma);
249fail_unmap_regs_dma:
250	iounmap(esp->dma_regs);
251fail_unmap_regs:
252	iounmap(esp->regs);
253fail_unlink:
254	scsi_host_put(host);
255fail:
256	return err;
257}
258
259static int __devexit esp_sun3x_remove(struct platform_device *dev)
260{
261	struct esp *esp = dev_get_drvdata(&dev->dev);
262	unsigned int irq = esp->host->irq;
263	u32 val;
264
265	scsi_esp_unregister(esp);
266
267	/* Disable interrupts.  */
268	val = dma_read32(DMA_CSR);
269	dma_write32(val & ~DMA_INT_ENAB, DMA_CSR);
270
271	free_irq(irq, esp);
272	dma_free_coherent(esp->dev, 16,
273			  esp->command_block,
274			  esp->command_block_dma);
275
276	scsi_host_put(esp->host);
277
278	return 0;
279}
280
281static struct platform_driver esp_sun3x_driver = {
282	.probe          = esp_sun3x_probe,
283	.remove         = __devexit_p(esp_sun3x_remove),
284	.driver = {
285		.name   = "sun3x_esp",
286		.owner	= THIS_MODULE,
287	},
288};
289
290static int __init sun3x_esp_init(void)
291{
292	return platform_driver_register(&esp_sun3x_driver);
293}
294
295static void __exit sun3x_esp_exit(void)
296{
297	platform_driver_unregister(&esp_sun3x_driver);
298}
299
300MODULE_DESCRIPTION("Sun3x ESP SCSI driver");
301MODULE_AUTHOR("Thomas Bogendoerfer (tsbogend@alpha.franken.de)");
302MODULE_LICENSE("GPL");
303MODULE_VERSION(DRV_VERSION);
304
305module_init(sun3x_esp_init);
306module_exit(sun3x_esp_exit);
307MODULE_ALIAS("platform:sun3x_esp");
308