regmap-mmio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. int min_stride;
  117. if (config->reg_bits != 32)
  118. return ERR_PTR(-EINVAL);
  119. if (config->pad_bits)
  120. return ERR_PTR(-EINVAL);
  121. switch (config->val_bits) {
  122. case 8:
  123. /* The core treats 0 as 1 */
  124. min_stride = 0;
  125. break;
  126. case 16:
  127. min_stride = 2;
  128. break;
  129. case 32:
  130. min_stride = 4;
  131. break;
  132. #ifdef CONFIG_64BIT
  133. case 64:
  134. min_stride = 8;
  135. break;
  136. #endif
  137. break;
  138. default:
  139. return ERR_PTR(-EINVAL);
  140. }
  141. if (config->reg_stride < min_stride)
  142. return ERR_PTR(-EINVAL);
  143. ctx = kzalloc(GFP_KERNEL, sizeof(*ctx));
  144. if (!ctx)
  145. return ERR_PTR(-ENOMEM);
  146. ctx->regs = regs;
  147. ctx->val_bytes = config->val_bits / 8;
  148. return ctx;
  149. }
  150. /**
  151. * regmap_init_mmio(): Initialise register map
  152. *
  153. * @dev: Device that will be interacted with
  154. * @regs: Pointer to memory-mapped IO region
  155. * @config: Configuration for register map
  156. *
  157. * The return value will be an ERR_PTR() on error or a valid pointer to
  158. * a struct regmap.
  159. */
  160. struct regmap *regmap_init_mmio(struct device *dev,
  161. void __iomem *regs,
  162. const struct regmap_config *config)
  163. {
  164. struct regmap_mmio_context *ctx;
  165. ctx = regmap_mmio_gen_context(regs, config);
  166. if (IS_ERR(ctx))
  167. return ERR_CAST(ctx);
  168. return regmap_init(dev, &regmap_mmio, ctx, config);
  169. }
  170. EXPORT_SYMBOL_GPL(regmap_init_mmio);
  171. /**
  172. * devm_regmap_init_mmio(): Initialise managed register map
  173. *
  174. * @dev: Device that will be interacted with
  175. * @regs: Pointer to memory-mapped IO region
  176. * @config: Configuration for register map
  177. *
  178. * The return value will be an ERR_PTR() on error or a valid pointer
  179. * to a struct regmap. The regmap will be automatically freed by the
  180. * device management code.
  181. */
  182. struct regmap *devm_regmap_init_mmio(struct device *dev,
  183. void __iomem *regs,
  184. const struct regmap_config *config)
  185. {
  186. struct regmap_mmio_context *ctx;
  187. ctx = regmap_mmio_gen_context(regs, config);
  188. if (IS_ERR(ctx))
  189. return ERR_CAST(ctx);
  190. return devm_regmap_init(dev, &regmap_mmio, ctx, config);
  191. }
  192. EXPORT_SYMBOL_GPL(devm_regmap_init_mmio);
  193. MODULE_LICENSE("GPL v2");