imx23_usbphy.c revision 1.1.4.2 1 1.1.4.2 rmind /* $Id: imx23_usbphy.c,v 1.1.4.2 2014/05/18 17:44:58 rmind Exp $ */
2 1.1.4.2 rmind
3 1.1.4.2 rmind /*
4 1.1.4.2 rmind * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1.4.2 rmind * All rights reserved.
6 1.1.4.2 rmind *
7 1.1.4.2 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.1.4.2 rmind * by Petri Laakso.
9 1.1.4.2 rmind *
10 1.1.4.2 rmind * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 rmind * modification, are permitted provided that the following conditions
12 1.1.4.2 rmind * are met:
13 1.1.4.2 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 rmind * documentation and/or other materials provided with the distribution.
18 1.1.4.2 rmind *
19 1.1.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.4.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.4.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.4.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.4.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.4.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.4.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.4.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.4.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.4.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.4.2 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1.4.2 rmind */
31 1.1.4.2 rmind
32 1.1.4.2 rmind #include <sys/param.h>
33 1.1.4.2 rmind #include <sys/types.h>
34 1.1.4.2 rmind #include <sys/bus.h>
35 1.1.4.2 rmind #include <sys/cdefs.h>
36 1.1.4.2 rmind #include <sys/device.h>
37 1.1.4.2 rmind #include <sys/errno.h>
38 1.1.4.2 rmind
39 1.1.4.2 rmind #include <arm/imx/imx23_usbphyreg.h>
40 1.1.4.2 rmind #include <arm/imx/imx23var.h>
41 1.1.4.2 rmind
42 1.1.4.2 rmind typedef struct usbphy_softc {
43 1.1.4.2 rmind device_t sc_dev;
44 1.1.4.2 rmind bus_space_tag_t sc_iot;
45 1.1.4.2 rmind bus_space_handle_t sc_hdl;
46 1.1.4.2 rmind } *usbphy_softc_t;
47 1.1.4.2 rmind
48 1.1.4.2 rmind static int usbphy_match(device_t, cfdata_t, void *);
49 1.1.4.2 rmind static void usbphy_attach(device_t, device_t, void *);
50 1.1.4.2 rmind static int usbphy_activate(device_t, enum devact);
51 1.1.4.2 rmind
52 1.1.4.2 rmind static void usbphy_reset(struct usbphy_softc *);
53 1.1.4.2 rmind static void usbphy_init(struct usbphy_softc *);
54 1.1.4.2 rmind
55 1.1.4.2 rmind CFATTACH_DECL3_NEW(usbphy,
56 1.1.4.2 rmind sizeof(struct usbphy_softc),
57 1.1.4.2 rmind usbphy_match,
58 1.1.4.2 rmind usbphy_attach,
59 1.1.4.2 rmind NULL,
60 1.1.4.2 rmind usbphy_activate,
61 1.1.4.2 rmind NULL,
62 1.1.4.2 rmind NULL,
63 1.1.4.2 rmind 0
64 1.1.4.2 rmind );
65 1.1.4.2 rmind
66 1.1.4.2 rmind #define PHY_RD(sc, reg) \
67 1.1.4.2 rmind bus_space_read_4(sc->sc_iot, sc->sc_hdl, (reg))
68 1.1.4.2 rmind #define PHY_WR(sc, reg, val) \
69 1.1.4.2 rmind bus_space_write_4(sc->sc_iot, sc->sc_hdl, (reg), (val))
70 1.1.4.2 rmind
71 1.1.4.2 rmind #define USBPHY_SOFT_RST_LOOP 455 /* At least 1 us ... */
72 1.1.4.2 rmind
73 1.1.4.2 rmind static int
74 1.1.4.2 rmind usbphy_match(device_t parent, cfdata_t match, void *aux)
75 1.1.4.2 rmind {
76 1.1.4.2 rmind struct apb_attach_args *aa = aux;
77 1.1.4.2 rmind
78 1.1.4.2 rmind if ((aa->aa_addr == HW_USBPHY_BASE) && (aa->aa_size == HW_USBPHY_SIZE))
79 1.1.4.2 rmind return 1;
80 1.1.4.2 rmind
81 1.1.4.2 rmind return 0;
82 1.1.4.2 rmind }
83 1.1.4.2 rmind
84 1.1.4.2 rmind static void
85 1.1.4.2 rmind usbphy_attach(device_t parent, device_t self, void *aux)
86 1.1.4.2 rmind {
87 1.1.4.2 rmind struct usbphy_softc *sc = device_private(self);
88 1.1.4.2 rmind struct apb_attach_args *aa = aux;
89 1.1.4.2 rmind static int usbphy_attached = 0;
90 1.1.4.2 rmind uint32_t phy_version;
91 1.1.4.2 rmind
92 1.1.4.2 rmind sc->sc_dev = self;
93 1.1.4.2 rmind sc->sc_iot = aa->aa_iot;
94 1.1.4.2 rmind
95 1.1.4.2 rmind if (usbphy_attached) {
96 1.1.4.2 rmind aprint_error_dev(sc->sc_dev, "already attached\n");
97 1.1.4.2 rmind return;
98 1.1.4.2 rmind }
99 1.1.4.2 rmind
100 1.1.4.2 rmind if (bus_space_map(sc->sc_iot, aa->aa_addr, aa->aa_size, 0,
101 1.1.4.2 rmind &sc->sc_hdl))
102 1.1.4.2 rmind {
103 1.1.4.2 rmind aprint_error_dev(sc->sc_dev, "Unable to map bus space\n");
104 1.1.4.2 rmind return;
105 1.1.4.2 rmind }
106 1.1.4.2 rmind
107 1.1.4.2 rmind usbphy_reset(sc);
108 1.1.4.2 rmind usbphy_init(sc);
109 1.1.4.2 rmind
110 1.1.4.2 rmind phy_version = PHY_RD(sc, HW_USBPHY_VERSION);
111 1.1.4.2 rmind aprint_normal(": USB PHY v%" __PRIuBIT ".%" __PRIuBIT "\n",
112 1.1.4.2 rmind __SHIFTOUT(phy_version, HW_USBPHY_VERSION_MAJOR),
113 1.1.4.2 rmind __SHIFTOUT(phy_version, HW_USBPHY_VERSION_MINOR));
114 1.1.4.2 rmind
115 1.1.4.2 rmind usbphy_attached = 1;
116 1.1.4.2 rmind
117 1.1.4.2 rmind return;
118 1.1.4.2 rmind }
119 1.1.4.2 rmind
120 1.1.4.2 rmind static int
121 1.1.4.2 rmind usbphy_activate(device_t self, enum devact act)
122 1.1.4.2 rmind {
123 1.1.4.2 rmind
124 1.1.4.2 rmind return EOPNOTSUPP;
125 1.1.4.2 rmind }
126 1.1.4.2 rmind
127 1.1.4.2 rmind /*
128 1.1.4.2 rmind * Reset the USB PHY.
129 1.1.4.2 rmind *
130 1.1.4.2 rmind * Inspired by i.MX23 RM "39.3.10 Correct Way to Soft Reset a Block"
131 1.1.4.2 rmind */
132 1.1.4.2 rmind static void
133 1.1.4.2 rmind usbphy_reset(struct usbphy_softc *sc)
134 1.1.4.2 rmind {
135 1.1.4.2 rmind unsigned int loop;
136 1.1.4.2 rmind
137 1.1.4.2 rmind /* Prepare for soft-reset by making sure that SFTRST is not currently
138 1.1.4.2 rmind * asserted. Also clear CLKGATE so we can wait for its assertion below.
139 1.1.4.2 rmind */
140 1.1.4.2 rmind PHY_WR(sc, HW_USBPHY_CTRL_CLR, HW_USBPHY_CTRL_SFTRST);
141 1.1.4.2 rmind
142 1.1.4.2 rmind /* Wait at least a microsecond for SFTRST to deassert. */
143 1.1.4.2 rmind loop = 0;
144 1.1.4.2 rmind while ((PHY_RD(sc, HW_USBPHY_CTRL) & HW_USBPHY_CTRL_SFTRST) ||
145 1.1.4.2 rmind (loop < USBPHY_SOFT_RST_LOOP))
146 1.1.4.2 rmind loop++;
147 1.1.4.2 rmind
148 1.1.4.2 rmind /* Clear CLKGATE so we can wait for its assertion below. */
149 1.1.4.2 rmind PHY_WR(sc, HW_USBPHY_CTRL_CLR, HW_USBPHY_CTRL_CLKGATE);
150 1.1.4.2 rmind
151 1.1.4.2 rmind /* Soft-reset the block. */
152 1.1.4.2 rmind PHY_WR(sc, HW_USBPHY_CTRL_SET, HW_USBPHY_CTRL_SFTRST);
153 1.1.4.2 rmind
154 1.1.4.2 rmind /* Wait until clock is in the gated state. */
155 1.1.4.2 rmind while (!(PHY_RD(sc, HW_USBPHY_CTRL) & HW_USBPHY_CTRL_CLKGATE));
156 1.1.4.2 rmind
157 1.1.4.2 rmind /* Bring block out of reset. */
158 1.1.4.2 rmind PHY_WR(sc, HW_USBPHY_CTRL_CLR, HW_USBPHY_CTRL_SFTRST);
159 1.1.4.2 rmind
160 1.1.4.2 rmind loop = 0;
161 1.1.4.2 rmind while ((PHY_RD(sc, HW_USBPHY_CTRL) & HW_USBPHY_CTRL_SFTRST) ||
162 1.1.4.2 rmind (loop < USBPHY_SOFT_RST_LOOP))
163 1.1.4.2 rmind loop++;
164 1.1.4.2 rmind
165 1.1.4.2 rmind PHY_WR(sc, HW_USBPHY_CTRL_CLR, HW_USBPHY_CTRL_CLKGATE);
166 1.1.4.2 rmind
167 1.1.4.2 rmind /* Wait until clock is in the NON-gated state. */
168 1.1.4.2 rmind while (PHY_RD(sc, HW_USBPHY_CTRL) & HW_USBPHY_CTRL_CLKGATE);
169 1.1.4.2 rmind
170 1.1.4.2 rmind return;
171 1.1.4.2 rmind }
172 1.1.4.2 rmind
173 1.1.4.2 rmind /*
174 1.1.4.2 rmind * Enable USB PHY.
175 1.1.4.2 rmind */
176 1.1.4.2 rmind static void
177 1.1.4.2 rmind usbphy_init(struct usbphy_softc *sc)
178 1.1.4.2 rmind {
179 1.1.4.2 rmind /* Disable power down bits. */
180 1.1.4.2 rmind PHY_WR(sc, HW_USBPHY_PWD_CLR,
181 1.1.4.2 rmind HW_USBPHY_PWD_RXPWDRX |
182 1.1.4.2 rmind HW_USBPHY_PWD_RXPWDDIFF |
183 1.1.4.2 rmind HW_USBPHY_PWD_RXPWD1PT1 |
184 1.1.4.2 rmind HW_USBPHY_PWD_RXPWDENV |
185 1.1.4.2 rmind HW_USBPHY_PWD_TXPWDV2I |
186 1.1.4.2 rmind HW_USBPHY_PWD_TXPWDIBIAS |
187 1.1.4.2 rmind HW_USBPHY_PWD_TXPWDFS
188 1.1.4.2 rmind );
189 1.1.4.2 rmind
190 1.1.4.2 rmind /* USB PLL Power on. */
191 1.1.4.2 rmind PHY_WR(sc, HW_USBPHY_IP_SET,
192 1.1.4.2 rmind HW_USBPHY_IP_PLL_POWER);
193 1.1.4.2 rmind
194 1.1.4.2 rmind /* Wait PLL to lock to 480MHz. */
195 1.1.4.2 rmind delay(10);
196 1.1.4.2 rmind
197 1.1.4.2 rmind PHY_WR(sc, HW_USBPHY_IP_SET, HW_USBPHY_IP_PLL_LOCKED);
198 1.1.4.2 rmind
199 1.1.4.2 rmind /* Ungate PLL clock to USB PHY. */
200 1.1.4.2 rmind PHY_WR(sc, HW_USBPHY_IP_SET, HW_USBPHY_IP_EN_USB_CLKS);
201 1.1.4.2 rmind
202 1.1.4.2 rmind return;
203 1.1.4.2 rmind }
204