atmel-smc.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Atmel SMC (Static Memory Controller) register offsets and bit definitions.
  3. *
  4. * Copyright (C) 2014 Atmel
  5. * Copyright (C) 2014 Free Electrons
  6. *
  7. * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #ifndef _LINUX_MFD_SYSCON_ATMEL_SMC_H_
  14. #define _LINUX_MFD_SYSCON_ATMEL_SMC_H_
  15. #include <linux/kernel.h>
  16. #include <linux/regmap.h>
  17. #define AT91SAM9_SMC_GENERIC 0x00
  18. #define AT91SAM9_SMC_GENERIC_BLK_SZ 0x10
  19. #define SAMA5_SMC_GENERIC 0x600
  20. #define SAMA5_SMC_GENERIC_BLK_SZ 0x14
  21. #define AT91SAM9_SMC_SETUP(o) ((o) + 0x00)
  22. #define AT91SAM9_SMC_NWESETUP(x) (x)
  23. #define AT91SAM9_SMC_NCS_WRSETUP(x) ((x) << 8)
  24. #define AT91SAM9_SMC_NRDSETUP(x) ((x) << 16)
  25. #define AT91SAM9_SMC_NCS_NRDSETUP(x) ((x) << 24)
  26. #define AT91SAM9_SMC_PULSE(o) ((o) + 0x04)
  27. #define AT91SAM9_SMC_NWEPULSE(x) (x)
  28. #define AT91SAM9_SMC_NCS_WRPULSE(x) ((x) << 8)
  29. #define AT91SAM9_SMC_NRDPULSE(x) ((x) << 16)
  30. #define AT91SAM9_SMC_NCS_NRDPULSE(x) ((x) << 24)
  31. #define AT91SAM9_SMC_CYCLE(o) ((o) + 0x08)
  32. #define AT91SAM9_SMC_NWECYCLE(x) (x)
  33. #define AT91SAM9_SMC_NRDCYCLE(x) ((x) << 16)
  34. #define AT91SAM9_SMC_MODE(o) ((o) + 0x0c)
  35. #define SAMA5_SMC_MODE(o) ((o) + 0x10)
  36. #define AT91_SMC_READMODE BIT(0)
  37. #define AT91_SMC_READMODE_NCS (0 << 0)
  38. #define AT91_SMC_READMODE_NRD (1 << 0)
  39. #define AT91_SMC_WRITEMODE BIT(1)
  40. #define AT91_SMC_WRITEMODE_NCS (0 << 1)
  41. #define AT91_SMC_WRITEMODE_NWE (1 << 1)
  42. #define AT91_SMC_EXNWMODE GENMASK(5, 4)
  43. #define AT91_SMC_EXNWMODE_DISABLE (0 << 4)
  44. #define AT91_SMC_EXNWMODE_FROZEN (2 << 4)
  45. #define AT91_SMC_EXNWMODE_READY (3 << 4)
  46. #define AT91_SMC_BAT BIT(8)
  47. #define AT91_SMC_BAT_SELECT (0 << 8)
  48. #define AT91_SMC_BAT_WRITE (1 << 8)
  49. #define AT91_SMC_DBW GENMASK(13, 12)
  50. #define AT91_SMC_DBW_8 (0 << 12)
  51. #define AT91_SMC_DBW_16 (1 << 12)
  52. #define AT91_SMC_DBW_32 (2 << 12)
  53. #define AT91_SMC_TDF GENMASK(19, 16)
  54. #define AT91_SMC_TDF_(x) ((((x) - 1) << 16) & AT91_SMC_TDF)
  55. #define AT91_SMC_TDF_MAX 16
  56. #define AT91_SMC_TDFMODE_OPTIMIZED BIT(20)
  57. #define AT91_SMC_PMEN BIT(24)
  58. #define AT91_SMC_PS GENMASK(29, 28)
  59. #define AT91_SMC_PS_4 (0 << 28)
  60. #define AT91_SMC_PS_8 (1 << 28)
  61. #define AT91_SMC_PS_16 (2 << 28)
  62. #define AT91_SMC_PS_32 (3 << 28)
  63. /*
  64. * This function converts a setup timing expressed in nanoseconds into an
  65. * encoded value that can be written in the SMC_SETUP register.
  66. *
  67. * The following formula is described in atmel datasheets (section
  68. * "SMC Setup Register"):
  69. *
  70. * setup length = (128* SETUP[5] + SETUP[4:0])
  71. *
  72. * where setup length is the timing expressed in cycles.
  73. */
  74. static inline u32 at91sam9_smc_setup_ns_to_cycles(unsigned int clk_rate,
  75. u32 timing_ns)
  76. {
  77. u32 clk_period = DIV_ROUND_UP(NSEC_PER_SEC, clk_rate);
  78. u32 coded_cycles = 0;
  79. u32 cycles;
  80. cycles = DIV_ROUND_UP(timing_ns, clk_period);
  81. if (cycles / 32) {
  82. coded_cycles |= 1 << 5;
  83. if (cycles < 128)
  84. cycles = 0;
  85. }
  86. coded_cycles |= cycles % 32;
  87. return coded_cycles;
  88. }
  89. /*
  90. * This function converts a pulse timing expressed in nanoseconds into an
  91. * encoded value that can be written in the SMC_PULSE register.
  92. *
  93. * The following formula is described in atmel datasheets (section
  94. * "SMC Pulse Register"):
  95. *
  96. * pulse length = (256* PULSE[6] + PULSE[5:0])
  97. *
  98. * where pulse length is the timing expressed in cycles.
  99. */
  100. static inline u32 at91sam9_smc_pulse_ns_to_cycles(unsigned int clk_rate,
  101. u32 timing_ns)
  102. {
  103. u32 clk_period = DIV_ROUND_UP(NSEC_PER_SEC, clk_rate);
  104. u32 coded_cycles = 0;
  105. u32 cycles;
  106. cycles = DIV_ROUND_UP(timing_ns, clk_period);
  107. if (cycles / 64) {
  108. coded_cycles |= 1 << 6;
  109. if (cycles < 256)
  110. cycles = 0;
  111. }
  112. coded_cycles |= cycles % 64;
  113. return coded_cycles;
  114. }
  115. /*
  116. * This function converts a cycle timing expressed in nanoseconds into an
  117. * encoded value that can be written in the SMC_CYCLE register.
  118. *
  119. * The following formula is described in atmel datasheets (section
  120. * "SMC Cycle Register"):
  121. *
  122. * cycle length = (CYCLE[8:7]*256 + CYCLE[6:0])
  123. *
  124. * where cycle length is the timing expressed in cycles.
  125. */
  126. static inline u32 at91sam9_smc_cycle_ns_to_cycles(unsigned int clk_rate,
  127. u32 timing_ns)
  128. {
  129. u32 clk_period = DIV_ROUND_UP(NSEC_PER_SEC, clk_rate);
  130. u32 coded_cycles = 0;
  131. u32 cycles;
  132. cycles = DIV_ROUND_UP(timing_ns, clk_period);
  133. if (cycles / 128) {
  134. coded_cycles = cycles / 256;
  135. cycles %= 256;
  136. if (cycles >= 128) {
  137. coded_cycles++;
  138. cycles = 0;
  139. }
  140. if (coded_cycles > 0x3) {
  141. coded_cycles = 0x3;
  142. cycles = 0x7f;
  143. }
  144. coded_cycles <<= 7;
  145. }
  146. coded_cycles |= cycles % 128;
  147. return coded_cycles;
  148. }
  149. #endif /* _LINUX_MFD_SYSCON_ATMEL_SMC_H_ */