xref: /linux/drivers/usb/isp1760/isp1760-core.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for the NXP ISP1760 chip
4  *
5  * Copyright 2014 Laurent Pinchart
6  * Copyright 2007 Sebastian Siewior
7  *
8  * Contacts:
9  *	Sebastian Siewior <bigeasy@linutronix.de>
10  *	Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * version 2 as published by the Free Software Foundation.
15  */
16 
17 #include <linux/delay.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/usb.h>
24 
25 #include "isp1760-core.h"
26 #include "isp1760-hcd.h"
27 #include "isp1760-regs.h"
28 #include "isp1760-udc.h"
29 
30 static void isp1760_init_core(struct isp1760_device *isp)
31 {
32 	u32 otgctrl;
33 	u32 hwmode;
34 
35 	/* Low-level chip reset */
36 	if (isp->rst_gpio) {
37 		gpiod_set_value_cansleep(isp->rst_gpio, 1);
38 		mdelay(50);
39 		gpiod_set_value_cansleep(isp->rst_gpio, 0);
40 	}
41 
42 	/*
43 	 * Reset the host controller, including the CPU interface
44 	 * configuration.
45 	 */
46 	isp1760_write32(isp->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
47 	msleep(100);
48 
49 	/* Setup HW Mode Control: This assumes a level active-low interrupt */
50 	hwmode = HW_DATA_BUS_32BIT;
51 
52 	if (isp->devflags & ISP1760_FLAG_BUS_WIDTH_16)
53 		hwmode &= ~HW_DATA_BUS_32BIT;
54 	if (isp->devflags & ISP1760_FLAG_ANALOG_OC)
55 		hwmode |= HW_ANA_DIGI_OC;
56 	if (isp->devflags & ISP1760_FLAG_DACK_POL_HIGH)
57 		hwmode |= HW_DACK_POL_HIGH;
58 	if (isp->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
59 		hwmode |= HW_DREQ_POL_HIGH;
60 	if (isp->devflags & ISP1760_FLAG_INTR_POL_HIGH)
61 		hwmode |= HW_INTR_HIGH_ACT;
62 	if (isp->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
63 		hwmode |= HW_INTR_EDGE_TRIG;
64 
65 	/*
66 	 * The ISP1761 has a dedicated DC IRQ line but supports sharing the HC
67 	 * IRQ line for both the host and device controllers. Hardcode IRQ
68 	 * sharing for now and disable the DC interrupts globally to avoid
69 	 * spurious interrupts during HCD registration.
70 	 */
71 	if (isp->devflags & ISP1760_FLAG_ISP1761) {
72 		isp1760_write32(isp->regs, DC_MODE, 0);
73 		hwmode |= HW_COMN_IRQ;
74 	}
75 
76 	/*
77 	 * We have to set this first in case we're in 16-bit mode.
78 	 * Write it twice to ensure correct upper bits if switching
79 	 * to 16-bit mode.
80 	 */
81 	isp1760_write32(isp->regs, HC_HW_MODE_CTRL, hwmode);
82 	isp1760_write32(isp->regs, HC_HW_MODE_CTRL, hwmode);
83 
84 	/*
85 	 * PORT 1 Control register of the ISP1760 is the OTG control register
86 	 * on ISP1761.
87 	 *
88 	 * TODO: Really support OTG. For now we configure port 1 in device mode
89 	 * when OTG is requested.
90 	 */
91 	if ((isp->devflags & ISP1760_FLAG_ISP1761) &&
92 	    (isp->devflags & ISP1760_FLAG_OTG_EN))
93 		otgctrl = ((HW_DM_PULLDOWN | HW_DP_PULLDOWN) << 16)
94 			| HW_OTG_DISABLE;
95 	else
96 		otgctrl = (HW_SW_SEL_HC_DC << 16)
97 			| (HW_VBUS_DRV | HW_SEL_CP_EXT);
98 
99 	isp1760_write32(isp->regs, HC_PORT1_CTRL, otgctrl);
100 
101 	dev_info(isp->dev, "bus width: %u, oc: %s\n",
102 		 isp->devflags & ISP1760_FLAG_BUS_WIDTH_16 ? 16 : 32,
103 		 isp->devflags & ISP1760_FLAG_ANALOG_OC ? "analog" : "digital");
104 }
105 
106 void isp1760_set_pullup(struct isp1760_device *isp, bool enable)
107 {
108 	isp1760_write32(isp->regs, HW_OTG_CTRL_SET,
109 			enable ? HW_DP_PULLUP : HW_DP_PULLUP << 16);
110 }
111 
112 int isp1760_register(struct resource *mem, int irq, unsigned long irqflags,
113 		     struct device *dev, unsigned int devflags)
114 {
115 	struct isp1760_device *isp;
116 	bool udc_disabled = !(devflags & ISP1760_FLAG_ISP1761);
117 	int ret;
118 
119 	/*
120 	 * If neither the HCD not the UDC is enabled return an error, as no
121 	 * device would be registered.
122 	 */
123 	if ((!IS_ENABLED(CONFIG_USB_ISP1760_HCD) || usb_disabled()) &&
124 	    (!IS_ENABLED(CONFIG_USB_ISP1761_UDC) || udc_disabled))
125 		return -ENODEV;
126 
127 	/* prevent usb-core allocating DMA pages */
128 	dev->dma_mask = NULL;
129 
130 	isp = devm_kzalloc(dev, sizeof(*isp), GFP_KERNEL);
131 	if (!isp)
132 		return -ENOMEM;
133 
134 	isp->dev = dev;
135 	isp->devflags = devflags;
136 
137 	isp->rst_gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
138 	if (IS_ERR(isp->rst_gpio))
139 		return PTR_ERR(isp->rst_gpio);
140 
141 	isp->regs = devm_ioremap_resource(dev, mem);
142 	if (IS_ERR(isp->regs))
143 		return PTR_ERR(isp->regs);
144 
145 	isp1760_init_core(isp);
146 
147 	if (IS_ENABLED(CONFIG_USB_ISP1760_HCD) && !usb_disabled()) {
148 		ret = isp1760_hcd_register(&isp->hcd, isp->regs, mem, irq,
149 					   irqflags | IRQF_SHARED, dev);
150 		if (ret < 0)
151 			return ret;
152 	}
153 
154 	if (IS_ENABLED(CONFIG_USB_ISP1761_UDC) && !udc_disabled) {
155 		ret = isp1760_udc_register(isp, irq, irqflags);
156 		if (ret < 0) {
157 			isp1760_hcd_unregister(&isp->hcd);
158 			return ret;
159 		}
160 	}
161 
162 	dev_set_drvdata(dev, isp);
163 
164 	return 0;
165 }
166 
167 void isp1760_unregister(struct device *dev)
168 {
169 	struct isp1760_device *isp = dev_get_drvdata(dev);
170 
171 	isp1760_udc_unregister(isp);
172 	isp1760_hcd_unregister(&isp->hcd);
173 }
174 
175 MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
176 MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
177 MODULE_LICENSE("GPL v2");
178