regmap-mmio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/err.h>
  19. #include <linux/init.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. };
  28. static int regmap_mmio_gather_write(void *context,
  29. const void *reg, size_t reg_size,
  30. const void *val, size_t val_size)
  31. {
  32. struct regmap_mmio_context *ctx = context;
  33. u32 offset;
  34. BUG_ON(reg_size != 4);
  35. offset = be32_to_cpup(reg);
  36. while (val_size) {
  37. switch (ctx->val_bytes) {
  38. case 1:
  39. writeb(*(u8 *)val, ctx->regs + offset);
  40. break;
  41. case 2:
  42. writew(be16_to_cpup(val), ctx->regs + offset);
  43. break;
  44. case 4:
  45. writel(be32_to_cpup(val), ctx->regs + offset);
  46. break;
  47. #ifdef CONFIG_64BIT
  48. case 8:
  49. writeq(be64_to_cpup(val), ctx->regs + offset);
  50. break;
  51. #endif
  52. default:
  53. /* Should be caught by regmap_mmio_check_config */
  54. BUG();
  55. }
  56. val_size -= ctx->val_bytes;
  57. val += ctx->val_bytes;
  58. offset += ctx->val_bytes;
  59. }
  60. return 0;
  61. }
  62. static int regmap_mmio_write(void *context, const void *data, size_t count)
  63. {
  64. BUG_ON(count < 4);
  65. return regmap_mmio_gather_write(context, data, 4, data + 4, count - 4);
  66. }
  67. static int regmap_mmio_read(void *context,
  68. const void *reg, size_t reg_size,
  69. void *val, size_t val_size)
  70. {
  71. struct regmap_mmio_context *ctx = context;
  72. u32 offset;
  73. BUG_ON(reg_size != 4);
  74. offset = be32_to_cpup(reg);
  75. while (val_size) {
  76. switch (ctx->val_bytes) {
  77. case 1:
  78. *(u8 *)val = readb(ctx->regs + offset);
  79. break;
  80. case 2:
  81. *(u16 *)val = cpu_to_be16(readw(ctx->regs + offset));
  82. break;
  83. case 4:
  84. *(u32 *)val = cpu_to_be32(readl(ctx->regs + offset));
  85. break;
  86. #ifdef CONFIG_64BIT
  87. case 8:
  88. *(u64 *)val = cpu_to_be32(readq(ctx->regs + offset));
  89. break;
  90. #endif
  91. default:
  92. /* Should be caught by regmap_mmio_check_config */
  93. BUG();
  94. }
  95. val_size -= ctx->val_bytes;
  96. val += ctx->val_bytes;
  97. offset += ctx->val_bytes;
  98. }
  99. return 0;
  100. }
  101. static void regmap_mmio_free_context(void *context)
  102. {
  103. kfree(context);
  104. }
  105. static struct regmap_bus regmap_mmio = {
  106. .fast_io = true,
  107. .write = regmap_mmio_write,
  108. .gather_write = regmap_mmio_gather_write,
  109. .read = regmap_mmio_read,
  110. .free_context = regmap_mmio_free_context,
  111. };
  112. struct regmap_mmio_context *regmap_mmio_gen_context(void __iomem *regs,
  113. const struct regmap_config *config)
  114. {
  115. struct regmap_mmio_context *ctx;
  116. if (config->reg_bits != 32)
  117. return ERR_PTR(-EINVAL);
  118. if (config->pad_bits)
  119. return ERR_PTR(-EINVAL);
  120. switch (config->val_bits) {
  121. case 8:
  122. case 16:
  123. case 32:
  124. #ifdef CONFIG_64BIT
  125. case 64:
  126. #endif
  127. break;
  128. default:
  129. return ERR_PTR(-EINVAL);
  130. }
  131. ctx = kzalloc(GFP_KERNEL, sizeof(*ctx));
  132. if (!ctx)
  133. return ERR_PTR(-ENOMEM);
  134. ctx->regs = regs;
  135. ctx->val_bytes = config->val_bits / 8;
  136. return ctx;
  137. }
  138. /**
  139. * regmap_init_mmio(): Initialise register map
  140. *
  141. * @dev: Device that will be interacted with
  142. * @regs: Pointer to memory-mapped IO region
  143. * @config: Configuration for register map
  144. *
  145. * The return value will be an ERR_PTR() on error or a valid pointer to
  146. * a struct regmap.
  147. */
  148. struct regmap *regmap_init_mmio(struct device *dev,
  149. void __iomem *regs,
  150. const struct regmap_config *config)
  151. {
  152. struct regmap_mmio_context *ctx;
  153. ctx = regmap_mmio_gen_context(regs, config);
  154. if (IS_ERR(ctx))
  155. return ERR_CAST(ctx);
  156. return regmap_init(dev, &regmap_mmio, ctx, config);
  157. }
  158. EXPORT_SYMBOL_GPL(regmap_init_mmio);
  159. /**
  160. * devm_regmap_init_mmio(): Initialise managed register map
  161. *
  162. * @dev: Device that will be interacted with
  163. * @regs: Pointer to memory-mapped IO region
  164. * @config: Configuration for register map
  165. *
  166. * The return value will be an ERR_PTR() on error or a valid pointer
  167. * to a struct regmap. The regmap will be automatically freed by the
  168. * device management code.
  169. */
  170. struct regmap *devm_regmap_init_mmio(struct device *dev,
  171. void __iomem *regs,
  172. const struct regmap_config *config)
  173. {
  174. struct regmap_mmio_context *ctx;
  175. ctx = regmap_mmio_gen_context(regs, config);
  176. if (IS_ERR(ctx))
  177. return ERR_CAST(ctx);
  178. return devm_regmap_init(dev, &regmap_mmio, ctx, config);
  179. }
  180. EXPORT_SYMBOL_GPL(devm_regmap_init_mmio);
  181. MODULE_LICENSE("GPL v2");