blob: 8e5c3bcf61bbc4927b2ccf577df927bafba1efb6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6f98b752015-06-23 15:38:42 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass6f98b752015-06-23 15:38:42 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Simon Glass6f98b752015-06-23 15:38:42 -060011#include <malloc.h>
12#include <mapmem.h>
13#include <regmap.h>
Paul Burton3bfb8cb2016-09-08 07:47:35 +010014#include <asm/io.h>
Simon Glass23d63262017-05-18 20:09:10 -060015#include <dm/of_addr.h>
16#include <linux/ioport.h>
Paul Burton3bfb8cb2016-09-08 07:47:35 +010017
Simon Glass6f98b752015-06-23 15:38:42 -060018DECLARE_GLOBAL_DATA_PTR;
19
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +090020static struct regmap *regmap_alloc(int count)
Simon Glassa9514312016-07-04 11:58:21 -060021{
22 struct regmap *map;
23
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +090024 map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
Simon Glassa9514312016-07-04 11:58:21 -060025 if (!map)
26 return NULL;
Simon Glassa9514312016-07-04 11:58:21 -060027 map->range_count = count;
28
29 return map;
30}
31
Simon Glass3b2a29e2016-07-04 11:57:59 -060032#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassc20ee0e2017-08-29 14:15:50 -060033int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
Simon Glass3b2a29e2016-07-04 11:57:59 -060034 struct regmap **mapp)
35{
Simon Glass1e6ca1a2016-07-04 11:58:22 -060036 struct regmap_range *range;
37 struct regmap *map;
38
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +090039 map = regmap_alloc(count);
Simon Glass1e6ca1a2016-07-04 11:58:22 -060040 if (!map)
41 return -ENOMEM;
42
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +090043 for (range = map->ranges; count > 0; reg += 2, range++, count--) {
Simon Glass1e6ca1a2016-07-04 11:58:22 -060044 range->start = *reg;
45 range->size = reg[1];
46 }
47
48 *mapp = map;
49
Simon Glass3b2a29e2016-07-04 11:57:59 -060050 return 0;
51}
52#else
Masahiro Yamadad3581232018-04-19 12:14:03 +090053int regmap_init_mem(ofnode node, struct regmap **mapp)
Simon Glass6f98b752015-06-23 15:38:42 -060054{
Simon Glass6f98b752015-06-23 15:38:42 -060055 struct regmap_range *range;
Simon Glass6f98b752015-06-23 15:38:42 -060056 struct regmap *map;
57 int count;
58 int addr_len, size_len, both_len;
Simon Glass6f98b752015-06-23 15:38:42 -060059 int len;
Jean-Jacques Hiblot18040442017-02-13 16:17:48 +010060 int index;
Simon Glass23d63262017-05-18 20:09:10 -060061 struct resource r;
Simon Glass6f98b752015-06-23 15:38:42 -060062
Masahiro Yamadad3581232018-04-19 12:14:03 +090063 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
64 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
Simon Glass6f98b752015-06-23 15:38:42 -060065 both_len = addr_len + size_len;
66
Masahiro Yamadad3581232018-04-19 12:14:03 +090067 len = ofnode_read_size(node, "reg");
Simon Glass23d63262017-05-18 20:09:10 -060068 if (len < 0)
69 return len;
70 len /= sizeof(fdt32_t);
Simon Glass6f98b752015-06-23 15:38:42 -060071 count = len / both_len;
Simon Glass23d63262017-05-18 20:09:10 -060072 if (!count)
Simon Glass6f98b752015-06-23 15:38:42 -060073 return -EINVAL;
74
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +090075 map = regmap_alloc(count);
Simon Glass6f98b752015-06-23 15:38:42 -060076 if (!map)
77 return -ENOMEM;
78
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +090079 for (range = map->ranges, index = 0; count > 0;
Simon Glass23d63262017-05-18 20:09:10 -060080 count--, range++, index++) {
Jean-Jacques Hiblot18040442017-02-13 16:17:48 +010081 fdt_size_t sz;
Simon Glass23d63262017-05-18 20:09:10 -060082 if (of_live_active()) {
83 of_address_to_resource(ofnode_to_np(node), index, &r);
84 range->start = r.start;
85 range->size = r.end - r.start + 1;
86 } else {
87 range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob,
Masahiro Yamadad3581232018-04-19 12:14:03 +090088 ofnode_to_offset(node), "reg", index,
Simon Glass23d63262017-05-18 20:09:10 -060089 addr_len, size_len, &sz, true);
90 range->size = sz;
91 }
Simon Glass6f98b752015-06-23 15:38:42 -060092 }
93
94 *mapp = map;
95
96 return 0;
97}
Simon Glass3b2a29e2016-07-04 11:57:59 -060098#endif
Simon Glass6f98b752015-06-23 15:38:42 -060099
100void *regmap_get_range(struct regmap *map, unsigned int range_num)
101{
102 struct regmap_range *range;
103
104 if (range_num >= map->range_count)
105 return NULL;
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +0900106 range = &map->ranges[range_num];
Simon Glass6f98b752015-06-23 15:38:42 -0600107
108 return map_sysmem(range->start, range->size);
109}
110
111int regmap_uninit(struct regmap *map)
112{
Simon Glass6f98b752015-06-23 15:38:42 -0600113 free(map);
114
115 return 0;
116}
Paul Burton3bfb8cb2016-09-08 07:47:35 +0100117
118int regmap_read(struct regmap *map, uint offset, uint *valp)
119{
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +0900120 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
Paul Burton3bfb8cb2016-09-08 07:47:35 +0100121
122 *valp = le32_to_cpu(readl(ptr));
123
124 return 0;
125}
126
127int regmap_write(struct regmap *map, uint offset, uint val)
128{
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +0900129 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
Paul Burton3bfb8cb2016-09-08 07:47:35 +0100130
131 writel(cpu_to_le32(val), ptr);
132
133 return 0;
134}
Neil Armstrong285cbcf2018-04-27 11:56:14 +0200135
136int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
137{
138 uint reg;
139 int ret;
140
141 ret = regmap_read(map, offset, &reg);
142 if (ret)
143 return ret;
144
145 reg &= ~mask;
146
147 return regmap_write(map, offset, reg | val);
148}