musb_core.h 15 KB

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