1 // SPDX-License-Identifier: GPL-2.0
3 * CPM serial console support.
5 * Copyright 2007 Freescale Semiconductor, Inc.
6 * Author: Scott Wood <scottwood@freescale.com>
8 * It is assumed that the firmware (or the platform file) has already set
66 u16 sc; /* Status and Control */
67 u16 len; /* Data length in buffer */
68 u8 *addr; /* Buffer address in host memory */
72 static struct cpm_param *param;
73 static struct cpm_smc *smc;
74 static struct cpm_scc *scc;
75 static struct cpm_bd *tbdf, *rbdf;
77 static void *cbd_addr;
78 static u32 cbd_offset;
80 static void (*do_cmd)(int op);
81 static void (*enable_port)(void);
82 static void (*disable_port)(void);
84 #define CPM_CMD_STOP_TX 4
85 #define CPM_CMD_RESTART_TX 6
86 #define CPM_CMD_INIT_RX_TX 0
88 static void cpm1_cmd(int op)
90 while (in_be16(cpcr) & 1)
93 out_be16(cpcr, (op << 8) | cpm_cmd | 1);
95 while (in_be16(cpcr) & 1)
99 static void cpm2_cmd(int op)
101 while (in_be32(cpcr) & 0x10000)
104 out_be32(cpcr, op | cpm_cmd | 0x10000);
106 while (in_be32(cpcr) & 0x10000)
110 static void smc_disable_port(void)
112 do_cmd(CPM_CMD_STOP_TX);
113 out_be16(&smc->smcmr, in_be16(&smc->smcmr) & ~3);
116 static void scc_disable_port(void)
118 do_cmd(CPM_CMD_STOP_TX);
119 out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) & ~0x30);
122 static void smc_enable_port(void)
124 out_be16(&smc->smcmr, in_be16(&smc->smcmr) | 3);
125 do_cmd(CPM_CMD_RESTART_TX);
128 static void scc_enable_port(void)
130 out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) | 0x30);
131 do_cmd(CPM_CMD_RESTART_TX);
134 static int cpm_serial_open(void)
138 out_8(¶m->rfcr, 0x10);
139 out_8(¶m->tfcr, 0x10);
140 out_be16(¶m->mrblr, 1);
141 out_be16(¶m->maxidl, 0);
142 out_be16(¶m->brkec, 0);
143 out_be16(¶m->brkln, 0);
144 out_be16(¶m->brkcr, 0);
147 rbdf->addr = (u8 *)rbdf - 1;
152 tbdf->addr = (u8 *)rbdf - 2;
157 out_be16(¶m->rbase, cbd_offset);
158 out_be16(¶m->tbase, cbd_offset + sizeof(struct cpm_bd));
160 do_cmd(CPM_CMD_INIT_RX_TX);
166 static void cpm_serial_putc(unsigned char c)
168 while (tbdf->sc & 0x8000)
178 static unsigned char cpm_serial_tstc(void)
181 return !(rbdf->sc & 0x8000);
184 static unsigned char cpm_serial_getc(void)
188 while (!cpm_serial_tstc())
199 int cpm_console_init(void *devp, struct serial_console_data *scdp)
203 int is_smc = 0, is_cpm2 = 0;
204 void *parent, *muram;
206 unsigned long muram_offset, muram_size;
208 if (dt_is_compatible(devp, "fsl,cpm1-smc-uart")) {
210 } else if (dt_is_compatible(devp, "fsl,cpm2-scc-uart")) {
212 } else if (dt_is_compatible(devp, "fsl,cpm2-smc-uart")) {
218 enable_port = smc_enable_port;
219 disable_port = smc_disable_port;
221 enable_port = scc_enable_port;
222 disable_port = scc_disable_port;
230 if (getprop(devp, "fsl,cpm-command", &cpm_cmd, 4) < 4)
233 if (dt_get_virtual_reg(devp, vreg, 2) < 2)
243 parent = get_parent(devp);
247 if (dt_get_virtual_reg(parent, &cpcr, 1) < 1)
250 muram = finddevice("/soc/cpm/muram/data");
254 /* For bootwrapper-compatible device trees, we assume that the first
255 * entry has at least 128 bytes, and that #address-cells/#data-cells
256 * is one for both parent and child.
259 if (dt_get_virtual_reg(muram, &muram_addr, 1) < 1)
262 if (getprop(muram, "reg", reg, 8) < 8)
265 muram_offset = reg[0];
268 /* Store the buffer descriptors at the end of the first muram chunk.
269 * For SMC ports on CPM2-based platforms, relocate the parameter RAM
270 * just before the buffer descriptors.
273 cbd_offset = muram_offset + muram_size - 2 * sizeof(struct cpm_bd);
275 if (is_cpm2 && is_smc) {
276 u16 *smc_base = (u16 *)param;
279 pram_offset = cbd_offset - 64;
280 pram_offset = _ALIGN_DOWN(pram_offset, 64);
283 out_be16(smc_base, pram_offset);
284 param = muram_addr - muram_offset + pram_offset;
287 cbd_addr = muram_addr - muram_offset + cbd_offset;
289 scdp->open = cpm_serial_open;
290 scdp->putc = cpm_serial_putc;
291 scdp->getc = cpm_serial_getc;
292 scdp->tstc = cpm_serial_tstc;