8171b17faf88ccba60deb759e16973ac8f089d45
[platform/kernel/u-boot.git] / drivers / serial / serial-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014 The Chromium OS Authors.
4  */
5
6 #define LOG_CATEGORY UCLASS_SERIAL
7
8 #include <common.h>
9 #include <dm.h>
10 #include <env_internal.h>
11 #include <errno.h>
12 #include <malloc.h>
13 #include <os.h>
14 #include <serial.h>
15 #include <stdio_dev.h>
16 #include <watchdog.h>
17 #include <asm/global_data.h>
18 #include <dm/lists.h>
19 #include <dm/device-internal.h>
20 #include <dm/of_access.h>
21 #include <linux/delay.h>
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 /*
26  * Table with supported baudrates (defined in config_xyz.h)
27  */
28 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
29
30 #if !CONFIG_VAL(SYS_MALLOC_F_LEN)
31 #error "Serial is required before relocation - define CONFIG_$(SPL_)SYS_MALLOC_F_LEN to make this work"
32 #endif
33
34 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
35 static int serial_check_stdout(const void *blob, struct udevice **devp)
36 {
37         int node = -1;
38         const char *str, *p, *name;
39         int namelen;
40
41         /* Check for a chosen console */
42         str = fdtdec_get_chosen_prop(blob, "stdout-path");
43         if (str) {
44                 p = strchr(str, ':');
45                 namelen = p ? p - str : strlen(str);
46                 node = fdt_path_offset_namelen(blob, str, namelen);
47
48                 if (node < 0) {
49                         /*
50                          * Deal with things like
51                          *      stdout-path = "serial0:115200n8";
52                          *
53                          * We need to look up the alias and then follow it to
54                          * the correct node.
55                          */
56                         name = fdt_get_alias_namelen(blob, str, namelen);
57                         if (name)
58                                 node = fdt_path_offset(blob, name);
59                 }
60         }
61
62         if (node < 0)
63                 node = fdt_path_offset(blob, "console");
64         if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp))
65                 return 0;
66
67         /*
68          * If the console is not marked to be bound before relocation, bind it
69          * anyway.
70          */
71         if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
72                                         devp, false)) {
73                 if (!device_probe(*devp))
74                         return 0;
75         }
76
77         return -ENODEV;
78 }
79
80 static void serial_find_console_or_panic(void)
81 {
82         const void *blob = gd->fdt_blob;
83         struct udevice *dev;
84 #ifdef CONFIG_SERIAL_SEARCH_ALL
85         int ret;
86 #endif
87
88         if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
89                 uclass_first_device(UCLASS_SERIAL, &dev);
90                 if (dev) {
91                         gd->cur_serial_dev = dev;
92                         return;
93                 }
94         } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
95                 /* Live tree has support for stdout */
96                 if (of_live_active()) {
97                         struct device_node *np = of_get_stdout();
98
99                         if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL,
100                                         np_to_ofnode(np), &dev)) {
101                                 gd->cur_serial_dev = dev;
102                                 return;
103                         }
104                 } else {
105                         if (!serial_check_stdout(blob, &dev)) {
106                                 gd->cur_serial_dev = dev;
107                                 return;
108                         }
109                 }
110         }
111         if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
112                 /*
113                  * Try to use CONFIG_CONS_INDEX if available (it is numbered
114                  * from 1!).
115                  *
116                  * Failing that, get the device with sequence number 0, or in
117                  * extremis just the first working serial device we can find.
118                  * But we insist on having a console (even if it is silent).
119                  */
120 #ifdef CONFIG_CONS_INDEX
121 #define INDEX (CONFIG_CONS_INDEX - 1)
122 #else
123 #define INDEX 0
124 #endif
125
126 #ifdef CONFIG_SERIAL_SEARCH_ALL
127                 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
128                     !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) {
129                         if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) {
130                                 gd->cur_serial_dev = dev;
131                                 return;
132                         }
133                 }
134
135                 /* Search for any working device */
136                 for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev);
137                      dev;
138                      ret = uclass_next_device_check(&dev)) {
139                         if (!ret) {
140                                 /* Device did succeed probing */
141                                 gd->cur_serial_dev = dev;
142                                 return;
143                         }
144                 }
145 #else
146                 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
147                     !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
148                     (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
149                         gd->cur_serial_dev = dev;
150                         return;
151                 }
152 #endif
153
154 #undef INDEX
155         }
156
157 #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
158         panic_str("No serial driver found");
159 #endif
160 }
161 #endif /* CONFIG_SERIAL_PRESENT */
162
163 /* Called prior to relocation */
164 int serial_init(void)
165 {
166 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
167         serial_find_console_or_panic();
168         gd->flags |= GD_FLG_SERIAL_READY;
169         serial_setbrg();
170 #endif
171
172         return 0;
173 }
174
175 /* Called after relocation */
176 int serial_initialize(void)
177 {
178         /* Scanning uclass to probe devices */
179         if (IS_ENABLED(CONFIG_SERIAL_PROBE_ALL)) {
180                 int ret;
181
182                 ret  = uclass_probe_all(UCLASS_SERIAL);
183                 if (ret)
184                         return ret;
185         }
186
187         return serial_init();
188 }
189
190 static void _serial_putc(struct udevice *dev, char ch)
191 {
192         struct dm_serial_ops *ops = serial_get_ops(dev);
193         int err;
194
195         if (ch == '\n')
196                 _serial_putc(dev, '\r');
197
198         do {
199                 err = ops->putc(dev, ch);
200         } while (err == -EAGAIN);
201 }
202
203 static void _serial_puts(struct udevice *dev, const char *str)
204 {
205         while (*str)
206                 _serial_putc(dev, *str++);
207 }
208
209 static int __serial_getc(struct udevice *dev)
210 {
211         struct dm_serial_ops *ops = serial_get_ops(dev);
212         int err;
213
214         do {
215                 err = ops->getc(dev);
216                 if (err == -EAGAIN)
217                         WATCHDOG_RESET();
218         } while (err == -EAGAIN);
219
220         return err >= 0 ? err : 0;
221 }
222
223 static int __serial_tstc(struct udevice *dev)
224 {
225         struct dm_serial_ops *ops = serial_get_ops(dev);
226
227         if (ops->pending)
228                 return ops->pending(dev, true);
229
230         return 1;
231 }
232
233 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
234 static int _serial_tstc(struct udevice *dev)
235 {
236         struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
237
238         /* Read all available chars into the RX buffer */
239         while (__serial_tstc(dev)) {
240                 upriv->buf[upriv->wr_ptr++] = __serial_getc(dev);
241                 upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
242         }
243
244         return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
245 }
246
247 static int _serial_getc(struct udevice *dev)
248 {
249         struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
250         char val;
251
252         if (upriv->rd_ptr == upriv->wr_ptr)
253                 return __serial_getc(dev);
254
255         val = upriv->buf[upriv->rd_ptr++];
256         upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
257
258         return val;
259 }
260
261 #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
262
263 static int _serial_getc(struct udevice *dev)
264 {
265         return __serial_getc(dev);
266 }
267
268 static int _serial_tstc(struct udevice *dev)
269 {
270         return __serial_tstc(dev);
271 }
272 #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
273
274 void serial_putc(char ch)
275 {
276         if (gd->cur_serial_dev)
277                 _serial_putc(gd->cur_serial_dev, ch);
278 }
279
280 void serial_puts(const char *str)
281 {
282         if (gd->cur_serial_dev)
283                 _serial_puts(gd->cur_serial_dev, str);
284 }
285
286 int serial_getc(void)
287 {
288         if (!gd->cur_serial_dev)
289                 return 0;
290
291         return _serial_getc(gd->cur_serial_dev);
292 }
293
294 int serial_tstc(void)
295 {
296         if (!gd->cur_serial_dev)
297                 return 0;
298
299         return _serial_tstc(gd->cur_serial_dev);
300 }
301
302 void serial_setbrg(void)
303 {
304         struct dm_serial_ops *ops;
305
306         if (!gd->cur_serial_dev)
307                 return;
308
309         ops = serial_get_ops(gd->cur_serial_dev);
310         if (ops->setbrg)
311                 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
312 }
313
314 int serial_getconfig(struct udevice *dev, uint *config)
315 {
316         struct dm_serial_ops *ops;
317
318         ops = serial_get_ops(dev);
319         if (ops->getconfig)
320                 return ops->getconfig(dev, config);
321
322         return 0;
323 }
324
325 int serial_setconfig(struct udevice *dev, uint config)
326 {
327         struct dm_serial_ops *ops;
328
329         ops = serial_get_ops(dev);
330         if (ops->setconfig)
331                 return ops->setconfig(dev, config);
332
333         return 0;
334 }
335
336 int serial_getinfo(struct udevice *dev, struct serial_device_info *info)
337 {
338         struct dm_serial_ops *ops;
339
340         if (!info)
341                 return -EINVAL;
342
343         info->baudrate = gd->baudrate;
344
345         ops = serial_get_ops(dev);
346         if (ops->getinfo)
347                 return ops->getinfo(dev, info);
348
349         return -EINVAL;
350 }
351
352 void serial_stdio_init(void)
353 {
354 }
355
356 #if defined(CONFIG_DM_STDIO)
357
358 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
359 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
360 {
361         _serial_putc(sdev->priv, ch);
362 }
363 #endif
364
365 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
366 {
367         _serial_puts(sdev->priv, str);
368 }
369
370 static int serial_stub_getc(struct stdio_dev *sdev)
371 {
372         return _serial_getc(sdev->priv);
373 }
374
375 static int serial_stub_tstc(struct stdio_dev *sdev)
376 {
377         return _serial_tstc(sdev->priv);
378 }
379 #endif
380
381 /**
382  * on_baudrate() - Update the actual baudrate when the env var changes
383  *
384  * This will check for a valid baudrate and only apply it if valid.
385  */
386 static int on_baudrate(const char *name, const char *value, enum env_op op,
387         int flags)
388 {
389         int i;
390         int baudrate;
391
392         switch (op) {
393         case env_op_create:
394         case env_op_overwrite:
395                 /*
396                  * Switch to new baudrate if new baudrate is supported
397                  */
398                 baudrate = dectoul(value, NULL);
399
400                 /* Not actually changing */
401                 if (gd->baudrate == baudrate)
402                         return 0;
403
404                 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
405                         if (baudrate == baudrate_table[i])
406                                 break;
407                 }
408                 if (i == ARRAY_SIZE(baudrate_table)) {
409                         if ((flags & H_FORCE) == 0)
410                                 printf("## Baudrate %d bps not supported\n",
411                                        baudrate);
412                         return 1;
413                 }
414                 if ((flags & H_INTERACTIVE) != 0) {
415                         printf("## Switch baudrate to %d bps and press ENTER ...\n",
416                                baudrate);
417                         udelay(50000);
418                 }
419
420                 gd->baudrate = baudrate;
421
422                 serial_setbrg();
423
424                 udelay(50000);
425
426                 if ((flags & H_INTERACTIVE) != 0)
427                         while (1) {
428                                 if (getchar() == '\r')
429                                         break;
430                         }
431
432                 return 0;
433         case env_op_delete:
434                 printf("## Baudrate may not be deleted\n");
435                 return 1;
436         default:
437                 return 0;
438         }
439 }
440 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
441
442 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
443 static int serial_post_probe(struct udevice *dev)
444 {
445         struct dm_serial_ops *ops = serial_get_ops(dev);
446 #ifdef CONFIG_DM_STDIO
447         struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
448         struct stdio_dev sdev;
449 #endif
450         int ret;
451
452 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
453         if (ops->setbrg)
454                 ops->setbrg += gd->reloc_off;
455         if (ops->getc)
456                 ops->getc += gd->reloc_off;
457         if (ops->putc)
458                 ops->putc += gd->reloc_off;
459         if (ops->pending)
460                 ops->pending += gd->reloc_off;
461         if (ops->clear)
462                 ops->clear += gd->reloc_off;
463         if (ops->getconfig)
464                 ops->getconfig += gd->reloc_off;
465         if (ops->setconfig)
466                 ops->setconfig += gd->reloc_off;
467 #if CONFIG_POST & CONFIG_SYS_POST_UART
468         if (ops->loop)
469                 ops->loop += gd->reloc_off;
470 #endif
471         if (ops->getinfo)
472                 ops->getinfo += gd->reloc_off;
473 #endif
474         /* Set the baud rate */
475         if (ops->setbrg) {
476                 ret = ops->setbrg(dev, gd->baudrate);
477                 if (ret)
478                         return ret;
479         }
480
481 #ifdef CONFIG_DM_STDIO
482         if (!(gd->flags & GD_FLG_RELOC))
483                 return 0;
484         memset(&sdev, '\0', sizeof(sdev));
485
486         strncpy(sdev.name, dev->name, sizeof(sdev.name));
487         sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
488         sdev.priv = dev;
489         sdev.putc = serial_stub_putc;
490         sdev.puts = serial_stub_puts;
491         sdev.getc = serial_stub_getc;
492         sdev.tstc = serial_stub_tstc;
493
494 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
495         /* Allocate the RX buffer */
496         upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE);
497 #endif
498
499         stdio_register_dev(&sdev, &upriv->sdev);
500 #endif
501         return 0;
502 }
503
504 static int serial_pre_remove(struct udevice *dev)
505 {
506 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
507         struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
508
509         if (stdio_deregister_dev(upriv->sdev, true))
510                 return -EPERM;
511 #endif
512
513         return 0;
514 }
515
516 UCLASS_DRIVER(serial) = {
517         .id             = UCLASS_SERIAL,
518         .name           = "serial",
519         .flags          = DM_UC_FLAG_SEQ_ALIAS,
520         .post_probe     = serial_post_probe,
521         .pre_remove     = serial_pre_remove,
522         .per_device_auto        = sizeof(struct serial_dev_priv),
523 };
524 #endif