Merge branch 'master' of git://git.denx.de/u-boot-usb
[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                 else {
175                         // Re-use old baud rate divisor to flush transmit reg.
176                         const int dll = serial_in(&com_port->dll);
177                         const int dlm = serial_in(&com_port->dlm);
178                         const int divisor = dll | (dlm << 8);
179                         NS16550_setbrg(com_port, divisor);
180                 }
181                 serial_out(0, &com_port->mdr1);
182         }
183 #endif
184
185         while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
186                 ;
187
188         serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
189 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_OMAP_SERIAL)
190         serial_out(0x7, &com_port->mdr1);       /* mode select reset TL16C750*/
191 #endif
192
193         serial_out(UART_MCRVAL, &com_port->mcr);
194         serial_out(ns16550_getfcr(com_port), &com_port->fcr);
195         /* initialize serial config to 8N1 before writing baudrate */
196         serial_out(UART_LCRVAL, &com_port->lcr);
197         if (baud_divisor != -1)
198                 NS16550_setbrg(com_port, baud_divisor);
199 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_SOC_DA8XX) || \
200         defined(CONFIG_OMAP_SERIAL)
201         /* /16 is proper to hit 115200 with 48MHz */
202         serial_out(0, &com_port->mdr1);
203 #endif
204 #if defined(CONFIG_SOC_KEYSTONE)
205         serial_out(UART_REG_VAL_PWREMU_MGMT_UART_ENABLE, &com_port->regC);
206 #endif
207 }
208
209 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
210 void NS16550_reinit(NS16550_t com_port, int baud_divisor)
211 {
212         serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
213         NS16550_setbrg(com_port, 0);
214         serial_out(UART_MCRVAL, &com_port->mcr);
215         serial_out(ns16550_getfcr(com_port), &com_port->fcr);
216         NS16550_setbrg(com_port, baud_divisor);
217 }
218 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
219
220 void NS16550_putc(NS16550_t com_port, char c)
221 {
222         while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
223                 ;
224         serial_out(c, &com_port->thr);
225
226         /*
227          * Call watchdog_reset() upon newline. This is done here in putc
228          * since the environment code uses a single puts() to print the complete
229          * environment upon "printenv". So we can't put this watchdog call
230          * in puts().
231          */
232         if (c == '\n')
233                 WATCHDOG_RESET();
234 }
235
236 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
237 char NS16550_getc(NS16550_t com_port)
238 {
239         while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
240 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_TTY)
241                 extern void usbtty_poll(void);
242                 usbtty_poll();
243 #endif
244                 WATCHDOG_RESET();
245         }
246         return serial_in(&com_port->rbr);
247 }
248
249 int NS16550_tstc(NS16550_t com_port)
250 {
251         return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0;
252 }
253
254 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
255
256 #ifdef CONFIG_DEBUG_UART_NS16550
257
258 #include <debug_uart.h>
259
260 static inline void _debug_uart_init(void)
261 {
262         struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
263         int baud_divisor;
264
265         /*
266          * We copy the code from above because it is already horribly messy.
267          * Trying to refactor to nicely remove the duplication doesn't seem
268          * feasible. The better fix is to move all users of this driver to
269          * driver model.
270          */
271         baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
272                                             CONFIG_BAUDRATE);
273         serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
274         serial_dout(&com_port->mcr, UART_MCRVAL);
275         serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
276
277         serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
278         serial_dout(&com_port->dll, baud_divisor & 0xff);
279         serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
280         serial_dout(&com_port->lcr, UART_LCRVAL);
281 }
282
283 static inline int NS16550_read_baud_divisor(struct NS16550 *com_port)
284 {
285         int ret;
286
287         serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
288         ret = serial_din(&com_port->dll) & 0xff;
289         ret |= (serial_din(&com_port->dlm) & 0xff) << 8;
290         serial_dout(&com_port->lcr, UART_LCRVAL);
291
292         return ret;
293 }
294
295 static inline void _debug_uart_putc(int ch)
296 {
297         struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
298
299         while (!(serial_din(&com_port->lsr) & UART_LSR_THRE)) {
300 #ifdef CONFIG_DEBUG_UART_NS16550_CHECK_ENABLED
301                 if (!NS16550_read_baud_divisor(com_port))
302                         return;
303 #endif
304         }
305         serial_dout(&com_port->thr, ch);
306 }
307
308 DEBUG_UART_FUNCS
309
310 #endif
311
312 #if CONFIG_IS_ENABLED(DM_SERIAL)
313 static int ns16550_serial_putc(struct udevice *dev, const char ch)
314 {
315         struct NS16550 *const com_port = dev_get_priv(dev);
316
317         if (!(serial_in(&com_port->lsr) & UART_LSR_THRE))
318                 return -EAGAIN;
319         serial_out(ch, &com_port->thr);
320
321         /*
322          * Call watchdog_reset() upon newline. This is done here in putc
323          * since the environment code uses a single puts() to print the complete
324          * environment upon "printenv". So we can't put this watchdog call
325          * in puts().
326          */
327         if (ch == '\n')
328                 WATCHDOG_RESET();
329
330         return 0;
331 }
332
333 static int ns16550_serial_pending(struct udevice *dev, bool input)
334 {
335         struct NS16550 *const com_port = dev_get_priv(dev);
336
337         if (input)
338                 return (serial_in(&com_port->lsr) & UART_LSR_DR) ? 1 : 0;
339         else
340                 return (serial_in(&com_port->lsr) & UART_LSR_THRE) ? 0 : 1;
341 }
342
343 static int ns16550_serial_getc(struct udevice *dev)
344 {
345         struct NS16550 *const com_port = dev_get_priv(dev);
346
347         if (!(serial_in(&com_port->lsr) & UART_LSR_DR))
348                 return -EAGAIN;
349
350         return serial_in(&com_port->rbr);
351 }
352
353 static int ns16550_serial_setbrg(struct udevice *dev, int baudrate)
354 {
355         struct NS16550 *const com_port = dev_get_priv(dev);
356         struct ns16550_platdata *plat = com_port->plat;
357         int clock_divisor;
358
359         clock_divisor = ns16550_calc_divisor(com_port, plat->clock, baudrate);
360
361         NS16550_setbrg(com_port, clock_divisor);
362
363         return 0;
364 }
365
366 static int ns16550_serial_setconfig(struct udevice *dev, uint serial_config)
367 {
368         struct NS16550 *const com_port = dev_get_priv(dev);
369         int lcr_val = UART_LCR_WLS_8;
370         uint parity = SERIAL_GET_PARITY(serial_config);
371         uint bits = SERIAL_GET_BITS(serial_config);
372         uint stop = SERIAL_GET_STOP(serial_config);
373
374         /*
375          * only parity config is implemented, check if other serial settings
376          * are the default one.
377          */
378         if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP)
379                 return -ENOTSUPP; /* not supported in driver*/
380
381         switch (parity) {
382         case SERIAL_PAR_NONE:
383                 /* no bits to add */
384                 break;
385         case SERIAL_PAR_ODD:
386                 lcr_val |= UART_LCR_PEN;
387                 break;
388         case SERIAL_PAR_EVEN:
389                 lcr_val |= UART_LCR_PEN | UART_LCR_EPS;
390                 break;
391         default:
392                 return -ENOTSUPP; /* not supported in driver*/
393         }
394
395         serial_out(lcr_val, &com_port->lcr);
396         return 0;
397 }
398
399 static int ns16550_serial_getinfo(struct udevice *dev,
400                                   struct serial_device_info *info)
401 {
402         struct NS16550 *const com_port = dev_get_priv(dev);
403         struct ns16550_platdata *plat = com_port->plat;
404
405         info->type = SERIAL_CHIP_16550_COMPATIBLE;
406 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
407         info->addr_space = SERIAL_ADDRESS_SPACE_IO;
408 #else
409         info->addr_space = SERIAL_ADDRESS_SPACE_MEMORY;
410 #endif
411         info->addr = plat->base;
412         info->reg_width = plat->reg_width;
413         info->reg_shift = plat->reg_shift;
414         info->reg_offset = plat->reg_offset;
415         return 0;
416 }
417
418 int ns16550_serial_probe(struct udevice *dev)
419 {
420         struct NS16550 *const com_port = dev_get_priv(dev);
421         struct reset_ctl_bulk reset_bulk;
422         int ret;
423
424         ret = reset_get_bulk(dev, &reset_bulk);
425         if (!ret)
426                 reset_deassert_bulk(&reset_bulk);
427
428         com_port->plat = dev_get_platdata(dev);
429         NS16550_init(com_port, -1);
430
431         return 0;
432 }
433
434 #if CONFIG_IS_ENABLED(OF_CONTROL)
435 enum {
436         PORT_NS16550 = 0,
437         PORT_JZ4780,
438 };
439 #endif
440
441 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
442 int ns16550_serial_ofdata_to_platdata(struct udevice *dev)
443 {
444         struct ns16550_platdata *plat = dev->platdata;
445         const u32 port_type = dev_get_driver_data(dev);
446         fdt_addr_t addr;
447         struct clk clk;
448         int err;
449
450         /* try Processor Local Bus device first */
451         addr = dev_read_addr_pci(dev);
452         if (addr == FDT_ADDR_T_NONE)
453                 return -EINVAL;
454
455 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
456         plat->base = addr;
457 #else
458         plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
459 #endif
460
461         plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
462         plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
463         plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
464
465         err = clk_get_by_index(dev, 0, &clk);
466         if (!err) {
467                 err = clk_get_rate(&clk);
468                 if (!IS_ERR_VALUE(err))
469                         plat->clock = err;
470         } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
471                 debug("ns16550 failed to get clock\n");
472                 return err;
473         }
474
475         if (!plat->clock)
476                 plat->clock = dev_read_u32_default(dev, "clock-frequency",
477                                                    CONFIG_SYS_NS16550_CLK);
478         if (!plat->clock) {
479                 debug("ns16550 clock not defined\n");
480                 return -EINVAL;
481         }
482
483         plat->fcr = UART_FCR_DEFVAL;
484         if (port_type == PORT_JZ4780)
485                 plat->fcr |= UART_FCR_UME;
486
487         return 0;
488 }
489 #endif
490
491 const struct dm_serial_ops ns16550_serial_ops = {
492         .putc = ns16550_serial_putc,
493         .pending = ns16550_serial_pending,
494         .getc = ns16550_serial_getc,
495         .setbrg = ns16550_serial_setbrg,
496         .setconfig = ns16550_serial_setconfig,
497         .getinfo = ns16550_serial_getinfo,
498 };
499
500 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
501 /*
502  * Please consider existing compatible strings before adding a new
503  * one to keep this table compact. Or you may add a generic "ns16550"
504  * compatible string to your dts.
505  */
506 static const struct udevice_id ns16550_serial_ids[] = {
507         { .compatible = "ns16550",              .data = PORT_NS16550 },
508         { .compatible = "ns16550a",             .data = PORT_NS16550 },
509         { .compatible = "ingenic,jz4780-uart",  .data = PORT_JZ4780  },
510         { .compatible = "nvidia,tegra20-uart",  .data = PORT_NS16550 },
511         { .compatible = "snps,dw-apb-uart",     .data = PORT_NS16550 },
512         {}
513 };
514 #endif /* OF_CONTROL && !OF_PLATDATA */
515
516 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
517
518 /* TODO(sjg@chromium.org): Integrate this into a macro like CONFIG_IS_ENABLED */
519 #if !defined(CONFIG_TPL_BUILD) || defined(CONFIG_TPL_DM_SERIAL)
520 U_BOOT_DRIVER(ns16550_serial) = {
521         .name   = "ns16550_serial",
522         .id     = UCLASS_SERIAL,
523 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
524         .of_match = ns16550_serial_ids,
525         .ofdata_to_platdata = ns16550_serial_ofdata_to_platdata,
526         .platdata_auto_alloc_size = sizeof(struct ns16550_platdata),
527 #endif
528         .priv_auto_alloc_size = sizeof(struct NS16550),
529         .probe = ns16550_serial_probe,
530         .ops    = &ns16550_serial_ops,
531 #if !CONFIG_IS_ENABLED(OF_CONTROL)
532         .flags  = DM_FLAG_PRE_RELOC,
533 #endif
534 };
535 #endif
536 #endif /* SERIAL_PRESENT */
537
538 #endif /* CONFIG_DM_SERIAL */