usb: dwc2: gadget: add TEST_MODE feature support
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc2 / core.h
1 /*
2  * core.h - DesignWare HS OTG Controller common declarations
3  *
4  * Copyright (C) 2004-2013 Synopsys, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The names of the above-listed copyright holders may not be used
16  *    to endorse or promote products derived from this software without
17  *    specific prior written permission.
18  *
19  * ALTERNATIVELY, this software may be distributed under the terms of the
20  * GNU General Public License ("GPL") as published by the Free Software
21  * Foundation; either version 2 of the License, or (at your option) any
22  * later version.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #ifndef __DWC2_CORE_H__
38 #define __DWC2_CORE_H__
39
40 #include <linux/phy/phy.h>
41 #include <linux/regulator/consumer.h>
42 #include <linux/usb/gadget.h>
43 #include <linux/usb/otg.h>
44 #include <linux/usb/phy.h>
45 #include "hw.h"
46
47 #ifdef DWC2_LOG_WRITES
48 static inline void do_write(u32 value, void *addr)
49 {
50         writel(value, addr);
51         pr_info("INFO:: wrote %08x to %p\n", value, addr);
52 }
53
54 #undef writel
55 #define writel(v, a)    do_write(v, a)
56 #endif
57
58 /* Maximum number of Endpoints/HostChannels */
59 #define MAX_EPS_CHANNELS        16
60
61 /* s3c-hsotg declarations */
62 static const char * const s3c_hsotg_supply_names[] = {
63         "vusb_d",               /* digital USB supply, 1.2V */
64         "vusb_a",               /* analog USB supply, 1.1V */
65 };
66
67 /*
68  * EP0_MPS_LIMIT
69  *
70  * Unfortunately there seems to be a limit of the amount of data that can
71  * be transferred by IN transactions on EP0. This is either 127 bytes or 3
72  * packets (which practically means 1 packet and 63 bytes of data) when the
73  * MPS is set to 64.
74  *
75  * This means if we are wanting to move >127 bytes of data, we need to
76  * split the transactions up, but just doing one packet at a time does
77  * not work (this may be an implicit DATA0 PID on first packet of the
78  * transaction) and doing 2 packets is outside the controller's limits.
79  *
80  * If we try to lower the MPS size for EP0, then no transfers work properly
81  * for EP0, and the system will fail basic enumeration. As no cause for this
82  * has currently been found, we cannot support any large IN transfers for
83  * EP0.
84  */
85 #define EP0_MPS_LIMIT   64
86
87 struct dwc2_hsotg;
88 struct s3c_hsotg_req;
89
90 /**
91  * struct s3c_hsotg_ep - driver endpoint definition.
92  * @ep: The gadget layer representation of the endpoint.
93  * @name: The driver generated name for the endpoint.
94  * @queue: Queue of requests for this endpoint.
95  * @parent: Reference back to the parent device structure.
96  * @req: The current request that the endpoint is processing. This is
97  *       used to indicate an request has been loaded onto the endpoint
98  *       and has yet to be completed (maybe due to data move, or simply
99  *       awaiting an ack from the core all the data has been completed).
100  * @debugfs: File entry for debugfs file for this endpoint.
101  * @lock: State lock to protect contents of endpoint.
102  * @dir_in: Set to true if this endpoint is of the IN direction, which
103  *          means that it is sending data to the Host.
104  * @index: The index for the endpoint registers.
105  * @mc: Multi Count - number of transactions per microframe
106  * @interval - Interval for periodic endpoints
107  * @name: The name array passed to the USB core.
108  * @halted: Set if the endpoint has been halted.
109  * @periodic: Set if this is a periodic ep, such as Interrupt
110  * @isochronous: Set if this is a isochronous ep
111  * @send_zlp: Set if we need to send a zero-length packet.
112  * @total_data: The total number of data bytes done.
113  * @fifo_size: The size of the FIFO (for periodic IN endpoints)
114  * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
115  * @last_load: The offset of data for the last start of request.
116  * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
117  *
118  * This is the driver's state for each registered enpoint, allowing it
119  * to keep track of transactions that need doing. Each endpoint has a
120  * lock to protect the state, to try and avoid using an overall lock
121  * for the host controller as much as possible.
122  *
123  * For periodic IN endpoints, we have fifo_size and fifo_load to try
124  * and keep track of the amount of data in the periodic FIFO for each
125  * of these as we don't have a status register that tells us how much
126  * is in each of them. (note, this may actually be useless information
127  * as in shared-fifo mode periodic in acts like a single-frame packet
128  * buffer than a fifo)
129  */
130 struct s3c_hsotg_ep {
131         struct usb_ep           ep;
132         struct list_head        queue;
133         struct dwc2_hsotg       *parent;
134         struct s3c_hsotg_req    *req;
135         struct dentry           *debugfs;
136
137         unsigned long           total_data;
138         unsigned int            size_loaded;
139         unsigned int            last_load;
140         unsigned int            fifo_load;
141         unsigned short          fifo_size;
142         unsigned short          fifo_index;
143
144         unsigned char           dir_in;
145         unsigned char           index;
146         unsigned char           mc;
147         unsigned char           interval;
148
149         unsigned int            halted:1;
150         unsigned int            periodic:1;
151         unsigned int            isochronous:1;
152         unsigned int            send_zlp:1;
153
154         char                    name[10];
155 };
156
157 /**
158  * struct s3c_hsotg_req - data transfer request
159  * @req: The USB gadget request
160  * @queue: The list of requests for the endpoint this is queued for.
161  */
162 struct s3c_hsotg_req {
163         struct usb_request      req;
164         struct list_head        queue;
165 };
166
167 #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
168 #define call_gadget(_hs, _entry) \
169 do { \
170         if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
171                 (_hs)->driver && (_hs)->driver->_entry) { \
172                 spin_unlock(&_hs->lock); \
173                 (_hs)->driver->_entry(&(_hs)->gadget); \
174                 spin_lock(&_hs->lock); \
175         } \
176 } while (0)
177 #else
178 #define call_gadget(_hs, _entry)        do {} while (0)
179 #endif
180
181 struct dwc2_hsotg;
182 struct dwc2_host_chan;
183
184 /* Device States */
185 enum dwc2_lx_state {
186         DWC2_L0,        /* On state */
187         DWC2_L1,        /* LPM sleep state */
188         DWC2_L2,        /* USB suspend state */
189         DWC2_L3,        /* Off state */
190 };
191
192 /*
193  * Gadget periodic tx fifo sizes as used by legacy driver
194  * EP0 is not included
195  */
196 #define DWC2_G_P_LEGACY_TX_FIFO_SIZE {256, 256, 256, 256, 768, 768, 768, \
197                                            768, 0, 0, 0, 0, 0, 0, 0}
198
199 /* Gadget ep0 states */
200 enum dwc2_ep0_state {
201         DWC2_EP0_SETUP,
202         DWC2_EP0_DATA_IN,
203         DWC2_EP0_DATA_OUT,
204         DWC2_EP0_STATUS_IN,
205         DWC2_EP0_STATUS_OUT,
206 };
207
208 /**
209  * struct dwc2_core_params - Parameters for configuring the core
210  *
211  * @otg_cap:            Specifies the OTG capabilities.
212  *                       0 - HNP and SRP capable
213  *                       1 - SRP Only capable
214  *                       2 - No HNP/SRP capable (always available)
215  *                      Defaults to best available option (0, 1, then 2)
216  * @otg_ver:            OTG version supported
217  *                       0 - 1.3 (default)
218  *                       1 - 2.0
219  * @dma_enable:         Specifies whether to use slave or DMA mode for accessing
220  *                      the data FIFOs. The driver will automatically detect the
221  *                      value for this parameter if none is specified.
222  *                       0 - Slave (always available)
223  *                       1 - DMA (default, if available)
224  * @dma_desc_enable:    When DMA mode is enabled, specifies whether to use
225  *                      address DMA mode or descriptor DMA mode for accessing
226  *                      the data FIFOs. The driver will automatically detect the
227  *                      value for this if none is specified.
228  *                       0 - Address DMA
229  *                       1 - Descriptor DMA (default, if available)
230  * @speed:              Specifies the maximum speed of operation in host and
231  *                      device mode. The actual speed depends on the speed of
232  *                      the attached device and the value of phy_type.
233  *                       0 - High Speed
234  *                           (default when phy_type is UTMI+ or ULPI)
235  *                       1 - Full Speed
236  *                           (default when phy_type is Full Speed)
237  * @enable_dynamic_fifo: 0 - Use coreConsultant-specified FIFO size parameters
238  *                       1 - Allow dynamic FIFO sizing (default, if available)
239  * @en_multiple_tx_fifo: Specifies whether dedicated per-endpoint transmit FIFOs
240  *                      are enabled
241  * @host_rx_fifo_size:  Number of 4-byte words in the Rx FIFO in host mode when
242  *                      dynamic FIFO sizing is enabled
243  *                       16 to 32768
244  *                      Actual maximum value is autodetected and also
245  *                      the default.
246  * @host_nperio_tx_fifo_size: Number of 4-byte words in the non-periodic Tx FIFO
247  *                      in host mode when dynamic FIFO sizing is enabled
248  *                       16 to 32768
249  *                      Actual maximum value is autodetected and also
250  *                      the default.
251  * @host_perio_tx_fifo_size: Number of 4-byte words in the periodic Tx FIFO in
252  *                      host mode when dynamic FIFO sizing is enabled
253  *                       16 to 32768
254  *                      Actual maximum value is autodetected and also
255  *                      the default.
256  * @max_transfer_size:  The maximum transfer size supported, in bytes
257  *                       2047 to 65,535
258  *                      Actual maximum value is autodetected and also
259  *                      the default.
260  * @max_packet_count:   The maximum number of packets in a transfer
261  *                       15 to 511
262  *                      Actual maximum value is autodetected and also
263  *                      the default.
264  * @host_channels:      The number of host channel registers to use
265  *                       1 to 16
266  *                      Actual maximum value is autodetected and also
267  *                      the default.
268  * @phy_type:           Specifies the type of PHY interface to use. By default,
269  *                      the driver will automatically detect the phy_type.
270  *                       0 - Full Speed Phy
271  *                       1 - UTMI+ Phy
272  *                       2 - ULPI Phy
273  *                      Defaults to best available option (2, 1, then 0)
274  * @phy_utmi_width:     Specifies the UTMI+ Data Width (in bits). This parameter
275  *                      is applicable for a phy_type of UTMI+ or ULPI. (For a
276  *                      ULPI phy_type, this parameter indicates the data width
277  *                      between the MAC and the ULPI Wrapper.) Also, this
278  *                      parameter is applicable only if the OTG_HSPHY_WIDTH cC
279  *                      parameter was set to "8 and 16 bits", meaning that the
280  *                      core has been configured to work at either data path
281  *                      width.
282  *                       8 or 16 (default 16 if available)
283  * @phy_ulpi_ddr:       Specifies whether the ULPI operates at double or single
284  *                      data rate. This parameter is only applicable if phy_type
285  *                      is ULPI.
286  *                       0 - single data rate ULPI interface with 8 bit wide
287  *                           data bus (default)
288  *                       1 - double data rate ULPI interface with 4 bit wide
289  *                           data bus
290  * @phy_ulpi_ext_vbus:  For a ULPI phy, specifies whether to use the internal or
291  *                      external supply to drive the VBus
292  *                       0 - Internal supply (default)
293  *                       1 - External supply
294  * @i2c_enable:         Specifies whether to use the I2Cinterface for a full
295  *                      speed PHY. This parameter is only applicable if phy_type
296  *                      is FS.
297  *                       0 - No (default)
298  *                       1 - Yes
299  * @ulpi_fs_ls:         Make ULPI phy operate in FS/LS mode only
300  *                       0 - No (default)
301  *                       1 - Yes
302  * @host_support_fs_ls_low_power: Specifies whether low power mode is supported
303  *                      when attached to a Full Speed or Low Speed device in
304  *                      host mode.
305  *                       0 - Don't support low power mode (default)
306  *                       1 - Support low power mode
307  * @host_ls_low_power_phy_clk: Specifies the PHY clock rate in low power mode
308  *                      when connected to a Low Speed device in host
309  *                      mode. This parameter is applicable only if
310  *                      host_support_fs_ls_low_power is enabled.
311  *                       0 - 48 MHz
312  *                           (default when phy_type is UTMI+ or ULPI)
313  *                       1 - 6 MHz
314  *                           (default when phy_type is Full Speed)
315  * @ts_dline:           Enable Term Select Dline pulsing
316  *                       0 - No (default)
317  *                       1 - Yes
318  * @reload_ctl:         Allow dynamic reloading of HFIR register during runtime
319  *                       0 - No (default for core < 2.92a)
320  *                       1 - Yes (default for core >= 2.92a)
321  * @ahbcfg:             This field allows the default value of the GAHBCFG
322  *                      register to be overridden
323  *                       -1         - GAHBCFG value will be set to 0x06
324  *                                    (INCR4, default)
325  *                       all others - GAHBCFG value will be overridden with
326  *                                    this value
327  *                      Not all bits can be controlled like this, the
328  *                      bits defined by GAHBCFG_CTRL_MASK are controlled
329  *                      by the driver and are ignored in this
330  *                      configuration value.
331  * @uframe_sched:       True to enable the microframe scheduler
332  *
333  * The following parameters may be specified when starting the module. These
334  * parameters define how the DWC_otg controller should be configured. A
335  * value of -1 (or any other out of range value) for any parameter means
336  * to read the value from hardware (if possible) or use the builtin
337  * default described above.
338  */
339 struct dwc2_core_params {
340         /*
341          * Don't add any non-int members here, this will break
342          * dwc2_set_all_params!
343          */
344         int otg_cap;
345         int otg_ver;
346         int dma_enable;
347         int dma_desc_enable;
348         int speed;
349         int enable_dynamic_fifo;
350         int en_multiple_tx_fifo;
351         int host_rx_fifo_size;
352         int host_nperio_tx_fifo_size;
353         int host_perio_tx_fifo_size;
354         int max_transfer_size;
355         int max_packet_count;
356         int host_channels;
357         int phy_type;
358         int phy_utmi_width;
359         int phy_ulpi_ddr;
360         int phy_ulpi_ext_vbus;
361         int i2c_enable;
362         int ulpi_fs_ls;
363         int host_support_fs_ls_low_power;
364         int host_ls_low_power_phy_clk;
365         int ts_dline;
366         int reload_ctl;
367         int ahbcfg;
368         int uframe_sched;
369 };
370
371 /**
372  * struct dwc2_hw_params - Autodetected parameters.
373  *
374  * These parameters are the various parameters read from hardware
375  * registers during initialization. They typically contain the best
376  * supported or maximum value that can be configured in the
377  * corresponding dwc2_core_params value.
378  *
379  * The values that are not in dwc2_core_params are documented below.
380  *
381  * @op_mode             Mode of Operation
382  *                       0 - HNP- and SRP-Capable OTG (Host & Device)
383  *                       1 - SRP-Capable OTG (Host & Device)
384  *                       2 - Non-HNP and Non-SRP Capable OTG (Host & Device)
385  *                       3 - SRP-Capable Device
386  *                       4 - Non-OTG Device
387  *                       5 - SRP-Capable Host
388  *                       6 - Non-OTG Host
389  * @arch                Architecture
390  *                       0 - Slave only
391  *                       1 - External DMA
392  *                       2 - Internal DMA
393  * @power_optimized     Are power optimizations enabled?
394  * @num_dev_ep          Number of device endpoints available
395  * @num_dev_perio_in_ep Number of device periodic IN endpoints
396  *                      available
397  * @dev_token_q_depth   Device Mode IN Token Sequence Learning Queue
398  *                      Depth
399  *                       0 to 30
400  * @host_perio_tx_q_depth
401  *                      Host Mode Periodic Request Queue Depth
402  *                       2, 4 or 8
403  * @nperio_tx_q_depth
404  *                      Non-Periodic Request Queue Depth
405  *                       2, 4 or 8
406  * @hs_phy_type         High-speed PHY interface type
407  *                       0 - High-speed interface not supported
408  *                       1 - UTMI+
409  *                       2 - ULPI
410  *                       3 - UTMI+ and ULPI
411  * @fs_phy_type         Full-speed PHY interface type
412  *                       0 - Full speed interface not supported
413  *                       1 - Dedicated full speed interface
414  *                       2 - FS pins shared with UTMI+ pins
415  *                       3 - FS pins shared with ULPI pins
416  * @total_fifo_size:    Total internal RAM for FIFOs (bytes)
417  * @utmi_phy_data_width UTMI+ PHY data width
418  *                       0 - 8 bits
419  *                       1 - 16 bits
420  *                       2 - 8 or 16 bits
421  * @snpsid:             Value from SNPSID register
422  */
423 struct dwc2_hw_params {
424         unsigned op_mode:3;
425         unsigned arch:2;
426         unsigned dma_desc_enable:1;
427         unsigned enable_dynamic_fifo:1;
428         unsigned en_multiple_tx_fifo:1;
429         unsigned host_rx_fifo_size:16;
430         unsigned host_nperio_tx_fifo_size:16;
431         unsigned host_perio_tx_fifo_size:16;
432         unsigned nperio_tx_q_depth:3;
433         unsigned host_perio_tx_q_depth:3;
434         unsigned dev_token_q_depth:5;
435         unsigned max_transfer_size:26;
436         unsigned max_packet_count:11;
437         unsigned host_channels:5;
438         unsigned hs_phy_type:2;
439         unsigned fs_phy_type:2;
440         unsigned i2c_enable:1;
441         unsigned num_dev_ep:4;
442         unsigned num_dev_perio_in_ep:4;
443         unsigned total_fifo_size:16;
444         unsigned power_optimized:1;
445         unsigned utmi_phy_data_width:2;
446         u32 snpsid;
447 };
448
449 /* Size of control and EP0 buffers */
450 #define DWC2_CTRL_BUFF_SIZE 8
451
452 /**
453  * struct dwc2_hsotg - Holds the state of the driver, including the non-periodic
454  * and periodic schedules
455  *
456  * These are common for both host and peripheral modes:
457  *
458  * @dev:                The struct device pointer
459  * @regs:               Pointer to controller regs
460  * @hw_params:          Parameters that were autodetected from the
461  *                      hardware registers
462  * @core_params:        Parameters that define how the core should be configured
463  * @op_state:           The operational State, during transitions (a_host=>
464  *                      a_peripheral and b_device=>b_host) this may not match
465  *                      the core, but allows the software to determine
466  *                      transitions
467  * @dr_mode:            Requested mode of operation, one of following:
468  *                      - USB_DR_MODE_PERIPHERAL
469  *                      - USB_DR_MODE_HOST
470  *                      - USB_DR_MODE_OTG
471  * @lock:               Spinlock that protects all the driver data structures
472  * @priv:               Stores a pointer to the struct usb_hcd
473  * @queuing_high_bandwidth: True if multiple packets of a high-bandwidth
474  *                      transfer are in process of being queued
475  * @srp_success:        Stores status of SRP request in the case of a FS PHY
476  *                      with an I2C interface
477  * @wq_otg:             Workqueue object used for handling of some interrupts
478  * @wf_otg:             Work object for handling Connector ID Status Change
479  *                      interrupt
480  * @wkp_timer:          Timer object for handling Wakeup Detected interrupt
481  * @lx_state:           Lx state of connected device
482  *
483  * These are for host mode:
484  *
485  * @flags:              Flags for handling root port state changes
486  * @non_periodic_sched_inactive: Inactive QHs in the non-periodic schedule.
487  *                      Transfers associated with these QHs are not currently
488  *                      assigned to a host channel.
489  * @non_periodic_sched_active: Active QHs in the non-periodic schedule.
490  *                      Transfers associated with these QHs are currently
491  *                      assigned to a host channel.
492  * @non_periodic_qh_ptr: Pointer to next QH to process in the active
493  *                      non-periodic schedule
494  * @periodic_sched_inactive: Inactive QHs in the periodic schedule. This is a
495  *                      list of QHs for periodic transfers that are _not_
496  *                      scheduled for the next frame. Each QH in the list has an
497  *                      interval counter that determines when it needs to be
498  *                      scheduled for execution. This scheduling mechanism
499  *                      allows only a simple calculation for periodic bandwidth
500  *                      used (i.e. must assume that all periodic transfers may
501  *                      need to execute in the same frame). However, it greatly
502  *                      simplifies scheduling and should be sufficient for the
503  *                      vast majority of OTG hosts, which need to connect to a
504  *                      small number of peripherals at one time. Items move from
505  *                      this list to periodic_sched_ready when the QH interval
506  *                      counter is 0 at SOF.
507  * @periodic_sched_ready:  List of periodic QHs that are ready for execution in
508  *                      the next frame, but have not yet been assigned to host
509  *                      channels. Items move from this list to
510  *                      periodic_sched_assigned as host channels become
511  *                      available during the current frame.
512  * @periodic_sched_assigned: List of periodic QHs to be executed in the next
513  *                      frame that are assigned to host channels. Items move
514  *                      from this list to periodic_sched_queued as the
515  *                      transactions for the QH are queued to the DWC_otg
516  *                      controller.
517  * @periodic_sched_queued: List of periodic QHs that have been queued for
518  *                      execution. Items move from this list to either
519  *                      periodic_sched_inactive or periodic_sched_ready when the
520  *                      channel associated with the transfer is released. If the
521  *                      interval for the QH is 1, the item moves to
522  *                      periodic_sched_ready because it must be rescheduled for
523  *                      the next frame. Otherwise, the item moves to
524  *                      periodic_sched_inactive.
525  * @periodic_usecs:     Total bandwidth claimed so far for periodic transfers.
526  *                      This value is in microseconds per (micro)frame. The
527  *                      assumption is that all periodic transfers may occur in
528  *                      the same (micro)frame.
529  * @frame_usecs:        Internal variable used by the microframe scheduler
530  * @frame_number:       Frame number read from the core at SOF. The value ranges
531  *                      from 0 to HFNUM_MAX_FRNUM.
532  * @periodic_qh_count:  Count of periodic QHs, if using several eps. Used for
533  *                      SOF enable/disable.
534  * @free_hc_list:       Free host channels in the controller. This is a list of
535  *                      struct dwc2_host_chan items.
536  * @periodic_channels:  Number of host channels assigned to periodic transfers.
537  *                      Currently assuming that there is a dedicated host
538  *                      channel for each periodic transaction and at least one
539  *                      host channel is available for non-periodic transactions.
540  * @non_periodic_channels: Number of host channels assigned to non-periodic
541  *                      transfers
542  * @available_host_channels Number of host channels available for the microframe
543  *                      scheduler to use
544  * @hc_ptr_array:       Array of pointers to the host channel descriptors.
545  *                      Allows accessing a host channel descriptor given the
546  *                      host channel number. This is useful in interrupt
547  *                      handlers.
548  * @status_buf:         Buffer used for data received during the status phase of
549  *                      a control transfer.
550  * @status_buf_dma:     DMA address for status_buf
551  * @start_work:         Delayed work for handling host A-cable connection
552  * @reset_work:         Delayed work for handling a port reset
553  * @otg_port:           OTG port number
554  * @frame_list:         Frame list
555  * @frame_list_dma:     Frame list DMA address
556  *
557  * These are for peripheral mode:
558  *
559  * @driver:             USB gadget driver
560  * @phy:                The otg phy transceiver structure for phy control.
561  * @uphy:               The otg phy transceiver structure for old USB phy control.
562  * @plat:               The platform specific configuration data. This can be removed once
563  *                      all SoCs support usb transceiver.
564  * @supplies:           Definition of USB power supplies
565  * @phyif:              PHY interface width
566  * @dedicated_fifos:    Set if the hardware has dedicated IN-EP fifos.
567  * @num_of_eps:         Number of available EPs (excluding EP0)
568  * @debug_root:         Root directrory for debugfs.
569  * @debug_file:         Main status file for debugfs.
570  * @debug_testmode:     Testmode status file for debugfs.
571  * @debug_fifo:         FIFO status file for debugfs.
572  * @ep0_reply:          Request used for ep0 reply.
573  * @ep0_buff:           Buffer for EP0 reply data, if needed.
574  * @ctrl_buff:          Buffer for EP0 control requests.
575  * @ctrl_req:           Request for EP0 control packets.
576  * @ep0_state:          EP0 control transfers state
577  * @test_mode:          USB test mode requested by the host
578  * @last_rst:           Time of last reset
579  * @eps:                The endpoints being supplied to the gadget framework
580  * @g_using_dma:          Indicate if dma usage is enabled
581  * @g_rx_fifo_sz:         Contains rx fifo size value
582  * @g_np_g_tx_fifo_sz:      Contains Non-Periodic tx fifo size value
583  * @g_tx_fifo_sz:         Contains tx fifo size value per endpoints
584  */
585 struct dwc2_hsotg {
586         struct device *dev;
587         void __iomem *regs;
588         /** Params detected from hardware */
589         struct dwc2_hw_params hw_params;
590         /** Params to actually use */
591         struct dwc2_core_params *core_params;
592         enum usb_otg_state op_state;
593         enum usb_dr_mode dr_mode;
594
595         struct phy *phy;
596         struct usb_phy *uphy;
597         struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsotg_supply_names)];
598
599         spinlock_t lock;
600         struct mutex init_mutex;
601         void *priv;
602         int     irq;
603         struct clk *clk;
604
605         unsigned int queuing_high_bandwidth:1;
606         unsigned int srp_success:1;
607
608         struct workqueue_struct *wq_otg;
609         struct work_struct wf_otg;
610         struct timer_list wkp_timer;
611         enum dwc2_lx_state lx_state;
612
613         struct dentry *debug_root;
614         struct dentry *debug_file;
615         struct dentry *debug_testmode;
616         struct dentry *debug_fifo;
617
618         /* DWC OTG HW Release versions */
619 #define DWC2_CORE_REV_2_71a     0x4f54271a
620 #define DWC2_CORE_REV_2_90a     0x4f54290a
621 #define DWC2_CORE_REV_2_92a     0x4f54292a
622 #define DWC2_CORE_REV_2_94a     0x4f54294a
623 #define DWC2_CORE_REV_3_00a     0x4f54300a
624
625 #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
626         union dwc2_hcd_internal_flags {
627                 u32 d32;
628                 struct {
629                         unsigned port_connect_status_change:1;
630                         unsigned port_connect_status:1;
631                         unsigned port_reset_change:1;
632                         unsigned port_enable_change:1;
633                         unsigned port_suspend_change:1;
634                         unsigned port_over_current_change:1;
635                         unsigned port_l1_change:1;
636                         unsigned reserved:25;
637                 } b;
638         } flags;
639
640         struct list_head non_periodic_sched_inactive;
641         struct list_head non_periodic_sched_active;
642         struct list_head *non_periodic_qh_ptr;
643         struct list_head periodic_sched_inactive;
644         struct list_head periodic_sched_ready;
645         struct list_head periodic_sched_assigned;
646         struct list_head periodic_sched_queued;
647         u16 periodic_usecs;
648         u16 frame_usecs[8];
649         u16 frame_number;
650         u16 periodic_qh_count;
651
652 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
653 #define FRAME_NUM_ARRAY_SIZE 1000
654         u16 last_frame_num;
655         u16 *frame_num_array;
656         u16 *last_frame_num_array;
657         int frame_num_idx;
658         int dumped_frame_num_array;
659 #endif
660
661         struct list_head free_hc_list;
662         int periodic_channels;
663         int non_periodic_channels;
664         int available_host_channels;
665         struct dwc2_host_chan *hc_ptr_array[MAX_EPS_CHANNELS];
666         u8 *status_buf;
667         dma_addr_t status_buf_dma;
668 #define DWC2_HCD_STATUS_BUF_SIZE 64
669
670         struct delayed_work start_work;
671         struct delayed_work reset_work;
672         u8 otg_port;
673         u32 *frame_list;
674         dma_addr_t frame_list_dma;
675
676 #ifdef DEBUG
677         u32 frrem_samples;
678         u64 frrem_accum;
679
680         u32 hfnum_7_samples_a;
681         u64 hfnum_7_frrem_accum_a;
682         u32 hfnum_0_samples_a;
683         u64 hfnum_0_frrem_accum_a;
684         u32 hfnum_other_samples_a;
685         u64 hfnum_other_frrem_accum_a;
686
687         u32 hfnum_7_samples_b;
688         u64 hfnum_7_frrem_accum_b;
689         u32 hfnum_0_samples_b;
690         u64 hfnum_0_frrem_accum_b;
691         u32 hfnum_other_samples_b;
692         u64 hfnum_other_frrem_accum_b;
693 #endif
694 #endif /* CONFIG_USB_DWC2_HOST || CONFIG_USB_DWC2_DUAL_ROLE */
695
696 #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
697         /* Gadget structures */
698         struct usb_gadget_driver *driver;
699         struct s3c_hsotg_plat *plat;
700
701         u32 phyif;
702         int fifo_mem;
703         unsigned int dedicated_fifos:1;
704         unsigned char num_of_eps;
705         u32 fifo_map;
706
707         struct usb_request *ep0_reply;
708         struct usb_request *ctrl_req;
709         void *ep0_buff;
710         void *ctrl_buff;
711         enum dwc2_ep0_state ep0_state;
712         u8 test_mode;
713
714         struct usb_gadget gadget;
715         unsigned int enabled:1;
716         unsigned int connected:1;
717         unsigned long last_rst;
718         struct s3c_hsotg_ep *eps_in[MAX_EPS_CHANNELS];
719         struct s3c_hsotg_ep *eps_out[MAX_EPS_CHANNELS];
720         u32 g_using_dma;
721         u32 g_rx_fifo_sz;
722         u32 g_np_g_tx_fifo_sz;
723         u32 g_tx_fifo_sz[MAX_EPS_CHANNELS];
724 #endif /* CONFIG_USB_DWC2_PERIPHERAL || CONFIG_USB_DWC2_DUAL_ROLE */
725 };
726
727 /* Reasons for halting a host channel */
728 enum dwc2_halt_status {
729         DWC2_HC_XFER_NO_HALT_STATUS,
730         DWC2_HC_XFER_COMPLETE,
731         DWC2_HC_XFER_URB_COMPLETE,
732         DWC2_HC_XFER_ACK,
733         DWC2_HC_XFER_NAK,
734         DWC2_HC_XFER_NYET,
735         DWC2_HC_XFER_STALL,
736         DWC2_HC_XFER_XACT_ERR,
737         DWC2_HC_XFER_FRAME_OVERRUN,
738         DWC2_HC_XFER_BABBLE_ERR,
739         DWC2_HC_XFER_DATA_TOGGLE_ERR,
740         DWC2_HC_XFER_AHB_ERR,
741         DWC2_HC_XFER_PERIODIC_INCOMPLETE,
742         DWC2_HC_XFER_URB_DEQUEUE,
743 };
744
745 /*
746  * The following functions support initialization of the core driver component
747  * and the DWC_otg controller
748  */
749 extern void dwc2_core_host_init(struct dwc2_hsotg *hsotg);
750
751 /*
752  * Host core Functions.
753  * The following functions support managing the DWC_otg controller in host
754  * mode.
755  */
756 extern void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan);
757 extern void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
758                          enum dwc2_halt_status halt_status);
759 extern void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg,
760                             struct dwc2_host_chan *chan);
761 extern void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
762                                    struct dwc2_host_chan *chan);
763 extern void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
764                                         struct dwc2_host_chan *chan);
765 extern int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
766                                      struct dwc2_host_chan *chan);
767 extern void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
768                             struct dwc2_host_chan *chan);
769 extern void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg);
770 extern void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg);
771
772 extern u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg);
773 extern bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg);
774
775 /*
776  * Common core Functions.
777  * The following functions support managing the DWC_otg controller in either
778  * device or host mode.
779  */
780 extern void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes);
781 extern void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num);
782 extern void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg);
783
784 extern int dwc2_core_init(struct dwc2_hsotg *hsotg, bool select_phy, int irq);
785 extern void dwc2_enable_global_interrupts(struct dwc2_hsotg *hcd);
786 extern void dwc2_disable_global_interrupts(struct dwc2_hsotg *hcd);
787
788 /* This function should be called on every hardware interrupt. */
789 extern irqreturn_t dwc2_handle_common_intr(int irq, void *dev);
790
791 /* OTG Core Parameters */
792
793 /*
794  * Specifies the OTG capabilities. The driver will automatically
795  * detect the value for this parameter if none is specified.
796  * 0 - HNP and SRP capable (default)
797  * 1 - SRP Only capable
798  * 2 - No HNP/SRP capable
799  */
800 extern void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val);
801 #define DWC2_CAP_PARAM_HNP_SRP_CAPABLE          0
802 #define DWC2_CAP_PARAM_SRP_ONLY_CAPABLE         1
803 #define DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE       2
804
805 /*
806  * Specifies whether to use slave or DMA mode for accessing the data
807  * FIFOs. The driver will automatically detect the value for this
808  * parameter if none is specified.
809  * 0 - Slave
810  * 1 - DMA (default, if available)
811  */
812 extern void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val);
813
814 /*
815  * When DMA mode is enabled specifies whether to use
816  * address DMA or DMA Descritor mode for accessing the data
817  * FIFOs in device mode. The driver will automatically detect
818  * the value for this parameter if none is specified.
819  * 0 - address DMA
820  * 1 - DMA Descriptor(default, if available)
821  */
822 extern void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val);
823
824 /*
825  * Specifies the maximum speed of operation in host and device mode.
826  * The actual speed depends on the speed of the attached device and
827  * the value of phy_type. The actual speed depends on the speed of the
828  * attached device.
829  * 0 - High Speed (default)
830  * 1 - Full Speed
831  */
832 extern void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val);
833 #define DWC2_SPEED_PARAM_HIGH   0
834 #define DWC2_SPEED_PARAM_FULL   1
835
836 /*
837  * Specifies whether low power mode is supported when attached
838  * to a Full Speed or Low Speed device in host mode.
839  *
840  * 0 - Don't support low power mode (default)
841  * 1 - Support low power mode
842  */
843 extern void dwc2_set_param_host_support_fs_ls_low_power(
844                 struct dwc2_hsotg *hsotg, int val);
845
846 /*
847  * Specifies the PHY clock rate in low power mode when connected to a
848  * Low Speed device in host mode. This parameter is applicable only if
849  * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
850  * then defaults to 6 MHZ otherwise 48 MHZ.
851  *
852  * 0 - 48 MHz
853  * 1 - 6 MHz
854  */
855 extern void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg,
856                                                      int val);
857 #define DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ      0
858 #define DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ       1
859
860 /*
861  * 0 - Use cC FIFO size parameters
862  * 1 - Allow dynamic FIFO sizing (default)
863  */
864 extern void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg,
865                                                int val);
866
867 /*
868  * Number of 4-byte words in the Rx FIFO in host mode when dynamic
869  * FIFO sizing is enabled.
870  * 16 to 32768 (default 1024)
871  */
872 extern void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val);
873
874 /*
875  * Number of 4-byte words in the non-periodic Tx FIFO in host mode
876  * when Dynamic FIFO sizing is enabled in the core.
877  * 16 to 32768 (default 256)
878  */
879 extern void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg,
880                                                     int val);
881
882 /*
883  * Number of 4-byte words in the host periodic Tx FIFO when dynamic
884  * FIFO sizing is enabled.
885  * 16 to 32768 (default 256)
886  */
887 extern void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg,
888                                                    int val);
889
890 /*
891  * The maximum transfer size supported in bytes.
892  * 2047 to 65,535  (default 65,535)
893  */
894 extern void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val);
895
896 /*
897  * The maximum number of packets in a transfer.
898  * 15 to 511  (default 511)
899  */
900 extern void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val);
901
902 /*
903  * The number of host channel registers to use.
904  * 1 to 16 (default 11)
905  * Note: The FPGA configuration supports a maximum of 11 host channels.
906  */
907 extern void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val);
908
909 /*
910  * Specifies the type of PHY interface to use. By default, the driver
911  * will automatically detect the phy_type.
912  *
913  * 0 - Full Speed PHY
914  * 1 - UTMI+ (default)
915  * 2 - ULPI
916  */
917 extern void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val);
918 #define DWC2_PHY_TYPE_PARAM_FS          0
919 #define DWC2_PHY_TYPE_PARAM_UTMI        1
920 #define DWC2_PHY_TYPE_PARAM_ULPI        2
921
922 /*
923  * Specifies the UTMI+ Data Width. This parameter is
924  * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
925  * PHY_TYPE, this parameter indicates the data width between
926  * the MAC and the ULPI Wrapper.) Also, this parameter is
927  * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
928  * to "8 and 16 bits", meaning that the core has been
929  * configured to work at either data path width.
930  *
931  * 8 or 16 bits (default 16)
932  */
933 extern void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val);
934
935 /*
936  * Specifies whether the ULPI operates at double or single
937  * data rate. This parameter is only applicable if PHY_TYPE is
938  * ULPI.
939  *
940  * 0 - single data rate ULPI interface with 8 bit wide data
941  * bus (default)
942  * 1 - double data rate ULPI interface with 4 bit wide data
943  * bus
944  */
945 extern void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val);
946
947 /*
948  * Specifies whether to use the internal or external supply to
949  * drive the vbus with a ULPI phy.
950  */
951 extern void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val);
952 #define DWC2_PHY_ULPI_INTERNAL_VBUS     0
953 #define DWC2_PHY_ULPI_EXTERNAL_VBUS     1
954
955 /*
956  * Specifies whether to use the I2Cinterface for full speed PHY. This
957  * parameter is only applicable if PHY_TYPE is FS.
958  * 0 - No (default)
959  * 1 - Yes
960  */
961 extern void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val);
962
963 extern void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val);
964
965 extern void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val);
966
967 /*
968  * Specifies whether dedicated transmit FIFOs are
969  * enabled for non periodic IN endpoints in device mode
970  * 0 - No
971  * 1 - Yes
972  */
973 extern void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg,
974                                                int val);
975
976 extern void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val);
977
978 extern void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val);
979
980 extern void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val);
981
982 /*
983  * Dump core registers and SPRAM
984  */
985 extern void dwc2_dump_dev_registers(struct dwc2_hsotg *hsotg);
986 extern void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg);
987 extern void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg);
988
989 /*
990  * Return OTG version - either 1.3 or 2.0
991  */
992 extern u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg);
993
994 /* Gadget defines */
995 #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
996 extern int s3c_hsotg_remove(struct dwc2_hsotg *hsotg);
997 extern int s3c_hsotg_suspend(struct dwc2_hsotg *dwc2);
998 extern int s3c_hsotg_resume(struct dwc2_hsotg *dwc2);
999 extern int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq);
1000 extern void s3c_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2);
1001 extern void s3c_hsotg_core_connect(struct dwc2_hsotg *hsotg);
1002 extern void s3c_hsotg_disconnect(struct dwc2_hsotg *dwc2);
1003 #else
1004 static inline int s3c_hsotg_remove(struct dwc2_hsotg *dwc2)
1005 { return 0; }
1006 static inline int s3c_hsotg_suspend(struct dwc2_hsotg *dwc2)
1007 { return 0; }
1008 static inline int s3c_hsotg_resume(struct dwc2_hsotg *dwc2)
1009 { return 0; }
1010 static inline int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
1011 { return 0; }
1012 static inline void s3c_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2) {}
1013 static inline void s3c_hsotg_core_connect(struct dwc2_hsotg *hsotg) {}
1014 static inline void s3c_hsotg_disconnect(struct dwc2_hsotg *dwc2) {}
1015 #endif
1016
1017 #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
1018 extern int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg);
1019 extern void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg);
1020 extern void dwc2_hcd_start(struct dwc2_hsotg *hsotg);
1021 #else
1022 static inline void dwc2_set_all_params(struct dwc2_core_params *params, int value) {}
1023 static inline int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
1024 { return 0; }
1025 static inline void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg) {}
1026 static inline void dwc2_hcd_start(struct dwc2_hsotg *hsotg) {}
1027 static inline void dwc2_hcd_remove(struct dwc2_hsotg *hsotg) {}
1028 static inline int dwc2_hcd_init(struct dwc2_hsotg *hsotg, int irq,
1029                                 const struct dwc2_core_params *params)
1030 { return 0; }
1031 #endif
1032
1033 #endif /* __DWC2_CORE_H__ */