atmel-smc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Atmel SMC (Static Memory Controller) helper functions.
  4. *
  5. * Copyright (C) 2017 Atmel
  6. * Copyright (C) 2017 Free Electrons
  7. *
  8. * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
  9. */
  10. #include <linux/mfd/syscon/atmel-smc.h>
  11. #include <linux/string.h>
  12. /**
  13. * atmel_smc_cs_conf_init - initialize a SMC CS conf
  14. * @conf: the SMC CS conf to initialize
  15. *
  16. * Set all fields to 0 so that one can start defining a new config.
  17. */
  18. void atmel_smc_cs_conf_init(struct atmel_smc_cs_conf *conf)
  19. {
  20. memset(conf, 0, sizeof(*conf));
  21. }
  22. EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_init);
  23. /**
  24. * atmel_smc_cs_encode_ncycles - encode a number of MCK clk cycles in the
  25. * format expected by the SMC engine
  26. * @ncycles: number of MCK clk cycles
  27. * @msbpos: position of the MSB part of the timing field
  28. * @msbwidth: width of the MSB part of the timing field
  29. * @msbfactor: factor applied to the MSB
  30. * @encodedval: param used to store the encoding result
  31. *
  32. * This function encodes the @ncycles value as described in the datasheet
  33. * (section "SMC Setup/Pulse/Cycle/Timings Register"). This is a generic
  34. * helper which called with different parameter depending on the encoding
  35. * scheme.
  36. *
  37. * If the @ncycles value is too big to be encoded, -ERANGE is returned and
  38. * the encodedval is contains the maximum val. Otherwise, 0 is returned.
  39. */
  40. static int atmel_smc_cs_encode_ncycles(unsigned int ncycles,
  41. unsigned int msbpos,
  42. unsigned int msbwidth,
  43. unsigned int msbfactor,
  44. unsigned int *encodedval)
  45. {
  46. unsigned int lsbmask = GENMASK(msbpos - 1, 0);
  47. unsigned int msbmask = GENMASK(msbwidth - 1, 0);
  48. unsigned int msb, lsb;
  49. int ret = 0;
  50. msb = ncycles / msbfactor;
  51. lsb = ncycles % msbfactor;
  52. if (lsb > lsbmask) {
  53. lsb = 0;
  54. msb++;
  55. }
  56. /*
  57. * Let's just put the maximum we can if the requested setting does
  58. * not fit in the register field.
  59. * We still return -ERANGE in case the caller cares.
  60. */
  61. if (msb > msbmask) {
  62. msb = msbmask;
  63. lsb = lsbmask;
  64. ret = -ERANGE;
  65. }
  66. *encodedval = (msb << msbpos) | lsb;
  67. return ret;
  68. }
  69. /**
  70. * atmel_smc_cs_conf_set_timing - set the SMC CS conf Txx parameter to a
  71. * specific value
  72. * @conf: SMC CS conf descriptor
  73. * @shift: the position of the Txx field in the TIMINGS register
  74. * @ncycles: value (expressed in MCK clk cycles) to assign to this Txx
  75. * parameter
  76. *
  77. * This function encodes the @ncycles value as described in the datasheet
  78. * (section "SMC Timings Register"), and then stores the result in the
  79. * @conf->timings field at @shift position.
  80. *
  81. * Returns -EINVAL if shift is invalid, -ERANGE if ncycles does not fit in
  82. * the field, and 0 otherwise.
  83. */
  84. int atmel_smc_cs_conf_set_timing(struct atmel_smc_cs_conf *conf,
  85. unsigned int shift, unsigned int ncycles)
  86. {
  87. unsigned int val;
  88. int ret;
  89. if (shift != ATMEL_HSMC_TIMINGS_TCLR_SHIFT &&
  90. shift != ATMEL_HSMC_TIMINGS_TADL_SHIFT &&
  91. shift != ATMEL_HSMC_TIMINGS_TAR_SHIFT &&
  92. shift != ATMEL_HSMC_TIMINGS_TRR_SHIFT &&
  93. shift != ATMEL_HSMC_TIMINGS_TWB_SHIFT)
  94. return -EINVAL;
  95. /*
  96. * The formula described in atmel datasheets (section "HSMC Timings
  97. * Register"):
  98. *
  99. * ncycles = (Txx[3] * 64) + Txx[2:0]
  100. */
  101. ret = atmel_smc_cs_encode_ncycles(ncycles, 3, 1, 64, &val);
  102. conf->timings &= ~GENMASK(shift + 3, shift);
  103. conf->timings |= val << shift;
  104. return ret;
  105. }
  106. EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_timing);
  107. /**
  108. * atmel_smc_cs_conf_set_setup - set the SMC CS conf xx_SETUP parameter to a
  109. * specific value
  110. * @conf: SMC CS conf descriptor
  111. * @shift: the position of the xx_SETUP field in the SETUP register
  112. * @ncycles: value (expressed in MCK clk cycles) to assign to this xx_SETUP
  113. * parameter
  114. *
  115. * This function encodes the @ncycles value as described in the datasheet
  116. * (section "SMC Setup Register"), and then stores the result in the
  117. * @conf->setup field at @shift position.
  118. *
  119. * Returns -EINVAL if @shift is invalid, -ERANGE if @ncycles does not fit in
  120. * the field, and 0 otherwise.
  121. */
  122. int atmel_smc_cs_conf_set_setup(struct atmel_smc_cs_conf *conf,
  123. unsigned int shift, unsigned int ncycles)
  124. {
  125. unsigned int val;
  126. int ret;
  127. if (shift != ATMEL_SMC_NWE_SHIFT && shift != ATMEL_SMC_NCS_WR_SHIFT &&
  128. shift != ATMEL_SMC_NRD_SHIFT && shift != ATMEL_SMC_NCS_RD_SHIFT)
  129. return -EINVAL;
  130. /*
  131. * The formula described in atmel datasheets (section "SMC Setup
  132. * Register"):
  133. *
  134. * ncycles = (128 * xx_SETUP[5]) + xx_SETUP[4:0]
  135. */
  136. ret = atmel_smc_cs_encode_ncycles(ncycles, 5, 1, 128, &val);
  137. conf->setup &= ~GENMASK(shift + 7, shift);
  138. conf->setup |= val << shift;
  139. return ret;
  140. }
  141. EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_setup);
  142. /**
  143. * atmel_smc_cs_conf_set_pulse - set the SMC CS conf xx_PULSE parameter to a
  144. * specific value
  145. * @conf: SMC CS conf descriptor
  146. * @shift: the position of the xx_PULSE field in the PULSE register
  147. * @ncycles: value (expressed in MCK clk cycles) to assign to this xx_PULSE
  148. * parameter
  149. *
  150. * This function encodes the @ncycles value as described in the datasheet
  151. * (section "SMC Pulse Register"), and then stores the result in the
  152. * @conf->setup field at @shift position.
  153. *
  154. * Returns -EINVAL if @shift is invalid, -ERANGE if @ncycles does not fit in
  155. * the field, and 0 otherwise.
  156. */
  157. int atmel_smc_cs_conf_set_pulse(struct atmel_smc_cs_conf *conf,
  158. unsigned int shift, unsigned int ncycles)
  159. {
  160. unsigned int val;
  161. int ret;
  162. if (shift != ATMEL_SMC_NWE_SHIFT && shift != ATMEL_SMC_NCS_WR_SHIFT &&
  163. shift != ATMEL_SMC_NRD_SHIFT && shift != ATMEL_SMC_NCS_RD_SHIFT)
  164. return -EINVAL;
  165. /*
  166. * The formula described in atmel datasheets (section "SMC Pulse
  167. * Register"):
  168. *
  169. * ncycles = (256 * xx_PULSE[6]) + xx_PULSE[5:0]
  170. */
  171. ret = atmel_smc_cs_encode_ncycles(ncycles, 6, 1, 256, &val);
  172. conf->pulse &= ~GENMASK(shift + 7, shift);
  173. conf->pulse |= val << shift;
  174. return ret;
  175. }
  176. EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_pulse);
  177. /**
  178. * atmel_smc_cs_conf_set_cycle - set the SMC CS conf xx_CYCLE parameter to a
  179. * specific value
  180. * @conf: SMC CS conf descriptor
  181. * @shift: the position of the xx_CYCLE field in the CYCLE register
  182. * @ncycles: value (expressed in MCK clk cycles) to assign to this xx_CYCLE
  183. * parameter
  184. *
  185. * This function encodes the @ncycles value as described in the datasheet
  186. * (section "SMC Cycle Register"), and then stores the result in the
  187. * @conf->setup field at @shift position.
  188. *
  189. * Returns -EINVAL if @shift is invalid, -ERANGE if @ncycles does not fit in
  190. * the field, and 0 otherwise.
  191. */
  192. int atmel_smc_cs_conf_set_cycle(struct atmel_smc_cs_conf *conf,
  193. unsigned int shift, unsigned int ncycles)
  194. {
  195. unsigned int val;
  196. int ret;
  197. if (shift != ATMEL_SMC_NWE_SHIFT && shift != ATMEL_SMC_NRD_SHIFT)
  198. return -EINVAL;
  199. /*
  200. * The formula described in atmel datasheets (section "SMC Cycle
  201. * Register"):
  202. *
  203. * ncycles = (xx_CYCLE[8:7] * 256) + xx_CYCLE[6:0]
  204. */
  205. ret = atmel_smc_cs_encode_ncycles(ncycles, 7, 2, 256, &val);
  206. conf->cycle &= ~GENMASK(shift + 15, shift);
  207. conf->cycle |= val << shift;
  208. return ret;
  209. }
  210. EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_cycle);
  211. /**
  212. * atmel_smc_cs_conf_apply - apply an SMC CS conf
  213. * @regmap: the SMC regmap
  214. * @cs: the CS id
  215. * @conf: the SMC CS conf to apply
  216. *
  217. * Applies an SMC CS configuration.
  218. * Only valid on at91sam9/avr32 SoCs.
  219. */
  220. void atmel_smc_cs_conf_apply(struct regmap *regmap, int cs,
  221. const struct atmel_smc_cs_conf *conf)
  222. {
  223. regmap_write(regmap, ATMEL_SMC_SETUP(cs), conf->setup);
  224. regmap_write(regmap, ATMEL_SMC_PULSE(cs), conf->pulse);
  225. regmap_write(regmap, ATMEL_SMC_CYCLE(cs), conf->cycle);
  226. regmap_write(regmap, ATMEL_SMC_MODE(cs), conf->mode);
  227. }
  228. EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_apply);
  229. /**
  230. * atmel_hsmc_cs_conf_apply - apply an SMC CS conf
  231. * @regmap: the HSMC regmap
  232. * @cs: the CS id
  233. * @layout: the layout of registers
  234. * @conf: the SMC CS conf to apply
  235. *
  236. * Applies an SMC CS configuration.
  237. * Only valid on post-sama5 SoCs.
  238. */
  239. void atmel_hsmc_cs_conf_apply(struct regmap *regmap,
  240. const struct atmel_hsmc_reg_layout *layout,
  241. int cs, const struct atmel_smc_cs_conf *conf)
  242. {
  243. regmap_write(regmap, ATMEL_HSMC_SETUP(layout, cs), conf->setup);
  244. regmap_write(regmap, ATMEL_HSMC_PULSE(layout, cs), conf->pulse);
  245. regmap_write(regmap, ATMEL_HSMC_CYCLE(layout, cs), conf->cycle);
  246. regmap_write(regmap, ATMEL_HSMC_TIMINGS(layout, cs), conf->timings);
  247. regmap_write(regmap, ATMEL_HSMC_MODE(layout, cs), conf->mode);
  248. }
  249. EXPORT_SYMBOL_GPL(atmel_hsmc_cs_conf_apply);
  250. /**
  251. * atmel_smc_cs_conf_get - retrieve the current SMC CS conf
  252. * @regmap: the SMC regmap
  253. * @cs: the CS id
  254. * @conf: the SMC CS conf object to store the current conf
  255. *
  256. * Retrieve the SMC CS configuration.
  257. * Only valid on at91sam9/avr32 SoCs.
  258. */
  259. void atmel_smc_cs_conf_get(struct regmap *regmap, int cs,
  260. struct atmel_smc_cs_conf *conf)
  261. {
  262. regmap_read(regmap, ATMEL_SMC_SETUP(cs), &conf->setup);
  263. regmap_read(regmap, ATMEL_SMC_PULSE(cs), &conf->pulse);
  264. regmap_read(regmap, ATMEL_SMC_CYCLE(cs), &conf->cycle);
  265. regmap_read(regmap, ATMEL_SMC_MODE(cs), &conf->mode);
  266. }
  267. EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_get);
  268. /**
  269. * atmel_hsmc_cs_conf_get - retrieve the current SMC CS conf
  270. * @regmap: the HSMC regmap
  271. * @cs: the CS id
  272. * @layout: the layout of registers
  273. * @conf: the SMC CS conf object to store the current conf
  274. *
  275. * Retrieve the SMC CS configuration.
  276. * Only valid on post-sama5 SoCs.
  277. */
  278. void atmel_hsmc_cs_conf_get(struct regmap *regmap,
  279. const struct atmel_hsmc_reg_layout *layout,
  280. int cs, struct atmel_smc_cs_conf *conf)
  281. {
  282. regmap_read(regmap, ATMEL_HSMC_SETUP(layout, cs), &conf->setup);
  283. regmap_read(regmap, ATMEL_HSMC_PULSE(layout, cs), &conf->pulse);
  284. regmap_read(regmap, ATMEL_HSMC_CYCLE(layout, cs), &conf->cycle);
  285. regmap_read(regmap, ATMEL_HSMC_TIMINGS(layout, cs), &conf->timings);
  286. regmap_read(regmap, ATMEL_HSMC_MODE(layout, cs), &conf->mode);
  287. }
  288. EXPORT_SYMBOL_GPL(atmel_hsmc_cs_conf_get);
  289. static const struct atmel_hsmc_reg_layout sama5d3_reg_layout = {
  290. .timing_regs_offset = 0x600,
  291. };
  292. static const struct atmel_hsmc_reg_layout sama5d2_reg_layout = {
  293. .timing_regs_offset = 0x700,
  294. };
  295. static const struct of_device_id atmel_smc_ids[] = {
  296. { .compatible = "atmel,at91sam9260-smc", .data = NULL },
  297. { .compatible = "atmel,sama5d3-smc", .data = &sama5d3_reg_layout },
  298. { .compatible = "atmel,sama5d2-smc", .data = &sama5d2_reg_layout },
  299. { /* sentinel */ },
  300. };
  301. /**
  302. * atmel_hsmc_get_reg_layout - retrieve the layout of HSMC registers
  303. * @np: the HSMC regmap
  304. *
  305. * Retrieve the layout of HSMC registers.
  306. *
  307. * Returns NULL in case of SMC, a struct atmel_hsmc_reg_layout pointer
  308. * in HSMC case, otherwise ERR_PTR(-EINVAL).
  309. */
  310. const struct atmel_hsmc_reg_layout *
  311. atmel_hsmc_get_reg_layout(struct device_node *np)
  312. {
  313. const struct of_device_id *match;
  314. match = of_match_node(atmel_smc_ids, np);
  315. return match ? match->data : ERR_PTR(-EINVAL);
  316. }
  317. EXPORT_SYMBOL_GPL(atmel_hsmc_get_reg_layout);