Merge tag '2020-01-20-ti-2020.04' of https://gitlab.denx.de/u-boot/custodians/u-boot-ti
[platform/kernel/u-boot.git] / drivers / serial / ns16550.c
1 /*
2  * COM1 NS16550 support
3  * originally from linux source (arch/powerpc/boot/ns16550.c)
4  * modified to use CONFIG_SYS_ISA_MEM and new defines
5  */
6
7 #include <clock_legacy.h>
8 #include <common.h>
9 #include <clk.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <ns16550.h>
13 #include <reset.h>
14 #include <serial.h>
15 #include <watchdog.h>
16 #include <linux/types.h>
17 #include <asm/io.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 #define UART_LCRVAL UART_LCR_8N1                /* 8 data, 1 stop, no parity */
22 #define UART_MCRVAL (UART_MCR_DTR | \
23                      UART_MCR_RTS)              /* RTS/DTR */
24
25 #if !CONFIG_IS_ENABLED(DM_SERIAL)
26 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
27 #define serial_out(x, y)        outb(x, (ulong)y)
28 #define serial_in(y)            inb((ulong)y)
29 #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE > 0)
30 #define serial_out(x, y)        out_be32(y, x)
31 #define serial_in(y)            in_be32(y)
32 #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE < 0)
33 #define serial_out(x, y)        out_le32(y, x)
34 #define serial_in(y)            in_le32(y)
35 #else
36 #define serial_out(x, y)        writeb(x, y)
37 #define serial_in(y)            readb(y)
38 #endif
39 #endif /* !CONFIG_DM_SERIAL */
40
41 #if defined(CONFIG_SOC_KEYSTONE)
42 #define UART_REG_VAL_PWREMU_MGMT_UART_DISABLE   0
43 #define UART_REG_VAL_PWREMU_MGMT_UART_ENABLE ((1 << 14) | (1 << 13) | (1 << 0))
44 #undef UART_MCRVAL
45 #ifdef CONFIG_SERIAL_HW_FLOW_CONTROL
46 #define UART_MCRVAL             (UART_MCR_RTS | UART_MCR_AFE)
47 #else
48 #define UART_MCRVAL             (UART_MCR_RTS)
49 #endif
50 #endif
51
52 #ifndef CONFIG_SYS_NS16550_IER
53 #define CONFIG_SYS_NS16550_IER  0x00
54 #endif /* CONFIG_SYS_NS16550_IER */
55
56 static inline void serial_out_shift(void *addr, int shift, int value)
57 {
58 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
59         outb(value, (ulong)addr);
60 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
61         out_le32(addr, value);
62 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
63         out_be32(addr, value);
64 #elif defined(CONFIG_SYS_NS16550_MEM32)
65         writel(value, addr);
66 #elif defined(CONFIG_SYS_BIG_ENDIAN)
67         writeb(value, addr + (1 << shift) - 1);
68 #else
69         writeb(value, addr);
70 #endif
71 }
72
73 static inline int serial_in_shift(void *addr, int shift)
74 {
75 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
76         return inb((ulong)addr);
77 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
78         return in_le32(addr);
79 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
80         return in_be32(addr);
81 #elif defined(CONFIG_SYS_NS16550_MEM32)
82         return readl(addr);
83 #elif defined(CONFIG_SYS_BIG_ENDIAN)
84         return readb(addr + (1 << shift) - 1);
85 #else
86         return readb(addr);
87 #endif
88 }
89
90 #if CONFIG_IS_ENABLED(DM_SERIAL)
91
92 #ifndef CONFIG_SYS_NS16550_CLK
93 #define CONFIG_SYS_NS16550_CLK  0
94 #endif
95
96 static void ns16550_writeb(NS16550_t port, int offset, int value)
97 {
98         struct ns16550_platdata *plat = port->plat;
99         unsigned char *addr;
100
101         offset *= 1 << plat->reg_shift;
102         addr = (unsigned char *)plat->base + offset;
103
104         /*
105          * As far as we know it doesn't make sense to support selection of
106          * these options at run-time, so use the existing CONFIG options.
107          */
108         serial_out_shift(addr + plat->reg_offset, plat->reg_shift, value);
109 }
110
111 static int ns16550_readb(NS16550_t port, int offset)
112 {
113         struct ns16550_platdata *plat = port->plat;
114         unsigned char *addr;
115
116         offset *= 1 << plat->reg_shift;
117         addr = (unsigned char *)plat->base + offset;
118
119         return serial_in_shift(addr + plat->reg_offset, plat->reg_shift);
120 }
121
122 static u32 ns16550_getfcr(NS16550_t port)
123 {
124         struct ns16550_platdata *plat = port->plat;
125
126         return plat->fcr;
127 }
128
129 /* We can clean these up once everything is moved to driver model */
130 #define serial_out(value, addr) \
131         ns16550_writeb(com_port, \
132                 (unsigned char *)addr - (unsigned char *)com_port, value)
133 #define serial_in(addr) \
134         ns16550_readb(com_port, \
135                 (unsigned char *)addr - (unsigned char *)com_port)
136 #else
137 static u32 ns16550_getfcr(NS16550_t port)
138 {
139         return UART_FCR_DEFVAL;
140 }
141 #endif
142
143 int ns16550_calc_divisor(NS16550_t port, int clock, int baudrate)
144 {
145         const unsigned int mode_x_div = 16;
146
147         return DIV_ROUND_CLOSEST(clock, mode_x_div * baudrate);
148 }
149
150 static void NS16550_setbrg(NS16550_t com_port, int baud_divisor)
151 {
152         /* to keep serial format, read lcr before writing BKSE */
153         int lcr_val = serial_in(&com_port->lcr) & ~UART_LCR_BKSE;
154
155         serial_out(UART_LCR_BKSE | lcr_val, &com_port->lcr);
156         serial_out(baud_divisor & 0xff, &com_port->dll);
157         serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
158         serial_out(lcr_val, &com_port->lcr);
159 }
160
161 void NS16550_init(NS16550_t com_port, int baud_divisor)
162 {
163 #if (defined(CONFIG_SPL_BUILD) && \
164                 (defined(CONFIG_OMAP34XX) || defined(CONFIG_OMAP44XX)))
165         /*
166          * On some OMAP3/OMAP4 devices when UART3 is configured for boot mode
167          * before SPL starts only THRE bit is set. We have to empty the
168          * transmitter before initialization starts.
169          */
170         if ((serial_in(&com_port->lsr) & (UART_LSR_TEMT | UART_LSR_THRE))
171              == UART_LSR_THRE) {
172                 if (baud_divisor != -1)
173                         NS16550_setbrg(com_port, baud_divisor);
174                 serial_out(0, &com_port->mdr1);
175         }
176 #endif
177
178         while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
179                 ;
180
181         serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
182 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_OMAP_SERIAL)
183         serial_out(0x7, &com_port->mdr1);       /* mode select reset TL16C750*/
184 #endif
185
186         serial_out(UART_MCRVAL, &com_port->mcr);
187         serial_out(ns16550_getfcr(com_port), &com_port->fcr);
188         /* initialize serial config to 8N1 before writing baudrate */
189         serial_out(UART_LCRVAL, &com_port->lcr);
190         if (baud_divisor != -1)
191                 NS16550_setbrg(com_port, baud_divisor);
192 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_SOC_DA8XX) || \
193         defined(CONFIG_OMAP_SERIAL)
194         /* /16 is proper to hit 115200 with 48MHz */
195         serial_out(0, &com_port->mdr1);
196 #endif
197 #if defined(CONFIG_SOC_KEYSTONE)
198         serial_out(UART_REG_VAL_PWREMU_MGMT_UART_ENABLE, &com_port->regC);
199 #endif
200 }
201
202 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
203 void NS16550_reinit(NS16550_t com_port, int baud_divisor)
204 {
205         serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
206         NS16550_setbrg(com_port, 0);
207         serial_out(UART_MCRVAL, &com_port->mcr);
208         serial_out(ns16550_getfcr(com_port), &com_port->fcr);
209         NS16550_setbrg(com_port, baud_divisor);
210 }
211 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
212
213 void NS16550_putc(NS16550_t com_port, char c)
214 {
215         while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
216                 ;
217         serial_out(c, &com_port->thr);
218
219         /*
220          * Call watchdog_reset() upon newline. This is done here in putc
221          * since the environment code uses a single puts() to print the complete
222          * environment upon "printenv". So we can't put this watchdog call
223          * in puts().
224          */
225         if (c == '\n')
226                 WATCHDOG_RESET();
227 }
228
229 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
230 char NS16550_getc(NS16550_t com_port)
231 {
232         while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
233 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_TTY)
234                 extern void usbtty_poll(void);
235                 usbtty_poll();
236 #endif
237                 WATCHDOG_RESET();
238         }
239         return serial_in(&com_port->rbr);
240 }
241
242 int NS16550_tstc(NS16550_t com_port)
243 {
244         return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0;
245 }
246
247 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
248
249 #ifdef CONFIG_DEBUG_UART_NS16550
250
251 #include <debug_uart.h>
252
253 static inline void _debug_uart_init(void)
254 {
255         struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
256         int baud_divisor;
257
258         /*
259          * We copy the code from above because it is already horribly messy.
260          * Trying to refactor to nicely remove the duplication doesn't seem
261          * feasible. The better fix is to move all users of this driver to
262          * driver model.
263          */
264         baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
265                                             CONFIG_BAUDRATE);
266         serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
267         serial_dout(&com_port->mcr, UART_MCRVAL);
268         serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
269
270         serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
271         serial_dout(&com_port->dll, baud_divisor & 0xff);
272         serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
273         serial_dout(&com_port->lcr, UART_LCRVAL);
274 }
275
276 static inline int NS16550_read_baud_divisor(struct NS16550 *com_port)
277 {
278         int ret;
279
280         serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
281         ret = serial_din(&com_port->dll) & 0xff;
282         ret |= (serial_din(&com_port->dlm) & 0xff) << 8;
283         serial_dout(&com_port->lcr, UART_LCRVAL);
284
285         return ret;
286 }
287
288 static inline void _debug_uart_putc(int ch)
289 {
290         struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
291
292         while (!(serial_din(&com_port->lsr) & UART_LSR_THRE)) {
293 #ifdef CONFIG_DEBUG_UART_NS16550_CHECK_ENABLED
294                 if (!NS16550_read_baud_divisor(com_port))
295                         return;
296 #endif
297         }
298         serial_dout(&com_port->thr, ch);
299 }
300
301 DEBUG_UART_FUNCS
302
303 #endif
304
305 #if CONFIG_IS_ENABLED(DM_SERIAL)
306 static int ns16550_serial_putc(struct udevice *dev, const char ch)
307 {
308         struct NS16550 *const com_port = dev_get_priv(dev);
309
310         if (!(serial_in(&com_port->lsr) & UART_LSR_THRE))
311                 return -EAGAIN;
312         serial_out(ch, &com_port->thr);
313
314         /*
315          * Call watchdog_reset() upon newline. This is done here in putc
316          * since the environment code uses a single puts() to print the complete
317          * environment upon "printenv". So we can't put this watchdog call
318          * in puts().
319          */
320         if (ch == '\n')
321                 WATCHDOG_RESET();
322
323         return 0;
324 }
325
326 static int ns16550_serial_pending(struct udevice *dev, bool input)
327 {
328         struct NS16550 *const com_port = dev_get_priv(dev);
329
330         if (input)
331                 return (serial_in(&com_port->lsr) & UART_LSR_DR) ? 1 : 0;
332         else
333                 return (serial_in(&com_port->lsr) & UART_LSR_THRE) ? 0 : 1;
334 }
335
336 static int ns16550_serial_getc(struct udevice *dev)
337 {
338         struct NS16550 *const com_port = dev_get_priv(dev);
339
340         if (!(serial_in(&com_port->lsr) & UART_LSR_DR))
341                 return -EAGAIN;
342
343         return serial_in(&com_port->rbr);
344 }
345
346 static int ns16550_serial_setbrg(struct udevice *dev, int baudrate)
347 {
348         struct NS16550 *const com_port = dev_get_priv(dev);
349         struct ns16550_platdata *plat = com_port->plat;
350         int clock_divisor;
351
352         clock_divisor = ns16550_calc_divisor(com_port, plat->clock, baudrate);
353
354         NS16550_setbrg(com_port, clock_divisor);
355
356         return 0;
357 }
358
359 static int ns16550_serial_setconfig(struct udevice *dev, uint serial_config)
360 {
361         struct NS16550 *const com_port = dev_get_priv(dev);
362         int lcr_val = UART_LCR_WLS_8;
363         uint parity = SERIAL_GET_PARITY(serial_config);
364         uint bits = SERIAL_GET_BITS(serial_config);
365         uint stop = SERIAL_GET_STOP(serial_config);
366
367         /*
368          * only parity config is implemented, check if other serial settings
369          * are the default one.
370          */
371         if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP)
372                 return -ENOTSUPP; /* not supported in driver*/
373
374         switch (parity) {
375         case SERIAL_PAR_NONE:
376                 /* no bits to add */
377                 break;
378         case SERIAL_PAR_ODD:
379                 lcr_val |= UART_LCR_PEN;
380                 break;
381         case SERIAL_PAR_EVEN:
382                 lcr_val |= UART_LCR_PEN | UART_LCR_EPS;
383                 break;
384         default:
385                 return -ENOTSUPP; /* not supported in driver*/
386         }
387
388         serial_out(lcr_val, &com_port->lcr);
389         return 0;
390 }
391
392 static int ns16550_serial_getinfo(struct udevice *dev,
393                                   struct serial_device_info *info)
394 {
395         struct NS16550 *const com_port = dev_get_priv(dev);
396         struct ns16550_platdata *plat = com_port->plat;
397
398         info->type = SERIAL_CHIP_16550_COMPATIBLE;
399 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
400         info->addr_space = SERIAL_ADDRESS_SPACE_IO;
401 #else
402         info->addr_space = SERIAL_ADDRESS_SPACE_MEMORY;
403 #endif
404         info->addr = plat->base;
405         info->reg_width = plat->reg_width;
406         info->reg_shift = plat->reg_shift;
407         info->reg_offset = plat->reg_offset;
408         return 0;
409 }
410
411 int ns16550_serial_probe(struct udevice *dev)
412 {
413         struct NS16550 *const com_port = dev_get_priv(dev);
414         struct reset_ctl_bulk reset_bulk;
415         int ret;
416
417         ret = reset_get_bulk(dev, &reset_bulk);
418         if (!ret)
419                 reset_deassert_bulk(&reset_bulk);
420
421         com_port->plat = dev_get_platdata(dev);
422         NS16550_init(com_port, -1);
423
424         return 0;
425 }
426
427 #if CONFIG_IS_ENABLED(OF_CONTROL)
428 enum {
429         PORT_NS16550 = 0,
430         PORT_JZ4780,
431 };
432 #endif
433
434 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
435 int ns16550_serial_ofdata_to_platdata(struct udevice *dev)
436 {
437         struct ns16550_platdata *plat = dev->platdata;
438         const u32 port_type = dev_get_driver_data(dev);
439         fdt_addr_t addr;
440         struct clk clk;
441         int err;
442
443         /* try Processor Local Bus device first */
444         addr = dev_read_addr_pci(dev);
445         if (addr == FDT_ADDR_T_NONE)
446                 return -EINVAL;
447
448 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
449         plat->base = addr;
450 #else
451         plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
452 #endif
453
454         plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
455         plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
456         plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
457
458         err = clk_get_by_index(dev, 0, &clk);
459         if (!err) {
460                 err = clk_get_rate(&clk);
461                 if (!IS_ERR_VALUE(err))
462                         plat->clock = err;
463         } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
464                 debug("ns16550 failed to get clock\n");
465                 return err;
466         }
467
468         if (!plat->clock)
469                 plat->clock = dev_read_u32_default(dev, "clock-frequency",
470                                                    CONFIG_SYS_NS16550_CLK);
471         if (!plat->clock) {
472                 debug("ns16550 clock not defined\n");
473                 return -EINVAL;
474         }
475
476         plat->fcr = UART_FCR_DEFVAL;
477         if (port_type == PORT_JZ4780)
478                 plat->fcr |= UART_FCR_UME;
479
480         return 0;
481 }
482 #endif
483
484 const struct dm_serial_ops ns16550_serial_ops = {
485         .putc = ns16550_serial_putc,
486         .pending = ns16550_serial_pending,
487         .getc = ns16550_serial_getc,
488         .setbrg = ns16550_serial_setbrg,
489         .setconfig = ns16550_serial_setconfig,
490         .getinfo = ns16550_serial_getinfo,
491 };
492
493 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
494 /*
495  * Please consider existing compatible strings before adding a new
496  * one to keep this table compact. Or you may add a generic "ns16550"
497  * compatible string to your dts.
498  */
499 static const struct udevice_id ns16550_serial_ids[] = {
500         { .compatible = "ns16550",              .data = PORT_NS16550 },
501         { .compatible = "ns16550a",             .data = PORT_NS16550 },
502         { .compatible = "ingenic,jz4780-uart",  .data = PORT_JZ4780  },
503         { .compatible = "nvidia,tegra20-uart",  .data = PORT_NS16550 },
504         { .compatible = "snps,dw-apb-uart",     .data = PORT_NS16550 },
505         {}
506 };
507 #endif /* OF_CONTROL && !OF_PLATDATA */
508
509 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
510
511 /* TODO(sjg@chromium.org): Integrate this into a macro like CONFIG_IS_ENABLED */
512 #if !defined(CONFIG_TPL_BUILD) || defined(CONFIG_TPL_DM_SERIAL)
513 U_BOOT_DRIVER(ns16550_serial) = {
514         .name   = "ns16550_serial",
515         .id     = UCLASS_SERIAL,
516 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
517         .of_match = ns16550_serial_ids,
518         .ofdata_to_platdata = ns16550_serial_ofdata_to_platdata,
519         .platdata_auto_alloc_size = sizeof(struct ns16550_platdata),
520 #endif
521         .priv_auto_alloc_size = sizeof(struct NS16550),
522         .probe = ns16550_serial_probe,
523         .ops    = &ns16550_serial_ops,
524 #if !CONFIG_IS_ENABLED(OF_CONTROL)
525         .flags  = DM_FLAG_PRE_RELOC,
526 #endif
527 };
528 #endif
529 #endif /* SERIAL_PRESENT */
530
531 #endif /* CONFIG_DM_SERIAL */