atmel-smc.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #define ATMEL_SMC_SETUP(cs) (((cs) * 0x10))
  64. #define ATMEL_HSMC_SETUP(cs) (0x600 + ((cs) * 0x14))
  65. #define ATMEL_SMC_PULSE(cs) (((cs) * 0x10) + 0x4)
  66. #define ATMEL_HSMC_PULSE(cs) (0x600 + ((cs) * 0x14) + 0x4)
  67. #define ATMEL_SMC_CYCLE(cs) (((cs) * 0x10) + 0x8)
  68. #define ATMEL_HSMC_CYCLE(cs) (0x600 + ((cs) * 0x14) + 0x8)
  69. #define ATMEL_SMC_NWE_SHIFT 0
  70. #define ATMEL_SMC_NCS_WR_SHIFT 8
  71. #define ATMEL_SMC_NRD_SHIFT 16
  72. #define ATMEL_SMC_NCS_RD_SHIFT 24
  73. #define ATMEL_SMC_MODE(cs) (((cs) * 0x10) + 0xc)
  74. #define ATMEL_HSMC_MODE(cs) (0x600 + ((cs) * 0x14) + 0x10)
  75. #define ATMEL_SMC_MODE_READMODE_MASK BIT(0)
  76. #define ATMEL_SMC_MODE_READMODE_NCS (0 << 0)
  77. #define ATMEL_SMC_MODE_READMODE_NRD (1 << 0)
  78. #define ATMEL_SMC_MODE_WRITEMODE_MASK BIT(1)
  79. #define ATMEL_SMC_MODE_WRITEMODE_NCS (0 << 1)
  80. #define ATMEL_SMC_MODE_WRITEMODE_NWE (1 << 1)
  81. #define ATMEL_SMC_MODE_EXNWMODE_MASK GENMASK(5, 4)
  82. #define ATMEL_SMC_MODE_EXNWMODE_DISABLE (0 << 4)
  83. #define ATMEL_SMC_MODE_EXNWMODE_FROZEN (2 << 4)
  84. #define ATMEL_SMC_MODE_EXNWMODE_READY (3 << 4)
  85. #define ATMEL_SMC_MODE_BAT_MASK BIT(8)
  86. #define ATMEL_SMC_MODE_BAT_SELECT (0 << 8)
  87. #define ATMEL_SMC_MODE_BAT_WRITE (1 << 8)
  88. #define ATMEL_SMC_MODE_DBW_MASK GENMASK(13, 12)
  89. #define ATMEL_SMC_MODE_DBW_8 (0 << 12)
  90. #define ATMEL_SMC_MODE_DBW_16 (1 << 12)
  91. #define ATMEL_SMC_MODE_DBW_32 (2 << 12)
  92. #define ATMEL_SMC_MODE_TDF_MASK GENMASK(19, 16)
  93. #define ATMEL_SMC_MODE_TDF(x) (((x) - 1) << 16)
  94. #define ATMEL_SMC_MODE_TDF_MAX 16
  95. #define ATMEL_SMC_MODE_TDF_MIN 1
  96. #define ATMEL_SMC_MODE_TDFMODE_OPTIMIZED BIT(20)
  97. #define ATMEL_SMC_MODE_PMEN BIT(24)
  98. #define ATMEL_SMC_MODE_PS_MASK GENMASK(29, 28)
  99. #define ATMEL_SMC_MODE_PS_4 (0 << 28)
  100. #define ATMEL_SMC_MODE_PS_8 (1 << 28)
  101. #define ATMEL_SMC_MODE_PS_16 (2 << 28)
  102. #define ATMEL_SMC_MODE_PS_32 (3 << 28)
  103. #define ATMEL_HSMC_TIMINGS(cs) (0x600 + ((cs) * 0x14) + 0xc)
  104. #define ATMEL_HSMC_TIMINGS_OCMS BIT(12)
  105. #define ATMEL_HSMC_TIMINGS_RBNSEL(x) ((x) << 28)
  106. #define ATMEL_HSMC_TIMINGS_NFSEL BIT(31)
  107. #define ATMEL_HSMC_TIMINGS_TCLR_SHIFT 0
  108. #define ATMEL_HSMC_TIMINGS_TADL_SHIFT 4
  109. #define ATMEL_HSMC_TIMINGS_TAR_SHIFT 8
  110. #define ATMEL_HSMC_TIMINGS_TRR_SHIFT 16
  111. #define ATMEL_HSMC_TIMINGS_TWB_SHIFT 24
  112. /**
  113. * struct atmel_smc_cs_conf - SMC CS config as described in the datasheet.
  114. * @setup: NCS/NWE/NRD setup timings (not applicable to at91rm9200)
  115. * @pulse: NCS/NWE/NRD pulse timings (not applicable to at91rm9200)
  116. * @cycle: NWE/NRD cycle timings (not applicable to at91rm9200)
  117. * @timings: advanced NAND related timings (only applicable to HSMC)
  118. * @mode: all kind of config parameters (see the fields definition above).
  119. * The mode fields are different on at91rm9200
  120. */
  121. struct atmel_smc_cs_conf {
  122. u32 setup;
  123. u32 pulse;
  124. u32 cycle;
  125. u32 timings;
  126. u32 mode;
  127. };
  128. void atmel_smc_cs_conf_init(struct atmel_smc_cs_conf *conf);
  129. int atmel_smc_cs_conf_set_timing(struct atmel_smc_cs_conf *conf,
  130. unsigned int shift,
  131. unsigned int ncycles);
  132. int atmel_smc_cs_conf_set_setup(struct atmel_smc_cs_conf *conf,
  133. unsigned int shift, unsigned int ncycles);
  134. int atmel_smc_cs_conf_set_pulse(struct atmel_smc_cs_conf *conf,
  135. unsigned int shift, unsigned int ncycles);
  136. int atmel_smc_cs_conf_set_cycle(struct atmel_smc_cs_conf *conf,
  137. unsigned int shift, unsigned int ncycles);
  138. void atmel_smc_cs_conf_apply(struct regmap *regmap, int cs,
  139. const struct atmel_smc_cs_conf *conf);
  140. void atmel_hsmc_cs_conf_apply(struct regmap *regmap, int cs,
  141. const struct atmel_smc_cs_conf *conf);
  142. void atmel_smc_cs_conf_get(struct regmap *regmap, int cs,
  143. struct atmel_smc_cs_conf *conf);
  144. void atmel_hsmc_cs_conf_get(struct regmap *regmap, int cs,
  145. struct atmel_smc_cs_conf *conf);
  146. /*
  147. * This function converts a setup timing expressed in nanoseconds into an
  148. * encoded value that can be written in the SMC_SETUP register.
  149. *
  150. * The following formula is described in atmel datasheets (section
  151. * "SMC Setup Register"):
  152. *
  153. * setup length = (128* SETUP[5] + SETUP[4:0])
  154. *
  155. * where setup length is the timing expressed in cycles.
  156. */
  157. static inline u32 at91sam9_smc_setup_ns_to_cycles(unsigned int clk_rate,
  158. u32 timing_ns)
  159. {
  160. u32 clk_period = DIV_ROUND_UP(NSEC_PER_SEC, clk_rate);
  161. u32 coded_cycles = 0;
  162. u32 cycles;
  163. cycles = DIV_ROUND_UP(timing_ns, clk_period);
  164. if (cycles / 32) {
  165. coded_cycles |= 1 << 5;
  166. if (cycles < 128)
  167. cycles = 0;
  168. }
  169. coded_cycles |= cycles % 32;
  170. return coded_cycles;
  171. }
  172. /*
  173. * This function converts a pulse timing expressed in nanoseconds into an
  174. * encoded value that can be written in the SMC_PULSE register.
  175. *
  176. * The following formula is described in atmel datasheets (section
  177. * "SMC Pulse Register"):
  178. *
  179. * pulse length = (256* PULSE[6] + PULSE[5:0])
  180. *
  181. * where pulse length is the timing expressed in cycles.
  182. */
  183. static inline u32 at91sam9_smc_pulse_ns_to_cycles(unsigned int clk_rate,
  184. u32 timing_ns)
  185. {
  186. u32 clk_period = DIV_ROUND_UP(NSEC_PER_SEC, clk_rate);
  187. u32 coded_cycles = 0;
  188. u32 cycles;
  189. cycles = DIV_ROUND_UP(timing_ns, clk_period);
  190. if (cycles / 64) {
  191. coded_cycles |= 1 << 6;
  192. if (cycles < 256)
  193. cycles = 0;
  194. }
  195. coded_cycles |= cycles % 64;
  196. return coded_cycles;
  197. }
  198. /*
  199. * This function converts a cycle timing expressed in nanoseconds into an
  200. * encoded value that can be written in the SMC_CYCLE register.
  201. *
  202. * The following formula is described in atmel datasheets (section
  203. * "SMC Cycle Register"):
  204. *
  205. * cycle length = (CYCLE[8:7]*256 + CYCLE[6:0])
  206. *
  207. * where cycle length is the timing expressed in cycles.
  208. */
  209. static inline u32 at91sam9_smc_cycle_ns_to_cycles(unsigned int clk_rate,
  210. u32 timing_ns)
  211. {
  212. u32 clk_period = DIV_ROUND_UP(NSEC_PER_SEC, clk_rate);
  213. u32 coded_cycles = 0;
  214. u32 cycles;
  215. cycles = DIV_ROUND_UP(timing_ns, clk_period);
  216. if (cycles / 128) {
  217. coded_cycles = cycles / 256;
  218. cycles %= 256;
  219. if (cycles >= 128) {
  220. coded_cycles++;
  221. cycles = 0;
  222. }
  223. if (coded_cycles > 0x3) {
  224. coded_cycles = 0x3;
  225. cycles = 0x7f;
  226. }
  227. coded_cycles <<= 7;
  228. }
  229. coded_cycles |= cycles % 128;
  230. return coded_cycles;
  231. }
  232. #endif /* _LINUX_MFD_SYSCON_ATMEL_SMC_H_ */