blob: fabcc5f53a838fb7bf1bc2a86f7cadfb0763e6e9 [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
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090020static struct regmap *regmap_alloc(int count)
Simon Glass30d73e82016-07-04 11:58:21 -060021{
22 struct regmap *map;
23
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090024 map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
Simon Glass30d73e82016-07-04 11:58:21 -060025 if (!map)
26 return NULL;
Simon Glass30d73e82016-07-04 11:58:21 -060027 map->range_count = count;
28
29 return map;
30}
31
Simon Glassb9443452016-07-04 11:57:59 -060032#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass1b1fe412017-08-29 14:15:50 -060033int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
Simon Glassb9443452016-07-04 11:57:59 -060034 struct regmap **mapp)
35{
Simon Glassb6114332016-07-04 11:58:22 -060036 struct regmap_range *range;
37 struct regmap *map;
38
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090039 map = regmap_alloc(count);
Simon Glassb6114332016-07-04 11:58:22 -060040 if (!map)
41 return -ENOMEM;
42
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090043 for (range = map->ranges; count > 0; reg += 2, range++, count--) {
Simon Glassb6114332016-07-04 11:58:22 -060044 range->start = *reg;
45 range->size = reg[1];
46 }
47
48 *mapp = map;
49
Simon Glassb9443452016-07-04 11:57:59 -060050 return 0;
51}
52#else
Masahiro Yamadae4873e32018-04-19 12:14:03 +090053int regmap_init_mem(ofnode node, struct regmap **mapp)
Simon Glassad8f8ab2015-06-23 15:38:42 -060054{
Simon Glassad8f8ab2015-06-23 15:38:42 -060055 struct regmap_range *range;
Simon Glassad8f8ab2015-06-23 15:38:42 -060056 struct regmap *map;
57 int count;
58 int addr_len, size_len, both_len;
Simon Glassad8f8ab2015-06-23 15:38:42 -060059 int len;
Jean-Jacques Hiblot024611b2017-02-13 16:17:48 +010060 int index;
Simon Glasseeeb5192017-05-18 20:09:10 -060061 struct resource r;
Simon Glassad8f8ab2015-06-23 15:38:42 -060062
Masahiro Yamadae4873e32018-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 Glassad8f8ab2015-06-23 15:38:42 -060065 both_len = addr_len + size_len;
66
Masahiro Yamadae4873e32018-04-19 12:14:03 +090067 len = ofnode_read_size(node, "reg");
Simon Glasseeeb5192017-05-18 20:09:10 -060068 if (len < 0)
69 return len;
70 len /= sizeof(fdt32_t);
Simon Glassad8f8ab2015-06-23 15:38:42 -060071 count = len / both_len;
Simon Glasseeeb5192017-05-18 20:09:10 -060072 if (!count)
Simon Glassad8f8ab2015-06-23 15:38:42 -060073 return -EINVAL;
74
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090075 map = regmap_alloc(count);
Simon Glassad8f8ab2015-06-23 15:38:42 -060076 if (!map)
77 return -ENOMEM;
78
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090079 for (range = map->ranges, index = 0; count > 0;
Simon Glasseeeb5192017-05-18 20:09:10 -060080 count--, range++, index++) {
Jean-Jacques Hiblot024611b2017-02-13 16:17:48 +010081 fdt_size_t sz;
Simon Glasseeeb5192017-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 Yamadae4873e32018-04-19 12:14:03 +090088 ofnode_to_offset(node), "reg", index,
Simon Glasseeeb5192017-05-18 20:09:10 -060089 addr_len, size_len, &sz, true);
90 range->size = sz;
91 }
Simon Glassad8f8ab2015-06-23 15:38:42 -060092 }
93
94 *mapp = map;
95
96 return 0;
97}
Simon Glassb9443452016-07-04 11:57:59 -060098#endif
Simon Glassad8f8ab2015-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 Yamada54c5ecb2018-04-19 12:14:01 +0900106 range = &map->ranges[range_num];
Simon Glassad8f8ab2015-06-23 15:38:42 -0600107
108 return map_sysmem(range->start, range->size);
109}
110
111int regmap_uninit(struct regmap *map)
112{
Simon Glassad8f8ab2015-06-23 15:38:42 -0600113 free(map);
114
115 return 0;
116}
Paul Burton39776032016-09-08 07:47:35 +0100117
118int regmap_read(struct regmap *map, uint offset, uint *valp)
119{
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900120 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
Paul Burton39776032016-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 Yamada54c5ecb2018-04-19 12:14:01 +0900129 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
Paul Burton39776032016-09-08 07:47:35 +0100130
131 writel(cpu_to_le32(val), ptr);
132
133 return 0;
134}