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