os/linux-2.6-tag--devboard-R2_10-4/drivers/ide/cris/ide-cris.c

Go to the documentation of this file.
00001 /* $Id: ide-cris.c,v 1.1.1.1 2008/11/27 20:04:01 elphel Exp $
00002  *
00003  * Etrax specific IDE functions, like init and PIO-mode setting etc.
00004  * Almost the entire ide.c is used for the rest of the Etrax ATA driver.
00005  * Copyright (c) 2000-2006 Axis Communications AB
00006  *
00007  * Authors:    Bjorn Wesen        (initial version)
00008  *             Mikael Starvik     (crisv32 port)
00009  */
00010 
00011 /* Regarding DMA:
00012  *
00013  * There are two forms of DMA - "DMA handshaking" between the interface and the drive,
00014  * and DMA between the memory and the interface. We can ALWAYS use the latter, since it's
00015  * something built-in in the Etrax. However only some drives support the DMA-mode handshaking
00016  * on the ATA-bus. The normal PC driver and Triton interface disables memory-if DMA when the
00017  * device can't do DMA handshaking for some stupid reason. We don't need to do that.
00018  */
00019 
00020 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
00021 
00022 #include <linux/types.h>
00023 #include <linux/kernel.h>
00024 #include <linux/timer.h>
00025 #include <linux/mm.h>
00026 #include <linux/interrupt.h>
00027 #include <linux/delay.h>
00028 #include <linux/blkdev.h>
00029 #include <linux/hdreg.h>
00030 #include <linux/ide.h>
00031 #include <linux/init.h>
00032 
00033 #include <asm/io.h>
00034 #include <asm/dma.h>
00035 
00036 /* number of DMA descriptors */
00037 #define MAX_DMA_DESCRS 64
00038 
00039 /* number of times to retry busy-flags when reading/writing IDE-registers
00040  * this can't be too high because a hung harddisk might cause the watchdog
00041  * to trigger (sometimes INB and OUTB are called with irq's disabled)
00042  */
00043 
00044 #define IDE_REGISTER_TIMEOUT 300
00045 
00047 
00048 #define LOWDB(x)
00049 #define D(x)
00050 
00051 //#define LOWDB(x) x
00052 //#define D(x) x
00053 
00054 
00055 
00056 enum /* Transfer types */
00057 {
00058         TYPE_PIO,
00059         TYPE_DMA,
00060         TYPE_UDMA
00061 };
00062 
00063 /* CRISv32 specifics */
00064 #ifdef CONFIG_ETRAX_ARCH_V32
00065 #include <asm/arch/hwregs/ata_defs.h>
00066 #include <asm/arch/hwregs/dma_defs.h>
00067 #include <asm/arch/hwregs/dma.h>
00068 #include <asm/arch/pinmux.h>
00069 
00070 #define ATA_UDMA2_CYC    2
00071 #define ATA_UDMA2_DVS    3
00072 #define ATA_UDMA1_CYC    2
00073 #define ATA_UDMA1_DVS    4
00074 #define ATA_UDMA0_CYC    4
00075 #define ATA_UDMA0_DVS    6
00076 #define ATA_DMA2_STROBE  7
00077 #define ATA_DMA2_HOLD    1
00078 #define ATA_DMA1_STROBE  8
00079 #define ATA_DMA1_HOLD    3
00080 #define ATA_DMA0_STROBE 25
00081 #define ATA_DMA0_HOLD   19
00082 #define ATA_PIO4_SETUP   3
00083 #define ATA_PIO4_STROBE  7
00084 #define ATA_PIO4_HOLD    1
00085 #define ATA_PIO3_SETUP   3
00086 #define ATA_PIO3_STROBE  9
00087 #define ATA_PIO3_HOLD    3
00088 #define ATA_PIO2_SETUP   3
00089 #define ATA_PIO2_STROBE 13
00090 #define ATA_PIO2_HOLD    5
00091 #define ATA_PIO1_SETUP   5
00092 #define ATA_PIO1_STROBE 23
00093 #define ATA_PIO1_HOLD    9
00094 #define ATA_PIO0_SETUP   9
00095 #define ATA_PIO0_STROBE 39
00096 #define ATA_PIO0_HOLD    9
00097 
00098 /*
00099  * On ETRAX FS, an interrupt remains latched and active until ack:ed.
00100  * Further, ATA acks are without effect as long as INTRQ is asserted, as the
00101  * corresponding ATA interrupt is continuously set to active.  There will be a
00102  * clearing ack at the usual cris_ide_ack_intr call, but that serves just to
00103  * gracefully handle an actual spurious interrupt or similar situation (which
00104  * will cause an early return without further actions, see the ide_intr
00105  * function).
00106  *
00107  * However, the normal case at time of this writing is that nothing has
00108  * changed from when INTRQ was asserted until the cris_ide_ack_intr call; no
00109  * ATA registers written and no status register read, so INTRQ will *remain*
00110  * asserted, thus *another* interrupt will be latched, and will be seen as a
00111  * spurious interrupt after the "real" interrupt is serviced.  With lots of
00112  * ATA traffic (as in a trivial file-copy between two drives), this will trig
00113  * the condition desc->irqs_unhandled > 99900 in
00114  * kernel/irq/spurious.c:note_interrupt and the system will halt.
00115  *
00116  * To actually get rid of the interrupt corresponding to the current INTRQ
00117  * assertion, we make a second ack after the next ATA register read or write;
00118  * i.e. when INTRQ must be deasserted.  At that time, we don't have the hwif
00119  * pointer available, so we need to stash a local copy (safe, because it'll be
00120  * set and cleared within the same spin_lock_irqsave region).  The pointer
00121  * serves doubly as a boolean flag that an ack is needed.  The caller must
00122  * NULL the pointer after the "second ack".
00123  */
00124 
00125 static ide_hwif_t *hwif_to_ack;
00126 
00127 static int
00128 cris_ide_ack_intr(ide_hwif_t* hwif)
00129 {
00130         /*
00131          * The interrupt is shared so we need to find the interface bit number
00132          * to ack.  We define the ATA I/O register addresses to have the
00133          * format of ata rw_ctrl2 register contents, conveniently holding this
00134          * number.
00135          */
00136         reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2, 
00137                                  int, hwif->io_ports[0]);
00138         REG_WR_INT(ata, regi_ata, rw_ack_intr, 1 << ctrl2.sel);
00139 
00140         /* Prepare to ack again, see above. */
00141         hwif_to_ack = hwif;
00142         return 1;
00143 }
00144 
00145 static inline int
00146 cris_ide_busy(void)
00147 {
00148         reg_ata_rs_stat_data stat_data;
00149         stat_data = REG_RD(ata, regi_ata, rs_stat_data);
00150         return stat_data.busy;
00151 }
00152 
00153 static inline int
00154 cris_ide_ready(void)
00155 {
00156         return !cris_ide_busy();
00157 }
00158 
00159 static inline int
00160 cris_ide_data_available(unsigned short* data)
00161 {
00162         reg_ata_rs_stat_data stat_data;
00163         stat_data = REG_RD(ata, regi_ata, rs_stat_data);
00164         *data = stat_data.data;
00165         return stat_data.dav;
00166 }
00167 
00168 static void
00169 cris_ide_write_command(unsigned long command)
00170 {       
00171         REG_WR_INT(ata, regi_ata, rw_ctrl2, command); /* write data to the drive's register */
00172 
00173         /*
00174          * Perform a pending ack if needed; see hwif_ack definition.  Perhaps
00175          * we should check closer that this call is really a part of the
00176          * preparation to read the ATA status register or write to the ATA
00177          * command register (causing deassert of INTRQ; see the ATA standard),
00178          * but at time of this writing (and expected to sanely remain so), the
00179          * first ATA register activity after an cris_ide_ack_intr call is
00180          * certain to do exactly that.
00181          */
00182         if (hwif_to_ack) {
00183                 /* The drive may take this long to deassert INTRQ. */
00184                 ndelay(400);
00185                 cris_ide_ack_intr(hwif_to_ack);
00186                 hwif_to_ack = NULL;
00187         }
00188 }
00189 
00190 static void
00191 cris_ide_set_speed(int type, int setup, int strobe, int hold)
00192 {
00193         reg_ata_rw_ctrl0 ctrl0 = REG_RD(ata, regi_ata, rw_ctrl0);
00194         reg_ata_rw_ctrl1 ctrl1 = REG_RD(ata, regi_ata, rw_ctrl1);
00195 
00196         if (type == TYPE_PIO) {
00197                 ctrl0.pio_setup = setup;
00198                 ctrl0.pio_strb = strobe;
00199                 ctrl0.pio_hold = hold;
00200         } else if (type == TYPE_DMA) {
00201                 ctrl0.dma_strb = strobe;
00202                 ctrl0.dma_hold = hold;
00203         } else if (type == TYPE_UDMA) {
00204                 ctrl1.udma_tcyc = setup;
00205                 ctrl1.udma_tdvs = strobe;
00206         }
00207         REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
00208         REG_WR(ata, regi_ata, rw_ctrl1, ctrl1);
00209 }
00210 
00211 static unsigned long
00212 cris_ide_base_address(int bus)
00213 {
00214         reg_ata_rw_ctrl2 ctrl2 = {0};
00215         ctrl2.sel = bus;
00216         return REG_TYPE_CONV(int, reg_ata_rw_ctrl2, ctrl2);
00217 }
00218 
00219 static unsigned long
00220 cris_ide_reg_addr(unsigned long addr, int cs0, int cs1)
00221 {
00222         reg_ata_rw_ctrl2 ctrl2 = {0};
00223         ctrl2.addr = addr;
00224         ctrl2.cs1 = !cs1;
00225         ctrl2.cs0 = !cs0;
00226         return REG_TYPE_CONV(int, reg_ata_rw_ctrl2, ctrl2);
00227 }
00228 
00229 static __init void
00230 cris_ide_reset(unsigned val)
00231 {
00232         reg_ata_rw_ctrl0 ctrl0 = {0};
00233         ctrl0.rst = val ? regk_ata_active : regk_ata_inactive;
00234         REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
00235 }
00236 
00237 static __init void
00238 cris_ide_init(void)
00239 {
00240 printk("cris_ide_init()\n");
00241 
00242         reg_ata_rw_ctrl0 ctrl0 = {0};
00243         reg_ata_rw_intr_mask intr_mask = {0};
00244 
00245         ctrl0.en = regk_ata_yes;
00246         REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
00247 
00248 /*
00249         intr_mask.bus0 = regk_ata_yes;
00250         intr_mask.bus1 = regk_ata_yes;
00251         intr_mask.bus2 = regk_ata_yes;          
00252         intr_mask.bus3 = regk_ata_yes;
00253 */
00254         intr_mask.bus0 = regk_ata_yes;
00255         intr_mask.bus1 = regk_ata_no;
00256         intr_mask.bus2 = regk_ata_no;           
00257         intr_mask.bus3 = regk_ata_no;
00258 
00259         REG_WR(ata, regi_ata, rw_intr_mask, intr_mask);
00260 
00261         crisv32_request_dma(2, "ETRAX FS built-in ATA", DMA_VERBOSE_ON_ERROR, 0, dma_ata);
00262         crisv32_request_dma(3, "ETRAX FS built-in ATA", DMA_VERBOSE_ON_ERROR, 0, dma_ata);
00263         
00264         crisv32_pinmux_alloc_fixed(pinmux_ata);
00265         crisv32_pinmux_alloc_fixed(pinmux_ata0);
00266         crisv32_pinmux_alloc_fixed(pinmux_ata1);
00267         crisv32_pinmux_alloc_fixed(pinmux_ata2);
00268         crisv32_pinmux_alloc_fixed(pinmux_ata3);
00269 
00270         DMA_RESET(regi_dma2);
00271         DMA_ENABLE(regi_dma2);
00272         DMA_RESET(regi_dma3);
00273         DMA_ENABLE(regi_dma3);
00274 
00275         DMA_WR_CMD (regi_dma2, regk_dma_set_w_size2);
00276         DMA_WR_CMD (regi_dma3, regk_dma_set_w_size2);   
00277 }
00278 
00279 static dma_descr_context mycontext __attribute__ ((__aligned__(32)));
00280 
00281 #define cris_dma_descr_type dma_descr_data
00282 #define cris_pio_read (regk_ata_rd << 24)
00283 //#define cris_ultra_mask 0x0 /* 0x7 for UDMA */
00284 #define cris_ultra_mask 0x0 /* 0x7 for UDMA */
00286 #define IRQ ATA_INTR_VECT
00287 #define MAX_DESCR_SIZE 0xffffffffUL
00288 
00289 static unsigned long
00290 cris_ide_get_reg(unsigned long reg)
00291 {
00292         return (reg & 0x0e000000) >> 25;
00293 }
00294 
00295 static void
00296 cris_ide_fill_descriptor(cris_dma_descr_type *d, void* buf, unsigned int len, int last)
00297 {
00298         d->buf = (char*)virt_to_phys(buf);
00299         d->after = d->buf + len;
00300         d->eol = last;
00301         /* assume descriptors are consecutively placed in memory */
00302         d->next = last ? 0 : (cris_dma_descr_type*)virt_to_phys(d+1);
00303 }
00304 
00305 static void
00306 cris_ide_start_dma(ide_drive_t *drive, cris_dma_descr_type *d, int dir,int type,int len)
00307 {
00308         reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2, int, IDE_DATA_REG);
00309         reg_ata_rw_trf_cnt trf_cnt = {0};
00310 
00311         mycontext.saved_data = (dma_descr_data*)virt_to_phys(d);
00312         mycontext.saved_data_buf = d->buf;
00313         /* start the dma channel */
00314         if (dir)
00315                 flush_dma_context(&mycontext); // Cache bug workaround   
00316         DMA_START_CONTEXT(dir ? regi_dma3 : regi_dma2, virt_to_phys(&mycontext));
00317         
00318         /* initiate a multi word dma read using PIO handshaking */
00319         trf_cnt.cnt = len >> 1;
00320         /* Due to a "feature" the transfer count has to be one extra word for UDMA. */
00321         if (type == TYPE_UDMA)
00322                 trf_cnt.cnt++;
00323         REG_WR(ata, regi_ata, rw_trf_cnt, trf_cnt);
00324 
00325         ctrl2.rw = dir ? regk_ata_rd : regk_ata_wr;
00326         ctrl2.trf_mode = regk_ata_dma;
00327         ctrl2.hsh = type == TYPE_PIO ? regk_ata_pio : 
00328                     type == TYPE_DMA ? regk_ata_dma : regk_ata_udma;
00329         ctrl2.multi = regk_ata_yes;
00330         ctrl2.dma_size = regk_ata_word;
00331         REG_WR(ata, regi_ata, rw_ctrl2, ctrl2);
00332 }
00333 
00334 static void
00335 cris_ide_wait_dma(int dir)
00336 {
00337 //printk("IDE wait DMA... 1\n");
00338         reg_dma_rw_stat status;
00339         do
00340         {
00341                 status = REG_RD(dma, dir ? regi_dma3 : regi_dma2, rw_stat);
00342         } while(status.list_state != regk_dma_data_at_eol);
00343 //printk("IDE wait DMA... 2\n");
00344 }
00345 
00346 static int cris_dma_test_irq(ide_drive_t *drive)
00347 {
00348         int intr = REG_RD_INT(ata, regi_ata, r_intr);
00349         reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2, int, IDE_DATA_REG);
00350         return intr & (1 << ctrl2.sel) ? 1 : 0;
00351 }
00352 
00353 static void cris_ide_initialize_dma(int dir)
00354 {
00355 }
00356 
00357 #else
00358 /* CRISv10 specifics */
00359 #include <asm/arch/svinto.h>
00360 #include <asm/arch/io_interface_mux.h>
00361 
00362 /* PIO timing (in R_ATA_CONFIG)
00363  *
00364  *                        _____________________________
00365  * ADDRESS :     ________/
00366  *
00367  *                            _______________
00368  * DIOR    :     ____________/               \__________
00369  *
00370  *                               _______________
00371  * DATA    :     XXXXXXXXXXXXXXXX_______________XXXXXXXX
00372  *
00373  *
00374  * DIOR is unbuffered while address and data is buffered.
00375  * This creates two problems:
00376  * 1. The DIOR pulse is to early (because it is unbuffered)
00377  * 2. The rise time of DIOR is long
00378  *
00379  * There are at least three different plausible solutions
00380  * 1. Use a pad capable of larger currents in Etrax
00381  * 2. Use an external buffer
00382  * 3. Make the strobe pulse longer
00383  *
00384  * Some of the strobe timings below are modified to compensate
00385  * for this. This implies a slight performance decrease.
00386  *
00387  * THIS SHOULD NEVER BE CHANGED!
00388  *
00389  * TODO: Is this true for the latest LX boards still ?
00390  */
00391 
00392 #define ATA_UDMA2_CYC    0 /* No UDMA supported, just to make it compile. */
00393 #define ATA_UDMA2_DVS    0
00394 #define ATA_UDMA1_CYC    0
00395 #define ATA_UDMA1_DVS    0
00396 #define ATA_UDMA0_CYC    0
00397 #define ATA_UDMA0_DVS    0
00398 #define ATA_DMA2_STROBE  4
00399 #define ATA_DMA2_HOLD    0
00400 #define ATA_DMA1_STROBE  4
00401 #define ATA_DMA1_HOLD    1
00402 #define ATA_DMA0_STROBE 12
00403 #define ATA_DMA0_HOLD    9
00404 #define ATA_PIO4_SETUP   1
00405 #define ATA_PIO4_STROBE  5
00406 #define ATA_PIO4_HOLD    0
00407 #define ATA_PIO3_SETUP   1
00408 #define ATA_PIO3_STROBE  5
00409 #define ATA_PIO3_HOLD    1
00410 #define ATA_PIO2_SETUP   1
00411 #define ATA_PIO2_STROBE  6
00412 #define ATA_PIO2_HOLD    2
00413 #define ATA_PIO1_SETUP   2
00414 #define ATA_PIO1_STROBE 11
00415 #define ATA_PIO1_HOLD    4
00416 #define ATA_PIO0_SETUP   4
00417 #define ATA_PIO0_STROBE 19
00418 #define ATA_PIO0_HOLD    4
00419 
00420 int 
00421 cris_ide_ack_intr(ide_hwif_t* hwif)
00422 {
00423         return 1;
00424 }
00425 
00426 static inline int
00427 cris_ide_busy(void)
00428 {
00429         return *R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy) ; 
00430 }
00431 
00432 static inline int
00433 cris_ide_ready(void)
00434 {
00435         return *R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, tr_rdy) ; 
00436 }
00437 
00438 static inline int
00439 cris_ide_data_available(unsigned short* data)
00440 {
00441         unsigned long status = *R_ATA_STATUS_DATA;
00442         *data = (unsigned short)status;
00443         return status & IO_MASK(R_ATA_STATUS_DATA, dav);
00444 }
00445         
00446 static void
00447 cris_ide_write_command(unsigned long command)
00448 {
00449         *R_ATA_CTRL_DATA = command; 
00450 }               
00451 
00452 static void
00453 cris_ide_set_speed(int type, int setup, int strobe, int hold)
00454 {
00455         static int pio_setup = ATA_PIO4_SETUP;
00456         static int pio_strobe = ATA_PIO4_STROBE;
00457         static int pio_hold = ATA_PIO4_HOLD;
00458         static int dma_strobe = ATA_DMA2_STROBE;
00459         static int dma_hold = ATA_DMA2_HOLD;
00460 
00461         if (type == TYPE_PIO) {
00462                 pio_setup = setup;
00463                 pio_strobe = strobe;
00464                 pio_hold = hold;
00465         } else if (type == TYPE_DMA) {
00466                 dma_strobe = strobe;
00467           dma_hold = hold;
00468         }
00469         *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) |
00470           IO_FIELD( R_ATA_CONFIG, dma_strobe, dma_strobe ) |
00471                 IO_FIELD( R_ATA_CONFIG, dma_hold,   dma_hold ) |
00472                 IO_FIELD( R_ATA_CONFIG, pio_setup,  pio_setup ) |
00473                 IO_FIELD( R_ATA_CONFIG, pio_strobe, pio_strobe ) |
00474                 IO_FIELD( R_ATA_CONFIG, pio_hold,   pio_hold ) );
00475 }
00476 
00477 static unsigned long
00478 cris_ide_base_address(int bus)
00479 {
00480         return IO_FIELD(R_ATA_CTRL_DATA, sel, bus);
00481 }
00482 
00483 static unsigned long
00484 cris_ide_reg_addr(unsigned long addr, int cs0, int cs1)
00485 {
00486         return IO_FIELD(R_ATA_CTRL_DATA, addr, addr) |
00487                IO_FIELD(R_ATA_CTRL_DATA, cs0, cs0 ? 0 : 1) |
00488                IO_FIELD(R_ATA_CTRL_DATA, cs1, cs1 ? 0 : 1);
00489 }
00490 
00491 static __init void
00492 cris_ide_reset(unsigned val)
00493 {
00494 #ifdef CONFIG_ETRAX_IDE_G27_RESET
00495         REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, 27, val);
00496 #endif
00497 #ifdef CONFIG_ETRAX_IDE_CSE1_16_RESET
00498         REG_SHADOW_SET(port_cse1_addr, port_cse1_shadow, 16, val);
00499 #endif
00500 #ifdef CONFIG_ETRAX_IDE_CSP0_8_RESET
00501         REG_SHADOW_SET(port_csp0_addr, port_csp0_shadow, 8, val);
00502 #endif
00503 #ifdef CONFIG_ETRAX_IDE_PB7_RESET
00504         port_pb_dir_shadow = port_pb_dir_shadow |
00505                 IO_STATE(R_PORT_PB_DIR, dir7, output);
00506         *R_PORT_PB_DIR = port_pb_dir_shadow;
00507         REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 7, val);
00508 #endif
00509 }
00510 
00511 static __init void
00512 cris_ide_init(void)
00513 {
00514         volatile unsigned int dummy;
00515 
00516         *R_ATA_CTRL_DATA = 0;
00517         *R_ATA_TRANSFER_CNT = 0;
00518         *R_ATA_CONFIG = 0;
00519 
00520         if (cris_request_io_interface(if_ata, "ETRAX100LX IDE")) {
00521                 printk(KERN_CRIT "ide: Failed to get IO interface\n");
00522                 return;
00523         } else if (cris_request_dma(ATA_TX_DMA_NBR,
00524                                           "ETRAX100LX IDE TX",
00525                                           DMA_VERBOSE_ON_ERROR,
00526                                           dma_ata)) {
00527                 cris_free_io_interface(if_ata);
00528                 printk(KERN_CRIT "ide: Failed to get Tx DMA channel\n");
00529                 return;
00530         } else if (cris_request_dma(ATA_RX_DMA_NBR,
00531                                           "ETRAX100LX IDE RX",
00532                                           DMA_VERBOSE_ON_ERROR,
00533                                           dma_ata)) {
00534                 cris_free_dma(ATA_TX_DMA_NBR, "ETRAX100LX IDE Tx");
00535                 cris_free_io_interface(if_ata);
00536                 printk(KERN_CRIT "ide: Failed to get Rx DMA channel\n");
00537                 return;
00538         }
00539 
00540         /* make a dummy read to set the ata controller in a proper state */
00541         dummy = *R_ATA_STATUS_DATA;
00542 
00543         *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ));
00544         *R_ATA_CTRL_DATA = ( IO_STATE( R_ATA_CTRL_DATA, rw,   read) |
00545                              IO_FIELD( R_ATA_CTRL_DATA, addr, 1   ) );
00546 
00547         while(*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy)); /* wait for busy flag*/
00548 
00549         *R_IRQ_MASK0_SET = ( IO_STATE( R_IRQ_MASK0_SET, ata_irq0, set ) |
00550                              IO_STATE( R_IRQ_MASK0_SET, ata_irq1, set ) |
00551                              IO_STATE( R_IRQ_MASK0_SET, ata_irq2, set ) |
00552                              IO_STATE( R_IRQ_MASK0_SET, ata_irq3, set ) );
00553 
00554         /* reset the dma channels we will use */
00555 
00556         RESET_DMA(ATA_TX_DMA_NBR);
00557         RESET_DMA(ATA_RX_DMA_NBR);
00558         WAIT_DMA(ATA_TX_DMA_NBR);
00559         WAIT_DMA(ATA_RX_DMA_NBR);
00560 }
00561 
00562 #define cris_dma_descr_type etrax_dma_descr
00563 #define cris_pio_read IO_STATE(R_ATA_CTRL_DATA, rw, read)
00564 #define cris_ultra_mask 0x0
00565 #define IRQ 4
00566 #define MAX_DESCR_SIZE 0x10000UL
00567 
00568 static unsigned long
00569 cris_ide_get_reg(unsigned long reg)
00570 {
00571         return (reg & 0x0e000000) >> 25;
00572 }
00573 
00574 static void
00575 cris_ide_fill_descriptor(cris_dma_descr_type *d, void* buf, unsigned int len, int last)
00576 {
00577         d->buf = virt_to_phys(buf);
00578         d->sw_len = len == MAX_DESCR_SIZE ? 0 : len;
00579         d->ctrl = last ? d_eol : 0;
00580         d->next = last ? 0 : virt_to_phys(d+1); /* assumes descr's in array */
00581 }
00582 
00583 static void cris_ide_start_dma(ide_drive_t *drive, cris_dma_descr_type *d, int dir, int type, int len)
00584 {
00585         unsigned long cmd;
00586 
00587         if (dir) {
00588                 /* need to do this before RX DMA due to a chip bug
00589                  * it is enough to just flush the part of the cache that
00590                  * corresponds to the buffers we start, but since HD transfers
00591                  * usually are more than 8 kB, it is easier to optimize for the
00592                  * normal case and just flush the entire cache. its the only
00593                  * way to be sure! (OB movie quote)
00594                  */
00595                 flush_etrax_cache();
00596                 *R_DMA_CH3_FIRST = virt_to_phys(d);
00597                 *R_DMA_CH3_CMD   = IO_STATE(R_DMA_CH3_CMD, cmd, start);
00598 
00599         } else {
00600                 *R_DMA_CH2_FIRST = virt_to_phys(d);
00601                 *R_DMA_CH2_CMD   = IO_STATE(R_DMA_CH2_CMD, cmd, start);
00602         }
00603         
00604         /* initiate a multi word dma read using DMA handshaking */
00605 
00606         *R_ATA_TRANSFER_CNT =
00607                 IO_FIELD(R_ATA_TRANSFER_CNT, count, len >> 1);
00608 
00609         cmd = dir ? IO_STATE(R_ATA_CTRL_DATA, rw, read) : IO_STATE(R_ATA_CTRL_DATA, rw, write);
00610         cmd |= type == TYPE_PIO ? IO_STATE(R_ATA_CTRL_DATA, handsh, pio) : 
00611                                   IO_STATE(R_ATA_CTRL_DATA, handsh, dma);
00612         *R_ATA_CTRL_DATA =
00613                 cmd |
00614                 IO_FIELD(R_ATA_CTRL_DATA, data, IDE_DATA_REG) |
00615                 IO_STATE(R_ATA_CTRL_DATA, src_dst,  dma)  |
00616                 IO_STATE(R_ATA_CTRL_DATA, multi,    on)   |
00617                 IO_STATE(R_ATA_CTRL_DATA, dma_size, word);
00618 }
00619 
00620 static void
00621 cris_ide_wait_dma(int dir)
00622 {
00623         if (dir)
00624                 WAIT_DMA(ATA_RX_DMA_NBR);
00625         else
00626                 WAIT_DMA(ATA_TX_DMA_NBR);
00627 }
00628 
00629 static int cris_dma_test_irq(ide_drive_t *drive)
00630 {
00631         int intr = *R_IRQ_MASK0_RD;
00632         int bus = IO_EXTRACT(R_ATA_CTRL_DATA, sel, IDE_DATA_REG);
00633         return intr & (1 << (bus + IO_BITNR(R_IRQ_MASK0_RD, ata_irq0))) ? 1 : 0;
00634 }
00635 
00636 
00637 static void cris_ide_initialize_dma(int dir)
00638 {
00639         if (dir)
00640         {
00641                 RESET_DMA(ATA_RX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
00642                 WAIT_DMA(ATA_RX_DMA_NBR);
00643         }
00644         else
00645         {
00646                 RESET_DMA(ATA_TX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
00647                 WAIT_DMA(ATA_TX_DMA_NBR);
00648         }
00649 }
00650 
00651 #endif
00652                 
00653 void
00654 cris_ide_outw(unsigned short data, unsigned long reg) {
00655         int timeleft;
00656 
00657         LOWDB(printk("ow: data 0x%x, reg 0x%x\n", data, reg));
00658 
00659         /* note the lack of handling any timeouts. we stop waiting, but we don't
00660          * really notify anybody.
00661          */
00662 
00663         timeleft = IDE_REGISTER_TIMEOUT;
00664         /* wait for busy flag */
00665         do {
00666                 timeleft--;
00667         } while(timeleft && cris_ide_busy());
00668 
00669         /*
00670          * Fall through at a timeout, so the ongoing command will be
00671          * aborted by the write below, which is expected to be a dummy
00672          * command to the command register.  This happens when a faulty
00673          * drive times out on a command.  See comment on timeout in
00674          * INB.
00675          */
00676         if(!timeleft)
00677                 printk("ATA timeout reg 0x%lx := 0x%x\n", reg, data);
00678 
00679         cris_ide_write_command(reg|data); /* write data to the drive's register */ 
00680 
00681         timeleft = IDE_REGISTER_TIMEOUT;
00682         /* wait for transmitter ready */
00683         do {
00684                 timeleft--;
00685         } while(timeleft && !cris_ide_ready());
00686 }
00687 
00688 void
00689 cris_ide_outb(unsigned char data, unsigned long reg)
00690 {
00691         cris_ide_outw(data, reg);
00692 }
00693 
00694 void
00695 cris_ide_outbsync(ide_drive_t *drive, u8 addr, unsigned long port)
00696 {
00697         cris_ide_outw(addr, port);
00698 }
00699 
00700 unsigned short
00701 cris_ide_inw(unsigned long reg) {
00702         int timeleft;
00703         unsigned short val;
00704 
00705         timeleft = IDE_REGISTER_TIMEOUT;
00706         /* wait for busy flag */
00707         do {
00708                 timeleft--;
00709         } while(timeleft && cris_ide_busy());
00710 
00711         if(!timeleft) {
00712                 /*
00713                  * If we're asked to read the status register, like for
00714                  * example when a command does not complete for an
00715                  * extended time, but the ATA interface is stuck in a
00716                  * busy state at the *ETRAX* ATA interface level (as has
00717                  * happened repeatedly with at least one bad disk), then
00718                  * the best thing to do is to pretend that we read
00719                  * "busy" in the status register, so the IDE driver will
00720                  * time-out, abort the ongoing command and perform a
00721                  * reset sequence.  Note that the subsequent OUT_BYTE
00722                  * call will also timeout on busy, but as long as the
00723                  * write is still performed, everything will be fine.
00724                  */
00725                 if (cris_ide_get_reg(reg) == IDE_STATUS_OFFSET)
00726                         return BUSY_STAT;
00727                 else
00728                         /* For other rare cases we assume 0 is good enough.  */
00729                         return 0;
00730         }
00731 
00732         cris_ide_write_command(reg | cris_pio_read);
00733 
00734         timeleft = IDE_REGISTER_TIMEOUT;
00735         /* wait for available */
00736         do {
00737                 timeleft--;
00738         } while(timeleft && !cris_ide_data_available(&val));
00739 
00740         if(!timeleft)
00741                 return 0;
00742 
00743         LOWDB(printk("inb: 0x%x from reg 0x%x\n", val & 0xff, reg));
00744 
00745         return val;
00746 }
00747 
00748 unsigned char
00749 cris_ide_inb(unsigned long reg)
00750 {
00751         return (unsigned char)cris_ide_inw(reg);
00752 }
00753 
00754 static int cris_dma_check (ide_drive_t *drive);
00755 static int cris_dma_end (ide_drive_t *drive);
00756 static int cris_dma_setup (ide_drive_t *drive);
00757 static void cris_dma_exec_cmd (ide_drive_t *drive, u8 command);
00758 static int cris_dma_test_irq(ide_drive_t *drive);
00759 static void cris_dma_start(ide_drive_t *drive);
00760 static void cris_ide_input_data (ide_drive_t *drive, void *, unsigned int);
00761 static void cris_ide_output_data (ide_drive_t *drive, void *, unsigned int);
00762 static void cris_atapi_input_bytes(ide_drive_t *drive, void *, unsigned int);
00763 static void cris_atapi_output_bytes(ide_drive_t *drive, void *, unsigned int);
00764 static int cris_dma_off (ide_drive_t *drive);
00765 static int cris_dma_on (ide_drive_t *drive);
00766 static int cris_dma_host_off (ide_drive_t *drive);
00767 static int cris_dma_host_on (ide_drive_t *drive);
00768 
00769 static void tune_cris_ide(ide_drive_t *drive, u8 pio)
00770 {
00771         int setup, strobe, hold;
00772 
00773         switch(pio)
00774         {       
00775                 case 0:
00776                         setup = ATA_PIO0_SETUP;
00777                         strobe = ATA_PIO0_STROBE;
00778                         hold = ATA_PIO0_HOLD;
00779                         break;
00780                 case 1:
00781                         setup = ATA_PIO1_SETUP;
00782                         strobe = ATA_PIO1_STROBE;
00783                         hold = ATA_PIO1_HOLD;
00784                         break;
00785                 case 2:
00786                         setup = ATA_PIO2_SETUP;
00787                         strobe = ATA_PIO2_STROBE;
00788                         hold = ATA_PIO2_HOLD;
00789                         break;
00790                 case 3:
00791                         setup = ATA_PIO3_SETUP;
00792                         strobe = ATA_PIO3_STROBE;
00793                         hold = ATA_PIO3_HOLD;
00794                         break;
00795                 case 4:
00796                         setup = ATA_PIO4_SETUP;
00797                         strobe = ATA_PIO4_STROBE;
00798                         hold = ATA_PIO4_HOLD;
00799                         break;    
00800                 default:
00801                         return;
00802         }
00803 
00804         cris_ide_set_speed(TYPE_PIO, setup, strobe, hold);
00805 }
00806 
00807 static int speed_cris_ide(ide_drive_t *drive, u8 speed)
00808 {
00809         int cyc = 0, dvs = 0, strobe = 0, hold = 0;
00810 
00811         if (speed >= XFER_PIO_0 && speed <= XFER_PIO_4) {
00812                 tune_cris_ide(drive, speed - XFER_PIO_0);
00813                 return 0;
00814         }
00815 
00816         switch(speed)
00817         {       
00818                 case XFER_UDMA_0:
00819                         cyc = ATA_UDMA0_CYC;
00820                         dvs = ATA_UDMA0_DVS;
00821                         break;
00822                 case XFER_UDMA_1:
00823                         cyc = ATA_UDMA1_CYC;
00824                         dvs = ATA_UDMA1_DVS;
00825                         break;
00826                 case XFER_UDMA_2:
00827                         cyc = ATA_UDMA2_CYC;
00828                         dvs = ATA_UDMA2_DVS;
00829                         break;
00830                 case XFER_MW_DMA_0:
00831                         strobe = ATA_DMA0_STROBE;
00832                         hold = ATA_DMA0_HOLD;
00833                         break;
00834                 case XFER_MW_DMA_1:
00835                         strobe = ATA_DMA1_STROBE;
00836                         hold = ATA_DMA1_HOLD;
00837                         break;
00838                 case XFER_MW_DMA_2:
00839                         strobe = ATA_DMA2_STROBE;
00840                         hold = ATA_DMA2_HOLD;
00841                         break;
00842                 default:
00843                         return 0;
00844         }
00845 
00846         if (speed >= XFER_UDMA_0)
00847                 cris_ide_set_speed(TYPE_UDMA, cyc, dvs, 0);
00848         else
00849                 cris_ide_set_speed(TYPE_DMA, 0, strobe, hold);  
00850 
00851         return 0;
00852 }
00853 
00854 extern int _353_io_board_present;
00855 
00856 void __init
00857 init_e100_ide (void)
00858 {
00859         hw_regs_t hw;
00860         int ide_offsets[IDE_NR_PORTS];
00861         int h;
00862         int i;
00863 
00864         printk("ide: ETRAX FS built-in ATA DMA controller\n");
00865         if(_353_io_board_present == 0) {
00866                 printk("ide: Elphel 353 io extension board not found, skipping initialization\n");
00867                 return;
00868         }
00869 
00870         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
00871                 ide_offsets[i] = cris_ide_reg_addr(i, 0, 1);
00872 
00873         /* the IDE control register is at ATA address 6, with CS1 active instead of CS0 */
00874         ide_offsets[IDE_CONTROL_OFFSET] = cris_ide_reg_addr(6, 1, 0);
00875 
00876         /* first fill in some stuff in the ide_hwifs fields */
00877 
00878         for(h = 0; h < MAX_HWIFS; h++) {
00879                 ide_hwif_t *hwif = &ide_hwifs[h];
00880                 memset(&hw, 0, sizeof(hw));
00881                 ide_setup_ports(&hw, cris_ide_base_address(h), 
00882                                 ide_offsets,
00883                                 0, 0, cris_ide_ack_intr,
00884                                 IRQ);
00885                 ide_register_hw(&hw, &hwif);
00886                 hwif->irq = IRQ;
00887                 hwif->mmio = 2;
00888                 hwif->chipset = ide_etrax100;
00889                 hwif->tuneproc = &tune_cris_ide;
00890                 hwif->speedproc = &speed_cris_ide;
00891                 hwif->ata_input_data = &cris_ide_input_data;
00892                 hwif->ata_output_data = &cris_ide_output_data;
00893                 hwif->atapi_input_bytes = &cris_atapi_input_bytes;
00894                 hwif->atapi_output_bytes = &cris_atapi_output_bytes;
00895                 hwif->ide_dma_check = &cris_dma_check;
00896                 hwif->ide_dma_end = &cris_dma_end;
00897                 hwif->dma_setup = &cris_dma_setup;
00898                 hwif->dma_exec_cmd = &cris_dma_exec_cmd;
00899                 hwif->ide_dma_test_irq = &cris_dma_test_irq;
00900                 hwif->dma_start = &cris_dma_start;
00901                 hwif->OUTB = &cris_ide_outb;
00902                 hwif->OUTW = &cris_ide_outw;
00903                 hwif->OUTBSYNC = &cris_ide_outbsync;
00904                 hwif->INB = &cris_ide_inb;
00905                 hwif->INW = &cris_ide_inw;
00906                 hwif->ide_dma_host_off = &cris_dma_host_off;
00907                 hwif->ide_dma_host_on = &cris_dma_host_on;
00908                 hwif->ide_dma_off_quietly = &cris_dma_off;
00909                 hwif->ide_dma_on = &cris_dma_on;
00910                 hwif->udma_four = 0;
00911                 hwif->ultra_mask = cris_ultra_mask;
00912 //              hwif->mwdma_mask = 0x07; /* Multiword DMA 0-2 */
00913                 hwif->mwdma_mask = 0x07; /* Multiword DMA 0-2 */
00914 //              hwif->swdma_mask = 0x07; /* Singleword DMA 0-2 */
00915                 hwif->swdma_mask = 0x07; /* Singleword DMA 0-2 */
00916                 hwif->rqsize = 256;
00917         }
00918 
00919         /* Reset pulse */
00920         cris_ide_reset(0);
00921         udelay(25);
00922         cris_ide_reset(1);
00923 
00924         cris_ide_init();
00925 
00926         cris_ide_set_speed(TYPE_PIO, ATA_PIO4_SETUP, ATA_PIO4_STROBE, ATA_PIO4_HOLD);
00927         cris_ide_set_speed(TYPE_DMA, 0, ATA_DMA2_STROBE, ATA_DMA2_HOLD);
00928         cris_ide_set_speed(TYPE_UDMA, ATA_UDMA2_CYC, ATA_UDMA2_DVS, 0);
00929 }
00930 
00931 static int cris_dma_host_off (ide_drive_t *drive)
00932 {
00933         return 0;
00934 }
00935 
00936 static int cris_dma_host_on (ide_drive_t *drive)
00937 {
00938         return 0;
00939 }
00940 
00941 static int cris_dma_off (ide_drive_t *drive)
00942 {
00943         drive->using_dma = 0;
00944         return 0;
00945 }
00946 
00947 static int cris_dma_on (ide_drive_t *drive)
00948 {
00949         drive->using_dma = 1;
00950         return 0;
00951 }
00952 
00953 
00954 static cris_dma_descr_type mydescr __attribute__ ((__aligned__(16)));
00955 
00956 /*
00957  * The following routines are mainly used by the ATAPI drivers.
00958  *
00959  * These routines will round up any request for an odd number of bytes,
00960  * so if an odd bytecount is specified, be sure that there's at least one
00961  * extra byte allocated for the buffer.
00962  */
00963 static void
00964 cris_atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
00965 {
00966         D(printk("atapi_input_bytes, buffer 0x%x, count %d\n",
00967                  buffer, bytecount));
00968 
00969         if(bytecount & 1) {
00970                 printk("warning, odd bytecount in cdrom_in_bytes = %d.\n", bytecount);
00971                 bytecount++; /* to round off */
00972         }
00973 
00974         /* setup DMA and start transfer */
00975 
00976         cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1);
00977         cris_ide_start_dma(drive, &mydescr, 1, TYPE_PIO, bytecount);
00978 
00979         /* wait for completion */
00980         LED_DISK_READ(1);
00981         cris_ide_wait_dma(1);
00982         LED_DISK_READ(0);
00983 }
00984 
00985 static void
00986 cris_atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
00987 {
00988         D(printk("atapi_output_bytes, buffer 0x%x, count %d\n",
00989                  buffer, bytecount));
00990 
00991         if(bytecount & 1) {
00992                 printk("odd bytecount %d in atapi_out_bytes!\n", bytecount);
00993                 bytecount++;
00994         }
00995 
00996         cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1);
00997         cris_ide_start_dma(drive, &mydescr, 0, TYPE_PIO, bytecount);
00998 
00999         /* wait for completion */
01000 
01001         LED_DISK_WRITE(1);
01002         LED_DISK_READ(1);
01003         cris_ide_wait_dma(0);
01004         LED_DISK_WRITE(0);
01005 }
01006 
01007 /*
01008  * This is used for most PIO data transfers *from* the IDE interface
01009  */
01010 static void
01011 cris_ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
01012 {
01013         cris_atapi_input_bytes(drive, buffer, wcount << 2);
01014 }
01015 
01016 /*
01017  * This is used for most PIO data transfers *to* the IDE interface
01018  */
01019 static void
01020 cris_ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
01021 {
01022         cris_atapi_output_bytes(drive, buffer, wcount << 2);
01023 }
01024 
01025 /* we only have one DMA channel on the chip for ATA, so we can keep these statically */
01026 static cris_dma_descr_type ata_descrs[MAX_DMA_DESCRS] __attribute__ ((__aligned__(16)));
01027 static unsigned int ata_tot_size;
01028 
01029 /*
01030  * cris_ide_build_dmatable() prepares a dma request.
01031  * Returns 0 if all went okay, returns 1 otherwise.
01032  */
01033 static int cris_ide_build_dmatable (ide_drive_t *drive)
01034 {
01035         ide_hwif_t *hwif = drive->hwif;
01036         struct scatterlist* sg;
01037         struct request *rq  = drive->hwif->hwgroup->rq;
01038         unsigned long size, addr;
01039         unsigned int count = 0;
01040         int i = 0;
01041 
01042         sg = hwif->sg_table;
01043 
01044         ata_tot_size = 0;
01045 
01046         ide_map_sg(drive, rq);
01047         i = hwif->sg_nents;
01048 
01049         while(i) {
01050                 /*
01051                  * Determine addr and size of next buffer area.  We assume that
01052                  * individual virtual buffers are always composed linearly in
01053                  * physical memory.  For example, we assume that any 8kB buffer
01054                  * is always composed of two adjacent physical 4kB pages rather
01055                  * than two possibly non-adjacent physical 4kB pages.
01056                  */
01057                 /* group sequential buffers into one large buffer */
01058                 addr = page_to_phys(sg->page) + sg->offset;
01059                 size = sg_dma_len(sg);
01060                 while (sg++, --i) {
01061                         if ((addr + size) != page_to_phys(sg->page) + sg->offset)
01062                                 break;
01063                         size += sg_dma_len(sg);
01064                 }
01065 
01066                 /* rw_trf_cnt cannot be more than 131072 words per transfer, 
01067                    (- 1 word for UDMA CRC) so in that case we need to either:
01068                    1) use a DMA interrupt to re-trigger rw_trf_cnt and continue with
01069                       the descriptors, or
01070                    2) simply do the request here, and get dma_intr to only ide_end_request on
01071                       those blocks that were actually set-up for transfer.
01072                       (The ide framework will issue a new request for the remainder)
01073                 */
01074 
01075                 if(ata_tot_size + size > 262140) {
01076                         printk("too large total ATA DMA request, %d + %d!\n", ata_tot_size, (int)size);
01077                         return 1;
01078                 }
01079 
01080                 /* If size > MAX_DESCR_SIZE it has to be splitted into new descriptors. */
01081 
01082                 while (size > MAX_DESCR_SIZE) {
01083                         /* did we run out of descriptors? */
01084                         if(count >= MAX_DMA_DESCRS) {
01085                                 printk("%s: too few DMA descriptors\n", drive->name);
01086                                 return 1;
01087                         }
01088                         cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, MAX_DESCR_SIZE, 0);
01089                         count++;
01090                         ata_tot_size += MAX_DESCR_SIZE;
01091                         size -= MAX_DESCR_SIZE;
01092                         addr += MAX_DESCR_SIZE;
01093                 }
01094 
01095                 /* did we run out of descriptors? */
01096                 if(count >= MAX_DMA_DESCRS) {
01097                         printk("%s: too few DMA descriptors\n", drive->name);
01098                         return 1;
01099                 }
01100                 cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, size,i ? 0 : 1);
01101                 count++;
01102                 ata_tot_size += size;
01103         }
01104 
01105         if (count) {
01106                 /* return and say all is ok */
01107                 return 0;
01108         }
01109 
01110         printk("%s: empty DMA table?\n", drive->name);
01111         return 1;       /* let the PIO routines handle this weirdness */
01112 }
01113 
01114 static int cris_config_drive_for_dma (ide_drive_t *drive)
01115 {
01116         u8 speed = ide_dma_speed(drive, 1);
01117 
01118         if (!speed)
01119                 return 0;
01120 
01121         speed_cris_ide(drive, speed);
01122         ide_config_drive_speed(drive, speed);
01123 
01124         return ide_dma_enable(drive);
01125 }
01126 
01127 /*
01128  * cris_dma_intr() is the handler for disk read/write DMA interrupts
01129  */
01130 static ide_startstop_t cris_dma_intr (ide_drive_t *drive)
01131 {
01132         LED_DISK_READ(0);
01133         LED_DISK_WRITE(0);
01134 
01135         return ide_dma_intr(drive);
01136 }
01137 
01138 /*
01139  * Functions below initiates/aborts DMA read/write operations on a drive.
01140  *
01141  * The caller is assumed to have selected the drive and programmed the drive's
01142  * sector address using CHS or LBA.  All that remains is to prepare for DMA
01143  * and then issue the actual read/write DMA/PIO command to the drive.
01144  *
01145  * For ATAPI devices, we just prepare for DMA and return. The caller should
01146  * then issue the packet command to the drive and call us again with
01147  * cris_dma_start afterwards.
01148  *
01149  * Returns 0 if all went well.
01150  * Returns 1 if DMA read/write could not be started, in which case
01151  * the caller should revert to PIO for the current request.
01152  */
01153 
01154 static int cris_dma_check(ide_drive_t *drive)
01155 {
01156         ide_hwif_t *hwif = drive->hwif;
01157         struct hd_driveid* id = drive->id;
01158 
01159         if (id && (id->capability & 1)) {
01160                 if (ide_use_dma(drive)) {
01161                         if (cris_config_drive_for_dma(drive)) {
01162                                 if (hwif->ide_dma_on)
01163                                         return hwif->ide_dma_on(drive);
01164                                 else
01165                                         return 1;
01166                         }
01167                 }
01168         }
01169 
01170         return hwif->ide_dma_off_quietly(drive);
01171 }
01172 
01173 static int cris_dma_end(ide_drive_t *drive)
01174 {
01175         drive->waiting_for_dma = 0;
01176         return 0;
01177 }
01178 
01179 static int cris_dma_setup(ide_drive_t *drive)
01180 {
01181         struct request *rq = drive->hwif->hwgroup->rq;
01182 
01183         cris_ide_initialize_dma(!rq_data_dir(rq));
01184         if (cris_ide_build_dmatable (drive)) {
01185                 ide_map_sg(drive, rq);
01186                 return 1;
01187         }
01188 
01189         drive->waiting_for_dma = 1;
01190         return 0;
01191 }
01192 
01193 static void cris_dma_exec_cmd(ide_drive_t *drive, u8 command)
01194 {
01195         /* set the irq handler which will finish the request when DMA is done */
01196         ide_set_handler(drive, &cris_dma_intr, WAIT_CMD, NULL);
01197 
01198         /* issue cmd to drive */
01199         cris_ide_outb(command, IDE_COMMAND_REG);
01200 }
01201 
01202 static void cris_dma_start(ide_drive_t *drive)
01203 {
01204         struct request *rq = drive->hwif->hwgroup->rq;
01205         int writing = rq_data_dir(rq);
01206         int type = TYPE_DMA;
01207 
01208         if (drive->current_speed >= XFER_UDMA_0)
01209                 type = TYPE_UDMA;
01210 
01211         cris_ide_start_dma(drive, &ata_descrs[0], writing ? 0 : 1, type, ata_tot_size);
01212 
01213         if (writing) {
01214                 LED_DISK_WRITE(1);
01215         } else {
01216                 LED_DISK_READ(1);
01217         }
01218 }

Generated on Fri Nov 28 00:06:24 2008 for elphel by  doxygen 1.5.1