Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*	$NetBSD: imxusb.c,v 1.15 2019/07/26 06:57:54 skrll Exp $	*/
/*
 * Copyright (c) 2009, 2010  Genetec Corporation.  All rights reserved.
 * Written by Hashimoto Kenichi and Hiroyuki Bessho for Genetec Corporation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: imxusb.c,v 1.15 2019/07/26 06:57:54 skrll Exp $");

#include "locators.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/intr.h>
#include <sys/bus.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usb_mem.h>

#include <dev/usb/ehcireg.h>
#include <dev/usb/ehcivar.h>

#include <arm/pic/picvar.h>	/* XXX: for intr_establish! */

#include <arm/imx/imxusbreg.h>
#include <arm/imx/imxusbvar.h>

#include <dev/usb/ulpireg.h>	/* for test */

static int	imxehci_match(device_t, cfdata_t, void *);
static void	imxehci_attach(device_t, device_t, void *);

uint8_t imxusb_ulpi_read(struct imxehci_softc *sc, int addr);
void imxusb_ulpi_write(struct imxehci_softc *sc, int addr, uint8_t data);
static void ulpi_reset(struct imxehci_softc *sc);

static void imxehci_select_interface(struct imxehci_softc *, enum imx_usb_if);
static void imxehci_init(struct ehci_softc *);

/* attach structures */
CFATTACH_DECL_NEW(imxehci, sizeof(struct imxehci_softc),
    imxehci_match, imxehci_attach, NULL, NULL);

static int
imxehci_match(device_t parent, cfdata_t cf, void *aux)
{
	struct imxusbc_attach_args *aa = aux;

	if (aa->aa_unit < 0 || 3 < aa->aa_unit)
		return 0;

	return 1;
}

static void
imxehci_attach(device_t parent, device_t self, void *aux)
{
	struct imxusbc_attach_args *aa = aux;
	struct imxusbc_softc *usbc = device_private(parent);
	struct imxehci_softc *sc = device_private(self);
	ehci_softc_t *hsc = &sc->sc_hsc;
	bus_space_tag_t iot;
	uint16_t hcirev;
	uint32_t id, hwhost, hwdevice;
	const char *comma;

	iot = aa->aa_iot;

	sc->sc_dev = self;
	sc->sc_unit = aa->aa_unit;
	sc->sc_usbc = usbc;
	sc->sc_iot = iot;

	hsc->sc_dev = self;
	hsc->iot = iot;
	hsc->sc_bus.ub_hcpriv = sc;
	hsc->sc_bus.ub_dmatag = aa->aa_dmat;
	hsc->sc_flags |= EHCIF_ETTF;
	hsc->sc_vendor_init = imxehci_init;

	aprint_naive("\n");
	aprint_normal(": i.MX USB Controller\n");

	if (usbc->sc_ehci_size == 0)
		usbc->sc_ehci_size = IMXUSB_EHCI_SIZE;	/* use default */

	/* per unit registers */
	if (bus_space_subregion(iot, aa->aa_ioh,
		sc->sc_unit * usbc->sc_ehci_offset, usbc->sc_ehci_size,
		&sc->sc_ioh) ||
	    bus_space_subregion(iot, aa->aa_ioh,
		sc->sc_unit * usbc->sc_ehci_offset + IMXUSB_EHCIREGS,
		usbc->sc_ehci_size - IMXUSB_EHCIREGS,
		&hsc->ioh)) {

		aprint_error_dev(self, "can't subregion\n");
		return;
	}

	id = bus_space_read_4(iot, sc->sc_ioh, IMXUSB_ID);
	hcirev = bus_space_read_2(iot, hsc->ioh, EHCI_HCIVERSION);

	aprint_normal_dev(self,
	    "id=%d revision=%d HCI revision=0x%x\n",
	    (int)__SHIFTOUT(id, IMXUSB_ID_ID),
	    (int)__SHIFTOUT(id, IMXUSB_ID_REVISION),
	    hcirev);

	hwhost = bus_space_read_4(iot, sc->sc_ioh, IMXUSB_HWHOST);
	hwdevice = bus_space_read_4(iot, sc->sc_ioh, IMXUSB_HWDEVICE);

	aprint_normal_dev(self, "");

	comma = "";
	if (hwhost & HWHOST_HC) {
		int n_ports = 1 + __SHIFTOUT(hwhost, HWHOST_NPORT);
		aprint_normal("%d host port%s",
		    n_ports, n_ports > 1 ? "s" : "");
		comma = ", ";
	}

	if (hwdevice & HWDEVICE_DC) {
		int n_endpoints = __SHIFTOUT(hwdevice, HWDEVICE_DEVEP);
		aprint_normal("%sdevice capable, %d endpoint%s",
		    comma,
		    n_endpoints, n_endpoints > 1 ? "s" : "");
	}
	aprint_normal("\n");

	hsc->sc_offs = bus_space_read_1(iot, hsc->ioh,
	    EHCI_CAPLENGTH);

	/* Platform dependent setup */
	if (usbc->sc_init_md_hook)
		usbc->sc_init_md_hook(sc);

	imxehci_reset(sc);
	imxehci_select_interface(sc, sc->sc_iftype);

	if (sc->sc_iftype == IMXUSBC_IF_ULPI) {
		bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXUSB_ULPIVIEW, 0);

		aprint_normal_dev(hsc->sc_dev,
		    "ULPI phy VID 0x%04x PID 0x%04x\n",
		    (imxusb_ulpi_read(sc, ULPI_VENDOR_ID_LOW) |
			imxusb_ulpi_read(sc, ULPI_VENDOR_ID_HIGH) << 8),
		    (imxusb_ulpi_read(sc, ULPI_PRODUCT_ID_LOW) |
			imxusb_ulpi_read(sc, ULPI_PRODUCT_ID_HIGH) << 8));

		ulpi_reset(sc);

	}

	if (usbc->sc_setup_md_hook)
		usbc->sc_setup_md_hook(sc, IMXUSB_HOST);

	if (sc->sc_iftype == IMXUSBC_IF_ULPI) {
#if 0
		if(hsc->sc_bus.ub_revision == USBREV_2_0)
			ulpi_write(hsc, ULPI_FUNCTION_CONTROL + ULPI_REG_CLEAR, (1 << 0));
		else
			ulpi_write(hsc, ULPI_FUNCTION_CONTROL + ULPI_REG_SET, (1 << 2));
#endif

		imxusb_ulpi_write(sc, ULPI_FUNCTION_CONTROL + ULPI_REG_CLEAR,
		    OTG_CONTROL_IDPULLUP);

		imxusb_ulpi_write(sc, ULPI_OTG_CONTROL + ULPI_REG_SET,
		    OTG_CONTROL_USEEXTVBUSIND |
		    OTG_CONTROL_DRVVBUSEXT |
		    OTG_CONTROL_DRVVBUS |
		    OTG_CONTROL_CHRGVBUS);
	}

	/* Disable interrupts, so we don't get any spurious ones. */
	EOWRITE4(hsc, EHCI_USBINTR, 0);

	if (usbc->sc_intr_establish_md_hook)
		sc->sc_ih = usbc->sc_intr_establish_md_hook(sc);
	else if (aa->aa_irq > 0)
		sc->sc_ih = intr_establish(aa->aa_irq, IPL_USB, IST_LEVEL, ehci_intr, hsc);
	KASSERT(sc->sc_ih != NULL);

	int err = ehci_init(hsc);
	if (err) {
		aprint_error_dev(self, "init failed, error=%d\n", err);
		return;
	}

	/* Attach usb device. */
	hsc->sc_child = config_found(self, &hsc->sc_bus, usbctlprint);
}

