📄 ide-cris.c
字号:
return intr & (1 << (bus + IO_BITNR(R_IRQ_MASK0_RD, ata_irq0))) ? 1 : 0;}static void cris_ide_initialize_dma(int dir){ if (dir) { RESET_DMA(ATA_RX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */ WAIT_DMA(ATA_RX_DMA_NBR); } else { RESET_DMA(ATA_TX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */ WAIT_DMA(ATA_TX_DMA_NBR); }}#endifvoidcris_ide_outw(unsigned short data, unsigned long reg) { int timeleft; LOWDB(printk("ow: data 0x%x, reg 0x%x\n", data, reg)); /* note the lack of handling any timeouts. we stop waiting, but we don't * really notify anybody. */ timeleft = IDE_REGISTER_TIMEOUT; /* wait for busy flag */ do { timeleft--; } while(timeleft && cris_ide_busy()); /* * Fall through at a timeout, so the ongoing command will be * aborted by the write below, which is expected to be a dummy * command to the command register. This happens when a faulty * drive times out on a command. See comment on timeout in * INB. */ if(!timeleft) printk("ATA timeout reg 0x%lx := 0x%x\n", reg, data); cris_ide_write_command(reg|data); /* write data to the drive's register */ timeleft = IDE_REGISTER_TIMEOUT; /* wait for transmitter ready */ do { timeleft--; } while(timeleft && !cris_ide_ready());}voidcris_ide_outb(unsigned char data, unsigned long reg){ cris_ide_outw(data, reg);}voidcris_ide_outbsync(ide_drive_t *drive, u8 addr, unsigned long port){ cris_ide_outw(addr, port);}unsigned shortcris_ide_inw(unsigned long reg) { int timeleft; unsigned short val; timeleft = IDE_REGISTER_TIMEOUT; /* wait for busy flag */ do { timeleft--; } while(timeleft && cris_ide_busy()); if(!timeleft) { /* * If we're asked to read the status register, like for * example when a command does not complete for an * extended time, but the ATA interface is stuck in a * busy state at the *ETRAX* ATA interface level (as has * happened repeatedly with at least one bad disk), then * the best thing to do is to pretend that we read * "busy" in the status register, so the IDE driver will * time-out, abort the ongoing command and perform a * reset sequence. Note that the subsequent OUT_BYTE * call will also timeout on busy, but as long as the * write is still performed, everything will be fine. */ if (cris_ide_get_reg(reg) == IDE_STATUS_OFFSET) return BUSY_STAT; else /* For other rare cases we assume 0 is good enough. */ return 0; } cris_ide_write_command(reg | cris_pio_read); timeleft = IDE_REGISTER_TIMEOUT; /* wait for available */ do { timeleft--; } while(timeleft && !cris_ide_data_available(&val)); if(!timeleft) return 0; LOWDB(printk("inb: 0x%x from reg 0x%x\n", val & 0xff, reg)); return val;}unsigned charcris_ide_inb(unsigned long reg){ return (unsigned char)cris_ide_inw(reg);}static int cris_dma_check (ide_drive_t *drive);static int cris_dma_end (ide_drive_t *drive);static int cris_dma_setup (ide_drive_t *drive);static void cris_dma_exec_cmd (ide_drive_t *drive, u8 command);static int cris_dma_test_irq(ide_drive_t *drive);static void cris_dma_start(ide_drive_t *drive);static void cris_ide_input_data (ide_drive_t *drive, void *, unsigned int);static void cris_ide_output_data (ide_drive_t *drive, void *, unsigned int);static void cris_atapi_input_bytes(ide_drive_t *drive, void *, unsigned int);static void cris_atapi_output_bytes(ide_drive_t *drive, void *, unsigned int);static int cris_dma_off (ide_drive_t *drive);static int cris_dma_on (ide_drive_t *drive);static void tune_cris_ide(ide_drive_t *drive, u8 pio){ int setup, strobe, hold; switch(pio) { case 0: setup = ATA_PIO0_SETUP; strobe = ATA_PIO0_STROBE; hold = ATA_PIO0_HOLD; break; case 1: setup = ATA_PIO1_SETUP; strobe = ATA_PIO1_STROBE; hold = ATA_PIO1_HOLD; break; case 2: setup = ATA_PIO2_SETUP; strobe = ATA_PIO2_STROBE; hold = ATA_PIO2_HOLD; break; case 3: setup = ATA_PIO3_SETUP; strobe = ATA_PIO3_STROBE; hold = ATA_PIO3_HOLD; break; case 4: setup = ATA_PIO4_SETUP; strobe = ATA_PIO4_STROBE; hold = ATA_PIO4_HOLD; break; default: return; } cris_ide_set_speed(TYPE_PIO, setup, strobe, hold);}static int speed_cris_ide(ide_drive_t *drive, u8 speed){ int cyc = 0, dvs = 0, strobe = 0, hold = 0; if (speed >= XFER_PIO_0 && speed <= XFER_PIO_4) { tune_cris_ide(drive, speed - XFER_PIO_0); return 0; } switch(speed) { case XFER_UDMA_0: cyc = ATA_UDMA0_CYC; dvs = ATA_UDMA0_DVS; break; case XFER_UDMA_1: cyc = ATA_UDMA1_CYC; dvs = ATA_UDMA1_DVS; break; case XFER_UDMA_2: cyc = ATA_UDMA2_CYC; dvs = ATA_UDMA2_DVS; break; case XFER_MW_DMA_0: strobe = ATA_DMA0_STROBE; hold = ATA_DMA0_HOLD; break; case XFER_MW_DMA_1: strobe = ATA_DMA1_STROBE; hold = ATA_DMA1_HOLD; break; case XFER_MW_DMA_2: strobe = ATA_DMA2_STROBE; hold = ATA_DMA2_HOLD; break; default: return 0; } if (speed >= XFER_UDMA_0) cris_ide_set_speed(TYPE_UDMA, cyc, dvs, 0); else cris_ide_set_speed(TYPE_DMA, 0, strobe, hold); return 0;}void __initinit_e100_ide (void){ hw_regs_t hw; int ide_offsets[IDE_NR_PORTS]; int h; int i; printk("ide: ETRAX FS built-in ATA DMA controller\n"); for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) ide_offsets[i] = cris_ide_reg_addr(i, 0, 1); /* the IDE control register is at ATA address 6, with CS1 active instead of CS0 */ ide_offsets[IDE_CONTROL_OFFSET] = cris_ide_reg_addr(6, 1, 0); /* first fill in some stuff in the ide_hwifs fields */ for(h = 0; h < MAX_HWIFS; h++) { ide_hwif_t *hwif = &ide_hwifs[h]; ide_setup_ports(&hw, cris_ide_base_address(h), ide_offsets, 0, 0, cris_ide_ack_intr, ide_default_irq(0)); ide_register_hw(&hw, &hwif); hwif->mmio = 2; hwif->chipset = ide_etrax100; hwif->tuneproc = &tune_cris_ide; hwif->speedproc = &speed_cris_ide; hwif->ata_input_data = &cris_ide_input_data; hwif->ata_output_data = &cris_ide_output_data; hwif->atapi_input_bytes = &cris_atapi_input_bytes; hwif->atapi_output_bytes = &cris_atapi_output_bytes; hwif->ide_dma_check = &cris_dma_check; hwif->ide_dma_end = &cris_dma_end; hwif->dma_setup = &cris_dma_setup; hwif->dma_exec_cmd = &cris_dma_exec_cmd; hwif->ide_dma_test_irq = &cris_dma_test_irq; hwif->dma_start = &cris_dma_start; hwif->OUTB = &cris_ide_outb; hwif->OUTW = &cris_ide_outw; hwif->OUTBSYNC = &cris_ide_outbsync; hwif->INB = &cris_ide_inb; hwif->INW = &cris_ide_inw; hwif->ide_dma_host_off = &cris_dma_off; hwif->ide_dma_host_on = &cris_dma_on; hwif->ide_dma_off_quietly = &cris_dma_off; hwif->udma_four = 0; hwif->ultra_mask = cris_ultra_mask; hwif->mwdma_mask = 0x07; /* Multiword DMA 0-2 */ hwif->swdma_mask = 0x07; /* Singleword DMA 0-2 */ } /* Reset pulse */ cris_ide_reset(0); udelay(25); cris_ide_reset(1); cris_ide_init(); cris_ide_set_speed(TYPE_PIO, ATA_PIO4_SETUP, ATA_PIO4_STROBE, ATA_PIO4_HOLD); cris_ide_set_speed(TYPE_DMA, 0, ATA_DMA2_STROBE, ATA_DMA2_HOLD); cris_ide_set_speed(TYPE_UDMA, ATA_UDMA2_CYC, ATA_UDMA2_DVS, 0);}static int cris_dma_off (ide_drive_t *drive){ return 0;}static int cris_dma_on (ide_drive_t *drive){ return 0;}static cris_dma_descr_type mydescr __attribute__ ((__aligned__(16)));/* * The following routines are mainly used by the ATAPI drivers. * * These routines will round up any request for an odd number of bytes, * so if an odd bytecount is specified, be sure that there's at least one * extra byte allocated for the buffer. */static voidcris_atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount){ D(printk("atapi_input_bytes, buffer 0x%x, count %d\n", buffer, bytecount)); if(bytecount & 1) { printk("warning, odd bytecount in cdrom_in_bytes = %d.\n", bytecount); bytecount++; /* to round off */ } /* setup DMA and start transfer */ cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1); cris_ide_start_dma(drive, &mydescr, 1, TYPE_PIO, bytecount); /* wait for completion */ LED_DISK_READ(1); cris_ide_wait_dma(1); LED_DISK_READ(0);}static voidcris_atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount){ D(printk("atapi_output_bytes, buffer 0x%x, count %d\n", buffer, bytecount)); if(bytecount & 1) { printk("odd bytecount %d in atapi_out_bytes!\n", bytecount); bytecount++; } cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1); cris_ide_start_dma(drive, &mydescr, 0, TYPE_PIO, bytecount); /* wait for completion */ LED_DISK_WRITE(1); LED_DISK_READ(1); cris_ide_wait_dma(0); LED_DISK_WRITE(0);}/* * This is used for most PIO data transfers *from* the IDE interface */static voidcris_ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount){ cris_atapi_input_bytes(drive, buffer, wcount << 2);}/* * This is used for most PIO data transfers *to* the IDE interface */static voidcris_ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount){ cris_atapi_output_bytes(drive, buffer, wcount << 2);}/* we only have one DMA channel on the chip for ATA, so we can keep these statically */static cris_dma_descr_type ata_descrs[MAX_DMA_DESCRS] __attribute__ ((__aligned__(16)));static unsigned int ata_tot_size;/* * cris_ide_build_dmatable() prepares a dma request. * Returns 0 if all went okay, returns 1 otherwise. */static int cris_ide_build_dmatable (ide_drive_t *drive){ ide_hwif_t *hwif = drive->hwif; struct scatterlist* sg; struct request *rq = drive->hwif->hwgroup->rq; unsigned long size, addr; unsigned int count = 0; int i = 0; sg = hwif->sg_table; ata_tot_size = 0; ide_map_sg(drive, rq); i = hwif->sg_nents; while(i) { /* * Determine addr and size of next buffer area. We assume that * individual virtual buffers are always composed linearly in * physical memory. For example, we assume that any 8kB buffer * is always composed of two adjacent physical 4kB pages rather * than two possibly non-adjacent physical 4kB pages. */ /* group sequential buffers into one large buffer */ addr = page_to_phys(sg->page) + sg->offset; size = sg_dma_len(sg); while (sg++, --i) { if ((addr + size) != page_to_phys(sg->page) + sg->offset) break; size += sg_dma_len(sg); } /* did we run out of descriptors? */ if(count >= MAX_DMA_DESCRS) { printk("%s: too few DMA descriptors\n", drive->name); return 1; } /* however, this case is more difficult - rw_trf_cnt cannot be more than 65536 words per transfer, so in that case we need to either 1) use a DMA interrupt to re-trigger rw_trf_cnt and continue with the descriptors, or 2) simply do the request here, and get dma_intr to only ide_end_request on those blocks that were actually set-up for transfer. */ if(ata_tot_size + size > 131072) { printk("too large total ATA DMA request, %d + %d!\n", ata_tot_size, (int)size); return 1; } /* If size > MAX_DESCR_SIZE it has to be splitted into new descriptors. Since we don't handle size > 131072 only one split is necessary */ if(size > MAX_DESCR_SIZE) { cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, MAX_DESCR_SIZE, 0); count++; ata_tot_size += MAX_DESCR_SIZE; size -= MAX_DESCR_SIZE; addr += MAX_DESCR_SIZE; } cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, size,i ? 0 : 1); count++; ata_tot_size += size; } if (count) { /* return and say all is ok */ return 0; } printk("%s: empty DMA table?\n", drive->name); return 1; /* let the PIO routines handle this weirdness */}static int cris_config_drive_for_dma (ide_drive_t *drive){ u8 speed = ide_dma_speed(drive, 1); if (!speed) return 0; speed_cris_ide(drive, speed); ide_config_drive_speed(drive, speed); return ide_dma_enable(drive);}/* * cris_dma_intr() is the handler for disk read/write DMA interrupts */static ide_startstop_t cris_dma_intr (ide_drive_t *drive){ LED_DISK_READ(0); LED_DISK_WRITE(0); return ide_dma_intr(drive);}/* * Functions below initiates/aborts DMA read/write operations on a drive. * * The caller is assumed to have selected the drive and programmed the drive's * sector address using CHS or LBA. All that remains is to prepare for DMA * and then issue the actual read/write DMA/PIO command to the drive. * * For ATAPI devices, we just prepare for DMA and return. The caller should * then issue the packet command to the drive and call us again with * cris_dma_start afterwards. * * Returns 0 if all went well. * Returns 1 if DMA read/write could not be started, in which case * the caller should revert to PIO for the current request. */static int cris_dma_check(ide_drive_t *drive){ ide_hwif_t *hwif = drive->hwif; struct hd_driveid* id = drive->id; if (id && (id->capability & 1)) { if (ide_use_dma(drive)) { if (cris_config_drive_for_dma(drive)) return hwif->ide_dma_on(drive); } } return hwif->ide_dma_off_quietly(drive);}static int cris_dma_end(ide_drive_t *drive){ drive->waiting_for_dma = 0; return 0;}static int cris_dma_setup(ide_drive_t *drive){ struct request *rq = drive->hwif->hwgroup->rq; cris_ide_initialize_dma(!rq_data_dir(rq)); if (cris_ide_build_dmatable (drive)) { ide_map_sg(drive, rq); return 1; } drive->waiting_for_dma = 1; return 0;}static void cris_dma_exec_cmd(ide_drive_t *drive, u8 command){ /* set the irq handler which will finish the request when DMA is done */ ide_set_handler(drive, &cris_dma_intr, WAIT_CMD, NULL); /* issue cmd to drive */ cris_ide_outb(command, IDE_COMMAND_REG);}static void cris_dma_start(ide_drive_t *drive){ struct request *rq = drive->hwif->hwgroup->rq; int writing = rq_data_dir(rq); int type = TYPE_DMA; if (drive->current_speed >= XFER_UDMA_0) type = TYPE_UDMA; cris_ide_start_dma(drive, &ata_descrs[0], writing ? 0 : 1, type, ata_tot_size); if (writing) { LED_DISK_WRITE(1); } else { LED_DISK_READ(1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -