regmap-mmio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Register map access API - MMIO support
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/module.h>
  22. #include <linux/regmap.h>
  23. #include <linux/slab.h>
  24. struct regmap_mmio_context {
  25. void __iomem *regs;
  26. unsigned reg_bytes;
  27. unsigned val_bytes;
  28. unsigned pad_bytes;
  29. struct clk *clk;
  30. };
  31. static inline void regmap_mmio_regsize_check(size_t reg_size)
  32. {
  33. switch (reg_size) {
  34. case 1:
  35. case 2:
  36. case 4:
  37. #ifdef CONFIG_64BIT
  38. case 8:
  39. #endif
  40. break;
  41. default:
  42. BUG();
  43. }
  44. }
  45. static int regmap_mmio_regbits_check(size_t reg_bits)
  46. {
  47. switch (reg_bits) {
  48. case 8:
  49. case 16:
  50. case 32:
  51. #ifdef CONFIG_64BIT
  52. case 64:
  53. #endif
  54. return 0;
  55. default:
  56. return -EINVAL;
  57. }
  58. }
  59. static inline void regmap_mmio_count_check(size_t count, u32 offset)
  60. {
  61. BUG_ON(count <= offset);
  62. }
  63. static inline unsigned int
  64. regmap_mmio_get_offset(const void *reg, size_t reg_size)
  65. {
  66. switch (reg_size) {
  67. case 1:
  68. return *(u8 *)reg;
  69. case 2:
  70. return *(u16 *)reg;
  71. case 4:
  72. return *(u32 *)reg;
  73. #ifdef CONFIG_64BIT
  74. case 8:
  75. return *(u64 *)reg;
  76. #endif
  77. default:
  78. BUG();
  79. }
  80. }
  81. static int regmap_mmio_gather_write(void *context,
  82. const void *reg, size_t reg_size,
  83. const void *val, size_t val_size)
  84. {
  85. struct regmap_mmio_context *ctx = context;
  86. unsigned int offset;
  87. int ret;
  88. regmap_mmio_regsize_check(reg_size);
  89. if (!IS_ERR(ctx->clk)) {
  90. ret = clk_enable(ctx->clk);
  91. if (ret < 0)
  92. return ret;
  93. }
  94. offset = regmap_mmio_get_offset(reg, reg_size);
  95. while (val_size) {
  96. switch (ctx->val_bytes) {
  97. case 1:
  98. writeb(*(u8 *)val, ctx->regs + offset);
  99. break;
  100. case 2:
  101. writew(*(u16 *)val, ctx->regs + offset);
  102. break;
  103. case 4:
  104. writel(*(u32 *)val, ctx->regs + offset);
  105. break;
  106. #ifdef CONFIG_64BIT
  107. case 8:
  108. writeq(*(u64 *)val, ctx->regs + offset);
  109. break;
  110. #endif
  111. default:
  112. /* Should be caught by regmap_mmio_check_config */
  113. BUG();
  114. }
  115. val_size -= ctx->val_bytes;
  116. val += ctx->val_bytes;
  117. offset += ctx->val_bytes;
  118. }
  119. if (!IS_ERR(ctx->clk))
  120. clk_disable(ctx->clk);
  121. return 0;
  122. }
  123. static int regmap_mmio_write(void *context, const void *data, size_t count)
  124. {
  125. struct regmap_mmio_context *ctx = context;
  126. unsigned int offset = ctx->reg_bytes + ctx->pad_bytes;
  127. regmap_mmio_count_check(count, offset);
  128. return regmap_mmio_gather_write(context, data, ctx->reg_bytes,
  129. data + offset, count - offset);
  130. }
  131. static int regmap_mmio_read(void *context,
  132. const void *reg, size_t reg_size,
  133. void *val, size_t val_size)
  134. {
  135. struct regmap_mmio_context *ctx = context;
  136. unsigned int offset;
  137. int ret;
  138. regmap_mmio_regsize_check(reg_size);
  139. if (!IS_ERR(ctx->clk)) {
  140. ret = clk_enable(ctx->clk);
  141. if (ret < 0)
  142. return ret;
  143. }
  144. offset = regmap_mmio_get_offset(reg, reg_size);
  145. while (val_size) {
  146. switch (ctx->val_bytes) {
  147. case 1:
  148. *(u8 *)val = readb(ctx->regs + offset);
  149. break;
  150. case 2:
  151. *(u16 *)val = readw(ctx->regs + offset);
  152. break;
  153. case 4:
  154. *(u32 *)val = readl(ctx->regs + offset);
  155. break;
  156. #ifdef CONFIG_64BIT
  157. case 8:
  158. *(u64 *)val = readq(ctx->regs + offset);
  159. break;
  160. #endif
  161. default:
  162. /* Should be caught by regmap_mmio_check_config */
  163. BUG();
  164. }
  165. val_size -= ctx->val_bytes;
  166. val += ctx->val_bytes;
  167. offset += ctx->val_bytes;
  168. }
  169. if (!IS_ERR(ctx->clk))
  170. clk_disable(ctx->clk);
  171. return 0;
  172. }
  173. static void regmap_mmio_free_context(void *context)
  174. {
  175. struct regmap_mmio_context *ctx = context;
  176. if (!IS_ERR(ctx->clk)) {
  177. clk_unprepare(ctx->clk);
  178. clk_put(ctx->clk);
  179. }
  180. kfree(context);
  181. }
  182. static struct regmap_bus regmap_mmio = {
  183. .fast_io = true,
  184. .write = regmap_mmio_write,
  185. .gather_write = regmap_mmio_gather_write,
  186. .read = regmap_mmio_read,
  187. .free_context = regmap_mmio_free_context,
  188. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  189. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  190. };
  191. static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
  192. const char *clk_id,
  193. void __iomem *regs,
  194. const struct regmap_config *config)
  195. {
  196. struct regmap_mmio_context *ctx;
  197. int min_stride;
  198. int ret;
  199. ret = regmap_mmio_regbits_check(config->reg_bits);
  200. if (ret)
  201. return ERR_PTR(ret);
  202. if (config->pad_bits)
  203. return ERR_PTR(-EINVAL);
  204. switch (config->val_bits) {
  205. case 8:
  206. /* The core treats 0 as 1 */
  207. min_stride = 0;
  208. break;
  209. case 16:
  210. min_stride = 2;
  211. break;
  212. case 32:
  213. min_stride = 4;
  214. break;
  215. #ifdef CONFIG_64BIT
  216. case 64:
  217. min_stride = 8;
  218. break;
  219. #endif
  220. break;
  221. default:
  222. return ERR_PTR(-EINVAL);
  223. }
  224. if (config->reg_stride < min_stride)
  225. return ERR_PTR(-EINVAL);
  226. switch (config->reg_format_endian) {
  227. case REGMAP_ENDIAN_DEFAULT:
  228. case REGMAP_ENDIAN_NATIVE:
  229. break;
  230. default:
  231. return ERR_PTR(-EINVAL);
  232. }
  233. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  234. if (!ctx)
  235. return ERR_PTR(-ENOMEM);
  236. ctx->regs = regs;
  237. ctx->val_bytes = config->val_bits / 8;
  238. ctx->reg_bytes = config->reg_bits / 8;
  239. ctx->pad_bytes = config->pad_bits / 8;
  240. ctx->clk = ERR_PTR(-ENODEV);
  241. if (clk_id == NULL)
  242. return ctx;
  243. ctx->clk = clk_get(dev, clk_id);
  244. if (IS_ERR(ctx->clk)) {
  245. ret = PTR_ERR(ctx->clk);
  246. goto err_free;
  247. }
  248. ret = clk_prepare(ctx->clk);
  249. if (ret < 0) {
  250. clk_put(ctx->clk);
  251. goto err_free;
  252. }
  253. return ctx;
  254. err_free:
  255. kfree(ctx);
  256. return ERR_PTR(ret);
  257. }
  258. struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
  259. void __iomem *regs,
  260. const struct regmap_config *config,
  261. struct lock_class_key *lock_key,
  262. const char *lock_name)
  263. {
  264. struct regmap_mmio_context *ctx;
  265. ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
  266. if (IS_ERR(ctx))
  267. return ERR_CAST(ctx);
  268. return __regmap_init(dev, &regmap_mmio, ctx, config,
  269. lock_key, lock_name);
  270. }
  271. EXPORT_SYMBOL_GPL(__regmap_init_mmio_clk);
  272. struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
  273. const char *clk_id,
  274. void __iomem *regs,
  275. const struct regmap_config *config,
  276. struct lock_class_key *lock_key,
  277. const char *lock_name)
  278. {
  279. struct regmap_mmio_context *ctx;
  280. ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
  281. if (IS_ERR(ctx))
  282. return ERR_CAST(ctx);
  283. return __devm_regmap_init(dev, &regmap_mmio, ctx, config,
  284. lock_key, lock_name);
  285. }
  286. EXPORT_SYMBOL_GPL(__devm_regmap_init_mmio_clk);
  287. MODULE_LICENSE("GPL v2");