blob: 07981e18ee4991cfdbdcb93036ef996909b7791d [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +01001/*
2 * Copyright (C) 2004-2007 ARM Limited.
3 * Copyright (C) 2008 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Michal Simek966bfa72016-02-23 10:02:28 +01004 * Copyright (C) 2015 - 2016 Xilinx, Inc, Michal Simek
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +01005 *
Tom Rini5b8031c2016-01-14 22:05:13 -05006 * SPDX-License-Identifier: GPL-2.0
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +01007 *
8 * As a special exception, if other files instantiate templates or use macros
9 * or inline functions from this file, or you compile this file and link it
10 * with other works to produce a work based on this file, this file does not
11 * by itself cause the resulting work to be covered by the GNU General Public
12 * License. However the source code for this file must still be made available
13 * in accordance with section (3) of the GNU General Public License.
14
15 * This exception does not invalidate any other reasons why a work based on
16 * this file might be covered by the GNU General Public License.
17 */
18
19#include <common.h>
Michal Simek966bfa72016-02-23 10:02:28 +010020#include <dm.h>
Jagannadha Sutradharudu Tekia168d3a2013-08-04 01:22:25 +053021#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +010022
Alexander Merklefd602c52015-03-19 18:37:19 +010023#if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V7)
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +010024/*
Alexander Merklefd602c52015-03-19 18:37:19 +010025 * ARMV6 & ARMV7
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +010026 */
Jean-Christophe PLAGNIOL-VILLARD66e8f9d2009-05-15 23:47:14 +020027#define DCC_RBIT (1 << 30)
28#define DCC_WBIT (1 << 29)
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +010029
Jean-Christophe PLAGNIOL-VILLARD66e8f9d2009-05-15 23:47:14 +020030#define write_dcc(x) \
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +010031 __asm__ volatile ("mcr p14, 0, %0, c0, c5, 0\n" : : "r" (x))
32
Jean-Christophe PLAGNIOL-VILLARD66e8f9d2009-05-15 23:47:14 +020033#define read_dcc(x) \
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +010034 __asm__ volatile ("mrc p14, 0, %0, c0, c5, 0\n" : "=r" (x))
35
Jean-Christophe PLAGNIOL-VILLARD66e8f9d2009-05-15 23:47:14 +020036#define status_dcc(x) \
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +010037 __asm__ volatile ("mrc p14, 0, %0, c0, c1, 0\n" : "=r" (x))
38
Jean-Christophe PLAGNIOL-VILLARD65a76d42009-05-15 23:47:14 +020039#elif defined(CONFIG_CPU_XSCALE)
40/*
41 * XSCALE
42 */
43#define DCC_RBIT (1 << 31)
44#define DCC_WBIT (1 << 28)
45
46#define write_dcc(x) \
47 __asm__ volatile ("mcr p14, 0, %0, c8, c0, 0\n" : : "r" (x))
48
49#define read_dcc(x) \
50 __asm__ volatile ("mrc p14, 0, %0, c9, c0, 0\n" : "=r" (x))
51
52#define status_dcc(x) \
53 __asm__ volatile ("mrc p14, 0, %0, c14, c0, 0\n" : "=r" (x))
54
Siva Durga Prasad Paladugue05412f2015-05-29 09:54:37 +020055#elif defined(CONFIG_CPU_ARMV8)
56/*
57 * ARMV8
58 */
59#define DCC_RBIT (1 << 30)
60#define DCC_WBIT (1 << 29)
61
62#define write_dcc(x) \
63 __asm__ volatile ("msr dbgdtrtx_el0, %0\n" : : "r" (x))
64
65#define read_dcc(x) \
66 __asm__ volatile ("mrs %0, dbgdtrrx_el0\n" : "=r" (x))
67
68#define status_dcc(x) \
69 __asm__ volatile ("mrs %0, mdccsr_el0\n" : "=r" (x))
70
Jean-Christophe PLAGNIOL-VILLARD66e8f9d2009-05-15 23:47:14 +020071#else
72#define DCC_RBIT (1 << 0)
73#define DCC_WBIT (1 << 1)
74
75#define write_dcc(x) \
76 __asm__ volatile ("mcr p14, 0, %0, c1, c0, 0\n" : : "r" (x))
77
78#define read_dcc(x) \
79 __asm__ volatile ("mrc p14, 0, %0, c1, c0, 0\n" : "=r" (x))
80
81#define status_dcc(x) \
82 __asm__ volatile ("mrc p14, 0, %0, c0, c0, 0\n" : "=r" (x))
83
84#endif
85
86#define can_read_dcc(x) do { \
87 status_dcc(x); \
88 x &= DCC_RBIT; \
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +010089 } while (0);
90
Jean-Christophe PLAGNIOL-VILLARD66e8f9d2009-05-15 23:47:14 +020091#define can_write_dcc(x) do { \
92 status_dcc(x); \
93 x &= DCC_WBIT; \
94 x = (x == 0); \
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +010095 } while (0);
96
97#define TIMEOUT_COUNT 0x4000000
98
Michal Simek966bfa72016-02-23 10:02:28 +010099static int arm_dcc_getc(struct udevice *dev)
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +0100100{
101 int ch;
102 register unsigned int reg;
103
Jean-Christophe PLAGNIOL-VILLARD66e8f9d2009-05-15 23:47:14 +0200104 do {
105 can_read_dcc(reg);
106 } while (!reg);
107 read_dcc(ch);
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +0100108
109 return ch;
110}
111
Michal Simek966bfa72016-02-23 10:02:28 +0100112static int arm_dcc_putc(struct udevice *dev, char ch)
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +0100113{
114 register unsigned int reg;
115 unsigned int timeout_count = TIMEOUT_COUNT;
116
Jean-Christophe PLAGNIOL-VILLARD66e8f9d2009-05-15 23:47:14 +0200117 while (--timeout_count) {
118 can_write_dcc(reg);
119 if (reg)
120 break;
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +0100121 }
Jean-Christophe PLAGNIOL-VILLARD66e8f9d2009-05-15 23:47:14 +0200122 if (timeout_count == 0)
Michal Simek966bfa72016-02-23 10:02:28 +0100123 return -EAGAIN;
Jean-Christophe PLAGNIOL-VILLARD66e8f9d2009-05-15 23:47:14 +0200124 else
125 write_dcc(ch);
Michal Simek966bfa72016-02-23 10:02:28 +0100126
127 return 0;
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +0100128}
129
Michal Simek966bfa72016-02-23 10:02:28 +0100130static int arm_dcc_pending(struct udevice *dev, bool input)
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +0100131{
132 register unsigned int reg;
133
Michal Simek966bfa72016-02-23 10:02:28 +0100134 if (input) {
135 can_read_dcc(reg);
136 } else {
137 can_write_dcc(reg);
138 }
Jean-Christophe PLAGNIOL-VILLARD4f572892009-02-22 15:49:28 +0100139
140 return reg;
141}
142
Michal Simek966bfa72016-02-23 10:02:28 +0100143static const struct dm_serial_ops arm_dcc_ops = {
144 .putc = arm_dcc_putc,
145 .pending = arm_dcc_pending,
146 .getc = arm_dcc_getc,
Jagannadha Sutradharudu Tekia168d3a2013-08-04 01:22:25 +0530147};
148
Michal Simek966bfa72016-02-23 10:02:28 +0100149static const struct udevice_id arm_dcc_ids[] = {
150 { .compatible = "arm,dcc", },
151 { }
152};
153
154U_BOOT_DRIVER(serial_dcc) = {
155 .name = "arm_dcc",
156 .id = UCLASS_SERIAL,
157 .of_match = arm_dcc_ids,
158 .ops = &arm_dcc_ops,
159 .flags = DM_FLAG_PRE_RELOC,
160};
161
162#ifdef CONFIG_DEBUG_UART_ARM_DCC
163
164#include <debug_uart.h>
165
166static inline void _debug_uart_init(void)
Jagannadha Sutradharudu Tekia168d3a2013-08-04 01:22:25 +0530167{
Jagannadha Sutradharudu Tekia168d3a2013-08-04 01:22:25 +0530168}
169
Michal Simek966bfa72016-02-23 10:02:28 +0100170static inline void _debug_uart_putc(int ch)
Michal Simeke70fb532013-01-22 23:40:06 +0000171{
Michal Simek966bfa72016-02-23 10:02:28 +0100172 arm_dcc_putc(NULL, ch);
Michal Simeke70fb532013-01-22 23:40:06 +0000173}
Michal Simek966bfa72016-02-23 10:02:28 +0100174
175DEBUG_UART_FUNCS
176#endif