2 * Copyright (C) 2014-2017 Broadcom
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.
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.
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>
21 #include <linux/string.h>
22 #include <linux/device.h>
23 #include <linux/list.h>
25 #include <linux/bitops.h>
30 #include <asm/signal.h>
34 #include <asm/traps.h>
37 #define ARB_ERR_CAP_CLEAR (1 << 0)
38 #define ARB_ERR_CAP_STATUS_TIMEOUT (1 << 12)
39 #define ARB_ERR_CAP_STATUS_TEA (1 << 11)
40 #define ARB_ERR_CAP_STATUS_WRITE (1 << 1)
41 #define ARB_ERR_CAP_STATUS_VALID (1 << 0)
52 static const int gisb_offsets_bcm7038
[] = {
54 [ARB_ERR_CAP_CLR
] = 0x0c4,
55 [ARB_ERR_CAP_HI_ADDR
] = -1,
56 [ARB_ERR_CAP_ADDR
] = 0x0c8,
57 [ARB_ERR_CAP_STATUS
] = 0x0d0,
58 [ARB_ERR_CAP_MASTER
] = -1,
61 static const int gisb_offsets_bcm7400
[] = {
63 [ARB_ERR_CAP_CLR
] = 0x0c8,
64 [ARB_ERR_CAP_HI_ADDR
] = -1,
65 [ARB_ERR_CAP_ADDR
] = 0x0cc,
66 [ARB_ERR_CAP_STATUS
] = 0x0d4,
67 [ARB_ERR_CAP_MASTER
] = 0x0d8,
70 static const int gisb_offsets_bcm7435
[] = {
72 [ARB_ERR_CAP_CLR
] = 0x168,
73 [ARB_ERR_CAP_HI_ADDR
] = -1,
74 [ARB_ERR_CAP_ADDR
] = 0x16c,
75 [ARB_ERR_CAP_STATUS
] = 0x174,
76 [ARB_ERR_CAP_MASTER
] = 0x178,
79 static const int gisb_offsets_bcm7445
[] = {
81 [ARB_ERR_CAP_CLR
] = 0x7e4,
82 [ARB_ERR_CAP_HI_ADDR
] = 0x7e8,
83 [ARB_ERR_CAP_ADDR
] = 0x7ec,
84 [ARB_ERR_CAP_STATUS
] = 0x7f4,
85 [ARB_ERR_CAP_MASTER
] = 0x7f8,
88 struct brcmstb_gisb_arb_device
{
90 const int *gisb_offsets
;
93 struct list_head next
;
95 const char *master_names
[sizeof(u32
) * BITS_PER_BYTE
];
99 static LIST_HEAD(brcmstb_gisb_arb_device_list
);
101 static u32
gisb_read(struct brcmstb_gisb_arb_device
*gdev
, int reg
)
103 int offset
= gdev
->gisb_offsets
[reg
];
106 /* return 1 if the hardware doesn't have ARB_ERR_CAP_MASTER */
107 if (reg
== ARB_ERR_CAP_MASTER
)
113 if (gdev
->big_endian
)
114 return ioread32be(gdev
->base
+ offset
);
116 return ioread32(gdev
->base
+ offset
);
119 static u64
gisb_read_address(struct brcmstb_gisb_arb_device
*gdev
)
123 value
= gisb_read(gdev
, ARB_ERR_CAP_ADDR
);
124 value
|= (u64
)gisb_read(gdev
, ARB_ERR_CAP_HI_ADDR
) << 32;
129 static void gisb_write(struct brcmstb_gisb_arb_device
*gdev
, u32 val
, int reg
)
131 int offset
= gdev
->gisb_offsets
[reg
];
136 if (gdev
->big_endian
)
137 iowrite32be(val
, gdev
->base
+ offset
);
139 iowrite32(val
, gdev
->base
+ offset
);
142 static ssize_t
gisb_arb_get_timeout(struct device
*dev
,
143 struct device_attribute
*attr
,
146 struct platform_device
*pdev
= to_platform_device(dev
);
147 struct brcmstb_gisb_arb_device
*gdev
= platform_get_drvdata(pdev
);
150 mutex_lock(&gdev
->lock
);
151 timeout
= gisb_read(gdev
, ARB_TIMER
);
152 mutex_unlock(&gdev
->lock
);
154 return sprintf(buf
, "%d", timeout
);
157 static ssize_t
gisb_arb_set_timeout(struct device
*dev
,
158 struct device_attribute
*attr
,
159 const char *buf
, size_t count
)
161 struct platform_device
*pdev
= to_platform_device(dev
);
162 struct brcmstb_gisb_arb_device
*gdev
= platform_get_drvdata(pdev
);
165 ret
= kstrtoint(buf
, 10, &val
);
169 if (val
== 0 || val
>= 0xffffffff)
172 mutex_lock(&gdev
->lock
);
173 gisb_write(gdev
, val
, ARB_TIMER
);
174 mutex_unlock(&gdev
->lock
);
180 brcmstb_gisb_master_to_str(struct brcmstb_gisb_arb_device
*gdev
,
183 u32 mask
= gdev
->valid_mask
& masters
;
185 if (hweight_long(mask
) != 1)
188 return gdev
->master_names
[ffs(mask
) - 1];
191 static int brcmstb_gisb_arb_decode_addr(struct brcmstb_gisb_arb_device
*gdev
,
200 cap_status
= gisb_read(gdev
, ARB_ERR_CAP_STATUS
);
202 /* Invalid captured address, bail out */
203 if (!(cap_status
& ARB_ERR_CAP_STATUS_VALID
))
206 /* Read the address and master */
207 arb_addr
= gisb_read_address(gdev
);
208 master
= gisb_read(gdev
, ARB_ERR_CAP_MASTER
);
210 m_name
= brcmstb_gisb_master_to_str(gdev
, master
);
212 snprintf(m_fmt
, sizeof(m_fmt
), "0x%08x", master
);
216 pr_crit("%s: %s at 0x%llx [%c %s], core: %s\n",
217 __func__
, reason
, arb_addr
,
218 cap_status
& ARB_ERR_CAP_STATUS_WRITE
? 'W' : 'R',
219 cap_status
& ARB_ERR_CAP_STATUS_TIMEOUT
? "timeout" : "",
222 /* clear the GISB error */
223 gisb_write(gdev
, ARB_ERR_CAP_CLEAR
, ARB_ERR_CAP_CLR
);
229 static int brcmstb_bus_error_handler(unsigned long addr
, unsigned int fsr
,
230 struct pt_regs
*regs
)
233 struct brcmstb_gisb_arb_device
*gdev
;
235 /* iterate over each GISB arb registered handlers */
236 list_for_each_entry(gdev
, &brcmstb_gisb_arb_device_list
, next
)
237 ret
|= brcmstb_gisb_arb_decode_addr(gdev
, "bus error");
239 * If it was an imprecise abort, then we need to correct the
240 * return address to be _after_ the instruction.
250 static int brcmstb_bus_error_handler(struct pt_regs
*regs
, int is_fixup
)
253 struct brcmstb_gisb_arb_device
*gdev
;
256 list_for_each_entry(gdev
, &brcmstb_gisb_arb_device_list
, next
) {
257 cap_status
= gisb_read(gdev
, ARB_ERR_CAP_STATUS
);
259 /* Invalid captured address, bail out */
260 if (!(cap_status
& ARB_ERR_CAP_STATUS_VALID
)) {
265 ret
|= brcmstb_gisb_arb_decode_addr(gdev
, "bus error");
268 return is_fixup
? MIPS_BE_FIXUP
: MIPS_BE_FATAL
;
272 static irqreturn_t
brcmstb_gisb_timeout_handler(int irq
, void *dev_id
)
274 brcmstb_gisb_arb_decode_addr(dev_id
, "timeout");
279 static irqreturn_t
brcmstb_gisb_tea_handler(int irq
, void *dev_id
)
281 brcmstb_gisb_arb_decode_addr(dev_id
, "target abort");
286 static DEVICE_ATTR(gisb_arb_timeout
, S_IWUSR
| S_IRUGO
,
287 gisb_arb_get_timeout
, gisb_arb_set_timeout
);
289 static struct attribute
*gisb_arb_sysfs_attrs
[] = {
290 &dev_attr_gisb_arb_timeout
.attr
,
294 static struct attribute_group gisb_arb_sysfs_attr_group
= {
295 .attrs
= gisb_arb_sysfs_attrs
,
298 static const struct of_device_id brcmstb_gisb_arb_of_match
[] = {
299 { .compatible
= "brcm,gisb-arb", .data
= gisb_offsets_bcm7445
},
300 { .compatible
= "brcm,bcm7445-gisb-arb", .data
= gisb_offsets_bcm7445
},
301 { .compatible
= "brcm,bcm7435-gisb-arb", .data
= gisb_offsets_bcm7435
},
302 { .compatible
= "brcm,bcm7400-gisb-arb", .data
= gisb_offsets_bcm7400
},
303 { .compatible
= "brcm,bcm7038-gisb-arb", .data
= gisb_offsets_bcm7038
},
307 static int __init
brcmstb_gisb_arb_probe(struct platform_device
*pdev
)
309 struct device_node
*dn
= pdev
->dev
.of_node
;
310 struct brcmstb_gisb_arb_device
*gdev
;
311 const struct of_device_id
*of_id
;
313 int err
, timeout_irq
, tea_irq
;
314 unsigned int num_masters
, j
= 0;
317 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
318 timeout_irq
= platform_get_irq(pdev
, 0);
319 tea_irq
= platform_get_irq(pdev
, 1);
321 gdev
= devm_kzalloc(&pdev
->dev
, sizeof(*gdev
), GFP_KERNEL
);
325 mutex_init(&gdev
->lock
);
326 INIT_LIST_HEAD(&gdev
->next
);
328 gdev
->base
= devm_ioremap_resource(&pdev
->dev
, r
);
329 if (IS_ERR(gdev
->base
))
330 return PTR_ERR(gdev
->base
);
332 of_id
= of_match_node(brcmstb_gisb_arb_of_match
, dn
);
334 pr_err("failed to look up compatible string\n");
337 gdev
->gisb_offsets
= of_id
->data
;
338 gdev
->big_endian
= of_device_is_big_endian(dn
);
340 err
= devm_request_irq(&pdev
->dev
, timeout_irq
,
341 brcmstb_gisb_timeout_handler
, 0, pdev
->name
,
346 err
= devm_request_irq(&pdev
->dev
, tea_irq
,
347 brcmstb_gisb_tea_handler
, 0, pdev
->name
,
352 /* If we do not have a valid mask, assume all masters are enabled */
353 if (of_property_read_u32(dn
, "brcm,gisb-arb-master-mask",
355 gdev
->valid_mask
= 0xffffffff;
357 /* Proceed with reading the litteral names if we agree on the
360 num_masters
= of_property_count_strings(dn
,
361 "brcm,gisb-arb-master-names");
362 if (hweight_long(gdev
->valid_mask
) == num_masters
) {
363 first
= ffs(gdev
->valid_mask
) - 1;
364 last
= fls(gdev
->valid_mask
) - 1;
366 for (i
= first
; i
< last
; i
++) {
367 if (!(gdev
->valid_mask
& BIT(i
)))
370 of_property_read_string_index(dn
,
371 "brcm,gisb-arb-master-names", j
,
372 &gdev
->master_names
[i
]);
377 err
= sysfs_create_group(&pdev
->dev
.kobj
, &gisb_arb_sysfs_attr_group
);
381 platform_set_drvdata(pdev
, gdev
);
383 list_add_tail(&gdev
->next
, &brcmstb_gisb_arb_device_list
);
386 hook_fault_code(22, brcmstb_bus_error_handler
, SIGBUS
, 0,
387 "imprecise external abort");
390 board_be_handler
= brcmstb_bus_error_handler
;
393 dev_info(&pdev
->dev
, "registered mem: %p, irqs: %d, %d\n",
394 gdev
->base
, timeout_irq
, tea_irq
);
399 #ifdef CONFIG_PM_SLEEP
400 static int brcmstb_gisb_arb_suspend(struct device
*dev
)
402 struct platform_device
*pdev
= to_platform_device(dev
);
403 struct brcmstb_gisb_arb_device
*gdev
= platform_get_drvdata(pdev
);
405 gdev
->saved_timeout
= gisb_read(gdev
, ARB_TIMER
);
410 /* Make sure we provide the same timeout value that was configured before, and
411 * do this before the GISB timeout interrupt handler has any chance to run.
413 static int brcmstb_gisb_arb_resume_noirq(struct device
*dev
)
415 struct platform_device
*pdev
= to_platform_device(dev
);
416 struct brcmstb_gisb_arb_device
*gdev
= platform_get_drvdata(pdev
);
418 gisb_write(gdev
, gdev
->saved_timeout
, ARB_TIMER
);
423 #define brcmstb_gisb_arb_suspend NULL
424 #define brcmstb_gisb_arb_resume_noirq NULL
427 static const struct dev_pm_ops brcmstb_gisb_arb_pm_ops
= {
428 .suspend
= brcmstb_gisb_arb_suspend
,
429 .resume_noirq
= brcmstb_gisb_arb_resume_noirq
,
432 static struct platform_driver brcmstb_gisb_arb_driver
= {
434 .name
= "brcm-gisb-arb",
435 .of_match_table
= brcmstb_gisb_arb_of_match
,
436 .pm
= &brcmstb_gisb_arb_pm_ops
,
440 static int __init
brcm_gisb_driver_init(void)
442 return platform_driver_probe(&brcmstb_gisb_arb_driver
,
443 brcmstb_gisb_arb_probe
);
446 module_init(brcm_gisb_driver_init
);