usb: xhci: tegra: fix tcrtl/rctrl for t124
[linux-3.10.git] / drivers / usb / host / xhci-tegra.c
1 /*
2  * xhci-tegra.c - Nvidia xHCI host controller driver
3  *
4  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <linux/clk.h>
20 #include <linux/platform_device.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/clk.h>
24 #include <linux/ioport.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/irq.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/platform_data/tegra_usb.h>
29 #include <linux/uaccess.h>
30 #include <linux/circ_buf.h>
31 #include <linux/vmalloc.h>
32 #include <linux/debugfs.h>
33 #include <linux/kthread.h>
34 #include <linux/gpio.h>
35
36 #include <mach/powergate.h>
37 #include <mach/clk.h>
38 #include <mach/tegra_usb_pad_ctrl.h>
39 #include <mach/tegra_usb_pmc.h>
40 #include <mach/pm_domains.h>
41 #include <mach/mc.h>
42 #include <mach/xusb.h>
43
44 #include "../../../arch/arm/mach-tegra/iomap.h" /* HACK -- remove */
45 #include "xhci-tegra.h"
46 #include "xhci.h"
47
48 /* macros */
49 #define FW_IOCTL_LOG_DEQUEUE_LOW        (4)
50 #define FW_IOCTL_LOG_DEQUEUE_HIGH       (5)
51 #define FW_IOCTL_DATA_SHIFT             (0)
52 #define FW_IOCTL_DATA_MASK              (0x00ffffff)
53 #define FW_IOCTL_TYPE_SHIFT             (24)
54 #define FW_IOCTL_TYPE_MASK              (0xff000000)
55 #define FW_LOG_SIZE                     (sizeof(struct log_entry))
56 #define FW_LOG_COUNT                    (4096)
57 #define FW_LOG_RING_SIZE                (FW_LOG_SIZE * FW_LOG_COUNT)
58 #define FW_LOG_PAYLOAD_SIZE             (27)
59 #define DRIVER                          (0x01)
60 #define CIRC_BUF_SIZE                   (4 * (1 << 20)) /* 4MB */
61 #define FW_LOG_THREAD_RELAX             (msecs_to_jiffies(100))
62
63 /* tegra_xhci_firmware_log.flags bits */
64 #define FW_LOG_CONTEXT_VALID            (0)
65 #define FW_LOG_FILE_OPENED              (1)
66
67 #define PAGE_SELECT_MASK                        0xFFFFFE00
68 #define PAGE_SELECT_SHIFT                       9
69 #define PAGE_OFFSET_MASK                        0x000001FF
70 #define CSB_PAGE_SELECT(_addr)                                          \
71         ({                                                              \
72                 typecheck(u32, _addr);                                  \
73                 ((_addr & PAGE_SELECT_MASK) >> PAGE_SELECT_SHIFT);      \
74         })
75 #define CSB_PAGE_OFFSET(_addr)                                          \
76         ({                                                              \
77                 typecheck(u32, _addr);                                  \
78                 (_addr & PAGE_OFFSET_MASK);                             \
79         })
80
81 /* PMC register definition */
82 #define PMC_PORT_UTMIP_P0               0
83 #define PMC_PORT_UTMIP_P1               1
84 #define PMC_PORT_UTMIP_P2               2
85 #define PMC_PORT_UHSIC_P0               3
86 #define PMC_PORT_NUM                    4
87
88 #define PMC_USB_DEBOUNCE_DEL_0                  0xec
89 #define   UTMIP_LINE_DEB_CNT(x)         (((x) & 0xf) << 16)
90 #define   UTMIP_LINE_DEB_CNT_MASK               (0xf << 16)
91
92 #define PMC_UTMIP_UHSIC_SLEEP_CFG_0             0x1fc
93
94 /* private data types */
95 /* command requests from the firmware */
96 enum MBOX_CMD_TYPE {
97         MBOX_CMD_MSG_ENABLED = 1,
98         MBOX_CMD_INC_FALC_CLOCK,
99         MBOX_CMD_DEC_FALC_CLOCK,
100         MBOX_CMD_INC_SSPI_CLOCK,
101         MBOX_CMD_DEC_SSPI_CLOCK, /* 5 */
102         MBOX_CMD_SET_BW,
103         MBOX_CMD_SET_SS_PWR_GATING,
104         MBOX_CMD_SET_SS_PWR_UNGATING, /* 8 */
105         MBOX_CMD_SAVE_DFE_CTLE_CTX,
106
107         /* needs to be the last cmd */
108         MBOX_CMD_MAX,
109
110         /* resp msg to ack above commands */
111         MBOX_CMD_ACK = 128,
112         MBOX_CMD_NACK
113 };
114
115 struct log_entry {
116         u32 sequence_no;
117         u8 data[FW_LOG_PAYLOAD_SIZE];
118         u8 owner;
119 };
120
121 /* Usb3 Firmware Cfg Table */
122 struct cfgtbl {
123         u32 boot_loadaddr_in_imem;
124         u32 boot_codedfi_offset;
125         u32 boot_codetag;
126         u32 boot_codesize;
127
128         /* Physical memory reserved by Bootloader/BIOS */
129         u32 phys_memaddr;
130         u16 reqphys_memsize;
131         u16 alloc_phys_memsize;
132
133         /* .rodata section */
134         u32 rodata_img_offset;
135         u32 rodata_section_start;
136         u32 rodata_section_end;
137         u32 main_fnaddr;
138
139         u32 fwimg_cksum;
140         u32 fwimg_created_time;
141
142         /* Fields that get filled by linker during linking phase
143          * or initialized in the FW code.
144          */
145         u32 imem_resident_start;
146         u32 imem_resident_end;
147         u32 idirect_start;
148         u32 idirect_end;
149         u32 l2_imem_start;
150         u32 l2_imem_end;
151         u32 version_id;
152         u8 init_ddirect;
153         u8 reserved[3];
154         u32 phys_addr_log_buffer;
155         u32 total_log_entries;
156         u32 dequeue_ptr;
157
158         /*      Below two dummy variables are used to replace
159          *      L2IMemSymTabOffsetInDFI and L2IMemSymTabSize in order to
160          *      retain the size of struct _CFG_TBL used by other AP/Module.
161          */
162         u32 dummy_var1;
163         u32 dummy_var2;
164
165         /* fwimg_len */
166         u32 fwimg_len;
167         u8 magic[8];
168         u32 SS_low_power_entry_timeout;
169         u8 padding[140]; /* padding bytes to makeup 256-bytes cfgtbl */
170 };
171
172 struct xusb_save_regs {
173         u32 msi_bar_sz;
174         u32 msi_axi_barst;
175         u32 msi_fpci_barst;
176         u32 msi_vec0;
177         u32 msi_en_vec0;
178         u32 fpci_error_masks;
179         u32 intr_mask;
180         u32 ipfs_intr_enable;
181         u32 ufpci_config;
182         u32 clkgate_hysteresis;
183         u32 xusb_host_mccif_fifo_cntrl;
184
185         /* PG does not mention below */
186         u32 hs_pls;
187         u32 fs_pls;
188         u32 hs_fs_speed;
189         u32 hs_fs_pp;
190         u32 cfg_aru;
191         u32 cfg_order;
192         u32 cfg_fladj;
193         u32 cfg_sid;
194         /* DFE and CTLE */
195         u32 tap1_val[2];
196         u32 amp_val[2];
197         u32 ctle_z_val[2];
198         u32 ctle_g_val[2];
199 };
200
201 struct tegra_xhci_firmware {
202         void *data; /* kernel virtual address */
203         size_t size; /* firmware size */
204         dma_addr_t dma; /* dma address for controller */
205 };
206
207 struct tegra_xhci_firmware_log {
208         dma_addr_t phys_addr;           /* dma-able address */
209         void *virt_addr;                /* kernel va of the shared log buffer */
210         struct log_entry *dequeue;      /* current dequeue pointer (va) */
211         struct circ_buf circ;           /* big circular buffer */
212         u32 seq;                        /* log sequence number */
213
214         struct task_struct *thread;     /* a thread to consume log */
215         struct mutex mutex;
216         wait_queue_head_t read_wait;
217         wait_queue_head_t write_wait;
218         wait_queue_head_t intr_wait;
219         struct dentry *path;
220         struct dentry *log_file;
221         unsigned long flags;
222 };
223
224 /* structure to hold the offsets of padctl registers */
225 struct tegra_xusb_padctl_regs {
226         u16 boot_media_0;
227         u16 usb2_pad_mux_0;
228         u16 usb2_port_cap_0;
229         u16 snps_oc_map_0;
230         u16 usb2_oc_map_0;
231         u16 ss_port_map_0;
232         u16 oc_det_0;
233         u16 elpg_program_0;
234         u16 usb2_bchrg_otgpad0_ctl0_0;
235         u16 usb2_bchrg_otgpad0_ctl1_0;
236         u16 usb2_bchrg_otgpad1_ctl0_0;
237         u16 usb2_bchrg_otgpad1_ctl1_0;
238         u16 usb2_bchrg_otgpad2_ctl0_0;
239         u16 usb2_bchrg_otgpad2_ctl1_0;
240         u16 usb2_bchrg_bias_pad_0;
241         u16 usb2_bchrg_tdcd_dbnc_timer_0;
242         u16 iophy_pll_p0_ctl1_0;
243         u16 iophy_pll_p0_ctl2_0;
244         u16 iophy_pll_p0_ctl3_0;
245         u16 iophy_pll_p0_ctl4_0;
246         u16 iophy_usb3_pad0_ctl1_0;
247         u16 iophy_usb3_pad1_ctl1_0;
248         u16 iophy_usb3_pad0_ctl2_0;
249         u16 iophy_usb3_pad1_ctl2_0;
250         u16 iophy_usb3_pad0_ctl3_0;
251         u16 iophy_usb3_pad1_ctl3_0;
252         u16 iophy_usb3_pad0_ctl4_0;
253         u16 iophy_usb3_pad1_ctl4_0;
254         u16 iophy_misc_pad_p0_ctl1_0;
255         u16 iophy_misc_pad_p1_ctl1_0;
256         u16 iophy_misc_pad_p0_ctl2_0;
257         u16 iophy_misc_pad_p1_ctl2_0;
258         u16 iophy_misc_pad_p0_ctl3_0;
259         u16 iophy_misc_pad_p1_ctl3_0;
260         u16 iophy_misc_pad_p0_ctl4_0;
261         u16 iophy_misc_pad_p1_ctl4_0;
262         u16 iophy_misc_pad_p0_ctl5_0;
263         u16 iophy_misc_pad_p1_ctl5_0;
264         u16 iophy_misc_pad_p0_ctl6_0;
265         u16 iophy_misc_pad_p1_ctl6_0;
266         u16 usb2_otg_pad0_ctl0_0;
267         u16 usb2_otg_pad1_ctl0_0;
268         u16 usb2_otg_pad2_ctl0_0;
269         u16 usb2_otg_pad0_ctl1_0;
270         u16 usb2_otg_pad1_ctl1_0;
271         u16 usb2_otg_pad2_ctl1_0;
272         u16 usb2_bias_pad_ctl0_0;
273         u16 usb2_bias_pad_ctl1_0;
274         u16 usb2_hsic_pad0_ctl0_0;
275         u16 usb2_hsic_pad1_ctl0_0;
276         u16 usb2_hsic_pad0_ctl1_0;
277         u16 usb2_hsic_pad1_ctl1_0;
278         u16 usb2_hsic_pad0_ctl2_0;
279         u16 usb2_hsic_pad1_ctl2_0;
280         u16 ulpi_link_trim_ctl0;
281         u16 ulpi_null_clk_trim_ctl0;
282         u16 hsic_strb_trim_ctl0;
283         u16 wake_ctl0;
284         u16 pm_spare0;
285         u16 iophy_misc_pad_p2_ctl1_0;
286         u16 iophy_misc_pad_p3_ctl1_0;
287         u16 iophy_misc_pad_p4_ctl1_0;
288         u16 iophy_misc_pad_p2_ctl2_0;
289         u16 iophy_misc_pad_p3_ctl2_0;
290         u16 iophy_misc_pad_p4_ctl2_0;
291         u16 iophy_misc_pad_p2_ctl3_0;
292         u16 iophy_misc_pad_p3_ctl3_0;
293         u16 iophy_misc_pad_p4_ctl3_0;
294         u16 iophy_misc_pad_p2_ctl4_0;
295         u16 iophy_misc_pad_p3_ctl4_0;
296         u16 iophy_misc_pad_p4_ctl4_0;
297         u16 iophy_misc_pad_p2_ctl5_0;
298         u16 iophy_misc_pad_p3_ctl5_0;
299         u16 iophy_misc_pad_p4_ctl5_0;
300         u16 iophy_misc_pad_p2_ctl6_0;
301         u16 iophy_misc_pad_p3_ctl6_0;
302         u16 iophy_misc_pad_p4_ctl6_0;
303         u16 usb3_pad_mux_0;
304         u16 iophy_pll_s0_ctl1_0;
305         u16 iophy_pll_s0_ctl2_0;
306         u16 iophy_pll_s0_ctl3_0;
307         u16 iophy_pll_s0_ctl4_0;
308         u16 iophy_misc_pad_s0_ctl1_0;
309         u16 iophy_misc_pad_s0_ctl2_0;
310         u16 iophy_misc_pad_s0_ctl3_0;
311         u16 iophy_misc_pad_s0_ctl4_0;
312         u16 iophy_misc_pad_s0_ctl5_0;
313         u16 iophy_misc_pad_s0_ctl6_0;
314 };
315
316 struct tegra_xhci_hcd {
317         struct platform_device *pdev;
318         struct xhci_hcd *xhci;
319         u16 device_id;
320
321         spinlock_t lock;
322         struct mutex sync_lock;
323
324         int smi_irq;
325         int padctl_irq;
326         int usb3_irq;
327
328         bool ss_wake_event;
329         bool ss_pwr_gated;
330         bool host_pwr_gated;
331         bool hs_wake_event;
332         bool host_resume_req;
333         bool lp0_exit;
334         bool dfe_ctle_ctx_saved;
335         unsigned long last_jiffies;
336         unsigned long host_phy_base;
337         void __iomem *host_phy_virt_base;
338
339         void __iomem *padctl_base;
340         void __iomem *fpci_base;
341         void __iomem *ipfs_base;
342
343         struct tegra_xusb_platform_data *pdata;
344         struct tegra_xusb_board_data *bdata;
345         struct tegra_xusb_padctl_regs *padregs;
346
347         /* mailbox variables */
348         struct mutex mbox_lock;
349         u32 mbox_owner;
350         u32 cmd_type;
351         u32 cmd_data;
352
353         struct regulator *xusb_s5p0v_reg;
354         struct regulator *xusb_s5p0v1_reg;
355         struct regulator *xusb_s5p0v2_reg;
356         struct regulator *xusb_s1p05v_reg;
357         struct regulator *xusb_s3p3v_reg;
358         struct regulator *xusb_s1p8v_reg;
359
360         struct work_struct mbox_work;
361         struct work_struct ss_elpg_exit_work;
362         struct work_struct host_elpg_exit_work;
363
364         struct clk *host_clk;
365         struct clk *ss_clk;
366
367         /* XUSB Falcon SuperSpeed Clock */
368         struct clk *falc_clk;
369
370         /* EMC Clock */
371         struct clk *emc_clk;
372         /* XUSB SS PI Clock */
373         struct clk *ss_src_clk;
374         /* PLLE Clock */
375         struct clk *plle_clk;
376         struct clk *pll_u_480M;
377         struct clk *clk_m;
378         /* refPLLE clk */
379         struct clk *pll_re_vco_clk;
380         /*
381          * XUSB/IPFS specific registers these need to be saved/restored in
382          * addition to spec defined registers
383          */
384         struct xusb_save_regs sregs;
385         bool usb2_rh_suspend;
386         bool usb3_rh_suspend;
387         bool hc_in_elpg;
388
389         unsigned long usb3_rh_remote_wakeup_ports; /* one bit per port */
390         /* firmware loading related */
391         struct tegra_xhci_firmware firmware;
392
393         struct tegra_xhci_firmware_log log;
394 };
395
396 static struct tegra_usb_pmc_data pmc_data;
397
398 /* functions */
399 static inline struct tegra_xhci_hcd *hcd_to_tegra_xhci(struct usb_hcd *hcd)
400 {
401         return (struct tegra_xhci_hcd *) dev_get_drvdata(hcd->self.controller);
402 }
403
404 #if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
405 static inline void must_have_sync_lock(struct tegra_xhci_hcd *tegra)
406 {
407         WARN_ON(tegra->sync_lock.owner != current);
408 }
409 #else
410 static inline void must_have_sync_lock(struct tegra_xhci_hcd *tegra)
411 #endif
412
413 static void tegra_xhci_setup_gpio_for_ss_lane(struct tegra_xhci_hcd *tegra)
414 {
415         int err = 0;
416
417         if (!tegra->bdata->gpio_controls_muxed_ss_lanes)
418                 return;
419
420         if (tegra->bdata->lane_owner & BIT(0)) {
421                 /* USB3_SS port1 is using SATA lane so set (MUX_SATA)
422                  * GPIO P11 to '0'
423                  */
424                 err = gpio_request((tegra->bdata->gpio_ss1_sata & 0xffff),
425                         "gpio_ss1_sata");
426                 if (err < 0)
427                         pr_err("%s: gpio_ss1_sata gpio_request failed %d\n",
428                                 __func__, err);
429                 err = gpio_direction_output((tegra->bdata->gpio_ss1_sata
430                         & 0xffff), 1);
431                 if (err < 0)
432                         pr_err("%s: gpio_ss1_sata gpio_direction failed %d\n",
433                                 __func__, err);
434                 __gpio_set_value((tegra->bdata->gpio_ss1_sata & 0xffff),
435                                 ((tegra->bdata->gpio_ss1_sata >> 16)));
436         }
437 }
438
439 static void debug_print_portsc(struct xhci_hcd *xhci)
440 {
441         __le32 __iomem *addr;
442         int i;
443         int ports;
444
445         ports = HCS_MAX_PORTS(xhci->hcs_params1);
446         addr = &xhci->op_regs->port_status_base;
447         for (i = 0; i < ports; i++) {
448                 xhci_dbg(xhci, "%p port %d status reg = 0x%x\n",
449                                 addr, i, (unsigned int) xhci_readl(xhci, addr));
450                 addr += NUM_PORT_REGS;
451         }
452 }
453 static void tegra_xhci_war_for_tctrl_rctrl(struct tegra_xhci_hcd *tegra);
454
455 static void update_speed(struct tegra_xhci_hcd *tegra, u8 port, bool setup_wake)
456 {
457         struct usb_hcd *hcd = xhci_to_hcd(tegra->xhci);
458         u32 portsc;
459
460         /* we don't need speed information to disable PMC */
461         if (!setup_wake)
462                 return;
463
464         portsc = readl(hcd->regs + BAR0_XHCI_OP_PORTSC(port));
465         if (DEV_FULLSPEED(portsc))
466                 pmc_data.port_speed = USB_PMC_PORT_SPEED_FULL;
467         else if (DEV_HIGHSPEED(portsc))
468                 pmc_data.port_speed = USB_PMC_PORT_SPEED_HIGH;
469         else if (DEV_LOWSPEED(portsc))
470                 pmc_data.port_speed = USB_PMC_PORT_SPEED_LOW;
471         else if (DEV_SUPERSPEED(portsc))
472                 pmc_data.port_speed = USB_PMC_PORT_SPEED_SUPER;
473         else
474                 pmc_data.port_speed = USB_PMC_PORT_SPEED_UNKNOWN;
475 }
476
477 static void setup_wake_detect(bool setup_wake)
478 {
479         if (setup_wake)
480                 pmc_data.pmc_ops->setup_pmc_wake_detect(&pmc_data);
481         else
482                 pmc_data.pmc_ops->disable_pmc_bus_ctrl(&pmc_data, 0);
483 }
484
485 static void pmc_init(struct tegra_xhci_hcd *tegra, bool setup_wake)
486 {
487         u32 portmap = tegra->bdata->portmap;
488
489         pmc_data.controller_type = TEGRA_USB_3_0;
490         if (portmap & TEGRA_XUSB_USB2_P0) {
491                 pmc_data.instance = (tegra->pdata->pmc_portmap >> 0) & 0xf;
492                 pmc_data.phy_type = TEGRA_USB_PHY_INTF_UTMI;
493                 update_speed(tegra, pmc_data.instance, setup_wake);
494                 tegra_usb_pmc_init(&pmc_data);
495                 setup_wake_detect(setup_wake);
496         }
497         if (portmap & TEGRA_XUSB_USB2_P1) {
498                 pmc_data.instance = (tegra->pdata->pmc_portmap >> 4) & 0xf;
499                 pmc_data.phy_type = TEGRA_USB_PHY_INTF_UTMI;
500                 update_speed(tegra, pmc_data.instance, setup_wake);
501                 tegra_usb_pmc_init(&pmc_data);
502                 setup_wake_detect(setup_wake);
503         }
504         if (portmap & TEGRA_XUSB_USB2_P2) {
505                 pmc_data.instance = (tegra->pdata->pmc_portmap >> 8) & 0xf;
506                 pmc_data.phy_type = TEGRA_USB_PHY_INTF_UTMI;
507                 update_speed(tegra, pmc_data.instance, setup_wake);
508                 tegra_usb_pmc_init(&pmc_data);
509                 setup_wake_detect(setup_wake);
510         }
511         if (portmap & TEGRA_XUSB_HSIC_P0) {
512                 pmc_data.instance = PMC_PORT_UHSIC_P0;
513                 pmc_data.phy_type = TEGRA_USB_PHY_INTF_HSIC;
514                 update_speed(tegra, pmc_data.instance, setup_wake);
515                 tegra_usb_pmc_init(&pmc_data);
516                 setup_wake_detect(setup_wake);
517         }
518 }
519
520 u32 csb_read(struct tegra_xhci_hcd *tegra, u32 addr)
521 {
522         void __iomem *fpci_base = tegra->fpci_base;
523         struct platform_device *pdev = tegra->pdev;
524         u32 input_addr;
525         u32 data;
526         u32 csb_page_select;
527
528         /* to select the appropriate CSB page to write to */
529         csb_page_select = CSB_PAGE_SELECT(addr);
530
531         dev_dbg(&pdev->dev, "csb_read: csb_page_select= 0x%08x\n",
532                         csb_page_select);
533
534         iowrite32(csb_page_select, fpci_base + XUSB_CFG_ARU_C11_CSBRANGE);
535
536         /* selects the appropriate offset in the page to read from */
537         input_addr = CSB_PAGE_OFFSET(addr);
538         data = ioread32(fpci_base + XUSB_CFG_CSB_BASE_ADDR + input_addr);
539
540         dev_dbg(&pdev->dev, "csb_read: input_addr = 0x%08x data = 0x%08x\n",
541                         input_addr, data);
542         return data;
543 }
544
545 void csb_write(struct tegra_xhci_hcd *tegra, u32 addr, u32 data)
546 {
547         void __iomem *fpci_base = tegra->fpci_base;
548         struct platform_device *pdev = tegra->pdev;
549         u32 input_addr;
550         u32 csb_page_select;
551
552         /* to select the appropriate CSB page to write to */
553         csb_page_select = CSB_PAGE_SELECT(addr);
554
555         dev_dbg(&pdev->dev, "csb_write:csb_page_selectx = 0x%08x\n",
556                         csb_page_select);
557
558         iowrite32(csb_page_select, fpci_base + XUSB_CFG_ARU_C11_CSBRANGE);
559
560         /* selects the appropriate offset in the page to write to */
561         input_addr = CSB_PAGE_OFFSET(addr);
562         iowrite32(data, fpci_base + XUSB_CFG_CSB_BASE_ADDR + input_addr);
563
564         dev_dbg(&pdev->dev, "csb_write: input_addr = 0x%08x data = %0x08x\n",
565                         input_addr, data);
566 }
567
568 /**
569  * fw_log_next - find next log entry in a tegra_xhci_firmware_log context.
570  *      This function takes care of wrapping. That means when current log entry
571  *      is the last one, it returns with the first one.
572  *
573  * @param log   The tegra_xhci_firmware_log context.
574  * @param this  The current log entry.
575  * @return      The log entry which is next to the current one.
576  */
577 static inline struct log_entry *fw_log_next(
578                 struct tegra_xhci_firmware_log *log, struct log_entry *this)
579 {
580         struct log_entry *first = (struct log_entry *) log->virt_addr;
581         struct log_entry *last = first + FW_LOG_COUNT - 1;
582
583         WARN((this < first) || (this > last), "%s: invalid input\n", __func__);
584
585         return (this == last) ? first : (this + 1);
586 }
587
588 /**
589  * fw_log_update_dequeue_pointer - update dequeue pointer to both firmware and
590  *      tegra_xhci_firmware_log.dequeue.
591  *
592  * @param log   The tegra_xhci_firmware_log context.
593  * @param n     Counts of log entries to fast-forward.
594  */
595 static inline void fw_log_update_deq_pointer(
596                 struct tegra_xhci_firmware_log *log, int n)
597 {
598         struct tegra_xhci_hcd *tegra =
599                         container_of(log, struct tegra_xhci_hcd, log);
600         struct device *dev = &tegra->pdev->dev;
601         struct log_entry *deq = tegra->log.dequeue;
602         dma_addr_t physical_addr;
603         u32 reg;
604
605         dev_dbg(dev, "curr 0x%p fast-forward %d entries\n", deq, n);
606         while (n-- > 0)
607                 deq = fw_log_next(log, deq);
608
609         tegra->log.dequeue = deq;
610         physical_addr = tegra->log.phys_addr +
611                         ((u8 *)deq - (u8 *)tegra->log.virt_addr);
612
613         /* update dequeue pointer to firmware */
614         reg = (FW_IOCTL_LOG_DEQUEUE_LOW << FW_IOCTL_TYPE_SHIFT);
615         reg |= (physical_addr & 0xffff); /* lower 16-bits */
616         iowrite32(reg, tegra->fpci_base + XUSB_CFG_ARU_FW_SCRATCH);
617
618         reg = (FW_IOCTL_LOG_DEQUEUE_HIGH << FW_IOCTL_TYPE_SHIFT);
619         reg |= ((physical_addr >> 16) & 0xffff); /* higher 16-bits */
620         iowrite32(reg, tegra->fpci_base + XUSB_CFG_ARU_FW_SCRATCH);
621
622         dev_dbg(dev, "new 0x%p physical addr 0x%x\n", deq, physical_addr);
623 }
624
625 static inline bool circ_buffer_full(struct circ_buf *circ)
626 {
627         int space = CIRC_SPACE(circ->head, circ->tail, CIRC_BUF_SIZE);
628
629         return (space <= FW_LOG_SIZE);
630 }
631
632 static inline bool fw_log_available(struct tegra_xhci_hcd *tegra)
633 {
634         return (tegra->log.dequeue->owner == DRIVER);
635 }
636
637 /**
638  * fw_log_wait_empty_timeout - wait firmware log thread to clean up shared
639  *      log buffer.
640  * @param tegra:        tegra_xhci_hcd context
641  * @param msec:         timeout value in millisecond
642  * @return true:        shared log buffer is empty,
643  *         false:       shared log buffer isn't empty.
644  */
645 static inline bool fw_log_wait_empty_timeout(struct tegra_xhci_hcd *tegra,
646                 unsigned timeout)
647 {
648         u32 target = jiffies + msecs_to_jiffies(timeout);
649         bool ret;
650
651         mutex_lock(&tegra->log.mutex);
652
653         while (fw_log_available(tegra) && time_is_after_jiffies(target)) {
654                 mutex_unlock(&tegra->log.mutex);
655                 usleep_range(1000, 2000);
656                 mutex_lock(&tegra->log.mutex);
657         }
658
659         ret = fw_log_available(tegra);
660         mutex_unlock(&tegra->log.mutex);
661
662         return ret;
663 }
664
665 /**
666  * fw_log_copy - copy firmware log from device's buffer to driver's circular
667  *      buffer.
668  * @param tegra tegra_xhci_hcd context
669  * @return true,        We still have firmware log in device's buffer to copy.
670  *                      This function returned due the driver's circular buffer
671  *                      is full. Caller should invoke this function again as
672  *                      soon as there is space in driver's circular buffer.
673  *         false,       Device's buffer is empty.
674  */
675 static inline bool fw_log_copy(struct tegra_xhci_hcd *tegra)
676 {
677         struct device *dev = &tegra->pdev->dev;
678         struct circ_buf *circ = &tegra->log.circ;
679         int head, tail;
680         int buffer_len, copy_len;
681         struct log_entry *entry;
682         struct log_entry *first = tegra->log.virt_addr;
683
684         while (fw_log_available(tegra)) {
685
686                 /* calculate maximum contiguous driver buffer length */
687                 head = circ->head;
688                 tail = ACCESS_ONCE(circ->tail);
689                 buffer_len = CIRC_SPACE_TO_END(head, tail, CIRC_BUF_SIZE);
690                 /* round down to FW_LOG_SIZE */
691                 buffer_len -= (buffer_len % FW_LOG_SIZE);
692                 if (!buffer_len)
693                         return true; /* log available but no space left */
694
695                 /* calculate maximum contiguous log copy length */
696                 entry = tegra->log.dequeue;
697                 copy_len = 0;
698                 do {
699                         if (tegra->log.seq != entry->sequence_no) {
700                                 dev_warn(dev,
701                                 "%s: discontinuous seq no, expect %u get %u\n",
702                                 __func__, tegra->log.seq, entry->sequence_no);
703                         }
704                         tegra->log.seq = entry->sequence_no + 1;
705
706                         copy_len += FW_LOG_SIZE;
707                         buffer_len -= FW_LOG_SIZE;
708                         if (!buffer_len)
709                                 break; /* no space left */
710                         entry = fw_log_next(&tegra->log, entry);
711                 } while ((entry->owner == DRIVER) && (entry != first));
712
713                 memcpy(&circ->buf[head], tegra->log.dequeue, copy_len);
714                 memset(tegra->log.dequeue, 0, copy_len);
715                 circ->head = (circ->head + copy_len) & (CIRC_BUF_SIZE - 1);
716
717                 mb();
718
719                 fw_log_update_deq_pointer(&tegra->log, copy_len/FW_LOG_SIZE);
720
721                 dev_dbg(dev, "copied %d entries, new dequeue 0x%p\n",
722                                 copy_len/FW_LOG_SIZE, tegra->log.dequeue);
723                 wake_up_interruptible(&tegra->log.read_wait);
724         }
725
726         return false;
727 }
728
729 static int fw_log_thread(void *data)
730 {
731         struct tegra_xhci_hcd *tegra = data;
732         struct device *dev = &tegra->pdev->dev;
733         struct circ_buf *circ = &tegra->log.circ;
734         bool logs_left;
735
736         dev_dbg(dev, "start firmware log thread\n");
737
738         do {
739                 mutex_lock(&tegra->log.mutex);
740                 if (circ_buffer_full(circ)) {
741                         mutex_unlock(&tegra->log.mutex);
742                         dev_info(dev, "%s: circ buffer full\n", __func__);
743                         wait_event_interruptible(tegra->log.write_wait,
744                             kthread_should_stop() || !circ_buffer_full(circ));
745                         mutex_lock(&tegra->log.mutex);
746                 }
747
748                 logs_left = fw_log_copy(tegra);
749                 mutex_unlock(&tegra->log.mutex);
750
751                 /* relax if no logs left  */
752                 if (!logs_left)
753                         wait_event_interruptible_timeout(tegra->log.intr_wait,
754                                 fw_log_available(tegra), FW_LOG_THREAD_RELAX);
755         } while (!kthread_should_stop());
756
757         dev_dbg(dev, "stop firmware log thread\n");
758         return 0;
759 }
760
761 static inline bool circ_buffer_empty(struct circ_buf *circ)
762 {
763         return (CIRC_CNT(circ->head, circ->tail, CIRC_BUF_SIZE) == 0);
764 }
765
766 static ssize_t fw_log_file_read(struct file *file, char __user *buf,
767                 size_t count, loff_t *offp)
768 {
769         struct tegra_xhci_hcd *tegra = file->private_data;
770         struct platform_device *pdev = tegra->pdev;
771         struct circ_buf *circ = &tegra->log.circ;
772         int head, tail;
773         size_t n = 0;
774         int s;
775
776         mutex_lock(&tegra->log.mutex);
777
778         while (circ_buffer_empty(circ)) {
779                 mutex_unlock(&tegra->log.mutex);
780                 if (file->f_flags & O_NONBLOCK)
781                         return -EAGAIN; /* non-blocking read */
782
783                 dev_dbg(&pdev->dev, "%s: nothing to read\n", __func__);
784
785                 if (wait_event_interruptible(tegra->log.read_wait,
786                                 !circ_buffer_empty(circ)))
787                         return -ERESTARTSYS;
788
789                 if (mutex_lock_interruptible(&tegra->log.mutex))
790                         return -ERESTARTSYS;
791         }
792
793         while (count > 0) {
794                 head = ACCESS_ONCE(circ->head);
795                 tail = circ->tail;
796                 s = min_t(int, count,
797                                 CIRC_CNT_TO_END(head, tail, CIRC_BUF_SIZE));
798
799                 if (s > 0) {
800                         if (copy_to_user(&buf[n], &circ->buf[tail], s)) {
801                                 dev_warn(&pdev->dev, "copy_to_user failed\n");
802                                 mutex_unlock(&tegra->log.mutex);
803                                 return -EFAULT;
804                         }
805                         circ->tail = (circ->tail + s) & (CIRC_BUF_SIZE - 1);
806
807                         count -= s;
808                         n += s;
809                 } else
810                         break;
811         }
812
813         mutex_unlock(&tegra->log.mutex);
814
815         wake_up_interruptible(&tegra->log.write_wait);
816
817         dev_dbg(&pdev->dev, "%s: %d bytes\n", __func__, n);
818
819         return n;
820 }
821
822 static int fw_log_file_open(struct inode *inode, struct file *file)
823 {
824         struct tegra_xhci_hcd *tegra;
825         file->private_data = inode->i_private;
826         tegra = file->private_data;
827
828         if (test_and_set_bit(FW_LOG_FILE_OPENED, &tegra->log.flags)) {
829                 dev_info(&tegra->pdev->dev, "%s: already opened\n", __func__);
830                 return -EBUSY;
831         }
832
833         return 0;
834 }
835
836 static int fw_log_file_close(struct inode *inode, struct file *file)
837 {
838         struct tegra_xhci_hcd *tegra = file->private_data;
839
840         clear_bit(FW_LOG_FILE_OPENED, &tegra->log.flags);
841
842         return 0;
843 }
844
845 static const struct file_operations firmware_log_fops = {
846                 .open           = fw_log_file_open,
847                 .release        = fw_log_file_close,
848                 .read           = fw_log_file_read,
849                 .owner          = THIS_MODULE,
850 };
851
852 static int fw_log_init(struct tegra_xhci_hcd *tegra)
853 {
854         struct platform_device *pdev = tegra->pdev;
855         int rc = 0;
856
857         /* allocate buffer to be shared between driver and firmware */
858         tegra->log.virt_addr = dma_alloc_writecombine(&pdev->dev,
859                         FW_LOG_RING_SIZE, &tegra->log.phys_addr, GFP_KERNEL);
860
861         if (!tegra->log.virt_addr) {
862                 dev_err(&pdev->dev, "dma_alloc_writecombine() size %d failed\n",
863                                 FW_LOG_RING_SIZE);
864                 return -ENOMEM;
865         }
866
867         dev_info(&pdev->dev, "%d bytes log buffer physical 0x%u virtual 0x%p\n",
868                 FW_LOG_RING_SIZE, tegra->log.phys_addr, tegra->log.virt_addr);
869
870         memset(tegra->log.virt_addr, 0, FW_LOG_RING_SIZE);
871         tegra->log.dequeue = tegra->log.virt_addr;
872
873         tegra->log.circ.buf = vmalloc(CIRC_BUF_SIZE);
874         if (!tegra->log.circ.buf) {
875                 dev_err(&pdev->dev, "vmalloc size %d failed\n", CIRC_BUF_SIZE);
876                 rc = -ENOMEM;
877                 goto error_free_dma;
878         }
879
880         tegra->log.circ.head = 0;
881         tegra->log.circ.tail = 0;
882
883         init_waitqueue_head(&tegra->log.read_wait);
884         init_waitqueue_head(&tegra->log.write_wait);
885         init_waitqueue_head(&tegra->log.intr_wait);
886
887         mutex_init(&tegra->log.mutex);
888
889         tegra->log.path = debugfs_create_dir("tegra_xhci", NULL);
890         if (IS_ERR_OR_NULL(tegra->log.path)) {
891                 dev_warn(&pdev->dev, "debugfs_create_dir() failed\n");
892                 rc = -ENOMEM;
893                 goto error_free_mem;
894         }
895
896         tegra->log.log_file = debugfs_create_file("firmware_log",
897                         S_IRUGO, tegra->log.path, tegra, &firmware_log_fops);
898         if ((!tegra->log.log_file) ||
899                         (tegra->log.log_file == ERR_PTR(-ENODEV))) {
900                 dev_warn(&pdev->dev, "debugfs_create_file() failed\n");
901                 rc = -ENOMEM;
902                 goto error_remove_debugfs_path;
903         }
904
905         tegra->log.thread = kthread_run(fw_log_thread, tegra, "xusb-fw-log");
906         if (IS_ERR(tegra->log.thread)) {
907                 dev_warn(&pdev->dev, "kthread_run() failed\n");
908                 rc = -ENOMEM;
909                 goto error_remove_debugfs_file;
910         }
911
912         set_bit(FW_LOG_CONTEXT_VALID, &tegra->log.flags);
913         return rc;
914
915 error_remove_debugfs_file:
916         debugfs_remove(tegra->log.log_file);
917 error_remove_debugfs_path:
918         debugfs_remove(tegra->log.path);
919 error_free_mem:
920         vfree(tegra->log.circ.buf);
921 error_free_dma:
922         dma_free_writecombine(&pdev->dev, FW_LOG_RING_SIZE,
923                         tegra->log.virt_addr, tegra->log.phys_addr);
924         memset(&tegra->log, sizeof(tegra->log), 0);
925         return rc;
926 }
927
928 static void fw_log_deinit(struct tegra_xhci_hcd *tegra)
929 {
930         struct platform_device *pdev = tegra->pdev;
931
932         if (test_and_clear_bit(FW_LOG_CONTEXT_VALID, &tegra->log.flags)) {
933
934                 debugfs_remove(tegra->log.log_file);
935                 debugfs_remove(tegra->log.path);
936
937                 wake_up_interruptible(&tegra->log.read_wait);
938                 wake_up_interruptible(&tegra->log.write_wait);
939                 kthread_stop(tegra->log.thread);
940
941                 mutex_lock(&tegra->log.mutex);
942                 dma_free_writecombine(&pdev->dev, FW_LOG_RING_SIZE,
943                         tegra->log.virt_addr, tegra->log.phys_addr);
944                 vfree(tegra->log.circ.buf);
945                 tegra->log.circ.head = tegra->log.circ.tail = 0;
946                 mutex_unlock(&tegra->log.mutex);
947
948                 mutex_destroy(&tegra->log.mutex);
949         }
950 }
951
952 static void tegra_xhci_debug_read_pads(struct tegra_xhci_hcd *tegra)
953 {
954         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
955         struct xhci_hcd *xhci = tegra->xhci;
956         u32 reg;
957
958         xhci_info(xhci, "============ PADCTL VALUES START =================\n");
959         reg = readl(tegra->padctl_base + padregs->usb2_pad_mux_0);
960         xhci_info(xhci, " PAD MUX = %x\n", reg);
961         reg = readl(tegra->padctl_base + padregs->usb2_port_cap_0);
962         xhci_info(xhci, " PORT CAP = %x\n", reg);
963         reg = readl(tegra->padctl_base + padregs->snps_oc_map_0);
964         xhci_info(xhci, " SNPS OC MAP = %x\n", reg);
965         reg = readl(tegra->padctl_base + padregs->usb2_oc_map_0);
966         xhci_info(xhci, " USB2 OC MAP = %x\n", reg);
967         reg = readl(tegra->padctl_base + padregs->ss_port_map_0);
968         xhci_info(xhci, " SS PORT MAP = %x\n", reg);
969         reg = readl(tegra->padctl_base + padregs->oc_det_0);
970         xhci_info(xhci, " OC DET 0= %x\n", reg);
971         reg = readl(tegra->padctl_base + padregs->iophy_usb3_pad0_ctl2_0);
972         xhci_info(xhci, " iophy_usb3_pad0_ctl2_0= %x\n", reg);
973         reg = readl(tegra->padctl_base + padregs->iophy_usb3_pad1_ctl2_0);
974         xhci_info(xhci, " iophy_usb3_pad1_ctl2_0= %x\n", reg);
975         reg = readl(tegra->padctl_base + padregs->usb2_otg_pad0_ctl0_0);
976         xhci_info(xhci, " usb2_otg_pad0_ctl0_0= %x\n", reg);
977         reg = readl(tegra->padctl_base + padregs->usb2_otg_pad1_ctl0_0);
978         xhci_info(xhci, " usb2_otg_pad1_ctl0_0= %x\n", reg);
979         reg = readl(tegra->padctl_base + padregs->usb2_otg_pad0_ctl1_0);
980         xhci_info(xhci, " usb2_otg_pad0_ctl1_0= %x\n", reg);
981         reg = readl(tegra->padctl_base + padregs->usb2_otg_pad1_ctl1_0);
982         xhci_info(xhci, " usb2_otg_pad1_ctl1_0= %x\n", reg);
983         reg = readl(tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
984         xhci_info(xhci, " usb2_bias_pad_ctl0_0= %x\n", reg);
985         reg = readl(tegra->padctl_base + padregs->usb2_hsic_pad0_ctl0_0);
986         xhci_info(xhci, " usb2_hsic_pad0_ctl0_0= %x\n", reg);
987         reg = readl(tegra->padctl_base + padregs->usb2_hsic_pad1_ctl0_0);
988         xhci_info(xhci, " usb2_hsic_pad1_ctl0_0= %x\n", reg);
989         xhci_info(xhci, "============ PADCTL VALUES END=================\n");
990 }
991
992 static void tegra_xhci_cfg(struct tegra_xhci_hcd *tegra)
993 {
994         u32 reg;
995
996         reg = readl(tegra->ipfs_base + IPFS_XUSB_HOST_CONFIGURATION_0);
997         reg |= IPFS_EN_FPCI;
998         writel(reg, tegra->ipfs_base + IPFS_XUSB_HOST_CONFIGURATION_0);
999         udelay(10);
1000
1001         /* Program Bar0 Space */
1002         reg = readl(tegra->fpci_base + XUSB_CFG_4);
1003         reg |= tegra->host_phy_base;
1004         writel(reg, tegra->fpci_base + XUSB_CFG_4);
1005         usleep_range(100, 200);
1006
1007         /* Enable Bus Master */
1008         reg = readl(tegra->fpci_base + XUSB_CFG_1);
1009         reg |= 0x7;
1010         writel(reg, tegra->fpci_base + XUSB_CFG_1);
1011
1012         /* Set intr mask to enable intr assertion */
1013         reg = readl(tegra->ipfs_base + IPFS_XUSB_HOST_INTR_MASK_0);
1014         reg |= IPFS_IP_INT_MASK;
1015         writel(reg, tegra->ipfs_base + IPFS_XUSB_HOST_INTR_MASK_0);
1016
1017         /* Set hysteris to 0x80 */
1018         writel(0x80, tegra->ipfs_base + IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0);
1019 }
1020
1021 static int tegra_xusb_regulator_init(struct tegra_xhci_hcd *tegra,
1022                 struct platform_device *pdev)
1023 {
1024         struct tegra_xusb_regulator_name *supply = &tegra->bdata->supply;
1025         int err = 0;
1026
1027         tegra->xusb_s3p3v_reg =
1028                         devm_regulator_get(&pdev->dev, supply->s3p3v);
1029         if (IS_ERR(tegra->xusb_s3p3v_reg)) {
1030                 dev_err(&pdev->dev, "3p3v: regulator not found: %ld."
1031                         , PTR_ERR(tegra->xusb_s3p3v_reg));
1032                 err = PTR_ERR(tegra->xusb_s3p3v_reg);
1033                 goto err_null_regulator;
1034         } else {
1035                 err = regulator_enable(tegra->xusb_s3p3v_reg);
1036                 if (err < 0) {
1037                         dev_err(&pdev->dev,
1038                                 "3p3v: regulator enable failed:%d\n", err);
1039                         goto err_null_regulator;
1040                 }
1041         }
1042
1043         tegra->xusb_s5p0v_reg = devm_regulator_get(&pdev->dev, supply->s5p0v);
1044         if (IS_ERR(tegra->xusb_s5p0v_reg)) {
1045                 dev_err(&pdev->dev, "5p0v regulator not found: %ld."
1046                         , PTR_ERR(tegra->xusb_s5p0v_reg));
1047                 err = PTR_ERR(tegra->xusb_s5p0v_reg);
1048                 goto err_put_s3p3v_reg;
1049         } else {
1050                 err = regulator_enable(tegra->xusb_s5p0v_reg);
1051                 if (err < 0) {
1052                         dev_err(&pdev->dev,
1053                                 "5p0v: regulator enable failed:%d\n", err);
1054                         goto err_put_s3p3v_reg;
1055                 }
1056         }
1057
1058         tegra->xusb_s1p8v_reg =
1059                 devm_regulator_get(&pdev->dev, supply->s1p8v);
1060         if (IS_ERR(tegra->xusb_s1p8v_reg)) {
1061                 dev_err(&pdev->dev, "1p8v regulator not found: %ld."
1062                         , PTR_ERR(tegra->xusb_s1p8v_reg));
1063                 err = PTR_ERR(tegra->xusb_s1p8v_reg);
1064                 goto err_put_s5p0v_reg;
1065         } else {
1066                 err = regulator_enable(tegra->xusb_s1p8v_reg);
1067                 if (err < 0) {
1068                         dev_err(&pdev->dev,
1069                         "1p8v: regulator enable failed:%d\n", err);
1070                         goto err_put_s5p0v_reg;
1071                 }
1072         }
1073
1074         tegra->xusb_s1p05v_reg =
1075                         devm_regulator_get(&pdev->dev, supply->s1p05v);
1076         if (IS_ERR(tegra->xusb_s1p05v_reg)) {
1077                 dev_err(&pdev->dev, "1p05v: regulator not found: %ld."
1078                         , PTR_ERR(tegra->xusb_s1p05v_reg));
1079                 err = PTR_ERR(tegra->xusb_s1p05v_reg);
1080                 goto err_put_s1p8v_reg;
1081         } else {
1082                 err = regulator_enable(tegra->xusb_s1p05v_reg);
1083                 if (err < 0) {
1084                         dev_err(&pdev->dev,
1085                         "1p05v: regulator enable failed:%d\n", err);
1086                         goto err_put_s1p8v_reg;
1087                 }
1088         }
1089
1090         if (tegra->bdata->uses_different_vbus_per_port) {
1091                 tegra->xusb_s5p0v1_reg = devm_regulator_get(&pdev->dev,
1092                                                 supply->s5p0v1);
1093                 if (IS_ERR(tegra->xusb_s5p0v1_reg)) {
1094                         dev_err(&pdev->dev, "5p0v1 regulator not found: %ld."
1095                                 , PTR_ERR(tegra->xusb_s5p0v1_reg));
1096                         err = PTR_ERR(tegra->xusb_s5p0v1_reg);
1097                         goto err_put_s1p05v_reg;
1098                 } else {
1099                         err = regulator_enable(tegra->xusb_s5p0v1_reg);
1100                         if (err < 0) {
1101                                 dev_err(&pdev->dev,
1102                                 "5p0v1: regulator enable failed:%d\n", err);
1103                                 goto err_put_s1p05v_reg;
1104                         }
1105                 }
1106
1107                 tegra->xusb_s5p0v2_reg = devm_regulator_get(&pdev->dev,
1108                                                 supply->s5p0v2);
1109                 if (IS_ERR(tegra->xusb_s5p0v2_reg)) {
1110                         dev_err(&pdev->dev, "5p0v2 regulator not found: %ld."
1111                                 , PTR_ERR(tegra->xusb_s5p0v2_reg));
1112                         err = PTR_ERR(tegra->xusb_s5p0v2_reg);
1113                         goto err_put_s1p5v1_reg;
1114                 } else {
1115                         err = regulator_enable(tegra->xusb_s5p0v2_reg);
1116                         if (err < 0) {
1117                                 dev_err(&pdev->dev,
1118                                 "5p0v2: regulator enable failed:%d\n", err);
1119                                 goto err_put_s1p5v1_reg;
1120                         }
1121                 }
1122         }
1123         return err;
1124
1125 err_put_s1p5v1_reg:
1126         if (tegra->bdata->uses_different_vbus_per_port)
1127                 regulator_disable(tegra->xusb_s5p0v1_reg);
1128 err_put_s1p05v_reg:
1129         regulator_disable(tegra->xusb_s1p05v_reg);
1130 err_put_s1p8v_reg:
1131         regulator_disable(tegra->xusb_s1p8v_reg);
1132 err_put_s5p0v_reg:
1133         regulator_disable(tegra->xusb_s5p0v_reg);
1134 err_put_s3p3v_reg:
1135         regulator_disable(tegra->xusb_s3p3v_reg);
1136 err_null_regulator:
1137         tegra->xusb_s5p0v_reg = NULL;
1138         tegra->xusb_s5p0v1_reg = NULL;
1139         tegra->xusb_s5p0v2_reg = NULL;
1140         tegra->xusb_s1p05v_reg = NULL;
1141         tegra->xusb_s3p3v_reg = NULL;
1142         tegra->xusb_s1p8v_reg = NULL;
1143         return err;
1144 }
1145
1146 static void tegra_xusb_regulator_deinit(struct tegra_xhci_hcd *tegra)
1147 {
1148         regulator_disable(tegra->xusb_s1p05v_reg);
1149         regulator_disable(tegra->xusb_s1p8v_reg);
1150         regulator_disable(tegra->xusb_s5p0v_reg);
1151         regulator_disable(tegra->xusb_s3p3v_reg);
1152         if (tegra->bdata->uses_different_vbus_per_port) {
1153                 regulator_disable(tegra->xusb_s5p0v1_reg);
1154                 regulator_disable(tegra->xusb_s5p0v2_reg);
1155         }
1156
1157         tegra->xusb_s1p05v_reg = NULL;
1158         tegra->xusb_s1p8v_reg = NULL;
1159         tegra->xusb_s5p0v_reg = NULL;
1160         tegra->xusb_s5p0v1_reg = NULL;
1161         tegra->xusb_s5p0v2_reg = NULL;
1162         tegra->xusb_s3p3v_reg = NULL;
1163 }
1164
1165 /*
1166  * We need to enable only plle_clk as pllu_clk, utmip_clk and plle_re_vco_clk
1167  * are under hardware control
1168  */
1169 static int tegra_usb2_clocks_init(struct tegra_xhci_hcd *tegra)
1170 {
1171         struct platform_device *pdev = tegra->pdev;
1172         int err = 0;
1173
1174         tegra->plle_clk = devm_clk_get(&pdev->dev, "pll_e");
1175         if (IS_ERR(tegra->plle_clk)) {
1176                 dev_err(&pdev->dev, "%s: Failed to get plle clock\n", __func__);
1177                 err = PTR_ERR(tegra->plle_clk);
1178                 return err;
1179         }
1180         err = clk_enable(tegra->plle_clk);
1181         if (err) {
1182                 dev_err(&pdev->dev, "%s: could not enable plle clock\n",
1183                         __func__);
1184                 return err;
1185         }
1186
1187         return err;
1188 }
1189
1190 static void tegra_usb2_clocks_deinit(struct tegra_xhci_hcd *tegra)
1191 {
1192         clk_disable(tegra->plle_clk);
1193         tegra->plle_clk = NULL;
1194 }
1195
1196 static int tegra_xusb_partitions_clk_init(struct tegra_xhci_hcd *tegra)
1197 {
1198         struct platform_device *pdev = tegra->pdev;
1199         int err = 0;
1200
1201         tegra->emc_clk = devm_clk_get(&pdev->dev, "emc");
1202         if (IS_ERR(tegra->emc_clk)) {
1203                 dev_err(&pdev->dev, "Failed to get xusb.emc clock\n");
1204                 return PTR_ERR(tegra->emc_clk);
1205         }
1206
1207         if (tegra->pdata->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2) {
1208                 tegra->pll_re_vco_clk = devm_clk_get(&pdev->dev, "pll_re_vco");
1209                 if (IS_ERR(tegra->pll_re_vco_clk)) {
1210                         dev_err(&pdev->dev, "Failed to get refPLLE clock\n");
1211                         err = PTR_ERR(tegra->pll_re_vco_clk);
1212                         goto get_pll_re_vco_clk_failed;
1213                 }
1214         }
1215
1216         /* get the clock handle of 120MHz clock source */
1217         tegra->pll_u_480M = devm_clk_get(&pdev->dev, "pll_u_480M");
1218         if (IS_ERR(tegra->pll_u_480M)) {
1219                 dev_err(&pdev->dev, "Failed to get pll_u_480M clk handle\n");
1220                 err = PTR_ERR(tegra->pll_u_480M);
1221                 goto get_pll_u_480M_failed;
1222         }
1223
1224         /* get the clock handle of 12MHz clock source */
1225         tegra->clk_m = devm_clk_get(&pdev->dev, "clk_m");
1226         if (IS_ERR(tegra->clk_m)) {
1227                 dev_err(&pdev->dev, "Failed to get clk_m clk handle\n");
1228                 err = PTR_ERR(tegra->clk_m);
1229                 goto clk_get_clk_m_failed;
1230         }
1231
1232         tegra->ss_src_clk = devm_clk_get(&pdev->dev, "ss_src");
1233         if (IS_ERR(tegra->ss_src_clk)) {
1234                 dev_err(&pdev->dev, "Failed to get SSPI clk\n");
1235                 err = PTR_ERR(tegra->ss_src_clk);
1236                 tegra->ss_src_clk = NULL;
1237                 goto get_ss_src_clk_failed;
1238         }
1239
1240         tegra->host_clk = devm_clk_get(&pdev->dev, "host");
1241         if (IS_ERR(tegra->host_clk)) {
1242                 dev_err(&pdev->dev, "Failed to get host partition clk\n");
1243                 err = PTR_ERR(tegra->host_clk);
1244                 tegra->host_clk = NULL;
1245                 goto get_host_clk_failed;
1246         }
1247
1248         tegra->ss_clk = devm_clk_get(&pdev->dev, "ss");
1249         if (IS_ERR(tegra->ss_clk)) {
1250                 dev_err(&pdev->dev, "Failed to get ss partition clk\n");
1251                 err = PTR_ERR(tegra->ss_clk);
1252                 tegra->ss_clk = NULL;
1253                 goto get_ss_clk_failed;
1254         }
1255
1256         if (tegra->pdata->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2) {
1257                 err = clk_enable(tegra->pll_re_vco_clk);
1258                 if (err) {
1259                         dev_err(&pdev->dev, "Failed to enable refPLLE clk\n");
1260                         goto enable_pll_re_vco_clk_failed;
1261                 }
1262         }
1263         /* enable ss clock */
1264         err = clk_enable(tegra->host_clk);
1265         if (err) {
1266                 dev_err(&pdev->dev, "Failed to enable host partition clk\n");
1267                 goto enable_host_clk_failed;
1268         }
1269
1270         err = clk_enable(tegra->ss_clk);
1271         if (err) {
1272                 dev_err(&pdev->dev, "Failed to enable ss partition clk\n");
1273                 goto eanble_ss_clk_failed;
1274         }
1275
1276         err = clk_enable(tegra->emc_clk);
1277         if (err) {
1278                 dev_err(&pdev->dev, "Failed to enable xusb.emc clk\n");
1279                 goto eanble_emc_clk_failed;
1280         }
1281
1282         return 0;
1283
1284 eanble_emc_clk_failed:
1285         clk_disable(tegra->ss_clk);
1286
1287 eanble_ss_clk_failed:
1288         clk_disable(tegra->host_clk);
1289
1290 enable_host_clk_failed:
1291         if (tegra->pdata->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
1292                 clk_disable(tegra->pll_re_vco_clk);
1293
1294 enable_pll_re_vco_clk_failed:
1295         tegra->ss_clk = NULL;
1296
1297 get_ss_clk_failed:
1298         tegra->host_clk = NULL;
1299
1300 get_host_clk_failed:
1301         tegra->ss_src_clk = NULL;
1302
1303 get_ss_src_clk_failed:
1304         tegra->clk_m = NULL;
1305
1306 clk_get_clk_m_failed:
1307         tegra->pll_u_480M = NULL;
1308
1309 get_pll_u_480M_failed:
1310         if (tegra->pdata->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
1311                 tegra->pll_re_vco_clk = NULL;
1312
1313 get_pll_re_vco_clk_failed:
1314         tegra->emc_clk = NULL;
1315
1316         return err;
1317 }
1318
1319 static void tegra_xusb_partitions_clk_deinit(struct tegra_xhci_hcd *tegra)
1320 {
1321         clk_disable(tegra->ss_clk);
1322         clk_disable(tegra->host_clk);
1323         if (tegra->pdata->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
1324                 clk_disable(tegra->pll_re_vco_clk);
1325         tegra->ss_clk = NULL;
1326         tegra->host_clk = NULL;
1327         tegra->ss_src_clk = NULL;
1328         tegra->clk_m = NULL;
1329         tegra->pll_u_480M = NULL;
1330         if (tegra->pdata->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
1331                 tegra->pll_re_vco_clk = NULL;
1332 }
1333
1334 static void tegra_xhci_rx_idle_mode_override(struct tegra_xhci_hcd *tegra,
1335         bool enable)
1336 {
1337         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1338         u32 reg;
1339
1340         /* Issue is only applicable for T114 */
1341         if (XUSB_DEVICE_ID_T114 != tegra->device_id)
1342                 return;
1343
1344         if (tegra->bdata->portmap & TEGRA_XUSB_SS_P0) {
1345                 reg = readl(tegra->padctl_base +
1346                         padregs->iophy_misc_pad_p0_ctl3_0);
1347                 if (enable) {
1348                         reg &= ~RX_IDLE_MODE;
1349                         reg |= RX_IDLE_MODE_OVRD;
1350                 } else {
1351                         reg |= RX_IDLE_MODE;
1352                         reg &= ~RX_IDLE_MODE_OVRD;
1353                 }
1354                 writel(reg, tegra->padctl_base +
1355                         padregs->iophy_misc_pad_p0_ctl3_0);
1356         }
1357
1358         if (tegra->bdata->portmap & TEGRA_XUSB_SS_P1) {
1359                 reg = readl(tegra->padctl_base +
1360                         padregs->iophy_misc_pad_p1_ctl3_0);
1361                 if (enable) {
1362                         reg &= ~RX_IDLE_MODE;
1363                         reg |= RX_IDLE_MODE_OVRD;
1364                 } else {
1365                         reg |= RX_IDLE_MODE;
1366                         reg &= ~RX_IDLE_MODE_OVRD;
1367                 }
1368                 writel(reg, tegra->padctl_base +
1369                         padregs->iophy_misc_pad_p1_ctl3_0);
1370
1371                 /* SATA lane also if USB3_SS port1 mapped to it */
1372                 if (XUSB_DEVICE_ID_T114 != tegra->device_id &&
1373                                 tegra->bdata->lane_owner & BIT(0)) {
1374                         reg = readl(tegra->padctl_base +
1375                                 padregs->iophy_misc_pad_s0_ctl3_0);
1376                         if (enable) {
1377                                 reg &= ~RX_IDLE_MODE;
1378                                 reg |= RX_IDLE_MODE_OVRD;
1379                         } else {
1380                                 reg |= RX_IDLE_MODE;
1381                                 reg &= ~RX_IDLE_MODE_OVRD;
1382                         }
1383                         writel(reg, tegra->padctl_base +
1384                                 padregs->iophy_misc_pad_s0_ctl3_0);
1385                 }
1386         }
1387 }
1388
1389 /* Enable ss clk, host clk, falcon clk,
1390  * fs clk, dev clk, plle and refplle
1391  */
1392
1393 static int
1394 tegra_xusb_request_clk_rate(struct tegra_xhci_hcd *tegra,
1395                 struct clk *clk_handle, u32 rate, u32 *sw_resp)
1396 {
1397         int ret = 0;
1398         enum MBOX_CMD_TYPE cmd_ack = MBOX_CMD_ACK;
1399         int fw_req_rate = rate, cur_rate;
1400
1401         /* Do not handle clock change as needed for HS disconnect issue */
1402         if (tegra->pdata->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2) {
1403                 *sw_resp = fw_req_rate | (MBOX_CMD_ACK << MBOX_CMD_SHIFT);
1404                 return ret;
1405         }
1406
1407         /* frequency request from firmware is in KHz.
1408          * Convert it to MHz
1409          */
1410
1411         /* get current rate of clock */
1412         cur_rate = clk_get_rate(clk_handle);
1413         cur_rate /= 1000;
1414
1415         if (fw_req_rate == cur_rate) {
1416                 cmd_ack = MBOX_CMD_ACK;
1417                 *sw_resp = fw_req_rate;
1418         } else {
1419
1420                 if (clk_handle == tegra->ss_src_clk && fw_req_rate == 12000) {
1421                         /* Change SS clock source to CLK_M at 12MHz */
1422                         clk_set_parent(clk_handle, tegra->clk_m);
1423                         clk_set_rate(clk_handle, fw_req_rate * 1000);
1424
1425                         /* save leakage power when SS freq is being decreased */
1426                         tegra_xhci_rx_idle_mode_override(tegra, true);
1427                 } else if (clk_handle == tegra->ss_src_clk &&
1428                                 fw_req_rate == 120000) {
1429                         /* Change SS clock source to HSIC_480 at 120MHz */
1430                         clk_set_rate(clk_handle,  3000 * 1000);
1431                         clk_set_parent(clk_handle, tegra->pll_u_480M);
1432
1433                         /* clear ovrd bits when SS freq is being increased */
1434                         tegra_xhci_rx_idle_mode_override(tegra, false);
1435                 }
1436
1437                 *sw_resp = clk_get_rate(clk_handle);
1438                 *sw_resp /= 1000;
1439
1440                 if (*sw_resp != fw_req_rate) {
1441                         xhci_err(tegra->xhci, "cur_rate=%d, fw_req_rate=%d\n",
1442                                 cur_rate, fw_req_rate);
1443                         cmd_ack = MBOX_CMD_NACK;
1444                 }
1445         }
1446         *sw_resp |= (cmd_ack << MBOX_CMD_SHIFT);
1447         return ret;
1448 }
1449
1450 static void tegra_xhci_save_dfe_ctle_context(struct tegra_xhci_hcd *tegra,
1451         u8 port)
1452 {
1453         struct xhci_hcd *xhci = tegra->xhci;
1454         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1455         u32 offset;
1456         u32 reg;
1457
1458         xhci_info(xhci, "saving dfe_cntl and ctle context for port %d\n", port);
1459
1460         /* if port1 is mapped to SATA lane then read from SATA register */
1461         if (port == 1 && XUSB_DEVICE_ID_T114 != tegra->device_id &&
1462                         tegra->bdata->lane_owner & BIT(0))
1463                 offset = padregs->iophy_misc_pad_s0_ctl6_0;
1464         else
1465                 offset = port ? padregs->iophy_misc_pad_p1_ctl6_0 :
1466                                 padregs->iophy_misc_pad_p0_ctl6_0;
1467
1468         /* save tap1_val[] for the port for dfe_cntl */
1469         reg = readl(tegra->padctl_base + offset);
1470         reg &= ~(0xff << 16);
1471         reg |= (0x32 << 16);
1472         writel(reg, tegra->padctl_base + offset);
1473
1474         reg = readl(tegra->padctl_base + offset);
1475         tegra->sregs.tap1_val[port] = ((reg & (0x1f << 24)) >> 24);
1476
1477         /* save amp_val[] for the port for dfe_cntl */
1478         reg = readl(tegra->padctl_base + offset);
1479         reg &= ~(0xff << 16);
1480         reg |= (0x33 << 16);
1481         writel(reg, tegra->padctl_base + offset);
1482
1483         reg = readl(tegra->padctl_base + offset);
1484         tegra->sregs.amp_val[port] = ((reg & (0x7f << 24)) >> 24);
1485
1486         /* save ctle_z_val[] for the port for ctle */
1487         reg = readl(tegra->padctl_base + offset);
1488         reg &= ~(0xff << 16);
1489         reg |= (0x20 << 16);
1490         writel(reg, tegra->padctl_base + offset);
1491
1492         reg = readl(tegra->padctl_base + offset);
1493         tegra->sregs.ctle_z_val[port] = ((reg & (0x3f << 24)) >> 24);
1494
1495         /* save ctle_g_val[] for the port for ctle */
1496         reg = readl(tegra->padctl_base + offset);
1497         reg &= ~(0xff << 16);
1498         reg |= (0x21 << 16);
1499         writel(reg, tegra->padctl_base + offset);
1500
1501         reg = readl(tegra->padctl_base + offset);
1502         tegra->sregs.ctle_g_val[port] = ((reg & (0x3f << 24)) >> 24);
1503         tegra->dfe_ctle_ctx_saved = true;
1504 }
1505
1506 static void tegra_xhci_restore_dfe_ctle_context(struct tegra_xhci_hcd *tegra,
1507         u8 port)
1508 {
1509         struct xhci_hcd *xhci = tegra->xhci;
1510         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1511         u32 ctl4_offset, ctl2_offset;
1512         u32 reg;
1513
1514         /* don't restore if not saved */
1515         if (tegra->dfe_ctle_ctx_saved == false)
1516                 return;
1517
1518         ctl4_offset = port ? padregs->iophy_usb3_pad1_ctl4_0 :
1519                         padregs->iophy_usb3_pad0_ctl4_0;
1520         ctl2_offset = port ? padregs->iophy_usb3_pad1_ctl2_0 :
1521                         padregs->iophy_usb3_pad0_ctl2_0;
1522
1523         xhci_info(xhci, "restoring dfe_cntl/ctle context of port %d\n", port);
1524
1525         /* restore dfe_cntl for the port */
1526         reg = readl(tegra->padctl_base + ctl4_offset);
1527         reg &= ~((0x7f << 16) | (0x1f << 24));
1528         reg |= ((tegra->sregs.amp_val[port] << 16) |
1529                 (tegra->sregs.tap1_val[port] << 24));
1530         writel(reg, tegra->padctl_base + ctl4_offset);
1531
1532         /* restore ctle for the port */
1533         reg = readl(tegra->padctl_base + ctl2_offset);
1534         reg &= ~((0x3f << 8) | (0x3f << 16));
1535         reg |= ((tegra->sregs.ctle_g_val[port] << 8) |
1536                 (tegra->sregs.ctle_z_val[port] << 16));
1537         writel(reg, tegra->padctl_base + ctl2_offset);
1538 }
1539
1540 static void tegra_xhci_program_ulpi_pad(struct tegra_xhci_hcd *tegra,
1541         u8 port)
1542 {
1543         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1544         u32 reg;
1545
1546         reg = readl(tegra->padctl_base + padregs->usb2_pad_mux_0);
1547         reg &= ~USB2_ULPI_PAD;
1548         reg |= USB2_ULPI_PAD_OWNER_XUSB;
1549         writel(reg, tegra->padctl_base + padregs->usb2_pad_mux_0);
1550
1551         reg = readl(tegra->padctl_base + padregs->usb2_port_cap_0);
1552         reg &= ~USB2_ULPI_PORT_CAP;
1553         reg |= (tegra->bdata->ulpicap << 24);
1554         writel(reg, tegra->padctl_base + padregs->usb2_port_cap_0);
1555         /* FIXME: Program below when more details available
1556          * XUSB_PADCTL_ULPI_LINK_TRIM_CONTROL_0
1557          * XUSB_PADCTL_ULPI_NULL_CLK_TRIM_CONTROL_0
1558          */
1559 }
1560
1561 static void tegra_xhci_program_hsic_pad(struct tegra_xhci_hcd *tegra,
1562         u8 port)
1563 {
1564         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1565         u32 ctl0_offset;
1566         u32 reg;
1567
1568         reg = readl(tegra->padctl_base + padregs->usb2_pad_mux_0);
1569         reg &= ~(port ? USB2_HSIC_PAD_PORT1 : USB2_HSIC_PAD_PORT0);
1570         reg |= port ? USB2_HSIC_PAD_P1_OWNER_XUSB :
1571                         USB2_HSIC_PAD_P0_OWNER_XUSB;
1572         writel(reg, tegra->padctl_base + padregs->usb2_pad_mux_0);
1573
1574         ctl0_offset = port ? padregs->usb2_hsic_pad1_ctl0_0 :
1575                         padregs->usb2_hsic_pad0_ctl0_0;
1576
1577         reg = readl(tegra->padctl_base + ctl0_offset);
1578         reg &= ~(HSIC_TX_SLEWP | HSIC_TX_SLEWN);
1579         writel(reg, tegra->padctl_base + ctl0_offset);
1580
1581         /* FIXME Program below when more details available
1582          * XUSB_PADCTL_HSIC_PAD0_CTL_0_0
1583          * XUSB_PADCTL_HSIC_PAD0_CTL_1_0
1584          * XUSB_PADCTL_HSIC_PAD0_CTL_2_0
1585          * XUSB_PADCTL_HSIC_PAD1_CTL_0_0
1586          * XUSB_PADCTL_HSIC_PAD1_CTL_1_0
1587          * XUSB_PADCTL_HSIC_PAD1_CTL_2_0
1588          * XUSB_PADCTL_HSIC_STRB_TRIM_CONTROL_0
1589          */
1590 }
1591
1592 static void tegra_xhci_program_utmip_pad(struct tegra_xhci_hcd *tegra,
1593         u8 port)
1594 {
1595         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1596         u32 reg;
1597         u32 ctl0_offset, ctl1_offset;
1598
1599         reg = readl(tegra->padctl_base + padregs->usb2_pad_mux_0);
1600         reg &= ~USB2_OTG_PAD_PORT_MASK(port);
1601         reg |= USB2_OTG_PAD_PORT_OWNER_XUSB(port);
1602         writel(reg, tegra->padctl_base + padregs->usb2_pad_mux_0);
1603
1604         reg = readl(tegra->padctl_base + padregs->usb2_port_cap_0);
1605         reg &= ~USB2_PORT_CAP_MASK(port);
1606         reg |= USB2_PORT_CAP_HOST(port);
1607         writel(reg, tegra->padctl_base + padregs->usb2_port_cap_0);
1608
1609         /*
1610          * Modify only the bits which belongs to the port
1611          * and enable respective VBUS_PAD for the port
1612          */
1613         if (tegra->bdata->uses_external_pmic == false) {
1614                 reg = readl(tegra->padctl_base + padregs->oc_det_0);
1615                 reg &= ~(port == 2 ? OC_DET_VBUS_ENABLE2_OC_MAP :
1616                         port ? OC_DET_VBUS_ENABLE1_OC_MAP :
1617                                 OC_DET_VBUS_ENABLE0_OC_MAP);
1618
1619                 reg |= (port == 2) ? OC_DET_VBUS_EN2_OC_DETECTED_VBUS_PAD2 :
1620                         port ? OC_DET_VBUS_EN1_OC_DETECTED_VBUS_PAD1 :
1621                                 OC_DET_VBUS_EN0_OC_DETECTED_VBUS_PAD0;
1622                 writel(reg, tegra->padctl_base + padregs->oc_det_0);
1623         }
1624         /*
1625          * enable respective VBUS_PAD if port is mapped to any SS port
1626          */
1627         reg = readl(tegra->padctl_base + padregs->usb2_oc_map_0);
1628         reg &= ~((port == 2) ? USB2_OC_MAP_PORT2 :
1629                 port ? USB2_OC_MAP_PORT1 : USB2_OC_MAP_PORT0);
1630         reg |= (0x4 | port) << (port * 3);
1631         writel(reg, tegra->padctl_base + padregs->usb2_oc_map_0);
1632
1633         ctl0_offset = (port == 2) ? padregs->usb2_otg_pad2_ctl0_0 :
1634                         port ? padregs->usb2_otg_pad1_ctl0_0 :
1635                                 padregs->usb2_otg_pad0_ctl0_0;
1636         ctl1_offset = (port == 2) ? padregs->usb2_otg_pad2_ctl1_0 :
1637                         port ? padregs->usb2_otg_pad1_ctl1_0 :
1638                                 padregs->usb2_otg_pad0_ctl1_0;
1639
1640         reg = readl(tegra->padctl_base + ctl0_offset);
1641         reg &= ~(USB2_OTG_HS_CURR_LVL | USB2_OTG_HS_SLEW |
1642                 USB2_OTG_FS_SLEW | USB2_OTG_LS_RSLEW |
1643                 USB2_OTG_PD | USB2_OTG_PD2 | USB2_OTG_PD_ZI);
1644
1645         reg |= tegra->pdata->hs_slew;
1646         reg |= (port == 2) ? tegra->pdata->ls_rslew_pad2 :
1647                         port ? tegra->pdata->ls_rslew_pad1 :
1648                         tegra->pdata->ls_rslew_pad0;
1649         reg |= (port == 2) ? tegra->pdata->hs_curr_level_pad2 :
1650                         port ? tegra->pdata->hs_curr_level_pad1 :
1651                         tegra->pdata->hs_curr_level_pad0;
1652         writel(reg, tegra->padctl_base + ctl0_offset);
1653
1654         reg = readl(tegra->padctl_base + ctl1_offset);
1655         reg &= ~(USB2_OTG_TERM_RANGE_AD | USB2_OTG_HS_IREF_CAP
1656                 | USB2_OTG_PD_CHRP_FORCE_POWERUP
1657                 | USB2_OTG_PD_DISC_FORCE_POWERUP
1658                 | USB2_OTG_PD_DR);
1659         reg |= (tegra->pdata->hs_iref_cap << 9) |
1660                 (tegra->pdata->hs_term_range_adj << 3);
1661         writel(reg, tegra->padctl_base + ctl1_offset);
1662 }
1663
1664 static void tegra_xhci_program_ss_pad(struct tegra_xhci_hcd *tegra,
1665         u8 port)
1666 {
1667         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1668         u32 ctl2_offset, ctl4_offset, ctl5_offset;
1669         u32 reg;
1670
1671         ctl2_offset = port ? padregs->iophy_usb3_pad1_ctl2_0 :
1672                         padregs->iophy_usb3_pad0_ctl2_0;
1673         ctl4_offset = port ? padregs->iophy_usb3_pad1_ctl4_0 :
1674                         padregs->iophy_usb3_pad0_ctl4_0;
1675         ctl5_offset = port ? padregs->iophy_misc_pad_p1_ctl5_0 :
1676                         padregs->iophy_misc_pad_p0_ctl5_0;
1677
1678         reg = readl(tegra->padctl_base + ctl2_offset);
1679         reg &= ~(IOPHY_USB3_RXWANDER | IOPHY_USB3_RXEQ |
1680                 IOPHY_USB3_CDRCNTL);
1681         reg |= tegra->pdata->rx_wander | tegra->pdata->rx_eq |
1682                 tegra->pdata->cdr_cntl;
1683         writel(reg, tegra->padctl_base + ctl2_offset);
1684
1685         reg = readl(tegra->padctl_base + ctl4_offset);
1686         reg = tegra->pdata->dfe_cntl;
1687         writel(reg, tegra->padctl_base + ctl4_offset);
1688
1689         reg = readl(tegra->padctl_base + ctl5_offset);
1690         reg |= RX_QEYE_EN;
1691         writel(reg, tegra->padctl_base + ctl5_offset);
1692
1693         reg = readl(tegra->padctl_base + padregs->ss_port_map_0);
1694         reg &= ~(port ? SS_PORT_MAP_P1 : SS_PORT_MAP_P0);
1695         reg |= (tegra->bdata->ss_portmap &
1696                 (port ? TEGRA_XUSB_SS1_PORT_MAP : TEGRA_XUSB_SS0_PORT_MAP));
1697         writel(reg, tegra->padctl_base + padregs->ss_port_map_0);
1698
1699         tegra_xhci_restore_dfe_ctle_context(tegra, port);
1700 }
1701
1702 /* This function assigns the USB ports to the controllers,
1703  * then programs the port capabilities and pad parameters
1704  * of ports assigned to XUSB after booted to OS.
1705  */
1706 void
1707 tegra_xhci_padctl_portmap_and_caps(struct tegra_xhci_hcd *tegra)
1708 {
1709         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1710         u32 reg, oc_bits = 0;
1711
1712         reg = readl(tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
1713         reg &= ~(USB2_BIAS_HS_SQUELCH_LEVEL | USB2_BIAS_HS_DISCON_LEVEL);
1714         reg |= tegra->pdata->hs_squelch_level | tegra->pdata->hs_disc_lvl;
1715         writel(reg, tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
1716
1717         reg = readl(tegra->padctl_base + padregs->snps_oc_map_0);
1718         reg |= SNPS_OC_MAP_CTRL1 | SNPS_OC_MAP_CTRL2 | SNPS_OC_MAP_CTRL3;
1719         writel(reg, tegra->padctl_base + padregs->snps_oc_map_0);
1720         reg = readl(tegra->padctl_base + padregs->snps_oc_map_0);
1721
1722         reg = readl(tegra->padctl_base + padregs->oc_det_0);
1723         reg |= OC_DET_VBUS_ENABLE0_OC_MAP | OC_DET_VBUS_ENABLE1_OC_MAP;
1724         if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
1725                 reg |= OC_DET_VBUS_ENABLE2_OC_MAP;
1726         writel(reg, tegra->padctl_base + padregs->oc_det_0);
1727
1728         /* check if over current seen. Clear if present */
1729         if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P0)
1730                 oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD0;
1731         if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P1)
1732                 oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD1;
1733         if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
1734                 oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD2;
1735
1736         reg = readl(tegra->padctl_base + padregs->oc_det_0);
1737         if (reg & oc_bits) {
1738                 xhci_info(tegra->xhci, "Over current detected. Clearing...\n");
1739                 writel(reg, tegra->padctl_base + padregs->oc_det_0);
1740
1741                 usleep_range(100, 200);
1742
1743                 reg = readl(tegra->padctl_base + padregs->oc_det_0);
1744                 if (reg & oc_bits)
1745                         xhci_info(tegra->xhci, "Over current still present\n");
1746         }
1747
1748         reg = readl(tegra->padctl_base + padregs->usb2_oc_map_0);
1749         reg = USB2_OC_MAP_PORT0 | USB2_OC_MAP_PORT1;
1750         if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
1751                 reg |= USB2_OC_MAP_PORT2;
1752         writel(reg, tegra->padctl_base + padregs->usb2_oc_map_0);
1753
1754         if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P0)
1755                 tegra_xhci_program_utmip_pad(tegra, 0);
1756         if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P1)
1757                 tegra_xhci_program_utmip_pad(tegra, 1);
1758         if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
1759                 tegra_xhci_program_utmip_pad(tegra, 2);
1760
1761         if (tegra->bdata->portmap & TEGRA_XUSB_ULPI_P0)
1762                 tegra_xhci_program_ulpi_pad(tegra, 0);
1763
1764         if (tegra->bdata->portmap & TEGRA_XUSB_HSIC_P0)
1765                 tegra_xhci_program_hsic_pad(tegra, 0);
1766         if (tegra->bdata->portmap & TEGRA_XUSB_HSIC_P1)
1767                 tegra_xhci_program_hsic_pad(tegra, 1);
1768
1769         if (tegra->bdata->portmap & TEGRA_XUSB_SS_P0) {
1770                 tegra_xhci_program_ss_pad(tegra, 0);
1771         } else {
1772                 /* set rx_idle_mode_ovrd for unused SS ports to save power */
1773                 reg = readl(tegra->padctl_base +
1774                         padregs->iophy_misc_pad_p0_ctl3_0);
1775                 reg &= ~RX_IDLE_MODE;
1776                 reg |= RX_IDLE_MODE_OVRD;
1777                 writel(reg, tegra->padctl_base +
1778                         padregs->iophy_misc_pad_p0_ctl3_0);
1779         }
1780
1781         if (tegra->bdata->portmap & TEGRA_XUSB_SS_P1) {
1782                 tegra_xhci_program_ss_pad(tegra, 1);
1783         } else {
1784                 /* set rx_idle_mode_ovrd for unused SS ports to save power */
1785                 reg = readl(tegra->padctl_base +
1786                         padregs->iophy_misc_pad_p1_ctl3_0);
1787                 reg &= ~RX_IDLE_MODE;
1788                 reg |= RX_IDLE_MODE_OVRD;
1789                 writel(reg, tegra->padctl_base +
1790                         padregs->iophy_misc_pad_p1_ctl3_0);
1791
1792                 /* SATA lane also if USB3_SS port1 mapped to it but unused */
1793                 if (XUSB_DEVICE_ID_T114 != tegra->device_id &&
1794                                 tegra->bdata->lane_owner & BIT(0)) {
1795                         reg = readl(tegra->padctl_base +
1796                                 padregs->iophy_misc_pad_s0_ctl3_0);
1797                         reg &= ~RX_IDLE_MODE;
1798                         reg |= RX_IDLE_MODE_OVRD;
1799                         writel(reg, tegra->padctl_base +
1800                                 padregs->iophy_misc_pad_s0_ctl3_0);
1801                 }
1802         }
1803         if (XUSB_DEVICE_ID_T114 != tegra->device_id) {
1804                 tegra_xhci_setup_gpio_for_ss_lane(tegra);
1805                 usb3_phy_pad_enable(tegra->bdata->lane_owner);
1806         }
1807 }
1808
1809 /* This function read XUSB registers and stores in device context */
1810 static void
1811 tegra_xhci_save_xusb_ctx(struct tegra_xhci_hcd *tegra)
1812 {
1813
1814         /* a. Save the IPFS registers */
1815         tegra->sregs.msi_bar_sz =
1816                 readl(tegra->ipfs_base + IPFS_XUSB_HOST_MSI_BAR_SZ_0);
1817
1818         tegra->sregs.msi_axi_barst =
1819                 readl(tegra->ipfs_base + IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0);
1820
1821         tegra->sregs.msi_fpci_barst =
1822                 readl(tegra->ipfs_base + IPFS_XUSB_HOST_FPCI_BAR_ST_0);
1823
1824         tegra->sregs.msi_vec0 =
1825                 readl(tegra->ipfs_base + IPFS_XUSB_HOST_MSI_VEC0_0);
1826
1827         tegra->sregs.msi_en_vec0 =
1828                 readl(tegra->ipfs_base + IPFS_XUSB_HOST_MSI_EN_VEC0_0);
1829
1830         tegra->sregs.fpci_error_masks =
1831                 readl(tegra->ipfs_base + IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0);
1832
1833         tegra->sregs.intr_mask =
1834                 readl(tegra->ipfs_base + IPFS_XUSB_HOST_INTR_MASK_0);
1835
1836         tegra->sregs.ipfs_intr_enable =
1837                 readl(tegra->ipfs_base + IPFS_XUSB_HOST_IPFS_INTR_ENABLE_0);
1838
1839         tegra->sregs.ufpci_config =
1840                 readl(tegra->ipfs_base + IPFS_XUSB_HOST_UFPCI_CONFIG_0);
1841
1842         tegra->sregs.clkgate_hysteresis =
1843                 readl(tegra->ipfs_base + IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0);
1844
1845         tegra->sregs.xusb_host_mccif_fifo_cntrl =
1846                 readl(tegra->ipfs_base + IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0);
1847
1848         /* b. Save the CFG registers */
1849
1850         tegra->sregs.hs_pls =
1851                 readl(tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HS_PLS);
1852
1853         tegra->sregs.fs_pls =
1854                 readl(tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_FS_PLS);
1855
1856         tegra->sregs.hs_fs_speed =
1857                 readl(tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HSFS_SPEED);
1858
1859         tegra->sregs.hs_fs_pp =
1860                 readl(tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HSFS_PP);
1861
1862         tegra->sregs.cfg_aru =
1863                 readl(tegra->fpci_base + XUSB_CFG_ARU_CONTEXT);
1864
1865         tegra->sregs.cfg_order =
1866                 readl(tegra->fpci_base + XUSB_CFG_FPCICFG);
1867
1868         tegra->sregs.cfg_fladj =
1869                 readl(tegra->fpci_base + XUSB_CFG_24);
1870
1871         tegra->sregs.cfg_sid =
1872                 readl(tegra->fpci_base + XUSB_CFG_16);
1873 }
1874
1875 /* This function restores XUSB registers from device context */
1876 static void
1877 tegra_xhci_restore_ctx(struct tegra_xhci_hcd *tegra)
1878 {
1879         /* Restore Cfg registers */
1880         writel(tegra->sregs.hs_pls,
1881                 tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HS_PLS);
1882
1883         writel(tegra->sregs.fs_pls,
1884                 tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_FS_PLS);
1885
1886         writel(tegra->sregs.hs_fs_speed,
1887                 tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HSFS_SPEED);
1888
1889         writel(tegra->sregs.hs_fs_pp,
1890                 tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HSFS_PP);
1891
1892         writel(tegra->sregs.cfg_aru,
1893                 tegra->fpci_base + XUSB_CFG_ARU_CONTEXT);
1894
1895         writel(tegra->sregs.cfg_order,
1896                 tegra->fpci_base + XUSB_CFG_FPCICFG);
1897
1898         writel(tegra->sregs.cfg_fladj,
1899                 tegra->fpci_base + XUSB_CFG_24);
1900
1901         writel(tegra->sregs.cfg_sid,
1902                 tegra->fpci_base + XUSB_CFG_16);
1903
1904         /* Restore IPFS registers */
1905
1906         writel(tegra->sregs.msi_bar_sz,
1907                 tegra->ipfs_base + IPFS_XUSB_HOST_MSI_BAR_SZ_0);
1908
1909         writel(tegra->sregs.msi_axi_barst,
1910                 tegra->ipfs_base + IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0);
1911
1912         writel(tegra->sregs.msi_fpci_barst,
1913                 tegra->ipfs_base + IPFS_XUSB_HOST_FPCI_BAR_ST_0);
1914
1915         writel(tegra->sregs.msi_vec0,
1916                 tegra->ipfs_base + IPFS_XUSB_HOST_MSI_VEC0_0);
1917
1918         writel(tegra->sregs.msi_en_vec0,
1919                 tegra->ipfs_base + IPFS_XUSB_HOST_MSI_EN_VEC0_0);
1920
1921         writel(tegra->sregs.fpci_error_masks,
1922                 tegra->ipfs_base + IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0);
1923
1924         writel(tegra->sregs.intr_mask,
1925                 tegra->ipfs_base + IPFS_XUSB_HOST_INTR_MASK_0);
1926
1927         writel(tegra->sregs.ipfs_intr_enable,
1928                 tegra->ipfs_base + IPFS_XUSB_HOST_IPFS_INTR_ENABLE_0);
1929
1930         writel(tegra->sregs.ufpci_config,
1931                 tegra->fpci_base + IPFS_XUSB_HOST_UFPCI_CONFIG_0);
1932
1933         writel(tegra->sregs.clkgate_hysteresis,
1934                 tegra->ipfs_base + IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0);
1935
1936         writel(tegra->sregs.xusb_host_mccif_fifo_cntrl,
1937                 tegra->ipfs_base + IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0);
1938 }
1939
1940 static void tegra_xhci_enable_fw_message(struct tegra_xhci_hcd *tegra)
1941 {
1942         struct platform_device *pdev = tegra->pdev;
1943         u32 reg, timeout = 0xff, cmd;
1944
1945         mutex_lock(&tegra->mbox_lock);
1946
1947         do {
1948                 writel(MBOX_OWNER_SW,
1949                         tegra->fpci_base + XUSB_CFG_ARU_MBOX_OWNER);
1950                 reg = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_OWNER);
1951                 usleep_range(10, 20);
1952         } while (reg != MBOX_OWNER_SW && timeout--);
1953
1954         if ((timeout == 0) && (reg != MBOX_OWNER_SW)) {
1955                 dev_err(&pdev->dev, "Failed to set mbox message owner ID\n");
1956                 mutex_unlock(&tegra->mbox_lock);
1957                 return;
1958         }
1959
1960         writel((MBOX_CMD_MSG_ENABLED << MBOX_CMD_SHIFT),
1961                         tegra->fpci_base + XUSB_CFG_ARU_MBOX_DATA_IN);
1962
1963         cmd = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
1964         cmd |= MBOX_INT_EN | MBOX_FALC_INT_EN;
1965         writel(cmd, tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
1966
1967         mutex_unlock(&tegra->mbox_lock);
1968 }
1969
1970 static int load_firmware(struct tegra_xhci_hcd *tegra, bool resetARU)
1971 {
1972         struct platform_device *pdev = tegra->pdev;
1973         struct cfgtbl *cfg_tbl = (struct cfgtbl *) tegra->firmware.data;
1974         u32 phys_addr_lo;
1975         u32 HwReg;
1976         u16 nblocks;
1977         time_t fw_time;
1978         struct tm fw_tm;
1979         u8 hc_caplength;
1980         u32 usbsts, count = 0xff;
1981         struct xhci_cap_regs __iomem *cap_regs;
1982         struct xhci_op_regs __iomem *op_regs;
1983
1984         /* enable mbox interrupt */
1985         writel(readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD) | MBOX_INT_EN,
1986                 tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
1987
1988         /* First thing, reset the ARU. By the time we get to
1989          * loading boot code below, reset would be complete.
1990          * alternatively we can busy wait on rst pending bit.
1991          */
1992         /* Don't reset during ELPG/LP0 exit path */
1993         if (resetARU) {
1994                 iowrite32(0x1, tegra->fpci_base + XUSB_CFG_ARU_RST);
1995                 usleep_range(1000, 2000);
1996         }
1997
1998         if (csb_read(tegra, XUSB_CSB_MP_ILOAD_BASE_LO) != 0) {
1999                 dev_info(&pdev->dev, "Firmware already loaded, Falcon state 0x%x\n",
2000                                 csb_read(tegra, XUSB_FALC_CPUCTL));
2001                 return 0;
2002         }
2003
2004         /* update the phys_log_buffer and total_entries here */
2005         cfg_tbl->phys_addr_log_buffer = tegra->log.phys_addr;
2006         cfg_tbl->total_log_entries = FW_LOG_COUNT;
2007
2008         phys_addr_lo = tegra->firmware.dma;
2009         phys_addr_lo += sizeof(struct cfgtbl);
2010
2011         /* Program the size of DFI into ILOAD_ATTR */
2012         csb_write(tegra, XUSB_CSB_MP_ILOAD_ATTR, tegra->firmware.size);
2013
2014         /* Boot code of the firmware reads the ILOAD_BASE_LO register
2015          * to get to the start of the dfi in system memory.
2016          */
2017         csb_write(tegra, XUSB_CSB_MP_ILOAD_BASE_LO, phys_addr_lo);
2018
2019         /* Program the ILOAD_BASE_HI with a value of MSB 32 bits */
2020         csb_write(tegra, XUSB_CSB_MP_ILOAD_BASE_HI, 0);
2021
2022         /* Set BOOTPATH to 1 in APMAP Register. Bit 31 is APMAP_BOOTMAP */
2023         csb_write(tegra, XUSB_CSB_MP_APMAP, APMAP_BOOTPATH);
2024
2025         /* Invalidate L2IMEM. */
2026         csb_write(tegra, XUSB_CSB_MP_L2IMEMOP_TRIG, L2IMEM_INVALIDATE_ALL);
2027
2028         /* Initiate fetch of Bootcode from system memory into L2IMEM.
2029          * Program BootCode location and size in system memory.
2030          */
2031         HwReg = ((cfg_tbl->boot_codetag / IMEM_BLOCK_SIZE) &
2032                         L2IMEMOP_SIZE_SRC_OFFSET_MASK)
2033                         << L2IMEMOP_SIZE_SRC_OFFSET_SHIFT;
2034         HwReg |= ((cfg_tbl->boot_codesize / IMEM_BLOCK_SIZE) &
2035                         L2IMEMOP_SIZE_SRC_COUNT_MASK)
2036                         << L2IMEMOP_SIZE_SRC_COUNT_SHIFT;
2037         csb_write(tegra, XUSB_CSB_MP_L2IMEMOP_SIZE, HwReg);
2038
2039         /* Trigger L2IMEM Load operation. */
2040         csb_write(tegra, XUSB_CSB_MP_L2IMEMOP_TRIG, L2IMEM_LOAD_LOCKED_RESULT);
2041
2042         /* Setup Falcon Auto-fill */
2043         nblocks = (cfg_tbl->boot_codesize / IMEM_BLOCK_SIZE);
2044         if ((cfg_tbl->boot_codesize % IMEM_BLOCK_SIZE) != 0)
2045                 nblocks += 1;
2046         csb_write(tegra, XUSB_FALC_IMFILLCTL, nblocks);
2047
2048         HwReg = (cfg_tbl->boot_codetag / IMEM_BLOCK_SIZE) & IMFILLRNG_TAG_MASK;
2049         HwReg |= (((cfg_tbl->boot_codetag + cfg_tbl->boot_codesize)
2050                         /IMEM_BLOCK_SIZE) - 1) << IMFILLRNG1_TAG_HI_SHIFT;
2051         csb_write(tegra, XUSB_FALC_IMFILLRNG1, HwReg);
2052
2053         csb_write(tegra, XUSB_FALC_DMACTL, 0);
2054         msleep(50);
2055
2056         csb_write(tegra, XUSB_FALC_BOOTVEC, cfg_tbl->boot_codetag);
2057
2058         /* Start Falcon CPU */
2059         csb_write(tegra, XUSB_FALC_CPUCTL, CPUCTL_STARTCPU);
2060         usleep_range(1000, 2000);
2061
2062         fw_time = cfg_tbl->fwimg_created_time;
2063         time_to_tm(fw_time, 0, &fw_tm);
2064         dev_info(&pdev->dev,
2065                 "Firmware timestamp: %ld-%02d-%02d %02d:%02d:%02d UTC, "\
2066                 "Falcon state 0x%x\n", fw_tm.tm_year + 1900,
2067                 fw_tm.tm_mon + 1, fw_tm.tm_mday, fw_tm.tm_hour,
2068                 fw_tm.tm_min, fw_tm.tm_sec,
2069                 csb_read(tegra, XUSB_FALC_CPUCTL));
2070
2071         /* return fail if firmware status is not good */
2072         if (csb_read(tegra, XUSB_FALC_CPUCTL) == XUSB_FALC_STATE_HALTED)
2073                 return -EFAULT;
2074
2075         cap_regs = IO_ADDRESS(tegra->host_phy_base);
2076         hc_caplength = HC_LENGTH(ioread32(&cap_regs->hc_capbase));
2077         op_regs = IO_ADDRESS(tegra->host_phy_base + hc_caplength);
2078
2079         /* wait for USBSTS_CNR to get set */
2080         do {
2081                 usbsts = ioread32(&op_regs->status);
2082         } while ((usbsts & STS_CNR) && count--);
2083
2084         if (!count && (usbsts & STS_CNR)) {
2085                 dev_err(&pdev->dev, "Controller not ready\n");
2086                 return -EFAULT;
2087         }
2088         return 0;
2089 }
2090
2091 static void tegra_xhci_release_port_ownership(struct tegra_xhci_hcd *tegra,
2092         bool release)
2093 {
2094         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
2095         u32 reg;
2096
2097         /* Issue is only applicable for T114 */
2098         if (XUSB_DEVICE_ID_T114 != tegra->device_id)
2099                 return;
2100
2101         reg = readl(tegra->padctl_base + padregs->usb2_pad_mux_0);
2102         reg &= ~(USB2_OTG_PAD_PORT_MASK(0) | USB2_OTG_PAD_PORT_MASK(1) |
2103                         USB2_OTG_PAD_PORT_MASK(2));
2104
2105         if (!release) {
2106                 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P0)
2107                         reg |= USB2_OTG_PAD_PORT_OWNER_XUSB(0);
2108                 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P1)
2109                         reg |= USB2_OTG_PAD_PORT_OWNER_XUSB(1);
2110                 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
2111                         reg |= USB2_OTG_PAD_PORT_OWNER_XUSB(2);
2112         }
2113
2114         writel(reg, tegra->padctl_base + padregs->usb2_pad_mux_0);
2115 }
2116 /* SS ELPG Entry initiated by fw */
2117 static int tegra_xhci_ss_elpg_entry(struct tegra_xhci_hcd *tegra)
2118 {
2119         struct xhci_hcd *xhci = tegra->xhci;
2120         u32 ret = 0;
2121
2122         must_have_sync_lock(tegra);
2123
2124         /* This is SS partition ELPG entry
2125          * STEP 0: firmware will set WOC WOD bits in PVTPORTSC2 regs.
2126          */
2127
2128         /* Step 0: Acquire mbox and send PWRGATE msg to firmware
2129          * only if it is sw initiated one
2130          */
2131
2132         /* STEP 1: xHCI firmware and xHCIPEP driver communicates
2133          * SuperSpeed partition ELPG entry via mailbox protocol
2134          */
2135
2136         /* STEP 2: xHCI PEP driver and XUSB device mode driver
2137          * enable the XUSB wakeup interrupts for the SuperSpeed
2138          * and USB2.0 ports assigned to host.Section 4.1 Step 3
2139          */
2140         tegra_xhci_ss_wake_on_interrupts(tegra->bdata->portmap, true);
2141
2142         /* STEP 3: xHCI PEP driver initiates the signal sequence
2143          * to enable the XUSB SSwake detection logic for the
2144          * SuperSpeed ports assigned to host.Section 4.1 Step 4
2145          */
2146         tegra_xhci_ss_wake_signal(tegra->bdata->portmap, true);
2147
2148         /* STEP 4: System Power Management driver asserts reset
2149          * to XUSB SuperSpeed partition then disables its clocks
2150          */
2151         tegra_periph_reset_assert(tegra->ss_clk);
2152         clk_disable(tegra->ss_clk);
2153
2154         usleep_range(100, 200);
2155
2156         /* STEP 5: System Power Management driver disables the
2157          * XUSB SuperSpeed partition power rails.
2158          */
2159         debug_print_portsc(xhci);
2160
2161         /* tegra_powergate_partition also does partition reset assert */
2162         ret = tegra_powergate_partition(TEGRA_POWERGATE_XUSBA);
2163         if (ret) {
2164                 xhci_err(xhci, "%s: could not powergate xusba partition\n",
2165                                 __func__);
2166                 /* TODO: error recovery? */
2167         }
2168         tegra->ss_pwr_gated = true;
2169
2170         /* STEP 6: xHCI PEP driver initiates the signal sequence
2171          * to enable the XUSB SSwake detection logic for the
2172          * SuperSpeed ports assigned to host.Section 4.1 Step 7
2173          */
2174         tegra_xhci_ss_vcore(tegra->bdata->portmap, true);
2175
2176         return ret;
2177 }
2178
2179 /* Host ELPG Entry */
2180 static int tegra_xhci_host_elpg_entry(struct tegra_xhci_hcd *tegra)
2181 {
2182         struct xhci_hcd *xhci = tegra->xhci;
2183         u32 ret;
2184
2185         must_have_sync_lock(tegra);
2186
2187         /* If ss is already powergated skip ss ctx save stuff */
2188         if (tegra->ss_pwr_gated) {
2189                 xhci_info(xhci, "%s: SS partition is already powergated\n",
2190                         __func__);
2191         } else {
2192                 ret = tegra_xhci_ss_elpg_entry(tegra);
2193                 if (ret) {
2194                         xhci_err(xhci, "%s: ss_elpg_entry failed %d\n",
2195                                 __func__, ret);
2196                         return ret;
2197                 }
2198         }
2199
2200         /* 1. IS INTR PENDING INT_PENDING=1 ? */
2201
2202         /* STEP 1.1: Do a context save of XUSB and IPFS registers */
2203         tegra_xhci_save_xusb_ctx(tegra);
2204
2205         /* calculate rctrl_val and tctrl_val */
2206         tegra_xhci_war_for_tctrl_rctrl(tegra);
2207
2208         pmc_init(tegra, 1);
2209
2210         tegra_xhci_hs_wake_on_interrupts(tegra->bdata->portmap, true);
2211         xhci_dbg(xhci, "%s: PMC_UTMIP_UHSIC_SLEEP_CFG_0 = %x\n", __func__,
2212                 tegra_usb_pmc_reg_read(PMC_UTMIP_UHSIC_SLEEP_CFG_0));
2213
2214         /* STEP 4: Assert reset to host clk and disable host clk */
2215         tegra_periph_reset_assert(tegra->host_clk);
2216
2217         clk_disable(tegra->host_clk);
2218
2219         /* wait 150us */
2220         usleep_range(150, 200);
2221
2222         /* flush MC client of XUSB_HOST */
2223         tegra_powergate_mc_flush(TEGRA_POWERGATE_XUSBC);
2224
2225         /* STEP 4: Powergate host partition */
2226         /* tegra_powergate_partition also does partition reset assert */
2227         ret = tegra_powergate_partition(TEGRA_POWERGATE_XUSBC);
2228         if (ret) {
2229                 xhci_err(xhci, "%s: could not unpowergate xusbc partition %d\n",
2230                         __func__, ret);
2231                 /* TODO: error handling? */
2232                 return ret;
2233         }
2234         tegra->host_pwr_gated = true;
2235
2236         if (tegra->pdata->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
2237                 clk_disable(tegra->pll_re_vco_clk);
2238         clk_disable(tegra->emc_clk);
2239         /* set port ownership to SNPS */
2240         tegra_xhci_release_port_ownership(tegra, true);
2241
2242         xhci_dbg(xhci, "%s: PMC_UTMIP_UHSIC_SLEEP_CFG_0 = %x\n", __func__,
2243                 tegra_usb_pmc_reg_read(PMC_UTMIP_UHSIC_SLEEP_CFG_0));
2244
2245         xhci_info(xhci, "%s: elpg_entry: completed\n", __func__);
2246         xhci_dbg(xhci, "%s: HOST POWER STATUS = %d\n",
2247                 __func__, tegra_powergate_is_powered(TEGRA_POWERGATE_XUSBC));
2248         return ret;
2249 }
2250
2251 /* SS ELPG Exit triggered by PADCTL irq */
2252 /**
2253  * tegra_xhci_ss_partition_elpg_exit - bring XUSBA partition out from elpg
2254  *
2255  * This function must be called with tegra->sync_lock acquired.
2256  *
2257  * @tegra: xhci controller context
2258  * @return 0 for success, or error numbers
2259  */
2260 static int tegra_xhci_ss_partition_elpg_exit(struct tegra_xhci_hcd *tegra)
2261 {
2262         struct xhci_hcd *xhci = tegra->xhci;
2263         int ret = 0;
2264
2265         must_have_sync_lock(tegra);
2266
2267         if (tegra->ss_pwr_gated && (tegra->ss_wake_event ||
2268                         tegra->hs_wake_event || tegra->host_resume_req)) {
2269
2270                 /*
2271                  * PWR_UNGATE SS partition. XUSBA
2272                  * tegra_unpowergate_partition also does partition reset
2273                  * deassert
2274                  */
2275                 ret = tegra_unpowergate_partition(TEGRA_POWERGATE_XUSBA);
2276                 if (ret) {
2277                         xhci_err(xhci,
2278                         "%s: could not unpowergate xusba partition %d\n",
2279                         __func__, ret);
2280                         goto out;
2281                 }
2282                 if (tegra->ss_wake_event)
2283                         tegra->ss_wake_event = false;
2284
2285         } else {
2286                 xhci_info(xhci, "%s: ss already power gated\n",
2287                         __func__);
2288                 return ret;
2289         }
2290
2291         /* Step 3: Enable clock to ss partition */
2292         clk_enable(tegra->ss_clk);
2293
2294         /* Step 4: Disable ss wake detection logic */
2295         tegra_xhci_ss_wake_on_interrupts(tegra->bdata->portmap, false);
2296
2297         /* Step 4.1: Disable ss wake detection logic */
2298         tegra_xhci_ss_vcore(tegra->bdata->portmap, false);
2299
2300         /* wait 150us */
2301         usleep_range(150, 200);
2302
2303         /* Step 4.2: Disable ss wake detection logic */
2304         tegra_xhci_ss_wake_signal(tegra->bdata->portmap, false);
2305
2306         /* Step 6 Deassert reset for ss clks */
2307         tegra_periph_reset_deassert(tegra->ss_clk);
2308
2309         xhci_dbg(xhci, "%s: SS ELPG EXIT. ALL DONE\n", __func__);
2310         tegra->ss_pwr_gated = false;
2311 out:
2312         return ret;
2313 }
2314
2315 static void ss_partition_elpg_exit_work(struct work_struct *work)
2316 {
2317         struct tegra_xhci_hcd *tegra = container_of(work, struct tegra_xhci_hcd,
2318                 ss_elpg_exit_work);
2319
2320         mutex_lock(&tegra->sync_lock);
2321         tegra_xhci_ss_partition_elpg_exit(tegra);
2322         mutex_unlock(&tegra->sync_lock);
2323 }
2324
2325 /* read pmc WAKE2_STATUS register to know if SS port caused remote wake */
2326 static void update_remote_wakeup_ports_pmc(struct tegra_xhci_hcd *tegra)
2327 {
2328         struct xhci_hcd *xhci = tegra->xhci;
2329         u32 wake2_status;
2330
2331 #define PMC_WAKE2_STATUS        0x168
2332 #define PADCTL_WAKE             (1 << (58 - 32)) /* PADCTL is WAKE#58 */
2333
2334         wake2_status = tegra_usb_pmc_reg_read(PMC_WAKE2_STATUS);
2335
2336         if (wake2_status & PADCTL_WAKE) {
2337                 /* FIXME: This is customized for Dalmore, find a generic way */
2338                 set_bit(0, &tegra->usb3_rh_remote_wakeup_ports);
2339                 /* clear wake status */
2340                 tegra_usb_pmc_reg_write(PMC_WAKE2_STATUS, PADCTL_WAKE);
2341         }
2342
2343         xhci_dbg(xhci, "%s: usb3 roothub remote_wakeup_ports 0x%lx\n",
2344                         __func__, tegra->usb3_rh_remote_wakeup_ports);
2345 }
2346
2347 static void wait_remote_wakeup_ports(struct usb_hcd *hcd)
2348 {
2349         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2350         struct tegra_xhci_hcd *tegra = hcd_to_tegra_xhci(hcd);
2351         int port, num_ports;
2352         unsigned long *remote_wakeup_ports;
2353         u32 portsc;
2354         __le32 __iomem  **port_array;
2355         unsigned char *rh;
2356         unsigned int retry = 64;
2357
2358
2359         if (hcd == xhci->shared_hcd) {
2360                 port_array = xhci->usb3_ports;
2361                 num_ports = xhci->num_usb3_ports;
2362                 remote_wakeup_ports = &tegra->usb3_rh_remote_wakeup_ports;
2363                 rh = "usb3 roothub";
2364         } else
2365                 return;
2366
2367         while (*remote_wakeup_ports && retry--) {
2368                 for_each_set_bit(port, remote_wakeup_ports, num_ports) {
2369                         portsc = xhci_readl(xhci, port_array[port]);
2370
2371                         if (!(portsc & PORT_CONNECT)) {
2372                                 /* nothing to do if already disconnected */
2373                                 clear_bit(port, remote_wakeup_ports);
2374                                 continue;
2375                         }
2376
2377                         if ((portsc & PORT_PLS_MASK) == XDEV_U0)
2378                                 clear_bit(port, remote_wakeup_ports);
2379                         else
2380                                 xhci_dbg(xhci, "%s: %s port %d status 0x%x\n",
2381                                                 __func__, rh, port, portsc);
2382                 }
2383
2384                 if (*remote_wakeup_ports)
2385                         msleep(20); /* give some time, irq will direct U0 */
2386         }
2387
2388         xhci_dbg(xhci, "%s: %s remote_wakeup_ports 0x%lx\n", __func__, rh,
2389                         *remote_wakeup_ports);
2390 }
2391
2392 static void tegra_xhci_war_for_tctrl_rctrl(struct tegra_xhci_hcd *tegra)
2393 {
2394         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
2395         u32 reg, utmip_rctrl_val, utmip_tctrl_val, pad_mux, portmux, portowner;
2396
2397         portmux = USB2_OTG_PAD_PORT_MASK(0) | USB2_OTG_PAD_PORT_MASK(1);
2398         portowner = USB2_OTG_PAD_PORT_OWNER_XUSB(0) |
2399                         USB2_OTG_PAD_PORT_OWNER_XUSB(1);
2400
2401         if (XUSB_DEVICE_ID_T114 != tegra->device_id) {
2402                 portmux |= USB2_OTG_PAD_PORT_MASK(2);
2403                 portowner |= USB2_OTG_PAD_PORT_OWNER_XUSB(2);
2404         }
2405
2406         /* Use xusb padctl space only when xusb owns all UTMIP port */
2407         pad_mux = readl(tegra->padctl_base + padregs->usb2_pad_mux_0);
2408         if ((pad_mux & portmux) == portowner) {
2409                 /* XUSB_PADCTL_USB2_BIAS_PAD_CTL_0_0::PD = 0 and
2410                  * XUSB_PADCTL_USB2_BIAS_PAD_CTL_0_0::PD_TRK = 0
2411                  */
2412                 reg = readl(tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
2413                 reg &= ~((1 << 12) | (1 << 13));
2414                 writel(reg, tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
2415
2416                 /* wait 20us */
2417                 usleep_range(20, 30);
2418
2419                 /* Read XUSB_PADCTL:: XUSB_PADCTL_USB2_BIAS_PAD_CTL_1_0
2420                  * :: TCTRL and RCTRL
2421                  */
2422                 reg = readl(tegra->padctl_base + padregs->usb2_bias_pad_ctl1_0);
2423                 utmip_rctrl_val = RCTRL(reg);
2424                 utmip_tctrl_val = TCTRL(reg);
2425
2426                 /*
2427                  * tctrl_val = 0x1f - (16 - ffz(utmip_tctrl_val)
2428                  * rctrl_val = 0x1f - (16 - ffz(utmip_rctrl_val)
2429                  */
2430                 pmc_data.utmip_rctrl_val = 0xf + ffz(utmip_rctrl_val);
2431                 pmc_data.utmip_tctrl_val = 0xf + ffz(utmip_tctrl_val);
2432
2433                 xhci_dbg(tegra->xhci, "rctrl_val = 0x%x, tctrl_val = 0x%x\n",
2434                         pmc_data.utmip_rctrl_val, pmc_data.utmip_tctrl_val);
2435
2436                 /* XUSB_PADCTL_USB2_BIAS_PAD_CTL_0_0::PD = 1 and
2437                  * XUSB_PADCTL_USB2_BIAS_PAD_CTL_0_0::PD_TRK = 1
2438                  */
2439                 reg = readl(tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
2440                 reg |= (1 << 13);
2441                 writel(reg, tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
2442
2443                 /* Program these values into PMC regiseter and program the
2444                  * PMC override
2445                  */
2446                 reg = PMC_TCTRL_VAL(pmc_data.utmip_tctrl_val) |
2447                         PMC_RCTRL_VAL(pmc_data.utmip_rctrl_val);
2448                 tegra_usb_pmc_reg_update(PMC_UTMIP_TERM_PAD_CFG,
2449                         0xffffffff, reg);
2450
2451                 reg = UTMIP_RCTRL_USE_PMC_P2 | UTMIP_TCTRL_USE_PMC_P2;
2452                 tegra_usb_pmc_reg_update(PMC_SLEEP_CFG, reg, reg);
2453         } else {
2454                 /* TODO use common PMC API to use SNPS register space */
2455         }
2456 }
2457
2458 /* Host ELPG Exit triggered by PADCTL irq */
2459 /**
2460  * tegra_xhci_host_partition_elpg_exit - bring XUSBC partition out from elpg
2461  *
2462  * This function must be called with tegra->sync_lock acquired.
2463  *
2464  * @tegra: xhci controller context
2465  * @return 0 for success, or error numbers
2466  */
2467 static int
2468 tegra_xhci_host_partition_elpg_exit(struct tegra_xhci_hcd *tegra)
2469 {
2470         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
2471         struct xhci_hcd *xhci = tegra->xhci;
2472         int ret = 0;
2473
2474         must_have_sync_lock(tegra);
2475
2476         if (!tegra->hc_in_elpg)
2477                 return 0;
2478
2479         clk_enable(tegra->emc_clk);
2480         if (tegra->pdata->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
2481                 clk_enable(tegra->pll_re_vco_clk);
2482         /* Step 2: Enable clock to host partition */
2483         clk_enable(tegra->host_clk);
2484
2485         if (tegra->lp0_exit) {
2486                 u32 reg, oc_bits = 0;
2487
2488                 /* Issue is only applicable for T114 */
2489                 if (XUSB_DEVICE_ID_T114 == tegra->device_id)
2490                         tegra_xhci_war_for_tctrl_rctrl(tegra);
2491                 /* check if over current seen. Clear if present */
2492                 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P0)
2493                         oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD0;
2494                 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P1)
2495                         oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD1;
2496                 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
2497                         oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD2;
2498
2499                 reg = readl(tegra->padctl_base + padregs->oc_det_0);
2500                 xhci_dbg(xhci, "%s: oc_det_0=0x%x\n", __func__, reg);
2501                 if (reg & oc_bits) {
2502                         xhci_info(xhci, "Over current detected. Clearing...\n");
2503                         writel(reg, tegra->padctl_base + padregs->oc_det_0);
2504
2505                         usleep_range(100, 200);
2506
2507                         reg = readl(tegra->padctl_base + padregs->oc_det_0);
2508                         if (reg & oc_bits)
2509                                 xhci_info(xhci, "Over current still present\n");
2510                 }
2511                 tegra_xhci_padctl_portmap_and_caps(tegra);
2512                 /* release clamps post deassert */
2513                 tegra->lp0_exit = false;
2514         }
2515
2516         /* Clear FLUSH_ENABLE of MC client */
2517         tegra_powergate_mc_flush_done(TEGRA_POWERGATE_XUSBC);
2518
2519         /* set port ownership back to xusb */
2520         tegra_xhci_release_port_ownership(tegra, false);
2521
2522         /*
2523          * PWR_UNGATE Host partition. XUSBC
2524          * tegra_unpowergate_partition also does partition reset deassert
2525          */
2526         ret = tegra_unpowergate_partition(TEGRA_POWERGATE_XUSBC);
2527         if (ret) {
2528                 xhci_err(xhci, "%s: could not unpowergate xusbc partition %d\n",
2529                         __func__, ret);
2530                 goto out;
2531         }
2532
2533         /* Step 4: Deassert reset to host partition clk */
2534         tegra_periph_reset_deassert(tegra->host_clk);
2535
2536         /* Step 6.1: IPFS and XUSB BAR initialization */
2537         tegra_xhci_cfg(tegra);
2538
2539         /* Step 6.2: IPFS and XUSB related restore */
2540         tegra_xhci_restore_ctx(tegra);
2541
2542         /* Step 8: xhci spec related ctx restore
2543          * will be done in xhci_resume().Do it here.
2544          */
2545
2546         tegra_xhci_ss_partition_elpg_exit(tegra);
2547
2548         /* Change SS clock source to HSIC_480 and set ss_src_clk at 120MHz */
2549         if (clk_get_rate(tegra->ss_src_clk) == 12000000) {
2550                 clk_set_rate(tegra->ss_src_clk,  3000 * 1000);
2551                 clk_set_parent(tegra->ss_src_clk, tegra->pll_u_480M);
2552         }
2553
2554         /* clear ovrd bits */
2555         tegra_xhci_rx_idle_mode_override(tegra, false);
2556
2557         /* Load firmware */
2558         xhci_dbg(xhci, "%s: elpg_exit: loading firmware from pmc.\n"
2559                         "ss (p1=0x%x, p2=0x%x, p3=0x%x), "
2560                         "hs (p1=0x%x, p2=0x%x, p3=0x%x),\n"
2561                         "fs (p1=0x%x, p2=0x%x, p3=0x%x)\n",
2562                         __func__,
2563                         csb_read(tegra, XUSB_FALC_SS_PVTPORTSC1),
2564                         csb_read(tegra, XUSB_FALC_SS_PVTPORTSC2),
2565                         csb_read(tegra, XUSB_FALC_SS_PVTPORTSC3),
2566                         csb_read(tegra, XUSB_FALC_HS_PVTPORTSC1),
2567                         csb_read(tegra, XUSB_FALC_HS_PVTPORTSC2),
2568                         csb_read(tegra, XUSB_FALC_HS_PVTPORTSC3),
2569                         csb_read(tegra, XUSB_FALC_FS_PVTPORTSC1),
2570                         csb_read(tegra, XUSB_FALC_FS_PVTPORTSC2),
2571                         csb_read(tegra, XUSB_FALC_FS_PVTPORTSC3));
2572         debug_print_portsc(xhci);
2573
2574         ret = load_firmware(tegra, false /* EPLG exit, do not reset ARU */);
2575         if (ret < 0) {
2576                 xhci_err(xhci, "%s: failed to load firmware %d\n",
2577                         __func__, ret);
2578                 goto out;
2579         }
2580
2581         pmc_init(tegra, 0);
2582
2583         tegra->hc_in_elpg = false;
2584         ret = xhci_resume(tegra->xhci, 0);
2585         if (ret) {
2586                 xhci_err(xhci, "%s: could not resume right %d\n",
2587                                 __func__, ret);
2588                 goto out;
2589         }
2590
2591         update_remote_wakeup_ports_pmc(tegra);
2592
2593         if (tegra->hs_wake_event)
2594                 tegra->hs_wake_event = false;
2595
2596         if (tegra->host_resume_req)
2597                 tegra->host_resume_req = false;
2598
2599         xhci_info(xhci, "elpg_exit: completed: lp0/elpg time=%d msec\n",
2600                 jiffies_to_msecs(jiffies - tegra->last_jiffies));
2601
2602         tegra->host_pwr_gated = false;
2603 out:
2604         return ret;
2605 }
2606
2607 static void host_partition_elpg_exit_work(struct work_struct *work)
2608 {
2609         struct tegra_xhci_hcd *tegra = container_of(work, struct tegra_xhci_hcd,
2610                 host_elpg_exit_work);
2611
2612         mutex_lock(&tegra->sync_lock);
2613         tegra_xhci_host_partition_elpg_exit(tegra);
2614         mutex_unlock(&tegra->sync_lock);
2615 }
2616
2617 /* Mailbox handling function. This function handles requests
2618  * from firmware and communicates with clock and powergating
2619  * module to alter clock rates and to power gate/ungate xusb
2620  * partitions.
2621  *
2622  * Following is the structure of mailbox messages.
2623  * bit 31:28 - msg type
2624  * bits 27:0 - mbox data
2625  * FIXME:  Check if we can just call clock functions like below
2626  * or should we schedule it for calling later ?
2627  */
2628
2629 static void
2630 tegra_xhci_process_mbox_message(struct work_struct *work)
2631 {
2632         u32 sw_resp = 0, cmd, data_in, fw_msg;
2633         int ret = 0;
2634         struct tegra_xhci_hcd *tegra = container_of(work, struct tegra_xhci_hcd,
2635                                         mbox_work);
2636         struct xhci_hcd *xhci = tegra->xhci;
2637         unsigned int freq_khz;
2638
2639         mutex_lock(&tegra->mbox_lock);
2640
2641         /* get the owner id */
2642         tegra->mbox_owner = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_OWNER);
2643         tegra->mbox_owner &= MBOX_OWNER_ID_MASK;
2644
2645         /* get the mbox message from firmware */
2646         fw_msg = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_DATA_OUT);
2647
2648         data_in = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_DATA_IN);
2649         if (data_in) {
2650                 mutex_unlock(&tegra->mbox_lock);
2651                 return;
2652         }
2653
2654         /* get cmd type and cmd data */
2655         tegra->cmd_type = (fw_msg & MBOX_CMD_TYPE_MASK) >> MBOX_CMD_SHIFT;
2656         tegra->cmd_data = (fw_msg & MBOX_CMD_DATA_MASK);
2657
2658         /* decode the message and make appropriate requests to
2659          * clock or powergating module.
2660          */
2661
2662         switch (tegra->cmd_type) {
2663         case MBOX_CMD_INC_FALC_CLOCK:
2664         case MBOX_CMD_DEC_FALC_CLOCK:
2665                 ret = tegra_xusb_request_clk_rate(
2666                                 tegra,
2667                                 tegra->falc_clk,
2668                                 tegra->cmd_data,
2669                                 &sw_resp);
2670                 if (ret)
2671                         xhci_err(xhci, "%s: could not set required falc rate\n",
2672                                 __func__);
2673                 goto send_sw_response;
2674         case MBOX_CMD_INC_SSPI_CLOCK:
2675         case MBOX_CMD_DEC_SSPI_CLOCK:
2676                 ret = tegra_xusb_request_clk_rate(
2677                                 tegra,
2678                                 tegra->ss_src_clk,
2679                                 tegra->cmd_data,
2680                                 &sw_resp);
2681                 if (ret)
2682                         xhci_err(xhci, "%s: could not set required ss rate.\n",
2683                                 __func__);
2684                 goto send_sw_response;
2685         case MBOX_CMD_SET_BW:
2686                 /* fw sends BW request in MByte/sec */
2687                 freq_khz = tegra_emc_bw_to_freq_req(tegra->cmd_data << 10);
2688                 clk_set_rate(tegra->emc_clk, freq_khz * 1000);
2689
2690                 /* clear MBOX_SMI_INT_EN bit */
2691                 cmd = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
2692                 cmd &= ~MBOX_SMI_INT_EN;
2693                 writel(cmd, tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
2694
2695                 /* clear mbox owner as ACK will not be sent for this request */
2696                 writel(0, tegra->fpci_base + XUSB_CFG_ARU_MBOX_OWNER);
2697                 break;
2698         case MBOX_CMD_SAVE_DFE_CTLE_CTX:
2699                 tegra_xhci_save_dfe_ctle_context(tegra, tegra->cmd_data);
2700                 tegra_xhci_restore_dfe_ctle_context(tegra, tegra->cmd_data);
2701                 sw_resp |= tegra->cmd_data | (MBOX_CMD_ACK << MBOX_CMD_SHIFT);
2702                 goto send_sw_response;
2703         case MBOX_CMD_ACK:
2704                 writel(0, tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
2705                 writel(0, tegra->fpci_base + XUSB_CFG_ARU_MBOX_OWNER);
2706                 break;
2707         case MBOX_CMD_NACK:
2708                 writel(0, tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
2709                 writel(0, tegra->fpci_base + XUSB_CFG_ARU_MBOX_OWNER);
2710                 break;
2711         default:
2712                 xhci_err(xhci, "%s: invalid cmdtype %d\n",
2713                                 __func__, tegra->cmd_type);
2714         }
2715         mutex_unlock(&tegra->mbox_lock);
2716         return;
2717
2718 send_sw_response:
2719         writel(sw_resp, tegra->fpci_base + XUSB_CFG_ARU_MBOX_DATA_IN);
2720         cmd = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
2721         cmd |= MBOX_INT_EN | MBOX_FALC_INT_EN;
2722         writel(cmd, tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
2723
2724         mutex_unlock(&tegra->mbox_lock);
2725 }
2726
2727 static irqreturn_t tegra_xhci_xusb_host_irq(int irq, void *ptrdev)
2728 {
2729         struct tegra_xhci_hcd *tegra = (struct tegra_xhci_hcd *) ptrdev;
2730         struct xhci_hcd *xhci = tegra->xhci;
2731
2732         xhci_dbg(xhci, "%s", __func__);
2733         return IRQ_HANDLED;
2734 }
2735
2736 static irqreturn_t tegra_xhci_padctl_irq(int irq, void *ptrdev)
2737 {
2738         struct tegra_xhci_hcd *tegra = (struct tegra_xhci_hcd *) ptrdev;
2739         struct xhci_hcd *xhci = tegra->xhci;
2740         struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
2741         u32 elpg_program0 = 0;
2742
2743         spin_lock(&tegra->lock);
2744
2745         tegra->last_jiffies = jiffies;
2746
2747         /* Check the intr cause. Could be  USB2 or HSIC or SS wake events */
2748         elpg_program0 = tegra_usb_pad_reg_read(padregs->elpg_program_0);
2749
2750         /* Clear the interrupt cause. We already read the intr status. */
2751         tegra_xhci_ss_wake_on_interrupts(tegra->bdata->portmap, false);
2752         tegra_xhci_hs_wake_on_interrupts(tegra->bdata->portmap, false);
2753
2754         xhci_dbg(xhci, "%s: elpg_program0 = %x\n", __func__, elpg_program0);
2755         xhci_dbg(xhci, "%s: PMC REGISTER = %x\n", __func__,
2756                 tegra_usb_pmc_reg_read(PMC_UTMIP_UHSIC_SLEEP_CFG_0));
2757         xhci_dbg(xhci, "%s: OC_DET Register = %x\n",
2758                 __func__, readl(tegra->padctl_base + padregs->oc_det_0));
2759         xhci_dbg(xhci, "%s: usb2_bchrg_otgpad0_ctl0_0 Register = %x\n",
2760                 __func__,
2761                 readl(tegra->padctl_base + padregs->usb2_bchrg_otgpad0_ctl0_0));
2762         xhci_dbg(xhci, "%s: usb2_bchrg_otgpad1_ctl0_0 Register = %x\n",
2763                 __func__,
2764                 readl(tegra->padctl_base + padregs->usb2_bchrg_otgpad1_ctl0_0));
2765         xhci_dbg(xhci, "%s: usb2_bchrg_bias_pad_0 Register = %x\n",
2766                 __func__,
2767                 readl(tegra->padctl_base + padregs->usb2_bchrg_bias_pad_0));
2768
2769         if (elpg_program0 & (SS_PORT0_WAKEUP_EVENT | SS_PORT1_WAKEUP_EVENT))
2770                 tegra->ss_wake_event = true;
2771         else if (elpg_program0 &
2772                         (USB2_PORT0_WAKEUP_EVENT | USB2_PORT1_WAKEUP_EVENT |
2773                                 USB2_PORT2_WAKEUP_EVENT))
2774                 tegra->hs_wake_event = true;
2775
2776         if (tegra->ss_wake_event || tegra->hs_wake_event) {
2777                 if (tegra->ss_pwr_gated && !tegra->host_pwr_gated) {
2778                         xhci_err(xhci, "SS gated Host ungated. Should not happen\n");
2779                         WARN_ON(tegra->ss_pwr_gated && tegra->host_pwr_gated);
2780                 } else if (tegra->ss_pwr_gated
2781                                 && tegra->host_pwr_gated) {
2782                         xhci_dbg(xhci, "[%s] schedule host_elpg_exit_work\n",
2783                                 __func__);
2784                         schedule_work(&tegra->host_elpg_exit_work);
2785                 }
2786         } else {
2787                 xhci_err(xhci, "error: wake due to no hs/ss event\n");
2788                 tegra_usb_pad_reg_write(padregs->elpg_program_0, 0xffffffff);
2789         }
2790         spin_unlock(&tegra->lock);
2791         return IRQ_HANDLED;
2792 }
2793
2794 static irqreturn_t tegra_xhci_smi_irq(int irq, void *ptrdev)
2795 {
2796         struct tegra_xhci_hcd *tegra = (struct tegra_xhci_hcd *) ptrdev;
2797         u32 temp;
2798
2799         spin_lock(&tegra->lock);
2800
2801         /* clear the mbox intr status 1st thing. Other
2802          * bits are W1C bits, so just write to SMI bit.
2803          */
2804
2805         temp = readl(tegra->fpci_base + XUSB_CFG_ARU_SMI_INTR);
2806
2807         /* write 1 to clear SMI INTR en bit ( bit 3 ) */
2808         temp = MBOX_SMI_INTR_EN;
2809         writel(temp, tegra->fpci_base + XUSB_CFG_ARU_SMI_INTR);
2810
2811         schedule_work(&tegra->mbox_work);
2812
2813         spin_unlock(&tegra->lock);
2814         return IRQ_HANDLED;
2815 }
2816
2817 static void tegra_xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
2818 {
2819         /*
2820          * As of now platform drivers don't provide MSI support so we ensure
2821          * here that the generic code does not try to make a pci_dev from our
2822          * dev struct in order to setup MSI
2823          */
2824         xhci->quirks |= XHCI_BROKEN_MSI;
2825         xhci->quirks &= ~XHCI_SPURIOUS_REBOOT;
2826 }
2827
2828 /* called during probe() after chip reset completes */
2829 static int xhci_plat_setup(struct usb_hcd *hcd)
2830 {
2831         return xhci_gen_setup(hcd, tegra_xhci_plat_quirks);
2832 }
2833
2834 static int tegra_xhci_request_mem_region(struct platform_device *pdev,
2835         const char *name, void __iomem **region)
2836 {
2837         struct resource *res;
2838         void __iomem *mem;
2839
2840         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
2841         if (!res) {
2842                 dev_err(&pdev->dev, "memory resource %s doesn't exist\n", name);
2843                 return -ENODEV;
2844         }
2845
2846         mem = devm_request_and_ioremap(&pdev->dev, res);
2847         if (!mem) {
2848                 dev_err(&pdev->dev, "failed to ioremap for %s\n", name);
2849                 return -EFAULT;
2850         }
2851         *region = mem;
2852
2853         return 0;
2854 }
2855
2856 static int tegra_xhci_request_irq(struct platform_device *pdev,
2857         const char *rscname, irq_handler_t handler, unsigned long irqflags,
2858         const char *devname, int *irq_no)
2859 {
2860         int ret;
2861         struct tegra_xhci_hcd *tegra = platform_get_drvdata(pdev);
2862         struct resource *res;
2863
2864         res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, rscname);
2865         if (!res) {
2866                 dev_err(&pdev->dev, "irq resource %s doesn't exist\n", rscname);
2867                 return -ENODEV;
2868         }
2869
2870         ret = devm_request_irq(&pdev->dev, res->start, handler, irqflags,
2871                         devname, tegra);
2872         if (ret != 0) {
2873                 dev_err(&pdev->dev,
2874                         "failed to request_irq for %s (irq %d), error = %d\n",
2875                         devname, res->start, ret);
2876                 return ret;
2877         }
2878         *irq_no = res->start;
2879
2880         return 0;
2881 }
2882
2883 #ifdef CONFIG_PM
2884
2885 static int tegra_xhci_bus_suspend(struct usb_hcd *hcd)
2886 {
2887         struct tegra_xhci_hcd *tegra = hcd_to_tegra_xhci(hcd);
2888         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2889         int err = 0;
2890         unsigned long flags;
2891
2892         mutex_lock(&tegra->sync_lock);
2893
2894         if (xhci->shared_hcd == hcd) {
2895                 tegra->usb3_rh_suspend = true;
2896                 xhci_dbg(xhci, "%s: usb3 root hub\n", __func__);
2897         } else if (xhci->main_hcd == hcd) {
2898                 tegra->usb2_rh_suspend = true;
2899                 xhci_dbg(xhci, "%s: usb2 root hub\n", __func__);
2900         }
2901
2902         WARN_ON(tegra->hc_in_elpg);
2903
2904         /* suspend xhci bus. This will also set remote mask */
2905         err = xhci_bus_suspend(hcd);
2906         if (err) {
2907                 xhci_err(xhci, "%s: xhci_bus_suspend failed %d\n",
2908                                 __func__, err);
2909                 goto xhci_bus_suspend_failed;
2910         }
2911
2912         if (!(tegra->usb2_rh_suspend && tegra->usb3_rh_suspend))
2913                 goto done; /* one of the root hubs is still working */
2914
2915         spin_lock_irqsave(&tegra->lock, flags);
2916         tegra->hc_in_elpg = true;
2917         spin_unlock_irqrestore(&tegra->lock, flags);
2918
2919         WARN_ON(tegra->ss_pwr_gated && tegra->host_pwr_gated);
2920
2921         /* save xhci spec ctx. Already done by xhci_suspend */
2922         err = xhci_suspend(tegra->xhci);
2923         if (err) {
2924                 xhci_err(xhci, "%s: xhci_suspend failed %d\n", __func__, err);
2925                 goto xhci_suspend_failed;
2926         }
2927
2928         /* Powergate host. Include ss power gate if not already done */
2929         err = tegra_xhci_host_elpg_entry(tegra);
2930         if (err) {
2931                 xhci_err(xhci, "%s: unable to perform elpg entry %d\n",
2932                                 __func__, err);
2933                 goto tegra_xhci_host_elpg_entry_failed;
2934         }
2935
2936         /* At this point,ensure ss/hs intr enables are always on */
2937         tegra_xhci_ss_wake_on_interrupts(tegra->bdata->portmap, true);
2938         tegra_xhci_hs_wake_on_interrupts(tegra->bdata->portmap, true);
2939
2940         /* In ELPG, firmware log context is gone. Rewind shared log buffer. */
2941         if (fw_log_wait_empty_timeout(tegra, 100))
2942                 xhci_warn(xhci, "%s still has logs\n", __func__);
2943         tegra->log.dequeue = tegra->log.virt_addr;
2944         tegra->log.seq = 0;
2945
2946 done:
2947         /* pads are disabled only if usb2 root hub in xusb is idle */
2948         /* pads will actually be disabled only when all usb2 ports are idle */
2949         if (xhci->main_hcd == hcd) {
2950                 utmi_phy_pad_disable();
2951                 utmi_phy_iddq_override(true);
2952         }
2953         mutex_unlock(&tegra->sync_lock);
2954         return 0;
2955
2956 tegra_xhci_host_elpg_entry_failed:
2957
2958 xhci_suspend_failed:
2959         tegra->hc_in_elpg = false;
2960 xhci_bus_suspend_failed:
2961         if (xhci->shared_hcd == hcd)
2962                 tegra->usb3_rh_suspend = false;
2963         else if (xhci->main_hcd == hcd)
2964                 tegra->usb2_rh_suspend = false;
2965
2966         mutex_unlock(&tegra->sync_lock);
2967         return err;
2968 }
2969
2970 /* First, USB2HCD and then USB3HCD resume will be called */
2971 static int tegra_xhci_bus_resume(struct usb_hcd *hcd)
2972 {
2973         struct tegra_xhci_hcd *tegra = hcd_to_tegra_xhci(hcd);
2974         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2975         int err = 0;
2976
2977         mutex_lock(&tegra->sync_lock);
2978
2979         tegra->host_resume_req = true;
2980
2981         if (xhci->shared_hcd == hcd)
2982                 xhci_dbg(xhci, "%s: usb3 root hub\n", __func__);
2983         else if (xhci->main_hcd == hcd)
2984                 xhci_dbg(xhci, "%s: usb2 root hub\n", __func__);
2985
2986         /* pads are disabled only if usb2 root hub in xusb is idle */
2987         /* pads will actually be disabled only when all usb2 ports are idle */
2988         if (xhci->main_hcd == hcd && tegra->usb2_rh_suspend) {
2989                 utmi_phy_pad_enable();
2990                 utmi_phy_iddq_override(false);
2991         }
2992         if (tegra->usb2_rh_suspend && tegra->usb3_rh_suspend) {
2993                 if (tegra->ss_pwr_gated && tegra->host_pwr_gated)
2994                         tegra_xhci_host_partition_elpg_exit(tegra);
2995         }
2996
2997          /* handle remote wakeup before resuming bus */
2998         wait_remote_wakeup_ports(hcd);
2999
3000         err = xhci_bus_resume(hcd);
3001         if (err) {
3002                 xhci_err(xhci, "%s: xhci_bus_resume failed %d\n",
3003                                 __func__, err);
3004                 goto xhci_bus_resume_failed;
3005         }
3006
3007         if (xhci->shared_hcd == hcd)
3008                 tegra->usb3_rh_suspend = false;
3009         else if (xhci->main_hcd == hcd)
3010                 tegra->usb2_rh_suspend = false;
3011
3012         mutex_unlock(&tegra->sync_lock);
3013         return 0;
3014
3015 xhci_bus_resume_failed:
3016         /* TODO: reverse elpg? */
3017         mutex_unlock(&tegra->sync_lock);
3018         return err;
3019 }
3020 #endif
3021
3022 static irqreturn_t tegra_xhci_irq(struct usb_hcd *hcd)
3023 {
3024         struct tegra_xhci_hcd *tegra = hcd_to_tegra_xhci(hcd);
3025         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3026         irqreturn_t iret = IRQ_HANDLED;
3027         u32 status;
3028
3029         spin_lock(&tegra->lock);
3030         if (tegra->hc_in_elpg) {
3031                 spin_lock(&xhci->lock);
3032                 if (HCD_HW_ACCESSIBLE(hcd)) {
3033                         status = xhci_readl(xhci, &xhci->op_regs->status);
3034                         status |= STS_EINT;
3035                         xhci_writel(xhci, status, &xhci->op_regs->status);
3036                 }
3037                 xhci_dbg(xhci, "%s: schedule host_elpg_exit_work\n",
3038                                 __func__);
3039                 schedule_work(&tegra->host_elpg_exit_work);
3040                 spin_unlock(&xhci->lock);
3041         } else
3042                 iret = xhci_irq(hcd);
3043         spin_unlock(&tegra->lock);
3044
3045         wake_up_interruptible(&tegra->log.intr_wait);
3046
3047         return iret;
3048 }
3049
3050
3051 static const struct hc_driver tegra_plat_xhci_driver = {
3052         .description =          "tegra-xhci",
3053         .product_desc =         "Nvidia xHCI Host Controller",
3054         .hcd_priv_size =        sizeof(struct xhci_hcd *),
3055
3056         /*
3057          * generic hardware linkage
3058          */
3059         .irq =                  tegra_xhci_irq,
3060         .flags =                HCD_MEMORY | HCD_USB3 | HCD_SHARED,
3061
3062         /*
3063          * basic lifecycle operations
3064          */
3065         .reset =                xhci_plat_setup,
3066         .start =                xhci_run,
3067         .stop =                 xhci_stop,
3068         .shutdown =             xhci_shutdown,
3069
3070         /*
3071          * managing i/o requests and associated device resources
3072          */
3073         .urb_enqueue =          xhci_urb_enqueue,
3074         .urb_dequeue =          xhci_urb_dequeue,
3075         .alloc_dev =            xhci_alloc_dev,
3076         .free_dev =             xhci_free_dev,
3077         .alloc_streams =        xhci_alloc_streams,
3078         .free_streams =         xhci_free_streams,
3079         .add_endpoint =         xhci_add_endpoint,
3080         .drop_endpoint =        xhci_drop_endpoint,
3081         .endpoint_reset =       xhci_endpoint_reset,
3082         .check_bandwidth =      xhci_check_bandwidth,
3083         .reset_bandwidth =      xhci_reset_bandwidth,
3084         .address_device =       xhci_address_device,
3085         .update_hub_device =    xhci_update_hub_device,
3086         .reset_device =         xhci_discover_or_reset_device,
3087
3088         /*
3089          * scheduling support
3090          */
3091         .get_frame_number =     xhci_get_frame,
3092
3093         /* Root hub support */
3094         .hub_control =          xhci_hub_control,
3095         .hub_status_data =      xhci_hub_status_data,
3096
3097 #ifdef CONFIG_PM
3098         .bus_suspend =          tegra_xhci_bus_suspend,
3099         .bus_resume =           tegra_xhci_bus_resume,
3100 #endif
3101 };
3102
3103 #ifdef CONFIG_PM
3104 static int
3105 tegra_xhci_suspend(struct platform_device *pdev,
3106                                                 pm_message_t state)
3107 {
3108         struct tegra_xhci_hcd *tegra = platform_get_drvdata(pdev);
3109         struct xhci_hcd *xhci = tegra->xhci;
3110
3111         int ret = 0;
3112
3113         mutex_lock(&tegra->sync_lock);
3114         if (!tegra->hc_in_elpg) {
3115                 xhci_warn(xhci, "%s: lp0 suspend entry while elpg not done\n",
3116                                 __func__);
3117                 mutex_unlock(&tegra->sync_lock);
3118                 return -EBUSY;
3119         }
3120         mutex_unlock(&tegra->sync_lock);
3121
3122         tegra_xhci_ss_wake_on_interrupts(tegra->bdata->portmap, false);
3123         tegra_xhci_hs_wake_on_interrupts(tegra->bdata->portmap, false);
3124
3125         /* enable_irq_wake for ss ports */
3126         ret = enable_irq_wake(tegra->padctl_irq);
3127         if (ret < 0) {
3128                 xhci_err(xhci,
3129                 "%s: Couldn't enable USB host mode wakeup, irq=%d, error=%d\n",
3130                 __func__, tegra->padctl_irq, ret);
3131         }
3132
3133         /* enable_irq_wake for hs/fs/ls ports */
3134         ret = enable_irq_wake(tegra->usb3_irq);
3135         if (ret < 0) {
3136                 xhci_err(xhci,
3137                 "%s: Couldn't enable USB host mode wakeup, irq=%d, error=%d\n",
3138                 __func__, tegra->usb3_irq, ret);
3139         }
3140         regulator_disable(tegra->xusb_s1p8v_reg);
3141         regulator_disable(tegra->xusb_s1p05v_reg);
3142         tegra_usb2_clocks_deinit(tegra);
3143
3144         return ret;
3145 }
3146
3147 static int
3148 tegra_xhci_resume(struct platform_device *pdev)
3149 {
3150         struct tegra_xhci_hcd *tegra = platform_get_drvdata(pdev);
3151
3152         dev_dbg(&pdev->dev, "%s\n", __func__);
3153
3154         tegra->last_jiffies = jiffies;
3155
3156         disable_irq_wake(tegra->padctl_irq);
3157         disable_irq_wake(tegra->usb3_irq);
3158         tegra->lp0_exit = true;
3159
3160         regulator_enable(tegra->xusb_s1p05v_reg);
3161         regulator_enable(tegra->xusb_s1p8v_reg);
3162         tegra_usb2_clocks_init(tegra);
3163
3164         return 0;
3165 }
3166 #endif
3167
3168
3169 static int init_bootloader_firmware(struct tegra_xhci_hcd *tegra)
3170 {
3171         struct platform_device *pdev = tegra->pdev;
3172         void __iomem *fw_mmio_base;
3173         phys_addr_t fw_mem_phy_addr;
3174         size_t fw_size;
3175         dma_addr_t fw_dma;
3176 #ifdef CONFIG_PLATFORM_ENABLE_IOMMU
3177         int ret;
3178 #endif
3179
3180         /* bootloader saved firmware memory address in PMC SCRATCH34 register */
3181         fw_mem_phy_addr = tegra_usb_pmc_reg_read(PMC_SCRATCH34);
3182
3183         fw_mmio_base = devm_ioremap_nocache(&pdev->dev,
3184                         fw_mem_phy_addr, sizeof(struct cfgtbl));
3185
3186         if (!fw_mmio_base) {
3187                         dev_err(&pdev->dev, "error mapping fw memory 0x%x\n",
3188                                         fw_mem_phy_addr);
3189                         return -ENOMEM;
3190         }
3191
3192         fw_size = ioread32(fw_mmio_base + FW_SIZE_OFFSET);
3193         devm_iounmap(&pdev->dev, fw_mmio_base);
3194
3195         fw_mmio_base = devm_ioremap_nocache(&pdev->dev,
3196                         fw_mem_phy_addr, fw_size);
3197         if (!fw_mmio_base) {
3198                         dev_err(&pdev->dev, "error mapping fw memory 0x%x\n",
3199                                         fw_mem_phy_addr);
3200                         return -ENOMEM;
3201         }
3202
3203         dev_info(&pdev->dev, "Firmware Memory: phy 0x%x mapped 0x%p (%d Bytes)\n",
3204                         fw_mem_phy_addr, fw_mmio_base, fw_size);
3205
3206 #ifdef CONFIG_PLATFORM_ENABLE_IOMMU
3207         fw_dma = dma_map_linear(&pdev->dev, fw_mem_phy_addr, fw_size,
3208                         DMA_TO_DEVICE);
3209         if (fw_dma == DMA_ERROR_CODE) {
3210                 dev_err(&pdev->dev, "%s: dma_map_linear failed\n",
3211                                 __func__);
3212                 ret = -ENOMEM;
3213                 goto error_iounmap;
3214         }
3215 #else
3216         fw_dma = fw_mem_phy_addr;
3217 #endif
3218         dev_info(&pdev->dev, "Firmware DMA Memory: dma 0x%p (%d Bytes)\n",
3219                         (void *) fw_dma, fw_size);
3220
3221         /* all set and ready to go */
3222         tegra->firmware.data = fw_mmio_base;
3223         tegra->firmware.dma = fw_dma;
3224         tegra->firmware.size = fw_size;
3225
3226         return 0;
3227
3228 #ifdef CONFIG_PLATFORM_ENABLE_IOMMU
3229 error_iounmap:
3230         devm_iounmap(&pdev->dev, fw_mmio_base);
3231         return ret;
3232 #endif
3233 }
3234
3235 static void deinit_bootloader_firmware(struct tegra_xhci_hcd *tegra)
3236 {
3237         struct platform_device *pdev = tegra->pdev;
3238         void __iomem *fw_mmio_base = tegra->firmware.data;
3239
3240 #ifdef CONFIG_PLATFORM_ENABLE_IOMMU
3241         dma_unmap_single(&pdev->dev, tegra->firmware.dma,
3242                         tegra->firmware.size, DMA_TO_DEVICE);
3243 #endif
3244         devm_iounmap(&pdev->dev, fw_mmio_base);
3245
3246         memset(&tegra->firmware, 0, sizeof(tegra->firmware));
3247 }
3248
3249 static int init_firmware(struct tegra_xhci_hcd *tegra)
3250 {
3251         return init_bootloader_firmware(tegra);
3252 }
3253
3254 static void deinit_firmware(struct tegra_xhci_hcd *tegra)
3255 {
3256         deinit_bootloader_firmware(tegra);
3257 }
3258
3259 static struct tegra_xusb_padctl_regs t114_padregs_offset = {
3260         .boot_media_0                   = 0x0,
3261         .usb2_pad_mux_0                 = 0x4,
3262         .usb2_port_cap_0                = 0x8,
3263         .snps_oc_map_0                  = 0xc,
3264         .usb2_oc_map_0                  = 0x10,
3265         .ss_port_map_0                  = 0x14,
3266         .oc_det_0                       = 0x18,
3267         .elpg_program_0                 = 0x1c,
3268         .usb2_bchrg_otgpad0_ctl0_0      = 0x20,
3269         .usb2_bchrg_otgpad0_ctl1_0      = 0xffff,
3270         .usb2_bchrg_otgpad1_ctl0_0      = 0x24,
3271         .usb2_bchrg_otgpad1_ctl1_0      = 0xffff,
3272         .usb2_bchrg_otgpad2_ctl0_0      = 0xffff,
3273         .usb2_bchrg_otgpad2_ctl1_0      = 0xffff,
3274         .usb2_bchrg_bias_pad_0          = 0x28,
3275         .usb2_bchrg_tdcd_dbnc_timer_0   = 0x2c,
3276         .iophy_pll_p0_ctl1_0            = 0x30,
3277         .iophy_pll_p0_ctl2_0            = 0x34,
3278         .iophy_pll_p0_ctl3_0            = 0x38,
3279         .iophy_pll_p0_ctl4_0            = 0x3c,
3280         .iophy_usb3_pad0_ctl1_0         = 0x40,
3281         .iophy_usb3_pad1_ctl1_0         = 0x44,
3282         .iophy_usb3_pad0_ctl2_0         = 0x48,
3283         .iophy_usb3_pad1_ctl2_0         = 0x4c,
3284         .iophy_usb3_pad0_ctl3_0         = 0x50,
3285         .iophy_usb3_pad1_ctl3_0         = 0x54,
3286         .iophy_usb3_pad0_ctl4_0         = 0x58,
3287         .iophy_usb3_pad1_ctl4_0         = 0x5c,
3288         .iophy_misc_pad_p0_ctl1_0       = 0x60,
3289         .iophy_misc_pad_p1_ctl1_0       = 0x64,
3290         .iophy_misc_pad_p0_ctl2_0       = 0x68,
3291         .iophy_misc_pad_p1_ctl2_0       = 0x6c,
3292         .iophy_misc_pad_p0_ctl3_0       = 0x70,
3293         .iophy_misc_pad_p1_ctl3_0       = 0x74,
3294         .iophy_misc_pad_p0_ctl4_0       = 0x78,
3295         .iophy_misc_pad_p1_ctl4_0       = 0x7c,
3296         .iophy_misc_pad_p0_ctl5_0       = 0x80,
3297         .iophy_misc_pad_p1_ctl5_0       = 0x84,
3298         .iophy_misc_pad_p0_ctl6_0       = 0x88,
3299         .iophy_misc_pad_p1_ctl6_0       = 0x8c,
3300         .usb2_otg_pad0_ctl0_0           = 0x90,
3301         .usb2_otg_pad1_ctl0_0           = 0x94,
3302         .usb2_otg_pad2_ctl0_0           = 0xffff,
3303         .usb2_otg_pad0_ctl1_0           = 0x98,
3304         .usb2_otg_pad1_ctl1_0           = 0x9c,
3305         .usb2_otg_pad2_ctl1_0           = 0xffff,
3306         .usb2_bias_pad_ctl0_0           = 0xa0,
3307         .usb2_bias_pad_ctl1_0           = 0xa4,
3308         .usb2_hsic_pad0_ctl0_0          = 0xa8,
3309         .usb2_hsic_pad1_ctl0_0          = 0xac,
3310         .usb2_hsic_pad0_ctl1_0          = 0xb0,
3311         .usb2_hsic_pad1_ctl1_0          = 0xb4,
3312         .usb2_hsic_pad0_ctl2_0          = 0xb8,
3313         .usb2_hsic_pad1_ctl2_0          = 0xbc,
3314         .ulpi_link_trim_ctl0            = 0xc0,
3315         .ulpi_null_clk_trim_ctl0        = 0xc4,
3316         .hsic_strb_trim_ctl0            = 0xc8,
3317         .wake_ctl0                      = 0xcc,
3318         .pm_spare0                      = 0xd0,
3319         .iophy_misc_pad_p2_ctl1_0       = 0xffff,
3320         .iophy_misc_pad_p3_ctl1_0       = 0xffff,
3321         .iophy_misc_pad_p4_ctl1_0       = 0xffff,
3322         .iophy_misc_pad_p2_ctl2_0       = 0xffff,
3323         .iophy_misc_pad_p3_ctl2_0       = 0xffff,
3324         .iophy_misc_pad_p4_ctl2_0       = 0xffff,
3325         .iophy_misc_pad_p2_ctl3_0       = 0xffff,
3326         .iophy_misc_pad_p3_ctl3_0       = 0xffff,
3327         .iophy_misc_pad_p4_ctl3_0       = 0xffff,
3328         .iophy_misc_pad_p2_ctl4_0       = 0xffff,
3329         .iophy_misc_pad_p3_ctl4_0       = 0xffff,
3330         .iophy_misc_pad_p4_ctl4_0       = 0xffff,
3331         .iophy_misc_pad_p2_ctl5_0       = 0xffff,
3332         .iophy_misc_pad_p3_ctl5_0       = 0xffff,
3333         .iophy_misc_pad_p4_ctl5_0       = 0xffff,
3334         .iophy_misc_pad_p2_ctl6_0       = 0xffff,
3335         .iophy_misc_pad_p3_ctl6_0       = 0xffff,
3336         .iophy_misc_pad_p4_ctl6_0       = 0xffff,
3337         .usb3_pad_mux_0                 = 0xffff,
3338         .iophy_pll_s0_ctl1_0            = 0xffff,
3339         .iophy_pll_s0_ctl2_0            = 0xffff,
3340         .iophy_pll_s0_ctl3_0            = 0xffff,
3341         .iophy_pll_s0_ctl4_0            = 0xffff,
3342         .iophy_misc_pad_s0_ctl1_0       = 0xffff,
3343         .iophy_misc_pad_s0_ctl2_0       = 0xffff,
3344         .iophy_misc_pad_s0_ctl3_0       = 0xffff,
3345         .iophy_misc_pad_s0_ctl4_0       = 0xffff,
3346         .iophy_misc_pad_s0_ctl5_0       = 0xffff,
3347         .iophy_misc_pad_s0_ctl6_0       = 0xffff,
3348 };
3349
3350 static struct tegra_xusb_padctl_regs t124_padregs_offset = {
3351         .boot_media_0                   = 0x0,
3352         .usb2_pad_mux_0                 = 0x4,
3353         .usb2_port_cap_0                = 0x8,
3354         .snps_oc_map_0                  = 0xc,
3355         .usb2_oc_map_0                  = 0x10,
3356         .ss_port_map_0                  = 0x14,
3357         .oc_det_0                       = 0x18,
3358         .elpg_program_0                 = 0x1c,
3359         .usb2_bchrg_otgpad0_ctl0_0      = 0x20,
3360         .usb2_bchrg_otgpad0_ctl1_0      = 0x24,
3361         .usb2_bchrg_otgpad1_ctl0_0      = 0x28,
3362         .usb2_bchrg_otgpad1_ctl1_0      = 0x2c,
3363         .usb2_bchrg_otgpad2_ctl0_0      = 0x30,
3364         .usb2_bchrg_otgpad2_ctl1_0      = 0x34,
3365         .usb2_bchrg_bias_pad_0          = 0x38,
3366         .usb2_bchrg_tdcd_dbnc_timer_0   = 0x3c,
3367         .iophy_pll_p0_ctl1_0            = 0x40,
3368         .iophy_pll_p0_ctl2_0            = 0x44,
3369         .iophy_pll_p0_ctl3_0            = 0x48,
3370         .iophy_pll_p0_ctl4_0            = 0x4c,
3371         .iophy_usb3_pad0_ctl1_0         = 0x50,
3372         .iophy_usb3_pad1_ctl1_0         = 0x54,
3373         .iophy_usb3_pad0_ctl2_0         = 0x58,
3374         .iophy_usb3_pad1_ctl2_0         = 0x5c,
3375         .iophy_usb3_pad0_ctl3_0         = 0x60,
3376         .iophy_usb3_pad1_ctl3_0         = 0x64,
3377         .iophy_usb3_pad0_ctl4_0         = 0x68,
3378         .iophy_usb3_pad1_ctl4_0         = 0x6c,
3379         .iophy_misc_pad_p0_ctl1_0       = 0x70,
3380         .iophy_misc_pad_p1_ctl1_0       = 0x74,
3381         .iophy_misc_pad_p0_ctl2_0       = 0x78,
3382         .iophy_misc_pad_p1_ctl2_0       = 0x7c,
3383         .iophy_misc_pad_p0_ctl3_0       = 0x80,
3384         .iophy_misc_pad_p1_ctl3_0       = 0x84,
3385         .iophy_misc_pad_p0_ctl4_0       = 0x88,
3386         .iophy_misc_pad_p1_ctl4_0       = 0x8c,
3387         .iophy_misc_pad_p0_ctl5_0       = 0x90,
3388         .iophy_misc_pad_p1_ctl5_0       = 0x94,
3389         .iophy_misc_pad_p0_ctl6_0       = 0x98,
3390         .iophy_misc_pad_p1_ctl6_0       = 0x9c,
3391         .usb2_otg_pad0_ctl0_0           = 0xa0,
3392         .usb2_otg_pad1_ctl0_0           = 0xa4,
3393         .usb2_otg_pad2_ctl0_0           = 0xa8,
3394         .usb2_otg_pad0_ctl1_0           = 0xac,
3395         .usb2_otg_pad1_ctl1_0           = 0xb0,
3396         .usb2_otg_pad2_ctl1_0           = 0xb4,
3397         .usb2_bias_pad_ctl0_0           = 0xb8,
3398         .usb2_bias_pad_ctl1_0           = 0xbc,
3399         .usb2_hsic_pad0_ctl0_0          = 0xc0,
3400         .usb2_hsic_pad1_ctl0_0          = 0xc4,
3401         .usb2_hsic_pad0_ctl1_0          = 0xc8,
3402         .usb2_hsic_pad1_ctl1_0          = 0xcc,
3403         .usb2_hsic_pad0_ctl2_0          = 0xd0,
3404         .usb2_hsic_pad1_ctl2_0          = 0xd4,
3405         .ulpi_link_trim_ctl0            = 0xd8,
3406         .ulpi_null_clk_trim_ctl0        = 0xdc,
3407         .hsic_strb_trim_ctl0            = 0xe0,
3408         .wake_ctl0                      = 0xe4,
3409         .pm_spare0                      = 0xe8,
3410         .iophy_misc_pad_p2_ctl1_0       = 0xec,
3411         .iophy_misc_pad_p3_ctl1_0       = 0xf0,
3412         .iophy_misc_pad_p4_ctl1_0       = 0xf4,
3413         .iophy_misc_pad_p2_ctl2_0       = 0xf8,
3414         .iophy_misc_pad_p3_ctl2_0       = 0xfc,
3415         .iophy_misc_pad_p4_ctl2_0       = 0x100,
3416         .iophy_misc_pad_p2_ctl3_0       = 0x104,
3417         .iophy_misc_pad_p3_ctl3_0       = 0x108,
3418         .iophy_misc_pad_p4_ctl3_0       = 0x10c,
3419         .iophy_misc_pad_p2_ctl4_0       = 0x110,
3420         .iophy_misc_pad_p3_ctl4_0       = 0x114,
3421         .iophy_misc_pad_p4_ctl4_0       = 0x118,
3422         .iophy_misc_pad_p2_ctl5_0       = 0x11c,
3423         .iophy_misc_pad_p3_ctl5_0       = 0x120,
3424         .iophy_misc_pad_p4_ctl5_0       = 0x124,
3425         .iophy_misc_pad_p2_ctl6_0       = 0x128,
3426         .iophy_misc_pad_p3_ctl6_0       = 0x12c,
3427         .iophy_misc_pad_p4_ctl6_0       = 0x130,
3428         .usb3_pad_mux_0                 = 0x134,
3429         .iophy_pll_s0_ctl1_0            = 0x138,
3430         .iophy_pll_s0_ctl2_0            = 0x13c,
3431         .iophy_pll_s0_ctl3_0            = 0x140,
3432         .iophy_pll_s0_ctl4_0            = 0x144,
3433         .iophy_misc_pad_s0_ctl1_0       = 0x148,
3434         .iophy_misc_pad_s0_ctl2_0       = 0x14c,
3435         .iophy_misc_pad_s0_ctl3_0       = 0x150,
3436         .iophy_misc_pad_s0_ctl4_0       = 0x154,
3437         .iophy_misc_pad_s0_ctl5_0       = 0x158,
3438         .iophy_misc_pad_s0_ctl6_0       = 0x15c,
3439 };
3440
3441 /* TODO: we have to refine error handling in tegra_xhci_probe() */
3442 static int tegra_xhci_probe(struct platform_device *pdev)
3443 {
3444         const struct hc_driver *driver;
3445         struct xhci_hcd *xhci;
3446         struct tegra_xhci_hcd *tegra;
3447         struct resource *res;
3448         struct usb_hcd  *hcd;
3449         u32 pmc_reg, val;
3450         int ret;
3451         int irq;
3452
3453         BUILD_BUG_ON(sizeof(struct cfgtbl) != 256);
3454
3455         if (usb_disabled())
3456                 return -ENODEV;
3457
3458         tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
3459         if (!tegra) {
3460                 dev_err(&pdev->dev, "memory alloc failed\n");
3461                 return -ENOMEM;
3462         }
3463         tegra->pdev = pdev;
3464         tegra->pdata = dev_get_platdata(&pdev->dev);
3465         tegra->bdata = tegra->pdata->bdata;
3466
3467         ret = tegra_xhci_request_mem_region(pdev, "padctl",
3468                         &tegra->padctl_base);
3469         if (ret) {
3470                 dev_err(&pdev->dev, "failed to map padctl\n");
3471                 return ret;
3472         }
3473
3474         ret = tegra_xhci_request_mem_region(pdev, "fpci", &tegra->fpci_base);
3475         if (ret) {
3476                 dev_err(&pdev->dev, "failed to map fpci\n");
3477                 return ret;
3478         }
3479
3480         ret = tegra_xhci_request_mem_region(pdev, "ipfs", &tegra->ipfs_base);
3481         if (ret) {
3482                 dev_err(&pdev->dev, "failed to map ipfs\n");
3483                 return ret;
3484         }
3485
3486         ret = tegra_xusb_partitions_clk_init(tegra);
3487         if (ret) {
3488                 dev_err(&pdev->dev,
3489                         "failed to initialize xusb partitions clocks\n");
3490                 return ret;
3491         }
3492
3493         /* Enable power rails to the PAD,VBUS
3494          * and pull-up voltage.Initialize the regulators
3495          */
3496         ret = tegra_xusb_regulator_init(tegra, pdev);
3497         if (ret) {
3498                 dev_err(&pdev->dev, "failed to initialize xusb regulator\n");
3499                 goto err_deinit_xusb_partition_clk;
3500         }
3501
3502         /* Enable UTMIP, PLLU and PLLE */
3503         ret = tegra_usb2_clocks_init(tegra);
3504         if (ret) {
3505                 dev_err(&pdev->dev, "error initializing usb2 clocks\n");
3506                 goto err_deinit_tegra_xusb_regulator;
3507         }
3508
3509         /* tegra_unpowergate_partition also does partition reset deassert */
3510         ret = tegra_unpowergate_partition(TEGRA_POWERGATE_XUSBA);
3511         if (ret)
3512                 dev_err(&pdev->dev, "could not unpowergate xusba partition\n");
3513
3514         /* tegra_unpowergate_partition also does partition reset deassert */
3515         ret = tegra_unpowergate_partition(TEGRA_POWERGATE_XUSBC);
3516         if (ret)
3517                 dev_err(&pdev->dev, "could not unpowergate xusbc partition\n");
3518
3519         /* reset the pointer back to NULL. driver uses it */
3520         /* platform_set_drvdata(pdev, NULL); */
3521
3522         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "host");
3523         if (!res) {
3524                 dev_err(&pdev->dev, "mem resource host doesn't exist\n");
3525                 ret = -ENODEV;
3526                 goto err_deinit_usb2_clocks;
3527         }
3528         tegra->host_phy_base = res->start;
3529
3530         tegra->host_phy_virt_base = devm_ioremap(&pdev->dev,
3531                                 res->start, resource_size(res));
3532         if (!tegra->host_phy_virt_base) {
3533                 dev_err(&pdev->dev, "error mapping host phy memory\n");
3534                 ret = -ENOMEM;
3535                 goto err_deinit_usb2_clocks;
3536         }
3537
3538         /* Setup IPFS access and BAR0 space */
3539         tegra_xhci_cfg(tegra);
3540
3541         val = readl(tegra->fpci_base + XUSB_CFG_0);
3542         tegra->device_id = (val >> 16) & 0xffff;
3543
3544         dev_info(&pdev->dev, "XUSB device id = 0x%x (%s)\n", tegra->device_id,
3545                 (XUSB_DEVICE_ID_T114 == tegra->device_id) ? "T114" : "T124+");
3546
3547         if (XUSB_DEVICE_ID_T114 == tegra->device_id) {
3548                 tegra->padregs = &t114_padregs_offset;
3549         } else if (XUSB_DEVICE_ID_T124 == tegra->device_id) {
3550                 tegra->padregs = &t124_padregs_offset;
3551         } else {
3552                 dev_info(&pdev->dev, "XUSB device_id neither T114 nor T124!\n");
3553                 dev_info(&pdev->dev, "XUSB using T124 pad register offsets!\n");
3554                 tegra->padregs = &t124_padregs_offset;
3555         }
3556
3557         /* calculate rctrl_val and tctrl_val once at boot time */
3558         /* Issue is only applicable for T114 */
3559         if (XUSB_DEVICE_ID_T114 == tegra->device_id)
3560                 tegra_xhci_war_for_tctrl_rctrl(tegra);
3561
3562         /* Program the XUSB pads to take ownership of ports */
3563         tegra_xhci_padctl_portmap_and_caps(tegra);
3564
3565         /* Release XUSB wake logic state latching */
3566         tegra_xhci_ss_wake_signal(tegra->bdata->portmap, false);
3567         tegra_xhci_ss_vcore(tegra->bdata->portmap, false);
3568
3569         /* Deassert reset to XUSB host, ss, dev clocks */
3570         tegra_periph_reset_deassert(tegra->host_clk);
3571         tegra_periph_reset_deassert(tegra->ss_clk);
3572
3573         fw_log_init(tegra);
3574         ret = init_firmware(tegra);
3575         if (ret < 0) {
3576                 dev_err(&pdev->dev, "failed to init firmware\n");
3577                 ret = -ENODEV;
3578                 goto err_deinit_usb2_clocks;
3579         }
3580
3581         ret = load_firmware(tegra, true /* do reset ARU */);
3582         if (ret < 0) {
3583                 dev_err(&pdev->dev, "failed to load firmware\n");
3584                 ret = -ENODEV;
3585                 goto err_deinit_firmware;
3586         }
3587
3588         device_init_wakeup(&pdev->dev, 1);
3589         driver = &tegra_plat_xhci_driver;
3590
3591         hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
3592         if (!hcd) {
3593                 dev_err(&pdev->dev, "failed to create usb2 hcd\n");
3594                 ret = -ENOMEM;
3595                 goto err_deinit_firmware;
3596         }
3597
3598         ret = tegra_xhci_request_mem_region(pdev, "host", &hcd->regs);
3599         if (ret) {
3600                 dev_err(&pdev->dev, "failed to map host\n");
3601                 goto err_put_usb2_hcd;
3602         }
3603         hcd->rsrc_start = res->start;
3604         hcd->rsrc_len = resource_size(res);
3605
3606         res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "host");
3607         if (!res) {
3608                 dev_err(&pdev->dev, "irq resource host doesn't exist\n");
3609                 ret = -ENODEV;
3610                 goto err_put_usb2_hcd;
3611         }
3612         irq = res->start;
3613         ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
3614         if (ret) {
3615                 dev_err(&pdev->dev, "failed to add usb2hcd, error = %d\n", ret);
3616                 goto err_put_usb2_hcd;
3617         }
3618
3619         /* USB 2.0 roothub is stored in the platform_device now. */
3620         hcd = dev_get_drvdata(&pdev->dev);
3621         xhci = hcd_to_xhci(hcd);
3622         tegra->xhci = xhci;
3623         platform_set_drvdata(pdev, tegra);
3624
3625         xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
3626                                                 dev_name(&pdev->dev), hcd);
3627         if (!xhci->shared_hcd) {
3628                 dev_err(&pdev->dev, "failed to create usb3 hcd\n");
3629                 ret = -ENOMEM;
3630                 goto err_remove_usb2_hcd;
3631         }
3632
3633         /*
3634          * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
3635          * is called by usb_add_hcd().
3636          */
3637         *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
3638
3639         ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
3640         if (ret) {
3641                 dev_err(&pdev->dev, "failed to add usb3hcd, error = %d\n", ret);
3642                 goto err_put_usb3_hcd;
3643         }
3644
3645         device_init_wakeup(&hcd->self.root_hub->dev, 1);
3646         device_init_wakeup(&xhci->shared_hcd->self.root_hub->dev, 1);
3647         spin_lock_init(&tegra->lock);
3648         mutex_init(&tegra->sync_lock);
3649         mutex_init(&tegra->mbox_lock);
3650
3651         /* do mailbox related initializations */
3652         tegra->mbox_owner = 0xffff;
3653         INIT_WORK(&tegra->mbox_work, tegra_xhci_process_mbox_message);
3654
3655         tegra_xhci_enable_fw_message(tegra);
3656
3657         /* do ss partition elpg exit related initialization */
3658         INIT_WORK(&tegra->ss_elpg_exit_work, ss_partition_elpg_exit_work);
3659
3660         /* do host partition elpg exit related initialization */
3661         INIT_WORK(&tegra->host_elpg_exit_work, host_partition_elpg_exit_work);
3662
3663         /* Register interrupt handler for SMI line to handle mailbox
3664          * interrupt from firmware
3665          */
3666         ret = tegra_xhci_request_irq(pdev, "host-smi", tegra_xhci_smi_irq,
3667                         IRQF_SHARED, "tegra_xhci_mbox_irq", &tegra->smi_irq);
3668         if (ret != 0)
3669                 goto err_remove_usb3_hcd;
3670
3671         /* Register interrupt handler for PADCTRL line to
3672          * handle wake on connect irqs interrupt from
3673          * firmware
3674          */
3675         ret = tegra_xhci_request_irq(pdev, "padctl", tegra_xhci_padctl_irq,
3676                         IRQF_SHARED | IRQF_TRIGGER_HIGH,
3677                         "tegra_xhci_padctl_irq", &tegra->padctl_irq);
3678         if (ret != 0)
3679                 goto err_remove_usb3_hcd;
3680
3681         ret = tegra_xhci_request_irq(pdev, "usb3", tegra_xhci_xusb_host_irq,
3682                         IRQF_SHARED | IRQF_TRIGGER_HIGH, "xusb_host_irq",
3683                         &tegra->usb3_irq);
3684         if (ret != 0)
3685                 goto err_remove_usb3_hcd;
3686
3687         tegra->ss_pwr_gated = false;
3688         tegra->host_pwr_gated = false;
3689         tegra->hc_in_elpg = false;
3690         tegra->hs_wake_event = false;
3691         tegra->host_resume_req = false;
3692         tegra->lp0_exit = false;
3693         tegra->dfe_ctle_ctx_saved = false;
3694
3695         /* reset wake event to NONE */
3696         pmc_reg = tegra_usb_pmc_reg_read(PMC_UTMIP_UHSIC_SLEEP_CFG_0);
3697         pmc_reg |= UTMIP_WAKE_VAL(0, WAKE_VAL_NONE);
3698         pmc_reg |= UTMIP_WAKE_VAL(1, WAKE_VAL_NONE);
3699         pmc_reg |= UTMIP_WAKE_VAL(2, WAKE_VAL_NONE);
3700         pmc_reg |= UTMIP_WAKE_VAL(3, WAKE_VAL_NONE);
3701         tegra_usb_pmc_reg_write(PMC_UTMIP_UHSIC_SLEEP_CFG_0, pmc_reg);
3702
3703         tegra_xhci_debug_read_pads(tegra);
3704         utmi_phy_pad_enable();
3705         utmi_phy_iddq_override(false);
3706
3707         tegra_pd_add_device(&pdev->dev);
3708
3709         return 0;
3710
3711 err_remove_usb3_hcd:
3712         usb_remove_hcd(xhci->shared_hcd);
3713 err_put_usb3_hcd:
3714         usb_put_hcd(xhci->shared_hcd);
3715 err_remove_usb2_hcd:
3716         kfree(tegra->xhci);
3717         usb_remove_hcd(hcd);
3718 err_put_usb2_hcd:
3719         usb_put_hcd(hcd);
3720 err_deinit_firmware:
3721         deinit_firmware(tegra);
3722 err_deinit_usb2_clocks:
3723         tegra_usb2_clocks_deinit(tegra);
3724 err_deinit_tegra_xusb_regulator:
3725         tegra_xusb_regulator_deinit(tegra);
3726 err_deinit_xusb_partition_clk:
3727         tegra_xusb_partitions_clk_deinit(tegra);
3728
3729         return ret;
3730 }
3731
3732 static int tegra_xhci_remove(struct platform_device *pdev)
3733 {
3734         struct tegra_xhci_hcd *tegra = platform_get_drvdata(pdev);
3735         struct xhci_hcd *xhci = NULL;
3736         struct usb_hcd *hcd = NULL;
3737
3738         if (tegra == NULL)
3739                 return -EINVAL;
3740
3741         xhci = tegra->xhci;
3742         hcd = xhci_to_hcd(xhci);
3743
3744         devm_free_irq(&pdev->dev, tegra->usb3_irq, tegra);
3745         devm_free_irq(&pdev->dev, tegra->padctl_irq, tegra);
3746         devm_free_irq(&pdev->dev, tegra->smi_irq, tegra);
3747         usb_remove_hcd(xhci->shared_hcd);
3748         usb_put_hcd(xhci->shared_hcd);
3749         usb_remove_hcd(hcd);
3750         usb_put_hcd(hcd);
3751         kfree(xhci);
3752
3753         deinit_firmware(tegra);
3754         fw_log_deinit(tegra);
3755         tegra_xusb_regulator_deinit(tegra);
3756         tegra_usb2_clocks_deinit(tegra);
3757         if (!tegra->hc_in_elpg)
3758                 tegra_xusb_partitions_clk_deinit(tegra);
3759         utmi_phy_pad_disable();
3760         utmi_phy_iddq_override(true);
3761
3762         tegra_pd_remove_device(&pdev->dev);
3763         return 0;
3764 }
3765
3766 static void tegra_xhci_shutdown(struct platform_device *pdev)
3767 {
3768         struct tegra_xhci_hcd *tegra = platform_get_drvdata(pdev);
3769         struct xhci_hcd *xhci = NULL;
3770         struct usb_hcd *hcd = NULL;
3771
3772         if (tegra == NULL)
3773                 return;
3774
3775         if (tegra->hc_in_elpg) {
3776                 mutex_lock(&tegra->sync_lock);
3777                 pmc_init(tegra, 0);
3778                 mutex_unlock(&tegra->sync_lock);
3779         } else {
3780                 xhci = tegra->xhci;
3781                 hcd = xhci_to_hcd(xhci);
3782                 xhci_shutdown(hcd);
3783         }
3784 }
3785
3786 static struct platform_driver tegra_xhci_driver = {
3787         .probe  = tegra_xhci_probe,
3788         .remove = tegra_xhci_remove,
3789         .shutdown = tegra_xhci_shutdown,
3790 #ifdef CONFIG_PM
3791         .suspend = tegra_xhci_suspend,
3792         .resume  = tegra_xhci_resume,
3793 #endif
3794         .driver = {
3795                 .name = "tegra-xhci",
3796         },
3797 };
3798 MODULE_ALIAS("platform:tegra-xhci");
3799
3800 int tegra_xhci_register_plat(void)
3801 {
3802         return platform_driver_register(&tegra_xhci_driver);
3803 }
3804
3805 void tegra_xhci_unregister_plat(void)
3806 {
3807         platform_driver_unregister(&tegra_xhci_driver);
3808 }