usb: cdnsp: cdns3 Add main part of Cadence USBSSP DRD Driver

This patch introduces the main part of Cadence USBSSP DRD driver
to Linux kernel.
To reduce the patch size a little bit, the header file gadget.h was
intentionally added as separate patch.

The Cadence USBSSP DRD Controller is a highly configurable IP Core which
can be instantiated as Dual-Role Device (DRD), Peripheral Only and
Host Only (XHCI)configurations.

The current driver has been validated with FPGA platform. We have
support for PCIe bus, which is used on FPGA prototyping.

The host side of USBSS DRD controller is compliant with XHCI.
The architecture for device side is almost the same as for host side,
and most of the XHCI specification can be used to understand how
this controller operates.

Signed-off-by: Pawel Laszczak <pawell@cadence.com>
Signed-off-by: Peter Chen <peter.chen@nxp.com>
This commit is contained in:
Pawel Laszczak 2020-12-07 11:32:24 +01:00 committed by Peter Chen
parent e93e58d274
commit 3d82904559
No known key found for this signature in database
GPG Key ID: 4859298150D671BB
15 changed files with 6643 additions and 31 deletions

View File

@ -13,7 +13,9 @@ obj-$(CONFIG_USB_DWC3) += dwc3/
obj-$(CONFIG_USB_DWC2) += dwc2/
obj-$(CONFIG_USB_ISP1760) += isp1760/
obj-$(CONFIG_USB_CDNS_SUPPORT) += cdns3/
obj-$(CONFIG_USB_CDNS3) += cdns3/
obj-$(CONFIG_USB_CDNSP_PCI) += cdns3/
obj-$(CONFIG_USB_MON) += mon/
obj-$(CONFIG_USB_MTU3) += mtu3/

View File

@ -1,21 +1,28 @@
config CDNS_USB_COMMON
tristate
config CDNS_USB_HOST
bool
config USB_CDNS3
tristate "Cadence USB3 Dual-Role Controller"
config USB_CDNS_SUPPORT
tristate "Cadence USB Support"
depends on USB_SUPPORT && (USB || USB_GADGET) && HAS_DMA
select USB_XHCI_PLATFORM if USB_XHCI_HCD
select USB_ROLE_SWITCH
select CDNS_USB_COMMON
help
Say Y here if your system has a Cadence USBSS or USBSSP
dual-role controller.
It supports: dual-role switch, Host-only, and Peripheral-only.
config USB_CDNS_HOST
bool
if USB_CDNS_SUPPORT
config USB_CDNS3
tristate "Cadence USB3 Dual-Role Controller"
depends on USB_CDNS_SUPPORT
help
Say Y here if your system has a Cadence USB3 dual-role controller.
It supports: dual-role switch, Host-only, and Peripheral-only.
If you choose to build this driver is a dynamically linked
as module, the module will be called cdns3.ko.
endif
if USB_CDNS3
@ -32,7 +39,7 @@ config USB_CDNS3_GADGET
config USB_CDNS3_HOST
bool "Cadence USB3 host controller"
depends on USB=y || USB=USB_CDNS3
select CDNS_USB_HOST
select USB_CDNS_HOST
help
Say Y here to enable host controller functionality of the
Cadence driver.
@ -72,3 +79,44 @@ config USB_CDNS3_IMX
For example, imx8qm and imx8qxp.
endif
if USB_CDNS_SUPPORT
config USB_CDNSP_PCI
tristate "Cadence CDNSP Dual-Role Controller"
depends on USB_CDNS_SUPPORT && USB_PCI && ACPI
help
Say Y here if your system has a Cadence CDNSP dual-role controller.
It supports: dual-role switch Host-only, and Peripheral-only.
If you choose to build this driver is a dynamically linked
module, the module will be called cdnsp.ko.
endif
if USB_CDNSP_PCI
config USB_CDNSP_GADGET
bool "Cadence CDNSP device controller"
depends on USB_GADGET=y || USB_GADGET=USB_CDNSP_PCI
help
Say Y here to enable device controller functionality of the
Cadence CDNSP-DEV driver.
Cadence CDNSP Device Controller in device mode is
very similar to XHCI controller. Therefore some algorithms
used has been taken from host driver.
This controller supports FF, HS, SS and SSP mode.
It doesn't support LS.
config USB_CDNSP_HOST
bool "Cadence CDNSP host controller"
depends on USB=y || USB=USB_CDNSP_PCI
select USB_CDNS_HOST
help
Say Y here to enable host controller functionality of the
Cadence driver.
Host controller is compliant with XHCI so it uses
standard XHCI driver.
endif

View File

