regmap-mmio.c 4.9 KB

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