regmap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
  60. both_len = addr_len + size_len;
  61. len = ofnode_read_size(node, "reg");
  62. if (len < 0)
  63. return len;
  64. len /= sizeof(fdt32_t);
  65. count = len / both_len;
  66. if (!count)
  67. return -EINVAL;
  68. map = regmap_alloc(count);
  69. if (!map)
  70. return -ENOMEM;
  71. for (range = map->ranges, index = 0; count > 0;
  72. count--, range++, index++) {
  73. fdt_size_t sz;
  74. if (of_live_active()) {
  75. of_address_to_resource(ofnode_to_np(node), index, &r);
  76. range->start = r.start;
  77. range->size = r.end - r.start + 1;
  78. } else {
  79. range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob,
  80. ofnode_to_offset(node), "reg", index,
  81. addr_len, size_len, &sz, true);
  82. range->size = sz;
  83. }
  84. }
  85. *mapp = map;
  86. return 0;
  87. }
  88. #endif
  89. void *regmap_get_range(struct regmap *map, unsigned int range_num)
  90. {
  91. struct regmap_range *range;
  92. if (range_num >= map->range_count)
  93. return NULL;
  94. range = &map->ranges[range_num];
  95. return map_sysmem(range->start, range->size);
  96. }
  97. int regmap_uninit(struct regmap *map)
  98. {
  99. free(map);
  100. return 0;
  101. }
  102. int regmap_read(struct regmap *map, uint offset, uint *valp)
  103. {
  104. u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
  105. *valp = le32_to_cpu(readl(ptr));
  106. return 0;
  107. }
  108. int regmap_write(struct regmap *map, uint offset, uint val)
  109. {
  110. u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
  111. writel(cpu_to_le32(val), ptr);
  112. return 0;
  113. }
  114. int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
  115. {
  116. uint reg;
  117. int ret;
  118. ret = regmap_read(map, offset, &reg);
  119. if (ret)
  120. return ret;
  121. reg &= ~mask;
  122. return regmap_write(map, offset, reg | val);
  123. }