1 /* asm-sparc/floppy.h: Sparc specific parts of the Floppy driver.
2 *
3 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
4 */
5
6 #ifndef __ASM_SPARC_FLOPPY_H
7 #define __ASM_SPARC_FLOPPY_H
8
9 #include <asm/page.h>
10 #include <asm/pgtable.h>
11 #include <asm/system.h>
12 #include <asm/idprom.h>
13 #include <asm/machines.h>
14 #include <asm/oplib.h>
15 #include <asm/auxio.h>
16 #include <asm/irq.h>
17
18 /* We don't need no stinkin' I/O port allocation crap. */
19 #undef release_region
20 #undef check_region
21 #undef request_region
22 #define release_region(X, Y) do { } while(0)
23 #define check_region(X, Y) (0)
24 #define request_region(X, Y, Z) (1)
25
26 /* References:
27 * 1) Netbsd Sun floppy driver.
28 * 2) NCR 82077 controller manual
29 * 3) Intel 82077 controller manual
30 */
31 struct sun_flpy_controller {
32 volatile unsigned char status_82072; /* Main Status reg. */
33 #define dcr_82072 status_82072 /* Digital Control reg. */
34 #define status1_82077 status_82072 /* Auxiliary Status reg. 1 */
35
36 volatile unsigned char data_82072; /* Data fifo. */
37 #define status2_82077 data_82072 /* Auxiliary Status reg. 2 */
38
39 volatile unsigned char dor_82077; /* Digital Output reg. */
40 volatile unsigned char tapectl_82077; /* What the? Tape control reg? */
41
42 volatile unsigned char status_82077; /* Main Status Register. */
43 #define drs_82077 status_82077 /* Digital Rate Select reg. */
44
45 volatile unsigned char data_82077; /* Data fifo. */
46 volatile unsigned char ___unused;
47 volatile unsigned char dir_82077; /* Digital Input reg. */
48 #define dcr_82077 dir_82077 /* Config Control reg. */
49 };
50
51 /* You'll only ever find one controller on a SparcStation anyways. */
52 static struct sun_flpy_controller *sun_fdc = NULL;
53 volatile unsigned char *fdc_status;
54
55 struct sun_floppy_ops {
56 unsigned char (*fd_inb)(int port);
57 void (*fd_outb)(unsigned char value, int port);
58 };
59
60 static struct sun_floppy_ops sun_fdops;
61
62 #define fd_inb(port) sun_fdops.fd_inb(port)
63 #define fd_outb(value,port) sun_fdops.fd_outb(value,port)
64 #define fd_enable_dma() sun_fd_enable_dma()
65 #define fd_disable_dma() sun_fd_disable_dma()
66 #define fd_request_dma() (0) /* nothing... */
67 #define fd_free_dma() /* nothing... */
68 #define fd_clear_dma_ff() /* nothing... */
69 #define fd_set_dma_mode(mode) sun_fd_set_dma_mode(mode)
70 #define fd_set_dma_addr(addr) sun_fd_set_dma_addr(addr)
71 #define fd_set_dma_count(count) sun_fd_set_dma_count(count)
72 #define fd_enable_irq() /* nothing... */
73 #define fd_disable_irq() /* nothing... */
74 #define fd_cacheflush(addr, size) /* nothing... */
75 #define fd_request_irq() sun_fd_request_irq()
76 #define fd_free_irq() /* nothing... */
77 #if 0 /* P3: added by Alain, these cause a MMU corruption. 19960524 XXX */
78 #define fd_dma_mem_alloc(size) ((unsigned long) vmalloc(size))
79 #define fd_dma_mem_free(addr,size) (vfree((void *)(addr)))
80 #endif
81
82 #define FLOPPY_MOTOR_MASK 0x10
83
84 /* XXX This isn't really correct. XXX */
85 #define get_dma_residue(x) (0)
86
87 #define FLOPPY0_TYPE 4
88 #define FLOPPY1_TYPE 0
89
90 /* Super paranoid... */
91 #undef HAVE_DISABLE_HLT
92
93 /* Here is where we catch the floppy driver trying to initialize,
94 * therefore this is where we call the PROM device tree probing
95 * routine etc. on the Sparc.
96 */
97 #define FDC1 sun_floppy_init()
98
99 #define N_FDC 1
100 #define N_DRIVE 8
101
102 /* No 64k boundary crossing problems on the Sparc. */
103 #define CROSS_64KB(a,s) (0)
104
105 /* Routines unique to each controller type on a Sun. */
sun_82072_fd_inb(int port)106 static unsigned char sun_82072_fd_inb(int port)
107 {
108 udelay(5);
109 switch(port & 7) {
110 default:
111 printk("floppy: Asked to read unknown port %d\n", port);
112 panic("floppy: Port bolixed.");
113 case 4: /* FD_STATUS */
114 return sun_fdc->status_82072 & ~STATUS_DMA;
115 case 5: /* FD_DATA */
116 return sun_fdc->data_82072;
117 case 7: /* FD_DIR */
118 return (get_auxio() & AUXIO_FLPY_DCHG)? 0x80: 0;
119 };
120 panic("sun_82072_fd_inb: How did I get here?");
121 }
122
sun_82072_fd_outb(unsigned char value,int port)123 static void sun_82072_fd_outb(unsigned char value, int port)
124 {
125 udelay(5);
126 switch(port & 7) {
127 default:
128 printk("floppy: Asked to write to unknown port %d\n", port);
129 panic("floppy: Port bolixed.");
130 case 2: /* FD_DOR */
131 /* Oh geese, 82072 on the Sun has no DOR register,
132 * the functionality is implemented via the AUXIO
133 * I/O register. So we must emulate the behavior.
134 *
135 * ASSUMPTIONS: There will only ever be one floppy
136 * drive attached to a Sun controller
137 * and it will be at drive zero.
138 */
139 {
140 unsigned bits = 0;
141 if (value & 0x10) bits |= AUXIO_FLPY_DSEL;
142 if ((value & 0x80) == 0) bits |= AUXIO_FLPY_EJCT;
143 set_auxio(bits, (~bits) & (AUXIO_FLPY_DSEL|AUXIO_FLPY_EJCT));
144 }
145 break;
146 case 5: /* FD_DATA */
147 sun_fdc->data_82072 = value;
148 break;
149 case 7: /* FD_DCR */
150 sun_fdc->dcr_82072 = value;
151 break;
152 case 4: /* FD_STATUS */
153 sun_fdc->status_82072 = value;
154 break;
155 };
156 return;
157 }
158
sun_82077_fd_inb(int port)159 static unsigned char sun_82077_fd_inb(int port)
160 {
161 udelay(5);
162 switch(port & 7) {
163 default:
164 printk("floppy: Asked to read unknown port %d\n", port);
165 panic("floppy: Port bolixed.");
166 case 4: /* FD_STATUS */
167 return sun_fdc->status_82077 & ~STATUS_DMA;
168 case 5: /* FD_DATA */
169 return sun_fdc->data_82077;
170 case 7: /* FD_DIR */
171 /* XXX: Is DCL on 0x80 in sun4m? */
172 return sun_fdc->dir_82077;
173 };
174 panic("sun_82072_fd_inb: How did I get here?");
175 }
176
sun_82077_fd_outb(unsigned char value,int port)177 static void sun_82077_fd_outb(unsigned char value, int port)
178 {
179 udelay(5);
180 switch(port & 7) {
181 default:
182 printk("floppy: Asked to write to unknown port %d\n", port);
183 panic("floppy: Port bolixed.");
184 case 2: /* FD_DOR */
185 /* Happily, the 82077 has a real DOR register. */
186 sun_fdc->dor_82077 = value;
187 break;
188 case 5: /* FD_DATA */
189 sun_fdc->data_82077 = value;
190 break;
191 case 7: /* FD_DCR */
192 sun_fdc->dcr_82077 = value;
193 break;
194 case 4: /* FD_STATUS */
195 sun_fdc->status_82077 = value;
196 break;
197 };
198 return;
199 }
200
201 /* For pseudo-dma (Sun floppy drives have no real DMA available to
202 * them so we must eat the data fifo bytes directly ourselves) we have
203 * three state variables. doing_pdma tells our inline low-level
204 * assembly floppy interrupt entry point whether it should sit and eat
205 * bytes from the fifo or just transfer control up to the higher level
206 * floppy interrupt c-code. I tried very hard but I could not get the
207 * pseudo-dma to work in c-code without getting many overruns and
208 * underruns. If non-zero, doing_pdma encodes the direction of
209 * the transfer for debugging. 1=read 2=write
210 */
211 char *pdma_vaddr;
212 unsigned long pdma_size;
213 volatile int doing_pdma = 0;
214
215 /* This is software state */
216 char *pdma_base = 0;
217 unsigned long pdma_areasize;
218
219 /* Common routines to all controller types on the Sparc. */
sun_fd_disable_dma(void)220 static __inline__ void sun_fd_disable_dma(void)
221 {
222 doing_pdma = 0;
223 if (pdma_base) {
224 mmu_unlockarea(pdma_base, pdma_areasize);
225 pdma_base = 0;
226 }
227 }
228
sun_fd_set_dma_mode(int mode)229 static __inline__ void sun_fd_set_dma_mode(int mode)
230 {
231 switch(mode) {
232 case DMA_MODE_READ:
233 doing_pdma = 1;
234 break;
235 case DMA_MODE_WRITE:
236 doing_pdma = 2;
237 break;
238 default:
239 printk("Unknown dma mode %d\n", mode);
240 panic("floppy: Giving up...");
241 }
242 }
243
sun_fd_set_dma_addr(char * buffer)244 static __inline__ void sun_fd_set_dma_addr(char *buffer)
245 {
246 pdma_vaddr = buffer;
247 }
248
sun_fd_set_dma_count(int length)249 static __inline__ void sun_fd_set_dma_count(int length)
250 {
251 pdma_size = length;
252 }
253
sun_fd_enable_dma(void)254 static __inline__ void sun_fd_enable_dma(void)
255 {
256 pdma_vaddr = mmu_lockarea(pdma_vaddr, pdma_size);
257 pdma_base = pdma_vaddr;
258 pdma_areasize = pdma_size;
259 }
260
261 /* Our low-level entry point in arch/sparc/kernel/entry.S */
262 extern void floppy_hardint(int irq, void *unused, struct pt_regs *regs);
263
sun_fd_request_irq(void)264 static int sun_fd_request_irq(void)
265 {
266 static int once = 0;
267 int error;
268
269 if(!once) {
270 once = 1;
271 error = request_fast_irq(FLOPPY_IRQ, floppy_hardint, SA_INTERRUPT, "floppy");
272 return ((error == 0) ? 0 : -1);
273 } else return 0;
274 }
275
276 static struct linux_prom_registers fd_regs[2];
277
sun_floppy_init(void)278 static int sun_floppy_init(void)
279 {
280 char state[128];
281 int tnode, fd_node, num_regs;
282 struct resource r;
283
284 use_virtual_dma = 1;
285
286 FLOPPY_IRQ = 11;
287 /* Forget it if we aren't on a machine that could possibly
288 * ever have a floppy drive.
289 */
290 if((sparc_cpu_model != sun4c && sparc_cpu_model != sun4m) ||
291 ((idprom->id_machtype == (SM_SUN4C | SM_4C_SLC)) ||
292 (idprom->id_machtype == (SM_SUN4C | SM_4C_ELC)))) {
293 /* We certainly don't have a floppy controller. */
294 goto no_sun_fdc;
295 }
296 /* Well, try to find one. */
297 tnode = prom_getchild(prom_root_node);
298 fd_node = prom_searchsiblings(tnode, "obio");
299 if(fd_node != 0) {
300 tnode = prom_getchild(fd_node);
301 fd_node = prom_searchsiblings(tnode, "SUNW,fdtwo");
302 } else {
303 fd_node = prom_searchsiblings(tnode, "fd");
304 }
305 if(fd_node == 0) {
306 goto no_sun_fdc;
307 }
308
309 /* The sun4m lets us know if the controller is actually usable. */
310 if(sparc_cpu_model == sun4m) {
311 prom_getproperty(fd_node, "status", state, sizeof(state));
312 if(!strcmp(state, "disabled")) {
313 goto no_sun_fdc;
314 }
315 }
316 num_regs = prom_getproperty(fd_node, "reg", (char *) fd_regs, sizeof(fd_regs));
317 num_regs = (num_regs / sizeof(fd_regs[0]));
318 prom_apply_obio_ranges(fd_regs, num_regs);
319 memset(&r, 0, sizeof(r));
320 r.flags = fd_regs[0].which_io;
321 r.start = fd_regs[0].phys_addr;
322 sun_fdc = (struct sun_flpy_controller *)
323 sbus_ioremap(&r, 0, fd_regs[0].reg_size, "floppy");
324
325 /* Last minute sanity check... */
326 if(sun_fdc->status_82072 == 0xff) {
327 sun_fdc = NULL;
328 goto no_sun_fdc;
329 }
330
331 if(sparc_cpu_model == sun4c) {
332 sun_fdops.fd_inb = sun_82072_fd_inb;
333 sun_fdops.fd_outb = sun_82072_fd_outb;
334 fdc_status = &sun_fdc->status_82072;
335 /* printk("AUXIO @0x%lx\n", auxio_register); */ /* P3 */
336 } else {
337 sun_fdops.fd_inb = sun_82077_fd_inb;
338 sun_fdops.fd_outb = sun_82077_fd_outb;
339 fdc_status = &sun_fdc->status_82077;
340 /* printk("DOR @0x%p\n", &sun_fdc->dor_82077); */ /* P3 */
341 }
342
343 /* Success... */
344 allowed_drive_mask = 0x01;
345 return (int) sun_fdc;
346
347 no_sun_fdc:
348 return -1;
349 }
350
sparc_eject(void)351 static int sparc_eject(void)
352 {
353 set_dor(0x00, 0xff, 0x90);
354 udelay(500);
355 set_dor(0x00, 0x6f, 0x00);
356 udelay(500);
357 return 0;
358 }
359
360 #define fd_eject(drive) sparc_eject()
361
362 #define EXTRA_FLOPPY_PARAMS
363
364 #endif /* !(__ASM_SPARC_FLOPPY_H) */
365