m66592-udc.c 42 KB

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