@ -1,20 +1,25 @@
# SPDX-License-Identifier: GPL-2.0
# define_trace.h needs to know how to find our header
CFLAGS_trace.o := -I$(src)
CFLAGS_trace.o := -I$(src)
cdns-usb-common-y := core.o drd.o
cdns3-y := cdns3-plat.o
cdns-usb-common-y := core.o drd.o
cdns3-y := cdns3-plat.o
obj-$(CONFIG_USB_CDNS3) += cdns3.o
obj-$(CONFIG_CDNS_USB_COMMON) += cdns-usb-common.o
obj-$(CONFIG_USB_CDNS3) += cdns3.o
obj-$(CONFIG_USB_CDNS_SUPPORT) += cdns-usb-common.o
cdns-usb-common-$(CONFIG_CDNS_USB_HOST) += host.o
cdns3-$(CONFIG_USB_CDNS3_GADGET) += gadget.o ep0.o
cdns-usb-common-$(CONFIG_USB_CDNS_HOST) += host.o
cdns3-$(CONFIG_USB_CDNS3_GADGET) += gadget.o ep0.o
ifneq ($(CONFIG_USB_CDNS3_GADGET),)
cdns3-$(CONFIG_TRACING) += trace.o
cdns3-$(CONFIG_TRACING) += trace.o
endif
obj-$(CONFIG_USB_CDNS3_PCI_WRAP) += cdns3-pci-wrap.o
obj-$(CONFIG_USB_CDNS3_TI) += cdns3-ti.o
obj-$(CONFIG_USB_CDNS3_IMX) += cdns3-imx.o
obj-$(CONFIG_USB_CDNS3_PCI_WRAP) += cdns3-pci-wrap.o
obj-$(CONFIG_USB_CDNS3_TI) += cdns3-ti.o
obj-$(CONFIG_USB_CDNS3_IMX) += cdns3-imx.o
cdnsp-udc-pci-y := cdnsp-pci.o
obj-$(CONFIG_USB_CDNSP_PCI) += cdnsp-udc-pci.o
cdnsp-udc-pci-$(CONFIG_USB_CDNSP_GADGET) += cdnsp-ring.o cdnsp-gadget.o \
cdnsp-mem.o cdnsp-ep0.o

View File

