1 /* 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 9 #include <linux/of.h> 10 #include <linux/of_device.h> 11 12 #include <asm/pgtable.h> 13 #include <asm/idprom.h> 14 #include <asm/oplib.h> 15 #include <asm/auxio.h> 16 #include <asm/setup.h> 17 #include <asm/page.h> 18 #include <asm/irq.h> 19 20 /* We don't need no stinkin' I/O port allocation crap. */ 21 #undef release_region 22 #undef request_region 23 #define release_region(X, Y) do { } while(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 54 struct sun_floppy_ops { 55 unsigned char (*fd_inb)(int port); 56 void (*fd_outb)(unsigned char value, int port); 57 }; 58 59 static 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_request_irq() sun_fd_request_irq() 74 #define fd_free_irq() /* nothing... */ 75 #if 0 /* P3: added by Alain, these cause a MMU corruption. 19960524 XXX */ 76 #define fd_dma_mem_alloc(size) ((unsigned long) vmalloc(size)) 77 #define fd_dma_mem_free(addr,size) (vfree((void *)(addr))) 78 #endif 79 80 /* XXX This isn't really correct. XXX */ 81 #define get_dma_residue(x) (0) 82 83 #define FLOPPY0_TYPE 4 84 #define FLOPPY1_TYPE 0 85 86 /* Super paranoid... */ 87 #undef HAVE_DISABLE_HLT 88 89 /* Here is where we catch the floppy driver trying to initialize, 90 * therefore this is where we call the PROM device tree probing 91 * routine etc. on the Sparc. 92 */ 93 #define FDC1 sun_floppy_init() 94 95 #define N_FDC 1 96 #define N_DRIVE 8 97 98 /* No 64k boundary crossing problems on the Sparc. */ 99 #define CROSS_64KB(a,s) (0) 100 101 /* Routines unique to each controller type on a Sun. */ 102 static void sun_set_dor(unsigned char value, int fdc_82077) 103 { 104 if (fdc_82077) 105 sun_fdc->dor_82077 = value; 106 } 107 108 static unsigned char sun_read_dir(void) 109 { 110 return sun_fdc->dir_82077; 111 } 112 113 static unsigned char sun_82072_fd_inb(int port) 114 { 115 udelay(5); 116 switch(port & 7) { 117 default: 118 printk("floppy: Asked to read unknown port %d\n", port); 119 panic("floppy: Port bolixed."); 120 case 4: /* FD_STATUS */ 121 return sun_fdc->status_82072 & ~STATUS_DMA; 122 case 5: /* FD_DATA */ 123 return sun_fdc->data_82072; 124 case 7: /* FD_DIR */ 125 return sun_read_dir(); 126 } 127 panic("sun_82072_fd_inb: How did I get here?"); 128 } 129 130 static void sun_82072_fd_outb(unsigned char value, int port) 131 { 132 udelay(5); 133 switch(port & 7) { 134 default: 135 printk("floppy: Asked to write to unknown port %d\n", port); 136 panic("floppy: Port bolixed."); 137 case 2: /* FD_DOR */ 138 sun_set_dor(value, 0); 139 break; 140 case 5: /* FD_DATA */ 141 sun_fdc->data_82072 = value; 142 break; 143 case 7: /* FD_DCR */ 144 sun_fdc->dcr_82072 = value; 145 break; 146 case 4: /* FD_STATUS */ 147 sun_fdc->status_82072 = value; 148 break; 149 } 150 return; 151 } 152 153 static unsigned char sun_82077_fd_inb(int port) 154 { 155 udelay(5); 156 switch(port & 7) { 157 default: 158 printk("floppy: Asked to read unknown port %d\n", port); 159 panic("floppy: Port bolixed."); 160 case 0: /* FD_STATUS_0 */ 161 return sun_fdc->status1_82077; 162 case 1: /* FD_STATUS_1 */ 163 return sun_fdc->status2_82077; 164 case 2: /* FD_DOR */ 165 return sun_fdc->dor_82077; 166 case 3: /* FD_TDR */ 167 return sun_fdc->tapectl_82077; 168 case 4: /* FD_STATUS */ 169 return sun_fdc->status_82077 & ~STATUS_DMA; 170 case 5: /* FD_DATA */ 171 return sun_fdc->data_82077; 172 case 7: /* FD_DIR */ 173 return sun_read_dir(); 174 } 175 panic("sun_82077_fd_inb: How did I get here?"); 176 } 177 178 static void sun_82077_fd_outb(unsigned char value, int port) 179 { 180 udelay(5); 181 switch(port & 7) { 182 default: 183 printk("floppy: Asked to write to unknown port %d\n", port); 184 panic("floppy: Port bolixed."); 185 case 2: /* FD_DOR */ 186 sun_set_dor(value, 1); 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 case 3: /* FD_TDR */ 198 sun_fdc->tapectl_82077 = value; 199 break; 200 } 201 return; 202 } 203 204 /* For pseudo-dma (Sun floppy drives have no real DMA available to 205 * them so we must eat the data fifo bytes directly ourselves) we have 206 * three state variables. doing_pdma tells our inline low-level 207 * assembly floppy interrupt entry point whether it should sit and eat 208 * bytes from the fifo or just transfer control up to the higher level 209 * floppy interrupt c-code. I tried very hard but I could not get the 210 * pseudo-dma to work in c-code without getting many overruns and 211 * underruns. If non-zero, doing_pdma encodes the direction of 212 * the transfer for debugging. 1=read 2=write 213 */ 214 215 /* Common routines to all controller types on the Sparc. */ 216 static inline void virtual_dma_init(void) 217 { 218 /* nothing... */ 219 } 220 221 static inline void sun_fd_disable_dma(void) 222 { 223 doing_pdma = 0; 224 pdma_base = NULL; 225 } 226 227 static inline void sun_fd_set_dma_mode(int mode) 228 { 229 switch(mode) { 230 case DMA_MODE_READ: 231 doing_pdma = 1; 232 break; 233 case DMA_MODE_WRITE: 234 doing_pdma = 2; 235 break; 236 default: 237 printk("Unknown dma mode %d\n", mode); 238 panic("floppy: Giving up..."); 239 } 240 } 241 242 static inline void sun_fd_set_dma_addr(char *buffer) 243 { 244 pdma_vaddr = buffer; 245 } 246 247 static inline void sun_fd_set_dma_count(int length) 248 { 249 pdma_size = length; 250 } 251 252 static inline void sun_fd_enable_dma(void) 253 { 254 pdma_base = pdma_vaddr; 255 pdma_areasize = pdma_size; 256 } 257 258 int sparc_floppy_request_irq(unsigned int irq, irq_handler_t irq_handler); 259 260 static int sun_fd_request_irq(void) 261 { 262 static int once = 0; 263 264 if (!once) { 265 once = 1; 266 return sparc_floppy_request_irq(FLOPPY_IRQ, floppy_interrupt); 267 } else { 268 return 0; 269 } 270 } 271 272 static struct linux_prom_registers fd_regs[2]; 273 274 static int sun_floppy_init(void) 275 { 276 struct platform_device *op; 277 struct device_node *dp; 278 struct resource r; 279 char state[128]; 280 phandle fd_node; 281 phandle tnode; 282 int num_regs; 283 284 use_virtual_dma = 1; 285 286 /* Forget it if we aren't on a machine that could possibly 287 * ever have a floppy drive. 288 */ 289 if (sparc_cpu_model != sun4m) { 290 /* We certainly don't have a floppy controller. */ 291 goto no_sun_fdc; 292 } 293 /* Well, try to find one. */ 294 tnode = prom_getchild(prom_root_node); 295 fd_node = prom_searchsiblings(tnode, "obio"); 296 if (fd_node != 0) { 297 tnode = prom_getchild(fd_node); 298 fd_node = prom_searchsiblings(tnode, "SUNW,fdtwo"); 299 } else { 300 fd_node = prom_searchsiblings(tnode, "fd"); 301 } 302 if (fd_node == 0) { 303 goto no_sun_fdc; 304 } 305 306 /* The sun4m lets us know if the controller is actually usable. */ 307 if (prom_getproperty(fd_node, "status", state, sizeof(state)) != -1) { 308 if(!strcmp(state, "disabled")) { 309 goto no_sun_fdc; 310 } 311 } 312 num_regs = prom_getproperty(fd_node, "reg", (char *) fd_regs, sizeof(fd_regs)); 313 num_regs = (num_regs / sizeof(fd_regs[0])); 314 prom_apply_obio_ranges(fd_regs, num_regs); 315 memset(&r, 0, sizeof(r)); 316 r.flags = fd_regs[0].which_io; 317 r.start = fd_regs[0].phys_addr; 318 sun_fdc = of_ioremap(&r, 0, fd_regs[0].reg_size, "floppy"); 319 320 /* Look up irq in platform_device. 321 * We try "SUNW,fdtwo" and "fd" 322 */ 323 op = NULL; 324 for_each_node_by_name(dp, "SUNW,fdtwo") { 325 op = of_find_device_by_node(dp); 326 if (op) 327 break; 328 } 329 if (!op) { 330 for_each_node_by_name(dp, "fd") { 331 op = of_find_device_by_node(dp); 332 if (op) 333 break; 334 } 335 } 336 if (!op) 337 goto no_sun_fdc; 338 339 FLOPPY_IRQ = op->archdata.irqs[0]; 340 341 /* Last minute sanity check... */ 342 if (sun_fdc->status_82072 == 0xff) { 343 sun_fdc = NULL; 344 goto no_sun_fdc; 345 } 346 347 sun_fdops.fd_inb = sun_82077_fd_inb; 348 sun_fdops.fd_outb = sun_82077_fd_outb; 349 fdc_status = &sun_fdc->status_82077; 350 351 if (sun_fdc->dor_82077 == 0x80) { 352 sun_fdc->dor_82077 = 0x02; 353 if (sun_fdc->dor_82077 == 0x80) { 354 sun_fdops.fd_inb = sun_82072_fd_inb; 355 sun_fdops.fd_outb = sun_82072_fd_outb; 356 fdc_status = &sun_fdc->status_82072; 357 } 358 } 359 360 /* Success... */ 361 allowed_drive_mask = 0x01; 362 return (int) sun_fdc; 363 364 no_sun_fdc: 365 return -1; 366 } 367 368 static int sparc_eject(void) 369 { 370 set_dor(0x00, 0xff, 0x90); 371 udelay(500); 372 set_dor(0x00, 0x6f, 0x00); 373 udelay(500); 374 return 0; 375 } 376 377 #define fd_eject(drive) sparc_eject() 378 379 #define EXTRA_FLOPPY_PARAMS 380 381 static DEFINE_SPINLOCK(dma_spin_lock); 382 383 #define claim_dma_lock() \ 384 ({ unsigned long flags; \ 385 spin_lock_irqsave(&dma_spin_lock, flags); \ 386 flags; \ 387 }) 388 389 #define release_dma_lock(__flags) \ 390 spin_unlock_irqrestore(&dma_spin_lock, __flags); 391 392 #endif /* !(__ASM_SPARC_FLOPPY_H) */ 393