summaryrefstree
path: root/kernel/drivers/trace/exynos-condbg-dev.c
blob: 484c7a18caba829470cc3dfee60e853265b1634f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * drivers/trace/exynos-condbg-dev.c
 *
 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
 *
 * Exynos-Console-Debugger for Exynos SoC
 * This codes are based on fiq_debugger of google
 * /driver/staging/android/fiq_debugger
 *
 * Author: Hosung Kim <hosung0.kim@samsung.com>
 *         Changki Kim <changki.kim@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/delay.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/stacktrace.h>
#include <linux/uaccess.h>
#include <linux/of.h>
#include <linux/serial_s3c.h>

#include "exynos-condbg-dev.h"

struct ecd_dev {
	struct ecd_pdata pdata;
	struct platform_device *pdev;
	void __iomem *debug_port_base;
	int irq;
	u32 baud;
	u32 frac_baud;
};

static inline struct ecd_dev *get_dbg(struct platform_device *pdev)
{
	struct ecd_pdata *pdata = dev_get_platdata(&pdev->dev);
	return container_of(pdata, struct ecd_dev, pdata);
}

static inline void exynos_write(struct ecd_dev *dbg,
			       unsigned int val, unsigned int off)
{
	__raw_writel(val, dbg->debug_port_base + off);
}

static inline unsigned int exynos_read(struct ecd_dev *dbg,
				      unsigned int off)
{
	return __raw_readl(dbg->debug_port_base + off);
}

static int debug_port_init(struct platform_device *pdev)
{
	struct ecd_dev *dbg = get_dbg(pdev);
	unsigned long timeout;

	exynos_write(dbg, dbg->baud, S3C2410_UBRDIV);
	exynos_write(dbg, dbg->frac_baud, S3C2443_DIVSLOT);

	/* Mask and clear all interrupts */
	exynos_write(dbg, 0xF, S3C64XX_UINTM);
	exynos_write(dbg, 0xF, S3C64XX_UINTP);

	exynos_write(dbg, S3C2410_LCON_CS8, S3C2410_ULCON);
	exynos_write(dbg, S5PV210_UCON_DEFAULT, S3C2410_UCON);
	exynos_write(dbg, S5PV210_UFCON_DEFAULT, S3C2410_UFCON);
	exynos_write(dbg, 0, S3C2410_UMCON);

	/* Reset TX and RX fifos */
	exynos_write(dbg, S5PV210_UFCON_DEFAULT | S3C2410_UFCON_RESETBOTH,
		S3C2410_UFCON);

	timeout = jiffies + HZ;
	while (exynos_read(dbg, S3C2410_UFCON) & S3C2410_UFCON_RESETBOTH)
		if (time_after(jiffies, timeout))
			return -ETIMEDOUT;

	/* Enable all interrupts except TX */
	exynos_write(dbg, S3C64XX_UINTM_TXD_MSK, S3C64XX_UINTM);

	return 0;
}

static int debug_getc(struct platform_device *pdev)
{
	struct ecd_dev *dbg = get_dbg(pdev);
	u32 stat;
	int ret = DEBUGGER_NO_CHAR;

	/* Clear all pending interrupts */
	exynos_write(dbg, 0xF, S3C64XX_UINTP);

	stat = exynos_read(dbg, S3C2410_UERSTAT);
	if (stat & S3C2410_UERSTAT_BREAK)
		return DEBUGGER_BREAK;

	stat = exynos_read(dbg, S3C2410_UTRSTAT);
	if (stat & S3C2410_UTRSTAT_RXDR)
		ret = exynos_read(dbg, S3C2410_URXH);

	return ret;
}

static void debug_putc(struct platform_device *pdev, unsigned int c)
{
	struct ecd_dev *dbg = get_dbg(pdev);
	int count = loops_per_jiffy;

	if (exynos_read(dbg, S3C2410_ULCON) != S3C2410_LCON_CS8)
		debug_port_init(pdev);

	while (exynos_read(dbg, S3C2410_UFSTAT) & S5PV210_UFSTAT_TXFULL)
		if (--count == 0)
			return;

	exynos_write(dbg, c, S3C2410_UTXH);
}

