host.c 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * host.c - ChipIdea USB host controller driver
 *
 * Copyright (c) 2012 Intel Corporation
 *
 * Author: Alexander Shishkin
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/kernel.h>
23
#include <linux/io.h>
24 25 26
#include <linux/usb.h>
#include <linux/usb/hcd.h>
#include <linux/usb/chipidea.h>
27
#include <linux/regulator/consumer.h>
28

29
#include "../host/ehci.h"
30 31 32 33 34

#include "ci.h"
#include "bits.h"
#include "host.h"

35
static struct hc_driver __read_mostly ci_ehci_hc_driver;
36
static int (*orig_bus_suspend)(struct usb_hcd *hcd);
37

38 39
struct ehci_ci_priv {
	struct regulator *reg_vbus;
40
	struct ci_hdrc *ci;
41 42 43 44 45 46
};

static int ehci_ci_portpower(struct usb_hcd *hcd, int portnum, bool enable)
{
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
	struct ehci_ci_priv *priv = (struct ehci_ci_priv *)ehci->priv;
47
	struct ci_hdrc *ci = priv->ci;
48 49 50 51
	struct device *dev = hcd->self.controller;
	int ret = 0;
	int port = HCS_N_PORTS(ehci->hcs_params);

52
	if (priv->reg_vbus) {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
		if (port > 1) {
			dev_warn(dev,
				"Not support multi-port regulator control\n");
			return 0;
		}
		if (enable)
			ret = regulator_enable(priv->reg_vbus);
		else
			ret = regulator_disable(priv->reg_vbus);
		if (ret) {
			dev_err(dev,
				"Failed to %s vbus regulator, ret=%d\n",
				enable ? "enable" : "disable", ret);
			return ret;
		}
	}
69 70 71 72 73 74 75 76 77

	if (enable && (ci->platdata->phy_mode == USBPHY_INTERFACE_MODE_HSIC)) {
		/*
		 * Marvell 28nm HSIC PHY requires forcing the port to HS mode.
		 * As HSIC is always HS, this should be safe for others.
		 */
		hw_port_test_set(ci, 5);
		hw_port_test_set(ci, 0);
	}
78 79 80 81 82 83 84 85
	return 0;
};

static const struct ehci_driver_overrides ehci_ci_overrides = {
	.extra_priv_size = sizeof(struct ehci_ci_priv),
	.port_power	 = ehci_ci_portpower,
};

86
static irqreturn_t host_irq(struct ci_hdrc *ci)
87 88 89 90
{
	return usb_hcd_irq(ci->irq, ci->hcd);
}

91
static int host_start(struct ci_hdrc *ci)
92 93 94
{
	struct usb_hcd *hcd;
	struct ehci_hcd *ehci;
95
	struct ehci_ci_priv *priv;
96 97 98 99 100 101 102 103 104
	int ret;

	if (usb_disabled())
		return -ENODEV;

	hcd = usb_create_hcd(&ci_ehci_hc_driver, ci->dev, dev_name(ci->dev));
	if (!hcd)
		return -ENOMEM;

105
	dev_set_drvdata(ci->dev, ci);
106 107 108 109 110
	hcd->rsrc_start = ci->hw_bank.phys;
	hcd->rsrc_len = ci->hw_bank.size;
	hcd->regs = ci->hw_bank.abs;
	hcd->has_tt = 1;

111
	hcd->power_budget = ci->platdata->power_budget;
112
	hcd->tpl_support = ci->platdata->tpl_support;
113 114 115 116
	if (ci->phy)
		hcd->phy = ci->phy;
	else
		hcd->usb_phy = ci->usb_phy;
117

118 119 120
	ehci = hcd_to_ehci(hcd);
	ehci->caps = ci->hw_bank.cap;
	ehci->has_hostpc = ci->hw_bank.lpm;
121
	ehci->has_tdi_phy_lpm = ci->hw_bank.lpm;
122
	ehci->imx28_write_fix = ci->imx28_write_fix;
123

124 125
	priv = (struct ehci_ci_priv *)ehci->priv;
	priv->reg_vbus = NULL;
126
	priv->ci = ci;
127

128 129 130 131 132 133 134 135 136 137 138 139 140
	if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci)) {
		if (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON) {
			ret = regulator_enable(ci->platdata->reg_vbus);
			if (ret) {
				dev_err(ci->dev,
				"Failed to enable vbus regulator, ret=%d\n",
									ret);
				goto put_hcd;
			}
		} else {
			priv->reg_vbus = ci->platdata->reg_vbus;
		}
	}
