Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Renesas USB driver |
| 3 | * |
| 4 | * Copyright (C) 2011 Renesas Solutions Corp. |
| 5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | * |
| 16 | */ |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/pm_runtime.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/sysfs.h> |
| 22 | #include "./common.h" |
| 23 | |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 24 | /* |
| 25 | * image of renesas_usbhs |
| 26 | * |
| 27 | * ex) gadget case |
| 28 | |
| 29 | * mod.c |
| 30 | * mod_gadget.c |
| 31 | * mod_host.c pipe.c fifo.c |
| 32 | * |
| 33 | * +-------+ +-----------+ |
| 34 | * | pipe0 |------>| fifo pio | |
| 35 | * +------------+ +-------+ +-----------+ |
| 36 | * | mod_gadget |=====> | pipe1 |--+ |
| 37 | * +------------+ +-------+ | +-----------+ |
| 38 | * | pipe2 | | +-| fifo dma0 | |
| 39 | * +------------+ +-------+ | | +-----------+ |
| 40 | * | mod_host | | pipe3 |<-|--+ |
| 41 | * +------------+ +-------+ | +-----------+ |
| 42 | * | .... | +--->| fifo dma1 | |
| 43 | * | .... | +-----------+ |
| 44 | */ |
| 45 | |
| 46 | |
Kuninori Morimoto | b002ff6 | 2011-04-28 16:41:20 +0900 | [diff] [blame] | 47 | #define USBHSF_RUNTIME_PWCTRL (1 << 0) |
| 48 | |
| 49 | /* status */ |
| 50 | #define usbhsc_flags_init(p) do {(p)->flags = 0; } while (0) |
| 51 | #define usbhsc_flags_set(p, b) ((p)->flags |= (b)) |
| 52 | #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b)) |
| 53 | #define usbhsc_flags_has(p, b) ((p)->flags & (b)) |
| 54 | |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 55 | /* |
| 56 | * platform call back |
| 57 | * |
| 58 | * renesas usb support platform callback function. |
| 59 | * Below macro call it. |
| 60 | * if platform doesn't have callback, it return 0 (no error) |
| 61 | */ |
| 62 | #define usbhs_platform_call(priv, func, args...)\ |
| 63 | (!(priv) ? -ENODEV : \ |
Kuninori Morimoto | 4829820 | 2011-10-12 21:02:22 -0700 | [diff] [blame] | 64 | !((priv)->pfunc.func) ? 0 : \ |
| 65 | (priv)->pfunc.func(args)) |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * common functions |
| 69 | */ |
| 70 | u16 usbhs_read(struct usbhs_priv *priv, u32 reg) |
| 71 | { |
| 72 | return ioread16(priv->base + reg); |
| 73 | } |
| 74 | |
| 75 | void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data) |
| 76 | { |
| 77 | iowrite16(data, priv->base + reg); |
| 78 | } |
| 79 | |
| 80 | void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data) |
| 81 | { |
| 82 | u16 val = usbhs_read(priv, reg); |
| 83 | |
| 84 | val &= ~mask; |
| 85 | val |= data & mask; |
| 86 | |
| 87 | usbhs_write(priv, reg, val); |
| 88 | } |
| 89 | |
Kuninori Morimoto | 206dcc2 | 2011-04-28 16:40:54 +0900 | [diff] [blame] | 90 | struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev) |
| 91 | { |
| 92 | return dev_get_drvdata(&pdev->dev); |
| 93 | } |
| 94 | |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 95 | /* |
| 96 | * syscfg functions |
| 97 | */ |
Kuninori Morimoto | 7619015 | 2011-10-23 19:56:41 -0700 | [diff] [blame] | 98 | static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable) |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 99 | { |
| 100 | usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0); |
| 101 | } |
| 102 | |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 103 | void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable) |
| 104 | { |
Kuninori Morimoto | 3244a7b | 2011-10-23 19:56:30 -0700 | [diff] [blame] | 105 | u16 mask = DCFM | DRPD | DPRPU | HSE | USBE; |
| 106 | u16 val = DCFM | DRPD | HSE | USBE; |
Kuninori Morimoto | f427eb6 | 2011-10-10 22:06:12 -0700 | [diff] [blame] | 107 | int has_otg = usbhs_get_dparam(priv, has_otg); |
| 108 | |
| 109 | if (has_otg) |
| 110 | usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN)); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * if enable |
| 114 | * |
| 115 | * - select Host mode |
| 116 | * - D+ Line/D- Line Pull-down |
| 117 | */ |
| 118 | usbhs_bset(priv, SYSCFG, mask, enable ? val : 0); |
| 119 | } |
| 120 | |
| 121 | void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable) |
| 122 | { |
Kuninori Morimoto | 3244a7b | 2011-10-23 19:56:30 -0700 | [diff] [blame] | 123 | u16 mask = DCFM | DRPD | DPRPU | HSE | USBE; |
| 124 | u16 val = DPRPU | HSE | USBE; |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * if enable |
| 128 | * |
| 129 | * - select Function mode |
| 130 | * - D+ Line Pull-up |
| 131 | */ |
| 132 | usbhs_bset(priv, SYSCFG, mask, enable ? val : 0); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * frame functions |
| 137 | */ |
| 138 | int usbhs_frame_get_num(struct usbhs_priv *priv) |
| 139 | { |
| 140 | return usbhs_read(priv, FRMNUM) & FRNM_MASK; |
| 141 | } |
| 142 | |
| 143 | /* |
Kuninori Morimoto | ef8bedb | 2011-10-10 22:02:33 -0700 | [diff] [blame] | 144 | * usb request functions |
| 145 | */ |
| 146 | void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req) |
| 147 | { |
| 148 | u16 val; |
| 149 | |
| 150 | val = usbhs_read(priv, USBREQ); |
| 151 | req->bRequest = (val >> 8) & 0xFF; |
| 152 | req->bRequestType = (val >> 0) & 0xFF; |
| 153 | |
| 154 | req->wValue = usbhs_read(priv, USBVAL); |
| 155 | req->wIndex = usbhs_read(priv, USBINDX); |
| 156 | req->wLength = usbhs_read(priv, USBLENG); |
| 157 | } |
| 158 | |
| 159 | void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req) |
| 160 | { |
| 161 | usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType); |
| 162 | usbhs_write(priv, USBVAL, req->wValue); |
| 163 | usbhs_write(priv, USBINDX, req->wIndex); |
| 164 | usbhs_write(priv, USBLENG, req->wLength); |
| 165 | |
| 166 | usbhs_bset(priv, DCPCTR, SUREQ, SUREQ); |
| 167 | } |
| 168 | |
| 169 | /* |
Kuninori Morimoto | 258485d | 2011-10-10 22:01:40 -0700 | [diff] [blame] | 170 | * bus/vbus functions |
| 171 | */ |
| 172 | void usbhs_bus_send_sof_enable(struct usbhs_priv *priv) |
| 173 | { |
Kuninori Morimoto | a9be4a4 | 2011-10-10 22:06:35 -0700 | [diff] [blame] | 174 | u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT); |
| 175 | |
| 176 | if (status != USBRST) { |
| 177 | struct device *dev = usbhs_priv_to_dev(priv); |
| 178 | dev_err(dev, "usbhs should be reset\n"); |
| 179 | } |
| 180 | |
Kuninori Morimoto | 258485d | 2011-10-10 22:01:40 -0700 | [diff] [blame] | 181 | usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT); |
| 182 | } |
| 183 | |
| 184 | void usbhs_bus_send_reset(struct usbhs_priv *priv) |
| 185 | { |
| 186 | usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST); |
| 187 | } |
| 188 | |
Kuninori Morimoto | 75587f5 | 2011-10-10 22:01:51 -0700 | [diff] [blame] | 189 | int usbhs_bus_get_speed(struct usbhs_priv *priv) |
| 190 | { |
| 191 | u16 dvstctr = usbhs_read(priv, DVSTCTR); |
| 192 | |
| 193 | switch (RHST & dvstctr) { |
| 194 | case RHST_LOW_SPEED: |
| 195 | return USB_SPEED_LOW; |
| 196 | case RHST_FULL_SPEED: |
| 197 | return USB_SPEED_FULL; |
| 198 | case RHST_HIGH_SPEED: |
| 199 | return USB_SPEED_HIGH; |
| 200 | } |
| 201 | |
| 202 | return USB_SPEED_UNKNOWN; |
| 203 | } |
| 204 | |
Kuninori Morimoto | 258485d | 2011-10-10 22:01:40 -0700 | [diff] [blame] | 205 | int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable) |
| 206 | { |
| 207 | struct platform_device *pdev = usbhs_priv_to_pdev(priv); |
| 208 | |
| 209 | return usbhs_platform_call(priv, set_vbus, pdev, enable); |
| 210 | } |
| 211 | |
| 212 | static void usbhsc_bus_init(struct usbhs_priv *priv) |
| 213 | { |
| 214 | usbhs_write(priv, DVSTCTR, 0); |
| 215 | |
| 216 | usbhs_vbus_ctrl(priv, 0); |
| 217 | } |
| 218 | |
| 219 | /* |
Kuninori Morimoto | eb05191 | 2011-10-10 22:06:46 -0700 | [diff] [blame] | 220 | * device configuration |
| 221 | */ |
| 222 | int usbhs_set_device_speed(struct usbhs_priv *priv, int devnum, |
| 223 | u16 upphub, u16 hubport, u16 speed) |
| 224 | { |
| 225 | struct device *dev = usbhs_priv_to_dev(priv); |
| 226 | u16 usbspd = 0; |
| 227 | u32 reg = DEVADD0 + (2 * devnum); |
| 228 | |
| 229 | if (devnum > 10) { |
| 230 | dev_err(dev, "cannot set speed to unknown device %d\n", devnum); |
| 231 | return -EIO; |
| 232 | } |
| 233 | |
| 234 | if (upphub > 0xA) { |
| 235 | dev_err(dev, "unsupported hub number %d\n", upphub); |
| 236 | return -EIO; |
| 237 | } |
| 238 | |
| 239 | switch (speed) { |
| 240 | case USB_SPEED_LOW: |
| 241 | usbspd = USBSPD_SPEED_LOW; |
| 242 | break; |
| 243 | case USB_SPEED_FULL: |
| 244 | usbspd = USBSPD_SPEED_FULL; |
| 245 | break; |
| 246 | case USB_SPEED_HIGH: |
| 247 | usbspd = USBSPD_SPEED_HIGH; |
| 248 | break; |
| 249 | default: |
| 250 | dev_err(dev, "unsupported speed %d\n", speed); |
| 251 | return -EIO; |
| 252 | } |
| 253 | |
| 254 | usbhs_write(priv, reg, UPPHUB(upphub) | |
| 255 | HUBPORT(hubport)| |
| 256 | USBSPD(usbspd)); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /* |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 262 | * local functions |
| 263 | */ |
Kuninori Morimoto | 11935de | 2011-10-10 22:01:28 -0700 | [diff] [blame] | 264 | static void usbhsc_set_buswait(struct usbhs_priv *priv) |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 265 | { |
| 266 | int wait = usbhs_get_dparam(priv, buswait_bwait); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 267 | |
Kuninori Morimoto | 11935de | 2011-10-10 22:01:28 -0700 | [diff] [blame] | 268 | /* set bus wait if platform have */ |
| 269 | if (wait) |
| 270 | usbhs_bset(priv, BUSWAIT, 0x000F, wait); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
| 274 | * platform default param |
| 275 | */ |
| 276 | static u32 usbhsc_default_pipe_type[] = { |
| 277 | USB_ENDPOINT_XFER_CONTROL, |
| 278 | USB_ENDPOINT_XFER_ISOC, |
| 279 | USB_ENDPOINT_XFER_ISOC, |
| 280 | USB_ENDPOINT_XFER_BULK, |
| 281 | USB_ENDPOINT_XFER_BULK, |
| 282 | USB_ENDPOINT_XFER_BULK, |
| 283 | USB_ENDPOINT_XFER_INT, |
| 284 | USB_ENDPOINT_XFER_INT, |
| 285 | USB_ENDPOINT_XFER_INT, |
| 286 | USB_ENDPOINT_XFER_INT, |
| 287 | }; |
| 288 | |
| 289 | /* |
Kuninori Morimoto | 6e267da | 2011-04-28 16:41:02 +0900 | [diff] [blame] | 290 | * power control |
| 291 | */ |
| 292 | static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable) |
| 293 | { |
Kuninori Morimoto | f1ee56a | 2011-10-23 19:57:10 -0700 | [diff] [blame^] | 294 | struct platform_device *pdev = usbhs_priv_to_pdev(priv); |
Kuninori Morimoto | 6e267da | 2011-04-28 16:41:02 +0900 | [diff] [blame] | 295 | struct device *dev = usbhs_priv_to_dev(priv); |
| 296 | |
| 297 | if (enable) { |
| 298 | /* enable PM */ |
| 299 | pm_runtime_get_sync(dev); |
| 300 | |
Kuninori Morimoto | f1ee56a | 2011-10-23 19:57:10 -0700 | [diff] [blame^] | 301 | /* enable platform power */ |
| 302 | usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable); |
| 303 | |
Kuninori Morimoto | 6e267da | 2011-04-28 16:41:02 +0900 | [diff] [blame] | 304 | /* USB on */ |
| 305 | usbhs_sys_clock_ctrl(priv, enable); |
Kuninori Morimoto | 6e267da | 2011-04-28 16:41:02 +0900 | [diff] [blame] | 306 | } else { |
| 307 | /* USB off */ |
Kuninori Morimoto | 6e267da | 2011-04-28 16:41:02 +0900 | [diff] [blame] | 308 | usbhs_sys_clock_ctrl(priv, enable); |
| 309 | |
Kuninori Morimoto | f1ee56a | 2011-10-23 19:57:10 -0700 | [diff] [blame^] | 310 | /* disable platform power */ |
| 311 | usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable); |
| 312 | |
Kuninori Morimoto | 6e267da | 2011-04-28 16:41:02 +0900 | [diff] [blame] | 313 | /* disable PM */ |
| 314 | pm_runtime_put_sync(dev); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* |
Kuninori Morimoto | ca8a282 | 2011-10-10 21:58:19 -0700 | [diff] [blame] | 319 | * hotplug |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 320 | */ |
Kuninori Morimoto | ca8a282 | 2011-10-10 21:58:19 -0700 | [diff] [blame] | 321 | static void usbhsc_hotplug(struct usbhs_priv *priv) |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 322 | { |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 323 | struct platform_device *pdev = usbhs_priv_to_pdev(priv); |
| 324 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 325 | int id; |
| 326 | int enable; |
| 327 | int ret; |
| 328 | |
| 329 | /* |
| 330 | * get vbus status from platform |
| 331 | */ |
| 332 | enable = usbhs_platform_call(priv, get_vbus, pdev); |
| 333 | |
| 334 | /* |
| 335 | * get id from platform |
| 336 | */ |
| 337 | id = usbhs_platform_call(priv, get_id, pdev); |
| 338 | |
| 339 | if (enable && !mod) { |
| 340 | ret = usbhs_mod_change(priv, id); |
| 341 | if (ret < 0) |
| 342 | return; |
| 343 | |
| 344 | dev_dbg(&pdev->dev, "%s enable\n", __func__); |
| 345 | |
Kuninori Morimoto | 6e267da | 2011-04-28 16:41:02 +0900 | [diff] [blame] | 346 | /* power on */ |
Kuninori Morimoto | b002ff6 | 2011-04-28 16:41:20 +0900 | [diff] [blame] | 347 | if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) |
| 348 | usbhsc_power_ctrl(priv, enable); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 349 | |
Kuninori Morimoto | 258485d | 2011-10-10 22:01:40 -0700 | [diff] [blame] | 350 | /* bus init */ |
| 351 | usbhsc_set_buswait(priv); |
| 352 | usbhsc_bus_init(priv); |
| 353 | |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 354 | /* module start */ |
| 355 | usbhs_mod_call(priv, start, priv); |
| 356 | |
| 357 | } else if (!enable && mod) { |
| 358 | dev_dbg(&pdev->dev, "%s disable\n", __func__); |
| 359 | |
| 360 | /* module stop */ |
| 361 | usbhs_mod_call(priv, stop, priv); |
| 362 | |
Kuninori Morimoto | 258485d | 2011-10-10 22:01:40 -0700 | [diff] [blame] | 363 | /* bus init */ |
| 364 | usbhsc_bus_init(priv); |
| 365 | |
Kuninori Morimoto | 6e267da | 2011-04-28 16:41:02 +0900 | [diff] [blame] | 366 | /* power off */ |
Kuninori Morimoto | b002ff6 | 2011-04-28 16:41:20 +0900 | [diff] [blame] | 367 | if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) |
| 368 | usbhsc_power_ctrl(priv, enable); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 369 | |
| 370 | usbhs_mod_change(priv, -1); |
| 371 | |
| 372 | /* reset phy for next connection */ |
| 373 | usbhs_platform_call(priv, phy_reset, pdev); |
| 374 | } |
| 375 | } |
| 376 | |
Kuninori Morimoto | ca8a282 | 2011-10-10 21:58:19 -0700 | [diff] [blame] | 377 | /* |
| 378 | * notify hotplug |
| 379 | */ |
| 380 | static void usbhsc_notify_hotplug(struct work_struct *work) |
| 381 | { |
| 382 | struct usbhs_priv *priv = container_of(work, |
| 383 | struct usbhs_priv, |
| 384 | notify_hotplug_work.work); |
| 385 | usbhsc_hotplug(priv); |
| 386 | } |
| 387 | |
Kuninori Morimoto | bc57381 | 2011-04-28 16:41:14 +0900 | [diff] [blame] | 388 | int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev) |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 389 | { |
Kuninori Morimoto | 206dcc2 | 2011-04-28 16:40:54 +0900 | [diff] [blame] | 390 | struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); |
Kuninori Morimoto | bc57381 | 2011-04-28 16:41:14 +0900 | [diff] [blame] | 391 | int delay = usbhs_get_dparam(priv, detection_delay); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 392 | |
| 393 | /* |
| 394 | * This functions will be called in interrupt. |
| 395 | * To make sure safety context, |
| 396 | * use workqueue for usbhs_notify_hotplug |
| 397 | */ |
Kuninori Morimoto | a49a88f | 2011-10-23 19:57:02 -0700 | [diff] [blame] | 398 | schedule_delayed_work(&priv->notify_hotplug_work, |
| 399 | msecs_to_jiffies(delay)); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * platform functions |
| 405 | */ |
Kuninori Morimoto | b7a8d17 | 2011-10-26 19:33:49 -0700 | [diff] [blame] | 406 | static int usbhs_probe(struct platform_device *pdev) |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 407 | { |
| 408 | struct renesas_usbhs_platform_info *info = pdev->dev.platform_data; |
| 409 | struct renesas_usbhs_driver_callback *dfunc; |
| 410 | struct usbhs_priv *priv; |
| 411 | struct resource *res; |
| 412 | unsigned int irq; |
| 413 | int ret; |
| 414 | |
| 415 | /* check platform information */ |
| 416 | if (!info || |
Kuninori Morimoto | b002ff6 | 2011-04-28 16:41:20 +0900 | [diff] [blame] | 417 | !info->platform_callback.get_id) { |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 418 | dev_err(&pdev->dev, "no platform information\n"); |
| 419 | return -EINVAL; |
| 420 | } |
| 421 | |
| 422 | /* platform data */ |
| 423 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 424 | irq = platform_get_irq(pdev, 0); |
| 425 | if (!res || (int)irq <= 0) { |
| 426 | dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n"); |
| 427 | return -ENODEV; |
| 428 | } |
| 429 | |
| 430 | /* usb private data */ |
| 431 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 432 | if (!priv) { |
| 433 | dev_err(&pdev->dev, "Could not allocate priv\n"); |
| 434 | return -ENOMEM; |
| 435 | } |
| 436 | |
| 437 | priv->base = ioremap_nocache(res->start, resource_size(res)); |
| 438 | if (!priv->base) { |
| 439 | dev_err(&pdev->dev, "ioremap error.\n"); |
| 440 | ret = -ENOMEM; |
| 441 | goto probe_end_kfree; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * care platform info |
| 446 | */ |
Kuninori Morimoto | 4829820 | 2011-10-12 21:02:22 -0700 | [diff] [blame] | 447 | memcpy(&priv->pfunc, |
| 448 | &info->platform_callback, |
| 449 | sizeof(struct renesas_usbhs_platform_callback)); |
| 450 | memcpy(&priv->dparam, |
| 451 | &info->driver_param, |
| 452 | sizeof(struct renesas_usbhs_driver_param)); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 453 | |
| 454 | /* set driver callback functions for platform */ |
| 455 | dfunc = &info->driver_callback; |
| 456 | dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug; |
| 457 | |
| 458 | /* set default param if platform doesn't have */ |
Kuninori Morimoto | 4829820 | 2011-10-12 21:02:22 -0700 | [diff] [blame] | 459 | if (!priv->dparam.pipe_type) { |
| 460 | priv->dparam.pipe_type = usbhsc_default_pipe_type; |
| 461 | priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 462 | } |
Kuninori Morimoto | 4829820 | 2011-10-12 21:02:22 -0700 | [diff] [blame] | 463 | if (!priv->dparam.pio_dma_border) |
| 464 | priv->dparam.pio_dma_border = 64; /* 64byte */ |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 465 | |
Kuninori Morimoto | b002ff6 | 2011-04-28 16:41:20 +0900 | [diff] [blame] | 466 | /* FIXME */ |
| 467 | /* runtime power control ? */ |
Kuninori Morimoto | 4829820 | 2011-10-12 21:02:22 -0700 | [diff] [blame] | 468 | if (priv->pfunc.get_vbus) |
Kuninori Morimoto | b002ff6 | 2011-04-28 16:41:20 +0900 | [diff] [blame] | 469 | usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL); |
| 470 | |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 471 | /* |
| 472 | * priv settings |
| 473 | */ |
| 474 | priv->irq = irq; |
| 475 | priv->pdev = pdev; |
Kuninori Morimoto | bc57381 | 2011-04-28 16:41:14 +0900 | [diff] [blame] | 476 | INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 477 | spin_lock_init(usbhs_priv_to_lock(priv)); |
| 478 | |
| 479 | /* call pipe and module init */ |
| 480 | ret = usbhs_pipe_probe(priv); |
| 481 | if (ret < 0) |
Kuninori Morimoto | 97f9322 | 2011-05-11 16:00:15 +0900 | [diff] [blame] | 482 | goto probe_end_iounmap; |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 483 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 484 | ret = usbhs_fifo_probe(priv); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 485 | if (ret < 0) |
Kuninori Morimoto | 97f9322 | 2011-05-11 16:00:15 +0900 | [diff] [blame] | 486 | goto probe_end_pipe_exit; |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 487 | |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 488 | ret = usbhs_mod_probe(priv); |
| 489 | if (ret < 0) |
| 490 | goto probe_end_fifo_exit; |
| 491 | |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 492 | /* dev_set_drvdata should be called after usbhs_mod_init */ |
| 493 | dev_set_drvdata(&pdev->dev, priv); |
| 494 | |
| 495 | /* |
| 496 | * deviece reset here because |
| 497 | * USB device might be used in boot loader. |
| 498 | */ |
| 499 | usbhs_sys_clock_ctrl(priv, 0); |
| 500 | |
| 501 | /* |
| 502 | * platform call |
| 503 | * |
| 504 | * USB phy setup might depend on CPU/Board. |
| 505 | * If platform has its callback functions, |
| 506 | * call it here. |
| 507 | */ |
| 508 | ret = usbhs_platform_call(priv, hardware_init, pdev); |
| 509 | if (ret < 0) { |
| 510 | dev_err(&pdev->dev, "platform prove failed.\n"); |
Kuninori Morimoto | 97f9322 | 2011-05-11 16:00:15 +0900 | [diff] [blame] | 511 | goto probe_end_mod_exit; |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /* reset phy for connection */ |
| 515 | usbhs_platform_call(priv, phy_reset, pdev); |
| 516 | |
Kuninori Morimoto | b002ff6 | 2011-04-28 16:41:20 +0900 | [diff] [blame] | 517 | /* power control */ |
| 518 | pm_runtime_enable(&pdev->dev); |
| 519 | if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) { |
| 520 | usbhsc_power_ctrl(priv, 1); |
| 521 | usbhs_mod_autonomy_mode(priv); |
| 522 | } |
| 523 | |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 524 | /* |
| 525 | * manual call notify_hotplug for cold plug |
| 526 | */ |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 527 | ret = usbhsc_drvcllbck_notify_hotplug(pdev); |
| 528 | if (ret < 0) |
| 529 | goto probe_end_call_remove; |
| 530 | |
| 531 | dev_info(&pdev->dev, "probed\n"); |
| 532 | |
| 533 | return ret; |
| 534 | |
| 535 | probe_end_call_remove: |
| 536 | usbhs_platform_call(priv, hardware_exit, pdev); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 537 | probe_end_mod_exit: |
| 538 | usbhs_mod_remove(priv); |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 539 | probe_end_fifo_exit: |
| 540 | usbhs_fifo_remove(priv); |
Kuninori Morimoto | 97f9322 | 2011-05-11 16:00:15 +0900 | [diff] [blame] | 541 | probe_end_pipe_exit: |
| 542 | usbhs_pipe_remove(priv); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 543 | probe_end_iounmap: |
| 544 | iounmap(priv->base); |
| 545 | probe_end_kfree: |
| 546 | kfree(priv); |
| 547 | |
| 548 | dev_info(&pdev->dev, "probe failed\n"); |
| 549 | |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | static int __devexit usbhs_remove(struct platform_device *pdev) |
| 554 | { |
Kuninori Morimoto | 206dcc2 | 2011-04-28 16:40:54 +0900 | [diff] [blame] | 555 | struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); |
Kuninori Morimoto | af32fe5 | 2011-04-21 14:10:16 +0900 | [diff] [blame] | 556 | struct renesas_usbhs_platform_info *info = pdev->dev.platform_data; |
| 557 | struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback; |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 558 | |
| 559 | dev_dbg(&pdev->dev, "usb remove\n"); |
| 560 | |
Kuninori Morimoto | af32fe5 | 2011-04-21 14:10:16 +0900 | [diff] [blame] | 561 | dfunc->notify_hotplug = NULL; |
| 562 | |
Kuninori Morimoto | b002ff6 | 2011-04-28 16:41:20 +0900 | [diff] [blame] | 563 | /* power off */ |
| 564 | if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) |
| 565 | usbhsc_power_ctrl(priv, 0); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 566 | |
Kuninori Morimoto | b002ff6 | 2011-04-28 16:41:20 +0900 | [diff] [blame] | 567 | pm_runtime_disable(&pdev->dev); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 568 | |
| 569 | usbhs_platform_call(priv, hardware_exit, pdev); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 570 | usbhs_mod_remove(priv); |
Kuninori Morimoto | d3af90a | 2011-06-06 14:18:44 +0900 | [diff] [blame] | 571 | usbhs_fifo_remove(priv); |
Kuninori Morimoto | 97f9322 | 2011-05-11 16:00:15 +0900 | [diff] [blame] | 572 | usbhs_pipe_remove(priv); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 573 | iounmap(priv->base); |
| 574 | kfree(priv); |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
Kuninori Morimoto | ca8a282 | 2011-10-10 21:58:19 -0700 | [diff] [blame] | 579 | static int usbhsc_suspend(struct device *dev) |
| 580 | { |
| 581 | struct usbhs_priv *priv = dev_get_drvdata(dev); |
| 582 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 583 | |
| 584 | if (mod) { |
| 585 | usbhs_mod_call(priv, stop, priv); |
| 586 | usbhs_mod_change(priv, -1); |
| 587 | } |
| 588 | |
| 589 | if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) |
| 590 | usbhsc_power_ctrl(priv, 0); |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | static int usbhsc_resume(struct device *dev) |
| 596 | { |
| 597 | struct usbhs_priv *priv = dev_get_drvdata(dev); |
| 598 | struct platform_device *pdev = usbhs_priv_to_pdev(priv); |
| 599 | |
| 600 | usbhs_platform_call(priv, phy_reset, pdev); |
| 601 | |
| 602 | if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) |
| 603 | usbhsc_power_ctrl(priv, 1); |
| 604 | |
| 605 | usbhsc_hotplug(priv); |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static int usbhsc_runtime_nop(struct device *dev) |
| 611 | { |
| 612 | /* Runtime PM callback shared between ->runtime_suspend() |
| 613 | * and ->runtime_resume(). Simply returns success. |
| 614 | * |
| 615 | * This driver re-initializes all registers after |
| 616 | * pm_runtime_get_sync() anyway so there is no need |
| 617 | * to save and restore registers here. |
| 618 | */ |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static const struct dev_pm_ops usbhsc_pm_ops = { |
| 623 | .suspend = usbhsc_suspend, |
| 624 | .resume = usbhsc_resume, |
| 625 | .runtime_suspend = usbhsc_runtime_nop, |
| 626 | .runtime_resume = usbhsc_runtime_nop, |
| 627 | }; |
| 628 | |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 629 | static struct platform_driver renesas_usbhs_driver = { |
| 630 | .driver = { |
| 631 | .name = "renesas_usbhs", |
Kuninori Morimoto | ca8a282 | 2011-10-10 21:58:19 -0700 | [diff] [blame] | 632 | .pm = &usbhsc_pm_ops, |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 633 | }, |
| 634 | .probe = usbhs_probe, |
| 635 | .remove = __devexit_p(usbhs_remove), |
| 636 | }; |
| 637 | |
| 638 | static int __init usbhs_init(void) |
| 639 | { |
| 640 | return platform_driver_register(&renesas_usbhs_driver); |
| 641 | } |
| 642 | |
| 643 | static void __exit usbhs_exit(void) |
| 644 | { |
| 645 | platform_driver_unregister(&renesas_usbhs_driver); |
| 646 | } |
| 647 | |
| 648 | module_init(usbhs_init); |
| 649 | module_exit(usbhs_exit); |
| 650 | |
| 651 | MODULE_LICENSE("GPL"); |
| 652 | MODULE_DESCRIPTION("Renesas USB driver"); |
| 653 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |