ARM: OMAP1: Fix compile for board-nokia770
[linux-2.6/openmoko-kernel.git] / arch / powerpc / boot / cpm-serial.c
blob28296facb2ae921876c8859c2cee95db80f72b10
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"
15 struct 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];
30 struct 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];
40 struct cpm_param {
41 u16 rbase;
42 u16 tbase;
43 u8 rfcr;
44 u8 tfcr;
47 struct cpm_bd {
48 u16 sc; /* Status and Control */
49 u16 len; /* Data length in buffer */
50 u8 *addr; /* Buffer address in host memory */
53 static void *cpcr;
54 static struct cpm_param *param;
55 static struct cpm_smc *smc;
56 static struct cpm_scc *scc;
57 struct cpm_bd *tbdf, *rbdf;
58 static u32 cpm_cmd;
59 static u8 *muram_start;
60 static u32 muram_offset;
62 static void (*do_cmd)(int op);
63 static void (*enable_port)(void);
64 static void (*disable_port)(void);
66 #define CPM_CMD_STOP_TX 4
67 #define CPM_CMD_RESTART_TX 6
68 #define CPM_CMD_INIT_RX_TX 0
70 static void cpm1_cmd(int op)
72 while (in_be16(cpcr) & 1)
75 out_be16(cpcr, (op << 8) | cpm_cmd | 1);
77 while (in_be16(cpcr) & 1)
81 static void cpm2_cmd(int op)
83 while (in_be32(cpcr) & 0x10000)
86 out_be32(cpcr, op | cpm_cmd | 0x10000);
88 while (in_be32(cpcr) & 0x10000)
92 static void smc_disable_port(void)
94 do_cmd(CPM_CMD_STOP_TX);
95 out_be16(&smc->smcmr, in_be16(&smc->smcmr) & ~3);
98 static void scc_disable_port(void)
100 do_cmd(CPM_CMD_STOP_TX);
101 out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) & ~0x30);
104 static void smc_enable_port(void)
106 out_be16(&smc->smcmr, in_be16(&smc->smcmr) | 3);
107 do_cmd(CPM_CMD_RESTART_TX);
110 static void scc_enable_port(void)
112 out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) | 0x30);
113 do_cmd(CPM_CMD_RESTART_TX);
116 static int cpm_serial_open(void)
118 disable_port();
120 out_8(&param->rfcr, 0x10);
121 out_8(&param->tfcr, 0x10);
123 rbdf = (struct cpm_bd *)muram_start;
124 rbdf->addr = (u8 *)(rbdf + 2);
125 rbdf->sc = 0xa000;
126 rbdf->len = 1;
128 tbdf = rbdf + 1;
129 tbdf->addr = (u8 *)(rbdf + 2) + 1;
130 tbdf->sc = 0x2000;
131 tbdf->len = 1;
133 sync();
134 out_be16(&param->rbase, muram_offset);
135 out_be16(&param->tbase, muram_offset + sizeof(struct cpm_bd));
137 do_cmd(CPM_CMD_INIT_RX_TX);
139 enable_port();
140 return 0;
143 static void cpm_serial_putc(unsigned char c)
145 while (tbdf->sc & 0x8000)
146 barrier();
148 sync();
150 tbdf->addr[0] = c;
151 eieio();
152 tbdf->sc |= 0x8000;
155 static unsigned char cpm_serial_tstc(void)
157 barrier();
158 return !(rbdf->sc & 0x8000);
161 static unsigned char cpm_serial_getc(void)
163 unsigned char c;
165 while (!cpm_serial_tstc())
168 sync();
169 c = rbdf->addr[0];
170 eieio();
171 rbdf->sc |= 0x8000;
173 return c;
176 int cpm_console_init(void *devp, struct serial_console_data *scdp)
178 void *reg_virt[2];
179 int is_smc = 0, is_cpm2 = 0, n;
180 unsigned long reg_phys;
181 void *parent, *muram;
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;
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;
200 if (is_cpm2)
201 do_cmd = cpm2_cmd;
202 else
203 do_cmd = cpm1_cmd;
205 n = getprop(devp, "fsl,cpm-command", &cpm_cmd, 4);
206 if (n < 4)
207 return -1;
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;
215 reg_virt[n] = (void *)reg_phys;
219 if (is_smc)
220 smc = reg_virt[0];
221 else
222 scc = reg_virt[0];
224 param = reg_virt[1];
226 parent = get_parent(devp);
227 if (!parent)
228 return -1;
230 n = getprop(parent, "virtual-reg", reg_virt, sizeof(reg_virt));
231 if (n < (int)sizeof(reg_virt)) {
232 if (!dt_xlate_reg(parent, 0, &reg_phys, NULL))
233 return -1;
235 reg_virt[0] = (void *)reg_phys;
238 cpcr = reg_virt[0];
240 muram = finddevice("/soc/cpm/muram/data");
241 if (!muram)
242 return -1;
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.
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;
254 reg_virt[0] = (void *)reg_phys;
257 muram_start = reg_virt[0];
259 n = getprop(muram, "reg", &muram_offset, 4);
260 if (n < 4)
261 return -1;
263 scdp->open = cpm_serial_open;
264 scdp->putc = cpm_serial_putc;
265 scdp->getc = cpm_serial_getc;
266 scdp->tstc = cpm_serial_tstc;
268 return 0;