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