mct.c revision 1.12 1 1.12 jmcneill /* $NetBSD: mct.c,v 1.12 2017/06/11 16:21:41 jmcneill Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by Reinoud Zandijk.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #include <sys/cdefs.h>
33 1.1 matt
34 1.12 jmcneill __KERNEL_RCSID(1, "$NetBSD: mct.c,v 1.12 2017/06/11 16:21:41 jmcneill Exp $");
35 1.1 matt
36 1.1 matt #include <sys/param.h>
37 1.1 matt #include <sys/bus.h>
38 1.1 matt #include <sys/device.h>
39 1.1 matt #include <sys/intr.h>
40 1.1 matt #include <sys/kernel.h>
41 1.1 matt #include <sys/proc.h>
42 1.1 matt #include <sys/systm.h>
43 1.1 matt #include <sys/timetc.h>
44 1.9 marty #include <sys/kmem.h>
45 1.1 matt
46 1.1 matt #include <prop/proplib.h>
47 1.1 matt
48 1.1 matt #include <arm/samsung/exynos_reg.h>
49 1.1 matt #include <arm/samsung/exynos_var.h>
50 1.1 matt #include <arm/samsung/mct_reg.h>
51 1.1 matt #include <arm/samsung/mct_var.h>
52 1.1 matt
53 1.12 jmcneill #include <arm/cortex/gtmr_intr.h>
54 1.12 jmcneill #include <arm/cortex/mpcore_var.h>
55 1.12 jmcneill #include <arm/cortex/gtmr_var.h>
56 1.12 jmcneill
57 1.7 marty #include <dev/fdt/fdtvar.h>
58 1.12 jmcneill #include <arm/fdt/arm_fdtvar.h>
59 1.1 matt
60 1.1 matt static int mct_match(device_t, cfdata_t, void *);
61 1.1 matt static void mct_attach(device_t, device_t, void *);
62 1.1 matt
63 1.1 matt CFATTACH_DECL_NEW(exyo_mct, 0, mct_match, mct_attach, NULL, NULL);
64 1.1 matt
65 1.1 matt static inline uint32_t
66 1.1 matt mct_read_global(struct mct_softc *sc, bus_size_t o)
67 1.1 matt {
68 1.1 matt return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
69 1.1 matt }
70 1.1 matt
71 1.1 matt static inline void
72 1.1 matt mct_write_global(struct mct_softc *sc, bus_size_t o, uint32_t v)
73 1.1 matt {
74 1.1 matt bus_size_t wreg;
75 1.1 matt uint32_t bit;
76 1.1 matt int i;
77 1.1 matt
78 1.1 matt /* do the write */
79 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
80 1.1 matt // printf("%s: write %#x at %#x\n",
81 1.1 matt // __func__, ((uint32_t) sc->sc_bsh + (uint32_t) o), v);
82 1.1 matt
83 1.1 matt /* dependent on the write address, do the ack dance */
84 1.1 matt if (o == MCT_G_CNT_L || o == MCT_G_CNT_U) {
85 1.1 matt wreg = MCT_G_CNT_WSTAT;
86 1.1 matt bit = (o == MCT_G_CNT_L) ? G_CNT_WSTAT_L : G_CNT_WSTAT_U;
87 1.1 matt } else {
88 1.1 matt wreg = MCT_G_WSTAT;
89 1.1 matt switch (o) {
90 1.1 matt case MCT_G_COMP0_L:
91 1.1 matt bit = G_WSTAT_COMP0_L;
92 1.1 matt break;
93 1.1 matt case MCT_G_COMP0_U:
94 1.1 matt bit = G_WSTAT_COMP0_U;
95 1.1 matt break;
96 1.1 matt case MCT_G_COMP0_ADD_INCR:
97 1.1 matt bit = G_WSTAT_ADD_INCR;
98 1.1 matt break;
99 1.1 matt case MCT_G_TCON:
100 1.1 matt bit = G_WSTAT_TCON;
101 1.1 matt break;
102 1.1 matt default:
103 1.1 matt /* all other registers */
104 1.1 matt return;
105 1.1 matt }
106 1.1 matt }
107 1.1 matt
108 1.1 matt /* wait for ack */
109 1.1 matt for (i = 0; i < 10000000; i++) {
110 1.1 matt /* value accepted by the hardware/hal ? */
111 1.1 matt if (mct_read_global(sc, wreg) & bit) {
112 1.1 matt /* ack */
113 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh, wreg, bit);
114 1.1 matt return;
115 1.1 matt }
116 1.1 matt }
117 1.1 matt panic("MCT hangs after writing %#x at %#x", v, (uint32_t) o);
118 1.1 matt }
119 1.1 matt
120 1.12 jmcneill static void
121 1.12 jmcneill mct_fdt_cpu_hatch(void *priv, struct cpu_info *ci)
122 1.12 jmcneill {
123 1.12 jmcneill gtmr_init_cpu_clock(ci);
124 1.12 jmcneill }
125 1.12 jmcneill
126 1.1 matt static int
127 1.1 matt mct_match(device_t parent, cfdata_t cf, void *aux)
128 1.1 matt {
129 1.7 marty const char * const compatible[] = { "samsung,exynos4210-mct",
130 1.7 marty NULL };
131 1.1 matt
132 1.7 marty struct fdt_attach_args * const faa = aux;
133 1.7 marty return of_match_compatible(faa->faa_phandle, compatible);
134 1.1 matt }
135 1.1 matt
136 1.1 matt static void
137 1.1 matt mct_attach(device_t parent, device_t self, void *aux)
138 1.1 matt {
139 1.1 matt struct mct_softc * const sc = &mct_sc;
140 1.7 marty struct fdt_attach_args * const faa = aux;
141 1.7 marty bus_addr_t addr;
142 1.7 marty bus_size_t size;
143 1.7 marty int error;
144 1.7 marty
145 1.7 marty if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
146 1.7 marty aprint_error(": couldn't get registers\n");
147 1.7 marty return;
148 1.7 marty }
149 1.1 matt
150 1.1 matt self->dv_private = sc;
151 1.1 matt sc->sc_dev = self;
152 1.7 marty sc->sc_bst = faa->faa_bst;
153 1.11 jmcneill sc->sc_freq = EXYNOS_F_IN_FREQ;
154 1.7 marty
155 1.7 marty error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
156 1.7 marty if (error) {
157 1.7 marty aprint_error(": couldn't map %#llx: %d",
158 1.7 marty (uint64_t)addr, error);
159 1.7 marty return;
160 1.7 marty }
161 1.1 matt
162 1.1 matt aprint_naive("\n");
163 1.11 jmcneill aprint_normal(": Exynos SoC multi core timer (64 bits)\n");
164 1.1 matt
165 1.12 jmcneill /* Start the timer */
166 1.12 jmcneill uint32_t tcon = mct_read_global(sc, MCT_G_TCON);
167 1.1 matt tcon |= G_TCON_START;
168 1.1 matt mct_write_global(sc, MCT_G_TCON, tcon);
169 1.1 matt
170 1.12 jmcneill /* Attach ARMv7 generic timer */
171 1.12 jmcneill struct mpcore_attach_args mpcaa = {
172 1.12 jmcneill .mpcaa_name = "armgtmr",
173 1.12 jmcneill .mpcaa_irq = IRQ_GTMR_PPI_VTIMER
174 1.12 jmcneill };
175 1.1 matt
176 1.12 jmcneill config_found(self, &mpcaa, NULL);
177 1.1 matt
178 1.12 jmcneill arm_fdt_cpu_hatch_register(self, mct_fdt_cpu_hatch);
179 1.11 jmcneill }
180