2 * Copyright (C) 2014 Broadcom Corporation
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>
28 #include <asm/signal.h>
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
45 struct brcmstb_gisb_arb_device
{
48 struct list_head next
;
50 const char *master_names
[sizeof(u32
) * BITS_PER_BYTE
];
53 static LIST_HEAD(brcmstb_gisb_arb_device_list
);
55 static ssize_t
gisb_arb_get_timeout(struct device
*dev
,
56 struct device_attribute
*attr
,
59 struct platform_device
*pdev
= to_platform_device(dev
);
60 struct brcmstb_gisb_arb_device
*gdev
= platform_get_drvdata(pdev
);
63 mutex_lock(&gdev
->lock
);
64 timeout
= ioread32(gdev
->base
+ ARB_TIMER
);
65 mutex_unlock(&gdev
->lock
);
67 return sprintf(buf
, "%d", timeout
);
70 static ssize_t
gisb_arb_set_timeout(struct device
*dev
,
71 struct device_attribute
*attr
,
72 const char *buf
, size_t count
)
74 struct platform_device
*pdev
= to_platform_device(dev
);
75 struct brcmstb_gisb_arb_device
*gdev
= platform_get_drvdata(pdev
);
78 ret
= kstrtoint(buf
, 10, &val
);
82 if (val
== 0 || val
>= 0xffffffff)
85 mutex_lock(&gdev
->lock
);
86 iowrite32(val
, gdev
->base
+ ARB_TIMER
);
87 mutex_unlock(&gdev
->lock
);
93 brcmstb_gisb_master_to_str(struct brcmstb_gisb_arb_device
*gdev
,
96 u32 mask
= gdev
->valid_mask
& masters
;
98 if (hweight_long(mask
) != 1)
101 return gdev
->master_names
[ffs(mask
) - 1];
104 static int brcmstb_gisb_arb_decode_addr(struct brcmstb_gisb_arb_device
*gdev
,
108 unsigned long arb_addr
;
113 cap_status
= ioread32(gdev
->base
+ ARB_ERR_CAP_STATUS
);
115 /* Invalid captured address, bail out */
116 if (!(cap_status
& ARB_ERR_CAP_STATUS_VALID
))
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;
124 master
= ioread32(gdev
->base
+ ARB_ERR_CAP_MASTER
);
126 m_name
= brcmstb_gisb_master_to_str(gdev
, master
);
128 snprintf(m_fmt
, sizeof(m_fmt
), "0x%08x", master
);
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" : "",
138 /* clear the GISB error */
139 iowrite32(ARB_ERR_CAP_CLEAR
, gdev
->base
+ ARB_ERR_CAP_CLR
);
144 static int brcmstb_bus_error_handler(unsigned long addr
, unsigned int fsr
,
145 struct pt_regs
*regs
)
148 struct brcmstb_gisb_arb_device
*gdev
;
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");
154 * If it was an imprecise abort, then we need to correct the
155 * return address to be _after_ the instruction.
163 void __init
brcmstb_hook_fault_code(void)
165 hook_fault_code(22, brcmstb_bus_error_handler
, SIGBUS
, 0,
166 "imprecise external abort");
169 static irqreturn_t
brcmstb_gisb_timeout_handler(int irq
, void *dev_id
)
171 brcmstb_gisb_arb_decode_addr(dev_id
, "timeout");
176 static irqreturn_t
brcmstb_gisb_tea_handler(int irq
, void *dev_id
)
178 brcmstb_gisb_arb_decode_addr(dev_id
, "target abort");
183 static DEVICE_ATTR(gisb_arb_timeout
, S_IWUSR
| S_IRUGO
,
184 gisb_arb_get_timeout
, gisb_arb_set_timeout
);
186 static struct attribute
*gisb_arb_sysfs_attrs
[] = {
187 &dev_attr_gisb_arb_timeout
.attr
,
191 static struct attribute_group gisb_arb_sysfs_attr_group
= {
192 .attrs
= gisb_arb_sysfs_attrs
,
195 static int brcmstb_gisb_arb_probe(struct platform_device
*pdev
)
197 struct device_node
*dn
= pdev
->dev
.of_node
;
198 struct brcmstb_gisb_arb_device
*gdev
;
200 int err
, timeout_irq
, tea_irq
;
201 unsigned int num_masters
, j
= 0;
204 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
205 timeout_irq
= platform_get_irq(pdev
, 0);
206 tea_irq
= platform_get_irq(pdev
, 1);
208 gdev
= devm_kzalloc(&pdev
->dev
, sizeof(*gdev
), GFP_KERNEL
);
212 mutex_init(&gdev
->lock
);
213 INIT_LIST_HEAD(&gdev
->next
);
215 gdev
->base
= devm_ioremap_resource(&pdev
->dev
, r
);
216 if (IS_ERR(gdev
->base
))
217 return PTR_ERR(gdev
->base
);
219 err
= devm_request_irq(&pdev
->dev
, timeout_irq
,
220 brcmstb_gisb_timeout_handler
, 0, pdev
->name
,
225 err
= devm_request_irq(&pdev
->dev
, tea_irq
,
226 brcmstb_gisb_tea_handler
, 0, pdev
->name
,
231 /* If we do not have a valid mask, assume all masters are enabled */
232 if (of_property_read_u32(dn
, "brcm,gisb-arb-master-mask",
234 gdev
->valid_mask
= 0xffffffff;
236 /* Proceed with reading the litteral names if we agree on the
239 num_masters
= of_property_count_strings(dn
,
240 "brcm,gisb-arb-master-names");
241 if (hweight_long(gdev
->valid_mask
) == num_masters
) {
242 first
= ffs(gdev
->valid_mask
) - 1;
243 last
= fls(gdev
->valid_mask
) - 1;
245 for (i
= first
; i
< last
; i
++) {
246 if (!(gdev
->valid_mask
& BIT(i
)))
249 of_property_read_string_index(dn
,
250 "brcm,gisb-arb-master-names", j
,
251 &gdev
->master_names
[i
]);
256 err
= sysfs_create_group(&pdev
->dev
.kobj
, &gisb_arb_sysfs_attr_group
);
260 platform_set_drvdata(pdev
, gdev
);
262 list_add_tail(&gdev
->next
, &brcmstb_gisb_arb_device_list
);
264 dev_info(&pdev
->dev
, "registered mem: %p, irqs: %d, %d\n",
265 gdev
->base
, timeout_irq
, tea_irq
);
270 static const struct of_device_id brcmstb_gisb_arb_of_match
[] = {
271 { .compatible
= "brcm,gisb-arb" },
275 static struct platform_driver brcmstb_gisb_arb_driver
= {
276 .probe
= brcmstb_gisb_arb_probe
,
278 .name
= "brcm-gisb-arb",
279 .owner
= THIS_MODULE
,
280 .of_match_table
= brcmstb_gisb_arb_of_match
,
284 static int __init
brcm_gisb_driver_init(void)
286 return platform_driver_register(&brcmstb_gisb_arb_driver
);
289 module_init(brcm_gisb_driver_init
);