1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *  linux/include/amba/bus.h
4  *
5  *  This device type deals with ARM PrimeCells and anything else that
6  *  presents a proper CID (0xB105F00D) at the end of the I/O register
7  *  region or that is derived from a PrimeCell.
8  *
9  *  Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
10  */
11 #ifndef ASMARM_AMBA_H
12 #define ASMARM_AMBA_H
13 
14 #include <linux/clk.h>
15 #include <linux/device.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/err.h>
18 #include <linux/resource.h>
19 #include <linux/regulator/consumer.h>
20 
21 #define AMBA_NR_IRQS	9
22 #define AMBA_CID	0xb105f00d
23 #define CORESIGHT_CID	0xb105900d
24 
25 /*
26  * CoreSight Architecture specification updates the ID specification
27  * for components on the AMBA bus. (ARM IHI 0029E)
28  *
29  * Bits 15:12 of the CID are the device class.
30  *
31  * Class 0xF remains for PrimeCell and legacy components. (AMBA_CID above)
32  * Class 0x9 defines the component as CoreSight (CORESIGHT_CID above)
33  * Class 0x0, 0x1, 0xB, 0xE define components that do not have driver support
34  * at present.
35  * Class 0x2-0x8,0xA and 0xD-0xD are presently reserved.
36  *
37  * Remaining CID bits stay as 0xb105-00d
38  */
39 
40 /**
41  * Class 0x9 components use additional values to form a Unique Component
42  * Identifier (UCI), where peripheral ID values are identical for different
43  * components. Passed to the amba bus code from the component driver via
44  * the amba_id->data pointer.
45  * @devarch	: coresight devarch register value
46  * @devarch_mask: mask bits used for matching. 0 indicates UCI not used.
47  * @devtype	: coresight device type value
48  * @data	: additional driver data. As we have usurped the original
49  *		pointer some devices may still need additional data
50  */
51 struct amba_cs_uci_id {
52 	unsigned int devarch;
53 	unsigned int devarch_mask;
54 	unsigned int devtype;
55 	void *data;
56 };
57 
58 /* define offsets for registers used by UCI */
59 #define UCI_REG_DEVTYPE_OFFSET	0xFCC
60 #define UCI_REG_DEVARCH_OFFSET	0xFBC
61 
62 struct clk;
63 
64 struct amba_device {
65 	struct device		dev;
66 	struct resource		res;
67 	struct clk		*pclk;
68 	struct device_dma_parameters dma_parms;
69 	unsigned int		periphid;
70 	unsigned int		cid;
71 	struct amba_cs_uci_id	uci;
72 	unsigned int		irq[AMBA_NR_IRQS];
73 	char			*driver_override;
74 };
75 
76 struct amba_driver {
77 	struct device_driver	drv;
78 	int			(*probe)(struct amba_device *, const struct amba_id *);
79 	void			(*remove)(struct amba_device *);
80 	void			(*shutdown)(struct amba_device *);
81 	const struct amba_id	*id_table;
82 };
83 
84 /*
85  * Constants for the designer field of the Peripheral ID register. When bit 7
86  * is set to '1', bits [6:0] should be the JEP106 manufacturer identity code.
87  */
88 enum amba_vendor {
89 	AMBA_VENDOR_ARM = 0x41,
90 	AMBA_VENDOR_ST = 0x80,
91 	AMBA_VENDOR_QCOM = 0x51,
92 	AMBA_VENDOR_LSI = 0xb6,
93 	AMBA_VENDOR_LINUX = 0xfe,	/* This value is not official */
94 };
95 
96 /* This is used to generate pseudo-ID for AMBA device */
97 #define AMBA_LINUX_ID(conf, rev, part) \
98 	(((conf) & 0xff) << 24 | ((rev) & 0xf) << 20 | \
99 	AMBA_VENDOR_LINUX << 12 | ((part) & 0xfff))
100 
101 extern struct bus_type amba_bustype;
102 
103 #define to_amba_device(d)	container_of(d, struct amba_device, dev)
104 
105 #define amba_get_drvdata(d)	dev_get_drvdata(&d->dev)
106 #define amba_set_drvdata(d,p)	dev_set_drvdata(&d->dev, p)
107 
108 #ifdef CONFIG_ARM_AMBA
109 int amba_driver_register(struct amba_driver *);
110 void amba_driver_unregister(struct amba_driver *);
111 #else
amba_driver_register(struct amba_driver * drv)112 static inline int amba_driver_register(struct amba_driver *drv)
113 {
114 	return -EINVAL;
115 }
amba_driver_unregister(struct amba_driver * drv)116 static inline void amba_driver_unregister(struct amba_driver *drv)
117 {
118 }
119 #endif
120 
121 struct amba_device *amba_device_alloc(const char *, resource_size_t, size_t);
122 void amba_device_put(struct amba_device *);
123 int amba_device_add(struct amba_device *, struct resource *);
124 int amba_device_register(struct amba_device *, struct resource *);
125 struct amba_device *amba_apb_device_add(struct device *parent, const char *name,
126 					resource_size_t base, size_t size,
127 					int irq1, int irq2, void *pdata,
128 					unsigned int periphid);
129 struct amba_device *amba_ahb_device_add(struct device *parent, const char *name,
130 					resource_size_t base, size_t size,
131 					int irq1, int irq2, void *pdata,
132 					unsigned int periphid);
133 struct amba_device *
134 amba_apb_device_add_res(struct device *parent, const char *name,
135 			resource_size_t base, size_t size, int irq1,
136 			int irq2, void *pdata, unsigned int periphid,
137 			struct resource *resbase);
138 struct amba_device *
139 amba_ahb_device_add_res(struct device *parent, const char *name,
140 			resource_size_t base, size_t size, int irq1,
141 			int irq2, void *pdata, unsigned int periphid,
142 			struct resource *resbase);
143 void amba_device_unregister(struct amba_device *);
144 struct amba_device *amba_find_device(const char *, struct device *, unsigned int, unsigned int);
145 int amba_request_regions(struct amba_device *, const char *);
146 void amba_release_regions(struct amba_device *);
147 
amba_pclk_enable(struct amba_device * dev)148 static inline int amba_pclk_enable(struct amba_device *dev)
149 {
150 	return clk_enable(dev->pclk);
151 }
152 
amba_pclk_disable(struct amba_device * dev)153 static inline void amba_pclk_disable(struct amba_device *dev)
154 {
155 	clk_disable(dev->pclk);
156 }
157 
amba_pclk_prepare(struct amba_device * dev)158 static inline int amba_pclk_prepare(struct amba_device *dev)
159 {
160 	return clk_prepare(dev->pclk);
161 }
162 
amba_pclk_unprepare(struct amba_device * dev)163 static inline void amba_pclk_unprepare(struct amba_device *dev)
164 {
165 	clk_unprepare(dev->pclk);
166 }
167 
168 /* Some drivers don't use the struct amba_device */
169 #define AMBA_CONFIG_BITS(a) (((a) >> 24) & 0xff)
170 #define AMBA_REV_BITS(a) (((a) >> 20) & 0x0f)
171 #define AMBA_MANF_BITS(a) (((a) >> 12) & 0xff)
172 #define AMBA_PART_BITS(a) ((a) & 0xfff)
173 
174 #define amba_config(d)	AMBA_CONFIG_BITS((d)->periphid)
175 #define amba_rev(d)	AMBA_REV_BITS((d)->periphid)
176 #define amba_manf(d)	AMBA_MANF_BITS((d)->periphid)
177 #define amba_part(d)	AMBA_PART_BITS((d)->periphid)
178 
179 #define __AMBA_DEV(busid, data, mask)				\
180 	{							\
181 		.coherent_dma_mask = mask,			\
182 		.init_name = busid,				\
183 		.platform_data = data,				\
184 	}
185 
186 /*
187  * APB devices do not themselves have the ability to address memory,
188  * so DMA masks should be zero (much like USB peripheral devices.)
189  * The DMA controller DMA masks should be used instead (much like
190  * USB host controllers in conventional PCs.)
191  */
192 #define AMBA_APB_DEVICE(name, busid, id, base, irqs, data)	\
193 struct amba_device name##_device = {				\
194 	.dev = __AMBA_DEV(busid, data, 0),			\
195 	.res = DEFINE_RES_MEM(base, SZ_4K),			\
196 	.irq = irqs,						\
197 	.periphid = id,						\
198 }
199 
200 /*
201  * AHB devices are DMA capable, so set their DMA masks
202  */
203 #define AMBA_AHB_DEVICE(name, busid, id, base, irqs, data)	\
204 struct amba_device name##_device = {				\
205 	.dev = __AMBA_DEV(busid, data, ~0ULL),			\
206 	.res = DEFINE_RES_MEM(base, SZ_4K),			\
207 	.irq = irqs,						\
208 	.periphid = id,						\
209 }
210 
211 /*
212  * module_amba_driver() - Helper macro for drivers that don't do anything
213  * special in module init/exit.  This eliminates a lot of boilerplate.  Each
214  * module may only use this macro once, and calling it replaces module_init()
215  * and module_exit()
216  */
217 #define module_amba_driver(__amba_drv) \
218 	module_driver(__amba_drv, amba_driver_register, amba_driver_unregister)
219 
220 /*
221  * builtin_amba_driver() - Helper macro for drivers that don't do anything
222  * special in driver initcall.  This eliminates a lot of boilerplate.  Each
223  * driver may only use this macro once, and calling it replaces the instance
224  * device_initcall().
225  */
226 #define builtin_amba_driver(__amba_drv) \
227 	builtin_driver(__amba_drv, amba_driver_register)
228 
229 #endif
230