@ -0,0 +1,477 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Cadence CDNSP DRD Driver.
*
* Copyright (C) 2020 Cadence.
*
* Author: Pawel Laszczak <pawell@cadence.com>
*
*/
#include <linux/usb/composite.h>
#include <linux/usb/gadget.h>
#include <linux/list.h>
#include "cdnsp-gadget.h"
static void cdnsp_ep0_stall(struct cdnsp_device *pdev)
{
struct cdnsp_request *preq;
struct cdnsp_ep *pep;
pep = &pdev->eps[0];
preq = next_request(&pep->pending_list);
if (pdev->three_stage_setup) {
cdnsp_halt_endpoint(pdev, pep, true);
if (preq)
cdnsp_gadget_giveback(pep, preq, -ECONNRESET);
} else {
pep->ep_state |= EP0_HALTED_STATUS;
if (preq)
list_del(&preq->list);
cdnsp_status_stage(pdev);
}
}
static int cdnsp_ep0_delegate_req(struct cdnsp_device *pdev,
struct usb_ctrlrequest *ctrl)
{
int ret;
spin_unlock(&pdev->lock);
ret = pdev->gadget_driver->setup(&pdev->gadget, ctrl);
spin_lock(&pdev->lock);
return ret;
}
static int cdnsp_ep0_set_config(struct cdnsp_device *pdev,
struct usb_ctrlrequest *ctrl)
{
enum usb_device_state state = pdev->gadget.state;
u32 cfg;
int ret;
cfg = le16_to_cpu(ctrl->wValue);
switch (state) {
case USB_STATE_ADDRESS:
break;
case USB_STATE_CONFIGURED:
break;
default:
dev_err(pdev->dev, "Set Configuration - bad device state\n");
return -EINVAL;
}
ret = cdnsp_ep0_delegate_req(pdev, ctrl);
if (ret)
return ret;
if (!cfg)
usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS);
return 0;
}
static int cdnsp_ep0_set_address(struct cdnsp_device *pdev,
struct usb_ctrlrequest *ctrl)
{
enum usb_device_state state = pdev->gadget.state;
struct cdnsp_slot_ctx *slot_ctx;
unsigned int slot_state;
int ret;
u32 addr;
addr = le16_to_cpu(ctrl->wValue);
if (addr > 127) {
dev_err(pdev->dev, "Invalid device address %d\n", addr);
return -EINVAL;
}
slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
if (state == USB_STATE_CONFIGURED) {
dev_err(pdev->dev, "Can't Set Address from Configured State\n");
return -EINVAL;
}
pdev->device_address = le16_to_cpu(ctrl->wValue);
slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
if (slot_state == SLOT_STATE_ADDRESSED)
cdnsp_reset_device(pdev);
/*set device address*/
ret = cdnsp_setup_device(pdev, SETUP_CONTEXT_ADDRESS);
if (ret)
return ret;
if (addr)
usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS);
else
usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT);
return 0;
}
int cdnsp_status_stage(struct cdnsp_device *pdev)
{
pdev->ep0_stage = CDNSP_STATUS_STAGE;
pdev->ep0_preq.request.length = 0;
return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
}
static int cdnsp_w_index_to_ep_index(__le32 wIndex)
{
wIndex = le32_to_cpu(wIndex);
if (!(wIndex & USB_ENDPOINT_NUMBER_MASK))
return 0;
return ((wIndex & USB_ENDPOINT_NUMBER_MASK) * 2) +
(wIndex & USB_ENDPOINT_DIR_MASK ? 1 : 0) - 1;
}
static int cdnsp_ep0_handle_status(struct cdnsp_device *pdev,
struct usb_ctrlrequest *ctrl)
{
struct cdnsp_ep *pep;
__le16 *response;
int ep_sts = 0;
u16 status = 0;
u32 recipient;
recipient = ctrl->bRequestType & USB_RECIP_MASK;
switch (recipient) {
case USB_RECIP_DEVICE:
status = pdev->gadget.is_selfpowered;
status |= pdev->may_wakeup << USB_DEVICE_REMOTE_WAKEUP;
if (pdev->gadget.speed >= USB_SPEED_SUPER) {
status |= pdev->u1_allowed << USB_DEV_STAT_U1_ENABLED;
status |= pdev->u2_allowed << USB_DEV_STAT_U2_ENABLED;
}
break;
case USB_RECIP_INTERFACE:
/*
* Function Remote Wake Capable D0
* Function Remote Wakeup D1
*/
return cdnsp_ep0_delegate_req(pdev, ctrl);
case USB_RECIP_ENDPOINT:
pep = &pdev->eps[cdnsp_w_index_to_ep_index(ctrl->wIndex)];
ep_sts = GET_EP_CTX_STATE(pep->out_ctx);
/* check if endpoint is stalled */
if (ep_sts == EP_STATE_HALTED)
status = BIT(USB_ENDPOINT_HALT);
break;
default:
return -EINVAL;
}
response = (__le16 *)pdev->setup_buf;
*response = cpu_to_le16(status);
pdev->ep0_preq.request.length = sizeof(*response);
pdev->ep0_preq.request.buf = pdev->setup_buf;
return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
}
static void cdnsp_enter_test_mode(struct cdnsp_device *pdev)
{
u32 temp;
temp = readl(&pdev->active_port->regs->portpmsc) & ~GENMASK(31, 28);
temp |= PORT_TEST_MODE(pdev->test_mode);
writel(temp, &pdev->active_port->regs->portpmsc);
}
static int cdnsp_ep0_handle_feature_device(struct cdnsp_device *pdev,
struct usb_ctrlrequest *ctrl,
int set)
{
enum usb_device_state state;
enum usb_device_speed speed;
u16 tmode;
state = pdev->gadget.state;
speed = pdev->gadget.speed;
switch (le16_to_cpu(ctrl->wValue)) {
case USB_DEVICE_REMOTE_WAKEUP:
pdev->may_wakeup = !!set;
break;
case USB_DEVICE_U1_ENABLE:
if (state != USB_STATE_CONFIGURED || speed < USB_SPEED_SUPER)
return -EINVAL;
pdev->u1_allowed = !!set;
break;
case USB_DEVICE_U2_ENABLE:
if (state != USB_STATE_CONFIGURED || speed < USB_SPEED_SUPER)
return -EINVAL;
pdev->u2_allowed = !!set;
break;
case USB_DEVICE_LTM_ENABLE:
return -EINVAL;
case USB_DEVICE_TEST_MODE:
if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH)
return -EINVAL;
tmode = le16_to_cpu(ctrl->wIndex);
if (!set || (tmode & 0xff) != 0)
return -EINVAL;
tmode = tmode >> 8;
if (tmode > USB_TEST_FORCE_ENABLE || tmode < USB_TEST_J)
return -EINVAL;
pdev->test_mode = tmode;
/*
* Test mode must be set before Status Stage but controller
* will start testing sequence after Status Stage.
*/
cdnsp_enter_test_mode(pdev);
break;
default:
return -EINVAL;
}
return 0;
}
static int cdnsp_ep0_handle_feature_intf(struct cdnsp_device *pdev,
struct usb_ctrlrequest *ctrl,
int set)
{
u16 wValue, wIndex;
int ret;
wValue = le16_to_cpu(ctrl->wValue);
wIndex = le16_to_cpu(ctrl->wIndex);
switch (wValue) {
case USB_INTRF_FUNC_SUSPEND:
ret = cdnsp_ep0_delegate_req(pdev, ctrl);
if (ret)
return ret;
/*
* Remote wakeup is enabled when any function within a device
* is enabled for function remote wakeup.
*/
if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
pdev->may_wakeup++;
else
if (pdev->may_wakeup > 0)
pdev->may_wakeup--;
return 0;
default:
return -EINVAL;
}
return 0;
}
static int cdnsp_ep0_handle_feature_endpoint(struct cdnsp_device *pdev,
struct usb_ctrlrequest *ctrl,
int set)
{
struct cdnsp_ep *pep;
u32 wValue;
wValue = le16_to_cpu(ctrl->wValue);
pep = &pdev->eps[cdnsp_w_index_to_ep_index(ctrl->wIndex)];
switch (wValue) {
case USB_ENDPOINT_HALT:
if (!set && (pep->ep_state & EP_WEDGE)) {
/* Resets Sequence Number */
cdnsp_halt_endpoint(pdev, pep, 0);
cdnsp_halt_endpoint(pdev, pep, 1);
break;
}
return cdnsp_halt_endpoint(pdev, pep, set);
default:
dev_warn(pdev->dev, "WARN Incorrect wValue %04x\n", wValue);
return -EINVAL;
}
return 0;
}
static int cdnsp_ep0_handle_feature(struct cdnsp_device *pdev,
struct usb_ctrlrequest *ctrl,
int set)
{
switch (ctrl->bRequestType & USB_RECIP_MASK) {
case USB_RECIP_DEVICE:
return cdnsp_ep0_handle_feature_device(pdev, ctrl, set);
case USB_RECIP_INTERFACE:
return cdnsp_ep0_handle_feature_intf(pdev, ctrl, set);
case USB_RECIP_ENDPOINT:
return cdnsp_ep0_handle_feature_endpoint(pdev, ctrl, set);
default:
return -EINVAL;
}
}
static int cdnsp_ep0_set_sel(struct cdnsp_device *pdev,
struct usb_ctrlrequest *ctrl)
{
enum usb_device_state state = pdev->gadget.state;
u16 wLength;
if (state == USB_STATE_DEFAULT)
return -EINVAL;
wLength = le16_to_cpu(ctrl->wLength);
if (wLength != 6) {
dev_err(pdev->dev, "Set SEL should be 6 bytes, got %d\n",
wLength);
return -EINVAL;
}
/*
* To handle Set SEL we need to receive 6 bytes from Host. So let's
* queue a usb_request for 6 bytes.
*/
pdev->ep0_preq.request.length = 6;
pdev->ep0_preq.request.buf = pdev->setup_buf;
return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
}
static int cdnsp_ep0_set_isoch_delay(struct cdnsp_device *pdev,
struct usb_ctrlrequest *ctrl)
{
if (le16_to_cpu(ctrl->wIndex) || le16_to_cpu(ctrl->wLength))
return -EINVAL;
pdev->gadget.isoch_delay = le16_to_cpu(ctrl->wValue);
return 0;
}
static int cdnsp_ep0_std_request(struct cdnsp_device *pdev,
struct usb_ctrlrequest *ctrl)
{
int ret;
switch (ctrl->bRequest) {
case USB_REQ_GET_STATUS:
ret = cdnsp_ep0_handle_status(pdev, ctrl);
break;
case USB_REQ_CLEAR_FEATURE:
ret = cdnsp_ep0_handle_feature(pdev, ctrl, 0);
break;
case USB_REQ_SET_FEATURE:
ret = cdnsp_ep0_handle_feature(pdev, ctrl, 1);
break;
case USB_REQ_SET_ADDRESS:
ret = cdnsp_ep0_set_address(pdev, ctrl);
break;
case USB_REQ_SET_CONFIGURATION:
ret = cdnsp_ep0_set_config(pdev, ctrl);
break;
case USB_REQ_SET_SEL:
ret = cdnsp_ep0_set_sel(pdev, ctrl);
break;
case USB_REQ_SET_ISOCH_DELAY:
ret = cdnsp_ep0_set_isoch_delay(pdev, ctrl);
break;
case USB_REQ_SET_INTERFACE:
/*
* Add request into pending list to block sending status stage
* by libcomposite.
*/
list_add_tail(&pdev->ep0_preq.list,
&pdev->ep0_preq.pep->pending_list);
ret = cdnsp_ep0_delegate_req(pdev, ctrl);
if (ret == -EBUSY)
ret = 0;
list_del(&pdev->ep0_preq.list);
break;
default:
ret = cdnsp_ep0_delegate_req(pdev, ctrl);
break;
}
return ret;
}
void cdnsp_setup_analyze(struct cdnsp_device *pdev)
{
struct usb_ctrlrequest *ctrl = &pdev->setup;
int ret = 0;
__le16 len;
if (!pdev->gadget_driver)
goto out;
if (pdev->gadget.state == USB_STATE_NOTATTACHED) {
dev_err(pdev->dev, "ERR: Setup detected in unattached state\n");
ret = -EINVAL;
goto out;
}
/* Restore the ep0 to Stopped/Running state. */
if (pdev->eps[0].ep_state & EP_HALTED)
cdnsp_halt_endpoint(pdev, &pdev->eps[0], 0);
/*
* Finishing previous SETUP transfer by removing request from
* list and informing upper layer
*/
if (!list_empty(&pdev->eps[0].pending_list)) {
struct cdnsp_request *req;
req = next_request(&pdev->eps[0].pending_list);
cdnsp_ep_dequeue(&pdev->eps[0], req);
}
len = le16_to_cpu(ctrl->wLength);
if (!len) {
pdev->three_stage_setup = false;
pdev->ep0_expect_in = false;
} else {
pdev->three_stage_setup = true;
pdev->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
}
if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
ret = cdnsp_ep0_std_request(pdev, ctrl);
else
ret = cdnsp_ep0_delegate_req(pdev, ctrl);
if (!len)
pdev->ep0_stage = CDNSP_STATUS_STAGE;
if (ret == USB_GADGET_DELAYED_STATUS)
return;
out:
if (ret < 0)
cdnsp_ep0_stall(pdev);
else if (pdev->ep0_stage == CDNSP_STATUS_STAGE)
cdnsp_status_stage(pdev);
}

