blob: 114dfe9b4b5733e5d87e181b4d4ab7010de1f2e9 [file] [log] [blame]
Christophe Leroy069fa832017-07-06 10:23:22 +02001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <commproc.h>
10#include <command.h>
11#include <serial.h>
12#include <watchdog.h>
13#include <linux/compiler.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17#if !defined(CONFIG_8xx_CONS_NONE) /* No Console at all */
18
19#if defined(CONFIG_8xx_CONS_SMC1) /* Console on SMC1 */
20#define SMC_INDEX 0
21#define PROFF_SMC PROFF_SMC1
22#define CPM_CR_CH_SMC CPM_CR_CH_SMC1
Christophe Leroy394f9b32017-07-06 10:33:13 +020023#define IOPINS 0xc0
Christophe Leroy069fa832017-07-06 10:23:22 +020024
25#elif defined(CONFIG_8xx_CONS_SMC2) /* Console on SMC2 */
26#define SMC_INDEX 1
27#define PROFF_SMC PROFF_SMC2
28#define CPM_CR_CH_SMC CPM_CR_CH_SMC2
Christophe Leroy394f9b32017-07-06 10:33:13 +020029#define IOPINS 0xc00
Christophe Leroy069fa832017-07-06 10:23:22 +020030
31#endif /* CONFIG_8xx_CONS_SMCx */
32
33#if !defined(CONFIG_SYS_SMC_RXBUFLEN)
34#define CONFIG_SYS_SMC_RXBUFLEN 1
35#define CONFIG_SYS_MAXIDLE 0
36#else
37#if !defined(CONFIG_SYS_MAXIDLE)
38#error "you must define CONFIG_SYS_MAXIDLE"
39#endif
40#endif
41
Christophe Leroy394f9b32017-07-06 10:33:13 +020042struct serialbuffer {
Christophe Leroy069fa832017-07-06 10:23:22 +020043 cbd_t rxbd; /* Rx BD */
44 cbd_t txbd; /* Tx BD */
45 uint rxindex; /* index for next character to read */
Christophe Leroy394f9b32017-07-06 10:33:13 +020046 uchar rxbuf[CONFIG_SYS_SMC_RXBUFLEN];/* rx buffers */
47 uchar txbuf; /* tx buffers */
48};
Christophe Leroy069fa832017-07-06 10:23:22 +020049
Christophe Leroy394f9b32017-07-06 10:33:13 +020050static void serial_setdivisor(cpm8xx_t __iomem *cp)
Christophe Leroy069fa832017-07-06 10:23:22 +020051{
Christophe Leroy48f896d2017-07-06 10:33:17 +020052 int divisor = (gd->cpu_clk + 8 * gd->baudrate) / 16 / gd->baudrate;
Christophe Leroy069fa832017-07-06 10:23:22 +020053
Christophe Leroy48f896d2017-07-06 10:33:17 +020054 if (divisor / 16 > 0x1000) {
Christophe Leroy069fa832017-07-06 10:23:22 +020055 /* bad divisor, assume 50MHz clock and 9600 baud */
Christophe Leroy48f896d2017-07-06 10:33:17 +020056 divisor = (50 * 1000 * 1000 + 8 * 9600) / 16 / 9600;
Christophe Leroy069fa832017-07-06 10:23:22 +020057 }
58
59#ifdef CONFIG_SYS_BRGCLK_PRESCALE
60 divisor /= CONFIG_SYS_BRGCLK_PRESCALE;
61#endif
62
Christophe Leroy394f9b32017-07-06 10:33:13 +020063 if (divisor <= 0x1000)
64 out_be32(&cp->cp_brgc1, ((divisor - 1) << 1) | CPM_BRG_EN);
65 else
66 out_be32(&cp->cp_brgc1, ((divisor / 16 - 1) << 1) | CPM_BRG_EN |
67 CPM_BRG_DIV16);
Christophe Leroy069fa832017-07-06 10:23:22 +020068}
69
70/*
71 * Minimal serial functions needed to use one of the SMC ports
72 * as serial console interface.
73 */
74
Christophe Leroy48f896d2017-07-06 10:33:17 +020075static void smc_setbrg(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020076{
Christophe Leroy394f9b32017-07-06 10:33:13 +020077 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
78 cpm8xx_t __iomem *cp = &(im->im_cpm);
Christophe Leroy069fa832017-07-06 10:23:22 +020079
80 /* Set up the baud rate generator.
81 * See 8xx_io/commproc.c for details.
82 *
83 * Wire BRG1 to SMCx
84 */
85
Christophe Leroy394f9b32017-07-06 10:33:13 +020086 out_be32(&cp->cp_simode, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +020087
88 serial_setdivisor(cp);
89}
90
Christophe Leroy48f896d2017-07-06 10:33:17 +020091static int smc_init(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020092{
Christophe Leroy394f9b32017-07-06 10:33:13 +020093 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
94 smc_t __iomem *sp;
95 smc_uart_t __iomem *up;
96 cpm8xx_t __iomem *cp = &(im->im_cpm);
97 struct serialbuffer __iomem *rtx;
Christophe Leroy069fa832017-07-06 10:23:22 +020098
99 /* initialize pointers to SMC */
100
Christophe Leroy394f9b32017-07-06 10:33:13 +0200101 sp = cp->cp_smc + SMC_INDEX;
102 up = (smc_uart_t __iomem *)&cp->cp_dparam[PROFF_SMC];
Christophe Leroy069fa832017-07-06 10:23:22 +0200103 /* Disable relocation */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200104 out_be16(&up->smc_rpbase, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200105
106 /* Disable transmitter/receiver. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200107 clrbits_be16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
Christophe Leroy069fa832017-07-06 10:23:22 +0200108
109 /* Enable SDMA. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200110 out_be32(&im->im_siu_conf.sc_sdcr, 1);
Christophe Leroy069fa832017-07-06 10:23:22 +0200111
112 /* clear error conditions */
113#ifdef CONFIG_SYS_SDSR
Christophe Leroy394f9b32017-07-06 10:33:13 +0200114 out_8(&im->im_sdma.sdma_sdsr, CONFIG_SYS_SDSR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200115#else
Christophe Leroy394f9b32017-07-06 10:33:13 +0200116 out_8(&im->im_sdma.sdma_sdsr, 0x83);
Christophe Leroy069fa832017-07-06 10:23:22 +0200117#endif
118
119 /* clear SDMA interrupt mask */
120#ifdef CONFIG_SYS_SDMR
Christophe Leroy394f9b32017-07-06 10:33:13 +0200121 out_8(&im->im_sdma.sdma_sdmr, CONFIG_SYS_SDMR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200122#else
Christophe Leroy394f9b32017-07-06 10:33:13 +0200123 out_8(&im->im_sdma.sdma_sdmr, 0x00);
Christophe Leroy069fa832017-07-06 10:23:22 +0200124#endif
125
Christophe Leroy394f9b32017-07-06 10:33:13 +0200126 /* Use Port B for SMCx instead of other functions. */
127 setbits_be32(&cp->cp_pbpar, IOPINS);
128 clrbits_be32(&cp->cp_pbdir, IOPINS);
129 clrbits_be16(&cp->cp_pbodr, IOPINS);
Christophe Leroy069fa832017-07-06 10:23:22 +0200130
131 /* Set the physical address of the host memory buffers in
132 * the buffer descriptors.
133 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200134 rtx = (struct serialbuffer __iomem *)&cp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy069fa832017-07-06 10:23:22 +0200135 /* Allocate space for two buffer descriptors in the DP ram.
136 * For now, this address seems OK, but it may have to
137 * change with newer versions of the firmware.
138 * damm: allocating space after the two buffers for rx/tx data
139 */
140
Christophe Leroy394f9b32017-07-06 10:33:13 +0200141 out_be32(&rtx->rxbd.cbd_bufaddr, (__force uint)&rtx->rxbuf);
142 out_be16(&rtx->rxbd.cbd_sc, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200143
Christophe Leroy394f9b32017-07-06 10:33:13 +0200144 out_be32(&rtx->txbd.cbd_bufaddr, (__force uint)&rtx->txbuf);
145 out_be16(&rtx->txbd.cbd_sc, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200146
147 /* Set up the uart parameters in the parameter ram. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200148 out_be16(&up->smc_rbase, CPM_SERIAL_BASE);
149 out_be16(&up->smc_tbase, CPM_SERIAL_BASE + sizeof(cbd_t));
150 out_8(&up->smc_rfcr, SMC_EB);
151 out_8(&up->smc_tfcr, SMC_EB);
Christophe Leroy069fa832017-07-06 10:23:22 +0200152
153 /* Set UART mode, 8 bit, no parity, one stop.
154 * Enable receive and transmit.
155 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200156 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
Christophe Leroy069fa832017-07-06 10:23:22 +0200157
158 /* Mask all interrupts and remove anything pending.
159 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200160 out_8(&sp->smc_smcm, 0);
161 out_8(&sp->smc_smce, 0xff);
Christophe Leroy069fa832017-07-06 10:23:22 +0200162
163 /* Set up the baud rate generator */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200164 smc_setbrg();
Christophe Leroy069fa832017-07-06 10:23:22 +0200165
166 /* Make the first buffer the only buffer. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200167 setbits_be16(&rtx->txbd.cbd_sc, BD_SC_WRAP);
168 setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
Christophe Leroy069fa832017-07-06 10:23:22 +0200169
170 /* single/multi character receive. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200171 out_be16(&up->smc_mrblr, CONFIG_SYS_SMC_RXBUFLEN);
172 out_be16(&up->smc_maxidl, CONFIG_SYS_MAXIDLE);
173 out_be32(&rtx->rxindex, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200174
175 /* Initialize Tx/Rx parameters. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200176 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG) /* wait if cp is busy */
177 ;
Christophe Leroy069fa832017-07-06 10:23:22 +0200178
Christophe Leroy394f9b32017-07-06 10:33:13 +0200179 out_be16(&cp->cp_cpcr,
180 mk_cr_cmd(CPM_CR_CH_SMC, CPM_CR_INIT_TRX) | CPM_CR_FLG);
Christophe Leroy069fa832017-07-06 10:23:22 +0200181
Christophe Leroy394f9b32017-07-06 10:33:13 +0200182 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG) /* wait if cp is busy */
183 ;
Christophe Leroy069fa832017-07-06 10:23:22 +0200184
185 /* Enable transmitter/receiver. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200186 setbits_be16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
Christophe Leroy069fa832017-07-06 10:23:22 +0200187
Christophe Leroy48f896d2017-07-06 10:33:17 +0200188 return 0;
Christophe Leroy069fa832017-07-06 10:23:22 +0200189}
190
Christophe Leroy48f896d2017-07-06 10:33:17 +0200191static void smc_putc(const char c)
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
197 if (c == '\n')
Christophe Leroy48f896d2017-07-06 10:33:17 +0200198 smc_putc('\r');
Christophe Leroy069fa832017-07-06 10:23:22 +0200199
Christophe Leroy394f9b32017-07-06 10:33:13 +0200200 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy069fa832017-07-06 10:23:22 +0200201
202 /* Wait for last character to go. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200203 out_8(&rtx->txbuf, c);
204 out_be16(&rtx->txbd.cbd_datlen, 1);
205 setbits_be16(&rtx->txbd.cbd_sc, BD_SC_READY);
Christophe Leroy069fa832017-07-06 10:23:22 +0200206
Christophe Leroy394f9b32017-07-06 10:33:13 +0200207 while (in_be16(&rtx->txbd.cbd_sc) & BD_SC_READY)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200208 WATCHDOG_RESET();
Christophe Leroy069fa832017-07-06 10:23:22 +0200209}
210
Christophe Leroy48f896d2017-07-06 10:33:17 +0200211static void smc_puts(const char *s)
Christophe Leroy069fa832017-07-06 10:23:22 +0200212{
Christophe Leroy48f896d2017-07-06 10:33:17 +0200213 while (*s)
214 smc_putc(*s++);
Christophe Leroy069fa832017-07-06 10:23:22 +0200215}
216
Christophe Leroy48f896d2017-07-06 10:33:17 +0200217static int smc_getc(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200218{
Christophe Leroy394f9b32017-07-06 10:33:13 +0200219 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
220 cpm8xx_t __iomem *cpmp = &(im->im_cpm);
221 struct serialbuffer __iomem *rtx;
Christophe Leroy069fa832017-07-06 10:23:22 +0200222 unsigned char c;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200223 uint rxindex;
Christophe Leroy069fa832017-07-06 10:23:22 +0200224
Christophe Leroy394f9b32017-07-06 10:33:13 +0200225 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy069fa832017-07-06 10:23:22 +0200226
227 /* Wait for character to show up. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200228 while (in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200229 WATCHDOG_RESET();
Christophe Leroy069fa832017-07-06 10:23:22 +0200230
231 /* the characters are read one by one,
232 * use the rxindex to know the next char to deliver
233 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200234 rxindex = in_be32(&rtx->rxindex);
235 c = in_8(rtx->rxbuf + rxindex);
236 rxindex++;
Christophe Leroy069fa832017-07-06 10:23:22 +0200237
238 /* check if all char are readout, then make prepare for next receive */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200239 if (rxindex >= in_be16(&rtx->rxbd.cbd_datlen)) {
240 rxindex = 0;
241 setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY);
Christophe Leroy069fa832017-07-06 10:23:22 +0200242 }
Christophe Leroy394f9b32017-07-06 10:33:13 +0200243 out_be32(&rtx->rxindex, rxindex);
Christophe Leroy48f896d2017-07-06 10:33:17 +0200244 return c;
Christophe Leroy069fa832017-07-06 10:23:22 +0200245}
246
Christophe Leroy48f896d2017-07-06 10:33:17 +0200247static int smc_tstc(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200248{
Christophe Leroy394f9b32017-07-06 10:33:13 +0200249 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
250 cpm8xx_t __iomem *cpmp = &(im->im_cpm);
251 struct serialbuffer __iomem *rtx;
Christophe Leroy069fa832017-07-06 10:23:22 +0200252
Christophe Leroy394f9b32017-07-06 10:33:13 +0200253 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy069fa832017-07-06 10:23:22 +0200254
Christophe Leroy394f9b32017-07-06 10:33:13 +0200255 return !(in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY);
Christophe Leroy069fa832017-07-06 10:23:22 +0200256}
257
Christophe Leroy48f896d2017-07-06 10:33:17 +0200258struct serial_device serial_smc_device = {
Christophe Leroy069fa832017-07-06 10:23:22 +0200259 .name = "serial_smc",
260 .start = smc_init,
261 .stop = NULL,
262 .setbrg = smc_setbrg,
263 .getc = smc_getc,
264 .tstc = smc_tstc,
265 .putc = smc_putc,
266 .puts = smc_puts,
267};
268
269__weak struct serial_device *default_serial_console(void)
270{
271 return &serial_smc_device;
272}
273
274void mpc8xx_serial_initialize(void)
275{
276 serial_register(&serial_smc_device);
277}
278
279#endif /* CONFIG_8xx_CONS_NONE */