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