📄 ide.c
字号:
/* $Id: ide.c,v 1.1 2004/01/22 08:22:58 starvik Exp $ * * Etrax specific IDE functions, like init and PIO-mode setting etc. * Almost the entire ide.c is used for the rest of the Etrax ATA driver. * Copyright (c) 2000-2004 Axis Communications AB * * Authors: Bjorn Wesen (initial version) * Mikael Starvik (pio setup stuff, Linux 2.6 port) *//* Regarding DMA: * * There are two forms of DMA - "DMA handshaking" between the interface and the drive, * and DMA between the memory and the interface. We can ALWAYS use the latter, since it's * something built-in in the Etrax. However only some drives support the DMA-mode handshaking * on the ATA-bus. The normal PC driver and Triton interface disables memory-if DMA when the * device can't do DMA handshaking for some stupid reason. We don't need to do that. */#undef REALLY_SLOW_IO /* most systems can safely undef this */#include <linux/config.h>#include <linux/types.h>#include <linux/kernel.h>#include <linux/timer.h>#include <linux/mm.h>#include <linux/interrupt.h>#include <linux/delay.h>#include <linux/blkdev.h>#include <linux/hdreg.h>#include <linux/ide.h>#include <linux/init.h>#include <asm/io.h>#include <asm/arch/svinto.h>#include <asm/dma.h>/* number of Etrax DMA descriptors */#define MAX_DMA_DESCRS 64/* number of times to retry busy-flags when reading/writing IDE-registers * this can't be too high because a hung harddisk might cause the watchdog * to trigger (sometimes INB and OUTB are called with irq's disabled) */#define IDE_REGISTER_TIMEOUT 300#ifdef CONFIG_ETRAX_IDE_CSE1_16_RESET/* address where the memory-mapped IDE reset bit lives, if used */static volatile unsigned long *reset_addr;#endifstatic int e100_read_command = 0;#define LOWDB(x)#define D(x)voidetrax100_ide_outw(unsigned short data, ide_ioreg_t 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 */ while(timeleft && (*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy))) timeleft--; /* * 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); *R_ATA_CTRL_DATA = reg | data; /* write data to the drive's register */ timeleft = IDE_REGISTER_TIMEOUT; /* wait for transmitter ready */ while(timeleft && !(*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, tr_rdy))) timeleft--;}voidetrax100_ide_outb(unsigned char data, ide_ioreg_t reg){ etrax100_ide_outw(data, reg);}voidetrax100_ide_outbsync(ide_drive_t *drive, u8 addr, unsigned long port){ etrax100_ide_outw(addr, port);}unsigned shortetrax100_ide_inw(ide_ioreg_t reg) { int status; int timeleft; timeleft = IDE_REGISTER_TIMEOUT; /* wait for busy flag */ while(timeleft && (*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy))) timeleft--; 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 ((reg & IO_MASK (R_ATA_CTRL_DATA, addr)) == IO_FIELD (R_ATA_CTRL_DATA, addr, IDE_STATUS_OFFSET)) return BUSY_STAT; else /* For other rare cases we assume 0 is good enough. */ return 0; } *R_ATA_CTRL_DATA = reg | IO_STATE(R_ATA_CTRL_DATA, rw, read); /* read data */ timeleft = IDE_REGISTER_TIMEOUT; /* wait for available */ while(timeleft && !((status = *R_ATA_STATUS_DATA) & IO_MASK(R_ATA_STATUS_DATA, dav))) timeleft--; if(!timeleft) return 0; LOWDB(printk("inb: 0x%x from reg 0x%x\n", status & 0xff, reg)); return (unsigned short)status;}unsigned charetrax100_ide_inb(ide_ioreg_t reg){ return (unsigned char)etrax100_ide_inw(reg);}/* PIO timing (in R_ATA_CONFIG) * * _____________________________ * ADDRESS : ________/ * * _______________ * DIOR : ____________/ \__________ * * _______________ * DATA : XXXXXXXXXXXXXXXX_______________XXXXXXXX * * * DIOR is unbuffered while address and data is buffered. * This creates two problems: * 1. The DIOR pulse is to early (because it is unbuffered) * 2. The rise time of DIOR is long * * There are at least three different plausible solutions * 1. Use a pad capable of larger currents in Etrax * 2. Use an external buffer * 3. Make the strobe pulse longer * * Some of the strobe timings below are modified to compensate * for this. This implies a slight performance decrease. * * THIS SHOULD NEVER BE CHANGED! * * TODO: Is this true for the latest LX boards still ? */#define ATA_DMA2_STROBE 4#define ATA_DMA2_HOLD 0#define ATA_DMA1_STROBE 4#define ATA_DMA1_HOLD 1#define ATA_DMA0_STROBE 12#define ATA_DMA0_HOLD 9#define ATA_PIO4_SETUP 1#define ATA_PIO4_STROBE 5#define ATA_PIO4_HOLD 0#define ATA_PIO3_SETUP 1#define ATA_PIO3_STROBE 5#define ATA_PIO3_HOLD 1#define ATA_PIO2_SETUP 1#define ATA_PIO2_STROBE 6#define ATA_PIO2_HOLD 2#define ATA_PIO1_SETUP 2#define ATA_PIO1_STROBE 11#define ATA_PIO1_HOLD 4#define ATA_PIO0_SETUP 4#define ATA_PIO0_STROBE 19#define ATA_PIO0_HOLD 4static int e100_dma_check (ide_drive_t *drive);static int e100_dma_begin (ide_drive_t *drive);static int e100_dma_end (ide_drive_t *drive);static int e100_dma_read (ide_drive_t *drive);static int e100_dma_write (ide_drive_t *drive);static void e100_ide_input_data (ide_drive_t *drive, void *, unsigned int);static void e100_ide_output_data (ide_drive_t *drive, void *, unsigned int);static void e100_atapi_input_bytes(ide_drive_t *drive, void *, unsigned int);static void e100_atapi_output_bytes(ide_drive_t *drive, void *, unsigned int);static int e100_dma_off (ide_drive_t *drive);static int e100_dma_verbose (ide_drive_t *drive);/* * good_dma_drives() lists the model names (from "hdparm -i") * of drives which do not support mword2 DMA but which are * known to work fine with this interface under Linux. */const char *good_dma_drives[] = {"Micropolis 2112A", "CONNER CTMA 4000", "CONNER CTT8000-A", NULL};static void tune_e100_ide(ide_drive_t *drive, byte pio){ pio = 4; /* pio = ide_get_best_pio_mode(drive, pio, 4, NULL); */ /* set pio mode! */ switch(pio) { case 0: *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) | IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) | IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) | IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO0_SETUP ) | IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO0_STROBE ) | IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO0_HOLD ) ); break; case 1: *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) | IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) | IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) | IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO1_SETUP ) | IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO1_STROBE ) | IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO1_HOLD ) ); break; case 2: *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) | IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) | IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) | IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO2_SETUP ) | IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO2_STROBE ) | IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO2_HOLD ) ); break; case 3: *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) | IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) | IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) | IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO3_SETUP ) | IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO3_STROBE ) | IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO3_HOLD ) ); break; case 4: *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) | IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) | IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) | IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO4_SETUP ) | IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO4_STROBE ) | IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO4_HOLD ) ); break; }}void __initinit_e100_ide (void){ volatile unsigned int dummy; int h; printk("ide: ETRAX 100LX built-in ATA DMA controller\n"); /* first fill in some stuff in the ide_hwifs fields */ for(h = 0; h < MAX_HWIFS; h++) { ide_hwif_t *hwif = &ide_hwifs[h]; hwif->mmio = 2; hwif->chipset = ide_etrax100; hwif->tuneproc = &tune_e100_ide; hwif->ata_input_data = &e100_ide_input_data; hwif->ata_output_data = &e100_ide_output_data; hwif->atapi_input_bytes = &e100_atapi_input_bytes; hwif->atapi_output_bytes = &e100_atapi_output_bytes; hwif->ide_dma_check = &e100_dma_check; hwif->ide_dma_end = &e100_dma_end; hwif->ide_dma_write = &e100_dma_write; hwif->ide_dma_read = &e100_dma_read; hwif->ide_dma_begin = &e100_dma_begin; hwif->OUTB = &etrax100_ide_outb; hwif->OUTW = &etrax100_ide_outw; hwif->OUTBSYNC = &etrax100_ide_outbsync; hwif->INB = &etrax100_ide_inb; hwif->INW = &etrax100_ide_inw; hwif->ide_dma_off_quietly = &e100_dma_off; hwif->ide_dma_verbose = &e100_dma_verbose; hwif->sg_table = kmalloc(sizeof(struct scatterlist) * PRD_ENTRIES, GFP_KERNEL); } /* actually reset and configure the etrax100 ide/ata interface */ *R_ATA_CTRL_DATA = 0; *R_ATA_TRANSFER_CNT = 0; *R_ATA_CONFIG = 0; genconfig_shadow = (genconfig_shadow & ~IO_MASK(R_GEN_CONFIG, dma2) & ~IO_MASK(R_GEN_CONFIG, dma3) & ~IO_MASK(R_GEN_CONFIG, ata)) | ( IO_STATE( R_GEN_CONFIG, dma3, ata ) | IO_STATE( R_GEN_CONFIG, dma2, ata ) | IO_STATE( R_GEN_CONFIG, ata, select ) ); *R_GEN_CONFIG = genconfig_shadow; /* pull the chosen /reset-line low */#ifdef CONFIG_ETRAX_IDE_G27_RESET REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, 27, 0);#endif#ifdef CONFIG_ETRAX_IDE_CSE1_16_RESET REG_SHADOW_SET(port_cse1_addr, port_cse1_shadow, 16, 0);#endif#ifdef CONFIG_ETRAX_IDE_CSP0_8_RESET REG_SHADOW_SET(port_csp0_addr, port_csp0_shadow, 8, 0);#endif#ifdef CONFIG_ETRAX_IDE_PB7_RESET port_pb_dir_shadow = port_pb_dir_shadow | IO_STATE(R_PORT_PB_DIR, dir7, output); *R_PORT_PB_DIR = port_pb_dir_shadow; REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 7, 1);#endif /* wait some */ udelay(25); /* de-assert bus-reset */#ifdef CONFIG_ETRAX_IDE_CSE1_16_RESET REG_SHADOW_SET(port_cse1_addr, port_cse1_shadow, 16, 1);#endif#ifdef CONFIG_ETRAX_IDE_CSP0_8_RESET REG_SHADOW_SET(port_csp0_addr, port_csp0_shadow, 8, 1);#endif#ifdef CONFIG_ETRAX_IDE_G27_RESET REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, 27, 1);#endif /* make a dummy read to set the ata controller in a proper state */ dummy = *R_ATA_STATUS_DATA; *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) | IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) | IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) | IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO4_SETUP ) | IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO4_STROBE ) | IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO4_HOLD ) ); *R_ATA_CTRL_DATA = ( IO_STATE( R_ATA_CTRL_DATA, rw, read) | IO_FIELD( R_ATA_CTRL_DATA, addr, 1 ) ); while(*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy)); /* wait for busy flag*/ *R_IRQ_MASK0_SET = ( IO_STATE( R_IRQ_MASK0_SET, ata_irq0, set ) | IO_STATE( R_IRQ_MASK0_SET, ata_irq1, set ) | IO_STATE( R_IRQ_MASK0_SET, ata_irq2, set ) | IO_STATE( R_IRQ_MASK0_SET, ata_irq3, set ) ); printk("ide: waiting %d seconds for drives to regain consciousness\n", CONFIG_ETRAX_IDE_DELAY); h = jiffies + (CONFIG_ETRAX_IDE_DELAY * HZ); while(time_before(jiffies, h)) /* nothing */ ; /* reset the dma channels we will use */ RESET_DMA(ATA_TX_DMA_NBR); RESET_DMA(ATA_RX_DMA_NBR); WAIT_DMA(ATA_TX_DMA_NBR); WAIT_DMA(ATA_RX_DMA_NBR);}static int e100_dma_off (ide_drive_t *drive){ return 0;}static int e100_dma_verbose (ide_drive_t *drive){ printk(", DMA(mode 2)"); return 0;}static etrax_dma_descr mydescr;/* * 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 voide100_atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount){ ide_ioreg_t data_reg = IDE_DATA_REG; D(printk("atapi_input_bytes, dreg 0x%x, buffer 0x%x, count %d\n", data_reg, buffer, bytecount)); if(bytecount & 1) { printk("warning, odd bytecount in cdrom_in_bytes = %d.\n", bytecount); bytecount++; /* to round off */ } /* make sure the DMA channel is available */ RESET_DMA(ATA_RX_DMA_NBR); WAIT_DMA(ATA_RX_DMA_NBR); /* setup DMA descriptor */ mydescr.sw_len = bytecount; mydescr.ctrl = d_eol; mydescr.buf = virt_to_phys(buffer); /* start the dma channel */ *R_DMA_CH3_FIRST = virt_to_phys(&mydescr); *R_DMA_CH3_CMD = IO_STATE(R_DMA_CH3_CMD, cmd, start); /* initiate a multi word dma read using PIO handshaking */ *R_ATA_TRANSFER_CNT = IO_FIELD(R_ATA_TRANSFER_CNT, count, bytecount >> 1); *R_ATA_CTRL_DATA = data_reg | IO_STATE(R_ATA_CTRL_DATA, rw, read) | IO_STATE(R_ATA_CTRL_DATA, src_dst, dma) | IO_STATE(R_ATA_CTRL_DATA, handsh, pio) | IO_STATE(R_ATA_CTRL_DATA, multi, on) | IO_STATE(R_ATA_CTRL_DATA, dma_size, word); /* wait for completion */ LED_DISK_READ(1); WAIT_DMA(ATA_RX_DMA_NBR); LED_DISK_READ(0);#if 0 /* old polled transfer code * this should be moved into a new function that can do polled * transfers if DMA is not available */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -