fusb300_udc.c 40 KB

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