fusb300_udc.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. /*
  2. * Fusb300 UDC (USB gadget)
  3. *
  4. * Copyright (C) 2010 Faraday Technology Corp.
  5. *
  6. * Author : Yuan-hsin Chen <yhchen@faraday-tech.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. #include <linux/dma-mapping.h>
  13. #include <linux/err.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/usb/ch9.h>
  19. #include <linux/usb/gadget.h>
  20. #include "fusb300_udc.h"
  21. MODULE_DESCRIPTION("FUSB300 USB gadget driver");
  22. MODULE_LICENSE("GPL");
  23. MODULE_AUTHOR("Yuan Hsin Chen <yhchen@faraday-tech.com>");
  24. MODULE_ALIAS("platform:fusb300_udc");
  25. #define DRIVER_VERSION "20 October 2010"
  26. static const char udc_name[] = "fusb300_udc";
  27. static const char * const fusb300_ep_name[] = {
  28. "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7", "ep8", "ep9",
  29. "ep10", "ep11", "ep12", "ep13", "ep14", "ep15"
  30. };
  31. static void done(struct fusb300_ep *ep, struct fusb300_request *req,
  32. int status);
  33. static void fusb300_enable_bit(struct fusb300 *fusb300, u32 offset,
  34. u32 value)
  35. {
  36. u32 reg = ioread32(fusb300->reg + offset);
  37. reg |= value;
  38. iowrite32(reg, fusb300->reg + offset);
  39. }
  40. static void fusb300_disable_bit(struct fusb300 *fusb300, u32 offset,
  41. u32 value)
  42. {
  43. u32 reg = ioread32(fusb300->reg + offset);
  44. reg &= ~value;
  45. iowrite32(reg, fusb300->reg + offset);
  46. }
  47. static void fusb300_ep_setting(struct fusb300_ep *ep,
  48. struct fusb300_ep_info info)
  49. {
  50. ep->epnum = info.epnum;
  51. ep->type = info.type;
  52. }
  53. static int fusb300_ep_release(struct fusb300_ep *ep)
  54. {
  55. if (!ep->epnum)
  56. return 0;
  57. ep->epnum = 0;
  58. ep->stall = 0;
  59. ep->wedged = 0;
  60. return 0;
  61. }
  62. static void fusb300_set_fifo_entry(struct fusb300 *fusb300,
  63. u32 ep)
  64. {
  65. u32 val = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
  66. val &= ~FUSB300_EPSET1_FIFOENTRY_MSK;
  67. val |= FUSB300_EPSET1_FIFOENTRY(FUSB300_FIFO_ENTRY_NUM);
  68. iowrite32(val, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
  69. }
  70. static void fusb300_set_start_entry(struct fusb300 *fusb300,
  71. u8 ep)
  72. {
  73. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
  74. u32 start_entry = fusb300->fifo_entry_num * FUSB300_FIFO_ENTRY_NUM;
  75. reg &= ~FUSB300_EPSET1_START_ENTRY_MSK ;
  76. reg |= FUSB300_EPSET1_START_ENTRY(start_entry);
  77. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
  78. if (fusb300->fifo_entry_num == FUSB300_MAX_FIFO_ENTRY) {
  79. fusb300->fifo_entry_num = 0;
  80. fusb300->addrofs = 0;
  81. pr_err("fifo entry is over the maximum number!\n");
  82. } else
  83. fusb300->fifo_entry_num++;
  84. }
  85. /* set fusb300_set_start_entry first before fusb300_set_epaddrofs */
  86. static void fusb300_set_epaddrofs(struct fusb300 *fusb300,
  87. struct fusb300_ep_info info)
  88. {
  89. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
  90. reg &= ~FUSB300_EPSET2_ADDROFS_MSK;
  91. reg |= FUSB300_EPSET2_ADDROFS(fusb300->addrofs);
  92. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
  93. fusb300->addrofs += (info.maxpacket + 7) / 8 * FUSB300_FIFO_ENTRY_NUM;
  94. }
  95. static void ep_fifo_setting(struct fusb300 *fusb300,
  96. struct fusb300_ep_info info)
  97. {
  98. fusb300_set_fifo_entry(fusb300, info.epnum);
  99. fusb300_set_start_entry(fusb300, info.epnum);
  100. fusb300_set_epaddrofs(fusb300, info);
  101. }
  102. static void fusb300_set_eptype(struct fusb300 *fusb300,
  103. struct fusb300_ep_info info)
  104. {
  105. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
  106. reg &= ~FUSB300_EPSET1_TYPE_MSK;
  107. reg |= FUSB300_EPSET1_TYPE(info.type);
  108. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
  109. }
  110. static void fusb300_set_epdir(struct fusb300 *fusb300,
  111. struct fusb300_ep_info info)
  112. {
  113. u32 reg;
  114. if (!info.dir_in)
  115. return;
  116. reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
  117. reg &= ~FUSB300_EPSET1_DIR_MSK;
  118. reg |= FUSB300_EPSET1_DIRIN;
  119. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
  120. }
  121. static void fusb300_set_ep_active(struct fusb300 *fusb300,
  122. u8 ep)
  123. {
  124. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
  125. reg |= FUSB300_EPSET1_ACTEN;
  126. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
  127. }
  128. static void fusb300_set_epmps(struct fusb300 *fusb300,
  129. struct fusb300_ep_info info)
  130. {
  131. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
  132. reg &= ~FUSB300_EPSET2_MPS_MSK;
  133. reg |= FUSB300_EPSET2_MPS(info.maxpacket);
  134. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
  135. }
  136. static void fusb300_set_interval(struct fusb300 *fusb300,
  137. struct fusb300_ep_info info)
  138. {
  139. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
  140. reg &= ~FUSB300_EPSET1_INTERVAL(0x7);
  141. reg |= FUSB300_EPSET1_INTERVAL(info.interval);
  142. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
  143. }
  144. static void fusb300_set_bwnum(struct fusb300 *fusb300,
  145. struct fusb300_ep_info info)
  146. {
  147. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
  148. reg &= ~FUSB300_EPSET1_BWNUM(0x3);
  149. reg |= FUSB300_EPSET1_BWNUM(info.bw_num);
  150. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
  151. }
  152. static void set_ep_reg(struct fusb300 *fusb300,
  153. struct fusb300_ep_info info)
  154. {
  155. fusb300_set_eptype(fusb300, info);
  156. fusb300_set_epdir(fusb300, info);
  157. fusb300_set_epmps(fusb300, info);
  158. if (info.interval)
  159. fusb300_set_interval(fusb300, info);
  160. if (info.bw_num)
  161. fusb300_set_bwnum(fusb300, info);
  162. fusb300_set_ep_active(fusb300, info.epnum);
  163. }
  164. static int config_ep(struct fusb300_ep *ep,
  165. const struct usb_endpoint_descriptor *desc)
  166. {
  167. struct fusb300 *fusb300 = ep->fusb300;
  168. struct fusb300_ep_info info;
  169. ep->ep.desc = desc;
  170. info.interval = 0;
  171. info.addrofs = 0;
  172. info.bw_num = 0;
  173. info.type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  174. info.dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
  175. info.maxpacket = usb_endpoint_maxp(desc);
  176. info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  177. if ((info.type == USB_ENDPOINT_XFER_INT) ||
  178. (info.type == USB_ENDPOINT_XFER_ISOC)) {
  179. info.interval = desc->bInterval;
  180. if (info.type == USB_ENDPOINT_XFER_ISOC)
  181. info.bw_num = ((desc->wMaxPacketSize & 0x1800) >> 11);
  182. }
  183. ep_fifo_setting(fusb300, info);
  184. set_ep_reg(fusb300, info);
  185. fusb300_ep_setting(ep, info);
  186. fusb300->ep[info.epnum] = ep;
  187. return 0;
  188. }
  189. static int fusb300_enable(struct usb_ep *_ep,
  190. const struct usb_endpoint_descriptor *desc)
  191. {
  192. struct fusb300_ep *ep;
  193. ep = container_of(_ep, struct fusb300_ep, ep);
  194. if (ep->fusb300->reenum) {
  195. ep->fusb300->fifo_entry_num = 0;
  196. ep->fusb300->addrofs = 0;
  197. ep->fusb300->reenum = 0;
  198. }
  199. return config_ep(ep, desc);
  200. }
  201. static int fusb300_disable(struct usb_ep *_ep)
  202. {
  203. struct fusb300_ep *ep;
  204. struct fusb300_request *req;
  205. unsigned long flags;
  206. ep = container_of(_ep, struct fusb300_ep, ep);
  207. BUG_ON(!ep);
  208. while (!list_empty(&ep->queue)) {
  209. req = list_entry(ep->queue.next, struct fusb300_request, queue);
  210. spin_lock_irqsave(&ep->fusb300->lock, flags);
  211. done(ep, req, -ECONNRESET);
  212. spin_unlock_irqrestore(&ep->fusb300->lock, flags);
  213. }
  214. return fusb300_ep_release(ep);
  215. }
  216. static struct usb_request *fusb300_alloc_request(struct usb_ep *_ep,
  217. gfp_t gfp_flags)
  218. {
  219. struct fusb300_request *req;
  220. req = kzalloc(sizeof(struct fusb300_request), gfp_flags);
  221. if (!req)
  222. return NULL;
  223. INIT_LIST_HEAD(&req->queue);
  224. return &req->req;
  225. }
  226. static void fusb300_free_request(struct usb_ep *_ep, struct usb_request *_req)
  227. {
  228. struct fusb300_request *req;
  229. req = container_of(_req, struct fusb300_request, req);
  230. kfree(req);
  231. }
  232. static int enable_fifo_int(struct fusb300_ep *ep)
  233. {
  234. struct fusb300 *fusb300 = ep->fusb300;
  235. if (ep->epnum) {
  236. fusb300_enable_bit(fusb300, FUSB300_OFFSET_IGER0,
  237. FUSB300_IGER0_EEPn_FIFO_INT(ep->epnum));
  238. } else {
  239. pr_err("can't enable_fifo_int ep0\n");
  240. return -EINVAL;
  241. }
  242. return 0;
  243. }
  244. static int disable_fifo_int(struct fusb300_ep *ep)
  245. {
  246. struct fusb300 *fusb300 = ep->fusb300;
  247. if (ep->epnum) {
  248. fusb300_disable_bit(fusb300, FUSB300_OFFSET_IGER0,
  249. FUSB300_IGER0_EEPn_FIFO_INT(ep->epnum));
  250. } else {
  251. pr_err("can't disable_fifo_int ep0\n");
  252. return -EINVAL;
  253. }
  254. return 0;
  255. }
  256. static void fusb300_set_cxlen(struct fusb300 *fusb300, u32 length)
  257. {
  258. u32 reg;
  259. reg = ioread32(fusb300->reg + FUSB300_OFFSET_CSR);
  260. reg &= ~FUSB300_CSR_LEN_MSK;
  261. reg |= FUSB300_CSR_LEN(length);
  262. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_CSR);
  263. }
  264. /* write data to cx fifo */
  265. static void fusb300_wrcxf(struct fusb300_ep *ep,
  266. struct fusb300_request *req)
  267. {
  268. int i = 0;
  269. u8 *tmp;
  270. u32 data;
  271. struct fusb300 *fusb300 = ep->fusb300;
  272. u32 length = req->req.length - req->req.actual;
  273. tmp = req->req.buf + req->req.actual;
  274. if (length > SS_CTL_MAX_PACKET_SIZE) {
  275. fusb300_set_cxlen(fusb300, SS_CTL_MAX_PACKET_SIZE);
  276. for (i = (SS_CTL_MAX_PACKET_SIZE >> 2); i > 0; i--) {
  277. data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16 |
  278. *(tmp + 3) << 24;
  279. iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
  280. tmp += 4;
  281. }
  282. req->req.actual += SS_CTL_MAX_PACKET_SIZE;
  283. } else { /* length is less than max packet size */
  284. fusb300_set_cxlen(fusb300, length);
  285. for (i = length >> 2; i > 0; i--) {
  286. data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16 |
  287. *(tmp + 3) << 24;
  288. printk(KERN_DEBUG " 0x%x\n", data);
  289. iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
  290. tmp = tmp + 4;
  291. }
  292. switch (length % 4) {
  293. case 1:
  294. data = *tmp;
  295. printk(KERN_DEBUG " 0x%x\n", data);
  296. iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
  297. break;
  298. case 2:
  299. data = *tmp | *(tmp + 1) << 8;
  300. printk(KERN_DEBUG " 0x%x\n", data);
  301. iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
  302. break;
  303. case 3:
  304. data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16;
  305. printk(KERN_DEBUG " 0x%x\n", data);
  306. iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
  307. break;
  308. default:
  309. break;
  310. }
  311. req->req.actual += length;
  312. }
  313. }
  314. static void fusb300_set_epnstall(struct fusb300 *fusb300, u8 ep)
  315. {
  316. fusb300_enable_bit(fusb300, FUSB300_OFFSET_EPSET0(ep),
  317. FUSB300_EPSET0_STL);
  318. }
  319. static void fusb300_clear_epnstall(struct fusb300 *fusb300, u8 ep)
  320. {
  321. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
  322. if (reg & FUSB300_EPSET0_STL) {
  323. printk(KERN_DEBUG "EP%d stall... Clear!!\n", ep);
  324. reg &= ~FUSB300_EPSET0_STL;
  325. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
  326. }
  327. }
  328. static void ep0_queue(struct fusb300_ep *ep, struct fusb300_request *req)
  329. {
  330. if (ep->fusb300->ep0_dir) { /* if IN */
  331. if (req->req.length) {
  332. fusb300_wrcxf(ep, req);
  333. } else
  334. printk(KERN_DEBUG "%s : req->req.length = 0x%x\n",
  335. __func__, req->req.length);
  336. if ((req->req.length == req->req.actual) ||
  337. (req->req.actual < ep->ep.maxpacket))
  338. done(ep, req, 0);
  339. } else { /* OUT */
  340. if (!req->req.length)
  341. done(ep, req, 0);
  342. else
  343. fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_IGER1,
  344. FUSB300_IGER1_CX_OUT_INT);
  345. }
  346. }
  347. static int fusb300_queue(struct usb_ep *_ep, struct usb_request *_req,
  348. gfp_t gfp_flags)
  349. {
  350. struct fusb300_ep *ep;
  351. struct fusb300_request *req;
  352. unsigned long flags;
  353. int request = 0;
  354. ep = container_of(_ep, struct fusb300_ep, ep);
  355. req = container_of(_req, struct fusb300_request, req);
  356. if (ep->fusb300->gadget.speed == USB_SPEED_UNKNOWN)
  357. return -ESHUTDOWN;
  358. spin_lock_irqsave(&ep->fusb300->lock, flags);
  359. if (list_empty(&ep->queue))
  360. request = 1;
  361. list_add_tail(&req->queue, &ep->queue);
  362. req->req.actual = 0;
  363. req->req.status = -EINPROGRESS;
  364. if (ep->ep.desc == NULL) /* ep0 */
  365. ep0_queue(ep, req);
  366. else if (request && !ep->stall)
  367. enable_fifo_int(ep);
  368. spin_unlock_irqrestore(&ep->fusb300->lock, flags);
  369. return 0;
  370. }
  371. static int fusb300_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  372. {
  373. struct fusb300_ep *ep;
  374. struct fusb300_request *req;
  375. unsigned long flags;
  376. ep = container_of(_ep, struct fusb300_ep, ep);
  377. req = container_of(_req, struct fusb300_request, req);
  378. spin_lock_irqsave(&ep->fusb300->lock, flags);
  379. if (!list_empty(&ep->queue))
  380. done(ep, req, -ECONNRESET);
  381. spin_unlock_irqrestore(&ep->fusb300->lock, flags);
  382. return 0;
  383. }
  384. static int fusb300_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge)
  385. {
  386. struct fusb300_ep *ep;
  387. struct fusb300 *fusb300;
  388. unsigned long flags;
  389. int ret = 0;
  390. ep = container_of(_ep, struct fusb300_ep, ep);
  391. fusb300 = ep->fusb300;
  392. spin_lock_irqsave(&ep->fusb300->lock, flags);
  393. if (!list_empty(&ep->queue)) {
  394. ret = -EAGAIN;
  395. goto out;
  396. }
  397. if (value) {
  398. fusb300_set_epnstall(fusb300, ep->epnum);
  399. ep->stall = 1;
  400. if (wedge)
  401. ep->wedged = 1;
  402. } else {
  403. fusb300_clear_epnstall(fusb300, ep->epnum);
  404. ep->stall = 0;
  405. ep->wedged = 0;
  406. }
  407. out:
  408. spin_unlock_irqrestore(&ep->fusb300->lock, flags);
  409. return ret;
  410. }
  411. static int fusb300_set_halt(struct usb_ep *_ep, int value)
  412. {
  413. return fusb300_set_halt_and_wedge(_ep, value, 0);
  414. }
  415. static int fusb300_set_wedge(struct usb_ep *_ep)
  416. {
  417. return fusb300_set_halt_and_wedge(_ep, 1, 1);
  418. }
  419. static void fusb300_fifo_flush(struct usb_ep *_ep)
  420. {
  421. }
  422. static struct usb_ep_ops fusb300_ep_ops = {
  423. .enable = fusb300_enable,
  424. .disable = fusb300_disable,
  425. .alloc_request = fusb300_alloc_request,
  426. .free_request = fusb300_free_request,
  427. .queue = fusb300_queue,
  428. .dequeue = fusb300_dequeue,
  429. .set_halt = fusb300_set_halt,
  430. .fifo_flush = fusb300_fifo_flush,
  431. .set_wedge = fusb300_set_wedge,
  432. };
  433. /*****************************************************************************/
  434. static void fusb300_clear_int(struct fusb300 *fusb300, u32 offset,
  435. u32 value)
  436. {
  437. iowrite32(value, fusb300->reg + offset);
  438. }
  439. static void fusb300_reset(void)
  440. {
  441. }
  442. static void fusb300_set_cxstall(struct fusb300 *fusb300)
  443. {
  444. fusb300_enable_bit(fusb300, FUSB300_OFFSET_CSR,
  445. FUSB300_CSR_STL);
  446. }
  447. static void fusb300_set_cxdone(struct fusb300 *fusb300)
  448. {
  449. fusb300_enable_bit(fusb300, FUSB300_OFFSET_CSR,
  450. FUSB300_CSR_DONE);
  451. }
  452. /* read data from cx fifo */
  453. void fusb300_rdcxf(struct fusb300 *fusb300,
  454. u8 *buffer, u32 length)
  455. {
  456. int i = 0;
  457. u8 *tmp;
  458. u32 data;
  459. tmp = buffer;
  460. for (i = (length >> 2); i > 0; i--) {
  461. data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
  462. printk(KERN_DEBUG " 0x%x\n", data);
  463. *tmp = data & 0xFF;
  464. *(tmp + 1) = (data >> 8) & 0xFF;
  465. *(tmp + 2) = (data >> 16) & 0xFF;
  466. *(tmp + 3) = (data >> 24) & 0xFF;
  467. tmp = tmp + 4;
  468. }
  469. switch (length % 4) {
  470. case 1:
  471. data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
  472. printk(KERN_DEBUG " 0x%x\n", data);
  473. *tmp = data & 0xFF;
  474. break;
  475. case 2:
  476. data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
  477. printk(KERN_DEBUG " 0x%x\n", data);
  478. *tmp = data & 0xFF;
  479. *(tmp + 1) = (data >> 8) & 0xFF;
  480. break;
  481. case 3:
  482. data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
  483. printk(KERN_DEBUG " 0x%x\n", data);
  484. *tmp = data & 0xFF;
  485. *(tmp + 1) = (data >> 8) & 0xFF;
  486. *(tmp + 2) = (data >> 16) & 0xFF;
  487. break;
  488. default:
  489. break;
  490. }
  491. }
  492. static void fusb300_rdfifo(struct fusb300_ep *ep,
  493. struct fusb300_request *req,
  494. u32 length)
  495. {
  496. int i = 0;
  497. u8 *tmp;
  498. u32 data, reg;
  499. struct fusb300 *fusb300 = ep->fusb300;
  500. tmp = req->req.buf + req->req.actual;
  501. req->req.actual += length;
  502. if (req->req.actual > req->req.length)
  503. printk(KERN_DEBUG "req->req.actual > req->req.length\n");
  504. for (i = (length >> 2); i > 0; i--) {
  505. data = ioread32(fusb300->reg +
  506. FUSB300_OFFSET_EPPORT(ep->epnum));
  507. *tmp = data & 0xFF;
  508. *(tmp + 1) = (data >> 8) & 0xFF;
  509. *(tmp + 2) = (data >> 16) & 0xFF;
  510. *(tmp + 3) = (data >> 24) & 0xFF;
  511. tmp = tmp + 4;
  512. }
  513. switch (length % 4) {
  514. case 1:
  515. data = ioread32(fusb300->reg +
  516. FUSB300_OFFSET_EPPORT(ep->epnum));
  517. *tmp = data & 0xFF;
  518. break;
  519. case 2:
  520. data = ioread32(fusb300->reg +
  521. FUSB300_OFFSET_EPPORT(ep->epnum));
  522. *tmp = data & 0xFF;
  523. *(tmp + 1) = (data >> 8) & 0xFF;
  524. break;
  525. case 3:
  526. data = ioread32(fusb300->reg +
  527. FUSB300_OFFSET_EPPORT(ep->epnum));
  528. *tmp = data & 0xFF;
  529. *(tmp + 1) = (data >> 8) & 0xFF;
  530. *(tmp + 2) = (data >> 16) & 0xFF;
  531. break;
  532. default:
  533. break;
  534. }
  535. do {
  536. reg = ioread32(fusb300->reg + FUSB300_OFFSET_IGR1);
  537. reg &= FUSB300_IGR1_SYNF0_EMPTY_INT;
  538. if (i)
  539. printk(KERN_INFO "sync fifo is not empty!\n");
  540. i++;
  541. } while (!reg);
  542. }
  543. static u8 fusb300_get_epnstall(struct fusb300 *fusb300, u8 ep)
  544. {
  545. u8 value;
  546. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
  547. value = reg & FUSB300_EPSET0_STL;
  548. return value;
  549. }
  550. static u8 fusb300_get_cxstall(struct fusb300 *fusb300)
  551. {
  552. u8 value;
  553. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_CSR);
  554. value = (reg & FUSB300_CSR_STL) >> 1;
  555. return value;
  556. }
  557. static void request_error(struct fusb300 *fusb300)
  558. {
  559. fusb300_set_cxstall(fusb300);
  560. printk(KERN_DEBUG "request error!!\n");
  561. }
  562. static void get_status(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
  563. __releases(fusb300->lock)
  564. __acquires(fusb300->lock)
  565. {
  566. u8 ep;
  567. u16 status = 0;
  568. u16 w_index = ctrl->wIndex;
  569. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  570. case USB_RECIP_DEVICE:
  571. status = 1 << USB_DEVICE_SELF_POWERED;
  572. break;
  573. case USB_RECIP_INTERFACE:
  574. status = 0;
  575. break;
  576. case USB_RECIP_ENDPOINT:
  577. ep = w_index & USB_ENDPOINT_NUMBER_MASK;
  578. if (ep) {
  579. if (fusb300_get_epnstall(fusb300, ep))
  580. status = 1 << USB_ENDPOINT_HALT;
  581. } else {
  582. if (fusb300_get_cxstall(fusb300))
  583. status = 0;
  584. }
  585. break;
  586. default:
  587. request_error(fusb300);
  588. return; /* exit */
  589. }
  590. fusb300->ep0_data = cpu_to_le16(status);
  591. fusb300->ep0_req->buf = &fusb300->ep0_data;
  592. fusb300->ep0_req->length = 2;
  593. spin_unlock(&fusb300->lock);
  594. fusb300_queue(fusb300->gadget.ep0, fusb300->ep0_req, GFP_KERNEL);
  595. spin_lock(&fusb300->lock);
  596. }
  597. static void set_feature(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
  598. {
  599. u8 ep;
  600. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  601. case USB_RECIP_DEVICE:
  602. fusb300_set_cxdone(fusb300);
  603. break;
  604. case USB_RECIP_INTERFACE:
  605. fusb300_set_cxdone(fusb300);
  606. break;
  607. case USB_RECIP_ENDPOINT: {
  608. u16 w_index = le16_to_cpu(ctrl->wIndex);
  609. ep = w_index & USB_ENDPOINT_NUMBER_MASK;
  610. if (ep)
  611. fusb300_set_epnstall(fusb300, ep);
  612. else
  613. fusb300_set_cxstall(fusb300);
  614. fusb300_set_cxdone(fusb300);
  615. }
  616. break;
  617. default:
  618. request_error(fusb300);
  619. break;
  620. }
  621. }
  622. static void fusb300_clear_seqnum(struct fusb300 *fusb300, u8 ep)
  623. {
  624. fusb300_enable_bit(fusb300, FUSB300_OFFSET_EPSET0(ep),
  625. FUSB300_EPSET0_CLRSEQNUM);
  626. }
  627. static void clear_feature(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
  628. {
  629. struct fusb300_ep *ep =
  630. fusb300->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK];
  631. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  632. case USB_RECIP_DEVICE:
  633. fusb300_set_cxdone(fusb300);
  634. break;
  635. case USB_RECIP_INTERFACE:
  636. fusb300_set_cxdone(fusb300);
  637. break;
  638. case USB_RECIP_ENDPOINT:
  639. if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) {
  640. if (ep->wedged) {
  641. fusb300_set_cxdone(fusb300);
  642. break;
  643. }
  644. if (ep->stall) {
  645. ep->stall = 0;
  646. fusb300_clear_seqnum(fusb300, ep->epnum);
  647. fusb300_clear_epnstall(fusb300, ep->epnum);
  648. if (!list_empty(&ep->queue))
  649. enable_fifo_int(ep);
  650. }
  651. }
  652. fusb300_set_cxdone(fusb300);
  653. break;
  654. default:
  655. request_error(fusb300);
  656. break;
  657. }
  658. }
  659. static void fusb300_set_dev_addr(struct fusb300 *fusb300, u16 addr)
  660. {
  661. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_DAR);
  662. reg &= ~FUSB300_DAR_DRVADDR_MSK;
  663. reg |= FUSB300_DAR_DRVADDR(addr);
  664. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_DAR);
  665. }
  666. static void set_address(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
  667. {
  668. if (ctrl->wValue >= 0x0100)
  669. request_error(fusb300);
  670. else {
  671. fusb300_set_dev_addr(fusb300, ctrl->wValue);
  672. fusb300_set_cxdone(fusb300);
  673. }
  674. }
  675. #define UVC_COPY_DESCRIPTORS(mem, src) \
  676. do { \
  677. const struct usb_descriptor_header * const *__src; \
  678. for (__src = src; *__src; ++__src) { \
  679. memcpy(mem, *__src, (*__src)->bLength); \
  680. mem += (*__src)->bLength; \
  681. } \
  682. } while (0)
  683. static int setup_packet(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
  684. {
  685. u8 *p = (u8 *)ctrl;
  686. u8 ret = 0;
  687. u8 i = 0;
  688. fusb300_rdcxf(fusb300, p, 8);
  689. fusb300->ep0_dir = ctrl->bRequestType & USB_DIR_IN;
  690. fusb300->ep0_length = ctrl->wLength;
  691. /* check request */
  692. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  693. switch (ctrl->bRequest) {
  694. case USB_REQ_GET_STATUS:
  695. get_status(fusb300, ctrl);
  696. break;
  697. case USB_REQ_CLEAR_FEATURE:
  698. clear_feature(fusb300, ctrl);
  699. break;
  700. case USB_REQ_SET_FEATURE:
  701. set_feature(fusb300, ctrl);
  702. break;
  703. case USB_REQ_SET_ADDRESS:
  704. set_address(fusb300, ctrl);
  705. break;
  706. case USB_REQ_SET_CONFIGURATION:
  707. fusb300_enable_bit(fusb300, FUSB300_OFFSET_DAR,
  708. FUSB300_DAR_SETCONFG);
  709. /* clear sequence number */
  710. for (i = 1; i <= FUSB300_MAX_NUM_EP; i++)
  711. fusb300_clear_seqnum(fusb300, i);
  712. fusb300->reenum = 1;
  713. ret = 1;
  714. break;
  715. default:
  716. ret = 1;
  717. break;
  718. }
  719. } else
  720. ret = 1;
  721. return ret;
  722. }
  723. static void done(struct fusb300_ep *ep, struct fusb300_request *req,
  724. int status)
  725. {
  726. list_del_init(&req->queue);
  727. /* don't modify queue heads during completion callback */
  728. if (ep->fusb300->gadget.speed == USB_SPEED_UNKNOWN)
  729. req->req.status = -ESHUTDOWN;
  730. else
  731. req->req.status = status;
  732. spin_unlock(&ep->fusb300->lock);
  733. req->req.complete(&ep->ep, &req->req);
  734. spin_lock(&ep->fusb300->lock);
  735. if (ep->epnum) {
  736. disable_fifo_int(ep);
  737. if (!list_empty(&ep->queue))
  738. enable_fifo_int(ep);
  739. } else
  740. fusb300_set_cxdone(ep->fusb300);
  741. }
  742. static void fusb300_fill_idma_prdtbl(struct fusb300_ep *ep, dma_addr_t d,
  743. u32 len)
  744. {
  745. u32 value;
  746. u32 reg;
  747. /* wait SW owner */
  748. do {
  749. reg = ioread32(ep->fusb300->reg +
  750. FUSB300_OFFSET_EPPRD_W0(ep->epnum));
  751. reg &= FUSB300_EPPRD0_H;
  752. } while (reg);
  753. iowrite32(d, ep->fusb300->reg + FUSB300_OFFSET_EPPRD_W1(ep->epnum));
  754. value = FUSB300_EPPRD0_BTC(len) | FUSB300_EPPRD0_H |
  755. FUSB300_EPPRD0_F | FUSB300_EPPRD0_L | FUSB300_EPPRD0_I;
  756. iowrite32(value, ep->fusb300->reg + FUSB300_OFFSET_EPPRD_W0(ep->epnum));
  757. iowrite32(0x0, ep->fusb300->reg + FUSB300_OFFSET_EPPRD_W2(ep->epnum));
  758. fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_EPPRDRDY,
  759. FUSB300_EPPRDR_EP_PRD_RDY(ep->epnum));
  760. }
  761. static void fusb300_wait_idma_finished(struct fusb300_ep *ep)
  762. {
  763. u32 reg;
  764. do {
  765. reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_IGR1);
  766. if ((reg & FUSB300_IGR1_VBUS_CHG_INT) ||
  767. (reg & FUSB300_IGR1_WARM_RST_INT) ||
  768. (reg & FUSB300_IGR1_HOT_RST_INT) ||
  769. (reg & FUSB300_IGR1_USBRST_INT)
  770. )
  771. goto IDMA_RESET;
  772. reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_IGR0);
  773. reg &= FUSB300_IGR0_EPn_PRD_INT(ep->epnum);
  774. } while (!reg);
  775. fusb300_clear_int(ep->fusb300, FUSB300_OFFSET_IGR0,
  776. FUSB300_IGR0_EPn_PRD_INT(ep->epnum));
  777. IDMA_RESET:
  778. fusb300_clear_int(ep->fusb300, FUSB300_OFFSET_IGER0,
  779. FUSB300_IGER0_EEPn_PRD_INT(ep->epnum));
  780. }
  781. static void fusb300_set_idma(struct fusb300_ep *ep,
  782. struct fusb300_request *req)
  783. {
  784. dma_addr_t d;
  785. d = dma_map_single(NULL, req->req.buf, req->req.length, DMA_TO_DEVICE);
  786. if (dma_mapping_error(NULL, d)) {
  787. printk(KERN_DEBUG "dma_mapping_error\n");
  788. return;
  789. }
  790. dma_sync_single_for_device(NULL, d, req->req.length, DMA_TO_DEVICE);
  791. fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_IGER0,
  792. FUSB300_IGER0_EEPn_PRD_INT(ep->epnum));
  793. fusb300_fill_idma_prdtbl(ep, d, req->req.length);
  794. /* check idma is done */
  795. fusb300_wait_idma_finished(ep);
  796. dma_unmap_single(NULL, d, req->req.length, DMA_TO_DEVICE);
  797. }
  798. static void in_ep_fifo_handler(struct fusb300_ep *ep)
  799. {
  800. struct fusb300_request *req = list_entry(ep->queue.next,
  801. struct fusb300_request, queue);
  802. if (req->req.length)
  803. fusb300_set_idma(ep, req);
  804. done(ep, req, 0);
  805. }
  806. static void out_ep_fifo_handler(struct fusb300_ep *ep)
  807. {
  808. struct fusb300 *fusb300 = ep->fusb300;
  809. struct fusb300_request *req = list_entry(ep->queue.next,
  810. struct fusb300_request, queue);
  811. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPFFR(ep->epnum));
  812. u32 length = reg & FUSB300_FFR_BYCNT;
  813. fusb300_rdfifo(ep, req, length);
  814. /* finish out transfer */
  815. if ((req->req.length == req->req.actual) || (length < ep->ep.maxpacket))
  816. done(ep, req, 0);
  817. }
  818. static void check_device_mode(struct fusb300 *fusb300)
  819. {
  820. u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_GCR);
  821. switch (reg & FUSB300_GCR_DEVEN_MSK) {
  822. case FUSB300_GCR_DEVEN_SS:
  823. fusb300->gadget.speed = USB_SPEED_SUPER;
  824. break;
  825. case FUSB300_GCR_DEVEN_HS:
  826. fusb300->gadget.speed = USB_SPEED_HIGH;
  827. break;
  828. case FUSB300_GCR_DEVEN_FS:
  829. fusb300->gadget.speed = USB_SPEED_FULL;
  830. break;
  831. default:
  832. fusb300->gadget.speed = USB_SPEED_UNKNOWN;
  833. break;
  834. }
  835. printk(KERN_INFO "dev_mode = %d\n", (reg & FUSB300_GCR_DEVEN_MSK));
  836. }
  837. static void fusb300_ep0out(struct fusb300 *fusb300)
  838. {
  839. struct fusb300_ep *ep = fusb300->ep[0];
  840. u32 reg;
  841. if (!list_empty(&ep->queue)) {
  842. struct fusb300_request *req;
  843. req = list_first_entry(&ep->queue,
  844. struct fusb300_request, queue);
  845. if (req->req.length)
  846. fusb300_rdcxf(ep->fusb300, req->req.buf,
  847. req->req.length);
  848. done(ep, req, 0);
  849. reg = ioread32(fusb300->reg + FUSB300_OFFSET_IGER1);
  850. reg &= ~FUSB300_IGER1_CX_OUT_INT;
  851. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_IGER1);
  852. } else
  853. pr_err("%s : empty queue\n", __func__);
  854. }
  855. static void fusb300_ep0in(struct fusb300 *fusb300)
  856. {
  857. struct fusb300_request *req;
  858. struct fusb300_ep *ep = fusb300->ep[0];
  859. if ((!list_empty(&ep->queue)) && (fusb300->ep0_dir)) {
  860. req = list_entry(ep->queue.next,
  861. struct fusb300_request, queue);
  862. if (req->req.length)
  863. fusb300_wrcxf(ep, req);
  864. if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
  865. done(ep, req, 0);
  866. } else
  867. fusb300_set_cxdone(fusb300);
  868. }
  869. static void fusb300_grp2_handler(void)
  870. {
  871. }
  872. static void fusb300_grp3_handler(void)
  873. {
  874. }
  875. static void fusb300_grp4_handler(void)
  876. {
  877. }
  878. static void fusb300_grp5_handler(void)
  879. {
  880. }
  881. static irqreturn_t fusb300_irq(int irq, void *_fusb300)
  882. {
  883. struct fusb300 *fusb300 = _fusb300;
  884. u32 int_grp1 = ioread32(fusb300->reg + FUSB300_OFFSET_IGR1);
  885. u32 int_grp1_en = ioread32(fusb300->reg + FUSB300_OFFSET_IGER1);
  886. u32 int_grp0 = ioread32(fusb300->reg + FUSB300_OFFSET_IGR0);
  887. u32 int_grp0_en = ioread32(fusb300->reg + FUSB300_OFFSET_IGER0);
  888. struct usb_ctrlrequest ctrl;
  889. u8 in;
  890. u32 reg;
  891. int i;
  892. spin_lock(&fusb300->lock);
  893. int_grp1 &= int_grp1_en;
  894. int_grp0 &= int_grp0_en;
  895. if (int_grp1 & FUSB300_IGR1_WARM_RST_INT) {
  896. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  897. FUSB300_IGR1_WARM_RST_INT);
  898. printk(KERN_INFO"fusb300_warmreset\n");
  899. fusb300_reset();
  900. }
  901. if (int_grp1 & FUSB300_IGR1_HOT_RST_INT) {
  902. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  903. FUSB300_IGR1_HOT_RST_INT);
  904. printk(KERN_INFO"fusb300_hotreset\n");
  905. fusb300_reset();
  906. }
  907. if (int_grp1 & FUSB300_IGR1_USBRST_INT) {
  908. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  909. FUSB300_IGR1_USBRST_INT);
  910. fusb300_reset();
  911. }
  912. /* COMABT_INT has a highest priority */
  913. if (int_grp1 & FUSB300_IGR1_CX_COMABT_INT) {
  914. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  915. FUSB300_IGR1_CX_COMABT_INT);
  916. printk(KERN_INFO"fusb300_ep0abt\n");
  917. }
  918. if (int_grp1 & FUSB300_IGR1_VBUS_CHG_INT) {
  919. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  920. FUSB300_IGR1_VBUS_CHG_INT);
  921. printk(KERN_INFO"fusb300_vbus_change\n");
  922. }
  923. if (int_grp1 & FUSB300_IGR1_U3_EXIT_FAIL_INT) {
  924. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  925. FUSB300_IGR1_U3_EXIT_FAIL_INT);
  926. }
  927. if (int_grp1 & FUSB300_IGR1_U2_EXIT_FAIL_INT) {
  928. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  929. FUSB300_IGR1_U2_EXIT_FAIL_INT);
  930. }
  931. if (int_grp1 & FUSB300_IGR1_U1_EXIT_FAIL_INT) {
  932. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  933. FUSB300_IGR1_U1_EXIT_FAIL_INT);
  934. }
  935. if (int_grp1 & FUSB300_IGR1_U2_ENTRY_FAIL_INT) {
  936. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  937. FUSB300_IGR1_U2_ENTRY_FAIL_INT);
  938. }
  939. if (int_grp1 & FUSB300_IGR1_U1_ENTRY_FAIL_INT) {
  940. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  941. FUSB300_IGR1_U1_ENTRY_FAIL_INT);
  942. }
  943. if (int_grp1 & FUSB300_IGR1_U3_EXIT_INT) {
  944. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  945. FUSB300_IGR1_U3_EXIT_INT);
  946. printk(KERN_INFO "FUSB300_IGR1_U3_EXIT_INT\n");
  947. }
  948. if (int_grp1 & FUSB300_IGR1_U2_EXIT_INT) {
  949. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  950. FUSB300_IGR1_U2_EXIT_INT);
  951. printk(KERN_INFO "FUSB300_IGR1_U2_EXIT_INT\n");
  952. }
  953. if (int_grp1 & FUSB300_IGR1_U1_EXIT_INT) {
  954. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  955. FUSB300_IGR1_U1_EXIT_INT);
  956. printk(KERN_INFO "FUSB300_IGR1_U1_EXIT_INT\n");
  957. }
  958. if (int_grp1 & FUSB300_IGR1_U3_ENTRY_INT) {
  959. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  960. FUSB300_IGR1_U3_ENTRY_INT);
  961. printk(KERN_INFO "FUSB300_IGR1_U3_ENTRY_INT\n");
  962. fusb300_enable_bit(fusb300, FUSB300_OFFSET_SSCR1,
  963. FUSB300_SSCR1_GO_U3_DONE);
  964. }
  965. if (int_grp1 & FUSB300_IGR1_U2_ENTRY_INT) {
  966. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  967. FUSB300_IGR1_U2_ENTRY_INT);
  968. printk(KERN_INFO "FUSB300_IGR1_U2_ENTRY_INT\n");
  969. }
  970. if (int_grp1 & FUSB300_IGR1_U1_ENTRY_INT) {
  971. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  972. FUSB300_IGR1_U1_ENTRY_INT);
  973. printk(KERN_INFO "FUSB300_IGR1_U1_ENTRY_INT\n");
  974. }
  975. if (int_grp1 & FUSB300_IGR1_RESM_INT) {
  976. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  977. FUSB300_IGR1_RESM_INT);
  978. printk(KERN_INFO "fusb300_resume\n");
  979. }
  980. if (int_grp1 & FUSB300_IGR1_SUSP_INT) {
  981. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  982. FUSB300_IGR1_SUSP_INT);
  983. printk(KERN_INFO "fusb300_suspend\n");
  984. }
  985. if (int_grp1 & FUSB300_IGR1_HS_LPM_INT) {
  986. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  987. FUSB300_IGR1_HS_LPM_INT);
  988. printk(KERN_INFO "fusb300_HS_LPM_INT\n");
  989. }
  990. if (int_grp1 & FUSB300_IGR1_DEV_MODE_CHG_INT) {
  991. fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
  992. FUSB300_IGR1_DEV_MODE_CHG_INT);
  993. check_device_mode(fusb300);
  994. }
  995. if (int_grp1 & FUSB300_IGR1_CX_COMFAIL_INT) {
  996. fusb300_set_cxstall(fusb300);
  997. printk(KERN_INFO "fusb300_ep0fail\n");
  998. }
  999. if (int_grp1 & FUSB300_IGR1_CX_SETUP_INT) {
  1000. printk(KERN_INFO "fusb300_ep0setup\n");
  1001. if (setup_packet(fusb300, &ctrl)) {
  1002. spin_unlock(&fusb300->lock);
  1003. if (fusb300->driver->setup(&fusb300->gadget, &ctrl) < 0)
  1004. fusb300_set_cxstall(fusb300);
  1005. spin_lock(&fusb300->lock);
  1006. }
  1007. }
  1008. if (int_grp1 & FUSB300_IGR1_CX_CMDEND_INT)
  1009. printk(KERN_INFO "fusb300_cmdend\n");
  1010. if (int_grp1 & FUSB300_IGR1_CX_OUT_INT) {
  1011. printk(KERN_INFO "fusb300_cxout\n");
  1012. fusb300_ep0out(fusb300);
  1013. }
  1014. if (int_grp1 & FUSB300_IGR1_CX_IN_INT) {
  1015. printk(KERN_INFO "fusb300_cxin\n");
  1016. fusb300_ep0in(fusb300);
  1017. }
  1018. if (int_grp1 & FUSB300_IGR1_INTGRP5)
  1019. fusb300_grp5_handler();
  1020. if (int_grp1 & FUSB300_IGR1_INTGRP4)
  1021. fusb300_grp4_handler();
  1022. if (int_grp1 & FUSB300_IGR1_INTGRP3)
  1023. fusb300_grp3_handler();
  1024. if (int_grp1 & FUSB300_IGR1_INTGRP2)
  1025. fusb300_grp2_handler();
  1026. if (int_grp0) {
  1027. for (i = 1; i < FUSB300_MAX_NUM_EP; i++) {
  1028. if (int_grp0 & FUSB300_IGR0_EPn_FIFO_INT(i)) {
  1029. reg = ioread32(fusb300->reg +
  1030. FUSB300_OFFSET_EPSET1(i));
  1031. in = (reg & FUSB300_EPSET1_DIRIN) ? 1 : 0;
  1032. if (in)
  1033. in_ep_fifo_handler(fusb300->ep[i]);
  1034. else
  1035. out_ep_fifo_handler(fusb300->ep[i]);
  1036. }
  1037. }
  1038. }
  1039. spin_unlock(&fusb300->lock);
  1040. return IRQ_HANDLED;
  1041. }
  1042. static void fusb300_set_u2_timeout(struct fusb300 *fusb300,
  1043. u32 time)
  1044. {
  1045. u32 reg;
  1046. reg = ioread32(fusb300->reg + FUSB300_OFFSET_TT);
  1047. reg &= ~0xff;
  1048. reg |= FUSB300_SSCR2_U2TIMEOUT(time);
  1049. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_TT);
  1050. }
  1051. static void fusb300_set_u1_timeout(struct fusb300 *fusb300,
  1052. u32 time)
  1053. {
  1054. u32 reg;
  1055. reg = ioread32(fusb300->reg + FUSB300_OFFSET_TT);
  1056. reg &= ~(0xff << 8);
  1057. reg |= FUSB300_SSCR2_U1TIMEOUT(time);
  1058. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_TT);
  1059. }
  1060. static void init_controller(struct fusb300 *fusb300)
  1061. {
  1062. u32 reg;
  1063. u32 mask = 0;
  1064. u32 val = 0;
  1065. /* split on */
  1066. mask = val = FUSB300_AHBBCR_S0_SPLIT_ON | FUSB300_AHBBCR_S1_SPLIT_ON;
  1067. reg = ioread32(fusb300->reg + FUSB300_OFFSET_AHBCR);
  1068. reg &= ~mask;
  1069. reg |= val;
  1070. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_AHBCR);
  1071. /* enable high-speed LPM */
  1072. mask = val = FUSB300_HSCR_HS_LPM_PERMIT;
  1073. reg = ioread32(fusb300->reg + FUSB300_OFFSET_HSCR);
  1074. reg &= ~mask;
  1075. reg |= val;
  1076. iowrite32(reg, fusb300->reg + FUSB300_OFFSET_HSCR);
  1077. /*set u1 u2 timmer*/
  1078. fusb300_set_u2_timeout(fusb300, 0xff);
  1079. fusb300_set_u1_timeout(fusb300, 0xff);
  1080. /* enable all grp1 interrupt */
  1081. iowrite32(0xcfffff9f, fusb300->reg + FUSB300_OFFSET_IGER1);
  1082. }
  1083. /*------------------------------------------------------------------------*/
  1084. static int fusb300_udc_start(struct usb_gadget *g,
  1085. struct usb_gadget_driver *driver)
  1086. {
  1087. struct fusb300 *fusb300 = to_fusb300(g);
  1088. /* hook up the driver */
  1089. driver->driver.bus = NULL;
  1090. fusb300->driver = driver;
  1091. fusb300->gadget.dev.driver = &driver->driver;
  1092. return 0;
  1093. }
  1094. static int fusb300_udc_stop(struct usb_gadget *g,
  1095. struct usb_gadget_driver *driver)
  1096. {
  1097. struct fusb300 *fusb300 = to_fusb300(g);
  1098. driver->unbind(&fusb300->gadget);
  1099. fusb300->gadget.dev.driver = NULL;
  1100. init_controller(fusb300);
  1101. fusb300->driver = NULL;
  1102. return 0;
  1103. }
  1104. /*--------------------------------------------------------------------------*/
  1105. static int fusb300_udc_pullup(struct usb_gadget *_gadget, int is_active)
  1106. {
  1107. return 0;
  1108. }
  1109. static const struct usb_gadget_ops fusb300_gadget_ops = {
  1110. .pullup = fusb300_udc_pullup,
  1111. .udc_start = fusb300_udc_start,
  1112. .udc_stop = fusb300_udc_stop,
  1113. };
  1114. static int __exit fusb300_remove(struct platform_device *pdev)
  1115. {
  1116. struct fusb300 *fusb300 = dev_get_drvdata(&pdev->dev);
  1117. usb_del_gadget_udc(&fusb300->gadget);
  1118. iounmap(fusb300->reg);
  1119. free_irq(platform_get_irq(pdev, 0), fusb300);
  1120. fusb300_free_request(&fusb300->ep[0]->ep, fusb300->ep0_req);
  1121. kfree(fusb300);
  1122. return 0;
  1123. }
  1124. static int __init fusb300_probe(struct platform_device *pdev)
  1125. {
  1126. struct resource *res, *ires, *ires1;
  1127. void __iomem *reg = NULL;
  1128. struct fusb300 *fusb300 = NULL;
  1129. struct fusb300_ep *_ep[FUSB300_MAX_NUM_EP];
  1130. int ret = 0;
  1131. int i;
  1132. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1133. if (!res) {
  1134. ret = -ENODEV;
  1135. pr_err("platform_get_resource error.\n");
  1136. goto clean_up;
  1137. }
  1138. ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1139. if (!ires) {
  1140. ret = -ENODEV;
  1141. dev_err(&pdev->dev,
  1142. "platform_get_resource IORESOURCE_IRQ error.\n");
  1143. goto clean_up;
  1144. }
  1145. ires1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  1146. if (!ires1) {
  1147. ret = -ENODEV;
  1148. dev_err(&pdev->dev,
  1149. "platform_get_resource IORESOURCE_IRQ 1 error.\n");
  1150. goto clean_up;
  1151. }
  1152. reg = ioremap(res->start, resource_size(res));
  1153. if (reg == NULL) {
  1154. ret = -ENOMEM;
  1155. pr_err("ioremap error.\n");
  1156. goto clean_up;
  1157. }
  1158. /* initialize udc */
  1159. fusb300 = kzalloc(sizeof(struct fusb300), GFP_KERNEL);
  1160. if (fusb300 == NULL) {
  1161. pr_err("kzalloc error\n");
  1162. goto clean_up;
  1163. }
  1164. for (i = 0; i < FUSB300_MAX_NUM_EP; i++) {
  1165. _ep[i] = kzalloc(sizeof(struct fusb300_ep), GFP_KERNEL);
  1166. if (_ep[i] == NULL) {
  1167. pr_err("_ep kzalloc error\n");
  1168. goto clean_up;
  1169. }
  1170. fusb300->ep[i] = _ep[i];
  1171. }
  1172. spin_lock_init(&fusb300->lock);
  1173. dev_set_drvdata(&pdev->dev, fusb300);
  1174. fusb300->gadget.ops = &fusb300_gadget_ops;
  1175. device_initialize(&fusb300->gadget.dev);
  1176. dev_set_name(&fusb300->gadget.dev, "gadget");
  1177. fusb300->gadget.max_speed = USB_SPEED_HIGH;
  1178. fusb300->gadget.dev.parent = &pdev->dev;
  1179. fusb300->gadget.dev.dma_mask = pdev->dev.dma_mask;
  1180. fusb300->gadget.dev.release = pdev->dev.release;
  1181. fusb300->gadget.name = udc_name;
  1182. fusb300->reg = reg;
  1183. ret = request_irq(ires->start, fusb300_irq, IRQF_SHARED,
  1184. udc_name, fusb300);
  1185. if (ret < 0) {
  1186. pr_err("request_irq error (%d)\n", ret);
  1187. goto clean_up;
  1188. }
  1189. ret = request_irq(ires1->start, fusb300_irq,
  1190. IRQF_SHARED, udc_name, fusb300);
  1191. if (ret < 0) {
  1192. pr_err("request_irq1 error (%d)\n", ret);
  1193. goto clean_up;
  1194. }
  1195. INIT_LIST_HEAD(&fusb300->gadget.ep_list);
  1196. for (i = 0; i < FUSB300_MAX_NUM_EP ; i++) {
  1197. struct fusb300_ep *ep = fusb300->ep[i];
  1198. if (i != 0) {
  1199. INIT_LIST_HEAD(&fusb300->ep[i]->ep.ep_list);
  1200. list_add_tail(&fusb300->ep[i]->ep.ep_list,
  1201. &fusb300->gadget.ep_list);
  1202. }
  1203. ep->fusb300 = fusb300;
  1204. INIT_LIST_HEAD(&ep->queue);
  1205. ep->ep.name = fusb300_ep_name[i];
  1206. ep->ep.ops = &fusb300_ep_ops;
  1207. ep->ep.maxpacket = HS_BULK_MAX_PACKET_SIZE;
  1208. }
  1209. fusb300->ep[0]->ep.maxpacket = HS_CTL_MAX_PACKET_SIZE;
  1210. fusb300->ep[0]->epnum = 0;
  1211. fusb300->gadget.ep0 = &fusb300->ep[0]->ep;
  1212. INIT_LIST_HEAD(&fusb300->gadget.ep0->ep_list);
  1213. fusb300->ep0_req = fusb300_alloc_request(&fusb300->ep[0]->ep,
  1214. GFP_KERNEL);
  1215. if (fusb300->ep0_req == NULL)
  1216. goto clean_up3;
  1217. init_controller(fusb300);
  1218. ret = usb_add_gadget_udc(&pdev->dev, &fusb300->gadget);
  1219. if (ret)
  1220. goto err_add_udc;
  1221. ret = device_add(&fusb300->gadget.dev);
  1222. if (ret) {
  1223. pr_err("device_add error (%d)\n", ret);
  1224. goto err_add_device;
  1225. }
  1226. dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
  1227. return 0;
  1228. err_add_device:
  1229. usb_del_gadget_udc(&fusb300->gadget);
  1230. err_add_udc:
  1231. fusb300_free_request(&fusb300->ep[0]->ep, fusb300->ep0_req);
  1232. clean_up3:
  1233. free_irq(ires->start, fusb300);
  1234. clean_up:
  1235. if (fusb300) {
  1236. if (fusb300->ep0_req)
  1237. fusb300_free_request(&fusb300->ep[0]->ep,
  1238. fusb300->ep0_req);
  1239. kfree(fusb300);
  1240. }
  1241. if (reg)
  1242. iounmap(reg);
  1243. return ret;
  1244. }
  1245. static struct platform_driver fusb300_driver = {
  1246. .remove = __exit_p(fusb300_remove),
  1247. .driver = {
  1248. .name = (char *) udc_name,
  1249. .owner = THIS_MODULE,
  1250. },
  1251. };
  1252. module_platform_driver_probe(fusb300_driver, fusb300_probe);