musb_core.h 15 KB

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