File diff suppressed because it is too large Load Diff

View File

@ -1460,4 +1460,141 @@ struct cdnsp_device {
u16 test_mode;
};
/*
* Registers should always be accessed with double word or quad word accesses.
*
* Registers with 64-bit address pointers should be written to with
* dword accesses by writing the low dword first (ptr[0]), then the high dword
* (ptr[1]) second. controller implementations that do not support 64-bit
* address pointers will ignore the high dword, and write order is irrelevant.
*/
static inline u64 cdnsp_read_64(__le64 __iomem *regs)
{
return lo_hi_readq(regs);
}
static inline void cdnsp_write_64(const u64 val, __le64 __iomem *regs)
{
lo_hi_writeq(val, regs);
}
/* CDNSP memory management functions. */
void cdnsp_mem_cleanup(struct cdnsp_device *pdev);
int cdnsp_mem_init(struct cdnsp_device *pdev, gfp_t flags);
int cdnsp_setup_addressable_priv_dev(struct cdnsp_device *pdev);
void cdnsp_copy_ep0_dequeue_into_input_ctx(struct cdnsp_device *pdev);
void cdnsp_endpoint_zero(struct cdnsp_device *pdev, struct cdnsp_ep *ep);
int cdnsp_endpoint_init(struct cdnsp_device *pdev,
struct cdnsp_ep *pep,
gfp_t mem_flags);
int cdnsp_ring_expansion(struct cdnsp_device *pdev,
struct cdnsp_ring *ring,
unsigned int num_trbs, gfp_t flags);
struct cdnsp_ring *cdnsp_dma_to_transfer_ring(struct cdnsp_ep *ep, u64 address);
int cdnsp_alloc_stream_info(struct cdnsp_device *pdev,
struct cdnsp_ep *pep,
unsigned int num_stream_ctxs,
unsigned int num_streams);
int cdnsp_alloc_streams(struct cdnsp_device *pdev, struct cdnsp_ep *pep);
void cdnsp_free_endpoint_rings(struct cdnsp_device *pdev, struct cdnsp_ep *pep);
/* Device controller glue. */
int cdnsp_find_next_ext_cap(void __iomem *base, u32 start, int id);
int cdnsp_halt(struct cdnsp_device *pdev);
void cdnsp_died(struct cdnsp_device *pdev);
int cdnsp_reset(struct cdnsp_device *pdev);
irqreturn_t cdnsp_irq_handler(int irq, void *priv);
int cdnsp_setup_device(struct cdnsp_device *pdev, enum cdnsp_setup_dev setup);
void cdnsp_set_usb2_hardware_lpm(struct cdnsp_device *usbsssp_data,
struct usb_request *req, int enable);
irqreturn_t cdnsp_thread_irq_handler(int irq, void *data);
/* Ring, segment, TRB, and TD functions. */
dma_addr_t cdnsp_trb_virt_to_dma(struct cdnsp_segment *seg,
union cdnsp_trb *trb);
bool cdnsp_last_trb_on_seg(struct cdnsp_segment *seg, union cdnsp_trb *trb);
bool cdnsp_last_trb_on_ring(struct cdnsp_ring *ring,
struct cdnsp_segment *seg,
union cdnsp_trb *trb);
int cdnsp_wait_for_cmd_compl(struct cdnsp_device *pdev);
void cdnsp_update_erst_dequeue(struct cdnsp_device *pdev,
union cdnsp_trb *event_ring_deq,
u8 clear_ehb);
void cdnsp_initialize_ring_info(struct cdnsp_ring *ring);
void cdnsp_ring_cmd_db(struct cdnsp_device *pdev);
void cdnsp_queue_slot_control(struct cdnsp_device *pdev, u32 trb_type);
void cdnsp_queue_address_device(struct cdnsp_device *pdev,
dma_addr_t in_ctx_ptr,
enum cdnsp_setup_dev setup);
void cdnsp_queue_stop_endpoint(struct cdnsp_device *pdev,
unsigned int ep_index);
int cdnsp_queue_ctrl_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq);
int cdnsp_queue_bulk_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq);
int cdnsp_queue_isoc_tx_prepare(struct cdnsp_device *pdev,
struct cdnsp_request *preq);
void cdnsp_queue_configure_endpoint(struct cdnsp_device *pdev,
dma_addr_t in_ctx_ptr);
void cdnsp_queue_reset_ep(struct cdnsp_device *pdev, unsigned int ep_index);
void cdnsp_queue_halt_endpoint(struct cdnsp_device *pdev,
unsigned int ep_index);
void cdnsp_queue_flush_endpoint(struct cdnsp_device *pdev,
unsigned int ep_index);
void cdnsp_force_header_wakeup(struct cdnsp_device *pdev, int intf_num);
void cdnsp_queue_reset_device(struct cdnsp_device *pdev);
void cdnsp_queue_new_dequeue_state(struct cdnsp_device *pdev,
struct cdnsp_ep *pep,
struct cdnsp_dequeue_state *deq_state);
void cdnsp_ring_doorbell_for_active_rings(struct cdnsp_device *pdev,
struct cdnsp_ep *pep);
void cdnsp_inc_deq(struct cdnsp_device *pdev, struct cdnsp_ring *ring);
void cdnsp_set_link_state(struct cdnsp_device *pdev,
__le32 __iomem *port_regs, u32 link_state);
u32 cdnsp_port_state_to_neutral(u32 state);
/* CDNSP device controller contexts. */
int cdnsp_enable_slot(struct cdnsp_device *pdev);
int cdnsp_disable_slot(struct cdnsp_device *pdev);
struct cdnsp_input_control_ctx
*cdnsp_get_input_control_ctx(struct cdnsp_container_ctx *ctx);
struct cdnsp_slot_ctx *cdnsp_get_slot_ctx(struct cdnsp_container_ctx *ctx);
struct cdnsp_ep_ctx *cdnsp_get_ep_ctx(struct cdnsp_container_ctx *ctx,
unsigned int ep_index);
/* CDNSP gadget interface. */
void cdnsp_suspend_gadget(struct cdnsp_device *pdev);
void cdnsp_resume_gadget(struct cdnsp_device *pdev);
void cdnsp_disconnect_gadget(struct cdnsp_device *pdev);
void cdnsp_gadget_giveback(struct cdnsp_ep *pep, struct cdnsp_request *preq,
int status);
int cdnsp_ep_enqueue(struct cdnsp_ep *pep, struct cdnsp_request *preq);
int cdnsp_ep_dequeue(struct cdnsp_ep *pep, struct cdnsp_request *preq);
unsigned int cdnsp_port_speed(unsigned int port_status);
void cdnsp_irq_reset(struct cdnsp_device *pdev);
int cdnsp_halt_endpoint(struct cdnsp_device *pdev,
struct cdnsp_ep *pep, int value);
int cdnsp_cmd_stop_ep(struct cdnsp_device *pdev, struct cdnsp_ep *pep);
int cdnsp_cmd_flush_ep(struct cdnsp_device *pdev, struct cdnsp_ep *pep);
void cdnsp_setup_analyze(struct cdnsp_device *pdev);
int cdnsp_status_stage(struct cdnsp_device *pdev);
int cdnsp_reset_device(struct cdnsp_device *pdev);
/**
* next_request - gets the next request on the given list
* @list: the request list to operate on
*
* Caller should take care of locking. This function return NULL or the first
* request available on list.
*/
static inline struct cdnsp_request *next_request(struct list_head *list)
{
return list_first_entry_or_null(list, struct cdnsp_request, list);
}
#define to_cdnsp_ep(ep) (container_of(ep, struct cdnsp_ep, endpoint))
#define gadget_to_cdnsp(g) (container_of(g, struct cdnsp_device, gadget))
#define request_to_cdnsp_request(r) (container_of(r, struct cdnsp_request, \
request))
#define to_cdnsp_request(r) (container_of(r, struct cdnsp_request, request))
int cdnsp_remove_request(struct cdnsp_device *pdev, struct cdnsp_request *preq,
struct cdnsp_ep *pep);
#endif /* __LINUX_CDNSP_GADGET_H */

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,254 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Cadence PCI Glue driver.
*
* Copyright (C) 2019 Cadence.
*
* Author: Pawel Laszczak <pawell@cadence.com>
*
*/
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include "core.h"
#include "gadget-export.h"
#define PCI_BAR_HOST 0
#define PCI_BAR_OTG 0
#define PCI_BAR_DEV 2
#define PCI_DEV_FN_HOST_DEVICE 0
#define PCI_DEV_FN_OTG 1
#define PCI_DRIVER_NAME "cdns-pci-usbssp"
#define PLAT_DRIVER_NAME "cdns-usbssp"
#define CDNS_VENDOR_ID 0x17cd
#define CDNS_DEVICE_ID 0x0100
#define CDNS_DRD_IF (PCI_CLASS_SERIAL_USB << 8 | 0x80)
static struct pci_dev *cdnsp_get_second_fun(struct pci_dev *pdev)
{
struct pci_dev *func;
/*
* Gets the second function.
* It's little tricky, but this platform has two function.
* The fist keeps resources for Host/Device while the second
* keeps resources for DRD/OTG.
*/
func = pci_get_device(pdev->vendor, pdev->device, NULL);
if (!func)
return NULL;
if (func->devfn == pdev->devfn) {
func = pci_get_device(pdev->vendor, pdev->device, func);
if (!func)
return NULL;
}
return func;
}
static int cdnsp_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *id)
{
struct device *dev = &pdev->dev;
struct pci_dev *func;
struct resource *res;
struct cdns *cdnsp;
int ret;
/*
* For GADGET/HOST PCI (devfn) function number is 0,
* for OTG PCI (devfn) function number is 1.
*/
if (!id || (pdev->devfn != PCI_DEV_FN_HOST_DEVICE &&
pdev->devfn != PCI_DEV_FN_OTG))
return -EINVAL;
func = cdnsp_get_second_fun(pdev);
if (!func)
return -EINVAL;
if (func->class == PCI_CLASS_SERIAL_USB_XHCI ||
pdev->class == PCI_CLASS_SERIAL_USB_XHCI) {
ret = -EINVAL;
goto put_pci;
}
ret = pcim_enable_device(pdev);
if (ret) {
dev_err(&pdev->dev, "Enabling PCI device has failed %d\n", ret);
goto put_pci;
}
pci_set_master(pdev);
if (pci_is_enabled(func)) {
cdnsp = pci_get_drvdata(func);
} else {
cdnsp = kzalloc(sizeof(*cdnsp), GFP_KERNEL);
if (!cdnsp) {
ret = -ENOMEM;
goto disable_pci;
}
}
/* For GADGET device function number is 0. */
if (pdev->devfn == 0) {
resource_size_t rsrc_start, rsrc_len;
/* Function 0: host(BAR_0) + device(BAR_1).*/
dev_dbg(dev, "Initialize resources\n");
rsrc_start = pci_resource_start(pdev, PCI_BAR_DEV);
rsrc_len = pci_resource_len(pdev, PCI_BAR_DEV);
res = devm_request_mem_region(dev, rsrc_start, rsrc_len, "dev");
if (!res) {
dev_dbg(dev, "controller already in use\n");
ret = -EBUSY;
goto free_cdnsp;
}
cdnsp->dev_regs = devm_ioremap(dev, rsrc_start, rsrc_len);
if (!cdnsp->dev_regs) {
dev_dbg(dev, "error mapping memory\n");
ret = -EFAULT;
goto free_cdnsp;
}
cdnsp->dev_irq = pdev->irq;
dev_dbg(dev, "USBSS-DEV physical base addr: %pa\n",
&rsrc_start);
res = &cdnsp->xhci_res[0];
res->start = pci_resource_start(pdev, PCI_BAR_HOST);
res->end = pci_resource_end(pdev, PCI_BAR_HOST);
res->name = "xhci";
res->flags = IORESOURCE_MEM;
dev_dbg(dev, "USBSS-XHCI physical base addr: %pa\n",
&res->start);
/* Interrupt for XHCI, */
res = &cdnsp->xhci_res[1];
res->start = pdev->irq;
res->name = "host";
res->flags = IORESOURCE_IRQ;
} else {
res = &cdnsp->otg_res;
res->start = pci_resource_start(pdev, PCI_BAR_OTG);
res->end = pci_resource_end(pdev, PCI_BAR_OTG);
res->name = "otg";
res->flags = IORESOURCE_MEM;
dev_dbg(dev, "CDNSP-DRD physical base addr: %pa\n",
&res->start);
/* Interrupt for OTG/DRD. */
cdnsp->otg_irq = pdev->irq;
}
if (pci_is_enabled(func)) {
cdnsp->dev = dev;
cdnsp->gadget_init = cdnsp_gadget_init;
ret = cdns_init(cdnsp);
if (ret)
goto free_cdnsp;
}
pci_set_drvdata(pdev, cdnsp);
device_wakeup_enable(&pdev->dev);
if (pci_dev_run_wake(pdev))
pm_runtime_put_noidle(&pdev->dev);
return 0;
free_cdnsp:
if (!pci_is_enabled(func))
kfree(cdnsp);
disable_pci:
pci_disable_device(pdev);
put_pci:
pci_dev_put(func);
return ret;
}
static void cdnsp_pci_remove(struct pci_dev *pdev)
{
struct cdns *cdnsp;
struct pci_dev *func;
func = cdnsp_get_second_fun(pdev);
cdnsp = (struct cdns *)pci_get_drvdata(pdev);
if (pci_dev_run_wake(pdev))
pm_runtime_get_noresume(&pdev->dev);
if (!pci_is_enabled(func)) {
kfree(cdnsp);
goto pci_put;
}
cdns_remove(cdnsp);
pci_put:
pci_dev_put(func);
}
static int __maybe_unused cdnsp_pci_suspend(struct device *dev)
{
struct cdns *cdns = dev_get_drvdata(dev);
return cdns_suspend(cdns);
}
static int __maybe_unused cdnsp_pci_resume(struct device *dev)
{
struct cdns *cdns = dev_get_drvdata(dev);
unsigned long flags;
int ret;
spin_lock_irqsave(&cdns->lock, flags);
ret = cdns_resume(cdns, 1);
spin_unlock_irqrestore(&cdns->lock, flags);
return ret;
}
static const struct dev_pm_ops cdnsp_pci_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(cdnsp_pci_suspend, cdnsp_pci_resume)
};
static const struct pci_device_id cdnsp_pci_ids[] = {
{ PCI_VENDOR_ID_CDNS, CDNS_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,
PCI_CLASS_SERIAL_USB_DEVICE, PCI_ANY_ID },
{ PCI_VENDOR_ID_CDNS, CDNS_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,
CDNS_DRD_IF, PCI_ANY_ID },
{ 0, }
};
static struct pci_driver cdnsp_pci_driver = {
.name = "cdnsp-pci",
.id_table = &cdnsp_pci_ids[0],
.probe = cdnsp_pci_probe,
.remove = cdnsp_pci_remove,
.driver = {
.pm = &cdnsp_pci_pm_ops,
}
};
module_pci_driver(cdnsp_pci_driver);
MODULE_DEVICE_TABLE(pci, cdnsp_pci_ids);
MODULE_ALIAS("pci:cdnsp");
MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Cadence CDNSP PCI driver");

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Cadence USBSS DRD Driver.
* Cadence USBSS and USBSSP DRD Driver.
*
* Copyright (C) 2018-2019 Cadence.
* Copyright (C) 2017-2018 NXP
@ -136,7 +136,14 @@ static int cdns_core_init_role(struct cdns *cdns)
dr_mode = best_dr_mode;
if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
ret = cdns_host_init(cdns);
if ((cdns->version == CDNSP_CONTROLLER_V2 &&
IS_ENABLED(CONFIG_USB_CDNSP_HOST)) ||
(cdns->version < CDNSP_CONTROLLER_V2 &&
IS_ENABLED(CONFIG_USB_CDNS3_HOST)))
ret = cdns_host_init(cdns);
else
ret = -ENXIO;
if (ret) {
dev_err(dev, "Host initialization failed with %d\n",
ret);

View File

@ -1,6 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Cadence USBSS DRD Header File.
* Cadence USBSS and USBSSP DRD Header File.
*
* Copyright (C) 2017-2018 NXP
* Copyright (C) 2018-2019 Cadence.

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Cadence USBSS DRD Driver.
* Cadence USBSS and USBSSP DRD Driver.
*
* Copyright (C) 2018-2020 Cadence.
* Copyright (C) 2019 Texas Instruments
@ -103,6 +103,32 @@ int cdns_get_vbus(struct cdns *cdns)
return vbus;
}
void cdns_clear_vbus(struct cdns *cdns)
{
u32 reg;
if (cdns->version != CDNSP_CONTROLLER_V2)
return;
reg = readl(&cdns->otg_cdnsp_regs->override);
reg |= OVERRIDE_SESS_VLD_SEL;
writel(reg, &cdns->otg_cdnsp_regs->override);
}
EXPORT_SYMBOL_GPL(cdns_clear_vbus);
void cdns_set_vbus(struct cdns *cdns)
{
u32 reg;
if (cdns->version != CDNSP_CONTROLLER_V2)
return;
reg = readl(&cdns->otg_cdnsp_regs->override);
reg &= ~OVERRIDE_SESS_VLD_SEL;
writel(reg, &cdns->otg_cdnsp_regs->override);
}
EXPORT_SYMBOL_GPL(cdns_set_vbus);
bool cdns_is_host(struct cdns *cdns)
{
if (cdns->dr_mode == USB_DR_MODE_HOST)
@ -449,5 +475,6 @@ int cdns_drd_init(struct cdns *cdns)
int cdns_drd_exit(struct cdns *cdns)
{
cdns_otg_disable_irq(cdns);
return 0;
}

View File

@ -206,6 +206,8 @@ bool cdns_is_host(struct cdns *cdns);
bool cdns_is_device(struct cdns *cdns);
int cdns_get_id(struct cdns *cdns);
int cdns_get_vbus(struct cdns *cdns);
void cdns_clear_vbus(struct cdns *cdns);
void cdns_set_vbus(struct cdns *cdns);
int cdns_drd_init(struct cdns *cdns);
int cdns_drd_exit(struct cdns *cdns);
int cdns_drd_update_mode(struct cdns *cdns);

View File

@ -1,6 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Cadence USBSS DRD Driver - Gadget Export APIs.
* Cadence USBSS and USBSSP DRD Driver - Gadget Export APIs.
*
* Copyright (C) 2017 NXP
* Copyright (C) 2017-2018 NXP
@ -10,7 +10,19 @@
#ifndef __LINUX_CDNS3_GADGET_EXPORT
#define __LINUX_CDNS3_GADGET_EXPORT
#ifdef CONFIG_USB_CDNS3_GADGET
#if IS_ENABLED(CONFIG_USB_CDNSP_GADGET)
int cdnsp_gadget_init(struct cdns *cdns);
#else
static inline int cdnsp_gadget_init(struct cdns *cdns)
{
return -ENXIO;
}
#endif /* CONFIG_USB_CDNSP_GADGET */
#if IS_ENABLED(CONFIG_USB_CDNS3_GADGET)
int cdns3_gadget_init(struct cdns *cdns);
#else
@ -20,6 +32,6 @@ static inline int cdns3_gadget_init(struct cdns *cdns)
return -ENXIO;
}
#endif
#endif /* CONFIG_USB_CDNS3_GADGET */
#endif /* __LINUX_CDNS3_GADGET_EXPORT */

View File

@ -1,6 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Cadence USBSS DRD Driver - Host Export APIs
* Cadence USBSS and USBSSP DRD Driver - Host Export APIs
*
* Copyright (C) 2017-2018 NXP
*
@ -9,8 +9,9 @@
#ifndef __LINUX_CDNS3_HOST_EXPORT
#define __LINUX_CDNS3_HOST_EXPORT
#if IS_ENABLED(CONFIG_USB_CDNS_HOST)
struct usb_hcd;
#ifdef CONFIG_USB_CDNS3_HOST
int cdns_host_init(struct cdns *cdns);
int xhci_cdns3_suspend_quirk(struct usb_hcd *hcd);
@ -28,6 +29,6 @@ static inline int xhci_cdns3_suspend_quirk(struct usb_hcd *hcd)
return 0;
}
#endif /* CONFIG_USB_CDNS3_HOST */
#endif /* USB_CDNS_HOST */
#endif /* __LINUX_CDNS3_HOST_EXPORT */