m66592-udc.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /*
  2. * M66592 UDC (USB gadget)
  3. *
  4. * Copyright (C) 2006-2007 Renesas Solutions Corp.
  5. *
  6. * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/err.h>
  29. #include <linux/usb/ch9.h>
  30. #include <linux/usb/gadget.h>
  31. #include "m66592-udc.h"
  32. MODULE_DESCRIPTION("M66592 USB gadget driver");
  33. MODULE_LICENSE("GPL");
  34. MODULE_AUTHOR("Yoshihiro Shimoda");
  35. MODULE_ALIAS("platform:m66592_udc");
  36. #define DRIVER_VERSION "21 July 2009"
  37. static const char udc_name[] = "m66592_udc";
  38. static const char *m66592_ep_name[] = {
  39. "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7"
  40. };
  41. static void disable_controller(struct m66592 *m66592);
  42. static void irq_ep0_write(struct m66592_ep *ep, struct m66592_request *req);
  43. static void irq_packet_write(struct m66592_ep *ep, struct m66592_request *req);
  44. static int m66592_queue(struct usb_ep *_ep, struct usb_request *_req,
  45. gfp_t gfp_flags);
  46. static void transfer_complete(struct m66592_ep *ep,
  47. struct m66592_request *req, int status);
  48. /*-------------------------------------------------------------------------*/
  49. static inline u16 get_usb_speed(struct m66592 *m66592)
  50. {
  51. return (m66592_read(m66592, M66592_DVSTCTR) & M66592_RHST);
  52. }
  53. static void enable_pipe_irq(struct m66592 *m66592, u16 pipenum,
  54. unsigned long reg)
  55. {
  56. u16 tmp;
  57. tmp = m66592_read(m66592, M66592_INTENB0);
  58. m66592_bclr(m66592, M66592_BEMPE | M66592_NRDYE | M66592_BRDYE,
  59. M66592_INTENB0);
  60. m66592_bset(m66592, (1 << pipenum), reg);
  61. m66592_write(m66592, tmp, M66592_INTENB0);
  62. }
  63. static void disable_pipe_irq(struct m66592 *m66592, u16 pipenum,
  64. unsigned long reg)
  65. {
  66. u16 tmp;
  67. tmp = m66592_read(m66592, M66592_INTENB0);
  68. m66592_bclr(m66592, M66592_BEMPE | M66592_NRDYE | M66592_BRDYE,
  69. M66592_INTENB0);
  70. m66592_bclr(m66592, (1 << pipenum), reg);
  71. m66592_write(m66592, tmp, M66592_INTENB0);
  72. }
  73. static void m66592_usb_connect(struct m66592 *m66592)
  74. {
  75. m66592_bset(m66592, M66592_CTRE, M66592_INTENB0);
  76. m66592_bset(m66592, M66592_WDST | M66592_RDST | M66592_CMPL,
  77. M66592_INTENB0);
  78. m66592_bset(m66592, M66592_BEMPE | M66592_BRDYE, M66592_INTENB0);
  79. m66592_bset(m66592, M66592_DPRPU, M66592_SYSCFG);
  80. }
  81. static void m66592_usb_disconnect(struct m66592 *m66592)
  82. __releases(m66592->lock)
  83. __acquires(m66592->lock)
  84. {
  85. m66592_bclr(m66592, M66592_CTRE, M66592_INTENB0);
  86. m66592_bclr(m66592, M66592_WDST | M66592_RDST | M66592_CMPL,
  87. M66592_INTENB0);
  88. m66592_bclr(m66592, M66592_BEMPE | M66592_BRDYE, M66592_INTENB0);
  89. m66592_bclr(m66592, M66592_DPRPU, M66592_SYSCFG);
  90. m66592->gadget.speed = USB_SPEED_UNKNOWN;
  91. spin_unlock(&m66592->lock);
  92. m66592->driver->disconnect(&m66592->gadget);
  93. spin_lock(&m66592->lock);
  94. disable_controller(m66592);
  95. INIT_LIST_HEAD(&m66592->ep[0].queue);
  96. }
  97. static inline u16 control_reg_get_pid(struct m66592 *m66592, u16 pipenum)
  98. {
  99. u16 pid = 0;
  100. unsigned long offset;
  101. if (pipenum == 0)
  102. pid = m66592_read(m66592, M66592_DCPCTR) & M66592_PID;
  103. else if (pipenum < M66592_MAX_NUM_PIPE) {
  104. offset = get_pipectr_addr(pipenum);
  105. pid = m66592_read(m66592, offset) & M66592_PID;
  106. } else
  107. pr_err("unexpect pipe num (%d)\n", pipenum);
  108. return pid;
  109. }
  110. static inline void control_reg_set_pid(struct m66592 *m66592, u16 pipenum,
  111. u16 pid)
  112. {
  113. unsigned long offset;
  114. if (pipenum == 0)
  115. m66592_mdfy(m66592, pid, M66592_PID, M66592_DCPCTR);
  116. else if (pipenum < M66592_MAX_NUM_PIPE) {
  117. offset = get_pipectr_addr(pipenum);
  118. m66592_mdfy(m66592, pid, M66592_PID, offset);
  119. } else
  120. pr_err("unexpect pipe num (%d)\n", pipenum);
  121. }
  122. static inline void pipe_start(struct m66592 *m66592, u16 pipenum)
  123. {
  124. control_reg_set_pid(m66592, pipenum, M66592_PID_BUF);
  125. }
  126. static inline void pipe_stop(struct m66592 *m66592, u16 pipenum)
  127. {
  128. control_reg_set_pid(m66592, pipenum, M66592_PID_NAK);
  129. }
  130. static inline void pipe_stall(struct m66592 *m66592, u16 pipenum)
  131. {
  132. control_reg_set_pid(m66592, pipenum, M66592_PID_STALL);
  133. }
  134. static inline u16 control_reg_get(struct m66592 *m66592, u16 pipenum)
  135. {
  136. u16 ret = 0;
  137. unsigned long offset;
  138. if (pipenum == 0)
  139. ret = m66592_read(m66592, M66592_DCPCTR);
  140. else if (pipenum < M66592_MAX_NUM_PIPE) {
  141. offset = get_pipectr_addr(pipenum);
  142. ret = m66592_read(m66592, offset);
  143. } else
  144. pr_err("unexpect pipe num (%d)\n", pipenum);
  145. return ret;
  146. }
  147. static inline void control_reg_sqclr(struct m66592 *m66592, u16 pipenum)
  148. {
  149. unsigned long offset;
  150. pipe_stop(m66592, pipenum);
  151. if (pipenum == 0)
  152. m66592_bset(m66592, M66592_SQCLR, M66592_DCPCTR);
  153. else if (pipenum < M66592_MAX_NUM_PIPE) {
  154. offset = get_pipectr_addr(pipenum);
  155. m66592_bset(m66592, M66592_SQCLR, offset);
  156. } else
  157. pr_err("unexpect pipe num(%d)\n", pipenum);
  158. }
  159. static inline int get_buffer_size(struct m66592 *m66592, u16 pipenum)
  160. {
  161. u16 tmp;
  162. int size;
  163. if (pipenum == 0) {
  164. tmp = m66592_read(m66592, M66592_DCPCFG);
  165. if ((tmp & M66592_CNTMD) != 0)
  166. size = 256;
  167. else {
  168. tmp = m66592_read(m66592, M66592_DCPMAXP);
  169. size = tmp & M66592_MAXP;
  170. }
  171. } else {
  172. m66592_write(m66592, pipenum, M66592_PIPESEL);
  173. tmp = m66592_read(m66592, M66592_PIPECFG);
  174. if ((tmp & M66592_CNTMD) != 0) {
  175. tmp = m66592_read(m66592, M66592_PIPEBUF);
  176. size = ((tmp >> 10) + 1) * 64;
  177. } else {
  178. tmp = m66592_read(m66592, M66592_PIPEMAXP);
  179. size = tmp & M66592_MXPS;
  180. }
  181. }
  182. return size;
  183. }
  184. static inline void pipe_change(struct m66592 *m66592, u16 pipenum)
  185. {
  186. struct m66592_ep *ep = m66592->pipenum2ep[pipenum];
  187. unsigned short mbw;
  188. if (ep->use_dma)
  189. return;
  190. m66592_mdfy(m66592, pipenum, M66592_CURPIPE, ep->fifosel);
  191. ndelay(450);
  192. if (m66592->pdata->on_chip)
  193. mbw = M66592_MBW_32;
  194. else
  195. mbw = M66592_MBW_16;
  196. m66592_bset(m66592, mbw, ep->fifosel);
  197. }
  198. static int pipe_buffer_setting(struct m66592 *m66592,
  199. struct m66592_pipe_info *info)
  200. {
  201. u16 bufnum = 0, buf_bsize = 0;
  202. u16 pipecfg = 0;
  203. if (info->pipe == 0)
  204. return -EINVAL;
  205. m66592_write(m66592, info->pipe, M66592_PIPESEL);
  206. if (info->dir_in)
  207. pipecfg |= M66592_DIR;
  208. pipecfg |= info->type;
  209. pipecfg |= info->epnum;
  210. switch (info->type) {
  211. case M66592_INT:
  212. bufnum = 4 + (info->pipe - M66592_BASE_PIPENUM_INT);
  213. buf_bsize = 0;
  214. break;
  215. case M66592_BULK:
  216. /* isochronous pipes may be used as bulk pipes */
  217. if (info->pipe >= M66592_BASE_PIPENUM_BULK)
  218. bufnum = info->pipe - M66592_BASE_PIPENUM_BULK;
  219. else
  220. bufnum = info->pipe - M66592_BASE_PIPENUM_ISOC;
  221. bufnum = M66592_BASE_BUFNUM + (bufnum * 16);
  222. buf_bsize = 7;
  223. pipecfg |= M66592_DBLB;
  224. if (!info->dir_in)
  225. pipecfg |= M66592_SHTNAK;
  226. break;
  227. case M66592_ISO:
  228. bufnum = M66592_BASE_BUFNUM +
  229. (info->pipe - M66592_BASE_PIPENUM_ISOC) * 16;
  230. buf_bsize = 7;
  231. break;
  232. }
  233. if (buf_bsize && ((bufnum + 16) >= M66592_MAX_BUFNUM)) {
  234. pr_err("m66592 pipe memory is insufficient\n");
  235. return -ENOMEM;
  236. }
  237. m66592_write(m66592, pipecfg, M66592_PIPECFG);
  238. m66592_write(m66592, (buf_bsize << 10) | (bufnum), M66592_PIPEBUF);
  239. m66592_write(m66592, info->maxpacket, M66592_PIPEMAXP);
  240. if (info->interval)
  241. info->interval--;
  242. m66592_write(m66592, info->interval, M66592_PIPEPERI);
  243. return 0;
  244. }
  245. static void pipe_buffer_release(struct m66592 *m66592,
  246. struct m66592_pipe_info *info)
  247. {
  248. if (info->pipe == 0)
  249. return;
  250. if (is_bulk_pipe(info->pipe)) {
  251. m66592->bulk--;
  252. } else if (is_interrupt_pipe(info->pipe))
  253. m66592->interrupt--;
  254. else if (is_isoc_pipe(info->pipe)) {
  255. m66592->isochronous--;
  256. if (info->type == M66592_BULK)
  257. m66592->bulk--;
  258. } else
  259. pr_err("ep_release: unexpect pipenum (%d)\n",
  260. info->pipe);
  261. }
  262. static void pipe_initialize(struct m66592_ep *ep)
  263. {
  264. struct m66592 *m66592 = ep->m66592;
  265. unsigned short mbw;
  266. m66592_mdfy(m66592, 0, M66592_CURPIPE, ep->fifosel);
  267. m66592_write(m66592, M66592_ACLRM, ep->pipectr);
  268. m66592_write(m66592, 0, ep->pipectr);
  269. m66592_write(m66592, M66592_SQCLR, ep->pipectr);
  270. if (ep->use_dma) {
  271. m66592_mdfy(m66592, ep->pipenum, M66592_CURPIPE, ep->fifosel);
  272. ndelay(450);
  273. if (m66592->pdata->on_chip)
  274. mbw = M66592_MBW_32;
  275. else
  276. mbw = M66592_MBW_16;
  277. m66592_bset(m66592, mbw, ep->fifosel);
  278. }
  279. }
  280. static void m66592_ep_setting(struct m66592 *m66592, struct m66592_ep *ep,
  281. const struct usb_endpoint_descriptor *desc,
  282. u16 pipenum, int dma)
  283. {
  284. if ((pipenum != 0) && dma) {
  285. if (m66592->num_dma == 0) {
  286. m66592->num_dma++;
  287. ep->use_dma = 1;
  288. ep->fifoaddr = M66592_D0FIFO;
  289. ep->fifosel = M66592_D0FIFOSEL;
  290. ep->fifoctr = M66592_D0FIFOCTR;
  291. ep->fifotrn = M66592_D0FIFOTRN;
  292. } else if (!m66592->pdata->on_chip && m66592->num_dma == 1) {
  293. m66592->num_dma++;
  294. ep->use_dma = 1;
  295. ep->fifoaddr = M66592_D1FIFO;
  296. ep->fifosel = M66592_D1FIFOSEL;
  297. ep->fifoctr = M66592_D1FIFOCTR;
  298. ep->fifotrn = M66592_D1FIFOTRN;
  299. } else {
  300. ep->use_dma = 0;
  301. ep->fifoaddr = M66592_CFIFO;
  302. ep->fifosel = M66592_CFIFOSEL;
  303. ep->fifoctr = M66592_CFIFOCTR;
  304. ep->fifotrn = 0;
  305. }
  306. } else {
  307. ep->use_dma = 0;
  308. ep->fifoaddr = M66592_CFIFO;
  309. ep->fifosel = M66592_CFIFOSEL;
  310. ep->fifoctr = M66592_CFIFOCTR;
  311. ep->fifotrn = 0;
  312. }
  313. ep->pipectr = get_pipectr_addr(pipenum);
  314. ep->pipenum = pipenum;
  315. ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
  316. m66592->pipenum2ep[pipenum] = ep;
  317. m66592->epaddr2ep[desc->bEndpointAddress&USB_ENDPOINT_NUMBER_MASK] = ep;
  318. INIT_LIST_HEAD(&ep->queue);
  319. }
  320. static void m66592_ep_release(struct m66592_ep *ep)
  321. {
  322. struct m66592 *m66592 = ep->m66592;
  323. u16 pipenum = ep->pipenum;
  324. if (pipenum == 0)
  325. return;
  326. if (ep->use_dma)
  327. m66592->num_dma--;
  328. ep->pipenum = 0;
  329. ep->busy = 0;
  330. ep->use_dma = 0;
  331. }
  332. static int alloc_pipe_config(struct m66592_ep *ep,
  333. const struct usb_endpoint_descriptor *desc)
  334. {
  335. struct m66592 *m66592 = ep->m66592;
  336. struct m66592_pipe_info info;
  337. int dma = 0;
  338. int *counter;
  339. int ret;
  340. ep->desc = desc;
  341. BUG_ON(ep->pipenum);
  342. switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  343. case USB_ENDPOINT_XFER_BULK:
  344. if (m66592->bulk >= M66592_MAX_NUM_BULK) {
  345. if (m66592->isochronous >= M66592_MAX_NUM_ISOC) {
  346. pr_err("bulk pipe is insufficient\n");
  347. return -ENODEV;
  348. } else {
  349. info.pipe = M66592_BASE_PIPENUM_ISOC
  350. + m66592->isochronous;
  351. counter = &m66592->isochronous;
  352. }
  353. } else {
  354. info.pipe = M66592_BASE_PIPENUM_BULK + m66592->bulk;
  355. counter = &m66592->bulk;
  356. }
  357. info.type = M66592_BULK;
  358. dma = 1;
  359. break;
  360. case USB_ENDPOINT_XFER_INT:
  361. if (m66592->interrupt >= M66592_MAX_NUM_INT) {
  362. pr_err("interrupt pipe is insufficient\n");
  363. return -ENODEV;
  364. }
  365. info.pipe = M66592_BASE_PIPENUM_INT + m66592->interrupt;
  366. info.type = M66592_INT;
  367. counter = &m66592->interrupt;
  368. break;
  369. case USB_ENDPOINT_XFER_ISOC:
  370. if (m66592->isochronous >= M66592_MAX_NUM_ISOC) {
  371. pr_err("isochronous pipe is insufficient\n");
  372. return -ENODEV;
  373. }
  374. info.pipe = M66592_BASE_PIPENUM_ISOC + m66592->isochronous;
  375. info.type = M66592_ISO;
  376. counter = &m66592->isochronous;
  377. break;
  378. default:
  379. pr_err("unexpect xfer type\n");
  380. return -EINVAL;
  381. }
  382. ep->type = info.type;
  383. info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  384. info.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
  385. info.interval = desc->bInterval;
  386. if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  387. info.dir_in = 1;
  388. else
  389. info.dir_in = 0;
  390. ret = pipe_buffer_setting(m66592, &info);
  391. if (ret < 0) {
  392. pr_err("pipe_buffer_setting fail\n");
  393. return ret;
  394. }
  395. (*counter)++;
  396. if ((counter == &m66592->isochronous) && info.type == M66592_BULK)
  397. m66592->bulk++;
  398. m66592_ep_setting(m66592, ep, desc, info.pipe, dma);
  399. pipe_initialize(ep);
  400. return 0;
  401. }
  402. static int free_pipe_config(struct m66592_ep *ep)
  403. {
  404. struct m66592 *m66592 = ep->m66592;
  405. struct m66592_pipe_info info;
  406. info.pipe = ep->pipenum;
  407. info.type = ep->type;
  408. pipe_buffer_release(m66592, &info);
  409. m66592_ep_release(ep);
  410. return 0;
  411. }
  412. /*-------------------------------------------------------------------------*/
  413. static void pipe_irq_enable(struct m66592 *m66592, u16 pipenum)
  414. {
  415. enable_irq_ready(m66592, pipenum);
  416. enable_irq_nrdy(m66592, pipenum);
  417. }
  418. static void pipe_irq_disable(struct m66592 *m66592, u16 pipenum)
  419. {
  420. disable_irq_ready(m66592, pipenum);
  421. disable_irq_nrdy(m66592, pipenum);
  422. }
  423. /* if complete is true, gadget driver complete function is not call */
  424. static void control_end(struct m66592 *m66592, unsigned ccpl)
  425. {
  426. m66592->ep[0].internal_ccpl = ccpl;
  427. pipe_start(m66592, 0);
  428. m66592_bset(m66592, M66592_CCPL, M66592_DCPCTR);
  429. }
  430. static void start_ep0_write(struct m66592_ep *ep, struct m66592_request *req)
  431. {
  432. struct m66592 *m66592 = ep->m66592;
  433. pipe_change(m66592, ep->pipenum);
  434. m66592_mdfy(m66592, M66592_ISEL | M66592_PIPE0,
  435. (M66592_ISEL | M66592_CURPIPE),
  436. M66592_CFIFOSEL);
  437. m66592_write(m66592, M66592_BCLR, ep->fifoctr);
  438. if (req->req.length == 0) {
  439. m66592_bset(m66592, M66592_BVAL, ep->fifoctr);
  440. pipe_start(m66592, 0);
  441. transfer_complete(ep, req, 0);
  442. } else {
  443. m66592_write(m66592, ~M66592_BEMP0, M66592_BEMPSTS);
  444. irq_ep0_write(ep, req);
  445. }
  446. }
  447. static void start_packet_write(struct m66592_ep *ep, struct m66592_request *req)
  448. {
  449. struct m66592 *m66592 = ep->m66592;
  450. u16 tmp;
  451. pipe_change(m66592, ep->pipenum);
  452. disable_irq_empty(m66592, ep->pipenum);
  453. pipe_start(m66592, ep->pipenum);
  454. tmp = m66592_read(m66592, ep->fifoctr);
  455. if (unlikely((tmp & M66592_FRDY) == 0))
  456. pipe_irq_enable(m66592, ep->pipenum);
  457. else
  458. irq_packet_write(ep, req);
  459. }
  460. static void start_packet_read(struct m66592_ep *ep, struct m66592_request *req)
  461. {
  462. struct m66592 *m66592 = ep->m66592;
  463. u16 pipenum = ep->pipenum;
  464. if (ep->pipenum == 0) {
  465. m66592_mdfy(m66592, M66592_PIPE0,
  466. (M66592_ISEL | M66592_CURPIPE),
  467. M66592_CFIFOSEL);
  468. m66592_write(m66592, M66592_BCLR, ep->fifoctr);
  469. pipe_start(m66592, pipenum);
  470. pipe_irq_enable(m66592, pipenum);
  471. } else {
  472. if (ep->use_dma) {
  473. m66592_bset(m66592, M66592_TRCLR, ep->fifosel);
  474. pipe_change(m66592, pipenum);
  475. m66592_bset(m66592, M66592_TRENB, ep->fifosel);
  476. m66592_write(m66592,
  477. (req->req.length + ep->ep.maxpacket - 1)
  478. / ep->ep.maxpacket,
  479. ep->fifotrn);
  480. }
  481. pipe_start(m66592, pipenum); /* trigger once */
  482. pipe_irq_enable(m66592, pipenum);
  483. }
  484. }
  485. static void start_packet(struct m66592_ep *ep, struct m66592_request *req)
  486. {
  487. if (ep->desc->bEndpointAddress & USB_DIR_IN)
  488. start_packet_write(ep, req);
  489. else
  490. start_packet_read(ep, req);
  491. }
  492. static void start_ep0(struct m66592_ep *ep, struct m66592_request *req)
  493. {
  494. u16 ctsq;
  495. ctsq = m66592_read(ep->m66592, M66592_INTSTS0) & M66592_CTSQ;
  496. switch (ctsq) {
  497. case M66592_CS_RDDS:
  498. start_ep0_write(ep, req);
  499. break;
  500. case M66592_CS_WRDS:
  501. start_packet_read(ep, req);
  502. break;
  503. case M66592_CS_WRND:
  504. control_end(ep->m66592, 0);
  505. break;
  506. default:
  507. pr_err("start_ep0: unexpect ctsq(%x)\n", ctsq);
  508. break;
  509. }
  510. }
  511. static void init_controller(struct m66592 *m66592)
  512. {
  513. unsigned int endian;
  514. if (m66592->pdata->on_chip) {
  515. if (m66592->pdata->endian)
  516. endian = 0; /* big endian */
  517. else
  518. endian = M66592_LITTLE; /* little endian */
  519. m66592_bset(m66592, M66592_HSE, M66592_SYSCFG); /* High spd */
  520. m66592_bclr(m66592, M66592_USBE, M66592_SYSCFG);
  521. m66592_bclr(m66592, M66592_DPRPU, M66592_SYSCFG);
  522. m66592_bset(m66592, M66592_USBE, M66592_SYSCFG);
  523. /* This is a workaound for SH7722 2nd cut */
  524. m66592_bset(m66592, 0x8000, M66592_DVSTCTR);
  525. m66592_bset(m66592, 0x1000, M66592_TESTMODE);
  526. m66592_bclr(m66592, 0x8000, M66592_DVSTCTR);
  527. m66592_bset(m66592, M66592_INTL, M66592_INTENB1);
  528. m66592_write(m66592, 0, M66592_CFBCFG);
  529. m66592_write(m66592, 0, M66592_D0FBCFG);
  530. m66592_bset(m66592, endian, M66592_CFBCFG);
  531. m66592_bset(m66592, endian, M66592_D0FBCFG);
  532. } else {
  533. unsigned int clock, vif, irq_sense;
  534. if (m66592->pdata->endian)
  535. endian = M66592_BIGEND; /* big endian */
  536. else
  537. endian = 0; /* little endian */
  538. if (m66592->pdata->vif)
  539. vif = M66592_LDRV; /* 3.3v */
  540. else
  541. vif = 0; /* 1.5v */
  542. switch (m66592->pdata->xtal) {
  543. case M66592_PLATDATA_XTAL_12MHZ:
  544. clock = M66592_XTAL12;
  545. break;
  546. case M66592_PLATDATA_XTAL_24MHZ:
  547. clock = M66592_XTAL24;
  548. break;
  549. case M66592_PLATDATA_XTAL_48MHZ:
  550. clock = M66592_XTAL48;
  551. break;
  552. default:
  553. pr_warning("m66592-udc: xtal configuration error\n");
  554. clock = 0;
  555. }
  556. switch (m66592->irq_trigger) {
  557. case IRQF_TRIGGER_LOW:
  558. irq_sense = M66592_INTL;
  559. break;
  560. case IRQF_TRIGGER_FALLING:
  561. irq_sense = 0;
  562. break;
  563. default:
  564. pr_warning("m66592-udc: irq trigger config error\n");
  565. irq_sense = 0;
  566. }
  567. m66592_bset(m66592,
  568. (vif & M66592_LDRV) | (endian & M66592_BIGEND),
  569. M66592_PINCFG);
  570. m66592_bset(m66592, M66592_HSE, M66592_SYSCFG); /* High spd */
  571. m66592_mdfy(m66592, clock & M66592_XTAL, M66592_XTAL,
  572. M66592_SYSCFG);
  573. m66592_bclr(m66592, M66592_USBE, M66592_SYSCFG);
  574. m66592_bclr(m66592, M66592_DPRPU, M66592_SYSCFG);
  575. m66592_bset(m66592, M66592_USBE, M66592_SYSCFG);
  576. m66592_bset(m66592, M66592_XCKE, M66592_SYSCFG);
  577. msleep(3);
  578. m66592_bset(m66592, M66592_RCKE | M66592_PLLC, M66592_SYSCFG);
  579. msleep(1);
  580. m66592_bset(m66592, M66592_SCKE, M66592_SYSCFG);
  581. m66592_bset(m66592, irq_sense & M66592_INTL, M66592_INTENB1);
  582. m66592_write(m66592, M66592_BURST | M66592_CPU_ADR_RD_WR,
  583. M66592_DMA0CFG);
  584. }
  585. }
  586. static void disable_controller(struct m66592 *m66592)
  587. {
  588. if (!m66592->pdata->on_chip) {
  589. m66592_bclr(m66592, M66592_SCKE, M66592_SYSCFG);
  590. udelay(1);
  591. m66592_bclr(m66592, M66592_PLLC, M66592_SYSCFG);
  592. udelay(1);
  593. m66592_bclr(m66592, M66592_RCKE, M66592_SYSCFG);
  594. udelay(1);
  595. m66592_bclr(m66592, M66592_XCKE, M66592_SYSCFG);
  596. }
  597. }
  598. static void m66592_start_xclock(struct m66592 *m66592)
  599. {
  600. u16 tmp;
  601. if (!m66592->pdata->on_chip) {
  602. tmp = m66592_read(m66592, M66592_SYSCFG);
  603. if (!(tmp & M66592_XCKE))
  604. m66592_bset(m66592, M66592_XCKE, M66592_SYSCFG);
  605. }
  606. }
  607. /*-------------------------------------------------------------------------*/
  608. static void transfer_complete(struct m66592_ep *ep,
  609. struct m66592_request *req, int status)
  610. __releases(m66592->lock)
  611. __acquires(m66592->lock)
  612. {
  613. int restart = 0;
  614. if (unlikely(ep->pipenum == 0)) {
  615. if (ep->internal_ccpl) {
  616. ep->internal_ccpl = 0;
  617. return;
  618. }
  619. }
  620. list_del_init(&req->queue);
  621. if (ep->m66592->gadget.speed == USB_SPEED_UNKNOWN)
  622. req->req.status = -ESHUTDOWN;
  623. else
  624. req->req.status = status;
  625. if (!list_empty(&ep->queue))
  626. restart = 1;
  627. spin_unlock(&ep->m66592->lock);
  628. req->req.complete(&ep->ep, &req->req);
  629. spin_lock(&ep->m66592->lock);
  630. if (restart) {
  631. req = list_entry(ep->queue.next, struct m66592_request, queue);
  632. if (ep->desc)
  633. start_packet(ep, req);
  634. }
  635. }
  636. static void irq_ep0_write(struct m66592_ep *ep, struct m66592_request *req)
  637. {
  638. int i;
  639. u16 tmp;
  640. unsigned bufsize;
  641. size_t size;
  642. void *buf;
  643. u16 pipenum = ep->pipenum;
  644. struct m66592 *m66592 = ep->m66592;
  645. pipe_change(m66592, pipenum);
  646. m66592_bset(m66592, M66592_ISEL, ep->fifosel);
  647. i = 0;
  648. do {
  649. tmp = m66592_read(m66592, ep->fifoctr);
  650. if (i++ > 100000) {
  651. pr_err("pipe0 is busy. maybe cpu i/o bus "
  652. "conflict. please power off this controller.");
  653. return;
  654. }
  655. ndelay(1);
  656. } while ((tmp & M66592_FRDY) == 0);
  657. /* prepare parameters */
  658. bufsize = get_buffer_size(m66592, pipenum);
  659. buf = req->req.buf + req->req.actual;
  660. size = min(bufsize, req->req.length - req->req.actual);
  661. /* write fifo */
  662. if (req->req.buf) {
  663. if (size > 0)
  664. m66592_write_fifo(m66592, ep->fifoaddr, buf, size);
  665. if ((size == 0) || ((size % ep->ep.maxpacket) != 0))
  666. m66592_bset(m66592, M66592_BVAL, ep->fifoctr);
  667. }
  668. /* update parameters */
  669. req->req.actual += size;
  670. /* check transfer finish */
  671. if ((!req->req.zero && (req->req.actual == req->req.length))
  672. || (size % ep->ep.maxpacket)
  673. || (size == 0)) {
  674. disable_irq_ready(m66592, pipenum);
  675. disable_irq_empty(m66592, pipenum);
  676. } else {
  677. disable_irq_ready(m66592, pipenum);
  678. enable_irq_empty(m66592, pipenum);
  679. }
  680. pipe_start(m66592, pipenum);
  681. }
  682. static void irq_packet_write(struct m66592_ep *ep, struct m66592_request *req)
  683. {
  684. u16 tmp;
  685. unsigned bufsize;
  686. size_t size;
  687. void *buf;
  688. u16 pipenum = ep->pipenum;
  689. struct m66592 *m66592 = ep->m66592;
  690. pipe_change(m66592, pipenum);
  691. tmp = m66592_read(m66592, ep->fifoctr);
  692. if (unlikely((tmp & M66592_FRDY) == 0)) {
  693. pipe_stop(m66592, pipenum);
  694. pipe_irq_disable(m66592, pipenum);
  695. pr_err("write fifo not ready. pipnum=%d\n", pipenum);
  696. return;
  697. }
  698. /* prepare parameters */
  699. bufsize = get_buffer_size(m66592, pipenum);
  700. buf = req->req.buf + req->req.actual;
  701. size = min(bufsize, req->req.length - req->req.actual);
  702. /* write fifo */
  703. if (req->req.buf) {
  704. m66592_write_fifo(m66592, ep->fifoaddr, buf, size);
  705. if ((size == 0)
  706. || ((size % ep->ep.maxpacket) != 0)
  707. || ((bufsize != ep->ep.maxpacket)
  708. && (bufsize > size)))
  709. m66592_bset(m66592, M66592_BVAL, ep->fifoctr);
  710. }
  711. /* update parameters */
  712. req->req.actual += size;
  713. /* check transfer finish */
  714. if ((!req->req.zero && (req->req.actual == req->req.length))
  715. || (size % ep->ep.maxpacket)
  716. || (size == 0)) {
  717. disable_irq_ready(m66592, pipenum);
  718. enable_irq_empty(m66592, pipenum);
  719. } else {
  720. disable_irq_empty(m66592, pipenum);
  721. pipe_irq_enable(m66592, pipenum);
  722. }
  723. }
  724. static void irq_packet_read(struct m66592_ep *ep, struct m66592_request *req)
  725. {
  726. u16 tmp;
  727. int rcv_len, bufsize, req_len;
  728. int size;
  729. void *buf;
  730. u16 pipenum = ep->pipenum;
  731. struct m66592 *m66592 = ep->m66592;
  732. int finish = 0;
  733. pipe_change(m66592, pipenum);
  734. tmp = m66592_read(m66592, ep->fifoctr);
  735. if (unlikely((tmp & M66592_FRDY) == 0)) {
  736. req->req.status = -EPIPE;
  737. pipe_stop(m66592, pipenum);
  738. pipe_irq_disable(m66592, pipenum);
  739. pr_err("read fifo not ready");
  740. return;
  741. }
  742. /* prepare parameters */
  743. rcv_len = tmp & M66592_DTLN;
  744. bufsize = get_buffer_size(m66592, pipenum);
  745. buf = req->req.buf + req->req.actual;
  746. req_len = req->req.length - req->req.actual;
  747. if (rcv_len < bufsize)
  748. size = min(rcv_len, req_len);
  749. else
  750. size = min(bufsize, req_len);
  751. /* update parameters */
  752. req->req.actual += size;
  753. /* check transfer finish */
  754. if ((!req->req.zero && (req->req.actual == req->req.length))
  755. || (size % ep->ep.maxpacket)
  756. || (size == 0)) {
  757. pipe_stop(m66592, pipenum);
  758. pipe_irq_disable(m66592, pipenum);
  759. finish = 1;
  760. }
  761. /* read fifo */
  762. if (req->req.buf) {
  763. if (size == 0)
  764. m66592_write(m66592, M66592_BCLR, ep->fifoctr);
  765. else
  766. m66592_read_fifo(m66592, ep->fifoaddr, buf, size);
  767. }
  768. if ((ep->pipenum != 0) && finish)
  769. transfer_complete(ep, req, 0);
  770. }
  771. static void irq_pipe_ready(struct m66592 *m66592, u16 status, u16 enb)
  772. {
  773. u16 check;
  774. u16 pipenum;
  775. struct m66592_ep *ep;
  776. struct m66592_request *req;
  777. if ((status & M66592_BRDY0) && (enb & M66592_BRDY0)) {
  778. m66592_write(m66592, ~M66592_BRDY0, M66592_BRDYSTS);
  779. m66592_mdfy(m66592, M66592_PIPE0, M66592_CURPIPE,
  780. M66592_CFIFOSEL);
  781. ep = &m66592->ep[0];
  782. req = list_entry(ep->queue.next, struct m66592_request, queue);
  783. irq_packet_read(ep, req);
  784. } else {
  785. for (pipenum = 1; pipenum < M66592_MAX_NUM_PIPE; pipenum++) {
  786. check = 1 << pipenum;
  787. if ((status & check) && (enb & check)) {
  788. m66592_write(m66592, ~check, M66592_BRDYSTS);
  789. ep = m66592->pipenum2ep[pipenum];
  790. req = list_entry(ep->queue.next,
  791. struct m66592_request, queue);
  792. if (ep->desc->bEndpointAddress & USB_DIR_IN)
  793. irq_packet_write(ep, req);
  794. else
  795. irq_packet_read(ep, req);
  796. }
  797. }
  798. }
  799. }
  800. static void irq_pipe_empty(struct m66592 *m66592, u16 status, u16 enb)
  801. {
  802. u16 tmp;
  803. u16 check;
  804. u16 pipenum;
  805. struct m66592_ep *ep;
  806. struct m66592_request *req;
  807. if ((status & M66592_BEMP0) && (enb & M66592_BEMP0)) {
  808. m66592_write(m66592, ~M66592_BEMP0, M66592_BEMPSTS);
  809. ep = &m66592->ep[0];
  810. req = list_entry(ep->queue.next, struct m66592_request, queue);
  811. irq_ep0_write(ep, req);
  812. } else {
  813. for (pipenum = 1; pipenum < M66592_MAX_NUM_PIPE; pipenum++) {
  814. check = 1 << pipenum;
  815. if ((status & check) && (enb & check)) {
  816. m66592_write(m66592, ~check, M66592_BEMPSTS);
  817. tmp = control_reg_get(m66592, pipenum);
  818. if ((tmp & M66592_INBUFM) == 0) {
  819. disable_irq_empty(m66592, pipenum);
  820. pipe_irq_disable(m66592, pipenum);
  821. pipe_stop(m66592, pipenum);
  822. ep = m66592->pipenum2ep[pipenum];
  823. req = list_entry(ep->queue.next,
  824. struct m66592_request,
  825. queue);
  826. if (!list_empty(&ep->queue))
  827. transfer_complete(ep, req, 0);
  828. }
  829. }
  830. }
  831. }
  832. }
  833. static void get_status(struct m66592 *m66592, struct usb_ctrlrequest *ctrl)
  834. __releases(m66592->lock)
  835. __acquires(m66592->lock)
  836. {
  837. struct m66592_ep *ep;
  838. u16 pid;
  839. u16 status = 0;
  840. u16 w_index = le16_to_cpu(ctrl->wIndex);
  841. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  842. case USB_RECIP_DEVICE:
  843. status = 1 << USB_DEVICE_SELF_POWERED;
  844. break;
  845. case USB_RECIP_INTERFACE:
  846. status = 0;
  847. break;
  848. case USB_RECIP_ENDPOINT:
  849. ep = m66592->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
  850. pid = control_reg_get_pid(m66592, ep->pipenum);
  851. if (pid == M66592_PID_STALL)
  852. status = 1 << USB_ENDPOINT_HALT;
  853. else
  854. status = 0;
  855. break;
  856. default:
  857. pipe_stall(m66592, 0);
  858. return; /* exit */
  859. }
  860. m66592->ep0_data = cpu_to_le16(status);
  861. m66592->ep0_req->buf = &m66592->ep0_data;
  862. m66592->ep0_req->length = 2;
  863. /* AV: what happens if we get called again before that gets through? */
  864. spin_unlock(&m66592->lock);
  865. m66592_queue(m66592->gadget.ep0, m66592->ep0_req, GFP_KERNEL);
  866. spin_lock(&m66592->lock);
  867. }
  868. static void clear_feature(struct m66592 *m66592, struct usb_ctrlrequest *ctrl)
  869. {
  870. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  871. case USB_RECIP_DEVICE:
  872. control_end(m66592, 1);
  873. break;
  874. case USB_RECIP_INTERFACE:
  875. control_end(m66592, 1);
  876. break;
  877. case USB_RECIP_ENDPOINT: {
  878. struct m66592_ep *ep;
  879. struct m66592_request *req;
  880. u16 w_index = le16_to_cpu(ctrl->wIndex);
  881. ep = m66592->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
  882. pipe_stop(m66592, ep->pipenum);
  883. control_reg_sqclr(m66592, ep->pipenum);
  884. control_end(m66592, 1);
  885. req = list_entry(ep->queue.next,
  886. struct m66592_request, queue);
  887. if (ep->busy) {
  888. ep->busy = 0;
  889. if (list_empty(&ep->queue))
  890. break;
  891. start_packet(ep, req);
  892. } else if (!list_empty(&ep->queue))
  893. pipe_start(m66592, ep->pipenum);
  894. }
  895. break;
  896. default:
  897. pipe_stall(m66592, 0);
  898. break;
  899. }
  900. }
  901. static void set_feature(struct m66592 *m66592, struct usb_ctrlrequest *ctrl)
  902. {
  903. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  904. case USB_RECIP_DEVICE:
  905. control_end(m66592, 1);
  906. break;
  907. case USB_RECIP_INTERFACE:
  908. control_end(m66592, 1);
  909. break;
  910. case USB_RECIP_ENDPOINT: {
  911. struct m66592_ep *ep;
  912. u16 w_index = le16_to_cpu(ctrl->wIndex);
  913. ep = m66592->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
  914. pipe_stall(m66592, ep->pipenum);
  915. control_end(m66592, 1);
  916. }
  917. break;
  918. default:
  919. pipe_stall(m66592, 0);
  920. break;
  921. }
  922. }
  923. /* if return value is true, call class driver's setup() */
  924. static int setup_packet(struct m66592 *m66592, struct usb_ctrlrequest *ctrl)
  925. {
  926. u16 *p = (u16 *)ctrl;
  927. unsigned long offset = M66592_USBREQ;
  928. int i, ret = 0;
  929. /* read fifo */
  930. m66592_write(m66592, ~M66592_VALID, M66592_INTSTS0);
  931. for (i = 0; i < 4; i++)
  932. p[i] = m66592_read(m66592, offset + i*2);
  933. /* check request */
  934. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  935. switch (ctrl->bRequest) {
  936. case USB_REQ_GET_STATUS:
  937. get_status(m66592, ctrl);
  938. break;
  939. case USB_REQ_CLEAR_FEATURE:
  940. clear_feature(m66592, ctrl);
  941. break;
  942. case USB_REQ_SET_FEATURE:
  943. set_feature(m66592, ctrl);
  944. break;
  945. default:
  946. ret = 1;
  947. break;
  948. }
  949. } else
  950. ret = 1;
  951. return ret;
  952. }
  953. static void m66592_update_usb_speed(struct m66592 *m66592)
  954. {
  955. u16 speed = get_usb_speed(m66592);
  956. switch (speed) {
  957. case M66592_HSMODE:
  958. m66592->gadget.speed = USB_SPEED_HIGH;
  959. break;
  960. case M66592_FSMODE:
  961. m66592->gadget.speed = USB_SPEED_FULL;
  962. break;
  963. default:
  964. m66592->gadget.speed = USB_SPEED_UNKNOWN;
  965. pr_err("USB speed unknown\n");
  966. }
  967. }
  968. static void irq_device_state(struct m66592 *m66592)
  969. {
  970. u16 dvsq;
  971. dvsq = m66592_read(m66592, M66592_INTSTS0) & M66592_DVSQ;
  972. m66592_write(m66592, ~M66592_DVST, M66592_INTSTS0);
  973. if (dvsq == M66592_DS_DFLT) { /* bus reset */
  974. m66592->driver->disconnect(&m66592->gadget);
  975. m66592_update_usb_speed(m66592);
  976. }
  977. if (m66592->old_dvsq == M66592_DS_CNFG && dvsq != M66592_DS_CNFG)
  978. m66592_update_usb_speed(m66592);
  979. if ((dvsq == M66592_DS_CNFG || dvsq == M66592_DS_ADDS)
  980. && m66592->gadget.speed == USB_SPEED_UNKNOWN)
  981. m66592_update_usb_speed(m66592);
  982. m66592->old_dvsq = dvsq;
  983. }
  984. static void irq_control_stage(struct m66592 *m66592)
  985. __releases(m66592->lock)
  986. __acquires(m66592->lock)
  987. {
  988. struct usb_ctrlrequest ctrl;
  989. u16 ctsq;
  990. ctsq = m66592_read(m66592, M66592_INTSTS0) & M66592_CTSQ;
  991. m66592_write(m66592, ~M66592_CTRT, M66592_INTSTS0);
  992. switch (ctsq) {
  993. case M66592_CS_IDST: {
  994. struct m66592_ep *ep;
  995. struct m66592_request *req;
  996. ep = &m66592->ep[0];
  997. req = list_entry(ep->queue.next, struct m66592_request, queue);
  998. transfer_complete(ep, req, 0);
  999. }
  1000. break;
  1001. case M66592_CS_RDDS:
  1002. case M66592_CS_WRDS:
  1003. case M66592_CS_WRND:
  1004. if (setup_packet(m66592, &ctrl)) {
  1005. spin_unlock(&m66592->lock);
  1006. if (m66592->driver->setup(&m66592->gadget, &ctrl) < 0)
  1007. pipe_stall(m66592, 0);
  1008. spin_lock(&m66592->lock);
  1009. }
  1010. break;
  1011. case M66592_CS_RDSS:
  1012. case M66592_CS_WRSS:
  1013. control_end(m66592, 0);
  1014. break;
  1015. default:
  1016. pr_err("ctrl_stage: unexpect ctsq(%x)\n", ctsq);
  1017. break;
  1018. }
  1019. }
  1020. static irqreturn_t m66592_irq(int irq, void *_m66592)
  1021. {
  1022. struct m66592 *m66592 = _m66592;
  1023. u16 intsts0;
  1024. u16 intenb0;
  1025. u16 brdysts, nrdysts, bempsts;
  1026. u16 brdyenb, nrdyenb, bempenb;
  1027. u16 savepipe;
  1028. u16 mask0;
  1029. spin_lock(&m66592->lock);
  1030. intsts0 = m66592_read(m66592, M66592_INTSTS0);
  1031. intenb0 = m66592_read(m66592, M66592_INTENB0);
  1032. if (m66592->pdata->on_chip && !intsts0 && !intenb0) {
  1033. /*
  1034. * When USB clock stops, it cannot read register. Even if a
  1035. * clock stops, the interrupt occurs. So this driver turn on
  1036. * a clock by this timing and do re-reading of register.
  1037. */
  1038. m66592_start_xclock(m66592);
  1039. intsts0 = m66592_read(m66592, M66592_INTSTS0);
  1040. intenb0 = m66592_read(m66592, M66592_INTENB0);
  1041. }
  1042. savepipe = m66592_read(m66592, M66592_CFIFOSEL);
  1043. mask0 = intsts0 & intenb0;
  1044. if (mask0) {
  1045. brdysts = m66592_read(m66592, M66592_BRDYSTS);
  1046. nrdysts = m66592_read(m66592, M66592_NRDYSTS);
  1047. bempsts = m66592_read(m66592, M66592_BEMPSTS);
  1048. brdyenb = m66592_read(m66592, M66592_BRDYENB);
  1049. nrdyenb = m66592_read(m66592, M66592_NRDYENB);
  1050. bempenb = m66592_read(m66592, M66592_BEMPENB);
  1051. if (mask0 & M66592_VBINT) {
  1052. m66592_write(m66592, 0xffff & ~M66592_VBINT,
  1053. M66592_INTSTS0);
  1054. m66592_start_xclock(m66592);
  1055. /* start vbus sampling */
  1056. m66592->old_vbus = m66592_read(m66592, M66592_INTSTS0)
  1057. & M66592_VBSTS;
  1058. m66592->scount = M66592_MAX_SAMPLING;
  1059. mod_timer(&m66592->timer,
  1060. jiffies + msecs_to_jiffies(50));
  1061. }
  1062. if (intsts0 & M66592_DVSQ)
  1063. irq_device_state(m66592);
  1064. if ((intsts0 & M66592_BRDY) && (intenb0 & M66592_BRDYE)
  1065. && (brdysts & brdyenb)) {
  1066. irq_pipe_ready(m66592, brdysts, brdyenb);
  1067. }
  1068. if ((intsts0 & M66592_BEMP) && (intenb0 & M66592_BEMPE)
  1069. && (bempsts & bempenb)) {
  1070. irq_pipe_empty(m66592, bempsts, bempenb);
  1071. }
  1072. if (intsts0 & M66592_CTRT)
  1073. irq_control_stage(m66592);
  1074. }
  1075. m66592_write(m66592, savepipe, M66592_CFIFOSEL);
  1076. spin_unlock(&m66592->lock);
  1077. return IRQ_HANDLED;
  1078. }
  1079. static void m66592_timer(unsigned long _m66592)
  1080. {
  1081. struct m66592 *m66592 = (struct m66592 *)_m66592;
  1082. unsigned long flags;
  1083. u16 tmp;
  1084. spin_lock_irqsave(&m66592->lock, flags);
  1085. tmp = m66592_read(m66592, M66592_SYSCFG);
  1086. if (!(tmp & M66592_RCKE)) {
  1087. m66592_bset(m66592, M66592_RCKE | M66592_PLLC, M66592_SYSCFG);
  1088. udelay(10);
  1089. m66592_bset(m66592, M66592_SCKE, M66592_SYSCFG);
  1090. }
  1091. if (m66592->scount > 0) {
  1092. tmp = m66592_read(m66592, M66592_INTSTS0) & M66592_VBSTS;
  1093. if (tmp == m66592->old_vbus) {
  1094. m66592->scount--;
  1095. if (m66592->scount == 0) {
  1096. if (tmp == M66592_VBSTS)
  1097. m66592_usb_connect(m66592);
  1098. else
  1099. m66592_usb_disconnect(m66592);
  1100. } else {
  1101. mod_timer(&m66592->timer,
  1102. jiffies + msecs_to_jiffies(50));
  1103. }
  1104. } else {
  1105. m66592->scount = M66592_MAX_SAMPLING;
  1106. m66592->old_vbus = tmp;
  1107. mod_timer(&m66592->timer,
  1108. jiffies + msecs_to_jiffies(50));
  1109. }
  1110. }
  1111. spin_unlock_irqrestore(&m66592->lock, flags);
  1112. }
  1113. /*-------------------------------------------------------------------------*/
  1114. static int m66592_enable(struct usb_ep *_ep,
  1115. const struct usb_endpoint_descriptor *desc)
  1116. {
  1117. struct m66592_ep *ep;
  1118. ep = container_of(_ep, struct m66592_ep, ep);
  1119. return alloc_pipe_config(ep, desc);
  1120. }
  1121. static int m66592_disable(struct usb_ep *_ep)
  1122. {
  1123. struct m66592_ep *ep;
  1124. struct m66592_request *req;
  1125. unsigned long flags;
  1126. ep = container_of(_ep, struct m66592_ep, ep);
  1127. BUG_ON(!ep);
  1128. while (!list_empty(&ep->queue)) {
  1129. req = list_entry(ep->queue.next, struct m66592_request, queue);
  1130. spin_lock_irqsave(&ep->m66592->lock, flags);
  1131. transfer_complete(ep, req, -ECONNRESET);
  1132. spin_unlock_irqrestore(&ep->m66592->lock, flags);
  1133. }
  1134. pipe_irq_disable(ep->m66592, ep->pipenum);
  1135. return free_pipe_config(ep);
  1136. }
  1137. static struct usb_request *m66592_alloc_request(struct usb_ep *_ep,
  1138. gfp_t gfp_flags)
  1139. {
  1140. struct m66592_request *req;
  1141. req = kzalloc(sizeof(struct m66592_request), gfp_flags);
  1142. if (!req)
  1143. return NULL;
  1144. INIT_LIST_HEAD(&req->queue);
  1145. return &req->req;
  1146. }
  1147. static void m66592_free_request(struct usb_ep *_ep, struct usb_request *_req)
  1148. {
  1149. struct m66592_request *req;
  1150. req = container_of(_req, struct m66592_request, req);
  1151. kfree(req);
  1152. }
  1153. static int m66592_queue(struct usb_ep *_ep, struct usb_request *_req,
  1154. gfp_t gfp_flags)
  1155. {
  1156. struct m66592_ep *ep;
  1157. struct m66592_request *req;
  1158. unsigned long flags;
  1159. int request = 0;
  1160. ep = container_of(_ep, struct m66592_ep, ep);
  1161. req = container_of(_req, struct m66592_request, req);
  1162. if (ep->m66592->gadget.speed == USB_SPEED_UNKNOWN)
  1163. return -ESHUTDOWN;
  1164. spin_lock_irqsave(&ep->m66592->lock, flags);
  1165. if (list_empty(&ep->queue))
  1166. request = 1;
  1167. list_add_tail(&req->queue, &ep->queue);
  1168. req->req.actual = 0;
  1169. req->req.status = -EINPROGRESS;
  1170. if (ep->desc == NULL) /* control */
  1171. start_ep0(ep, req);
  1172. else {
  1173. if (request && !ep->busy)
  1174. start_packet(ep, req);
  1175. }
  1176. spin_unlock_irqrestore(&ep->m66592->lock, flags);
  1177. return 0;
  1178. }
  1179. static int m66592_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1180. {
  1181. struct m66592_ep *ep;
  1182. struct m66592_request *req;
  1183. unsigned long flags;
  1184. ep = container_of(_ep, struct m66592_ep, ep);
  1185. req = container_of(_req, struct m66592_request, req);
  1186. spin_lock_irqsave(&ep->m66592->lock, flags);
  1187. if (!list_empty(&ep->queue))
  1188. transfer_complete(ep, req, -ECONNRESET);
  1189. spin_unlock_irqrestore(&ep->m66592->lock, flags);
  1190. return 0;
  1191. }
  1192. static int m66592_set_halt(struct usb_ep *_ep, int value)
  1193. {
  1194. struct m66592_ep *ep;
  1195. struct m66592_request *req;
  1196. unsigned long flags;
  1197. int ret = 0;
  1198. ep = container_of(_ep, struct m66592_ep, ep);
  1199. req = list_entry(ep->queue.next, struct m66592_request, queue);
  1200. spin_lock_irqsave(&ep->m66592->lock, flags);
  1201. if (!list_empty(&ep->queue)) {
  1202. ret = -EAGAIN;
  1203. goto out;
  1204. }
  1205. if (value) {
  1206. ep->busy = 1;
  1207. pipe_stall(ep->m66592, ep->pipenum);
  1208. } else {
  1209. ep->busy = 0;
  1210. pipe_stop(ep->m66592, ep->pipenum);
  1211. }
  1212. out:
  1213. spin_unlock_irqrestore(&ep->m66592->lock, flags);
  1214. return ret;
  1215. }
  1216. static void m66592_fifo_flush(struct usb_ep *_ep)
  1217. {
  1218. struct m66592_ep *ep;
  1219. unsigned long flags;
  1220. ep = container_of(_ep, struct m66592_ep, ep);
  1221. spin_lock_irqsave(&ep->m66592->lock, flags);
  1222. if (list_empty(&ep->queue) && !ep->busy) {
  1223. pipe_stop(ep->m66592, ep->pipenum);
  1224. m66592_bclr(ep->m66592, M66592_BCLR, ep->fifoctr);
  1225. }
  1226. spin_unlock_irqrestore(&ep->m66592->lock, flags);
  1227. }
  1228. static struct usb_ep_ops m66592_ep_ops = {
  1229. .enable = m66592_enable,
  1230. .disable = m66592_disable,
  1231. .alloc_request = m66592_alloc_request,
  1232. .free_request = m66592_free_request,
  1233. .queue = m66592_queue,
  1234. .dequeue = m66592_dequeue,
  1235. .set_halt = m66592_set_halt,
  1236. .fifo_flush = m66592_fifo_flush,
  1237. };
  1238. /*-------------------------------------------------------------------------*/
  1239. static struct m66592 *the_controller;
  1240. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  1241. int (*bind)(struct usb_gadget *))
  1242. {
  1243. struct m66592 *m66592 = the_controller;
  1244. int retval;
  1245. if (!driver
  1246. || driver->speed != USB_SPEED_HIGH
  1247. || !bind
  1248. || !driver->setup)
  1249. return -EINVAL;
  1250. if (!m66592)
  1251. return -ENODEV;
  1252. if (m66592->driver)
  1253. return -EBUSY;
  1254. /* hook up the driver */
  1255. driver->driver.bus = NULL;
  1256. m66592->driver = driver;
  1257. m66592->gadget.dev.driver = &driver->driver;
  1258. retval = device_add(&m66592->gadget.dev);
  1259. if (retval) {
  1260. pr_err("device_add error (%d)\n", retval);
  1261. goto error;
  1262. }
  1263. retval = bind(&m66592->gadget);
  1264. if (retval) {
  1265. pr_err("bind to driver error (%d)\n", retval);
  1266. device_del(&m66592->gadget.dev);
  1267. goto error;
  1268. }
  1269. m66592_bset(m66592, M66592_VBSE | M66592_URST, M66592_INTENB0);
  1270. if (m66592_read(m66592, M66592_INTSTS0) & M66592_VBSTS) {
  1271. m66592_start_xclock(m66592);
  1272. /* start vbus sampling */
  1273. m66592->old_vbus = m66592_read(m66592,
  1274. M66592_INTSTS0) & M66592_VBSTS;
  1275. m66592->scount = M66592_MAX_SAMPLING;
  1276. mod_timer(&m66592->timer, jiffies + msecs_to_jiffies(50));
  1277. }
  1278. return 0;
  1279. error:
  1280. m66592->driver = NULL;
  1281. m66592->gadget.dev.driver = NULL;
  1282. return retval;
  1283. }
  1284. EXPORT_SYMBOL(usb_gadget_probe_driver);
  1285. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1286. {
  1287. struct m66592 *m66592 = the_controller;
  1288. unsigned long flags;
  1289. if (driver != m66592->driver || !driver->unbind)
  1290. return -EINVAL;
  1291. spin_lock_irqsave(&m66592->lock, flags);
  1292. if (m66592->gadget.speed != USB_SPEED_UNKNOWN)
  1293. m66592_usb_disconnect(m66592);
  1294. spin_unlock_irqrestore(&m66592->lock, flags);
  1295. m66592_bclr(m66592, M66592_VBSE | M66592_URST, M66592_INTENB0);
  1296. driver->unbind(&m66592->gadget);
  1297. m66592->gadget.dev.driver = NULL;
  1298. init_controller(m66592);
  1299. disable_controller(m66592);
  1300. device_del(&m66592->gadget.dev);
  1301. m66592->driver = NULL;
  1302. return 0;
  1303. }
  1304. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1305. /*-------------------------------------------------------------------------*/
  1306. static int m66592_get_frame(struct usb_gadget *_gadget)
  1307. {
  1308. struct m66592 *m66592 = gadget_to_m66592(_gadget);
  1309. return m66592_read(m66592, M66592_FRMNUM) & 0x03FF;
  1310. }
  1311. static struct usb_gadget_ops m66592_gadget_ops = {
  1312. .get_frame = m66592_get_frame,
  1313. };
  1314. static int __exit m66592_remove(struct platform_device *pdev)
  1315. {
  1316. struct m66592 *m66592 = dev_get_drvdata(&pdev->dev);
  1317. del_timer_sync(&m66592->timer);
  1318. iounmap(m66592->reg);
  1319. free_irq(platform_get_irq(pdev, 0), m66592);
  1320. m66592_free_request(&m66592->ep[0].ep, m66592->ep0_req);
  1321. #ifdef CONFIG_HAVE_CLK
  1322. if (m66592->pdata->on_chip) {
  1323. clk_disable(m66592->clk);
  1324. clk_put(m66592->clk);
  1325. }
  1326. #endif
  1327. kfree(m66592);
  1328. return 0;
  1329. }
  1330. static void nop_completion(struct usb_ep *ep, struct usb_request *r)
  1331. {
  1332. }
  1333. static int __init m66592_probe(struct platform_device *pdev)
  1334. {
  1335. struct resource *res, *ires;
  1336. void __iomem *reg = NULL;
  1337. struct m66592 *m66592 = NULL;
  1338. #ifdef CONFIG_HAVE_CLK
  1339. char clk_name[8];
  1340. #endif
  1341. int ret = 0;
  1342. int i;
  1343. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1344. if (!res) {
  1345. ret = -ENODEV;
  1346. pr_err("platform_get_resource error.\n");
  1347. goto clean_up;
  1348. }
  1349. ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1350. if (!ires) {
  1351. ret = -ENODEV;
  1352. dev_err(&pdev->dev,
  1353. "platform_get_resource IORESOURCE_IRQ error.\n");
  1354. goto clean_up;
  1355. }
  1356. reg = ioremap(res->start, resource_size(res));
  1357. if (reg == NULL) {
  1358. ret = -ENOMEM;
  1359. pr_err("ioremap error.\n");
  1360. goto clean_up;
  1361. }
  1362. if (pdev->dev.platform_data == NULL) {
  1363. dev_err(&pdev->dev, "no platform data\n");
  1364. ret = -ENODEV;
  1365. goto clean_up;
  1366. }
  1367. /* initialize ucd */
  1368. m66592 = kzalloc(sizeof(struct m66592), GFP_KERNEL);
  1369. if (m66592 == NULL) {
  1370. ret = -ENOMEM;
  1371. pr_err("kzalloc error\n");
  1372. goto clean_up;
  1373. }
  1374. m66592->pdata = pdev->dev.platform_data;
  1375. m66592->irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
  1376. spin_lock_init(&m66592->lock);
  1377. dev_set_drvdata(&pdev->dev, m66592);
  1378. m66592->gadget.ops = &m66592_gadget_ops;
  1379. device_initialize(&m66592->gadget.dev);
  1380. dev_set_name(&m66592->gadget.dev, "gadget");
  1381. m66592->gadget.is_dualspeed = 1;
  1382. m66592->gadget.dev.parent = &pdev->dev;
  1383. m66592->gadget.dev.dma_mask = pdev->dev.dma_mask;
  1384. m66592->gadget.dev.release = pdev->dev.release;
  1385. m66592->gadget.name = udc_name;
  1386. init_timer(&m66592->timer);
  1387. m66592->timer.function = m66592_timer;
  1388. m66592->timer.data = (unsigned long)m66592;
  1389. m66592->reg = reg;
  1390. ret = request_irq(ires->start, m66592_irq, IRQF_DISABLED | IRQF_SHARED,
  1391. udc_name, m66592);
  1392. if (ret < 0) {
  1393. pr_err("request_irq error (%d)\n", ret);
  1394. goto clean_up;
  1395. }
  1396. #ifdef CONFIG_HAVE_CLK
  1397. if (m66592->pdata->on_chip) {
  1398. snprintf(clk_name, sizeof(clk_name), "usbf%d", pdev->id);
  1399. m66592->clk = clk_get(&pdev->dev, clk_name);
  1400. if (IS_ERR(m66592->clk)) {
  1401. dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
  1402. clk_name);
  1403. ret = PTR_ERR(m66592->clk);
  1404. goto clean_up2;
  1405. }
  1406. clk_enable(m66592->clk);
  1407. }
  1408. #endif
  1409. INIT_LIST_HEAD(&m66592->gadget.ep_list);
  1410. m66592->gadget.ep0 = &m66592->ep[0].ep;
  1411. INIT_LIST_HEAD(&m66592->gadget.ep0->ep_list);
  1412. for (i = 0; i < M66592_MAX_NUM_PIPE; i++) {
  1413. struct m66592_ep *ep = &m66592->ep[i];
  1414. if (i != 0) {
  1415. INIT_LIST_HEAD(&m66592->ep[i].ep.ep_list);
  1416. list_add_tail(&m66592->ep[i].ep.ep_list,
  1417. &m66592->gadget.ep_list);
  1418. }
  1419. ep->m66592 = m66592;
  1420. INIT_LIST_HEAD(&ep->queue);
  1421. ep->ep.name = m66592_ep_name[i];
  1422. ep->ep.ops = &m66592_ep_ops;
  1423. ep->ep.maxpacket = 512;
  1424. }
  1425. m66592->ep[0].ep.maxpacket = 64;
  1426. m66592->ep[0].pipenum = 0;
  1427. m66592->ep[0].fifoaddr = M66592_CFIFO;
  1428. m66592->ep[0].fifosel = M66592_CFIFOSEL;
  1429. m66592->ep[0].fifoctr = M66592_CFIFOCTR;
  1430. m66592->ep[0].fifotrn = 0;
  1431. m66592->ep[0].pipectr = get_pipectr_addr(0);
  1432. m66592->pipenum2ep[0] = &m66592->ep[0];
  1433. m66592->epaddr2ep[0] = &m66592->ep[0];
  1434. the_controller = m66592;
  1435. m66592->ep0_req = m66592_alloc_request(&m66592->ep[0].ep, GFP_KERNEL);
  1436. if (m66592->ep0_req == NULL)
  1437. goto clean_up3;
  1438. m66592->ep0_req->complete = nop_completion;
  1439. init_controller(m66592);
  1440. dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
  1441. return 0;
  1442. clean_up3:
  1443. #ifdef CONFIG_HAVE_CLK
  1444. if (m66592->pdata->on_chip) {
  1445. clk_disable(m66592->clk);
  1446. clk_put(m66592->clk);
  1447. }
  1448. clean_up2:
  1449. #endif
  1450. free_irq(ires->start, m66592);
  1451. clean_up:
  1452. if (m66592) {
  1453. if (m66592->ep0_req)
  1454. m66592_free_request(&m66592->ep[0].ep, m66592->ep0_req);
  1455. kfree(m66592);
  1456. }
  1457. if (reg)
  1458. iounmap(reg);
  1459. return ret;
  1460. }
  1461. /*-------------------------------------------------------------------------*/
  1462. static struct platform_driver m66592_driver = {
  1463. .remove = __exit_p(m66592_remove),
  1464. .driver = {
  1465. .name = (char *) udc_name,
  1466. .owner = THIS_MODULE,
  1467. },
  1468. };
  1469. static int __init m66592_udc_init(void)
  1470. {
  1471. return platform_driver_probe(&m66592_driver, m66592_probe);
  1472. }
  1473. module_init(m66592_udc_init);
  1474. static void __exit m66592_udc_cleanup(void)
  1475. {
  1476. platform_driver_unregister(&m66592_driver);
  1477. }
  1478. module_exit(m66592_udc_cleanup);