Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * driver for NXP USB Host devices
0004  *
0005  * Currently supported OHCI host devices:
0006  * - NXP LPC32xx
0007  *
0008  * Authors: Dmitry Chigirev <source@mvista.com>
0009  *      Vitaly Wool <vitalywool@gmail.com>
0010  *
0011  * register initialization is based on code examples provided by Philips
0012  * Copyright (c) 2005 Koninklijke Philips Electronics N.V.
0013  *
0014  * NOTE: This driver does not have suspend/resume functionality
0015  * This driver is intended for engineering development purposes only
0016  *
0017  * 2005-2006 (c) MontaVista Software, Inc.
0018  */
0019 #include <linux/clk.h>
0020 #include <linux/dma-mapping.h>
0021 #include <linux/io.h>
0022 #include <linux/i2c.h>
0023 #include <linux/module.h>
0024 #include <linux/of.h>
0025 #include <linux/platform_device.h>
0026 #include <linux/usb/isp1301.h>
0027 #include <linux/usb.h>
0028 #include <linux/usb/hcd.h>
0029 
0030 #include "ohci.h"
0031 
0032 #define USB_CONFIG_BASE     0x31020000
0033 
0034 /* USB_OTG_STAT_CONTROL bit defines */
0035 #define TRANSPARENT_I2C_EN  (1 << 7)
0036 #define HOST_EN         (1 << 0)
0037 
0038 /* On LPC32xx, those are undefined */
0039 #ifndef start_int_set_falling_edge
0040 #define start_int_set_falling_edge(irq)
0041 #define start_int_set_rising_edge(irq)
0042 #define start_int_ack(irq)
0043 #define start_int_mask(irq)
0044 #define start_int_umask(irq)
0045 #endif
0046 
0047 #define DRIVER_DESC "OHCI NXP driver"
0048 
0049 static const char hcd_name[] = "ohci-nxp";
0050 static struct hc_driver __read_mostly ohci_nxp_hc_driver;
0051 
0052 static struct i2c_client *isp1301_i2c_client;
0053 
0054 static struct clk *usb_host_clk;
0055 
0056 static void isp1301_configure_lpc32xx(void)
0057 {
0058     /* LPC32XX only supports DAT_SE0 USB mode */
0059     /* This sequence is important */
0060 
0061     /* Disable transparent UART mode first */
0062     i2c_smbus_write_byte_data(isp1301_i2c_client,
0063         (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
0064         MC1_UART_EN);
0065     i2c_smbus_write_byte_data(isp1301_i2c_client,
0066         (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
0067         ~MC1_SPEED_REG);
0068     i2c_smbus_write_byte_data(isp1301_i2c_client,
0069                   ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
0070     i2c_smbus_write_byte_data(isp1301_i2c_client,
0071           (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR),
0072           ~0);
0073     i2c_smbus_write_byte_data(isp1301_i2c_client,
0074         ISP1301_I2C_MODE_CONTROL_2,
0075         (MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL));
0076     i2c_smbus_write_byte_data(isp1301_i2c_client,
0077         (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
0078     i2c_smbus_write_byte_data(isp1301_i2c_client,
0079         ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
0080     i2c_smbus_write_byte_data(isp1301_i2c_client,
0081         ISP1301_I2C_OTG_CONTROL_1,
0082         (OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
0083     i2c_smbus_write_byte_data(isp1301_i2c_client,
0084         (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
0085         (OTG1_DM_PULLUP | OTG1_DP_PULLUP));
0086     i2c_smbus_write_byte_data(isp1301_i2c_client,
0087         ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
0088     i2c_smbus_write_byte_data(isp1301_i2c_client,
0089         ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
0090         ~0);
0091     i2c_smbus_write_byte_data(isp1301_i2c_client,
0092         ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
0093 
0094     printk(KERN_INFO "ISP1301 Vendor ID  : 0x%04x\n",
0095           i2c_smbus_read_word_data(isp1301_i2c_client, 0x00));
0096     printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n",
0097           i2c_smbus_read_word_data(isp1301_i2c_client, 0x02));
0098     printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n",
0099           i2c_smbus_read_word_data(isp1301_i2c_client, 0x14));
0100 }
0101 
0102 static void isp1301_configure(void)
0103 {
0104     isp1301_configure_lpc32xx();
0105 }
0106 
0107 static inline void isp1301_vbus_on(void)
0108 {
0109     i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1,
0110                   OTG1_VBUS_DRV);
0111 }
0112 
0113 static inline void isp1301_vbus_off(void)
0114 {
0115     i2c_smbus_write_byte_data(isp1301_i2c_client,
0116         ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
0117         OTG1_VBUS_DRV);
0118 }
0119 
0120 static void ohci_nxp_start_hc(void)
0121 {
0122     void __iomem *usb_otg_stat_control = ioremap(USB_CONFIG_BASE + 0x110, 4);
0123     unsigned long tmp;
0124 
0125     if (WARN_ON(!usb_otg_stat_control))
0126         return;
0127 
0128     tmp = __raw_readl(usb_otg_stat_control) | HOST_EN;
0129 
0130     __raw_writel(tmp, usb_otg_stat_control);
0131     isp1301_vbus_on();
0132 
0133     iounmap(usb_otg_stat_control);
0134 }
0135 
0136 static void ohci_nxp_stop_hc(void)
0137 {
0138     void __iomem *usb_otg_stat_control = ioremap(USB_CONFIG_BASE + 0x110, 4);
0139     unsigned long tmp;
0140 
0141     if (WARN_ON(!usb_otg_stat_control))
0142         return;
0143 
0144     isp1301_vbus_off();
0145     tmp = __raw_readl(usb_otg_stat_control) & ~HOST_EN;
0146     __raw_writel(tmp, usb_otg_stat_control);
0147 
0148     iounmap(usb_otg_stat_control);
0149 }
0150 
0151 static int ohci_hcd_nxp_probe(struct platform_device *pdev)
0152 {
0153     struct usb_hcd *hcd = NULL;
0154     const struct hc_driver *driver = &ohci_nxp_hc_driver;
0155     struct resource *res;
0156     int ret = 0, irq;
0157     struct device_node *isp1301_node;
0158 
0159     if (pdev->dev.of_node) {
0160         isp1301_node = of_parse_phandle(pdev->dev.of_node,
0161                         "transceiver", 0);
0162     } else {
0163         isp1301_node = NULL;
0164     }
0165 
0166     isp1301_i2c_client = isp1301_get_client(isp1301_node);
0167     of_node_put(isp1301_node);
0168     if (!isp1301_i2c_client)
0169         return -EPROBE_DEFER;
0170 
0171     ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
0172     if (ret)
0173         goto fail_disable;
0174 
0175     dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
0176     if (usb_disabled()) {
0177         dev_err(&pdev->dev, "USB is disabled\n");
0178         ret = -ENODEV;
0179         goto fail_disable;
0180     }
0181 
0182     /* Enable USB host clock */
0183     usb_host_clk = devm_clk_get(&pdev->dev, NULL);
0184     if (IS_ERR(usb_host_clk)) {
0185         dev_err(&pdev->dev, "failed to acquire USB OHCI clock\n");
0186         ret = PTR_ERR(usb_host_clk);
0187         goto fail_disable;
0188     }
0189 
0190     ret = clk_prepare_enable(usb_host_clk);
0191     if (ret < 0) {
0192         dev_err(&pdev->dev, "failed to start USB OHCI clock\n");
0193         goto fail_disable;
0194     }
0195 
0196     isp1301_configure();
0197 
0198     hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
0199     if (!hcd) {
0200         dev_err(&pdev->dev, "Failed to allocate HC buffer\n");
0201         ret = -ENOMEM;
0202         goto fail_hcd;
0203     }
0204 
0205     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0206     hcd->regs = devm_ioremap_resource(&pdev->dev, res);
0207     if (IS_ERR(hcd->regs)) {
0208         ret = PTR_ERR(hcd->regs);
0209         goto fail_resource;
0210     }
0211     hcd->rsrc_start = res->start;
0212     hcd->rsrc_len = resource_size(res);
0213 
0214     irq = platform_get_irq(pdev, 0);
0215     if (irq < 0) {
0216         ret = -ENXIO;
0217         goto fail_resource;
0218     }
0219 
0220     ohci_nxp_start_hc();
0221     platform_set_drvdata(pdev, hcd);
0222 
0223     dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
0224     ret = usb_add_hcd(hcd, irq, 0);
0225     if (ret == 0) {
0226         device_wakeup_enable(hcd->self.controller);
0227         return ret;
0228     }
0229 
0230     ohci_nxp_stop_hc();
0231 fail_resource:
0232     usb_put_hcd(hcd);
0233 fail_hcd:
0234     clk_disable_unprepare(usb_host_clk);
0235 fail_disable:
0236     isp1301_i2c_client = NULL;
0237     return ret;
0238 }
0239 
0240 static int ohci_hcd_nxp_remove(struct platform_device *pdev)
0241 {
0242     struct usb_hcd *hcd = platform_get_drvdata(pdev);
0243 
0244     usb_remove_hcd(hcd);
0245     ohci_nxp_stop_hc();
0246     usb_put_hcd(hcd);
0247     clk_disable_unprepare(usb_host_clk);
0248     isp1301_i2c_client = NULL;
0249 
0250     return 0;
0251 }
0252 
0253 /* work with hotplug and coldplug */
0254 MODULE_ALIAS("platform:usb-ohci");
0255 
0256 #ifdef CONFIG_OF
0257 static const struct of_device_id ohci_hcd_nxp_match[] = {
0258     { .compatible = "nxp,ohci-nxp" },
0259     {},
0260 };
0261 MODULE_DEVICE_TABLE(of, ohci_hcd_nxp_match);
0262 #endif
0263 
0264 static struct platform_driver ohci_hcd_nxp_driver = {
0265     .driver = {
0266         .name = "usb-ohci",
0267         .of_match_table = of_match_ptr(ohci_hcd_nxp_match),
0268     },
0269     .probe = ohci_hcd_nxp_probe,
0270     .remove = ohci_hcd_nxp_remove,
0271 };
0272 
0273 static int __init ohci_nxp_init(void)
0274 {
0275     if (usb_disabled())
0276         return -ENODEV;
0277 
0278     pr_info("%s: " DRIVER_DESC "\n", hcd_name);
0279 
0280     ohci_init_driver(&ohci_nxp_hc_driver, NULL);
0281     return platform_driver_register(&ohci_hcd_nxp_driver);
0282 }
0283 module_init(ohci_nxp_init);
0284 
0285 static void __exit ohci_nxp_cleanup(void)
0286 {
0287     platform_driver_unregister(&ohci_hcd_nxp_driver);
0288 }
0289 module_exit(ohci_nxp_cleanup);
0290 
0291 MODULE_DESCRIPTION(DRIVER_DESC);
0292 MODULE_LICENSE("GPL v2");