static void
imxehci_select_interface(struct imxehci_softc *sc, enum imx_usb_if interface)
{
	uint32_t reg;
	struct ehci_softc *hsc = &sc->sc_hsc;

	reg = EOREAD4(hsc, EHCI_PORTSC(1));
	reg &= ~(PORTSC_PTS | PORTSC_PTW | PORTSC_PTS2);
	switch (interface) {
	case IMXUSBC_IF_UTMI_WIDE:
		reg |= PORTSC_PTW_16;
	case IMXUSBC_IF_UTMI:
		reg |= PORTSC_PTS_UTMI;
		break;
	case IMXUSBC_IF_PHILIPS:
		reg |= PORTSC_PTS_PHILIPS;
		break;
	case IMXUSBC_IF_ULPI:
		reg |= PORTSC_PTS_ULPI;
		break;
	case IMXUSBC_IF_SERIAL:
		reg |= PORTSC_PTS_SERIAL;
		break;
	case IMXUSBC_IF_HSIC:
		reg |= PORTSC_PTS2;
		break;
	}
	EOWRITE4(hsc, EHCI_PORTSC(1), reg);
}

static uint32_t
ulpi_wakeup(struct imxehci_softc *sc, int tout)
{
	struct ehci_softc *hsc = &sc->sc_hsc;
	uint32_t ulpi_view;

	ulpi_view = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IMXUSB_ULPIVIEW);

	if (!(ulpi_view & ULPI_SS)) {
		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
		    IMXUSB_ULPIVIEW, ULPI_WU);
		while (tout-- > 0) {
			ulpi_view = bus_space_read_4(sc->sc_iot,
			    sc->sc_ioh, IMXUSB_ULPIVIEW);
			if (!(ulpi_view & ULPI_WU))
				break;
			delay(1);
		};
	}

	if (tout == 0)
		aprint_error_dev(hsc->sc_dev, "%s: timeout\n", __func__);

	return ulpi_view;
}

