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