ingenic_com.c revision 1.8 1 1.8 thorpej /* $NetBSD: ingenic_com.c,v 1.8 2018/12/08 21:14:36 thorpej Exp $ */
2 1.1 macallan
3 1.1 macallan /*-
4 1.1 macallan * Copyright (c) 2014 Michael Lorenz
5 1.1 macallan * All rights reserved.
6 1.1 macallan *
7 1.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1 macallan * modification, are permitted provided that the following conditions
9 1.1 macallan * are met:
10 1.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1 macallan *
16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 macallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 macallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 macallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 macallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 macallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 macallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 macallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 macallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 macallan * POSSIBILITY OF SUCH DAMAGE.
27 1.1 macallan */
28 1.1 macallan
29 1.1 macallan #include <sys/cdefs.h>
30 1.8 thorpej __KERNEL_RCSID(0, "$NetBSD: ingenic_com.c,v 1.8 2018/12/08 21:14:36 thorpej Exp $");
31 1.1 macallan
32 1.1 macallan #include <sys/param.h>
33 1.1 macallan #include <sys/systm.h>
34 1.1 macallan #include <sys/device.h>
35 1.1 macallan #include <sys/kernel.h>
36 1.1 macallan #include <sys/termios.h>
37 1.1 macallan #include <sys/ttydefaults.h>
38 1.1 macallan #include <sys/types.h>
39 1.1 macallan
40 1.1 macallan #include <sys/bus.h>
41 1.1 macallan
42 1.1 macallan #include <dev/cons.h>
43 1.1 macallan #include <dev/ic/comreg.h>
44 1.1 macallan #include <dev/ic/comvar.h>
45 1.1 macallan
46 1.1 macallan #include <mips/cpuregs.h>
47 1.1 macallan
48 1.5 macallan #include <mips/ingenic/ingenic_var.h>
49 1.1 macallan #include <mips/ingenic/ingenic_regs.h>
50 1.1 macallan
51 1.1 macallan volatile int32_t *com0addr = (int32_t *)MIPS_PHYS_TO_KSEG1(JZ_UART0);
52 1.1 macallan
53 1.1 macallan void ingenic_putchar_init(void);
54 1.1 macallan void ingenic_puts(const char *);
55 1.1 macallan void ingenic_putchar(char);
56 1.1 macallan
57 1.1 macallan #ifndef CONMODE
58 1.4 macallan # define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
59 1.1 macallan #endif
60 1.1 macallan
61 1.1 macallan void ingenic_com_cnattach(void);
62 1.1 macallan
63 1.1 macallan static int ingenic_com_match(device_t, cfdata_t , void *);
64 1.1 macallan static void ingenic_com_attach(device_t, device_t, void *);
65 1.1 macallan
66 1.1 macallan struct ingenic_com_softc {
67 1.1 macallan struct com_softc sc_com;
68 1.5 macallan bus_space_tag_t sc_tag;
69 1.5 macallan bus_space_handle_t sc_regh;
70 1.1 macallan };
71 1.1 macallan
72 1.5 macallan CFATTACH_DECL_NEW(ingenic_com, sizeof(struct ingenic_com_softc),
73 1.1 macallan ingenic_com_match, ingenic_com_attach, NULL, NULL);
74 1.1 macallan
75 1.5 macallan static bus_space_handle_t regh = 0;
76 1.5 macallan static bus_addr_t cons_com = 0;
77 1.7 thorpej static struct com_regs cons_regs;
78 1.5 macallan extern bus_space_tag_t apbus_memt;
79 1.1 macallan
80 1.7 thorpej static void
81 1.7 thorpej ingenic_com_init_regs(struct com_regs *regs, bus_space_tag_t st,
82 1.7 thorpej bus_space_handle_t sh, bus_addr_t addr)
83 1.7 thorpej {
84 1.7 thorpej
85 1.7 thorpej com_init_regs(regs, st, sh, addr);
86 1.7 thorpej for (size_t i = 0; i < __arraycount(regs->cr_map); i++) {
87 1.7 thorpej regs->cr_map[i] = regs->cr_map[i] << 2;
88 1.7 thorpej }
89 1.7 thorpej regs->cr_nports <<= 2;
90 1.7 thorpej }
91 1.7 thorpej
92 1.1 macallan void
93 1.1 macallan ingenic_putchar_init(void)
94 1.1 macallan {
95 1.1 macallan /*
96 1.1 macallan * XXX don't screw with the UART's speed until we know what clock
97 1.1 macallan * we're on
98 1.1 macallan */
99 1.1 macallan #if 0
100 1.1 macallan int rate;
101 1.1 macallan #endif
102 1.1 macallan extern int comspeed(long, long, int);
103 1.1 macallan
104 1.1 macallan com0addr = (uint32_t *)MIPS_PHYS_TO_KSEG1(JZ_UART0);
105 1.1 macallan #if 0
106 1.1 macallan if (comcnfreq != -1) {
107 1.1 macallan rate = comspeed(comcnspeed, comcnfreq, COM_TYPE_INGENIC);
108 1.1 macallan if (rate < 0)
109 1.1 macallan return; /* XXX */
110 1.1 macallan #endif
111 1.1 macallan com0addr[com_ier] = 0;
112 1.1 macallan com0addr[com_lctl] = htole32(LCR_DLAB);
113 1.1 macallan #if 0
114 1.1 macallan com0addr[com_dlbl] = htole32(rate & 0xff);
115 1.1 macallan com0addr[com_dlbh] = htole32(rate >> 8);
116 1.1 macallan #endif
117 1.1 macallan com0addr[com_lctl] = htole32(LCR_8BITS); /* XXX */
118 1.1 macallan com0addr[com_mcr] = htole32(MCR_DTR|MCR_RTS);
119 1.1 macallan com0addr[com_fifo] = htole32(
120 1.6 skrll FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
121 1.1 macallan FIFO_TRIGGER_1 | FIFO_UART_ON);
122 1.1 macallan #if 0
123 1.1 macallan }
124 1.1 macallan #endif
125 1.1 macallan }
126 1.1 macallan
127 1.1 macallan
128 1.1 macallan void
129 1.1 macallan ingenic_putchar(char c)
130 1.1 macallan {
131 1.1 macallan int timo = 150000;
132 1.1 macallan
133 1.1 macallan while ((le32toh(com0addr[com_lsr]) & LSR_TXRDY) == 0)
134 1.1 macallan if (--timo == 0)
135 1.1 macallan break;
136 1.1 macallan
137 1.1 macallan com0addr[com_data] = htole32((uint32_t)c);
138 1.1 macallan
139 1.1 macallan while ((le32toh(com0addr[com_lsr]) & LSR_TSRE) == 0)
140 1.1 macallan if (--timo == 0)
141 1.1 macallan break;
142 1.1 macallan }
143 1.1 macallan
144 1.1 macallan void
145 1.1 macallan ingenic_puts(const char *restrict s)
146 1.1 macallan {
147 1.1 macallan char c;
148 1.1 macallan
149 1.1 macallan while ((c = *s++) != 0)
150 1.1 macallan ingenic_putchar(c);
151 1.1 macallan }
152 1.1 macallan
153 1.1 macallan void
154 1.1 macallan ingenic_com_cnattach(void)
155 1.1 macallan {
156 1.6 skrll
157 1.5 macallan bus_space_map(apbus_memt, JZ_UART0, 0x100, 0, ®h);
158 1.5 macallan cons_com = JZ_UART0;
159 1.7 thorpej ingenic_com_init_regs(&cons_regs, apbus_memt, regh, JZ_UART0);
160 1.1 macallan
161 1.7 thorpej comcnattach1(&cons_regs, 115200, 48000000, COM_TYPE_INGENIC, CONMODE);
162 1.1 macallan }
163 1.1 macallan
164 1.1 macallan static int
165 1.1 macallan ingenic_com_match(device_t parent, cfdata_t cfdata, void *args)
166 1.1 macallan {
167 1.1 macallan struct mainbusdev {
168 1.1 macallan const char *md_name;
169 1.1 macallan } *aa = args;
170 1.1 macallan if (strcmp(aa->md_name, "com") == 0) return 1;
171 1.1 macallan return 0;
172 1.1 macallan }
173 1.1 macallan
174 1.1 macallan
175 1.1 macallan static void
176 1.1 macallan ingenic_com_attach(device_t parent, device_t self, void *args)
177 1.1 macallan {
178 1.1 macallan struct ingenic_com_softc *isc = device_private(self);
179 1.1 macallan struct com_softc *sc = &isc->sc_com;
180 1.5 macallan struct apbus_attach_args *aa = args;
181 1.1 macallan
182 1.1 macallan sc->sc_dev = self;
183 1.4 macallan sc->sc_frequency = 48000000;
184 1.1 macallan sc->sc_type = COM_TYPE_INGENIC;
185 1.5 macallan isc->sc_tag = aa->aa_bst;
186 1.5 macallan
187 1.5 macallan if (cons_com == aa->aa_addr) {
188 1.5 macallan isc->sc_regh = regh;
189 1.5 macallan } else {
190 1.5 macallan bus_space_map(apbus_memt, aa->aa_addr, 0x1000, 0, &isc->sc_regh);
191 1.5 macallan }
192 1.7 thorpej ingenic_com_init_regs(&sc->sc_regs, aa->aa_bst, isc->sc_regh,
193 1.7 thorpej aa->aa_addr);
194 1.5 macallan
195 1.1 macallan com_attach_subr(sc);
196 1.5 macallan evbmips_intr_establish(aa->aa_irq, comintr, sc);
197 1.1 macallan }
198