aboutsummaryrefslogtreecommitdiff
path: root/drivers/usb/musb/stm_musb.c
blob: e04d4323d83b0a31bff463f67d961aec8531b41b (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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
 * Copyright (C) 2009 STMicroelectronics
 * Copyright (C) 2009 ST-Ericsson SA
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

/** @file  stm_musb.c
  * @brief This file contains the USB controller and Phy initialization
  * with default as interrupt mode was implemented
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include "musb_core.h"
#include "stm_musb.h"
#include <mach/musb_db8500.h>

static u8 ulpi_read_register(struct musb *musb, u8 address);
static u8 ulpi_write_register(struct musb *musb, u8 address, u8 data);
/* callback argument for AB8500 callback functions */
static struct musb *musb_status;
static spinlock_t musb_ulpi_spinlock;
static unsigned musb_power;

/**
 * musb_set_session() - Start the USB session
 *
 * This function is used to start the USB sessios in USB host mode
 * once the A cable is plugged in
 */
void musb_set_session(void)
{
	u8 val;
	void __iomem *regs;

	if (musb_status == NULL) {
		printk(KERN_ERR "Error: devctl session cannot be set\n");
		return;
	}
	regs = musb_status->mregs;
	val = musb_readb(regs, MUSB_DEVCTL);
	musb_writeb(regs, MUSB_DEVCTL, val | MUSB_DEVCTL_SESSION);
}
EXPORT_SYMBOL(musb_set_session);

void
ab8500_bm_usb_state_changed_wrapper(u8 bm_usb_state)
{
	if ((bm_usb_state == AB8500_BM_USB_STATE_RESET_HS) ||
	    (bm_usb_state == AB8500_BM_USB_STATE_RESET_FS)) {
		musb_power = 0;
	}
#ifdef CONFIG_AB8500_BM
	ab8500_bm_usb_state_changed(bm_usb_state, musb_power);
#endif
}

void
ab8500_bm_ulpi_set_char_speed_mode(u8 mode)
{
	if (musb_status == NULL) {
		printk(KERN_ALERT "musb_status is NULL. "
		    "Cannot write to ULPI.\n");
		return;
	}

	mode &= 3;
	mode <<= 5;
	mode |= ulpi_read_register(musb_status, ULPI_ULINKSTAT) &
	    ~ULPI_ULINKSTAT_CHARSPEED_MODE;

	printk(KERN_INFO "Writing 0x%02x to USB char-speed mode bits\n", mode);

	ulpi_write_register(musb_status, ULPI_ULINKSTAT, mode);
}

void
ab8500_bm_ulpi_set_char_suspend_mode(u8 suspend)
{
	if (musb_status == NULL) {
		printk(KERN_ALERT "musb_status is NULL. "
		    "Cannot write to ULPI.\n");
		return;
	}

	suspend &= 1;
	suspend <<= 7;
	suspend |= ulpi_read_register(musb_status, ULPI_ULINKSTAT) &
	    ~ULPI_ULINKSTAT_SUSPEND_MODE;

	printk(KERN_INFO "Writing 0x%02x to USB suspend mode bits\n", suspend);

	ulpi_write_register(musb_status, ULPI_ULINKSTAT, suspend);
}

/* Sys interfaces */
static struct kobject *usbstatus_kobj;
static ssize_t usb_cable_status
		(struct kobject *kobj, struct attribute *attr, char *buf)
{
	u8 is_active = 0;

	if (strcmp(attr->name, "cable_connect") == 0) {
		is_active = musb_status->is_active;
		sprintf(buf, "%d\n", is_active);
	}
	return strlen(buf);
}

static struct attribute usb_cable_connect_attribute = \
			{.name = "cable_connect", .mode = S_IRUGO};
static struct attribute *usb_status[] = {
	&usb_cable_connect_attribute,
	NULL
};

struct sysfs_ops usb_sysfs_ops = {
	.show  = usb_cable_status,
};

static struct kobj_type ktype_usbstatus = {
	.sysfs_ops = &usb_sysfs_ops,
	.default_attrs = usb_status,
};

/*
 * A structure was declared as global for timer in USB host mode
 */
static struct timer_list notify_timer;

/**
 * ulpi_read_register() - Read the usb register from address writing into ULPI
 * @musb: struct musb pointer.
 * @address: address for reading from ULPI register of USB
 *
 * This function read the value from the specific address in USB host mode.
 */
static u8 ulpi_read_register(struct musb *musb, u8 address)
{
	void __iomem *mbase = musb->mregs;
	unsigned long flags;
	int count = 200;
	u8 val;

	spin_lock_irqsave(&musb_ulpi_spinlock, flags);

	/* set ULPI register address */
	musb_writeb(mbase, OTG_UREGADDR, address);

	/* request a read access */
	val = musb_readb(mbase, OTG_UREGCTRL);
	val |= OTG_UREGCTRL_URW;
	musb_writeb(mbase, OTG_UREGCTRL, val);

	/* perform access */
	val = musb_readb(mbase, OTG_UREGCTRL);
	val |= OTG_UREGCTRL_REGREQ;
	musb_writeb(mbase, OTG_UREGCTRL, val);

	/* wait for completion with a time-out */
	do {
		udelay(10);
		val = musb_readb(mbase, OTG_UREGCTRL);
		count--;
	} while (!(val & OTG_UREGCTRL_REGCMP) && (count > 0));

	/* check for time-out */
	if (!(val & OTG_UREGCTRL_REGCMP) && (count == 0)) {
		spin_unlock_irqrestore(&musb_ulpi_spinlock, flags);
		if (printk_ratelimit())
			printk(KERN_ALERT "U8500 USB : ULPI read timed out\n");
		return 0;
	}

	/* acknowledge completion */
	val &= ~OTG_UREGCTRL_REGCMP;
	musb_writeb(mbase, OTG_UREGCTRL, val);

	/* get data */
	val = musb_readb(mbase, OTG_UREGDATA);
	spin_unlock_irqrestore(&musb_ulpi_spinlock, flags);

	return val;
}

/**
 * ulpi_write_register() - Write to a usb phy's ULPI register
 * using the Mentor ULPI wrapper functionality
 * @musb: struct musb pointer.
 * @address: address of ULPI register
 * @data: data for ULPI register
 * This function writes the value given by data to the specific address
 */
static u8 ulpi_write_register(struct musb *musb, u8 address, u8 data)
{
	void __iomem *mbase = musb->mregs;
	unsigned long flags;
	int count = 200;
	u8 val;

	spin_lock_irqsave(&musb_ulpi_spinlock, flags);

	/* First write to ULPI wrapper registers */
	/* set ULPI register address */
	musb_writeb(mbase, OTG_UREGADDR, address);

	/* request a write access */
	val = musb_readb(mbase, OTG_UREGCTRL);
	val &= ~OTG_UREGCTRL_URW;
	musb_writeb(mbase, OTG_UREGCTRL, val);

	/* Write data to ULPI wrapper data register */
	musb_writeb(mbase, OTG_UREGDATA, data);

	/* perform access  */
	val = musb_readb(mbase, OTG_UREGCTRL);
	val |= OTG_UREGCTRL_REGREQ;
	musb_writeb(mbase, OTG_UREGCTRL, val);

	/* wait for completion with a time-out */
	do {
		udelay(10);
		val = musb_readb(mbase, OTG_UREGCTRL);
		count--;
	} while (!(val & OTG_UREGCTRL_REGCMP) && (count > 0));

	/* check for time-out */
	if (!(val & OTG_UREGCTRL_REGCMP) && (count == 0)) {
		spin_unlock_irqrestore(&musb_ulpi_spinlock, flags);
		if (printk_ratelimit())
			printk(KERN_ALERT "U8500 USB : ULPI write timed out\n");
		return 0;
	}

	/* acknowledge completion */
	val &= ~OTG_UREGCTRL_REGCMP;
	musb_writeb(mbase, OTG_UREGCTRL, val);

	spin_unlock_irqrestore(&musb_ulpi_spinlock, flags);

	return 0;

}

/**
 * musb_stm_hs_otg_init() - Initialize the USB for paltform specific.
 * @musb: struct musb pointer.
 *
 * This function initialize the USB with the given musb structure information.
 */
int __init musb_stm_hs_otg_init(struct musb *musb)
{
	u8 val;
	int status;

	if (musb->clock)
		clk_enable(musb->clock);
	status = stm_gpio_altfuncenable(GPIO_ALT_USB_OTG);
	if (status) {
		DBG(1, "U8500 USB : Problem in enabling alternate function\n");
		return -EBUSY;
	}

	/* enable ULPI interface */
	val = musb_readb(musb->mregs, OTG_TOPCTRL);
	val |= OTG_TOPCTRL_MODE_ULPI;
	musb_writeb(musb->mregs, OTG_TOPCTRL, val);

	/* do soft reset */
	val = musb_readb(musb->mregs, 0x7F);
	val |= 0x2;
	musb_writeb(musb->mregs, 0x7F, val);

	return 0;
}
/**
 * musb_stm_fs_init() - Initialize the file system for the USB.
 * @musb: struct musb pointer.
 *
 * This function initialize the file system of USB.
 */
int __init musb_stm_fs_init(struct musb *musb)
{
	return 0;
}
/**
 * musb_platform_enable() - Enable the USB.
 * @musb: struct musb pointer.
 *
 * This function enables the USB.
 */
void musb_platform_enable(struct musb *musb)
{
}
/**
 * musb_platform_disable() - Disable the USB.
 * @musb: struct musb pointer.
 *
 * This function disables the USB.
 */
void musb_platform_disable(struct musb *musb)
{
}

/**
 * musb_platform_try_idle() - Check the USB state active or not.
 * @musb: struct musb pointer.
 * @timeout: set the timeout to keep the host in idle mode.
 *
 * This function keeps the USB host in idle state based on the musb inforamtion.
 */
void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
{
	if (musb->board_mode != MUSB_PERIPHERAL) {
		unsigned long default_timeout =
			jiffies + msecs_to_jiffies(1000);
		static unsigned long last_timer;

		if (timeout == 0)
			timeout = default_timeout;

		/* Never idle if active, or when VBUS
			 timeout is not set as host */
		if (musb->is_active || ((musb->a_wait_bcon == 0)
			&& (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
			DBG(4, "%s active, deleting timer\n",
				otg_state_string(musb));
			del_timer(&notify_timer);
			last_timer = jiffies;
			return;
		}

		if (time_after(last_timer, timeout)) {
			if (!timer_pending(&notify_timer))
				last_timer = timeout;
			else {
				DBG(4,
				"Longer idle timer already pending,ignoring\n");
				return;
			}
		}
		last_timer = timeout;

		DBG(4, "%s inactive, for idle timer for %lu ms\n",
		otg_state_string(musb),
		(unsigned long)jiffies_to_msecs(timeout - jiffies));
		mod_timer(&notify_timer, timeout);
	}
}

/**
 * set_vbus() - Set the Vbus for the USB.
 * @musb: struct musb pointer.
 * @is_on: set Vbus for USB or not.
 *
 * This function set the Vbus for USB.
 */
static void set_vbus(struct musb *musb, int is_on)
{
	u8 devctl;
	/* HDRC controls CPEN, but beware current surges during device
	 * connect. They can trigger transient overcurrent conditions
	 * that must be ignored.
	 */

	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);

	if (is_on) {
		musb->is_active = 1;
		musb->xceiv->default_a = 1;
		musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
		devctl |= MUSB_DEVCTL_SESSION;

		MUSB_HST_MODE(musb);
	} else {
		musb->is_active = 0;

		/* NOTE:  we're skipping A_WAIT_VFALL -> A_IDLE and
		 * jumping right to B_IDLE...
		 */

		musb->xceiv->default_a = 0;
		musb->xceiv->state = OTG_STATE_B_IDLE;
		devctl &= ~MUSB_DEVCTL_SESSION;

		MUSB_DEV_MODE(musb);
	}
	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);

	DBG(1, "VBUS %s, devctl %02x "
		/* otg %3x conf %08x prcm %08x */ "\n",
		otg_state_string(musb),
		musb_readb(musb->mregs, MUSB_DEVCTL));
}
/**
 * set_power() - Set the power for the USB transceiver.
 * @x: struct usb_transceiver pointer.
 * @mA: set mA power for USB.
 *
 * This function set the power for the USB.
 */
static int set_power(struct otg_transceiver *x, unsigned mA)
{
	/*
	* It appears we get some bad events having mA == 0.
	* Mask away those events.
	*/
	if (mA != 0) {
		musb_power = mA;
		ab8500_bm_usb_state_changed_wrapper(
			AB8500_BM_USB_STATE_CONFIGURED);
	}
	return 0;
}
/**
 * musb_platform_set_mode() - Set the mode for the USB driver.
 * @musb: struct musb pointer.
 * @musb_mode: usb mode.
 *
 * This function set the mode for the USB.
 */
int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
{
	u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);

	devctl |= MUSB_DEVCTL_SESSION;
	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);

	switch (musb_mode) {
	case MUSB_HOST:
		otg_set_host(musb->xceiv, musb->xceiv->host);
		break;
	case MUSB_PERIPHERAL:
		otg_set_peripheral(musb->xceiv, musb->xceiv->gadget);
		break;
	case MUSB_OTG:
		break;
	default:
		return -EINVAL;
	}
	return 0;
}
/**
 * funct_host_notify_timer() - Initialize the timer for USB host driver.
 * @data: usb host data.
 *
 * This function runs the timer for the USB host mode.
 */
