regmap-mmio.c 8.2 KB

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