Home | History | Annotate | Line # | Download | only in dwc2
dwc2.h revision 1.6
      1 /*	$NetBSD: dwc2.h,v 1.6 2015/08/30 12:26:29 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nick Hudson
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _EXTERNAL_BSD_DWC2_DWC2_H_
     33 #define _EXTERNAL_BSD_DWC2_DWC2_H_
     34 
     35 #include <sys/param.h>
     36 
     37 #include <sys/callout.h>
     38 #include <sys/kernel.h>
     39 #include <sys/workqueue.h>
     40 
     41 #include <linux/list.h>
     42 
     43 #include "opt_usb.h"
     44 // #define VERBOSE_DEBUG
     45 // #define DWC2_DUMP_FRREM
     46 // #define CONFIG_USB_DWC2_TRACK_MISSED_SOFS
     47 
     48 typedef int irqreturn_t;
     49 #define	IRQ_NONE 0
     50 #define IRQ_HANDLED 1
     51 
     52 #define	u8	uint8_t
     53 #define	u16	uint16_t
     54 #define	s16	int16_t
     55 #define	u32	uint32_t
     56 #define	u64	uint64_t
     57 
     58 #define	dma_addr_t	bus_addr_t
     59 
     60 #define DWC2_READ_4(hsotg, reg) \
     61     bus_space_read_4((hsotg)->hsotg_sc->sc_iot, (hsotg)->hsotg_sc->sc_ioh, (reg))
     62 #define DWC2_WRITE_4(hsotg, reg, data)  \
     63     bus_space_write_4((hsotg)->hsotg_sc->sc_iot, (hsotg)->hsotg_sc->sc_ioh, (reg), (data));
     64 
     65 #ifdef DWC2_DEBUG
     66 extern int dwc2debug;
     67 #define WARN_ON(x)	KASSERT(!(x))
     68 
     69 #define	dev_info(d,fmt,...) do {			\
     70 	printf("%s: " fmt, device_xname(d), 		\
     71 	    ## __VA_ARGS__);				\
     72 } while (0)
     73 #define	dev_warn(d,fmt,...) do {			\
     74 	printf("%s: " fmt, device_xname(d), 		\
     75 	    ## __VA_ARGS__);				\
     76 } while (0)
     77 #define	dev_err(d,fmt,...) do {				\
     78 	printf("%s: " fmt, device_xname(d), 		\
     79 	    ## __VA_ARGS__);				\
     80 } while (0)
     81 #define	dev_dbg(d,fmt,...) do {				\
     82 	if (dwc2debug >= 1) {				\
     83 	    printf("%s: " fmt, device_xname(d), 	\
     84 		    ## __VA_ARGS__);			\
     85 	}						\
     86 } while (0)
     87 #define	dev_vdbg(d,fmt,...) do {			\
     88 	if (dwc2debug >= 2) {				\
     89 	    printf("%s: " fmt, device_xname(d), 	\
     90 		    ## __VA_ARGS__);			\
     91 	}						\
     92 } while (0)
     93 #else
     94 #define WARN_ON(x)
     95 #define	dev_info(...) do { } while (0)
     96 #define	dev_warn(...) do { } while (0)
     97 #define	dev_err(...) do { } while (0)
     98 #define	dev_dbg(...) do { } while (0)
     99 #define	dev_vdbg(...) do { } while (0)
    100 #endif
    101 
    102 #define jiffies			hardclock_ticks
    103 #define msecs_to_jiffies	mstohz
    104 
    105 #define gfp_t		int
    106 #define GFP_KERNEL	 KM_SLEEP
    107 #define GFP_ATOMIC	 KM_NOSLEEP
    108 
    109 enum usb_otg_state {
    110 	OTG_STATE_RESERVED = 0,
    111 
    112 	OTG_STATE_A_HOST,
    113 	OTG_STATE_A_PERIPHERAL,
    114 	OTG_STATE_A_SUSPEND,
    115 	OTG_STATE_B_HOST,
    116 	OTG_STATE_B_PERIPHERAL,
    117 };
    118 
    119 #define usleep_range(l, u)	do { DELAY(u); } while (0)
    120 
    121 #define spinlock_t		kmutex_t
    122 #define spin_lock_init(lock)	mutex_init(lock, MUTEX_DEFAULT, IPL_VM)
    123 #define	spin_lock(l)		do { mutex_spin_enter(l); } while (0)
    124 #define	spin_unlock(l)		do { mutex_spin_exit(l); } while (0)
    125 
    126 #define	spin_lock_irqsave(l, f)		\
    127 	do { mutex_spin_enter(l); (void)(f); } while (0)
    128 
    129 #define	spin_unlock_irqrestore(l, f)	\
    130 	do { mutex_spin_exit(l); (void)(f); } while (0)
    131 
    132 #define	IRQ_RETVAL(r)	(r)
    133 
    134 #define	USB_ENDPOINT_XFER_CONTROL	UE_CONTROL		/* 0 */
    135 #define	USB_ENDPOINT_XFER_ISOC		UE_ISOCHRONOUS		/* 1 */
    136 #define	USB_ENDPOINT_XFER_BULK		UE_BULK			/* 2 */
    137 #define	USB_ENDPOINT_XFER_INT		UE_INTERRUPT		/* 3 */
    138 
    139 #define USB_DIR_IN			UE_DIR_IN
    140 #define USB_DIR_OUT			UE_DIR_OUT
    141 
    142 #define	USB_PORT_FEAT_CONNECTION	UHF_PORT_CONNECTION
    143 #define	USB_PORT_FEAT_ENABLE		UHF_PORT_ENABLE
    144 #define	USB_PORT_FEAT_SUSPEND		UHF_PORT_SUSPEND
    145 #define	USB_PORT_FEAT_OVER_CURRENT	UHF_PORT_OVER_CURRENT
    146 #define	USB_PORT_FEAT_RESET		UHF_PORT_RESET
    147 // #define	USB_PORT_FEAT_L1		5	/* L1 suspend */
    148 #define	USB_PORT_FEAT_POWER		UHF_PORT_POWER
    149 #define	USB_PORT_FEAT_LOWSPEED		UHF_PORT_LOW_SPEED
    150 #define	USB_PORT_FEAT_C_CONNECTION	UHF_C_PORT_CONNECTION
    151 #define	USB_PORT_FEAT_C_ENABLE		UHF_C_PORT_ENABLE
    152 #define	USB_PORT_FEAT_C_SUSPEND		UHF_C_PORT_SUSPEND
    153 #define	USB_PORT_FEAT_C_OVER_CURRENT	UHF_C_PORT_OVER_CURRENT
    154 #define	USB_PORT_FEAT_C_RESET		UHF_C_PORT_RESET
    155 #define	USB_PORT_FEAT_TEST              UHF_PORT_TEST
    156 #define	USB_PORT_FEAT_INDICATOR         UHF_PORT_INDICATOR
    157 #define	USB_PORT_FEAT_C_PORT_L1         UHF_C_PORT_L1
    158 
    159 #define	C_HUB_LOCAL_POWER		UHF_C_HUB_LOCAL_POWER
    160 #define	C_HUB_OVER_CURRENT		UHF_C_HUB_OVER_CURRENT
    161 
    162 #define USB_REQ_GET_STATUS		UR_GET_STATUS
    163 #define USB_REQ_CLEAR_FEATURE		UR_CLEAR_FEATURE
    164 #define USB_REQ_SET_FEATURE		UR_SET_FEATURE
    165 #define USB_REQ_GET_DESCRIPTOR		UR_GET_DESCRIPTOR
    166 
    167 #define	ClearHubFeature		((UT_WRITE_CLASS_DEVICE << 8) | USB_REQ_CLEAR_FEATURE)
    168 #define	ClearPortFeature	((UT_WRITE_CLASS_OTHER << 8) | USB_REQ_CLEAR_FEATURE)
    169 #define	GetHubDescriptor	((UT_READ_CLASS_DEVICE << 8) | USB_REQ_GET_DESCRIPTOR)
    170 #define	GetHubStatus		((UT_READ_CLASS_DEVICE << 8) | USB_REQ_GET_STATUS)
    171 #define	GetPortStatus		((UT_READ_CLASS_OTHER << 8) | USB_REQ_GET_STATUS)
    172 #define	SetHubFeature		((UT_WRITE_CLASS_DEVICE << 8) | USB_REQ_SET_FEATURE)
    173 #define	SetPortFeature		((UT_WRITE_CLASS_OTHER << 8) | USB_REQ_SET_FEATURE)
    174 
    175 #define	USB_PORT_STAT_CONNECTION	UPS_CURRENT_CONNECT_STATUS
    176 #define	USB_PORT_STAT_ENABLE		UPS_PORT_ENABLED
    177 #define	USB_PORT_STAT_SUSPEND		UPS_SUSPEND
    178 #define	USB_PORT_STAT_OVERCURRENT	UPS_OVERCURRENT_INDICATOR
    179 #define	USB_PORT_STAT_RESET		UPS_RESET
    180 #define	USB_PORT_STAT_L1		UPS_PORT_L1
    181 #define	USB_PORT_STAT_POWER		UPS_PORT_POWER
    182 #define	USB_PORT_STAT_LOW_SPEED		UPS_LOW_SPEED
    183 #define	USB_PORT_STAT_HIGH_SPEED        UPS_HIGH_SPEED
    184 #define	USB_PORT_STAT_TEST              UPS_PORT_TEST
    185 #define	USB_PORT_STAT_INDICATOR         UPS_PORT_INDICATOR
    186 
    187 #define	USB_PORT_STAT_C_CONNECTION	UPS_C_CONNECT_STATUS
    188 #define	USB_PORT_STAT_C_ENABLE		UPS_C_PORT_ENABLED
    189 #define	USB_PORT_STAT_C_SUSPEND		UPS_C_SUSPEND
    190 #define	USB_PORT_STAT_C_OVERCURRENT	UPS_C_OVERCURRENT_INDICATOR
    191 #define	USB_PORT_STAT_C_RESET		UPS_C_PORT_RESET
    192 #define	USB_PORT_STAT_C_L1		UPS_C_PORT_L1
    193 
    194 static inline void
    195 udelay(unsigned long usecs)
    196 {
    197 	DELAY(usecs);
    198 }
    199 
    200 #define	EREMOTEIO	EIO
    201 #define	ECOMM		EIO
    202 
    203 #define NS_TO_US(ns)	((ns + 500L) / 1000L)
    204 
    205 void dw_callout(void *);
    206 void dwc2_worker(struct work *, void *);
    207 
    208 struct delayed_work {
    209 	struct work work;
    210 	struct callout dw_timer;
    211 
    212 	struct workqueue *dw_wq;
    213 };
    214 
    215 static inline void
    216 INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(struct work *))
    217 {
    218 	callout_init(&dw->dw_timer, CALLOUT_MPSAFE);
    219 }
    220 
    221 static inline void
    222 queue_delayed_work(struct workqueue *wq, struct delayed_work *dw, int j)
    223 {
    224 	callout_reset(&dw->dw_timer, j, dw_callout, dw);
    225 }
    226 
    227 #endif
    228