summaryrefslogtreecommitdiff
path: root/drivers/usb/musb-new/musb_uboot.c
blob: 9c8cc6e584439850310ee126c268346cadcbc613 (plain)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <common.h>
#include <console.h>
#include <watchdog.h>
#include <linux/errno.h>
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>

#include <usb.h>
#include "linux-compat.h"
#include "usb-compat.h"
#include "musb_core.h"
#include "musb_host.h"
#include "musb_gadget.h"
#include "musb_uboot.h"

#ifdef CONFIG_USB_MUSB_HOST
struct int_queue {
	struct usb_host_endpoint hep;
	struct urb urb;
};

#if !CONFIG_IS_ENABLED(DM_USB)
struct musb_host_data musb_host;
#endif

static void musb_host_complete_urb(struct urb *urb)
{
	urb->dev->status &= ~USB_ST_NOT_PROC;
	urb->dev->act_len = urb->actual_length;
}

static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep,
			  struct usb_device *dev, int endpoint_type,
			  unsigned long pipe, void *buffer, int len,
			  struct devrequest *setup, int interval)
{
	int epnum = usb_pipeendpoint(pipe);
	int is_in = usb_pipein(pipe);

	memset(urb, 0, sizeof(struct urb));
	memset(hep, 0, sizeof(struct usb_host_endpoint));
	INIT_LIST_HEAD(&hep->urb_list);
	INIT_LIST_HEAD(&urb->urb_list);
	urb->ep = hep;
	urb->complete = musb_host_complete_urb;
	urb->status = -EINPROGRESS;
	urb->dev = dev;
	urb->pipe = pipe;
	urb->transfer_buffer = buffer;
	urb->transfer_dma = (unsigned long)buffer;
	urb->transfer_buffer_length = len;
	urb->setup_packet = (unsigned char *)setup;

	urb->ep->desc.wMaxPacketSize =
		__cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
				dev->epmaxpacketout[epnum]);
	urb->ep->desc.bmAttributes = endpoint_type;
	urb->ep->desc.bEndpointAddress =
		(is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
	urb->ep->desc.bInterval = interval;
}

static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
{
	struct musb *host = hcd->hcd_priv;
	int ret;
	unsigned long timeout;

	ret = musb_urb_enqueue(hcd, urb, 0);
	if (ret < 0) {
		printf("Failed to enqueue URB to controller\n");
		return ret;
	}

	timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
	do {
		if (ctrlc())
			return -EIO;
		host->isr(0, host);
	} while (urb->status == -EINPROGRESS &&
		 get_timer(0) < timeout);

	if (urb->status == -EINPROGRESS)
		musb_urb_dequeue(hcd, urb, -ETIME);

	return urb->status;
}

static int _musb_submit_control_msg(struct musb_host_data *host,
	struct usb_device *dev, unsigned long pipe,
	void *buffer, int len, struct devrequest *setup)
{
	construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL,
		      pipe, buffer, len, setup, 0);

	/* Fix speed for non hub-attached devices */
	if (!usb_dev_get_parent(dev))
		dev->speed = host->host_speed;

	return submit_urb(&host->hcd, &host->urb);
}

static int _musb_submit_bulk_msg(struct musb_host_data *host,
	struct usb_device *dev, unsigned long pipe, void *buffer, int len)
{
	construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_BULK,
		      pipe, buffer, len, NULL, 0);
	return submit_urb(&host->hcd, &host->urb);
}

static int _musb_submit_int_msg(struct musb_host_data *host,
	struct usb_device *dev, unsigned long pipe,
	void *buffer, int len, int interval)
{
	construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_INT, pipe,
		      buffer, len, NULL, interval);
	return submit_urb(&host->hcd, &host->urb);
}

