meson_uart.c revision 1.3.2.2 1 1.3.2.2 christos /* $NetBSD: meson_uart.c,v 1.3.2.2 2019/06/10 22:05:51 christos Exp $ */
2 1.3.2.2 christos
3 1.3.2.2 christos /*-
4 1.3.2.2 christos * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.3.2.2 christos * All rights reserved.
6 1.3.2.2 christos *
7 1.3.2.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 christos * by Matt Thomas of 3am Software Foundry.
9 1.3.2.2 christos *
10 1.3.2.2 christos * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 christos * modification, are permitted provided that the following conditions
12 1.3.2.2 christos * are met:
13 1.3.2.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 christos * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 christos * documentation and/or other materials provided with the distribution.
18 1.3.2.2 christos *
19 1.3.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3.2.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3.2.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3.2.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3.2.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3.2.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3.2.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3.2.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3.2.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3.2.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3.2.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.3.2.2 christos */
31 1.3.2.2 christos
32 1.3.2.2 christos #include "opt_console.h"
33 1.3.2.2 christos #include "locators.h"
34 1.3.2.2 christos
35 1.3.2.2 christos #include <sys/cdefs.h>
36 1.3.2.2 christos
37 1.3.2.2 christos __KERNEL_RCSID(1, "$NetBSD: meson_uart.c,v 1.3.2.2 2019/06/10 22:05:51 christos Exp $");
38 1.3.2.2 christos
39 1.3.2.2 christos #define cn_trap() \
40 1.3.2.2 christos do { \
41 1.3.2.2 christos console_debugger(); \
42 1.3.2.2 christos cn_trapped = 1; \
43 1.3.2.2 christos } while (/* CONSTCOND */ 0)
44 1.3.2.2 christos
45 1.3.2.2 christos #include <sys/param.h>
46 1.3.2.2 christos #include <sys/bus.h>
47 1.3.2.2 christos #include <sys/device.h>
48 1.3.2.2 christos #include <sys/conf.h>
49 1.3.2.2 christos #include <sys/intr.h>
50 1.3.2.2 christos #include <sys/systm.h>
51 1.3.2.2 christos #include <sys/time.h>
52 1.3.2.2 christos #include <sys/termios.h>
53 1.3.2.2 christos #include <sys/kauth.h>
54 1.3.2.2 christos #include <sys/lwp.h>
55 1.3.2.2 christos #include <sys/tty.h>
56 1.3.2.2 christos
57 1.3.2.2 christos #include <dev/cons.h>
58 1.3.2.2 christos
59 1.3.2.2 christos #include <dev/fdt/fdtvar.h>
60 1.3.2.2 christos
61 1.3.2.2 christos #include <arm/amlogic/meson_uart.h>
62 1.3.2.2 christos
63 1.3.2.2 christos static int meson_uart_match(device_t, cfdata_t, void *);
64 1.3.2.2 christos static void meson_uart_attach(device_t, device_t, void *);
65 1.3.2.2 christos
66 1.3.2.2 christos static int meson_uart_intr(void *);
67 1.3.2.2 christos static void meson_uart_rxsoft(void *);
68 1.3.2.2 christos
69 1.3.2.2 christos static int meson_uart_cngetc(dev_t);
70 1.3.2.2 christos static void meson_uart_cnputc(dev_t, int);
71 1.3.2.2 christos static void meson_uart_cnpollc(dev_t, int);
72 1.3.2.2 christos
73 1.3.2.2 christos static void meson_uart_start(struct tty *);
74 1.3.2.2 christos static int meson_uart_param(struct tty *, struct termios *);
75 1.3.2.2 christos
76 1.3.2.2 christos extern struct cfdriver mesonuart_cd;
77 1.3.2.2 christos
78 1.3.2.2 christos static const char * const compatible[] = {
79 1.3.2.2 christos "amlogic,meson6-uart",
80 1.3.2.2 christos "amlogic,meson8-uart",
81 1.3.2.2 christos "amlogic,meson8b-uart",
82 1.3.2.2 christos "amlogic,meson-gx-uart",
83 1.3.2.2 christos NULL
84 1.3.2.2 christos };
85 1.3.2.2 christos
86 1.3.2.2 christos struct meson_uart_softc {
87 1.3.2.2 christos device_t sc_dev;
88 1.3.2.2 christos bus_space_tag_t sc_bst;
89 1.3.2.2 christos bus_space_handle_t sc_bsh;
90 1.3.2.2 christos kmutex_t sc_intr_lock;
91 1.3.2.2 christos void *sc_ih;
92 1.3.2.2 christos void *sc_sih;
93 1.3.2.2 christos
94 1.3.2.2 christos struct tty *sc_tty;
95 1.3.2.2 christos
96 1.3.2.2 christos int sc_ospeed;
97 1.3.2.2 christos unsigned int sc_rbuf_w; /* write ptr of sc_rbuf[] */
98 1.3.2.2 christos unsigned int sc_rbuf_r; /* read ptr of sc_rbuf[] */
99 1.3.2.2 christos tcflag_t sc_cflag;
100 1.3.2.2 christos
101 1.3.2.2 christos u_char sc_buf[1024];
102 1.3.2.2 christos #define MESON_RBUFSZ 128 /* must be 2^n */
103 1.3.2.2 christos u_char sc_rbuf[MESON_RBUFSZ]; /* good enough for sizeof RXFIFO */
104 1.3.2.2 christos };
105 1.3.2.2 christos
106 1.3.2.2 christos static int meson_uart_console_phandle = -1;
107 1.3.2.2 christos
108 1.3.2.2 christos static struct meson_uart_softc meson_uart_cnsc;
109 1.3.2.2 christos
110 1.3.2.2 christos static struct cnm_state meson_uart_cnm_state;
111 1.3.2.2 christos
112 1.3.2.2 christos struct consdev meson_uart_consdev = {
113 1.3.2.2 christos .cn_getc = meson_uart_cngetc,
114 1.3.2.2 christos .cn_putc = meson_uart_cnputc,
115 1.3.2.2 christos .cn_pollc = meson_uart_cnpollc,
116 1.3.2.2 christos .cn_dev = NODEV,
117 1.3.2.2 christos .cn_pri = CN_NORMAL,
118 1.3.2.2 christos };
119 1.3.2.2 christos
120 1.3.2.2 christos static dev_type_open(meson_uart_open);
121 1.3.2.2 christos static dev_type_open(meson_uart_close);
122 1.3.2.2 christos static dev_type_read(meson_uart_read);
123 1.3.2.2 christos static dev_type_write(meson_uart_write);
124 1.3.2.2 christos static dev_type_ioctl(meson_uart_ioctl);
125 1.3.2.2 christos static dev_type_tty(meson_uart_tty);
126 1.3.2.2 christos static dev_type_poll(meson_uart_poll);
127 1.3.2.2 christos static dev_type_stop(meson_uart_stop);
128 1.3.2.2 christos
129 1.3.2.2 christos const struct cdevsw mesonuart_cdevsw = {
130 1.3.2.2 christos .d_open = meson_uart_open,
131 1.3.2.2 christos .d_close = meson_uart_close,
132 1.3.2.2 christos .d_read = meson_uart_read,
133 1.3.2.2 christos .d_write = meson_uart_write,
134 1.3.2.2 christos .d_ioctl = meson_uart_ioctl,
135 1.3.2.2 christos .d_stop = meson_uart_stop,
136 1.3.2.2 christos .d_tty = meson_uart_tty,
137 1.3.2.2 christos .d_poll = meson_uart_poll,
138 1.3.2.2 christos .d_mmap = nommap,
139 1.3.2.2 christos .d_kqfilter = ttykqfilter,
140 1.3.2.2 christos .d_discard = nodiscard,
141 1.3.2.2 christos .d_flag = D_TTY
142 1.3.2.2 christos };
143 1.3.2.2 christos
144 1.3.2.2 christos static int meson_uart_cmajor = -1;
145 1.3.2.2 christos
146 1.3.2.2 christos CFATTACH_DECL_NEW(meson_uart, sizeof(struct meson_uart_softc),
147 1.3.2.2 christos meson_uart_match, meson_uart_attach, NULL, NULL);
148 1.3.2.2 christos
149 1.3.2.2 christos static int
150 1.3.2.2 christos meson_uart_match(device_t parent, cfdata_t cf, void *aux)
151 1.3.2.2 christos {
152 1.3.2.2 christos struct fdt_attach_args * const faa = aux;
153 1.3.2.2 christos
154 1.3.2.2 christos return of_match_compatible(faa->faa_phandle, compatible);
155 1.3.2.2 christos }
156 1.3.2.2 christos
157 1.3.2.2 christos static void
158 1.3.2.2 christos meson_uart_attach(device_t parent, device_t self, void *aux)
159 1.3.2.2 christos {
160 1.3.2.2 christos struct meson_uart_softc * const sc = device_private(self);
161 1.3.2.2 christos struct fdt_attach_args * const faa = aux;
162 1.3.2.2 christos const int phandle = faa->faa_phandle;
163 1.3.2.2 christos char intrstr[128];
164 1.3.2.2 christos bus_addr_t addr;
165 1.3.2.2 christos bus_size_t size;
166 1.3.2.2 christos struct tty *tp;
167 1.3.2.2 christos int major, minor, error;
168 1.3.2.2 christos uint32_t misc, control;
169 1.3.2.2 christos
170 1.3.2.2 christos if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
171 1.3.2.2 christos aprint_error(": couldn't get registers\n");
172 1.3.2.2 christos return;
173 1.3.2.2 christos }
174 1.3.2.2 christos
175 1.3.2.2 christos sc->sc_dev = self;
176 1.3.2.2 christos sc->sc_bst = faa->faa_bst;
177 1.3.2.2 christos
178 1.3.2.2 christos error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
179 1.3.2.2 christos if (error != 0) {
180 1.3.2.2 christos aprint_error(": couldn't map registers\n");
181 1.3.2.2 christos return;
182 1.3.2.2 christos }
183 1.3.2.2 christos
184 1.3.2.2 christos if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
185 1.3.2.2 christos aprint_error(": failed to decode interrupt\n");
186 1.3.2.2 christos return;
187 1.3.2.2 christos }
188 1.3.2.2 christos
189 1.3.2.2 christos mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL);
190 1.3.2.2 christos sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SERIAL,
191 1.3.2.2 christos FDT_INTR_MPSAFE, meson_uart_intr, sc);
192 1.3.2.2 christos if (sc->sc_ih == NULL) {
193 1.3.2.2 christos aprint_error(": failed to establish interrupt on %s\n",
194 1.3.2.2 christos intrstr);
195 1.3.2.2 christos return;
196 1.3.2.2 christos }
197 1.3.2.2 christos
198 1.3.2.2 christos sc->sc_sih = softint_establish(SOFTINT_SERIAL, meson_uart_rxsoft, sc);
199 1.3.2.2 christos if (sc->sc_sih == NULL) {
200 1.3.2.2 christos aprint_error(": failed to establish softint\n");
201 1.3.2.2 christos return;
202 1.3.2.2 christos }
203 1.3.2.2 christos
204 1.3.2.2 christos if (meson_uart_cmajor == -1) {
205 1.3.2.2 christos /* allocate a major number */
206 1.3.2.2 christos int bmajor = -1, cmajor = -1;
207 1.3.2.2 christos error = devsw_attach("mesonuart", NULL, &bmajor,
208 1.3.2.2 christos &mesonuart_cdevsw, &cmajor);
209 1.3.2.2 christos if (error) {
210 1.3.2.2 christos aprint_error(": couldn't allocate major number\n");
211 1.3.2.2 christos return;
212 1.3.2.2 christos }
213 1.3.2.2 christos meson_uart_cmajor = cmajor;
214 1.3.2.2 christos }
215 1.3.2.2 christos
216 1.3.2.2 christos major = cdevsw_lookup_major(&mesonuart_cdevsw);
217 1.3.2.2 christos minor = device_unit(self);
218 1.3.2.2 christos
219 1.3.2.2 christos tp = sc->sc_tty = tty_alloc();
220 1.3.2.2 christos tp->t_oproc = meson_uart_start;
221 1.3.2.2 christos tp->t_param = meson_uart_param;
222 1.3.2.2 christos tp->t_dev = makedev(major, minor);
223 1.3.2.2 christos tp->t_sc = sc;
224 1.3.2.2 christos tty_attach(tp);
225 1.3.2.2 christos
226 1.3.2.2 christos aprint_naive("\n");
227 1.3.2.2 christos if (meson_uart_console_phandle == phandle) {
228 1.3.2.2 christos cn_tab->cn_dev = tp->t_dev;
229 1.3.2.2 christos aprint_normal(": console");
230 1.3.2.2 christos }
231 1.3.2.2 christos aprint_normal("\n");
232 1.3.2.2 christos
233 1.3.2.2 christos aprint_normal_dev(self, "interrupting on %s\n", intrstr);
234 1.3.2.2 christos
235 1.3.2.2 christos misc = bus_space_read_4(sc->sc_bst, sc->sc_bsh, UART_MISC_REG);
236 1.3.2.2 christos misc &= ~UART_MISC_TX_IRQ_CNT;
237 1.3.2.2 christos misc |= __SHIFTIN(0, UART_MISC_TX_IRQ_CNT);
238 1.3.2.2 christos misc &= ~UART_MISC_RX_IRQ_CNT;
239 1.3.2.2 christos misc |= __SHIFTIN(1, UART_MISC_RX_IRQ_CNT);
240 1.3.2.2 christos bus_space_write_4(sc->sc_bst, sc->sc_bsh, UART_MISC_REG, misc);
241 1.3.2.2 christos
242 1.3.2.2 christos control = bus_space_read_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG);
243 1.3.2.2 christos control &= ~(UART_CONTROL_TX_INT_EN|UART_CONTROL_RX_INT_EN);
244 1.3.2.2 christos bus_space_write_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG, control);
245 1.3.2.2 christos }
246 1.3.2.2 christos
247 1.3.2.2 christos static int
248 1.3.2.2 christos meson_uart_cngetc(dev_t dev)
249 1.3.2.2 christos {
250 1.3.2.2 christos bus_space_tag_t bst = meson_uart_cnsc.sc_bst;
251 1.3.2.2 christos bus_space_handle_t bsh = meson_uart_cnsc.sc_bsh;
252 1.3.2.2 christos uint32_t status;
253 1.3.2.2 christos int s, c;
254 1.3.2.2 christos
255 1.3.2.2 christos s = splserial();
256 1.3.2.2 christos
257 1.3.2.2 christos status = bus_space_read_4(bst, bsh, UART_STATUS_REG);
258 1.3.2.2 christos if (status & UART_STATUS_RX_EMPTY) {
259 1.3.2.2 christos splx(s);
260 1.3.2.2 christos return -1;
261 1.3.2.2 christos }
262 1.3.2.2 christos
263 1.3.2.2 christos c = bus_space_read_4(bst, bsh, UART_RFIFO_REG);
264 1.3.2.2 christos #if defined(DDB)
265 1.3.2.2 christos extern int db_active;
266 1.3.2.2 christos if (!db_active)
267 1.3.2.2 christos #endif
268 1.3.2.2 christos {
269 1.3.2.2 christos int cn_trapped __unused = 0;
270 1.3.2.2 christos cn_check_magic(dev, c, meson_uart_cnm_state);
271 1.3.2.2 christos }
272 1.3.2.2 christos
273 1.3.2.2 christos splx(s);
274 1.3.2.2 christos
275 1.3.2.2 christos return c & 0xff;
276 1.3.2.2 christos }
277 1.3.2.2 christos
278 1.3.2.2 christos static void
279 1.3.2.2 christos meson_uart_cnputc(dev_t dev, int c)
280 1.3.2.2 christos {
281 1.3.2.2 christos bus_space_tag_t bst = meson_uart_cnsc.sc_bst;
282 1.3.2.2 christos bus_space_handle_t bsh = meson_uart_cnsc.sc_bsh;
283 1.3.2.2 christos int s;
284 1.3.2.2 christos
285 1.3.2.2 christos s = splserial();
286 1.3.2.2 christos
287 1.3.2.2 christos while ((bus_space_read_4(bst, bsh, UART_STATUS_REG) & UART_STATUS_TX_FULL) != 0)
288 1.3.2.2 christos ;
289 1.3.2.2 christos
290 1.3.2.2 christos bus_space_write_4(bst, bsh, UART_WFIFO_REG, c);
291 1.3.2.2 christos
292 1.3.2.2 christos splx(s);
293 1.3.2.2 christos }
294 1.3.2.2 christos
295 1.3.2.2 christos
296 1.3.2.2 christos static void
297 1.3.2.2 christos meson_uart_cnpollc(dev_t dev, int on)
298 1.3.2.2 christos {
299 1.3.2.2 christos }
300 1.3.2.2 christos
301 1.3.2.2 christos static int
302 1.3.2.2 christos meson_uart_open(dev_t dev, int flag, int mode, lwp_t *l)
303 1.3.2.2 christos {
304 1.3.2.2 christos struct meson_uart_softc *sc =
305 1.3.2.2 christos device_lookup_private(&mesonuart_cd, minor(dev));
306 1.3.2.2 christos struct tty *tp = sc->sc_tty;
307 1.3.2.2 christos uint32_t control;
308 1.3.2.2 christos
309 1.3.2.2 christos if (kauth_authorize_device_tty(l->l_cred,
310 1.3.2.2 christos KAUTH_DEVICE_TTY_OPEN, tp) != 0) {
311 1.3.2.2 christos return EBUSY;
312 1.3.2.2 christos }
313 1.3.2.2 christos
314 1.3.2.2 christos if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
315 1.3.2.2 christos tp->t_dev = dev;
316 1.3.2.2 christos ttychars(tp);
317 1.3.2.2 christos tp->t_iflag = TTYDEF_IFLAG;
318 1.3.2.2 christos tp->t_oflag = TTYDEF_OFLAG;
319 1.3.2.2 christos tp->t_cflag = TTYDEF_CFLAG;
320 1.3.2.2 christos tp->t_lflag = TTYDEF_LFLAG;
321 1.3.2.2 christos tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
322 1.3.2.2 christos ttsetwater(tp);
323 1.3.2.2 christos
324 1.3.2.2 christos control = bus_space_read_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG);
325 1.3.2.2 christos control |= UART_CONTROL_RX_INT_EN;
326 1.3.2.2 christos bus_space_write_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG, control);
327 1.3.2.2 christos }
328 1.3.2.2 christos tp->t_state |= TS_CARR_ON;
329 1.3.2.2 christos
330 1.3.2.2 christos return tp->t_linesw->l_open(dev, tp);
331 1.3.2.2 christos }
332 1.3.2.2 christos
333 1.3.2.2 christos static int
334 1.3.2.2 christos meson_uart_close(dev_t dev, int flag, int mode, lwp_t *l)
335 1.3.2.2 christos {
336 1.3.2.2 christos struct meson_uart_softc *sc =
337 1.3.2.2 christos device_lookup_private(&mesonuart_cd, minor(dev));
338 1.3.2.2 christos struct tty *tp = sc->sc_tty;
339 1.3.2.2 christos uint32_t control;
340 1.3.2.2 christos
341 1.3.2.2 christos tp->t_linesw->l_close(tp, flag);
342 1.3.2.2 christos ttyclose(tp);
343 1.3.2.2 christos
344 1.3.2.2 christos if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
345 1.3.2.2 christos control = bus_space_read_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG);
346 1.3.2.2 christos control &= ~UART_CONTROL_RX_INT_EN;
347 1.3.2.2 christos bus_space_write_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG, control);
348 1.3.2.2 christos }
349 1.3.2.2 christos
350 1.3.2.2 christos return 0;
351 1.3.2.2 christos }
352 1.3.2.2 christos
353 1.3.2.2 christos static int
354 1.3.2.2 christos meson_uart_read(dev_t dev, struct uio *uio, int flag)
355 1.3.2.2 christos {
356 1.3.2.2 christos struct meson_uart_softc *sc =
357 1.3.2.2 christos device_lookup_private(&mesonuart_cd, minor(dev));
358 1.3.2.2 christos struct tty *tp = sc->sc_tty;
359 1.3.2.2 christos
360 1.3.2.2 christos return tp->t_linesw->l_read(tp, uio, flag);
361 1.3.2.2 christos }
362 1.3.2.2 christos
363 1.3.2.2 christos static int
364 1.3.2.2 christos meson_uart_write(dev_t dev, struct uio *uio, int flag)
365 1.3.2.2 christos {
366 1.3.2.2 christos struct meson_uart_softc *sc =
367 1.3.2.2 christos device_lookup_private(&mesonuart_cd, minor(dev));
368 1.3.2.2 christos struct tty *tp = sc->sc_tty;
369 1.3.2.2 christos
370 1.3.2.2 christos return tp->t_linesw->l_write(tp, uio, flag);
371 1.3.2.2 christos }
372 1.3.2.2 christos
373 1.3.2.2 christos static int
374 1.3.2.2 christos meson_uart_poll(dev_t dev, int events, lwp_t *l)
375 1.3.2.2 christos {
376 1.3.2.2 christos struct meson_uart_softc *sc =
377 1.3.2.2 christos device_lookup_private(&mesonuart_cd, minor(dev));
378 1.3.2.2 christos struct tty *tp = sc->sc_tty;
379 1.3.2.2 christos
380 1.3.2.2 christos return tp->t_linesw->l_poll(tp, events, l);
381 1.3.2.2 christos }
382 1.3.2.2 christos
383 1.3.2.2 christos static int
384 1.3.2.2 christos meson_uart_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
385 1.3.2.2 christos {
386 1.3.2.2 christos struct meson_uart_softc *sc =
387 1.3.2.2 christos device_lookup_private(&mesonuart_cd, minor(dev));
388 1.3.2.2 christos struct tty *tp = sc->sc_tty;
389 1.3.2.2 christos int error;
390 1.3.2.2 christos
391 1.3.2.2 christos error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
392 1.3.2.2 christos if (error != EPASSTHROUGH)
393 1.3.2.2 christos return error;
394 1.3.2.2 christos
395 1.3.2.2 christos return ttioctl(tp, cmd, data, flag, l);
396 1.3.2.2 christos }
397 1.3.2.2 christos
398 1.3.2.2 christos static struct tty *
399 1.3.2.2 christos meson_uart_tty(dev_t dev)
400 1.3.2.2 christos {
401 1.3.2.2 christos struct meson_uart_softc *sc =
402 1.3.2.2 christos device_lookup_private(&mesonuart_cd, minor(dev));
403 1.3.2.2 christos
404 1.3.2.2 christos return sc->sc_tty;
405 1.3.2.2 christos }
406 1.3.2.2 christos
407 1.3.2.2 christos static void
408 1.3.2.2 christos meson_uart_stop(struct tty *tp, int flag)
409 1.3.2.2 christos {
410 1.3.2.2 christos }
411 1.3.2.2 christos
412 1.3.2.2 christos static void
413 1.3.2.2 christos meson_uart_start(struct tty *tp)
414 1.3.2.2 christos {
415 1.3.2.2 christos struct meson_uart_softc *sc = tp->t_sc;
416 1.3.2.2 christos u_char *p = sc->sc_buf;
417 1.3.2.2 christos int s, brem;
418 1.3.2.2 christos
419 1.3.2.2 christos s = spltty();
420 1.3.2.2 christos
421 1.3.2.2 christos if (tp->t_state & (TS_TTSTOP | TS_BUSY | TS_TIMEOUT)) {
422 1.3.2.2 christos splx(s);
423 1.3.2.2 christos return;
424 1.3.2.2 christos }
425 1.3.2.2 christos tp->t_state |= TS_BUSY;
426 1.3.2.2 christos
427 1.3.2.2 christos splx(s);
428 1.3.2.2 christos
429 1.3.2.2 christos for (brem = q_to_b(&tp->t_outq, sc->sc_buf, sizeof(sc->sc_buf));
430 1.3.2.2 christos brem > 0;
431 1.3.2.2 christos brem--, p++) {
432 1.3.2.2 christos while ((bus_space_read_4(sc->sc_bst, sc->sc_bsh,
433 1.3.2.2 christos UART_STATUS_REG) & UART_STATUS_TX_FULL) != 0)
434 1.3.2.2 christos ;
435 1.3.2.2 christos
436 1.3.2.2 christos bus_space_write_4(sc->sc_bst, sc->sc_bsh,
437 1.3.2.2 christos UART_WFIFO_REG, *p);
438 1.3.2.2 christos }
439 1.3.2.2 christos
440 1.3.2.2 christos s = spltty();
441 1.3.2.2 christos tp->t_state &= ~TS_BUSY;
442 1.3.2.2 christos if (ttypull(tp)) {
443 1.3.2.2 christos tp->t_state |= TS_TIMEOUT;
444 1.3.2.2 christos callout_schedule(&tp->t_rstrt_ch, 1);
445 1.3.2.2 christos }
446 1.3.2.2 christos splx(s);
447 1.3.2.2 christos }
448 1.3.2.2 christos
449 1.3.2.2 christos static int
450 1.3.2.2 christos meson_uart_param(struct tty *tp, struct termios *t)
451 1.3.2.2 christos {
452 1.3.2.2 christos
453 1.3.2.2 christos tp->t_ispeed = t->c_ispeed;
454 1.3.2.2 christos tp->t_ospeed = t->c_ospeed;
455 1.3.2.2 christos tp->t_cflag = t->c_cflag;
456 1.3.2.2 christos
457 1.3.2.2 christos return 0;
458 1.3.2.2 christos }
459 1.3.2.2 christos
460 1.3.2.2 christos static int
461 1.3.2.2 christos meson_uart_intr(void *priv)
462 1.3.2.2 christos {
463 1.3.2.2 christos struct meson_uart_softc *sc = priv;
464 1.3.2.2 christos struct tty *tp = sc->sc_tty;
465 1.3.2.2 christos uint32_t status, c;
466 1.3.2.2 christos
467 1.3.2.2 christos mutex_spin_enter(&sc->sc_intr_lock);
468 1.3.2.2 christos for (;;) {
469 1.3.2.2 christos int cn_trapped = 0;
470 1.3.2.2 christos status = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
471 1.3.2.2 christos UART_STATUS_REG);
472 1.3.2.2 christos if (status & UART_STATUS_RX_EMPTY) {
473 1.3.2.2 christos break;
474 1.3.2.2 christos }
475 1.3.2.2 christos if (status & UART_STATUS_BREAK) {
476 1.3.2.2 christos cn_check_magic(tp->t_dev, CNC_BREAK,
477 1.3.2.2 christos meson_uart_cnm_state);
478 1.3.2.2 christos if (cn_trapped)
479 1.3.2.2 christos continue;
480 1.3.2.2 christos }
481 1.3.2.2 christos
482 1.3.2.2 christos c = bus_space_read_4(sc->sc_bst, sc->sc_bsh, UART_RFIFO_REG);
483 1.3.2.2 christos cn_check_magic(tp->t_dev, c & 0xff, meson_uart_cnm_state);
484 1.3.2.2 christos if (cn_trapped)
485 1.3.2.2 christos continue;
486 1.3.2.2 christos
487 1.3.2.2 christos if ((sc->sc_rbuf_w - sc->sc_rbuf_r) >= (MESON_RBUFSZ - 1))
488 1.3.2.2 christos continue;
489 1.3.2.2 christos sc->sc_rbuf[sc->sc_rbuf_w++ & (MESON_RBUFSZ - 1)] = c;
490 1.3.2.2 christos }
491 1.3.2.2 christos mutex_spin_exit(&sc->sc_intr_lock);
492 1.3.2.2 christos
493 1.3.2.2 christos if (sc->sc_rbuf_w != sc->sc_rbuf_r)
494 1.3.2.2 christos softint_schedule(sc->sc_sih);
495 1.3.2.2 christos
496 1.3.2.2 christos return 0;
497 1.3.2.2 christos }
498 1.3.2.2 christos
499 1.3.2.2 christos static void
500 1.3.2.2 christos meson_uart_rxsoft(void *priv)
501 1.3.2.2 christos {
502 1.3.2.2 christos struct meson_uart_softc *sc = priv;
503 1.3.2.2 christos struct tty *tp = sc->sc_tty;
504 1.3.2.2 christos int c;
505 1.3.2.2 christos
506 1.3.2.2 christos while (sc->sc_rbuf_w != sc->sc_rbuf_r) {
507 1.3.2.2 christos c = sc->sc_rbuf[sc->sc_rbuf_r++ & (MESON_RBUFSZ - 1)];
508 1.3.2.2 christos if (tp->t_linesw->l_rint(c & 0xff, tp) == -1)
509 1.3.2.2 christos break;
510 1.3.2.2 christos }
511 1.3.2.2 christos }
512 1.3.2.2 christos
513 1.3.2.2 christos static int
514 1.3.2.2 christos meson_uart_console_match(int phandle)
515 1.3.2.2 christos {
516 1.3.2.2 christos return of_match_compatible(phandle, compatible);
517 1.3.2.2 christos }
518 1.3.2.2 christos
519 1.3.2.2 christos static void
520 1.3.2.2 christos meson_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
521 1.3.2.2 christos {
522 1.3.2.2 christos struct meson_uart_softc *sc = &meson_uart_cnsc;
523 1.3.2.2 christos const int phandle = faa->faa_phandle;
524 1.3.2.2 christos bus_addr_t addr;
525 1.3.2.2 christos bus_size_t size;
526 1.3.2.2 christos int error;
527 1.3.2.2 christos
528 1.3.2.2 christos fdtbus_get_reg(phandle, 0, &addr, &size);
529 1.3.2.2 christos
530 1.3.2.2 christos sc->sc_bst = faa->faa_bst;
531 1.3.2.2 christos sc->sc_ospeed = fdtbus_get_stdout_speed();
532 1.3.2.2 christos if (sc->sc_ospeed < 0)
533 1.3.2.2 christos sc->sc_ospeed = 115200;
534 1.3.2.2 christos sc->sc_cflag = fdtbus_get_stdout_flags();
535 1.3.2.2 christos
536 1.3.2.2 christos error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
537 1.3.2.2 christos if (error != 0)
538 1.3.2.2 christos panic("failed to map console, error = %d", error);
539 1.3.2.2 christos
540 1.3.2.2 christos cn_tab = &meson_uart_consdev;
541 1.3.2.2 christos cn_init_magic(&meson_uart_cnm_state);
542 1.3.2.2 christos cn_set_magic("\047\001");
543 1.3.2.2 christos
544 1.3.2.2 christos meson_uart_console_phandle = phandle;
545 1.3.2.2 christos }
546 1.3.2.2 christos
547 1.3.2.2 christos static const struct fdt_console meson_uart_console = {
548 1.3.2.2 christos .match = meson_uart_console_match,
549 1.3.2.2 christos .consinit = meson_uart_console_consinit,
550 1.3.2.2 christos };
551 1.3.2.2 christos
552 1.3.2.2 christos FDT_CONSOLE(meson_uart, &meson_uart_console);
553