blob: c514864cd2e2870b25c211b858b948f4e100cb43 [file] [log] [blame]
Sam Ravnborga439fe52008-07-27 23:00:59 +02001/* asm/floppy.h: Sparc specific parts of the Floppy driver.
2 *
3 * Copyright (C) 1995 David S. Miller (davem@davemloft.net)
4 */
5
6#ifndef __ASM_SPARC_FLOPPY_H
7#define __ASM_SPARC_FLOPPY_H
8
David S. Miller454eeb22008-08-27 04:05:35 -07009#include <linux/of.h>
10#include <linux/of_device.h>
11
Sam Ravnborga439fe52008-07-27 23:00:59 +020012#include <asm/page.h>
13#include <asm/pgtable.h>
Sam Ravnborga439fe52008-07-27 23:00:59 +020014#include <asm/idprom.h>
Sam Ravnborga439fe52008-07-27 23:00:59 +020015#include <asm/oplib.h>
16#include <asm/auxio.h>
17#include <asm/irq.h>
18
19/* We don't need no stinkin' I/O port allocation crap. */
20#undef release_region
21#undef request_region
22#define release_region(X, Y) do { } while(0)
23#define request_region(X, Y, Z) (1)
24
25/* References:
26 * 1) Netbsd Sun floppy driver.
27 * 2) NCR 82077 controller manual
28 * 3) Intel 82077 controller manual
29 */
30struct sun_flpy_controller {
31 volatile unsigned char status_82072; /* Main Status reg. */
32#define dcr_82072 status_82072 /* Digital Control reg. */
33#define status1_82077 status_82072 /* Auxiliary Status reg. 1 */
34
35 volatile unsigned char data_82072; /* Data fifo. */
36#define status2_82077 data_82072 /* Auxiliary Status reg. 2 */
37
38 volatile unsigned char dor_82077; /* Digital Output reg. */
39 volatile unsigned char tapectl_82077; /* What the? Tape control reg? */
40
41 volatile unsigned char status_82077; /* Main Status Register. */
42#define drs_82077 status_82077 /* Digital Rate Select reg. */
43
44 volatile unsigned char data_82077; /* Data fifo. */
45 volatile unsigned char ___unused;
46 volatile unsigned char dir_82077; /* Digital Input reg. */
47#define dcr_82077 dir_82077 /* Config Control reg. */
48};
49
50/* You'll only ever find one controller on a SparcStation anyways. */
51static struct sun_flpy_controller *sun_fdc = NULL;
52extern volatile unsigned char *fdc_status;
53
54struct sun_floppy_ops {
55 unsigned char (*fd_inb)(int port);
56 void (*fd_outb)(unsigned char value, int port);
57};
58
59static struct sun_floppy_ops sun_fdops;
60
61#define fd_inb(port) sun_fdops.fd_inb(port)
62#define fd_outb(value,port) sun_fdops.fd_outb(value,port)
63#define fd_enable_dma() sun_fd_enable_dma()
64#define fd_disable_dma() sun_fd_disable_dma()
65#define fd_request_dma() (0) /* nothing... */
66#define fd_free_dma() /* nothing... */
67#define fd_clear_dma_ff() /* nothing... */
68#define fd_set_dma_mode(mode) sun_fd_set_dma_mode(mode)
69#define fd_set_dma_addr(addr) sun_fd_set_dma_addr(addr)
70#define fd_set_dma_count(count) sun_fd_set_dma_count(count)
71#define fd_enable_irq() /* nothing... */
72#define fd_disable_irq() /* nothing... */
73#define fd_cacheflush(addr, size) /* nothing... */
74#define fd_request_irq() sun_fd_request_irq()
75#define fd_free_irq() /* nothing... */
76#if 0 /* P3: added by Alain, these cause a MMU corruption. 19960524 XXX */
77#define fd_dma_mem_alloc(size) ((unsigned long) vmalloc(size))
78#define fd_dma_mem_free(addr,size) (vfree((void *)(addr)))
79#endif
80
81/* XXX This isn't really correct. XXX */
82#define get_dma_residue(x) (0)
83
84#define FLOPPY0_TYPE 4
85#define FLOPPY1_TYPE 0
86
87/* Super paranoid... */
88#undef HAVE_DISABLE_HLT
89
90/* Here is where we catch the floppy driver trying to initialize,
91 * therefore this is where we call the PROM device tree probing
92 * routine etc. on the Sparc.
93 */
94#define FDC1 sun_floppy_init()
95
96#define N_FDC 1
97#define N_DRIVE 8
98
99/* No 64k boundary crossing problems on the Sparc. */
100#define CROSS_64KB(a,s) (0)
101
102/* Routines unique to each controller type on a Sun. */
103static void sun_set_dor(unsigned char value, int fdc_82077)
104{
David S. Miller0301a6c2012-05-11 21:39:48 -0700105 if (fdc_82077)
Sam Ravnborga439fe52008-07-27 23:00:59 +0200106 sun_fdc->dor_82077 = value;
Sam Ravnborga439fe52008-07-27 23:00:59 +0200107}
108
109static unsigned char sun_read_dir(void)
110{
David S. Miller0301a6c2012-05-11 21:39:48 -0700111 return sun_fdc->dir_82077;
Sam Ravnborga439fe52008-07-27 23:00:59 +0200112}
113
114static unsigned char sun_82072_fd_inb(int port)
115{
116 udelay(5);
117 switch(port & 7) {
118 default:
119 printk("floppy: Asked to read unknown port %d\n", port);
120 panic("floppy: Port bolixed.");
121 case 4: /* FD_STATUS */
122 return sun_fdc->status_82072 & ~STATUS_DMA;
123 case 5: /* FD_DATA */
124 return sun_fdc->data_82072;
125 case 7: /* FD_DIR */
126 return sun_read_dir();
Joe Perches6cb79b32011-06-03 14:45:23 +0000127 }
Sam Ravnborga439fe52008-07-27 23:00:59 +0200128 panic("sun_82072_fd_inb: How did I get here?");
129}
130
131static void sun_82072_fd_outb(unsigned char value, int port)
132{
133 udelay(5);
134 switch(port & 7) {
135 default:
136 printk("floppy: Asked to write to unknown port %d\n", port);
137 panic("floppy: Port bolixed.");
138 case 2: /* FD_DOR */
139 sun_set_dor(value, 0);
140 break;
141 case 5: /* FD_DATA */
142 sun_fdc->data_82072 = value;
143 break;
144 case 7: /* FD_DCR */
145 sun_fdc->dcr_82072 = value;
146 break;
147 case 4: /* FD_STATUS */
148 sun_fdc->status_82072 = value;
149 break;
Joe Perches6cb79b32011-06-03 14:45:23 +0000150 }
Sam Ravnborga439fe52008-07-27 23:00:59 +0200151 return;
152}
153
154static unsigned char sun_82077_fd_inb(int port)
155{
156 udelay(5);
157 switch(port & 7) {
158 default:
159 printk("floppy: Asked to read unknown port %d\n", port);
160 panic("floppy: Port bolixed.");
161 case 0: /* FD_STATUS_0 */
162 return sun_fdc->status1_82077;
163 case 1: /* FD_STATUS_1 */
164 return sun_fdc->status2_82077;
165 case 2: /* FD_DOR */
166 return sun_fdc->dor_82077;
167 case 3: /* FD_TDR */
168 return sun_fdc->tapectl_82077;
169 case 4: /* FD_STATUS */
170 return sun_fdc->status_82077 & ~STATUS_DMA;
171 case 5: /* FD_DATA */
172 return sun_fdc->data_82077;
173 case 7: /* FD_DIR */
174 return sun_read_dir();
Joe Perches6cb79b32011-06-03 14:45:23 +0000175 }
Sam Ravnborga439fe52008-07-27 23:00:59 +0200176 panic("sun_82077_fd_inb: How did I get here?");
177}
178
179static void sun_82077_fd_outb(unsigned char value, int port)
180{
181 udelay(5);
182 switch(port & 7) {
183 default:
184 printk("floppy: Asked to write to unknown port %d\n", port);
185 panic("floppy: Port bolixed.");
186 case 2: /* FD_DOR */
187 sun_set_dor(value, 1);
188 break;
189 case 5: /* FD_DATA */
190 sun_fdc->data_82077 = value;
191 break;
192 case 7: /* FD_DCR */
193 sun_fdc->dcr_82077 = value;
194 break;
195 case 4: /* FD_STATUS */
196 sun_fdc->status_82077 = value;
197 break;
198 case 3: /* FD_TDR */
199 sun_fdc->tapectl_82077 = value;
200 break;
Joe Perches6cb79b32011-06-03 14:45:23 +0000201 }
Sam Ravnborga439fe52008-07-27 23:00:59 +0200202 return;
203}
204
205/* For pseudo-dma (Sun floppy drives have no real DMA available to
206 * them so we must eat the data fifo bytes directly ourselves) we have
207 * three state variables. doing_pdma tells our inline low-level
208 * assembly floppy interrupt entry point whether it should sit and eat
209 * bytes from the fifo or just transfer control up to the higher level
210 * floppy interrupt c-code. I tried very hard but I could not get the
211 * pseudo-dma to work in c-code without getting many overruns and
212 * underruns. If non-zero, doing_pdma encodes the direction of
213 * the transfer for debugging. 1=read 2=write
214 */
215extern char *pdma_vaddr;
216extern unsigned long pdma_size;
217extern volatile int doing_pdma;
218
219/* This is software state */
220extern char *pdma_base;
221extern unsigned long pdma_areasize;
222
223/* Common routines to all controller types on the Sparc. */
224static inline void virtual_dma_init(void)
225{
226 /* nothing... */
227}
228
229static inline void sun_fd_disable_dma(void)
230{
231 doing_pdma = 0;
232 if (pdma_base) {
233 mmu_unlockarea(pdma_base, pdma_areasize);
234 pdma_base = NULL;
235 }
236}
237
238static inline void sun_fd_set_dma_mode(int mode)
239{
240 switch(mode) {
241 case DMA_MODE_READ:
242 doing_pdma = 1;
243 break;
244 case DMA_MODE_WRITE:
245 doing_pdma = 2;
246 break;
247 default:
248 printk("Unknown dma mode %d\n", mode);
249 panic("floppy: Giving up...");
250 }
251}
252
253static inline void sun_fd_set_dma_addr(char *buffer)
254{
255 pdma_vaddr = buffer;
256}
257
258static inline void sun_fd_set_dma_count(int length)
259{
260 pdma_size = length;
261}
262
263static inline void sun_fd_enable_dma(void)
264{
265 pdma_vaddr = mmu_lockarea(pdma_vaddr, pdma_size);
266 pdma_base = pdma_vaddr;
267 pdma_areasize = pdma_size;
268}
269
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000270extern int sparc_floppy_request_irq(unsigned int irq,
271 irq_handler_t irq_handler);
Sam Ravnborga439fe52008-07-27 23:00:59 +0200272
273static int sun_fd_request_irq(void)
274{
275 static int once = 0;
Sam Ravnborga439fe52008-07-27 23:00:59 +0200276
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000277 if (!once) {
Sam Ravnborga439fe52008-07-27 23:00:59 +0200278 once = 1;
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000279 return sparc_floppy_request_irq(FLOPPY_IRQ, floppy_interrupt);
280 } else {
281 return 0;
282 }
Sam Ravnborga439fe52008-07-27 23:00:59 +0200283}
284
285static struct linux_prom_registers fd_regs[2];
286
287static int sun_floppy_init(void)
288{
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000289 struct platform_device *op;
290 struct device_node *dp;
Sam Ravnborga439fe52008-07-27 23:00:59 +0200291 struct resource r;
Sam Ravnborg144e9882012-05-12 08:04:10 +0000292 char state[128];
293 phandle fd_node;
294 phandle tnode;
295 int num_regs;
Sam Ravnborga439fe52008-07-27 23:00:59 +0200296
297 use_virtual_dma = 1;
298
Sam Ravnborga439fe52008-07-27 23:00:59 +0200299 /* Forget it if we aren't on a machine that could possibly
300 * ever have a floppy drive.
301 */
David S. Miller58fa4dc2012-05-11 20:45:18 -0700302 if (sparc_cpu_model != sun4m) {
Sam Ravnborga439fe52008-07-27 23:00:59 +0200303 /* We certainly don't have a floppy controller. */
304 goto no_sun_fdc;
305 }
306 /* Well, try to find one. */
307 tnode = prom_getchild(prom_root_node);
308 fd_node = prom_searchsiblings(tnode, "obio");
Sam Ravnborg144e9882012-05-12 08:04:10 +0000309 if (fd_node != 0) {
Sam Ravnborga439fe52008-07-27 23:00:59 +0200310 tnode = prom_getchild(fd_node);
311 fd_node = prom_searchsiblings(tnode, "SUNW,fdtwo");
312 } else {
313 fd_node = prom_searchsiblings(tnode, "fd");
314 }
Sam Ravnborg144e9882012-05-12 08:04:10 +0000315 if (fd_node == 0) {
Sam Ravnborga439fe52008-07-27 23:00:59 +0200316 goto no_sun_fdc;
317 }
318
319 /* The sun4m lets us know if the controller is actually usable. */
Sam Ravnborg144e9882012-05-12 08:04:10 +0000320 if (prom_getproperty(fd_node, "status", state, sizeof(state)) != -1) {
Sam Ravnborga439fe52008-07-27 23:00:59 +0200321 if(!strcmp(state, "disabled")) {
322 goto no_sun_fdc;
323 }
324 }
325 num_regs = prom_getproperty(fd_node, "reg", (char *) fd_regs, sizeof(fd_regs));
326 num_regs = (num_regs / sizeof(fd_regs[0]));
327 prom_apply_obio_ranges(fd_regs, num_regs);
328 memset(&r, 0, sizeof(r));
329 r.flags = fd_regs[0].which_io;
330 r.start = fd_regs[0].phys_addr;
Sam Ravnborg144e9882012-05-12 08:04:10 +0000331 sun_fdc = of_ioremap(&r, 0, fd_regs[0].reg_size, "floppy");
Sam Ravnborga439fe52008-07-27 23:00:59 +0200332
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000333 /* Look up irq in platform_device.
334 * We try "SUNW,fdtwo" and "fd"
335 */
Sam Ravnborg918f8322012-05-12 08:04:09 +0000336 op = NULL;
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000337 for_each_node_by_name(dp, "SUNW,fdtwo") {
338 op = of_find_device_by_node(dp);
339 if (op)
340 break;
341 }
342 if (!op) {
343 for_each_node_by_name(dp, "fd") {
344 op = of_find_device_by_node(dp);
345 if (op)
346 break;
347 }
348 }
349 if (!op)
350 goto no_sun_fdc;
351
352 FLOPPY_IRQ = op->archdata.irqs[0];
353
Sam Ravnborga439fe52008-07-27 23:00:59 +0200354 /* Last minute sanity check... */
Sam Ravnborg144e9882012-05-12 08:04:10 +0000355 if (sun_fdc->status_82072 == 0xff) {
Sam Ravnborga439fe52008-07-27 23:00:59 +0200356 sun_fdc = NULL;
357 goto no_sun_fdc;
358 }
359
360 sun_fdops.fd_inb = sun_82077_fd_inb;
361 sun_fdops.fd_outb = sun_82077_fd_outb;
362 fdc_status = &sun_fdc->status_82077;
363
364 if (sun_fdc->dor_82077 == 0x80) {
365 sun_fdc->dor_82077 = 0x02;
366 if (sun_fdc->dor_82077 == 0x80) {
367 sun_fdops.fd_inb = sun_82072_fd_inb;
368 sun_fdops.fd_outb = sun_82072_fd_outb;
369 fdc_status = &sun_fdc->status_82072;
370 }
371 }
372
373 /* Success... */
374 allowed_drive_mask = 0x01;
375 return (int) sun_fdc;
376
377no_sun_fdc:
378 return -1;
379}
380
381static int sparc_eject(void)
382{
383 set_dor(0x00, 0xff, 0x90);
384 udelay(500);
385 set_dor(0x00, 0x6f, 0x00);
386 udelay(500);
387 return 0;
388}
389
390#define fd_eject(drive) sparc_eject()
391
392#define EXTRA_FLOPPY_PARAMS
393
David S. Miller334ae612008-08-27 17:01:57 -0700394static DEFINE_SPINLOCK(dma_spin_lock);
395
396#define claim_dma_lock() \
397({ unsigned long flags; \
398 spin_lock_irqsave(&dma_spin_lock, flags); \
399 flags; \
400})
401
402#define release_dma_lock(__flags) \
403 spin_unlock_irqrestore(&dma_spin_lock, __flags);
404
Sam Ravnborga439fe52008-07-27 23:00:59 +0200405#endif /* !(__ASM_SPARC_FLOPPY_H) */