regmap-mmio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 = *(u32 *)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(*(u16 *)val, ctx->regs + offset);
  43. break;
  44. case 4:
  45. writel(*(u32 *)val, ctx->regs + offset);
  46. break;
  47. #ifdef CONFIG_64BIT
  48. case 8:
  49. writeq(*(u64 *)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 = *(u32 *)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 = readw(ctx->regs + offset);
  82. break;
  83. case 4:
  84. *(u32 *)val = readl(ctx->regs + offset);
  85. break;
  86. #ifdef CONFIG_64BIT
  87. case 8:
  88. *(u64 *)val = 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. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  112. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  113. };
  114. static struct regmap_mmio_context *regmap_mmio_gen_context(void __iomem *regs,
  115. const struct regmap_config *config)
  116. {
  117. struct regmap_mmio_context *ctx;
  118. int min_stride;
  119. if (config->reg_bits != 32)
  120. return ERR_PTR(-EINVAL);
  121. if (config->pad_bits)
  122. return ERR_PTR(-EINVAL);
  123. switch (config->val_bits) {
  124. case 8:
  125. /* The core treats 0 as 1 */
  126. min_stride = 0;
  127. break;
  128. case 16:
  129. min_stride = 2;
  130. break;
  131. case 32:
  132. min_stride = 4;
  133. break;
  134. #ifdef CONFIG_64BIT
  135. case 64:
  136. min_stride = 8;
  137. break;
  138. #endif
  139. break;
  140. default:
  141. return ERR_PTR(-EINVAL);
  142. }
  143. if (config->reg_stride < min_stride)
  144. return ERR_PTR(-EINVAL);
  145. switch (config->reg_format_endian) {
  146. case REGMAP_ENDIAN_DEFAULT:
  147. case REGMAP_ENDIAN_NATIVE:
  148. break;
  149. default:
  150. return ERR_PTR(-EINVAL);
  151. }
  152. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  153. if (!ctx)
  154. return ERR_PTR(-ENOMEM);
  155. ctx->regs = regs;
  156. ctx->val_bytes = config->val_bits / 8;
  157. return ctx;
  158. }
  159. /**
  160. * regmap_init_mmio(): Initialise 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 to
  167. * a struct regmap.
  168. */
  169. struct regmap *regmap_init_mmio(struct device *dev,
  170. void __iomem *regs,
  171. const struct regmap_config *config)
  172. {
  173. struct regmap_mmio_context *ctx;
  174. ctx = regmap_mmio_gen_context(regs, config);
  175. if (IS_ERR(ctx))
  176. return ERR_CAST(ctx);
  177. return regmap_init(dev, &regmap_mmio, ctx, config);
  178. }
  179. EXPORT_SYMBOL_GPL(regmap_init_mmio);
  180. /**
  181. * devm_regmap_init_mmio(): Initialise managed register map
  182. *
  183. * @dev: Device that will be interacted with
  184. * @regs: Pointer to memory-mapped IO region
  185. * @config: Configuration for register map
  186. *
  187. * The return value will be an ERR_PTR() on error or a valid pointer
  188. * to a struct regmap. The regmap will be automatically freed by the
  189. * device management code.
  190. */
  191. struct regmap *devm_regmap_init_mmio(struct device *dev,
  192. void __iomem *regs,
  193. const struct regmap_config *config)
  194. {
  195. struct regmap_mmio_context *ctx;
  196. ctx = regmap_mmio_gen_context(regs, config);
  197. if (IS_ERR(ctx))
  198. return ERR_CAST(ctx);
  199. return devm_regmap_init(dev, &regmap_mmio, ctx, config);
  200. }
  201. EXPORT_SYMBOL_GPL(devm_regmap_init_mmio);
  202. MODULE_LICENSE("GPL v2");