linux/drivers/ide/cris/ide-cris.c
<<
>>
Prefs
   1/*
   2 * Etrax specific IDE functions, like init and PIO-mode setting etc.
   3 * Almost the entire ide.c is used for the rest of the Etrax ATA driver.
   4 * Copyright (c) 2000-2005 Axis Communications AB
   5 *
   6 * Authors:    Bjorn Wesen        (initial version)
   7 *             Mikael Starvik     (crisv32 port)
   8 */
   9
  10/* Regarding DMA:
  11 *
  12 * There are two forms of DMA - "DMA handshaking" between the interface and the drive,
  13 * and DMA between the memory and the interface. We can ALWAYS use the latter, since it's
  14 * something built-in in the Etrax. However only some drives support the DMA-mode handshaking
  15 * on the ATA-bus. The normal PC driver and Triton interface disables memory-if DMA when the
  16 * device can't do DMA handshaking for some stupid reason. We don't need to do that.
  17 */
  18
  19#include <linux/types.h>
  20#include <linux/kernel.h>
  21#include <linux/timer.h>
  22#include <linux/mm.h>
  23#include <linux/interrupt.h>
  24#include <linux/delay.h>
  25#include <linux/blkdev.h>
  26#include <linux/hdreg.h>
  27#include <linux/ide.h>
  28#include <linux/init.h>
  29
  30#include <asm/io.h>
  31#include <asm/dma.h>
  32
  33/* number of DMA descriptors */
  34#define MAX_DMA_DESCRS 64
  35
  36/* number of times to retry busy-flags when reading/writing IDE-registers
  37 * this can't be too high because a hung harddisk might cause the watchdog
  38 * to trigger (sometimes INB and OUTB are called with irq's disabled)
  39 */
  40
  41#define IDE_REGISTER_TIMEOUT 300
  42
  43#define LOWDB(x)
  44#define D(x)
  45
  46enum /* Transfer types */
  47{
  48        TYPE_PIO,
  49        TYPE_DMA,
  50        TYPE_UDMA
  51};
  52
  53/* CRISv32 specifics */
  54#ifdef CONFIG_ETRAX_ARCH_V32
  55#include <asm/arch/hwregs/ata_defs.h>
  56#include <asm/arch/hwregs/dma_defs.h>
  57#include <asm/arch/hwregs/dma.h>
  58#include <asm/arch/pinmux.h>
  59
  60#define ATA_UDMA2_CYC    2
  61#define ATA_UDMA2_DVS    3
  62#define ATA_UDMA1_CYC    2
  63#define ATA_UDMA1_DVS    4
  64#define ATA_UDMA0_CYC    4
  65#define ATA_UDMA0_DVS    6
  66#define ATA_DMA2_STROBE  7
  67#define ATA_DMA2_HOLD    1
  68#define ATA_DMA1_STROBE  8
  69#define ATA_DMA1_HOLD    3
  70#define ATA_DMA0_STROBE 25
  71#define ATA_DMA0_HOLD   19
  72#define ATA_PIO4_SETUP   3
  73#define ATA_PIO4_STROBE  7
  74#define ATA_PIO4_HOLD    1
  75#define ATA_PIO3_SETUP   3
  76#define ATA_PIO3_STROBE  9
  77#define ATA_PIO3_HOLD    3
  78#define ATA_PIO2_SETUP   3
  79#define ATA_PIO2_STROBE 13
  80#define ATA_PIO2_HOLD    5
  81#define ATA_PIO1_SETUP   5
  82#define ATA_PIO1_STROBE 23
  83#define ATA_PIO1_HOLD    9
  84#define ATA_PIO0_SETUP   9
  85#define ATA_PIO0_STROBE 39
  86#define ATA_PIO0_HOLD    9
  87
  88int
  89cris_ide_ack_intr(ide_hwif_t* hwif)
  90{
  91        reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2,
  92                                 int, hwif->io_ports[0]);
  93        REG_WR_INT(ata, regi_ata, rw_ack_intr, 1 << ctrl2.sel);
  94        return 1;
  95}
  96
  97static inline int
  98cris_ide_busy(void)
  99{
 100        reg_ata_rs_stat_data stat_data;
 101        stat_data = REG_RD(ata, regi_ata, rs_stat_data);
 102        return stat_data.busy;
 103}
 104
 105static inline int
 106cris_ide_ready(void)
 107{
 108        return !cris_ide_busy();
 109}
 110
 111static inline int
 112cris_ide_data_available(unsigned short* data)
 113{
 114        reg_ata_rs_stat_data stat_data;
 115        stat_data = REG_RD(ata, regi_ata, rs_stat_data);
 116        *data = stat_data.data;
 117        return stat_data.dav;
 118}
 119
 120static void
 121cris_ide_write_command(unsigned long command)
 122{
 123        REG_WR_INT(ata, regi_ata, rw_ctrl2, command); /* write data to the drive's register */
 124}
 125
 126static void
 127cris_ide_set_speed(int type, int setup, int strobe, int hold)
 128{
 129        reg_ata_rw_ctrl0 ctrl0 = REG_RD(ata, regi_ata, rw_ctrl0);
 130        reg_ata_rw_ctrl1 ctrl1 = REG_RD(ata, regi_ata, rw_ctrl1);
 131
 132        if (type == TYPE_PIO) {
 133                ctrl0.pio_setup = setup;
 134                ctrl0.pio_strb = strobe;
 135                ctrl0.pio_hold = hold;
 136        } else if (type == TYPE_DMA) {
 137                ctrl0.dma_strb = strobe;
 138                ctrl0.dma_hold = hold;
 139        } else if (type == TYPE_UDMA) {
 140                ctrl1.udma_tcyc = setup;
 141                ctrl1.udma_tdvs = strobe;
 142        }
 143        REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
 144        REG_WR(ata, regi_ata, rw_ctrl1, ctrl1);
 145}
 146
 147static unsigned long
 148cris_ide_base_address(int bus)
 149{
 150        reg_ata_rw_ctrl2 ctrl2 = {0};
 151        ctrl2.sel = bus;
 152        return REG_TYPE_CONV(int, reg_ata_rw_ctrl2, ctrl2);
 153}
 154
 155static unsigned long
 156cris_ide_reg_addr(unsigned long addr, int cs0, int cs1)
 157{
 158        reg_ata_rw_ctrl2 ctrl2 = {0};
 159        ctrl2.addr = addr;
 160        ctrl2.cs1 = cs1;
 161        ctrl2.cs0 = cs0;
 162        return REG_TYPE_CONV(int, reg_ata_rw_ctrl2, ctrl2);
 163}
 164
 165static __init void
 166cris_ide_reset(unsigned val)
 167{
 168        reg_ata_rw_ctrl0 ctrl0 = {0};
 169        ctrl0.rst = val ? regk_ata_active : regk_ata_inactive;
 170        REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
 171}
 172
 173static __init void
 174cris_ide_init(void)
 175{
 176        reg_ata_rw_ctrl0 ctrl0 = {0};
 177        reg_ata_rw_intr_mask intr_mask = {0};
 178
 179        ctrl0.en = regk_ata_yes;
 180        REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
 181
 182        intr_mask.bus0 = regk_ata_yes;
 183        intr_mask.bus1 = regk_ata_yes;
 184        intr_mask.bus2 = regk_ata_yes;
 185        intr_mask.bus3 = regk_ata_yes;
 186
 187        REG_WR(ata, regi_ata, rw_intr_mask, intr_mask);
 188
 189        crisv32_request_dma(2, "ETRAX FS built-in ATA", DMA_VERBOSE_ON_ERROR, 0, dma_ata);
 190        crisv32_request_dma(3, "ETRAX FS built-in ATA", DMA_VERBOSE_ON_ERROR, 0, dma_ata);
 191
 192        crisv32_pinmux_alloc_fixed(pinmux_ata);
 193        crisv32_pinmux_alloc_fixed(pinmux_ata0);
 194        crisv32_pinmux_alloc_fixed(pinmux_ata1);
 195        crisv32_pinmux_alloc_fixed(pinmux_ata2);
 196        crisv32_pinmux_alloc_fixed(pinmux_ata3);
 197
 198        DMA_RESET(regi_dma2);
 199        DMA_ENABLE(regi_dma2);
 200        DMA_RESET(regi_dma3);
 201        DMA_ENABLE(regi_dma3);
 202
 203        DMA_WR_CMD (regi_dma2, regk_dma_set_w_size2);
 204        DMA_WR_CMD (regi_dma3, regk_dma_set_w_size2);
 205}
 206
 207static dma_descr_context mycontext __attribute__ ((__aligned__(32)));
 208
 209#define cris_dma_descr_type dma_descr_data
 210#define cris_pio_read regk_ata_rd
 211#define cris_ultra_mask 0x7
 212#define MAX_DESCR_SIZE 0xffffffffUL
 213
 214static unsigned long
 215cris_ide_get_reg(unsigned long reg)
 216{
 217        return (reg & 0x0e000000) >> 25;
 218}
 219
 220static void
 221cris_ide_fill_descriptor(cris_dma_descr_type *d, void* buf, unsigned int len, int last)
 222{
 223        d->buf = (char*)virt_to_phys(buf);
 224        d->after = d->buf + len;
 225        d->eol = last;
 226}
 227
 228static void
 229cris_ide_start_dma(ide_drive_t *drive, cris_dma_descr_type *d, int dir,int type,int len)
 230{
 231        reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2, int, IDE_DATA_REG);
 232        reg_ata_rw_trf_cnt trf_cnt = {0};
 233
 234        mycontext.saved_data = (dma_descr_data*)virt_to_phys(d);
 235        mycontext.saved_data_buf = d->buf;
 236        /* start the dma channel */
 237        DMA_START_CONTEXT(dir ? regi_dma3 : regi_dma2, virt_to_phys(&mycontext));
 238
 239        /* initiate a multi word dma read using PIO handshaking */
 240        trf_cnt.cnt = len >> 1;
 241        /* Due to a "feature" the transfer count has to be one extra word for UDMA. */
 242        if (type == TYPE_UDMA)
 243                trf_cnt.cnt++;
 244        REG_WR(ata, regi_ata, rw_trf_cnt, trf_cnt);
 245
 246        ctrl2.rw = dir ? regk_ata_rd : regk_ata_wr;
 247        ctrl2.trf_mode = regk_ata_dma;
 248        ctrl2.hsh = type == TYPE_PIO ? regk_ata_pio :
 249                    type == TYPE_DMA ? regk_ata_dma : regk_ata_udma;
 250        ctrl2.multi = regk_ata_yes;
 251        ctrl2.dma_size = regk_ata_word;
 252        REG_WR(ata, regi_ata, rw_ctrl2, ctrl2);
 253}
 254
 255static void
 256cris_ide_wait_dma(int dir)
 257{
 258        reg_dma_rw_stat status;
 259        do
 260        {
 261                status = REG_RD(dma, dir ? regi_dma3 : regi_dma2, rw_stat);
 262        } while(status.list_state != regk_dma_data_at_eol);
 263}
 264
 265static int cris_dma_test_irq(ide_drive_t *drive)
 266{
 267        int intr = REG_RD_INT(ata, regi_ata, r_intr);
 268        reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2, int, IDE_DATA_REG);
 269        return intr & (1 << ctrl2.sel) ? 1 : 0;
 270}
 271
 272static void cris_ide_initialize_dma(int dir)
 273{
 274}
 275
 276#else
 277/* CRISv10 specifics */
 278#include <asm/arch/svinto.h>
 279#include <asm/arch/io_interface_mux.h>
 280
 281/* PIO timing (in R_ATA_CONFIG)
 282 *
 283 *                        _____________________________
 284 * ADDRESS :     ________/
 285 *
 286 *                            _______________
 287 * DIOR    :     ____________/               \__________
 288 *
 289 *                               _______________
 290 * DATA    :     XXXXXXXXXXXXXXXX_______________XXXXXXXX
 291 *
 292 *
 293 * DIOR is unbuffered while address and data is buffered.
 294 * This creates two problems:
 295 * 1. The DIOR pulse is to early (because it is unbuffered)
 296 * 2. The rise time of DIOR is long
 297 *
 298 * There are at least three different plausible solutions
 299 * 1. Use a pad capable of larger currents in Etrax
 300 * 2. Use an external buffer
 301 * 3. Make the strobe pulse longer
 302 *
 303 * Some of the strobe timings below are modified to compensate
 304 * for this. This implies a slight performance decrease.
 305 *
 306 * THIS SHOULD NEVER BE CHANGED!
 307 *
 308 * TODO: Is this true for the latest LX boards still ?
 309 */
 310
 311#define ATA_UDMA2_CYC    0 /* No UDMA supported, just to make it compile. */
 312#define ATA_UDMA2_DVS    0
 313#define ATA_UDMA1_CYC    0
 314#define ATA_UDMA1_DVS    0
 315#define ATA_UDMA0_CYC    0
 316#define ATA_UDMA0_DVS    0
 317#define ATA_DMA2_STROBE  4
 318#define ATA_DMA2_HOLD    0
 319#define ATA_DMA1_STROBE  4
 320#define ATA_DMA1_HOLD    1
 321#define ATA_DMA0_STROBE 12
 322#define ATA_DMA0_HOLD    9
 323#define ATA_PIO4_SETUP   1
 324#define ATA_PIO4_STROBE  5
 325#define ATA_PIO4_HOLD    0
 326#define ATA_PIO3_SETUP   1
 327#define ATA_PIO3_STROBE  5
 328#define ATA_PIO3_HOLD    1
 329#define ATA_PIO2_SETUP   1
 330#define ATA_PIO2_STROBE  6
 331#define ATA_PIO2_HOLD    2
 332#define ATA_PIO1_SETUP   2
 333#define ATA_PIO1_STROBE 11
 334#define ATA_PIO1_HOLD    4
 335#define ATA_PIO0_SETUP   4
 336#define ATA_PIO0_STROBE 19
 337#define ATA_PIO0_HOLD    4
 338
 339int
 340cris_ide_ack_intr(ide_hwif_t* hwif)
 341{
 342        return 1;
 343}
 344
 345static inline int
 346cris_ide_busy(void)
 347{
 348        return *R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy) ;
 349}
 350
 351static inline int
 352cris_ide_ready(void)
 353{
 354        return *R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, tr_rdy) ;
 355}
 356
 357static inline int
 358cris_ide_data_available(unsigned short* data)
 359{
 360        unsigned long status = *R_ATA_STATUS_DATA;
 361        *data = (unsigned short)status;
 362        return status & IO_MASK(R_ATA_STATUS_DATA, dav);
 363}
 364
 365static void
 366cris_ide_write_command(unsigned long command)
 367{
 368        *R_ATA_CTRL_DATA = command;
 369}
 370
 371static void
 372cris_ide_set_speed(int type, int setup, int strobe, int hold)
 373{
 374        static int pio_setup = ATA_PIO4_SETUP;
 375        static int pio_strobe = ATA_PIO4_STROBE;
 376        static int pio_hold = ATA_PIO4_HOLD;
 377        static int dma_strobe = ATA_DMA2_STROBE;
 378        static int dma_hold = ATA_DMA2_HOLD;
 379
 380        if (type == TYPE_PIO) {
 381                pio_setup = setup;
 382                pio_strobe = strobe;
 383                pio_hold = hold;
 384        } else if (type == TYPE_DMA) {
 385                dma_strobe = strobe;
 386          dma_hold = hold;
 387        }
 388        *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) |
 389          IO_FIELD( R_ATA_CONFIG, dma_strobe, dma_strobe ) |
 390                IO_FIELD( R_ATA_CONFIG, dma_hold,   dma_hold ) |
 391                IO_FIELD( R_ATA_CONFIG, pio_setup,  pio_setup ) |
 392                IO_FIELD( R_ATA_CONFIG, pio_strobe, pio_strobe ) |
 393                IO_FIELD( R_ATA_CONFIG, pio_hold,   pio_hold ) );
 394}
 395
 396static unsigned long
 397cris_ide_base_address(int bus)
 398{
 399        return IO_FIELD(R_ATA_CTRL_DATA, sel, bus);
 400}
 401
 402static unsigned long
 403cris_ide_reg_addr(unsigned long addr, int cs0, int cs1)
 404{
 405        return IO_FIELD(R_ATA_CTRL_DATA, addr, addr) |
 406               IO_FIELD(R_ATA_CTRL_DATA, cs0, cs0) |
 407               IO_FIELD(R_ATA_CTRL_DATA, cs1, cs1);
 408}
 409
 410static __init void
 411cris_ide_reset(unsigned val)
 412{
 413#ifdef CONFIG_ETRAX_IDE_G27_RESET
 414        REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, 27, val);
 415#endif
 416#ifdef CONFIG_ETRAX_IDE_PB7_RESET
 417        port_pb_dir_shadow = port_pb_dir_shadow |
 418                IO_STATE(R_PORT_PB_DIR, dir7, output);
 419        *R_PORT_PB_DIR = port_pb_dir_shadow;
 420        REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 7, val);
 421#endif
 422}
 423
 424static __init void
 425cris_ide_init(void)
 426{
 427        volatile unsigned int dummy;
 428
 429        *R_ATA_CTRL_DATA = 0;
 430        *R_ATA_TRANSFER_CNT = 0;
 431        *R_ATA_CONFIG = 0;
 432
 433        if (cris_request_io_interface(if_ata, "ETRAX100LX IDE")) {
 434                printk(KERN_CRIT "ide: Failed to get IO interface\n");
 435                return;
 436        } else if (cris_request_dma(ATA_TX_DMA_NBR,
 437                                          "ETRAX100LX IDE TX",
 438                                          DMA_VERBOSE_ON_ERROR,
 439                                          dma_ata)) {
 440                cris_free_io_interface(if_ata);
 441                printk(KERN_CRIT "ide: Failed to get Tx DMA channel\n");
 442                return;
 443        } else if (cris_request_dma(ATA_RX_DMA_NBR,
 444                                          "ETRAX100LX IDE RX",
 445                                          DMA_VERBOSE_ON_ERROR,
 446                                          dma_ata)) {
 447                cris_free_dma(ATA_TX_DMA_NBR, "ETRAX100LX IDE Tx");
 448                cris_free_io_interface(if_ata);
 449                printk(KERN_CRIT "ide: Failed to get Rx DMA channel\n");
 450                return;
 451        }
 452
 453        /* make a dummy read to set the ata controller in a proper state */
 454        dummy = *R_ATA_STATUS_DATA;
 455
 456        *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ));
 457        *R_ATA_CTRL_DATA = ( IO_STATE( R_ATA_CTRL_DATA, rw,   read) |
 458                             IO_FIELD( R_ATA_CTRL_DATA, addr, 1   ) );
 459
 460        while(*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy)); /* wait for busy flag*/
 461
 462        *R_IRQ_MASK0_SET = ( IO_STATE( R_IRQ_MASK0_SET, ata_irq0, set ) |
 463                             IO_STATE( R_IRQ_MASK0_SET, ata_irq1, set ) |
 464                             IO_STATE( R_IRQ_MASK0_SET, ata_irq2, set ) |
 465                             IO_STATE( R_IRQ_MASK0_SET, ata_irq3, set ) );
 466
 467        /* reset the dma channels we will use */
 468
 469        RESET_DMA(ATA_TX_DMA_NBR);
 470        RESET_DMA(ATA_RX_DMA_NBR);
 471        WAIT_DMA(ATA_TX_DMA_NBR);
 472        WAIT_DMA(ATA_RX_DMA_NBR);
 473}
 474
 475#define cris_dma_descr_type etrax_dma_descr
 476#define cris_pio_read IO_STATE(R_ATA_CTRL_DATA, rw, read)
 477#define cris_ultra_mask 0x0
 478#define MAX_DESCR_SIZE 0x10000UL
 479
 480static unsigned long
 481cris_ide_get_reg(unsigned long reg)
 482{
 483        return (reg & 0x0e000000) >> 25;
 484}
 485
 486static void
 487cris_ide_fill_descriptor(cris_dma_descr_type *d, void* buf, unsigned int len, int last)
 488{
 489        d->buf = virt_to_phys(buf);
 490        d->sw_len = len == MAX_DESCR_SIZE ? 0 : len;
 491        if (last)
 492                d->ctrl |= d_eol;
 493}
 494
 495static void cris_ide_start_dma(ide_drive_t *drive, cris_dma_descr_type *d, int dir, int type, int len)
 496{
 497        unsigned long cmd;
 498
 499        if (dir) {
 500                /* need to do this before RX DMA due to a chip bug
 501                 * it is enough to just flush the part of the cache that
 502                 * corresponds to the buffers we start, but since HD transfers
 503                 * usually are more than 8 kB, it is easier to optimize for the
 504                 * normal case and just flush the entire cache. its the only
 505                 * way to be sure! (OB movie quote)
 506                 */
 507                flush_etrax_cache();
 508                *R_DMA_CH3_FIRST = virt_to_phys(d);
 509                *R_DMA_CH3_CMD   = IO_STATE(R_DMA_CH3_CMD, cmd, start);
 510
 511        } else {
 512                *R_DMA_CH2_FIRST = virt_to_phys(d);
 513                *R_DMA_CH2_CMD   = IO_STATE(R_DMA_CH2_CMD, cmd, start);
 514        }
 515
 516        /* initiate a multi word dma read using DMA handshaking */
 517
 518        *R_ATA_TRANSFER_CNT =
 519                IO_FIELD(R_ATA_TRANSFER_CNT, count, len >> 1);
 520
 521        cmd = dir ? IO_STATE(R_ATA_CTRL_DATA, rw, read) : IO_STATE(R_ATA_CTRL_DATA, rw, write);
 522        cmd |= type == TYPE_PIO ? IO_STATE(R_ATA_CTRL_DATA, handsh, pio) :
 523                                  IO_STATE(R_ATA_CTRL_DATA, handsh, dma);
 524        *R_ATA_CTRL_DATA =
 525                cmd |
 526                IO_FIELD(R_ATA_CTRL_DATA, data, IDE_DATA_REG) |
 527                IO_STATE(R_ATA_CTRL_DATA, src_dst,  dma)  |
 528                IO_STATE(R_ATA_CTRL_DATA, multi,    on)   |
 529                IO_STATE(R_ATA_CTRL_DATA, dma_size, word);
 530}
 531
 532static void
 533cris_ide_wait_dma(int dir)
 534{
 535        if (dir)
 536                WAIT_DMA(ATA_RX_DMA_NBR);
 537        else
 538                WAIT_DMA(ATA_TX_DMA_NBR);
 539}
 540
 541static int cris_dma_test_irq(ide_drive_t *drive)
 542{
 543        int intr = *R_IRQ_MASK0_RD;
 544        int bus = IO_EXTRACT(R_ATA_CTRL_DATA, sel, IDE_DATA_REG);
 545        return intr & (1 << (bus + IO_BITNR(R_IRQ_MASK0_RD, ata_irq0))) ? 1 : 0;
 546}
 547
 548
 549static void cris_ide_initialize_dma(int dir)
 550{
 551        if (dir)
 552        {
 553                RESET_DMA(ATA_RX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
 554                WAIT_DMA(ATA_RX_DMA_NBR);
 555        }
 556        else
 557        {
 558                RESET_DMA(ATA_TX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
 559                WAIT_DMA(ATA_TX_DMA_NBR);
 560        }
 561}
 562
 563#endif
 564
 565void
 566cris_ide_outw(unsigned short data, unsigned long reg) {
 567        int timeleft;
 568
 569        LOWDB(printk("ow: data 0x%x, reg 0x%x\n", data, reg));
 570
 571        /* note the lack of handling any timeouts. we stop waiting, but we don't
 572         * really notify anybody.
 573         */
 574
 575        timeleft = IDE_REGISTER_TIMEOUT;
 576        /* wait for busy flag */
 577        do {
 578                timeleft--;
 579        } while(timeleft && cris_ide_busy());
 580
 581        /*
 582         * Fall through at a timeout, so the ongoing command will be
 583         * aborted by the write below, which is expected to be a dummy
 584         * command to the command register.  This happens when a faulty
 585         * drive times out on a command.  See comment on timeout in
 586         * INB.
 587         */
 588        if(!timeleft)
 589                printk("ATA timeout reg 0x%lx := 0x%x\n", reg, data);
 590
 591        cris_ide_write_command(reg|data); /* write data to the drive's register */
 592
 593        timeleft = IDE_REGISTER_TIMEOUT;
 594        /* wait for transmitter ready */
 595        do {
 596                timeleft--;
 597        } while(timeleft && !cris_ide_ready());
 598}
 599
 600void
 601cris_ide_outb(unsigned char data, unsigned long reg)
 602{
 603        cris_ide_outw(data, reg);
 604}
 605
 606void
 607cris_ide_outbsync(ide_drive_t *drive, u8 addr, unsigned long port)
 608{
 609        cris_ide_outw(addr, port);
 610}
 611
 612unsigned short
 613cris_ide_inw(unsigned long reg) {
 614        int timeleft;
 615        unsigned short val;
 616
 617        timeleft = IDE_REGISTER_TIMEOUT;
 618        /* wait for busy flag */
 619        do {
 620                timeleft--;
 621        } while(timeleft && cris_ide_busy());
 622
 623        if(!timeleft) {
 624                /*
 625                 * If we're asked to read the status register, like for
 626                 * example when a command does not complete for an
 627                 * extended time, but the ATA interface is stuck in a
 628                 * busy state at the *ETRAX* ATA interface level (as has
 629                 * happened repeatedly with at least one bad disk), then
 630                 * the best thing to do is to pretend that we read
 631                 * "busy" in the status register, so the IDE driver will
 632                 * time-out, abort the ongoing command and perform a
 633                 * reset sequence.  Note that the subsequent OUT_BYTE
 634                 * call will also timeout on busy, but as long as the
 635                 * write is still performed, everything will be fine.
 636                 */
 637                if (cris_ide_get_reg(reg) == IDE_STATUS_OFFSET)
 638                        return BUSY_STAT;
 639                else
 640                        /* For other rare cases we assume 0 is good enough.  */
 641                        return 0;
 642        }
 643
 644        cris_ide_write_command(reg | cris_pio_read);
 645
 646        timeleft = IDE_REGISTER_TIMEOUT;
 647        /* wait for available */
 648        do {
 649                timeleft--;
 650        } while(timeleft && !cris_ide_data_available(&val));
 651
 652        if(!timeleft)
 653                return 0;
 654
 655        LOWDB(printk("inb: 0x%x from reg 0x%x\n", val & 0xff, reg));
 656
 657        return val;
 658}
 659
 660unsigned char
 661cris_ide_inb(unsigned long reg)
 662{
 663        return (unsigned char)cris_ide_inw(reg);
 664}
 665
 666static int cris_dma_end (ide_drive_t *drive);
 667static int cris_dma_setup (ide_drive_t *drive);
 668static void cris_dma_exec_cmd (ide_drive_t *drive, u8 command);
 669static int cris_dma_test_irq(ide_drive_t *drive);
 670static void cris_dma_start(ide_drive_t *drive);
 671static void cris_ide_input_data (ide_drive_t *drive, void *, unsigned int);
 672static void cris_ide_output_data (ide_drive_t *drive, void *, unsigned int);
 673static void cris_atapi_input_bytes(ide_drive_t *drive, void *, unsigned int);
 674static void cris_atapi_output_bytes(ide_drive_t *drive, void *, unsigned int);
 675
 676static void cris_dma_host_set(ide_drive_t *drive, int on)
 677{
 678}
 679
 680static void cris_set_pio_mode(ide_drive_t *drive, const u8 pio)
 681{
 682        int setup, strobe, hold;
 683
 684        switch(pio)
 685        {
 686                case 0:
 687                        setup = ATA_PIO0_SETUP;
 688                        strobe = ATA_PIO0_STROBE;
 689                        hold = ATA_PIO0_HOLD;
 690                        break;
 691                case 1:
 692                        setup = ATA_PIO1_SETUP;
 693                        strobe = ATA_PIO1_STROBE;
 694                        hold = ATA_PIO1_HOLD;
 695                        break;
 696                case 2:
 697                        setup = ATA_PIO2_SETUP;
 698                        strobe = ATA_PIO2_STROBE;
 699                        hold = ATA_PIO2_HOLD;
 700                        break;
 701                case 3:
 702                        setup = ATA_PIO3_SETUP;
 703                        strobe = ATA_PIO3_STROBE;
 704                        hold = ATA_PIO3_HOLD;
 705                        break;
 706                case 4:
 707                        setup = ATA_PIO4_SETUP;
 708                        strobe = ATA_PIO4_STROBE;
 709                        hold = ATA_PIO4_HOLD;
 710                        break;
 711                default:
 712                        return;
 713        }
 714
 715        cris_ide_set_speed(TYPE_PIO, setup, strobe, hold);
 716}
 717
 718static void cris_set_dma_mode(ide_drive_t *drive, const u8 speed)
 719{
 720        int cyc = 0, dvs = 0, strobe = 0, hold = 0;
 721
 722        switch(speed)
 723        {
 724                case XFER_UDMA_0:
 725                        cyc = ATA_UDMA0_CYC;
 726                        dvs = ATA_UDMA0_DVS;
 727                        break;
 728                case XFER_UDMA_1:
 729                        cyc = ATA_UDMA1_CYC;
 730                        dvs = ATA_UDMA1_DVS;
 731                        break;
 732                case XFER_UDMA_2:
 733                        cyc = ATA_UDMA2_CYC;
 734                        dvs = ATA_UDMA2_DVS;
 735                        break;
 736                case XFER_MW_DMA_0:
 737                        strobe = ATA_DMA0_STROBE;
 738                        hold = ATA_DMA0_HOLD;
 739                        break;
 740                case XFER_MW_DMA_1:
 741                        strobe = ATA_DMA1_STROBE;
 742                        hold = ATA_DMA1_HOLD;
 743                        break;
 744                case XFER_MW_DMA_2:
 745                        strobe = ATA_DMA2_STROBE;
 746                        hold = ATA_DMA2_HOLD;
 747                        break;
 748        }
 749
 750        if (speed >= XFER_UDMA_0)
 751                cris_ide_set_speed(TYPE_UDMA, cyc, dvs, 0);
 752        else
 753                cris_ide_set_speed(TYPE_DMA, 0, strobe, hold);
 754}
 755
 756static void __init cris_setup_ports(hw_regs_t *hw, unsigned long base)
 757{
 758        int i;
 759
 760        memset(hw, 0, sizeof(*hw));
 761
 762        for (i = 0; i <= 7; i++)
 763                hw->io_ports[i] = base + cris_ide_reg_addr(i, 0, 1);
 764
 765        /*
 766         * the IDE control register is at ATA address 6,
 767         * with CS1 active instead of CS0
 768         */
 769        hw->io_ports[IDE_CONTROL_OFFSET] = base + cris_ide_reg_addr(6, 1, 0);
 770
 771        hw->irq = ide_default_irq(0);
 772        hw->ack_intr = cris_ide_ack_intr;
 773}
 774
 775static const struct ide_port_info cris_port_info __initdata = {
 776        .chipset                = ide_etrax100,
 777        .host_flags             = IDE_HFLAG_NO_ATAPI_DMA |
 778                                  IDE_HFLAG_NO_DMA, /* no SFF-style DMA */
 779        .pio_mask               = ATA_PIO4,
 780        .udma_mask              = cris_ultra_mask,
 781        .mwdma_mask             = ATA_MWDMA2,
 782};
 783
 784static int __init init_e100_ide(void)
 785{
 786        hw_regs_t hw;
 787        int h;
 788        u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
 789
 790        printk("ide: ETRAX FS built-in ATA DMA controller\n");
 791
 792        for (h = 0; h < 4; h++) {
 793                ide_hwif_t *hwif = NULL;
 794
 795                cris_setup_ports(&hw, cris_ide_base_address(h));
 796
 797                hwif = ide_find_port(hw.io_ports[IDE_DATA_OFFSET]);
 798                if (hwif == NULL)
 799                        continue;
 800                ide_init_port_data(hwif, hwif->index);
 801                ide_init_port_hw(hwif, &hw);
 802                hwif->mmio = 1;
 803                hwif->set_pio_mode = &cris_set_pio_mode;
 804                hwif->set_dma_mode = &cris_set_dma_mode;
 805                hwif->ata_input_data = &cris_ide_input_data;
 806                hwif->ata_output_data = &cris_ide_output_data;
 807                hwif->atapi_input_bytes = &cris_atapi_input_bytes;
 808                hwif->atapi_output_bytes = &cris_atapi_output_bytes;
 809                hwif->dma_host_set = &cris_dma_host_set;
 810                hwif->ide_dma_end = &cris_dma_end;
 811                hwif->dma_setup = &cris_dma_setup;
 812                hwif->dma_exec_cmd = &cris_dma_exec_cmd;
 813                hwif->ide_dma_test_irq = &cris_dma_test_irq;
 814                hwif->dma_start = &cris_dma_start;
 815                hwif->OUTB = &cris_ide_outb;
 816                hwif->OUTW = &cris_ide_outw;
 817                hwif->OUTBSYNC = &cris_ide_outbsync;
 818                hwif->INB = &cris_ide_inb;
 819                hwif->INW = &cris_ide_inw;
 820                hwif->cbl = ATA_CBL_PATA40;
 821
 822                idx[h] = hwif->index;
 823        }
 824
 825        /* Reset pulse */
 826        cris_ide_reset(0);
 827        udelay(25);
 828        cris_ide_reset(1);
 829
 830        cris_ide_init();
 831
 832        cris_ide_set_speed(TYPE_PIO, ATA_PIO4_SETUP, ATA_PIO4_STROBE, ATA_PIO4_HOLD);
 833        cris_ide_set_speed(TYPE_DMA, 0, ATA_DMA2_STROBE, ATA_DMA2_HOLD);
 834        cris_ide_set_speed(TYPE_UDMA, ATA_UDMA2_CYC, ATA_UDMA2_DVS, 0);
 835
 836        ide_device_add(idx, &cris_port_info);
 837
 838        return 0;
 839}
 840
 841static cris_dma_descr_type mydescr __attribute__ ((__aligned__(16)));
 842
 843/*
 844 * The following routines are mainly used by the ATAPI drivers.
 845 *
 846 * These routines will round up any request for an odd number of bytes,
 847 * so if an odd bytecount is specified, be sure that there's at least one
 848 * extra byte allocated for the buffer.
 849 */
 850static void
 851cris_atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
 852{
 853        D(printk("atapi_input_bytes, buffer 0x%x, count %d\n",
 854                 buffer, bytecount));
 855
 856        if(bytecount & 1) {
 857                printk("warning, odd bytecount in cdrom_in_bytes = %d.\n", bytecount);
 858                bytecount++; /* to round off */
 859        }
 860
 861        /* setup DMA and start transfer */
 862
 863        cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1);
 864        cris_ide_start_dma(drive, &mydescr, 1, TYPE_PIO, bytecount);
 865
 866        /* wait for completion */
 867        LED_DISK_READ(1);
 868        cris_ide_wait_dma(1);
 869        LED_DISK_READ(0);
 870}
 871
 872static void
 873cris_atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
 874{
 875        D(printk("atapi_output_bytes, buffer 0x%x, count %d\n",
 876                 buffer, bytecount));
 877
 878        if(bytecount & 1) {
 879                printk("odd bytecount %d in atapi_out_bytes!\n", bytecount);
 880                bytecount++;
 881        }
 882
 883        cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1);
 884        cris_ide_start_dma(drive, &mydescr, 0, TYPE_PIO, bytecount);
 885
 886        /* wait for completion */
 887
 888        LED_DISK_WRITE(1);
 889        LED_DISK_READ(1);
 890        cris_ide_wait_dma(0);
 891        LED_DISK_WRITE(0);
 892}
 893
 894/*
 895 * This is used for most PIO data transfers *from* the IDE interface
 896 */
 897static void
 898cris_ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
 899{
 900        cris_atapi_input_bytes(drive, buffer, wcount << 2);
 901}
 902
 903/*
 904 * This is used for most PIO data transfers *to* the IDE interface
 905 */
 906static void
 907cris_ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
 908{
 909        cris_atapi_output_bytes(drive, buffer, wcount << 2);
 910}
 911
 912/* we only have one DMA channel on the chip for ATA, so we can keep these statically */
 913static cris_dma_descr_type ata_descrs[MAX_DMA_DESCRS] __attribute__ ((__aligned__(16)));
 914static unsigned int ata_tot_size;
 915
 916/*
 917 * cris_ide_build_dmatable() prepares a dma request.
 918 * Returns 0 if all went okay, returns 1 otherwise.
 919 */
 920static int cris_ide_build_dmatable (ide_drive_t *drive)
 921{
 922        ide_hwif_t *hwif = drive->hwif;
 923        struct scatterlist* sg;
 924        struct request *rq  = drive->hwif->hwgroup->rq;
 925        unsigned long size, addr;
 926        unsigned int count = 0;
 927        int i = 0;
 928
 929        sg = hwif->sg_table;
 930
 931        ata_tot_size = 0;
 932
 933        ide_map_sg(drive, rq);
 934        i = hwif->sg_nents;
 935
 936        while(i) {
 937                /*
 938                 * Determine addr and size of next buffer area.  We assume that
 939                 * individual virtual buffers are always composed linearly in
 940                 * physical memory.  For example, we assume that any 8kB buffer
 941                 * is always composed of two adjacent physical 4kB pages rather
 942                 * than two possibly non-adjacent physical 4kB pages.
 943                 */
 944                /* group sequential buffers into one large buffer */
 945                addr = sg_phys(sg);
 946                size = sg_dma_len(sg);
 947                while (--i) {
 948                        sg = sg_next(sg);
 949                        if ((addr + size) != sg_phys(sg))
 950                                break;
 951                        size += sg_dma_len(sg);
 952                }
 953
 954                /* did we run out of descriptors? */
 955
 956                if(count >= MAX_DMA_DESCRS) {
 957                        printk("%s: too few DMA descriptors\n", drive->name);
 958                        return 1;
 959                }
 960
 961                /* however, this case is more difficult - rw_trf_cnt cannot be more
 962                   than 65536 words per transfer, so in that case we need to either
 963                   1) use a DMA interrupt to re-trigger rw_trf_cnt and continue with
 964                      the descriptors, or
 965                   2) simply do the request here, and get dma_intr to only ide_end_request on
 966                      those blocks that were actually set-up for transfer.
 967                */
 968
 969                if(ata_tot_size + size > 131072) {
 970                        printk("too large total ATA DMA request, %d + %d!\n", ata_tot_size, (int)size);
 971                        return 1;
 972                }
 973
 974                /* If size > MAX_DESCR_SIZE it has to be splitted into new descriptors. Since we
 975                   don't handle size > 131072 only one split is necessary */
 976
 977                if(size > MAX_DESCR_SIZE) {
 978                        cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, MAX_DESCR_SIZE, 0);
 979                        count++;
 980                        ata_tot_size += MAX_DESCR_SIZE;
 981                        size -= MAX_DESCR_SIZE;
 982                        addr += MAX_DESCR_SIZE;
 983                }
 984
 985                cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, size,i ? 0 : 1);
 986                count++;
 987                ata_tot_size += size;
 988        }
 989
 990        if (count) {
 991                /* return and say all is ok */
 992                return 0;
 993        }
 994
 995        printk("%s: empty DMA table?\n", drive->name);
 996        return 1;       /* let the PIO routines handle this weirdness */
 997}
 998
 999/*
1000 * cris_dma_intr() is the handler for disk read/write DMA interrupts
1001 */
1002static ide_startstop_t cris_dma_intr (ide_drive_t *drive)
1003{
1004        LED_DISK_READ(0);
1005        LED_DISK_WRITE(0);
1006
1007        return ide_dma_intr(drive);
1008}
1009
1010/*
1011 * Functions below initiates/aborts DMA read/write operations on a drive.
1012 *
1013 * The caller is assumed to have selected the drive and programmed the drive's
1014 * sector address using CHS or LBA.  All that remains is to prepare for DMA
1015 * and then issue the actual read/write DMA/PIO command to the drive.
1016 *
1017 * For ATAPI devices, we just prepare for DMA and return. The caller should
1018 * then issue the packet command to the drive and call us again with
1019 * cris_dma_start afterwards.
1020 *
1021 * Returns 0 if all went well.
1022 * Returns 1 if DMA read/write could not be started, in which case
1023 * the caller should revert to PIO for the current request.
1024 */
1025
1026static int cris_dma_end(ide_drive_t *drive)
1027{
1028        drive->waiting_for_dma = 0;
1029        return 0;
1030}
1031
1032static int cris_dma_setup(ide_drive_t *drive)
1033{
1034        struct request *rq = drive->hwif->hwgroup->rq;
1035
1036        cris_ide_initialize_dma(!rq_data_dir(rq));
1037        if (cris_ide_build_dmatable (drive)) {
1038                ide_map_sg(drive, rq);
1039                return 1;
1040        }
1041
1042        drive->waiting_for_dma = 1;
1043        return 0;
1044}
1045
1046static void cris_dma_exec_cmd(ide_drive_t *drive, u8 command)
1047{
1048        ide_execute_command(drive, command, &cris_dma_intr, WAIT_CMD, NULL);
1049}
1050
1051static void cris_dma_start(ide_drive_t *drive)
1052{
1053        struct request *rq = drive->hwif->hwgroup->rq;
1054        int writing = rq_data_dir(rq);
1055        int type = TYPE_DMA;
1056
1057        if (drive->current_speed >= XFER_UDMA_0)
1058                type = TYPE_UDMA;
1059
1060        cris_ide_start_dma(drive, &ata_descrs[0], writing ? 0 : 1, type, ata_tot_size);
1061
1062        if (writing) {
1063                LED_DISK_WRITE(1);
1064        } else {
1065                LED_DISK_READ(1);
1066        }
1067}
1068
1069module_init(init_e100_ide);
1070
1071MODULE_LICENSE("GPL");
1072
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.