From patchwork Thu Jul 10 15:18:04 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Griffin X-Patchwork-Id: 33437 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pd0-f198.google.com (mail-pd0-f198.google.com [209.85.192.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id A955E203F4 for ; Thu, 10 Jul 2014 15:18:34 +0000 (UTC) Received: by mail-pd0-f198.google.com with SMTP id y10sf56067248pdj.9 for ; Thu, 10 Jul 2014 08:18:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=3l7bb0VrvEieABC+tpgLNXsPQugwQvqTzaZKbaQJ9Xs=; b=gyNQbswx5FqTDsX/b2Hy5TkuvFrirSZPfB+PWnFJ5r94rvMjx2kJUyoWRx9WKPSMlf af4YEa8vUyhxl0ihXHGtV/G32jgrsyoVm2QrooLePan1pEKCG6heCGaMd1w7AVPkEFSw hGJssT0836+6djdeAr94H2ntMsxP7oc2Y5Y9CDYqvNpEbOGHrdmDTWs0pFGgzuY9mcvf a7Lu78Gfdw/cLMDbsObXaNxGOvlvrmgzpXiw2DfAHThiY1acul41sTjTpVW9dkLs9Va0 f3V7w0qW51tntnDYLXsYra0Snn17AfyDlignlxyDvmBYB1O3TG4YC6RxTxbeQatQ20FP TdRA== X-Gm-Message-State: ALoCoQk88oce+bUafK1o8K7wIrQ13gMp6MYNe+V/cQRuv51lSF8+5JDrobL2zTLiew/lI1B54woD X-Received: by 10.66.222.138 with SMTP id qm10mr7462223pac.9.1405005511001; Thu, 10 Jul 2014 08:18:31 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.47.179 with SMTP id m48ls95814qga.84.gmail; Thu, 10 Jul 2014 08:18:30 -0700 (PDT) X-Received: by 10.52.82.166 with SMTP id j6mr125016vdy.87.1405005510873; Thu, 10 Jul 2014 08:18:30 -0700 (PDT) Received: from mail-vc0-f177.google.com (mail-vc0-f177.google.com [209.85.220.177]) by mx.google.com with ESMTPS id py4si23054499vcb.49.2014.07.10.08.18.30 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 10 Jul 2014 08:18:30 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.177 as permitted sender) client-ip=209.85.220.177; Received: by mail-vc0-f177.google.com with SMTP id ij19so10666371vcb.22 for ; Thu, 10 Jul 2014 08:18:30 -0700 (PDT) X-Received: by 10.58.112.65 with SMTP id io1mr841001veb.61.1405005510758; Thu, 10 Jul 2014 08:18:30 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp143023vcb; Thu, 10 Jul 2014 08:18:30 -0700 (PDT) X-Received: by 10.66.66.166 with SMTP id g6mr21908925pat.108.1405005509831; Thu, 10 Jul 2014 08:18:29 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id vt4si29620113pab.110.2014.07.10.08.18.29; Thu, 10 Jul 2014 08:18:29 -0700 (PDT) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753908AbaGJPST (ORCPT + 28 others); Thu, 10 Jul 2014 11:18:19 -0400 Received: from mail-we0-f175.google.com ([74.125.82.175]:48660 "EHLO mail-we0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752891AbaGJPSR (ORCPT ); Thu, 10 Jul 2014 11:18:17 -0400 Received: by mail-we0-f175.google.com with SMTP id k48so9033643wev.34 for ; Thu, 10 Jul 2014 08:18:15 -0700 (PDT) X-Received: by 10.194.48.103 with SMTP id k7mr56195948wjn.68.1405005494777; Thu, 10 Jul 2014 08:18:14 -0700 (PDT) Received: from localhost.localdomain (cpc14-aztw22-2-0-cust189.18-1.cable.virginm.net. [82.45.1.190]) by mx.google.com with ESMTPSA id ch5sm110990943wjb.18.2014.07.10.08.18.13 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 10 Jul 2014 08:18:14 -0700 (PDT) From: Peter Griffin To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, maxime.coquelin@st.com, patrice.chotard@st.com, gregkh@linuxfoundation.org, srinivas.kandagatla@gmail.com, linux-usb@vger.kernel.org, devicetree@vger.kernel.org Cc: peter.griffin@linaro.org, lee.jones@linaro.org Subject: [PATCH 1/3] usb: host: st-hcd: Add USB HCD support for STi SoCs Date: Thu, 10 Jul 2014 16:18:04 +0100 Message-Id: <1405005486-26355-2-git-send-email-peter.griffin@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1405005486-26355-1-git-send-email-peter.griffin@linaro.org> References: <1405005486-26355-1-git-send-email-peter.griffin@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: peter.griffin@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.177 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , This driver adds support for the USB HCD present in STi SoC's from STMicroelectronics. It has been tested on the stih416-b2020 board. Signed-off-by: Peter Griffin --- drivers/usb/host/Kconfig | 17 ++ drivers/usb/host/Makefile | 1 + drivers/usb/host/st-hcd.c | 471 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 489 insertions(+) create mode 100644 drivers/usb/host/st-hcd.c diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig index 61b7817..dc0358e 100644 --- a/drivers/usb/host/Kconfig +++ b/drivers/usb/host/Kconfig @@ -753,6 +753,23 @@ config USB_HCD_SSB If unsure, say N. +config USB_HCD_ST + tristate "STMicroelectronics STi family HCD support" + depends on ARCH_STI + select USB_OHCI_HCD_PLATFORM if USB_OHCI_HCD + select USB_EHCI_HCD_PLATFORM if USB_EHCI_HCD + select MFD_SYSCON + select GENERIC_PHY + help + Enable support for the EHCI and OCHI host controller on ST + consumer electronics SoCs. + + It converts the ST driver into two platform device drivers + for EHCI and OHCI and provides bus configuration, clock and power + management for the combined hardware block. + + If unsure, say N. + config USB_HCD_TEST_MODE bool "HCD test mode support" ---help--- diff --git a/drivers/usb/host/Makefile b/drivers/usb/host/Makefile index af89a90..af0b81d 100644 --- a/drivers/usb/host/Makefile +++ b/drivers/usb/host/Makefile @@ -74,3 +74,4 @@ obj-$(CONFIG_USB_HCD_SSB) += ssb-hcd.o obj-$(CONFIG_USB_FUSBH200_HCD) += fusbh200-hcd.o obj-$(CONFIG_USB_FOTG210_HCD) += fotg210-hcd.o obj-$(CONFIG_USB_MAX3421_HCD) += max3421-hcd.o +obj-$(CONFIG_USB_HCD_ST) += st-hcd.o diff --git a/drivers/usb/host/st-hcd.c b/drivers/usb/host/st-hcd.c new file mode 100644 index 0000000..8a9636c --- /dev/null +++ b/drivers/usb/host/st-hcd.c @@ -0,0 +1,471 @@ +/* + * STMicroelectronics HCD (Host Controller Driver) for USB 2.0 and 1.1. + * + * Copyright (c) 2013 STMicroelectronics (R&D) Ltd. + * Authors: Stephen Gallimore + * Peter Griffin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ohci.h" + +#define EHCI_CAPS_SIZE 0x10 +#define AHB2STBUS_INSREG01 (EHCI_CAPS_SIZE + 0x84) + +struct st_hcd_dev { + int port_nr; + struct platform_device *ehci_device; + struct platform_device *ohci_device; + struct clk *ic_clk; /* Interconnect clock to the controller block */ + struct clk *ohci_clk; /* 48MHz clock for OHCI */ + struct reset_control *pwr; + struct reset_control *rst; + struct phy *phy; +}; + +static inline void st_ehci_configure_bus(void __iomem *regs) +{ + /* Set EHCI packet buffer IN/OUT threshold to 128 bytes */ + u32 threshold = 128 | (128 << 16); + + writel(threshold, regs + AHB2STBUS_INSREG01); +} + +static int st_hcd_enable_clocks(struct device *dev, + struct st_hcd_dev *hcd_dev) +{ + int err; + /* + * The interconnect input clock have either fixed + * rate or the rate is defined on boot, so we are only concerned about + * enabling any gates for this clock. + */ + err = clk_prepare_enable(hcd_dev->ic_clk); + if (err) { + dev_err(dev, "can't enable ic clock\n"); + return err; + } + /* + * The 48MHz OHCI clock is usually provided by a programmable + * frequency synthesizer, which is often not programmed on boot/chip + * reset, so we set its rate here to ensure it is correct. + * + * However not all SoC's have a dedicated ohci clock so it isn't fatal + * for this not to exist. + */ + if (!IS_ERR(hcd_dev->ohci_clk)) { + err = clk_set_rate(hcd_dev->ohci_clk, 48000000); + if (err) { + dev_err(dev, "can't set ohci_clk rate\n"); + goto error; + } + + err = clk_prepare_enable(hcd_dev->ohci_clk); + if (err) { + dev_err(dev, "can't enable ohci_clk\n"); + goto error; + } + } + + return 0; +error: + clk_disable_unprepare(hcd_dev->ic_clk); + return err; +} + + +static void st_hcd_disable_clocks(struct st_hcd_dev *hcd_dev) +{ + /* not all SoCs provide a dedicated ohci_clk */ + if (!IS_ERR(hcd_dev->ohci_clk)) + clk_disable_unprepare(hcd_dev->ohci_clk); + + clk_disable_unprepare(hcd_dev->ic_clk); +} + +static void st_hcd_assert_resets(struct device *dev, + struct st_hcd_dev *hcd_dev) +{ + int err; + + err = reset_control_assert(hcd_dev->pwr); + if (err) + dev_err(dev, "unable to put into powerdown\n"); + + err = reset_control_assert(hcd_dev->rst); + if (err) + dev_err(dev, "unable to put into softreset\n"); +} + +static int st_hcd_deassert_resets(struct device *dev, + struct st_hcd_dev *hcd_dev) +{ + int err; + + err = reset_control_deassert(hcd_dev->pwr); + if (err) { + dev_err(dev, "unable to bring out of powerdown\n"); + return err; + } + + err = reset_control_deassert(hcd_dev->rst); + if (err) { + dev_err(dev, "unable to bring out of softreset\n"); + goto err_assert_power; + } + + return 0; + +err_assert_power: + reset_control_assert(hcd_dev->pwr); + return err; +} + +static const struct usb_ehci_pdata ehci_pdata = { +}; + +static const struct usb_ohci_pdata ohci_pdata = { +}; + +static int st_hcd_remove(struct platform_device *pdev) +{ + struct st_hcd_dev *hcd_dev = platform_get_drvdata(pdev); + + platform_device_unregister(hcd_dev->ehci_device); + platform_device_unregister(hcd_dev->ohci_device); + + phy_power_off(hcd_dev->phy); + + phy_exit(hcd_dev->phy); + + st_hcd_assert_resets(&pdev->dev, hcd_dev); + + st_hcd_disable_clocks(hcd_dev); + + return 0; +} + +static struct platform_device *st_hcd_device_create(const char *name, int id, + struct platform_device *parent) +{ + struct platform_device *pdev; + const char *platform_name; + struct resource *res; + struct resource hcd_res[2]; + int ret; + + res = platform_get_resource_byname(parent, IORESOURCE_MEM, name); + if (!res) + return ERR_PTR(-ENODEV); + + hcd_res[0] = *res; + + res = platform_get_resource_byname(parent, IORESOURCE_IRQ, name); + if (!res) + return ERR_PTR(-ENODEV); + + hcd_res[1] = *res; + + platform_name = kasprintf(GFP_KERNEL, "%s-platform", name); + if (!platform_name) + return ERR_PTR(-ENOMEM); + + pdev = platform_device_alloc(platform_name, id); + + kfree(platform_name); + + if (!pdev) + return ERR_PTR(-ENOMEM); + + pdev->dev.parent = &parent->dev; + pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; + pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); + + ret = platform_device_add_resources(pdev, hcd_res, ARRAY_SIZE(hcd_res)); + if (ret) + goto error; + + if (!strcmp(name, "ohci")) + ret = platform_device_add_data(pdev, &ohci_pdata, + sizeof(ohci_pdata)); + else + ret = platform_device_add_data(pdev, &ehci_pdata, + sizeof(ehci_pdata)); + + if (ret) + goto error; + + ret = platform_device_add(pdev); + if (ret) + goto error; + + return pdev; + +error: + platform_device_put(pdev); + return ERR_PTR(ret); +} + +#ifdef CONFIG_PM_SLEEP +static int st_hcd_resume(struct device *dev) +{ + struct st_hcd_dev *hcd_dev = dev_get_drvdata(dev); + struct usb_hcd *ehci_hcd = platform_get_drvdata(hcd_dev->ehci_device); + int err; + + pinctrl_pm_select_default_state(dev); + + err = st_hcd_enable_clocks(dev, hcd_dev); + if (err) + return err; + + err = phy_init(hcd_dev->phy); + if (err) { + dev_err(dev, "phy initialization failed\n"); + goto err_disable_clocks; + } + + err = phy_power_on(hcd_dev->phy); + if (err && (err != -ENOTSUPP)) { + dev_err(dev, "phy power on failed\n"); + goto err_phy_exit; + } + + err = st_hcd_deassert_resets(dev, hcd_dev); + if (err) + goto err_phy_off; + + st_ehci_configure_bus(ehci_hcd->regs); + + return 0; + +err_phy_off: + phy_power_off(hcd_dev->phy); +err_phy_exit: + phy_exit(hcd_dev->phy); +err_disable_clocks: + st_hcd_disable_clocks(hcd_dev); + + return err; +} + +static int st_hcd_suspend(struct device *dev) +{ + struct st_hcd_dev *hcd_dev = dev_get_drvdata(dev); + int err; + + err = reset_control_assert(hcd_dev->pwr); + if (err) { + dev_err(dev, "unable to put into powerdown\n"); + return err; + } + + err = reset_control_assert(hcd_dev->rst); + if (err) { + dev_err(dev, "unable to put into softreset\n"); + return err; + } + + err = phy_power_off(hcd_dev->phy); + if (err && (err != -ENOTSUPP)) { + dev_err(dev, "phy power off failed\n"); + return err; + } + + phy_exit(hcd_dev->phy); + + st_hcd_disable_clocks(hcd_dev); + + pinctrl_pm_select_sleep_state(dev); + + return 0; +} +#endif + +static SIMPLE_DEV_PM_OPS(st_hcd_pm, st_hcd_suspend, st_hcd_resume); + +static struct of_device_id st_hcd_match[] = { + { .compatible = "st,usb-300x" }, + { /* sentinel */ }, +}; +MODULE_DEVICE_TABLE(of, st_hcd_match); + +static int st_hcd_probe_clocks(struct device *dev, + struct st_hcd_dev *hcd_dev) +{ + hcd_dev->ic_clk = devm_clk_get(dev, "ic"); + if (IS_ERR(hcd_dev->ic_clk)) { + dev_err(dev, "ic clk not found\n"); + return PTR_ERR(hcd_dev->ic_clk); + } + + /* some SoCs don't have a dedicated ohci clk */ + hcd_dev->ohci_clk = devm_clk_get(dev, "ohci"); + if (IS_ERR(hcd_dev->ohci_clk)) + dev_info(dev, "48MHz ohci clk not found\n"); + + return st_hcd_enable_clocks(dev, hcd_dev); +} + + +static int st_hcd_probe_resets(struct device *dev, + struct st_hcd_dev *hcd_dev) +{ + hcd_dev->pwr = devm_reset_control_get(dev, "power"); + if (IS_ERR(hcd_dev->pwr)) { + dev_err(dev, "power reset control not found\n"); + return PTR_ERR(hcd_dev->pwr); + } + + hcd_dev->rst = devm_reset_control_get(dev, "softreset"); + if (IS_ERR(hcd_dev->rst)) { + dev_err(dev, "soft reset control not found\n"); + return PTR_ERR(hcd_dev->rst); + } + + return st_hcd_deassert_resets(dev, hcd_dev); +} + +static int st_hcd_probe_ehci_setup(struct platform_device *pdev) +{ + struct resource *res; + void __iomem *ehci_regs; + + /* + * We need to do some integration specific setup in the EHCI + * controller, which the EHCI platform driver does not provide any + * hooks to allow us to do during it's initialisation. + */ + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ehci"); + if (!res) + return -ENODEV; + + ehci_regs = devm_ioremap(&pdev->dev, res->start, resource_size(res)); + if (!ehci_regs) + return -ENOMEM; + + st_ehci_configure_bus(ehci_regs); + devm_iounmap(&pdev->dev, ehci_regs); + + return 0; +} + +static int st_hcd_probe(struct platform_device *pdev) +{ + struct st_hcd_dev *hcd_dev; + int id; + int err; + + if (!pdev->dev.of_node) + return -ENODEV; + + id = of_alias_get_id(pdev->dev.of_node, "usb"); + + if (id < 0) { + dev_err(&pdev->dev, "No ID specified via DT alias\n"); + return -ENODEV; + } + + hcd_dev = devm_kzalloc(&pdev->dev, sizeof(*hcd_dev), GFP_KERNEL); + if (!hcd_dev) + return -ENOMEM; + + hcd_dev->port_nr = id; + + err = st_hcd_probe_clocks(&pdev->dev, hcd_dev); + if (err) + return err; + + err = st_hcd_probe_resets(&pdev->dev, hcd_dev); + if (err) + goto err_disable_clocks; + + err = st_hcd_probe_ehci_setup(pdev); + if (err) + goto err_assert_resets; + + hcd_dev->phy = devm_phy_get(&pdev->dev, "usb2-phy"); + if (IS_ERR(hcd_dev->phy)) { + dev_err(&pdev->dev, "no PHY configured\n"); + err = PTR_ERR(hcd_dev->phy); + goto err_assert_resets; + } + + err = phy_init(hcd_dev->phy); + if (err) { + dev_err(&pdev->dev, "phy initialization failed\n"); + goto err_assert_resets; + } + + err = phy_power_on(hcd_dev->phy); + if (err && (err != -ENOTSUPP)) { + dev_err(&pdev->dev, "phy power on failed\n"); + goto err_phy_exit; + } + + hcd_dev->ehci_device = st_hcd_device_create("ehci", id, pdev); + if (IS_ERR(hcd_dev->ehci_device)) { + err = PTR_ERR(hcd_dev->ehci_device); + goto err_phy_off; + } + + hcd_dev->ohci_device = st_hcd_device_create("ohci", id, pdev); + if (IS_ERR(hcd_dev->ohci_device)) { + platform_device_del(hcd_dev->ehci_device); + err = PTR_ERR(hcd_dev->ohci_device); + goto err_phy_off; + } + + platform_set_drvdata(pdev, hcd_dev); + + return 0; + +err_phy_off: + phy_power_off(hcd_dev->phy); +err_phy_exit: + phy_exit(hcd_dev->phy); +err_assert_resets: + st_hcd_assert_resets(&pdev->dev, hcd_dev); +err_disable_clocks: + st_hcd_disable_clocks(hcd_dev); + + return err; +} + +static struct platform_driver st_hcd_driver = { + .probe = st_hcd_probe, + .remove = st_hcd_remove, + .driver = { + .name = "st-hcd", + .pm = &st_hcd_pm, + .of_match_table = st_hcd_match, + }, +}; + +module_platform_driver(st_hcd_driver); + +MODULE_DESCRIPTION("STMicroelectronics On-Chip USB Host Controller"); +MODULE_AUTHOR("Stephen Gallimore "); +MODULE_LICENSE("GPL v2");