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