static uint32_t
ulpi_wait(struct imxehci_softc *sc, int tout)
{
	struct ehci_softc *hsc = &sc->sc_hsc;
	uint32_t ulpi_view;

	ulpi_view = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IMXUSB_ULPIVIEW);

	while (tout-- > 0) {
		ulpi_view = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
		    IMXUSB_ULPIVIEW);
		if (!(ulpi_view & ULPI_RUN))
			break;
		delay(1);
	}

	if (tout == 0)
		aprint_error_dev(hsc->sc_dev, "%s: timeout\n", __func__);

	return ulpi_view;
}

#define	TIMEOUT	100000

uint8_t
imxusb_ulpi_read(struct imxehci_softc *sc, int addr)
{
	uint32_t reg;

	ulpi_wakeup(sc, TIMEOUT);

	reg = ULPI_RUN | __SHIFTIN(addr, ULPI_ADDR);
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXUSB_ULPIVIEW, reg);

	reg = ulpi_wait(sc, TIMEOUT);

	return __SHIFTOUT(reg, ULPI_DATRD);
}

void
imxusb_ulpi_write(struct imxehci_softc *sc, int addr, uint8_t data)
{
	uint32_t reg;

	ulpi_wakeup(sc, TIMEOUT);

	reg = ULPI_RUN | ULPI_RW | __SHIFTIN(addr, ULPI_ADDR) | __SHIFTIN(data, ULPI_DATWR);
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXUSB_ULPIVIEW, reg);

	ulpi_wait(sc, TIMEOUT);

	return;
}

static void
ulpi_reset(struct imxehci_softc *sc)
{
	struct ehci_softc *hsc = &sc->sc_hsc;
	uint8_t data;
	int timo = 1000 * 1000;	/* XXXX: 1sec */

	imxusb_ulpi_write(sc, ULPI_FUNCTION_CONTROL + ULPI_REG_SET,
	    FUNCTION_CONTROL_RESET /*0x20*/);
	do {
		data = imxusb_ulpi_read(sc, ULPI_FUNCTION_CONTROL);
		if (!(data & FUNCTION_CONTROL_RESET))
			break;
		delay(100);
		timo -= 100;
	} while (timo > 0);

	if (timo <= 0) {
		aprint_error_dev(hsc->sc_dev, "%s: reset failed!!\n",
		    __func__);
		return;
	}

	return;
}

void
imxehci_reset(struct imxehci_softc *sc)
{
	uint32_t reg;
	struct ehci_softc *hsc = &sc->sc_hsc;
	int tout;
#define	RESET_TIMEOUT 100

	reg = EOREAD4(hsc, EHCI_USBCMD);
	reg &= ~EHCI_CMD_RS;
	EOWRITE4(hsc, EHCI_USBCMD, reg);

	for (tout = RESET_TIMEOUT; tout > 0; tout--) {
		reg = EOREAD4(hsc, EHCI_USBCMD);
		if ((reg & EHCI_CMD_RS) == 0)
			break;
		usb_delay_ms(&hsc->sc_bus, 1);
	}

	EOWRITE4(hsc, EHCI_USBCMD, reg | EHCI_CMD_HCRESET);

	for (tout = RESET_TIMEOUT; tout > 0; tout--) {
		reg = EOREAD4(hsc, EHCI_USBCMD);
		if ((reg &  EHCI_CMD_HCRESET) == 0)
			break;
		usb_delay_ms(&hsc->sc_bus, 1);
	}

	if (tout == 0)
		aprint_error_dev(hsc->sc_dev, "reset timeout (%x)\n", reg);

	usb_delay_ms(&hsc->sc_bus, 100);
}

static void
imxehci_init(struct ehci_softc *hsc)
{
	struct imxehci_softc *sc = device_private(hsc->sc_dev);
	uint32_t reg;

	reg = EOREAD4(hsc, EHCI_PORTSC(1));
	reg &= ~(EHCI_PS_CSC | EHCI_PS_PEC | EHCI_PS_OCC);
	reg |= EHCI_PS_PP | EHCI_PS_PE;
	EOWRITE4(hsc, EHCI_PORTSC(1), reg);

	reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IMXUSB_OTGSC);
	reg |= OTGSC_IDPU;
	/* disable IDIE not to conflict with SSP1_DETECT. */
	//reg |= OTGSC_DPIE | OTGSC_IDIE;
	reg |= OTGSC_DPIE;
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXUSB_OTGSC, reg);

	reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IMXUSB_USBMODE);
	reg &= ~USBMODE_CM;
	reg |= USBMODE_CM_HOST;
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXUSB_USBMODE, reg);
}