141

142
	ret = usb_add_hcd(hcd, 0, 0);
143
	if (ret) {
144
		goto disable_reg;
145
	} else {
146
		struct usb_otg *otg = &ci->otg;
147

148
		ci->hcd = hcd;
149 150

		if (ci_otg_is_fsm_mode(ci)) {
151 152 153 154
			otg->host = &hcd->self;
			hcd->self.otg_port = 1;
		}
	}
155

156
	if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING)
157 158
		hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);

159 160 161
	if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED)
		hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC);

162
	return ret;
163

164 165 166 167
disable_reg:
	if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
			(ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
		regulator_disable(ci->platdata->reg_vbus);
168 169 170 171
put_hcd:
	usb_put_hcd(hcd);

	return ret;
172 173
}

174
static void host_stop(struct ci_hdrc *ci)
175 176 177
{
	struct usb_hcd *hcd = ci->hcd;

178 179 180
	if (hcd) {
		usb_remove_hcd(hcd);
		usb_put_hcd(hcd);
181 182 183
		if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
			(ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
				regulator_disable(ci->platdata->reg_vbus);
184
	}
185 186
}

187 188 189

void ci_hdrc_host_destroy(struct ci_hdrc *ci)
{
190
	if (ci->role == CI_ROLE_HOST && ci->hcd)
191 192 193
		host_stop(ci);
}

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
static int ci_ehci_bus_suspend(struct usb_hcd *hcd)
{
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
	int port;
	u32 tmp;

	int ret = orig_bus_suspend(hcd);

	if (ret)
		return ret;

	port = HCS_N_PORTS(ehci->hcs_params);
	while (port--) {
		u32 __iomem *reg = &ehci->regs->port_status[port];
		u32 portsc = ehci_readl(ehci, reg);

		if (portsc & PORT_CONNECT) {
			/*
			 * For chipidea, the resume signal will be ended
			 * automatically, so for remote wakeup case, the
			 * usbcmd.rs may not be set before the resume has
			 * ended if other resume paths consumes too much
			 * time (~24ms), in that case, the SOF will not
			 * send out within 3ms after resume ends, then the
			 * high speed device will enter full speed mode.
			 */

			tmp = ehci_readl(ehci, &ehci->regs->command);
			tmp |= CMD_RUN;
			ehci_writel(ehci, tmp, &ehci->regs->command);
			/*
			 * It needs a short delay between set RS bit and PHCD.
			 */
			usleep_range(150, 200);
			break;
		}
	}

	return 0;
}

235
int ci_hdrc_host_init(struct ci_hdrc *ci)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
{
	struct ci_role_driver *rdrv;

	if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_HC))
		return -ENXIO;

	rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
	if (!rdrv)
		return -ENOMEM;

	rdrv->start	= host_start;
	rdrv->stop	= host_stop;
	rdrv->irq	= host_irq;
	rdrv->name	= "host";
	ci->roles[CI_ROLE_HOST] = rdrv;

252 253 254 255 256
	return 0;
}

void ci_hdrc_host_driver_init(void)
{
257
	ehci_init_driver(&ci_ehci_hc_driver, &ehci_ci_overrides);
258 259
	orig_bus_suspend = ci_ehci_hc_driver.bus_suspend;
	ci_ehci_hc_driver.bus_suspend = ci_ehci_bus_suspend;
260
}