blob: 34a4b00dda56ded00b9fd52bd2f047e91984a461 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sylvain Lemieux890cc772015-08-13 15:40:22 -04002/*
3 * Copyright (C) 2008 by NXP Semiconductors
4 * @Author: Based on code by Kevin Wells
5 * @Descr: USB driver - Embedded Artists LPC3250 OEM Board support functions
6 *
7 * Copyright (c) 2015 Tyco Fire Protection Products.
Sylvain Lemieux890cc772015-08-13 15:40:22 -04008 */
9
10#include <common.h>
Liam Beguinf24aa672017-05-17 13:01:15 -040011#include <dm.h>
Sylvain Lemieux890cc772015-08-13 15:40:22 -040012#include <errno.h>
Mateusz Kulikowskiaf70d022016-01-23 11:54:31 +010013#include <wait_bit.h>
Sylvain Lemieux890cc772015-08-13 15:40:22 -040014#include <asm/io.h>
15#include <asm/arch/cpu.h>
16#include <asm/arch/clk.h>
Liam Beguinf24aa672017-05-17 13:01:15 -040017#include <asm/arch/i2c.h>
Sylvain Lemieux890cc772015-08-13 15:40:22 -040018#include <usb.h>
19#include <i2c.h>
20
21/* OTG I2C controller module register structures */
22struct otgi2c_regs {
23 u32 otg_i2c_txrx; /* OTG I2C Tx/Rx Data FIFO */
24 u32 otg_i2c_stat; /* OTG I2C Status Register */
25 u32 otg_i2c_ctrl; /* OTG I2C Control Register */
26 u32 otg_i2c_clk_hi; /* OTG I2C Clock Divider high */
27 u32 otg_i2c_clk_lo; /* OTG I2C Clock Divider low */
28};
29
30/* OTG controller module register structures */
31struct otg_regs {
32 u32 reserved1[64];
33 u32 otg_int_sts; /* OTG int status register */
34 u32 otg_int_enab; /* OTG int enable register */
35 u32 otg_int_set; /* OTG int set register */
36 u32 otg_int_clr; /* OTG int clear register */
37 u32 otg_sts_ctrl; /* OTG status/control register */
38 u32 otg_timer; /* OTG timer register */
39 u32 reserved2[122];
40 struct otgi2c_regs otg_i2c;
41 u32 reserved3[824];
42 u32 otg_clk_ctrl; /* OTG clock control reg */
43 u32 otg_clk_sts; /* OTG clock status reg */
44};
45
46/* otg_sts_ctrl register definitions */
47#define OTG_HOST_EN (1 << 0) /* Enable host mode */
48
49/* otg_clk_ctrl and otg_clk_sts register definitions */
50#define OTG_CLK_AHB_EN (1 << 4) /* Enable AHB clock */
51#define OTG_CLK_OTG_EN (1 << 3) /* Enable OTG clock */
52#define OTG_CLK_I2C_EN (1 << 2) /* Enable I2C clock */
53#define OTG_CLK_HOST_EN (1 << 0) /* Enable host clock */
54
55/* ISP1301 USB transceiver I2C registers */
56#define MC1_SPEED_REG (1 << 0)
57#define MC1_DAT_SE0 (1 << 2)
58#define MC1_UART_EN (1 << 6)
59
60#define MC2_SPD_SUSP_CTRL (1 << 1)
61#define MC2_BI_DI (1 << 2)
62#define MC2_PSW_EN (1 << 6)
63
64#define OTG1_DP_PULLUP (1 << 0)
65#define OTG1_DM_PULLUP (1 << 1)
66#define OTG1_DP_PULLDOWN (1 << 2)
67#define OTG1_DM_PULLDOWN (1 << 3)
68#define OTG1_VBUS_DRV (1 << 5)
69
70#define ISP1301_I2C_ADDR CONFIG_USB_ISP1301_I2C_ADDR
71
72#define ISP1301_I2C_MODE_CONTROL_1_SET 0x04
73#define ISP1301_I2C_MODE_CONTROL_1_CLR 0x05
74#define ISP1301_I2C_MODE_CONTROL_2_SET 0x12
75#define ISP1301_I2C_MODE_CONTROL_2_CLR 0x13
76#define ISP1301_I2C_OTG_CONTROL_1_SET 0x06
77#define ISP1301_I2C_OTG_CONTROL_1_CLR 0x07
78#define ISP1301_I2C_INTERRUPT_LATCH_CLR 0x0B
79#define ISP1301_I2C_INTERRUPT_FALLING_CLR 0x0D
80#define ISP1301_I2C_INTERRUPT_RISING_CLR 0x0F
81
82static struct otg_regs *otg = (struct otg_regs *)USB_BASE;
83static struct clk_pm_regs *clk_pwr = (struct clk_pm_regs *)CLK_PM_BASE;
84
Liam Beguinf24aa672017-05-17 13:01:15 -040085static int isp1301_set_value(struct udevice *dev, int reg, u8 value)
Sylvain Lemieux890cc772015-08-13 15:40:22 -040086{
Liam Beguinf24aa672017-05-17 13:01:15 -040087#ifndef CONFIG_DM_I2C
Sylvain Lemieux890cc772015-08-13 15:40:22 -040088 return i2c_write(ISP1301_I2C_ADDR, reg, 1, &value, 1);
Liam Beguinf24aa672017-05-17 13:01:15 -040089#else
90 return dm_i2c_write(dev, reg, &value, 1);
91#endif
Sylvain Lemieux890cc772015-08-13 15:40:22 -040092}
93
Liam Beguinf24aa672017-05-17 13:01:15 -040094static void isp1301_configure(struct udevice *dev)
Sylvain Lemieux890cc772015-08-13 15:40:22 -040095{
Liam Beguinf24aa672017-05-17 13:01:15 -040096#ifndef CONFIG_DM_I2C
Sylvain Lemieux890cc772015-08-13 15:40:22 -040097 i2c_set_bus_num(I2C_2);
Liam Beguinf24aa672017-05-17 13:01:15 -040098#endif
Sylvain Lemieux890cc772015-08-13 15:40:22 -040099
100 /*
101 * LPC32XX only supports DAT_SE0 USB mode
102 * This sequence is important
103 */
104
105 /* Disable transparent UART mode first */
Liam Beguinf24aa672017-05-17 13:01:15 -0400106 isp1301_set_value(dev, ISP1301_I2C_MODE_CONTROL_1_CLR, MC1_UART_EN);
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400107
Liam Beguinf24aa672017-05-17 13:01:15 -0400108 isp1301_set_value(dev, ISP1301_I2C_MODE_CONTROL_1_CLR, ~MC1_SPEED_REG);
109 isp1301_set_value(dev, ISP1301_I2C_MODE_CONTROL_1_SET, MC1_SPEED_REG);
110 isp1301_set_value(dev, ISP1301_I2C_MODE_CONTROL_2_CLR, ~0);
111 isp1301_set_value(dev, ISP1301_I2C_MODE_CONTROL_2_SET,
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400112 MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL);
113
Liam Beguinf24aa672017-05-17 13:01:15 -0400114 isp1301_set_value(dev, ISP1301_I2C_OTG_CONTROL_1_CLR, ~0);
115 isp1301_set_value(dev, ISP1301_I2C_MODE_CONTROL_1_SET, MC1_DAT_SE0);
116 isp1301_set_value(dev, ISP1301_I2C_OTG_CONTROL_1_SET,
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400117 OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
Liam Beguinf24aa672017-05-17 13:01:15 -0400118 isp1301_set_value(dev, ISP1301_I2C_OTG_CONTROL_1_CLR,
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400119 OTG1_DM_PULLUP | OTG1_DP_PULLUP);
Liam Beguinf24aa672017-05-17 13:01:15 -0400120 isp1301_set_value(dev, ISP1301_I2C_INTERRUPT_LATCH_CLR, ~0);
121 isp1301_set_value(dev, ISP1301_I2C_INTERRUPT_FALLING_CLR, ~0);
122 isp1301_set_value(dev, ISP1301_I2C_INTERRUPT_RISING_CLR, ~0);
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400123
124 /* Enable usb_need_clk clock after transceiver is initialized */
125 setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_USBDVND_EN);
126}
127
128static int usbpll_setup(void)
129{
130 u32 ret;
131
132 /* make sure clocks are disabled */
133 clrbits_le32(&clk_pwr->usb_ctrl,
134 CLK_USBCTRL_CLK_EN1 | CLK_USBCTRL_CLK_EN2);
135
136 /* start PLL clock input */
137 setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_CLK_EN1);
138
139 /* Setup PLL. */
140 setbits_le32(&clk_pwr->usb_ctrl,
141 CLK_USBCTRL_FDBK_PLUS1(192 - 1));
142 setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_POSTDIV_2POW(0x01));
143 setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_PLL_PWRUP);
144
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100145 ret = wait_for_bit_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_PLL_STS,
146 true, CONFIG_SYS_HZ, false);
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400147 if (ret)
148 return ret;
149
150 /* enable PLL output */
151 setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_CLK_EN2);
152
153 return 0;
154}
155
156int usb_cpu_init(void)
157{
158 u32 ret;
Liam Beguinf24aa672017-05-17 13:01:15 -0400159 struct udevice *dev = NULL;
160
161#ifdef CONFIG_DM_I2C
162 ret = i2c_get_chip_for_busnum(I2C_2, ISP1301_I2C_ADDR, 1, &dev);
163 if (ret) {
164 debug("%s: No bus %d\n", __func__, I2C_2);
165 return ret;
166 }
167#endif
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400168
169 /*
170 * USB pins routing setup is done by "lpc32xx_usb_init()" and should
171 * be call by board "board_init()" or "misc_init_r()" functions.
172 */
173
174 /* enable AHB slave USB clock */
175 setbits_le32(&clk_pwr->usb_ctrl,
176 CLK_USBCTRL_HCLK_EN | CLK_USBCTRL_BUS_KEEPER);
177
178 /* enable I2C clock */
179 writel(OTG_CLK_I2C_EN, &otg->otg_clk_ctrl);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100180 ret = wait_for_bit_le32(&otg->otg_clk_sts, OTG_CLK_I2C_EN, true,
181 CONFIG_SYS_HZ, false);
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400182 if (ret)
183 return ret;
184
185 /* Configure ISP1301 */
Liam Beguinf24aa672017-05-17 13:01:15 -0400186 isp1301_configure(dev);
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400187
188 /* setup USB clocks and PLL */
189 ret = usbpll_setup();
190 if (ret)
191 return ret;
192
193 /* enable usb_host_need_clk */
194 setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_USBHSTND_EN);
195
196 /* enable all needed USB clocks */
197 const u32 mask = OTG_CLK_AHB_EN | OTG_CLK_OTG_EN |
198 OTG_CLK_I2C_EN | OTG_CLK_HOST_EN;
199 writel(mask, &otg->otg_clk_ctrl);
200
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100201 ret = wait_for_bit_le32(&otg->otg_clk_sts, mask, true,
202 CONFIG_SYS_HZ, false);
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400203 if (ret)
204 return ret;
205
206 setbits_le32(&otg->otg_sts_ctrl, OTG_HOST_EN);
Liam Beguinf24aa672017-05-17 13:01:15 -0400207 isp1301_set_value(dev, ISP1301_I2C_OTG_CONTROL_1_SET, OTG1_VBUS_DRV);
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400208
209 return 0;
210}
211
212int usb_cpu_stop(void)
213{
Liam Beguinf24aa672017-05-17 13:01:15 -0400214 struct udevice *dev = NULL;
215 int ret = 0;
216
217#ifdef CONFIG_DM_I2C
218 ret = i2c_get_chip_for_busnum(I2C_2, ISP1301_I2C_ADDR, 1, &dev);
219 if (ret) {
220 debug("%s: No bus %d\n", __func__, I2C_2);
221 return ret;
222 }
223#endif
224
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400225 /* vbus off */
Liam Beguinf24aa672017-05-17 13:01:15 -0400226 isp1301_set_value(dev, ISP1301_I2C_OTG_CONTROL_1_SET, OTG1_VBUS_DRV);
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400227
228 clrbits_le32(&otg->otg_sts_ctrl, OTG_HOST_EN);
229
230 clrbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_HCLK_EN);
231
Liam Beguinf24aa672017-05-17 13:01:15 -0400232 return ret;
Sylvain Lemieux890cc772015-08-13 15:40:22 -0400233}
234
235int usb_cpu_init_fail(void)
236{
237 return usb_cpu_stop();
238}