blob: 9ce3fc3d9ec4319d3514e3bdd84ca77b6fd13d87 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Christophe Leroy907208c2017-07-06 10:23:22 +02002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Christophe Leroy907208c2017-07-06 10:23:22 +02005 */
6
Christophe Leroy907208c2017-07-06 10:23:22 +02007#include <command.h>
Christophe Leroybdfa11e2018-11-21 08:51:49 +00008#include <dm.h>
Christophe Leroy907208c2017-07-06 10:23:22 +02009#include <serial.h>
10#include <watchdog.h>
Christophe Leroy18f8d4c2018-03-16 17:20:43 +010011#include <asm/cpm_8xx.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Christophe Leroy907208c2017-07-06 10:23:22 +020013#include <linux/compiler.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
Christophe Leroy907208c2017-07-06 10:23:22 +020017#if defined(CONFIG_8xx_CONS_SMC1) /* Console on SMC1 */
18#define SMC_INDEX 0
19#define PROFF_SMC PROFF_SMC1
20#define CPM_CR_CH_SMC CPM_CR_CH_SMC1
Christophe Leroyba3da732017-07-06 10:33:13 +020021#define IOPINS 0xc0
Christophe Leroy907208c2017-07-06 10:23:22 +020022
23#elif defined(CONFIG_8xx_CONS_SMC2) /* Console on SMC2 */
24#define SMC_INDEX 1
25#define PROFF_SMC PROFF_SMC2
26#define CPM_CR_CH_SMC CPM_CR_CH_SMC2
Christophe Leroyba3da732017-07-06 10:33:13 +020027#define IOPINS 0xc00
Christophe Leroy907208c2017-07-06 10:23:22 +020028
29#endif /* CONFIG_8xx_CONS_SMCx */
30
Christophe Leroyba3da732017-07-06 10:33:13 +020031struct serialbuffer {
Christophe Leroy907208c2017-07-06 10:23:22 +020032 cbd_t rxbd; /* Rx BD */
33 cbd_t txbd; /* Tx BD */
34 uint rxindex; /* index for next character to read */
Christophe Leroyba3da732017-07-06 10:33:13 +020035 uchar rxbuf[CONFIG_SYS_SMC_RXBUFLEN];/* rx buffers */
36 uchar txbuf; /* tx buffers */
37};
Christophe Leroy907208c2017-07-06 10:23:22 +020038
Christophe Leroybdfa11e2018-11-21 08:51:49 +000039static void serial_setdivisor(cpm8xx_t __iomem *cp, int baudrate)
Christophe Leroy907208c2017-07-06 10:23:22 +020040{
Christophe Leroybdfa11e2018-11-21 08:51:49 +000041 int divisor = (gd->cpu_clk + 8 * baudrate) / 16 / baudrate;
Christophe Leroy907208c2017-07-06 10:23:22 +020042
Christophe Leroy70fd0712017-07-06 10:33:17 +020043 if (divisor / 16 > 0x1000) {
Christophe Leroy907208c2017-07-06 10:23:22 +020044 /* bad divisor, assume 50MHz clock and 9600 baud */
Christophe Leroy70fd0712017-07-06 10:33:17 +020045 divisor = (50 * 1000 * 1000 + 8 * 9600) / 16 / 9600;
Christophe Leroy907208c2017-07-06 10:23:22 +020046 }
47
Christophe Leroy907208c2017-07-06 10:23:22 +020048 divisor /= CONFIG_SYS_BRGCLK_PRESCALE;
Christophe Leroy907208c2017-07-06 10:23:22 +020049
Christophe Leroyba3da732017-07-06 10:33:13 +020050 if (divisor <= 0x1000)
51 out_be32(&cp->cp_brgc1, ((divisor - 1) << 1) | CPM_BRG_EN);
52 else
53 out_be32(&cp->cp_brgc1, ((divisor / 16 - 1) << 1) | CPM_BRG_EN |
54 CPM_BRG_DIV16);
Christophe Leroy907208c2017-07-06 10:23:22 +020055}
56
57/*
58 * Minimal serial functions needed to use one of the SMC ports
59 * as serial console interface.
60 */
61
Christophe Leroy42b54012018-11-21 08:51:53 +000062static int serial_mpc8xx_setbrg(struct udevice *dev, int baudrate)
Christophe Leroy907208c2017-07-06 10:23:22 +020063{
Christophe Leroyba3da732017-07-06 10:33:13 +020064 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
65 cpm8xx_t __iomem *cp = &(im->im_cpm);
Christophe Leroy907208c2017-07-06 10:23:22 +020066
67 /* Set up the baud rate generator.
68 * See 8xx_io/commproc.c for details.
69 *
70 * Wire BRG1 to SMCx
71 */
72
Christophe Leroyba3da732017-07-06 10:33:13 +020073 out_be32(&cp->cp_simode, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +020074
Christophe Leroy42b54012018-11-21 08:51:53 +000075 serial_setdivisor(cp, baudrate);
76
77 return 0;
Christophe Leroy907208c2017-07-06 10:23:22 +020078}
79
Christophe Leroy42b54012018-11-21 08:51:53 +000080static int serial_mpc8xx_probe(struct udevice *dev)
Christophe Leroy907208c2017-07-06 10:23:22 +020081{
Christophe Leroyba3da732017-07-06 10:33:13 +020082 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
83 smc_t __iomem *sp;
84 smc_uart_t __iomem *up;
Christophe Leroy388cb1a2023-05-03 09:20:15 +020085 u16 smc_rpbase;
Christophe Leroyba3da732017-07-06 10:33:13 +020086 cpm8xx_t __iomem *cp = &(im->im_cpm);
87 struct serialbuffer __iomem *rtx;
Christophe Leroy907208c2017-07-06 10:23:22 +020088
89 /* initialize pointers to SMC */
90
Christophe Leroyba3da732017-07-06 10:33:13 +020091 sp = cp->cp_smc + SMC_INDEX;
Christophe Leroyfdd243d2023-05-03 10:31:19 +020092 up = (smc_uart_t __iomem *)&cp->cp_dpmem[PROFF_SMC];
Christophe Leroy388cb1a2023-05-03 09:20:15 +020093
94 smc_rpbase = in_be16(&up->smc_rpbase);
95 if (smc_rpbase)
96 up = (smc_uart_t __iomem *)&cp->cp_dpmem[smc_rpbase];
Christophe Leroy907208c2017-07-06 10:23:22 +020097
98 /* Disable transmitter/receiver. */
Christophe Leroyba3da732017-07-06 10:33:13 +020099 clrbits_be16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
Christophe Leroy907208c2017-07-06 10:23:22 +0200100
101 /* Enable SDMA. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200102 out_be32(&im->im_siu_conf.sc_sdcr, 1);
Christophe Leroy907208c2017-07-06 10:23:22 +0200103
104 /* clear error conditions */
Christophe Leroyba3da732017-07-06 10:33:13 +0200105 out_8(&im->im_sdma.sdma_sdsr, CONFIG_SYS_SDSR);
Christophe Leroy907208c2017-07-06 10:23:22 +0200106
107 /* clear SDMA interrupt mask */
Christophe Leroyba3da732017-07-06 10:33:13 +0200108 out_8(&im->im_sdma.sdma_sdmr, CONFIG_SYS_SDMR);
Christophe Leroy907208c2017-07-06 10:23:22 +0200109
Christophe Leroyba3da732017-07-06 10:33:13 +0200110 /* Use Port B for SMCx instead of other functions. */
111 setbits_be32(&cp->cp_pbpar, IOPINS);
112 clrbits_be32(&cp->cp_pbdir, IOPINS);
113 clrbits_be16(&cp->cp_pbodr, IOPINS);
Christophe Leroy907208c2017-07-06 10:23:22 +0200114
115 /* Set the physical address of the host memory buffers in
116 * the buffer descriptors.
117 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200118 rtx = (struct serialbuffer __iomem *)&cp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy907208c2017-07-06 10:23:22 +0200119 /* Allocate space for two buffer descriptors in the DP ram.
120 * For now, this address seems OK, but it may have to
121 * change with newer versions of the firmware.
122 * damm: allocating space after the two buffers for rx/tx data
123 */
124
Christophe Leroyba3da732017-07-06 10:33:13 +0200125 out_be32(&rtx->rxbd.cbd_bufaddr, (__force uint)&rtx->rxbuf);
126 out_be16(&rtx->rxbd.cbd_sc, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +0200127
Christophe Leroyba3da732017-07-06 10:33:13 +0200128 out_be32(&rtx->txbd.cbd_bufaddr, (__force uint)&rtx->txbuf);
129 out_be16(&rtx->txbd.cbd_sc, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +0200130
131 /* Set up the uart parameters in the parameter ram. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200132 out_be16(&up->smc_rbase, CPM_SERIAL_BASE);
133 out_be16(&up->smc_tbase, CPM_SERIAL_BASE + sizeof(cbd_t));
134 out_8(&up->smc_rfcr, SMC_EB);
135 out_8(&up->smc_tfcr, SMC_EB);
Christophe Leroy907208c2017-07-06 10:23:22 +0200136
137 /* Set UART mode, 8 bit, no parity, one stop.
138 * Enable receive and transmit.
139 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200140 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
Christophe Leroy907208c2017-07-06 10:23:22 +0200141
142 /* Mask all interrupts and remove anything pending.
143 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200144 out_8(&sp->smc_smcm, 0);
145 out_8(&sp->smc_smce, 0xff);
Christophe Leroy907208c2017-07-06 10:23:22 +0200146
147 /* Set up the baud rate generator */
Christophe Leroy42b54012018-11-21 08:51:53 +0000148 serial_mpc8xx_setbrg(dev, gd->baudrate);
Christophe Leroy907208c2017-07-06 10:23:22 +0200149
150 /* Make the first buffer the only buffer. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200151 setbits_be16(&rtx->txbd.cbd_sc, BD_SC_WRAP);
152 setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
Christophe Leroy907208c2017-07-06 10:23:22 +0200153
154 /* single/multi character receive. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200155 out_be16(&up->smc_mrblr, CONFIG_SYS_SMC_RXBUFLEN);
156 out_be16(&up->smc_maxidl, CONFIG_SYS_MAXIDLE);
157 out_be32(&rtx->rxindex, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +0200158
Christophe Leroy388cb1a2023-05-03 09:20:15 +0200159 out_be32(&up->smc_rstate, 0);
160 out_be32(&up->smc_tstate, 0);
161 out_be16(&up->smc_rbptr, CPM_SERIAL_BASE);
162 out_be16(&up->smc_tbptr, CPM_SERIAL_BASE + sizeof(cbd_t));
163 out_be16(&up->smc_brkcr, 1);
164 out_be16(&up->smc_brkec, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +0200165
166 /* Enable transmitter/receiver. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200167 setbits_be16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
Christophe Leroy907208c2017-07-06 10:23:22 +0200168
Christophe Leroy70fd0712017-07-06 10:33:17 +0200169 return 0;
Christophe Leroy907208c2017-07-06 10:23:22 +0200170}
171
Christophe Leroy42b54012018-11-21 08:51:53 +0000172static int serial_mpc8xx_putc(struct udevice *dev, const char c)
Christophe Leroy907208c2017-07-06 10:23:22 +0200173{
Christophe Leroyba3da732017-07-06 10:33:13 +0200174 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
175 cpm8xx_t __iomem *cpmp = &(im->im_cpm);
176 struct serialbuffer __iomem *rtx;
Christophe Leroy907208c2017-07-06 10:23:22 +0200177
Christophe Leroyba3da732017-07-06 10:33:13 +0200178 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy907208c2017-07-06 10:23:22 +0200179
Pali Rohár1138bbe2022-12-11 00:31:21 +0100180 if (in_be16(&rtx->txbd.cbd_sc) & BD_SC_READY)
181 return -EAGAIN;
182
Christophe Leroyba3da732017-07-06 10:33:13 +0200183 out_8(&rtx->txbuf, c);
184 out_be16(&rtx->txbd.cbd_datlen, 1);
185 setbits_be16(&rtx->txbd.cbd_sc, BD_SC_READY);
Christophe Leroy907208c2017-07-06 10:23:22 +0200186
Christophe Leroy42b54012018-11-21 08:51:53 +0000187 return 0;
Christophe Leroy907208c2017-07-06 10:23:22 +0200188}
189
Christophe Leroy42b54012018-11-21 08:51:53 +0000190static int serial_mpc8xx_getc(struct udevice *dev)
Christophe Leroy907208c2017-07-06 10:23:22 +0200191{
Christophe Leroyba3da732017-07-06 10:33:13 +0200192 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
193 cpm8xx_t __iomem *cpmp = &(im->im_cpm);
194 struct serialbuffer __iomem *rtx;
Christophe Leroy907208c2017-07-06 10:23:22 +0200195 unsigned char c;
Christophe Leroyba3da732017-07-06 10:33:13 +0200196 uint rxindex;
Christophe Leroy907208c2017-07-06 10:23:22 +0200197
Christophe Leroyba3da732017-07-06 10:33:13 +0200198 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy907208c2017-07-06 10:23:22 +0200199
Pali Rohár1138bbe2022-12-11 00:31:21 +0100200 if (in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY)
201 return -EAGAIN;
Christophe Leroy907208c2017-07-06 10:23:22 +0200202
203 /* the characters are read one by one,
204 * use the rxindex to know the next char to deliver
205 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200206 rxindex = in_be32(&rtx->rxindex);
207 c = in_8(rtx->rxbuf + rxindex);
208 rxindex++;
Christophe Leroy907208c2017-07-06 10:23:22 +0200209
210 /* check if all char are readout, then make prepare for next receive */
Christophe Leroyba3da732017-07-06 10:33:13 +0200211 if (rxindex >= in_be16(&rtx->rxbd.cbd_datlen)) {
212 rxindex = 0;
213 setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY);
Christophe Leroy907208c2017-07-06 10:23:22 +0200214 }
Christophe Leroyba3da732017-07-06 10:33:13 +0200215 out_be32(&rtx->rxindex, rxindex);
Christophe Leroy70fd0712017-07-06 10:33:17 +0200216 return c;
Christophe Leroy907208c2017-07-06 10:23:22 +0200217}
218
Christophe Leroy42b54012018-11-21 08:51:53 +0000219static int serial_mpc8xx_pending(struct udevice *dev, bool input)
Christophe Leroy907208c2017-07-06 10:23:22 +0200220{
Christophe Leroyba3da732017-07-06 10:33:13 +0200221 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
222 cpm8xx_t __iomem *cpmp = &(im->im_cpm);
223 struct serialbuffer __iomem *rtx;
Christophe Leroy907208c2017-07-06 10:23:22 +0200224
Christophe Leroy42b54012018-11-21 08:51:53 +0000225 if (!input)
226 return 0;
227
Christophe Leroyba3da732017-07-06 10:33:13 +0200228 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy907208c2017-07-06 10:23:22 +0200229
Christophe Leroyba3da732017-07-06 10:33:13 +0200230 return !(in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY);
Christophe Leroy907208c2017-07-06 10:23:22 +0200231}
232
Christophe Leroybdfa11e2018-11-21 08:51:49 +0000233static const struct dm_serial_ops serial_mpc8xx_ops = {
234 .putc = serial_mpc8xx_putc,
235 .pending = serial_mpc8xx_pending,
236 .getc = serial_mpc8xx_getc,
237 .setbrg = serial_mpc8xx_setbrg,
238};
239
240static const struct udevice_id serial_mpc8xx_ids[] = {
241 { .compatible = "fsl,pq1-smc" },
242 { }
243};
244
245U_BOOT_DRIVER(serial_mpc8xx) = {
246 .name = "serial_mpc8xx",
247 .id = UCLASS_SERIAL,
248 .of_match = serial_mpc8xx_ids,
249 .probe = serial_mpc8xx_probe,
250 .ops = &serial_mpc8xx_ops,
251 .flags = DM_FLAG_PRE_RELOC,
252};