blob: b2d481dd37505f43f25d9c0cc79659e900bb25ca [file] [log] [blame]
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +02001/* jazz_esp.c: ESP front-end for MIPS JAZZ systems.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +02003 * Copyright (C) 2007 Thomas Bogendörfer (tsbogend@alpha.frankende)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/types.h>
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +02008#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <asm/irq.h>
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020015#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/dma.h>
17
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020018#include <asm/jazz.h>
19#include <asm/jazzdma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020021#include <scsi/scsi_host.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020023#include "esp_scsi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020025#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020030static void jazz_esp_write8(struct esp *esp, u8 val, unsigned long reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020032 *(volatile u8 *)(esp->regs + reg) = val;
33}
34
35static u8 jazz_esp_read8(struct esp *esp, unsigned long reg)
36{
37 return *(volatile u8 *)(esp->regs + reg);
38}
39
40static dma_addr_t jazz_esp_map_single(struct esp *esp, void *buf,
41 size_t sz, int dir)
42{
43 return dma_map_single(esp->dev, buf, sz, dir);
44}
45
46static int jazz_esp_map_sg(struct esp *esp, struct scatterlist *sg,
47 int num_sg, int dir)
48{
49 return dma_map_sg(esp->dev, sg, num_sg, dir);
50}
51
52static void jazz_esp_unmap_single(struct esp *esp, dma_addr_t addr,
53 size_t sz, int dir)
54{
55 dma_unmap_single(esp->dev, addr, sz, dir);
56}
57
58static void jazz_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
59 int num_sg, int dir)
60{
61 dma_unmap_sg(esp->dev, sg, num_sg, dir);
62}
63
64static int jazz_esp_irq_pending(struct esp *esp)
65{
66 if (jazz_esp_read8(esp, ESP_STATUS) & ESP_STAT_INTR)
67 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return 0;
69}
70
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020071static void jazz_esp_reset_dma(struct esp *esp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020073 vdma_disable ((int)esp->dma_regs);
74}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020076static void jazz_esp_dma_drain(struct esp *esp)
77{
78 /* nothing to do */
79}
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020081static void jazz_esp_dma_invalidate(struct esp *esp)
82{
83 vdma_disable ((int)esp->dma_regs);
84}
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020086static void jazz_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
87 u32 dma_count, int write, u8 cmd)
88{
89 BUG_ON(!(cmd & ESP_CMD_DMA));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020091 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);
104}
105
106static int jazz_esp_dma_error(struct esp *esp)
107{
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;
114}
115
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)
132{
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;
Christoph Hellwig2b14ec72007-05-31 20:12:32 +0200146 esp = shost_priv(host);
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200147
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 esp->cfreq = 40000000;
181
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200182 dev_set_drvdata(&dev->dev, esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200184 err = scsi_esp_register(esp, &dev->dev);
185 if (err)
186 goto fail_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200188 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200203static int __devexit esp_jazz_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200205 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Kay Sieversecc1241e2008-04-18 13:57:19 -0700220/* work with hotplug and coldplug */
221MODULE_ALIAS("platform:jazz_esp");
222
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200223static struct platform_driver esp_jazz_driver = {
224 .probe = esp_jazz_probe,
225 .remove = __devexit_p(esp_jazz_remove),
226 .driver = {
227 .name = "jazz_esp",
Kay Sieversecc1241e2008-04-18 13:57:19 -0700228 .owner = THIS_MODULE,
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200229 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230};
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200231
232static int __init jazz_esp_init(void)
233{
234 return platform_driver_register(&esp_jazz_driver);
235}
236
237static void __exit jazz_esp_exit(void)
238{
239 platform_driver_unregister(&esp_jazz_driver);
240}
241
242MODULE_DESCRIPTION("JAZZ ESP SCSI driver");
243MODULE_AUTHOR("Thomas Bogendoerfer (tsbogend@alpha.franken.de)");
244MODULE_LICENSE("GPL");
245MODULE_VERSION(DRV_VERSION);
246
247module_init(jazz_esp_init);
248module_exit(jazz_esp_exit);