]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/serial/serial-uclass.c
dm: serial: Put common code into separate functions
[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 <errno.h>
10 #include <fdtdec.h>
11 #include <os.h>
12 #include <serial.h>
13 #include <stdio_dev.h>
14 #include <dm/lists.h>
15 #include <dm/device-internal.h>
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 /* The currently-selected console serial device */
20 struct udevice *cur_dev __attribute__ ((section(".data")));
21
22 #ifndef CONFIG_SYS_MALLOC_F_LEN
23 #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
24 #endif
25
26 static void serial_find_console_or_panic(void)
27 {
28 #ifdef CONFIG_OF_CONTROL
29         int node;
30
31         /* Check for a chosen console */
32         node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
33         if (node < 0)
34                 node = fdtdec_get_alias_node(gd->fdt_blob, "console");
35         if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &cur_dev))
36                 return;
37
38         /*
39          * If the console is not marked to be bound before relocation, bind
40          * it anyway.
41          */
42         if (node > 0 &&
43             !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &cur_dev)) {
44                 if (!device_probe(cur_dev))
45                         return;
46                 cur_dev = NULL;
47         }
48 #endif
49         /*
50          * Failing that, get the device with sequence number 0, or in extremis
51          * just the first serial device we can find. But we insist on having
52          * a console (even if it is silent).
53          */
54         if (uclass_get_device_by_seq(UCLASS_SERIAL, 0, &cur_dev) &&
55             (uclass_first_device(UCLASS_SERIAL, &cur_dev) || !cur_dev))
56                 panic("No serial driver found");
57 }
58
59 /* Called prior to relocation */
60 int serial_init(void)
61 {
62         serial_find_console_or_panic();
63         gd->flags |= GD_FLG_SERIAL_READY;
64
65         return 0;
66 }
67
68 /* Called after relocation */
69 void serial_initialize(void)
70 {
71         serial_find_console_or_panic();
72 }
73
74 static void serial_putc_dev(struct udevice *dev, char ch)
75 {
76         struct dm_serial_ops *ops = serial_get_ops(cur_dev);
77         int err;
78
79         do {
80                 err = ops->putc(cur_dev, ch);
81         } while (err == -EAGAIN);
82         if (ch == '\n')
83                 serial_putc('\r');
84 }
85
86 void serial_putc(char ch)
87 {
88         serial_putc_dev(cur_dev, ch);
89 }
90
91 void serial_setbrg(void)
92 {
93         struct dm_serial_ops *ops = serial_get_ops(cur_dev);
94
95         if (ops->setbrg)
96                 ops->setbrg(cur_dev, gd->baudrate);
97 }
98
99 void serial_puts(const char *str)
100 {
101         while (*str)
102                 serial_putc(*str++);
103 }
104
105 int serial_tstc(void)
106 {
107         struct dm_serial_ops *ops = serial_get_ops(cur_dev);
108
109         if (ops->pending)
110                 return ops->pending(cur_dev, true);
111
112         return 1;
113 }
114
115 static int serial_getc_dev(struct udevice *dev)
116 {
117         struct dm_serial_ops *ops = serial_get_ops(dev);
118         int err;
119
120         do {
121                 err = ops->getc(dev);
122         } while (err == -EAGAIN);
123
124         return err >= 0 ? err : 0;
125 }
126
127 int serial_getc(void)
128 {
129         return serial_getc_dev(cur_dev);
130 }
131
132 void serial_stdio_init(void)
133 {
134 }
135
136 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
137 {
138         struct udevice *dev = sdev->priv;
139
140         serial_putc_dev(dev, ch);
141 }
142
143 void serial_stub_puts(struct stdio_dev *sdev, const char *str)
144 {
145         while (*str)
146                 serial_stub_putc(sdev, *str++);
147 }
148
149 int serial_stub_getc(struct stdio_dev *sdev)
150 {
151         struct udevice *dev = sdev->priv;
152
153         return serial_getc_dev(dev);
154 }
155
156 int serial_stub_tstc(struct stdio_dev *sdev)
157 {
158         struct udevice *dev = sdev->priv;
159         struct dm_serial_ops *ops = serial_get_ops(dev);
160
161         if (ops->pending)
162                 return ops->pending(dev, true);
163
164         return 1;
165 }
166
167 static int serial_post_probe(struct udevice *dev)
168 {
169         struct stdio_dev sdev;
170         struct dm_serial_ops *ops = serial_get_ops(dev);
171         struct serial_dev_priv *upriv = dev->uclass_priv;
172         int ret;
173
174         /* Set the baud rate */
175         if (ops->setbrg) {
176                 ret = ops->setbrg(dev, gd->baudrate);
177                 if (ret)
178                         return ret;
179         }
180
181         if (!(gd->flags & GD_FLG_RELOC))
182                 return 0;
183
184         memset(&sdev, '\0', sizeof(sdev));
185
186         strncpy(sdev.name, dev->name, sizeof(sdev.name));
187         sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
188         sdev.priv = dev;
189         sdev.putc = serial_stub_putc;
190         sdev.puts = serial_stub_puts;
191         sdev.getc = serial_stub_getc;
192         sdev.tstc = serial_stub_tstc;
193         stdio_register_dev(&sdev, &upriv->sdev);
194
195         return 0;
196 }
197
198 static int serial_pre_remove(struct udevice *dev)
199 {
200 #ifdef CONFIG_SYS_STDIO_DEREGISTER
201         struct serial_dev_priv *upriv = dev->uclass_priv;
202
203         if (stdio_deregister_dev(upriv->sdev, 0))
204                 return -EPERM;
205 #endif
206
207         return 0;
208 }
209
210 UCLASS_DRIVER(serial) = {
211         .id             = UCLASS_SERIAL,
212         .name           = "serial",
213         .post_probe     = serial_post_probe,
214         .pre_remove     = serial_pre_remove,
215         .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
216 };