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