Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __LINUX_UHCI_HCD_H
0003 #define __LINUX_UHCI_HCD_H
0004 
0005 #include <linux/list.h>
0006 #include <linux/usb.h>
0007 #include <linux/clk.h>
0008 
0009 #define usb_packetid(pipe)  (usb_pipein(pipe) ? USB_PID_IN : USB_PID_OUT)
0010 #define PIPE_DEVEP_MASK     0x0007ff00
0011 
0012 
0013 /*
0014  * Universal Host Controller Interface data structures and defines
0015  */
0016 
0017 /* Command register */
0018 #define USBCMD      0
0019 #define   USBCMD_RS     0x0001  /* Run/Stop */
0020 #define   USBCMD_HCRESET    0x0002  /* Host reset */
0021 #define   USBCMD_GRESET     0x0004  /* Global reset */
0022 #define   USBCMD_EGSM       0x0008  /* Global Suspend Mode */
0023 #define   USBCMD_FGR        0x0010  /* Force Global Resume */
0024 #define   USBCMD_SWDBG      0x0020  /* SW Debug mode */
0025 #define   USBCMD_CF     0x0040  /* Config Flag (sw only) */
0026 #define   USBCMD_MAXP       0x0080  /* Max Packet (0 = 32, 1 = 64) */
0027 
0028 /* Status register */
0029 #define USBSTS      2
0030 #define   USBSTS_USBINT     0x0001  /* Interrupt due to IOC */
0031 #define   USBSTS_ERROR      0x0002  /* Interrupt due to error */
0032 #define   USBSTS_RD     0x0004  /* Resume Detect */
0033 #define   USBSTS_HSE        0x0008  /* Host System Error: PCI problems */
0034 #define   USBSTS_HCPE       0x0010  /* Host Controller Process Error:
0035                      * the schedule is buggy */
0036 #define   USBSTS_HCH        0x0020  /* HC Halted */
0037 
0038 /* Interrupt enable register */
0039 #define USBINTR     4
0040 #define   USBINTR_TIMEOUT   0x0001  /* Timeout/CRC error enable */
0041 #define   USBINTR_RESUME    0x0002  /* Resume interrupt enable */
0042 #define   USBINTR_IOC       0x0004  /* Interrupt On Complete enable */
0043 #define   USBINTR_SP        0x0008  /* Short packet interrupt enable */
0044 
0045 #define USBFRNUM    6
0046 #define USBFLBASEADD    8
0047 #define USBSOF      12
0048 #define   USBSOF_DEFAULT    64  /* Frame length is exactly 1 ms */
0049 
0050 /* USB port status and control registers */
0051 #define USBPORTSC1  16
0052 #define USBPORTSC2  18
0053 #define USBPORTSC3  20
0054 #define USBPORTSC4  22
0055 #define   USBPORTSC_CCS     0x0001  /* Current Connect Status
0056                      * ("device present") */
0057 #define   USBPORTSC_CSC     0x0002  /* Connect Status Change */
0058 #define   USBPORTSC_PE      0x0004  /* Port Enable */
0059 #define   USBPORTSC_PEC     0x0008  /* Port Enable Change */
0060 #define   USBPORTSC_DPLUS   0x0010  /* D+ high (line status) */
0061 #define   USBPORTSC_DMINUS  0x0020  /* D- high (line status) */
0062 #define   USBPORTSC_RD      0x0040  /* Resume Detect */
0063 #define   USBPORTSC_RES1    0x0080  /* reserved, always 1 */
0064 #define   USBPORTSC_LSDA    0x0100  /* Low Speed Device Attached */
0065 #define   USBPORTSC_PR      0x0200  /* Port Reset */
0066 /* OC and OCC from Intel 430TX and later (not UHCI 1.1d spec) */
0067 #define   USBPORTSC_OC      0x0400  /* Over Current condition */
0068 #define   USBPORTSC_OCC     0x0800  /* Over Current Change R/WC */
0069 #define   USBPORTSC_SUSP    0x1000  /* Suspend */
0070 #define   USBPORTSC_RES2    0x2000  /* reserved, write zeroes */
0071 #define   USBPORTSC_RES3    0x4000  /* reserved, write zeroes */
0072 #define   USBPORTSC_RES4    0x8000  /* reserved, write zeroes */
0073 
0074 /* PCI legacy support register */
0075 #define USBLEGSUP       0xc0
0076 #define   USBLEGSUP_DEFAULT 0x2000  /* only PIRQ enable set */
0077 #define   USBLEGSUP_RWC     0x8f00  /* the R/WC bits */
0078 #define   USBLEGSUP_RO      0x5040  /* R/O and reserved bits */
0079 
0080 /* PCI Intel-specific resume-enable register */
0081 #define USBRES_INTEL        0xc4
0082 #define   USBPORT1EN        0x01
0083 #define   USBPORT2EN        0x02
0084 
0085 #define UHCI_PTR_BITS(uhci) cpu_to_hc32((uhci), 0x000F)
0086 #define UHCI_PTR_TERM(uhci) cpu_to_hc32((uhci), 0x0001)
0087 #define UHCI_PTR_QH(uhci)   cpu_to_hc32((uhci), 0x0002)
0088 #define UHCI_PTR_DEPTH(uhci)    cpu_to_hc32((uhci), 0x0004)
0089 #define UHCI_PTR_BREADTH(uhci)  cpu_to_hc32((uhci), 0x0000)
0090 
0091 #define UHCI_NUMFRAMES      1024    /* in the frame list [array] */
0092 #define UHCI_MAX_SOF_NUMBER 2047    /* in an SOF packet */
0093 #define CAN_SCHEDULE_FRAMES 1000    /* how far in the future frames
0094                      * can be scheduled */
0095 #define MAX_PHASE       32  /* Periodic scheduling length */
0096 
0097 /* When no queues need Full-Speed Bandwidth Reclamation,
0098  * delay this long before turning FSBR off */
0099 #define FSBR_OFF_DELAY      msecs_to_jiffies(10)
0100 
0101 /* If a queue hasn't advanced after this much time, assume it is stuck */
0102 #define QH_WAIT_TIMEOUT     msecs_to_jiffies(200)
0103 
0104 
0105 /*
0106  * __hc32 and __hc16 are "Host Controller" types, they may be equivalent to
0107  * __leXX (normally) or __beXX (given UHCI_BIG_ENDIAN_DESC), depending on
0108  * the host controller implementation.
0109  *
0110  * To facilitate the strongest possible byte-order checking from "sparse"
0111  * and so on, we use __leXX unless that's not practical.
0112  */
0113 #ifdef CONFIG_USB_UHCI_BIG_ENDIAN_DESC
0114 typedef __u32 __bitwise __hc32;
0115 typedef __u16 __bitwise __hc16;
0116 #else
0117 #define __hc32  __le32
0118 #define __hc16  __le16
0119 #endif
0120 
0121 /*
0122  *  Queue Headers
0123  */
0124 
0125 /*
0126  * One role of a QH is to hold a queue of TDs for some endpoint.  One QH goes
0127  * with each endpoint, and qh->element (updated by the HC) is either:
0128  *   - the next unprocessed TD in the endpoint's queue, or
0129  *   - UHCI_PTR_TERM (when there's no more traffic for this endpoint).
0130  *
0131  * The other role of a QH is to serve as a "skeleton" framelist entry, so we
0132  * can easily splice a QH for some endpoint into the schedule at the right
0133  * place.  Then qh->element is UHCI_PTR_TERM.
0134  *
0135  * In the schedule, qh->link maintains a list of QHs seen by the HC:
0136  *     skel1 --> ep1-qh --> ep2-qh --> ... --> skel2 --> ...
0137  *
0138  * qh->node is the software equivalent of qh->link.  The differences
0139  * are that the software list is doubly-linked and QHs in the UNLINKING
0140  * state are on the software list but not the hardware schedule.
0141  *
0142  * For bookkeeping purposes we maintain QHs even for Isochronous endpoints,
0143  * but they never get added to the hardware schedule.
0144  */
0145 #define QH_STATE_IDLE       1   /* QH is not being used */
0146 #define QH_STATE_UNLINKING  2   /* QH has been removed from the
0147                      * schedule but the hardware may
0148                      * still be using it */
0149 #define QH_STATE_ACTIVE     3   /* QH is on the schedule */
0150 
0151 struct uhci_qh {
0152     /* Hardware fields */
0153     __hc32 link;            /* Next QH in the schedule */
0154     __hc32 element;         /* Queue element (TD) pointer */
0155 
0156     /* Software fields */
0157     dma_addr_t dma_handle;
0158 
0159     struct list_head node;      /* Node in the list of QHs */
0160     struct usb_host_endpoint *hep;  /* Endpoint information */
0161     struct usb_device *udev;
0162     struct list_head queue;     /* Queue of urbps for this QH */
0163     struct uhci_td *dummy_td;   /* Dummy TD to end the queue */
0164     struct uhci_td *post_td;    /* Last TD completed */
0165 
0166     struct usb_iso_packet_descriptor *iso_packet_desc;
0167                     /* Next urb->iso_frame_desc entry */
0168     unsigned long advance_jiffies;  /* Time of last queue advance */
0169     unsigned int unlink_frame;  /* When the QH was unlinked */
0170     unsigned int period;        /* For Interrupt and Isochronous QHs */
0171     short phase;            /* Between 0 and period-1 */
0172     short load;         /* Periodic time requirement, in us */
0173     unsigned int iso_frame;     /* Frame # for iso_packet_desc */
0174 
0175     int state;          /* QH_STATE_xxx; see above */
0176     int type;           /* Queue type (control, bulk, etc) */
0177     int skel;           /* Skeleton queue number */
0178 
0179     unsigned int initial_toggle:1;  /* Endpoint's current toggle value */
0180     unsigned int needs_fixup:1; /* Must fix the TD toggle values */
0181     unsigned int is_stopped:1;  /* Queue was stopped by error/unlink */
0182     unsigned int wait_expired:1;    /* QH_WAIT_TIMEOUT has expired */
0183     unsigned int bandwidth_reserved:1;  /* Periodic bandwidth has
0184                          * been allocated */
0185 } __attribute__((aligned(16)));
0186 
0187 /*
0188  * We need a special accessor for the element pointer because it is
0189  * subject to asynchronous updates by the controller.
0190  */
0191 #define qh_element(qh)      READ_ONCE((qh)->element)
0192 
0193 #define LINK_TO_QH(uhci, qh)    (UHCI_PTR_QH((uhci)) | \
0194                 cpu_to_hc32((uhci), (qh)->dma_handle))
0195 
0196 
0197 /*
0198  *  Transfer Descriptors
0199  */
0200 
0201 /*
0202  * for TD <status>:
0203  */
0204 #define TD_CTRL_SPD     (1 << 29)   /* Short Packet Detect */
0205 #define TD_CTRL_C_ERR_MASK  (3 << 27)   /* Error Counter bits */
0206 #define TD_CTRL_C_ERR_SHIFT 27
0207 #define TD_CTRL_LS      (1 << 26)   /* Low Speed Device */
0208 #define TD_CTRL_IOS     (1 << 25)   /* Isochronous Select */
0209 #define TD_CTRL_IOC     (1 << 24)   /* Interrupt on Complete */
0210 #define TD_CTRL_ACTIVE      (1 << 23)   /* TD Active */
0211 #define TD_CTRL_STALLED     (1 << 22)   /* TD Stalled */
0212 #define TD_CTRL_DBUFERR     (1 << 21)   /* Data Buffer Error */
0213 #define TD_CTRL_BABBLE      (1 << 20)   /* Babble Detected */
0214 #define TD_CTRL_NAK     (1 << 19)   /* NAK Received */
0215 #define TD_CTRL_CRCTIMEO    (1 << 18)   /* CRC/Time Out Error */
0216 #define TD_CTRL_BITSTUFF    (1 << 17)   /* Bit Stuff Error */
0217 #define TD_CTRL_ACTLEN_MASK 0x7FF   /* actual length, encoded as n - 1 */
0218 
0219 #define uhci_maxerr(err)        ((err) << TD_CTRL_C_ERR_SHIFT)
0220 #define uhci_status_bits(ctrl_sts)  ((ctrl_sts) & 0xF60000)
0221 #define uhci_actual_length(ctrl_sts)    (((ctrl_sts) + 1) & \
0222             TD_CTRL_ACTLEN_MASK)    /* 1-based */
0223 
0224 /*
0225  * for TD <info>: (a.k.a. Token)
0226  */
0227 #define td_token(uhci, td)  hc32_to_cpu((uhci), (td)->token)
0228 #define TD_TOKEN_DEVADDR_SHIFT  8
0229 #define TD_TOKEN_TOGGLE_SHIFT   19
0230 #define TD_TOKEN_TOGGLE     (1 << 19)
0231 #define TD_TOKEN_EXPLEN_SHIFT   21
0232 #define TD_TOKEN_EXPLEN_MASK    0x7FF   /* expected length, encoded as n-1 */
0233 #define TD_TOKEN_PID_MASK   0xFF
0234 
0235 #define uhci_explen(len)    ((((len) - 1) & TD_TOKEN_EXPLEN_MASK) << \
0236                     TD_TOKEN_EXPLEN_SHIFT)
0237 
0238 #define uhci_expected_length(token) ((((token) >> TD_TOKEN_EXPLEN_SHIFT) + \
0239                     1) & TD_TOKEN_EXPLEN_MASK)
0240 #define uhci_toggle(token)  (((token) >> TD_TOKEN_TOGGLE_SHIFT) & 1)
0241 #define uhci_endpoint(token)    (((token) >> 15) & 0xf)
0242 #define uhci_devaddr(token) (((token) >> TD_TOKEN_DEVADDR_SHIFT) & 0x7f)
0243 #define uhci_devep(token)   (((token) >> TD_TOKEN_DEVADDR_SHIFT) & 0x7ff)
0244 #define uhci_packetid(token)    ((token) & TD_TOKEN_PID_MASK)
0245 #define uhci_packetout(token)   (uhci_packetid(token) != USB_PID_IN)
0246 #define uhci_packetin(token)    (uhci_packetid(token) == USB_PID_IN)
0247 
0248 /*
0249  * The documentation says "4 words for hardware, 4 words for software".
0250  *
0251  * That's silly, the hardware doesn't care. The hardware only cares that
0252  * the hardware words are 16-byte aligned, and we can have any amount of
0253  * sw space after the TD entry.
0254  *
0255  * td->link points to either another TD (not necessarily for the same urb or
0256  * even the same endpoint), or nothing (PTR_TERM), or a QH.
0257  */
0258 struct uhci_td {
0259     /* Hardware fields */
0260     __hc32 link;
0261     __hc32 status;
0262     __hc32 token;
0263     __hc32 buffer;
0264 
0265     /* Software fields */
0266     dma_addr_t dma_handle;
0267 
0268     struct list_head list;
0269 
0270     int frame;          /* for iso: what frame? */
0271     struct list_head fl_list;
0272 } __attribute__((aligned(16)));
0273 
0274 /*
0275  * We need a special accessor for the control/status word because it is
0276  * subject to asynchronous updates by the controller.
0277  */
0278 #define td_status(uhci, td)     hc32_to_cpu((uhci), \
0279                         READ_ONCE((td)->status))
0280 
0281 #define LINK_TO_TD(uhci, td)        (cpu_to_hc32((uhci), (td)->dma_handle))
0282 
0283 
0284 /*
0285  *  Skeleton Queue Headers
0286  */
0287 
0288 /*
0289  * The UHCI driver uses QHs with Interrupt, Control and Bulk URBs for
0290  * automatic queuing. To make it easy to insert entries into the schedule,
0291  * we have a skeleton of QHs for each predefined Interrupt latency.
0292  * Asynchronous QHs (low-speed control, full-speed control, and bulk)
0293  * go onto the period-1 interrupt list, since they all get accessed on
0294  * every frame.
0295  *
0296  * When we want to add a new QH, we add it to the list starting from the
0297  * appropriate skeleton QH.  For instance, the schedule can look like this:
0298  *
0299  * skel int128 QH
0300  * dev 1 interrupt QH
0301  * dev 5 interrupt QH
0302  * skel int64 QH
0303  * skel int32 QH
0304  * ...
0305  * skel int1 + async QH
0306  * dev 5 low-speed control QH
0307  * dev 1 bulk QH
0308  * dev 2 bulk QH
0309  *
0310  * There is a special terminating QH used to keep full-speed bandwidth
0311  * reclamation active when no full-speed control or bulk QHs are linked
0312  * into the schedule.  It has an inactive TD (to work around a PIIX bug,
0313  * see the Intel errata) and it points back to itself.
0314  *
0315  * There's a special skeleton QH for Isochronous QHs which never appears
0316  * on the schedule.  Isochronous TDs go on the schedule before the
0317  * skeleton QHs.  The hardware accesses them directly rather than
0318  * through their QH, which is used only for bookkeeping purposes.
0319  * While the UHCI spec doesn't forbid the use of QHs for Isochronous,
0320  * it doesn't use them either.  And the spec says that queues never
0321  * advance on an error completion status, which makes them totally
0322  * unsuitable for Isochronous transfers.
0323  *
0324  * There's also a special skeleton QH used for QHs which are in the process
0325  * of unlinking and so may still be in use by the hardware.  It too never
0326  * appears on the schedule.
0327  */
0328 
0329 #define UHCI_NUM_SKELQH     11
0330 #define SKEL_UNLINK     0
0331 #define skel_unlink_qh      skelqh[SKEL_UNLINK]
0332 #define SKEL_ISO        1
0333 #define skel_iso_qh     skelqh[SKEL_ISO]
0334     /* int128, int64, ..., int1 = 2, 3, ..., 9 */
0335 #define SKEL_INDEX(exponent)    (9 - exponent)
0336 #define SKEL_ASYNC      9
0337 #define skel_async_qh       skelqh[SKEL_ASYNC]
0338 #define SKEL_TERM       10
0339 #define skel_term_qh        skelqh[SKEL_TERM]
0340 
0341 /* The following entries refer to sublists of skel_async_qh */
0342 #define SKEL_LS_CONTROL     20
0343 #define SKEL_FS_CONTROL     21
0344 #define SKEL_FSBR       SKEL_FS_CONTROL
0345 #define SKEL_BULK       22
0346 
0347 /*
0348  *  The UHCI controller and root hub
0349  */
0350 
0351 /*
0352  * States for the root hub:
0353  *
0354  * To prevent "bouncing" in the presence of electrical noise,
0355  * when there are no devices attached we delay for 1 second in the
0356  * RUNNING_NODEVS state before switching to the AUTO_STOPPED state.
0357  * 
0358  * (Note that the AUTO_STOPPED state won't be necessary once the hub
0359  * driver learns to autosuspend.)
0360  */
0361 enum uhci_rh_state {
0362     /* In the following states the HC must be halted.
0363      * These two must come first. */
0364     UHCI_RH_RESET,
0365     UHCI_RH_SUSPENDED,
0366 
0367     UHCI_RH_AUTO_STOPPED,
0368     UHCI_RH_RESUMING,
0369 
0370     /* In this state the HC changes from running to halted,
0371      * so it can legally appear either way. */
0372     UHCI_RH_SUSPENDING,
0373 
0374     /* In the following states it's an error if the HC is halted.
0375      * These two must come last. */
0376     UHCI_RH_RUNNING,        /* The normal state */
0377     UHCI_RH_RUNNING_NODEVS,     /* Running with no devices attached */
0378 };
0379 
0380 /*
0381  * The full UHCI controller information:
0382  */
0383 struct uhci_hcd {
0384     /* Grabbed from PCI */
0385     unsigned long io_addr;
0386 
0387     /* Used when registers are memory mapped */
0388     void __iomem *regs;
0389 
0390     struct dma_pool *qh_pool;
0391     struct dma_pool *td_pool;
0392 
0393     struct uhci_td *term_td;    /* Terminating TD, see UHCI bug */
0394     struct uhci_qh *skelqh[UHCI_NUM_SKELQH];    /* Skeleton QHs */
0395     struct uhci_qh *next_qh;    /* Next QH to scan */
0396 
0397     spinlock_t lock;
0398 
0399     dma_addr_t frame_dma_handle;    /* Hardware frame list */
0400     __hc32 *frame;
0401     void **frame_cpu;       /* CPU's frame list */
0402 
0403     enum uhci_rh_state rh_state;
0404     unsigned long auto_stop_time;       /* When to AUTO_STOP */
0405 
0406     unsigned int frame_number;      /* As of last check */
0407     unsigned int is_stopped;
0408 #define UHCI_IS_STOPPED     9999        /* Larger than a frame # */
0409     unsigned int last_iso_frame;        /* Frame of last scan */
0410     unsigned int cur_iso_frame;     /* Frame for current scan */
0411 
0412     unsigned int scan_in_progress:1;    /* Schedule scan is running */
0413     unsigned int need_rescan:1;     /* Redo the schedule scan */
0414     unsigned int dead:1;            /* Controller has died */
0415     unsigned int RD_enable:1;       /* Suspended root hub with
0416                            Resume-Detect interrupts
0417                            enabled */
0418     unsigned int is_initialized:1;      /* Data structure is usable */
0419     unsigned int fsbr_is_on:1;      /* FSBR is turned on */
0420     unsigned int fsbr_is_wanted:1;      /* Does any URB want FSBR? */
0421     unsigned int fsbr_expiring:1;       /* FSBR is timing out */
0422 
0423     struct timer_list fsbr_timer;       /* For turning off FBSR */
0424 
0425     /* Silicon quirks */
0426     unsigned int oc_low:1;          /* OverCurrent bit active low */
0427     unsigned int wait_for_hp:1;     /* Wait for HP port reset */
0428     unsigned int big_endian_mmio:1;     /* Big endian registers */
0429     unsigned int big_endian_desc:1;     /* Big endian descriptors */
0430     unsigned int is_aspeed:1;       /* Aspeed impl. workarounds */
0431 
0432     /* Support for port suspend/resume/reset */
0433     unsigned long port_c_suspend;       /* Bit-arrays of ports */
0434     unsigned long resuming_ports;
0435     unsigned long ports_timeout;        /* Time to stop signalling */
0436 
0437     struct list_head idle_qh_list;      /* Where the idle QHs live */
0438 
0439     int rh_numports;            /* Number of root-hub ports */
0440 
0441     wait_queue_head_t waitqh;       /* endpoint_disable waiters */
0442     int num_waiting;            /* Number of waiters */
0443 
0444     int total_load;             /* Sum of array values */
0445     short load[MAX_PHASE];          /* Periodic allocations */
0446 
0447     struct clk *clk;            /* (optional) clock source */
0448 
0449     /* Reset host controller */
0450     void    (*reset_hc) (struct uhci_hcd *uhci);
0451     int (*check_and_reset_hc) (struct uhci_hcd *uhci);
0452     /* configure_hc should perform arch specific settings, if needed */
0453     void    (*configure_hc) (struct uhci_hcd *uhci);
0454     /* Check for broken resume detect interrupts */
0455     int (*resume_detect_interrupts_are_broken) (struct uhci_hcd *uhci);
0456     /* Check for broken global suspend */
0457     int (*global_suspend_mode_is_broken) (struct uhci_hcd *uhci);
0458 };
0459 
0460 /* Convert between a usb_hcd pointer and the corresponding uhci_hcd */
0461 static inline struct uhci_hcd *hcd_to_uhci(struct usb_hcd *hcd)
0462 {
0463     return (struct uhci_hcd *) (hcd->hcd_priv);
0464 }
0465 static inline struct usb_hcd *uhci_to_hcd(struct uhci_hcd *uhci)
0466 {
0467     return container_of((void *) uhci, struct usb_hcd, hcd_priv);
0468 }
0469 
0470 #define uhci_dev(u) (uhci_to_hcd(u)->self.controller)
0471 
0472 /* Utility macro for comparing frame numbers */
0473 #define uhci_frame_before_eq(f1, f2)    (0 <= (int) ((f2) - (f1)))
0474 
0475 
0476 /*
0477  *  Private per-URB data
0478  */
0479 struct urb_priv {
0480     struct list_head node;      /* Node in the QH's urbp list */
0481 
0482     struct urb *urb;
0483 
0484     struct uhci_qh *qh;     /* QH for this URB */
0485     struct list_head td_list;
0486 
0487     unsigned fsbr:1;        /* URB wants FSBR */
0488 };
0489 
0490 
0491 /* Some special IDs */
0492 
0493 #define PCI_VENDOR_ID_GENESYS       0x17a0
0494 #define PCI_DEVICE_ID_GL880S_UHCI   0x8083
0495 
0496 /* Aspeed SoC needs some quirks */
0497 static inline bool uhci_is_aspeed(const struct uhci_hcd *uhci)
0498 {
0499     return IS_ENABLED(CONFIG_USB_UHCI_ASPEED) && uhci->is_aspeed;
0500 }
0501 
0502 /*
0503  * Functions used to access controller registers. The UCHI spec says that host
0504  * controller I/O registers are mapped into PCI I/O space. For non-PCI hosts
0505  * we use memory mapped registers.
0506  */
0507 
0508 #ifndef CONFIG_USB_UHCI_SUPPORT_NON_PCI_HC
0509 /* Support PCI only */
0510 static inline u32 uhci_readl(const struct uhci_hcd *uhci, int reg)
0511 {
0512     return inl(uhci->io_addr + reg);
0513 }
0514 
0515 static inline void uhci_writel(const struct uhci_hcd *uhci, u32 val, int reg)
0516 {
0517     outl(val, uhci->io_addr + reg);
0518 }
0519 
0520 static inline u16 uhci_readw(const struct uhci_hcd *uhci, int reg)
0521 {
0522     return inw(uhci->io_addr + reg);
0523 }
0524 
0525 static inline void uhci_writew(const struct uhci_hcd *uhci, u16 val, int reg)
0526 {
0527     outw(val, uhci->io_addr + reg);
0528 }
0529 
0530 static inline u8 uhci_readb(const struct uhci_hcd *uhci, int reg)
0531 {
0532     return inb(uhci->io_addr + reg);
0533 }
0534 
0535 static inline void uhci_writeb(const struct uhci_hcd *uhci, u8 val, int reg)
0536 {
0537     outb(val, uhci->io_addr + reg);
0538 }
0539 
0540 #else
0541 /* Support non-PCI host controllers */
0542 #ifdef CONFIG_USB_PCI
0543 /* Support PCI and non-PCI host controllers */
0544 #define uhci_has_pci_registers(u)   ((u)->io_addr != 0)
0545 #else
0546 /* Support non-PCI host controllers only */
0547 #define uhci_has_pci_registers(u)   0
0548 #endif
0549 
0550 #ifdef CONFIG_USB_UHCI_BIG_ENDIAN_MMIO
0551 /* Support (non-PCI) big endian host controllers */
0552 #define uhci_big_endian_mmio(u)     ((u)->big_endian_mmio)
0553 #else
0554 #define uhci_big_endian_mmio(u)     0
0555 #endif
0556 
0557 static inline int uhci_aspeed_reg(unsigned int reg)
0558 {
0559     switch (reg) {
0560     case USBCMD:
0561         return 00;
0562     case USBSTS:
0563         return 0x04;
0564     case USBINTR:
0565         return 0x08;
0566     case USBFRNUM:
0567         return 0x80;
0568     case USBFLBASEADD:
0569         return 0x0c;
0570     case USBSOF:
0571         return 0x84;
0572     case USBPORTSC1:
0573         return 0x88;
0574     case USBPORTSC2:
0575         return 0x8c;
0576     case USBPORTSC3:
0577         return 0x90;
0578     case USBPORTSC4:
0579         return 0x94;
0580     default:
0581         pr_warn("UHCI: Unsupported register 0x%02x on Aspeed\n", reg);
0582         /* Return an unimplemented register */
0583         return 0x10;
0584     }
0585 }
0586 
0587 static inline u32 uhci_readl(const struct uhci_hcd *uhci, int reg)
0588 {
0589     if (uhci_has_pci_registers(uhci))
0590         return inl(uhci->io_addr + reg);
0591     else if (uhci_is_aspeed(uhci))
0592         return readl(uhci->regs + uhci_aspeed_reg(reg));
0593 #ifdef CONFIG_USB_UHCI_BIG_ENDIAN_MMIO
0594     else if (uhci_big_endian_mmio(uhci))
0595         return readl_be(uhci->regs + reg);
0596 #endif
0597     else
0598         return readl(uhci->regs + reg);
0599 }
0600 
0601 static inline void uhci_writel(const struct uhci_hcd *uhci, u32 val, int reg)
0602 {
0603     if (uhci_has_pci_registers(uhci))
0604         outl(val, uhci->io_addr + reg);
0605     else if (uhci_is_aspeed(uhci))
0606         writel(val, uhci->regs + uhci_aspeed_reg(reg));
0607 #ifdef CONFIG_USB_UHCI_BIG_ENDIAN_MMIO
0608     else if (uhci_big_endian_mmio(uhci))
0609         writel_be(val, uhci->regs + reg);
0610 #endif
0611     else
0612         writel(val, uhci->regs + reg);
0613 }
0614 
0615 static inline u16 uhci_readw(const struct uhci_hcd *uhci, int reg)
0616 {
0617     if (uhci_has_pci_registers(uhci))
0618         return inw(uhci->io_addr + reg);
0619     else if (uhci_is_aspeed(uhci))
0620         return readl(uhci->regs + uhci_aspeed_reg(reg));
0621 #ifdef CONFIG_USB_UHCI_BIG_ENDIAN_MMIO
0622     else if (uhci_big_endian_mmio(uhci))
0623         return readw_be(uhci->regs + reg);
0624 #endif
0625     else
0626         return readw(uhci->regs + reg);
0627 }
0628 
0629 static inline void uhci_writew(const struct uhci_hcd *uhci, u16 val, int reg)
0630 {
0631     if (uhci_has_pci_registers(uhci))
0632         outw(val, uhci->io_addr + reg);
0633     else if (uhci_is_aspeed(uhci))
0634         writel(val, uhci->regs + uhci_aspeed_reg(reg));
0635 #ifdef CONFIG_USB_UHCI_BIG_ENDIAN_MMIO
0636     else if (uhci_big_endian_mmio(uhci))
0637         writew_be(val, uhci->regs + reg);
0638 #endif
0639     else
0640         writew(val, uhci->regs + reg);
0641 }
0642 
0643 static inline u8 uhci_readb(const struct uhci_hcd *uhci, int reg)
0644 {
0645     if (uhci_has_pci_registers(uhci))
0646         return inb(uhci->io_addr + reg);
0647     else if (uhci_is_aspeed(uhci))
0648         return readl(uhci->regs + uhci_aspeed_reg(reg));
0649 #ifdef CONFIG_USB_UHCI_BIG_ENDIAN_MMIO
0650     else if (uhci_big_endian_mmio(uhci))
0651         return readb_be(uhci->regs + reg);
0652 #endif
0653     else
0654         return readb(uhci->regs + reg);
0655 }
0656 
0657 static inline void uhci_writeb(const struct uhci_hcd *uhci, u8 val, int reg)
0658 {
0659     if (uhci_has_pci_registers(uhci))
0660         outb(val, uhci->io_addr + reg);
0661     else if (uhci_is_aspeed(uhci))
0662         writel(val, uhci->regs + uhci_aspeed_reg(reg));
0663 #ifdef CONFIG_USB_UHCI_BIG_ENDIAN_MMIO
0664     else if (uhci_big_endian_mmio(uhci))
0665         writeb_be(val, uhci->regs + reg);
0666 #endif
0667     else
0668         writeb(val, uhci->regs + reg);
0669 }
0670 #endif /* CONFIG_USB_UHCI_SUPPORT_NON_PCI_HC */
0671 
0672 /*
0673  * The GRLIB GRUSBHC controller can use big endian format for its descriptors.
0674  *
0675  * UHCI controllers accessed through PCI work normally (little-endian
0676  * everywhere), so we don't bother supporting a BE-only mode.
0677  */
0678 #ifdef CONFIG_USB_UHCI_BIG_ENDIAN_DESC
0679 #define uhci_big_endian_desc(u)     ((u)->big_endian_desc)
0680 
0681 /* cpu to uhci */
0682 static inline __hc32 cpu_to_hc32(const struct uhci_hcd *uhci, const u32 x)
0683 {
0684     return uhci_big_endian_desc(uhci)
0685         ? (__force __hc32)cpu_to_be32(x)
0686         : (__force __hc32)cpu_to_le32(x);
0687 }
0688 
0689 /* uhci to cpu */
0690 static inline u32 hc32_to_cpu(const struct uhci_hcd *uhci, const __hc32 x)
0691 {
0692     return uhci_big_endian_desc(uhci)
0693         ? be32_to_cpu((__force __be32)x)
0694         : le32_to_cpu((__force __le32)x);
0695 }
0696 
0697 #else
0698 /* cpu to uhci */
0699 static inline __hc32 cpu_to_hc32(const struct uhci_hcd *uhci, const u32 x)
0700 {
0701     return cpu_to_le32(x);
0702 }
0703 
0704 /* uhci to cpu */
0705 static inline u32 hc32_to_cpu(const struct uhci_hcd *uhci, const __hc32 x)
0706 {
0707     return le32_to_cpu(x);
0708 }
0709 #endif
0710 
0711 #endif