mvsata_ide.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2010 Albert ARIBAUD <albert.aribaud@free.fr>
  3. *
  4. * Written-by: Albert ARIBAUD <albert.aribaud@free.fr>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. * MA 02110-1301 USA
  23. */
  24. #include <common.h>
  25. #include <asm/io.h>
  26. #if defined(CONFIG_ORION5X)
  27. #include <asm/arch/orion5x.h>
  28. #elif defined(CONFIG_KIRKWOOD)
  29. #include <asm/arch/kirkwood.h>
  30. #endif
  31. /* SATA port registers */
  32. struct mvsata_port_registers {
  33. u32 reserved1[192];
  34. /* offset 0x300 : ATA Interface registers */
  35. u32 sstatus;
  36. u32 serror;
  37. u32 scontrol;
  38. u32 ltmode;
  39. u32 phymode3;
  40. u32 phymode4;
  41. u32 reserved2[5];
  42. u32 phymode1;
  43. u32 phymode2;
  44. u32 bist_cr;
  45. u32 bist_dw1;
  46. u32 bist_dw2;
  47. u32 serrorintrmask;
  48. };
  49. /*
  50. * Sanity checks:
  51. * - to compile at all, we need CONFIG_SYS_ATA_BASE_ADDR.
  52. * - for ide_preinit to make sense, we need at least one of
  53. * CONFIG_SYS_ATA_IDE0_OFFSET or CONFIG_SYS_ATA_IDE0_OFFSET;
  54. * - for inde_preinit to be called, we need CONFIG_IDE_PREINIT.
  55. * Fail with an explanation message if these conditions are not met.
  56. * This is particularly important for CONFIG_IDE_PREINIT, because
  57. * its lack would not cause a build error.
  58. */
  59. #if !defined(CONFIG_SYS_ATA_BASE_ADDR)
  60. #error CONFIG_SYS_ATA_BASE_ADDR must be defined
  61. #elif !defined(CONFIG_SYS_ATA_IDE0_OFFSET) \
  62. && !defined(CONFIG_SYS_ATA_IDE1_OFFSET)
  63. #error CONFIG_SYS_ATA_IDE0_OFFSET or CONFIG_SYS_ATA_IDE1_OFFSET \
  64. must be defined
  65. #elif !defined(CONFIG_IDE_PREINIT)
  66. #error CONFIG_IDE_PREINIT must be defined
  67. #endif
  68. /*
  69. * Masks and values for SControl DETection and Interface Power Management,
  70. * and for SStatus DETection.
  71. */
  72. #define MVSATA_SCONTROL_DET_MASK 0x0000000F
  73. #define MVSATA_SCONTROL_DET_NONE 0x00000000
  74. #define MVSATA_SCONTROL_DET_INIT 0x00000001
  75. #define MVSATA_SCONTROL_IPM_MASK 0x00000F00
  76. #define MVSATA_SCONTROL_IPM_NO_LP_ALLOWED 0x00000300
  77. #define MVSATA_SCONTROL_MASK \
  78. (MVSATA_SCONTROL_DET_MASK|MVSATA_SCONTROL_IPM_MASK)
  79. #define MVSATA_PORT_INIT \
  80. (MVSATA_SCONTROL_DET_INIT|MVSATA_SCONTROL_IPM_NO_LP_ALLOWED)
  81. #define MVSATA_PORT_USE \
  82. (MVSATA_SCONTROL_DET_NONE|MVSATA_SCONTROL_IPM_NO_LP_ALLOWED)
  83. #define MVSATA_SSTATUS_DET_MASK 0x0000000F
  84. #define MVSATA_SSTATUS_DET_DEVCOMM 0x00000003
  85. /*
  86. * Initialize one MVSATAHC port: set SControl's IPM to "always active"
  87. * and DET to "reset", then wait for SStatus's DET to become "device and
  88. * comm ok" (or time out after 50 us if no device), then set SControl's
  89. * DET back to "no action".
  90. */
  91. static void mvsata_ide_initialize_port(struct mvsata_port_registers *port)
  92. {
  93. u32 control;
  94. u32 status;
  95. u32 tout = 50; /* wait at most 50 us for SATA reset to complete */
  96. control = readl(&port->scontrol);
  97. control = (control & ~MVSATA_SCONTROL_MASK) | MVSATA_PORT_INIT;
  98. writel(control, &port->scontrol);
  99. while (--tout) {
  100. status = readl(&port->sstatus) & MVSATA_SSTATUS_DET_MASK;
  101. if (status == MVSATA_SSTATUS_DET_DEVCOMM)
  102. break;
  103. udelay(1);
  104. }
  105. control = (control & ~MVSATA_SCONTROL_MASK) | MVSATA_PORT_USE;
  106. writel(control, &port->scontrol);
  107. }
  108. /*
  109. * ide_preinit() will be called by ide_init in cmd_ide.c and will
  110. * reset the MVSTATHC ports needed by the board.
  111. */
  112. int ide_preinit(void)
  113. {
  114. /* Enable ATA port 0 (could be SATA port 0 or 1) if declared */
  115. #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
  116. mvsata_ide_initialize_port(
  117. (struct mvsata_port_registers *)
  118. (CONFIG_SYS_ATA_BASE_ADDR + CONFIG_SYS_ATA_IDE0_OFFSET));
  119. #endif
  120. /* Enable ATA port 1 (could be SATA port 0 or 1) if declared */
  121. #if defined(CONFIG_SYS_ATA_IDE1_OFFSET)
  122. mvsata_ide_initialize_port(
  123. (struct mvsata_port_registers *)
  124. (CONFIG_SYS_ATA_BASE_ADDR + CONFIG_SYS_ATA_IDE1_OFFSET));
  125. #endif
  126. /* return 0 as we always succeed */
  127. return 0;
  128. }