apbus.c revision 1.12 1 1.12 macallan /* $NetBSD: apbus.c,v 1.12 2015/04/21 19:57:41 macallan 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 /* catch-all for on-chip peripherals */
30 1.1 macallan
31 1.1 macallan #include <sys/cdefs.h>
32 1.12 macallan __KERNEL_RCSID(0, "$NetBSD: apbus.c,v 1.12 2015/04/21 19:57:41 macallan Exp $");
33 1.1 macallan
34 1.1 macallan #include "locators.h"
35 1.1 macallan #define _MIPS_BUS_DMA_PRIVATE
36 1.1 macallan
37 1.1 macallan #include <sys/param.h>
38 1.1 macallan #include <sys/bus.h>
39 1.1 macallan #include <sys/device.h>
40 1.1 macallan #include <sys/extent.h>
41 1.1 macallan #include <sys/systm.h>
42 1.1 macallan
43 1.1 macallan #include <mips/ingenic/ingenic_var.h>
44 1.1 macallan #include <mips/ingenic/ingenic_regs.h>
45 1.1 macallan
46 1.2 macallan #include "opt_ingenic.h"
47 1.2 macallan
48 1.1 macallan static int apbus_match(device_t, cfdata_t, void *);
49 1.1 macallan static void apbus_attach(device_t, device_t, void *);
50 1.1 macallan static int apbus_print(void *, const char *);
51 1.1 macallan static void apbus_bus_mem_init(bus_space_tag_t, void *);
52 1.1 macallan
53 1.1 macallan CFATTACH_DECL_NEW(apbus, 0, apbus_match, apbus_attach, NULL, NULL);
54 1.1 macallan
55 1.1 macallan static struct mips_bus_space apbus_mbst;
56 1.1 macallan bus_space_tag_t apbus_memt = NULL;
57 1.1 macallan
58 1.1 macallan struct mips_bus_dma_tag apbus_dmat = {
59 1.6 macallan ._bounce_alloc_hi = 0x10000000,
60 1.1 macallan ._dmamap_ops = _BUS_DMAMAP_OPS_INITIALIZER,
61 1.1 macallan ._dmamem_ops = _BUS_DMAMEM_OPS_INITIALIZER,
62 1.1 macallan ._dmatag_ops = _BUS_DMATAG_OPS_INITIALIZER,
63 1.1 macallan };
64 1.1 macallan
65 1.8 macallan typedef struct apbus_dev {
66 1.12 macallan const char *name; /* driver name */
67 1.12 macallan bus_addr_t addr; /* base address */
68 1.12 macallan uint32_t irq; /* interrupt */
69 1.12 macallan uint32_t clk0; /* bit(s) in CLKGR0 */
70 1.12 macallan uint32_t clk1; /* bit(s) in CLKGR1 */
71 1.8 macallan } apbus_dev_t;
72 1.8 macallan
73 1.8 macallan static const apbus_dev_t apbus_devs[] = {
74 1.12 macallan { "dwctwo", JZ_DWC2_BASE, 21, CLK_OTG0 | CLK_UHC, CLK_OTG1},
75 1.12 macallan { "ohci", JZ_OHCI_BASE, 5, CLK_UHC, 0},
76 1.12 macallan { "ehci", JZ_EHCI_BASE, 20, CLK_UHC, 0},
77 1.12 macallan { "dme", JZ_DME_BASE, -1, 0, 0},
78 1.12 macallan { "jzgpio", JZ_GPIO_A_BASE, 17, 0, 0},
79 1.12 macallan { "jzgpio", JZ_GPIO_B_BASE, 16, 0, 0},
80 1.12 macallan { "jzgpio", JZ_GPIO_C_BASE, 15, 0, 0},
81 1.12 macallan { "jzgpio", JZ_GPIO_D_BASE, 14, 0, 0},
82 1.12 macallan { "jzgpio", JZ_GPIO_E_BASE, 13, 0, 0},
83 1.12 macallan { "jzgpio", JZ_GPIO_F_BASE, 12, 0, 0},
84 1.12 macallan { "jziic", JZ_SMB0_BASE, 60, CLK_SMB0, 0},
85 1.12 macallan { "jziic", JZ_SMB1_BASE, 59, CLK_SMB1, 0},
86 1.12 macallan { "jziic", JZ_SMB2_BASE, 58, CLK_SMB2, 0},
87 1.12 macallan { "jziic", JZ_SMB3_BASE, 57, 0, CLK_SMB3},
88 1.12 macallan { "jziic", JZ_SMB4_BASE, 56, 0, CLK_SMB4},
89 1.12 macallan { "jzfb", JZ_LCDC0_BASE, 31, CLK_LCD, CLK_HDMI},
90 1.12 macallan { NULL, -1, -1, 0, 0}
91 1.1 macallan };
92 1.1 macallan
93 1.1 macallan void
94 1.1 macallan apbus_init(void)
95 1.1 macallan {
96 1.1 macallan static bool done = false;
97 1.1 macallan if (done)
98 1.1 macallan return;
99 1.1 macallan done = true;
100 1.1 macallan
101 1.1 macallan apbus_bus_mem_init(&apbus_mbst, NULL);
102 1.1 macallan apbus_memt = &apbus_mbst;
103 1.1 macallan }
104 1.1 macallan
105 1.1 macallan int
106 1.1 macallan apbus_match(device_t parent, cfdata_t match, void *aux)
107 1.1 macallan {
108 1.1 macallan struct mainbusdev {
109 1.1 macallan const char *md_name;
110 1.1 macallan } *aa = aux;
111 1.1 macallan if (strcmp(aa->md_name, "apbus") == 0) return 1;
112 1.1 macallan return 0;
113 1.1 macallan }
114 1.1 macallan
115 1.1 macallan void
116 1.1 macallan apbus_attach(device_t parent, device_t self, void *aux)
117 1.1 macallan {
118 1.11 macallan uint32_t reg, mpll, m, n, p, mclk, pclk, pdiv;
119 1.1 macallan aprint_normal("\n");
120 1.1 macallan
121 1.1 macallan /* should have been called early on */
122 1.1 macallan apbus_init();
123 1.1 macallan
124 1.2 macallan #ifdef INGENIC_DEBUG
125 1.2 macallan printf("core ctrl: %08x\n", MFC0(12, 2));
126 1.2 macallan printf("core status: %08x\n", MFC0(12, 3));
127 1.2 macallan printf("REIM: %08x\n", MFC0(12, 4));
128 1.2 macallan printf("ID: %08x\n", MFC0(15, 1));
129 1.2 macallan #endif
130 1.11 macallan /* assuming we're using MPLL */
131 1.11 macallan mpll = readreg(JZ_CPMPCR);
132 1.11 macallan m = (mpll & JZ_PLLM_M) >> JZ_PLLM_S;
133 1.11 macallan n = (mpll & JZ_PLLN_M) >> JZ_PLLN_S;
134 1.11 macallan p = (mpll & JZ_PLLP_M) >> JZ_PLLP_S;
135 1.11 macallan
136 1.11 macallan /* assuming 48MHz EXTCLK */
137 1.11 macallan mclk = (48000 * (m + 1) / (n + 1)) / (p + 1);
138 1.11 macallan
139 1.11 macallan reg = readreg(JZ_CPCCR);
140 1.11 macallan pdiv = (reg & JZ_PDIV_M) >> JZ_PDIV_S;
141 1.11 macallan pclk = mclk / pdiv;
142 1.11 macallan #ifdef INGENIC_DEBUG
143 1.11 macallan printf("mclk %d kHz\n", mclk);
144 1.11 macallan printf("pclk %d kHz\n", pclk);
145 1.11 macallan #endif
146 1.2 macallan
147 1.11 macallan /* enable clocks */
148 1.2 macallan reg = readreg(JZ_CLKGR1);
149 1.12 macallan reg &= ~CLK_AHB_MON; /* AHB_MON clock */
150 1.2 macallan writereg(JZ_CLKGR1, reg);
151 1.2 macallan
152 1.3 macallan /* wake up the USB part */
153 1.3 macallan reg = readreg(JZ_OPCR);
154 1.4 macallan reg |= OPCR_SPENDN0 | OPCR_SPENDN1;
155 1.3 macallan writereg(JZ_OPCR, reg);
156 1.3 macallan
157 1.11 macallan /* setup GPIOs for I2C buses */
158 1.11 macallan /* iic0 */
159 1.11 macallan gpio_as_dev0(3, 30);
160 1.11 macallan gpio_as_dev0(3, 31);
161 1.11 macallan /* iic1 */
162 1.11 macallan gpio_as_dev0(4, 30);
163 1.11 macallan gpio_as_dev0(4, 31);
164 1.11 macallan /* iic2 */
165 1.11 macallan gpio_as_dev2(5, 16);
166 1.11 macallan gpio_as_dev2(5, 17);
167 1.11 macallan /* iic3 */
168 1.11 macallan gpio_as_dev1(3, 10);
169 1.11 macallan gpio_as_dev1(3, 11);
170 1.11 macallan /* iic4 */
171 1.11 macallan /* make sure these aren't SMB4 */
172 1.11 macallan gpio_as_dev3(4, 3);
173 1.11 macallan gpio_as_dev3(4, 4);
174 1.11 macallan /* these are supposed to be connected to the RTC */
175 1.11 macallan gpio_as_dev1(4, 12);
176 1.11 macallan gpio_as_dev1(4, 13);
177 1.11 macallan /* these can be DDC2 or SMB4, set them to DDC2 */
178 1.11 macallan gpio_as_dev0(5, 24);
179 1.11 macallan gpio_as_dev0(5, 25);
180 1.11 macallan
181 1.2 macallan #ifdef INGENIC_DEBUG
182 1.2 macallan printf("JZ_CLKGR0 %08x\n", readreg(JZ_CLKGR0));
183 1.2 macallan printf("JZ_CLKGR1 %08x\n", readreg(JZ_CLKGR1));
184 1.2 macallan printf("JZ_SPCR0 %08x\n", readreg(JZ_SPCR0));
185 1.2 macallan printf("JZ_SPCR1 %08x\n", readreg(JZ_SPCR1));
186 1.2 macallan printf("JZ_SRBC %08x\n", readreg(JZ_SRBC));
187 1.3 macallan printf("JZ_OPCR %08x\n", readreg(JZ_OPCR));
188 1.5 macallan printf("JZ_UHCCDR %08x\n", readreg(JZ_UHCCDR));
189 1.2 macallan #endif
190 1.1 macallan
191 1.8 macallan for (const apbus_dev_t *adv = apbus_devs; adv->name != NULL; adv++) {
192 1.1 macallan struct apbus_attach_args aa;
193 1.8 macallan aa.aa_name = adv->name;
194 1.8 macallan aa.aa_addr = adv->addr;
195 1.8 macallan aa.aa_irq = adv->irq;
196 1.1 macallan aa.aa_dmat = &apbus_dmat;
197 1.1 macallan aa.aa_bst = apbus_memt;
198 1.11 macallan aa.aa_pclk = pclk;
199 1.1 macallan
200 1.12 macallan /* enable clocks as needed */
201 1.12 macallan if (adv->clk0 != 0) {
202 1.12 macallan reg = readreg(JZ_CLKGR0);
203 1.12 macallan reg &= ~adv->clk0;
204 1.12 macallan writereg(JZ_CLKGR0, reg);
205 1.12 macallan }
206 1.12 macallan
207 1.12 macallan if (adv->clk1 != 0) {
208 1.12 macallan reg = readreg(JZ_CLKGR1);
209 1.12 macallan reg &= ~adv->clk1;
210 1.12 macallan writereg(JZ_CLKGR1, reg);
211 1.12 macallan }
212 1.12 macallan
213 1.1 macallan (void) config_found_ia(self, "apbus", &aa, apbus_print);
214 1.1 macallan }
215 1.1 macallan }
216 1.1 macallan
217 1.1 macallan int
218 1.1 macallan apbus_print(void *aux, const char *pnp)
219 1.1 macallan {
220 1.1 macallan struct apbus_attach_args *aa = aux;
221 1.1 macallan
222 1.8 macallan if (pnp) {
223 1.1 macallan aprint_normal("%s at %s", aa->aa_name, pnp);
224 1.8 macallan }
225 1.9 macallan if (aa->aa_addr != -1)
226 1.9 macallan aprint_normal(" addr 0x%" PRIxBUSADDR, aa->aa_addr);
227 1.9 macallan if ((pnp == NULL) && (aa->aa_irq != -1))
228 1.9 macallan aprint_normal(" irq %d", aa->aa_irq);
229 1.1 macallan return (UNCONF);
230 1.1 macallan }
231 1.1 macallan
232 1.1 macallan #define CHIP apbus
233 1.1 macallan #define CHIP_MEM /* defined */
234 1.1 macallan #define CHIP_W1_BUS_START(v) 0x10000000UL
235 1.1 macallan #define CHIP_W1_BUS_END(v) 0x20000000UL
236 1.1 macallan #define CHIP_W1_SYS_START(v) 0x10000000UL
237 1.1 macallan #define CHIP_W1_SYS_END(v) 0x20000000UL
238 1.1 macallan
239 1.1 macallan #include <mips/mips/bus_space_alignstride_chipdep.c>
240