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/kernel.h>
10 #include <linux/delay.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/blk.h>
15 #include <linux/proc_fs.h>
16 #include <linux/stat.h>
17 
18 #include "scsi.h"
19 #include "hosts.h"
20 #include "NCR53C9x.h"
21 #include "jazz_esp.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, 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, Scsi_Cmnd *sp);
41 static void dma_mmu_get_scsi_sgl (struct NCR_ESP *esp, Scsi_Cmnd *sp);
42 static void dma_mmu_release_scsi_one (struct NCR_ESP *esp, Scsi_Cmnd *sp);
43 static void dma_mmu_release_scsi_sgl (struct NCR_ESP *esp, Scsi_Cmnd *sp);
44 static void dma_advance_sg (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 /***************************************************************** Detection */
jazz_esp_detect(Scsi_Host_Template * tpnt)56 int jazz_esp_detect(Scsi_Host_Template *tpnt)
57 {
58     struct NCR_ESP *esp;
59     struct ConfigDev *esp_dev;
60 
61     /*
62      * first assumption it is there:-)
63      */
64     if (1) {
65 	esp_dev = 0;
66 	esp = esp_allocate(tpnt, (void *) esp_dev);
67 
68 	/* Do command transfer with programmed I/O */
69 	esp->do_pio_cmds = 1;
70 
71 	/* Required functions */
72 	esp->dma_bytes_sent = &dma_bytes_sent;
73 	esp->dma_can_transfer = &dma_can_transfer;
74 	esp->dma_dump_state = &dma_dump_state;
75 	esp->dma_init_read = &dma_init_read;
76 	esp->dma_init_write = &dma_init_write;
77 	esp->dma_ints_off = &dma_ints_off;
78 	esp->dma_ints_on = &dma_ints_on;
79 	esp->dma_irq_p = &dma_irq_p;
80 	esp->dma_ports_p = &dma_ports_p;
81 	esp->dma_setup = &dma_setup;
82 
83 	/* Optional functions */
84 	esp->dma_barrier = 0;
85 	esp->dma_drain = 0;
86 	esp->dma_invalidate = 0;
87 	esp->dma_irq_entry = 0;
88 	esp->dma_irq_exit = 0;
89 	esp->dma_poll = 0;
90 	esp->dma_reset = 0;
91 	esp->dma_led_off = &dma_led_off;
92 	esp->dma_led_on = &dma_led_on;
93 
94 	/* virtual DMA functions */
95 	esp->dma_mmu_get_scsi_one = &dma_mmu_get_scsi_one;
96 	esp->dma_mmu_get_scsi_sgl = &dma_mmu_get_scsi_sgl;
97 	esp->dma_mmu_release_scsi_one = &dma_mmu_release_scsi_one;
98 	esp->dma_mmu_release_scsi_sgl = &dma_mmu_release_scsi_sgl;
99 	esp->dma_advance_sg = &dma_advance_sg;
100 
101 
102 	/* SCSI chip speed */
103 	esp->cfreq = 40000000;
104 
105 	/*
106 	 * we don't give the address of DMA channel, but the number
107 	 * of DMA channel, so we can use the jazz DMA functions
108 	 *
109 	 */
110 	esp->dregs = JAZZ_SCSI_DMA;
111 
112 	/* ESP register base */
113 	esp->eregs = (struct ESP_regs *)(JAZZ_SCSI_BASE);
114 
115 	/* Set the command buffer */
116 	esp->esp_command = (volatile unsigned char *)cmd_buffer;
117 
118 	/* get virtual dma address for command buffer */
119 	esp->esp_command_dvma = vdma_alloc(PHYSADDR(cmd_buffer), sizeof (cmd_buffer));
120 
121 	esp->irq = JAZZ_SCSI_IRQ;
122 	request_irq(JAZZ_SCSI_IRQ, esp_intr, SA_INTERRUPT, "JAZZ SCSI",
123 	            NULL);
124 
125 	/*
126 	 * FIXME, look if the scsi id is available from NVRAM
127 	 */
128 	esp->scsi_id = 7;
129 
130 	/* Check for differential SCSI-bus */
131 	/* What is this stuff? */
132 	esp->diff = 0;
133 
134 	esp_initialize(esp);
135 
136 	printk("ESP: Total of %d ESP hosts found, %d actually in use.\n", nesps,esps_in_use);
137 	esps_running = esps_in_use;
138 	return esps_in_use;
139     }
140     return 0;
141 }
142 
143 /************************************************************* DMA Functions */
dma_bytes_sent(struct NCR_ESP * esp,int fifo_count)144 static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count)
145 {
146     return fifo_count;
147 }
148 
dma_can_transfer(struct NCR_ESP * esp,Scsi_Cmnd * sp)149 static int dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp)
150 {
151     /*
152      * maximum DMA size is 1MB
153      */
154     unsigned long sz = sp->SCp.this_residual;
155     if(sz > 0x100000)
156 	sz = 0x100000;
157     return sz;
158 }
159 
dma_dump_state(struct NCR_ESP * esp)160 static void dma_dump_state(struct NCR_ESP *esp)
161 {
162 
163     ESPLOG(("esp%d: dma -- enable <%08x> residue <%08x\n",
164 	    esp->esp_id, vdma_get_enable((int)esp->dregs), vdma_get_residue((int)esp->dregs)));
165 }
166 
dma_init_read(struct NCR_ESP * esp,__u32 vaddress,int length)167 static void dma_init_read(struct NCR_ESP *esp, __u32 vaddress, int length)
168 {
169     dma_cache_wback_inv ((unsigned long)phys_to_virt(vdma_log2phys(vaddress)), length);
170     vdma_disable ((int)esp->dregs);
171     vdma_set_mode ((int)esp->dregs, DMA_MODE_READ);
172     vdma_set_addr ((int)esp->dregs, vaddress);
173     vdma_set_count ((int)esp->dregs, length);
174     vdma_enable ((int)esp->dregs);
175 }
176 
dma_init_write(struct NCR_ESP * esp,__u32 vaddress,int length)177 static void dma_init_write(struct NCR_ESP *esp, __u32 vaddress, int length)
178 {
179     dma_cache_wback_inv ((unsigned long)phys_to_virt(vdma_log2phys(vaddress)), length);
180     vdma_disable ((int)esp->dregs);
181     vdma_set_mode ((int)esp->dregs, DMA_MODE_WRITE);
182     vdma_set_addr ((int)esp->dregs, vaddress);
183     vdma_set_count ((int)esp->dregs, length);
184     vdma_enable ((int)esp->dregs);
185 }
186 
dma_ints_off(struct NCR_ESP * esp)187 static void dma_ints_off(struct NCR_ESP *esp)
188 {
189     disable_irq(esp->irq);
190 }
191 
dma_ints_on(struct NCR_ESP * esp)192 static void dma_ints_on(struct NCR_ESP *esp)
193 {
194     enable_irq(esp->irq);
195 }
196 
dma_irq_p(struct NCR_ESP * esp)197 static int dma_irq_p(struct NCR_ESP *esp)
198 {
199     return (esp_read(esp->eregs->esp_status) & ESP_STAT_INTR);
200 }
201 
dma_ports_p(struct NCR_ESP * esp)202 static int dma_ports_p(struct NCR_ESP *esp)
203 {
204     int enable = vdma_get_enable((int)esp->dregs);
205 
206     return (enable & R4030_CHNL_ENABLE);
207 }
208 
dma_setup(struct NCR_ESP * esp,__u32 addr,int count,int write)209 static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write)
210 {
211     /*
212      * On the Sparc, DMA_ST_WRITE means "move data from device to memory"
213      * so when (write) is true, it actually means READ!
214      */
215     if(write){
216 	dma_init_read(esp, addr, count);
217     } else {
218 	dma_init_write(esp, addr, count);
219     }
220 }
221 
dma_mmu_get_scsi_one(struct NCR_ESP * esp,Scsi_Cmnd * sp)222 static void dma_mmu_get_scsi_one (struct NCR_ESP *esp, Scsi_Cmnd *sp)
223 {
224     sp->SCp.have_data_in = vdma_alloc(PHYSADDR(sp->SCp.buffer), sp->SCp.this_residual);
225     sp->SCp.ptr = (char *)((unsigned long)sp->SCp.have_data_in);
226 }
227 
dma_mmu_get_scsi_sgl(struct NCR_ESP * esp,Scsi_Cmnd * sp)228 static void dma_mmu_get_scsi_sgl (struct NCR_ESP *esp, Scsi_Cmnd *sp)
229 {
230     int sz = sp->SCp.buffers_residual;
231     struct mmu_sglist *sg = (struct mmu_sglist *) sp->SCp.buffer;
232 
233     while (sz >= 0) {
234 	sg[sz].dvma_addr = vdma_alloc(PHYSADDR(sg[sz].addr), sg[sz].len);
235 	sz--;
236     }
237     sp->SCp.ptr=(char *)((unsigned long)sp->SCp.buffer->dvma_address);
238 }
239 
dma_mmu_release_scsi_one(struct NCR_ESP * esp,Scsi_Cmnd * sp)240 static void dma_mmu_release_scsi_one (struct NCR_ESP *esp, Scsi_Cmnd *sp)
241 {
242     vdma_free(sp->SCp.have_data_in);
243 }
244 
dma_mmu_release_scsi_sgl(struct NCR_ESP * esp,Scsi_Cmnd * sp)245 static void dma_mmu_release_scsi_sgl (struct NCR_ESP *esp, Scsi_Cmnd *sp)
246 {
247     int sz = sp->use_sg - 1;
248     struct mmu_sglist *sg = (struct mmu_sglist *)sp->buffer;
249 
250     while(sz >= 0) {
251 	vdma_free(sg[sz].dvma_addr);
252 	sz--;
253     }
254 }
255 
dma_advance_sg(Scsi_Cmnd * sp)256 static void dma_advance_sg (Scsi_Cmnd *sp)
257 {
258     sp->SCp.ptr = (char *)((unsigned long)sp->SCp.buffer->dvma_address);
259 }
260 
261 #define JAZZ_HDC_LED   0xe000d100 /* FIXME, find correct address */
262 
dma_led_off(struct NCR_ESP * esp)263 static void dma_led_off(struct NCR_ESP *esp)
264 {
265 #if 0
266     *(unsigned char *)JAZZ_HDC_LED = 0;
267 #endif
268 }
269 
dma_led_on(struct NCR_ESP * esp)270 static void dma_led_on(struct NCR_ESP *esp)
271 {
272 #if 0
273     *(unsigned char *)JAZZ_HDC_LED = 1;
274 #endif
275 }
276