⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ata.c

📁 u盘MCU端源代码,开发U盘的可以参考.
💻 C
📖 第 1 页 / 共 5 页
字号:
    g_sector_buffer[i+1] = _mcu_register_rd(x_msb_ata);
  }
  #endif
  _mcu_end_critical_section();

  //!!!$$$ check status for error - as per end of this funct
  trace0(0, ata, 1, "ata_identify_device() - entire xfer complete");

  // examine the data
  _mcu_begin_critical_section();
  // grab the product id string for non-210 devices.
  #ifndef k_flash_family
  for (i=0; i<8; i++)
  {
    // grab 2 chars
    devword = iddev_word(i+27);
    // store them in our sram
    _lun_data(device_id)[2*i]   = _h(devword);
    _lun_data(device_id)[2*i+1] = _l(devword);
    trace1(0, dev_id, 0, "%c", _lun_data(device_id)[2*i]);
    trace1(0, dev_id, 0, "%c", _lun_data(device_id)[2*i+1]);
  }
  #endif

  //profile the edata card and drop it back to mode 1
  for (i=0; i<8; i++)
  {
    // grab 2 chars
    devword = iddev_word(i+27);
    // store them in our sram
    g_sector_buffer[2*i]   = _h(devword);
    g_sector_buffer[2*i+1] = _l(devword);
    trace1(0, ata, 0, "%c", _h(devword));
    trace1(0, ata, 0, "%c", _l(devword));
  }

  g_ata_dev_caps_1 = iddev_word(k_ata_iddev_caps_l);
  g_ata_dev_caps_2 = iddev_word(k_ata_iddev_caps_h);
  // device capabilities
  trace2(0, ata, 0, "dev_caps_1:%04X dev_caps_2:%04X", g_ata_dev_caps_1, g_ata_dev_caps_2);
  // do atapi/ata specific interpretation here
  switch (_lun_data(device_type))
  {
    case k_device_type_ata:
      // general config word
      // set removable/present properties
      if (iddev_word(k_ata_iddev_general_config) & kbm_ata_iddev_gc_removable)
      {
        trace0(0, ata, 0, "  GCFG:  Removable Media Device");
        // set removable property for Inquiry data
        _lun_data(media) |= kbm_lun_media_removable;
        // set media present property (because if we got here, the compact flash media is present)
        _lun_data(media) |= kbm_lun_media_present;
      }
      else
      {
        // fixed media.  media is always present
        _lun_data(media) |= kbm_lun_media_present;
      }
      if (iddev_word(k_ata_iddev_general_config) & kbm_ata_iddev_gc_fixed)
      {
        trace0(0, ata, 0, "  GCFG:  Controller/Device NOT removable");
        // not a removable device
        //_lun_data(media) &= ~kbm_lun_media_removable;
        trace0(0, ata, 0, "WARNING:  Controller/Device NOT removable? For CompactFlash?  Forcing the removable bit!");
        _lun_data(media) |= kbm_lun_media_removable;
        _lun_data(media) |= kbm_lun_media_present;
      }
      // dev caps
      if (g_ata_dev_caps_1& kbm_ata_iddev_caps_timer_values_per_spec)
      {
        trace0(0, ata, 0, "Standby timer values as specified in ATA spec are supported");
      }
      else
      {
        trace0(0, ata, 0, "Standby timer values managed by device");
      }
      #if k_dma_mode
      g_use_dma = g_ata_dev_caps_1&kbm_ata_iddev_caps_support_dma?k_true:k_false;
      #else
      trace0(0, ata, 0, "** firmware dma support disabled ** ");
      g_use_dma = k_false;
      #endif
      g_disable_iordy = (g_ata_dev_caps_1 & kbm_ata_iddev_caps_disable_iordy) ? k_true : k_false;
      yes_no_flag=g_use_dma ? 'Y' : 'N';
      trace1(0, ata, 0, "  DMA Support:%c", yes_no_flag);
      yes_no_flag=(devword & kbm_ata_iddev_caps_support_lba) ? 'Y' : 'N';
      trace1(0, ata, 0, "  LBA Support:%c", yes_no_flag);
      yes_no_flag=(g_disable_iordy) ? 'Y' : 'N';
      trace1(0, ata, 0, "  Can Disable IORDY:%c", yes_no_flag);
      yes_no_flag=(devword & kbm_ata_iddev_caps_support_iordy) ? 'Y' : 'N';
      trace1(0, ata, 0, "  IORDY Support:%c", yes_no_flag);
      // ata-only info
      g_ata_dev_num_log_cyl = iddev_word(k_ata_iddev_num_log_cyl);
      g_ata_dev_num_log_hds = iddev_word(k_ata_iddev_num_log_hds);
      g_ata_dev_num_log_sec_per_trk = iddev_word(k_ata_iddev_num_log_sec_per_log_track);
      _lun_data(capacity).lba_max.u32 = (uint32)iddev_word(k_ata_iddev_total_user_addressable_sectors_h) << 16L;
      _lun_data(capacity).lba_max.u32 += (uint32)iddev_word(k_ata_iddev_total_user_addressable_sectors_l);
      _lun_data(capacity).lba_max.u32 -= 1;
      trace2(0, ata, 0, "Total User-addressable Sectors: %04X%04X", _hw(_lun_data(capacity).lba_max.u32), _lw(_lun_data(capacity).lba_max.u32));
      _lun_data(capacity).lba_sz.u32 = 512;
      _lun_data(max_lb_per_split) = 256;
      _lun_data(max_lb_per_burst) = 256;
      break;
  }
  // $$$ marked obsolete in ATA-6 spec, but, seems like CF cards use it
  devword = iddev_word(k_ata_iddev_pio_mode) ;
  trace1(0, ata, 0, "Device PIO Tranfer Cycle Mode:%d", (_h(devword))) ;
  // RUDIMENTARY PROFILING OF ATA DRIVES
  // throttle the edata card back to mode 1 because it says it runs at mode 2 3 but it
  // fails to run at mode 3 or 2, and works in modes 1 and 0.
  if (memcmp(g_sector_buffer, "HYPERSTONE FLASH", 16))
    _mcu_register_set_bits(x_cfc_ata_mode_ctl, _h(devword) );
  trace1(0, ata, 0, "x_cfc_ata_mode_ctl now: %02x", x_cfc_ata_mode_ctl) ;

  if (iddev_word(k_ata_iddev_valid_fields) & kbm_ata_iddev_valid_fields_pio_mwdma_timing)
  {
    trace1(0, ata, 1, "PIO Modes Supported:%04X", iddev_word(k_ata_iddev_pio_mode_support));
    trace1(0, ata, 1, "Min MWDMA xfer cycle time: %d ns", iddev_word(k_ata_iddev_min_mwdma_cycles));
    trace1(0, ata, 1, "Manufacturer's recommended Multiword DMA transfer cycle time: %d ns", iddev_word(k_ata_iddev_mfr_recommened_mwdma_cycles));
    trace1(0, ata, 1, "Minimum PIO transfer cycle time without flow control: %d ns",  iddev_word(k_ata_iddev_min_pio_xfer_cycle_no_flow));
    trace1(0, ata, 1, "Minimum PIO transfer cycle time with IORDY flow control: %d ns", iddev_word(k_ata_iddev_min_pio_xfer_cycle_flow));
  }
  if (iddev_word(k_ata_iddev_valid_fields) & kbm_ata_iddev_valid_fields_cur_log_chs_cap)
  {
    trace1(0, ata, 0, "Number of current logical cylinders: %d", iddev_word(k_ata_iddev_cur_log_cyl));
    trace1(0, ata, 0, "Number of current logical heads: %d", iddev_word(k_ata_iddev_cur_log_hds));
    trace1(0, ata, 0, "Number of current logical sectors per track: %d", iddev_word(k_ata_iddev_cur_log_sec_per_trk));
    trace2(0, ata, 0, "Current sector capacity: %04X%04X", iddev_word(k_ata_iddev_cur_sector_cap_h), iddev_word(k_ata_iddev_cur_sector_cap_l));
    //_lun_data(capacity).lba_max.u32 = (uint32)iddev_word(k_ata_iddev_cur_sector_cap_h) << 16L;
    //_lun_data(capacity).lba_max.u32 += (uint32)iddev_word(k_ata_iddev_cur_sector_cap_l);
    //TRACE2(389, ata, 0, "Total User-addressable Sectors: %04X%04X", _hw(_lun_data(capacity).lba_max.u32), _lw(_lun_data(capacity).lba_max.u32));
  }

  trace1(0, ata, 0, "Number of sectors xferred per irq on READ/WRITE Multiple: %d", (iddev_word(k_ata_iddev_multi_burst_size)&0x00ff)) ;

  #if 0
  // dma info - already detected above if we support it
  if (g_use_dma)
  {
    devword = iddev_word(k_ata_iddev_mwdma_mode_support);
    mode_sel = (devword & kbm_ata_iddev_mwdma_mode_0_selected) ? 0 :
    (devword & kbm_ata_iddev_mwdma_mode_1_selected) ? 1 :
    (devword & kbm_ata_iddev_mwdma_mode_2_selected) ? 2 :
    (0xff);
    g_dev_max_dma_mode = (devword & kbm_ata_iddev_mwdma_mode_2_supported) ? k_dma_mode_multiword_2 :
    (devword & kbm_ata_iddev_mwdma_mode_1_supported) ? k_dma_mode_multiword_1 :
    (devword & kbm_ata_iddev_mwdma_mode_0_supported) ? k_dma_mode_multiword_0 :
    (k_dma_mode_disabled);
    if (mode_sel != 0xff)
    {
      trace1(0, ata, 0, "Multiword DMA Mode %d selected", mode_sel);
    }
    if (g_dev_max_dma_mode)
    {
      trace1(0, ata, 0, "Device Supports Multiword DMA up to mode %d", g_dev_max_dma_mode & 0x0f);
    }
    if (iddev_word(k_ata_iddev_valid_fields) & kbm_ata_iddev_valid_fields_udma_modes)
    {
      g_use_dma = k_true;
      // Read the Ultra DMA Mode word (word 88)
      devword = iddev_word(k_ata_iddev_udma_mode_support);
      trace1(0, ata, 0, "Ultra DMA Mode Enabled/Supported: %04X", devword);
      if (mode_sel == 0xff)
      {
        mode_sel = (devword & kbm_ata_iddev_udma_mode_0_selected) ? 0 :
          (devword & kbm_ata_iddev_udma_mode_1_selected) ? 1 :
          (devword & kbm_ata_iddev_udma_mode_2_selected) ? 2 :
          (devword & kbm_ata_iddev_udma_mode_3_selected) ? 3 :
          (devword & kbm_ata_iddev_udma_mode_4_selected) ? 4 :
          (devword & kbm_ata_iddev_udma_mode_5_selected) ? 5 :
          (0xff);
        if (mode_sel != 0xff)
        {
          trace1(0, ata, 0, "Ultra DMA Mode %d currently selected", mode_sel);
        }
      }
      g_dev_max_dma_mode = (devword & kbm_ata_iddev_udma_mode_5_supported) ? k_dma_mode_ultra_dma_5 :
        (devword & kbm_ata_iddev_udma_mode_4_supported) ? k_dma_mode_ultra_dma_4 :
        (devword & kbm_ata_iddev_udma_mode_3_supported) ? k_dma_mode_ultra_dma_3 :
        (devword & kbm_ata_iddev_udma_mode_2_supported) ? k_dma_mode_ultra_dma_2 :
        (devword & kbm_ata_iddev_udma_mode_1_supported) ? k_dma_mode_ultra_dma_1 :
        (devword & kbm_ata_iddev_udma_mode_0_supported) ? k_dma_mode_ultra_dma_0 :
        (g_dev_max_dma_mode);
      trace1(0, ata, 0, "Device Supports up to Ultra DMA mode %d", g_dev_max_dma_mode & 0x0f);
    }
  }
  #endif
  // feature sets - surprisingly, identical for ata or atapi
  // store local copy of device support/enabled values
  g_ata_dev_cmdset_support_1 = iddev_word(k_ata_dev_cmdset_support_1);
  g_ata_dev_cmdset_enabled_1 = iddev_word(k_ata_dev_cmdset_enabled_1);
  g_ata_dev_cmdset_support_2 = iddev_word(k_ata_dev_cmdset_support_2);
  g_ata_dev_cmdset_enabled_2 = iddev_word(k_ata_dev_cmdset_enabled_2);
  g_ata_dev_cmdset_support_3 = iddev_word(k_ata_dev_cmdset_support_3);
  g_ata_dev_cmdset_enabled_3 = iddev_word(k_ata_dev_cmdset_enabled_3);
  // support_1 options
  trace2(0, ata, 0, "command/feature set (1) support:%04X enabled:%04X", g_ata_dev_cmdset_support_1, g_ata_dev_cmdset_enabled_1);
  trace2(0, ata, 0, "command/feature set (2) support:%04X enabled:%04X", g_ata_dev_cmdset_support_2, g_ata_dev_cmdset_enabled_2);
  trace2(0, ata, 0, "command/feature set (3) support:%04X enabled:%04X", g_ata_dev_cmdset_support_3, g_ata_dev_cmdset_enabled_3);
  trace1(0, ata, 2, "Command NOP Supported...................................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_cmd_nop)?'Y':'N' );
  trace1(0, ata, 2, "Command READ BUFFER Supported...........................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_cmd_read_buffer)?'Y':'N' );
  trace1(0, ata, 2, "Command WRITE BUFFER Supported..........................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_cmd_write_buffer)?'Y':'N' );
  trace1(0, ata, 2, "Host Protected Area Supported...........................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_host_protected_area)?'Y':'N' );
  trace1(0, ata, 2, "Command DEVICE RESET Supported..........................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_cmd_device_reset)?'Y':'N' );
  trace1(0, ata, 2, "Command DEVICE RESET Enabled............................%c", (g_ata_dev_cmdset_enabled_1&kbm_ata_iddev_cmd_device_reset)?'Y':'N' );
  trace1(0, ata, 2, "SERVICE interrupt Supported.............................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_service_intrpt)?'Y':'N' );
  trace1(0, ata, 2, "Release interrupt Supported.............................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_release_intrpt)?'Y':'N' );
  trace1(0, ata, 2, "write cache supported...................................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_cmd_write_cache)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - PACKET Supported..........................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_fs_packet)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - PACKET Supported..........................%c", (g_ata_dev_cmdset_enabled_1&kbm_ata_iddev_fs_packet)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - Power Mgmt Supported......................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_fs_power_mgmt)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - Power Mgmt Enabled........................%c", (g_ata_dev_cmdset_enabled_1&kbm_ata_iddev_fs_power_mgmt)?'Y':'N' );
  trace1(0, ata, 2, "Removable Media Feature Set Supported...................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_fs_removable_media)?'Y':'N' );
  trace1(0, ata, 2, "Security Mode Feature Set Supported.....................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_fs_security_mode)?'Y':'N' );
  trace1(0, ata, 2, "SMART Feature Set Supported.............................%c", (g_ata_dev_cmdset_support_1&kbm_ata_iddev_fs_smart)?'Y':'N' );

  // support_2 options
  trace1(0, ata, 2, "Command FLUSH CACHE EXT Supported.......................%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_cmd_flush_cache_ext)?'Y':'N' );
  trace1(0, ata, 2, "Command FLUSH CACHE Supported...........................%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_cmd_flush_cache)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - Device Configuration Overlay Supported....%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_fs_device_cfg_overlay)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - 48 Bit Addressing Support.................%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_fs_48bit_address)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - Auto Acoustic Mgmt Support................%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_fs_auto_acoustic_mgmt)?'Y':'N' );
  trace1(0, ata, 2, "Command Set - Max Security Extension Supported..........%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_cmd_set_max_security_ext)?'Y':'N' );
  trace1(0, ata, 2, "Command Set - Support Set Features to spin-up after por.%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_cfg_set_features_spinup_req_on_por)?'Y':'N' );
  trace1(0, ata, 2, "Command Set - Enable Set Features to spin-up after por..%c", (g_ata_dev_cmdset_enabled_2&kbm_ata_iddev_cfg_set_features_spinup_req_on_por)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - Support Powerup in Standby................%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_fs_powerup_in_standby)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - Enable Powerup in Standby.................%c", (g_ata_dev_cmdset_enabled_2&kbm_ata_iddev_fs_powerup_in_standby)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - Removable Media Status Notification Supported:%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_fs_removable_media_status_notify)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - Advanced Power Management Supported.......%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_fs_adv_power_mgmt)?'Y':'N' );
  trace1(0, ata, 2, "Feature Set - CFA Supported.............................%c", (g_ata_dev_cmdset_support_2& kbm_ata_iddev_fs_cfa)?'Y':'N' );
  trace1(0, ata, 2, "Command READ/WRITE DMA QUEUED Supported.................%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_cmd_rw_dma_queued)?'Y':'N' );
  trace1(0, ata, 2, "Command Download Microcode Supported....................%c", (g_ata_dev_cmdset_support_2&kbm_ata_iddev_cmd_download_microcode)?'Y':'N' );
  _mcu_end_critical_section();
  if (k_success != ata_wait_status_with_timeout(kbm_ata_status_bsy, 0, 5000, &status))
  {
    // blow all the way out of dfa_ata_initialize_media.
    // back to the dev thread, which will reinit the unit and restart the mscbot dfa.
    //thread_end_dfa();  //<--EXIT
    return k_error;
  }
  if (status & kbm_ata_status_err)
  {
    status = ata_register_rd(&ata_error);
    trace1(0, ata, 1, "ata_identify_device() - error: 0x%02X", status);
    return k_error;
  }
  // cf always fails set pio mode cause it can only be set to 4
  //return ata_set_pio_transfer_mode();
  return k_success;
}

//+-----------------------------------------------------------------------------
// Name:
//   dfa_ata_identify_media
//
// Declaration:
//   void dfa_ata_identify_media(void);
//
// Purpose:
//   Go figure out what drive/card type is plugged in.
//
// Arguments:
//   None.
//
// Return:
//   No return value.
//   However, on exit the DFA's argument pointer is written with a t_csw_status indicating:
//     k_command_passed - command completed.
//     k_command_failed - an error occurred.
//
// Notes:
//   This is a DFA, not a FUNCTION.
//   It overrides dfa_lun_identify.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
void dfa_ata_identify_media() reentrant
{
  trace0(0, ata, 0, "dfa_ata_identify_media()");
  if (k_success != ata_identify_device(k_ata_command_identify_device))
  {
    _thread_return_dfa(k_command_failed);
  }
  // looks like media exists
  _lun_data(media) &= ~kbm_lun_media_unknown;
  _lun_data(sensep) = &sense_media_change;
  _thread_return_dfa(k_command_passed);
}

//+-----------------------------------------------------------------------------
// Name:
//   ata_initialize_media2
//
// Declaration:
//   void ata_initialize_media2(void);
//
// Purpose:
//   Wait for the BSY bit to clear, then initialize the ata drive.
//
// Arguments:
//   None.
//
// Return:
//   No return value.
//   However, the DFA's argument pointer is written with a t_result indicating:
//     k_success - command completed.
//     k_usbrst  - usb reset signalling was detected while executing the command.
//     k_aborted - traffic occurred on the control pipe while wexecuting the command (probably a mass storage reset).
//     k_timeout - a timeout limit exceeded while executing the command.
//   (This allows the caller to see the return value, via the argp, after the DFA terminates).
//
// Notes:
//   This is a STATE of the dfa_ata_initialize_media DFA.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
void ata_initialize_media2(void) reentrant;
void ata_initialize_media2() reentrant
{
  uint8 error;
  uint8 sector_cnt;
  uint8 sector_num;
  uint8 cylinder_lo;
  uint8 cylinder_hi;

  trace0(0, ata, 1, "ata_initialize_media2()");
  trace1(0, ata, 0, "-- imr0:0x%02X", x_imr0);
  if (thread_got_sync(kbm_sync_ata_rdy))
  {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -