aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-omap2/omap_fiq_debugger.c
blob: 174bba044d29af2506fe667b94d36c244e3aea53 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * Serial Debugger Interface for Omap
 *
 * Copyright (C) 2011 Google, Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/serial_reg.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/stacktrace.h>
#include <linux/uaccess.h>

#include <plat/omap_device.h>
#include <plat/omap-pm.h>
#include <plat/omap-serial.h>

#include <asm/fiq_debugger.h>

#include <mach/omap_fiq_debugger.h>
#include <mach/system.h>

#include "mux.h"

struct omap_fiq_debugger {
	struct fiq_debugger_pdata pdata;
	struct platform_device *pdev;
	void __iomem *debug_port_base;
	bool suspended;
	spinlock_t lock;
	bool have_state;

	/* uart state */
	unsigned char lcr;
	unsigned char fcr;
	unsigned char efr;
	unsigned char dll;
	unsigned char dlh;
	unsigned char mcr;
	unsigned char ier;
	unsigned char wer;
};

static struct omap_fiq_debugger *dbgs[OMAP_MAX_HSUART_PORTS];

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

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

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

static void debug_omap_port_enable(struct platform_device *pdev)
{
	pm_runtime_get_sync(&pdev->dev);
}

static void debug_omap_port_disable(struct platform_device *pdev)
{
	struct omap_fiq_debugger *dbg = get_dbg(pdev);
	unsigned long flags;

	spin_lock_irqsave(&dbg->lock, flags);
	if (!dbg->suspended) {
		pm_runtime_mark_last_busy(&pdev->dev);
		pm_runtime_put_autosuspend(&pdev->dev);
	} else {
		pm_runtime_put_sync_suspend(&pdev->dev);
	}
	spin_unlock_irqrestore(&dbg->lock, flags);
}

static int debug_omap_port_resume(struct platform_device *pdev)
{
	struct omap_fiq_debugger *dbg = get_dbg(pdev);

	dbg->suspended = false;
	barrier();
	return 0;
}

static int debug_omap_port_suspend(struct platform_device *pdev)
{
	struct omap_fiq_debugger *dbg = get_dbg(pdev);
	unsigned long flags;

	/* this will force the device to be idle'd now, in case it was
	 * autosuspended but timer has not yet run out.
	 */
	spin_lock_irqsave(&dbg->lock, flags);
	dbg->suspended = true;
	pm_runtime_get_sync(&pdev->dev);
	pm_runtime_put_sync_suspend(&pdev->dev);
	spin_unlock_irqrestore(&dbg->lock, flags);

	return 0;
}

/* mostly copied from drivers/tty/serial/omap-serial.c */
static void omap_write_mdr1(struct omap_fiq_debugger *dbg, u8 mdr1)
{
	u8 timeout = 255;

	if (!(cpu_is_omap34xx() || cpu_is_omap44xx())) {
		omap_write(dbg, UART_OMAP_MDR1_DISABLE, UART_OMAP_MDR1);
		return;
	}

	omap_write(dbg, mdr1, UART_OMAP_MDR1);
	udelay(2);
	omap_write(dbg, dbg->fcr | UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR,
		   UART_FCR);

	/*
	 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
	 * TX_FIFO_E bit is 1.
	 */
	while (UART_LSR_THRE !=
	       (omap_read(dbg, UART_LSR) & (UART_LSR_THRE | UART_LSR_DR))) {
		timeout--;
		if (!timeout) {
			/* Should *never* happen. we warn and carry on */
			dev_crit(&dbg->pdev->dev, "Errata i202: timedout %x\n",
				 omap_read(dbg, UART_LSR));
			break;
		}
		udelay(1);
	}
}

/* assume the bootloader programmed us correctly */
static void debug_port_read_state(struct omap_fiq_debugger *dbg)
{
	/* assume we're in operational mode when we are called */
	dbg->lcr = omap_read(dbg, UART_LCR);

	/* config mode A */
	omap_write(dbg, UART_LCR_CONF_MODE_A, UART_LCR);
	dbg->mcr = omap_read(dbg, UART_MCR);

	/* config mode B */
	omap_write(dbg, UART_LCR_CONF_MODE_B, UART_LCR);
	dbg->efr = omap_read(dbg, UART_EFR);
	dbg->dll = omap_read(dbg, UART_DLL);
	dbg->dlh = omap_read(dbg, UART_DLM);

	/* back to operational */
	omap_write(dbg, dbg->lcr, UART_LCR);

	pr_debug("%s: lcr=%02x mcr=%02x efr=%02x dll=%02x dlh=%02x\n",
		 __func__, dbg->lcr, dbg->mcr, dbg->efr, dbg->dll, dbg->dlh);
}

static void debug_port_restore(struct omap_fiq_debugger *dbg)
{
	omap_write(dbg, UART_LCR_CONF_MODE_B, UART_LCR); /* Config B mode */
	omap_write(dbg, dbg->efr | UART_EFR_ECB, UART_EFR);
	omap_write(dbg, 0x0, UART_LCR); /* Operational mode */
	omap_write(dbg, 0x0, UART_IER);
	omap_write(dbg, UART_LCR_CONF_MODE_B, UART_LCR); /* Config B mode */
	omap_write(dbg, 0, UART_DLL);
	omap_write(dbg, 0, UART_DLM);
	omap_write(dbg, UART_LCR_CONF_MODE_A, UART_LCR);
	omap_write(dbg, dbg->mcr | UART_MCR_TCRTLR, UART_MCR);
	omap_write(dbg, UART_LCR_CONF_MODE_B, UART_LCR);
	omap_write(dbg, 0, UART_TI752_TLR);
	omap_write(dbg, 0, UART_SCR);
	omap_write(dbg, dbg->efr, UART_EFR);
	omap_write(dbg, UART_LCR_CONF_MODE_A, UART_LCR);
	omap_write(dbg, dbg->fcr, UART_FCR);
	omap_write(dbg, dbg->mcr, UART_MCR);
	omap_write_mdr1(dbg, UART_OMAP_MDR1_DISABLE);

	omap_write(dbg, UART_LCR_CONF_MODE_B, UART_LCR);
	omap_write(dbg, dbg->efr | UART_EFR_ECB, UART_EFR);
	omap_write(dbg, dbg->dll, UART_DLL);
	omap_write(dbg, dbg->dlh, UART_DLM);
	omap_write(dbg, 0, UART_LCR);
	omap_write(dbg, dbg->ier, UART_IER);
	omap_write(dbg, UART_LCR_CONF_MODE_B, UART_LCR);
	omap_write(dbg, dbg->efr, UART_EFR);

	/* will put us back to operational mode */
	omap_write(dbg, dbg->lcr, UART_LCR);
	omap_write_mdr1(dbg, UART_OMAP_MDR1_16X_MODE);

	omap_write(dbg, dbg->wer, UART_OMAP_WER);
}

u32 omap_debug_uart_resume_idle(void)
{
	int i;
	u32 ret = 0;

	for (i = 0; i < OMAP_MAX_HSUART_PORTS; i++) {
		struct omap_fiq_debugger *dbg = dbgs[i];
		struct omap_device *od;

		if (!dbg || !dbg->pdev)
			continue;

		od = to_omap_device(dbg->pdev);
		if (omap_hwmod_pad_get_wakeup_status(od->hwmods[0])) {
			/*
			 * poke the uart and let it stay on long enough
			 * to process any further data. It's ok to use
			 * autosuspend here since this is on the resume path
			 * during the wakeup. We'll still go through a full
			 * resume cycle, so if we go back to suspend
			 * the suspended flag will properly get reset.
			 */
			pm_runtime_get_sync(&dbg->pdev->dev);
			pm_runtime_mark_last_busy(&dbg->pdev->dev);
			pm_runtime_put_autosuspend(&dbg->pdev->dev);
			dev_dbg(&dbg->pdev->dev, "woke up from IO pad\n");
			ret++;
		}
	}

	return ret;
}

static int debug_port_init(struct platform_device *pdev)
{
	struct omap_fiq_debugger *dbg = get_dbg(pdev);

	dbg->ier = UART_IER_RLSI | UART_IER_RDI;
	dbg->fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
		UART_FCR_T_TRIG_01;
	dbg->wer = 0;

	device_init_wakeup(&pdev->dev, true);

	pm_runtime_use_autosuspend(&pdev->dev);
	pm_runtime_set_autosuspend_delay(&pdev->dev, DEFAULT_AUTOSUSPEND_DELAY);

	pm_runtime_enable(&pdev->dev);
	pm_runtime_irq_safe(&pdev->dev);

	omap_hwmod_idle(to_omap_device(pdev)->hwmods[0]);
	debug_omap_port_enable(pdev);
	debug_omap_port_disable(pdev);

	debug_omap_port_enable(pdev);

	if (device_may_wakeup(&pdev->dev))
		omap_hwmod_enable_wakeup(to_omap_device(pdev)->hwmods[0]);

	debug_port_read_state(dbg);
	debug_port_restore(dbg);

	dbg->have_state = true;


	debug_omap_port_disable(pdev);
	return 0;
}

static int debug_getc(struct platform_device *pdev)
{
	struct omap_fiq_debugger *dbg = get_dbg(pdev);
	unsigned int lsr;
	int ret = FIQ_DEBUGGER_NO_CHAR;

	lsr = omap_read(dbg, UART_LSR);
	if (lsr & UART_LSR_BI) {
		/* need to read RHR to clear the BI condition */
		omap_read(dbg, UART_RX);
		ret = FIQ_DEBUGGER_BREAK;
	} else if (lsr & UART_LSR_DR) {
		ret = omap_read(dbg, UART_RX);
	}

	return ret;
}

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

	while (!(omap_read(dbg, UART_LSR) & UART_LSR_THRE))
		cpu_relax();

	omap_write(dbg, c, UART_TX);
}

static void debug_flush(struct platform_device *pdev)
{
	struct omap_fiq_debugger *dbg = get_dbg(pdev);

	while (!(omap_read(dbg, UART_LSR) & UART_LSR_TEMT))
		cpu_relax();
}

static int uart_idle_hwmod(struct omap_device *od)
{
	omap_hwmod_idle(od->hwmods[0]);

	return 0;
}

static int uart_enable_hwmod(struct omap_device *od)
{
	struct platform_device *pdev = &od->pdev;
	struct omap_fiq_debugger *dbg = get_dbg(pdev);

	omap_hwmod_enable(od->hwmods[0]);
	if (omap_pm_was_context_lost(&pdev->dev) && dbg->have_state) {
		debug_port_restore(dbg);
		dev_dbg(&pdev->dev, "restoring lost context!\n");
	}

	return 0;
}

static struct omap_device_pm_latency omap_uart_latency[] = {
	{
		.deactivate_func = uart_idle_hwmod,
		.activate_func	 = uart_enable_hwmod,
		.flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
	},
};

extern struct omap_hwmod *omap_uart_hwmod_lookup(int num);

int __init omap_serial_debug_init(int id, bool is_fiq, bool is_high_prio_irq,
				  struct omap_device_pad *pads, int num_pads)
{
	struct omap_fiq_debugger *dbg;
	struct omap_hwmod *oh;
	struct omap_device *od;
	int ret;

	if (id >= OMAP_MAX_HSUART_PORTS)
		return -EINVAL;
	if (dbgs[id])
		return -EBUSY;

	oh = omap_uart_hwmod_lookup(id);
	if (!oh)
		return -ENODEV;

	oh->mpu_irqs[0].name = "uart_irq";
	oh->mux = omap_hwmod_mux_init(pads, num_pads);

	dbg = kzalloc(sizeof(struct omap_fiq_debugger), GFP_KERNEL);
	if (!dbg) {
		pr_err("Failed to allocate for fiq debugger\n");
		return -ENOMEM;
	}

	dbg->debug_port_base = ioremap(oh->slaves[0]->addr[0].pa_start,
				       PAGE_SIZE);
	if (!dbg->debug_port_base) {
		pr_err("Failed to ioremap for fiq debugger\n");
		ret = -ENOMEM;
		goto err_ioremap;
	}

	spin_lock_init(&dbg->lock);

	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_enable = debug_omap_port_enable;
	dbg->pdata.uart_disable = debug_omap_port_disable;
	dbg->pdata.uart_dev_suspend = debug_omap_port_suspend;
	dbg->pdata.uart_dev_resume = debug_omap_port_resume;

	od = omap_device_build("fiq_debugger", id,
			       oh, dbg, sizeof(*dbg), omap_uart_latency,
			       ARRAY_SIZE(omap_uart_latency), false);
	if (IS_ERR(od)) {
		pr_err("Could not build omap_device for fiq_debugger: %s\n",
		       oh->name);
		ret = PTR_ERR(od);
		goto err_dev_build;
	}

	dbg->pdev = &od->pdev;
	dbgs[id] = dbg;

	return 0;

err_dev_build:
	iounmap(dbg->debug_port_base);
err_ioremap:
	kfree(dbg);
	return ret;
}