blob: 50d6e70f17735c0af598ef24e2b9998b3c31dd56 [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
7#include <common.h>
Christophe Leroy907208c2017-07-06 10:23:22 +02008#include <command.h>
Christophe Leroybdfa11e2018-11-21 08:51:49 +00009#include <dm.h>
Christophe Leroy907208c2017-07-06 10:23:22 +020010#include <serial.h>
11#include <watchdog.h>
Christophe Leroy18f8d4c2018-03-16 17:20:43 +010012#include <asm/cpm_8xx.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;
85 cpm8xx_t __iomem *cp = &(im->im_cpm);
86 struct serialbuffer __iomem *rtx;
Christophe Leroy907208c2017-07-06 10:23:22 +020087
88 /* initialize pointers to SMC */
89
Christophe Leroyba3da732017-07-06 10:33:13 +020090 sp = cp->cp_smc + SMC_INDEX;
91 up = (smc_uart_t __iomem *)&cp->cp_dparam[PROFF_SMC];
Christophe Leroy907208c2017-07-06 10:23:22 +020092 /* Disable relocation */
Christophe Leroyba3da732017-07-06 10:33:13 +020093 out_be16(&up->smc_rpbase, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +020094
95 /* Disable transmitter/receiver. */
Christophe Leroyba3da732017-07-06 10:33:13 +020096 clrbits_be16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
Christophe Leroy907208c2017-07-06 10:23:22 +020097
98 /* Enable SDMA. */
Christophe Leroyba3da732017-07-06 10:33:13 +020099 out_be32(&im->im_siu_conf.sc_sdcr, 1);
Christophe Leroy907208c2017-07-06 10:23:22 +0200100
101 /* clear error conditions */
Christophe Leroyba3da732017-07-06 10:33:13 +0200102 out_8(&im->im_sdma.sdma_sdsr, CONFIG_SYS_SDSR);
Christophe Leroy907208c2017-07-06 10:23:22 +0200103
104 /* clear SDMA interrupt mask */
Christophe Leroyba3da732017-07-06 10:33:13 +0200105 out_8(&im->im_sdma.sdma_sdmr, CONFIG_SYS_SDMR);
Christophe Leroy907208c2017-07-06 10:23:22 +0200106
Christophe Leroyba3da732017-07-06 10:33:13 +0200107 /* Use Port B for SMCx instead of other functions. */
108 setbits_be32(&cp->cp_pbpar, IOPINS);
109 clrbits_be32(&cp->cp_pbdir, IOPINS);
110 clrbits_be16(&cp->cp_pbodr, IOPINS);
Christophe Leroy907208c2017-07-06 10:23:22 +0200111
112 /* Set the physical address of the host memory buffers in
113 * the buffer descriptors.
114 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200115 rtx = (struct serialbuffer __iomem *)&cp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy907208c2017-07-06 10:23:22 +0200116 /* Allocate space for two buffer descriptors in the DP ram.
117 * For now, this address seems OK, but it may have to
118 * change with newer versions of the firmware.
119 * damm: allocating space after the two buffers for rx/tx data
120 */
121
Christophe Leroyba3da732017-07-06 10:33:13 +0200122 out_be32(&rtx->rxbd.cbd_bufaddr, (__force uint)&rtx->rxbuf);
123 out_be16(&rtx->rxbd.cbd_sc, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +0200124
Christophe Leroyba3da732017-07-06 10:33:13 +0200125 out_be32(&rtx->txbd.cbd_bufaddr, (__force uint)&rtx->txbuf);
126 out_be16(&rtx->txbd.cbd_sc, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +0200127
128 /* Set up the uart parameters in the parameter ram. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200129 out_be16(&up->smc_rbase, CPM_SERIAL_BASE);
130 out_be16(&up->smc_tbase, CPM_SERIAL_BASE + sizeof(cbd_t));
131 out_8(&up->smc_rfcr, SMC_EB);
132 out_8(&up->smc_tfcr, SMC_EB);
Christophe Leroy907208c2017-07-06 10:23:22 +0200133
134 /* Set UART mode, 8 bit, no parity, one stop.
135 * Enable receive and transmit.
136 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200137 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
Christophe Leroy907208c2017-07-06 10:23:22 +0200138
139 /* Mask all interrupts and remove anything pending.
140 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200141 out_8(&sp->smc_smcm, 0);
142 out_8(&sp->smc_smce, 0xff);
Christophe Leroy907208c2017-07-06 10:23:22 +0200143
144 /* Set up the baud rate generator */
Christophe Leroy42b54012018-11-21 08:51:53 +0000145 serial_mpc8xx_setbrg(dev, gd->baudrate);
Christophe Leroy907208c2017-07-06 10:23:22 +0200146
147 /* Make the first buffer the only buffer. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200148 setbits_be16(&rtx->txbd.cbd_sc, BD_SC_WRAP);
149 setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
Christophe Leroy907208c2017-07-06 10:23:22 +0200150
151 /* single/multi character receive. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200152 out_be16(&up->smc_mrblr, CONFIG_SYS_SMC_RXBUFLEN);
153 out_be16(&up->smc_maxidl, CONFIG_SYS_MAXIDLE);
154 out_be32(&rtx->rxindex, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +0200155
156 /* Initialize Tx/Rx parameters. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200157 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG) /* wait if cp is busy */
158 ;
Christophe Leroy907208c2017-07-06 10:23:22 +0200159
Christophe Leroyba3da732017-07-06 10:33:13 +0200160 out_be16(&cp->cp_cpcr,
161 mk_cr_cmd(CPM_CR_CH_SMC, CPM_CR_INIT_TRX) | CPM_CR_FLG);
Christophe Leroy907208c2017-07-06 10:23:22 +0200162
Christophe Leroyba3da732017-07-06 10:33:13 +0200163 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG) /* wait if cp is busy */
164 ;
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
178 if (c == '\n')
Christophe Leroy42b54012018-11-21 08:51:53 +0000179 serial_mpc8xx_putc(dev, '\r');
Christophe Leroy907208c2017-07-06 10:23:22 +0200180
Christophe Leroyba3da732017-07-06 10:33:13 +0200181 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy907208c2017-07-06 10:23:22 +0200182
183 /* Wait for last character to go. */
Christophe Leroyba3da732017-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 Leroy907208c2017-07-06 10:23:22 +0200187
Christophe Leroyba3da732017-07-06 10:33:13 +0200188 while (in_be16(&rtx->txbd.cbd_sc) & BD_SC_READY)
Christophe Leroy70fd0712017-07-06 10:33:17 +0200189 WATCHDOG_RESET();
Christophe Leroy42b54012018-11-21 08:51:53 +0000190
191 return 0;
Christophe Leroy907208c2017-07-06 10:23:22 +0200192}
193
Christophe Leroy42b54012018-11-21 08:51:53 +0000194static int serial_mpc8xx_getc(struct udevice *dev)
Christophe Leroy907208c2017-07-06 10:23:22 +0200195{
Christophe Leroyba3da732017-07-06 10:33:13 +0200196 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
197 cpm8xx_t __iomem *cpmp = &(im->im_cpm);
198 struct serialbuffer __iomem *rtx;
Christophe Leroy907208c2017-07-06 10:23:22 +0200199 unsigned char c;
Christophe Leroyba3da732017-07-06 10:33:13 +0200200 uint rxindex;
Christophe Leroy907208c2017-07-06 10:23:22 +0200201
Christophe Leroyba3da732017-07-06 10:33:13 +0200202 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy907208c2017-07-06 10:23:22 +0200203
204 /* Wait for character to show up. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200205 while (in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY)
Christophe Leroy70fd0712017-07-06 10:33:17 +0200206 WATCHDOG_RESET();
Christophe Leroy907208c2017-07-06 10:23:22 +0200207
208 /* the characters are read one by one,
209 * use the rxindex to know the next char to deliver
210 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200211 rxindex = in_be32(&rtx->rxindex);
212 c = in_8(rtx->rxbuf + rxindex);
213 rxindex++;
Christophe Leroy907208c2017-07-06 10:23:22 +0200214
215 /* check if all char are readout, then make prepare for next receive */
Christophe Leroyba3da732017-07-06 10:33:13 +0200216 if (rxindex >= in_be16(&rtx->rxbd.cbd_datlen)) {
217 rxindex = 0;
218 setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY);
Christophe Leroy907208c2017-07-06 10:23:22 +0200219 }
Christophe Leroyba3da732017-07-06 10:33:13 +0200220 out_be32(&rtx->rxindex, rxindex);
Christophe Leroy70fd0712017-07-06 10:33:17 +0200221 return c;
Christophe Leroy907208c2017-07-06 10:23:22 +0200222}
223
Christophe Leroy42b54012018-11-21 08:51:53 +0000224static int serial_mpc8xx_pending(struct udevice *dev, bool input)
Christophe Leroy907208c2017-07-06 10:23:22 +0200225{
Christophe Leroyba3da732017-07-06 10:33:13 +0200226 immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
227 cpm8xx_t __iomem *cpmp = &(im->im_cpm);
228 struct serialbuffer __iomem *rtx;
Christophe Leroy907208c2017-07-06 10:23:22 +0200229
Christophe Leroy42b54012018-11-21 08:51:53 +0000230 if (!input)
231 return 0;
232
Christophe Leroyba3da732017-07-06 10:33:13 +0200233 rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
Christophe Leroy907208c2017-07-06 10:23:22 +0200234
Christophe Leroyba3da732017-07-06 10:33:13 +0200235 return !(in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY);
Christophe Leroy907208c2017-07-06 10:23:22 +0200236}
237
Christophe Leroybdfa11e2018-11-21 08:51:49 +0000238static const struct dm_serial_ops serial_mpc8xx_ops = {
239 .putc = serial_mpc8xx_putc,
240 .pending = serial_mpc8xx_pending,
241 .getc = serial_mpc8xx_getc,
242 .setbrg = serial_mpc8xx_setbrg,
243};
244
245static const struct udevice_id serial_mpc8xx_ids[] = {
246 { .compatible = "fsl,pq1-smc" },
247 { }
248};
249
250U_BOOT_DRIVER(serial_mpc8xx) = {
251 .name = "serial_mpc8xx",
252 .id = UCLASS_SERIAL,
253 .of_match = serial_mpc8xx_ids,
254 .probe = serial_mpc8xx_probe,
255 .ops = &serial_mpc8xx_ops,
256 .flags = DM_FLAG_PRE_RELOC,
257};