]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/serial/serial-uclass.c
dm: Allow stdio registration to be dropped
[karo-tx-uboot.git] / drivers / serial / serial-uclass.c
1 /*
2  * Copyright (c) 2014 The Chromium OS Authors.
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <environment.h>
10 #include <errno.h>
11 #include <fdtdec.h>
12 #include <os.h>
13 #include <serial.h>
14 #include <stdio_dev.h>
15 #include <watchdog.h>
16 #include <dm/lists.h>
17 #include <dm/device-internal.h>
18
19 #include <ns16550.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 /* The currently-selected console serial device */
24 struct udevice *cur_dev __attribute__ ((section(".data")));
25
26 /*
27  * Table with supported baudrates (defined in config_xyz.h)
28  */
29 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
30
31 #ifndef CONFIG_SYS_MALLOC_F_LEN
32 #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
33 #endif
34
35 static void serial_find_console_or_panic(void)
36 {
37 #ifdef CONFIG_OF_CONTROL
38         int node;
39
40         /* Check for a chosen console */
41         node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
42         if (node < 0)
43                 node = fdtdec_get_alias_node(gd->fdt_blob, "console");
44         if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &cur_dev))
45                 return;
46
47         /*
48          * If the console is not marked to be bound before relocation, bind
49          * it anyway.
50          */
51         if (node > 0 &&
52             !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &cur_dev)) {
53                 if (!device_probe(cur_dev))
54                         return;
55                 cur_dev = NULL;
56         }
57 #endif
58         /*
59          * Try to use CONFIG_CONS_INDEX if available (it is numbered from 1!).
60          *
61          * Failing that, get the device with sequence number 0, or in extremis
62          * just the first serial device we can find. But we insist on having
63          * a console (even if it is silent).
64          */
65 #ifdef CONFIG_CONS_INDEX
66 #define INDEX (CONFIG_CONS_INDEX - 1)
67 #else
68 #define INDEX 0
69 #endif
70         if (uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &cur_dev) &&
71             uclass_get_device(UCLASS_SERIAL, INDEX, &cur_dev) &&
72             (uclass_first_device(UCLASS_SERIAL, &cur_dev) || !cur_dev))
73                 panic("No serial driver found");
74 #undef INDEX
75 }
76
77 /* Called prior to relocation */
78 int serial_init(void)
79 {
80         serial_find_console_or_panic();
81         gd->flags |= GD_FLG_SERIAL_READY;
82
83         return 0;
84 }
85
86 /* Called after relocation */
87 void serial_initialize(void)
88 {
89         serial_find_console_or_panic();
90 }
91
92 static void _serial_putc(struct udevice *dev, char ch)
93 {
94         struct dm_serial_ops *ops = serial_get_ops(dev);
95         int err;
96
97         do {
98                 err = ops->putc(dev, ch);
99         } while (err == -EAGAIN);
100         if (ch == '\n')
101                 _serial_putc(dev, '\r');
102 }
103
104 static void _serial_puts(struct udevice *dev, const char *str)
105 {
106         while (*str)
107                 _serial_putc(dev, *str++);
108 }
109
110 static int _serial_getc(struct udevice *dev)
111 {
112         struct dm_serial_ops *ops = serial_get_ops(dev);
113         int err;
114
115         do {
116                 err = ops->getc(dev);
117                 if (err == -EAGAIN)
118                         WATCHDOG_RESET();
119         } while (err == -EAGAIN);
120
121         return err >= 0 ? err : 0;
122 }
123
124 static int _serial_tstc(struct udevice *dev)
125 {
126         struct dm_serial_ops *ops = serial_get_ops(dev);
127
128         if (ops->pending)
129                 return ops->pending(dev, true);
130
131         return 1;
132 }
133
134 void serial_putc(char ch)
135 {
136         _serial_putc(cur_dev, ch);
137 }
138
139 void serial_puts(const char *str)
140 {
141         _serial_puts(cur_dev, str);
142 }
143
144 int serial_getc(void)
145 {
146         return _serial_getc(cur_dev);
147 }
148
149 int serial_tstc(void)
150 {
151         return _serial_tstc(cur_dev);
152 }
153
154 void serial_setbrg(void)
155 {
156         struct dm_serial_ops *ops = serial_get_ops(cur_dev);
157
158         if (ops->setbrg)
159                 ops->setbrg(cur_dev, gd->baudrate);
160 }
161
162 void serial_stdio_init(void)
163 {
164 }
165
166 #ifdef CONFIG_DM_STDIO
167 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
168 {
169         _serial_putc(sdev->priv, ch);
170 }
171 #endif
172
173 void serial_stub_puts(struct stdio_dev *sdev, const char *str)
174 {
175         _serial_puts(sdev->priv, str);
176 }
177
178 int serial_stub_getc(struct stdio_dev *sdev)
179 {
180         return _serial_getc(sdev->priv);
181 }
182
183 int serial_stub_tstc(struct stdio_dev *sdev)
184 {
185         return _serial_tstc(sdev->priv);
186 }
187
188 /**
189  * on_baudrate() - Update the actual baudrate when the env var changes
190  *
191  * This will check for a valid baudrate and only apply it if valid.
192  */
193 static int on_baudrate(const char *name, const char *value, enum env_op op,
194         int flags)
195 {
196         int i;
197         int baudrate;
198
199         switch (op) {
200         case env_op_create:
201         case env_op_overwrite:
202                 /*
203                  * Switch to new baudrate if new baudrate is supported
204                  */
205                 baudrate = simple_strtoul(value, NULL, 10);
206
207                 /* Not actually changing */
208                 if (gd->baudrate == baudrate)
209                         return 0;
210
211                 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
212                         if (baudrate == baudrate_table[i])
213                                 break;
214                 }
215                 if (i == ARRAY_SIZE(baudrate_table)) {
216                         if ((flags & H_FORCE) == 0)
217                                 printf("## Baudrate %d bps not supported\n",
218                                        baudrate);
219                         return 1;
220                 }
221                 if ((flags & H_INTERACTIVE) != 0) {
222                         printf("## Switch baudrate to %d bps and press ENTER ...\n",
223                                baudrate);
224                         udelay(50000);
225                 }
226
227                 gd->baudrate = baudrate;
228
229                 serial_setbrg();
230
231                 udelay(50000);
232
233                 if ((flags & H_INTERACTIVE) != 0)
234                         while (1) {
235                                 if (getc() == '\r')
236                                         break;
237                         }
238
239                 return 0;
240         case env_op_delete:
241                 printf("## Baudrate may not be deleted\n");
242                 return 1;
243         default:
244                 return 0;
245         }
246 }
247 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
248
249 static int serial_post_probe(struct udevice *dev)
250 {
251         struct dm_serial_ops *ops = serial_get_ops(dev);
252 #ifdef CONFIG_DM_STDIO
253         struct serial_dev_priv *upriv = dev->uclass_priv;
254         struct stdio_dev sdev;
255 #endif
256         int ret;
257
258         /* Set the baud rate */
259         if (ops->setbrg) {
260                 ret = ops->setbrg(dev, gd->baudrate);
261                 if (ret)
262                         return ret;
263         }
264
265 #ifdef CONFIG_DM_STDIO
266         if (!(gd->flags & GD_FLG_RELOC))
267                 return 0;
268         memset(&sdev, '\0', sizeof(sdev));
269
270         strncpy(sdev.name, dev->name, sizeof(sdev.name));
271         sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
272         sdev.priv = dev;
273         sdev.putc = serial_stub_putc;
274         sdev.puts = serial_stub_puts;
275         sdev.getc = serial_stub_getc;
276         sdev.tstc = serial_stub_tstc;
277         stdio_register_dev(&sdev, &upriv->sdev);
278 #endif
279         return 0;
280 }
281
282 static int serial_pre_remove(struct udevice *dev)
283 {
284 #ifdef CONFIG_SYS_STDIO_DEREGISTER
285         struct serial_dev_priv *upriv = dev->uclass_priv;
286
287         if (stdio_deregister_dev(upriv->sdev, 0))
288                 return -EPERM;
289 #endif
290
291         return 0;
292 }
293
294 UCLASS_DRIVER(serial) = {
295         .id             = UCLASS_SERIAL,
296         .name           = "serial",
297         .post_probe     = serial_post_probe,
298         .pre_remove     = serial_pre_remove,
299         .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
300 };