blob: a07e930a9c045b83d335287404d517992e0b046a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * jazz_esp.c: Driver for SCSI chip on Mips Magnum Boards (JAZZ architecture)
3 *
4 * Copyright (C) 1997 Thomas Bogendoerfer (tsbogend@alpha.franken.de)
5 *
6 * jazz_esp is based on David S. Miller's ESP driver and cyber_esp
7 */
8
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/delay.h>
12#include <linux/types.h>
13#include <linux/string.h>
14#include <linux/slab.h>
15#include <linux/blkdev.h>
16#include <linux/proc_fs.h>
17#include <linux/stat.h>
18
19#include "scsi.h"
20#include <scsi/scsi_host.h>
21#include "NCR53C9x.h"
22
23#include <asm/irq.h>
24#include <asm/jazz.h>
25#include <asm/jazzdma.h>
26#include <asm/dma.h>
27
28#include <asm/pgtable.h>
29
30static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count);
31static int dma_can_transfer(struct NCR_ESP *esp, struct scsi_cmnd *sp);
32static void dma_dump_state(struct NCR_ESP *esp);
33static void dma_init_read(struct NCR_ESP *esp, __u32 vaddress, int length);
34static void dma_init_write(struct NCR_ESP *esp, __u32 vaddress, int length);
35static void dma_ints_off(struct NCR_ESP *esp);
36static void dma_ints_on(struct NCR_ESP *esp);
37static int dma_irq_p(struct NCR_ESP *esp);
38static int dma_ports_p(struct NCR_ESP *esp);
39static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write);
40static void dma_mmu_get_scsi_one (struct NCR_ESP *esp, struct scsi_cmnd *sp);
41static void dma_mmu_get_scsi_sgl (struct NCR_ESP *esp, struct scsi_cmnd *sp);
42static void dma_mmu_release_scsi_one (struct NCR_ESP *esp, struct scsi_cmnd *sp);
43static void dma_mmu_release_scsi_sgl (struct NCR_ESP *esp, struct scsi_cmnd *sp);
44static void dma_advance_sg (struct scsi_cmnd *sp);
45static void dma_led_off(struct NCR_ESP *);
46static void dma_led_on(struct NCR_ESP *);
47
48
49static volatile unsigned char cmd_buffer[16];
50 /* This is where all commands are put
51 * before they are trasfered to the ESP chip
52 * via PIO.
53 */
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static int jazz_esp_release(struct Scsi_Host *shost)
56{
57 if (shost->irq)
58 free_irq(shost->irq, NULL);
59 if (shost->dma_channel != 0xff)
60 free_dma(shost->dma_channel);
61 if (shost->io_port && shost->n_io_port)
62 release_region(shost->io_port, shost->n_io_port);
63 scsi_unregister(shost);
64 return 0;
65}
66
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +010067static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .proc_name = "jazz_esp",
69 .proc_info = &esp_proc_info,
70 .name = "ESP 100/100a/200",
71 .detect = jazz_esp_detect,
72 .slave_alloc = esp_slave_alloc,
73 .slave_destroy = esp_slave_destroy,
74 .release = jazz_esp_release,
75 .info = esp_info,
76 .queuecommand = esp_queue,
77 .eh_abort_handler = esp_abort,
78 .eh_bus_reset_handler = esp_reset,
79 .can_queue = 7,
80 .this_id = 7,
81 .sg_tablesize = SG_ALL,
82 .cmd_per_lun = 1,
83 .use_clustering = DISABLE_CLUSTERING,
84};
85
86#include "scsi_module.c"
87
88/***************************************************************** Detection */
89static int jazz_esp_detect(struct scsi_host_template *tpnt)
90{
91 struct NCR_ESP *esp;
92 struct ConfigDev *esp_dev;
93
94 /*
95 * first assumption it is there:-)
96 */
97 if (1) {
Ralf Baechleae198df2006-02-09 11:16:27 -050098 esp_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 esp = esp_allocate(tpnt, (void *) esp_dev);
100
101 /* Do command transfer with programmed I/O */
102 esp->do_pio_cmds = 1;
103
104 /* Required functions */
105 esp->dma_bytes_sent = &dma_bytes_sent;
106 esp->dma_can_transfer = &dma_can_transfer;
107 esp->dma_dump_state = &dma_dump_state;
108 esp->dma_init_read = &dma_init_read;
109 esp->dma_init_write = &dma_init_write;
110 esp->dma_ints_off = &dma_ints_off;
111 esp->dma_ints_on = &dma_ints_on;
112 esp->dma_irq_p = &dma_irq_p;
113 esp->dma_ports_p = &dma_ports_p;
114 esp->dma_setup = &dma_setup;
115
116 /* Optional functions */
Ralf Baechleae198df2006-02-09 11:16:27 -0500117 esp->dma_barrier = NULL;
118 esp->dma_drain = NULL;
119 esp->dma_invalidate = NULL;
120 esp->dma_irq_entry = NULL;
121 esp->dma_irq_exit = NULL;
122 esp->dma_poll = NULL;
123 esp->dma_reset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 esp->dma_led_off = &dma_led_off;
125 esp->dma_led_on = &dma_led_on;
126
127 /* virtual DMA functions */
128 esp->dma_mmu_get_scsi_one = &dma_mmu_get_scsi_one;
129 esp->dma_mmu_get_scsi_sgl = &dma_mmu_get_scsi_sgl;
130 esp->dma_mmu_release_scsi_one = &dma_mmu_release_scsi_one;
131 esp->dma_mmu_release_scsi_sgl = &dma_mmu_release_scsi_sgl;
132 esp->dma_advance_sg = &dma_advance_sg;
133
134
135 /* SCSI chip speed */
136 esp->cfreq = 40000000;
137
138 /*
139 * we don't give the address of DMA channel, but the number
140 * of DMA channel, so we can use the jazz DMA functions
141 *
142 */
Ralf Baechleae198df2006-02-09 11:16:27 -0500143 esp->dregs = (void *) JAZZ_SCSI_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* ESP register base */
146 esp->eregs = (struct ESP_regs *)(JAZZ_SCSI_BASE);
147
148 /* Set the command buffer */
149 esp->esp_command = (volatile unsigned char *)cmd_buffer;
150
151 /* get virtual dma address for command buffer */
152 esp->esp_command_dvma = vdma_alloc(CPHYSADDR(cmd_buffer), sizeof (cmd_buffer));
153
154 esp->irq = JAZZ_SCSI_IRQ;
155 request_irq(JAZZ_SCSI_IRQ, esp_intr, SA_INTERRUPT, "JAZZ SCSI",
156 esp->ehost);
157
158 /*
159 * FIXME, look if the scsi id is available from NVRAM
160 */
161 esp->scsi_id = 7;
162
163 /* Check for differential SCSI-bus */
164 /* What is this stuff? */
165 esp->diff = 0;
166
167 esp_initialize(esp);
168
169 printk("ESP: Total of %d ESP hosts found, %d actually in use.\n", nesps,esps_in_use);
170 esps_running = esps_in_use;
171 return esps_in_use;
172 }
173 return 0;
174}
175
176/************************************************************* DMA Functions */
177static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count)
178{
179 return fifo_count;
180}
181
182static int dma_can_transfer(struct NCR_ESP *esp, struct scsi_cmnd *sp)
183{
184 /*
185 * maximum DMA size is 1MB
186 */
187 unsigned long sz = sp->SCp.this_residual;
188 if(sz > 0x100000)
189 sz = 0x100000;
190 return sz;
191}
192
193static void dma_dump_state(struct NCR_ESP *esp)
194{
195
196 ESPLOG(("esp%d: dma -- enable <%08x> residue <%08x\n",
197 esp->esp_id, vdma_get_enable((int)esp->dregs), vdma_get_residue((int)esp->dregs)));
198}
199
200static void dma_init_read(struct NCR_ESP *esp, __u32 vaddress, int length)
201{
202 dma_cache_wback_inv ((unsigned long)phys_to_virt(vdma_log2phys(vaddress)), length);
203 vdma_disable ((int)esp->dregs);
204 vdma_set_mode ((int)esp->dregs, DMA_MODE_READ);
205 vdma_set_addr ((int)esp->dregs, vaddress);
206 vdma_set_count ((int)esp->dregs, length);
207 vdma_enable ((int)esp->dregs);
208}
209
210static void dma_init_write(struct NCR_ESP *esp, __u32 vaddress, int length)
211{
212 dma_cache_wback_inv ((unsigned long)phys_to_virt(vdma_log2phys(vaddress)), length);
213 vdma_disable ((int)esp->dregs);
214 vdma_set_mode ((int)esp->dregs, DMA_MODE_WRITE);
215 vdma_set_addr ((int)esp->dregs, vaddress);
216 vdma_set_count ((int)esp->dregs, length);
217 vdma_enable ((int)esp->dregs);
218}
219
220static void dma_ints_off(struct NCR_ESP *esp)
221{
222 disable_irq(esp->irq);
223}
224
225static void dma_ints_on(struct NCR_ESP *esp)
226{
227 enable_irq(esp->irq);
228}
229
230static int dma_irq_p(struct NCR_ESP *esp)
231{
232 return (esp_read(esp->eregs->esp_status) & ESP_STAT_INTR);
233}
234
235static int dma_ports_p(struct NCR_ESP *esp)
236{
237 int enable = vdma_get_enable((int)esp->dregs);
238
239 return (enable & R4030_CHNL_ENABLE);
240}
241
242static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write)
243{
244 /*
245 * On the Sparc, DMA_ST_WRITE means "move data from device to memory"
246 * so when (write) is true, it actually means READ!
247 */
248 if(write){
249 dma_init_read(esp, addr, count);
250 } else {
251 dma_init_write(esp, addr, count);
252 }
253}
254
255static void dma_mmu_get_scsi_one (struct NCR_ESP *esp, struct scsi_cmnd *sp)
256{
257 sp->SCp.have_data_in = vdma_alloc(CPHYSADDR(sp->SCp.buffer), sp->SCp.this_residual);
258 sp->SCp.ptr = (char *)((unsigned long)sp->SCp.have_data_in);
259}
260
261static void dma_mmu_get_scsi_sgl (struct NCR_ESP *esp, struct scsi_cmnd *sp)
262{
263 int sz = sp->SCp.buffers_residual;
264 struct scatterlist *sg = (struct scatterlist *) sp->SCp.buffer;
265
266 while (sz >= 0) {
267 sg[sz].dma_address = vdma_alloc(CPHYSADDR(page_address(sg[sz].page) + sg[sz].offset), sg[sz].length);
268 sz--;
269 }
270 sp->SCp.ptr=(char *)(sp->SCp.buffer->dma_address);
271}
272
273static void dma_mmu_release_scsi_one (struct NCR_ESP *esp, struct scsi_cmnd *sp)
274{
275 vdma_free(sp->SCp.have_data_in);
276}
277
278static void dma_mmu_release_scsi_sgl (struct NCR_ESP *esp, struct scsi_cmnd *sp)
279{
280 int sz = sp->use_sg - 1;
281 struct scatterlist *sg = (struct scatterlist *)sp->buffer;
282
283 while(sz >= 0) {
284 vdma_free(sg[sz].dma_address);
285 sz--;
286 }
287}
288
289static void dma_advance_sg (struct scsi_cmnd *sp)
290{
291 sp->SCp.ptr = (char *)(sp->SCp.buffer->dma_address);
292}
293
294#define JAZZ_HDC_LED 0xe000d100 /* FIXME, find correct address */
295
296static void dma_led_off(struct NCR_ESP *esp)
297{
298#if 0
299 *(unsigned char *)JAZZ_HDC_LED = 0;
300#endif
301}
302
303static void dma_led_on(struct NCR_ESP *esp)
304{
305#if 0
306 *(unsigned char *)JAZZ_HDC_LED = 1;
307#endif
308}
309
310static struct scsi_host_template driver_template = {
311 .proc_name = "jazz_esp",
312 .proc_info = esp_proc_info,
313 .name = "ESP 100/100a/200",
314 .detect = jazz_esp_detect,
315 .slave_alloc = esp_slave_alloc,
316 .slave_destroy = esp_slave_destroy,
317 .release = jazz_esp_release,
318 .info = esp_info,
319 .queuecommand = esp_queue,
320 .eh_abort_handler = esp_abort,
321 .eh_bus_reset_handler = esp_reset,
322 .can_queue = 7,
323 .this_id = 7,
324 .sg_tablesize = SG_ALL,
325 .cmd_per_lun = 1,
326 .use_clustering = DISABLE_CLUSTERING,
327};
328#include "scsi_module.c"