1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/delay.h>
14#include <linux/gpio/consumer.h>
15#include <linux/io.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/usb.h>
20
21#include "isp1760-core.h"
22#include "isp1760-hcd.h"
23#include "isp1760-regs.h"
24#include "isp1760-udc.h"
25
26static void isp1760_init_core(struct isp1760_device *isp)
27{
28 u32 otgctrl;
29 u32 hwmode;
30
31
32 if (isp->rst_gpio) {
33 gpiod_set_value_cansleep(isp->rst_gpio, 1);
34 msleep(50);
35 gpiod_set_value_cansleep(isp->rst_gpio, 0);
36 }
37
38
39
40
41
42 isp1760_write32(isp->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
43 msleep(100);
44
45
46 hwmode = HW_DATA_BUS_32BIT;
47
48 if (isp->devflags & ISP1760_FLAG_BUS_WIDTH_16)
49 hwmode &= ~HW_DATA_BUS_32BIT;
50 if (isp->devflags & ISP1760_FLAG_ANALOG_OC)
51 hwmode |= HW_ANA_DIGI_OC;
52 if (isp->devflags & ISP1760_FLAG_DACK_POL_HIGH)
53 hwmode |= HW_DACK_POL_HIGH;
54 if (isp->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
55 hwmode |= HW_DREQ_POL_HIGH;
56 if (isp->devflags & ISP1760_FLAG_INTR_POL_HIGH)
57 hwmode |= HW_INTR_HIGH_ACT;
58 if (isp->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
59 hwmode |= HW_INTR_EDGE_TRIG;
60
61
62
63
64
65
66
67 if (isp->devflags & ISP1760_FLAG_ISP1761) {
68 isp1760_write32(isp->regs, DC_MODE, 0);
69 hwmode |= HW_COMN_IRQ;
70 }
71
72
73
74
75
76
77 isp1760_write32(isp->regs, HC_HW_MODE_CTRL, hwmode);
78 isp1760_write32(isp->regs, HC_HW_MODE_CTRL, hwmode);
79
80
81
82
83
84
85
86
87 if ((isp->devflags & ISP1760_FLAG_ISP1761) &&
88 (isp->devflags & ISP1760_FLAG_OTG_EN))
89 otgctrl = ((HW_DM_PULLDOWN | HW_DP_PULLDOWN) << 16)
90 | HW_OTG_DISABLE;
91 else
92 otgctrl = (HW_SW_SEL_HC_DC << 16)
93 | (HW_VBUS_DRV | HW_SEL_CP_EXT);
94
95 isp1760_write32(isp->regs, HC_PORT1_CTRL, otgctrl);
96
97 dev_info(isp->dev, "bus width: %u, oc: %s\n",
98 isp->devflags & ISP1760_FLAG_BUS_WIDTH_16 ? 16 : 32,
99 isp->devflags & ISP1760_FLAG_ANALOG_OC ? "analog" : "digital");
100}
101
102void isp1760_set_pullup(struct isp1760_device *isp, bool enable)
103{
104 isp1760_write32(isp->regs, HW_OTG_CTRL_SET,
105 enable ? HW_DP_PULLUP : HW_DP_PULLUP << 16);
106}
107
108int isp1760_register(struct resource *mem, int irq, unsigned long irqflags,
109 struct device *dev, unsigned int devflags)
110{
111 struct isp1760_device *isp;
112 bool udc_disabled = !(devflags & ISP1760_FLAG_ISP1761);
113 int ret;
114
115
116
117
118
119 if ((!IS_ENABLED(CONFIG_USB_ISP1760_HCD) || usb_disabled()) &&
120 (!IS_ENABLED(CONFIG_USB_ISP1761_UDC) || udc_disabled))
121 return -ENODEV;
122
123
124 dev->dma_mask = NULL;
125
126 isp = devm_kzalloc(dev, sizeof(*isp), GFP_KERNEL);
127 if (!isp)
128 return -ENOMEM;
129
130 isp->dev = dev;
131 isp->devflags = devflags;
132
133 isp->rst_gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
134 if (IS_ERR(isp->rst_gpio))
135 return PTR_ERR(isp->rst_gpio);
136
137 isp->regs = devm_ioremap_resource(dev, mem);
138 if (IS_ERR(isp->regs))
139 return PTR_ERR(isp->regs);
140
141 isp1760_init_core(isp);
142
143 if (IS_ENABLED(CONFIG_USB_ISP1760_HCD) && !usb_disabled()) {
144 ret = isp1760_hcd_register(&isp->hcd, isp->regs, mem, irq,
145 irqflags | IRQF_SHARED, dev);
146 if (ret < 0)
147 return ret;
148 }
149
150 if (IS_ENABLED(CONFIG_USB_ISP1761_UDC) && !udc_disabled) {
151 ret = isp1760_udc_register(isp, irq, irqflags);
152 if (ret < 0) {
153 isp1760_hcd_unregister(&isp->hcd);
154 return ret;
155 }
156 }
157
158 dev_set_drvdata(dev, isp);
159
160 return 0;
161}
162
163void isp1760_unregister(struct device *dev)
164{
165 struct isp1760_device *isp = dev_get_drvdata(dev);
166
167 isp1760_udc_unregister(isp);
168 isp1760_hcd_unregister(&isp->hcd);
169}
170
171MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
172MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
173MODULE_LICENSE("GPL v2");
174