regmap.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <linux/libfdt.h>
  10. #include <malloc.h>
  11. #include <mapmem.h>
  12. #include <regmap.h>
  13. #include <asm/io.h>
  14. #include <dm/of_addr.h>
  15. #include <linux/ioport.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. /**
  18. * regmap_alloc() - Allocate a regmap with a given number of ranges.
  19. *
  20. * @count: Number of ranges to be allocated for the regmap.
  21. * Return: A pointer to the newly allocated regmap, or NULL on error.
  22. */
  23. static struct regmap *regmap_alloc(int count)
  24. {
  25. struct regmap *map;
  26. map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
  27. if (!map)
  28. return NULL;
  29. map->range_count = count;
  30. return map;
  31. }
  32. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  33. int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
  34. struct regmap **mapp)
  35. {
  36. struct regmap_range *range;
  37. struct regmap *map;
  38. map = regmap_alloc(count);
  39. if (!map)
  40. return -ENOMEM;
  41. for (range = map->ranges; count > 0; reg += 2, range++, count--) {
  42. range->start = *reg;
  43. range->size = reg[1];
  44. }
  45. *mapp = map;
  46. return 0;
  47. }
  48. #else
  49. int regmap_init_mem(ofnode node, struct regmap **mapp)
  50. {
  51. struct regmap_range *range;
  52. struct regmap *map;
  53. int count;
  54. int addr_len, size_len, both_len;
  55. int len;
  56. int index;
  57. struct resource r;
  58. addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
  59. if (addr_len < 0) {
  60. debug("%s: Error while reading the addr length (ret = %d)\n",
  61. ofnode_get_name(node), addr_len);
  62. return addr_len;
  63. }
  64. size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
  65. if (size_len < 0) {
  66. debug("%s: Error while reading the size length: (ret = %d)\n",
  67. ofnode_get_name(node), size_len);
  68. return size_len;
  69. }
  70. both_len = addr_len + size_len;
  71. if (!both_len) {
  72. debug("%s: Both addr and size length are zero\n",
  73. ofnode_get_name(node));
  74. return -EINVAL;
  75. }
  76. len = ofnode_read_size(node, "reg");
  77. if (len < 0)
  78. return len;
  79. len /= sizeof(fdt32_t);
  80. count = len / both_len;
  81. if (!count)
  82. return -EINVAL;
  83. map = regmap_alloc(count);
  84. if (!map)
  85. return -ENOMEM;
  86. for (range = map->ranges, index = 0; count > 0;
  87. count--, range++, index++) {
  88. fdt_size_t sz;
  89. if (of_live_active()) {
  90. of_address_to_resource(ofnode_to_np(node), index, &r);
  91. range->start = r.start;
  92. range->size = r.end - r.start + 1;
  93. } else {
  94. range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob,
  95. ofnode_to_offset(node), "reg", index,
  96. addr_len, size_len, &sz, true);
  97. range->size = sz;
  98. }
  99. }
  100. *mapp = map;
  101. return 0;
  102. }
  103. #endif
  104. void *regmap_get_range(struct regmap *map, unsigned int range_num)
  105. {
  106. struct regmap_range *range;
  107. if (range_num >= map->range_count)
  108. return NULL;
  109. range = &map->ranges[range_num];
  110. return map_sysmem(range->start, range->size);
  111. }
  112. int regmap_uninit(struct regmap *map)
  113. {
  114. free(map);
  115. return 0;
  116. }
  117. int regmap_read(struct regmap *map, uint offset, uint *valp)
  118. {
  119. u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
  120. *valp = le32_to_cpu(readl(ptr));
  121. return 0;
  122. }
  123. int regmap_write(struct regmap *map, uint offset, uint val)
  124. {
  125. u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
  126. writel(cpu_to_le32(val), ptr);
  127. return 0;
  128. }
  129. int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
  130. {
  131. uint reg;
  132. int ret;
  133. ret = regmap_read(map, offset, &reg);
  134. if (ret)
  135. return ret;
  136. reg &= ~mask;
  137. return regmap_write(map, offset, reg | val);
  138. }