xref: /linux/drivers/usb/isp1760/isp1760-core.c (revision 0f0290089abd37ca54db764c294fe7e47624a313)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
27ef077a8SLaurent Pinchart /*
37ef077a8SLaurent Pinchart  * Driver for the NXP ISP1760 chip
47ef077a8SLaurent Pinchart  *
57ef077a8SLaurent Pinchart  * Copyright 2014 Laurent Pinchart
67ef077a8SLaurent Pinchart  * Copyright 2007 Sebastian Siewior
77ef077a8SLaurent Pinchart  *
87ef077a8SLaurent Pinchart  * Contacts:
97ef077a8SLaurent Pinchart  *	Sebastian Siewior <bigeasy@linutronix.de>
107ef077a8SLaurent Pinchart  *	Laurent Pinchart <laurent.pinchart@ideasonboard.com>
117ef077a8SLaurent Pinchart  */
127ef077a8SLaurent Pinchart 
137ef077a8SLaurent Pinchart #include <linux/delay.h>
147ef077a8SLaurent Pinchart #include <linux/gpio/consumer.h>
157ef077a8SLaurent Pinchart #include <linux/io.h>
167ef077a8SLaurent Pinchart #include <linux/kernel.h>
177ef077a8SLaurent Pinchart #include <linux/module.h>
187ef077a8SLaurent Pinchart #include <linux/slab.h>
197ef077a8SLaurent Pinchart #include <linux/usb.h>
207ef077a8SLaurent Pinchart 
217ef077a8SLaurent Pinchart #include "isp1760-core.h"
227ef077a8SLaurent Pinchart #include "isp1760-hcd.h"
237ef077a8SLaurent Pinchart #include "isp1760-regs.h"
247ef077a8SLaurent Pinchart #include "isp1760-udc.h"
257ef077a8SLaurent Pinchart 
267ef077a8SLaurent Pinchart static void isp1760_init_core(struct isp1760_device *isp)
277ef077a8SLaurent Pinchart {
287ef077a8SLaurent Pinchart 	u32 otgctrl;
297ef077a8SLaurent Pinchart 	u32 hwmode;
307ef077a8SLaurent Pinchart 
317ef077a8SLaurent Pinchart 	/* Low-level chip reset */
327ef077a8SLaurent Pinchart 	if (isp->rst_gpio) {
337ef077a8SLaurent Pinchart 		gpiod_set_value_cansleep(isp->rst_gpio, 1);
34*0f029008SJia-Ju Bai 		msleep(50);
357ef077a8SLaurent Pinchart 		gpiod_set_value_cansleep(isp->rst_gpio, 0);
367ef077a8SLaurent Pinchart 	}
377ef077a8SLaurent Pinchart 
387ef077a8SLaurent Pinchart 	/*
397ef077a8SLaurent Pinchart 	 * Reset the host controller, including the CPU interface
407ef077a8SLaurent Pinchart 	 * configuration.
417ef077a8SLaurent Pinchart 	 */
427ef077a8SLaurent Pinchart 	isp1760_write32(isp->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
437ef077a8SLaurent Pinchart 	msleep(100);
447ef077a8SLaurent Pinchart 
457ef077a8SLaurent Pinchart 	/* Setup HW Mode Control: This assumes a level active-low interrupt */
467ef077a8SLaurent Pinchart 	hwmode = HW_DATA_BUS_32BIT;
477ef077a8SLaurent Pinchart 
487ef077a8SLaurent Pinchart 	if (isp->devflags & ISP1760_FLAG_BUS_WIDTH_16)
497ef077a8SLaurent Pinchart 		hwmode &= ~HW_DATA_BUS_32BIT;
507ef077a8SLaurent Pinchart 	if (isp->devflags & ISP1760_FLAG_ANALOG_OC)
517ef077a8SLaurent Pinchart 		hwmode |= HW_ANA_DIGI_OC;
527ef077a8SLaurent Pinchart 	if (isp->devflags & ISP1760_FLAG_DACK_POL_HIGH)
537ef077a8SLaurent Pinchart 		hwmode |= HW_DACK_POL_HIGH;
547ef077a8SLaurent Pinchart 	if (isp->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
557ef077a8SLaurent Pinchart 		hwmode |= HW_DREQ_POL_HIGH;
567ef077a8SLaurent Pinchart 	if (isp->devflags & ISP1760_FLAG_INTR_POL_HIGH)
577ef077a8SLaurent Pinchart 		hwmode |= HW_INTR_HIGH_ACT;
587ef077a8SLaurent Pinchart 	if (isp->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
597ef077a8SLaurent Pinchart 		hwmode |= HW_INTR_EDGE_TRIG;
607ef077a8SLaurent Pinchart 
617ef077a8SLaurent Pinchart 	/*
627ef077a8SLaurent Pinchart 	 * The ISP1761 has a dedicated DC IRQ line but supports sharing the HC
637ef077a8SLaurent Pinchart 	 * IRQ line for both the host and device controllers. Hardcode IRQ
647ef077a8SLaurent Pinchart 	 * sharing for now and disable the DC interrupts globally to avoid
657ef077a8SLaurent Pinchart 	 * spurious interrupts during HCD registration.
667ef077a8SLaurent Pinchart 	 */
677ef077a8SLaurent Pinchart 	if (isp->devflags & ISP1760_FLAG_ISP1761) {
687ef077a8SLaurent Pinchart 		isp1760_write32(isp->regs, DC_MODE, 0);
697ef077a8SLaurent Pinchart 		hwmode |= HW_COMN_IRQ;
707ef077a8SLaurent Pinchart 	}
717ef077a8SLaurent Pinchart 
727ef077a8SLaurent Pinchart 	/*
737ef077a8SLaurent Pinchart 	 * We have to set this first in case we're in 16-bit mode.
747ef077a8SLaurent Pinchart 	 * Write it twice to ensure correct upper bits if switching
757ef077a8SLaurent Pinchart 	 * to 16-bit mode.
767ef077a8SLaurent Pinchart 	 */
777ef077a8SLaurent Pinchart 	isp1760_write32(isp->regs, HC_HW_MODE_CTRL, hwmode);
787ef077a8SLaurent Pinchart 	isp1760_write32(isp->regs, HC_HW_MODE_CTRL, hwmode);
797ef077a8SLaurent Pinchart 
807ef077a8SLaurent Pinchart 	/*
817ef077a8SLaurent Pinchart 	 * PORT 1 Control register of the ISP1760 is the OTG control register
827ef077a8SLaurent Pinchart 	 * on ISP1761.
837ef077a8SLaurent Pinchart 	 *
847ef077a8SLaurent Pinchart 	 * TODO: Really support OTG. For now we configure port 1 in device mode
857ef077a8SLaurent Pinchart 	 * when OTG is requested.
867ef077a8SLaurent Pinchart 	 */
877ef077a8SLaurent Pinchart 	if ((isp->devflags & ISP1760_FLAG_ISP1761) &&
887ef077a8SLaurent Pinchart 	    (isp->devflags & ISP1760_FLAG_OTG_EN))
897ef077a8SLaurent Pinchart 		otgctrl = ((HW_DM_PULLDOWN | HW_DP_PULLDOWN) << 16)
907ef077a8SLaurent Pinchart 			| HW_OTG_DISABLE;
917ef077a8SLaurent Pinchart 	else
927ef077a8SLaurent Pinchart 		otgctrl = (HW_SW_SEL_HC_DC << 16)
937ef077a8SLaurent Pinchart 			| (HW_VBUS_DRV | HW_SEL_CP_EXT);
947ef077a8SLaurent Pinchart 
957ef077a8SLaurent Pinchart 	isp1760_write32(isp->regs, HC_PORT1_CTRL, otgctrl);
967ef077a8SLaurent Pinchart 
977ef077a8SLaurent Pinchart 	dev_info(isp->dev, "bus width: %u, oc: %s\n",
987ef077a8SLaurent Pinchart 		 isp->devflags & ISP1760_FLAG_BUS_WIDTH_16 ? 16 : 32,
997ef077a8SLaurent Pinchart 		 isp->devflags & ISP1760_FLAG_ANALOG_OC ? "analog" : "digital");
1007ef077a8SLaurent Pinchart }
1017ef077a8SLaurent Pinchart 
1027ef077a8SLaurent Pinchart void isp1760_set_pullup(struct isp1760_device *isp, bool enable)
1037ef077a8SLaurent Pinchart {
1047ef077a8SLaurent Pinchart 	isp1760_write32(isp->regs, HW_OTG_CTRL_SET,
1057ef077a8SLaurent Pinchart 			enable ? HW_DP_PULLUP : HW_DP_PULLUP << 16);
1067ef077a8SLaurent Pinchart }
1077ef077a8SLaurent Pinchart 
1087ef077a8SLaurent Pinchart int isp1760_register(struct resource *mem, int irq, unsigned long irqflags,
1097ef077a8SLaurent Pinchart 		     struct device *dev, unsigned int devflags)
1107ef077a8SLaurent Pinchart {
1117ef077a8SLaurent Pinchart 	struct isp1760_device *isp;
112d21daf1eSLaurent Pinchart 	bool udc_disabled = !(devflags & ISP1760_FLAG_ISP1761);
1137ef077a8SLaurent Pinchart 	int ret;
1147ef077a8SLaurent Pinchart 
115d21daf1eSLaurent Pinchart 	/*
116d21daf1eSLaurent Pinchart 	 * If neither the HCD not the UDC is enabled return an error, as no
117d21daf1eSLaurent Pinchart 	 * device would be registered.
118d21daf1eSLaurent Pinchart 	 */
119d21daf1eSLaurent Pinchart 	if ((!IS_ENABLED(CONFIG_USB_ISP1760_HCD) || usb_disabled()) &&
120d21daf1eSLaurent Pinchart 	    (!IS_ENABLED(CONFIG_USB_ISP1761_UDC) || udc_disabled))
1217ef077a8SLaurent Pinchart 		return -ENODEV;
1227ef077a8SLaurent Pinchart 
1237ef077a8SLaurent Pinchart 	/* prevent usb-core allocating DMA pages */
1247ef077a8SLaurent Pinchart 	dev->dma_mask = NULL;
1257ef077a8SLaurent Pinchart 
1267ef077a8SLaurent Pinchart 	isp = devm_kzalloc(dev, sizeof(*isp), GFP_KERNEL);
1277ef077a8SLaurent Pinchart 	if (!isp)
1287ef077a8SLaurent Pinchart 		return -ENOMEM;
1297ef077a8SLaurent Pinchart 
1307ef077a8SLaurent Pinchart 	isp->dev = dev;
1317ef077a8SLaurent Pinchart 	isp->devflags = devflags;
1327ef077a8SLaurent Pinchart 
1337ef077a8SLaurent Pinchart 	isp->rst_gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
1347ef077a8SLaurent Pinchart 	if (IS_ERR(isp->rst_gpio))
1357ef077a8SLaurent Pinchart 		return PTR_ERR(isp->rst_gpio);
1367ef077a8SLaurent Pinchart 
1377ef077a8SLaurent Pinchart 	isp->regs = devm_ioremap_resource(dev, mem);
1387ef077a8SLaurent Pinchart 	if (IS_ERR(isp->regs))
1397ef077a8SLaurent Pinchart 		return PTR_ERR(isp->regs);
1407ef077a8SLaurent Pinchart 
1417ef077a8SLaurent Pinchart 	isp1760_init_core(isp);
1427ef077a8SLaurent Pinchart 
143d21daf1eSLaurent Pinchart 	if (IS_ENABLED(CONFIG_USB_ISP1760_HCD) && !usb_disabled()) {
1447ef077a8SLaurent Pinchart 		ret = isp1760_hcd_register(&isp->hcd, isp->regs, mem, irq,
1457ef077a8SLaurent Pinchart 					   irqflags | IRQF_SHARED, dev);
1467ef077a8SLaurent Pinchart 		if (ret < 0)
1477ef077a8SLaurent Pinchart 			return ret;
148d21daf1eSLaurent Pinchart 	}
1497ef077a8SLaurent Pinchart 
150d21daf1eSLaurent Pinchart 	if (IS_ENABLED(CONFIG_USB_ISP1761_UDC) && !udc_disabled) {
15180b4a0f8SValentin Rothberg 		ret = isp1760_udc_register(isp, irq, irqflags);
1527ef077a8SLaurent Pinchart 		if (ret < 0) {
1537ef077a8SLaurent Pinchart 			isp1760_hcd_unregister(&isp->hcd);
1547ef077a8SLaurent Pinchart 			return ret;
1557ef077a8SLaurent Pinchart 		}
1567ef077a8SLaurent Pinchart 	}
1577ef077a8SLaurent Pinchart 
1587ef077a8SLaurent Pinchart 	dev_set_drvdata(dev, isp);
1597ef077a8SLaurent Pinchart 
1607ef077a8SLaurent Pinchart 	return 0;
1617ef077a8SLaurent Pinchart }
1627ef077a8SLaurent Pinchart 
1637ef077a8SLaurent Pinchart void isp1760_unregister(struct device *dev)
1647ef077a8SLaurent Pinchart {
1657ef077a8SLaurent Pinchart 	struct isp1760_device *isp = dev_get_drvdata(dev);
1667ef077a8SLaurent Pinchart 
1677ef077a8SLaurent Pinchart 	isp1760_udc_unregister(isp);
1687ef077a8SLaurent Pinchart 	isp1760_hcd_unregister(&isp->hcd);
1697ef077a8SLaurent Pinchart }
1707ef077a8SLaurent Pinchart 
1717ef077a8SLaurent Pinchart MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
1727ef077a8SLaurent Pinchart MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
1737ef077a8SLaurent Pinchart MODULE_LICENSE("GPL v2");
174