mxc_ata.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Freescale iMX51 ATA driver
  3. *
  4. * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
  5. *
  6. * Based on code by:
  7. * Mahesh Mahadevan <mahesh.mahadevan@freescale.com>
  8. *
  9. * Based on code from original FSL ATA driver, which is
  10. * part of eCos, the Embedded Configurable Operating System.
  11. * Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. #include <command.h>
  30. #include <config.h>
  31. #include <asm/byteorder.h>
  32. #include <asm/io.h>
  33. #include <ide.h>
  34. #include <asm/arch/imx-regs.h>
  35. #include <asm/arch/clock.h>
  36. /* MXC ATA register offsets */
  37. struct mxc_ata_config_regs {
  38. u8 time_off; /* 0x00 */
  39. u8 time_on;
  40. u8 time_1;
  41. u8 time_2w;
  42. u8 time_2r;
  43. u8 time_ax;
  44. u8 time_pio_rdx;
  45. u8 time_4;
  46. u8 time_9;
  47. u8 time_m;
  48. u8 time_jn;
  49. u8 time_d;
  50. u8 time_k;
  51. u8 time_ack;
  52. u8 time_env;
  53. u8 time_udma_rdx;
  54. u8 time_zah; /* 0x10 */
  55. u8 time_mlix;
  56. u8 time_dvh;
  57. u8 time_dzfs;
  58. u8 time_dvs;
  59. u8 time_cvh;
  60. u8 time_ss;
  61. u8 time_cyc;
  62. u32 fifo_data_32; /* 0x18 */
  63. u32 fifo_data_16;
  64. u32 fifo_fill;
  65. u32 ata_control;
  66. u32 interrupt_pending;
  67. u32 interrupt_enable;
  68. u32 interrupt_clear;
  69. u32 fifo_alarm;
  70. };
  71. struct mxc_data_hdd_regs {
  72. u32 drive_data; /* 0xa0 */
  73. u32 drive_features;
  74. u32 drive_sector_count;
  75. u32 drive_sector_num;
  76. u32 drive_cyl_low;
  77. u32 drive_cyl_high;
  78. u32 drive_dev_head;
  79. u32 command;
  80. u32 status;
  81. u32 alt_status;
  82. };
  83. /* PIO timing table */
  84. #define NR_PIO_SPECS 5
  85. static uint16_t pio_t1[NR_PIO_SPECS] = { 70, 50, 30, 30, 25 };
  86. static uint16_t pio_t2_8[NR_PIO_SPECS] = { 290, 290, 290, 80, 70 };
  87. static uint16_t pio_t4[NR_PIO_SPECS] = { 30, 20, 15, 10, 10 };
  88. static uint16_t pio_t9[NR_PIO_SPECS] = { 20, 15, 10, 10, 10 };
  89. static uint16_t pio_tA[NR_PIO_SPECS] = { 50, 50, 50, 50, 50 };
  90. #define REG2OFF(reg) ((((uint32_t)reg) & 0x3) * 8)
  91. static void set_ata_bus_timing(unsigned char mode)
  92. {
  93. uint32_t val;
  94. uint32_t T = 1000000000 / mxc_get_clock(MXC_IPG_CLK);
  95. struct mxc_ata_config_regs *ata_regs;
  96. ata_regs = (struct mxc_ata_config_regs *)CONFIG_SYS_ATA_BASE_ADDR;
  97. if (mode >= NR_PIO_SPECS)
  98. return;
  99. /* Write TIME_OFF/ON/1/2W */
  100. val = (3 << REG2OFF(&ata_regs->time_off)) |
  101. (3 << REG2OFF(&ata_regs->time_on)) |
  102. (((pio_t1[mode] + T) / T) << REG2OFF(&ata_regs->time_1)) |
  103. (((pio_t2_8[mode] + T) / T) << REG2OFF(&ata_regs->time_2w));
  104. writel(val, &ata_regs->time_off);
  105. /* Write TIME_2R/AX/RDX/4 */
  106. val = (((pio_t2_8[mode] + T) / T) << REG2OFF(&ata_regs->time_2r)) |
  107. (((pio_tA[mode] + T) / T + 2) << REG2OFF(&ata_regs->time_ax)) |
  108. (1 << REG2OFF(&ata_regs->time_pio_rdx)) |
  109. (((pio_t4[mode] + T) / T) << REG2OFF(&ata_regs->time_4));
  110. writel(val, &ata_regs->time_2r);
  111. /* Write TIME_9 ; the rest of timing registers is irrelevant for PIO */
  112. val = (((pio_t9[mode] + T) / T) << REG2OFF(&ata_regs->time_9));
  113. writel(val, &ata_regs->time_9);
  114. }
  115. int ide_preinit(void)
  116. {
  117. struct mxc_ata_config_regs *ata_regs;
  118. ata_regs = (struct mxc_ata_config_regs *)CONFIG_SYS_ATA_BASE_ADDR;
  119. /* 46.3.3.4 @ FSL iMX51 manual */
  120. /* FIFO normal op., drive reset */
  121. writel(0x80, &ata_regs->ata_control);
  122. /* FIFO normal op., drive not reset */
  123. writel(0xc0, &ata_regs->ata_control);
  124. /* Configure the PIO timing */
  125. set_ata_bus_timing(CONFIG_MXC_ATA_PIO_MODE);
  126. /* 46.3.3.4 @ FSL iMX51 manual */
  127. /* Drive not reset, IORDY handshake */
  128. writel(0x41, &ata_regs->ata_control);
  129. return 0;
  130. }