blob: 154426269d97ab87ceeea516c444c3fb49a629e4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassad8f8ab2015-06-23 15:38:42 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassad8f8ab2015-06-23 15:38:42 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Simon Glassad8f8ab2015-06-23 15:38:42 -060011#include <malloc.h>
12#include <mapmem.h>
13#include <regmap.h>
Paul Burton39776032016-09-08 07:47:35 +010014#include <asm/io.h>
Simon Glasseeeb5192017-05-18 20:09:10 -060015#include <dm/of_addr.h>
16#include <linux/ioport.h>
Paul Burton39776032016-09-08 07:47:35 +010017
Simon Glassad8f8ab2015-06-23 15:38:42 -060018DECLARE_GLOBAL_DATA_PTR;
19
Mario Six0aa52992018-10-04 09:00:42 +020020/**
21 * regmap_alloc() - Allocate a regmap with a given number of ranges.
22 *
23 * @count: Number of ranges to be allocated for the regmap.
24 * Return: A pointer to the newly allocated regmap, or NULL on error.
25 */
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090026static struct regmap *regmap_alloc(int count)
Simon Glass30d73e82016-07-04 11:58:21 -060027{
28 struct regmap *map;
29
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090030 map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
Simon Glass30d73e82016-07-04 11:58:21 -060031 if (!map)
32 return NULL;
Simon Glass30d73e82016-07-04 11:58:21 -060033 map->range_count = count;
34
35 return map;
36}
37
Simon Glassb9443452016-07-04 11:57:59 -060038#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass1b1fe412017-08-29 14:15:50 -060039int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
Simon Glassb9443452016-07-04 11:57:59 -060040 struct regmap **mapp)
41{
Simon Glassb6114332016-07-04 11:58:22 -060042 struct regmap_range *range;
43 struct regmap *map;
44
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090045 map = regmap_alloc(count);
Simon Glassb6114332016-07-04 11:58:22 -060046 if (!map)
47 return -ENOMEM;
48
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090049 for (range = map->ranges; count > 0; reg += 2, range++, count--) {
Simon Glassb6114332016-07-04 11:58:22 -060050 range->start = *reg;
51 range->size = reg[1];
52 }
53
54 *mapp = map;
55
Simon Glassb9443452016-07-04 11:57:59 -060056 return 0;
57}
58#else
Mario Six5159c0c2018-10-15 09:24:07 +020059/**
60 * init_range() - Initialize a single range of a regmap
61 * @node: Device node that will use the map in question
62 * @range: Pointer to a regmap_range structure that will be initialized
63 * @addr_len: The length of the addr parts of the reg property
64 * @size_len: The length of the size parts of the reg property
65 * @index: The index of the range to initialize
66 *
67 * This function will read the necessary 'reg' information from the device tree
68 * (the 'addr' part, and the 'length' part), and initialize the range in
69 * quesion.
70 *
71 * Return: 0 if OK, -ve on error
72 */
73static int init_range(ofnode node, struct regmap_range *range, int addr_len,
74 int size_len, int index)
75{
76 fdt_size_t sz;
77 struct resource r;
78
79 if (of_live_active()) {
80 int ret;
81
82 ret = of_address_to_resource(ofnode_to_np(node),
83 index, &r);
84 if (ret) {
85 debug("%s: Could not read resource of range %d (ret = %d)\n",
86 ofnode_get_name(node), index, ret);
87 return ret;
88 }
89
90 range->start = r.start;
91 range->size = r.end - r.start + 1;
92 } else {
93 int offset = ofnode_to_offset(node);
94
95 range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob, offset,
96 "reg", index,
97 addr_len, size_len,
98 &sz, true);
99 if (range->start == FDT_ADDR_T_NONE) {
100 debug("%s: Could not read start of range %d\n",
101 ofnode_get_name(node), index);
102 return -EINVAL;
103 }
104
105 range->size = sz;
106 }
107
108 return 0;
109}
110
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900111int regmap_init_mem(ofnode node, struct regmap **mapp)
Simon Glassad8f8ab2015-06-23 15:38:42 -0600112{
Simon Glassad8f8ab2015-06-23 15:38:42 -0600113 struct regmap_range *range;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600114 struct regmap *map;
115 int count;
116 int addr_len, size_len, both_len;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600117 int len;
Jean-Jacques Hiblot024611b2017-02-13 16:17:48 +0100118 int index;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600119
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900120 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
Mario Six12321162018-10-04 09:00:43 +0200121 if (addr_len < 0) {
122 debug("%s: Error while reading the addr length (ret = %d)\n",
123 ofnode_get_name(node), addr_len);
124 return addr_len;
125 }
126
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900127 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
Mario Six12321162018-10-04 09:00:43 +0200128 if (size_len < 0) {
129 debug("%s: Error while reading the size length: (ret = %d)\n",
130 ofnode_get_name(node), size_len);
131 return size_len;
132 }
133
Simon Glassad8f8ab2015-06-23 15:38:42 -0600134 both_len = addr_len + size_len;
Mario Six12321162018-10-04 09:00:43 +0200135 if (!both_len) {
136 debug("%s: Both addr and size length are zero\n",
137 ofnode_get_name(node));
138 return -EINVAL;
139 }
Simon Glassad8f8ab2015-06-23 15:38:42 -0600140
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900141 len = ofnode_read_size(node, "reg");
Mario Six6e96ba22018-10-15 09:24:08 +0200142 if (len < 0) {
143 debug("%s: Error while reading reg size (ret = %d)\n",
144 ofnode_get_name(node), len);
Simon Glasseeeb5192017-05-18 20:09:10 -0600145 return len;
Mario Six6e96ba22018-10-15 09:24:08 +0200146 }
Simon Glasseeeb5192017-05-18 20:09:10 -0600147 len /= sizeof(fdt32_t);
Simon Glassad8f8ab2015-06-23 15:38:42 -0600148 count = len / both_len;
Mario Six6e96ba22018-10-15 09:24:08 +0200149 if (!count) {
150 debug("%s: Not enough data in reg property\n",
151 ofnode_get_name(node));
Simon Glassad8f8ab2015-06-23 15:38:42 -0600152 return -EINVAL;
Mario Six6e96ba22018-10-15 09:24:08 +0200153 }
Simon Glassad8f8ab2015-06-23 15:38:42 -0600154
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900155 map = regmap_alloc(count);
Simon Glassad8f8ab2015-06-23 15:38:42 -0600156 if (!map)
157 return -ENOMEM;
158
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900159 for (range = map->ranges, index = 0; count > 0;
Simon Glasseeeb5192017-05-18 20:09:10 -0600160 count--, range++, index++) {
Mario Six5159c0c2018-10-15 09:24:07 +0200161 int ret = init_range(node, range, addr_len, size_len, index);
162
163 if (ret)
164 return ret;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600165 }
166
167 *mapp = map;
168
169 return 0;
170}
Simon Glassb9443452016-07-04 11:57:59 -0600171#endif
Simon Glassad8f8ab2015-06-23 15:38:42 -0600172
173void *regmap_get_range(struct regmap *map, unsigned int range_num)
174{
175 struct regmap_range *range;
176
177 if (range_num >= map->range_count)
178 return NULL;
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900179 range = &map->ranges[range_num];
Simon Glassad8f8ab2015-06-23 15:38:42 -0600180
181 return map_sysmem(range->start, range->size);
182}
183
184int regmap_uninit(struct regmap *map)
185{
Simon Glassad8f8ab2015-06-23 15:38:42 -0600186 free(map);
187
188 return 0;
189}
Paul Burton39776032016-09-08 07:47:35 +0100190
191int regmap_read(struct regmap *map, uint offset, uint *valp)
192{
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900193 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
Paul Burton39776032016-09-08 07:47:35 +0100194
195 *valp = le32_to_cpu(readl(ptr));
196
197 return 0;
198}
199
200int regmap_write(struct regmap *map, uint offset, uint val)
201{
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900202 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
Paul Burton39776032016-09-08 07:47:35 +0100203
204 writel(cpu_to_le32(val), ptr);
205
206 return 0;
207}
Neil Armstrong5444ec62018-04-27 11:56:14 +0200208
209int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
210{
211 uint reg;
212 int ret;
213
214 ret = regmap_read(map, offset, &reg);
215 if (ret)
216 return ret;
217
218 reg &= ~mask;
219
220 return regmap_write(map, offset, reg | val);
221}