static struct int_queue *_musb_create_int_queue(struct musb_host_data *host,
	struct usb_device *dev, unsigned long pipe, int queuesize,
	int elementsize, void *buffer, int interval)
{
	struct int_queue *queue;
	int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe);

	if (queuesize != 1) {
		printf("ERROR musb int-queues only support queuesize 1\n");
		return NULL;
	}

	if (dev->int_pending & (1 << index)) {
		printf("ERROR int-urb is already pending on pipe %lx\n", pipe);
		return NULL;
	}

	queue = malloc(sizeof(*queue));
	if (!queue)
		return NULL;

	construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT,
		      pipe, buffer, elementsize, NULL, interval);

	ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0);
	if (ret < 0) {
		printf("Failed to enqueue URB to controller\n");
		free(queue);
		return NULL;
	}

	dev->int_pending |= 1 << index;
	return queue;
}

static int _musb_destroy_int_queue(struct musb_host_data *host,
	struct usb_device *dev, struct int_queue *queue)
{
	int index = usb_pipein(queue->urb.pipe) * 16 + 
		    usb_pipeendpoint(queue->urb.pipe);

	if (queue->urb.status == -EINPROGRESS)
		musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME);

	dev->int_pending &= ~(1 << index);
	free(queue);
	return 0;
}

static void *_musb_poll_int_queue(struct musb_host_data *host,
	struct usb_device *dev, struct int_queue *queue)
{
	if (queue->urb.status != -EINPROGRESS)
		return NULL; /* URB has already completed in a prev. poll */

	host->host->isr(0, host->host);

	if (queue->urb.status != -EINPROGRESS)
		return queue->urb.transfer_buffer; /* Done */

	return NULL; /* URB still pending */
}

static int _musb_reset_root_port(struct musb_host_data *host,
	struct usb_device *dev)
{
	void *mbase = host->host->mregs;
	u8 power;

	power = musb_readb(mbase, MUSB_POWER);
	power &= 0xf0;
	musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
	mdelay(50);

	if (host->host->ops->pre_root_reset_end)
		host->host->ops->pre_root_reset_end(host->host);

	power = musb_readb(mbase, MUSB_POWER);
	musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);

	if (host->host->ops->post_root_reset_end)
		host->host->ops->post_root_reset_end(host->host);

	host->host->isr(0, host->host);
	host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
			USB_SPEED_HIGH :
			(musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
			USB_SPEED_FULL : USB_SPEED_LOW;
	mdelay((host->host_speed == USB_SPEED_LOW) ? 200 : 50);

	return 0;
}

int musb_lowlevel_init(struct musb_host_data *host)
{
	void *mbase;
	/* USB spec says it may take up to 1 second for a device to connect */
	unsigned long timeout = get_timer(0) + 1000;
	int ret;

	if (!host->host) {
		printf("MUSB host is not registered\n");
		return -ENODEV;
	}

	ret = musb_start(host->host);
	if (ret)
		return ret;

	mbase = host->host->mregs;
	do {
		if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
			break;
	} while (get_timer(0) < timeout);
	if (get_timer(0) >= timeout) {
		musb_stop(host->host);
		return -ENODEV;
	}

	_musb_reset_root_port(host, NULL);
	host->host->is_active = 1;
	host->hcd.hcd_priv = host->host;

	return 0;
}

#if !CONFIG_IS_ENABLED(DM_USB)
int usb_lowlevel_stop(int index)
{
	if (!musb_host.host) {
		printf("MUSB host is not registered\n");
		return -ENODEV;
	}

	musb_stop(musb_host.host);
	return 0;
}

int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
			    void *buffer, int length)
{
	return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length);
}

int submit_control_msg(struct usb_device *dev, unsigned long pipe,
		       void *buffer, int length, struct devrequest *setup)
{
	return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup);
}

int submit_int_msg(struct usb_device *dev, unsigned long pipe,
		   void *buffer, int length, int interval)
{
	return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length, interval);
}

struct int_queue *create_int_queue(struct usb_device *dev,
		unsigned long pipe, int queuesize, int elementsize,
		void *buffer, int interval)
{
	return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize,
				      buffer, interval);
}

void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
{
	return _musb_poll_int_queue(&musb_host, dev, queue);
}

int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
{
	return _musb_destroy_int_queue(&musb_host, dev, queue);
}

int usb_reset_root_port(struct usb_device *dev)
{
	return _musb_reset_root_port(&musb_host, dev);
}

int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
{
	return musb_lowlevel_init(&musb_host);
}
#endif /* !CONFIG_IS_ENABLED(DM_USB) */

