regmap.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. static struct regmap *regmap_alloc(int count)
  18. {
  19. struct regmap *map;
  20. map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
  21. if (!map)
  22. return NULL;
  23. map->range_count = count;
  24. return map;
  25. }
  26. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  27. int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
  28. struct regmap **mapp)
  29. {
  30. struct regmap_range *range;
  31. struct regmap *map;
  32. map = regmap_alloc(count);
  33. if (!map)
  34. return -ENOMEM;
  35. for (range = map->ranges; count > 0; reg += 2, range++, count--) {
  36. range->start = *reg;
  37. range->size = reg[1];
  38. }
  39. *mapp = map;
  40. return 0;
  41. }
  42. #else
  43. int regmap_init_mem(ofnode node, struct regmap **mapp)
  44. {
  45. struct regmap_range *range;
  46. struct regmap *map;
  47. int count;
  48. int addr_len, size_len, both_len;
  49. int len;
  50. int index;
  51. struct resource r;
  52. addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
  53. size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
  54. both_len = addr_len + size_len;
  55. len = ofnode_read_size(node, "reg");
  56. if (len < 0)
  57. return len;
  58. len /= sizeof(fdt32_t);
  59. count = len / both_len;
  60. if (!count)
  61. return -EINVAL;
  62. map = regmap_alloc(count);
  63. if (!map)
  64. return -ENOMEM;
  65. for (range = map->ranges, index = 0; count > 0;
  66. count--, range++, index++) {
  67. fdt_size_t sz;
  68. if (of_live_active()) {
  69. of_address_to_resource(ofnode_to_np(node), index, &r);
  70. range->start = r.start;
  71. range->size = r.end - r.start + 1;
  72. } else {
  73. range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob,
  74. ofnode_to_offset(node), "reg", index,
  75. addr_len, size_len, &sz, true);
  76. range->size = sz;
  77. }
  78. }
  79. *mapp = map;
  80. return 0;
  81. }
  82. #endif
  83. void *regmap_get_range(struct regmap *map, unsigned int range_num)
  84. {
  85. struct regmap_range *range;
  86. if (range_num >= map->range_count)
  87. return NULL;
  88. range = &map->ranges[range_num];
  89. return map_sysmem(range->start, range->size);
  90. }
  91. int regmap_uninit(struct regmap *map)
  92. {
  93. free(map);
  94. return 0;
  95. }
  96. int regmap_read(struct regmap *map, uint offset, uint *valp)
  97. {
  98. u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
  99. *valp = le32_to_cpu(readl(ptr));
  100. return 0;
  101. }
  102. int regmap_write(struct regmap *map, uint offset, uint val)
  103. {
  104. u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
  105. writel(cpu_to_le32(val), ptr);
  106. return 0;
  107. }
  108. int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
  109. {
  110. uint reg;
  111. int ret;
  112. ret = regmap_read(map, offset, &reg);
  113. if (ret)
  114. return ret;
  115. reg &= ~mask;
  116. return regmap_write(map, offset, reg | val);
  117. }