[SCSI] jazz_esp: Converted to use esp_core.
[linux-2.6-block.git] / drivers / scsi / jazz_esp.c
CommitLineData
5ff26366 1/* jazz_esp.c: ESP front-end for MIPS JAZZ systems.
1da177e4 2 *
5ff26366 3 * Copyright (C) 2007 Thomas Bogendörfer (tsbogend@alpha.frankende)
1da177e4
LT
4 */
5
1da177e4 6#include <linux/kernel.h>
1da177e4 7#include <linux/types.h>
5ff26366
TB
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/interrupt.h>
11#include <linux/platform_device.h>
12#include <linux/dma-mapping.h>
1da177e4
LT
13
14#include <asm/irq.h>
5ff26366
TB
15#include <asm/io.h>
16#include <asm/dma.h>
17
1da177e4
LT
18#include <asm/jazz.h>
19#include <asm/jazzdma.h>
1da177e4 20
5ff26366 21#include <scsi/scsi_host.h>
1da177e4 22
5ff26366 23#include "esp_scsi.h"
1da177e4 24
5ff26366
TB
25#define DRV_MODULE_NAME "jazz_esp"
26#define PFX DRV_MODULE_NAME ": "
27#define DRV_VERSION "1.000"
28#define DRV_MODULE_RELDATE "May 19, 2007"
1da177e4 29
5ff26366 30static void jazz_esp_write8(struct esp *esp, u8 val, unsigned long reg)
1da177e4 31{
5ff26366 32 *(volatile u8 *)(esp->regs + reg) = val;
1da177e4
LT
33}
34
5ff26366 35static u8 jazz_esp_read8(struct esp *esp, unsigned long reg)
1da177e4 36{
5ff26366 37 return *(volatile u8 *)(esp->regs + reg);
1da177e4
LT
38}
39
5ff26366
TB
40static dma_addr_t jazz_esp_map_single(struct esp *esp, void *buf,
41 size_t sz, int dir)
1da177e4 42{
5ff26366 43 return dma_map_single(esp->dev, buf, sz, dir);
1da177e4
LT
44}
45
5ff26366
TB
46static int jazz_esp_map_sg(struct esp *esp, struct scatterlist *sg,
47 int num_sg, int dir)
1da177e4 48{
5ff26366 49 return dma_map_sg(esp->dev, sg, num_sg, dir);
1da177e4
LT
50}
51
5ff26366
TB
52static void jazz_esp_unmap_single(struct esp *esp, dma_addr_t addr,
53 size_t sz, int dir)
1da177e4 54{
5ff26366 55 dma_unmap_single(esp->dev, addr, sz, dir);
1da177e4
LT
56}
57
5ff26366
TB
58static void jazz_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
59 int num_sg, int dir)
1da177e4 60{
5ff26366 61 dma_unmap_sg(esp->dev, sg, num_sg, dir);
1da177e4
LT
62}
63
5ff26366 64static int jazz_esp_irq_pending(struct esp *esp)
1da177e4 65{
5ff26366
TB
66 if (jazz_esp_read8(esp, ESP_STATUS) & ESP_STAT_INTR)
67 return 1;
68 return 0;
1da177e4
LT
69}
70
5ff26366 71static void jazz_esp_reset_dma(struct esp *esp)
1da177e4 72{
5ff26366 73 vdma_disable ((int)esp->dma_regs);
1da177e4
LT
74}
75
5ff26366 76static void jazz_esp_dma_drain(struct esp *esp)
1da177e4 77{
5ff26366 78 /* nothing to do */
1da177e4
LT
79}
80
5ff26366 81static void jazz_esp_dma_invalidate(struct esp *esp)
1da177e4 82{
5ff26366 83 vdma_disable ((int)esp->dma_regs);
1da177e4
LT
84}
85
5ff26366
TB
86static void jazz_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
87 u32 dma_count, int write, u8 cmd)
1da177e4 88{
5ff26366
TB
89 BUG_ON(!(cmd & ESP_CMD_DMA));
90
91 jazz_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
92 jazz_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
93 vdma_disable ((int)esp->dma_regs);
94 if (write)
95 vdma_set_mode ((int)esp->dma_regs, DMA_MODE_READ);
96 else
97 vdma_set_mode ((int)esp->dma_regs, DMA_MODE_WRITE);
98
99 vdma_set_addr ((int)esp->dma_regs, addr);
100 vdma_set_count ((int)esp->dma_regs, dma_count);
101 vdma_enable ((int)esp->dma_regs);
102
103 scsi_esp_cmd(esp, cmd);
1da177e4
LT
104}
105
5ff26366 106static int jazz_esp_dma_error(struct esp *esp)
1da177e4 107{
5ff26366
TB
108 u32 enable = vdma_get_enable((int)esp->dma_regs);
109
110 if (enable & (R4030_MEM_INTR|R4030_ADDR_INTR))
111 return 1;
112
113 return 0;
1da177e4
LT
114}
115
5ff26366
TB
116static const struct esp_driver_ops jazz_esp_ops = {
117 .esp_write8 = jazz_esp_write8,
118 .esp_read8 = jazz_esp_read8,
119 .map_single = jazz_esp_map_single,
120 .map_sg = jazz_esp_map_sg,
121 .unmap_single = jazz_esp_unmap_single,
122 .unmap_sg = jazz_esp_unmap_sg,
123 .irq_pending = jazz_esp_irq_pending,
124 .reset_dma = jazz_esp_reset_dma,
125 .dma_drain = jazz_esp_dma_drain,
126 .dma_invalidate = jazz_esp_dma_invalidate,
127 .send_dma_cmd = jazz_esp_send_dma_cmd,
128 .dma_error = jazz_esp_dma_error,
129};
130
131static int __devinit esp_jazz_probe(struct platform_device *dev)
1da177e4 132{
5ff26366
TB
133 struct scsi_host_template *tpnt = &scsi_esp_template;
134 struct Scsi_Host *host;
135 struct esp *esp;
136 struct resource *res;
137 int err;
138
139 host = scsi_host_alloc(tpnt, sizeof(struct esp));
140
141 err = -ENOMEM;
142 if (!host)
143 goto fail;
144
145 host->max_id = 8;
146 esp = host_to_esp(host);
147
148 esp->host = host;
149 esp->dev = dev;
150 esp->ops = &jazz_esp_ops;
151
152 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
153 if (!res)
154 goto fail_unlink;
155
156 esp->regs = (void __iomem *)res->start;
157 if (!esp->regs)
158 goto fail_unlink;
159
160 res = platform_get_resource(dev, IORESOURCE_MEM, 1);
161 if (!res)
162 goto fail_unlink;
163
164 esp->dma_regs = (void __iomem *)res->start;
165
166 esp->command_block = dma_alloc_coherent(esp->dev, 16,
167 &esp->command_block_dma,
168 GFP_KERNEL);
169 if (!esp->command_block)
170 goto fail_unmap_regs;
171
172 host->irq = platform_get_irq(dev, 0);
173 err = request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "ESP", esp);
174 if (err < 0)
175 goto fail_unmap_command_block;
176
177 esp->scsi_id = 7;
178 esp->host->this_id = esp->scsi_id;
179 esp->scsi_id_mask = (1 << esp->scsi_id);
180 esp->cfreq = 40000000;
181
182 dev_set_drvdata(&dev->dev, esp);
183
184 err = scsi_esp_register(esp, &dev->dev);
185 if (err)
186 goto fail_free_irq;
187
188 return 0;
189
190fail_free_irq:
191 free_irq(host->irq, esp);
192fail_unmap_command_block:
193 dma_free_coherent(esp->dev, 16,
194 esp->command_block,
195 esp->command_block_dma);
196fail_unmap_regs:
197fail_unlink:
198 scsi_host_put(host);
199fail:
200 return err;
1da177e4
LT
201}
202
5ff26366 203static int __devexit esp_jazz_remove(struct platform_device *dev)
1da177e4 204{
5ff26366
TB
205 struct esp *esp = dev_get_drvdata(&dev->dev);
206 unsigned int irq = esp->host->irq;
207
208 scsi_esp_unregister(esp);
209
210 free_irq(irq, esp);
211 dma_free_coherent(esp->dev, 16,
212 esp->command_block,
213 esp->command_block_dma);
214
215 scsi_host_put(esp->host);
216
217 return 0;
1da177e4
LT
218}
219
5ff26366
TB
220static struct platform_driver esp_jazz_driver = {
221 .probe = esp_jazz_probe,
222 .remove = __devexit_p(esp_jazz_remove),
223 .driver = {
224 .name = "jazz_esp",
225 },
226};
1da177e4 227
5ff26366 228static int __init jazz_esp_init(void)
1da177e4 229{
5ff26366 230 return platform_driver_register(&esp_jazz_driver);
1da177e4
LT
231}
232
5ff26366
TB
233static void __exit jazz_esp_exit(void)
234{
235 platform_driver_unregister(&esp_jazz_driver);
1da177e4
LT
236}
237
5ff26366
TB
238MODULE_DESCRIPTION("JAZZ ESP SCSI driver");
239MODULE_AUTHOR("Thomas Bogendoerfer (tsbogend@alpha.franken.de)");
240MODULE_LICENSE("GPL");
241MODULE_VERSION(DRV_VERSION);
242
243module_init(jazz_esp_init);
244module_exit(jazz_esp_exit);