r8a66597.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * R8A66597 HCD (Host Controller Driver)
  4. *
  5. * Copyright (C) 2006-2007 Renesas Solutions Corp.
  6. * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
  7. * Portions Copyright (C) 2004-2005 David Brownell
  8. * Portions Copyright (C) 1999 Roman Weissgaerber
  9. *
  10. * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
  11. */
  12. #ifndef __R8A66597_H__
  13. #define __R8A66597_H__
  14. #include <linux/clk.h>
  15. #include <linux/usb/r8a66597.h>
  16. #define R8A66597_MAX_NUM_PIPE 10
  17. #define R8A66597_BUF_BSIZE 8
  18. #define R8A66597_MAX_DEVICE 10
  19. #define R8A66597_MAX_ROOT_HUB 2
  20. #define R8A66597_MAX_SAMPLING 5
  21. #define R8A66597_RH_POLL_TIME 10
  22. #define R8A66597_MAX_DMA_CHANNEL 2
  23. #define R8A66597_PIPE_NO_DMA R8A66597_MAX_DMA_CHANNEL
  24. #define check_bulk_or_isoc(pipenum) ((pipenum >= 1 && pipenum <= 5))
  25. #define check_interrupt(pipenum) ((pipenum >= 6 && pipenum <= 9))
  26. #define make_devsel(addr) (addr << 12)
  27. struct r8a66597_pipe_info {
  28. unsigned long timer_interval;
  29. u16 pipenum;
  30. u16 address; /* R8A66597 HCD usb address */
  31. u16 epnum;
  32. u16 maxpacket;
  33. u16 type;
  34. u16 bufnum;
  35. u16 buf_bsize;
  36. u16 interval;
  37. u16 dir_in;
  38. };
  39. struct r8a66597_pipe {
  40. struct r8a66597_pipe_info info;
  41. unsigned long fifoaddr;
  42. unsigned long fifosel;
  43. unsigned long fifoctr;
  44. unsigned long pipectr;
  45. unsigned long pipetre;
  46. unsigned long pipetrn;
  47. };
  48. struct r8a66597_td {
  49. struct r8a66597_pipe *pipe;
  50. struct urb *urb;
  51. struct list_head queue;
  52. u16 type;
  53. u16 pipenum;
  54. int iso_cnt;
  55. u16 address; /* R8A66597's USB address */
  56. u16 maxpacket;
  57. unsigned zero_packet:1;
  58. unsigned short_packet:1;
  59. unsigned set_address:1;
  60. };
  61. struct r8a66597_device {
  62. u16 address; /* R8A66597's USB address */
  63. u16 hub_port;
  64. u16 root_port;
  65. unsigned short ep_in_toggle;
  66. unsigned short ep_out_toggle;
  67. unsigned char pipe_cnt[R8A66597_MAX_NUM_PIPE];
  68. unsigned char dma_map;
  69. enum usb_device_state state;
  70. struct usb_device *udev;
  71. int usb_address;
  72. struct list_head device_list;
  73. };
  74. struct r8a66597_root_hub {
  75. u32 port;
  76. u16 old_syssts;
  77. int scount;
  78. struct r8a66597_device *dev;
  79. };
  80. struct r8a66597;
  81. struct r8a66597_timers {
  82. struct timer_list td;
  83. struct timer_list interval;
  84. struct r8a66597 *r8a66597;
  85. };
  86. struct r8a66597 {
  87. spinlock_t lock;
  88. void __iomem *reg;
  89. struct clk *clk;
  90. struct r8a66597_platdata *pdata;
  91. struct r8a66597_device device0;
  92. struct r8a66597_root_hub root_hub[R8A66597_MAX_ROOT_HUB];
  93. struct list_head pipe_queue[R8A66597_MAX_NUM_PIPE];
  94. struct timer_list rh_timer;
  95. struct r8a66597_timers timers[R8A66597_MAX_NUM_PIPE];
  96. unsigned short address_map;
  97. unsigned short timeout_map;
  98. unsigned short interval_map;
  99. unsigned char pipe_cnt[R8A66597_MAX_NUM_PIPE];
  100. unsigned char dma_map;
  101. unsigned int max_root_hub;
  102. struct list_head child_device;
  103. unsigned long child_connect_map[4];
  104. unsigned bus_suspended:1;
  105. unsigned irq_sense_low:1;
  106. };
  107. static inline struct r8a66597 *hcd_to_r8a66597(struct usb_hcd *hcd)
  108. {
  109. return (struct r8a66597 *)(hcd->hcd_priv);
  110. }
  111. static inline struct usb_hcd *r8a66597_to_hcd(struct r8a66597 *r8a66597)
  112. {
  113. return container_of((void *)r8a66597, struct usb_hcd, hcd_priv);
  114. }
  115. static inline struct r8a66597_td *r8a66597_get_td(struct r8a66597 *r8a66597,
  116. u16 pipenum)
  117. {
  118. if (unlikely(list_empty(&r8a66597->pipe_queue[pipenum])))
  119. return NULL;
  120. return list_entry(r8a66597->pipe_queue[pipenum].next,
  121. struct r8a66597_td, queue);
  122. }
  123. static inline struct urb *r8a66597_get_urb(struct r8a66597 *r8a66597,
  124. u16 pipenum)
  125. {
  126. struct r8a66597_td *td;
  127. td = r8a66597_get_td(r8a66597, pipenum);
  128. return (td ? td->urb : NULL);
  129. }
  130. static inline u16 r8a66597_read(struct r8a66597 *r8a66597, unsigned long offset)
  131. {
  132. return ioread16(r8a66597->reg + offset);
  133. }
  134. static inline void r8a66597_read_fifo(struct r8a66597 *r8a66597,
  135. unsigned long offset, u16 *buf,
  136. int len)
  137. {
  138. void __iomem *fifoaddr = r8a66597->reg + offset;
  139. unsigned long count;
  140. if (r8a66597->pdata->on_chip) {
  141. count = len / 4;
  142. ioread32_rep(fifoaddr, buf, count);
  143. if (len & 0x00000003) {
  144. unsigned long tmp = ioread32(fifoaddr);
  145. memcpy((unsigned char *)buf + count * 4, &tmp,
  146. len & 0x03);
  147. }
  148. } else {
  149. len = (len + 1) / 2;
  150. ioread16_rep(fifoaddr, buf, len);
  151. }
  152. }
  153. static inline void r8a66597_write(struct r8a66597 *r8a66597, u16 val,
  154. unsigned long offset)
  155. {
  156. iowrite16(val, r8a66597->reg + offset);
  157. }
  158. static inline void r8a66597_mdfy(struct r8a66597 *r8a66597,
  159. u16 val, u16 pat, unsigned long offset)
  160. {
  161. u16 tmp;
  162. tmp = r8a66597_read(r8a66597, offset);
  163. tmp = tmp & (~pat);
  164. tmp = tmp | val;
  165. r8a66597_write(r8a66597, tmp, offset);
  166. }
  167. #define r8a66597_bclr(r8a66597, val, offset) \
  168. r8a66597_mdfy(r8a66597, 0, val, offset)
  169. #define r8a66597_bset(r8a66597, val, offset) \
  170. r8a66597_mdfy(r8a66597, val, 0, offset)
  171. static inline void r8a66597_write_fifo(struct r8a66597 *r8a66597,
  172. struct r8a66597_pipe *pipe, u16 *buf,
  173. int len)
  174. {
  175. void __iomem *fifoaddr = r8a66597->reg + pipe->fifoaddr;
  176. unsigned long count;
  177. unsigned char *pb;
  178. int i;
  179. if (r8a66597->pdata->on_chip) {
  180. count = len / 4;
  181. iowrite32_rep(fifoaddr, buf, count);
  182. if (len & 0x00000003) {
  183. pb = (unsigned char *)buf + count * 4;
  184. for (i = 0; i < (len & 0x00000003); i++) {
  185. if (r8a66597_read(r8a66597, CFIFOSEL) & BIGEND)
  186. iowrite8(pb[i], fifoaddr + i);
  187. else
  188. iowrite8(pb[i], fifoaddr + 3 - i);
  189. }
  190. }
  191. } else {
  192. int odd = len & 0x0001;
  193. len = len / 2;
  194. iowrite16_rep(fifoaddr, buf, len);
  195. if (unlikely(odd)) {
  196. buf = &buf[len];
  197. if (r8a66597->pdata->wr0_shorted_to_wr1)
  198. r8a66597_bclr(r8a66597, MBW_16, pipe->fifosel);
  199. iowrite8((unsigned char)*buf, fifoaddr);
  200. if (r8a66597->pdata->wr0_shorted_to_wr1)
  201. r8a66597_bset(r8a66597, MBW_16, pipe->fifosel);
  202. }
  203. }
  204. }
  205. static inline unsigned long get_syscfg_reg(int port)
  206. {
  207. return port == 0 ? SYSCFG0 : SYSCFG1;
  208. }
  209. static inline unsigned long get_syssts_reg(int port)
  210. {
  211. return port == 0 ? SYSSTS0 : SYSSTS1;
  212. }
  213. static inline unsigned long get_dvstctr_reg(int port)
  214. {
  215. return port == 0 ? DVSTCTR0 : DVSTCTR1;
  216. }
  217. static inline unsigned long get_dmacfg_reg(int port)
  218. {
  219. return port == 0 ? DMA0CFG : DMA1CFG;
  220. }
  221. static inline unsigned long get_intenb_reg(int port)
  222. {
  223. return port == 0 ? INTENB1 : INTENB2;
  224. }
  225. static inline unsigned long get_intsts_reg(int port)
  226. {
  227. return port == 0 ? INTSTS1 : INTSTS2;
  228. }
  229. static inline u16 get_rh_usb_speed(struct r8a66597 *r8a66597, int port)
  230. {
  231. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  232. return r8a66597_read(r8a66597, dvstctr_reg) & RHST;
  233. }
  234. static inline void r8a66597_port_power(struct r8a66597 *r8a66597, int port,
  235. int power)
  236. {
  237. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  238. if (r8a66597->pdata->port_power) {
  239. r8a66597->pdata->port_power(port, power);
  240. } else {
  241. if (power)
  242. r8a66597_bset(r8a66597, VBOUT, dvstctr_reg);
  243. else
  244. r8a66597_bclr(r8a66597, VBOUT, dvstctr_reg);
  245. }
  246. }
  247. static inline u16 get_xtal_from_pdata(struct r8a66597_platdata *pdata)
  248. {
  249. u16 clock = 0;
  250. switch (pdata->xtal) {
  251. case R8A66597_PLATDATA_XTAL_12MHZ:
  252. clock = XTAL12;
  253. break;
  254. case R8A66597_PLATDATA_XTAL_24MHZ:
  255. clock = XTAL24;
  256. break;
  257. case R8A66597_PLATDATA_XTAL_48MHZ:
  258. clock = XTAL48;
  259. break;
  260. default:
  261. printk(KERN_ERR "r8a66597: platdata clock is wrong.\n");
  262. break;
  263. }
  264. return clock;
  265. }
  266. #define get_pipectr_addr(pipenum) (PIPE1CTR + (pipenum - 1) * 2)
  267. #define get_pipetre_addr(pipenum) (PIPE1TRE + (pipenum - 1) * 4)
  268. #define get_pipetrn_addr(pipenum) (PIPE1TRN + (pipenum - 1) * 4)
  269. #define get_devadd_addr(address) (DEVADD0 + address * 2)
  270. #define enable_irq_ready(r8a66597, pipenum) \
  271. enable_pipe_irq(r8a66597, pipenum, BRDYENB)
  272. #define disable_irq_ready(r8a66597, pipenum) \
  273. disable_pipe_irq(r8a66597, pipenum, BRDYENB)
  274. #define enable_irq_empty(r8a66597, pipenum) \
  275. enable_pipe_irq(r8a66597, pipenum, BEMPENB)
  276. #define disable_irq_empty(r8a66597, pipenum) \
  277. disable_pipe_irq(r8a66597, pipenum, BEMPENB)
  278. #define enable_irq_nrdy(r8a66597, pipenum) \
  279. enable_pipe_irq(r8a66597, pipenum, NRDYENB)
  280. #define disable_irq_nrdy(r8a66597, pipenum) \
  281. disable_pipe_irq(r8a66597, pipenum, NRDYENB)
  282. #endif /* __R8A66597_H__ */