Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 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 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <libfdt.h> |
| 12 | #include <malloc.h> |
| 13 | #include <mapmem.h> |
| 14 | #include <regmap.h> |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 15 | #include <asm/io.h> |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 16 | #include <dm/of_addr.h> |
| 17 | #include <linux/ioport.h> |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 18 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 21 | static struct regmap *regmap_alloc_count(int count) |
| 22 | { |
| 23 | struct regmap *map; |
| 24 | |
| 25 | map = malloc(sizeof(struct regmap)); |
| 26 | if (!map) |
| 27 | return NULL; |
| 28 | if (count <= 1) { |
| 29 | map->range = &map->base_range; |
| 30 | } else { |
| 31 | map->range = malloc(count * sizeof(struct regmap_range)); |
| 32 | if (!map->range) { |
| 33 | free(map); |
| 34 | return NULL; |
| 35 | } |
| 36 | } |
| 37 | map->range_count = count; |
| 38 | |
| 39 | return map; |
| 40 | } |
| 41 | |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 42 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 43 | int regmap_init_mem_platdata(struct udevice *dev, u32 *reg, int count, |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 44 | struct regmap **mapp) |
| 45 | { |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 46 | struct regmap_range *range; |
| 47 | struct regmap *map; |
| 48 | |
| 49 | map = regmap_alloc_count(count); |
| 50 | if (!map) |
| 51 | return -ENOMEM; |
| 52 | |
| 53 | map->base = *reg; |
| 54 | for (range = map->range; count > 0; reg += 2, range++, count--) { |
| 55 | range->start = *reg; |
| 56 | range->size = reg[1]; |
| 57 | } |
| 58 | |
| 59 | *mapp = map; |
| 60 | |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | #else |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 64 | int regmap_init_mem(struct udevice *dev, struct regmap **mapp) |
| 65 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 66 | struct regmap_range *range; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 67 | struct regmap *map; |
| 68 | int count; |
| 69 | int addr_len, size_len, both_len; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 70 | int len; |
Jean-Jacques Hiblot | 024611b | 2017-02-13 16:17:48 +0100 | [diff] [blame] | 71 | int index; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 72 | ofnode node = dev_ofnode(dev); |
| 73 | struct resource r; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 74 | |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame^] | 75 | addr_len = dev_read_simple_addr_cells(dev->parent); |
| 76 | size_len = dev_read_simple_size_cells(dev->parent); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 77 | both_len = addr_len + size_len; |
| 78 | |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 79 | len = dev_read_size(dev, "reg"); |
| 80 | if (len < 0) |
| 81 | return len; |
| 82 | len /= sizeof(fdt32_t); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 83 | count = len / both_len; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 84 | if (!count) |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 85 | return -EINVAL; |
| 86 | |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 87 | map = regmap_alloc_count(count); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 88 | if (!map) |
| 89 | return -ENOMEM; |
| 90 | |
Jean-Jacques Hiblot | 024611b | 2017-02-13 16:17:48 +0100 | [diff] [blame] | 91 | for (range = map->range, index = 0; count > 0; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 92 | count--, range++, index++) { |
Jean-Jacques Hiblot | 024611b | 2017-02-13 16:17:48 +0100 | [diff] [blame] | 93 | fdt_size_t sz; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 94 | if (of_live_active()) { |
| 95 | of_address_to_resource(ofnode_to_np(node), index, &r); |
| 96 | range->start = r.start; |
| 97 | range->size = r.end - r.start + 1; |
| 98 | } else { |
| 99 | range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob, |
| 100 | dev_of_offset(dev), "reg", index, |
| 101 | addr_len, size_len, &sz, true); |
| 102 | range->size = sz; |
| 103 | } |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 104 | } |
Jean-Jacques Hiblot | 024611b | 2017-02-13 16:17:48 +0100 | [diff] [blame] | 105 | map->base = map->range[0].start; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 106 | |
| 107 | *mapp = map; |
| 108 | |
| 109 | return 0; |
| 110 | } |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 111 | #endif |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 112 | |
| 113 | void *regmap_get_range(struct regmap *map, unsigned int range_num) |
| 114 | { |
| 115 | struct regmap_range *range; |
| 116 | |
| 117 | if (range_num >= map->range_count) |
| 118 | return NULL; |
| 119 | range = &map->range[range_num]; |
| 120 | |
| 121 | return map_sysmem(range->start, range->size); |
| 122 | } |
| 123 | |
| 124 | int regmap_uninit(struct regmap *map) |
| 125 | { |
| 126 | if (map->range_count > 1) |
| 127 | free(map->range); |
| 128 | free(map); |
| 129 | |
| 130 | return 0; |
| 131 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 132 | |
| 133 | int regmap_read(struct regmap *map, uint offset, uint *valp) |
| 134 | { |
| 135 | uint32_t *ptr = map_physmem(map->base + offset, 4, MAP_NOCACHE); |
| 136 | |
| 137 | *valp = le32_to_cpu(readl(ptr)); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | int regmap_write(struct regmap *map, uint offset, uint val) |
| 143 | { |
| 144 | uint32_t *ptr = map_physmem(map->base + offset, 4, MAP_NOCACHE); |
| 145 | |
| 146 | writel(cpu_to_le32(val), ptr); |
| 147 | |
| 148 | return 0; |
| 149 | } |