summaryrefslogtreecommitdiffstats
path: root/drivers/amba/bus.c
blob: a8cd16835dbdbb6ebc5a68a2c70b989a6d89914e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
 * Copyright (c) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
 *
 * Under GPLv2.
 */
#include <common.h>
#include <driver.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/amba/bus.h>
#include <io.h>
#include <init.h>

#define to_amba_driver(d)	container_of(d, struct amba_driver, drv)

static const struct amba_id *
amba_lookup(const struct amba_id *table, struct amba_device *dev)
{
	int ret = 0;

	while (table->mask) {
		ret = (dev->periphid & table->mask) == table->id;
		if (ret)
			break;
		table++;
	}

	return ret ? table : NULL;
}

static int amba_match(struct device_d *dev, struct driver_d *drv)
{
	struct amba_device *pcdev = to_amba_device(dev);

	struct amba_driver *pcdrv = to_amba_driver(drv);

	return amba_lookup(pcdrv->id_table, pcdev) == NULL;
}

static int amba_get_enable_pclk(struct amba_device *pcdev)
{
	struct clk *pclk = clk_get(&pcdev->dev, "apb_pclk");
	int ret;

	pcdev->pclk = pclk;

	if (IS_ERR(pclk))
		return PTR_ERR(pclk);

	ret = clk_enable(pclk);
	if (ret) {
		clk_put(pclk);
	}

	return ret;
}

static int amba_probe(struct device_d *dev)
{
	struct amba_device *pcdev = to_amba_device(dev);
	struct amba_driver *pcdrv = to_amba_driver(dev->driver);
	const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);

	return pcdrv->probe(pcdev, id);
}

static void amba_remove(struct device_d *dev)
{
	struct amba_device *pcdev = to_amba_device(dev);
	struct amba_driver *drv = to_amba_driver(dev->driver);

	if (drv->remove)
		drv->remove(pcdev);
}

struct bus_type amba_bustype = {
	.name = "amba",
	.match = amba_match,
	.probe = amba_probe,
	.remove = amba_remove,
};

int amba_driver_register(struct amba_driver *drv)
{
	drv->drv.bus = &amba_bustype;

	if (drv->probe)
		drv->drv.probe = amba_probe;
	if (drv->remove)
		drv->drv.remove = amba_remove;

	return register_driver(&drv->drv);
}

/**
 *	amba_device_add - add a previously allocated AMBA device structure
 *	@dev: AMBA device allocated by amba_device_alloc
 *	@parent: resource parent for this devices resources
 *
 *	Claim the resource, and read the device cell ID if not already
 *	initialized.  Register the AMBA device with the Linux device
 *	manager.
 */
int amba_device_add(struct amba_device *dev)
{
	u32 size;
	void __iomem *tmp;
	int ret;
	struct resource *res = NULL;

	dev->dev.bus = &amba_bustype;

	/*
	 * Dynamically calculate the size of the resource
	 * and use this for iomap
	 */
	size = resource_size(&dev->res);
	res = request_iomem_region("amba", dev->res.start, dev->res.end);
	if (IS_ERR(res))
		return PTR_ERR(res);
	dev->base = tmp = (void __force __iomem *)res->start;
	if (!tmp) {
		ret = -ENOMEM;
		goto err_release;
	}

	/* Hard-coded primecell ID instead of plug-n-play */
	if (dev->periphid != 0)
		goto skip_probe;

	ret = amba_get_enable_pclk(dev);
	if (ret == 0) {
		u32 pid, cid;

		/*
		 * Read pid and cid based on size of resource
		 * they are located at end of region
		 */
		pid = amba_device_get_pid(tmp, size);
		cid = amba_device_get_cid(tmp, size);

		if (cid == AMBA_CID)
			dev->periphid = pid;

		if (!dev->periphid)
			ret = -ENODEV;
	}

	if (ret)
		goto err_release;

 skip_probe:
	ret = register_device(&dev->dev);
	if (ret)
		goto err_release;

	dev_add_param_uint32_fixed(&dev->dev, "periphid", dev->periphid, "0x%08x");

	return ret;
 err_release:
	release_region(res);
	return ret;
}

struct amba_device *
amba_aphb_device_add(struct device_d *parent, const char *name, int id,
		     resource_size_t base, size_t size,
		     void *pdata, unsigned int periphid)
{
	struct amba_device *dev;
	int ret;

	dev = amba_device_alloc(name, id, base, size);

	dev->periphid = periphid;
	dev->dev.platform_data = pdata;
	dev->dev.parent = parent;

	ret = amba_device_add(dev);
	if (ret)
		return ERR_PTR(ret);

	return dev;
}

/**
 *	amba_device_alloc - allocate an AMBA device
 *	@name: sysfs name of the AMBA device
 *	@base: base of AMBA device
 *	@size: size of AMBA device
 *
 *	Allocate and initialize an AMBA device structure.  Returns %NULL
 *	on failure.
 */
struct amba_device *amba_device_alloc(const char *name, int id, resource_size_t base,
	size_t size)
{
	struct amba_device *dev;

	dev = xzalloc(sizeof(*dev));

	dev_set_name(&dev->dev, name);
	dev->dev.id = id;
	dev->res.start = base;
	dev->res.end = base + size - 1;
	dev->res.flags = IORESOURCE_MEM;
	dev->dev.resource = &dev->res;

	return dev;
}


static int amba_bus_init(void)
{
	return bus_register(&amba_bustype);
}
pure_initcall(amba_bus_init);