xref: /openbsd/sys/arch/octeon/dev/octdwctwo.c (revision fc61954a)
1 /*	$OpenBSD: octdwctwo.c,v 1.10 2016/03/19 17:17:06 visa Exp $	*/
2 
3 /*
4  * Copyright (c) 2015 Masao Uebayashi <uebayasi@tombiinc.com>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/device.h>
22 
23 #include <machine/intr.h>
24 #include <machine/bus.h>
25 #include <machine/octeonreg.h>
26 #include <machine/octeonvar.h>
27 #include <machine/octeon_model.h>
28 
29 #include <octeon/dev/iobusvar.h>
30 #include <octeon/dev/octhcireg.h>
31 
32 #include <dev/usb/usb.h>
33 #include <dev/usb/usbdi.h>
34 #include <dev/usb/usbdivar.h>
35 
36 #include <dev/usb/dwc2/dwc2var.h>
37 #include <dev/usb/dwc2/dwc2.h>
38 #include <dev/usb/dwc2/dwc2_core.h>
39 
40 struct octdwctwo_softc {
41 	struct dwc2_softc	sc_dwc2;
42 
43 	/* USBN bus space */
44 	bus_space_tag_t		sc_bust;
45 	bus_space_handle_t	sc_regh;
46 	bus_space_handle_t	sc_regh2;
47 
48 	void			*sc_ih;
49 };
50 
51 int			octdwctwo_match(struct device *, void *, void *);
52 void			octdwctwo_attach(struct device *, struct device *,
53 			    void *);
54 int			octdwctwo_set_dma_addr(void *, dma_addr_t, int);
55 u_int64_t		octdwctwo_reg2_rd(struct octdwctwo_softc *, bus_size_t);
56 void			octdwctwo_reg2_wr(struct octdwctwo_softc *, bus_size_t,
57 			    u_int64_t);
58 void			octdwctwo_reg_set(struct octdwctwo_softc *, bus_size_t,
59 			    u_int64_t);
60 void			octdwctwo_reg_clear(struct octdwctwo_softc *,
61 			    bus_size_t, u_int64_t);
62 u_int32_t		octdwctwo_read_4(bus_space_tag_t, bus_space_handle_t,
63 			    bus_size_t);
64 void			octdwctwo_write_4(bus_space_tag_t, bus_space_handle_t,
65 			    bus_size_t, u_int32_t);
66 
67 
68 const struct cfattach octdwctwo_ca = {
69 	sizeof(struct octdwctwo_softc), octdwctwo_match, octdwctwo_attach,
70 };
71 
72 struct cfdriver dwctwo_cd = {
73 	NULL, "dwctwo", DV_DULL
74 };
75 
76 static struct dwc2_core_params octdwctwo_params = {
77 	.otg_cap = 2,
78 	.otg_ver = 0,
79 	.dma_enable = 1,
80 	.dma_desc_enable = 0,
81 	.speed = 0,
82 	.enable_dynamic_fifo = 1,
83 	.en_multiple_tx_fifo = 0,
84 	.host_rx_fifo_size = 456,
85 	.host_nperio_tx_fifo_size = 912,
86 	.host_perio_tx_fifo_size = 256,
87 	.max_transfer_size = 65535,
88 	.max_packet_count = 511,
89 	.host_channels = 8,
90 	.phy_type = 1,
91 	.phy_utmi_width = 16,
92 	.phy_ulpi_ddr = 0,
93 	.phy_ulpi_ext_vbus = 0,
94 	.i2c_enable = 0,
95 	.ulpi_fs_ls = 0,
96 	.host_support_fs_ls_low_power = 0,
97 	.host_ls_low_power_phy_clk = 0,
98 	.ts_dline = 0,
99 	.reload_ctl = 0,
100 	.ahbcfg = 0x7,
101 	.uframe_sched = 1,
102 };
103 
104 static struct dwc2_core_dma_config octdwctwo_dma_config = {
105 	.set_dma_addr = octdwctwo_set_dma_addr,
106 };
107 
108 /*
109  * This bus space tag adjusts register addresses to account for
110  * dwc2 using little endian addressing.  dwc2 only does 32bit reads
111  * and writes, so only those functions are provided.
112  */
113 bus_space_t octdwctwo_tag = {
114 	.bus_base = PHYS_TO_XKPHYS(0, CCA_NC),
115 	.bus_private = NULL,
116 	._space_read_4 =	octdwctwo_read_4,
117 	._space_write_4 =	octdwctwo_write_4,
118 	._space_map =		iobus_space_map,
119 	._space_unmap =		iobus_space_unmap,
120 	._space_subregion =	generic_space_region,
121 	._space_vaddr =		generic_space_vaddr
122 };
123 
124 int
125 octdwctwo_match(struct device *parent, void *match, void *aux)
126 {
127 	int id;
128 
129 	id = octeon_get_chipid();
130 	switch (octeon_model_family(id)) {
131 	case OCTEON_MODEL_FAMILY_CN30XX:
132 	case OCTEON_MODEL_FAMILY_CN31XX:
133 	case OCTEON_MODEL_FAMILY_CN50XX:
134 		return (1);
135 	default:
136 		return (0);
137 	}
138 }
139 
140 void
141 octdwctwo_attach(struct device *parent, struct device *self, void *aux)
142 {
143 	struct octdwctwo_softc *sc = (struct octdwctwo_softc *)self;
144 	struct iobus_attach_args *aa = aux;
145 	uint64_t clk;
146 	int rc;
147 
148 	sc->sc_dwc2.sc_iot = &octdwctwo_tag;
149 	sc->sc_dwc2.sc_bus.pipe_size = sizeof(struct usbd_pipe);
150 	sc->sc_dwc2.sc_bus.dmatag = aa->aa_dmat;
151 	sc->sc_dwc2.sc_params = &octdwctwo_params;
152 
153 	rc = bus_space_map(sc->sc_dwc2.sc_iot, USBC_BASE, USBC_SIZE,
154 	    0, &sc->sc_dwc2.sc_ioh);
155 	KASSERT(rc == 0);
156 
157 	sc->sc_bust = aa->aa_bust;
158 	rc = bus_space_map(sc->sc_bust, USBN_BASE, USBN_SIZE,
159 	    0, &sc->sc_regh);
160 	KASSERT(rc == 0);
161 	rc = bus_space_map(sc->sc_bust, USBN_2_BASE, USBN_2_SIZE,
162 	    0, &sc->sc_regh2);
163 	KASSERT(rc == 0);
164 
165 	/*
166 	 * Clock setup.
167 	 */
168 	clk = bus_space_read_8(sc->sc_bust, sc->sc_regh, USBN_CLK_CTL_OFFSET);
169 	clk |= USBN_CLK_CTL_POR;
170 	clk &= ~(USBN_CLK_CTL_HRST | USBN_CLK_CTL_PRST | USBN_CLK_CTL_HCLK_RST |
171 	    USBN_CLK_CTL_ENABLE | USBN_CLK_CTL_P_C_SEL | USBN_CLK_CTL_P_RTYPE);
172 	clk |= SET_USBN_CLK_CTL_DIVIDE(0x4ULL)
173 	    | SET_USBN_CLK_CTL_DIVIDE2(0x0ULL);
174 
175 	bus_space_write_8(sc->sc_bust, sc->sc_regh, USBN_CLK_CTL_OFFSET, clk);
176 	bus_space_read_8(sc->sc_bust, sc->sc_regh, USBN_CLK_CTL_OFFSET);
177 
178 	/*
179 	 * Reset HCLK and wait for it to stabilize.
180 	 */
181 	octdwctwo_reg_set(sc, USBN_CLK_CTL_OFFSET, USBN_CLK_CTL_HCLK_RST);
182 	delay(64);
183 
184 	octdwctwo_reg_clear(sc, USBN_CLK_CTL_OFFSET, USBN_CLK_CTL_POR);
185 
186 	/*
187 	 * Wait for the PHY clock to start.
188 	 */
189 	delay(1000);
190 
191 	octdwctwo_reg_set(sc, USBN_USBP_CTL_STATUS_OFFSET,
192 	    USBN_USBP_CTL_STATUS_ATE_RESET);
193 	delay(10);
194 
195 	octdwctwo_reg_clear(sc, USBN_USBP_CTL_STATUS_OFFSET,
196 			USBN_USBP_CTL_STATUS_ATE_RESET);
197 	octdwctwo_reg_set(sc, USBN_CLK_CTL_OFFSET, USBN_CLK_CTL_PRST);
198 
199 	/*
200 	 * Select host mode.
201 	 */
202 	octdwctwo_reg_clear(sc, USBN_USBP_CTL_STATUS_OFFSET,
203 	    USBN_USBP_CTL_STATUS_HST_MODE);
204 	delay(1);
205 
206 	octdwctwo_reg_set(sc, USBN_CLK_CTL_OFFSET, USBN_CLK_CTL_HRST);
207 
208 	/*
209 	 * Enable clock.
210 	 */
211 	octdwctwo_reg_set(sc, USBN_CLK_CTL_OFFSET, USBN_CLK_CTL_ENABLE);
212 	delay(1);
213 
214 	strlcpy(sc->sc_dwc2.sc_vendor, "Octeon", sizeof(sc->sc_dwc2.sc_vendor));
215 
216 	rc = dwc2_init(&sc->sc_dwc2);
217 	if (rc != 0)
218 		return;
219 	octdwctwo_dma_config.set_dma_addr_data = sc;
220 	rc = dwc2_dma_config(&sc->sc_dwc2, &octdwctwo_dma_config);
221 	if (rc != 0)
222 		return;
223 
224 	printf("\n");
225 
226 	sc->sc_dwc2.sc_child = config_found(&sc->sc_dwc2.sc_bus.bdev,
227 	    &sc->sc_dwc2.sc_bus, usbctlprint);
228 
229 	sc->sc_ih = octeon_intr_establish(CIU_INT_USB, IPL_USB, dwc2_intr,
230 	    (void *)&sc->sc_dwc2, sc->sc_dwc2.sc_bus.bdev.dv_xname);
231 	KASSERT(sc->sc_ih != NULL);
232 }
233 
234 int
235 octdwctwo_set_dma_addr(void *data, dma_addr_t dma_addr, int ch)
236 {
237 	struct octdwctwo_softc *sc = data;
238 
239 	octdwctwo_reg2_wr(sc,
240 	    USBN_DMA0_INB_CHN0_OFFSET + ch * 0x8, dma_addr);
241 	octdwctwo_reg2_wr(sc,
242 	    USBN_DMA0_OUTB_CHN0_OFFSET + ch * 0x8, dma_addr);
243 	return 0;
244 }
245 
246 u_int64_t
247 octdwctwo_reg2_rd(struct octdwctwo_softc *sc, bus_size_t offset)
248 {
249 	u_int64_t value;
250 
251 	value = bus_space_read_8(sc->sc_bust, sc->sc_regh2, offset);
252 	return value;
253 }
254 
255 void
256 octdwctwo_reg2_wr(struct octdwctwo_softc *sc, bus_size_t offset, u_int64_t value)
257 {
258 	bus_space_write_8(sc->sc_bust, sc->sc_regh2, offset, value);
259 	/* guarantee completion of the store operation on RSL registers*/
260 	bus_space_read_8(sc->sc_bust, sc->sc_regh2, offset);
261 }
262 
263 void
264 octdwctwo_reg_set(struct octdwctwo_softc *sc, bus_size_t offset,
265     u_int64_t bits)
266 {
267 	u_int64_t value;
268 	value = bus_space_read_8(sc->sc_bust, sc->sc_regh, offset);
269 	value |= bits;
270 
271 	bus_space_write_8(sc->sc_bust, sc->sc_regh, offset, value);
272 	bus_space_read_8(sc->sc_bust, sc->sc_regh, offset);
273 }
274 
275 void
276 octdwctwo_reg_clear(struct octdwctwo_softc *sc, bus_size_t offset,
277     u_int64_t bits)
278 {
279 	u_int64_t value;
280 	value = bus_space_read_8(sc->sc_bust, sc->sc_regh, offset);
281 	value &= ~bits;
282 
283 	bus_space_write_8(sc->sc_bust, sc->sc_regh, offset, value);
284 	bus_space_read_8(sc->sc_bust, sc->sc_regh, offset);
285 }
286 
287 u_int32_t
288 octdwctwo_read_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
289 {
290 	return *(volatile u_int32_t *)(h + (o^4));
291 }
292 
293 void
294 octdwctwo_write_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
295     u_int32_t v)
296 {
297 	*(volatile u_int32_t *)(h + (o^4)) = v;
298 }
299