Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1 | /* |
| 2 | * core.c - ChipIdea USB IP core family device controller |
| 3 | * |
| 4 | * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved. |
| 5 | * |
| 6 | * Author: David Lopo |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Description: ChipIdea USB IP core family device controller |
| 15 | * |
| 16 | * This driver is composed of several blocks: |
| 17 | * - HW: hardware interface |
| 18 | * - DBG: debug facilities (optional) |
| 19 | * - UTIL: utilities |
| 20 | * - ISR: interrupts handling |
| 21 | * - ENDPT: endpoint operations (Gadget API) |
| 22 | * - GADGET: gadget operations (Gadget API) |
| 23 | * - BUS: bus glue code, bus abstraction layer |
| 24 | * |
| 25 | * Compile Options |
Peter Chen | 58ce849 | 2014-05-23 08:12:47 +0800 | [diff] [blame] | 26 | * - CONFIG_USB_CHIPIDEA_DEBUG: enable debug facilities |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 27 | * - STALL_IN: non-empty bulk-in pipes cannot be halted |
| 28 | * if defined mass storage compliance succeeds but with warnings |
| 29 | * => case 4: Hi > Dn |
| 30 | * => case 5: Hi > Di |
| 31 | * => case 8: Hi <> Do |
| 32 | * if undefined usbtest 13 fails |
| 33 | * - TRACE: enable function tracing (depends on DEBUG) |
| 34 | * |
| 35 | * Main Features |
| 36 | * - Chapter 9 & Mass Storage Compliance with Gadget File Storage |
| 37 | * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined) |
| 38 | * - Normal & LPM support |
| 39 | * |
| 40 | * USBTEST Report |
| 41 | * - OK: 0-12, 13 (STALL_IN defined) & 14 |
| 42 | * - Not Supported: 15 & 16 (ISO) |
| 43 | * |
| 44 | * TODO List |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 45 | * - Suspend & Remote Wakeup |
| 46 | */ |
| 47 | #include <linux/delay.h> |
| 48 | #include <linux/device.h> |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 49 | #include <linux/dma-mapping.h> |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame^] | 50 | #include <linux/extcon.h> |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 51 | #include <linux/phy/phy.h> |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 52 | #include <linux/platform_device.h> |
| 53 | #include <linux/module.h> |
Richard Zhao | fe6e125 | 2012-07-07 22:56:42 +0800 | [diff] [blame] | 54 | #include <linux/idr.h> |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 55 | #include <linux/interrupt.h> |
| 56 | #include <linux/io.h> |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 57 | #include <linux/kernel.h> |
| 58 | #include <linux/slab.h> |
| 59 | #include <linux/pm_runtime.h> |
| 60 | #include <linux/usb/ch9.h> |
| 61 | #include <linux/usb/gadget.h> |
| 62 | #include <linux/usb/otg.h> |
| 63 | #include <linux/usb/chipidea.h> |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 64 | #include <linux/usb/of.h> |
Michael Grzeschik | 4f6743d | 2014-02-19 13:41:43 +0800 | [diff] [blame] | 65 | #include <linux/of.h> |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 66 | #include <linux/phy.h> |
Peter Chen | 1542d9c | 2013-08-14 12:44:03 +0300 | [diff] [blame] | 67 | #include <linux/regulator/consumer.h> |
Peter Chen | 8022d3d | 2014-10-30 09:15:15 +0800 | [diff] [blame] | 68 | #include <linux/usb/ehci_def.h> |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 69 | |
| 70 | #include "ci.h" |
| 71 | #include "udc.h" |
| 72 | #include "bits.h" |
Alexander Shishkin | eb70e5a | 2012-05-11 17:25:54 +0300 | [diff] [blame] | 73 | #include "host.h" |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 74 | #include "debug.h" |
Peter Chen | c10b4f0 | 2013-08-14 12:44:06 +0300 | [diff] [blame] | 75 | #include "otg.h" |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 76 | #include "otg_fsm.h" |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 77 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 78 | /* Controller register map */ |
Marc Kleine-Budde | 987e7bc | 2014-01-06 10:10:39 +0800 | [diff] [blame] | 79 | static const u8 ci_regs_nolpm[] = { |
| 80 | [CAP_CAPLENGTH] = 0x00U, |
| 81 | [CAP_HCCPARAMS] = 0x08U, |
| 82 | [CAP_DCCPARAMS] = 0x24U, |
| 83 | [CAP_TESTMODE] = 0x38U, |
| 84 | [OP_USBCMD] = 0x00U, |
| 85 | [OP_USBSTS] = 0x04U, |
| 86 | [OP_USBINTR] = 0x08U, |
| 87 | [OP_DEVICEADDR] = 0x14U, |
| 88 | [OP_ENDPTLISTADDR] = 0x18U, |
Peter Chen | 2836267 | 2015-06-18 11:51:53 +0800 | [diff] [blame] | 89 | [OP_TTCTRL] = 0x1CU, |
Peter Chen | 96625ea | 2015-03-17 17:32:45 +0800 | [diff] [blame] | 90 | [OP_BURSTSIZE] = 0x20U, |
Marc Kleine-Budde | 987e7bc | 2014-01-06 10:10:39 +0800 | [diff] [blame] | 91 | [OP_PORTSC] = 0x44U, |
| 92 | [OP_DEVLC] = 0x84U, |
| 93 | [OP_OTGSC] = 0x64U, |
| 94 | [OP_USBMODE] = 0x68U, |
| 95 | [OP_ENDPTSETUPSTAT] = 0x6CU, |
| 96 | [OP_ENDPTPRIME] = 0x70U, |
| 97 | [OP_ENDPTFLUSH] = 0x74U, |
| 98 | [OP_ENDPTSTAT] = 0x78U, |
| 99 | [OP_ENDPTCOMPLETE] = 0x7CU, |
| 100 | [OP_ENDPTCTRL] = 0x80U, |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 101 | }; |
| 102 | |
Marc Kleine-Budde | 987e7bc | 2014-01-06 10:10:39 +0800 | [diff] [blame] | 103 | static const u8 ci_regs_lpm[] = { |
| 104 | [CAP_CAPLENGTH] = 0x00U, |
| 105 | [CAP_HCCPARAMS] = 0x08U, |
| 106 | [CAP_DCCPARAMS] = 0x24U, |
| 107 | [CAP_TESTMODE] = 0xFCU, |
| 108 | [OP_USBCMD] = 0x00U, |
| 109 | [OP_USBSTS] = 0x04U, |
| 110 | [OP_USBINTR] = 0x08U, |
| 111 | [OP_DEVICEADDR] = 0x14U, |
| 112 | [OP_ENDPTLISTADDR] = 0x18U, |
Peter Chen | 2836267 | 2015-06-18 11:51:53 +0800 | [diff] [blame] | 113 | [OP_TTCTRL] = 0x1CU, |
Peter Chen | 96625ea | 2015-03-17 17:32:45 +0800 | [diff] [blame] | 114 | [OP_BURSTSIZE] = 0x20U, |
Marc Kleine-Budde | 987e7bc | 2014-01-06 10:10:39 +0800 | [diff] [blame] | 115 | [OP_PORTSC] = 0x44U, |
| 116 | [OP_DEVLC] = 0x84U, |
| 117 | [OP_OTGSC] = 0xC4U, |
| 118 | [OP_USBMODE] = 0xC8U, |
| 119 | [OP_ENDPTSETUPSTAT] = 0xD8U, |
| 120 | [OP_ENDPTPRIME] = 0xDCU, |
| 121 | [OP_ENDPTFLUSH] = 0xE0U, |
| 122 | [OP_ENDPTSTAT] = 0xE4U, |
| 123 | [OP_ENDPTCOMPLETE] = 0xE8U, |
| 124 | [OP_ENDPTCTRL] = 0xECU, |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 125 | }; |
| 126 | |
Nicholas Krause | 158ec07 | 2015-06-27 00:34:48 -0400 | [diff] [blame] | 127 | static void hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 128 | { |
| 129 | int i; |
| 130 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 131 | for (i = 0; i < OP_ENDPTCTRL; i++) |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 132 | ci->hw_bank.regmap[i] = |
| 133 | (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) + |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 134 | (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]); |
| 135 | |
| 136 | for (; i <= OP_LAST; i++) |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 137 | ci->hw_bank.regmap[i] = ci->hw_bank.op + |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 138 | 4 * (i - OP_ENDPTCTRL) + |
| 139 | (is_lpm |
| 140 | ? ci_regs_lpm[OP_ENDPTCTRL] |
| 141 | : ci_regs_nolpm[OP_ENDPTCTRL]); |
| 142 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 143 | } |
| 144 | |
Peter Chen | cb271f3 | 2015-02-11 12:44:55 +0800 | [diff] [blame] | 145 | static enum ci_revision ci_get_revision(struct ci_hdrc *ci) |
| 146 | { |
| 147 | int ver = hw_read_id_reg(ci, ID_ID, VERSION) >> __ffs(VERSION); |
| 148 | enum ci_revision rev = CI_REVISION_UNKNOWN; |
| 149 | |
| 150 | if (ver == 0x2) { |
| 151 | rev = hw_read_id_reg(ci, ID_ID, REVISION) |
| 152 | >> __ffs(REVISION); |
| 153 | rev += CI_REVISION_20; |
| 154 | } else if (ver == 0x0) { |
| 155 | rev = CI_REVISION_1X; |
| 156 | } |
| 157 | |
| 158 | return rev; |
| 159 | } |
| 160 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 161 | /** |
Li Jun | 36304b0 | 2014-04-23 15:56:39 +0800 | [diff] [blame] | 162 | * hw_read_intr_enable: returns interrupt enable register |
| 163 | * |
Peter Chen | 1935388 | 2014-09-22 08:14:17 +0800 | [diff] [blame] | 164 | * @ci: the controller |
| 165 | * |
Li Jun | 36304b0 | 2014-04-23 15:56:39 +0800 | [diff] [blame] | 166 | * This function returns register data |
| 167 | */ |
| 168 | u32 hw_read_intr_enable(struct ci_hdrc *ci) |
| 169 | { |
| 170 | return hw_read(ci, OP_USBINTR, ~0); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * hw_read_intr_status: returns interrupt status register |
| 175 | * |
Peter Chen | 1935388 | 2014-09-22 08:14:17 +0800 | [diff] [blame] | 176 | * @ci: the controller |
| 177 | * |
Li Jun | 36304b0 | 2014-04-23 15:56:39 +0800 | [diff] [blame] | 178 | * This function returns register data |
| 179 | */ |
| 180 | u32 hw_read_intr_status(struct ci_hdrc *ci) |
| 181 | { |
| 182 | return hw_read(ci, OP_USBSTS, ~0); |
| 183 | } |
| 184 | |
| 185 | /** |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 186 | * hw_port_test_set: writes port test mode (execute without interruption) |
| 187 | * @mode: new value |
| 188 | * |
| 189 | * This function returns an error code |
| 190 | */ |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 191 | int hw_port_test_set(struct ci_hdrc *ci, u8 mode) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 192 | { |
| 193 | const u8 TEST_MODE_MAX = 7; |
| 194 | |
| 195 | if (mode > TEST_MODE_MAX) |
| 196 | return -EINVAL; |
| 197 | |
Felipe Balbi | 727b4dd | 2013-03-30 12:53:55 +0200 | [diff] [blame] | 198 | hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC)); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * hw_port_test_get: reads port test mode value |
| 204 | * |
Peter Chen | 1935388 | 2014-09-22 08:14:17 +0800 | [diff] [blame] | 205 | * @ci: the controller |
| 206 | * |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 207 | * This function returns port test mode value |
| 208 | */ |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 209 | u8 hw_port_test_get(struct ci_hdrc *ci) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 210 | { |
Felipe Balbi | 727b4dd | 2013-03-30 12:53:55 +0200 | [diff] [blame] | 211 | return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 212 | } |
| 213 | |
Peter Chen | b82613c | 2014-11-26 13:44:28 +0800 | [diff] [blame] | 214 | static void hw_wait_phy_stable(void) |
| 215 | { |
| 216 | /* |
| 217 | * The phy needs some delay to output the stable status from low |
| 218 | * power mode. And for OTGSC, the status inputs are debounced |
| 219 | * using a 1 ms time constant, so, delay 2ms for controller to get |
| 220 | * the stable status, like vbus and id when the phy leaves low power. |
| 221 | */ |
| 222 | usleep_range(2000, 2500); |
| 223 | } |
| 224 | |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 225 | /* The PHY enters/leaves low power mode */ |
| 226 | static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable) |
| 227 | { |
| 228 | enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC; |
| 229 | bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm))); |
| 230 | |
Peter Chen | 6d037db | 2014-11-26 13:44:27 +0800 | [diff] [blame] | 231 | if (enable && !lpm) |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 232 | hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm), |
| 233 | PORTSC_PHCD(ci->hw_bank.lpm)); |
Peter Chen | 6d037db | 2014-11-26 13:44:27 +0800 | [diff] [blame] | 234 | else if (!enable && lpm) |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 235 | hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm), |
| 236 | 0); |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 237 | } |
| 238 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 239 | static int hw_device_init(struct ci_hdrc *ci, void __iomem *base) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 240 | { |
| 241 | u32 reg; |
| 242 | |
| 243 | /* bank is a module variable */ |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 244 | ci->hw_bank.abs = base; |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 245 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 246 | ci->hw_bank.cap = ci->hw_bank.abs; |
Richard Zhao | 77c4400 | 2012-06-29 17:48:53 +0800 | [diff] [blame] | 247 | ci->hw_bank.cap += ci->platdata->capoffset; |
Svetoslav Neykov | 938d323 | 2013-03-30 12:54:03 +0200 | [diff] [blame] | 248 | ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 249 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 250 | hw_alloc_regmap(ci, false); |
| 251 | reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >> |
Felipe Balbi | 727b4dd | 2013-03-30 12:53:55 +0200 | [diff] [blame] | 252 | __ffs(HCCPARAMS_LEN); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 253 | ci->hw_bank.lpm = reg; |
Chris Ruehl | aeb2c12 | 2013-12-06 16:35:12 +0800 | [diff] [blame] | 254 | if (reg) |
| 255 | hw_alloc_regmap(ci, !!reg); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 256 | ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs; |
| 257 | ci->hw_bank.size += OP_LAST; |
| 258 | ci->hw_bank.size /= sizeof(u32); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 259 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 260 | reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >> |
Felipe Balbi | 727b4dd | 2013-03-30 12:53:55 +0200 | [diff] [blame] | 261 | __ffs(DCCPARAMS_DEN); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 262 | ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */ |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 263 | |
Richard Zhao | 09c94e6 | 2012-05-15 21:58:18 +0800 | [diff] [blame] | 264 | if (ci->hw_ep_max > ENDPT_MAX) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 265 | return -ENODEV; |
| 266 | |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 267 | ci_hdrc_enter_lpm(ci, false); |
| 268 | |
Peter Chen | c344b51 | 2013-08-14 12:44:09 +0300 | [diff] [blame] | 269 | /* Disable all interrupts bits */ |
| 270 | hw_write(ci, OP_USBINTR, 0xffffffff, 0); |
| 271 | |
| 272 | /* Clear all interrupts status bits*/ |
| 273 | hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff); |
| 274 | |
Peter Chen | cb271f3 | 2015-02-11 12:44:55 +0800 | [diff] [blame] | 275 | ci->rev = ci_get_revision(ci); |
| 276 | |
| 277 | dev_dbg(ci->dev, |
| 278 | "ChipIdea HDRC found, revision: %d, lpm: %d; cap: %p op: %p\n", |
| 279 | ci->rev, ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 280 | |
| 281 | /* setup lock mode ? */ |
| 282 | |
| 283 | /* ENDPTSETUPSTAT is '0' by default */ |
| 284 | |
| 285 | /* HCSPARAMS.bf.ppc SHOULD BE zero for device */ |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 290 | static void hw_phymode_configure(struct ci_hdrc *ci) |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 291 | { |
Chris Ruehl | 3b5d3e6 | 2014-01-10 13:51:29 +0800 | [diff] [blame] | 292 | u32 portsc, lpm, sts = 0; |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 293 | |
| 294 | switch (ci->platdata->phy_mode) { |
| 295 | case USBPHY_INTERFACE_MODE_UTMI: |
| 296 | portsc = PORTSC_PTS(PTS_UTMI); |
| 297 | lpm = DEVLC_PTS(PTS_UTMI); |
| 298 | break; |
| 299 | case USBPHY_INTERFACE_MODE_UTMIW: |
| 300 | portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW; |
| 301 | lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW; |
| 302 | break; |
| 303 | case USBPHY_INTERFACE_MODE_ULPI: |
| 304 | portsc = PORTSC_PTS(PTS_ULPI); |
| 305 | lpm = DEVLC_PTS(PTS_ULPI); |
| 306 | break; |
| 307 | case USBPHY_INTERFACE_MODE_SERIAL: |
| 308 | portsc = PORTSC_PTS(PTS_SERIAL); |
| 309 | lpm = DEVLC_PTS(PTS_SERIAL); |
| 310 | sts = 1; |
| 311 | break; |
| 312 | case USBPHY_INTERFACE_MODE_HSIC: |
| 313 | portsc = PORTSC_PTS(PTS_HSIC); |
| 314 | lpm = DEVLC_PTS(PTS_HSIC); |
| 315 | break; |
| 316 | default: |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | if (ci->hw_bank.lpm) { |
| 321 | hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm); |
Chris Ruehl | 3b5d3e6 | 2014-01-10 13:51:29 +0800 | [diff] [blame] | 322 | if (sts) |
| 323 | hw_write(ci, OP_DEVLC, DEVLC_STS, DEVLC_STS); |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 324 | } else { |
| 325 | hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc); |
Chris Ruehl | 3b5d3e6 | 2014-01-10 13:51:29 +0800 | [diff] [blame] | 326 | if (sts) |
| 327 | hw_write(ci, OP_PORTSC, PORTSC_STS, PORTSC_STS); |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 331 | /** |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 332 | * _ci_usb_phy_init: initialize phy taking in account both phy and usb_phy |
| 333 | * interfaces |
| 334 | * @ci: the controller |
| 335 | * |
| 336 | * This function returns an error code if the phy failed to init |
| 337 | */ |
| 338 | static int _ci_usb_phy_init(struct ci_hdrc *ci) |
| 339 | { |
| 340 | int ret; |
| 341 | |
| 342 | if (ci->phy) { |
| 343 | ret = phy_init(ci->phy); |
| 344 | if (ret) |
| 345 | return ret; |
| 346 | |
| 347 | ret = phy_power_on(ci->phy); |
| 348 | if (ret) { |
| 349 | phy_exit(ci->phy); |
| 350 | return ret; |
| 351 | } |
| 352 | } else { |
| 353 | ret = usb_phy_init(ci->usb_phy); |
| 354 | } |
| 355 | |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * _ci_usb_phy_exit: deinitialize phy taking in account both phy and usb_phy |
| 361 | * interfaces |
| 362 | * @ci: the controller |
| 363 | */ |
| 364 | static void ci_usb_phy_exit(struct ci_hdrc *ci) |
| 365 | { |
| 366 | if (ci->phy) { |
| 367 | phy_power_off(ci->phy); |
| 368 | phy_exit(ci->phy); |
| 369 | } else { |
| 370 | usb_phy_shutdown(ci->usb_phy); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /** |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 375 | * ci_usb_phy_init: initialize phy according to different phy type |
| 376 | * @ci: the controller |
Peter Chen | 1935388 | 2014-09-22 08:14:17 +0800 | [diff] [blame] | 377 | * |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 378 | * This function returns an error code if usb_phy_init has failed |
| 379 | */ |
| 380 | static int ci_usb_phy_init(struct ci_hdrc *ci) |
| 381 | { |
| 382 | int ret; |
| 383 | |
| 384 | switch (ci->platdata->phy_mode) { |
| 385 | case USBPHY_INTERFACE_MODE_UTMI: |
| 386 | case USBPHY_INTERFACE_MODE_UTMIW: |
| 387 | case USBPHY_INTERFACE_MODE_HSIC: |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 388 | ret = _ci_usb_phy_init(ci); |
Peter Chen | b82613c | 2014-11-26 13:44:28 +0800 | [diff] [blame] | 389 | if (!ret) |
| 390 | hw_wait_phy_stable(); |
| 391 | else |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 392 | return ret; |
| 393 | hw_phymode_configure(ci); |
| 394 | break; |
| 395 | case USBPHY_INTERFACE_MODE_ULPI: |
| 396 | case USBPHY_INTERFACE_MODE_SERIAL: |
| 397 | hw_phymode_configure(ci); |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 398 | ret = _ci_usb_phy_init(ci); |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 399 | if (ret) |
| 400 | return ret; |
| 401 | break; |
| 402 | default: |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 403 | ret = _ci_usb_phy_init(ci); |
Peter Chen | b82613c | 2014-11-26 13:44:28 +0800 | [diff] [blame] | 404 | if (!ret) |
| 405 | hw_wait_phy_stable(); |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | return ret; |
| 409 | } |
| 410 | |
Peter Chen | bf9c85e | 2015-03-17 10:40:50 +0800 | [diff] [blame] | 411 | |
| 412 | /** |
| 413 | * ci_platform_configure: do controller configure |
| 414 | * @ci: the controller |
| 415 | * |
| 416 | */ |
| 417 | void ci_platform_configure(struct ci_hdrc *ci) |
| 418 | { |
Peter Chen | 8022d3d | 2014-10-30 09:15:15 +0800 | [diff] [blame] | 419 | bool is_device_mode, is_host_mode; |
| 420 | |
| 421 | is_device_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_DC; |
| 422 | is_host_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_HC; |
| 423 | |
| 424 | if (is_device_mode && |
| 425 | (ci->platdata->flags & CI_HDRC_DISABLE_DEVICE_STREAMING)) |
| 426 | hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS); |
| 427 | |
| 428 | if (is_host_mode && |
| 429 | (ci->platdata->flags & CI_HDRC_DISABLE_HOST_STREAMING)) |
Peter Chen | bf9c85e | 2015-03-17 10:40:50 +0800 | [diff] [blame] | 430 | hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS); |
| 431 | |
| 432 | if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED) { |
| 433 | if (ci->hw_bank.lpm) |
| 434 | hw_write(ci, OP_DEVLC, DEVLC_PFSC, DEVLC_PFSC); |
| 435 | else |
| 436 | hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC); |
| 437 | } |
| 438 | |
| 439 | if (ci->platdata->flags & CI_HDRC_SET_NON_ZERO_TTHA) |
| 440 | hw_write(ci, OP_TTCTRL, TTCTRL_TTHA_MASK, TTCTRL_TTHA); |
Peter Chen | df96ed8 | 2014-09-22 16:45:39 +0800 | [diff] [blame] | 441 | |
| 442 | hw_write(ci, OP_USBCMD, 0xff0000, ci->platdata->itc_setting << 16); |
| 443 | |
Peter Chen | 6566871 | 2015-03-17 14:21:00 +0800 | [diff] [blame] | 444 | if (ci->platdata->flags & CI_HDRC_OVERRIDE_AHB_BURST) |
| 445 | hw_write_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK, |
| 446 | ci->platdata->ahb_burst_config); |
Peter Chen | 96625ea | 2015-03-17 17:32:45 +0800 | [diff] [blame] | 447 | |
| 448 | /* override burst size, take effect only when ahb_burst_config is 0 */ |
| 449 | if (!hw_read_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK)) { |
| 450 | if (ci->platdata->flags & CI_HDRC_OVERRIDE_TX_BURST) |
| 451 | hw_write(ci, OP_BURSTSIZE, TX_BURST_MASK, |
| 452 | ci->platdata->tx_burst_size << __ffs(TX_BURST_MASK)); |
| 453 | |
| 454 | if (ci->platdata->flags & CI_HDRC_OVERRIDE_RX_BURST) |
| 455 | hw_write(ci, OP_BURSTSIZE, RX_BURST_MASK, |
| 456 | ci->platdata->rx_burst_size); |
| 457 | } |
Peter Chen | bf9c85e | 2015-03-17 10:40:50 +0800 | [diff] [blame] | 458 | } |
| 459 | |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 460 | /** |
Peter Chen | cdd278f | 2014-11-26 13:44:32 +0800 | [diff] [blame] | 461 | * hw_controller_reset: do controller reset |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 462 | * @ci: the controller |
| 463 | * |
| 464 | * This function returns an error code |
| 465 | */ |
Peter Chen | cdd278f | 2014-11-26 13:44:32 +0800 | [diff] [blame] | 466 | static int hw_controller_reset(struct ci_hdrc *ci) |
| 467 | { |
| 468 | int count = 0; |
| 469 | |
| 470 | hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST); |
| 471 | while (hw_read(ci, OP_USBCMD, USBCMD_RST)) { |
| 472 | udelay(10); |
| 473 | if (count++ > 1000) |
| 474 | return -ETIMEDOUT; |
| 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * hw_device_reset: resets chip (execute without interruption) |
| 482 | * @ci: the controller |
| 483 | * |
| 484 | * This function returns an error code |
| 485 | */ |
Peter Chen | 5b15730 | 2014-11-26 13:44:33 +0800 | [diff] [blame] | 486 | int hw_device_reset(struct ci_hdrc *ci) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 487 | { |
Peter Chen | cdd278f | 2014-11-26 13:44:32 +0800 | [diff] [blame] | 488 | int ret; |
| 489 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 490 | /* should flush & stop before reset */ |
| 491 | hw_write(ci, OP_ENDPTFLUSH, ~0, ~0); |
| 492 | hw_write(ci, OP_USBCMD, USBCMD_RS, 0); |
| 493 | |
Peter Chen | cdd278f | 2014-11-26 13:44:32 +0800 | [diff] [blame] | 494 | ret = hw_controller_reset(ci); |
| 495 | if (ret) { |
| 496 | dev_err(ci->dev, "error resetting controller, ret=%d\n", ret); |
| 497 | return ret; |
| 498 | } |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 499 | |
Richard Zhao | 77c4400 | 2012-06-29 17:48:53 +0800 | [diff] [blame] | 500 | if (ci->platdata->notify_event) |
| 501 | ci->platdata->notify_event(ci, |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 502 | CI_HDRC_CONTROLLER_RESET_EVENT); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 503 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 504 | /* USBMODE should be configured step by step */ |
| 505 | hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE); |
Peter Chen | 5b15730 | 2014-11-26 13:44:33 +0800 | [diff] [blame] | 506 | hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_DC); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 507 | /* HW >= 2.3 */ |
| 508 | hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM); |
| 509 | |
Peter Chen | 5b15730 | 2014-11-26 13:44:33 +0800 | [diff] [blame] | 510 | if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DC) { |
| 511 | pr_err("cannot enter in %s device mode", ci_role(ci)->name); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 512 | pr_err("lpm = %i", ci->hw_bank.lpm); |
| 513 | return -ENODEV; |
| 514 | } |
| 515 | |
Peter Chen | bf9c85e | 2015-03-17 10:40:50 +0800 | [diff] [blame] | 516 | ci_platform_configure(ci); |
| 517 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | |
Peter Chen | 22fa844 | 2013-08-14 12:44:12 +0300 | [diff] [blame] | 521 | /** |
| 522 | * hw_wait_reg: wait the register value |
| 523 | * |
| 524 | * Sometimes, it needs to wait register value before going on. |
| 525 | * Eg, when switch to device mode, the vbus value should be lower |
| 526 | * than OTGSC_BSV before connects to host. |
| 527 | * |
| 528 | * @ci: the controller |
| 529 | * @reg: register index |
| 530 | * @mask: mast bit |
| 531 | * @value: the bit value to wait |
| 532 | * @timeout_ms: timeout in millisecond |
| 533 | * |
| 534 | * This function returns an error code if timeout |
| 535 | */ |
| 536 | int hw_wait_reg(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask, |
| 537 | u32 value, unsigned int timeout_ms) |
| 538 | { |
| 539 | unsigned long elapse = jiffies + msecs_to_jiffies(timeout_ms); |
| 540 | |
| 541 | while (hw_read(ci, reg, mask) != value) { |
| 542 | if (time_after(jiffies, elapse)) { |
| 543 | dev_err(ci->dev, "timeout waiting for %08x in %d\n", |
| 544 | mask, reg); |
| 545 | return -ETIMEDOUT; |
| 546 | } |
| 547 | msleep(20); |
| 548 | } |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 553 | static irqreturn_t ci_irq(int irq, void *data) |
| 554 | { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 555 | struct ci_hdrc *ci = data; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 556 | irqreturn_t ret = IRQ_NONE; |
Richard Zhao | b183c19 | 2012-09-12 14:58:11 +0300 | [diff] [blame] | 557 | u32 otgsc = 0; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 558 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 559 | if (ci->in_lpm) { |
| 560 | disable_irq_nosync(irq); |
| 561 | ci->wakeup_int = true; |
| 562 | pm_runtime_get(ci->dev); |
| 563 | return IRQ_HANDLED; |
| 564 | } |
| 565 | |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 566 | if (ci->is_otg) { |
Li Jun | 0c33bf7 | 2014-04-23 15:56:38 +0800 | [diff] [blame] | 567 | otgsc = hw_read_otgsc(ci, ~0); |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 568 | if (ci_otg_is_fsm_mode(ci)) { |
| 569 | ret = ci_otg_fsm_irq(ci); |
| 570 | if (ret == IRQ_HANDLED) |
| 571 | return ret; |
| 572 | } |
| 573 | } |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 574 | |
Peter Chen | a107f8c | 2013-08-14 12:44:11 +0300 | [diff] [blame] | 575 | /* |
| 576 | * Handle id change interrupt, it indicates device/host function |
| 577 | * switch. |
| 578 | */ |
| 579 | if (ci->is_otg && (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) { |
| 580 | ci->id_event = true; |
Li Jun | 0c33bf7 | 2014-04-23 15:56:38 +0800 | [diff] [blame] | 581 | /* Clear ID change irq status */ |
| 582 | hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS); |
Peter Chen | be6b0c1 | 2014-05-23 08:12:49 +0800 | [diff] [blame] | 583 | ci_otg_queue_work(ci); |
Peter Chen | a107f8c | 2013-08-14 12:44:11 +0300 | [diff] [blame] | 584 | return IRQ_HANDLED; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 585 | } |
| 586 | |
Peter Chen | a107f8c | 2013-08-14 12:44:11 +0300 | [diff] [blame] | 587 | /* |
| 588 | * Handle vbus change interrupt, it indicates device connection |
| 589 | * and disconnection events. |
| 590 | */ |
| 591 | if (ci->is_otg && (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) { |
| 592 | ci->b_sess_valid_event = true; |
Li Jun | 0c33bf7 | 2014-04-23 15:56:38 +0800 | [diff] [blame] | 593 | /* Clear BSV irq */ |
| 594 | hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS); |
Peter Chen | be6b0c1 | 2014-05-23 08:12:49 +0800 | [diff] [blame] | 595 | ci_otg_queue_work(ci); |
Peter Chen | a107f8c | 2013-08-14 12:44:11 +0300 | [diff] [blame] | 596 | return IRQ_HANDLED; |
| 597 | } |
| 598 | |
| 599 | /* Handle device/host interrupt */ |
| 600 | if (ci->role != CI_ROLE_END) |
| 601 | ret = ci_role(ci)->irq(ci); |
| 602 | |
Richard Zhao | b183c19 | 2012-09-12 14:58:11 +0300 | [diff] [blame] | 603 | return ret; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 604 | } |
| 605 | |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame^] | 606 | static int ci_vbus_notifier(struct notifier_block *nb, unsigned long event, |
| 607 | void *ptr) |
| 608 | { |
| 609 | struct ci_hdrc_cable *vbus = container_of(nb, struct ci_hdrc_cable, nb); |
| 610 | struct ci_hdrc *ci = vbus->ci; |
| 611 | |
| 612 | if (event) |
| 613 | vbus->state = true; |
| 614 | else |
| 615 | vbus->state = false; |
| 616 | |
| 617 | vbus->changed = true; |
| 618 | |
| 619 | ci_irq(ci->irq, ci); |
| 620 | return NOTIFY_DONE; |
| 621 | } |
| 622 | |
| 623 | static int ci_id_notifier(struct notifier_block *nb, unsigned long event, |
| 624 | void *ptr) |
| 625 | { |
| 626 | struct ci_hdrc_cable *id = container_of(nb, struct ci_hdrc_cable, nb); |
| 627 | struct ci_hdrc *ci = id->ci; |
| 628 | |
| 629 | if (event) |
| 630 | id->state = false; |
| 631 | else |
| 632 | id->state = true; |
| 633 | |
| 634 | id->changed = true; |
| 635 | |
| 636 | ci_irq(ci->irq, ci); |
| 637 | return NOTIFY_DONE; |
| 638 | } |
| 639 | |
Peter Chen | 1542d9c | 2013-08-14 12:44:03 +0300 | [diff] [blame] | 640 | static int ci_get_platdata(struct device *dev, |
| 641 | struct ci_hdrc_platform_data *platdata) |
| 642 | { |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame^] | 643 | struct extcon_dev *ext_vbus, *ext_id; |
| 644 | struct ci_hdrc_cable *cable; |
Li Jun | 7974235 | 2015-07-09 15:18:45 +0800 | [diff] [blame] | 645 | int ret; |
| 646 | |
Peter Chen | c22600c | 2013-09-17 12:37:22 +0800 | [diff] [blame] | 647 | if (!platdata->phy_mode) |
| 648 | platdata->phy_mode = of_usb_get_phy_mode(dev->of_node); |
| 649 | |
| 650 | if (!platdata->dr_mode) |
| 651 | platdata->dr_mode = of_usb_get_dr_mode(dev->of_node); |
| 652 | |
| 653 | if (platdata->dr_mode == USB_DR_MODE_UNKNOWN) |
| 654 | platdata->dr_mode = USB_DR_MODE_OTG; |
| 655 | |
Peter Chen | c2ec3a7 | 2013-10-30 09:19:29 +0800 | [diff] [blame] | 656 | if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) { |
| 657 | /* Get the vbus regulator */ |
| 658 | platdata->reg_vbus = devm_regulator_get(dev, "vbus"); |
| 659 | if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) { |
| 660 | return -EPROBE_DEFER; |
| 661 | } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) { |
Mickael Maison | 6629467 | 2014-11-26 13:44:38 +0800 | [diff] [blame] | 662 | /* no vbus regulator is needed */ |
Peter Chen | c2ec3a7 | 2013-10-30 09:19:29 +0800 | [diff] [blame] | 663 | platdata->reg_vbus = NULL; |
| 664 | } else if (IS_ERR(platdata->reg_vbus)) { |
| 665 | dev_err(dev, "Getting regulator error: %ld\n", |
| 666 | PTR_ERR(platdata->reg_vbus)); |
| 667 | return PTR_ERR(platdata->reg_vbus); |
| 668 | } |
Peter Chen | f6a9ff0 | 2014-08-19 09:51:56 +0800 | [diff] [blame] | 669 | /* Get TPL support */ |
| 670 | if (!platdata->tpl_support) |
| 671 | platdata->tpl_support = |
| 672 | of_usb_host_tpl_support(dev->of_node); |
Peter Chen | c2ec3a7 | 2013-10-30 09:19:29 +0800 | [diff] [blame] | 673 | } |
| 674 | |
Li Jun | 7974235 | 2015-07-09 15:18:45 +0800 | [diff] [blame] | 675 | if (platdata->dr_mode == USB_DR_MODE_OTG) { |
| 676 | /* We can support HNP and SRP of OTG 2.0 */ |
| 677 | platdata->ci_otg_caps.otg_rev = 0x0200; |
| 678 | platdata->ci_otg_caps.hnp_support = true; |
| 679 | platdata->ci_otg_caps.srp_support = true; |
| 680 | |
| 681 | /* Update otg capabilities by DT properties */ |
| 682 | ret = of_usb_update_otg_caps(dev->of_node, |
| 683 | &platdata->ci_otg_caps); |
| 684 | if (ret) |
| 685 | return ret; |
| 686 | } |
| 687 | |
Michael Grzeschik | 4f6743d | 2014-02-19 13:41:43 +0800 | [diff] [blame] | 688 | if (of_usb_get_maximum_speed(dev->of_node) == USB_SPEED_FULL) |
| 689 | platdata->flags |= CI_HDRC_FORCE_FULLSPEED; |
| 690 | |
Peter Chen | df96ed8 | 2014-09-22 16:45:39 +0800 | [diff] [blame] | 691 | platdata->itc_setting = 1; |
| 692 | if (of_find_property(dev->of_node, "itc-setting", NULL)) { |
| 693 | ret = of_property_read_u32(dev->of_node, "itc-setting", |
| 694 | &platdata->itc_setting); |
| 695 | if (ret) { |
| 696 | dev_err(dev, |
| 697 | "failed to get itc-setting\n"); |
| 698 | return ret; |
| 699 | } |
| 700 | } |
| 701 | |
Peter Chen | 6566871 | 2015-03-17 14:21:00 +0800 | [diff] [blame] | 702 | if (of_find_property(dev->of_node, "ahb-burst-config", NULL)) { |
| 703 | ret = of_property_read_u32(dev->of_node, "ahb-burst-config", |
| 704 | &platdata->ahb_burst_config); |
| 705 | if (ret) { |
| 706 | dev_err(dev, |
| 707 | "failed to get ahb-burst-config\n"); |
| 708 | return ret; |
| 709 | } |
| 710 | platdata->flags |= CI_HDRC_OVERRIDE_AHB_BURST; |
| 711 | } |
| 712 | |
Peter Chen | 96625ea | 2015-03-17 17:32:45 +0800 | [diff] [blame] | 713 | if (of_find_property(dev->of_node, "tx-burst-size-dword", NULL)) { |
| 714 | ret = of_property_read_u32(dev->of_node, "tx-burst-size-dword", |
| 715 | &platdata->tx_burst_size); |
| 716 | if (ret) { |
| 717 | dev_err(dev, |
| 718 | "failed to get tx-burst-size-dword\n"); |
| 719 | return ret; |
| 720 | } |
| 721 | platdata->flags |= CI_HDRC_OVERRIDE_TX_BURST; |
| 722 | } |
| 723 | |
| 724 | if (of_find_property(dev->of_node, "rx-burst-size-dword", NULL)) { |
| 725 | ret = of_property_read_u32(dev->of_node, "rx-burst-size-dword", |
| 726 | &platdata->rx_burst_size); |
| 727 | if (ret) { |
| 728 | dev_err(dev, |
| 729 | "failed to get rx-burst-size-dword\n"); |
| 730 | return ret; |
| 731 | } |
| 732 | platdata->flags |= CI_HDRC_OVERRIDE_RX_BURST; |
| 733 | } |
| 734 | |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame^] | 735 | ext_id = ERR_PTR(-ENODEV); |
| 736 | ext_vbus = ERR_PTR(-ENODEV); |
| 737 | if (of_property_read_bool(dev->of_node, "extcon")) { |
| 738 | /* Each one of them is not mandatory */ |
| 739 | ext_vbus = extcon_get_edev_by_phandle(dev, 0); |
| 740 | if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV) |
| 741 | return PTR_ERR(ext_vbus); |
| 742 | |
| 743 | ext_id = extcon_get_edev_by_phandle(dev, 1); |
| 744 | if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV) |
| 745 | return PTR_ERR(ext_id); |
| 746 | } |
| 747 | |
| 748 | cable = &platdata->vbus_extcon; |
| 749 | cable->nb.notifier_call = ci_vbus_notifier; |
| 750 | cable->edev = ext_vbus; |
| 751 | |
| 752 | if (!IS_ERR(ext_vbus)) { |
| 753 | ret = extcon_get_cable_state_(cable->edev, EXTCON_USB); |
| 754 | if (ret) |
| 755 | cable->state = true; |
| 756 | else |
| 757 | cable->state = false; |
| 758 | } |
| 759 | |
| 760 | cable = &platdata->id_extcon; |
| 761 | cable->nb.notifier_call = ci_id_notifier; |
| 762 | cable->edev = ext_id; |
| 763 | |
| 764 | if (!IS_ERR(ext_id)) { |
| 765 | ret = extcon_get_cable_state_(cable->edev, EXTCON_USB_HOST); |
| 766 | if (ret) |
| 767 | cable->state = false; |
| 768 | else |
| 769 | cable->state = true; |
| 770 | } |
Peter Chen | 1542d9c | 2013-08-14 12:44:03 +0300 | [diff] [blame] | 771 | return 0; |
| 772 | } |
| 773 | |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame^] | 774 | static int ci_extcon_register(struct ci_hdrc *ci) |
| 775 | { |
| 776 | struct ci_hdrc_cable *id, *vbus; |
| 777 | int ret; |
| 778 | |
| 779 | id = &ci->platdata->id_extcon; |
| 780 | id->ci = ci; |
| 781 | if (!IS_ERR(id->edev)) { |
| 782 | ret = extcon_register_notifier(id->edev, EXTCON_USB_HOST, |
| 783 | &id->nb); |
| 784 | if (ret < 0) { |
| 785 | dev_err(ci->dev, "register ID failed\n"); |
| 786 | return ret; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | vbus = &ci->platdata->vbus_extcon; |
| 791 | vbus->ci = ci; |
| 792 | if (!IS_ERR(vbus->edev)) { |
| 793 | ret = extcon_register_notifier(vbus->edev, EXTCON_USB, |
| 794 | &vbus->nb); |
| 795 | if (ret < 0) { |
| 796 | extcon_unregister_notifier(id->edev, EXTCON_USB_HOST, |
| 797 | &id->nb); |
| 798 | dev_err(ci->dev, "register VBUS failed\n"); |
| 799 | return ret; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | static void ci_extcon_unregister(struct ci_hdrc *ci) |
| 807 | { |
| 808 | struct ci_hdrc_cable *cable; |
| 809 | |
| 810 | cable = &ci->platdata->id_extcon; |
| 811 | if (!IS_ERR(cable->edev)) |
| 812 | extcon_unregister_notifier(cable->edev, EXTCON_USB_HOST, |
| 813 | &cable->nb); |
| 814 | |
| 815 | cable = &ci->platdata->vbus_extcon; |
| 816 | if (!IS_ERR(cable->edev)) |
| 817 | extcon_unregister_notifier(cable->edev, EXTCON_USB, &cable->nb); |
| 818 | } |
| 819 | |
Richard Zhao | fe6e125 | 2012-07-07 22:56:42 +0800 | [diff] [blame] | 820 | static DEFINE_IDA(ci_ida); |
| 821 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 822 | struct platform_device *ci_hdrc_add_device(struct device *dev, |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 823 | struct resource *res, int nres, |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 824 | struct ci_hdrc_platform_data *platdata) |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 825 | { |
| 826 | struct platform_device *pdev; |
Richard Zhao | fe6e125 | 2012-07-07 22:56:42 +0800 | [diff] [blame] | 827 | int id, ret; |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 828 | |
Peter Chen | 1542d9c | 2013-08-14 12:44:03 +0300 | [diff] [blame] | 829 | ret = ci_get_platdata(dev, platdata); |
| 830 | if (ret) |
| 831 | return ERR_PTR(ret); |
| 832 | |
Richard Zhao | fe6e125 | 2012-07-07 22:56:42 +0800 | [diff] [blame] | 833 | id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL); |
| 834 | if (id < 0) |
| 835 | return ERR_PTR(id); |
| 836 | |
| 837 | pdev = platform_device_alloc("ci_hdrc", id); |
| 838 | if (!pdev) { |
| 839 | ret = -ENOMEM; |
| 840 | goto put_id; |
| 841 | } |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 842 | |
| 843 | pdev->dev.parent = dev; |
| 844 | pdev->dev.dma_mask = dev->dma_mask; |
| 845 | pdev->dev.dma_parms = dev->dma_parms; |
| 846 | dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask); |
| 847 | |
| 848 | ret = platform_device_add_resources(pdev, res, nres); |
| 849 | if (ret) |
| 850 | goto err; |
| 851 | |
| 852 | ret = platform_device_add_data(pdev, platdata, sizeof(*platdata)); |
| 853 | if (ret) |
| 854 | goto err; |
| 855 | |
| 856 | ret = platform_device_add(pdev); |
| 857 | if (ret) |
| 858 | goto err; |
| 859 | |
| 860 | return pdev; |
| 861 | |
| 862 | err: |
| 863 | platform_device_put(pdev); |
Richard Zhao | fe6e125 | 2012-07-07 22:56:42 +0800 | [diff] [blame] | 864 | put_id: |
| 865 | ida_simple_remove(&ci_ida, id); |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 866 | return ERR_PTR(ret); |
| 867 | } |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 868 | EXPORT_SYMBOL_GPL(ci_hdrc_add_device); |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 869 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 870 | void ci_hdrc_remove_device(struct platform_device *pdev) |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 871 | { |
Lothar Waßmann | 98c3553 | 2012-11-22 10:11:25 +0100 | [diff] [blame] | 872 | int id = pdev->id; |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 873 | platform_device_unregister(pdev); |
Lothar Waßmann | 98c3553 | 2012-11-22 10:11:25 +0100 | [diff] [blame] | 874 | ida_simple_remove(&ci_ida, id); |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 875 | } |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 876 | EXPORT_SYMBOL_GPL(ci_hdrc_remove_device); |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 877 | |
Peter Chen | 3f124d2 | 2013-08-14 12:44:07 +0300 | [diff] [blame] | 878 | static inline void ci_role_destroy(struct ci_hdrc *ci) |
| 879 | { |
| 880 | ci_hdrc_gadget_destroy(ci); |
| 881 | ci_hdrc_host_destroy(ci); |
Peter Chen | cbec6bd | 2013-08-14 12:44:10 +0300 | [diff] [blame] | 882 | if (ci->is_otg) |
| 883 | ci_hdrc_otg_destroy(ci); |
Peter Chen | 3f124d2 | 2013-08-14 12:44:07 +0300 | [diff] [blame] | 884 | } |
| 885 | |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 886 | static void ci_get_otg_capable(struct ci_hdrc *ci) |
| 887 | { |
| 888 | if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG) |
| 889 | ci->is_otg = false; |
| 890 | else |
| 891 | ci->is_otg = (hw_read(ci, CAP_DCCPARAMS, |
| 892 | DCCPARAMS_DC | DCCPARAMS_HC) |
| 893 | == (DCCPARAMS_DC | DCCPARAMS_HC)); |
Peter Chen | 2e37cfd | 2015-02-11 12:44:51 +0800 | [diff] [blame] | 894 | if (ci->is_otg) { |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 895 | dev_dbg(ci->dev, "It is OTG capable controller\n"); |
Peter Chen | 2e37cfd | 2015-02-11 12:44:51 +0800 | [diff] [blame] | 896 | /* Disable and clear all OTG irq */ |
| 897 | hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS, |
| 898 | OTGSC_INT_STATUS_BITS); |
| 899 | } |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 900 | } |
| 901 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 902 | static int ci_hdrc_probe(struct platform_device *pdev) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 903 | { |
| 904 | struct device *dev = &pdev->dev; |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 905 | struct ci_hdrc *ci; |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 906 | struct resource *res; |
| 907 | void __iomem *base; |
| 908 | int ret; |
Sascha Hauer | 691962d | 2013-06-13 17:59:57 +0300 | [diff] [blame] | 909 | enum usb_dr_mode dr_mode; |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 910 | |
Jingoo Han | fad5674 | 2014-02-19 13:41:42 +0800 | [diff] [blame] | 911 | if (!dev_get_platdata(dev)) { |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 912 | dev_err(dev, "platform data missing\n"); |
| 913 | return -ENODEV; |
| 914 | } |
| 915 | |
| 916 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Felipe Balbi | 1929081 | 2013-03-30 02:46:27 +0200 | [diff] [blame] | 917 | base = devm_ioremap_resource(dev, res); |
| 918 | if (IS_ERR(base)) |
| 919 | return PTR_ERR(base); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 920 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 921 | ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL); |
Fabio Estevam | d0f9924 | 2014-11-26 13:44:23 +0800 | [diff] [blame] | 922 | if (!ci) |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 923 | return -ENOMEM; |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 924 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 925 | ci->dev = dev; |
Jingoo Han | fad5674 | 2014-02-19 13:41:42 +0800 | [diff] [blame] | 926 | ci->platdata = dev_get_platdata(dev); |
Peter Chen | ed8f831 | 2014-01-10 13:51:27 +0800 | [diff] [blame] | 927 | ci->imx28_write_fix = !!(ci->platdata->flags & |
| 928 | CI_HDRC_IMX28_WRITE_FIX); |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 929 | ci->supports_runtime_pm = !!(ci->platdata->flags & |
| 930 | CI_HDRC_SUPPORTS_RUNTIME_PM); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 931 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 932 | ret = hw_device_init(ci, base); |
| 933 | if (ret < 0) { |
| 934 | dev_err(dev, "can't initialize hardware\n"); |
| 935 | return -ENODEV; |
| 936 | } |
| 937 | |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 938 | if (ci->platdata->phy) { |
| 939 | ci->phy = ci->platdata->phy; |
| 940 | } else if (ci->platdata->usb_phy) { |
Antoine Tenart | ef44cb4 | 2014-10-30 18:41:16 +0100 | [diff] [blame] | 941 | ci->usb_phy = ci->platdata->usb_phy; |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 942 | } else { |
Antoine Tenart | 21a5b57 | 2014-11-26 13:44:35 +0800 | [diff] [blame] | 943 | ci->phy = devm_phy_get(dev->parent, "usb-phy"); |
| 944 | ci->usb_phy = devm_usb_get_phy(dev->parent, USB_PHY_TYPE_USB2); |
Peter Chen | c859aa65 | 2014-02-19 13:41:40 +0800 | [diff] [blame] | 945 | |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 946 | /* if both generic PHY and USB PHY layers aren't enabled */ |
| 947 | if (PTR_ERR(ci->phy) == -ENOSYS && |
| 948 | PTR_ERR(ci->usb_phy) == -ENXIO) |
| 949 | return -ENXIO; |
Peter Chen | c859aa65 | 2014-02-19 13:41:40 +0800 | [diff] [blame] | 950 | |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 951 | if (IS_ERR(ci->phy) && IS_ERR(ci->usb_phy)) |
| 952 | return -EPROBE_DEFER; |
| 953 | |
| 954 | if (IS_ERR(ci->phy)) |
| 955 | ci->phy = NULL; |
| 956 | else if (IS_ERR(ci->usb_phy)) |
| 957 | ci->usb_phy = NULL; |
Peter Chen | c859aa65 | 2014-02-19 13:41:40 +0800 | [diff] [blame] | 958 | } |
| 959 | |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 960 | ret = ci_usb_phy_init(ci); |
Peter Chen | 74475ed | 2013-09-24 12:47:53 +0800 | [diff] [blame] | 961 | if (ret) { |
| 962 | dev_err(dev, "unable to init phy: %d\n", ret); |
| 963 | return ret; |
| 964 | } |
| 965 | |
Alexander Shishkin | eb70e5a | 2012-05-11 17:25:54 +0300 | [diff] [blame] | 966 | ci->hw_bank.phys = res->start; |
| 967 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 968 | ci->irq = platform_get_irq(pdev, 0); |
| 969 | if (ci->irq < 0) { |
| 970 | dev_err(dev, "missing IRQ\n"); |
Fabio Estevam | 42d1821 | 2014-02-19 13:41:44 +0800 | [diff] [blame] | 971 | ret = ci->irq; |
Peter Chen | c859aa65 | 2014-02-19 13:41:40 +0800 | [diff] [blame] | 972 | goto deinit_phy; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 973 | } |
| 974 | |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 975 | ci_get_otg_capable(ci); |
| 976 | |
Sascha Hauer | 691962d | 2013-06-13 17:59:57 +0300 | [diff] [blame] | 977 | dr_mode = ci->platdata->dr_mode; |
| 978 | /* initialize role(s) before the interrupt is requested */ |
| 979 | if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) { |
| 980 | ret = ci_hdrc_host_init(ci); |
| 981 | if (ret) |
| 982 | dev_info(dev, "doesn't support host\n"); |
| 983 | } |
| 984 | |
| 985 | if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) { |
| 986 | ret = ci_hdrc_gadget_init(ci); |
| 987 | if (ret) |
| 988 | dev_info(dev, "doesn't support gadget\n"); |
| 989 | } |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 990 | |
| 991 | if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) { |
| 992 | dev_err(dev, "no supported roles\n"); |
Peter Chen | 74475ed | 2013-09-24 12:47:53 +0800 | [diff] [blame] | 993 | ret = -ENODEV; |
Peter Chen | c859aa65 | 2014-02-19 13:41:40 +0800 | [diff] [blame] | 994 | goto deinit_phy; |
Peter Chen | cbec6bd | 2013-08-14 12:44:10 +0300 | [diff] [blame] | 995 | } |
| 996 | |
Peter Chen | 27c62c2 | 2014-09-22 08:14:16 +0800 | [diff] [blame] | 997 | if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) { |
Peter Chen | cbec6bd | 2013-08-14 12:44:10 +0300 | [diff] [blame] | 998 | ret = ci_hdrc_otg_init(ci); |
| 999 | if (ret) { |
| 1000 | dev_err(dev, "init otg fails, ret = %d\n", ret); |
| 1001 | goto stop; |
| 1002 | } |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) { |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 1006 | if (ci->is_otg) { |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 1007 | ci->role = ci_otg_role(ci); |
Li Jun | 0c33bf7 | 2014-04-23 15:56:38 +0800 | [diff] [blame] | 1008 | /* Enable ID change irq */ |
| 1009 | hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE); |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 1010 | } else { |
| 1011 | /* |
| 1012 | * If the controller is not OTG capable, but support |
| 1013 | * role switch, the defalt role is gadget, and the |
| 1014 | * user can switch it through debugfs. |
| 1015 | */ |
| 1016 | ci->role = CI_ROLE_GADGET; |
| 1017 | } |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1018 | } else { |
| 1019 | ci->role = ci->roles[CI_ROLE_HOST] |
| 1020 | ? CI_ROLE_HOST |
| 1021 | : CI_ROLE_GADGET; |
| 1022 | } |
| 1023 | |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 1024 | if (!ci_otg_is_fsm_mode(ci)) { |
Li Jun | 961ea49 | 2015-02-11 12:45:03 +0800 | [diff] [blame] | 1025 | /* only update vbus status for peripheral */ |
| 1026 | if (ci->role == CI_ROLE_GADGET) |
| 1027 | ci_handle_vbus_change(ci); |
| 1028 | |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 1029 | ret = ci_role_start(ci, ci->role); |
| 1030 | if (ret) { |
| 1031 | dev_err(dev, "can't start %s role\n", |
| 1032 | ci_role(ci)->name); |
| 1033 | goto stop; |
| 1034 | } |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1035 | } |
| 1036 | |
Peter Chen | 24c498d | 2014-12-24 11:33:17 +0800 | [diff] [blame] | 1037 | platform_set_drvdata(pdev, ci); |
Peter Chen | 4c503dd | 2014-11-26 13:44:22 +0800 | [diff] [blame] | 1038 | ret = devm_request_irq(dev, ci->irq, ci_irq, IRQF_SHARED, |
| 1039 | ci->platdata->name, ci); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1040 | if (ret) |
| 1041 | goto stop; |
| 1042 | |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame^] | 1043 | ret = ci_extcon_register(ci); |
| 1044 | if (ret) |
| 1045 | goto stop; |
| 1046 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1047 | if (ci->supports_runtime_pm) { |
| 1048 | pm_runtime_set_active(&pdev->dev); |
| 1049 | pm_runtime_enable(&pdev->dev); |
| 1050 | pm_runtime_set_autosuspend_delay(&pdev->dev, 2000); |
| 1051 | pm_runtime_mark_last_busy(ci->dev); |
| 1052 | pm_runtime_use_autosuspend(&pdev->dev); |
| 1053 | } |
| 1054 | |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 1055 | if (ci_otg_is_fsm_mode(ci)) |
| 1056 | ci_hdrc_otg_fsm_start(ci); |
| 1057 | |
Peter Chen | f8efa76 | 2015-02-11 12:44:48 +0800 | [diff] [blame] | 1058 | device_set_wakeup_capable(&pdev->dev, true); |
| 1059 | |
Alexander Shishkin | adf0f73 | 2013-03-30 12:53:53 +0200 | [diff] [blame] | 1060 | ret = dbg_create_files(ci); |
| 1061 | if (!ret) |
| 1062 | return 0; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1063 | |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame^] | 1064 | ci_extcon_unregister(ci); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1065 | stop: |
Peter Chen | 3f124d2 | 2013-08-14 12:44:07 +0300 | [diff] [blame] | 1066 | ci_role_destroy(ci); |
Peter Chen | c859aa65 | 2014-02-19 13:41:40 +0800 | [diff] [blame] | 1067 | deinit_phy: |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 1068 | ci_usb_phy_exit(ci); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1069 | |
| 1070 | return ret; |
| 1071 | } |
| 1072 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 1073 | static int ci_hdrc_remove(struct platform_device *pdev) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1074 | { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 1075 | struct ci_hdrc *ci = platform_get_drvdata(pdev); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1076 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1077 | if (ci->supports_runtime_pm) { |
| 1078 | pm_runtime_get_sync(&pdev->dev); |
| 1079 | pm_runtime_disable(&pdev->dev); |
| 1080 | pm_runtime_put_noidle(&pdev->dev); |
| 1081 | } |
| 1082 | |
Alexander Shishkin | adf0f73 | 2013-03-30 12:53:53 +0200 | [diff] [blame] | 1083 | dbg_remove_files(ci); |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame^] | 1084 | ci_extcon_unregister(ci); |
Peter Chen | 3f124d2 | 2013-08-14 12:44:07 +0300 | [diff] [blame] | 1085 | ci_role_destroy(ci); |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 1086 | ci_hdrc_enter_lpm(ci, true); |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 1087 | ci_usb_phy_exit(ci); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1088 | |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1092 | #ifdef CONFIG_PM |
Li Jun | 961ea49 | 2015-02-11 12:45:03 +0800 | [diff] [blame] | 1093 | /* Prepare wakeup by SRP before suspend */ |
| 1094 | static void ci_otg_fsm_suspend_for_srp(struct ci_hdrc *ci) |
| 1095 | { |
| 1096 | if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) && |
| 1097 | !hw_read_otgsc(ci, OTGSC_ID)) { |
| 1098 | hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP, |
| 1099 | PORTSC_PP); |
| 1100 | hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_WKCN, |
| 1101 | PORTSC_WKCN); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | /* Handle SRP when wakeup by data pulse */ |
| 1106 | static void ci_otg_fsm_wakeup_by_srp(struct ci_hdrc *ci) |
| 1107 | { |
| 1108 | if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) && |
| 1109 | (ci->fsm.a_bus_drop == 1) && (ci->fsm.a_bus_req == 0)) { |
| 1110 | if (!hw_read_otgsc(ci, OTGSC_ID)) { |
| 1111 | ci->fsm.a_srp_det = 1; |
| 1112 | ci->fsm.a_bus_drop = 0; |
| 1113 | } else { |
| 1114 | ci->fsm.id = 1; |
| 1115 | } |
| 1116 | ci_otg_queue_work(ci); |
| 1117 | } |
| 1118 | } |
| 1119 | |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1120 | static void ci_controller_suspend(struct ci_hdrc *ci) |
| 1121 | { |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1122 | disable_irq(ci->irq); |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1123 | ci_hdrc_enter_lpm(ci, true); |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1124 | usb_phy_set_suspend(ci->usb_phy, 1); |
| 1125 | ci->in_lpm = true; |
| 1126 | enable_irq(ci->irq); |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | static int ci_controller_resume(struct device *dev) |
| 1130 | { |
| 1131 | struct ci_hdrc *ci = dev_get_drvdata(dev); |
| 1132 | |
| 1133 | dev_dbg(dev, "at %s\n", __func__); |
| 1134 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1135 | if (!ci->in_lpm) { |
| 1136 | WARN_ON(1); |
| 1137 | return 0; |
| 1138 | } |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1139 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1140 | ci_hdrc_enter_lpm(ci, false); |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1141 | if (ci->usb_phy) { |
| 1142 | usb_phy_set_suspend(ci->usb_phy, 0); |
| 1143 | usb_phy_set_wakeup(ci->usb_phy, false); |
| 1144 | hw_wait_phy_stable(); |
| 1145 | } |
| 1146 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1147 | ci->in_lpm = false; |
| 1148 | if (ci->wakeup_int) { |
| 1149 | ci->wakeup_int = false; |
| 1150 | pm_runtime_mark_last_busy(ci->dev); |
| 1151 | pm_runtime_put_autosuspend(ci->dev); |
| 1152 | enable_irq(ci->irq); |
Li Jun | 961ea49 | 2015-02-11 12:45:03 +0800 | [diff] [blame] | 1153 | if (ci_otg_is_fsm_mode(ci)) |
| 1154 | ci_otg_fsm_wakeup_by_srp(ci); |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1155 | } |
| 1156 | |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1157 | return 0; |
| 1158 | } |
| 1159 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1160 | #ifdef CONFIG_PM_SLEEP |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1161 | static int ci_suspend(struct device *dev) |
| 1162 | { |
| 1163 | struct ci_hdrc *ci = dev_get_drvdata(dev); |
| 1164 | |
| 1165 | if (ci->wq) |
| 1166 | flush_workqueue(ci->wq); |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1167 | /* |
| 1168 | * Controller needs to be active during suspend, otherwise the core |
| 1169 | * may run resume when the parent is at suspend if other driver's |
| 1170 | * suspend fails, it occurs before parent's suspend has not started, |
| 1171 | * but the core suspend has finished. |
| 1172 | */ |
| 1173 | if (ci->in_lpm) |
| 1174 | pm_runtime_resume(dev); |
| 1175 | |
| 1176 | if (ci->in_lpm) { |
| 1177 | WARN_ON(1); |
| 1178 | return 0; |
| 1179 | } |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1180 | |
Peter Chen | f8efa76 | 2015-02-11 12:44:48 +0800 | [diff] [blame] | 1181 | if (device_may_wakeup(dev)) { |
Li Jun | 961ea49 | 2015-02-11 12:45:03 +0800 | [diff] [blame] | 1182 | if (ci_otg_is_fsm_mode(ci)) |
| 1183 | ci_otg_fsm_suspend_for_srp(ci); |
| 1184 | |
Peter Chen | f8efa76 | 2015-02-11 12:44:48 +0800 | [diff] [blame] | 1185 | usb_phy_set_wakeup(ci->usb_phy, true); |
| 1186 | enable_irq_wake(ci->irq); |
| 1187 | } |
| 1188 | |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1189 | ci_controller_suspend(ci); |
| 1190 | |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | static int ci_resume(struct device *dev) |
| 1195 | { |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1196 | struct ci_hdrc *ci = dev_get_drvdata(dev); |
| 1197 | int ret; |
| 1198 | |
Peter Chen | f8efa76 | 2015-02-11 12:44:48 +0800 | [diff] [blame] | 1199 | if (device_may_wakeup(dev)) |
| 1200 | disable_irq_wake(ci->irq); |
| 1201 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1202 | ret = ci_controller_resume(dev); |
| 1203 | if (ret) |
| 1204 | return ret; |
| 1205 | |
| 1206 | if (ci->supports_runtime_pm) { |
| 1207 | pm_runtime_disable(dev); |
| 1208 | pm_runtime_set_active(dev); |
| 1209 | pm_runtime_enable(dev); |
| 1210 | } |
| 1211 | |
| 1212 | return ret; |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1213 | } |
| 1214 | #endif /* CONFIG_PM_SLEEP */ |
| 1215 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1216 | static int ci_runtime_suspend(struct device *dev) |
| 1217 | { |
| 1218 | struct ci_hdrc *ci = dev_get_drvdata(dev); |
| 1219 | |
| 1220 | dev_dbg(dev, "at %s\n", __func__); |
| 1221 | |
| 1222 | if (ci->in_lpm) { |
| 1223 | WARN_ON(1); |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
Li Jun | 961ea49 | 2015-02-11 12:45:03 +0800 | [diff] [blame] | 1227 | if (ci_otg_is_fsm_mode(ci)) |
| 1228 | ci_otg_fsm_suspend_for_srp(ci); |
| 1229 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1230 | usb_phy_set_wakeup(ci->usb_phy, true); |
| 1231 | ci_controller_suspend(ci); |
| 1232 | |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | static int ci_runtime_resume(struct device *dev) |
| 1237 | { |
| 1238 | return ci_controller_resume(dev); |
| 1239 | } |
| 1240 | |
| 1241 | #endif /* CONFIG_PM */ |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1242 | static const struct dev_pm_ops ci_pm_ops = { |
| 1243 | SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume) |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1244 | SET_RUNTIME_PM_OPS(ci_runtime_suspend, ci_runtime_resume, NULL) |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1245 | }; |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1246 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1247 | static struct platform_driver ci_hdrc_driver = { |
| 1248 | .probe = ci_hdrc_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 1249 | .remove = ci_hdrc_remove, |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1250 | .driver = { |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1251 | .name = "ci_hdrc", |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1252 | .pm = &ci_pm_ops, |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1253 | }, |
| 1254 | }; |
| 1255 | |
Peter Chen | 2f01a33 | 2015-07-21 09:51:29 +0800 | [diff] [blame] | 1256 | static int __init ci_hdrc_platform_register(void) |
| 1257 | { |
| 1258 | ci_hdrc_host_driver_init(); |
| 1259 | return platform_driver_register(&ci_hdrc_driver); |
| 1260 | } |
| 1261 | module_init(ci_hdrc_platform_register); |
| 1262 | |
| 1263 | static void __exit ci_hdrc_platform_unregister(void) |
| 1264 | { |
| 1265 | platform_driver_unregister(&ci_hdrc_driver); |
| 1266 | } |
| 1267 | module_exit(ci_hdrc_platform_unregister); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1268 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1269 | MODULE_ALIAS("platform:ci_hdrc"); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1270 | MODULE_LICENSE("GPL v2"); |
| 1271 | MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>"); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1272 | MODULE_DESCRIPTION("ChipIdea HDRC Driver"); |