m66592-udc.c 41 KB

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