musb_core.h 15 KB

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