static void debug_flush(struct platform_device *pdev)
{
	struct ecd_dev *dbg = get_dbg(pdev);
	int count = loops_per_jiffy * HZ;

	while (!(exynos_read(dbg, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE))
		if (--count == 0)
			return;
}

static int debug_suspend(struct platform_device *pdev)
{
	struct ecd_dev *dbg = get_dbg(pdev);
#ifdef CONFIG_SERIAL_SAMSUNG_HWACG
	u32 ucon;
#endif

	exynos_write(dbg, 0xF, S3C64XX_UINTM);

#ifdef CONFIG_SERIAL_SAMSUNG_HWACG
	ucon = exynos_read(dbg, S3C2410_UCON);
	ucon &= ~(S3C2410_UCON_RXIRQMODE | S3C2410_UCON_TXIRQMODE);
	exynos_write(dbg, ucon, S3C2410_UCON);
#endif

	return 0;
}

static int debug_resume(struct platform_device *pdev)
{
	debug_port_init(pdev);

	return 0;
}

static int ecd_dev_probe(struct platform_device *pdev)
{
	struct device_node *np;
	int uart_irq;
	unsigned int uart_port;
	int ret = -ENOMEM;
	struct ecd_dev *dbg = NULL;
	struct resource *res;

	dbg = devm_kzalloc(&pdev->dev, sizeof(struct ecd_dev), GFP_KERNEL);
	if (!dbg) {
		dev_err(&pdev->dev, "Failed to allocate dbg structure\n");
		return -ENOMEM;
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev, "No mem resource\n");
		return -EINVAL;
	}

	dbg->debug_port_base = devm_ioremap_resource(&pdev->dev, res);
	if (dbg->debug_port_base == NULL) {
		dev_err(&pdev->dev, "failed to claim register region\n");
		return -ENOENT;
	}

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!res)
		return -ENXIO;

	uart_irq = platform_get_irq_byname(pdev, "uart_irq");
	if (uart_irq < 0) {
		dev_err(&pdev->dev, "No IRQ for uart_irq, error=%d\n", uart_irq);
		return uart_irq;
	}

	np = pdev->dev.of_node;
	of_property_read_u32(np, "uart_port", &uart_port);
	if (uart_port > CONFIG_SERIAL_SAMSUNG_UARTS) {
		dev_err(&pdev->dev, "wrong descriptor for uart_port, error=%d\n", uart_port);
		return -EINVAL;
	}

	pdev = kzalloc(sizeof(struct platform_device), GFP_KERNEL);
	if(!pdev) {
		pr_err("Failed to alloc ecd_dev platform device\n");
		goto err_pdev;
	}

	dbg->pdata.uart_init = debug_port_init;
	dbg->pdata.uart_getc = debug_getc;
	dbg->pdata.uart_putc = debug_putc;
	dbg->pdata.uart_flush = debug_flush;
	dbg->pdata.uart_dev_suspend = debug_suspend;
	dbg->pdata.uart_dev_resume = debug_resume;

	dbg->pdev = pdev;
	dbg->baud = exynos_read(dbg, S3C2410_UBRDIV);
	dbg->frac_baud = exynos_read(dbg, S3C2443_DIVSLOT);

	pdev->name = "console_debugger";
	pdev->id = uart_port;
	pdev->dev.platform_data = &dbg->pdata;
	pdev->resource = res;
	pdev->num_resources = 1;

	if (platform_device_register(pdev)) {
		pr_err("Failed to register ecd_dev \n");
		goto err_free;
	}

	pr_info("Success to register ECD UART\n");
	return 0;

err_pdev:
	kfree(pdev);
err_free:
	kfree(dbg);
	return ret;
}

static const struct of_device_id ecd_dev_match[] = {
	{.compatible = "samsung,exynos_console_debugger",},
	{},
};

static struct platform_driver ecd_dev_driver = {
	.probe      = ecd_dev_probe,
	.driver     = {
		.name   = "exynos_console_debugger",
		.owner  = THIS_MODULE,
		.of_match_table = of_match_ptr(ecd_dev_match),
	},
};

static int __init ecd_dev_init(void)
{
	return platform_driver_register(&ecd_dev_driver);
}

postcore_initcall_sync(ecd_dev_init);