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