GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / sun3x_esp.c
blob0c8dfe6e092cb5ced3937393595d4047f39ae55b
1 /* sun3x_esp.c: ESP front-end for Sun3x systems.
3 * Copyright (C) 2007,2008 Thomas Bogendoerfer (tsbogend@alpha.franken.de)
4 */
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>
16 #include <asm/sun3x.h>
17 #include <asm/io.h>
18 #include <asm/dma.h>
19 #include <asm/dvma.h>
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 */
27 #include <scsi/scsi_host.h>
29 #include "esp_scsi.h"
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"
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)
41 static void sun3x_esp_write8(struct esp *esp, u8 val, unsigned long reg)
43 writeb(val, esp->regs + (reg * 4UL));
46 static u8 sun3x_esp_read8(struct esp *esp, unsigned long reg)
48 return readb(esp->regs + (reg * 4UL));
51 static dma_addr_t sun3x_esp_map_single(struct esp *esp, void *buf,
52 size_t sz, int dir)
54 return dma_map_single(esp->dev, buf, sz, dir);
57 static int sun3x_esp_map_sg(struct esp *esp, struct scatterlist *sg,
58 int num_sg, int dir)
60 return dma_map_sg(esp->dev, sg, num_sg, dir);
63 static void sun3x_esp_unmap_single(struct esp *esp, dma_addr_t addr,
64 size_t sz, int dir)
66 dma_unmap_single(esp->dev, addr, sz, dir);
69 static void sun3x_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
70 int num_sg, int dir)
72 dma_unmap_sg(esp->dev, sg, num_sg, dir);
75 static int sun3x_esp_irq_pending(struct esp *esp)
77 if (dma_read32(DMA_CSR) & (DMA_HNDL_INTR | DMA_HNDL_ERROR))
78 return 1;
79 return 0;
82 static void sun3x_esp_reset_dma(struct esp *esp)
84 u32 val;
86 val = dma_read32(DMA_CSR);
87 dma_write32(val | DMA_RST_SCSI, DMA_CSR);
88 dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
90 /* Enable interrupts. */
91 val = dma_read32(DMA_CSR);
92 dma_write32(val | DMA_INT_ENAB, DMA_CSR);
95 static void sun3x_esp_dma_drain(struct esp *esp)
97 u32 csr;
98 int lim;
100 csr = dma_read32(DMA_CSR);
101 if (!(csr & DMA_FIFO_ISDRAIN))
102 return;
104 dma_write32(csr | DMA_FIFO_STDRAIN, DMA_CSR);
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;
113 udelay(1);
117 static void sun3x_esp_dma_invalidate(struct esp *esp)
119 u32 val;
120 int lim;
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;
129 udelay(1);
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);
139 static void sun3x_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
140 u32 dma_count, int write, u8 cmd)
142 u32 csr;
144 BUG_ON(!(cmd & ESP_CMD_DMA));
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);
157 scsi_esp_cmd(esp, cmd);
160 static int sun3x_esp_dma_error(struct esp *esp)
162 u32 csr = dma_read32(DMA_CSR);
164 if (csr & DMA_HNDL_ERROR)
165 return 1;
167 return 0;
170 static 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,
185 static int __devinit esp_sun3x_probe(struct platform_device *dev)
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;
193 host = scsi_host_alloc(tpnt, sizeof(struct esp));
194 if (!host)
195 goto fail;
197 host->max_id = 8;
198 esp = shost_priv(host);
200 esp->host = host;
201 esp->dev = dev;
202 esp->ops = &sun3x_esp_ops;
204 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
205 if (!res || !res->start)
206 goto fail_unlink;
208 esp->regs = ioremap_nocache(res->start, 0x20);
209 if (!esp->regs)
210 goto fail_unmap_regs;
212 res = platform_get_resource(dev, IORESOURCE_MEM, 1);
213 if (!res || !res->start)
214 goto fail_unmap_regs;
216 esp->dma_regs = ioremap_nocache(res->start, 0x10);
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;
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;
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;
235 dev_set_drvdata(&dev->dev, esp);
237 err = scsi_esp_register(esp, &dev->dev);
238 if (err)
239 goto fail_free_irq;
241 return 0;
243 fail_free_irq:
244 free_irq(host->irq, esp);
245 fail_unmap_command_block:
246 dma_free_coherent(esp->dev, 16,
247 esp->command_block,
248 esp->command_block_dma);
249 fail_unmap_regs_dma:
250 iounmap(esp->dma_regs);
251 fail_unmap_regs:
252 iounmap(esp->regs);
253 fail_unlink:
254 scsi_host_put(host);
255 fail:
256 return err;
259 static int __devexit esp_sun3x_remove(struct platform_device *dev)
261 struct esp *esp = dev_get_drvdata(&dev->dev);
262 unsigned int irq = esp->host->irq;
263 u32 val;
265 scsi_esp_unregister(esp);
267 /* Disable interrupts. */
268 val = dma_read32(DMA_CSR);
269 dma_write32(val & ~DMA_INT_ENAB, DMA_CSR);
271 free_irq(irq, esp);
272 dma_free_coherent(esp->dev, 16,
273 esp->command_block,
274 esp->command_block_dma);
276 scsi_host_put(esp->host);
278 return 0;
281 static 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,
290 static int __init sun3x_esp_init(void)
292 return platform_driver_register(&esp_sun3x_driver);
295 static void __exit sun3x_esp_exit(void)
297 platform_driver_unregister(&esp_sun3x_driver);
300 MODULE_DESCRIPTION("Sun3x ESP SCSI driver");
301 MODULE_AUTHOR("Thomas Bogendoerfer (tsbogend@alpha.franken.de)");
302 MODULE_LICENSE("GPL");
303 MODULE_VERSION(DRV_VERSION);
305 module_init(sun3x_esp_init);
306 module_exit(sun3x_esp_exit);
307 MODULE_ALIAS("platform:sun3x_esp");