regmap.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <libfdt.h>
  11. #include <malloc.h>
  12. #include <mapmem.h>
  13. #include <regmap.h>
  14. #include <asm/io.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. static struct regmap *regmap_alloc_count(int count)
  17. {
  18. struct regmap *map;
  19. map = malloc(sizeof(struct regmap));
  20. if (!map)
  21. return NULL;
  22. if (count <= 1) {
  23. map->range = &map->base_range;
  24. } else {
  25. map->range = malloc(count * sizeof(struct regmap_range));
  26. if (!map->range) {
  27. free(map);
  28. return NULL;
  29. }
  30. }
  31. map->range_count = count;
  32. return map;
  33. }
  34. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  35. int regmap_init_mem_platdata(struct udevice *dev, u32 *reg, int count,
  36. struct regmap **mapp)
  37. {
  38. struct regmap_range *range;
  39. struct regmap *map;
  40. map = regmap_alloc_count(count);
  41. if (!map)
  42. return -ENOMEM;
  43. map->base = *reg;
  44. for (range = map->range; count > 0; reg += 2, range++, count--) {
  45. range->start = *reg;
  46. range->size = reg[1];
  47. }
  48. *mapp = map;
  49. return 0;
  50. }
  51. #else
  52. int regmap_init_mem(struct udevice *dev, struct regmap **mapp)
  53. {
  54. const void *blob = gd->fdt_blob;
  55. struct regmap_range *range;
  56. const fdt32_t *cell;
  57. struct regmap *map;
  58. int count;
  59. int addr_len, size_len, both_len;
  60. int parent;
  61. int len;
  62. int index;
  63. parent = dev_of_offset(dev->parent);
  64. addr_len = fdt_address_cells(blob, parent);
  65. size_len = fdt_size_cells(blob, parent);
  66. both_len = addr_len + size_len;
  67. cell = fdt_getprop(blob, dev_of_offset(dev), "reg", &len);
  68. len /= sizeof(*cell);
  69. count = len / both_len;
  70. if (!cell || !count)
  71. return -EINVAL;
  72. map = regmap_alloc_count(count);
  73. if (!map)
  74. return -ENOMEM;
  75. for (range = map->range, index = 0; count > 0;
  76. count--, cell += both_len, range++, index++) {
  77. fdt_size_t sz;
  78. range->start = fdtdec_get_addr_size_fixed(blob,
  79. dev_of_offset(dev), "reg", index, addr_len,
  80. size_len, &sz, true);
  81. range->size = sz;
  82. }
  83. map->base = map->range[0].start;
  84. *mapp = map;
  85. return 0;
  86. }
  87. #endif
  88. void *regmap_get_range(struct regmap *map, unsigned int range_num)
  89. {
  90. struct regmap_range *range;
  91. if (range_num >= map->range_count)
  92. return NULL;
  93. range = &map->range[range_num];
  94. return map_sysmem(range->start, range->size);
  95. }
  96. int regmap_uninit(struct regmap *map)
  97. {
  98. if (map->range_count > 1)
  99. free(map->range);
  100. free(map);
  101. return 0;
  102. }
  103. int regmap_read(struct regmap *map, uint offset, uint *valp)
  104. {
  105. uint32_t *ptr = map_physmem(map->base + offset, 4, MAP_NOCACHE);
  106. *valp = le32_to_cpu(readl(ptr));
  107. return 0;
  108. }
  109. int regmap_write(struct regmap *map, uint offset, uint val)
  110. {
  111. uint32_t *ptr = map_physmem(map->base + offset, 4, MAP_NOCACHE);
  112. writel(cpu_to_le32(val), ptr);
  113. return 0;
  114. }