#if CONFIG_IS_ENABLED(DM_USB)
static int musb_submit_control_msg(struct udevice *dev, struct usb_device *udev,
				   unsigned long pipe, void *buffer, int length,
				   struct devrequest *setup)
{
	struct musb_host_data *host = dev_get_priv(dev);
	return _musb_submit_control_msg(host, udev, pipe, buffer, length, setup);
}

static int musb_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
				unsigned long pipe, void *buffer, int length)
{
	struct musb_host_data *host = dev_get_priv(dev);
	return _musb_submit_bulk_msg(host, udev, pipe, buffer, length);
}

static int musb_submit_int_msg(struct udevice *dev, struct usb_device *udev,
			       unsigned long pipe, void *buffer, int length,
			       int interval)
{
	struct musb_host_data *host = dev_get_priv(dev);
	return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval);
}

static struct int_queue *musb_create_int_queue(struct udevice *dev,
		struct usb_device *udev, unsigned long pipe, int queuesize,
		int elementsize, void *buffer, int interval)
{
	struct musb_host_data *host = dev_get_priv(dev);
	return _musb_create_int_queue(host, udev, pipe, queuesize, elementsize,
				      buffer, interval);
}

static void *musb_poll_int_queue(struct udevice *dev, struct usb_device *udev,
				 struct int_queue *queue)
{
	struct musb_host_data *host = dev_get_priv(dev);
	return _musb_poll_int_queue(host, udev, queue);
}

static int musb_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
				  struct int_queue *queue)
{
	struct musb_host_data *host = dev_get_priv(dev);
	return _musb_destroy_int_queue(host, udev, queue);
}

static int musb_reset_root_port(struct udevice *dev, struct usb_device *udev)
{
	struct musb_host_data *host = dev_get_priv(dev);
	return _musb_reset_root_port(host, udev);
}

struct dm_usb_ops musb_usb_ops = {
	.control = musb_submit_control_msg,
	.bulk = musb_submit_bulk_msg,
	.interrupt = musb_submit_int_msg,
	.create_int_queue = musb_create_int_queue,
	.poll_int_queue = musb_poll_int_queue,
	.destroy_int_queue = musb_destroy_int_queue,
	.reset_root_port = musb_reset_root_port,
};
#endif /* CONFIG_IS_ENABLED(DM_USB) */
#endif /* CONFIG_USB_MUSB_HOST */

#if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
static struct musb *gadget;

int usb_gadget_handle_interrupts(int index)
{
	WATCHDOG_RESET();
	if (!gadget || !gadget->isr)
		return -EINVAL;

	return gadget->isr(0, gadget);
}

int usb_gadget_register_driver(struct usb_gadget_driver *driver)
{
	int ret;

	if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
	    !driver->setup) {
		printf("bad parameter.\n");
		return -EINVAL;
	}

	if (!gadget) {
		printf("Controller uninitialized\n");
		return -ENXIO;
	}

	ret = musb_gadget_start(&gadget->g, driver);
	if (ret < 0) {
		printf("gadget_start failed with %d\n", ret);
		return ret;
	}

	ret = driver->bind(&gadget->g);
	if (ret < 0) {
		printf("bind failed with %d\n", ret);
		return ret;
	}

	return 0;
}

int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
{
	if (driver->disconnect)
		driver->disconnect(&gadget->g);
	if (driver->unbind)
		driver->unbind(&gadget->g);
	return 0;
}
#endif /* CONFIG_USB_MUSB_GADGET */

struct musb *musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
			   void *ctl_regs)
{
	struct musb **musbp;

	switch (plat->mode) {
#if defined(CONFIG_USB_MUSB_HOST) && !CONFIG_IS_ENABLED(DM_USB)
	case MUSB_HOST:
		musbp = &musb_host.host;
		break;
#endif
#if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
	case MUSB_PERIPHERAL:
		musbp = &gadget;
		break;
#endif
	default:
		return ERR_PTR(-EINVAL);
	}

	*musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
	if (IS_ERR(*musbp)) {
		printf("Failed to init the controller\n");
		return ERR_CAST(*musbp);
	}

	return *musbp;
}