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