Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1 | /** |
| 2 | * linux/drivers/usb/gadget/s3c-hsotg.c |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3 | * |
Anton Tikhomirov | dfbc6fa | 2011-04-21 17:06:43 +0900 | [diff] [blame] | 4 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com |
| 6 | * |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 7 | * Copyright 2008 Openmoko, Inc. |
| 8 | * Copyright 2008 Simtec Electronics |
| 9 | * Ben Dooks <ben@simtec.co.uk> |
| 10 | * http://armlinux.simtec.co.uk/ |
| 11 | * |
| 12 | * S3C USB2.0 High-speed / OtG driver |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License version 2 as |
| 16 | * published by the Free Software Foundation. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 17 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/dma-mapping.h> |
| 25 | #include <linux/debugfs.h> |
| 26 | #include <linux/seq_file.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Maurus Cuelenaere | e50bf38 | 2010-07-19 09:40:50 +0100 | [diff] [blame] | 30 | #include <linux/clk.h> |
Lukasz Majewski | fc9a731 | 2012-05-04 14:17:02 +0200 | [diff] [blame] | 31 | #include <linux/regulator/consumer.h> |
Tomasz Figa | c50f056c | 2013-06-25 17:38:23 +0200 | [diff] [blame] | 32 | #include <linux/of_platform.h> |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 33 | |
| 34 | #include <linux/usb/ch9.h> |
| 35 | #include <linux/usb/gadget.h> |
Praveen Paneri | b2e587d | 2012-11-14 15:57:16 +0530 | [diff] [blame] | 36 | #include <linux/usb/phy.h> |
Lukasz Majewski | 126625e | 2012-05-09 13:16:53 +0200 | [diff] [blame] | 37 | #include <linux/platform_data/s3c-hsotg.h> |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 38 | |
| 39 | #include <mach/map.h> |
| 40 | |
Lukasz Majewski | 127d42a | 2012-05-04 14:16:59 +0200 | [diff] [blame] | 41 | #include "s3c-hsotg.h" |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 42 | |
Lukasz Majewski | fc9a731 | 2012-05-04 14:17:02 +0200 | [diff] [blame] | 43 | static const char * const s3c_hsotg_supply_names[] = { |
| 44 | "vusb_d", /* digital USB supply, 1.2V */ |
| 45 | "vusb_a", /* analog USB supply, 1.1V */ |
| 46 | }; |
| 47 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 48 | /* |
| 49 | * EP0_MPS_LIMIT |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 50 | * |
| 51 | * Unfortunately there seems to be a limit of the amount of data that can |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 52 | * be transferred by IN transactions on EP0. This is either 127 bytes or 3 |
| 53 | * packets (which practically means 1 packet and 63 bytes of data) when the |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 54 | * MPS is set to 64. |
| 55 | * |
| 56 | * This means if we are wanting to move >127 bytes of data, we need to |
| 57 | * split the transactions up, but just doing one packet at a time does |
| 58 | * not work (this may be an implicit DATA0 PID on first packet of the |
| 59 | * transaction) and doing 2 packets is outside the controller's limits. |
| 60 | * |
| 61 | * If we try to lower the MPS size for EP0, then no transfers work properly |
| 62 | * for EP0, and the system will fail basic enumeration. As no cause for this |
| 63 | * has currently been found, we cannot support any large IN transfers for |
| 64 | * EP0. |
| 65 | */ |
| 66 | #define EP0_MPS_LIMIT 64 |
| 67 | |
| 68 | struct s3c_hsotg; |
| 69 | struct s3c_hsotg_req; |
| 70 | |
| 71 | /** |
| 72 | * struct s3c_hsotg_ep - driver endpoint definition. |
| 73 | * @ep: The gadget layer representation of the endpoint. |
| 74 | * @name: The driver generated name for the endpoint. |
| 75 | * @queue: Queue of requests for this endpoint. |
| 76 | * @parent: Reference back to the parent device structure. |
| 77 | * @req: The current request that the endpoint is processing. This is |
| 78 | * used to indicate an request has been loaded onto the endpoint |
| 79 | * and has yet to be completed (maybe due to data move, or simply |
| 80 | * awaiting an ack from the core all the data has been completed). |
| 81 | * @debugfs: File entry for debugfs file for this endpoint. |
| 82 | * @lock: State lock to protect contents of endpoint. |
| 83 | * @dir_in: Set to true if this endpoint is of the IN direction, which |
| 84 | * means that it is sending data to the Host. |
| 85 | * @index: The index for the endpoint registers. |
| 86 | * @name: The name array passed to the USB core. |
| 87 | * @halted: Set if the endpoint has been halted. |
| 88 | * @periodic: Set if this is a periodic ep, such as Interrupt |
| 89 | * @sent_zlp: Set if we've sent a zero-length packet. |
| 90 | * @total_data: The total number of data bytes done. |
| 91 | * @fifo_size: The size of the FIFO (for periodic IN endpoints) |
| 92 | * @fifo_load: The amount of data loaded into the FIFO (periodic IN) |
| 93 | * @last_load: The offset of data for the last start of request. |
| 94 | * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN |
| 95 | * |
| 96 | * This is the driver's state for each registered enpoint, allowing it |
| 97 | * to keep track of transactions that need doing. Each endpoint has a |
| 98 | * lock to protect the state, to try and avoid using an overall lock |
| 99 | * for the host controller as much as possible. |
| 100 | * |
| 101 | * For periodic IN endpoints, we have fifo_size and fifo_load to try |
| 102 | * and keep track of the amount of data in the periodic FIFO for each |
| 103 | * of these as we don't have a status register that tells us how much |
Ben Dooks | e7a9ff5 | 2010-07-19 09:40:42 +0100 | [diff] [blame] | 104 | * is in each of them. (note, this may actually be useless information |
| 105 | * as in shared-fifo mode periodic in acts like a single-frame packet |
| 106 | * buffer than a fifo) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 107 | */ |
| 108 | struct s3c_hsotg_ep { |
| 109 | struct usb_ep ep; |
| 110 | struct list_head queue; |
| 111 | struct s3c_hsotg *parent; |
| 112 | struct s3c_hsotg_req *req; |
| 113 | struct dentry *debugfs; |
| 114 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 115 | |
| 116 | unsigned long total_data; |
| 117 | unsigned int size_loaded; |
| 118 | unsigned int last_load; |
| 119 | unsigned int fifo_load; |
| 120 | unsigned short fifo_size; |
| 121 | |
| 122 | unsigned char dir_in; |
| 123 | unsigned char index; |
| 124 | |
| 125 | unsigned int halted:1; |
| 126 | unsigned int periodic:1; |
| 127 | unsigned int sent_zlp:1; |
| 128 | |
| 129 | char name[10]; |
| 130 | }; |
| 131 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 132 | /** |
| 133 | * struct s3c_hsotg - driver state. |
| 134 | * @dev: The parent device supplied to the probe function |
| 135 | * @driver: USB gadget driver |
Praveen Paneri | b2e587d | 2012-11-14 15:57:16 +0530 | [diff] [blame] | 136 | * @phy: The otg phy transceiver structure for phy control. |
| 137 | * @plat: The platform specific configuration data. This can be removed once |
| 138 | * all SoCs support usb transceiver. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 139 | * @regs: The memory area mapped for accessing registers. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 140 | * @irq: The IRQ number we are using |
Lukasz Majewski | fc9a731 | 2012-05-04 14:17:02 +0200 | [diff] [blame] | 141 | * @supplies: Definition of USB power supplies |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 142 | * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos. |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 143 | * @num_of_eps: Number of available EPs (excluding EP0) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 144 | * @debug_root: root directrory for debugfs. |
| 145 | * @debug_file: main status file for debugfs. |
| 146 | * @debug_fifo: FIFO status file for debugfs. |
| 147 | * @ep0_reply: Request used for ep0 reply. |
| 148 | * @ep0_buff: Buffer for EP0 reply data, if needed. |
| 149 | * @ctrl_buff: Buffer for EP0 control requests. |
| 150 | * @ctrl_req: Request for EP0 control packets. |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 151 | * @setup: NAK management for EP0 SETUP |
Lukasz Majewski | 12a1f4d | 2012-05-04 14:17:08 +0200 | [diff] [blame] | 152 | * @last_rst: Time of last reset |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 153 | * @eps: The endpoints being supplied to the gadget framework |
| 154 | */ |
| 155 | struct s3c_hsotg { |
| 156 | struct device *dev; |
| 157 | struct usb_gadget_driver *driver; |
Praveen Paneri | b2e587d | 2012-11-14 15:57:16 +0530 | [diff] [blame] | 158 | struct usb_phy *phy; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 159 | struct s3c_hsotg_plat *plat; |
| 160 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 161 | spinlock_t lock; |
| 162 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 163 | void __iomem *regs; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 164 | int irq; |
Marek Szyprowski | 31ee04d | 2010-07-19 16:01:42 +0200 | [diff] [blame] | 165 | struct clk *clk; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 166 | |
Lukasz Majewski | fc9a731 | 2012-05-04 14:17:02 +0200 | [diff] [blame] | 167 | struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsotg_supply_names)]; |
| 168 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 169 | unsigned int dedicated_fifos:1; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 170 | unsigned char num_of_eps; |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 171 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 172 | struct dentry *debug_root; |
| 173 | struct dentry *debug_file; |
| 174 | struct dentry *debug_fifo; |
| 175 | |
| 176 | struct usb_request *ep0_reply; |
| 177 | struct usb_request *ctrl_req; |
| 178 | u8 ep0_buff[8]; |
| 179 | u8 ctrl_buff[8]; |
| 180 | |
| 181 | struct usb_gadget gadget; |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 182 | unsigned int setup; |
Lukasz Majewski | 12a1f4d | 2012-05-04 14:17:08 +0200 | [diff] [blame] | 183 | unsigned long last_rst; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 184 | struct s3c_hsotg_ep *eps; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | /** |
| 188 | * struct s3c_hsotg_req - data transfer request |
| 189 | * @req: The USB gadget request |
| 190 | * @queue: The list of requests for the endpoint this is queued for. |
| 191 | * @in_progress: Has already had size/packets written to core |
| 192 | * @mapped: DMA buffer for this request has been mapped via dma_map_single(). |
| 193 | */ |
| 194 | struct s3c_hsotg_req { |
| 195 | struct usb_request req; |
| 196 | struct list_head queue; |
| 197 | unsigned char in_progress; |
| 198 | unsigned char mapped; |
| 199 | }; |
| 200 | |
| 201 | /* conversion functions */ |
| 202 | static inline struct s3c_hsotg_req *our_req(struct usb_request *req) |
| 203 | { |
| 204 | return container_of(req, struct s3c_hsotg_req, req); |
| 205 | } |
| 206 | |
| 207 | static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep) |
| 208 | { |
| 209 | return container_of(ep, struct s3c_hsotg_ep, ep); |
| 210 | } |
| 211 | |
| 212 | static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget) |
| 213 | { |
| 214 | return container_of(gadget, struct s3c_hsotg, gadget); |
| 215 | } |
| 216 | |
| 217 | static inline void __orr32(void __iomem *ptr, u32 val) |
| 218 | { |
| 219 | writel(readl(ptr) | val, ptr); |
| 220 | } |
| 221 | |
| 222 | static inline void __bic32(void __iomem *ptr, u32 val) |
| 223 | { |
| 224 | writel(readl(ptr) & ~val, ptr); |
| 225 | } |
| 226 | |
| 227 | /* forward decleration of functions */ |
| 228 | static void s3c_hsotg_dump(struct s3c_hsotg *hsotg); |
| 229 | |
| 230 | /** |
| 231 | * using_dma - return the DMA status of the driver. |
| 232 | * @hsotg: The driver state. |
| 233 | * |
| 234 | * Return true if we're using DMA. |
| 235 | * |
| 236 | * Currently, we have the DMA support code worked into everywhere |
| 237 | * that needs it, but the AMBA DMA implementation in the hardware can |
| 238 | * only DMA from 32bit aligned addresses. This means that gadgets such |
| 239 | * as the CDC Ethernet cannot work as they often pass packets which are |
| 240 | * not 32bit aligned. |
| 241 | * |
| 242 | * Unfortunately the choice to use DMA or not is global to the controller |
| 243 | * and seems to be only settable when the controller is being put through |
| 244 | * a core reset. This means we either need to fix the gadgets to take |
| 245 | * account of DMA alignment, or add bounce buffers (yuerk). |
| 246 | * |
| 247 | * Until this issue is sorted out, we always return 'false'. |
| 248 | */ |
| 249 | static inline bool using_dma(struct s3c_hsotg *hsotg) |
| 250 | { |
| 251 | return false; /* support is not complete */ |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * s3c_hsotg_en_gsint - enable one or more of the general interrupt |
| 256 | * @hsotg: The device state |
| 257 | * @ints: A bitmask of the interrupts to enable |
| 258 | */ |
| 259 | static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints) |
| 260 | { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 261 | u32 gsintmsk = readl(hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 262 | u32 new_gsintmsk; |
| 263 | |
| 264 | new_gsintmsk = gsintmsk | ints; |
| 265 | |
| 266 | if (new_gsintmsk != gsintmsk) { |
| 267 | dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 268 | writel(new_gsintmsk, hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * s3c_hsotg_disable_gsint - disable one or more of the general interrupt |
| 274 | * @hsotg: The device state |
| 275 | * @ints: A bitmask of the interrupts to enable |
| 276 | */ |
| 277 | static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints) |
| 278 | { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 279 | u32 gsintmsk = readl(hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 280 | u32 new_gsintmsk; |
| 281 | |
| 282 | new_gsintmsk = gsintmsk & ~ints; |
| 283 | |
| 284 | if (new_gsintmsk != gsintmsk) |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 285 | writel(new_gsintmsk, hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq |
| 290 | * @hsotg: The device state |
| 291 | * @ep: The endpoint index |
| 292 | * @dir_in: True if direction is in. |
| 293 | * @en: The enable value, true to enable |
| 294 | * |
| 295 | * Set or clear the mask for an individual endpoint's interrupt |
| 296 | * request. |
| 297 | */ |
| 298 | static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg, |
| 299 | unsigned int ep, unsigned int dir_in, |
| 300 | unsigned int en) |
| 301 | { |
| 302 | unsigned long flags; |
| 303 | u32 bit = 1 << ep; |
| 304 | u32 daint; |
| 305 | |
| 306 | if (!dir_in) |
| 307 | bit <<= 16; |
| 308 | |
| 309 | local_irq_save(flags); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 310 | daint = readl(hsotg->regs + DAINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 311 | if (en) |
| 312 | daint |= bit; |
| 313 | else |
| 314 | daint &= ~bit; |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 315 | writel(daint, hsotg->regs + DAINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 316 | local_irq_restore(flags); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * s3c_hsotg_init_fifo - initialise non-periodic FIFOs |
| 321 | * @hsotg: The device instance. |
| 322 | */ |
| 323 | static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg) |
| 324 | { |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 325 | unsigned int ep; |
| 326 | unsigned int addr; |
| 327 | unsigned int size; |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 328 | int timeout; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 329 | u32 val; |
| 330 | |
Ben Dooks | 6d091ee | 2010-07-19 09:40:40 +0100 | [diff] [blame] | 331 | /* set FIFO sizes to 2048/1024 */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 332 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 333 | writel(2048, hsotg->regs + GRXFSIZ); |
| 334 | writel(GNPTXFSIZ_NPTxFStAddr(2048) | |
| 335 | GNPTXFSIZ_NPTxFDep(1024), |
| 336 | hsotg->regs + GNPTXFSIZ); |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 337 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 338 | /* |
| 339 | * arange all the rest of the TX FIFOs, as some versions of this |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 340 | * block have overlapping default addresses. This also ensures |
| 341 | * that if the settings have been changed, then they are set to |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 342 | * known values. |
| 343 | */ |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 344 | |
| 345 | /* start at the end of the GNPTXFSIZ, rounded up */ |
| 346 | addr = 2048 + 1024; |
| 347 | size = 768; |
| 348 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 349 | /* |
| 350 | * currently we allocate TX FIFOs for all possible endpoints, |
| 351 | * and assume that they are all the same size. |
| 352 | */ |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 353 | |
Anton Tikhomirov | f7a83fe | 2012-03-06 14:05:49 +0900 | [diff] [blame] | 354 | for (ep = 1; ep <= 15; ep++) { |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 355 | val = addr; |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 356 | val |= size << DPTXFSIZn_DPTxFSize_SHIFT; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 357 | addr += size; |
| 358 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 359 | writel(val, hsotg->regs + DPTXFSIZn(ep)); |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 360 | } |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 361 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 362 | /* |
| 363 | * according to p428 of the design guide, we need to ensure that |
| 364 | * all fifos are flushed before continuing |
| 365 | */ |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 366 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 367 | writel(GRSTCTL_TxFNum(0x10) | GRSTCTL_TxFFlsh | |
| 368 | GRSTCTL_RxFFlsh, hsotg->regs + GRSTCTL); |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 369 | |
| 370 | /* wait until the fifos are both flushed */ |
| 371 | timeout = 100; |
| 372 | while (1) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 373 | val = readl(hsotg->regs + GRSTCTL); |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 374 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 375 | if ((val & (GRSTCTL_TxFFlsh | GRSTCTL_RxFFlsh)) == 0) |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 376 | break; |
| 377 | |
| 378 | if (--timeout == 0) { |
| 379 | dev_err(hsotg->dev, |
| 380 | "%s: timeout flushing fifos (GRSTCTL=%08x)\n", |
| 381 | __func__, val); |
| 382 | } |
| 383 | |
| 384 | udelay(1); |
| 385 | } |
| 386 | |
| 387 | dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
| 391 | * @ep: USB endpoint to allocate request for. |
| 392 | * @flags: Allocation flags |
| 393 | * |
| 394 | * Allocate a new USB request structure appropriate for the specified endpoint |
| 395 | */ |
Mark Brown | 0978f8c | 2010-01-18 13:18:35 +0000 | [diff] [blame] | 396 | static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep, |
| 397 | gfp_t flags) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 398 | { |
| 399 | struct s3c_hsotg_req *req; |
| 400 | |
| 401 | req = kzalloc(sizeof(struct s3c_hsotg_req), flags); |
| 402 | if (!req) |
| 403 | return NULL; |
| 404 | |
| 405 | INIT_LIST_HEAD(&req->queue); |
| 406 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 407 | return &req->req; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * is_ep_periodic - return true if the endpoint is in periodic mode. |
| 412 | * @hs_ep: The endpoint to query. |
| 413 | * |
| 414 | * Returns true if the endpoint is in periodic mode, meaning it is being |
| 415 | * used for an Interrupt or ISO transfer. |
| 416 | */ |
| 417 | static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep) |
| 418 | { |
| 419 | return hs_ep->periodic; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request |
| 424 | * @hsotg: The device state. |
| 425 | * @hs_ep: The endpoint for the request |
| 426 | * @hs_req: The request being processed. |
| 427 | * |
| 428 | * This is the reverse of s3c_hsotg_map_dma(), called for the completion |
| 429 | * of a request to ensure the buffer is ready for access by the caller. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 430 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 431 | static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg, |
| 432 | struct s3c_hsotg_ep *hs_ep, |
| 433 | struct s3c_hsotg_req *hs_req) |
| 434 | { |
| 435 | struct usb_request *req = &hs_req->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 436 | |
| 437 | /* ignore this if we're not moving any data */ |
| 438 | if (hs_req->req.length == 0) |
| 439 | return; |
| 440 | |
Jingoo Han | 17d966a | 2013-05-11 21:14:00 +0900 | [diff] [blame] | 441 | usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /** |
| 445 | * s3c_hsotg_write_fifo - write packet Data to the TxFIFO |
| 446 | * @hsotg: The controller state. |
| 447 | * @hs_ep: The endpoint we're going to write for. |
| 448 | * @hs_req: The request to write data for. |
| 449 | * |
| 450 | * This is called when the TxFIFO has some space in it to hold a new |
| 451 | * transmission and we have something to give it. The actual setup of |
| 452 | * the data size is done elsewhere, so all we have to do is to actually |
| 453 | * write the data. |
| 454 | * |
| 455 | * The return value is zero if there is more space (or nothing was done) |
| 456 | * otherwise -ENOSPC is returned if the FIFO space was used up. |
| 457 | * |
| 458 | * This routine is only needed for PIO |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 459 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 460 | static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg, |
| 461 | struct s3c_hsotg_ep *hs_ep, |
| 462 | struct s3c_hsotg_req *hs_req) |
| 463 | { |
| 464 | bool periodic = is_ep_periodic(hs_ep); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 465 | u32 gnptxsts = readl(hsotg->regs + GNPTXSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 466 | int buf_pos = hs_req->req.actual; |
| 467 | int to_write = hs_ep->size_loaded; |
| 468 | void *data; |
| 469 | int can_write; |
| 470 | int pkt_round; |
| 471 | |
| 472 | to_write -= (buf_pos - hs_ep->last_load); |
| 473 | |
| 474 | /* if there's nothing to write, get out early */ |
| 475 | if (to_write == 0) |
| 476 | return 0; |
| 477 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 478 | if (periodic && !hsotg->dedicated_fifos) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 479 | u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 480 | int size_left; |
| 481 | int size_done; |
| 482 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 483 | /* |
| 484 | * work out how much data was loaded so we can calculate |
| 485 | * how much data is left in the fifo. |
| 486 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 487 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 488 | size_left = DxEPTSIZ_XferSize_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 489 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 490 | /* |
| 491 | * if shared fifo, we cannot write anything until the |
Ben Dooks | e7a9ff5 | 2010-07-19 09:40:42 +0100 | [diff] [blame] | 492 | * previous data has been completely sent. |
| 493 | */ |
| 494 | if (hs_ep->fifo_load != 0) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 495 | s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp); |
Ben Dooks | e7a9ff5 | 2010-07-19 09:40:42 +0100 | [diff] [blame] | 496 | return -ENOSPC; |
| 497 | } |
| 498 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 499 | dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n", |
| 500 | __func__, size_left, |
| 501 | hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size); |
| 502 | |
| 503 | /* how much of the data has moved */ |
| 504 | size_done = hs_ep->size_loaded - size_left; |
| 505 | |
| 506 | /* how much data is left in the fifo */ |
| 507 | can_write = hs_ep->fifo_load - size_done; |
| 508 | dev_dbg(hsotg->dev, "%s: => can_write1=%d\n", |
| 509 | __func__, can_write); |
| 510 | |
| 511 | can_write = hs_ep->fifo_size - can_write; |
| 512 | dev_dbg(hsotg->dev, "%s: => can_write2=%d\n", |
| 513 | __func__, can_write); |
| 514 | |
| 515 | if (can_write <= 0) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 516 | s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 517 | return -ENOSPC; |
| 518 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 519 | } else if (hsotg->dedicated_fifos && hs_ep->index != 0) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 520 | can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index)); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 521 | |
| 522 | can_write &= 0xffff; |
| 523 | can_write *= 4; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 524 | } else { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 525 | if (GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 526 | dev_dbg(hsotg->dev, |
| 527 | "%s: no queue slots available (0x%08x)\n", |
| 528 | __func__, gnptxsts); |
| 529 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 530 | s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTxFEmp); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 531 | return -ENOSPC; |
| 532 | } |
| 533 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 534 | can_write = GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts); |
Ben Dooks | 679f9b7 | 2010-07-19 09:40:41 +0100 | [diff] [blame] | 535 | can_write *= 4; /* fifo size is in 32bit quantities. */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, mps %d\n", |
| 539 | __func__, gnptxsts, can_write, to_write, hs_ep->ep.maxpacket); |
| 540 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 541 | /* |
| 542 | * limit to 512 bytes of data, it seems at least on the non-periodic |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 543 | * FIFO, requests of >512 cause the endpoint to get stuck with a |
| 544 | * fragment of the end of the transfer in it. |
| 545 | */ |
| 546 | if (can_write > 512) |
| 547 | can_write = 512; |
| 548 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 549 | /* |
| 550 | * limit the write to one max-packet size worth of data, but allow |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 551 | * the transfer to return that it did not run out of fifo space |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 552 | * doing it. |
| 553 | */ |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 554 | if (to_write > hs_ep->ep.maxpacket) { |
| 555 | to_write = hs_ep->ep.maxpacket; |
| 556 | |
| 557 | s3c_hsotg_en_gsint(hsotg, |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 558 | periodic ? GINTSTS_PTxFEmp : |
| 559 | GINTSTS_NPTxFEmp); |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 560 | } |
| 561 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 562 | /* see if we can write data */ |
| 563 | |
| 564 | if (to_write > can_write) { |
| 565 | to_write = can_write; |
| 566 | pkt_round = to_write % hs_ep->ep.maxpacket; |
| 567 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 568 | /* |
| 569 | * Round the write down to an |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 570 | * exact number of packets. |
| 571 | * |
| 572 | * Note, we do not currently check to see if we can ever |
| 573 | * write a full packet or not to the FIFO. |
| 574 | */ |
| 575 | |
| 576 | if (pkt_round) |
| 577 | to_write -= pkt_round; |
| 578 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 579 | /* |
| 580 | * enable correct FIFO interrupt to alert us when there |
| 581 | * is more room left. |
| 582 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 583 | |
| 584 | s3c_hsotg_en_gsint(hsotg, |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 585 | periodic ? GINTSTS_PTxFEmp : |
| 586 | GINTSTS_NPTxFEmp); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n", |
| 590 | to_write, hs_req->req.length, can_write, buf_pos); |
| 591 | |
| 592 | if (to_write <= 0) |
| 593 | return -ENOSPC; |
| 594 | |
| 595 | hs_req->req.actual = buf_pos + to_write; |
| 596 | hs_ep->total_data += to_write; |
| 597 | |
| 598 | if (periodic) |
| 599 | hs_ep->fifo_load += to_write; |
| 600 | |
| 601 | to_write = DIV_ROUND_UP(to_write, 4); |
| 602 | data = hs_req->req.buf + buf_pos; |
| 603 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 604 | writesl(hsotg->regs + EPFIFO(hs_ep->index), data, to_write); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 605 | |
| 606 | return (to_write >= can_write) ? -ENOSPC : 0; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * get_ep_limit - get the maximum data legnth for this endpoint |
| 611 | * @hs_ep: The endpoint |
| 612 | * |
| 613 | * Return the maximum data that can be queued in one go on a given endpoint |
| 614 | * so that transfers that are too long can be split. |
| 615 | */ |
| 616 | static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep) |
| 617 | { |
| 618 | int index = hs_ep->index; |
| 619 | unsigned maxsize; |
| 620 | unsigned maxpkt; |
| 621 | |
| 622 | if (index != 0) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 623 | maxsize = DxEPTSIZ_XferSize_LIMIT + 1; |
| 624 | maxpkt = DxEPTSIZ_PktCnt_LIMIT + 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 625 | } else { |
Ben Dooks | b05ca58 | 2010-07-19 09:40:48 +0100 | [diff] [blame] | 626 | maxsize = 64+64; |
Jingoo Han | 66e5c64 | 2011-05-13 21:26:15 +0900 | [diff] [blame] | 627 | if (hs_ep->dir_in) |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 628 | maxpkt = DIEPTSIZ0_PktCnt_LIMIT + 1; |
Jingoo Han | 66e5c64 | 2011-05-13 21:26:15 +0900 | [diff] [blame] | 629 | else |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 630 | maxpkt = 2; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* we made the constant loading easier above by using +1 */ |
| 634 | maxpkt--; |
| 635 | maxsize--; |
| 636 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 637 | /* |
| 638 | * constrain by packet count if maxpkts*pktsize is greater |
| 639 | * than the length register size. |
| 640 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 641 | |
| 642 | if ((maxpkt * hs_ep->ep.maxpacket) < maxsize) |
| 643 | maxsize = maxpkt * hs_ep->ep.maxpacket; |
| 644 | |
| 645 | return maxsize; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * s3c_hsotg_start_req - start a USB request from an endpoint's queue |
| 650 | * @hsotg: The controller state. |
| 651 | * @hs_ep: The endpoint to process a request for |
| 652 | * @hs_req: The request to start. |
| 653 | * @continuing: True if we are doing more for the current request. |
| 654 | * |
| 655 | * Start the given request running by setting the endpoint registers |
| 656 | * appropriately, and writing any data to the FIFOs. |
| 657 | */ |
| 658 | static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg, |
| 659 | struct s3c_hsotg_ep *hs_ep, |
| 660 | struct s3c_hsotg_req *hs_req, |
| 661 | bool continuing) |
| 662 | { |
| 663 | struct usb_request *ureq = &hs_req->req; |
| 664 | int index = hs_ep->index; |
| 665 | int dir_in = hs_ep->dir_in; |
| 666 | u32 epctrl_reg; |
| 667 | u32 epsize_reg; |
| 668 | u32 epsize; |
| 669 | u32 ctrl; |
| 670 | unsigned length; |
| 671 | unsigned packets; |
| 672 | unsigned maxreq; |
| 673 | |
| 674 | if (index != 0) { |
| 675 | if (hs_ep->req && !continuing) { |
| 676 | dev_err(hsotg->dev, "%s: active request\n", __func__); |
| 677 | WARN_ON(1); |
| 678 | return; |
| 679 | } else if (hs_ep->req != hs_req && continuing) { |
| 680 | dev_err(hsotg->dev, |
| 681 | "%s: continue different req\n", __func__); |
| 682 | WARN_ON(1); |
| 683 | return; |
| 684 | } |
| 685 | } |
| 686 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 687 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 688 | epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 689 | |
| 690 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n", |
| 691 | __func__, readl(hsotg->regs + epctrl_reg), index, |
| 692 | hs_ep->dir_in ? "in" : "out"); |
| 693 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 694 | /* If endpoint is stalled, we will restart request later */ |
| 695 | ctrl = readl(hsotg->regs + epctrl_reg); |
| 696 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 697 | if (ctrl & DxEPCTL_Stall) { |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 698 | dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index); |
| 699 | return; |
| 700 | } |
| 701 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 702 | length = ureq->length - ureq->actual; |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 703 | dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n", |
| 704 | ureq->length, ureq->actual); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 705 | if (0) |
| 706 | dev_dbg(hsotg->dev, |
| 707 | "REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n", |
| 708 | ureq->buf, length, ureq->dma, |
| 709 | ureq->no_interrupt, ureq->zero, ureq->short_not_ok); |
| 710 | |
| 711 | maxreq = get_ep_limit(hs_ep); |
| 712 | if (length > maxreq) { |
| 713 | int round = maxreq % hs_ep->ep.maxpacket; |
| 714 | |
| 715 | dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n", |
| 716 | __func__, length, maxreq, round); |
| 717 | |
| 718 | /* round down to multiple of packets */ |
| 719 | if (round) |
| 720 | maxreq -= round; |
| 721 | |
| 722 | length = maxreq; |
| 723 | } |
| 724 | |
| 725 | if (length) |
| 726 | packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket); |
| 727 | else |
| 728 | packets = 1; /* send one packet if length is zero. */ |
| 729 | |
| 730 | if (dir_in && index != 0) |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 731 | epsize = DxEPTSIZ_MC(1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 732 | else |
| 733 | epsize = 0; |
| 734 | |
| 735 | if (index != 0 && ureq->zero) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 736 | /* |
| 737 | * test for the packets being exactly right for the |
| 738 | * transfer |
| 739 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 740 | |
| 741 | if (length == (packets * hs_ep->ep.maxpacket)) |
| 742 | packets++; |
| 743 | } |
| 744 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 745 | epsize |= DxEPTSIZ_PktCnt(packets); |
| 746 | epsize |= DxEPTSIZ_XferSize(length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 747 | |
| 748 | dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n", |
| 749 | __func__, packets, length, ureq->length, epsize, epsize_reg); |
| 750 | |
| 751 | /* store the request as the current one we're doing */ |
| 752 | hs_ep->req = hs_req; |
| 753 | |
| 754 | /* write size / packets */ |
| 755 | writel(epsize, hsotg->regs + epsize_reg); |
| 756 | |
Anton Tikhomirov | db1d8ba | 2012-03-06 14:09:19 +0900 | [diff] [blame] | 757 | if (using_dma(hsotg) && !continuing) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 758 | unsigned int dma_reg; |
| 759 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 760 | /* |
| 761 | * write DMA address to control register, buffer already |
| 762 | * synced by s3c_hsotg_ep_queue(). |
| 763 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 764 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 765 | dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 766 | writel(ureq->dma, hsotg->regs + dma_reg); |
| 767 | |
| 768 | dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n", |
| 769 | __func__, ureq->dma, dma_reg); |
| 770 | } |
| 771 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 772 | ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */ |
| 773 | ctrl |= DxEPCTL_USBActEp; |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 774 | |
| 775 | dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup); |
| 776 | |
| 777 | /* For Setup request do not clear NAK */ |
| 778 | if (hsotg->setup && index == 0) |
| 779 | hsotg->setup = 0; |
| 780 | else |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 781 | ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */ |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 782 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 783 | |
| 784 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
| 785 | writel(ctrl, hsotg->regs + epctrl_reg); |
| 786 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 787 | /* |
| 788 | * set these, it seems that DMA support increments past the end |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 789 | * of the packet buffer so we need to calculate the length from |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 790 | * this information. |
| 791 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 792 | hs_ep->size_loaded = length; |
| 793 | hs_ep->last_load = ureq->actual; |
| 794 | |
| 795 | if (dir_in && !using_dma(hsotg)) { |
| 796 | /* set these anyway, we may need them for non-periodic in */ |
| 797 | hs_ep->fifo_load = 0; |
| 798 | |
| 799 | s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
| 800 | } |
| 801 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 802 | /* |
| 803 | * clear the INTknTXFEmpMsk when we start request, more as a aide |
| 804 | * to debugging to see what is going on. |
| 805 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 806 | if (dir_in) |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 807 | writel(DIEPMSK_INTknTXFEmpMsk, |
| 808 | hsotg->regs + DIEPINT(index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 809 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 810 | /* |
| 811 | * Note, trying to clear the NAK here causes problems with transmit |
| 812 | * on the S3C6400 ending up with the TXFIFO becoming full. |
| 813 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 814 | |
| 815 | /* check ep is enabled */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 816 | if (!(readl(hsotg->regs + epctrl_reg) & DxEPCTL_EPEna)) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 817 | dev_warn(hsotg->dev, |
| 818 | "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n", |
| 819 | index, readl(hsotg->regs + epctrl_reg)); |
| 820 | |
| 821 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", |
| 822 | __func__, readl(hsotg->regs + epctrl_reg)); |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * s3c_hsotg_map_dma - map the DMA memory being used for the request |
| 827 | * @hsotg: The device state. |
| 828 | * @hs_ep: The endpoint the request is on. |
| 829 | * @req: The request being processed. |
| 830 | * |
| 831 | * We've been asked to queue a request, so ensure that the memory buffer |
| 832 | * is correctly setup for DMA. If we've been passed an extant DMA address |
| 833 | * then ensure the buffer has been synced to memory. If our buffer has no |
| 834 | * DMA memory, then we map the memory and mark our request to allow us to |
| 835 | * cleanup on completion. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 836 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 837 | static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg, |
| 838 | struct s3c_hsotg_ep *hs_ep, |
| 839 | struct usb_request *req) |
| 840 | { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 841 | struct s3c_hsotg_req *hs_req = our_req(req); |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 842 | int ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 843 | |
| 844 | /* if the length is zero, ignore the DMA data */ |
| 845 | if (hs_req->req.length == 0) |
| 846 | return 0; |
| 847 | |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 848 | ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in); |
| 849 | if (ret) |
| 850 | goto dma_error; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 851 | |
| 852 | return 0; |
| 853 | |
| 854 | dma_error: |
| 855 | dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n", |
| 856 | __func__, req->buf, req->length); |
| 857 | |
| 858 | return -EIO; |
| 859 | } |
| 860 | |
| 861 | static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req, |
| 862 | gfp_t gfp_flags) |
| 863 | { |
| 864 | struct s3c_hsotg_req *hs_req = our_req(req); |
| 865 | struct s3c_hsotg_ep *hs_ep = our_ep(ep); |
| 866 | struct s3c_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 867 | bool first; |
| 868 | |
| 869 | dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n", |
| 870 | ep->name, req, req->length, req->buf, req->no_interrupt, |
| 871 | req->zero, req->short_not_ok); |
| 872 | |
| 873 | /* initialise status of the request */ |
| 874 | INIT_LIST_HEAD(&hs_req->queue); |
| 875 | req->actual = 0; |
| 876 | req->status = -EINPROGRESS; |
| 877 | |
| 878 | /* if we're using DMA, sync the buffers as necessary */ |
| 879 | if (using_dma(hs)) { |
| 880 | int ret = s3c_hsotg_map_dma(hs, hs_ep, req); |
| 881 | if (ret) |
| 882 | return ret; |
| 883 | } |
| 884 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 885 | first = list_empty(&hs_ep->queue); |
| 886 | list_add_tail(&hs_req->queue, &hs_ep->queue); |
| 887 | |
| 888 | if (first) |
| 889 | s3c_hsotg_start_req(hs, hs_ep, hs_req, false); |
| 890 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 891 | return 0; |
| 892 | } |
| 893 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 894 | static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req, |
| 895 | gfp_t gfp_flags) |
| 896 | { |
| 897 | struct s3c_hsotg_ep *hs_ep = our_ep(ep); |
| 898 | struct s3c_hsotg *hs = hs_ep->parent; |
| 899 | unsigned long flags = 0; |
| 900 | int ret = 0; |
| 901 | |
| 902 | spin_lock_irqsave(&hs->lock, flags); |
| 903 | ret = s3c_hsotg_ep_queue(ep, req, gfp_flags); |
| 904 | spin_unlock_irqrestore(&hs->lock, flags); |
| 905 | |
| 906 | return ret; |
| 907 | } |
| 908 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 909 | static void s3c_hsotg_ep_free_request(struct usb_ep *ep, |
| 910 | struct usb_request *req) |
| 911 | { |
| 912 | struct s3c_hsotg_req *hs_req = our_req(req); |
| 913 | |
| 914 | kfree(hs_req); |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * s3c_hsotg_complete_oursetup - setup completion callback |
| 919 | * @ep: The endpoint the request was on. |
| 920 | * @req: The request completed. |
| 921 | * |
| 922 | * Called on completion of any requests the driver itself |
| 923 | * submitted that need cleaning up. |
| 924 | */ |
| 925 | static void s3c_hsotg_complete_oursetup(struct usb_ep *ep, |
| 926 | struct usb_request *req) |
| 927 | { |
| 928 | struct s3c_hsotg_ep *hs_ep = our_ep(ep); |
| 929 | struct s3c_hsotg *hsotg = hs_ep->parent; |
| 930 | |
| 931 | dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req); |
| 932 | |
| 933 | s3c_hsotg_ep_free_request(ep, req); |
| 934 | } |
| 935 | |
| 936 | /** |
| 937 | * ep_from_windex - convert control wIndex value to endpoint |
| 938 | * @hsotg: The driver state. |
| 939 | * @windex: The control request wIndex field (in host order). |
| 940 | * |
| 941 | * Convert the given wIndex into a pointer to an driver endpoint |
| 942 | * structure, or return NULL if it is not a valid endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 943 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 944 | static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg, |
| 945 | u32 windex) |
| 946 | { |
| 947 | struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F]; |
| 948 | int dir = (windex & USB_DIR_IN) ? 1 : 0; |
| 949 | int idx = windex & 0x7F; |
| 950 | |
| 951 | if (windex >= 0x100) |
| 952 | return NULL; |
| 953 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 954 | if (idx > hsotg->num_of_eps) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 955 | return NULL; |
| 956 | |
| 957 | if (idx && ep->dir_in != dir) |
| 958 | return NULL; |
| 959 | |
| 960 | return ep; |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * s3c_hsotg_send_reply - send reply to control request |
| 965 | * @hsotg: The device state |
| 966 | * @ep: Endpoint 0 |
| 967 | * @buff: Buffer for request |
| 968 | * @length: Length of reply. |
| 969 | * |
| 970 | * Create a request and queue it on the given endpoint. This is useful as |
| 971 | * an internal method of sending replies to certain control requests, etc. |
| 972 | */ |
| 973 | static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg, |
| 974 | struct s3c_hsotg_ep *ep, |
| 975 | void *buff, |
| 976 | int length) |
| 977 | { |
| 978 | struct usb_request *req; |
| 979 | int ret; |
| 980 | |
| 981 | dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length); |
| 982 | |
| 983 | req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC); |
| 984 | hsotg->ep0_reply = req; |
| 985 | if (!req) { |
| 986 | dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__); |
| 987 | return -ENOMEM; |
| 988 | } |
| 989 | |
| 990 | req->buf = hsotg->ep0_buff; |
| 991 | req->length = length; |
| 992 | req->zero = 1; /* always do zero-length final transfer */ |
| 993 | req->complete = s3c_hsotg_complete_oursetup; |
| 994 | |
| 995 | if (length) |
| 996 | memcpy(req->buf, buff, length); |
| 997 | else |
| 998 | ep->sent_zlp = 1; |
| 999 | |
| 1000 | ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC); |
| 1001 | if (ret) { |
| 1002 | dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__); |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * s3c_hsotg_process_req_status - process request GET_STATUS |
| 1011 | * @hsotg: The device state |
| 1012 | * @ctrl: USB control request |
| 1013 | */ |
| 1014 | static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg, |
| 1015 | struct usb_ctrlrequest *ctrl) |
| 1016 | { |
| 1017 | struct s3c_hsotg_ep *ep0 = &hsotg->eps[0]; |
| 1018 | struct s3c_hsotg_ep *ep; |
| 1019 | __le16 reply; |
| 1020 | int ret; |
| 1021 | |
| 1022 | dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__); |
| 1023 | |
| 1024 | if (!ep0->dir_in) { |
| 1025 | dev_warn(hsotg->dev, "%s: direction out?\n", __func__); |
| 1026 | return -EINVAL; |
| 1027 | } |
| 1028 | |
| 1029 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1030 | case USB_RECIP_DEVICE: |
| 1031 | reply = cpu_to_le16(0); /* bit 0 => self powered, |
| 1032 | * bit 1 => remote wakeup */ |
| 1033 | break; |
| 1034 | |
| 1035 | case USB_RECIP_INTERFACE: |
| 1036 | /* currently, the data result should be zero */ |
| 1037 | reply = cpu_to_le16(0); |
| 1038 | break; |
| 1039 | |
| 1040 | case USB_RECIP_ENDPOINT: |
| 1041 | ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex)); |
| 1042 | if (!ep) |
| 1043 | return -ENOENT; |
| 1044 | |
| 1045 | reply = cpu_to_le16(ep->halted ? 1 : 0); |
| 1046 | break; |
| 1047 | |
| 1048 | default: |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | if (le16_to_cpu(ctrl->wLength) != 2) |
| 1053 | return -EINVAL; |
| 1054 | |
| 1055 | ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2); |
| 1056 | if (ret) { |
| 1057 | dev_err(hsotg->dev, "%s: failed to send reply\n", __func__); |
| 1058 | return ret; |
| 1059 | } |
| 1060 | |
| 1061 | return 1; |
| 1062 | } |
| 1063 | |
| 1064 | static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value); |
| 1065 | |
| 1066 | /** |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1067 | * get_ep_head - return the first request on the endpoint |
| 1068 | * @hs_ep: The controller endpoint to get |
| 1069 | * |
| 1070 | * Get the first request on the endpoint. |
| 1071 | */ |
| 1072 | static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep) |
| 1073 | { |
| 1074 | if (list_empty(&hs_ep->queue)) |
| 1075 | return NULL; |
| 1076 | |
| 1077 | return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue); |
| 1078 | } |
| 1079 | |
| 1080 | /** |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1081 | * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE |
| 1082 | * @hsotg: The device state |
| 1083 | * @ctrl: USB control request |
| 1084 | */ |
| 1085 | static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg, |
| 1086 | struct usb_ctrlrequest *ctrl) |
| 1087 | { |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1088 | struct s3c_hsotg_ep *ep0 = &hsotg->eps[0]; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1089 | struct s3c_hsotg_req *hs_req; |
| 1090 | bool restart; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1091 | bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE); |
| 1092 | struct s3c_hsotg_ep *ep; |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1093 | int ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1094 | |
| 1095 | dev_dbg(hsotg->dev, "%s: %s_FEATURE\n", |
| 1096 | __func__, set ? "SET" : "CLEAR"); |
| 1097 | |
| 1098 | if (ctrl->bRequestType == USB_RECIP_ENDPOINT) { |
| 1099 | ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex)); |
| 1100 | if (!ep) { |
| 1101 | dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n", |
| 1102 | __func__, le16_to_cpu(ctrl->wIndex)); |
| 1103 | return -ENOENT; |
| 1104 | } |
| 1105 | |
| 1106 | switch (le16_to_cpu(ctrl->wValue)) { |
| 1107 | case USB_ENDPOINT_HALT: |
| 1108 | s3c_hsotg_ep_sethalt(&ep->ep, set); |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1109 | |
| 1110 | ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0); |
| 1111 | if (ret) { |
| 1112 | dev_err(hsotg->dev, |
| 1113 | "%s: failed to send reply\n", __func__); |
| 1114 | return ret; |
| 1115 | } |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1116 | |
| 1117 | if (!set) { |
| 1118 | /* |
| 1119 | * If we have request in progress, |
| 1120 | * then complete it |
| 1121 | */ |
| 1122 | if (ep->req) { |
| 1123 | hs_req = ep->req; |
| 1124 | ep->req = NULL; |
| 1125 | list_del_init(&hs_req->queue); |
| 1126 | hs_req->req.complete(&ep->ep, |
| 1127 | &hs_req->req); |
| 1128 | } |
| 1129 | |
| 1130 | /* If we have pending request, then start it */ |
| 1131 | restart = !list_empty(&ep->queue); |
| 1132 | if (restart) { |
| 1133 | hs_req = get_ep_head(ep); |
| 1134 | s3c_hsotg_start_req(hsotg, ep, |
| 1135 | hs_req, false); |
| 1136 | } |
| 1137 | } |
| 1138 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1139 | break; |
| 1140 | |
| 1141 | default: |
| 1142 | return -ENOENT; |
| 1143 | } |
| 1144 | } else |
| 1145 | return -ENOENT; /* currently only deal with endpoint */ |
| 1146 | |
| 1147 | return 1; |
| 1148 | } |
| 1149 | |
| 1150 | /** |
| 1151 | * s3c_hsotg_process_control - process a control request |
| 1152 | * @hsotg: The device state |
| 1153 | * @ctrl: The control request received |
| 1154 | * |
| 1155 | * The controller has received the SETUP phase of a control request, and |
| 1156 | * needs to work out what to do next (and whether to pass it on to the |
| 1157 | * gadget driver). |
| 1158 | */ |
| 1159 | static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg, |
| 1160 | struct usb_ctrlrequest *ctrl) |
| 1161 | { |
| 1162 | struct s3c_hsotg_ep *ep0 = &hsotg->eps[0]; |
| 1163 | int ret = 0; |
| 1164 | u32 dcfg; |
| 1165 | |
| 1166 | ep0->sent_zlp = 0; |
| 1167 | |
| 1168 | dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n", |
| 1169 | ctrl->bRequest, ctrl->bRequestType, |
| 1170 | ctrl->wValue, ctrl->wLength); |
| 1171 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1172 | /* |
| 1173 | * record the direction of the request, for later use when enquing |
| 1174 | * packets onto EP0. |
| 1175 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1176 | |
| 1177 | ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0; |
| 1178 | dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in); |
| 1179 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1180 | /* |
| 1181 | * if we've no data with this request, then the last part of the |
| 1182 | * transaction is going to implicitly be IN. |
| 1183 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1184 | if (ctrl->wLength == 0) |
| 1185 | ep0->dir_in = 1; |
| 1186 | |
| 1187 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { |
| 1188 | switch (ctrl->bRequest) { |
| 1189 | case USB_REQ_SET_ADDRESS: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1190 | dcfg = readl(hsotg->regs + DCFG); |
| 1191 | dcfg &= ~DCFG_DevAddr_MASK; |
| 1192 | dcfg |= ctrl->wValue << DCFG_DevAddr_SHIFT; |
| 1193 | writel(dcfg, hsotg->regs + DCFG); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1194 | |
| 1195 | dev_info(hsotg->dev, "new address %d\n", ctrl->wValue); |
| 1196 | |
| 1197 | ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0); |
| 1198 | return; |
| 1199 | |
| 1200 | case USB_REQ_GET_STATUS: |
| 1201 | ret = s3c_hsotg_process_req_status(hsotg, ctrl); |
| 1202 | break; |
| 1203 | |
| 1204 | case USB_REQ_CLEAR_FEATURE: |
| 1205 | case USB_REQ_SET_FEATURE: |
| 1206 | ret = s3c_hsotg_process_req_feature(hsotg, ctrl); |
| 1207 | break; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | /* as a fallback, try delivering it to the driver to deal with */ |
| 1212 | |
| 1213 | if (ret == 0 && hsotg->driver) { |
| 1214 | ret = hsotg->driver->setup(&hsotg->gadget, ctrl); |
| 1215 | if (ret < 0) |
| 1216 | dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret); |
| 1217 | } |
| 1218 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1219 | /* |
| 1220 | * the request is either unhandlable, or is not formatted correctly |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1221 | * so respond with a STALL for the status stage to indicate failure. |
| 1222 | */ |
| 1223 | |
| 1224 | if (ret < 0) { |
| 1225 | u32 reg; |
| 1226 | u32 ctrl; |
| 1227 | |
| 1228 | dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1229 | reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1230 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1231 | /* |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1232 | * DxEPCTL_Stall will be cleared by EP once it has |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1233 | * taken effect, so no need to clear later. |
| 1234 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1235 | |
| 1236 | ctrl = readl(hsotg->regs + reg); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1237 | ctrl |= DxEPCTL_Stall; |
| 1238 | ctrl |= DxEPCTL_CNAK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1239 | writel(ctrl, hsotg->regs + reg); |
| 1240 | |
| 1241 | dev_dbg(hsotg->dev, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1242 | "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n", |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1243 | ctrl, reg, readl(hsotg->regs + reg)); |
| 1244 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1245 | /* |
| 1246 | * don't believe we need to anything more to get the EP |
| 1247 | * to reply with a STALL packet |
| 1248 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg); |
| 1253 | |
| 1254 | /** |
| 1255 | * s3c_hsotg_complete_setup - completion of a setup transfer |
| 1256 | * @ep: The endpoint the request was on. |
| 1257 | * @req: The request completed. |
| 1258 | * |
| 1259 | * Called on completion of any requests the driver itself submitted for |
| 1260 | * EP0 setup packets |
| 1261 | */ |
| 1262 | static void s3c_hsotg_complete_setup(struct usb_ep *ep, |
| 1263 | struct usb_request *req) |
| 1264 | { |
| 1265 | struct s3c_hsotg_ep *hs_ep = our_ep(ep); |
| 1266 | struct s3c_hsotg *hsotg = hs_ep->parent; |
| 1267 | |
| 1268 | if (req->status < 0) { |
| 1269 | dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status); |
| 1270 | return; |
| 1271 | } |
| 1272 | |
| 1273 | if (req->actual == 0) |
| 1274 | s3c_hsotg_enqueue_setup(hsotg); |
| 1275 | else |
| 1276 | s3c_hsotg_process_control(hsotg, req->buf); |
| 1277 | } |
| 1278 | |
| 1279 | /** |
| 1280 | * s3c_hsotg_enqueue_setup - start a request for EP0 packets |
| 1281 | * @hsotg: The device state. |
| 1282 | * |
| 1283 | * Enqueue a request on EP0 if necessary to received any SETUP packets |
| 1284 | * received from the host. |
| 1285 | */ |
| 1286 | static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg) |
| 1287 | { |
| 1288 | struct usb_request *req = hsotg->ctrl_req; |
| 1289 | struct s3c_hsotg_req *hs_req = our_req(req); |
| 1290 | int ret; |
| 1291 | |
| 1292 | dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__); |
| 1293 | |
| 1294 | req->zero = 0; |
| 1295 | req->length = 8; |
| 1296 | req->buf = hsotg->ctrl_buff; |
| 1297 | req->complete = s3c_hsotg_complete_setup; |
| 1298 | |
| 1299 | if (!list_empty(&hs_req->queue)) { |
| 1300 | dev_dbg(hsotg->dev, "%s already queued???\n", __func__); |
| 1301 | return; |
| 1302 | } |
| 1303 | |
| 1304 | hsotg->eps[0].dir_in = 0; |
| 1305 | |
| 1306 | ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC); |
| 1307 | if (ret < 0) { |
| 1308 | dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret); |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1309 | /* |
| 1310 | * Don't think there's much we can do other than watch the |
| 1311 | * driver fail. |
| 1312 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /** |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1317 | * s3c_hsotg_complete_request - complete a request given to us |
| 1318 | * @hsotg: The device state. |
| 1319 | * @hs_ep: The endpoint the request was on. |
| 1320 | * @hs_req: The request to complete. |
| 1321 | * @result: The result code (0 => Ok, otherwise errno) |
| 1322 | * |
| 1323 | * The given request has finished, so call the necessary completion |
| 1324 | * if it has one and then look to see if we can start a new request |
| 1325 | * on the endpoint. |
| 1326 | * |
| 1327 | * Note, expects the ep to already be locked as appropriate. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1328 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1329 | static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg, |
| 1330 | struct s3c_hsotg_ep *hs_ep, |
| 1331 | struct s3c_hsotg_req *hs_req, |
| 1332 | int result) |
| 1333 | { |
| 1334 | bool restart; |
| 1335 | |
| 1336 | if (!hs_req) { |
| 1337 | dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__); |
| 1338 | return; |
| 1339 | } |
| 1340 | |
| 1341 | dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n", |
| 1342 | hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete); |
| 1343 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1344 | /* |
| 1345 | * only replace the status if we've not already set an error |
| 1346 | * from a previous transaction |
| 1347 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1348 | |
| 1349 | if (hs_req->req.status == -EINPROGRESS) |
| 1350 | hs_req->req.status = result; |
| 1351 | |
| 1352 | hs_ep->req = NULL; |
| 1353 | list_del_init(&hs_req->queue); |
| 1354 | |
| 1355 | if (using_dma(hsotg)) |
| 1356 | s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req); |
| 1357 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1358 | /* |
| 1359 | * call the complete request with the locks off, just in case the |
| 1360 | * request tries to queue more work for this endpoint. |
| 1361 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1362 | |
| 1363 | if (hs_req->req.complete) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 1364 | spin_unlock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1365 | hs_req->req.complete(&hs_ep->ep, &hs_req->req); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 1366 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1367 | } |
| 1368 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1369 | /* |
| 1370 | * Look to see if there is anything else to do. Note, the completion |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1371 | * of the previous request may have caused a new request to be started |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1372 | * so be careful when doing this. |
| 1373 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1374 | |
| 1375 | if (!hs_ep->req && result >= 0) { |
| 1376 | restart = !list_empty(&hs_ep->queue); |
| 1377 | if (restart) { |
| 1378 | hs_req = get_ep_head(hs_ep); |
| 1379 | s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false); |
| 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | /** |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1385 | * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint |
| 1386 | * @hsotg: The device state. |
| 1387 | * @ep_idx: The endpoint index for the data |
| 1388 | * @size: The size of data in the fifo, in bytes |
| 1389 | * |
| 1390 | * The FIFO status shows there is data to read from the FIFO for a given |
| 1391 | * endpoint, so sort out whether we need to read the data into a request |
| 1392 | * that has been made for that endpoint. |
| 1393 | */ |
| 1394 | static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size) |
| 1395 | { |
| 1396 | struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx]; |
| 1397 | struct s3c_hsotg_req *hs_req = hs_ep->req; |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1398 | void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1399 | int to_read; |
| 1400 | int max_req; |
| 1401 | int read_ptr; |
| 1402 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 1403 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1404 | if (!hs_req) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1405 | u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1406 | int ptr; |
| 1407 | |
| 1408 | dev_warn(hsotg->dev, |
| 1409 | "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n", |
| 1410 | __func__, size, ep_idx, epctl); |
| 1411 | |
| 1412 | /* dump the data from the FIFO, we've nothing we can do */ |
| 1413 | for (ptr = 0; ptr < size; ptr += 4) |
| 1414 | (void)readl(fifo); |
| 1415 | |
| 1416 | return; |
| 1417 | } |
| 1418 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1419 | to_read = size; |
| 1420 | read_ptr = hs_req->req.actual; |
| 1421 | max_req = hs_req->req.length - read_ptr; |
| 1422 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 1423 | dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n", |
| 1424 | __func__, to_read, max_req, read_ptr, hs_req->req.length); |
| 1425 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1426 | if (to_read > max_req) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1427 | /* |
| 1428 | * more data appeared than we where willing |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1429 | * to deal with in this request. |
| 1430 | */ |
| 1431 | |
| 1432 | /* currently we don't deal this */ |
| 1433 | WARN_ON_ONCE(1); |
| 1434 | } |
| 1435 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1436 | hs_ep->total_data += to_read; |
| 1437 | hs_req->req.actual += to_read; |
| 1438 | to_read = DIV_ROUND_UP(to_read, 4); |
| 1439 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1440 | /* |
| 1441 | * note, we might over-write the buffer end by 3 bytes depending on |
| 1442 | * alignment of the data. |
| 1443 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1444 | readsl(fifo, hs_req->req.buf + read_ptr, to_read); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | /** |
| 1448 | * s3c_hsotg_send_zlp - send zero-length packet on control endpoint |
| 1449 | * @hsotg: The device instance |
| 1450 | * @req: The request currently on this endpoint |
| 1451 | * |
| 1452 | * Generate a zero-length IN packet request for terminating a SETUP |
| 1453 | * transaction. |
| 1454 | * |
| 1455 | * Note, since we don't write any data to the TxFIFO, then it is |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1456 | * currently believed that we do not need to wait for any space in |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1457 | * the TxFIFO. |
| 1458 | */ |
| 1459 | static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg, |
| 1460 | struct s3c_hsotg_req *req) |
| 1461 | { |
| 1462 | u32 ctrl; |
| 1463 | |
| 1464 | if (!req) { |
| 1465 | dev_warn(hsotg->dev, "%s: no request?\n", __func__); |
| 1466 | return; |
| 1467 | } |
| 1468 | |
| 1469 | if (req->req.length == 0) { |
| 1470 | hsotg->eps[0].sent_zlp = 1; |
| 1471 | s3c_hsotg_enqueue_setup(hsotg); |
| 1472 | return; |
| 1473 | } |
| 1474 | |
| 1475 | hsotg->eps[0].dir_in = 1; |
| 1476 | hsotg->eps[0].sent_zlp = 1; |
| 1477 | |
| 1478 | dev_dbg(hsotg->dev, "sending zero-length packet\n"); |
| 1479 | |
| 1480 | /* issue a zero-sized packet to terminate this */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1481 | writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) | |
| 1482 | DxEPTSIZ_XferSize(0), hsotg->regs + DIEPTSIZ(0)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1483 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1484 | ctrl = readl(hsotg->regs + DIEPCTL0); |
| 1485 | ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */ |
| 1486 | ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */ |
| 1487 | ctrl |= DxEPCTL_USBActEp; |
| 1488 | writel(ctrl, hsotg->regs + DIEPCTL0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | /** |
| 1492 | * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO |
| 1493 | * @hsotg: The device instance |
| 1494 | * @epnum: The endpoint received from |
| 1495 | * @was_setup: Set if processing a SetupDone event. |
| 1496 | * |
| 1497 | * The RXFIFO has delivered an OutDone event, which means that the data |
| 1498 | * transfer for an OUT endpoint has been completed, either by a short |
| 1499 | * packet or by the finish of a transfer. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1500 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1501 | static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg, |
| 1502 | int epnum, bool was_setup) |
| 1503 | { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1504 | u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1505 | struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum]; |
| 1506 | struct s3c_hsotg_req *hs_req = hs_ep->req; |
| 1507 | struct usb_request *req = &hs_req->req; |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1508 | unsigned size_left = DxEPTSIZ_XferSize_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1509 | int result = 0; |
| 1510 | |
| 1511 | if (!hs_req) { |
| 1512 | dev_dbg(hsotg->dev, "%s: no request active\n", __func__); |
| 1513 | return; |
| 1514 | } |
| 1515 | |
| 1516 | if (using_dma(hsotg)) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1517 | unsigned size_done; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1518 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1519 | /* |
| 1520 | * Calculate the size of the transfer by checking how much |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1521 | * is left in the endpoint size register and then working it |
| 1522 | * out from the amount we loaded for the transfer. |
| 1523 | * |
| 1524 | * We need to do this as DMA pointers are always 32bit aligned |
| 1525 | * so may overshoot/undershoot the transfer. |
| 1526 | */ |
| 1527 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1528 | size_done = hs_ep->size_loaded - size_left; |
| 1529 | size_done += hs_ep->last_load; |
| 1530 | |
| 1531 | req->actual = size_done; |
| 1532 | } |
| 1533 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 1534 | /* if there is more request to do, schedule new transfer */ |
| 1535 | if (req->actual < req->length && size_left == 0) { |
| 1536 | s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
| 1537 | return; |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1538 | } else if (epnum == 0) { |
| 1539 | /* |
| 1540 | * After was_setup = 1 => |
| 1541 | * set CNAK for non Setup requests |
| 1542 | */ |
| 1543 | hsotg->setup = was_setup ? 0 : 1; |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 1544 | } |
| 1545 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1546 | if (req->actual < req->length && req->short_not_ok) { |
| 1547 | dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n", |
| 1548 | __func__, req->actual, req->length); |
| 1549 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1550 | /* |
| 1551 | * todo - what should we return here? there's no one else |
| 1552 | * even bothering to check the status. |
| 1553 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | if (epnum == 0) { |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 1557 | /* |
| 1558 | * Condition req->complete != s3c_hsotg_complete_setup says: |
| 1559 | * send ZLP when we have an asynchronous request from gadget |
| 1560 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1561 | if (!was_setup && req->complete != s3c_hsotg_complete_setup) |
| 1562 | s3c_hsotg_send_zlp(hsotg, hs_req); |
| 1563 | } |
| 1564 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1565 | s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | /** |
| 1569 | * s3c_hsotg_read_frameno - read current frame number |
| 1570 | * @hsotg: The device instance |
| 1571 | * |
| 1572 | * Return the current frame number |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1573 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1574 | static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg) |
| 1575 | { |
| 1576 | u32 dsts; |
| 1577 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1578 | dsts = readl(hsotg->regs + DSTS); |
| 1579 | dsts &= DSTS_SOFFN_MASK; |
| 1580 | dsts >>= DSTS_SOFFN_SHIFT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1581 | |
| 1582 | return dsts; |
| 1583 | } |
| 1584 | |
| 1585 | /** |
| 1586 | * s3c_hsotg_handle_rx - RX FIFO has data |
| 1587 | * @hsotg: The device instance |
| 1588 | * |
| 1589 | * The IRQ handler has detected that the RX FIFO has some data in it |
| 1590 | * that requires processing, so find out what is in there and do the |
| 1591 | * appropriate read. |
| 1592 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1593 | * The RXFIFO is a true FIFO, the packets coming out are still in packet |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1594 | * chunks, so if you have x packets received on an endpoint you'll get x |
| 1595 | * FIFO events delivered, each with a packet's worth of data in it. |
| 1596 | * |
| 1597 | * When using DMA, we should not be processing events from the RXFIFO |
| 1598 | * as the actual data should be sent to the memory directly and we turn |
| 1599 | * on the completion interrupts to get notifications of transfer completion. |
| 1600 | */ |
Mark Brown | 0978f8c | 2010-01-18 13:18:35 +0000 | [diff] [blame] | 1601 | static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1602 | { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1603 | u32 grxstsr = readl(hsotg->regs + GRXSTSP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1604 | u32 epnum, status, size; |
| 1605 | |
| 1606 | WARN_ON(using_dma(hsotg)); |
| 1607 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1608 | epnum = grxstsr & GRXSTS_EPNum_MASK; |
| 1609 | status = grxstsr & GRXSTS_PktSts_MASK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1610 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1611 | size = grxstsr & GRXSTS_ByteCnt_MASK; |
| 1612 | size >>= GRXSTS_ByteCnt_SHIFT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1613 | |
| 1614 | if (1) |
| 1615 | dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n", |
| 1616 | __func__, grxstsr, size, epnum); |
| 1617 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1618 | #define __status(x) ((x) >> GRXSTS_PktSts_SHIFT) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1619 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1620 | switch (status >> GRXSTS_PktSts_SHIFT) { |
| 1621 | case __status(GRXSTS_PktSts_GlobalOutNAK): |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1622 | dev_dbg(hsotg->dev, "GlobalOutNAK\n"); |
| 1623 | break; |
| 1624 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1625 | case __status(GRXSTS_PktSts_OutDone): |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1626 | dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n", |
| 1627 | s3c_hsotg_read_frameno(hsotg)); |
| 1628 | |
| 1629 | if (!using_dma(hsotg)) |
| 1630 | s3c_hsotg_handle_outdone(hsotg, epnum, false); |
| 1631 | break; |
| 1632 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1633 | case __status(GRXSTS_PktSts_SetupDone): |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1634 | dev_dbg(hsotg->dev, |
| 1635 | "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
| 1636 | s3c_hsotg_read_frameno(hsotg), |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1637 | readl(hsotg->regs + DOEPCTL(0))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1638 | |
| 1639 | s3c_hsotg_handle_outdone(hsotg, epnum, true); |
| 1640 | break; |
| 1641 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1642 | case __status(GRXSTS_PktSts_OutRX): |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1643 | s3c_hsotg_rx_data(hsotg, epnum, size); |
| 1644 | break; |
| 1645 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1646 | case __status(GRXSTS_PktSts_SetupRX): |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1647 | dev_dbg(hsotg->dev, |
| 1648 | "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
| 1649 | s3c_hsotg_read_frameno(hsotg), |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1650 | readl(hsotg->regs + DOEPCTL(0))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1651 | |
| 1652 | s3c_hsotg_rx_data(hsotg, epnum, size); |
| 1653 | break; |
| 1654 | |
| 1655 | default: |
| 1656 | dev_warn(hsotg->dev, "%s: unknown status %08x\n", |
| 1657 | __func__, grxstsr); |
| 1658 | |
| 1659 | s3c_hsotg_dump(hsotg); |
| 1660 | break; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | /** |
| 1665 | * s3c_hsotg_ep0_mps - turn max packet size into register setting |
| 1666 | * @mps: The maximum packet size in bytes. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1667 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1668 | static u32 s3c_hsotg_ep0_mps(unsigned int mps) |
| 1669 | { |
| 1670 | switch (mps) { |
| 1671 | case 64: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1672 | return D0EPCTL_MPS_64; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1673 | case 32: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1674 | return D0EPCTL_MPS_32; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1675 | case 16: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1676 | return D0EPCTL_MPS_16; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1677 | case 8: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1678 | return D0EPCTL_MPS_8; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | /* bad max packet size, warn and return invalid result */ |
| 1682 | WARN_ON(1); |
| 1683 | return (u32)-1; |
| 1684 | } |
| 1685 | |
| 1686 | /** |
| 1687 | * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field |
| 1688 | * @hsotg: The driver state. |
| 1689 | * @ep: The index number of the endpoint |
| 1690 | * @mps: The maximum packet size in bytes |
| 1691 | * |
| 1692 | * Configure the maximum packet size for the given endpoint, updating |
| 1693 | * the hardware control registers to reflect this. |
| 1694 | */ |
| 1695 | static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg, |
| 1696 | unsigned int ep, unsigned int mps) |
| 1697 | { |
| 1698 | struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep]; |
| 1699 | void __iomem *regs = hsotg->regs; |
| 1700 | u32 mpsval; |
| 1701 | u32 reg; |
| 1702 | |
| 1703 | if (ep == 0) { |
| 1704 | /* EP0 is a special case */ |
| 1705 | mpsval = s3c_hsotg_ep0_mps(mps); |
| 1706 | if (mpsval > 3) |
| 1707 | goto bad_mps; |
| 1708 | } else { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1709 | if (mps >= DxEPCTL_MPS_LIMIT+1) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1710 | goto bad_mps; |
| 1711 | |
| 1712 | mpsval = mps; |
| 1713 | } |
| 1714 | |
| 1715 | hs_ep->ep.maxpacket = mps; |
| 1716 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1717 | /* |
| 1718 | * update both the in and out endpoint controldir_ registers, even |
| 1719 | * if one of the directions may not be in use. |
| 1720 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1721 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1722 | reg = readl(regs + DIEPCTL(ep)); |
| 1723 | reg &= ~DxEPCTL_MPS_MASK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1724 | reg |= mpsval; |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1725 | writel(reg, regs + DIEPCTL(ep)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1726 | |
Anton Tikhomirov | 659ad60 | 2012-03-06 14:07:29 +0900 | [diff] [blame] | 1727 | if (ep) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1728 | reg = readl(regs + DOEPCTL(ep)); |
| 1729 | reg &= ~DxEPCTL_MPS_MASK; |
Anton Tikhomirov | 659ad60 | 2012-03-06 14:07:29 +0900 | [diff] [blame] | 1730 | reg |= mpsval; |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1731 | writel(reg, regs + DOEPCTL(ep)); |
Anton Tikhomirov | 659ad60 | 2012-03-06 14:07:29 +0900 | [diff] [blame] | 1732 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1733 | |
| 1734 | return; |
| 1735 | |
| 1736 | bad_mps: |
| 1737 | dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps); |
| 1738 | } |
| 1739 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1740 | /** |
| 1741 | * s3c_hsotg_txfifo_flush - flush Tx FIFO |
| 1742 | * @hsotg: The driver state |
| 1743 | * @idx: The index for the endpoint (0..15) |
| 1744 | */ |
| 1745 | static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx) |
| 1746 | { |
| 1747 | int timeout; |
| 1748 | int val; |
| 1749 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1750 | writel(GRSTCTL_TxFNum(idx) | GRSTCTL_TxFFlsh, |
| 1751 | hsotg->regs + GRSTCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1752 | |
| 1753 | /* wait until the fifo is flushed */ |
| 1754 | timeout = 100; |
| 1755 | |
| 1756 | while (1) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1757 | val = readl(hsotg->regs + GRSTCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1758 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1759 | if ((val & (GRSTCTL_TxFFlsh)) == 0) |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1760 | break; |
| 1761 | |
| 1762 | if (--timeout == 0) { |
| 1763 | dev_err(hsotg->dev, |
| 1764 | "%s: timeout flushing fifo (GRSTCTL=%08x)\n", |
| 1765 | __func__, val); |
| 1766 | } |
| 1767 | |
| 1768 | udelay(1); |
| 1769 | } |
| 1770 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1771 | |
| 1772 | /** |
| 1773 | * s3c_hsotg_trytx - check to see if anything needs transmitting |
| 1774 | * @hsotg: The driver state |
| 1775 | * @hs_ep: The driver endpoint to check. |
| 1776 | * |
| 1777 | * Check to see if there is a request that has data to send, and if so |
| 1778 | * make an attempt to write data into the FIFO. |
| 1779 | */ |
| 1780 | static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg, |
| 1781 | struct s3c_hsotg_ep *hs_ep) |
| 1782 | { |
| 1783 | struct s3c_hsotg_req *hs_req = hs_ep->req; |
| 1784 | |
| 1785 | if (!hs_ep->dir_in || !hs_req) |
| 1786 | return 0; |
| 1787 | |
| 1788 | if (hs_req->req.actual < hs_req->req.length) { |
| 1789 | dev_dbg(hsotg->dev, "trying to write more for ep%d\n", |
| 1790 | hs_ep->index); |
| 1791 | return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
| 1792 | } |
| 1793 | |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
| 1797 | /** |
| 1798 | * s3c_hsotg_complete_in - complete IN transfer |
| 1799 | * @hsotg: The device state. |
| 1800 | * @hs_ep: The endpoint that has just completed. |
| 1801 | * |
| 1802 | * An IN transfer has been completed, update the transfer's state and then |
| 1803 | * call the relevant completion routines. |
| 1804 | */ |
| 1805 | static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg, |
| 1806 | struct s3c_hsotg_ep *hs_ep) |
| 1807 | { |
| 1808 | struct s3c_hsotg_req *hs_req = hs_ep->req; |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1809 | u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1810 | int size_left, size_done; |
| 1811 | |
| 1812 | if (!hs_req) { |
| 1813 | dev_dbg(hsotg->dev, "XferCompl but no req\n"); |
| 1814 | return; |
| 1815 | } |
| 1816 | |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 1817 | /* Finish ZLP handling for IN EP0 transactions */ |
| 1818 | if (hsotg->eps[0].sent_zlp) { |
| 1819 | dev_dbg(hsotg->dev, "zlp packet received\n"); |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1820 | s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 1821 | return; |
| 1822 | } |
| 1823 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1824 | /* |
| 1825 | * Calculate the size of the transfer by checking how much is left |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1826 | * in the endpoint size register and then working it out from |
| 1827 | * the amount we loaded for the transfer. |
| 1828 | * |
| 1829 | * We do this even for DMA, as the transfer may have incremented |
| 1830 | * past the end of the buffer (DMA transfers are always 32bit |
| 1831 | * aligned). |
| 1832 | */ |
| 1833 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1834 | size_left = DxEPTSIZ_XferSize_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1835 | |
| 1836 | size_done = hs_ep->size_loaded - size_left; |
| 1837 | size_done += hs_ep->last_load; |
| 1838 | |
| 1839 | if (hs_req->req.actual != size_done) |
| 1840 | dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n", |
| 1841 | __func__, hs_req->req.actual, size_done); |
| 1842 | |
| 1843 | hs_req->req.actual = size_done; |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 1844 | dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n", |
| 1845 | hs_req->req.length, hs_req->req.actual, hs_req->req.zero); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1846 | |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 1847 | /* |
| 1848 | * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0 |
| 1849 | * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B |
| 1850 | * ,256B ... ), after last MPS sized packet send IN ZLP packet to |
| 1851 | * inform the host that no more data is available. |
| 1852 | * The state of req.zero member is checked to be sure that the value to |
| 1853 | * send is smaller than wValue expected from host. |
| 1854 | * Check req.length to NOT send another ZLP when the current one is |
| 1855 | * under completion (the one for which this completion has been called). |
| 1856 | */ |
| 1857 | if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero && |
| 1858 | hs_req->req.length == hs_req->req.actual && |
| 1859 | !(hs_req->req.length % hs_ep->ep.maxpacket)) { |
| 1860 | |
| 1861 | dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n"); |
| 1862 | s3c_hsotg_send_zlp(hsotg, hs_req); |
| 1863 | |
| 1864 | return; |
| 1865 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1866 | |
| 1867 | if (!size_left && hs_req->req.actual < hs_req->req.length) { |
| 1868 | dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__); |
| 1869 | s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
| 1870 | } else |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1871 | s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | /** |
| 1875 | * s3c_hsotg_epint - handle an in/out endpoint interrupt |
| 1876 | * @hsotg: The driver state |
| 1877 | * @idx: The index for the endpoint (0..15) |
| 1878 | * @dir_in: Set if this is an IN endpoint |
| 1879 | * |
| 1880 | * Process and clear any interrupt pending for an individual endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1881 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1882 | static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx, |
| 1883 | int dir_in) |
| 1884 | { |
| 1885 | struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx]; |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1886 | u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); |
| 1887 | u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); |
| 1888 | u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1889 | u32 ints; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1890 | |
| 1891 | ints = readl(hsotg->regs + epint_reg); |
| 1892 | |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 1893 | /* Clear endpoint interrupts */ |
| 1894 | writel(ints, hsotg->regs + epint_reg); |
| 1895 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1896 | dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n", |
| 1897 | __func__, idx, dir_in ? "in" : "out", ints); |
| 1898 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1899 | if (ints & DxEPINT_XferCompl) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1900 | dev_dbg(hsotg->dev, |
| 1901 | "%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n", |
| 1902 | __func__, readl(hsotg->regs + epctl_reg), |
| 1903 | readl(hsotg->regs + epsiz_reg)); |
| 1904 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1905 | /* |
| 1906 | * we get OutDone from the FIFO, so we only need to look |
| 1907 | * at completing IN requests here |
| 1908 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1909 | if (dir_in) { |
| 1910 | s3c_hsotg_complete_in(hsotg, hs_ep); |
| 1911 | |
Ben Dooks | c9a64ea | 2010-07-19 09:40:46 +0100 | [diff] [blame] | 1912 | if (idx == 0 && !hs_ep->req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1913 | s3c_hsotg_enqueue_setup(hsotg); |
| 1914 | } else if (using_dma(hsotg)) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1915 | /* |
| 1916 | * We're using DMA, we need to fire an OutDone here |
| 1917 | * as we ignore the RXFIFO. |
| 1918 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1919 | |
| 1920 | s3c_hsotg_handle_outdone(hsotg, idx, false); |
| 1921 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1922 | } |
| 1923 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1924 | if (ints & DxEPINT_EPDisbld) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1925 | dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1926 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1927 | if (dir_in) { |
| 1928 | int epctl = readl(hsotg->regs + epctl_reg); |
| 1929 | |
| 1930 | s3c_hsotg_txfifo_flush(hsotg, idx); |
| 1931 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1932 | if ((epctl & DxEPCTL_Stall) && |
| 1933 | (epctl & DxEPCTL_EPType_Bulk)) { |
| 1934 | int dctl = readl(hsotg->regs + DCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1935 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1936 | dctl |= DCTL_CGNPInNAK; |
| 1937 | writel(dctl, hsotg->regs + DCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | } |
| 1941 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1942 | if (ints & DxEPINT_AHBErr) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1943 | dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1944 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1945 | if (ints & DxEPINT_Setup) { /* Setup or Timeout */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1946 | dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__); |
| 1947 | |
| 1948 | if (using_dma(hsotg) && idx == 0) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1949 | /* |
| 1950 | * this is the notification we've received a |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1951 | * setup packet. In non-DMA mode we'd get this |
| 1952 | * from the RXFIFO, instead we need to process |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1953 | * the setup here. |
| 1954 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1955 | |
| 1956 | if (dir_in) |
| 1957 | WARN_ON_ONCE(1); |
| 1958 | else |
| 1959 | s3c_hsotg_handle_outdone(hsotg, 0, true); |
| 1960 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1961 | } |
| 1962 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1963 | if (ints & DxEPINT_Back2BackSetup) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1964 | dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1965 | |
| 1966 | if (dir_in) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1967 | /* not sure if this is important, but we'll clear it anyway */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1968 | if (ints & DIEPMSK_INTknTXFEmpMsk) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1969 | dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n", |
| 1970 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | /* this probably means something bad is happening */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1974 | if (ints & DIEPMSK_INTknEPMisMsk) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1975 | dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n", |
| 1976 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1977 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 1978 | |
| 1979 | /* FIFO has space or is empty (see GAHBCFG) */ |
| 1980 | if (hsotg->dedicated_fifos && |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1981 | ints & DIEPMSK_TxFIFOEmpty) { |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 1982 | dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n", |
| 1983 | __func__, idx); |
Anton Tikhomirov | 70fa030 | 2012-03-06 14:08:29 +0900 | [diff] [blame] | 1984 | if (!using_dma(hsotg)) |
| 1985 | s3c_hsotg_trytx(hsotg, hs_ep); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 1986 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1987 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /** |
| 1991 | * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done) |
| 1992 | * @hsotg: The device state. |
| 1993 | * |
| 1994 | * Handle updating the device settings after the enumeration phase has |
| 1995 | * been completed. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1996 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1997 | static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg) |
| 1998 | { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1999 | u32 dsts = readl(hsotg->regs + DSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2000 | int ep0_mps = 0, ep_mps; |
| 2001 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2002 | /* |
| 2003 | * This should signal the finish of the enumeration phase |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2004 | * of the USB handshaking, so we should now know what rate |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2005 | * we connected at. |
| 2006 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2007 | |
| 2008 | dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts); |
| 2009 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2010 | /* |
| 2011 | * note, since we're limited by the size of transfer on EP0, and |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2012 | * it seems IN transfers must be a even number of packets we do |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2013 | * not advertise a 64byte MPS on EP0. |
| 2014 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2015 | |
| 2016 | /* catch both EnumSpd_FS and EnumSpd_FS48 */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2017 | switch (dsts & DSTS_EnumSpd_MASK) { |
| 2018 | case DSTS_EnumSpd_FS: |
| 2019 | case DSTS_EnumSpd_FS48: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2020 | hsotg->gadget.speed = USB_SPEED_FULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2021 | ep0_mps = EP0_MPS_LIMIT; |
| 2022 | ep_mps = 64; |
| 2023 | break; |
| 2024 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2025 | case DSTS_EnumSpd_HS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2026 | hsotg->gadget.speed = USB_SPEED_HIGH; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2027 | ep0_mps = EP0_MPS_LIMIT; |
| 2028 | ep_mps = 512; |
| 2029 | break; |
| 2030 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2031 | case DSTS_EnumSpd_LS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2032 | hsotg->gadget.speed = USB_SPEED_LOW; |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2033 | /* |
| 2034 | * note, we don't actually support LS in this driver at the |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2035 | * moment, and the documentation seems to imply that it isn't |
| 2036 | * supported by the PHYs on some of the devices. |
| 2037 | */ |
| 2038 | break; |
| 2039 | } |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 2040 | dev_info(hsotg->dev, "new device is %s\n", |
| 2041 | usb_speed_string(hsotg->gadget.speed)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2042 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2043 | /* |
| 2044 | * we should now know the maximum packet size for an |
| 2045 | * endpoint, so set the endpoints to a default value. |
| 2046 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2047 | |
| 2048 | if (ep0_mps) { |
| 2049 | int i; |
| 2050 | s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2051 | for (i = 1; i < hsotg->num_of_eps; i++) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2052 | s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps); |
| 2053 | } |
| 2054 | |
| 2055 | /* ensure after enumeration our EP0 is active */ |
| 2056 | |
| 2057 | s3c_hsotg_enqueue_setup(hsotg); |
| 2058 | |
| 2059 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2060 | readl(hsotg->regs + DIEPCTL0), |
| 2061 | readl(hsotg->regs + DOEPCTL0)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | /** |
| 2065 | * kill_all_requests - remove all requests from the endpoint's queue |
| 2066 | * @hsotg: The device state. |
| 2067 | * @ep: The endpoint the requests may be on. |
| 2068 | * @result: The result code to use. |
| 2069 | * @force: Force removal of any current requests |
| 2070 | * |
| 2071 | * Go through the requests on the given endpoint and mark them |
| 2072 | * completed with the given result code. |
| 2073 | */ |
| 2074 | static void kill_all_requests(struct s3c_hsotg *hsotg, |
| 2075 | struct s3c_hsotg_ep *ep, |
| 2076 | int result, bool force) |
| 2077 | { |
| 2078 | struct s3c_hsotg_req *req, *treq; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2079 | |
| 2080 | list_for_each_entry_safe(req, treq, &ep->queue, queue) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2081 | /* |
| 2082 | * currently, we can't do much about an already |
| 2083 | * running request on an in endpoint |
| 2084 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2085 | |
| 2086 | if (ep->req == req && ep->dir_in && !force) |
| 2087 | continue; |
| 2088 | |
| 2089 | s3c_hsotg_complete_request(hsotg, ep, req, |
| 2090 | result); |
| 2091 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | #define call_gadget(_hs, _entry) \ |
| 2095 | if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \ |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 2096 | (_hs)->driver && (_hs)->driver->_entry) { \ |
| 2097 | spin_unlock(&_hs->lock); \ |
| 2098 | (_hs)->driver->_entry(&(_hs)->gadget); \ |
| 2099 | spin_lock(&_hs->lock); \ |
| 2100 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2101 | |
| 2102 | /** |
Lukasz Majewski | 5e89134 | 2012-05-04 14:17:07 +0200 | [diff] [blame] | 2103 | * s3c_hsotg_disconnect - disconnect service |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2104 | * @hsotg: The device state. |
| 2105 | * |
Lukasz Majewski | 5e89134 | 2012-05-04 14:17:07 +0200 | [diff] [blame] | 2106 | * The device has been disconnected. Remove all current |
| 2107 | * transactions and signal the gadget driver that this |
| 2108 | * has happened. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2109 | */ |
Lukasz Majewski | 5e89134 | 2012-05-04 14:17:07 +0200 | [diff] [blame] | 2110 | static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2111 | { |
| 2112 | unsigned ep; |
| 2113 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2114 | for (ep = 0; ep < hsotg->num_of_eps; ep++) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2115 | kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true); |
| 2116 | |
| 2117 | call_gadget(hsotg, disconnect); |
| 2118 | } |
| 2119 | |
| 2120 | /** |
| 2121 | * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler |
| 2122 | * @hsotg: The device state: |
| 2123 | * @periodic: True if this is a periodic FIFO interrupt |
| 2124 | */ |
| 2125 | static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic) |
| 2126 | { |
| 2127 | struct s3c_hsotg_ep *ep; |
| 2128 | int epno, ret; |
| 2129 | |
| 2130 | /* look through for any more data to transmit */ |
| 2131 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2132 | for (epno = 0; epno < hsotg->num_of_eps; epno++) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2133 | ep = &hsotg->eps[epno]; |
| 2134 | |
| 2135 | if (!ep->dir_in) |
| 2136 | continue; |
| 2137 | |
| 2138 | if ((periodic && !ep->periodic) || |
| 2139 | (!periodic && ep->periodic)) |
| 2140 | continue; |
| 2141 | |
| 2142 | ret = s3c_hsotg_trytx(hsotg, ep); |
| 2143 | if (ret < 0) |
| 2144 | break; |
| 2145 | } |
| 2146 | } |
| 2147 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2148 | /* IRQ flags which will trigger a retry around the IRQ loop */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2149 | #define IRQ_RETRY_MASK (GINTSTS_NPTxFEmp | \ |
| 2150 | GINTSTS_PTxFEmp | \ |
| 2151 | GINTSTS_RxFLvl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2152 | |
| 2153 | /** |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2154 | * s3c_hsotg_corereset - issue softreset to the core |
| 2155 | * @hsotg: The device state |
| 2156 | * |
| 2157 | * Issue a soft reset to the core, and await the core finishing it. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2158 | */ |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2159 | static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg) |
| 2160 | { |
| 2161 | int timeout; |
| 2162 | u32 grstctl; |
| 2163 | |
| 2164 | dev_dbg(hsotg->dev, "resetting core\n"); |
| 2165 | |
| 2166 | /* issue soft reset */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2167 | writel(GRSTCTL_CSftRst, hsotg->regs + GRSTCTL); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2168 | |
Du, Changbin | 2868fea | 2012-07-24 08:19:25 +0800 | [diff] [blame] | 2169 | timeout = 10000; |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2170 | do { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2171 | grstctl = readl(hsotg->regs + GRSTCTL); |
| 2172 | } while ((grstctl & GRSTCTL_CSftRst) && timeout-- > 0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2173 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2174 | if (grstctl & GRSTCTL_CSftRst) { |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2175 | dev_err(hsotg->dev, "Failed to get CSftRst asserted\n"); |
| 2176 | return -EINVAL; |
| 2177 | } |
| 2178 | |
Du, Changbin | 2868fea | 2012-07-24 08:19:25 +0800 | [diff] [blame] | 2179 | timeout = 10000; |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2180 | |
| 2181 | while (1) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2182 | u32 grstctl = readl(hsotg->regs + GRSTCTL); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2183 | |
| 2184 | if (timeout-- < 0) { |
| 2185 | dev_info(hsotg->dev, |
| 2186 | "%s: reset failed, GRSTCTL=%08x\n", |
| 2187 | __func__, grstctl); |
| 2188 | return -ETIMEDOUT; |
| 2189 | } |
| 2190 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2191 | if (!(grstctl & GRSTCTL_AHBIdle)) |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2192 | continue; |
| 2193 | |
| 2194 | break; /* reset done */ |
| 2195 | } |
| 2196 | |
| 2197 | dev_dbg(hsotg->dev, "reset successful\n"); |
| 2198 | return 0; |
| 2199 | } |
| 2200 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2201 | /** |
| 2202 | * s3c_hsotg_core_init - issue softreset to the core |
| 2203 | * @hsotg: The device state |
| 2204 | * |
| 2205 | * Issue a soft reset to the core, and await the core finishing it. |
| 2206 | */ |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2207 | static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg) |
| 2208 | { |
| 2209 | s3c_hsotg_corereset(hsotg); |
| 2210 | |
| 2211 | /* |
| 2212 | * we must now enable ep0 ready for host detection and then |
| 2213 | * set configuration. |
| 2214 | */ |
| 2215 | |
| 2216 | /* set the PLL on, remove the HNP/SRP and set the PHY */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2217 | writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) | |
| 2218 | (0x5 << 10), hsotg->regs + GUSBCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2219 | |
| 2220 | s3c_hsotg_init_fifo(hsotg); |
| 2221 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2222 | __orr32(hsotg->regs + DCTL, DCTL_SftDiscon); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2223 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2224 | writel(1 << 18 | DCFG_DevSpd_HS, hsotg->regs + DCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2225 | |
| 2226 | /* Clear any pending OTG interrupts */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2227 | writel(0xffffffff, hsotg->regs + GOTGINT); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2228 | |
| 2229 | /* Clear any pending interrupts */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2230 | writel(0xffffffff, hsotg->regs + GINTSTS); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2231 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2232 | writel(GINTSTS_ErlySusp | GINTSTS_SessReqInt | |
| 2233 | GINTSTS_GOUTNakEff | GINTSTS_GINNakEff | |
| 2234 | GINTSTS_ConIDStsChng | GINTSTS_USBRst | |
| 2235 | GINTSTS_EnumDone | GINTSTS_OTGInt | |
| 2236 | GINTSTS_USBSusp | GINTSTS_WkUpInt, |
| 2237 | hsotg->regs + GINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2238 | |
| 2239 | if (using_dma(hsotg)) |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2240 | writel(GAHBCFG_GlblIntrEn | GAHBCFG_DMAEn | |
| 2241 | GAHBCFG_HBstLen_Incr4, |
| 2242 | hsotg->regs + GAHBCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2243 | else |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2244 | writel(GAHBCFG_GlblIntrEn, hsotg->regs + GAHBCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2245 | |
| 2246 | /* |
| 2247 | * Enabling INTknTXFEmpMsk here seems to be a big mistake, we end |
| 2248 | * up being flooded with interrupts if the host is polling the |
| 2249 | * endpoint to try and read data. |
| 2250 | */ |
| 2251 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2252 | writel(((hsotg->dedicated_fifos) ? DIEPMSK_TxFIFOEmpty : 0) | |
| 2253 | DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk | |
| 2254 | DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk | |
| 2255 | DIEPMSK_INTknEPMisMsk, |
| 2256 | hsotg->regs + DIEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2257 | |
| 2258 | /* |
| 2259 | * don't need XferCompl, we get that from RXFIFO in slave mode. In |
| 2260 | * DMA mode we may need this. |
| 2261 | */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2262 | writel((using_dma(hsotg) ? (DIEPMSK_XferComplMsk | |
| 2263 | DIEPMSK_TimeOUTMsk) : 0) | |
| 2264 | DOEPMSK_EPDisbldMsk | DOEPMSK_AHBErrMsk | |
| 2265 | DOEPMSK_SetupMsk, |
| 2266 | hsotg->regs + DOEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2267 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2268 | writel(0, hsotg->regs + DAINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2269 | |
| 2270 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2271 | readl(hsotg->regs + DIEPCTL0), |
| 2272 | readl(hsotg->regs + DOEPCTL0)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2273 | |
| 2274 | /* enable in and out endpoint interrupts */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2275 | s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPInt | GINTSTS_IEPInt); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2276 | |
| 2277 | /* |
| 2278 | * Enable the RXFIFO when in slave mode, as this is how we collect |
| 2279 | * the data. In DMA mode, we get events from the FIFO but also |
| 2280 | * things we cannot process, so do not use it. |
| 2281 | */ |
| 2282 | if (!using_dma(hsotg)) |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2283 | s3c_hsotg_en_gsint(hsotg, GINTSTS_RxFLvl); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2284 | |
| 2285 | /* Enable interrupts for EP0 in and out */ |
| 2286 | s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1); |
| 2287 | s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1); |
| 2288 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2289 | __orr32(hsotg->regs + DCTL, DCTL_PWROnPrgDone); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2290 | udelay(10); /* see openiboot */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2291 | __bic32(hsotg->regs + DCTL, DCTL_PWROnPrgDone); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2292 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2293 | dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2294 | |
| 2295 | /* |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2296 | * DxEPCTL_USBActEp says RO in manual, but seems to be set by |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2297 | * writing to the EPCTL register.. |
| 2298 | */ |
| 2299 | |
| 2300 | /* set to read 1 8byte packet */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2301 | writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) | |
| 2302 | DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2303 | |
| 2304 | writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2305 | DxEPCTL_CNAK | DxEPCTL_EPEna | |
| 2306 | DxEPCTL_USBActEp, |
| 2307 | hsotg->regs + DOEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2308 | |
| 2309 | /* enable, but don't activate EP0in */ |
| 2310 | writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2311 | DxEPCTL_USBActEp, hsotg->regs + DIEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2312 | |
| 2313 | s3c_hsotg_enqueue_setup(hsotg); |
| 2314 | |
| 2315 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2316 | readl(hsotg->regs + DIEPCTL0), |
| 2317 | readl(hsotg->regs + DOEPCTL0)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2318 | |
| 2319 | /* clear global NAKs */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2320 | writel(DCTL_CGOUTNak | DCTL_CGNPInNAK, |
| 2321 | hsotg->regs + DCTL); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2322 | |
| 2323 | /* must be at-least 3ms to allow bus to see disconnect */ |
| 2324 | mdelay(3); |
| 2325 | |
| 2326 | /* remove the soft-disconnect and let's go */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2327 | __bic32(hsotg->regs + DCTL, DCTL_SftDiscon); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | /** |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2331 | * s3c_hsotg_irq - handle device interrupt |
| 2332 | * @irq: The IRQ number triggered |
| 2333 | * @pw: The pw value when registered the handler. |
| 2334 | */ |
| 2335 | static irqreturn_t s3c_hsotg_irq(int irq, void *pw) |
| 2336 | { |
| 2337 | struct s3c_hsotg *hsotg = pw; |
| 2338 | int retry_count = 8; |
| 2339 | u32 gintsts; |
| 2340 | u32 gintmsk; |
| 2341 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 2342 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2343 | irq_retry: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2344 | gintsts = readl(hsotg->regs + GINTSTS); |
| 2345 | gintmsk = readl(hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2346 | |
| 2347 | dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n", |
| 2348 | __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count); |
| 2349 | |
| 2350 | gintsts &= gintmsk; |
| 2351 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2352 | if (gintsts & GINTSTS_OTGInt) { |
| 2353 | u32 otgint = readl(hsotg->regs + GOTGINT); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2354 | |
| 2355 | dev_info(hsotg->dev, "OTGInt: %08x\n", otgint); |
| 2356 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2357 | writel(otgint, hsotg->regs + GOTGINT); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2358 | } |
| 2359 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2360 | if (gintsts & GINTSTS_SessReqInt) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2361 | dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2362 | writel(GINTSTS_SessReqInt, hsotg->regs + GINTSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2363 | } |
| 2364 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2365 | if (gintsts & GINTSTS_EnumDone) { |
| 2366 | writel(GINTSTS_EnumDone, hsotg->regs + GINTSTS); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2367 | |
| 2368 | s3c_hsotg_irq_enumdone(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2369 | } |
| 2370 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2371 | if (gintsts & GINTSTS_ConIDStsChng) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2372 | dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2373 | readl(hsotg->regs + DSTS), |
| 2374 | readl(hsotg->regs + GOTGCTL)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2375 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2376 | writel(GINTSTS_ConIDStsChng, hsotg->regs + GINTSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2379 | if (gintsts & (GINTSTS_OEPInt | GINTSTS_IEPInt)) { |
| 2380 | u32 daint = readl(hsotg->regs + DAINT); |
| 2381 | u32 daint_out = daint >> DAINT_OutEP_SHIFT; |
| 2382 | u32 daint_in = daint & ~(daint_out << DAINT_OutEP_SHIFT); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2383 | int ep; |
| 2384 | |
| 2385 | dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint); |
| 2386 | |
| 2387 | for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) { |
| 2388 | if (daint_out & 1) |
| 2389 | s3c_hsotg_epint(hsotg, ep, 0); |
| 2390 | } |
| 2391 | |
| 2392 | for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) { |
| 2393 | if (daint_in & 1) |
| 2394 | s3c_hsotg_epint(hsotg, ep, 1); |
| 2395 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2396 | } |
| 2397 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2398 | if (gintsts & GINTSTS_USBRst) { |
Lukasz Majewski | 12a1f4d | 2012-05-04 14:17:08 +0200 | [diff] [blame] | 2399 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2400 | u32 usb_status = readl(hsotg->regs + GOTGCTL); |
Lukasz Majewski | 12a1f4d | 2012-05-04 14:17:08 +0200 | [diff] [blame] | 2401 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2402 | dev_info(hsotg->dev, "%s: USBRst\n", __func__); |
| 2403 | dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2404 | readl(hsotg->regs + GNPTXSTS)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2405 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2406 | writel(GINTSTS_USBRst, hsotg->regs + GINTSTS); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2407 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2408 | if (usb_status & GOTGCTL_BSESVLD) { |
Lukasz Majewski | 12a1f4d | 2012-05-04 14:17:08 +0200 | [diff] [blame] | 2409 | if (time_after(jiffies, hsotg->last_rst + |
| 2410 | msecs_to_jiffies(200))) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2411 | |
Lukasz Majewski | 12a1f4d | 2012-05-04 14:17:08 +0200 | [diff] [blame] | 2412 | kill_all_requests(hsotg, &hsotg->eps[0], |
| 2413 | -ECONNRESET, true); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2414 | |
Lukasz Majewski | 12a1f4d | 2012-05-04 14:17:08 +0200 | [diff] [blame] | 2415 | s3c_hsotg_core_init(hsotg); |
| 2416 | hsotg->last_rst = jiffies; |
| 2417 | } |
| 2418 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | /* check both FIFOs */ |
| 2422 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2423 | if (gintsts & GINTSTS_NPTxFEmp) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2424 | dev_dbg(hsotg->dev, "NPTxFEmp\n"); |
| 2425 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2426 | /* |
| 2427 | * Disable the interrupt to stop it happening again |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2428 | * unless one of these endpoint routines decides that |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2429 | * it needs re-enabling |
| 2430 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2431 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2432 | s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTxFEmp); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2433 | s3c_hsotg_irq_fifoempty(hsotg, false); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2434 | } |
| 2435 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2436 | if (gintsts & GINTSTS_PTxFEmp) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2437 | dev_dbg(hsotg->dev, "PTxFEmp\n"); |
| 2438 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2439 | /* See note in GINTSTS_NPTxFEmp */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2440 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2441 | s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTxFEmp); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2442 | s3c_hsotg_irq_fifoempty(hsotg, true); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2443 | } |
| 2444 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2445 | if (gintsts & GINTSTS_RxFLvl) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2446 | /* |
| 2447 | * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2448 | * we need to retry s3c_hsotg_handle_rx if this is still |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2449 | * set. |
| 2450 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2451 | |
| 2452 | s3c_hsotg_handle_rx(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2453 | } |
| 2454 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2455 | if (gintsts & GINTSTS_ModeMis) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2456 | dev_warn(hsotg->dev, "warning, mode mismatch triggered\n"); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2457 | writel(GINTSTS_ModeMis, hsotg->regs + GINTSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2458 | } |
| 2459 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2460 | if (gintsts & GINTSTS_USBSusp) { |
| 2461 | dev_info(hsotg->dev, "GINTSTS_USBSusp\n"); |
| 2462 | writel(GINTSTS_USBSusp, hsotg->regs + GINTSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2463 | |
| 2464 | call_gadget(hsotg, suspend); |
Lukasz Majewski | 12a1f4d | 2012-05-04 14:17:08 +0200 | [diff] [blame] | 2465 | s3c_hsotg_disconnect(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2466 | } |
| 2467 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2468 | if (gintsts & GINTSTS_WkUpInt) { |
| 2469 | dev_info(hsotg->dev, "GINTSTS_WkUpIn\n"); |
| 2470 | writel(GINTSTS_WkUpInt, hsotg->regs + GINTSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2471 | |
| 2472 | call_gadget(hsotg, resume); |
| 2473 | } |
| 2474 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2475 | if (gintsts & GINTSTS_ErlySusp) { |
| 2476 | dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n"); |
| 2477 | writel(GINTSTS_ErlySusp, hsotg->regs + GINTSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2478 | } |
| 2479 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2480 | /* |
| 2481 | * these next two seem to crop-up occasionally causing the core |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2482 | * to shutdown the USB transfer, so try clearing them and logging |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2483 | * the occurrence. |
| 2484 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2485 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2486 | if (gintsts & GINTSTS_GOUTNakEff) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2487 | dev_info(hsotg->dev, "GOUTNakEff triggered\n"); |
| 2488 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2489 | writel(DCTL_CGOUTNak, hsotg->regs + DCTL); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2490 | |
| 2491 | s3c_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2492 | } |
| 2493 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2494 | if (gintsts & GINTSTS_GINNakEff) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2495 | dev_info(hsotg->dev, "GINNakEff triggered\n"); |
| 2496 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2497 | writel(DCTL_CGNPInNAK, hsotg->regs + DCTL); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2498 | |
| 2499 | s3c_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2500 | } |
| 2501 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2502 | /* |
| 2503 | * if we've had fifo events, we should try and go around the |
| 2504 | * loop again to see if there's any point in returning yet. |
| 2505 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2506 | |
| 2507 | if (gintsts & IRQ_RETRY_MASK && --retry_count > 0) |
| 2508 | goto irq_retry; |
| 2509 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 2510 | spin_unlock(&hsotg->lock); |
| 2511 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2512 | return IRQ_HANDLED; |
| 2513 | } |
| 2514 | |
| 2515 | /** |
| 2516 | * s3c_hsotg_ep_enable - enable the given endpoint |
| 2517 | * @ep: The USB endpint to configure |
| 2518 | * @desc: The USB endpoint descriptor to configure with. |
| 2519 | * |
| 2520 | * This is called from the USB gadget code's usb_ep_enable(). |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2521 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2522 | static int s3c_hsotg_ep_enable(struct usb_ep *ep, |
| 2523 | const struct usb_endpoint_descriptor *desc) |
| 2524 | { |
| 2525 | struct s3c_hsotg_ep *hs_ep = our_ep(ep); |
| 2526 | struct s3c_hsotg *hsotg = hs_ep->parent; |
| 2527 | unsigned long flags; |
| 2528 | int index = hs_ep->index; |
| 2529 | u32 epctrl_reg; |
| 2530 | u32 epctrl; |
| 2531 | u32 mps; |
| 2532 | int dir_in; |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 2533 | int ret = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2534 | |
| 2535 | dev_dbg(hsotg->dev, |
| 2536 | "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n", |
| 2537 | __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes, |
| 2538 | desc->wMaxPacketSize, desc->bInterval); |
| 2539 | |
| 2540 | /* not to be called for EP0 */ |
| 2541 | WARN_ON(index == 0); |
| 2542 | |
| 2543 | dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0; |
| 2544 | if (dir_in != hs_ep->dir_in) { |
| 2545 | dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__); |
| 2546 | return -EINVAL; |
| 2547 | } |
| 2548 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 2549 | mps = usb_endpoint_maxp(desc); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2550 | |
| 2551 | /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */ |
| 2552 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2553 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2554 | epctrl = readl(hsotg->regs + epctrl_reg); |
| 2555 | |
| 2556 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n", |
| 2557 | __func__, epctrl, epctrl_reg); |
| 2558 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2559 | spin_lock_irqsave(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2560 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2561 | epctrl &= ~(DxEPCTL_EPType_MASK | DxEPCTL_MPS_MASK); |
| 2562 | epctrl |= DxEPCTL_MPS(mps); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2563 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2564 | /* |
| 2565 | * mark the endpoint as active, otherwise the core may ignore |
| 2566 | * transactions entirely for this endpoint |
| 2567 | */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2568 | epctrl |= DxEPCTL_USBActEp; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2569 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2570 | /* |
| 2571 | * set the NAK status on the endpoint, otherwise we might try and |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2572 | * do something with data that we've yet got a request to process |
| 2573 | * since the RXFIFO will take data for an endpoint even if the |
| 2574 | * size register hasn't been set. |
| 2575 | */ |
| 2576 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2577 | epctrl |= DxEPCTL_SNAK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2578 | |
| 2579 | /* update the endpoint state */ |
| 2580 | hs_ep->ep.maxpacket = mps; |
| 2581 | |
| 2582 | /* default, set to non-periodic */ |
| 2583 | hs_ep->periodic = 0; |
| 2584 | |
| 2585 | switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
| 2586 | case USB_ENDPOINT_XFER_ISOC: |
| 2587 | dev_err(hsotg->dev, "no current ISOC support\n"); |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 2588 | ret = -EINVAL; |
| 2589 | goto out; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2590 | |
| 2591 | case USB_ENDPOINT_XFER_BULK: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2592 | epctrl |= DxEPCTL_EPType_Bulk; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2593 | break; |
| 2594 | |
| 2595 | case USB_ENDPOINT_XFER_INT: |
| 2596 | if (dir_in) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2597 | /* |
| 2598 | * Allocate our TxFNum by simply using the index |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2599 | * of the endpoint for the moment. We could do |
| 2600 | * something better if the host indicates how |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2601 | * many FIFOs we are expecting to use. |
| 2602 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2603 | |
| 2604 | hs_ep->periodic = 1; |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2605 | epctrl |= DxEPCTL_TxFNum(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2606 | } |
| 2607 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2608 | epctrl |= DxEPCTL_EPType_Intterupt; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2609 | break; |
| 2610 | |
| 2611 | case USB_ENDPOINT_XFER_CONTROL: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2612 | epctrl |= DxEPCTL_EPType_Control; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2613 | break; |
| 2614 | } |
| 2615 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2616 | /* |
| 2617 | * if the hardware has dedicated fifos, we must give each IN EP |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 2618 | * a unique tx-fifo even if it is non-periodic. |
| 2619 | */ |
| 2620 | if (dir_in && hsotg->dedicated_fifos) |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2621 | epctrl |= DxEPCTL_TxFNum(index); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 2622 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2623 | /* for non control endpoints, set PID to D0 */ |
| 2624 | if (index) |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2625 | epctrl |= DxEPCTL_SetD0PID; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2626 | |
| 2627 | dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n", |
| 2628 | __func__, epctrl); |
| 2629 | |
| 2630 | writel(epctrl, hsotg->regs + epctrl_reg); |
| 2631 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n", |
| 2632 | __func__, readl(hsotg->regs + epctrl_reg)); |
| 2633 | |
| 2634 | /* enable the endpoint interrupt */ |
| 2635 | s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1); |
| 2636 | |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 2637 | out: |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2638 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 2639 | return ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2640 | } |
| 2641 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2642 | /** |
| 2643 | * s3c_hsotg_ep_disable - disable given endpoint |
| 2644 | * @ep: The endpoint to disable. |
| 2645 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2646 | static int s3c_hsotg_ep_disable(struct usb_ep *ep) |
| 2647 | { |
| 2648 | struct s3c_hsotg_ep *hs_ep = our_ep(ep); |
| 2649 | struct s3c_hsotg *hsotg = hs_ep->parent; |
| 2650 | int dir_in = hs_ep->dir_in; |
| 2651 | int index = hs_ep->index; |
| 2652 | unsigned long flags; |
| 2653 | u32 epctrl_reg; |
| 2654 | u32 ctrl; |
| 2655 | |
| 2656 | dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep); |
| 2657 | |
| 2658 | if (ep == &hsotg->eps[0].ep) { |
| 2659 | dev_err(hsotg->dev, "%s: called for ep0\n", __func__); |
| 2660 | return -EINVAL; |
| 2661 | } |
| 2662 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2663 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2664 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 2665 | spin_lock_irqsave(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2666 | /* terminate all requests with shutdown */ |
| 2667 | kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false); |
| 2668 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2669 | |
| 2670 | ctrl = readl(hsotg->regs + epctrl_reg); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2671 | ctrl &= ~DxEPCTL_EPEna; |
| 2672 | ctrl &= ~DxEPCTL_USBActEp; |
| 2673 | ctrl |= DxEPCTL_SNAK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2674 | |
| 2675 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
| 2676 | writel(ctrl, hsotg->regs + epctrl_reg); |
| 2677 | |
| 2678 | /* disable endpoint interrupts */ |
| 2679 | s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0); |
| 2680 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2681 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2682 | return 0; |
| 2683 | } |
| 2684 | |
| 2685 | /** |
| 2686 | * on_list - check request is on the given endpoint |
| 2687 | * @ep: The endpoint to check. |
| 2688 | * @test: The request to test if it is on the endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2689 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2690 | static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test) |
| 2691 | { |
| 2692 | struct s3c_hsotg_req *req, *treq; |
| 2693 | |
| 2694 | list_for_each_entry_safe(req, treq, &ep->queue, queue) { |
| 2695 | if (req == test) |
| 2696 | return true; |
| 2697 | } |
| 2698 | |
| 2699 | return false; |
| 2700 | } |
| 2701 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2702 | /** |
| 2703 | * s3c_hsotg_ep_dequeue - dequeue given endpoint |
| 2704 | * @ep: The endpoint to dequeue. |
| 2705 | * @req: The request to be removed from a queue. |
| 2706 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2707 | static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req) |
| 2708 | { |
| 2709 | struct s3c_hsotg_req *hs_req = our_req(req); |
| 2710 | struct s3c_hsotg_ep *hs_ep = our_ep(ep); |
| 2711 | struct s3c_hsotg *hs = hs_ep->parent; |
| 2712 | unsigned long flags; |
| 2713 | |
| 2714 | dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req); |
| 2715 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2716 | spin_lock_irqsave(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2717 | |
| 2718 | if (!on_list(hs_ep, hs_req)) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2719 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2720 | return -EINVAL; |
| 2721 | } |
| 2722 | |
| 2723 | s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2724 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2725 | |
| 2726 | return 0; |
| 2727 | } |
| 2728 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2729 | /** |
| 2730 | * s3c_hsotg_ep_sethalt - set halt on a given endpoint |
| 2731 | * @ep: The endpoint to set halt. |
| 2732 | * @value: Set or unset the halt. |
| 2733 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2734 | static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value) |
| 2735 | { |
| 2736 | struct s3c_hsotg_ep *hs_ep = our_ep(ep); |
| 2737 | struct s3c_hsotg *hs = hs_ep->parent; |
| 2738 | int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2739 | u32 epreg; |
| 2740 | u32 epctl; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2741 | u32 xfertype; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2742 | |
| 2743 | dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value); |
| 2744 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2745 | /* write both IN and OUT control registers */ |
| 2746 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2747 | epreg = DIEPCTL(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2748 | epctl = readl(hs->regs + epreg); |
| 2749 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2750 | if (value) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2751 | epctl |= DxEPCTL_Stall + DxEPCTL_SNAK; |
| 2752 | if (epctl & DxEPCTL_EPEna) |
| 2753 | epctl |= DxEPCTL_EPDis; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2754 | } else { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2755 | epctl &= ~DxEPCTL_Stall; |
| 2756 | xfertype = epctl & DxEPCTL_EPType_MASK; |
| 2757 | if (xfertype == DxEPCTL_EPType_Bulk || |
| 2758 | xfertype == DxEPCTL_EPType_Intterupt) |
| 2759 | epctl |= DxEPCTL_SetD0PID; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2760 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2761 | |
| 2762 | writel(epctl, hs->regs + epreg); |
| 2763 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2764 | epreg = DOEPCTL(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2765 | epctl = readl(hs->regs + epreg); |
| 2766 | |
| 2767 | if (value) |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2768 | epctl |= DxEPCTL_Stall; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2769 | else { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2770 | epctl &= ~DxEPCTL_Stall; |
| 2771 | xfertype = epctl & DxEPCTL_EPType_MASK; |
| 2772 | if (xfertype == DxEPCTL_EPType_Bulk || |
| 2773 | xfertype == DxEPCTL_EPType_Intterupt) |
| 2774 | epctl |= DxEPCTL_SetD0PID; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2775 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2776 | |
| 2777 | writel(epctl, hs->regs + epreg); |
| 2778 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2779 | return 0; |
| 2780 | } |
| 2781 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 2782 | /** |
| 2783 | * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held |
| 2784 | * @ep: The endpoint to set halt. |
| 2785 | * @value: Set or unset the halt. |
| 2786 | */ |
| 2787 | static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value) |
| 2788 | { |
| 2789 | struct s3c_hsotg_ep *hs_ep = our_ep(ep); |
| 2790 | struct s3c_hsotg *hs = hs_ep->parent; |
| 2791 | unsigned long flags = 0; |
| 2792 | int ret = 0; |
| 2793 | |
| 2794 | spin_lock_irqsave(&hs->lock, flags); |
| 2795 | ret = s3c_hsotg_ep_sethalt(ep, value); |
| 2796 | spin_unlock_irqrestore(&hs->lock, flags); |
| 2797 | |
| 2798 | return ret; |
| 2799 | } |
| 2800 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2801 | static struct usb_ep_ops s3c_hsotg_ep_ops = { |
| 2802 | .enable = s3c_hsotg_ep_enable, |
| 2803 | .disable = s3c_hsotg_ep_disable, |
| 2804 | .alloc_request = s3c_hsotg_ep_alloc_request, |
| 2805 | .free_request = s3c_hsotg_ep_free_request, |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 2806 | .queue = s3c_hsotg_ep_queue_lock, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2807 | .dequeue = s3c_hsotg_ep_dequeue, |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 2808 | .set_halt = s3c_hsotg_ep_sethalt_lock, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2809 | /* note, don't believe we have any call for the fifo routines */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2810 | }; |
| 2811 | |
| 2812 | /** |
Lukasz Majewski | 4118878 | 2012-05-04 14:17:01 +0200 | [diff] [blame] | 2813 | * s3c_hsotg_phy_enable - enable platform phy dev |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2814 | * @hsotg: The driver state |
Lukasz Majewski | 4118878 | 2012-05-04 14:17:01 +0200 | [diff] [blame] | 2815 | * |
| 2816 | * A wrapper for platform code responsible for controlling |
| 2817 | * low-level USB code |
| 2818 | */ |
| 2819 | static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg) |
| 2820 | { |
| 2821 | struct platform_device *pdev = to_platform_device(hsotg->dev); |
| 2822 | |
| 2823 | dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev); |
Praveen Paneri | b2e587d | 2012-11-14 15:57:16 +0530 | [diff] [blame] | 2824 | |
| 2825 | if (hsotg->phy) |
| 2826 | usb_phy_init(hsotg->phy); |
| 2827 | else if (hsotg->plat->phy_init) |
Lukasz Majewski | 4118878 | 2012-05-04 14:17:01 +0200 | [diff] [blame] | 2828 | hsotg->plat->phy_init(pdev, hsotg->plat->phy_type); |
| 2829 | } |
| 2830 | |
| 2831 | /** |
| 2832 | * s3c_hsotg_phy_disable - disable platform phy dev |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2833 | * @hsotg: The driver state |
Lukasz Majewski | 4118878 | 2012-05-04 14:17:01 +0200 | [diff] [blame] | 2834 | * |
| 2835 | * A wrapper for platform code responsible for controlling |
| 2836 | * low-level USB code |
| 2837 | */ |
| 2838 | static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg) |
| 2839 | { |
| 2840 | struct platform_device *pdev = to_platform_device(hsotg->dev); |
| 2841 | |
Praveen Paneri | b2e587d | 2012-11-14 15:57:16 +0530 | [diff] [blame] | 2842 | if (hsotg->phy) |
| 2843 | usb_phy_shutdown(hsotg->phy); |
| 2844 | else if (hsotg->plat->phy_exit) |
Lukasz Majewski | 4118878 | 2012-05-04 14:17:01 +0200 | [diff] [blame] | 2845 | hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type); |
| 2846 | } |
| 2847 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2848 | /** |
| 2849 | * s3c_hsotg_init - initalize the usb core |
| 2850 | * @hsotg: The driver state |
| 2851 | */ |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2852 | static void s3c_hsotg_init(struct s3c_hsotg *hsotg) |
| 2853 | { |
| 2854 | /* unmask subset of endpoint interrupts */ |
| 2855 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2856 | writel(DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk | |
| 2857 | DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk, |
| 2858 | hsotg->regs + DIEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2859 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2860 | writel(DOEPMSK_SetupMsk | DOEPMSK_AHBErrMsk | |
| 2861 | DOEPMSK_EPDisbldMsk | DOEPMSK_XferComplMsk, |
| 2862 | hsotg->regs + DOEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2863 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2864 | writel(0, hsotg->regs + DAINTMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2865 | |
| 2866 | /* Be in disconnected state until gadget is registered */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2867 | __orr32(hsotg->regs + DCTL, DCTL_SftDiscon); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2868 | |
| 2869 | if (0) { |
| 2870 | /* post global nak until we're ready */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2871 | writel(DCTL_SGNPInNAK | DCTL_SGOUTNak, |
| 2872 | hsotg->regs + DCTL); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | /* setup fifos */ |
| 2876 | |
| 2877 | dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2878 | readl(hsotg->regs + GRXFSIZ), |
| 2879 | readl(hsotg->regs + GNPTXFSIZ)); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2880 | |
| 2881 | s3c_hsotg_init_fifo(hsotg); |
| 2882 | |
| 2883 | /* set the PLL on, remove the HNP/SRP and set the PHY */ |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2884 | writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) | (0x5 << 10), |
| 2885 | hsotg->regs + GUSBCFG); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2886 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2887 | writel(using_dma(hsotg) ? GAHBCFG_DMAEn : 0x0, |
| 2888 | hsotg->regs + GAHBCFG); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2889 | } |
| 2890 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2891 | /** |
| 2892 | * s3c_hsotg_udc_start - prepare the udc for work |
| 2893 | * @gadget: The usb gadget state |
| 2894 | * @driver: The usb gadget driver |
| 2895 | * |
| 2896 | * Perform initialization to prepare udc device and driver |
| 2897 | * to work. |
| 2898 | */ |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 2899 | static int s3c_hsotg_udc_start(struct usb_gadget *gadget, |
| 2900 | struct usb_gadget_driver *driver) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2901 | { |
Lukasz Majewski | f99b2bf | 2012-05-04 14:17:12 +0200 | [diff] [blame] | 2902 | struct s3c_hsotg *hsotg = to_hsotg(gadget); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2903 | int ret; |
| 2904 | |
| 2905 | if (!hsotg) { |
| 2906 | printk(KERN_ERR "%s: called with no device\n", __func__); |
| 2907 | return -ENODEV; |
| 2908 | } |
| 2909 | |
| 2910 | if (!driver) { |
| 2911 | dev_err(hsotg->dev, "%s: no driver\n", __func__); |
| 2912 | return -EINVAL; |
| 2913 | } |
| 2914 | |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 2915 | if (driver->max_speed < USB_SPEED_FULL) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2916 | dev_err(hsotg->dev, "%s: bad speed\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2917 | |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 2918 | if (!driver->setup) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2919 | dev_err(hsotg->dev, "%s: missing entry points\n", __func__); |
| 2920 | return -EINVAL; |
| 2921 | } |
| 2922 | |
| 2923 | WARN_ON(hsotg->driver); |
| 2924 | |
| 2925 | driver->driver.bus = NULL; |
| 2926 | hsotg->driver = driver; |
Alexandre Pereira da Silva | 7d7b229 | 2012-06-26 11:27:10 -0300 | [diff] [blame] | 2927 | hsotg->gadget.dev.of_node = hsotg->dev->of_node; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2928 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 2929 | |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 2930 | ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies), |
| 2931 | hsotg->supplies); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2932 | if (ret) { |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 2933 | dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2934 | goto err; |
| 2935 | } |
| 2936 | |
Lukasz Majewski | 12a1f4d | 2012-05-04 14:17:08 +0200 | [diff] [blame] | 2937 | hsotg->last_rst = jiffies; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2938 | dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name); |
| 2939 | return 0; |
| 2940 | |
| 2941 | err: |
| 2942 | hsotg->driver = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2943 | return ret; |
| 2944 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2945 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2946 | /** |
| 2947 | * s3c_hsotg_udc_stop - stop the udc |
| 2948 | * @gadget: The usb gadget state |
| 2949 | * @driver: The usb gadget driver |
| 2950 | * |
| 2951 | * Stop udc hw block and stay tunned for future transmissions |
| 2952 | */ |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 2953 | static int s3c_hsotg_udc_stop(struct usb_gadget *gadget, |
| 2954 | struct usb_gadget_driver *driver) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2955 | { |
Lukasz Majewski | f99b2bf | 2012-05-04 14:17:12 +0200 | [diff] [blame] | 2956 | struct s3c_hsotg *hsotg = to_hsotg(gadget); |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 2957 | unsigned long flags = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2958 | int ep; |
| 2959 | |
| 2960 | if (!hsotg) |
| 2961 | return -ENODEV; |
| 2962 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2963 | /* all endpoints should be shutdown */ |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2964 | for (ep = 0; ep < hsotg->num_of_eps; ep++) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2965 | s3c_hsotg_ep_disable(&hsotg->eps[ep].ep); |
| 2966 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 2967 | spin_lock_irqsave(&hsotg->lock, flags); |
| 2968 | |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 2969 | s3c_hsotg_phy_disable(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2970 | |
Marek Szyprowski | c8c1025 | 2013-09-12 16:18:48 +0200 | [diff] [blame] | 2971 | if (!driver) |
| 2972 | hsotg->driver = NULL; |
| 2973 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2974 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2975 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 2976 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 2977 | |
Marek Szyprowski | c8c1025 | 2013-09-12 16:18:48 +0200 | [diff] [blame] | 2978 | regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2979 | |
| 2980 | return 0; |
| 2981 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2982 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2983 | /** |
| 2984 | * s3c_hsotg_gadget_getframe - read the frame number |
| 2985 | * @gadget: The usb gadget state |
| 2986 | * |
| 2987 | * Read the {micro} frame number |
| 2988 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2989 | static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget) |
| 2990 | { |
| 2991 | return s3c_hsotg_read_frameno(to_hsotg(gadget)); |
| 2992 | } |
| 2993 | |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 2994 | /** |
| 2995 | * s3c_hsotg_pullup - connect/disconnect the USB PHY |
| 2996 | * @gadget: The usb gadget state |
| 2997 | * @is_on: Current state of the USB PHY |
| 2998 | * |
| 2999 | * Connect/Disconnect the USB PHY pullup |
| 3000 | */ |
| 3001 | static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on) |
| 3002 | { |
| 3003 | struct s3c_hsotg *hsotg = to_hsotg(gadget); |
| 3004 | unsigned long flags = 0; |
| 3005 | |
| 3006 | dev_dbg(hsotg->dev, "%s: is_in: %d\n", __func__, is_on); |
| 3007 | |
| 3008 | spin_lock_irqsave(&hsotg->lock, flags); |
| 3009 | if (is_on) { |
| 3010 | s3c_hsotg_phy_enable(hsotg); |
| 3011 | s3c_hsotg_core_init(hsotg); |
| 3012 | } else { |
| 3013 | s3c_hsotg_disconnect(hsotg); |
| 3014 | s3c_hsotg_phy_disable(hsotg); |
| 3015 | } |
| 3016 | |
| 3017 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 3018 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 3019 | |
| 3020 | return 0; |
| 3021 | } |
| 3022 | |
Felipe Balbi | eeef458 | 2013-01-24 17:58:16 +0200 | [diff] [blame] | 3023 | static const struct usb_gadget_ops s3c_hsotg_gadget_ops = { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3024 | .get_frame = s3c_hsotg_gadget_getframe, |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 3025 | .udc_start = s3c_hsotg_udc_start, |
| 3026 | .udc_stop = s3c_hsotg_udc_stop, |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 3027 | .pullup = s3c_hsotg_pullup, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3028 | }; |
| 3029 | |
| 3030 | /** |
| 3031 | * s3c_hsotg_initep - initialise a single endpoint |
| 3032 | * @hsotg: The device state. |
| 3033 | * @hs_ep: The endpoint to be initialised. |
| 3034 | * @epnum: The endpoint number |
| 3035 | * |
| 3036 | * Initialise the given endpoint (as part of the probe and device state |
| 3037 | * creation) to give to the gadget driver. Setup the endpoint name, any |
| 3038 | * direction information and other state that may be required. |
| 3039 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 3040 | static void s3c_hsotg_initep(struct s3c_hsotg *hsotg, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3041 | struct s3c_hsotg_ep *hs_ep, |
| 3042 | int epnum) |
| 3043 | { |
| 3044 | u32 ptxfifo; |
| 3045 | char *dir; |
| 3046 | |
| 3047 | if (epnum == 0) |
| 3048 | dir = ""; |
| 3049 | else if ((epnum % 2) == 0) { |
| 3050 | dir = "out"; |
| 3051 | } else { |
| 3052 | dir = "in"; |
| 3053 | hs_ep->dir_in = 1; |
| 3054 | } |
| 3055 | |
| 3056 | hs_ep->index = epnum; |
| 3057 | |
| 3058 | snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir); |
| 3059 | |
| 3060 | INIT_LIST_HEAD(&hs_ep->queue); |
| 3061 | INIT_LIST_HEAD(&hs_ep->ep.ep_list); |
| 3062 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3063 | /* add to the list of endpoints known by the gadget driver */ |
| 3064 | if (epnum) |
| 3065 | list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list); |
| 3066 | |
| 3067 | hs_ep->parent = hsotg; |
| 3068 | hs_ep->ep.name = hs_ep->name; |
| 3069 | hs_ep->ep.maxpacket = epnum ? 512 : EP0_MPS_LIMIT; |
| 3070 | hs_ep->ep.ops = &s3c_hsotg_ep_ops; |
| 3071 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3072 | /* |
| 3073 | * Read the FIFO size for the Periodic TX FIFO, even if we're |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3074 | * an OUT endpoint, we may as well do this if in future the |
| 3075 | * code is changed to make each endpoint's direction changeable. |
| 3076 | */ |
| 3077 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3078 | ptxfifo = readl(hsotg->regs + DPTXFSIZn(epnum)); |
| 3079 | hs_ep->fifo_size = DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3080 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3081 | /* |
| 3082 | * if we're using dma, we need to set the next-endpoint pointer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3083 | * to be something valid. |
| 3084 | */ |
| 3085 | |
| 3086 | if (using_dma(hsotg)) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3087 | u32 next = DxEPCTL_NextEp((epnum + 1) % 15); |
| 3088 | writel(next, hsotg->regs + DIEPCTL(epnum)); |
| 3089 | writel(next, hsotg->regs + DOEPCTL(epnum)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3090 | } |
| 3091 | } |
| 3092 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3093 | /** |
| 3094 | * s3c_hsotg_hw_cfg - read HW configuration registers |
| 3095 | * @param: The device state |
| 3096 | * |
| 3097 | * Read the USB core HW configuration registers |
| 3098 | */ |
| 3099 | static void s3c_hsotg_hw_cfg(struct s3c_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3100 | { |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3101 | u32 cfg2, cfg4; |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3102 | /* check hardware configuration */ |
| 3103 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3104 | cfg2 = readl(hsotg->regs + 0x48); |
| 3105 | hsotg->num_of_eps = (cfg2 >> 10) & 0xF; |
| 3106 | |
| 3107 | dev_info(hsotg->dev, "EPs:%d\n", hsotg->num_of_eps); |
| 3108 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3109 | cfg4 = readl(hsotg->regs + 0x50); |
| 3110 | hsotg->dedicated_fifos = (cfg4 >> 25) & 1; |
| 3111 | |
| 3112 | dev_info(hsotg->dev, "%s fifos\n", |
| 3113 | hsotg->dedicated_fifos ? "dedicated" : "shared"); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3114 | } |
| 3115 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3116 | /** |
| 3117 | * s3c_hsotg_dump - dump state of the udc |
| 3118 | * @param: The device state |
| 3119 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3120 | static void s3c_hsotg_dump(struct s3c_hsotg *hsotg) |
| 3121 | { |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 3122 | #ifdef DEBUG |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3123 | struct device *dev = hsotg->dev; |
| 3124 | void __iomem *regs = hsotg->regs; |
| 3125 | u32 val; |
| 3126 | int idx; |
| 3127 | |
| 3128 | dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3129 | readl(regs + DCFG), readl(regs + DCTL), |
| 3130 | readl(regs + DIEPMSK)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3131 | |
| 3132 | dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3133 | readl(regs + GAHBCFG), readl(regs + 0x44)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3134 | |
| 3135 | dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3136 | readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3137 | |
| 3138 | /* show periodic fifo settings */ |
| 3139 | |
| 3140 | for (idx = 1; idx <= 15; idx++) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3141 | val = readl(regs + DPTXFSIZn(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3142 | dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx, |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3143 | val >> DPTXFSIZn_DPTxFSize_SHIFT, |
| 3144 | val & DPTXFSIZn_DPTxFStAddr_MASK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | for (idx = 0; idx < 15; idx++) { |
| 3148 | dev_info(dev, |
| 3149 | "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx, |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3150 | readl(regs + DIEPCTL(idx)), |
| 3151 | readl(regs + DIEPTSIZ(idx)), |
| 3152 | readl(regs + DIEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3153 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3154 | val = readl(regs + DOEPCTL(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3155 | dev_info(dev, |
| 3156 | "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3157 | idx, readl(regs + DOEPCTL(idx)), |
| 3158 | readl(regs + DOEPTSIZ(idx)), |
| 3159 | readl(regs + DOEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3160 | |
| 3161 | } |
| 3162 | |
| 3163 | dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3164 | readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE)); |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 3165 | #endif |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3166 | } |
| 3167 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3168 | /** |
| 3169 | * state_show - debugfs: show overall driver and device state. |
| 3170 | * @seq: The seq file to write to. |
| 3171 | * @v: Unused parameter. |
| 3172 | * |
| 3173 | * This debugfs entry shows the overall state of the hardware and |
| 3174 | * some general information about each of the endpoints available |
| 3175 | * to the system. |
| 3176 | */ |
| 3177 | static int state_show(struct seq_file *seq, void *v) |
| 3178 | { |
| 3179 | struct s3c_hsotg *hsotg = seq->private; |
| 3180 | void __iomem *regs = hsotg->regs; |
| 3181 | int idx; |
| 3182 | |
| 3183 | seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3184 | readl(regs + DCFG), |
| 3185 | readl(regs + DCTL), |
| 3186 | readl(regs + DSTS)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3187 | |
| 3188 | seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3189 | readl(regs + DIEPMSK), readl(regs + DOEPMSK)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3190 | |
| 3191 | seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3192 | readl(regs + GINTMSK), |
| 3193 | readl(regs + GINTSTS)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3194 | |
| 3195 | seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3196 | readl(regs + DAINTMSK), |
| 3197 | readl(regs + DAINT)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3198 | |
| 3199 | seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3200 | readl(regs + GNPTXSTS), |
| 3201 | readl(regs + GRXSTSR)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3202 | |
| 3203 | seq_printf(seq, "\nEndpoint status:\n"); |
| 3204 | |
| 3205 | for (idx = 0; idx < 15; idx++) { |
| 3206 | u32 in, out; |
| 3207 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3208 | in = readl(regs + DIEPCTL(idx)); |
| 3209 | out = readl(regs + DOEPCTL(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3210 | |
| 3211 | seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x", |
| 3212 | idx, in, out); |
| 3213 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3214 | in = readl(regs + DIEPTSIZ(idx)); |
| 3215 | out = readl(regs + DOEPTSIZ(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3216 | |
| 3217 | seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x", |
| 3218 | in, out); |
| 3219 | |
| 3220 | seq_printf(seq, "\n"); |
| 3221 | } |
| 3222 | |
| 3223 | return 0; |
| 3224 | } |
| 3225 | |
| 3226 | static int state_open(struct inode *inode, struct file *file) |
| 3227 | { |
| 3228 | return single_open(file, state_show, inode->i_private); |
| 3229 | } |
| 3230 | |
| 3231 | static const struct file_operations state_fops = { |
| 3232 | .owner = THIS_MODULE, |
| 3233 | .open = state_open, |
| 3234 | .read = seq_read, |
| 3235 | .llseek = seq_lseek, |
| 3236 | .release = single_release, |
| 3237 | }; |
| 3238 | |
| 3239 | /** |
| 3240 | * fifo_show - debugfs: show the fifo information |
| 3241 | * @seq: The seq_file to write data to. |
| 3242 | * @v: Unused parameter. |
| 3243 | * |
| 3244 | * Show the FIFO information for the overall fifo and all the |
| 3245 | * periodic transmission FIFOs. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3246 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3247 | static int fifo_show(struct seq_file *seq, void *v) |
| 3248 | { |
| 3249 | struct s3c_hsotg *hsotg = seq->private; |
| 3250 | void __iomem *regs = hsotg->regs; |
| 3251 | u32 val; |
| 3252 | int idx; |
| 3253 | |
| 3254 | seq_printf(seq, "Non-periodic FIFOs:\n"); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3255 | seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3256 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3257 | val = readl(regs + GNPTXFSIZ); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3258 | seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3259 | val >> GNPTXFSIZ_NPTxFDep_SHIFT, |
| 3260 | val & GNPTXFSIZ_NPTxFStAddr_MASK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3261 | |
| 3262 | seq_printf(seq, "\nPeriodic TXFIFOs:\n"); |
| 3263 | |
| 3264 | for (idx = 1; idx <= 15; idx++) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3265 | val = readl(regs + DPTXFSIZn(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3266 | |
| 3267 | seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx, |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3268 | val >> DPTXFSIZn_DPTxFSize_SHIFT, |
| 3269 | val & DPTXFSIZn_DPTxFStAddr_MASK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | return 0; |
| 3273 | } |
| 3274 | |
| 3275 | static int fifo_open(struct inode *inode, struct file *file) |
| 3276 | { |
| 3277 | return single_open(file, fifo_show, inode->i_private); |
| 3278 | } |
| 3279 | |
| 3280 | static const struct file_operations fifo_fops = { |
| 3281 | .owner = THIS_MODULE, |
| 3282 | .open = fifo_open, |
| 3283 | .read = seq_read, |
| 3284 | .llseek = seq_lseek, |
| 3285 | .release = single_release, |
| 3286 | }; |
| 3287 | |
| 3288 | |
| 3289 | static const char *decode_direction(int is_in) |
| 3290 | { |
| 3291 | return is_in ? "in" : "out"; |
| 3292 | } |
| 3293 | |
| 3294 | /** |
| 3295 | * ep_show - debugfs: show the state of an endpoint. |
| 3296 | * @seq: The seq_file to write data to. |
| 3297 | * @v: Unused parameter. |
| 3298 | * |
| 3299 | * This debugfs entry shows the state of the given endpoint (one is |
| 3300 | * registered for each available). |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3301 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3302 | static int ep_show(struct seq_file *seq, void *v) |
| 3303 | { |
| 3304 | struct s3c_hsotg_ep *ep = seq->private; |
| 3305 | struct s3c_hsotg *hsotg = ep->parent; |
| 3306 | struct s3c_hsotg_req *req; |
| 3307 | void __iomem *regs = hsotg->regs; |
| 3308 | int index = ep->index; |
| 3309 | int show_limit = 15; |
| 3310 | unsigned long flags; |
| 3311 | |
| 3312 | seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n", |
| 3313 | ep->index, ep->ep.name, decode_direction(ep->dir_in)); |
| 3314 | |
| 3315 | /* first show the register state */ |
| 3316 | |
| 3317 | seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3318 | readl(regs + DIEPCTL(index)), |
| 3319 | readl(regs + DOEPCTL(index))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3320 | |
| 3321 | seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3322 | readl(regs + DIEPDMA(index)), |
| 3323 | readl(regs + DOEPDMA(index))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3324 | |
| 3325 | seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3326 | readl(regs + DIEPINT(index)), |
| 3327 | readl(regs + DOEPINT(index))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3328 | |
| 3329 | seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n", |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3330 | readl(regs + DIEPTSIZ(index)), |
| 3331 | readl(regs + DOEPTSIZ(index))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3332 | |
| 3333 | seq_printf(seq, "\n"); |
| 3334 | seq_printf(seq, "mps %d\n", ep->ep.maxpacket); |
| 3335 | seq_printf(seq, "total_data=%ld\n", ep->total_data); |
| 3336 | |
| 3337 | seq_printf(seq, "request list (%p,%p):\n", |
| 3338 | ep->queue.next, ep->queue.prev); |
| 3339 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 3340 | spin_lock_irqsave(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3341 | |
| 3342 | list_for_each_entry(req, &ep->queue, queue) { |
| 3343 | if (--show_limit < 0) { |
| 3344 | seq_printf(seq, "not showing more requests...\n"); |
| 3345 | break; |
| 3346 | } |
| 3347 | |
| 3348 | seq_printf(seq, "%c req %p: %d bytes @%p, ", |
| 3349 | req == ep->req ? '*' : ' ', |
| 3350 | req, req->req.length, req->req.buf); |
| 3351 | seq_printf(seq, "%d done, res %d\n", |
| 3352 | req->req.actual, req->req.status); |
| 3353 | } |
| 3354 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 3355 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3356 | |
| 3357 | return 0; |
| 3358 | } |
| 3359 | |
| 3360 | static int ep_open(struct inode *inode, struct file *file) |
| 3361 | { |
| 3362 | return single_open(file, ep_show, inode->i_private); |
| 3363 | } |
| 3364 | |
| 3365 | static const struct file_operations ep_fops = { |
| 3366 | .owner = THIS_MODULE, |
| 3367 | .open = ep_open, |
| 3368 | .read = seq_read, |
| 3369 | .llseek = seq_lseek, |
| 3370 | .release = single_release, |
| 3371 | }; |
| 3372 | |
| 3373 | /** |
| 3374 | * s3c_hsotg_create_debug - create debugfs directory and files |
| 3375 | * @hsotg: The driver state |
| 3376 | * |
| 3377 | * Create the debugfs files to allow the user to get information |
| 3378 | * about the state of the system. The directory name is created |
| 3379 | * with the same name as the device itself, in case we end up |
| 3380 | * with multiple blocks in future systems. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3381 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 3382 | static void s3c_hsotg_create_debug(struct s3c_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3383 | { |
| 3384 | struct dentry *root; |
| 3385 | unsigned epidx; |
| 3386 | |
| 3387 | root = debugfs_create_dir(dev_name(hsotg->dev), NULL); |
| 3388 | hsotg->debug_root = root; |
| 3389 | if (IS_ERR(root)) { |
| 3390 | dev_err(hsotg->dev, "cannot create debug root\n"); |
| 3391 | return; |
| 3392 | } |
| 3393 | |
| 3394 | /* create general state file */ |
| 3395 | |
| 3396 | hsotg->debug_file = debugfs_create_file("state", 0444, root, |
| 3397 | hsotg, &state_fops); |
| 3398 | |
| 3399 | if (IS_ERR(hsotg->debug_file)) |
| 3400 | dev_err(hsotg->dev, "%s: failed to create state\n", __func__); |
| 3401 | |
| 3402 | hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root, |
| 3403 | hsotg, &fifo_fops); |
| 3404 | |
| 3405 | if (IS_ERR(hsotg->debug_fifo)) |
| 3406 | dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__); |
| 3407 | |
| 3408 | /* create one file for each endpoint */ |
| 3409 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3410 | for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3411 | struct s3c_hsotg_ep *ep = &hsotg->eps[epidx]; |
| 3412 | |
| 3413 | ep->debugfs = debugfs_create_file(ep->name, 0444, |
| 3414 | root, ep, &ep_fops); |
| 3415 | |
| 3416 | if (IS_ERR(ep->debugfs)) |
| 3417 | dev_err(hsotg->dev, "failed to create %s debug file\n", |
| 3418 | ep->name); |
| 3419 | } |
| 3420 | } |
| 3421 | |
| 3422 | /** |
| 3423 | * s3c_hsotg_delete_debug - cleanup debugfs entries |
| 3424 | * @hsotg: The driver state |
| 3425 | * |
| 3426 | * Cleanup (remove) the debugfs files for use on module exit. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3427 | */ |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 3428 | static void s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3429 | { |
| 3430 | unsigned epidx; |
| 3431 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3432 | for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3433 | struct s3c_hsotg_ep *ep = &hsotg->eps[epidx]; |
| 3434 | debugfs_remove(ep->debugfs); |
| 3435 | } |
| 3436 | |
| 3437 | debugfs_remove(hsotg->debug_file); |
| 3438 | debugfs_remove(hsotg->debug_fifo); |
| 3439 | debugfs_remove(hsotg->debug_root); |
| 3440 | } |
| 3441 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3442 | /** |
| 3443 | * s3c_hsotg_probe - probe function for hsotg driver |
| 3444 | * @pdev: The platform information for the driver |
| 3445 | */ |
Lukasz Majewski | f026a52 | 2012-05-04 14:17:13 +0200 | [diff] [blame] | 3446 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 3447 | static int s3c_hsotg_probe(struct platform_device *pdev) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3448 | { |
Jingoo Han | e01ee9f | 2013-07-30 17:00:51 +0900 | [diff] [blame] | 3449 | struct s3c_hsotg_plat *plat = dev_get_platdata(&pdev->dev); |
Praveen Paneri | b2e587d | 2012-11-14 15:57:16 +0530 | [diff] [blame] | 3450 | struct usb_phy *phy; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3451 | struct device *dev = &pdev->dev; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3452 | struct s3c_hsotg_ep *eps; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3453 | struct s3c_hsotg *hsotg; |
| 3454 | struct resource *res; |
| 3455 | int epnum; |
| 3456 | int ret; |
Lukasz Majewski | fc9a731 | 2012-05-04 14:17:02 +0200 | [diff] [blame] | 3457 | int i; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3458 | |
Sachin Kamat | 338edab | 2012-05-18 14:33:46 +0530 | [diff] [blame] | 3459 | hsotg = devm_kzalloc(&pdev->dev, sizeof(struct s3c_hsotg), GFP_KERNEL); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3460 | if (!hsotg) { |
| 3461 | dev_err(dev, "cannot get memory\n"); |
| 3462 | return -ENOMEM; |
| 3463 | } |
| 3464 | |
Praveen Paneri | b2e587d | 2012-11-14 15:57:16 +0530 | [diff] [blame] | 3465 | phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); |
Felipe Balbi | f4f5ba5 | 2013-03-15 10:56:19 +0200 | [diff] [blame] | 3466 | if (IS_ERR(phy)) { |
Praveen Paneri | b2e587d | 2012-11-14 15:57:16 +0530 | [diff] [blame] | 3467 | /* Fallback for pdata */ |
Jingoo Han | e01ee9f | 2013-07-30 17:00:51 +0900 | [diff] [blame] | 3468 | plat = dev_get_platdata(&pdev->dev); |
Praveen Paneri | b2e587d | 2012-11-14 15:57:16 +0530 | [diff] [blame] | 3469 | if (!plat) { |
| 3470 | dev_err(&pdev->dev, "no platform data or transceiver defined\n"); |
| 3471 | return -EPROBE_DEFER; |
| 3472 | } else { |
| 3473 | hsotg->plat = plat; |
| 3474 | } |
| 3475 | } else { |
| 3476 | hsotg->phy = phy; |
| 3477 | } |
| 3478 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3479 | hsotg->dev = dev; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3480 | |
Sachin Kamat | 84749c6 | 2012-09-03 16:15:18 +0530 | [diff] [blame] | 3481 | hsotg->clk = devm_clk_get(&pdev->dev, "otg"); |
Marek Szyprowski | 31ee04d | 2010-07-19 16:01:42 +0200 | [diff] [blame] | 3482 | if (IS_ERR(hsotg->clk)) { |
| 3483 | dev_err(dev, "cannot get otg clock\n"); |
Sachin Kamat | 338edab | 2012-05-18 14:33:46 +0530 | [diff] [blame] | 3484 | return PTR_ERR(hsotg->clk); |
Marek Szyprowski | 31ee04d | 2010-07-19 16:01:42 +0200 | [diff] [blame] | 3485 | } |
| 3486 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3487 | platform_set_drvdata(pdev, hsotg); |
| 3488 | |
| 3489 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3490 | |
Thierry Reding | 148e113 | 2013-01-21 11:09:22 +0100 | [diff] [blame] | 3491 | hsotg->regs = devm_ioremap_resource(&pdev->dev, res); |
| 3492 | if (IS_ERR(hsotg->regs)) { |
| 3493 | ret = PTR_ERR(hsotg->regs); |
Sachin Kamat | 338edab | 2012-05-18 14:33:46 +0530 | [diff] [blame] | 3494 | goto err_clk; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | ret = platform_get_irq(pdev, 0); |
| 3498 | if (ret < 0) { |
| 3499 | dev_err(dev, "cannot find IRQ\n"); |
Sachin Kamat | 338edab | 2012-05-18 14:33:46 +0530 | [diff] [blame] | 3500 | goto err_clk; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3501 | } |
| 3502 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 3503 | spin_lock_init(&hsotg->lock); |
| 3504 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3505 | hsotg->irq = ret; |
| 3506 | |
Sachin Kamat | 338edab | 2012-05-18 14:33:46 +0530 | [diff] [blame] | 3507 | ret = devm_request_irq(&pdev->dev, hsotg->irq, s3c_hsotg_irq, 0, |
| 3508 | dev_name(dev), hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3509 | if (ret < 0) { |
| 3510 | dev_err(dev, "cannot claim IRQ\n"); |
Sachin Kamat | 338edab | 2012-05-18 14:33:46 +0530 | [diff] [blame] | 3511 | goto err_clk; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq); |
| 3515 | |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 3516 | hsotg->gadget.max_speed = USB_SPEED_HIGH; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3517 | hsotg->gadget.ops = &s3c_hsotg_gadget_ops; |
| 3518 | hsotg->gadget.name = dev_name(dev); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3519 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3520 | /* reset the system */ |
| 3521 | |
Lukasz Majewski | 04b4a0f | 2012-05-04 14:17:15 +0200 | [diff] [blame] | 3522 | clk_prepare_enable(hsotg->clk); |
Marek Szyprowski | 31ee04d | 2010-07-19 16:01:42 +0200 | [diff] [blame] | 3523 | |
Lukasz Majewski | fc9a731 | 2012-05-04 14:17:02 +0200 | [diff] [blame] | 3524 | /* regulators */ |
| 3525 | |
| 3526 | for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++) |
| 3527 | hsotg->supplies[i].supply = s3c_hsotg_supply_names[i]; |
| 3528 | |
Sachin Kamat | cd76213 | 2013-01-08 14:27:00 +0530 | [diff] [blame] | 3529 | ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies), |
Lukasz Majewski | fc9a731 | 2012-05-04 14:17:02 +0200 | [diff] [blame] | 3530 | hsotg->supplies); |
| 3531 | if (ret) { |
| 3532 | dev_err(dev, "failed to request supplies: %d\n", ret); |
Sachin Kamat | 338edab | 2012-05-18 14:33:46 +0530 | [diff] [blame] | 3533 | goto err_clk; |
Lukasz Majewski | fc9a731 | 2012-05-04 14:17:02 +0200 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies), |
| 3537 | hsotg->supplies); |
| 3538 | |
| 3539 | if (ret) { |
| 3540 | dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret); |
| 3541 | goto err_supplies; |
| 3542 | } |
| 3543 | |
Lukasz Majewski | 4118878 | 2012-05-04 14:17:01 +0200 | [diff] [blame] | 3544 | /* usb phy enable */ |
| 3545 | s3c_hsotg_phy_enable(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3546 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3547 | s3c_hsotg_corereset(hsotg); |
| 3548 | s3c_hsotg_init(hsotg); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3549 | s3c_hsotg_hw_cfg(hsotg); |
| 3550 | |
| 3551 | /* hsotg->num_of_eps holds number of EPs other than ep0 */ |
| 3552 | |
| 3553 | if (hsotg->num_of_eps == 0) { |
| 3554 | dev_err(dev, "wrong number of EPs (zero)\n"); |
Julia Lawall | dfdda5a | 2012-08-14 08:47:34 +0200 | [diff] [blame] | 3555 | ret = -EINVAL; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3556 | goto err_supplies; |
| 3557 | } |
| 3558 | |
| 3559 | eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep), |
| 3560 | GFP_KERNEL); |
| 3561 | if (!eps) { |
| 3562 | dev_err(dev, "cannot get memory\n"); |
Julia Lawall | dfdda5a | 2012-08-14 08:47:34 +0200 | [diff] [blame] | 3563 | ret = -ENOMEM; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3564 | goto err_supplies; |
| 3565 | } |
| 3566 | |
| 3567 | hsotg->eps = eps; |
| 3568 | |
| 3569 | /* setup endpoint information */ |
| 3570 | |
| 3571 | INIT_LIST_HEAD(&hsotg->gadget.ep_list); |
| 3572 | hsotg->gadget.ep0 = &hsotg->eps[0].ep; |
| 3573 | |
| 3574 | /* allocate EP0 request */ |
| 3575 | |
| 3576 | hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep, |
| 3577 | GFP_KERNEL); |
| 3578 | if (!hsotg->ctrl_req) { |
| 3579 | dev_err(dev, "failed to allocate ctrl req\n"); |
Julia Lawall | dfdda5a | 2012-08-14 08:47:34 +0200 | [diff] [blame] | 3580 | ret = -ENOMEM; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3581 | goto err_ep_mem; |
| 3582 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3583 | |
| 3584 | /* initialise the endpoints now the core has been initialised */ |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3585 | for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3586 | s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum); |
| 3587 | |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 3588 | /* disable power and clock */ |
| 3589 | |
| 3590 | ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), |
| 3591 | hsotg->supplies); |
| 3592 | if (ret) { |
| 3593 | dev_err(hsotg->dev, "failed to disable supplies: %d\n", ret); |
| 3594 | goto err_ep_mem; |
| 3595 | } |
| 3596 | |
| 3597 | s3c_hsotg_phy_disable(hsotg); |
| 3598 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 3599 | ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget); |
| 3600 | if (ret) |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3601 | goto err_ep_mem; |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 3602 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3603 | s3c_hsotg_create_debug(hsotg); |
| 3604 | |
| 3605 | s3c_hsotg_dump(hsotg); |
| 3606 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3607 | return 0; |
| 3608 | |
Lukasz Majewski | 1d144c6 | 2012-05-04 14:17:16 +0200 | [diff] [blame] | 3609 | err_ep_mem: |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3610 | kfree(eps); |
Lukasz Majewski | fc9a731 | 2012-05-04 14:17:02 +0200 | [diff] [blame] | 3611 | err_supplies: |
Lukasz Majewski | 4118878 | 2012-05-04 14:17:01 +0200 | [diff] [blame] | 3612 | s3c_hsotg_phy_disable(hsotg); |
Marek Szyprowski | 31ee04d | 2010-07-19 16:01:42 +0200 | [diff] [blame] | 3613 | err_clk: |
Lukasz Majewski | 1d144c6 | 2012-05-04 14:17:16 +0200 | [diff] [blame] | 3614 | clk_disable_unprepare(hsotg->clk); |
Sachin Kamat | 338edab | 2012-05-18 14:33:46 +0530 | [diff] [blame] | 3615 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3616 | return ret; |
| 3617 | } |
| 3618 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3619 | /** |
| 3620 | * s3c_hsotg_remove - remove function for hsotg driver |
| 3621 | * @pdev: The platform information for the driver |
| 3622 | */ |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 3623 | static int s3c_hsotg_remove(struct platform_device *pdev) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3624 | { |
| 3625 | struct s3c_hsotg *hsotg = platform_get_drvdata(pdev); |
| 3626 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 3627 | usb_del_gadget_udc(&hsotg->gadget); |
| 3628 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3629 | s3c_hsotg_delete_debug(hsotg); |
| 3630 | |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 3631 | if (hsotg->driver) { |
| 3632 | /* should have been done already by driver model core */ |
| 3633 | usb_gadget_unregister_driver(hsotg->driver); |
| 3634 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3635 | |
Lukasz Majewski | 4118878 | 2012-05-04 14:17:01 +0200 | [diff] [blame] | 3636 | s3c_hsotg_phy_disable(hsotg); |
Lukasz Majewski | 04b4a0f | 2012-05-04 14:17:15 +0200 | [diff] [blame] | 3637 | clk_disable_unprepare(hsotg->clk); |
Marek Szyprowski | 31ee04d | 2010-07-19 16:01:42 +0200 | [diff] [blame] | 3638 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3639 | return 0; |
| 3640 | } |
| 3641 | |
| 3642 | #if 1 |
| 3643 | #define s3c_hsotg_suspend NULL |
| 3644 | #define s3c_hsotg_resume NULL |
| 3645 | #endif |
| 3646 | |
Tomasz Figa | c50f056c | 2013-06-25 17:38:23 +0200 | [diff] [blame] | 3647 | #ifdef CONFIG_OF |
| 3648 | static const struct of_device_id s3c_hsotg_of_ids[] = { |
| 3649 | { .compatible = "samsung,s3c6400-hsotg", }, |
| 3650 | { /* sentinel */ } |
| 3651 | }; |
| 3652 | MODULE_DEVICE_TABLE(of, s3c_hsotg_of_ids); |
| 3653 | #endif |
| 3654 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3655 | static struct platform_driver s3c_hsotg_driver = { |
| 3656 | .driver = { |
| 3657 | .name = "s3c-hsotg", |
| 3658 | .owner = THIS_MODULE, |
Tomasz Figa | c50f056c | 2013-06-25 17:38:23 +0200 | [diff] [blame] | 3659 | .of_match_table = of_match_ptr(s3c_hsotg_of_ids), |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3660 | }, |
| 3661 | .probe = s3c_hsotg_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 3662 | .remove = s3c_hsotg_remove, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3663 | .suspend = s3c_hsotg_suspend, |
| 3664 | .resume = s3c_hsotg_resume, |
| 3665 | }; |
| 3666 | |
Axel Lin | cc27c96 | 2011-11-27 20:16:27 +0800 | [diff] [blame] | 3667 | module_platform_driver(s3c_hsotg_driver); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3668 | |
| 3669 | MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device"); |
| 3670 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); |
| 3671 | MODULE_LICENSE("GPL"); |
| 3672 | MODULE_ALIAS("platform:s3c-hsotg"); |