apbus.c revision 1.11.2.4 1 /* $NetBSD: apbus.c,v 1.11.2.4 2015/09/22 12:05:47 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2014 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* catch-all for on-chip peripherals */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: apbus.c,v 1.11.2.4 2015/09/22 12:05:47 skrll Exp $");
33
34 #include "locators.h"
35 #define _MIPS_BUS_DMA_PRIVATE
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/device.h>
40 #include <sys/extent.h>
41 #include <sys/systm.h>
42
43 #include <mips/ingenic/ingenic_var.h>
44 #include <mips/ingenic/ingenic_regs.h>
45
46 #include "opt_ingenic.h"
47
48 static int apbus_match(device_t, cfdata_t, void *);
49 static void apbus_attach(device_t, device_t, void *);
50 static int apbus_print(void *, const char *);
51 static void apbus_bus_mem_init(bus_space_tag_t, void *);
52
53 CFATTACH_DECL_NEW(apbus, 0, apbus_match, apbus_attach, NULL, NULL);
54
55 static struct mips_bus_space apbus_mbst;
56 bus_space_tag_t apbus_memt = NULL;
57
58 struct mips_bus_dma_tag apbus_dmat = {
59 ._bounce_alloc_hi = 0x10000000,
60 ._dmamap_ops = _BUS_DMAMAP_OPS_INITIALIZER,
61 ._dmamem_ops = _BUS_DMAMEM_OPS_INITIALIZER,
62 ._dmatag_ops = _BUS_DMATAG_OPS_INITIALIZER,
63 };
64
65 typedef struct apbus_dev {
66 const char *name; /* driver name */
67 bus_addr_t addr; /* base address */
68 uint32_t irq; /* interrupt */
69 uint32_t clk0; /* bit(s) in CLKGR0 */
70 uint32_t clk1; /* bit(s) in CLKGR1 */
71 uint32_t clkreg; /* CGU register */
72 } apbus_dev_t;
73
74 static const apbus_dev_t apbus_devs[] = {
75 { "com", JZ_UART0, 51, CLK_UART0, 0, 0},
76 { "com", JZ_UART1, 50, CLK_UART1, 0, 0},
77 { "com", JZ_UART2, 49, CLK_UART2, 0, 0},
78 { "com", JZ_UART3, 48, CLK_UART3, 0, 0},
79 { "com", JZ_UART4, 34, 0, CLK_UART4, 0},
80 { "dwctwo", JZ_DWC2_BASE, 21, CLK_OTG0 | CLK_UHC, CLK_OTG1, 0},
81 { "ohci", JZ_OHCI_BASE, 5, CLK_UHC, 0, 0},
82 { "ehci", JZ_EHCI_BASE, 20, CLK_UHC, 0, 0},
83 { "dme", JZ_DME_BASE, -1, 0, 0, 0},
84 { "jzgpio", JZ_GPIO_A_BASE, 17, 0, 0, 0},
85 { "jzgpio", JZ_GPIO_B_BASE, 16, 0, 0, 0},
86 { "jzgpio", JZ_GPIO_C_BASE, 15, 0, 0, 0},
87 { "jzgpio", JZ_GPIO_D_BASE, 14, 0, 0, 0},
88 { "jzgpio", JZ_GPIO_E_BASE, 13, 0, 0, 0},
89 { "jzgpio", JZ_GPIO_F_BASE, 12, 0, 0, 0},
90 { "jziic", JZ_SMB0_BASE, 60, CLK_SMB0, 0, 0},
91 { "jziic", JZ_SMB1_BASE, 59, CLK_SMB1, 0, 0},
92 { "jziic", JZ_SMB2_BASE, 58, CLK_SMB2, 0, 0},
93 { "jziic", JZ_SMB3_BASE, 57, 0, CLK_SMB3, 0},
94 { "jziic", JZ_SMB4_BASE, 56, 0, CLK_SMB4, 0},
95 { "jzmmc", JZ_MSC0_BASE, 37, CLK_MSC0, 0, JZ_MSC0CDR},
96 { "jzmmc", JZ_MSC1_BASE, 36, CLK_MSC1, 0, JZ_MSC1CDR},
97 { "jzmmc", JZ_MSC2_BASE, 35, CLK_MSC2, 0, JZ_MSC2CDR},
98 { "jzfb", JZ_LCDC0_BASE, 31, CLK_LCD, CLK_HDMI, 0},
99 { "jzrng", JZ_RNG, -1, 0, 0, 0},
100 { NULL, -1, -1, 0, 0, 0}
101 };
102
103 void
104 apbus_init(void)
105 {
106 static bool done = false;
107 if (done)
108 return;
109 done = true;
110
111 apbus_bus_mem_init(&apbus_mbst, NULL);
112 apbus_memt = &apbus_mbst;
113 }
114
115 int
116 apbus_match(device_t parent, cfdata_t match, void *aux)
117 {
118 struct mainbusdev {
119 const char *md_name;
120 } *aa = aux;
121 if (strcmp(aa->md_name, "apbus") == 0) return 1;
122 return 0;
123 }
124
125 void
126 apbus_attach(device_t parent, device_t self, void *aux)
127 {
128 uint32_t reg, mpll, m, n, p, mclk, pclk, pdiv, cclk, cdiv;
129 aprint_normal("\n");
130
131 /* should have been called early on */
132 apbus_init();
133
134 #ifdef INGENIC_DEBUG
135 printf("core ctrl: %08x\n", MFC0(12, 2));
136 printf("core status: %08x\n", MFC0(12, 3));
137 printf("REIM: %08x\n", MFC0(12, 4));
138 printf("ID: %08x\n", MFC0(15, 1));
139 #endif
140 /* assuming we're using MPLL */
141 mpll = readreg(JZ_CPMPCR);
142 m = (mpll & JZ_PLLM_M) >> JZ_PLLM_S;
143 n = (mpll & JZ_PLLN_M) >> JZ_PLLN_S;
144 p = (mpll & JZ_PLLP_M) >> JZ_PLLP_S;
145
146 /* assuming 48MHz EXTCLK */
147 mclk = (48000 * (m + 1) / (n + 1)) / (p + 1);
148
149 reg = readreg(JZ_CPCCR);
150 pdiv = ((reg & JZ_PDIV_M) >> JZ_PDIV_S) + 1;
151 pclk = mclk / pdiv;
152 cdiv = (reg & JZ_CDIV_M) + 1;
153 cclk = mclk / cdiv;
154
155 aprint_debug_dev(self, "mclk %d kHz\n", mclk);
156 aprint_debug_dev(self, "pclk %d kHz\n", pclk);
157 aprint_debug_dev(self, "CPU clock %d kHz\n", cclk);
158
159 /* enable clocks */
160 reg = readreg(JZ_CLKGR1);
161 reg &= ~CLK_AHB_MON; /* AHB_MON clock */
162 writereg(JZ_CLKGR1, reg);
163
164 /* enable RNG */
165 writereg(JZ_ERNG, 1);
166
167 /* wake up the USB part */
168 reg = readreg(JZ_OPCR);
169 reg |= OPCR_SPENDN0 | OPCR_SPENDN1;
170 writereg(JZ_OPCR, reg);
171
172 /* wire up GPIOs */
173 /* iic0 */
174 gpio_as_dev0(3, 30);
175 gpio_as_dev0(3, 31);
176 /* iic1 */
177 gpio_as_dev0(4, 30);
178 gpio_as_dev0(4, 31);
179 /* iic2 */
180 gpio_as_dev2(5, 16);
181 gpio_as_dev2(5, 17);
182 /* iic3 */
183 gpio_as_dev1(3, 10);
184 gpio_as_dev1(3, 11);
185 /* iic4 */
186 /* make sure these aren't SMB4 */
187 gpio_as_dev3(4, 3);
188 gpio_as_dev3(4, 4);
189 /* these are supposed to be connected to the RTC */
190 gpio_as_dev1(4, 12);
191 gpio_as_dev1(4, 13);
192 /* these can be DDC2 or SMB4 */
193 #if 0
194 /* DDC2 devices show up at SMB4 */
195 gpio_as_dev1(5, 24);
196 gpio_as_dev1(5, 25);
197 #else
198 gpio_as_dev0(5, 24);
199 gpio_as_dev0(5, 25);
200 #endif
201 /* MSC0 */
202 gpio_as_dev1(0, 4);
203 gpio_as_dev1(0, 5);
204 gpio_as_dev1(0, 6);
205 gpio_as_dev1(0, 7);
206 gpio_as_dev1(0, 18);
207 gpio_as_dev1(0, 19);
208 gpio_as_dev1(0, 20);
209 gpio_as_dev1(0, 21);
210 gpio_as_dev1(0, 22);
211 gpio_as_dev1(0, 23);
212 gpio_as_dev1(0, 24);
213 gpio_as_intr_level_low(5, 20); /* card detect */
214
215 /* MSC1, for wifi/bt */
216 gpio_as_dev0(3, 20);
217 gpio_as_dev0(3, 21);
218 gpio_as_dev0(3, 22);
219 gpio_as_dev0(3, 23);
220 gpio_as_dev0(3, 24);
221 gpio_as_dev0(3, 25);
222
223 /* MSC2, on expansion header */
224 gpio_as_dev0(1, 20);
225 gpio_as_dev0(1, 21);
226 gpio_as_dev0(1, 28);
227 gpio_as_dev0(1, 29);
228 gpio_as_dev0(1, 30);
229 gpio_as_dev0(1, 31);
230
231 #ifndef INGENIC_DEBUG
232 printf("JZ_CLKGR0 %08x\n", readreg(JZ_CLKGR0));
233 printf("JZ_CLKGR1 %08x\n", readreg(JZ_CLKGR1));
234 printf("JZ_SPCR0 %08x\n", readreg(JZ_SPCR0));
235 printf("JZ_SPCR1 %08x\n", readreg(JZ_SPCR1));
236 printf("JZ_SRBC %08x\n", readreg(JZ_SRBC));
237 printf("JZ_OPCR %08x\n", readreg(JZ_OPCR));
238 printf("JZ_UHCCDR %08x\n", readreg(JZ_UHCCDR));
239 printf("JZ_ERNG %08x\n", readreg(JZ_ERNG));
240 printf("JZ_RNG %08x\n", readreg(JZ_RNG));
241 #endif
242
243 for (const apbus_dev_t *adv = apbus_devs; adv->name != NULL; adv++) {
244 struct apbus_attach_args aa;
245 aa.aa_name = adv->name;
246 aa.aa_addr = adv->addr;
247 aa.aa_irq = adv->irq;
248 aa.aa_dmat = &apbus_dmat;
249 aa.aa_bst = apbus_memt;
250 aa.aa_pclk = pclk;
251 aa.aa_mclk = mclk;
252 aa.aa_clockreg = adv->clkreg;
253
254 /* enable clocks as needed */
255 if (adv->clk0 != 0) {
256 reg = readreg(JZ_CLKGR0);
257 reg &= ~adv->clk0;
258 writereg(JZ_CLKGR0, reg);
259 }
260
261 if (adv->clk1 != 0) {
262 reg = readreg(JZ_CLKGR1);
263 reg &= ~adv->clk1;
264 writereg(JZ_CLKGR1, reg);
265 }
266
267 (void) config_found_ia(self, "apbus", &aa, apbus_print);
268 }
269 }
270
271 int
272 apbus_print(void *aux, const char *pnp)
273 {
274 struct apbus_attach_args *aa = aux;
275
276 if (pnp) {
277 aprint_normal("%s at %s", aa->aa_name, pnp);
278 }
279 if (aa->aa_addr != -1)
280 aprint_normal(" addr 0x%" PRIxBUSADDR, aa->aa_addr);
281 if ((pnp == NULL) && (aa->aa_irq != -1))
282 aprint_normal(" irq %d", aa->aa_irq);
283 return (UNCONF);
284 }
285
286 #define CHIP apbus
287 #define CHIP_MEM /* defined */
288 #define CHIP_W1_BUS_START(v) 0x10000000UL
289 #define CHIP_W1_BUS_END(v) 0x20000000UL
290 #define CHIP_W1_SYS_START(v) 0x10000000UL
291 #define CHIP_W1_SYS_END(v) 0x20000000UL
292
293 #include <mips/mips/bus_space_alignstride_chipdep.c>
294