OMAP3: PM: Ensure MUSB block can idle when driver not loaded
[linux-ginger.git] / arch / powerpc / boot / cpm-serial.c
blob19dc15abe43de27a693f214ec44aa7de77260834
1 /*
2 * CPM serial console support.
4 * Copyright 2007 Freescale Semiconductor, Inc.
5 * Author: Scott Wood <scottwood@freescale.com>
7 * It is assumed that the firmware (or the platform file) has already set
8 * up the port.
9 */
11 #include "types.h"
12 #include "io.h"
13 #include "ops.h"
14 #include "page.h"
16 struct cpm_scc {
17 u32 gsmrl;
18 u32 gsmrh;
19 u16 psmr;
20 u8 res1[2];
21 u16 todr;
22 u16 dsr;
23 u16 scce;
24 u8 res2[2];
25 u16 sccm;
26 u8 res3;
27 u8 sccs;
28 u8 res4[8];
31 struct cpm_smc {
32 u8 res1[2];
33 u16 smcmr;
34 u8 res2[2];
35 u8 smce;
36 u8 res3[3];
37 u8 smcm;
38 u8 res4[5];
41 struct cpm_param {
42 u16 rbase;
43 u16 tbase;
44 u8 rfcr;
45 u8 tfcr;
46 u16 mrblr;
47 u32 rstate;
48 u8 res1[4];
49 u16 rbptr;
50 u8 res2[6];
51 u32 tstate;
52 u8 res3[4];
53 u16 tbptr;
54 u8 res4[6];
55 u16 maxidl;
56 u16 idlc;
57 u16 brkln;
58 u16 brkec;
59 u16 brkcr;
60 u16 rmask;
61 u8 res5[4];
64 struct cpm_bd {
65 u16 sc; /* Status and Control */
66 u16 len; /* Data length in buffer */
67 u8 *addr; /* Buffer address in host memory */
70 static void *cpcr;
71 static struct cpm_param *param;
72 static struct cpm_smc *smc;
73 static struct cpm_scc *scc;
74 static struct cpm_bd *tbdf, *rbdf;
75 static u32 cpm_cmd;
76 static void *cbd_addr;
77 static u32 cbd_offset;
79 static void (*do_cmd)(int op);
80 static void (*enable_port)(void);
81 static void (*disable_port)(void);
83 #define CPM_CMD_STOP_TX 4
84 #define CPM_CMD_RESTART_TX 6
85 #define CPM_CMD_INIT_RX_TX 0
87 static void cpm1_cmd(int op)
89 while (in_be16(cpcr) & 1)
92 out_be16(cpcr, (op << 8) | cpm_cmd | 1);
94 while (in_be16(cpcr) & 1)
98 static void cpm2_cmd(int op)
100 while (in_be32(cpcr) & 0x10000)
103 out_be32(cpcr, op | cpm_cmd | 0x10000);
105 while (in_be32(cpcr) & 0x10000)
109 static void smc_disable_port(void)
111 do_cmd(CPM_CMD_STOP_TX);
112 out_be16(&smc->smcmr, in_be16(&smc->smcmr) & ~3);
115 static void scc_disable_port(void)
117 do_cmd(CPM_CMD_STOP_TX);
118 out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) & ~0x30);
121 static void smc_enable_port(void)
123 out_be16(&smc->smcmr, in_be16(&smc->smcmr) | 3);
124 do_cmd(CPM_CMD_RESTART_TX);
127 static void scc_enable_port(void)
129 out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) | 0x30);
130 do_cmd(CPM_CMD_RESTART_TX);
133 static int cpm_serial_open(void)
135 disable_port();
137 out_8(&param->rfcr, 0x10);
138 out_8(&param->tfcr, 0x10);
139 out_be16(&param->mrblr, 1);
140 out_be16(&param->maxidl, 0);
141 out_be16(&param->brkec, 0);
142 out_be16(&param->brkln, 0);
143 out_be16(&param->brkcr, 0);
145 rbdf = cbd_addr;
146 rbdf->addr = (u8 *)rbdf - 1;
147 rbdf->sc = 0xa000;
148 rbdf->len = 1;
150 tbdf = rbdf + 1;
151 tbdf->addr = (u8 *)rbdf - 2;
152 tbdf->sc = 0x2000;
153 tbdf->len = 1;
155 sync();
156 out_be16(&param->rbase, cbd_offset);
157 out_be16(&param->tbase, cbd_offset + sizeof(struct cpm_bd));
159 do_cmd(CPM_CMD_INIT_RX_TX);
161 enable_port();
162 return 0;
165 static void cpm_serial_putc(unsigned char c)
167 while (tbdf->sc & 0x8000)
168 barrier();
170 sync();
172 tbdf->addr[0] = c;
173 eieio();
174 tbdf->sc |= 0x8000;
177 static unsigned char cpm_serial_tstc(void)
179 barrier();
180 return !(rbdf->sc & 0x8000);
183 static unsigned char cpm_serial_getc(void)
185 unsigned char c;
187 while (!cpm_serial_tstc())
190 sync();
191 c = rbdf->addr[0];
192 eieio();
193 rbdf->sc |= 0x8000;
195 return c;
198 int cpm_console_init(void *devp, struct serial_console_data *scdp)
200 void *vreg[2];
201 u32 reg[2];
202 int is_smc = 0, is_cpm2 = 0;
203 void *parent, *muram;
204 void *muram_addr;
205 unsigned long muram_offset, muram_size;
207 if (dt_is_compatible(devp, "fsl,cpm1-smc-uart")) {
208 is_smc = 1;
209 } else if (dt_is_compatible(devp, "fsl,cpm2-scc-uart")) {
210 is_cpm2 = 1;
211 } else if (dt_is_compatible(devp, "fsl,cpm2-smc-uart")) {
212 is_cpm2 = 1;
213 is_smc = 1;
216 if (is_smc) {
217 enable_port = smc_enable_port;
218 disable_port = smc_disable_port;
219 } else {
220 enable_port = scc_enable_port;
221 disable_port = scc_disable_port;
224 if (is_cpm2)
225 do_cmd = cpm2_cmd;
226 else
227 do_cmd = cpm1_cmd;
229 if (getprop(devp, "fsl,cpm-command", &cpm_cmd, 4) < 4)
230 return -1;
232 if (dt_get_virtual_reg(devp, vreg, 2) < 2)
233 return -1;
235 if (is_smc)
236 smc = vreg[0];
237 else
238 scc = vreg[0];
240 param = vreg[1];
242 parent = get_parent(devp);
243 if (!parent)
244 return -1;
246 if (dt_get_virtual_reg(parent, &cpcr, 1) < 1)
247 return -1;
249 muram = finddevice("/soc/cpm/muram/data");
250 if (!muram)
251 return -1;
253 /* For bootwrapper-compatible device trees, we assume that the first
254 * entry has at least 128 bytes, and that #address-cells/#data-cells
255 * is one for both parent and child.
258 if (dt_get_virtual_reg(muram, &muram_addr, 1) < 1)
259 return -1;
261 if (getprop(muram, "reg", reg, 8) < 8)
262 return -1;
264 muram_offset = reg[0];
265 muram_size = reg[1];
267 /* Store the buffer descriptors at the end of the first muram chunk.
268 * For SMC ports on CPM2-based platforms, relocate the parameter RAM
269 * just before the buffer descriptors.
272 cbd_offset = muram_offset + muram_size - 2 * sizeof(struct cpm_bd);
274 if (is_cpm2 && is_smc) {
275 u16 *smc_base = (u16 *)param;
276 u16 pram_offset;
278 pram_offset = cbd_offset - 64;
279 pram_offset = _ALIGN_DOWN(pram_offset, 64);
281 disable_port();
282 out_be16(smc_base, pram_offset);
283 param = muram_addr - muram_offset + pram_offset;
286 cbd_addr = muram_addr - muram_offset + cbd_offset;
288 scdp->open = cpm_serial_open;
289 scdp->putc = cpm_serial_putc;
290 scdp->getc = cpm_serial_getc;
291 scdp->tstc = cpm_serial_tstc;
293 return 0;