blob: d82760c7f10adee337ee3bff7b8f93969be09799 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Christophe Leroy069fa832017-07-06 10:23:22 +02002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Christophe Leroy069fa832017-07-06 10:23:22 +02005 */
6
7#include <common.h>
Christophe Leroy069fa832017-07-06 10:23:22 +02008#include <command.h>
Christophe Leroy12bbc0f2018-11-21 08:51:49 +00009#include <dm.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020010#include <serial.h>
11#include <watchdog.h>
Christophe Leroy10ff63a2018-03-16 17:20:43 +010012#include <asm/cpm_8xx.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020014#include <linux/compiler.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
Christophe Leroy069fa832017-07-06 10:23:22 +020018#if defined(CONFIG_8xx_CONS_SMC1) /* Console on SMC1 */
19#define SMC_INDEX 0
20#define PROFF_SMC PROFF_SMC1
21#define CPM_CR_CH_SMC CPM_CR_CH_SMC1
Christophe Leroy394f9b32017-07-06 10:33:13 +020022#define IOPINS 0xc0
Christophe Leroy069fa832017-07-06 10:23:22 +020023
24#elif defined(CONFIG_8xx_CONS_SMC2) /* Console on SMC2 */
25#define SMC_INDEX 1
26#define PROFF_SMC PROFF_SMC2
27#define CPM_CR_CH_SMC CPM_CR_CH_SMC2
Christophe Leroy394f9b32017-07-06 10:33:13 +020028#define IOPINS 0xc00
Christophe Leroy069fa832017-07-06 10:23:22 +020029
30#endif /* CONFIG_8xx_CONS_SMCx */
31
Christophe Leroy394f9b32017-07-06 10:33:13 +020032struct serialbuffer {
Christophe Leroy069fa832017-07-06 10:23:22 +020033 cbd_t rxbd; /* Rx BD */
34 cbd_t txbd; /* Tx BD */
35 uint rxindex; /* index for next character to read */
Christophe Leroy394f9b32017-07-06 10:33:13 +020036 uchar rxbuf[CONFIG_SYS_SMC_RXBUFLEN];/* rx buffers */
37 uchar txbuf; /* tx buffers */
38};
Christophe Leroy069fa832017-07-06 10:23:22 +020039
Christophe Leroy12bbc0f2018-11-21 08:51:49 +000040static void serial_setdivisor(cpm8xx_t __iomem *cp, int baudrate)
Christophe Leroy069fa832017-07-06 10:23:22 +020041{
Christophe Leroy12bbc0f2018-11-21 08:51:49 +000042 int divisor = (gd->cpu_clk + 8 * baudrate) / 16 / baudrate;
Christophe Leroy069fa832017-07-06 10:23:22 +020043
Christophe Leroy48f896d2017-07-06 10:33:17 +020044 if (divisor / 16 > 0x1000) {
Christophe Leroy069fa832017-07-06 10:23:22 +020045 /* bad divisor, assume 50MHz clock and 9600 baud */
Christophe Leroy48f896d2017-07-06 10:33:17 +020046 divisor = (50 * 1000 * 1000 + 8 * 9600) / 16 / 9600;
Christophe Leroy069fa832017-07-06 10:23:22 +020047 }
48
Christophe Leroy069fa832017-07-06 10:23:22 +020049 divisor /= CONFIG_SYS_BRGCLK_PRESCALE;
Christophe Leroy069fa832017-07-06 10:23:22 +020050
Christophe Leroy394f9b32017-07-06 10:33:13 +020051 if (divisor <= 0x1000)
52 out_be32(&cp->cp_brgc1, ((divisor - 1) << 1) | CPM_BRG_EN);
53 else
54 out_be32(&cp->cp_brgc1, ((divisor / 16 - 1) << 1) | CPM_BRG_EN |
55 CPM_BRG_DIV16);
Christophe Leroy069fa832017-07-06 10:23:22 +020056}
57
58/*
59 * Minimal serial functions needed to use one of the SMC ports
60 * as serial console interface.
61 */
62
Christophe Leroye8800e12018-11-21 08:51:53 +000063static int serial_mpc8xx_setbrg(struct udevice *dev, int baudrate)
Christophe Leroy069fa832017-07-06 10:23:22 +020064{
Christophe Leroy394f9b32017-07-06 10:33:13 +020065 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
66 cpm8xx_t __iomem *cp = &(im->im_cpm);
Christophe Leroy069fa832017-07-06 10:23:22 +020067
68 /* Set up the baud rate generator.
69 * See 8xx_io/commproc.c for details.
70 *
71 * Wire BRG1 to SMCx
72 */
73
Christophe Leroy394f9b32017-07-06 10:33:13 +020074 out_be32(&cp->cp_simode, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +020075
Christophe Leroye8800e12018-11-21 08:51:53 +000076 serial_setdivisor(cp, baudrate);
77
78 return 0;
Christophe Leroy069fa832017-07-06 10:23:22 +020079}
80
Christophe Leroye8800e12018-11-21 08:51:53 +000081static int serial_mpc8xx_probe(struct udevice *dev)
Christophe Leroy069fa832017-07-06 10:23:22 +020082{
Christophe Leroy394f9b32017-07-06 10:33:13 +020083 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
84 smc_t __iomem *sp;
85 smc_uart_t __iomem *up;
Christophe Leroyf3bc7fc2023-05-03 09:20:15 +020086 u16 smc_rpbase;
Christophe Leroy394f9b32017-07-06 10:33:13 +020087 cpm8xx_t __iomem *cp = &(im->im_cpm);
88 struct serialbuffer __iomem *rtx;
Christophe Leroy069fa832017-07-06 10:23:22 +020089
90 /* initialize pointers to SMC */
91
Christophe Leroy394f9b32017-07-06 10:33:13 +020092 sp = cp->cp_smc + SMC_INDEX;
Christophe Leroye6050cc2023-05-03 10:31:19 +020093 up = (smc_uart_t __iomem *)&cp->cp_dpmem[PROFF_SMC];
Christophe Leroyf3bc7fc2023-05-03 09:20:15 +020094
95 smc_rpbase = in_be16(&up->smc_rpbase);
96 if (smc_rpbase)
97 up = (smc_uart_t __iomem *)&cp->cp_dpmem[smc_rpbase];
Christophe Leroy069fa832017-07-06 10:23:22 +020098
99 /* Disable transmitter/receiver. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200100 clrbits_be16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
Christophe Leroy069fa832017-07-06 10:23:22 +0200101
102 /* Enable SDMA. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200103 out_be32(&im->im_siu_conf.sc_sdcr, 1);
Christophe Leroy069fa832017-07-06 10:23:22 +0200104
105 /* clear error conditions */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200106 out_8(&im->im_sdma.sdma_sdsr, CONFIG_SYS_SDSR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200107
108 /* clear SDMA interrupt mask */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200109 out_8(&im->im_sdma.sdma_sdmr, CONFIG_SYS_SDMR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200110
Christophe Leroy394f9b32017-07-06 10:33:13 +0200111 /* Use Port B for SMCx instead of other functions. */
112 setbits_be32(&cp->cp_pbpar, IOPINS);
113 clrbits_be32(&cp->cp_pbdir, IOPINS);
114 clrbits_be16(&cp->cp_pbodr, IOPINS);
Christophe Leroy069fa832017-07-06 10:23:22 +0200115
116 /* Set the physical address of the host memory buffers in
117 * the buffer descriptors.
118 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200119 rtx = (struct serialbuffer __iomem *)&cp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy069fa832017-07-06 10:23:22 +0200120 /* Allocate space for two buffer descriptors in the DP ram.
121 * For now, this address seems OK, but it may have to
122 * change with newer versions of the firmware.
123 * damm: allocating space after the two buffers for rx/tx data
124 */
125
Christophe Leroy394f9b32017-07-06 10:33:13 +0200126 out_be32(&rtx->rxbd.cbd_bufaddr, (__force uint)&rtx->rxbuf);
127 out_be16(&rtx->rxbd.cbd_sc, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200128
Christophe Leroy394f9b32017-07-06 10:33:13 +0200129 out_be32(&rtx->txbd.cbd_bufaddr, (__force uint)&rtx->txbuf);
130 out_be16(&rtx->txbd.cbd_sc, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200131
132 /* Set up the uart parameters in the parameter ram. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200133 out_be16(&up->smc_rbase, CPM_SERIAL_BASE);
134 out_be16(&up->smc_tbase, CPM_SERIAL_BASE + sizeof(cbd_t));
135 out_8(&up->smc_rfcr, SMC_EB);
136 out_8(&up->smc_tfcr, SMC_EB);
Christophe Leroy069fa832017-07-06 10:23:22 +0200137
138 /* Set UART mode, 8 bit, no parity, one stop.
139 * Enable receive and transmit.
140 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200141 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
Christophe Leroy069fa832017-07-06 10:23:22 +0200142
143 /* Mask all interrupts and remove anything pending.
144 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200145 out_8(&sp->smc_smcm, 0);
146 out_8(&sp->smc_smce, 0xff);
Christophe Leroy069fa832017-07-06 10:23:22 +0200147
148 /* Set up the baud rate generator */
Christophe Leroye8800e12018-11-21 08:51:53 +0000149 serial_mpc8xx_setbrg(dev, gd->baudrate);
Christophe Leroy069fa832017-07-06 10:23:22 +0200150
151 /* Make the first buffer the only buffer. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200152 setbits_be16(&rtx->txbd.cbd_sc, BD_SC_WRAP);
153 setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
Christophe Leroy069fa832017-07-06 10:23:22 +0200154
155 /* single/multi character receive. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200156 out_be16(&up->smc_mrblr, CONFIG_SYS_SMC_RXBUFLEN);
157 out_be16(&up->smc_maxidl, CONFIG_SYS_MAXIDLE);
158 out_be32(&rtx->rxindex, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200159
Christophe Leroyf3bc7fc2023-05-03 09:20:15 +0200160 out_be32(&up->smc_rstate, 0);
161 out_be32(&up->smc_tstate, 0);
162 out_be16(&up->smc_rbptr, CPM_SERIAL_BASE);
163 out_be16(&up->smc_tbptr, CPM_SERIAL_BASE + sizeof(cbd_t));
164 out_be16(&up->smc_brkcr, 1);
165 out_be16(&up->smc_brkec, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200166
167 /* Enable transmitter/receiver. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200168 setbits_be16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
Christophe Leroy069fa832017-07-06 10:23:22 +0200169
Christophe Leroy48f896d2017-07-06 10:33:17 +0200170 return 0;
Christophe Leroy069fa832017-07-06 10:23:22 +0200171}
172
Christophe Leroye8800e12018-11-21 08:51:53 +0000173static int serial_mpc8xx_putc(struct udevice *dev, const char c)
Christophe Leroy069fa832017-07-06 10:23:22 +0200174{
Christophe Leroy394f9b32017-07-06 10:33:13 +0200175 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
176 cpm8xx_t __iomem *cpmp = &(im->im_cpm);
177 struct serialbuffer __iomem *rtx;
Christophe Leroy069fa832017-07-06 10:23:22 +0200178
Christophe Leroy394f9b32017-07-06 10:33:13 +0200179 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy069fa832017-07-06 10:23:22 +0200180
Pali Rohár241f12d2022-12-11 00:31:21 +0100181 if (in_be16(&rtx->txbd.cbd_sc) & BD_SC_READY)
182 return -EAGAIN;
183
Christophe Leroy394f9b32017-07-06 10:33:13 +0200184 out_8(&rtx->txbuf, c);
185 out_be16(&rtx->txbd.cbd_datlen, 1);
186 setbits_be16(&rtx->txbd.cbd_sc, BD_SC_READY);
Christophe Leroy069fa832017-07-06 10:23:22 +0200187
Christophe Leroye8800e12018-11-21 08:51:53 +0000188 return 0;
Christophe Leroy069fa832017-07-06 10:23:22 +0200189}
190
Christophe Leroye8800e12018-11-21 08:51:53 +0000191static int serial_mpc8xx_getc(struct udevice *dev)
Christophe Leroy069fa832017-07-06 10:23:22 +0200192{
Christophe Leroy394f9b32017-07-06 10:33:13 +0200193 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
194 cpm8xx_t __iomem *cpmp = &(im->im_cpm);
195 struct serialbuffer __iomem *rtx;
Christophe Leroy069fa832017-07-06 10:23:22 +0200196 unsigned char c;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200197 uint rxindex;
Christophe Leroy069fa832017-07-06 10:23:22 +0200198
Christophe Leroy394f9b32017-07-06 10:33:13 +0200199 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy069fa832017-07-06 10:23:22 +0200200
Pali Rohár241f12d2022-12-11 00:31:21 +0100201 if (in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY)
202 return -EAGAIN;
Christophe Leroy069fa832017-07-06 10:23:22 +0200203
204 /* the characters are read one by one,
205 * use the rxindex to know the next char to deliver
206 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200207 rxindex = in_be32(&rtx->rxindex);
208 c = in_8(rtx->rxbuf + rxindex);
209 rxindex++;
Christophe Leroy069fa832017-07-06 10:23:22 +0200210
211 /* check if all char are readout, then make prepare for next receive */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200212 if (rxindex >= in_be16(&rtx->rxbd.cbd_datlen)) {
213 rxindex = 0;
214 setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY);
Christophe Leroy069fa832017-07-06 10:23:22 +0200215 }
Christophe Leroy394f9b32017-07-06 10:33:13 +0200216 out_be32(&rtx->rxindex, rxindex);
Christophe Leroy48f896d2017-07-06 10:33:17 +0200217 return c;
Christophe Leroy069fa832017-07-06 10:23:22 +0200218}
219
Christophe Leroye8800e12018-11-21 08:51:53 +0000220static int serial_mpc8xx_pending(struct udevice *dev, bool input)
Christophe Leroy069fa832017-07-06 10:23:22 +0200221{
Christophe Leroy394f9b32017-07-06 10:33:13 +0200222 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
223 cpm8xx_t __iomem *cpmp = &(im->im_cpm);
224 struct serialbuffer __iomem *rtx;
Christophe Leroy069fa832017-07-06 10:23:22 +0200225
Christophe Leroye8800e12018-11-21 08:51:53 +0000226 if (!input)
227 return 0;
228
Christophe Leroy394f9b32017-07-06 10:33:13 +0200229 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy069fa832017-07-06 10:23:22 +0200230
Christophe Leroy394f9b32017-07-06 10:33:13 +0200231 return !(in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY);
Christophe Leroy069fa832017-07-06 10:23:22 +0200232}
233
Christophe Leroy12bbc0f2018-11-21 08:51:49 +0000234static const struct dm_serial_ops serial_mpc8xx_ops = {
235 .putc = serial_mpc8xx_putc,
236 .pending = serial_mpc8xx_pending,
237 .getc = serial_mpc8xx_getc,
238 .setbrg = serial_mpc8xx_setbrg,
239};
240
241static const struct udevice_id serial_mpc8xx_ids[] = {
242 { .compatible = "fsl,pq1-smc" },
243 { }
244};
245
246U_BOOT_DRIVER(serial_mpc8xx) = {
247 .name = "serial_mpc8xx",
248 .id = UCLASS_SERIAL,
249 .of_match = serial_mpc8xx_ids,
250 .probe = serial_mpc8xx_probe,
251 .ops = &serial_mpc8xx_ops,
252 .flags = DM_FLAG_PRE_RELOC,
253};