static void funct_host_notify_timer(unsigned long data)
{
	/* TODO: Cleanup this timer */
}
/**
 * musb_platform_init() - Initialize the platform USB driver.
 * @musb: struct musb pointer.
 *
 * This function initialize the USB controller and Phy.
 */
int __init musb_platform_init(struct musb *musb)
{
	int ret;

	usb_nop_xceiv_register();

	musb->xceiv = otg_get_transceiver();
	if (!musb->xceiv) {
		pr_err("U8500 USB : no transceiver configured\n");
		ret = -ENODEV;
		goto done;
	}

	ret = musb_stm_hs_otg_init(musb);
	if (ret < 0)
		goto done;
	if (is_host_enabled(musb))
		musb->board_set_vbus = set_vbus;
	if (is_peripheral_enabled(musb))
		musb->xceiv->set_power = set_power;

	ret = musb_phy_en(musb->board_mode);
	if (ret < 0)
		goto done;

	if (musb_status == NULL) {
		musb_status = musb;
		spin_lock_init(&musb_ulpi_spinlock);
	}

	/* Registering usb device  for sysfs */
	usbstatus_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);

	if (usbstatus_kobj == NULL)
		ret = -ENOMEM;
	usbstatus_kobj->ktype = &ktype_usbstatus;
	kobject_init(usbstatus_kobj, usbstatus_kobj->ktype);

	ret = kobject_set_name(usbstatus_kobj, "usb_status");
	if (ret)
		kfree(usbstatus_kobj);

	ret = kobject_add(usbstatus_kobj, NULL, "usb_status");
	if (ret)
		kfree(usbstatus_kobj);

	if (musb->board_mode != MUSB_PERIPHERAL) {
		init_timer(&notify_timer);
		notify_timer.expires = jiffies + msecs_to_jiffies(1000);
		notify_timer.function = funct_host_notify_timer;
		notify_timer.data = (unsigned long)musb;
		add_timer(&notify_timer);
	}
	ret = musb_force_detect(musb->board_mode);
	if (ret < 0)
		goto done;
	return 0;

done:
	usb_nop_xceiv_unregister();
	return ret;
}
/**
 * musb_platform_exit() - unregister the platform USB driver.
 * @musb: struct musb pointer.
 *
 * This function unregisters the USB controller.
 */
int musb_platform_exit(struct musb *musb)
{
	int status = 0;
	musb->clock = 0;
	status = stm_gpio_altfuncdisable(GPIO_ALT_USB_OTG);
	if (status) {
		DBG(1, "U8500 USB : Problem in disabling alternate function\n");
		return -EBUSY;
	}
	if (musb->board_mode != MUSB_PERIPHERAL)
		del_timer_sync(&notify_timer);

	usb_nop_xceiv_unregister();

	musb_status = NULL;

	return 0;
}