regmap-mmio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 val_bytes;
  27. struct clk *clk;
  28. void (*reg_write)(struct regmap_mmio_context *ctx,
  29. unsigned int reg, unsigned int val);
  30. unsigned int (*reg_read)(struct regmap_mmio_context *ctx,
  31. unsigned int reg);
  32. };
  33. static int regmap_mmio_regbits_check(size_t reg_bits)
  34. {
  35. switch (reg_bits) {
  36. case 8:
  37. case 16:
  38. case 32:
  39. #ifdef CONFIG_64BIT
  40. case 64:
  41. #endif
  42. return 0;
  43. default:
  44. return -EINVAL;
  45. }
  46. }
  47. static int regmap_mmio_get_min_stride(size_t val_bits)
  48. {
  49. int min_stride;
  50. switch (val_bits) {
  51. case 8:
  52. /* The core treats 0 as 1 */
  53. min_stride = 0;
  54. return 0;
  55. case 16:
  56. min_stride = 2;
  57. break;
  58. case 32:
  59. min_stride = 4;
  60. break;
  61. #ifdef CONFIG_64BIT
  62. case 64:
  63. min_stride = 8;
  64. break;
  65. #endif
  66. default:
  67. return -EINVAL;
  68. }
  69. return min_stride;
  70. }
  71. static void regmap_mmio_write8(struct regmap_mmio_context *ctx,
  72. unsigned int reg,
  73. unsigned int val)
  74. {
  75. writeb(val, ctx->regs + reg);
  76. }
  77. static void regmap_mmio_write16le(struct regmap_mmio_context *ctx,
  78. unsigned int reg,
  79. unsigned int val)
  80. {
  81. writew(val, ctx->regs + reg);
  82. }
  83. static void regmap_mmio_write16be(struct regmap_mmio_context *ctx,
  84. unsigned int reg,
  85. unsigned int val)
  86. {
  87. iowrite16be(val, ctx->regs + reg);
  88. }
  89. static void regmap_mmio_write32le(struct regmap_mmio_context *ctx,
  90. unsigned int reg,
  91. unsigned int val)
  92. {
  93. writel(val, ctx->regs + reg);
  94. }
  95. static void regmap_mmio_write32be(struct regmap_mmio_context *ctx,
  96. unsigned int reg,
  97. unsigned int val)
  98. {
  99. iowrite32be(val, ctx->regs + reg);
  100. }
  101. #ifdef CONFIG_64BIT
  102. static void regmap_mmio_write64le(struct regmap_mmio_context *ctx,
  103. unsigned int reg,
  104. unsigned int val)
  105. {
  106. writeq(val, ctx->regs + reg);
  107. }
  108. #endif
  109. static int regmap_mmio_write(void *context, unsigned int reg, unsigned int val)
  110. {
  111. struct regmap_mmio_context *ctx = context;
  112. int ret;
  113. if (!IS_ERR(ctx->clk)) {
  114. ret = clk_enable(ctx->clk);
  115. if (ret < 0)
  116. return ret;
  117. }
  118. ctx->reg_write(ctx, reg, val);
  119. if (!IS_ERR(ctx->clk))
  120. clk_disable(ctx->clk);
  121. return 0;
  122. }
  123. static unsigned int regmap_mmio_read8(struct regmap_mmio_context *ctx,
  124. unsigned int reg)
  125. {
  126. return readb(ctx->regs + reg);
  127. }
  128. static unsigned int regmap_mmio_read16le(struct regmap_mmio_context *ctx,
  129. unsigned int reg)
  130. {
  131. return readw(ctx->regs + reg);
  132. }
  133. static unsigned int regmap_mmio_read16be(struct regmap_mmio_context *ctx,
  134. unsigned int reg)
  135. {
  136. return ioread16be(ctx->regs + reg);
  137. }
  138. static unsigned int regmap_mmio_read32le(struct regmap_mmio_context *ctx,
  139. unsigned int reg)
  140. {
  141. return readl(ctx->regs + reg);
  142. }
  143. static unsigned int regmap_mmio_read32be(struct regmap_mmio_context *ctx,
  144. unsigned int reg)
  145. {
  146. return ioread32be(ctx->regs + reg);
  147. }
  148. #ifdef CONFIG_64BIT
  149. static unsigned int regmap_mmio_read64le(struct regmap_mmio_context *ctx,
  150. unsigned int reg)
  151. {
  152. return readq(ctx->regs + reg);
  153. }
  154. #endif
  155. static int regmap_mmio_read(void *context, unsigned int reg, unsigned int *val)
  156. {
  157. struct regmap_mmio_context *ctx = context;
  158. int ret;
  159. if (!IS_ERR(ctx->clk)) {
  160. ret = clk_enable(ctx->clk);
  161. if (ret < 0)
  162. return ret;
  163. }
  164. *val = ctx->reg_read(ctx, reg);
  165. if (!IS_ERR(ctx->clk))
  166. clk_disable(ctx->clk);
  167. return 0;
  168. }
  169. static void regmap_mmio_free_context(void *context)
  170. {
  171. struct regmap_mmio_context *ctx = context;
  172. if (!IS_ERR(ctx->clk)) {
  173. clk_unprepare(ctx->clk);
  174. clk_put(ctx->clk);
  175. }
  176. kfree(context);
  177. }
  178. static const struct regmap_bus regmap_mmio = {
  179. .fast_io = true,
  180. .reg_write = regmap_mmio_write,
  181. .reg_read = regmap_mmio_read,
  182. .free_context = regmap_mmio_free_context,
  183. };
  184. static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
  185. const char *clk_id,
  186. void __iomem *regs,
  187. const struct regmap_config *config)
  188. {
  189. struct regmap_mmio_context *ctx;
  190. int min_stride;
  191. int ret;
  192. ret = regmap_mmio_regbits_check(config->reg_bits);
  193. if (ret)
  194. return ERR_PTR(ret);
  195. if (config->pad_bits)
  196. return ERR_PTR(-EINVAL);
  197. min_stride = regmap_mmio_get_min_stride(config->val_bits);
  198. if (min_stride < 0)
  199. return ERR_PTR(min_stride);
  200. if (config->reg_stride < min_stride)
  201. return ERR_PTR(-EINVAL);
  202. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  203. if (!ctx)
  204. return ERR_PTR(-ENOMEM);
  205. ctx->regs = regs;
  206. ctx->val_bytes = config->val_bits / 8;
  207. ctx->clk = ERR_PTR(-ENODEV);
  208. switch (config->reg_format_endian) {
  209. case REGMAP_ENDIAN_DEFAULT:
  210. case REGMAP_ENDIAN_LITTLE:
  211. #ifdef __LITTLE_ENDIAN
  212. case REGMAP_ENDIAN_NATIVE:
  213. #endif
  214. switch (config->val_bits) {
  215. case 8:
  216. ctx->reg_read = regmap_mmio_read8;
  217. ctx->reg_write = regmap_mmio_write8;
  218. break;
  219. case 16:
  220. ctx->reg_read = regmap_mmio_read16le;
  221. ctx->reg_write = regmap_mmio_write16le;
  222. break;
  223. case 32:
  224. ctx->reg_read = regmap_mmio_read32le;
  225. ctx->reg_write = regmap_mmio_write32le;
  226. break;
  227. #ifdef CONFIG_64BIT
  228. case 64:
  229. ctx->reg_read = regmap_mmio_read64le;
  230. ctx->reg_write = regmap_mmio_write64le;
  231. break;
  232. #endif
  233. default:
  234. ret = -EINVAL;
  235. goto err_free;
  236. }
  237. break;
  238. case REGMAP_ENDIAN_BIG:
  239. #ifdef __BIG_ENDIAN
  240. case REGMAP_ENDIAN_NATIVE:
  241. #endif
  242. switch (config->val_bits) {
  243. case 8:
  244. ctx->reg_read = regmap_mmio_read8;
  245. ctx->reg_write = regmap_mmio_write8;
  246. break;
  247. case 16:
  248. ctx->reg_read = regmap_mmio_read16be;
  249. ctx->reg_write = regmap_mmio_write16be;
  250. break;
  251. case 32:
  252. ctx->reg_read = regmap_mmio_read32be;
  253. ctx->reg_write = regmap_mmio_write32be;
  254. break;
  255. default:
  256. ret = -EINVAL;
  257. goto err_free;
  258. }
  259. break;
  260. default:
  261. ret = -EINVAL;
  262. goto err_free;
  263. }
  264. if (clk_id == NULL)
  265. return ctx;
  266. ctx->clk = clk_get(dev, clk_id);
  267. if (IS_ERR(ctx->clk)) {
  268. ret = PTR_ERR(ctx->clk);
  269. goto err_free;
  270. }
  271. ret = clk_prepare(ctx->clk);
  272. if (ret < 0) {
  273. clk_put(ctx->clk);
  274. goto err_free;
  275. }
  276. return ctx;
  277. err_free:
  278. kfree(ctx);
  279. return ERR_PTR(ret);
  280. }
  281. struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
  282. void __iomem *regs,
  283. const struct regmap_config *config,
  284. struct lock_class_key *lock_key,
  285. const char *lock_name)
  286. {
  287. struct regmap_mmio_context *ctx;
  288. ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
  289. if (IS_ERR(ctx))
  290. return ERR_CAST(ctx);
  291. return __regmap_init(dev, &regmap_mmio, ctx, config,
  292. lock_key, lock_name);
  293. }
  294. EXPORT_SYMBOL_GPL(__regmap_init_mmio_clk);
  295. struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
  296. const char *clk_id,
  297. void __iomem *regs,
  298. const struct regmap_config *config,
  299. struct lock_class_key *lock_key,
  300. const char *lock_name)
  301. {
  302. struct regmap_mmio_context *ctx;
  303. ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
  304. if (IS_ERR(ctx))
  305. return ERR_CAST(ctx);
  306. return __devm_regmap_init(dev, &regmap_mmio, ctx, config,
  307. lock_key, lock_name);
  308. }
  309. EXPORT_SYMBOL_GPL(__devm_regmap_init_mmio_clk);
  310. MODULE_LICENSE("GPL v2");