blob: 28296facb2ae921876c8859c2cee95db80f72b10 [file] [log] [blame]
Scott Woodd0f53fa2007-08-21 03:39:57 +10001/*
2 * CPM serial console support.
3 *
4 * Copyright 2007 Freescale Semiconductor, Inc.
5 * Author: Scott Wood <scottwood@freescale.com>
6 *
7 * It is assumed that the firmware (or the platform file) has already set
8 * up the port.
9 */
10
11#include "types.h"
12#include "io.h"
13#include "ops.h"
14
15struct cpm_scc {
16 u32 gsmrl;
17 u32 gsmrh;
18 u16 psmr;
19 u8 res1[2];
20 u16 todr;
21 u16 dsr;
22 u16 scce;
23 u8 res2[2];
24 u16 sccm;
25 u8 res3;
26 u8 sccs;
27 u8 res4[8];
28};
29
30struct cpm_smc {
31 u8 res1[2];
32 u16 smcmr;
33 u8 res2[2];
34 u8 smce;
35 u8 res3[3];
36 u8 smcm;
37 u8 res4[5];
38};
39
40struct cpm_param {
41 u16 rbase;
42 u16 tbase;
43 u8 rfcr;
44 u8 tfcr;
45};
46
47struct cpm_bd {
48 u16 sc; /* Status and Control */
49 u16 len; /* Data length in buffer */
50 u8 *addr; /* Buffer address in host memory */
51};
52
53static void *cpcr;
54static struct cpm_param *param;
55static struct cpm_smc *smc;
56static struct cpm_scc *scc;
57struct cpm_bd *tbdf, *rbdf;
58static u32 cpm_cmd;
Scott Wood15f8c602007-09-28 14:06:16 -050059static u8 *muram_start;
60static u32 muram_offset;
Scott Woodd0f53fa2007-08-21 03:39:57 +100061
62static void (*do_cmd)(int op);
63static void (*enable_port)(void);
64static void (*disable_port)(void);
65
66#define CPM_CMD_STOP_TX 4
67#define CPM_CMD_RESTART_TX 6
68#define CPM_CMD_INIT_RX_TX 0
69
70static void cpm1_cmd(int op)
71{
72 while (in_be16(cpcr) & 1)
73 ;
74
75 out_be16(cpcr, (op << 8) | cpm_cmd | 1);
76
77 while (in_be16(cpcr) & 1)
78 ;
79}
80
81static void cpm2_cmd(int op)
82{
83 while (in_be32(cpcr) & 0x10000)
84 ;
85
86 out_be32(cpcr, op | cpm_cmd | 0x10000);
87
88 while (in_be32(cpcr) & 0x10000)
89 ;
90}
91
92static void smc_disable_port(void)
93{
94 do_cmd(CPM_CMD_STOP_TX);
95 out_be16(&smc->smcmr, in_be16(&smc->smcmr) & ~3);
96}
97
98static void scc_disable_port(void)
99{
100 do_cmd(CPM_CMD_STOP_TX);
101 out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) & ~0x30);
102}
103
104static void smc_enable_port(void)
105{
106 out_be16(&smc->smcmr, in_be16(&smc->smcmr) | 3);
107 do_cmd(CPM_CMD_RESTART_TX);
108}
109
110static void scc_enable_port(void)
111{
112 out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) | 0x30);
113 do_cmd(CPM_CMD_RESTART_TX);
114}
115
116static int cpm_serial_open(void)
117{
Scott Woodd0f53fa2007-08-21 03:39:57 +1000118 disable_port();
119
120 out_8(&param->rfcr, 0x10);
121 out_8(&param->tfcr, 0x10);
122
Scott Wood15f8c602007-09-28 14:06:16 -0500123 rbdf = (struct cpm_bd *)muram_start;
Scott Woodd0f53fa2007-08-21 03:39:57 +1000124 rbdf->addr = (u8 *)(rbdf + 2);
125 rbdf->sc = 0xa000;
126 rbdf->len = 1;
127
128 tbdf = rbdf + 1;
129 tbdf->addr = (u8 *)(rbdf + 2) + 1;
130 tbdf->sc = 0x2000;
131 tbdf->len = 1;
132
133 sync();
Scott Wood15f8c602007-09-28 14:06:16 -0500134 out_be16(&param->rbase, muram_offset);
135 out_be16(&param->tbase, muram_offset + sizeof(struct cpm_bd));
Scott Woodd0f53fa2007-08-21 03:39:57 +1000136
137 do_cmd(CPM_CMD_INIT_RX_TX);
138
139 enable_port();
140 return 0;
141}
142
143static void cpm_serial_putc(unsigned char c)
144{
145 while (tbdf->sc & 0x8000)
146 barrier();
147
148 sync();
149
150 tbdf->addr[0] = c;
151 eieio();
152 tbdf->sc |= 0x8000;
153}
154
155static unsigned char cpm_serial_tstc(void)
156{
157 barrier();
158 return !(rbdf->sc & 0x8000);
159}
160
161static unsigned char cpm_serial_getc(void)
162{
163 unsigned char c;
164
165 while (!cpm_serial_tstc())
166 ;
167
168 sync();
169 c = rbdf->addr[0];
170 eieio();
171 rbdf->sc |= 0x8000;
172
173 return c;
174}
175
176int cpm_console_init(void *devp, struct serial_console_data *scdp)
177{
178 void *reg_virt[2];
179 int is_smc = 0, is_cpm2 = 0, n;
180 unsigned long reg_phys;
Scott Wood15f8c602007-09-28 14:06:16 -0500181 void *parent, *muram;
Scott Woodd0f53fa2007-08-21 03:39:57 +1000182
183 if (dt_is_compatible(devp, "fsl,cpm1-smc-uart")) {
184 is_smc = 1;
185 } else if (dt_is_compatible(devp, "fsl,cpm2-scc-uart")) {
186 is_cpm2 = 1;
187 } else if (dt_is_compatible(devp, "fsl,cpm2-smc-uart")) {
188 is_cpm2 = 1;
189 is_smc = 1;
190 }
191
192 if (is_smc) {
193 enable_port = smc_enable_port;
194 disable_port = smc_disable_port;
195 } else {
196 enable_port = scc_enable_port;
197 disable_port = scc_disable_port;
198 }
199
200 if (is_cpm2)
201 do_cmd = cpm2_cmd;
202 else
203 do_cmd = cpm1_cmd;
204
205 n = getprop(devp, "fsl,cpm-command", &cpm_cmd, 4);
206 if (n < 4)
207 return -1;
208
209 n = getprop(devp, "virtual-reg", reg_virt, sizeof(reg_virt));
210 if (n < (int)sizeof(reg_virt)) {
211 for (n = 0; n < 2; n++) {
212 if (!dt_xlate_reg(devp, n, &reg_phys, NULL))
213 return -1;
214
215 reg_virt[n] = (void *)reg_phys;
216 }
217 }
218
219 if (is_smc)
220 smc = reg_virt[0];
221 else
222 scc = reg_virt[0];
223
224 param = reg_virt[1];
225
226 parent = get_parent(devp);
227 if (!parent)
228 return -1;
229
230 n = getprop(parent, "virtual-reg", reg_virt, sizeof(reg_virt));
231 if (n < (int)sizeof(reg_virt)) {
Scott Wood15f8c602007-09-28 14:06:16 -0500232 if (!dt_xlate_reg(parent, 0, &reg_phys, NULL))
233 return -1;
Scott Woodd0f53fa2007-08-21 03:39:57 +1000234
Scott Wood15f8c602007-09-28 14:06:16 -0500235 reg_virt[0] = (void *)reg_phys;
Scott Woodd0f53fa2007-08-21 03:39:57 +1000236 }
237
238 cpcr = reg_virt[0];
Scott Wood15f8c602007-09-28 14:06:16 -0500239
240 muram = finddevice("/soc/cpm/muram/data");
241 if (!muram)
242 return -1;
243
244 /* For bootwrapper-compatible device trees, we assume that the first
245 * entry has at least 18 bytes, and that #address-cells/#data-cells
246 * is one for both parent and child.
247 */
248
249 n = getprop(muram, "virtual-reg", reg_virt, sizeof(reg_virt));
250 if (n < (int)sizeof(reg_virt)) {
251 if (!dt_xlate_reg(muram, 0, &reg_phys, NULL))
252 return -1;
253
254 reg_virt[0] = (void *)reg_phys;
255 }
256
257 muram_start = reg_virt[0];
258
259 n = getprop(muram, "reg", &muram_offset, 4);
260 if (n < 4)
261 return -1;
Scott Woodd0f53fa2007-08-21 03:39:57 +1000262
263 scdp->open = cpm_serial_open;
264 scdp->putc = cpm_serial_putc;
265 scdp->getc = cpm_serial_getc;
266 scdp->tstc = cpm_serial_tstc;
267
268 return 0;
269}