0001
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
0015
0016
0017
0018 #define USBCMD 0
0019 #define USBCMD_RS 0x0001
0020 #define USBCMD_HCRESET 0x0002
0021 #define USBCMD_GRESET 0x0004
0022 #define USBCMD_EGSM 0x0008
0023 #define USBCMD_FGR 0x0010
0024 #define USBCMD_SWDBG 0x0020
0025 #define USBCMD_CF 0x0040
0026 #define USBCMD_MAXP 0x0080
0027
0028
0029 #define USBSTS 2
0030 #define USBSTS_USBINT 0x0001
0031 #define USBSTS_ERROR 0x0002
0032 #define USBSTS_RD 0x0004
0033 #define USBSTS_HSE 0x0008
0034 #define USBSTS_HCPE 0x0010
0035
0036 #define USBSTS_HCH 0x0020
0037
0038
0039 #define USBINTR 4
0040 #define USBINTR_TIMEOUT 0x0001
0041 #define USBINTR_RESUME 0x0002
0042 #define USBINTR_IOC 0x0004
0043 #define USBINTR_SP 0x0008
0044
0045 #define USBFRNUM 6
0046 #define USBFLBASEADD 8
0047 #define USBSOF 12
0048 #define USBSOF_DEFAULT 64
0049
0050
0051 #define USBPORTSC1 16
0052 #define USBPORTSC2 18
0053 #define USBPORTSC3 20
0054 #define USBPORTSC4 22
0055 #define USBPORTSC_CCS 0x0001
0056
0057 #define USBPORTSC_CSC 0x0002
0058 #define USBPORTSC_PE 0x0004
0059 #define USBPORTSC_PEC 0x0008
0060 #define USBPORTSC_DPLUS 0x0010
0061 #define USBPORTSC_DMINUS 0x0020
0062 #define USBPORTSC_RD 0x0040
0063 #define USBPORTSC_RES1 0x0080
0064 #define USBPORTSC_LSDA 0x0100
0065 #define USBPORTSC_PR 0x0200
0066
0067 #define USBPORTSC_OC 0x0400
0068 #define USBPORTSC_OCC 0x0800
0069 #define USBPORTSC_SUSP 0x1000
0070 #define USBPORTSC_RES2 0x2000
0071 #define USBPORTSC_RES3 0x4000
0072 #define USBPORTSC_RES4 0x8000
0073
0074
0075 #define USBLEGSUP 0xc0
0076 #define USBLEGSUP_DEFAULT 0x2000
0077 #define USBLEGSUP_RWC 0x8f00
0078 #define USBLEGSUP_RO 0x5040
0079
0080
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
0092 #define UHCI_MAX_SOF_NUMBER 2047
0093 #define CAN_SCHEDULE_FRAMES 1000
0094
0095 #define MAX_PHASE 32
0096
0097
0098
0099 #define FSBR_OFF_DELAY msecs_to_jiffies(10)
0100
0101
0102 #define QH_WAIT_TIMEOUT msecs_to_jiffies(200)
0103
0104
0105
0106
0107
0108
0109
0110
0111
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
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145 #define QH_STATE_IDLE 1
0146 #define QH_STATE_UNLINKING 2
0147
0148
0149 #define QH_STATE_ACTIVE 3
0150
0151 struct uhci_qh {
0152
0153 __hc32 link;
0154 __hc32 element;
0155
0156
0157 dma_addr_t dma_handle;
0158
0159 struct list_head node;
0160 struct usb_host_endpoint *hep;
0161 struct usb_device *udev;
0162 struct list_head queue;
0163 struct uhci_td *dummy_td;
0164 struct uhci_td *post_td;
0165
0166 struct usb_iso_packet_descriptor *iso_packet_desc;
0167
0168 unsigned long advance_jiffies;
0169 unsigned int unlink_frame;
0170 unsigned int period;
0171 short phase;
0172 short load;
0173 unsigned int iso_frame;
0174
0175 int state;
0176 int type;
0177 int skel;
0178
0179 unsigned int initial_toggle:1;
0180 unsigned int needs_fixup:1;
0181 unsigned int is_stopped:1;
0182 unsigned int wait_expired:1;
0183 unsigned int bandwidth_reserved:1;
0184
0185 } __attribute__((aligned(16)));
0186
0187
0188
0189
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
0199
0200
0201
0202
0203
0204 #define TD_CTRL_SPD (1 << 29)
0205 #define TD_CTRL_C_ERR_MASK (3 << 27)
0206 #define TD_CTRL_C_ERR_SHIFT 27
0207 #define TD_CTRL_LS (1 << 26)
0208 #define TD_CTRL_IOS (1 << 25)
0209 #define TD_CTRL_IOC (1 << 24)
0210 #define TD_CTRL_ACTIVE (1 << 23)
0211 #define TD_CTRL_STALLED (1 << 22)
0212 #define TD_CTRL_DBUFERR (1 << 21)
0213 #define TD_CTRL_BABBLE (1 << 20)
0214 #define TD_CTRL_NAK (1 << 19)
0215 #define TD_CTRL_CRCTIMEO (1 << 18)
0216 #define TD_CTRL_BITSTUFF (1 << 17)
0217 #define TD_CTRL_ACTLEN_MASK 0x7FF
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)
0223
0224
0225
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
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
0250
0251
0252
0253
0254
0255
0256
0257
0258 struct uhci_td {
0259
0260 __hc32 link;
0261 __hc32 status;
0262 __hc32 token;
0263 __hc32 buffer;
0264
0265
0266 dma_addr_t dma_handle;
0267
0268 struct list_head list;
0269
0270 int frame;
0271 struct list_head fl_list;
0272 } __attribute__((aligned(16)));
0273
0274
0275
0276
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
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
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
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
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
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361 enum uhci_rh_state {
0362
0363
0364 UHCI_RH_RESET,
0365 UHCI_RH_SUSPENDED,
0366
0367 UHCI_RH_AUTO_STOPPED,
0368 UHCI_RH_RESUMING,
0369
0370
0371
0372 UHCI_RH_SUSPENDING,
0373
0374
0375
0376 UHCI_RH_RUNNING,
0377 UHCI_RH_RUNNING_NODEVS,
0378 };
0379
0380
0381
0382
0383 struct uhci_hcd {
0384
0385 unsigned long io_addr;
0386
0387
0388 void __iomem *regs;
0389
0390 struct dma_pool *qh_pool;
0391 struct dma_pool *td_pool;
0392
0393 struct uhci_td *term_td;
0394 struct uhci_qh *skelqh[UHCI_NUM_SKELQH];
0395 struct uhci_qh *next_qh;
0396
0397 spinlock_t lock;
0398
0399 dma_addr_t frame_dma_handle;
0400 __hc32 *frame;
0401 void **frame_cpu;
0402
0403 enum uhci_rh_state rh_state;
0404 unsigned long auto_stop_time;
0405
0406 unsigned int frame_number;
0407 unsigned int is_stopped;
0408 #define UHCI_IS_STOPPED 9999
0409 unsigned int last_iso_frame;
0410 unsigned int cur_iso_frame;
0411
0412 unsigned int scan_in_progress:1;
0413 unsigned int need_rescan:1;
0414 unsigned int dead:1;
0415 unsigned int RD_enable:1;
0416
0417
0418 unsigned int is_initialized:1;
0419 unsigned int fsbr_is_on:1;
0420 unsigned int fsbr_is_wanted:1;
0421 unsigned int fsbr_expiring:1;
0422
0423 struct timer_list fsbr_timer;
0424
0425
0426 unsigned int oc_low:1;
0427 unsigned int wait_for_hp:1;
0428 unsigned int big_endian_mmio:1;
0429 unsigned int big_endian_desc:1;
0430 unsigned int is_aspeed:1;
0431
0432
0433 unsigned long port_c_suspend;
0434 unsigned long resuming_ports;
0435 unsigned long ports_timeout;
0436
0437 struct list_head idle_qh_list;
0438
0439 int rh_numports;
0440
0441 wait_queue_head_t waitqh;
0442 int num_waiting;
0443
0444 int total_load;
0445 short load[MAX_PHASE];
0446
0447 struct clk *clk;
0448
0449
0450 void (*reset_hc) (struct uhci_hcd *uhci);
0451 int (*check_and_reset_hc) (struct uhci_hcd *uhci);
0452
0453 void (*configure_hc) (struct uhci_hcd *uhci);
0454
0455 int (*resume_detect_interrupts_are_broken) (struct uhci_hcd *uhci);
0456
0457 int (*global_suspend_mode_is_broken) (struct uhci_hcd *uhci);
0458 };
0459
0460
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
0473 #define uhci_frame_before_eq(f1, f2) (0 <= (int) ((f2) - (f1)))
0474
0475
0476
0477
0478
0479 struct urb_priv {
0480 struct list_head node;
0481
0482 struct urb *urb;
0483
0484 struct uhci_qh *qh;
0485 struct list_head td_list;
0486
0487 unsigned fsbr:1;
0488 };
0489
0490
0491
0492
0493 #define PCI_VENDOR_ID_GENESYS 0x17a0
0494 #define PCI_DEVICE_ID_GL880S_UHCI 0x8083
0495
0496
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
0504
0505
0506
0507
0508 #ifndef CONFIG_USB_UHCI_SUPPORT_NON_PCI_HC
0509
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
0542 #ifdef CONFIG_USB_PCI
0543
0544 #define uhci_has_pci_registers(u) ((u)->io_addr != 0)
0545 #else
0546
0547 #define uhci_has_pci_registers(u) 0
0548 #endif
0549
0550 #ifdef CONFIG_USB_UHCI_BIG_ENDIAN_MMIO
0551
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
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
0671
0672
0673
0674
0675
0676
0677
0678 #ifdef CONFIG_USB_UHCI_BIG_ENDIAN_DESC
0679 #define uhci_big_endian_desc(u) ((u)->big_endian_desc)
0680
0681
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
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
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
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