ba505f14160685683d3fba4c64ca680abf78e65d
[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 s3c_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  * @sent_zlp: Set if we've sent 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 s3c_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
143         unsigned char           dir_in;
144         unsigned char           index;
145         unsigned char           mc;
146         unsigned char           interval;
147
148         unsigned int            halted:1;
149         unsigned int            periodic:1;
150         unsigned int            isochronous:1;
151         unsigned int            sent_zlp:1;
152
153         char                    name[10];
154 };
155
156 /**
157  * struct s3c_hsotg - driver state.
158  * @dev: The parent device supplied to the probe function
159  * @driver: USB gadget driver
160  * @phy: The otg phy transceiver structure for phy control.
161  * @uphy: The otg phy transceiver structure for old USB phy control.
162  * @plat: The platform specific configuration data. This can be removed once
163  * all SoCs support usb transceiver.
164  * @regs: The memory area mapped for accessing registers.
165  * @irq: The IRQ number we are using
166  * @supplies: Definition of USB power supplies
167  * @phyif: PHY interface width
168  * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos.
169  * @num_of_eps: Number of available EPs (excluding EP0)
170  * @debug_root: root directrory for debugfs.
171  * @debug_file: main status file for debugfs.
172  * @debug_fifo: FIFO status file for debugfs.
173  * @ep0_reply: Request used for ep0 reply.
174  * @ep0_buff: Buffer for EP0 reply data, if needed.
175  * @ctrl_buff: Buffer for EP0 control requests.
176  * @ctrl_req: Request for EP0 control packets.
177  * @setup: NAK management for EP0 SETUP
178  * @last_rst: Time of last reset
179  * @eps: The endpoints being supplied to the gadget framework
180  */
181 struct s3c_hsotg {
182         struct device            *dev;
183         struct usb_gadget_driver *driver;
184         struct phy               *phy;
185         struct usb_phy           *uphy;
186         struct s3c_hsotg_plat    *plat;
187
188         spinlock_t              lock;
189
190         void __iomem            *regs;
191         int                     irq;
192         struct clk              *clk;
193
194         struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsotg_supply_names)];
195
196         u32                     phyif;
197         int                     fifo_mem;
198         unsigned int            dedicated_fifos:1;
199         unsigned char           num_of_eps;
200
201         struct dentry           *debug_root;
202         struct dentry           *debug_file;
203         struct dentry           *debug_fifo;
204
205         struct usb_request      *ep0_reply;
206         struct usb_request      *ctrl_req;
207         u8                      ep0_buff[8];
208         u8                      ctrl_buff[8];
209
210         struct usb_gadget       gadget;
211         unsigned int            setup;
212         unsigned long           last_rst;
213         struct s3c_hsotg_ep     *eps;
214 };
215
216 /**
217  * struct s3c_hsotg_req - data transfer request
218  * @req: The USB gadget request
219  * @queue: The list of requests for the endpoint this is queued for.
220  * @in_progress: Has already had size/packets written to core
221  * @mapped: DMA buffer for this request has been mapped via dma_map_single().
222  */
223 struct s3c_hsotg_req {
224         struct usb_request      req;
225         struct list_head        queue;
226         unsigned char           in_progress;
227         unsigned char           mapped;
228 };
229
230 #define call_gadget(_hs, _entry) \
231 do { \
232         if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
233                 (_hs)->driver && (_hs)->driver->_entry) { \
234                 spin_unlock(&_hs->lock); \
235                 (_hs)->driver->_entry(&(_hs)->gadget); \
236                 spin_lock(&_hs->lock); \
237         } \
238 } while (0)
239
240 struct dwc2_hsotg;
241 struct dwc2_host_chan;
242
243 /* Device States */
244 enum dwc2_lx_state {
245         DWC2_L0,        /* On state */
246         DWC2_L1,        /* LPM sleep state */
247         DWC2_L2,        /* USB suspend state */
248         DWC2_L3,        /* Off state */
249 };
250
251 /**
252  * struct dwc2_core_params - Parameters for configuring the core
253  *
254  * @otg_cap:            Specifies the OTG capabilities.
255  *                       0 - HNP and SRP capable
256  *                       1 - SRP Only capable
257  *                       2 - No HNP/SRP capable (always available)
258  *                      Defaults to best available option (0, 1, then 2)
259  * @otg_ver:            OTG version supported
260  *                       0 - 1.3 (default)
261  *                       1 - 2.0
262  * @dma_enable:         Specifies whether to use slave or DMA mode for accessing
263  *                      the data FIFOs. The driver will automatically detect the
264  *                      value for this parameter if none is specified.
265  *                       0 - Slave (always available)
266  *                       1 - DMA (default, if available)
267  * @dma_desc_enable:    When DMA mode is enabled, specifies whether to use
268  *                      address DMA mode or descriptor DMA mode for accessing
269  *                      the data FIFOs. The driver will automatically detect the
270  *                      value for this if none is specified.
271  *                       0 - Address DMA
272  *                       1 - Descriptor DMA (default, if available)
273  * @speed:              Specifies the maximum speed of operation in host and
274  *                      device mode. The actual speed depends on the speed of
275  *                      the attached device and the value of phy_type.
276  *                       0 - High Speed
277  *                           (default when phy_type is UTMI+ or ULPI)
278  *                       1 - Full Speed
279  *                           (default when phy_type is Full Speed)
280  * @enable_dynamic_fifo: 0 - Use coreConsultant-specified FIFO size parameters
281  *                       1 - Allow dynamic FIFO sizing (default, if available)
282  * @en_multiple_tx_fifo: Specifies whether dedicated per-endpoint transmit FIFOs
283  *                      are enabled
284  * @host_rx_fifo_size:  Number of 4-byte words in the Rx FIFO in host mode when
285  *                      dynamic FIFO sizing is enabled
286  *                       16 to 32768
287  *                      Actual maximum value is autodetected and also
288  *                      the default.
289  * @host_nperio_tx_fifo_size: Number of 4-byte words in the non-periodic Tx FIFO
290  *                      in host mode when dynamic FIFO sizing is enabled
291  *                       16 to 32768
292  *                      Actual maximum value is autodetected and also
293  *                      the default.
294  * @host_perio_tx_fifo_size: Number of 4-byte words in the periodic Tx FIFO in
295  *                      host mode when dynamic FIFO sizing is enabled
296  *                       16 to 32768
297  *                      Actual maximum value is autodetected and also
298  *                      the default.
299  * @max_transfer_size:  The maximum transfer size supported, in bytes
300  *                       2047 to 65,535
301  *                      Actual maximum value is autodetected and also
302  *                      the default.
303  * @max_packet_count:   The maximum number of packets in a transfer
304  *                       15 to 511
305  *                      Actual maximum value is autodetected and also
306  *                      the default.
307  * @host_channels:      The number of host channel registers to use
308  *                       1 to 16
309  *                      Actual maximum value is autodetected and also
310  *                      the default.
311  * @phy_type:           Specifies the type of PHY interface to use. By default,
312  *                      the driver will automatically detect the phy_type.
313  *                       0 - Full Speed Phy
314  *                       1 - UTMI+ Phy
315  *                       2 - ULPI Phy
316  *                      Defaults to best available option (2, 1, then 0)
317  * @phy_utmi_width:     Specifies the UTMI+ Data Width (in bits). This parameter
318  *                      is applicable for a phy_type of UTMI+ or ULPI. (For a
319  *                      ULPI phy_type, this parameter indicates the data width
320  *                      between the MAC and the ULPI Wrapper.) Also, this
321  *                      parameter is applicable only if the OTG_HSPHY_WIDTH cC
322  *                      parameter was set to "8 and 16 bits", meaning that the
323  *                      core has been configured to work at either data path
324  *                      width.
325  *                       8 or 16 (default 16 if available)
326  * @phy_ulpi_ddr:       Specifies whether the ULPI operates at double or single
327  *                      data rate. This parameter is only applicable if phy_type
328  *                      is ULPI.
329  *                       0 - single data rate ULPI interface with 8 bit wide
330  *                           data bus (default)
331  *                       1 - double data rate ULPI interface with 4 bit wide
332  *                           data bus
333  * @phy_ulpi_ext_vbus:  For a ULPI phy, specifies whether to use the internal or
334  *                      external supply to drive the VBus
335  *                       0 - Internal supply (default)
336  *                       1 - External supply
337  * @i2c_enable:         Specifies whether to use the I2Cinterface for a full
338  *                      speed PHY. This parameter is only applicable if phy_type
339  *                      is FS.
340  *                       0 - No (default)
341  *                       1 - Yes
342  * @ulpi_fs_ls:         Make ULPI phy operate in FS/LS mode only
343  *                       0 - No (default)
344  *                       1 - Yes
345  * @host_support_fs_ls_low_power: Specifies whether low power mode is supported
346  *                      when attached to a Full Speed or Low Speed device in
347  *                      host mode.
348  *                       0 - Don't support low power mode (default)
349  *                       1 - Support low power mode
350  * @host_ls_low_power_phy_clk: Specifies the PHY clock rate in low power mode
351  *                      when connected to a Low Speed device in host
352  *                      mode. This parameter is applicable only if
353  *                      host_support_fs_ls_low_power is enabled.
354  *                       0 - 48 MHz
355  *                           (default when phy_type is UTMI+ or ULPI)
356  *                       1 - 6 MHz
357  *                           (default when phy_type is Full Speed)
358  * @ts_dline:           Enable Term Select Dline pulsing
359  *                       0 - No (default)
360  *                       1 - Yes
361  * @reload_ctl:         Allow dynamic reloading of HFIR register during runtime
362  *                       0 - No (default for core < 2.92a)
363  *                       1 - Yes (default for core >= 2.92a)
364  * @ahbcfg:             This field allows the default value of the GAHBCFG
365  *                      register to be overridden
366  *                       -1         - GAHBCFG value will be set to 0x06
367  *                                    (INCR4, default)
368  *                       all others - GAHBCFG value will be overridden with
369  *                                    this value
370  *                      Not all bits can be controlled like this, the
371  *                      bits defined by GAHBCFG_CTRL_MASK are controlled
372  *                      by the driver and are ignored in this
373  *                      configuration value.
374  * @uframe_sched:       True to enable the microframe scheduler
375  *
376  * The following parameters may be specified when starting the module. These
377  * parameters define how the DWC_otg controller should be configured. A
378  * value of -1 (or any other out of range value) for any parameter means
379  * to read the value from hardware (if possible) or use the builtin
380  * default described above.
381  */
382 struct dwc2_core_params {
383         /*
384          * Don't add any non-int members here, this will break
385          * dwc2_set_all_params!
386          */
387         int otg_cap;
388         int otg_ver;
389         int dma_enable;
390         int dma_desc_enable;
391         int speed;
392         int enable_dynamic_fifo;
393         int en_multiple_tx_fifo;
394         int host_rx_fifo_size;
395         int host_nperio_tx_fifo_size;
396         int host_perio_tx_fifo_size;
397         int max_transfer_size;
398         int max_packet_count;
399         int host_channels;
400         int phy_type;
401         int phy_utmi_width;
402         int phy_ulpi_ddr;
403         int phy_ulpi_ext_vbus;
404         int i2c_enable;
405         int ulpi_fs_ls;
406         int host_support_fs_ls_low_power;
407         int host_ls_low_power_phy_clk;
408         int ts_dline;
409         int reload_ctl;
410         int ahbcfg;
411         int uframe_sched;
412 };
413
414 /**
415  * struct dwc2_hw_params - Autodetected parameters.
416  *
417  * These parameters are the various parameters read from hardware
418  * registers during initialization. They typically contain the best
419  * supported or maximum value that can be configured in the
420  * corresponding dwc2_core_params value.
421  *
422  * The values that are not in dwc2_core_params are documented below.
423  *
424  * @op_mode             Mode of Operation
425  *                       0 - HNP- and SRP-Capable OTG (Host & Device)
426  *                       1 - SRP-Capable OTG (Host & Device)
427  *                       2 - Non-HNP and Non-SRP Capable OTG (Host & Device)
428  *                       3 - SRP-Capable Device
429  *                       4 - Non-OTG Device
430  *                       5 - SRP-Capable Host
431  *                       6 - Non-OTG Host
432  * @arch                Architecture
433  *                       0 - Slave only
434  *                       1 - External DMA
435  *                       2 - Internal DMA
436  * @power_optimized     Are power optimizations enabled?
437  * @num_dev_ep          Number of device endpoints available
438  * @num_dev_perio_in_ep Number of device periodic IN endpoints
439  *                      avaialable
440  * @dev_token_q_depth   Device Mode IN Token Sequence Learning Queue
441  *                      Depth
442  *                       0 to 30
443  * @host_perio_tx_q_depth
444  *                      Host Mode Periodic Request Queue Depth
445  *                       2, 4 or 8
446  * @nperio_tx_q_depth
447  *                      Non-Periodic Request Queue Depth
448  *                       2, 4 or 8
449  * @hs_phy_type         High-speed PHY interface type
450  *                       0 - High-speed interface not supported
451  *                       1 - UTMI+
452  *                       2 - ULPI
453  *                       3 - UTMI+ and ULPI
454  * @fs_phy_type         Full-speed PHY interface type
455  *                       0 - Full speed interface not supported
456  *                       1 - Dedicated full speed interface
457  *                       2 - FS pins shared with UTMI+ pins
458  *                       3 - FS pins shared with ULPI pins
459  * @total_fifo_size:    Total internal RAM for FIFOs (bytes)
460  * @utmi_phy_data_width UTMI+ PHY data width
461  *                       0 - 8 bits
462  *                       1 - 16 bits
463  *                       2 - 8 or 16 bits
464  * @snpsid:             Value from SNPSID register
465  */
466 struct dwc2_hw_params {
467         unsigned op_mode:3;
468         unsigned arch:2;
469         unsigned dma_desc_enable:1;
470         unsigned enable_dynamic_fifo:1;
471         unsigned en_multiple_tx_fifo:1;
472         unsigned host_rx_fifo_size:16;
473         unsigned host_nperio_tx_fifo_size:16;
474         unsigned host_perio_tx_fifo_size:16;
475         unsigned nperio_tx_q_depth:3;
476         unsigned host_perio_tx_q_depth:3;
477         unsigned dev_token_q_depth:5;
478         unsigned max_transfer_size:26;
479         unsigned max_packet_count:11;
480         unsigned host_channels:5;
481         unsigned hs_phy_type:2;
482         unsigned fs_phy_type:2;
483         unsigned i2c_enable:1;
484         unsigned num_dev_ep:4;
485         unsigned num_dev_perio_in_ep:4;
486         unsigned total_fifo_size:16;
487         unsigned power_optimized:1;
488         unsigned utmi_phy_data_width:2;
489         u32 snpsid;
490 };
491
492 /**
493  * struct dwc2_hsotg - Holds the state of the driver, including the non-periodic
494  * and periodic schedules
495  *
496  * @dev:                The struct device pointer
497  * @regs:               Pointer to controller regs
498  * @core_params:        Parameters that define how the core should be configured
499  * @hw_params:          Parameters that were autodetected from the
500  *                      hardware registers
501  * @op_state:           The operational State, during transitions (a_host=>
502  *                      a_peripheral and b_device=>b_host) this may not match
503  *                      the core, but allows the software to determine
504  *                      transitions
505  * @dr_mode:            Requested mode of operation, one of following:
506  *                      - USB_DR_MODE_PERIPHERAL
507  *                      - USB_DR_MODE_HOST
508  *                      - USB_DR_MODE_OTG
509  * @queuing_high_bandwidth: True if multiple packets of a high-bandwidth
510  *                      transfer are in process of being queued
511  * @srp_success:        Stores status of SRP request in the case of a FS PHY
512  *                      with an I2C interface
513  * @wq_otg:             Workqueue object used for handling of some interrupts
514  * @wf_otg:             Work object for handling Connector ID Status Change
515  *                      interrupt
516  * @wkp_timer:          Timer object for handling Wakeup Detected interrupt
517  * @lx_state:           Lx state of connected device
518  * @flags:              Flags for handling root port state changes
519  * @non_periodic_sched_inactive: Inactive QHs in the non-periodic schedule.
520  *                      Transfers associated with these QHs are not currently
521  *                      assigned to a host channel.
522  * @non_periodic_sched_active: Active QHs in the non-periodic schedule.
523  *                      Transfers associated with these QHs are currently
524  *                      assigned to a host channel.
525  * @non_periodic_qh_ptr: Pointer to next QH to process in the active
526  *                      non-periodic schedule
527  * @periodic_sched_inactive: Inactive QHs in the periodic schedule. This is a
528  *                      list of QHs for periodic transfers that are _not_
529  *                      scheduled for the next frame. Each QH in the list has an
530  *                      interval counter that determines when it needs to be
531  *                      scheduled for execution. This scheduling mechanism
532  *                      allows only a simple calculation for periodic bandwidth
533  *                      used (i.e. must assume that all periodic transfers may
534  *                      need to execute in the same frame). However, it greatly
535  *                      simplifies scheduling and should be sufficient for the
536  *                      vast majority of OTG hosts, which need to connect to a
537  *                      small number of peripherals at one time. Items move from
538  *                      this list to periodic_sched_ready when the QH interval
539  *                      counter is 0 at SOF.
540  * @periodic_sched_ready:  List of periodic QHs that are ready for execution in
541  *                      the next frame, but have not yet been assigned to host
542  *                      channels. Items move from this list to
543  *                      periodic_sched_assigned as host channels become
544  *                      available during the current frame.
545  * @periodic_sched_assigned: List of periodic QHs to be executed in the next
546  *                      frame that are assigned to host channels. Items move
547  *                      from this list to periodic_sched_queued as the
548  *                      transactions for the QH are queued to the DWC_otg
549  *                      controller.
550  * @periodic_sched_queued: List of periodic QHs that have been queued for
551  *                      execution. Items move from this list to either
552  *                      periodic_sched_inactive or periodic_sched_ready when the
553  *                      channel associated with the transfer is released. If the
554  *                      interval for the QH is 1, the item moves to
555  *                      periodic_sched_ready because it must be rescheduled for
556  *                      the next frame. Otherwise, the item moves to
557  *                      periodic_sched_inactive.
558  * @periodic_usecs:     Total bandwidth claimed so far for periodic transfers.
559  *                      This value is in microseconds per (micro)frame. The
560  *                      assumption is that all periodic transfers may occur in
561  *                      the same (micro)frame.
562  * @frame_usecs:        Internal variable used by the microframe scheduler
563  * @frame_number:       Frame number read from the core at SOF. The value ranges
564  *                      from 0 to HFNUM_MAX_FRNUM.
565  * @periodic_qh_count:  Count of periodic QHs, if using several eps. Used for
566  *                      SOF enable/disable.
567  * @free_hc_list:       Free host channels in the controller. This is a list of
568  *                      struct dwc2_host_chan items.
569  * @periodic_channels:  Number of host channels assigned to periodic transfers.
570  *                      Currently assuming that there is a dedicated host
571  *                      channel for each periodic transaction and at least one
572  *                      host channel is available for non-periodic transactions.
573  * @non_periodic_channels: Number of host channels assigned to non-periodic
574  *                      transfers
575  * @available_host_channels Number of host channels available for the microframe
576  *                      scheduler to use
577  * @hc_ptr_array:       Array of pointers to the host channel descriptors.
578  *                      Allows accessing a host channel descriptor given the
579  *                      host channel number. This is useful in interrupt
580  *                      handlers.
581  * @status_buf:         Buffer used for data received during the status phase of
582  *                      a control transfer.
583  * @status_buf_dma:     DMA address for status_buf
584  * @start_work:         Delayed work for handling host A-cable connection
585  * @reset_work:         Delayed work for handling a port reset
586  * @lock:               Spinlock that protects all the driver data structures
587  * @priv:               Stores a pointer to the struct usb_hcd
588  * @otg_port:           OTG port number
589  * @frame_list:         Frame list
590  * @frame_list_dma:     Frame list DMA address
591  */
592 struct dwc2_hsotg {
593         struct device *dev;
594         void __iomem *regs;
595         /** Params detected from hardware */
596         struct dwc2_hw_params hw_params;
597         /** Params to actually use */
598         struct dwc2_core_params *core_params;
599         enum usb_otg_state op_state;
600         enum usb_dr_mode dr_mode;
601
602         unsigned int queuing_high_bandwidth:1;
603         unsigned int srp_success:1;
604
605         struct workqueue_struct *wq_otg;
606         struct work_struct wf_otg;
607         struct timer_list wkp_timer;
608         enum dwc2_lx_state lx_state;
609
610         union dwc2_hcd_internal_flags {
611                 u32 d32;
612                 struct {
613                         unsigned port_connect_status_change:1;
614                         unsigned port_connect_status:1;
615                         unsigned port_reset_change:1;
616                         unsigned port_enable_change:1;
617                         unsigned port_suspend_change:1;
618                         unsigned port_over_current_change:1;
619                         unsigned port_l1_change:1;
620                         unsigned reserved:26;
621                 } b;
622         } flags;
623
624         struct list_head non_periodic_sched_inactive;
625         struct list_head non_periodic_sched_active;
626         struct list_head *non_periodic_qh_ptr;
627         struct list_head periodic_sched_inactive;
628         struct list_head periodic_sched_ready;
629         struct list_head periodic_sched_assigned;
630         struct list_head periodic_sched_queued;
631         u16 periodic_usecs;
632         u16 frame_usecs[8];
633         u16 frame_number;
634         u16 periodic_qh_count;
635
636 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
637 #define FRAME_NUM_ARRAY_SIZE 1000
638         u16 last_frame_num;
639         u16 *frame_num_array;
640         u16 *last_frame_num_array;
641         int frame_num_idx;
642         int dumped_frame_num_array;
643 #endif
644
645         struct list_head free_hc_list;
646         int periodic_channels;
647         int non_periodic_channels;
648         int available_host_channels;
649         struct dwc2_host_chan *hc_ptr_array[MAX_EPS_CHANNELS];
650         u8 *status_buf;
651         dma_addr_t status_buf_dma;
652 #define DWC2_HCD_STATUS_BUF_SIZE 64
653
654         struct delayed_work start_work;
655         struct delayed_work reset_work;
656         spinlock_t lock;
657         void *priv;
658         u8 otg_port;
659         u32 *frame_list;
660         dma_addr_t frame_list_dma;
661
662         /* DWC OTG HW Release versions */
663 #define DWC2_CORE_REV_2_71a     0x4f54271a
664 #define DWC2_CORE_REV_2_90a     0x4f54290a
665 #define DWC2_CORE_REV_2_92a     0x4f54292a
666 #define DWC2_CORE_REV_2_94a     0x4f54294a
667 #define DWC2_CORE_REV_3_00a     0x4f54300a
668
669 #ifdef DEBUG
670         u32 frrem_samples;
671         u64 frrem_accum;
672
673         u32 hfnum_7_samples_a;
674         u64 hfnum_7_frrem_accum_a;
675         u32 hfnum_0_samples_a;
676         u64 hfnum_0_frrem_accum_a;
677         u32 hfnum_other_samples_a;
678         u64 hfnum_other_frrem_accum_a;
679
680         u32 hfnum_7_samples_b;
681         u64 hfnum_7_frrem_accum_b;
682         u32 hfnum_0_samples_b;
683         u64 hfnum_0_frrem_accum_b;
684         u32 hfnum_other_samples_b;
685         u64 hfnum_other_frrem_accum_b;
686 #endif
687 };
688
689 /* Reasons for halting a host channel */
690 enum dwc2_halt_status {
691         DWC2_HC_XFER_NO_HALT_STATUS,
692         DWC2_HC_XFER_COMPLETE,
693         DWC2_HC_XFER_URB_COMPLETE,
694         DWC2_HC_XFER_ACK,
695         DWC2_HC_XFER_NAK,
696         DWC2_HC_XFER_NYET,
697         DWC2_HC_XFER_STALL,
698         DWC2_HC_XFER_XACT_ERR,
699         DWC2_HC_XFER_FRAME_OVERRUN,
700         DWC2_HC_XFER_BABBLE_ERR,
701         DWC2_HC_XFER_DATA_TOGGLE_ERR,
702         DWC2_HC_XFER_AHB_ERR,
703         DWC2_HC_XFER_PERIODIC_INCOMPLETE,
704         DWC2_HC_XFER_URB_DEQUEUE,
705 };
706
707 /*
708  * The following functions support initialization of the core driver component
709  * and the DWC_otg controller
710  */
711 extern void dwc2_core_host_init(struct dwc2_hsotg *hsotg);
712
713 /*
714  * Host core Functions.
715  * The following functions support managing the DWC_otg controller in host
716  * mode.
717  */
718 extern void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan);
719 extern void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
720                          enum dwc2_halt_status halt_status);
721 extern void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg,
722                             struct dwc2_host_chan *chan);
723 extern void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
724                                    struct dwc2_host_chan *chan);
725 extern void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
726                                         struct dwc2_host_chan *chan);
727 extern int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
728                                      struct dwc2_host_chan *chan);
729 extern void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
730                             struct dwc2_host_chan *chan);
731 extern void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg);
732 extern void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg);
733
734 extern u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg);
735 extern bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg);
736
737 /*
738  * Common core Functions.
739  * The following functions support managing the DWC_otg controller in either
740  * device or host mode.
741  */
742 extern void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes);
743 extern void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num);
744 extern void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg);
745
746 extern int dwc2_core_init(struct dwc2_hsotg *hsotg, bool select_phy, int irq);
747 extern void dwc2_enable_global_interrupts(struct dwc2_hsotg *hcd);
748 extern void dwc2_disable_global_interrupts(struct dwc2_hsotg *hcd);
749
750 /* This function should be called on every hardware interrupt. */
751 extern irqreturn_t dwc2_handle_common_intr(int irq, void *dev);
752
753 /* OTG Core Parameters */
754
755 /*
756  * Specifies the OTG capabilities. The driver will automatically
757  * detect the value for this parameter if none is specified.
758  * 0 - HNP and SRP capable (default)
759  * 1 - SRP Only capable
760  * 2 - No HNP/SRP capable
761  */
762 extern void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val);
763 #define DWC2_CAP_PARAM_HNP_SRP_CAPABLE          0
764 #define DWC2_CAP_PARAM_SRP_ONLY_CAPABLE         1
765 #define DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE       2
766
767 /*
768  * Specifies whether to use slave or DMA mode for accessing the data
769  * FIFOs. The driver will automatically detect the value for this
770  * parameter if none is specified.
771  * 0 - Slave
772  * 1 - DMA (default, if available)
773  */
774 extern void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val);
775
776 /*
777  * When DMA mode is enabled specifies whether to use
778  * address DMA or DMA Descritor mode for accessing the data
779  * FIFOs in device mode. The driver will automatically detect
780  * the value for this parameter if none is specified.
781  * 0 - address DMA
782  * 1 - DMA Descriptor(default, if available)
783  */
784 extern void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val);
785
786 /*
787  * Specifies the maximum speed of operation in host and device mode.
788  * The actual speed depends on the speed of the attached device and
789  * the value of phy_type. The actual speed depends on the speed of the
790  * attached device.
791  * 0 - High Speed (default)
792  * 1 - Full Speed
793  */
794 extern void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val);
795 #define DWC2_SPEED_PARAM_HIGH   0
796 #define DWC2_SPEED_PARAM_FULL   1
797
798 /*
799  * Specifies whether low power mode is supported when attached
800  * to a Full Speed or Low Speed device in host mode.
801  *
802  * 0 - Don't support low power mode (default)
803  * 1 - Support low power mode
804  */
805 extern void dwc2_set_param_host_support_fs_ls_low_power(
806                 struct dwc2_hsotg *hsotg, int val);
807
808 /*
809  * Specifies the PHY clock rate in low power mode when connected to a
810  * Low Speed device in host mode. This parameter is applicable only if
811  * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
812  * then defaults to 6 MHZ otherwise 48 MHZ.
813  *
814  * 0 - 48 MHz
815  * 1 - 6 MHz
816  */
817 extern void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg,
818                                                      int val);
819 #define DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ      0
820 #define DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ       1
821
822 /*
823  * 0 - Use cC FIFO size parameters
824  * 1 - Allow dynamic FIFO sizing (default)
825  */
826 extern void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg,
827                                                int val);
828
829 /*
830  * Number of 4-byte words in the Rx FIFO in host mode when dynamic
831  * FIFO sizing is enabled.
832  * 16 to 32768 (default 1024)
833  */
834 extern void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val);
835
836 /*
837  * Number of 4-byte words in the non-periodic Tx FIFO in host mode
838  * when Dynamic FIFO sizing is enabled in the core.
839  * 16 to 32768 (default 256)
840  */
841 extern void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg,
842                                                     int val);
843
844 /*
845  * Number of 4-byte words in the host periodic Tx FIFO when dynamic
846  * FIFO sizing is enabled.
847  * 16 to 32768 (default 256)
848  */
849 extern void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg,
850                                                    int val);
851
852 /*
853  * The maximum transfer size supported in bytes.
854  * 2047 to 65,535  (default 65,535)
855  */
856 extern void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val);
857
858 /*
859  * The maximum number of packets in a transfer.
860  * 15 to 511  (default 511)
861  */
862 extern void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val);
863
864 /*
865  * The number of host channel registers to use.
866  * 1 to 16 (default 11)
867  * Note: The FPGA configuration supports a maximum of 11 host channels.
868  */
869 extern void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val);
870
871 /*
872  * Specifies the type of PHY interface to use. By default, the driver
873  * will automatically detect the phy_type.
874  *
875  * 0 - Full Speed PHY
876  * 1 - UTMI+ (default)
877  * 2 - ULPI
878  */
879 extern void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val);
880 #define DWC2_PHY_TYPE_PARAM_FS          0
881 #define DWC2_PHY_TYPE_PARAM_UTMI        1
882 #define DWC2_PHY_TYPE_PARAM_ULPI        2
883
884 /*
885  * Specifies the UTMI+ Data Width. This parameter is
886  * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
887  * PHY_TYPE, this parameter indicates the data width between
888  * the MAC and the ULPI Wrapper.) Also, this parameter is
889  * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
890  * to "8 and 16 bits", meaning that the core has been
891  * configured to work at either data path width.
892  *
893  * 8 or 16 bits (default 16)
894  */
895 extern void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val);
896
897 /*
898  * Specifies whether the ULPI operates at double or single
899  * data rate. This parameter is only applicable if PHY_TYPE is
900  * ULPI.
901  *
902  * 0 - single data rate ULPI interface with 8 bit wide data
903  * bus (default)
904  * 1 - double data rate ULPI interface with 4 bit wide data
905  * bus
906  */
907 extern void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val);
908
909 /*
910  * Specifies whether to use the internal or external supply to
911  * drive the vbus with a ULPI phy.
912  */
913 extern void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val);
914 #define DWC2_PHY_ULPI_INTERNAL_VBUS     0
915 #define DWC2_PHY_ULPI_EXTERNAL_VBUS     1
916
917 /*
918  * Specifies whether to use the I2Cinterface for full speed PHY. This
919  * parameter is only applicable if PHY_TYPE is FS.
920  * 0 - No (default)
921  * 1 - Yes
922  */
923 extern void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val);
924
925 extern void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val);
926
927 extern void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val);
928
929 /*
930  * Specifies whether dedicated transmit FIFOs are
931  * enabled for non periodic IN endpoints in device mode
932  * 0 - No
933  * 1 - Yes
934  */
935 extern void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg,
936                                                int val);
937
938 extern void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val);
939
940 extern void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val);
941
942 extern void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val);
943
944 /*
945  * Dump core registers and SPRAM
946  */
947 extern void dwc2_dump_dev_registers(struct dwc2_hsotg *hsotg);
948 extern void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg);
949 extern void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg);
950
951 /*
952  * Return OTG version - either 1.3 or 2.0
953  */
954 extern u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg);
955
956 #endif /* __DWC2_CORE_H__ */