musb_core.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * MUSB OTG driver defines
  4. *
  5. * Copyright 2005 Mentor Graphics Corporation
  6. * Copyright (C) 2005-2006 by Texas Instruments
  7. * Copyright (C) 2006-2007 Nokia Corporation
  8. */
  9. #ifndef __MUSB_CORE_H__
  10. #define __MUSB_CORE_H__
  11. #include <linux/slab.h>
  12. #include <linux/list.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/errno.h>
  15. #include <linux/timer.h>
  16. #include <linux/device.h>
  17. #include <linux/usb/ch9.h>
  18. #include <linux/usb/gadget.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/otg.h>
  21. #include <linux/usb/musb.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/workqueue.h>
  24. struct musb;
  25. struct musb_hw_ep;
  26. struct musb_ep;
  27. struct musb_qh;
  28. /* Helper defines for struct musb->hwvers */
  29. #define MUSB_HWVERS_MAJOR(x) ((x >> 10) & 0x1f)
  30. #define MUSB_HWVERS_MINOR(x) (x & 0x3ff)
  31. #define MUSB_HWVERS_RC 0x8000
  32. #define MUSB_HWVERS_1300 0x52C
  33. #define MUSB_HWVERS_1400 0x590
  34. #define MUSB_HWVERS_1800 0x720
  35. #define MUSB_HWVERS_1900 0x784
  36. #define MUSB_HWVERS_2000 0x800
  37. #include "musb_debug.h"
  38. #include "musb_dma.h"
  39. #include "musb_io.h"
  40. #include "musb_gadget.h"
  41. #include <linux/usb/hcd.h>
  42. #include "musb_host.h"
  43. /* NOTE: otg and peripheral-only state machines start at B_IDLE.
  44. * OTG or host-only go to A_IDLE when ID is sensed.
  45. */
  46. #define is_peripheral_active(m) (!(m)->is_host)
  47. #define is_host_active(m) ((m)->is_host)
  48. /****************************** CONSTANTS ********************************/
  49. #ifndef MUSB_C_NUM_EPS
  50. #define MUSB_C_NUM_EPS ((u8)16)
  51. #endif
  52. #ifndef MUSB_MAX_END0_PACKET
  53. #define MUSB_MAX_END0_PACKET ((u16)MUSB_EP0_FIFOSIZE)
  54. #endif
  55. /* host side ep0 states */
  56. enum musb_h_ep0_state {
  57. MUSB_EP0_IDLE,
  58. MUSB_EP0_START, /* expect ack of setup */
  59. MUSB_EP0_IN, /* expect IN DATA */
  60. MUSB_EP0_OUT, /* expect ack of OUT DATA */
  61. MUSB_EP0_STATUS, /* expect ack of STATUS */
  62. } __attribute__ ((packed));
  63. /* peripheral side ep0 states */
  64. enum musb_g_ep0_state {
  65. MUSB_EP0_STAGE_IDLE, /* idle, waiting for SETUP */
  66. MUSB_EP0_STAGE_SETUP, /* received SETUP */
  67. MUSB_EP0_STAGE_TX, /* IN data */
  68. MUSB_EP0_STAGE_RX, /* OUT data */
  69. MUSB_EP0_STAGE_STATUSIN, /* (after OUT data) */
  70. MUSB_EP0_STAGE_STATUSOUT, /* (after IN data) */
  71. MUSB_EP0_STAGE_ACKWAIT, /* after zlp, before statusin */
  72. } __attribute__ ((packed));
  73. /*
  74. * OTG protocol constants. See USB OTG 1.3 spec,
  75. * sections 5.5 "Device Timings" and 6.6.5 "Timers".
  76. */
  77. #define OTG_TIME_A_WAIT_VRISE 100 /* msec (max) */
  78. #define OTG_TIME_A_WAIT_BCON 1100 /* min 1 second */
  79. #define OTG_TIME_A_AIDL_BDIS 200 /* min 200 msec */
  80. #define OTG_TIME_B_ASE0_BRST 100 /* min 3.125 ms */
  81. /****************************** FUNCTIONS ********************************/
  82. #define MUSB_HST_MODE(_musb)\
  83. { (_musb)->is_host = true; }
  84. #define MUSB_DEV_MODE(_musb) \
  85. { (_musb)->is_host = false; }
  86. #define test_devctl_hst_mode(_x) \
  87. (musb_readb((_x)->mregs, MUSB_DEVCTL)&MUSB_DEVCTL_HM)
  88. #define MUSB_MODE(musb) ((musb)->is_host ? "Host" : "Peripheral")
  89. /******************************** TYPES *************************************/
  90. struct musb_io;
  91. /**
  92. * struct musb_platform_ops - Operations passed to musb_core by HW glue layer
  93. * @quirks: flags for platform specific quirks
  94. * @enable: enable device
  95. * @disable: disable device
  96. * @ep_offset: returns the end point offset
  97. * @ep_select: selects the specified end point
  98. * @fifo_mode: sets the fifo mode
  99. * @fifo_offset: returns the fifo offset
  100. * @readb: read 8 bits
  101. * @writeb: write 8 bits
  102. * @clearb: could be clear-on-readb or W1C
  103. * @readw: read 16 bits
  104. * @writew: write 16 bits
  105. * @clearw: could be clear-on-readw or W1C
  106. * @read_fifo: reads the fifo
  107. * @write_fifo: writes to fifo
  108. * @get_toggle: platform specific get toggle function
  109. * @set_toggle: platform specific set toggle function
  110. * @dma_init: platform specific dma init function
  111. * @dma_exit: platform specific dma exit function
  112. * @init: turns on clocks, sets up platform-specific registers, etc
  113. * @exit: undoes @init
  114. * @set_mode: forcefully changes operating mode
  115. * @try_idle: tries to idle the IP
  116. * @recover: platform-specific babble recovery
  117. * @vbus_status: returns vbus status if possible
  118. * @set_vbus: forces vbus status
  119. * @pre_root_reset_end: called before the root usb port reset flag gets cleared
  120. * @post_root_reset_end: called after the root usb port reset flag gets cleared
  121. * @phy_callback: optional callback function for the phy to call
  122. */
  123. struct musb_platform_ops {
  124. #define MUSB_G_NO_SKB_RESERVE BIT(9)
  125. #define MUSB_DA8XX BIT(8)
  126. #define MUSB_PRESERVE_SESSION BIT(7)
  127. #define MUSB_DMA_UX500 BIT(6)
  128. #define MUSB_DMA_CPPI41 BIT(5)
  129. #define MUSB_DMA_CPPI BIT(4)
  130. #define MUSB_DMA_TUSB_OMAP BIT(3)
  131. #define MUSB_DMA_INVENTRA BIT(2)
  132. #define MUSB_IN_TUSB BIT(1)
  133. #define MUSB_INDEXED_EP BIT(0)
  134. u32 quirks;
  135. int (*init)(struct musb *musb);
  136. int (*exit)(struct musb *musb);
  137. void (*enable)(struct musb *musb);
  138. void (*disable)(struct musb *musb);
  139. u32 (*ep_offset)(u8 epnum, u16 offset);
  140. void (*ep_select)(void __iomem *mbase, u8 epnum);
  141. u16 fifo_mode;
  142. u32 (*fifo_offset)(u8 epnum);
  143. u32 (*busctl_offset)(u8 epnum, u16 offset);
  144. u8 (*readb)(void __iomem *addr, u32 offset);
  145. void (*writeb)(void __iomem *addr, u32 offset, u8 data);
  146. u8 (*clearb)(void __iomem *addr, u32 offset);
  147. u16 (*readw)(void __iomem *addr, u32 offset);
  148. void (*writew)(void __iomem *addr, u32 offset, u16 data);
  149. u16 (*clearw)(void __iomem *addr, u32 offset);
  150. void (*read_fifo)(struct musb_hw_ep *hw_ep, u16 len, u8 *buf);
  151. void (*write_fifo)(struct musb_hw_ep *hw_ep, u16 len, const u8 *buf);
  152. u16 (*get_toggle)(struct musb_qh *qh, int is_out);
  153. u16 (*set_toggle)(struct musb_qh *qh, int is_out, struct urb *urb);
  154. struct dma_controller *
  155. (*dma_init) (struct musb *musb, void __iomem *base);
  156. void (*dma_exit)(struct dma_controller *c);
  157. int (*set_mode)(struct musb *musb, u8 mode);
  158. void (*try_idle)(struct musb *musb, unsigned long timeout);
  159. int (*recover)(struct musb *musb);
  160. int (*vbus_status)(struct musb *musb);
  161. void (*set_vbus)(struct musb *musb, int on);
  162. void (*pre_root_reset_end)(struct musb *musb);
  163. void (*post_root_reset_end)(struct musb *musb);
  164. int (*phy_callback)(enum musb_vbus_id_status status);
  165. void (*clear_ep_rxintr)(struct musb *musb, int epnum);
  166. };
  167. /*
  168. * struct musb_hw_ep - endpoint hardware (bidirectional)
  169. *
  170. * Ordered slightly for better cacheline locality.
  171. */
  172. struct musb_hw_ep {
  173. struct musb *musb;
  174. void __iomem *fifo;
  175. void __iomem *regs;
  176. #if IS_ENABLED(CONFIG_USB_MUSB_TUSB6010)
  177. void __iomem *conf;
  178. #endif
  179. /* index in musb->endpoints[] */
  180. u8 epnum;
  181. /* hardware configuration, possibly dynamic */
  182. bool is_shared_fifo;
  183. bool tx_double_buffered;
  184. bool rx_double_buffered;
  185. u16 max_packet_sz_tx;
  186. u16 max_packet_sz_rx;
  187. struct dma_channel *tx_channel;
  188. struct dma_channel *rx_channel;
  189. #if IS_ENABLED(CONFIG_USB_MUSB_TUSB6010)
  190. /* TUSB has "asynchronous" and "synchronous" dma modes */
  191. dma_addr_t fifo_async;
  192. dma_addr_t fifo_sync;
  193. void __iomem *fifo_sync_va;
  194. #endif
  195. /* currently scheduled peripheral endpoint */
  196. struct musb_qh *in_qh;
  197. struct musb_qh *out_qh;
  198. u8 rx_reinit;
  199. u8 tx_reinit;
  200. /* peripheral side */
  201. struct musb_ep ep_in; /* TX */
  202. struct musb_ep ep_out; /* RX */
  203. };
  204. static inline struct musb_request *next_in_request(struct musb_hw_ep *hw_ep)
  205. {
  206. return next_request(&hw_ep->ep_in);
  207. }
  208. static inline struct musb_request *next_out_request(struct musb_hw_ep *hw_ep)
  209. {
  210. return next_request(&hw_ep->ep_out);
  211. }
  212. struct musb_csr_regs {
  213. /* FIFO registers */
  214. u16 txmaxp, txcsr, rxmaxp, rxcsr;
  215. u16 rxfifoadd, txfifoadd;
  216. u8 txtype, txinterval, rxtype, rxinterval;
  217. u8 rxfifosz, txfifosz;
  218. u8 txfunaddr, txhubaddr, txhubport;
  219. u8 rxfunaddr, rxhubaddr, rxhubport;
  220. };
  221. struct musb_context_registers {
  222. u8 power;
  223. u8 intrusbe;
  224. u16 frame;
  225. u8 index, testmode;
  226. u8 devctl, busctl, misc;
  227. u32 otg_interfsel;
  228. struct musb_csr_regs index_regs[MUSB_C_NUM_EPS];
  229. };
  230. /*
  231. * struct musb - Driver instance data.
  232. */
  233. struct musb {
  234. /* device lock */
  235. spinlock_t lock;
  236. spinlock_t list_lock; /* resume work list lock */
  237. struct musb_io io;
  238. const struct musb_platform_ops *ops;
  239. struct musb_context_registers context;
  240. irqreturn_t (*isr)(int, void *);
  241. struct delayed_work irq_work;
  242. struct delayed_work deassert_reset_work;
  243. struct delayed_work finish_resume_work;
  244. struct delayed_work gadget_work;
  245. u16 hwvers;
  246. u16 intrrxe;
  247. u16 intrtxe;
  248. /* this hub status bit is reserved by USB 2.0 and not seen by usbcore */
  249. #define MUSB_PORT_STAT_RESUME (1 << 31)
  250. u32 port1_status;
  251. unsigned long rh_timer;
  252. enum musb_h_ep0_state ep0_stage;
  253. /* bulk traffic normally dedicates endpoint hardware, and each
  254. * direction has its own ring of host side endpoints.
  255. * we try to progress the transfer at the head of each endpoint's
  256. * queue until it completes or NAKs too much; then we try the next
  257. * endpoint.
  258. */
  259. struct musb_hw_ep *bulk_ep;
  260. struct list_head control; /* of musb_qh */
  261. struct list_head in_bulk; /* of musb_qh */
  262. struct list_head out_bulk; /* of musb_qh */
  263. struct list_head pending_list; /* pending work list */
  264. struct timer_list otg_timer;
  265. struct timer_list dev_timer;
  266. struct notifier_block nb;
  267. struct dma_controller *dma_controller;
  268. struct device *controller;
  269. void __iomem *ctrl_base;
  270. void __iomem *mregs;
  271. #if IS_ENABLED(CONFIG_USB_MUSB_TUSB6010)
  272. dma_addr_t async;
  273. dma_addr_t sync;
  274. void __iomem *sync_va;
  275. u8 tusb_revision;
  276. #endif
  277. /* passed down from chip/board specific irq handlers */
  278. u8 int_usb;
  279. u16 int_rx;
  280. u16 int_tx;
  281. struct usb_phy *xceiv;
  282. struct phy *phy;
  283. enum usb_otg_state otg_state;
  284. int nIrq;
  285. unsigned irq_wake:1;
  286. struct musb_hw_ep endpoints[MUSB_C_NUM_EPS];
  287. #define control_ep endpoints
  288. #define VBUSERR_RETRY_COUNT 3
  289. u16 vbuserr_retry;
  290. u16 epmask;
  291. u8 nr_endpoints;
  292. u8 min_power; /* vbus for periph, in mA/2 */
  293. enum musb_mode port_mode;
  294. bool session;
  295. unsigned long quirk_retries;
  296. bool is_host;
  297. int a_wait_bcon; /* VBUS timeout in msecs */
  298. unsigned long idle_timeout; /* Next timeout in jiffies */
  299. unsigned is_initialized:1;
  300. unsigned is_runtime_suspended:1;
  301. /* active means connected and not suspended */
  302. unsigned is_active:1;
  303. unsigned is_multipoint:1;
  304. unsigned hb_iso_rx:1; /* high bandwidth iso rx? */
  305. unsigned hb_iso_tx:1; /* high bandwidth iso tx? */
  306. unsigned dyn_fifo:1; /* dynamic FIFO supported? */
  307. unsigned bulk_split:1;
  308. #define can_bulk_split(musb, type) \
  309. (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bulk_split)
  310. unsigned bulk_combine:1;
  311. #define can_bulk_combine(musb, type) \
  312. (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bulk_combine)
  313. /* is_suspended means USB B_PERIPHERAL suspend */
  314. unsigned is_suspended:1;
  315. /* may_wakeup means remote wakeup is enabled */
  316. unsigned may_wakeup:1;
  317. /* is_self_powered is reported in device status and the
  318. * config descriptor. is_bus_powered means B_PERIPHERAL
  319. * draws some VBUS current; both can be true.
  320. */
  321. unsigned is_self_powered:1;
  322. unsigned is_bus_powered:1;
  323. unsigned set_address:1;
  324. unsigned test_mode:1;
  325. unsigned softconnect:1;
  326. unsigned flush_irq_work:1;
  327. u8 address;
  328. u8 test_mode_nr;
  329. u16 ackpend; /* ep0 */
  330. enum musb_g_ep0_state ep0_state;
  331. struct usb_gadget g; /* the gadget */
  332. struct usb_gadget_driver *gadget_driver; /* its driver */
  333. struct usb_hcd *hcd; /* the usb hcd */
  334. const struct musb_hdrc_config *config;
  335. int xceiv_old_state;
  336. #ifdef CONFIG_DEBUG_FS
  337. struct dentry *debugfs_root;
  338. #endif
  339. };
  340. /* This must be included after struct musb is defined */
  341. #include "musb_regs.h"
  342. static inline struct musb *gadget_to_musb(struct usb_gadget *g)
  343. {
  344. return container_of(g, struct musb, g);
  345. }
  346. static inline char *musb_ep_xfertype_string(u8 type)
  347. {
  348. char *s;
  349. switch (type) {
  350. case USB_ENDPOINT_XFER_CONTROL:
  351. s = "ctrl";
  352. break;
  353. case USB_ENDPOINT_XFER_ISOC:
  354. s = "iso";
  355. break;
  356. case USB_ENDPOINT_XFER_BULK:
  357. s = "bulk";
  358. break;
  359. case USB_ENDPOINT_XFER_INT:
  360. s = "int";
  361. break;
  362. default:
  363. s = "";
  364. break;
  365. }
  366. return s;
  367. }
  368. static inline int musb_read_fifosize(struct musb *musb,
  369. struct musb_hw_ep *hw_ep, u8 epnum)
  370. {
  371. void __iomem *mbase = musb->mregs;
  372. u8 reg = 0;
  373. /* read from core using indexed model */
  374. reg = musb_readb(mbase, musb->io.ep_offset(epnum, MUSB_FIFOSIZE));
  375. /* 0's returned when no more endpoints */
  376. if (!reg)
  377. return -ENODEV;
  378. musb->nr_endpoints++;
  379. musb->epmask |= (1 << epnum);
  380. hw_ep->max_packet_sz_tx = 1 << (reg & 0x0f);
  381. /* shared TX/RX FIFO? */
  382. if ((reg & 0xf0) == 0xf0) {
  383. hw_ep->max_packet_sz_rx = hw_ep->max_packet_sz_tx;
  384. hw_ep->is_shared_fifo = true;
  385. return 0;
  386. } else {
  387. hw_ep->max_packet_sz_rx = 1 << ((reg & 0xf0) >> 4);
  388. hw_ep->is_shared_fifo = false;
  389. }
  390. return 0;
  391. }
  392. static inline void musb_configure_ep0(struct musb *musb)
  393. {
  394. musb->endpoints[0].max_packet_sz_tx = MUSB_EP0_FIFOSIZE;
  395. musb->endpoints[0].max_packet_sz_rx = MUSB_EP0_FIFOSIZE;
  396. musb->endpoints[0].is_shared_fifo = true;
  397. }
  398. /***************************** Glue it together *****************************/
  399. extern const char musb_driver_name[];
  400. extern void musb_stop(struct musb *musb);
  401. extern void musb_start(struct musb *musb);
  402. extern void musb_write_fifo(struct musb_hw_ep *ep, u16 len, const u8 *src);
  403. extern void musb_read_fifo(struct musb_hw_ep *ep, u16 len, u8 *dst);
  404. extern int musb_set_host(struct musb *musb);
  405. extern int musb_set_peripheral(struct musb *musb);
  406. extern void musb_load_testpacket(struct musb *);
  407. extern irqreturn_t musb_interrupt(struct musb *);
  408. extern void musb_hnp_stop(struct musb *musb);
  409. int musb_queue_resume_work(struct musb *musb,
  410. int (*callback)(struct musb *musb, void *data),
  411. void *data);
  412. static inline void musb_platform_set_vbus(struct musb *musb, int is_on)
  413. {
  414. if (musb->ops->set_vbus)
  415. musb->ops->set_vbus(musb, is_on);
  416. }
  417. static inline void musb_platform_enable(struct musb *musb)
  418. {
  419. if (musb->ops->enable)
  420. musb->ops->enable(musb);
  421. }
  422. static inline void musb_platform_disable(struct musb *musb)
  423. {
  424. if (musb->ops->disable)
  425. musb->ops->disable(musb);
  426. }
  427. static inline int musb_platform_set_mode(struct musb *musb, u8 mode)
  428. {
  429. if (!musb->ops->set_mode)
  430. return 0;
  431. return musb->ops->set_mode(musb, mode);
  432. }
  433. static inline void musb_platform_try_idle(struct musb *musb,
  434. unsigned long timeout)
  435. {
  436. if (musb->ops->try_idle)
  437. musb->ops->try_idle(musb, timeout);
  438. }
  439. static inline int musb_platform_recover(struct musb *musb)
  440. {
  441. if (!musb->ops->recover)
  442. return 0;
  443. return musb->ops->recover(musb);
  444. }
  445. static inline int musb_platform_get_vbus_status(struct musb *musb)
  446. {
  447. if (!musb->ops->vbus_status)
  448. return -EINVAL;
  449. return musb->ops->vbus_status(musb);
  450. }
  451. static inline int musb_platform_init(struct musb *musb)
  452. {
  453. if (!musb->ops->init)
  454. return -EINVAL;
  455. return musb->ops->init(musb);
  456. }
  457. static inline int musb_platform_exit(struct musb *musb)
  458. {
  459. if (!musb->ops->exit)
  460. return -EINVAL;
  461. return musb->ops->exit(musb);
  462. }
  463. static inline void musb_platform_pre_root_reset_end(struct musb *musb)
  464. {
  465. if (musb->ops->pre_root_reset_end)
  466. musb->ops->pre_root_reset_end(musb);
  467. }
  468. static inline void musb_platform_post_root_reset_end(struct musb *musb)
  469. {
  470. if (musb->ops->post_root_reset_end)
  471. musb->ops->post_root_reset_end(musb);
  472. }
  473. static inline void musb_platform_clear_ep_rxintr(struct musb *musb, int epnum)
  474. {
  475. if (musb->ops->clear_ep_rxintr)
  476. musb->ops->clear_ep_rxintr(musb, epnum);
  477. }
  478. static inline void musb_set_state(struct musb *musb,
  479. enum usb_otg_state otg_state)
  480. {
  481. if (musb->xceiv)
  482. musb->xceiv->otg->state = otg_state;
  483. else
  484. musb->otg_state = otg_state;
  485. }
  486. static inline enum usb_otg_state musb_get_state(struct musb *musb)
  487. {
  488. if (musb->xceiv)
  489. return musb->xceiv->otg->state;
  490. return musb->otg_state;
  491. }
  492. static inline const char *musb_otg_state_string(struct musb *musb)
  493. {
  494. return usb_otg_state_string(musb_get_state(musb));
  495. }
  496. /*
  497. * gets the "dr_mode" property from DT and converts it into musb_mode
  498. * if the property is not found or not recognized returns MUSB_OTG
  499. */
  500. extern enum musb_mode musb_get_mode(struct device *dev);
  501. #endif /* __MUSB_CORE_H__ */