imx23_clkctrl.c revision 1.1.6.2 1 1.1.6.2 yamt /* $Id: imx23_clkctrl.c,v 1.1.6.2 2014/05/22 11:39:32 yamt Exp $ */
2 1.1.6.2 yamt
3 1.1.6.2 yamt /*
4 1.1.6.2 yamt * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1.6.2 yamt * All rights reserved.
6 1.1.6.2 yamt *
7 1.1.6.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.1.6.2 yamt * by Petri Laakso.
9 1.1.6.2 yamt *
10 1.1.6.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.1.6.2 yamt * modification, are permitted provided that the following conditions
12 1.1.6.2 yamt * are met:
13 1.1.6.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.1.6.2 yamt * documentation and/or other materials provided with the distribution.
18 1.1.6.2 yamt *
19 1.1.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.6.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.1.6.2 yamt */
31 1.1.6.2 yamt
32 1.1.6.2 yamt #include <sys/param.h>
33 1.1.6.2 yamt #include <sys/types.h>
34 1.1.6.2 yamt #include <sys/bus.h>
35 1.1.6.2 yamt #include <sys/cdefs.h>
36 1.1.6.2 yamt #include <sys/device.h>
37 1.1.6.2 yamt #include <sys/errno.h>
38 1.1.6.2 yamt
39 1.1.6.2 yamt #include <arm/imx/imx23_clkctrlreg.h>
40 1.1.6.2 yamt #include <arm/imx/imx23_clkctrlvar.h>
41 1.1.6.2 yamt #include <arm/imx/imx23var.h>
42 1.1.6.2 yamt
43 1.1.6.2 yamt typedef struct clkctrl_softc {
44 1.1.6.2 yamt device_t sc_dev;
45 1.1.6.2 yamt bus_space_tag_t sc_iot;
46 1.1.6.2 yamt bus_space_handle_t sc_hdl;
47 1.1.6.2 yamt } *clkctrl_softc_t;
48 1.1.6.2 yamt
49 1.1.6.2 yamt static int clkctrl_match(device_t, cfdata_t, void *);
50 1.1.6.2 yamt static void clkctrl_attach(device_t, device_t, void *);
51 1.1.6.2 yamt static int clkctrl_activate(device_t, enum devact);
52 1.1.6.2 yamt
53 1.1.6.2 yamt static void clkctrl_init(struct clkctrl_softc *);
54 1.1.6.2 yamt
55 1.1.6.2 yamt static clkctrl_softc_t _sc = NULL;
56 1.1.6.2 yamt
57 1.1.6.2 yamt CFATTACH_DECL3_NEW(clkctrl,
58 1.1.6.2 yamt sizeof(struct clkctrl_softc),
59 1.1.6.2 yamt clkctrl_match,
60 1.1.6.2 yamt clkctrl_attach,
61 1.1.6.2 yamt NULL,
62 1.1.6.2 yamt clkctrl_activate,
63 1.1.6.2 yamt NULL,
64 1.1.6.2 yamt NULL,
65 1.1.6.2 yamt 0
66 1.1.6.2 yamt );
67 1.1.6.2 yamt
68 1.1.6.2 yamt #define CLKCTRL_RD(sc, reg) \
69 1.1.6.2 yamt bus_space_read_4(sc->sc_iot, sc->sc_hdl, (reg))
70 1.1.6.2 yamt #define CLKCTRL_WR(sc, reg, val) \
71 1.1.6.2 yamt bus_space_write_4(sc->sc_iot, sc->sc_hdl, (reg), (val))
72 1.1.6.2 yamt
73 1.1.6.2 yamt #define CLKCTRL_SOFT_RST_LOOP 455 /* At least 1 us ... */
74 1.1.6.2 yamt
75 1.1.6.2 yamt static int
76 1.1.6.2 yamt clkctrl_match(device_t parent, cfdata_t match, void *aux)
77 1.1.6.2 yamt {
78 1.1.6.2 yamt struct apb_attach_args *aa = aux;
79 1.1.6.2 yamt
80 1.1.6.2 yamt if ((aa->aa_addr == HW_CLKCTRL_BASE) &&
81 1.1.6.2 yamt (aa->aa_size == HW_CLKCTRL_SIZE))
82 1.1.6.2 yamt return 1;
83 1.1.6.2 yamt
84 1.1.6.2 yamt return 0;
85 1.1.6.2 yamt }
86 1.1.6.2 yamt
87 1.1.6.2 yamt static void
88 1.1.6.2 yamt clkctrl_attach(device_t parent, device_t self, void *aux)
89 1.1.6.2 yamt {
90 1.1.6.2 yamt struct clkctrl_softc *sc = device_private(self);
91 1.1.6.2 yamt struct apb_attach_args *aa = aux;
92 1.1.6.2 yamt static int clkctrl_attached = 0;
93 1.1.6.2 yamt
94 1.1.6.2 yamt sc->sc_dev = self;
95 1.1.6.2 yamt sc->sc_iot = aa->aa_iot;
96 1.1.6.2 yamt
97 1.1.6.2 yamt if (clkctrl_attached) {
98 1.1.6.2 yamt aprint_error_dev(sc->sc_dev, "already attached\n");
99 1.1.6.2 yamt return;
100 1.1.6.2 yamt }
101 1.1.6.2 yamt
102 1.1.6.2 yamt if (bus_space_map(sc->sc_iot, aa->aa_addr, aa->aa_size, 0,
103 1.1.6.2 yamt &sc->sc_hdl))
104 1.1.6.2 yamt {
105 1.1.6.2 yamt aprint_error_dev(sc->sc_dev, "Unable to map bus space\n");
106 1.1.6.2 yamt return;
107 1.1.6.2 yamt }
108 1.1.6.2 yamt
109 1.1.6.2 yamt
110 1.1.6.2 yamt clkctrl_init(sc);
111 1.1.6.2 yamt
112 1.1.6.2 yamt aprint_normal("\n");
113 1.1.6.2 yamt
114 1.1.6.2 yamt clkctrl_attached = 1;
115 1.1.6.2 yamt
116 1.1.6.2 yamt return;
117 1.1.6.2 yamt }
118 1.1.6.2 yamt
119 1.1.6.2 yamt static int
120 1.1.6.2 yamt clkctrl_activate(device_t self, enum devact act)
121 1.1.6.2 yamt {
122 1.1.6.2 yamt
123 1.1.6.2 yamt return EOPNOTSUPP;
124 1.1.6.2 yamt }
125 1.1.6.2 yamt
126 1.1.6.2 yamt static void
127 1.1.6.2 yamt clkctrl_init(struct clkctrl_softc *sc)
128 1.1.6.2 yamt {
129 1.1.6.2 yamt _sc = sc;
130 1.1.6.2 yamt return;
131 1.1.6.2 yamt }
132 1.1.6.2 yamt
133 1.1.6.2 yamt /*
134 1.1.6.2 yamt * Power up 8-phase PLL outputs for USB PHY
135 1.1.6.2 yamt *
136 1.1.6.2 yamt */
137 1.1.6.2 yamt void
138 1.1.6.2 yamt clkctrl_en_usb(void)
139 1.1.6.2 yamt {
140 1.1.6.2 yamt struct clkctrl_softc *sc = _sc;
141 1.1.6.2 yamt
142 1.1.6.2 yamt if (sc == NULL) {
143 1.1.6.2 yamt aprint_error("clkctrl is not initalized");
144 1.1.6.2 yamt return;
145 1.1.6.2 yamt }
146 1.1.6.2 yamt
147 1.1.6.2 yamt CLKCTRL_WR(sc, HW_CLKCTRL_PLLCTRL0_SET,
148 1.1.6.2 yamt HW_CLKCTRL_PLLCTRL0_EN_USB_CLKS);
149 1.1.6.2 yamt
150 1.1.6.2 yamt return;
151 1.1.6.2 yamt }
152