UPSTREAM: usb: dwc2: Improve handling of host and device hwparams
[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 CONFIG_MIPS
48 /*
49  * There are some MIPS machines that can run in either big-endian
50  * or little-endian mode and that use the dwc2 register without
51  * a byteswap in both ways.
52  * Unlike other architectures, MIPS apparently does not require a
53  * barrier before the __raw_writel() to synchronize with DMA but does
54  * require the barrier after the __raw_writel() to serialize a set of
55  * writes. This set of operations was added specifically for MIPS and
56  * should only be used there.
57  */
58 static inline u32 dwc2_readl(const void __iomem *addr)
59 {
60         u32 value = __raw_readl(addr);
61
62         /* In order to preserve endianness __raw_* operation is used. Therefore
63          * a barrier is needed to ensure IO access is not re-ordered across
64          * reads or writes
65          */
66         mb();
67         return value;
68 }
69
70 static inline void dwc2_writel(u32 value, void __iomem *addr)
71 {
72         __raw_writel(value, addr);
73
74         /*
75          * In order to preserve endianness __raw_* operation is used. Therefore
76          * a barrier is needed to ensure IO access is not re-ordered across
77          * reads or writes
78          */
79         mb();
80 #ifdef DWC2_LOG_WRITES
81         pr_info("INFO:: wrote %08x to %p\n", value, addr);
82 #endif
83 }
84 #else
85 /* Normal architectures just use readl/write */
86 static inline u32 dwc2_readl(const void __iomem *addr)
87 {
88         return readl(addr);
89 }
90
91 static inline void dwc2_writel(u32 value, void __iomem *addr)
92 {
93         writel(value, addr);
94
95 #ifdef DWC2_LOG_WRITES
96         pr_info("info:: wrote %08x to %p\n", value, addr);
97 #endif
98 }
99 #endif
100
101 /* Maximum number of Endpoints/HostChannels */
102 #define MAX_EPS_CHANNELS        16
103
104 /* dwc2-hsotg declarations */
105 static const char * const dwc2_hsotg_supply_names[] = {
106         "vusb_d",               /* digital USB supply, 1.2V */
107         "vusb_a",               /* analog USB supply, 1.1V */
108 };
109
110 /*
111  * EP0_MPS_LIMIT
112  *
113  * Unfortunately there seems to be a limit of the amount of data that can
114  * be transferred by IN transactions on EP0. This is either 127 bytes or 3
115  * packets (which practically means 1 packet and 63 bytes of data) when the
116  * MPS is set to 64.
117  *
118  * This means if we are wanting to move >127 bytes of data, we need to
119  * split the transactions up, but just doing one packet at a time does
120  * not work (this may be an implicit DATA0 PID on first packet of the
121  * transaction) and doing 2 packets is outside the controller's limits.
122  *
123  * If we try to lower the MPS size for EP0, then no transfers work properly
124  * for EP0, and the system will fail basic enumeration. As no cause for this
125  * has currently been found, we cannot support any large IN transfers for
126  * EP0.
127  */
128 #define EP0_MPS_LIMIT   64
129
130 struct dwc2_hsotg;
131 struct dwc2_hsotg_req;
132
133 /**
134  * struct dwc2_hsotg_ep - driver endpoint definition.
135  * @ep: The gadget layer representation of the endpoint.
136  * @name: The driver generated name for the endpoint.
137  * @queue: Queue of requests for this endpoint.
138  * @parent: Reference back to the parent device structure.
139  * @req: The current request that the endpoint is processing. This is
140  *       used to indicate an request has been loaded onto the endpoint
141  *       and has yet to be completed (maybe due to data move, or simply
142  *       awaiting an ack from the core all the data has been completed).
143  * @debugfs: File entry for debugfs file for this endpoint.
144  * @lock: State lock to protect contents of endpoint.
145  * @dir_in: Set to true if this endpoint is of the IN direction, which
146  *          means that it is sending data to the Host.
147  * @index: The index for the endpoint registers.
148  * @mc: Multi Count - number of transactions per microframe
149  * @interval - Interval for periodic endpoints
150  * @name: The name array passed to the USB core.
151  * @halted: Set if the endpoint has been halted.
152  * @periodic: Set if this is a periodic ep, such as Interrupt
153  * @isochronous: Set if this is a isochronous ep
154  * @send_zlp: Set if we need to send a zero-length packet.
155  * @total_data: The total number of data bytes done.
156  * @fifo_size: The size of the FIFO (for periodic IN endpoints)
157  * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
158  * @last_load: The offset of data for the last start of request.
159  * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
160  *
161  * This is the driver's state for each registered enpoint, allowing it
162  * to keep track of transactions that need doing. Each endpoint has a
163  * lock to protect the state, to try and avoid using an overall lock
164  * for the host controller as much as possible.
165  *
166  * For periodic IN endpoints, we have fifo_size and fifo_load to try
167  * and keep track of the amount of data in the periodic FIFO for each
168  * of these as we don't have a status register that tells us how much
169  * is in each of them. (note, this may actually be useless information
170  * as in shared-fifo mode periodic in acts like a single-frame packet
171  * buffer than a fifo)
172  */
173 struct dwc2_hsotg_ep {
174         struct usb_ep           ep;
175         struct list_head        queue;
176         struct dwc2_hsotg       *parent;
177         struct dwc2_hsotg_req    *req;
178         struct dentry           *debugfs;
179
180         unsigned long           total_data;
181         unsigned int            size_loaded;
182         unsigned int            last_load;
183         unsigned int            fifo_load;
184         unsigned short          fifo_size;
185         unsigned short          fifo_index;
186
187         unsigned char           dir_in;
188         unsigned char           index;
189         unsigned char           mc;
190         unsigned char           interval;
191
192         unsigned int            halted:1;
193         unsigned int            periodic:1;
194         unsigned int            isochronous:1;
195         unsigned int            send_zlp:1;
196         unsigned int            has_correct_parity:1;
197
198         char                    name[10];
199 };
200
201 /**
202  * struct dwc2_hsotg_req - data transfer request
203  * @req: The USB gadget request
204  * @queue: The list of requests for the endpoint this is queued for.
205  * @saved_req_buf: variable to save req.buf when bounce buffers are used.
206  */
207 struct dwc2_hsotg_req {
208         struct usb_request      req;
209         struct list_head        queue;
210         void *saved_req_buf;
211 };
212
213 #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
214 #define call_gadget(_hs, _entry) \
215 do { \
216         if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
217                 (_hs)->driver && (_hs)->driver->_entry) { \
218                 spin_unlock(&_hs->lock); \
219                 (_hs)->driver->_entry(&(_hs)->gadget); \
220                 spin_lock(&_hs->lock); \
221         } \
222 } while (0)
223 #else
224 #define call_gadget(_hs, _entry)        do {} while (0)
225 #endif
226
227 struct dwc2_hsotg;
228 struct dwc2_host_chan;
229
230 /* Device States */
231 enum dwc2_lx_state {
232         DWC2_L0,        /* On state */
233         DWC2_L1,        /* LPM sleep state */
234         DWC2_L2,        /* USB suspend state */
235         DWC2_L3,        /* Off state */
236 };
237
238 /*
239  * Gadget periodic tx fifo sizes as used by legacy driver
240  * EP0 is not included
241  */
242 #define DWC2_G_P_LEGACY_TX_FIFO_SIZE {256, 256, 256, 256, 768, 768, 768, \
243                                            768, 0, 0, 0, 0, 0, 0, 0}
244
245 /* Gadget ep0 states */
246 enum dwc2_ep0_state {
247         DWC2_EP0_SETUP,
248         DWC2_EP0_DATA_IN,
249         DWC2_EP0_DATA_OUT,
250         DWC2_EP0_STATUS_IN,
251         DWC2_EP0_STATUS_OUT,
252 };
253
254 /**
255  * struct dwc2_core_params - Parameters for configuring the core
256  *
257  * @otg_cap:            Specifies the OTG capabilities.
258  *                       0 - HNP and SRP capable
259  *                       1 - SRP Only capable
260  *                       2 - No HNP/SRP capable (always available)
261  *                      Defaults to best available option (0, 1, then 2)
262  * @otg_ver:            OTG version supported
263  *                       0 - 1.3 (default)
264  *                       1 - 2.0
265  * @dma_enable:         Specifies whether to use slave or DMA mode for accessing
266  *                      the data FIFOs. The driver will automatically detect the
267  *                      value for this parameter if none is specified.
268  *                       0 - Slave (always available)
269  *                       1 - DMA (default, if available)
270  * @dma_desc_enable:    When DMA mode is enabled, specifies whether to use
271  *                      address DMA mode or descriptor DMA mode for accessing
272  *                      the data FIFOs. The driver will automatically detect the
273  *                      value for this if none is specified.
274  *                       0 - Address DMA
275  *                       1 - Descriptor DMA (default, if available)
276  * @dma_desc_fs_enable: When DMA mode is enabled, specifies whether to use
277  *                      address DMA mode or descriptor DMA mode for accessing
278  *                      the data FIFOs in Full Speed mode only. The driver
279  *                      will automatically detect the value for this if none is
280  *                      specified.
281  *                       0 - Address DMA
282  *                       1 - Descriptor DMA in FS (default, if available)
283  * @speed:              Specifies the maximum speed of operation in host and
284  *                      device mode. The actual speed depends on the speed of
285  *                      the attached device and the value of phy_type.
286  *                       0 - High Speed
287  *                           (default when phy_type is UTMI+ or ULPI)
288  *                       1 - Full Speed
289  *                           (default when phy_type is Full Speed)
290  * @enable_dynamic_fifo: 0 - Use coreConsultant-specified FIFO size parameters
291  *                       1 - Allow dynamic FIFO sizing (default, if available)
292  * @en_multiple_tx_fifo: Specifies whether dedicated per-endpoint transmit FIFOs
293  *                      are enabled
294  * @host_rx_fifo_size:  Number of 4-byte words in the Rx FIFO in host mode when
295  *                      dynamic FIFO sizing is enabled
296  *                       16 to 32768
297  *                      Actual maximum value is autodetected and also
298  *                      the default.
299  * @host_nperio_tx_fifo_size: Number of 4-byte words in the non-periodic Tx FIFO
300  *                      in host mode when dynamic FIFO sizing is enabled
301  *                       16 to 32768
302  *                      Actual maximum value is autodetected and also
303  *                      the default.
304  * @host_perio_tx_fifo_size: Number of 4-byte words in the periodic Tx FIFO in
305  *                      host mode when dynamic FIFO sizing is enabled
306  *                       16 to 32768
307  *                      Actual maximum value is autodetected and also
308  *                      the default.
309  * @max_transfer_size:  The maximum transfer size supported, in bytes
310  *                       2047 to 65,535
311  *                      Actual maximum value is autodetected and also
312  *                      the default.
313  * @max_packet_count:   The maximum number of packets in a transfer
314  *                       15 to 511
315  *                      Actual maximum value is autodetected and also
316  *                      the default.
317  * @host_channels:      The number of host channel registers to use
318  *                       1 to 16
319  *                      Actual maximum value is autodetected and also
320  *                      the default.
321  * @phy_type:           Specifies the type of PHY interface to use. By default,
322  *                      the driver will automatically detect the phy_type.
323  *                       0 - Full Speed Phy
324  *                       1 - UTMI+ Phy
325  *                       2 - ULPI Phy
326  *                      Defaults to best available option (2, 1, then 0)
327  * @phy_utmi_width:     Specifies the UTMI+ Data Width (in bits). This parameter
328  *                      is applicable for a phy_type of UTMI+ or ULPI. (For a
329  *                      ULPI phy_type, this parameter indicates the data width
330  *                      between the MAC and the ULPI Wrapper.) Also, this
331  *                      parameter is applicable only if the OTG_HSPHY_WIDTH cC
332  *                      parameter was set to "8 and 16 bits", meaning that the
333  *                      core has been configured to work at either data path
334  *                      width.
335  *                       8 or 16 (default 16 if available)
336  * @phy_ulpi_ddr:       Specifies whether the ULPI operates at double or single
337  *                      data rate. This parameter is only applicable if phy_type
338  *                      is ULPI.
339  *                       0 - single data rate ULPI interface with 8 bit wide
340  *                           data bus (default)
341  *                       1 - double data rate ULPI interface with 4 bit wide
342  *                           data bus
343  * @phy_ulpi_ext_vbus:  For a ULPI phy, specifies whether to use the internal or
344  *                      external supply to drive the VBus
345  *                       0 - Internal supply (default)
346  *                       1 - External supply
347  * @i2c_enable:         Specifies whether to use the I2Cinterface for a full
348  *                      speed PHY. This parameter is only applicable if phy_type
349  *                      is FS.
350  *                       0 - No (default)
351  *                       1 - Yes
352  * @ulpi_fs_ls:         Make ULPI phy operate in FS/LS mode only
353  *                       0 - No (default)
354  *                       1 - Yes
355  * @host_support_fs_ls_low_power: Specifies whether low power mode is supported
356  *                      when attached to a Full Speed or Low Speed device in
357  *                      host mode.
358  *                       0 - Don't support low power mode (default)
359  *                       1 - Support low power mode
360  * @host_ls_low_power_phy_clk: Specifies the PHY clock rate in low power mode
361  *                      when connected to a Low Speed device in host
362  *                      mode. This parameter is applicable only if
363  *                      host_support_fs_ls_low_power is enabled.
364  *                       0 - 48 MHz
365  *                           (default when phy_type is UTMI+ or ULPI)
366  *                       1 - 6 MHz
367  *                           (default when phy_type is Full Speed)
368  * @ts_dline:           Enable Term Select Dline pulsing
369  *                       0 - No (default)
370  *                       1 - Yes
371  * @reload_ctl:         Allow dynamic reloading of HFIR register during runtime
372  *                       0 - No (default for core < 2.92a)
373  *                       1 - Yes (default for core >= 2.92a)
374  * @ahbcfg:             This field allows the default value of the GAHBCFG
375  *                      register to be overridden
376  *                       -1         - GAHBCFG value will be set to 0x06
377  *                                    (INCR4, default)
378  *                       all others - GAHBCFG value will be overridden with
379  *                                    this value
380  *                      Not all bits can be controlled like this, the
381  *                      bits defined by GAHBCFG_CTRL_MASK are controlled
382  *                      by the driver and are ignored in this
383  *                      configuration value.
384  * @uframe_sched:       True to enable the microframe scheduler
385  * @external_id_pin_ctl: Specifies whether ID pin is handled externally.
386  *                      Disable CONIDSTSCHNG controller interrupt in such
387  *                      case.
388  *                      0 - No (default)
389  *                      1 - Yes
390  * @hibernation:        Specifies whether the controller support hibernation.
391  *                      If hibernation is enabled, the controller will enter
392  *                      hibernation in both peripheral and host mode when
393  *                      needed.
394  *                      0 - No (default)
395  *                      1 - Yes
396  *
397  * The following parameters may be specified when starting the module. These
398  * parameters define how the DWC_otg controller should be configured. A
399  * value of -1 (or any other out of range value) for any parameter means
400  * to read the value from hardware (if possible) or use the builtin
401  * default described above.
402  */
403 struct dwc2_core_params {
404         /*
405          * Don't add any non-int members here, this will break
406          * dwc2_set_all_params!
407          */
408         int otg_cap;
409         int otg_ver;
410         int dma_enable;
411         int dma_desc_enable;
412         int dma_desc_fs_enable;
413         int speed;
414         int enable_dynamic_fifo;
415         int en_multiple_tx_fifo;
416         int host_rx_fifo_size;
417         int host_nperio_tx_fifo_size;
418         int host_perio_tx_fifo_size;
419         int max_transfer_size;
420         int max_packet_count;
421         int host_channels;
422         int phy_type;
423         int phy_utmi_width;
424         int phy_ulpi_ddr;
425         int phy_ulpi_ext_vbus;
426         int i2c_enable;
427         int ulpi_fs_ls;
428         int host_support_fs_ls_low_power;
429         int host_ls_low_power_phy_clk;
430         int ts_dline;
431         int reload_ctl;
432         int ahbcfg;
433         int uframe_sched;
434         int external_id_pin_ctl;
435         int hibernation;
436 };
437
438 /**
439  * struct dwc2_hw_params - Autodetected parameters.
440  *
441  * These parameters are the various parameters read from hardware
442  * registers during initialization. They typically contain the best
443  * supported or maximum value that can be configured in the
444  * corresponding dwc2_core_params value.
445  *
446  * The values that are not in dwc2_core_params are documented below.
447  *
448  * @op_mode             Mode of Operation
449  *                       0 - HNP- and SRP-Capable OTG (Host & Device)
450  *                       1 - SRP-Capable OTG (Host & Device)
451  *                       2 - Non-HNP and Non-SRP Capable OTG (Host & Device)
452  *                       3 - SRP-Capable Device
453  *                       4 - Non-OTG Device
454  *                       5 - SRP-Capable Host
455  *                       6 - Non-OTG Host
456  * @arch                Architecture
457  *                       0 - Slave only
458  *                       1 - External DMA
459  *                       2 - Internal DMA
460  * @power_optimized     Are power optimizations enabled?
461  * @num_dev_ep          Number of device endpoints available
462  * @num_dev_perio_in_ep Number of device periodic IN endpoints
463  *                      available
464  * @dev_token_q_depth   Device Mode IN Token Sequence Learning Queue
465  *                      Depth
466  *                       0 to 30
467  * @host_perio_tx_q_depth
468  *                      Host Mode Periodic Request Queue Depth
469  *                       2, 4 or 8
470  * @nperio_tx_q_depth
471  *                      Non-Periodic Request Queue Depth
472  *                       2, 4 or 8
473  * @hs_phy_type         High-speed PHY interface type
474  *                       0 - High-speed interface not supported
475  *                       1 - UTMI+
476  *                       2 - ULPI
477  *                       3 - UTMI+ and ULPI
478  * @fs_phy_type         Full-speed PHY interface type
479  *                       0 - Full speed interface not supported
480  *                       1 - Dedicated full speed interface
481  *                       2 - FS pins shared with UTMI+ pins
482  *                       3 - FS pins shared with ULPI pins
483  * @total_fifo_size:    Total internal RAM for FIFOs (bytes)
484  * @utmi_phy_data_width UTMI+ PHY data width
485  *                       0 - 8 bits
486  *                       1 - 16 bits
487  *                       2 - 8 or 16 bits
488  * @snpsid:             Value from SNPSID register
489  * @dev_ep_dirs:        Direction of device endpoints (GHWCFG1)
490  */
491 struct dwc2_hw_params {
492         unsigned op_mode:3;
493         unsigned arch:2;
494         unsigned dma_desc_enable:1;
495         unsigned dma_desc_fs_enable:1;
496         unsigned enable_dynamic_fifo:1;
497         unsigned en_multiple_tx_fifo:1;
498         unsigned host_rx_fifo_size:16;
499         unsigned host_nperio_tx_fifo_size:16;
500         unsigned dev_nperio_tx_fifo_size:16;
501         unsigned host_perio_tx_fifo_size:16;
502         unsigned nperio_tx_q_depth:3;
503         unsigned host_perio_tx_q_depth:3;
504         unsigned dev_token_q_depth:5;
505         unsigned max_transfer_size:26;
506         unsigned max_packet_count:11;
507         unsigned host_channels:5;
508         unsigned hs_phy_type:2;
509         unsigned fs_phy_type:2;
510         unsigned i2c_enable:1;
511         unsigned num_dev_ep:4;
512         unsigned num_dev_perio_in_ep:4;
513         unsigned total_fifo_size:16;
514         unsigned power_optimized:1;
515         unsigned utmi_phy_data_width:2;
516         u32 snpsid;
517         u32 dev_ep_dirs;
518 };
519
520 /* Size of control and EP0 buffers */
521 #define DWC2_CTRL_BUFF_SIZE 8
522
523 /**
524  * struct dwc2_gregs_backup - Holds global registers state before entering partial
525  * power down
526  * @gotgctl:            Backup of GOTGCTL register
527  * @gintmsk:            Backup of GINTMSK register
528  * @gahbcfg:            Backup of GAHBCFG register
529  * @gusbcfg:            Backup of GUSBCFG register
530  * @grxfsiz:            Backup of GRXFSIZ register
531  * @gnptxfsiz:          Backup of GNPTXFSIZ register
532  * @gi2cctl:            Backup of GI2CCTL register
533  * @hptxfsiz:           Backup of HPTXFSIZ register
534  * @gdfifocfg:          Backup of GDFIFOCFG register
535  * @dtxfsiz:            Backup of DTXFSIZ registers for each endpoint
536  * @gpwrdn:             Backup of GPWRDN register
537  */
538 struct dwc2_gregs_backup {
539         u32 gotgctl;
540         u32 gintmsk;
541         u32 gahbcfg;
542         u32 gusbcfg;
543         u32 grxfsiz;
544         u32 gnptxfsiz;
545         u32 gi2cctl;
546         u32 hptxfsiz;
547         u32 pcgcctl;
548         u32 gdfifocfg;
549         u32 dtxfsiz[MAX_EPS_CHANNELS];
550         u32 gpwrdn;
551         bool valid;
552 };
553
554 /**
555  * struct  dwc2_dregs_backup - Holds device registers state before entering partial
556  * power down
557  * @dcfg:               Backup of DCFG register
558  * @dctl:               Backup of DCTL register
559  * @daintmsk:           Backup of DAINTMSK register
560  * @diepmsk:            Backup of DIEPMSK register
561  * @doepmsk:            Backup of DOEPMSK register
562  * @diepctl:            Backup of DIEPCTL register
563  * @dieptsiz:           Backup of DIEPTSIZ register
564  * @diepdma:            Backup of DIEPDMA register
565  * @doepctl:            Backup of DOEPCTL register
566  * @doeptsiz:           Backup of DOEPTSIZ register
567  * @doepdma:            Backup of DOEPDMA register
568  */
569 struct dwc2_dregs_backup {
570         u32 dcfg;
571         u32 dctl;
572         u32 daintmsk;
573         u32 diepmsk;
574         u32 doepmsk;
575         u32 diepctl[MAX_EPS_CHANNELS];
576         u32 dieptsiz[MAX_EPS_CHANNELS];
577         u32 diepdma[MAX_EPS_CHANNELS];
578         u32 doepctl[MAX_EPS_CHANNELS];
579         u32 doeptsiz[MAX_EPS_CHANNELS];
580         u32 doepdma[MAX_EPS_CHANNELS];
581         bool valid;
582 };
583
584 /**
585  * struct  dwc2_hregs_backup - Holds host registers state before entering partial
586  * power down
587  * @hcfg:               Backup of HCFG register
588  * @haintmsk:           Backup of HAINTMSK register
589  * @hcintmsk:           Backup of HCINTMSK register
590  * @hptr0:              Backup of HPTR0 register
591  * @hfir:               Backup of HFIR register
592  */
593 struct dwc2_hregs_backup {
594         u32 hcfg;
595         u32 haintmsk;
596         u32 hcintmsk[MAX_EPS_CHANNELS];
597         u32 hprt0;
598         u32 hfir;
599         bool valid;
600 };
601
602 /**
603  * struct dwc2_hsotg - Holds the state of the driver, including the non-periodic
604  * and periodic schedules
605  *
606  * These are common for both host and peripheral modes:
607  *
608  * @dev:                The struct device pointer
609  * @regs:               Pointer to controller regs
610  * @hw_params:          Parameters that were autodetected from the
611  *                      hardware registers
612  * @core_params:        Parameters that define how the core should be configured
613  * @op_state:           The operational State, during transitions (a_host=>
614  *                      a_peripheral and b_device=>b_host) this may not match
615  *                      the core, but allows the software to determine
616  *                      transitions
617  * @dr_mode:            Requested mode of operation, one of following:
618  *                      - USB_DR_MODE_PERIPHERAL
619  *                      - USB_DR_MODE_HOST
620  *                      - USB_DR_MODE_OTG
621  * @hcd_enabled         Host mode sub-driver initialization indicator.
622  * @gadget_enabled      Peripheral mode sub-driver initialization indicator.
623  * @ll_hw_enabled       Status of low-level hardware resources.
624  * @phy:                The otg phy transceiver structure for phy control.
625  * @uphy:               The otg phy transceiver structure for old USB phy control.
626  * @plat:               The platform specific configuration data. This can be removed once
627  *                      all SoCs support usb transceiver.
628  * @supplies:           Definition of USB power supplies
629  * @phyif:              PHY interface width
630  * @lock:               Spinlock that protects all the driver data structures
631  * @priv:               Stores a pointer to the struct usb_hcd
632  * @queuing_high_bandwidth: True if multiple packets of a high-bandwidth
633  *                      transfer are in process of being queued
634  * @srp_success:        Stores status of SRP request in the case of a FS PHY
635  *                      with an I2C interface
636  * @wq_otg:             Workqueue object used for handling of some interrupts
637  * @wf_otg:             Work object for handling Connector ID Status Change
638  *                      interrupt
639  * @wkp_timer:          Timer object for handling Wakeup Detected interrupt
640  * @lx_state:           Lx state of connected device
641  * @gregs_backup: Backup of global registers during suspend
642  * @dregs_backup: Backup of device registers during suspend
643  * @hregs_backup: Backup of host registers during suspend
644  *
645  * These are for host mode:
646  *
647  * @flags:              Flags for handling root port state changes
648  * @non_periodic_sched_inactive: Inactive QHs in the non-periodic schedule.
649  *                      Transfers associated with these QHs are not currently
650  *                      assigned to a host channel.
651  * @non_periodic_sched_active: Active QHs in the non-periodic schedule.
652  *                      Transfers associated with these QHs are currently
653  *                      assigned to a host channel.
654  * @non_periodic_qh_ptr: Pointer to next QH to process in the active
655  *                      non-periodic schedule
656  * @periodic_sched_inactive: Inactive QHs in the periodic schedule. This is a
657  *                      list of QHs for periodic transfers that are _not_
658  *                      scheduled for the next frame. Each QH in the list has an
659  *                      interval counter that determines when it needs to be
660  *                      scheduled for execution. This scheduling mechanism
661  *                      allows only a simple calculation for periodic bandwidth
662  *                      used (i.e. must assume that all periodic transfers may
663  *                      need to execute in the same frame). However, it greatly
664  *                      simplifies scheduling and should be sufficient for the
665  *                      vast majority of OTG hosts, which need to connect to a
666  *                      small number of peripherals at one time. Items move from
667  *                      this list to periodic_sched_ready when the QH interval
668  *                      counter is 0 at SOF.
669  * @periodic_sched_ready:  List of periodic QHs that are ready for execution in
670  *                      the next frame, but have not yet been assigned to host
671  *                      channels. Items move from this list to
672  *                      periodic_sched_assigned as host channels become
673  *                      available during the current frame.
674  * @periodic_sched_assigned: List of periodic QHs to be executed in the next
675  *                      frame that are assigned to host channels. Items move
676  *                      from this list to periodic_sched_queued as the
677  *                      transactions for the QH are queued to the DWC_otg
678  *                      controller.
679  * @periodic_sched_queued: List of periodic QHs that have been queued for
680  *                      execution. Items move from this list to either
681  *                      periodic_sched_inactive or periodic_sched_ready when the
682  *                      channel associated with the transfer is released. If the
683  *                      interval for the QH is 1, the item moves to
684  *                      periodic_sched_ready because it must be rescheduled for
685  *                      the next frame. Otherwise, the item moves to
686  *                      periodic_sched_inactive.
687  * @periodic_usecs:     Total bandwidth claimed so far for periodic transfers.
688  *                      This value is in microseconds per (micro)frame. The
689  *                      assumption is that all periodic transfers may occur in
690  *                      the same (micro)frame.
691  * @frame_usecs:        Internal variable used by the microframe scheduler
692  * @frame_number:       Frame number read from the core at SOF. The value ranges
693  *                      from 0 to HFNUM_MAX_FRNUM.
694  * @periodic_qh_count:  Count of periodic QHs, if using several eps. Used for
695  *                      SOF enable/disable.
696  * @free_hc_list:       Free host channels in the controller. This is a list of
697  *                      struct dwc2_host_chan items.
698  * @periodic_channels:  Number of host channels assigned to periodic transfers.
699  *                      Currently assuming that there is a dedicated host
700  *                      channel for each periodic transaction and at least one
701  *                      host channel is available for non-periodic transactions.
702  * @non_periodic_channels: Number of host channels assigned to non-periodic
703  *                      transfers
704  * @available_host_channels Number of host channels available for the microframe
705  *                      scheduler to use
706  * @hc_ptr_array:       Array of pointers to the host channel descriptors.
707  *                      Allows accessing a host channel descriptor given the
708  *                      host channel number. This is useful in interrupt
709  *                      handlers.
710  * @status_buf:         Buffer used for data received during the status phase of
711  *                      a control transfer.
712  * @status_buf_dma:     DMA address for status_buf
713  * @start_work:         Delayed work for handling host A-cable connection
714  * @reset_work:         Delayed work for handling a port reset
715  * @otg_port:           OTG port number
716  * @frame_list:         Frame list
717  * @frame_list_dma:     Frame list DMA address
718  * @frame_list_sz:      Frame list size
719  * @desc_gen_cache:     Kmem cache for generic descriptors
720  * @desc_hsisoc_cache:  Kmem cache for hs isochronous descriptors
721  *
722  * These are for peripheral mode:
723  *
724  * @driver:             USB gadget driver
725  * @dedicated_fifos:    Set if the hardware has dedicated IN-EP fifos.
726  * @num_of_eps:         Number of available EPs (excluding EP0)
727  * @debug_root:         Root directrory for debugfs.
728  * @debug_file:         Main status file for debugfs.
729  * @debug_testmode:     Testmode status file for debugfs.
730  * @debug_fifo:         FIFO status file for debugfs.
731  * @ep0_reply:          Request used for ep0 reply.
732  * @ep0_buff:           Buffer for EP0 reply data, if needed.
733  * @ctrl_buff:          Buffer for EP0 control requests.
734  * @ctrl_req:           Request for EP0 control packets.
735  * @ep0_state:          EP0 control transfers state
736  * @test_mode:          USB test mode requested by the host
737  * @eps:                The endpoints being supplied to the gadget framework
738  * @g_using_dma:          Indicate if dma usage is enabled
739  * @g_rx_fifo_sz:         Contains rx fifo size value
740  * @g_np_g_tx_fifo_sz:      Contains Non-Periodic tx fifo size value
741  * @g_tx_fifo_sz:         Contains tx fifo size value per endpoints
742  */
743 struct dwc2_hsotg {
744         struct device *dev;
745         void __iomem *regs;
746         /** Params detected from hardware */
747         struct dwc2_hw_params hw_params;
748         /** Params to actually use */
749         struct dwc2_core_params *core_params;
750         enum usb_otg_state op_state;
751         enum usb_dr_mode dr_mode;
752         unsigned int hcd_enabled:1;
753         unsigned int gadget_enabled:1;
754         unsigned int ll_hw_enabled:1;
755
756         struct phy *phy;
757         struct work_struct phy_rst_work;
758         struct usb_phy *uphy;
759         struct dwc2_hsotg_plat *plat;
760         struct regulator_bulk_data supplies[ARRAY_SIZE(dwc2_hsotg_supply_names)];
761         u32 phyif;
762
763         spinlock_t lock;
764         void *priv;
765         int     irq;
766         struct clk *clk;
767
768         unsigned int queuing_high_bandwidth:1;
769         unsigned int srp_success:1;
770
771         struct workqueue_struct *wq_otg;
772         struct work_struct wf_otg;
773         struct timer_list wkp_timer;
774         enum dwc2_lx_state lx_state;
775         struct dwc2_gregs_backup gr_backup;
776         struct dwc2_dregs_backup dr_backup;
777         struct dwc2_hregs_backup hr_backup;
778
779         struct dentry *debug_root;
780         struct debugfs_regset32 *regset;
781
782         /* DWC OTG HW Release versions */
783 #define DWC2_CORE_REV_2_71a     0x4f54271a
784 #define DWC2_CORE_REV_2_90a     0x4f54290a
785 #define DWC2_CORE_REV_2_92a     0x4f54292a
786 #define DWC2_CORE_REV_2_94a     0x4f54294a
787 #define DWC2_CORE_REV_3_00a     0x4f54300a
788
789 #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
790         union dwc2_hcd_internal_flags {
791                 u32 d32;
792                 struct {
793                         unsigned port_connect_status_change:1;
794                         unsigned port_connect_status:1;
795                         unsigned port_reset_change:1;
796                         unsigned port_enable_change:1;
797                         unsigned port_suspend_change:1;
798                         unsigned port_over_current_change:1;
799                         unsigned port_l1_change:1;
800                         unsigned reserved:25;
801                 } b;
802         } flags;
803
804         struct list_head non_periodic_sched_inactive;
805         struct list_head non_periodic_sched_active;
806         struct list_head *non_periodic_qh_ptr;
807         struct list_head periodic_sched_inactive;
808         struct list_head periodic_sched_ready;
809         struct list_head periodic_sched_assigned;
810         struct list_head periodic_sched_queued;
811         u16 periodic_usecs;
812         u16 frame_usecs[8];
813         u16 frame_number;
814         u16 periodic_qh_count;
815         bool bus_suspended;
816         bool new_connection;
817
818 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
819 #define FRAME_NUM_ARRAY_SIZE 1000
820         u16 last_frame_num;
821         u16 *frame_num_array;
822         u16 *last_frame_num_array;
823         int frame_num_idx;
824         int dumped_frame_num_array;
825 #endif
826
827         struct list_head free_hc_list;
828         int periodic_channels;
829         int non_periodic_channels;
830         int available_host_channels;
831         struct dwc2_host_chan *hc_ptr_array[MAX_EPS_CHANNELS];
832         u8 *status_buf;
833         dma_addr_t status_buf_dma;
834 #define DWC2_HCD_STATUS_BUF_SIZE 64
835
836         struct delayed_work start_work;
837         struct delayed_work reset_work;
838         u8 otg_port;
839         u32 *frame_list;
840         dma_addr_t frame_list_dma;
841         u32 frame_list_sz;
842         struct kmem_cache *desc_gen_cache;
843         struct kmem_cache *desc_hsisoc_cache;
844
845 #ifdef DEBUG
846         u32 frrem_samples;
847         u64 frrem_accum;
848
849         u32 hfnum_7_samples_a;
850         u64 hfnum_7_frrem_accum_a;
851         u32 hfnum_0_samples_a;
852         u64 hfnum_0_frrem_accum_a;
853         u32 hfnum_other_samples_a;
854         u64 hfnum_other_frrem_accum_a;
855
856         u32 hfnum_7_samples_b;
857         u64 hfnum_7_frrem_accum_b;
858         u32 hfnum_0_samples_b;
859         u64 hfnum_0_frrem_accum_b;
860         u32 hfnum_other_samples_b;
861         u64 hfnum_other_frrem_accum_b;
862 #endif
863 #endif /* CONFIG_USB_DWC2_HOST || CONFIG_USB_DWC2_DUAL_ROLE */
864
865 #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
866         /* Gadget structures */
867         struct usb_gadget_driver *driver;
868         int fifo_mem;
869         unsigned int dedicated_fifos:1;
870         unsigned char num_of_eps;
871         u32 fifo_map;
872
873         struct usb_request *ep0_reply;
874         struct usb_request *ctrl_req;
875         void *ep0_buff;
876         void *ctrl_buff;
877         enum dwc2_ep0_state ep0_state;
878         u8 test_mode;
879
880         struct usb_gadget gadget;
881         unsigned int enabled:1;
882         unsigned int connected:1;
883         struct dwc2_hsotg_ep *eps_in[MAX_EPS_CHANNELS];
884         struct dwc2_hsotg_ep *eps_out[MAX_EPS_CHANNELS];
885         u32 g_using_dma;
886         u32 g_rx_fifo_sz;
887         u32 g_np_g_tx_fifo_sz;
888         u32 g_tx_fifo_sz[MAX_EPS_CHANNELS];
889 #endif /* CONFIG_USB_DWC2_PERIPHERAL || CONFIG_USB_DWC2_DUAL_ROLE */
890 };
891
892 /* Reasons for halting a host channel */
893 enum dwc2_halt_status {
894         DWC2_HC_XFER_NO_HALT_STATUS,
895         DWC2_HC_XFER_COMPLETE,
896         DWC2_HC_XFER_URB_COMPLETE,
897         DWC2_HC_XFER_ACK,
898         DWC2_HC_XFER_NAK,
899         DWC2_HC_XFER_NYET,
900         DWC2_HC_XFER_STALL,
901         DWC2_HC_XFER_XACT_ERR,
902         DWC2_HC_XFER_FRAME_OVERRUN,
903         DWC2_HC_XFER_BABBLE_ERR,
904         DWC2_HC_XFER_DATA_TOGGLE_ERR,
905         DWC2_HC_XFER_AHB_ERR,
906         DWC2_HC_XFER_PERIODIC_INCOMPLETE,
907         DWC2_HC_XFER_URB_DEQUEUE,
908 };
909
910 /*
911  * The following functions support initialization of the core driver component
912  * and the DWC_otg controller
913  */
914 extern int dwc2_core_reset(struct dwc2_hsotg *hsotg);
915 extern int dwc2_core_reset_and_force_dr_mode(struct dwc2_hsotg *hsotg);
916 extern void dwc2_core_host_init(struct dwc2_hsotg *hsotg);
917 extern int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg);
918 extern int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore);
919
920 void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg);
921
922 /*
923  * Host core Functions.
924  * The following functions support managing the DWC_otg controller in host
925  * mode.
926  */
927 extern void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan);
928 extern void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
929                          enum dwc2_halt_status halt_status);
930 extern void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg,
931                             struct dwc2_host_chan *chan);
932 extern void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
933                                    struct dwc2_host_chan *chan);
934 extern void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
935                                         struct dwc2_host_chan *chan);
936 extern int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
937                                      struct dwc2_host_chan *chan);
938 extern void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
939                             struct dwc2_host_chan *chan);
940 extern void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg);
941 extern void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg);
942
943 extern u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg);
944 extern bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg);
945
946 /*
947  * Common core Functions.
948  * The following functions support managing the DWC_otg controller in either
949  * device or host mode.
950  */
951 extern void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes);
952 extern void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num);
953 extern void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg);
954
955 extern int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup);
956 extern void dwc2_enable_global_interrupts(struct dwc2_hsotg *hcd);
957 extern void dwc2_disable_global_interrupts(struct dwc2_hsotg *hcd);
958
959 /* This function should be called on every hardware interrupt. */
960 extern irqreturn_t dwc2_handle_common_intr(int irq, void *dev);
961
962 /* OTG Core Parameters */
963
964 /*
965  * Specifies the OTG capabilities. The driver will automatically
966  * detect the value for this parameter if none is specified.
967  * 0 - HNP and SRP capable (default)
968  * 1 - SRP Only capable
969  * 2 - No HNP/SRP capable
970  */
971 extern void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val);
972 #define DWC2_CAP_PARAM_HNP_SRP_CAPABLE          0
973 #define DWC2_CAP_PARAM_SRP_ONLY_CAPABLE         1
974 #define DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE       2
975
976 /*
977  * Specifies whether to use slave or DMA mode for accessing the data
978  * FIFOs. The driver will automatically detect the value for this
979  * parameter if none is specified.
980  * 0 - Slave
981  * 1 - DMA (default, if available)
982  */
983 extern void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val);
984
985 /*
986  * When DMA mode is enabled specifies whether to use
987  * address DMA or DMA Descritor mode for accessing the data
988  * FIFOs in device mode. The driver will automatically detect
989  * the value for this parameter if none is specified.
990  * 0 - address DMA
991  * 1 - DMA Descriptor(default, if available)
992  */
993 extern void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val);
994
995 /*
996  * When DMA mode is enabled specifies whether to use
997  * address DMA or DMA Descritor mode with full speed devices
998  * for accessing the data FIFOs in host mode.
999  * 0 - address DMA
1000  * 1 - FS DMA Descriptor(default, if available)
1001  */
1002 extern void dwc2_set_param_dma_desc_fs_enable(struct dwc2_hsotg *hsotg,
1003                                               int val);
1004
1005 /*
1006  * Specifies the maximum speed of operation in host and device mode.
1007  * The actual speed depends on the speed of the attached device and
1008  * the value of phy_type. The actual speed depends on the speed of the
1009  * attached device.
1010  * 0 - High Speed (default)
1011  * 1 - Full Speed
1012  */
1013 extern void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val);
1014 #define DWC2_SPEED_PARAM_HIGH   0
1015 #define DWC2_SPEED_PARAM_FULL   1
1016
1017 /*
1018  * Specifies whether low power mode is supported when attached
1019  * to a Full Speed or Low Speed device in host mode.
1020  *
1021  * 0 - Don't support low power mode (default)
1022  * 1 - Support low power mode
1023  */
1024 extern void dwc2_set_param_host_support_fs_ls_low_power(
1025                 struct dwc2_hsotg *hsotg, int val);
1026
1027 /*
1028  * Specifies the PHY clock rate in low power mode when connected to a
1029  * Low Speed device in host mode. This parameter is applicable only if
1030  * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
1031  * then defaults to 6 MHZ otherwise 48 MHZ.
1032  *
1033  * 0 - 48 MHz
1034  * 1 - 6 MHz
1035  */
1036 extern void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg,
1037                                                      int val);
1038 #define DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ      0
1039 #define DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ       1
1040
1041 /*
1042  * 0 - Use cC FIFO size parameters
1043  * 1 - Allow dynamic FIFO sizing (default)
1044  */
1045 extern void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg,
1046                                                int val);
1047
1048 /*
1049  * Number of 4-byte words in the Rx FIFO in host mode when dynamic
1050  * FIFO sizing is enabled.
1051  * 16 to 32768 (default 1024)
1052  */
1053 extern void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val);
1054
1055 /*
1056  * Number of 4-byte words in the non-periodic Tx FIFO in host mode
1057  * when Dynamic FIFO sizing is enabled in the core.
1058  * 16 to 32768 (default 256)
1059  */
1060 extern void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg,
1061                                                     int val);
1062
1063 /*
1064  * Number of 4-byte words in the host periodic Tx FIFO when dynamic
1065  * FIFO sizing is enabled.
1066  * 16 to 32768 (default 256)
1067  */
1068 extern void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg,
1069                                                    int val);
1070
1071 /*
1072  * The maximum transfer size supported in bytes.
1073  * 2047 to 65,535  (default 65,535)
1074  */
1075 extern void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val);
1076
1077 /*
1078  * The maximum number of packets in a transfer.
1079  * 15 to 511  (default 511)
1080  */
1081 extern void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val);
1082
1083 /*
1084  * The number of host channel registers to use.
1085  * 1 to 16 (default 11)
1086  * Note: The FPGA configuration supports a maximum of 11 host channels.
1087  */
1088 extern void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val);
1089
1090 /*
1091  * Specifies the type of PHY interface to use. By default, the driver
1092  * will automatically detect the phy_type.
1093  *
1094  * 0 - Full Speed PHY
1095  * 1 - UTMI+ (default)
1096  * 2 - ULPI
1097  */
1098 extern void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val);
1099 #define DWC2_PHY_TYPE_PARAM_FS          0
1100 #define DWC2_PHY_TYPE_PARAM_UTMI        1
1101 #define DWC2_PHY_TYPE_PARAM_ULPI        2
1102
1103 /*
1104  * Specifies the UTMI+ Data Width. This parameter is
1105  * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
1106  * PHY_TYPE, this parameter indicates the data width between
1107  * the MAC and the ULPI Wrapper.) Also, this parameter is
1108  * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
1109  * to "8 and 16 bits", meaning that the core has been
1110  * configured to work at either data path width.
1111  *
1112  * 8 or 16 bits (default 16)
1113  */
1114 extern void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val);
1115
1116 /*
1117  * Specifies whether the ULPI operates at double or single
1118  * data rate. This parameter is only applicable if PHY_TYPE is
1119  * ULPI.
1120  *
1121  * 0 - single data rate ULPI interface with 8 bit wide data
1122  * bus (default)
1123  * 1 - double data rate ULPI interface with 4 bit wide data
1124  * bus
1125  */
1126 extern void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val);
1127
1128 /*
1129  * Specifies whether to use the internal or external supply to
1130  * drive the vbus with a ULPI phy.
1131  */
1132 extern void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val);
1133 #define DWC2_PHY_ULPI_INTERNAL_VBUS     0
1134 #define DWC2_PHY_ULPI_EXTERNAL_VBUS     1
1135
1136 /*
1137  * Specifies whether to use the I2Cinterface for full speed PHY. This
1138  * parameter is only applicable if PHY_TYPE is FS.
1139  * 0 - No (default)
1140  * 1 - Yes
1141  */
1142 extern void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val);
1143
1144 extern void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val);
1145
1146 extern void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val);
1147
1148 /*
1149  * Specifies whether dedicated transmit FIFOs are
1150  * enabled for non periodic IN endpoints in device mode
1151  * 0 - No
1152  * 1 - Yes
1153  */
1154 extern void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg,
1155                                                int val);
1156
1157 extern void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val);
1158
1159 extern void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val);
1160
1161 extern void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val);
1162
1163 extern void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
1164                                 const struct dwc2_core_params *params);
1165
1166 extern void dwc2_set_all_params(struct dwc2_core_params *params, int value);
1167
1168 extern int dwc2_get_hwparams(struct dwc2_hsotg *hsotg);
1169
1170 extern int dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg);
1171 extern int dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg);
1172
1173 /*
1174  * The following functions check the controller's OTG operation mode
1175  * capability (GHWCFG2.OTG_MODE).
1176  *
1177  * These functions can be used before the internal hsotg->hw_params
1178  * are read in and cached so they always read directly from the
1179  * GHWCFG2 register.
1180  */
1181 unsigned dwc2_op_mode(struct dwc2_hsotg *hsotg);
1182 bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg);
1183 bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg);
1184 bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg);
1185
1186 /*
1187  * Returns the mode of operation, host or device
1188  */
1189 static inline int dwc2_is_host_mode(struct dwc2_hsotg *hsotg)
1190 {
1191         return (dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_CURMODE_HOST) != 0;
1192 }
1193 static inline int dwc2_is_device_mode(struct dwc2_hsotg *hsotg)
1194 {
1195         return (dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_CURMODE_HOST) == 0;
1196 }
1197
1198 /*
1199  * Dump core registers and SPRAM
1200  */
1201 extern void dwc2_dump_dev_registers(struct dwc2_hsotg *hsotg);
1202 extern void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg);
1203 extern void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg);
1204
1205 /*
1206  * Return OTG version - either 1.3 or 2.0
1207  */
1208 extern u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg);
1209
1210 /* Gadget defines */
1211 #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
1212 extern int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg);
1213 extern int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2);
1214 extern int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2);
1215 extern int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq);
1216 extern void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
1217                 bool reset);
1218 extern void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg);
1219 extern void dwc2_hsotg_disconnect(struct dwc2_hsotg *dwc2);
1220 extern int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode);
1221 #define dwc2_is_device_connected(hsotg) (hsotg->connected)
1222 #else
1223 static inline int dwc2_hsotg_remove(struct dwc2_hsotg *dwc2)
1224 { return 0; }
1225 static inline int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2)
1226 { return 0; }
1227 static inline int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2)
1228 { return 0; }
1229 static inline int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
1230 { return 0; }
1231 static inline void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
1232                 bool reset) {}
1233 static inline void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg) {}
1234 static inline void dwc2_hsotg_disconnect(struct dwc2_hsotg *dwc2) {}
1235 static inline int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg,
1236                                                         int testmode)
1237 { return 0; }
1238 #define dwc2_is_device_connected(hsotg) (0)
1239 #endif
1240
1241 #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
1242 extern int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg);
1243 extern void dwc2_hcd_connect(struct dwc2_hsotg *hsotg);
1244 extern void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force);
1245 extern void dwc2_hcd_start(struct dwc2_hsotg *hsotg);
1246 #else
1247 static inline int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
1248 { return 0; }
1249 static inline void dwc2_hcd_connect(struct dwc2_hsotg *hsotg) {}
1250 static inline void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force) {}
1251 static inline void dwc2_hcd_start(struct dwc2_hsotg *hsotg) {}
1252 static inline void dwc2_hcd_remove(struct dwc2_hsotg *hsotg) {}
1253 static inline int dwc2_hcd_init(struct dwc2_hsotg *hsotg, int irq)
1254 { return 0; }
1255 #endif
1256
1257 #endif /* __DWC2_CORE_H__ */