1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Russell King
4  *  Rewritten from the dovefb driver, and Armada510 manuals.
5  */
6 #include <linux/ctype.h>
7 #include <linux/debugfs.h>
8 #include <linux/module.h>
9 #include <linux/seq_file.h>
10 #include <drm/drmP.h>
11 #include "armada_crtc.h"
12 #include "armada_drm.h"
13 
14 static int armada_debugfs_gem_linear_show(struct seq_file *m, void *data)
15 {
16 	struct drm_info_node *node = m->private;
17 	struct drm_device *dev = node->minor->dev;
18 	struct armada_private *priv = dev->dev_private;
19 	struct drm_printer p = drm_seq_file_printer(m);
20 
21 	mutex_lock(&priv->linear_lock);
22 	drm_mm_print(&priv->linear, &p);
23 	mutex_unlock(&priv->linear_lock);
24 
25 	return 0;
26 }
27 
28 static int armada_debugfs_crtc_reg_show(struct seq_file *m, void *data)
29 {
30 	struct armada_crtc *dcrtc = m->private;
31 	int i;
32 
33 	for (i = 0x84; i <= 0x1c4; i += 4) {
34 		u32 v = readl_relaxed(dcrtc->base + i);
35 		seq_printf(m, "0x%04x: 0x%08x\n", i, v);
36 	}
37 
38 	return 0;
39 }
40 
41 static int armada_debugfs_crtc_reg_open(struct inode *inode, struct file *file)
42 {
43 	return single_open(file, armada_debugfs_crtc_reg_show,
44 			   inode->i_private);
45 }
46 
47 static int armada_debugfs_crtc_reg_write(struct file *file,
48 	const char __user *ptr, size_t len, loff_t *off)
49 {
50 	struct armada_crtc *dcrtc;
51 	unsigned long reg, mask, val;
52 	char buf[32];
53 	int ret;
54 	u32 v;
55 
56 	if (*off != 0)
57 		return 0;
58 
59 	if (len > sizeof(buf) - 1)
60 		len = sizeof(buf) - 1;
61 
62 	ret = strncpy_from_user(buf, ptr, len);
63 	if (ret < 0)
64 		return ret;
65 	buf[len] = '\0';
66 
67 	if (sscanf(buf, "%lx %lx %lx", &reg, &mask, &val) != 3)
68 		return -EINVAL;
69 	if (reg < 0x84 || reg > 0x1c4 || reg & 3)
70 		return -ERANGE;
71 
72 	dcrtc = ((struct seq_file *)file->private_data)->private;
73 	v = readl(dcrtc->base + reg);
74 	v &= ~mask;
75 	v |= val & mask;
76 	writel(v, dcrtc->base + reg);
77 
78 	return len;
79 }
80 
81 static const struct file_operations armada_debugfs_crtc_reg_fops = {
82 	.owner = THIS_MODULE,
83 	.open = armada_debugfs_crtc_reg_open,
84 	.read = seq_read,
85 	.write = armada_debugfs_crtc_reg_write,
86 	.llseek = seq_lseek,
87 	.release = single_release,
88 };
89 
90 void armada_drm_crtc_debugfs_init(struct armada_crtc *dcrtc)
91 {
92 	debugfs_create_file("armada-regs", 0600, dcrtc->crtc.debugfs_entry,
93 			    dcrtc, &armada_debugfs_crtc_reg_fops);
94 }
95 
96 static struct drm_info_list armada_debugfs_list[] = {
97 	{ "gem_linear", armada_debugfs_gem_linear_show, 0 },
98 };
99 #define ARMADA_DEBUGFS_ENTRIES ARRAY_SIZE(armada_debugfs_list)
100 
101 int armada_drm_debugfs_init(struct drm_minor *minor)
102 {
103 	drm_debugfs_create_files(armada_debugfs_list, ARMADA_DEBUGFS_ENTRIES,
104 				 minor->debugfs_root, minor);
105 
106 	return 0;
107 }
108