]>
Commit | Line | Data |
---|---|---|
44127b77 FF |
1 | /* |
2 | * Copyright (C) 2014 Broadcom Corporation | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | */ | |
13 | ||
14 | #include <linux/init.h> | |
15 | #include <linux/types.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/platform_device.h> | |
18 | #include <linux/interrupt.h> | |
19 | #include <linux/sysfs.h> | |
20 | #include <linux/io.h> | |
21 | #include <linux/string.h> | |
22 | #include <linux/device.h> | |
23 | #include <linux/list.h> | |
24 | #include <linux/of.h> | |
25 | #include <linux/bitops.h> | |
26 | ||
27 | #include <asm/bug.h> | |
28 | #include <asm/signal.h> | |
29 | ||
30 | #define ARB_TIMER 0x008 | |
31 | #define ARB_ERR_CAP_CLR 0x7e4 | |
32 | #define ARB_ERR_CAP_CLEAR (1 << 0) | |
33 | #define ARB_ERR_CAP_HI_ADDR 0x7e8 | |
34 | #define ARB_ERR_CAP_ADDR 0x7ec | |
35 | #define ARB_ERR_CAP_DATA 0x7f0 | |
36 | #define ARB_ERR_CAP_STATUS 0x7f4 | |
37 | #define ARB_ERR_CAP_STATUS_TIMEOUT (1 << 12) | |
38 | #define ARB_ERR_CAP_STATUS_TEA (1 << 11) | |
39 | #define ARB_ERR_CAP_STATUS_BS_SHIFT (1 << 2) | |
40 | #define ARB_ERR_CAP_STATUS_BS_MASK 0x3c | |
41 | #define ARB_ERR_CAP_STATUS_WRITE (1 << 1) | |
42 | #define ARB_ERR_CAP_STATUS_VALID (1 << 0) | |
43 | #define ARB_ERR_CAP_MASTER 0x7f8 | |
44 | ||
45 | struct brcmstb_gisb_arb_device { | |
46 | void __iomem *base; | |
47 | struct mutex lock; | |
48 | struct list_head next; | |
49 | u32 valid_mask; | |
50 | const char *master_names[sizeof(u32) * BITS_PER_BYTE]; | |
51 | }; | |
52 | ||
53 | static LIST_HEAD(brcmstb_gisb_arb_device_list); | |
54 | ||
55 | static ssize_t gisb_arb_get_timeout(struct device *dev, | |
56 | struct device_attribute *attr, | |
57 | char *buf) | |
58 | { | |
59 | struct platform_device *pdev = to_platform_device(dev); | |
60 | struct brcmstb_gisb_arb_device *gdev = platform_get_drvdata(pdev); | |
61 | u32 timeout; | |
62 | ||
63 | mutex_lock(&gdev->lock); | |
64 | timeout = ioread32(gdev->base + ARB_TIMER); | |
65 | mutex_unlock(&gdev->lock); | |
66 | ||
67 | return sprintf(buf, "%d", timeout); | |
68 | } | |
69 | ||
70 | static ssize_t gisb_arb_set_timeout(struct device *dev, | |
71 | struct device_attribute *attr, | |
72 | const char *buf, size_t count) | |
73 | { | |
74 | struct platform_device *pdev = to_platform_device(dev); | |
75 | struct brcmstb_gisb_arb_device *gdev = platform_get_drvdata(pdev); | |
76 | int val, ret; | |
77 | ||
78 | ret = kstrtoint(buf, 10, &val); | |
79 | if (ret < 0) | |
80 | return ret; | |
81 | ||
82 | if (val == 0 || val >= 0xffffffff) | |
83 | return -EINVAL; | |
84 | ||
85 | mutex_lock(&gdev->lock); | |
86 | iowrite32(val, gdev->base + ARB_TIMER); | |
87 | mutex_unlock(&gdev->lock); | |
88 | ||
89 | return count; | |
90 | } | |
91 | ||
92 | static const char * | |
93 | brcmstb_gisb_master_to_str(struct brcmstb_gisb_arb_device *gdev, | |
94 | u32 masters) | |
95 | { | |
96 | u32 mask = gdev->valid_mask & masters; | |
97 | ||
98 | if (hweight_long(mask) != 1) | |
99 | return NULL; | |
100 | ||
101 | return gdev->master_names[ffs(mask) - 1]; | |
102 | } | |
103 | ||
104 | static int brcmstb_gisb_arb_decode_addr(struct brcmstb_gisb_arb_device *gdev, | |
105 | const char *reason) | |
106 | { | |
107 | u32 cap_status; | |
108 | unsigned long arb_addr; | |
109 | u32 master; | |
110 | const char *m_name; | |
111 | char m_fmt[11]; | |
112 | ||
113 | cap_status = ioread32(gdev->base + ARB_ERR_CAP_STATUS); | |
114 | ||
115 | /* Invalid captured address, bail out */ | |
116 | if (!(cap_status & ARB_ERR_CAP_STATUS_VALID)) | |
117 | return 1; | |
118 | ||
119 | /* Read the address and master */ | |
120 | arb_addr = ioread32(gdev->base + ARB_ERR_CAP_ADDR) & 0xffffffff; | |
121 | #if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT)) | |
122 | arb_addr |= (u64)ioread32(gdev->base + ARB_ERR_CAP_HI_ADDR) << 32; | |
123 | #endif | |
124 | master = ioread32(gdev->base + ARB_ERR_CAP_MASTER); | |
125 | ||
126 | m_name = brcmstb_gisb_master_to_str(gdev, master); | |
127 | if (!m_name) { | |
128 | snprintf(m_fmt, sizeof(m_fmt), "0x%08x", master); | |
129 | m_name = m_fmt; | |
130 | } | |
131 | ||
132 | pr_crit("%s: %s at 0x%lx [%c %s], core: %s\n", | |
133 | __func__, reason, arb_addr, | |
134 | cap_status & ARB_ERR_CAP_STATUS_WRITE ? 'W' : 'R', | |
135 | cap_status & ARB_ERR_CAP_STATUS_TIMEOUT ? "timeout" : "", | |
136 | m_name); | |
137 | ||
138 | /* clear the GISB error */ | |
139 | iowrite32(ARB_ERR_CAP_CLEAR, gdev->base + ARB_ERR_CAP_CLR); | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
144 | static int brcmstb_bus_error_handler(unsigned long addr, unsigned int fsr, | |
145 | struct pt_regs *regs) | |
146 | { | |
147 | int ret = 0; | |
148 | struct brcmstb_gisb_arb_device *gdev; | |
149 | ||
150 | /* iterate over each GISB arb registered handlers */ | |
151 | list_for_each_entry(gdev, &brcmstb_gisb_arb_device_list, next) | |
152 | ret |= brcmstb_gisb_arb_decode_addr(gdev, "bus error"); | |
153 | /* | |
154 | * If it was an imprecise abort, then we need to correct the | |
155 | * return address to be _after_ the instruction. | |
156 | */ | |
157 | if (fsr & (1 << 10)) | |
158 | regs->ARM_pc += 4; | |
159 | ||
160 | return ret; | |
161 | } | |
162 | ||
44127b77 FF |
163 | static irqreturn_t brcmstb_gisb_timeout_handler(int irq, void *dev_id) |
164 | { | |
165 | brcmstb_gisb_arb_decode_addr(dev_id, "timeout"); | |
166 | ||
167 | return IRQ_HANDLED; | |
168 | } | |
169 | ||
170 | static irqreturn_t brcmstb_gisb_tea_handler(int irq, void *dev_id) | |
171 | { | |
172 | brcmstb_gisb_arb_decode_addr(dev_id, "target abort"); | |
173 | ||
174 | return IRQ_HANDLED; | |
175 | } | |
176 | ||
177 | static DEVICE_ATTR(gisb_arb_timeout, S_IWUSR | S_IRUGO, | |
178 | gisb_arb_get_timeout, gisb_arb_set_timeout); | |
179 | ||
180 | static struct attribute *gisb_arb_sysfs_attrs[] = { | |
181 | &dev_attr_gisb_arb_timeout.attr, | |
182 | NULL, | |
183 | }; | |
184 | ||
185 | static struct attribute_group gisb_arb_sysfs_attr_group = { | |
186 | .attrs = gisb_arb_sysfs_attrs, | |
187 | }; | |
188 | ||
189 | static int brcmstb_gisb_arb_probe(struct platform_device *pdev) | |
190 | { | |
191 | struct device_node *dn = pdev->dev.of_node; | |
192 | struct brcmstb_gisb_arb_device *gdev; | |
193 | struct resource *r; | |
194 | int err, timeout_irq, tea_irq; | |
195 | unsigned int num_masters, j = 0; | |
196 | int i, first, last; | |
197 | ||
198 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
199 | timeout_irq = platform_get_irq(pdev, 0); | |
200 | tea_irq = platform_get_irq(pdev, 1); | |
201 | ||
202 | gdev = devm_kzalloc(&pdev->dev, sizeof(*gdev), GFP_KERNEL); | |
203 | if (!gdev) | |
204 | return -ENOMEM; | |
205 | ||
206 | mutex_init(&gdev->lock); | |
207 | INIT_LIST_HEAD(&gdev->next); | |
208 | ||
c9d53c0f JH |
209 | gdev->base = devm_ioremap_resource(&pdev->dev, r); |
210 | if (IS_ERR(gdev->base)) | |
211 | return PTR_ERR(gdev->base); | |
44127b77 FF |
212 | |
213 | err = devm_request_irq(&pdev->dev, timeout_irq, | |
214 | brcmstb_gisb_timeout_handler, 0, pdev->name, | |
215 | gdev); | |
216 | if (err < 0) | |
217 | return err; | |
218 | ||
219 | err = devm_request_irq(&pdev->dev, tea_irq, | |
220 | brcmstb_gisb_tea_handler, 0, pdev->name, | |
221 | gdev); | |
222 | if (err < 0) | |
223 | return err; | |
224 | ||
225 | /* If we do not have a valid mask, assume all masters are enabled */ | |
226 | if (of_property_read_u32(dn, "brcm,gisb-arb-master-mask", | |
227 | &gdev->valid_mask)) | |
228 | gdev->valid_mask = 0xffffffff; | |
229 | ||
230 | /* Proceed with reading the litteral names if we agree on the | |
231 | * number of masters | |
232 | */ | |
233 | num_masters = of_property_count_strings(dn, | |
234 | "brcm,gisb-arb-master-names"); | |
235 | if (hweight_long(gdev->valid_mask) == num_masters) { | |
236 | first = ffs(gdev->valid_mask) - 1; | |
237 | last = fls(gdev->valid_mask) - 1; | |
238 | ||
239 | for (i = first; i < last; i++) { | |
240 | if (!(gdev->valid_mask & BIT(i))) | |
241 | continue; | |
242 | ||
243 | of_property_read_string_index(dn, | |
244 | "brcm,gisb-arb-master-names", j, | |
245 | &gdev->master_names[i]); | |
246 | j++; | |
247 | } | |
248 | } | |
249 | ||
250 | err = sysfs_create_group(&pdev->dev.kobj, &gisb_arb_sysfs_attr_group); | |
251 | if (err) | |
252 | return err; | |
253 | ||
254 | platform_set_drvdata(pdev, gdev); | |
255 | ||
256 | list_add_tail(&gdev->next, &brcmstb_gisb_arb_device_list); | |
257 | ||
f1bee783 FF |
258 | hook_fault_code(22, brcmstb_bus_error_handler, SIGBUS, 0, |
259 | "imprecise external abort"); | |
260 | ||
44127b77 FF |
261 | dev_info(&pdev->dev, "registered mem: %p, irqs: %d, %d\n", |
262 | gdev->base, timeout_irq, tea_irq); | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
267 | static const struct of_device_id brcmstb_gisb_arb_of_match[] = { | |
268 | { .compatible = "brcm,gisb-arb" }, | |
269 | { }, | |
270 | }; | |
271 | ||
272 | static struct platform_driver brcmstb_gisb_arb_driver = { | |
273 | .probe = brcmstb_gisb_arb_probe, | |
274 | .driver = { | |
275 | .name = "brcm-gisb-arb", | |
276 | .owner = THIS_MODULE, | |
277 | .of_match_table = brcmstb_gisb_arb_of_match, | |
278 | }, | |
279 | }; | |
280 | ||
281 | static int __init brcm_gisb_driver_init(void) | |
282 | { | |
283 | return platform_driver_register(&brcmstb_gisb_arb_driver); | |
284 | } | |
285 | ||
286 | module_init(brcm_gisb_driver_init); |