xref: /openbmc/linux/drivers/soc/tegra/cbb/tegra-cbb.c (revision 404e077a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved
4  */
5 
6 #include <linux/clk.h>
7 #include <linux/cpufeature.h>
8 #include <linux/debugfs.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <linux/device.h>
14 #include <linux/io.h>
15 #include <linux/of_irq.h>
16 #include <linux/of_address.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <soc/tegra/fuse.h>
20 #include <soc/tegra/tegra-cbb.h>
21 
22 void tegra_cbb_print_err(struct seq_file *file, const char *fmt, ...)
23 {
24 	struct va_format vaf;
25 	va_list args;
26 
27 	va_start(args, fmt);
28 
29 	if (file) {
30 		seq_vprintf(file, fmt, args);
31 	} else {
32 		vaf.fmt = fmt;
33 		vaf.va = &args;
34 		pr_crit("%pV", &vaf);
35 	}
36 
37 	va_end(args);
38 }
39 
40 void tegra_cbb_print_cache(struct seq_file *file, u32 cache)
41 {
42 	const char *buff_str, *mod_str, *rd_str, *wr_str;
43 
44 	buff_str = (cache & BIT(0)) ? "Bufferable " : "";
45 	mod_str = (cache & BIT(1)) ? "Modifiable " : "";
46 	rd_str = (cache & BIT(2)) ? "Read-Allocate " : "";
47 	wr_str = (cache & BIT(3)) ? "Write-Allocate" : "";
48 
49 	if (cache == 0x0)
50 		buff_str = "Device Non-Bufferable";
51 
52 	tegra_cbb_print_err(file, "\t  Cache\t\t\t: 0x%x -- %s%s%s%s\n",
53 			    cache, buff_str, mod_str, rd_str, wr_str);
54 }
55 
56 void tegra_cbb_print_prot(struct seq_file *file, u32 prot)
57 {
58 	const char *data_str, *secure_str, *priv_str;
59 
60 	data_str = (prot & 0x4) ? "Instruction" : "Data";
61 	secure_str = (prot & 0x2) ? "Non-Secure" : "Secure";
62 	priv_str = (prot & 0x1) ? "Privileged" : "Unprivileged";
63 
64 	tegra_cbb_print_err(file, "\t  Protection\t\t: 0x%x -- %s, %s, %s Access\n",
65 			    prot, priv_str, secure_str, data_str);
66 }
67 
68 static int tegra_cbb_err_show(struct seq_file *file, void *data)
69 {
70 	struct tegra_cbb *cbb = file->private;
71 
72 	return cbb->ops->debugfs_show(cbb, file, data);
73 }
74 DEFINE_SHOW_ATTRIBUTE(tegra_cbb_err);
75 
76 static int tegra_cbb_err_debugfs_init(struct tegra_cbb *cbb)
77 {
78 	static struct dentry *root;
79 
80 	if (!root) {
81 		root = debugfs_create_file("tegra_cbb_err", 0444, NULL, cbb, &tegra_cbb_err_fops);
82 		if (IS_ERR_OR_NULL(root)) {
83 			pr_err("%s(): could not create debugfs node\n", __func__);
84 			return PTR_ERR(root);
85 		}
86 	}
87 
88 	return 0;
89 }
90 
91 void tegra_cbb_stall_enable(struct tegra_cbb *cbb)
92 {
93 	if (cbb->ops->stall_enable)
94 		cbb->ops->stall_enable(cbb);
95 }
96 
97 void tegra_cbb_fault_enable(struct tegra_cbb *cbb)
98 {
99 	if (cbb->ops->fault_enable)
100 		cbb->ops->fault_enable(cbb);
101 }
102 
103 void tegra_cbb_error_clear(struct tegra_cbb *cbb)
104 {
105 	if (cbb->ops->error_clear)
106 		cbb->ops->error_clear(cbb);
107 }
108 
109 u32 tegra_cbb_get_status(struct tegra_cbb *cbb)
110 {
111 	if (cbb->ops->get_status)
112 		return cbb->ops->get_status(cbb);
113 
114 	return 0;
115 }
116 
117 int tegra_cbb_get_irq(struct platform_device *pdev, unsigned int *nonsec_irq,
118 		      unsigned int *sec_irq)
119 {
120 	unsigned int index = 0;
121 	int num_intr = 0, irq;
122 
123 	num_intr = platform_irq_count(pdev);
124 	if (!num_intr)
125 		return -EINVAL;
126 
127 	if (num_intr == 2) {
128 		irq = platform_get_irq(pdev, index);
129 		if (irq <= 0) {
130 			dev_err(&pdev->dev, "failed to get non-secure IRQ: %d\n", irq);
131 			return -ENOENT;
132 		}
133 
134 		*nonsec_irq = irq;
135 		index++;
136 	}
137 
138 	irq = platform_get_irq(pdev, index);
139 	if (irq <= 0) {
140 		dev_err(&pdev->dev, "failed to get secure IRQ: %d\n", irq);
141 		return -ENOENT;
142 	}
143 
144 	*sec_irq = irq;
145 
146 	if (num_intr == 1)
147 		dev_dbg(&pdev->dev, "secure IRQ: %u\n", *sec_irq);
148 
149 	if (num_intr == 2)
150 		dev_dbg(&pdev->dev, "secure IRQ: %u, non-secure IRQ: %u\n", *sec_irq, *nonsec_irq);
151 
152 	return 0;
153 }
154 
155 int tegra_cbb_register(struct tegra_cbb *cbb)
156 {
157 	int ret;
158 
159 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
160 		ret = tegra_cbb_err_debugfs_init(cbb);
161 		if (ret) {
162 			dev_err(cbb->dev, "failed to create debugfs\n");
163 			return ret;
164 		}
165 	}
166 
167 	/* register interrupt handler for errors due to different initiators */
168 	ret = cbb->ops->interrupt_enable(cbb);
169 	if (ret < 0) {
170 		dev_err(cbb->dev, "Failed to register CBB Interrupt ISR");
171 		return ret;
172 	}
173 
174 	cbb->ops->error_enable(cbb);
175 	dsb(sy);
176 
177 	return 0;
178 }
179