s3c-hsudc.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. /* linux/drivers/usb/gadget/s3c-hsudc.c
  2. *
  3. * Copyright (c) 2010 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com/
  5. *
  6. * S3C24XX USB 2.0 High-speed USB controller gadget driver
  7. *
  8. * The S3C24XX USB 2.0 high-speed USB controller supports upto 9 endpoints.
  9. * Each endpoint can be configured as either in or out endpoint. Endpoints
  10. * can be configured for Bulk or Interrupt transfer mode.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/delay.h>
  23. #include <linux/io.h>
  24. #include <linux/slab.h>
  25. #include <linux/clk.h>
  26. #include <linux/usb/ch9.h>
  27. #include <linux/usb/gadget.h>
  28. #include <mach/regs-s3c2443-clock.h>
  29. #include <plat/udc.h>
  30. #define S3C_HSUDC_REG(x) (x)
  31. /* Non-Indexed Registers */
  32. #define S3C_IR S3C_HSUDC_REG(0x00) /* Index Register */
  33. #define S3C_EIR S3C_HSUDC_REG(0x04) /* EP Intr Status */
  34. #define S3C_EIR_EP0 (1<<0)
  35. #define S3C_EIER S3C_HSUDC_REG(0x08) /* EP Intr Enable */
  36. #define S3C_FAR S3C_HSUDC_REG(0x0c) /* Gadget Address */
  37. #define S3C_FNR S3C_HSUDC_REG(0x10) /* Frame Number */
  38. #define S3C_EDR S3C_HSUDC_REG(0x14) /* EP Direction */
  39. #define S3C_TR S3C_HSUDC_REG(0x18) /* Test Register */
  40. #define S3C_SSR S3C_HSUDC_REG(0x1c) /* System Status */
  41. #define S3C_SSR_DTZIEN_EN (0xff8f)
  42. #define S3C_SSR_ERR (0xff80)
  43. #define S3C_SSR_VBUSON (1 << 8)
  44. #define S3C_SSR_HSP (1 << 4)
  45. #define S3C_SSR_SDE (1 << 3)
  46. #define S3C_SSR_RESUME (1 << 2)
  47. #define S3C_SSR_SUSPEND (1 << 1)
  48. #define S3C_SSR_RESET (1 << 0)
  49. #define S3C_SCR S3C_HSUDC_REG(0x20) /* System Control */
  50. #define S3C_SCR_DTZIEN_EN (1 << 14)
  51. #define S3C_SCR_RRD_EN (1 << 5)
  52. #define S3C_SCR_SUS_EN (1 << 1)
  53. #define S3C_SCR_RST_EN (1 << 0)
  54. #define S3C_EP0SR S3C_HSUDC_REG(0x24) /* EP0 Status */
  55. #define S3C_EP0SR_EP0_LWO (1 << 6)
  56. #define S3C_EP0SR_STALL (1 << 4)
  57. #define S3C_EP0SR_TX_SUCCESS (1 << 1)
  58. #define S3C_EP0SR_RX_SUCCESS (1 << 0)
  59. #define S3C_EP0CR S3C_HSUDC_REG(0x28) /* EP0 Control */
  60. #define S3C_BR(_x) S3C_HSUDC_REG(0x60 + (_x * 4))
  61. /* Indexed Registers */
  62. #define S3C_ESR S3C_HSUDC_REG(0x2c) /* EPn Status */
  63. #define S3C_ESR_FLUSH (1 << 6)
  64. #define S3C_ESR_STALL (1 << 5)
  65. #define S3C_ESR_LWO (1 << 4)
  66. #define S3C_ESR_PSIF_ONE (1 << 2)
  67. #define S3C_ESR_PSIF_TWO (2 << 2)
  68. #define S3C_ESR_TX_SUCCESS (1 << 1)
  69. #define S3C_ESR_RX_SUCCESS (1 << 0)
  70. #define S3C_ECR S3C_HSUDC_REG(0x30) /* EPn Control */
  71. #define S3C_ECR_DUEN (1 << 7)
  72. #define S3C_ECR_FLUSH (1 << 6)
  73. #define S3C_ECR_STALL (1 << 1)
  74. #define S3C_ECR_IEMS (1 << 0)
  75. #define S3C_BRCR S3C_HSUDC_REG(0x34) /* Read Count */
  76. #define S3C_BWCR S3C_HSUDC_REG(0x38) /* Write Count */
  77. #define S3C_MPR S3C_HSUDC_REG(0x3c) /* Max Pkt Size */
  78. #define WAIT_FOR_SETUP (0)
  79. #define DATA_STATE_XMIT (1)
  80. #define DATA_STATE_RECV (2)
  81. /**
  82. * struct s3c_hsudc_ep - Endpoint representation used by driver.
  83. * @ep: USB gadget layer representation of device endpoint.
  84. * @name: Endpoint name (as required by ep autoconfiguration).
  85. * @dev: Reference to the device controller to which this EP belongs.
  86. * @desc: Endpoint descriptor obtained from the gadget driver.
  87. * @queue: Transfer request queue for the endpoint.
  88. * @stopped: Maintains state of endpoint, set if EP is halted.
  89. * @bEndpointAddress: EP address (including direction bit).
  90. * @fifo: Base address of EP FIFO.
  91. */
  92. struct s3c_hsudc_ep {
  93. struct usb_ep ep;
  94. char name[20];
  95. struct s3c_hsudc *dev;
  96. const struct usb_endpoint_descriptor *desc;
  97. struct list_head queue;
  98. u8 stopped;
  99. u8 wedge;
  100. u8 bEndpointAddress;
  101. void __iomem *fifo;
  102. };
  103. /**
  104. * struct s3c_hsudc_req - Driver encapsulation of USB gadget transfer request.
  105. * @req: Reference to USB gadget transfer request.
  106. * @queue: Used for inserting this request to the endpoint request queue.
  107. */
  108. struct s3c_hsudc_req {
  109. struct usb_request req;
  110. struct list_head queue;
  111. };
  112. /**
  113. * struct s3c_hsudc - Driver's abstraction of the device controller.
  114. * @gadget: Instance of usb_gadget which is referenced by gadget driver.
  115. * @driver: Reference to currenty active gadget driver.
  116. * @dev: The device reference used by probe function.
  117. * @lock: Lock to synchronize the usage of Endpoints (EP's are indexed).
  118. * @regs: Remapped base address of controller's register space.
  119. * @mem_rsrc: Device memory resource used for remapping device register space.
  120. * irq: IRQ number used by the controller.
  121. * uclk: Reference to the controller clock.
  122. * ep0state: Current state of EP0.
  123. * ep: List of endpoints supported by the controller.
  124. */
  125. struct s3c_hsudc {
  126. struct usb_gadget gadget;
  127. struct usb_gadget_driver *driver;
  128. struct device *dev;
  129. struct s3c24xx_hsudc_platdata *pd;
  130. spinlock_t lock;
  131. void __iomem *regs;
  132. struct resource *mem_rsrc;
  133. int irq;
  134. struct clk *uclk;
  135. int ep0state;
  136. struct s3c_hsudc_ep ep[];
  137. };
  138. #define ep_maxpacket(_ep) ((_ep)->ep.maxpacket)
  139. #define ep_is_in(_ep) ((_ep)->bEndpointAddress & USB_DIR_IN)
  140. #define ep_index(_ep) ((_ep)->bEndpointAddress & \
  141. USB_ENDPOINT_NUMBER_MASK)
  142. static struct s3c_hsudc *the_controller;
  143. static const char driver_name[] = "s3c-udc";
  144. static const char ep0name[] = "ep0-control";
  145. static inline struct s3c_hsudc_req *our_req(struct usb_request *req)
  146. {
  147. return container_of(req, struct s3c_hsudc_req, req);
  148. }
  149. static inline struct s3c_hsudc_ep *our_ep(struct usb_ep *ep)
  150. {
  151. return container_of(ep, struct s3c_hsudc_ep, ep);
  152. }
  153. static inline struct s3c_hsudc *to_hsudc(struct usb_gadget *gadget)
  154. {
  155. return container_of(gadget, struct s3c_hsudc, gadget);
  156. }
  157. static inline void set_index(struct s3c_hsudc *hsudc, int ep_addr)
  158. {
  159. ep_addr &= USB_ENDPOINT_NUMBER_MASK;
  160. writel(ep_addr, hsudc->regs + S3C_IR);
  161. }
  162. static inline void __orr32(void __iomem *ptr, u32 val)
  163. {
  164. writel(readl(ptr) | val, ptr);
  165. }
  166. static void s3c_hsudc_init_phy(void)
  167. {
  168. u32 cfg;
  169. cfg = readl(S3C2443_PWRCFG) | S3C2443_PWRCFG_USBPHY;
  170. writel(cfg, S3C2443_PWRCFG);
  171. cfg = readl(S3C2443_URSTCON);
  172. cfg |= (S3C2443_URSTCON_FUNCRST | S3C2443_URSTCON_PHYRST);
  173. writel(cfg, S3C2443_URSTCON);
  174. mdelay(1);
  175. cfg = readl(S3C2443_URSTCON);
  176. cfg &= ~(S3C2443_URSTCON_FUNCRST | S3C2443_URSTCON_PHYRST);
  177. writel(cfg, S3C2443_URSTCON);
  178. cfg = readl(S3C2443_PHYCTRL);
  179. cfg &= ~(S3C2443_PHYCTRL_CLKSEL | S3C2443_PHYCTRL_DSPORT);
  180. cfg |= (S3C2443_PHYCTRL_EXTCLK | S3C2443_PHYCTRL_PLLSEL);
  181. writel(cfg, S3C2443_PHYCTRL);
  182. cfg = readl(S3C2443_PHYPWR);
  183. cfg &= ~(S3C2443_PHYPWR_FSUSPEND | S3C2443_PHYPWR_PLL_PWRDN |
  184. S3C2443_PHYPWR_XO_ON | S3C2443_PHYPWR_PLL_REFCLK |
  185. S3C2443_PHYPWR_ANALOG_PD);
  186. cfg |= S3C2443_PHYPWR_COMMON_ON;
  187. writel(cfg, S3C2443_PHYPWR);
  188. cfg = readl(S3C2443_UCLKCON);
  189. cfg |= (S3C2443_UCLKCON_DETECT_VBUS | S3C2443_UCLKCON_FUNC_CLKEN |
  190. S3C2443_UCLKCON_TCLKEN);
  191. writel(cfg, S3C2443_UCLKCON);
  192. }
  193. static void s3c_hsudc_uninit_phy(void)
  194. {
  195. u32 cfg;
  196. cfg = readl(S3C2443_PWRCFG) & ~S3C2443_PWRCFG_USBPHY;
  197. writel(cfg, S3C2443_PWRCFG);
  198. writel(S3C2443_PHYPWR_FSUSPEND, S3C2443_PHYPWR);
  199. cfg = readl(S3C2443_UCLKCON) & ~S3C2443_UCLKCON_FUNC_CLKEN;
  200. writel(cfg, S3C2443_UCLKCON);
  201. }
  202. /**
  203. * s3c_hsudc_complete_request - Complete a transfer request.
  204. * @hsep: Endpoint to which the request belongs.
  205. * @hsreq: Transfer request to be completed.
  206. * @status: Transfer completion status for the transfer request.
  207. */
  208. static void s3c_hsudc_complete_request(struct s3c_hsudc_ep *hsep,
  209. struct s3c_hsudc_req *hsreq, int status)
  210. {
  211. unsigned int stopped = hsep->stopped;
  212. struct s3c_hsudc *hsudc = hsep->dev;
  213. list_del_init(&hsreq->queue);
  214. hsreq->req.status = status;
  215. if (!ep_index(hsep)) {
  216. hsudc->ep0state = WAIT_FOR_SETUP;
  217. hsep->bEndpointAddress &= ~USB_DIR_IN;
  218. }
  219. hsep->stopped = 1;
  220. spin_unlock(&hsudc->lock);
  221. if (hsreq->req.complete != NULL)
  222. hsreq->req.complete(&hsep->ep, &hsreq->req);
  223. spin_lock(&hsudc->lock);
  224. hsep->stopped = stopped;
  225. }
  226. /**
  227. * s3c_hsudc_nuke_ep - Terminate all requests queued for a endpoint.
  228. * @hsep: Endpoint for which queued requests have to be terminated.
  229. * @status: Transfer completion status for the transfer request.
  230. */
  231. static void s3c_hsudc_nuke_ep(struct s3c_hsudc_ep *hsep, int status)
  232. {
  233. struct s3c_hsudc_req *hsreq;
  234. while (!list_empty(&hsep->queue)) {
  235. hsreq = list_entry(hsep->queue.next,
  236. struct s3c_hsudc_req, queue);
  237. s3c_hsudc_complete_request(hsep, hsreq, status);
  238. }
  239. }
  240. /**
  241. * s3c_hsudc_stop_activity - Stop activity on all endpoints.
  242. * @hsudc: Device controller for which EP activity is to be stopped.
  243. * @driver: Reference to the gadget driver which is currently active.
  244. *
  245. * All the endpoints are stopped and any pending transfer requests if any on
  246. * the endpoint are terminated.
  247. */
  248. static void s3c_hsudc_stop_activity(struct s3c_hsudc *hsudc,
  249. struct usb_gadget_driver *driver)
  250. {
  251. struct s3c_hsudc_ep *hsep;
  252. int epnum;
  253. hsudc->gadget.speed = USB_SPEED_UNKNOWN;
  254. for (epnum = 0; epnum < hsudc->pd->epnum; epnum++) {
  255. hsep = &hsudc->ep[epnum];
  256. hsep->stopped = 1;
  257. s3c_hsudc_nuke_ep(hsep, -ESHUTDOWN);
  258. }
  259. spin_unlock(&hsudc->lock);
  260. driver->disconnect(&hsudc->gadget);
  261. spin_lock(&hsudc->lock);
  262. }
  263. /**
  264. * s3c_hsudc_read_setup_pkt - Read the received setup packet from EP0 fifo.
  265. * @hsudc: Device controller from which setup packet is to be read.
  266. * @buf: The buffer into which the setup packet is read.
  267. *
  268. * The setup packet received in the EP0 fifo is read and stored into a
  269. * given buffer address.
  270. */
  271. static void s3c_hsudc_read_setup_pkt(struct s3c_hsudc *hsudc, u16 *buf)
  272. {
  273. int count;
  274. count = readl(hsudc->regs + S3C_BRCR);
  275. while (count--)
  276. *buf++ = (u16)readl(hsudc->regs + S3C_BR(0));
  277. writel(S3C_EP0SR_RX_SUCCESS, hsudc->regs + S3C_EP0SR);
  278. }
  279. /**
  280. * s3c_hsudc_write_fifo - Write next chunk of transfer data to EP fifo.
  281. * @hsep: Endpoint to which the data is to be written.
  282. * @hsreq: Transfer request from which the next chunk of data is written.
  283. *
  284. * Write the next chunk of data from a transfer request to the endpoint FIFO.
  285. * If the transfer request completes, 1 is returned, otherwise 0 is returned.
  286. */
  287. static int s3c_hsudc_write_fifo(struct s3c_hsudc_ep *hsep,
  288. struct s3c_hsudc_req *hsreq)
  289. {
  290. u16 *buf;
  291. u32 max = ep_maxpacket(hsep);
  292. u32 count, length;
  293. bool is_last;
  294. void __iomem *fifo = hsep->fifo;
  295. buf = hsreq->req.buf + hsreq->req.actual;
  296. prefetch(buf);
  297. length = hsreq->req.length - hsreq->req.actual;
  298. length = min(length, max);
  299. hsreq->req.actual += length;
  300. writel(length, hsep->dev->regs + S3C_BWCR);
  301. for (count = 0; count < length; count += 2)
  302. writel(*buf++, fifo);
  303. if (count != max) {
  304. is_last = true;
  305. } else {
  306. if (hsreq->req.length != hsreq->req.actual || hsreq->req.zero)
  307. is_last = false;
  308. else
  309. is_last = true;
  310. }
  311. if (is_last) {
  312. s3c_hsudc_complete_request(hsep, hsreq, 0);
  313. return 1;
  314. }
  315. return 0;
  316. }
  317. /**
  318. * s3c_hsudc_read_fifo - Read the next chunk of data from EP fifo.
  319. * @hsep: Endpoint from which the data is to be read.
  320. * @hsreq: Transfer request to which the next chunk of data read is written.
  321. *
  322. * Read the next chunk of data from the endpoint FIFO and a write it to the
  323. * transfer request buffer. If the transfer request completes, 1 is returned,
  324. * otherwise 0 is returned.
  325. */
  326. static int s3c_hsudc_read_fifo(struct s3c_hsudc_ep *hsep,
  327. struct s3c_hsudc_req *hsreq)
  328. {
  329. struct s3c_hsudc *hsudc = hsep->dev;
  330. u32 csr, offset;
  331. u16 *buf, word;
  332. u32 buflen, rcnt, rlen;
  333. void __iomem *fifo = hsep->fifo;
  334. u32 is_short = 0;
  335. offset = (ep_index(hsep)) ? S3C_ESR : S3C_EP0SR;
  336. csr = readl(hsudc->regs + offset);
  337. if (!(csr & S3C_ESR_RX_SUCCESS))
  338. return -EINVAL;
  339. buf = hsreq->req.buf + hsreq->req.actual;
  340. prefetchw(buf);
  341. buflen = hsreq->req.length - hsreq->req.actual;
  342. rcnt = readl(hsudc->regs + S3C_BRCR);
  343. rlen = (csr & S3C_ESR_LWO) ? (rcnt * 2 - 1) : (rcnt * 2);
  344. hsreq->req.actual += min(rlen, buflen);
  345. is_short = (rlen < hsep->ep.maxpacket);
  346. while (rcnt-- != 0) {
  347. word = (u16)readl(fifo);
  348. if (buflen) {
  349. *buf++ = word;
  350. buflen--;
  351. } else {
  352. hsreq->req.status = -EOVERFLOW;
  353. }
  354. }
  355. writel(S3C_ESR_RX_SUCCESS, hsudc->regs + offset);
  356. if (is_short || hsreq->req.actual == hsreq->req.length) {
  357. s3c_hsudc_complete_request(hsep, hsreq, 0);
  358. return 1;
  359. }
  360. return 0;
  361. }
  362. /**
  363. * s3c_hsudc_epin_intr - Handle in-endpoint interrupt.
  364. * @hsudc - Device controller for which the interrupt is to be handled.
  365. * @ep_idx - Endpoint number on which an interrupt is pending.
  366. *
  367. * Handles interrupt for a in-endpoint. The interrupts that are handled are
  368. * stall and data transmit complete interrupt.
  369. */
  370. static void s3c_hsudc_epin_intr(struct s3c_hsudc *hsudc, u32 ep_idx)
  371. {
  372. struct s3c_hsudc_ep *hsep = &hsudc->ep[ep_idx];
  373. struct s3c_hsudc_req *hsreq;
  374. u32 csr;
  375. csr = readl((u32)hsudc->regs + S3C_ESR);
  376. if (csr & S3C_ESR_STALL) {
  377. writel(S3C_ESR_STALL, hsudc->regs + S3C_ESR);
  378. return;
  379. }
  380. if (csr & S3C_ESR_TX_SUCCESS) {
  381. writel(S3C_ESR_TX_SUCCESS, hsudc->regs + S3C_ESR);
  382. if (list_empty(&hsep->queue))
  383. return;
  384. hsreq = list_entry(hsep->queue.next,
  385. struct s3c_hsudc_req, queue);
  386. if ((s3c_hsudc_write_fifo(hsep, hsreq) == 0) &&
  387. (csr & S3C_ESR_PSIF_TWO))
  388. s3c_hsudc_write_fifo(hsep, hsreq);
  389. }
  390. }
  391. /**
  392. * s3c_hsudc_epout_intr - Handle out-endpoint interrupt.
  393. * @hsudc - Device controller for which the interrupt is to be handled.
  394. * @ep_idx - Endpoint number on which an interrupt is pending.
  395. *
  396. * Handles interrupt for a out-endpoint. The interrupts that are handled are
  397. * stall, flush and data ready interrupt.
  398. */
  399. static void s3c_hsudc_epout_intr(struct s3c_hsudc *hsudc, u32 ep_idx)
  400. {
  401. struct s3c_hsudc_ep *hsep = &hsudc->ep[ep_idx];
  402. struct s3c_hsudc_req *hsreq;
  403. u32 csr;
  404. csr = readl((u32)hsudc->regs + S3C_ESR);
  405. if (csr & S3C_ESR_STALL) {
  406. writel(S3C_ESR_STALL, hsudc->regs + S3C_ESR);
  407. return;
  408. }
  409. if (csr & S3C_ESR_FLUSH) {
  410. __orr32(hsudc->regs + S3C_ECR, S3C_ECR_FLUSH);
  411. return;
  412. }
  413. if (csr & S3C_ESR_RX_SUCCESS) {
  414. if (list_empty(&hsep->queue))
  415. return;
  416. hsreq = list_entry(hsep->queue.next,
  417. struct s3c_hsudc_req, queue);
  418. if (((s3c_hsudc_read_fifo(hsep, hsreq)) == 0) &&
  419. (csr & S3C_ESR_PSIF_TWO))
  420. s3c_hsudc_read_fifo(hsep, hsreq);
  421. }
  422. }
  423. /** s3c_hsudc_set_halt - Set or clear a endpoint halt.
  424. * @_ep: Endpoint on which halt has to be set or cleared.
  425. * @value: 1 for setting halt on endpoint, 0 to clear halt.
  426. *
  427. * Set or clear endpoint halt. If halt is set, the endpoint is stopped.
  428. * If halt is cleared, for in-endpoints, if there are any pending
  429. * transfer requests, transfers are started.
  430. */
  431. static int s3c_hsudc_set_halt(struct usb_ep *_ep, int value)
  432. {
  433. struct s3c_hsudc_ep *hsep = our_ep(_ep);
  434. struct s3c_hsudc *hsudc = hsep->dev;
  435. struct s3c_hsudc_req *hsreq;
  436. unsigned long irqflags;
  437. u32 ecr;
  438. u32 offset;
  439. if (value && ep_is_in(hsep) && !list_empty(&hsep->queue))
  440. return -EAGAIN;
  441. spin_lock_irqsave(&hsudc->lock, irqflags);
  442. set_index(hsudc, ep_index(hsep));
  443. offset = (ep_index(hsep)) ? S3C_ECR : S3C_EP0CR;
  444. ecr = readl(hsudc->regs + offset);
  445. if (value) {
  446. ecr |= S3C_ECR_STALL;
  447. if (ep_index(hsep))
  448. ecr |= S3C_ECR_FLUSH;
  449. hsep->stopped = 1;
  450. } else {
  451. ecr &= ~S3C_ECR_STALL;
  452. hsep->stopped = hsep->wedge = 0;
  453. }
  454. writel(ecr, hsudc->regs + offset);
  455. if (ep_is_in(hsep) && !list_empty(&hsep->queue) && !value) {
  456. hsreq = list_entry(hsep->queue.next,
  457. struct s3c_hsudc_req, queue);
  458. if (hsreq)
  459. s3c_hsudc_write_fifo(hsep, hsreq);
  460. }
  461. spin_unlock_irqrestore(&hsudc->lock, irqflags);
  462. return 0;
  463. }
  464. /** s3c_hsudc_set_wedge - Sets the halt feature with the clear requests ignored
  465. * @_ep: Endpoint on which wedge has to be set.
  466. *
  467. * Sets the halt feature with the clear requests ignored.
  468. */
  469. static int s3c_hsudc_set_wedge(struct usb_ep *_ep)
  470. {
  471. struct s3c_hsudc_ep *hsep = our_ep(_ep);
  472. if (!hsep)
  473. return -EINVAL;
  474. hsep->wedge = 1;
  475. return usb_ep_set_halt(_ep);
  476. }
  477. /** s3c_hsudc_handle_reqfeat - Handle set feature or clear feature requests.
  478. * @_ep: Device controller on which the set/clear feature needs to be handled.
  479. * @ctrl: Control request as received on the endpoint 0.
  480. *
  481. * Handle set feature or clear feature control requests on the control endpoint.
  482. */
  483. static int s3c_hsudc_handle_reqfeat(struct s3c_hsudc *hsudc,
  484. struct usb_ctrlrequest *ctrl)
  485. {
  486. struct s3c_hsudc_ep *hsep;
  487. bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
  488. u8 ep_num = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK;
  489. if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
  490. hsep = &hsudc->ep[ep_num];
  491. switch (le16_to_cpu(ctrl->wValue)) {
  492. case USB_ENDPOINT_HALT:
  493. if (set || (!set && !hsep->wedge))
  494. s3c_hsudc_set_halt(&hsep->ep, set);
  495. return 0;
  496. }
  497. }
  498. return -ENOENT;
  499. }
  500. /**
  501. * s3c_hsudc_process_req_status - Handle get status control request.
  502. * @hsudc: Device controller on which get status request has be handled.
  503. * @ctrl: Control request as received on the endpoint 0.
  504. *
  505. * Handle get status control request received on control endpoint.
  506. */
  507. static void s3c_hsudc_process_req_status(struct s3c_hsudc *hsudc,
  508. struct usb_ctrlrequest *ctrl)
  509. {
  510. struct s3c_hsudc_ep *hsep0 = &hsudc->ep[0];
  511. struct s3c_hsudc_req hsreq;
  512. struct s3c_hsudc_ep *hsep;
  513. __le16 reply;
  514. u8 epnum;
  515. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  516. case USB_RECIP_DEVICE:
  517. reply = cpu_to_le16(0);
  518. break;
  519. case USB_RECIP_INTERFACE:
  520. reply = cpu_to_le16(0);
  521. break;
  522. case USB_RECIP_ENDPOINT:
  523. epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  524. hsep = &hsudc->ep[epnum];
  525. reply = cpu_to_le16(hsep->stopped ? 1 : 0);
  526. break;
  527. }
  528. INIT_LIST_HEAD(&hsreq.queue);
  529. hsreq.req.length = 2;
  530. hsreq.req.buf = &reply;
  531. hsreq.req.actual = 0;
  532. hsreq.req.complete = NULL;
  533. s3c_hsudc_write_fifo(hsep0, &hsreq);
  534. }
  535. /**
  536. * s3c_hsudc_process_setup - Process control request received on endpoint 0.
  537. * @hsudc: Device controller on which control request has been received.
  538. *
  539. * Read the control request received on endpoint 0, decode it and handle
  540. * the request.
  541. */
  542. static void s3c_hsudc_process_setup(struct s3c_hsudc *hsudc)
  543. {
  544. struct s3c_hsudc_ep *hsep = &hsudc->ep[0];
  545. struct usb_ctrlrequest ctrl = {0};
  546. int ret;
  547. s3c_hsudc_nuke_ep(hsep, -EPROTO);
  548. s3c_hsudc_read_setup_pkt(hsudc, (u16 *)&ctrl);
  549. if (ctrl.bRequestType & USB_DIR_IN) {
  550. hsep->bEndpointAddress |= USB_DIR_IN;
  551. hsudc->ep0state = DATA_STATE_XMIT;
  552. } else {
  553. hsep->bEndpointAddress &= ~USB_DIR_IN;
  554. hsudc->ep0state = DATA_STATE_RECV;
  555. }
  556. switch (ctrl.bRequest) {
  557. case USB_REQ_SET_ADDRESS:
  558. if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
  559. break;
  560. hsudc->ep0state = WAIT_FOR_SETUP;
  561. return;
  562. case USB_REQ_GET_STATUS:
  563. if ((ctrl.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
  564. break;
  565. s3c_hsudc_process_req_status(hsudc, &ctrl);
  566. return;
  567. case USB_REQ_SET_FEATURE:
  568. case USB_REQ_CLEAR_FEATURE:
  569. if ((ctrl.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
  570. break;
  571. s3c_hsudc_handle_reqfeat(hsudc, &ctrl);
  572. hsudc->ep0state = WAIT_FOR_SETUP;
  573. return;
  574. }
  575. if (hsudc->driver) {
  576. spin_unlock(&hsudc->lock);
  577. ret = hsudc->driver->setup(&hsudc->gadget, &ctrl);
  578. spin_lock(&hsudc->lock);
  579. if (ctrl.bRequest == USB_REQ_SET_CONFIGURATION) {
  580. hsep->bEndpointAddress &= ~USB_DIR_IN;
  581. hsudc->ep0state = WAIT_FOR_SETUP;
  582. }
  583. if (ret < 0) {
  584. dev_err(hsudc->dev, "setup failed, returned %d\n",
  585. ret);
  586. s3c_hsudc_set_halt(&hsep->ep, 1);
  587. hsudc->ep0state = WAIT_FOR_SETUP;
  588. hsep->bEndpointAddress &= ~USB_DIR_IN;
  589. }
  590. }
  591. }
  592. /** s3c_hsudc_handle_ep0_intr - Handle endpoint 0 interrupt.
  593. * @hsudc: Device controller on which endpoint 0 interrupt has occured.
  594. *
  595. * Handle endpoint 0 interrupt when it occurs. EP0 interrupt could occur
  596. * when a stall handshake is sent to host or data is sent/received on
  597. * endpoint 0.
  598. */
  599. static void s3c_hsudc_handle_ep0_intr(struct s3c_hsudc *hsudc)
  600. {
  601. struct s3c_hsudc_ep *hsep = &hsudc->ep[0];
  602. struct s3c_hsudc_req *hsreq;
  603. u32 csr = readl(hsudc->regs + S3C_EP0SR);
  604. u32 ecr;
  605. if (csr & S3C_EP0SR_STALL) {
  606. ecr = readl(hsudc->regs + S3C_EP0CR);
  607. ecr &= ~(S3C_ECR_STALL | S3C_ECR_FLUSH);
  608. writel(ecr, hsudc->regs + S3C_EP0CR);
  609. writel(S3C_EP0SR_STALL, hsudc->regs + S3C_EP0SR);
  610. hsep->stopped = 0;
  611. s3c_hsudc_nuke_ep(hsep, -ECONNABORTED);
  612. hsudc->ep0state = WAIT_FOR_SETUP;
  613. hsep->bEndpointAddress &= ~USB_DIR_IN;
  614. return;
  615. }
  616. if (csr & S3C_EP0SR_TX_SUCCESS) {
  617. writel(S3C_EP0SR_TX_SUCCESS, hsudc->regs + S3C_EP0SR);
  618. if (ep_is_in(hsep)) {
  619. if (list_empty(&hsep->queue))
  620. return;
  621. hsreq = list_entry(hsep->queue.next,
  622. struct s3c_hsudc_req, queue);
  623. s3c_hsudc_write_fifo(hsep, hsreq);
  624. }
  625. }
  626. if (csr & S3C_EP0SR_RX_SUCCESS) {
  627. if (hsudc->ep0state == WAIT_FOR_SETUP)
  628. s3c_hsudc_process_setup(hsudc);
  629. else {
  630. if (!ep_is_in(hsep)) {
  631. if (list_empty(&hsep->queue))
  632. return;
  633. hsreq = list_entry(hsep->queue.next,
  634. struct s3c_hsudc_req, queue);
  635. s3c_hsudc_read_fifo(hsep, hsreq);
  636. }
  637. }
  638. }
  639. }
  640. /**
  641. * s3c_hsudc_ep_enable - Enable a endpoint.
  642. * @_ep: The endpoint to be enabled.
  643. * @desc: Endpoint descriptor.
  644. *
  645. * Enables a endpoint when called from the gadget driver. Endpoint stall if
  646. * any is cleared, transfer type is configured and endpoint interrupt is
  647. * enabled.
  648. */
  649. static int s3c_hsudc_ep_enable(struct usb_ep *_ep,
  650. const struct usb_endpoint_descriptor *desc)
  651. {
  652. struct s3c_hsudc_ep *hsep;
  653. struct s3c_hsudc *hsudc;
  654. unsigned long flags;
  655. u32 ecr = 0;
  656. hsep = container_of(_ep, struct s3c_hsudc_ep, ep);
  657. if (!_ep || !desc || hsep->desc || _ep->name == ep0name
  658. || desc->bDescriptorType != USB_DT_ENDPOINT
  659. || hsep->bEndpointAddress != desc->bEndpointAddress
  660. || ep_maxpacket(hsep) < le16_to_cpu(desc->wMaxPacketSize))
  661. return -EINVAL;
  662. if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
  663. && le16_to_cpu(desc->wMaxPacketSize) != ep_maxpacket(hsep))
  664. || !desc->wMaxPacketSize)
  665. return -ERANGE;
  666. hsudc = hsep->dev;
  667. if (!hsudc->driver || hsudc->gadget.speed == USB_SPEED_UNKNOWN)
  668. return -ESHUTDOWN;
  669. spin_lock_irqsave(&hsudc->lock, flags);
  670. set_index(hsudc, hsep->bEndpointAddress);
  671. ecr |= ((usb_endpoint_xfer_int(desc)) ? S3C_ECR_IEMS : S3C_ECR_DUEN);
  672. writel(ecr, hsudc->regs + S3C_ECR);
  673. hsep->stopped = hsep->wedge = 0;
  674. hsep->desc = desc;
  675. hsep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
  676. s3c_hsudc_set_halt(_ep, 0);
  677. __set_bit(ep_index(hsep), hsudc->regs + S3C_EIER);
  678. spin_unlock_irqrestore(&hsudc->lock, flags);
  679. return 0;
  680. }
  681. /**
  682. * s3c_hsudc_ep_disable - Disable a endpoint.
  683. * @_ep: The endpoint to be disabled.
  684. * @desc: Endpoint descriptor.
  685. *
  686. * Disables a endpoint when called from the gadget driver.
  687. */
  688. static int s3c_hsudc_ep_disable(struct usb_ep *_ep)
  689. {
  690. struct s3c_hsudc_ep *hsep = our_ep(_ep);
  691. struct s3c_hsudc *hsudc = hsep->dev;
  692. unsigned long flags;
  693. if (!_ep || !hsep->desc)
  694. return -EINVAL;
  695. spin_lock_irqsave(&hsudc->lock, flags);
  696. set_index(hsudc, hsep->bEndpointAddress);
  697. __clear_bit(ep_index(hsep), hsudc->regs + S3C_EIER);
  698. s3c_hsudc_nuke_ep(hsep, -ESHUTDOWN);
  699. hsep->desc = 0;
  700. hsep->stopped = 1;
  701. spin_unlock_irqrestore(&hsudc->lock, flags);
  702. return 0;
  703. }
  704. /**
  705. * s3c_hsudc_alloc_request - Allocate a new request.
  706. * @_ep: Endpoint for which request is allocated (not used).
  707. * @gfp_flags: Flags used for the allocation.
  708. *
  709. * Allocates a single transfer request structure when called from gadget driver.
  710. */
  711. static struct usb_request *s3c_hsudc_alloc_request(struct usb_ep *_ep,
  712. gfp_t gfp_flags)
  713. {
  714. struct s3c_hsudc_req *hsreq;
  715. hsreq = kzalloc(sizeof *hsreq, gfp_flags);
  716. if (!hsreq)
  717. return 0;
  718. INIT_LIST_HEAD(&hsreq->queue);
  719. return &hsreq->req;
  720. }
  721. /**
  722. * s3c_hsudc_free_request - Deallocate a request.
  723. * @ep: Endpoint for which request is deallocated (not used).
  724. * @_req: Request to be deallocated.
  725. *
  726. * Allocates a single transfer request structure when called from gadget driver.
  727. */
  728. static void s3c_hsudc_free_request(struct usb_ep *ep, struct usb_request *_req)
  729. {
  730. struct s3c_hsudc_req *hsreq;
  731. hsreq = container_of(_req, struct s3c_hsudc_req, req);
  732. WARN_ON(!list_empty(&hsreq->queue));
  733. kfree(hsreq);
  734. }
  735. /**
  736. * s3c_hsudc_queue - Queue a transfer request for the endpoint.
  737. * @_ep: Endpoint for which the request is queued.
  738. * @_req: Request to be queued.
  739. * @gfp_flags: Not used.
  740. *
  741. * Start or enqueue a request for a endpoint when called from gadget driver.
  742. */
  743. static int s3c_hsudc_queue(struct usb_ep *_ep, struct usb_request *_req,
  744. gfp_t gfp_flags)
  745. {
  746. struct s3c_hsudc_req *hsreq;
  747. struct s3c_hsudc_ep *hsep;
  748. struct s3c_hsudc *hsudc;
  749. unsigned long flags;
  750. u32 offset;
  751. u32 csr;
  752. hsreq = container_of(_req, struct s3c_hsudc_req, req);
  753. if ((!_req || !_req->complete || !_req->buf ||
  754. !list_empty(&hsreq->queue)))
  755. return -EINVAL;
  756. hsep = container_of(_ep, struct s3c_hsudc_ep, ep);
  757. hsudc = hsep->dev;
  758. if (!hsudc->driver || hsudc->gadget.speed == USB_SPEED_UNKNOWN)
  759. return -ESHUTDOWN;
  760. spin_lock_irqsave(&hsudc->lock, flags);
  761. set_index(hsudc, hsep->bEndpointAddress);
  762. _req->status = -EINPROGRESS;
  763. _req->actual = 0;
  764. if (!ep_index(hsep) && _req->length == 0) {
  765. hsudc->ep0state = WAIT_FOR_SETUP;
  766. s3c_hsudc_complete_request(hsep, hsreq, 0);
  767. spin_unlock_irqrestore(&hsudc->lock, flags);
  768. return 0;
  769. }
  770. if (list_empty(&hsep->queue) && !hsep->stopped) {
  771. offset = (ep_index(hsep)) ? S3C_ESR : S3C_EP0SR;
  772. if (ep_is_in(hsep)) {
  773. csr = readl((u32)hsudc->regs + offset);
  774. if (!(csr & S3C_ESR_TX_SUCCESS) &&
  775. (s3c_hsudc_write_fifo(hsep, hsreq) == 1))
  776. hsreq = 0;
  777. } else {
  778. csr = readl((u32)hsudc->regs + offset);
  779. if ((csr & S3C_ESR_RX_SUCCESS)
  780. && (s3c_hsudc_read_fifo(hsep, hsreq) == 1))
  781. hsreq = 0;
  782. }
  783. }
  784. if (hsreq != 0)
  785. list_add_tail(&hsreq->queue, &hsep->queue);
  786. spin_unlock_irqrestore(&hsudc->lock, flags);
  787. return 0;
  788. }
  789. /**
  790. * s3c_hsudc_dequeue - Dequeue a transfer request from an endpoint.
  791. * @_ep: Endpoint from which the request is dequeued.
  792. * @_req: Request to be dequeued.
  793. *
  794. * Dequeue a request from a endpoint when called from gadget driver.
  795. */
  796. static int s3c_hsudc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  797. {
  798. struct s3c_hsudc_ep *hsep = our_ep(_ep);
  799. struct s3c_hsudc *hsudc = hsep->dev;
  800. struct s3c_hsudc_req *hsreq;
  801. unsigned long flags;
  802. hsep = container_of(_ep, struct s3c_hsudc_ep, ep);
  803. if (!_ep || hsep->ep.name == ep0name)
  804. return -EINVAL;
  805. spin_lock_irqsave(&hsudc->lock, flags);
  806. list_for_each_entry(hsreq, &hsep->queue, queue) {
  807. if (&hsreq->req == _req)
  808. break;
  809. }
  810. if (&hsreq->req != _req) {
  811. spin_unlock_irqrestore(&hsudc->lock, flags);
  812. return -EINVAL;
  813. }
  814. set_index(hsudc, hsep->bEndpointAddress);
  815. s3c_hsudc_complete_request(hsep, hsreq, -ECONNRESET);
  816. spin_unlock_irqrestore(&hsudc->lock, flags);
  817. return 0;
  818. }
  819. static struct usb_ep_ops s3c_hsudc_ep_ops = {
  820. .enable = s3c_hsudc_ep_enable,
  821. .disable = s3c_hsudc_ep_disable,
  822. .alloc_request = s3c_hsudc_alloc_request,
  823. .free_request = s3c_hsudc_free_request,
  824. .queue = s3c_hsudc_queue,
  825. .dequeue = s3c_hsudc_dequeue,
  826. .set_halt = s3c_hsudc_set_halt,
  827. .set_wedge = s3c_hsudc_set_wedge,
  828. };
  829. /**
  830. * s3c_hsudc_initep - Initialize a endpoint to default state.
  831. * @hsudc - Reference to the device controller.
  832. * @hsep - Endpoint to be initialized.
  833. * @epnum - Address to be assigned to the endpoint.
  834. *
  835. * Initialize a endpoint with default configuration.
  836. */
  837. static void s3c_hsudc_initep(struct s3c_hsudc *hsudc,
  838. struct s3c_hsudc_ep *hsep, int epnum)
  839. {
  840. char *dir;
  841. if ((epnum % 2) == 0) {
  842. dir = "out";
  843. } else {
  844. dir = "in";
  845. hsep->bEndpointAddress = USB_DIR_IN;
  846. }
  847. hsep->bEndpointAddress |= epnum;
  848. if (epnum)
  849. snprintf(hsep->name, sizeof(hsep->name), "ep%d%s", epnum, dir);
  850. else
  851. snprintf(hsep->name, sizeof(hsep->name), "%s", ep0name);
  852. INIT_LIST_HEAD(&hsep->queue);
  853. INIT_LIST_HEAD(&hsep->ep.ep_list);
  854. if (epnum)
  855. list_add_tail(&hsep->ep.ep_list, &hsudc->gadget.ep_list);
  856. hsep->dev = hsudc;
  857. hsep->ep.name = hsep->name;
  858. hsep->ep.maxpacket = epnum ? 512 : 64;
  859. hsep->ep.ops = &s3c_hsudc_ep_ops;
  860. hsep->fifo = hsudc->regs + S3C_BR(epnum);
  861. hsep->desc = 0;
  862. hsep->stopped = 0;
  863. hsep->wedge = 0;
  864. set_index(hsudc, epnum);
  865. writel(hsep->ep.maxpacket, hsudc->regs + S3C_MPR);
  866. }
  867. /**
  868. * s3c_hsudc_setup_ep - Configure all endpoints to default state.
  869. * @hsudc: Reference to device controller.
  870. *
  871. * Configures all endpoints to default state.
  872. */
  873. static void s3c_hsudc_setup_ep(struct s3c_hsudc *hsudc)
  874. {
  875. int epnum;
  876. hsudc->ep0state = WAIT_FOR_SETUP;
  877. INIT_LIST_HEAD(&hsudc->gadget.ep_list);
  878. for (epnum = 0; epnum < hsudc->pd->epnum; epnum++)
  879. s3c_hsudc_initep(hsudc, &hsudc->ep[epnum], epnum);
  880. }
  881. /**
  882. * s3c_hsudc_reconfig - Reconfigure the device controller to default state.
  883. * @hsudc: Reference to device controller.
  884. *
  885. * Reconfigures the device controller registers to a default state.
  886. */
  887. static void s3c_hsudc_reconfig(struct s3c_hsudc *hsudc)
  888. {
  889. writel(0xAA, hsudc->regs + S3C_EDR);
  890. writel(1, hsudc->regs + S3C_EIER);
  891. writel(0, hsudc->regs + S3C_TR);
  892. writel(S3C_SCR_DTZIEN_EN | S3C_SCR_RRD_EN | S3C_SCR_SUS_EN |
  893. S3C_SCR_RST_EN, hsudc->regs + S3C_SCR);
  894. writel(0, hsudc->regs + S3C_EP0CR);
  895. s3c_hsudc_setup_ep(hsudc);
  896. }
  897. /**
  898. * s3c_hsudc_irq - Interrupt handler for device controller.
  899. * @irq: Not used.
  900. * @_dev: Reference to the device controller.
  901. *
  902. * Interrupt handler for the device controller. This handler handles controller
  903. * interrupts and endpoint interrupts.
  904. */
  905. static irqreturn_t s3c_hsudc_irq(int irq, void *_dev)
  906. {
  907. struct s3c_hsudc *hsudc = _dev;
  908. struct s3c_hsudc_ep *hsep;
  909. u32 ep_intr;
  910. u32 sys_status;
  911. u32 ep_idx;
  912. spin_lock(&hsudc->lock);
  913. sys_status = readl(hsudc->regs + S3C_SSR);
  914. ep_intr = readl(hsudc->regs + S3C_EIR) & 0x3FF;
  915. if (!ep_intr && !(sys_status & S3C_SSR_DTZIEN_EN)) {
  916. spin_unlock(&hsudc->lock);
  917. return IRQ_HANDLED;
  918. }
  919. if (sys_status) {
  920. if (sys_status & S3C_SSR_VBUSON)
  921. writel(S3C_SSR_VBUSON, hsudc->regs + S3C_SSR);
  922. if (sys_status & S3C_SSR_ERR)
  923. writel(S3C_SSR_ERR, hsudc->regs + S3C_SSR);
  924. if (sys_status & S3C_SSR_SDE) {
  925. writel(S3C_SSR_SDE, hsudc->regs + S3C_SSR);
  926. hsudc->gadget.speed = (sys_status & S3C_SSR_HSP) ?
  927. USB_SPEED_HIGH : USB_SPEED_FULL;
  928. }
  929. if (sys_status & S3C_SSR_SUSPEND) {
  930. writel(S3C_SSR_SUSPEND, hsudc->regs + S3C_SSR);
  931. if (hsudc->gadget.speed != USB_SPEED_UNKNOWN
  932. && hsudc->driver && hsudc->driver->suspend)
  933. hsudc->driver->suspend(&hsudc->gadget);
  934. }
  935. if (sys_status & S3C_SSR_RESUME) {
  936. writel(S3C_SSR_RESUME, hsudc->regs + S3C_SSR);
  937. if (hsudc->gadget.speed != USB_SPEED_UNKNOWN
  938. && hsudc->driver && hsudc->driver->resume)
  939. hsudc->driver->resume(&hsudc->gadget);
  940. }
  941. if (sys_status & S3C_SSR_RESET) {
  942. writel(S3C_SSR_RESET, hsudc->regs + S3C_SSR);
  943. for (ep_idx = 0; ep_idx < hsudc->pd->epnum; ep_idx++) {
  944. hsep = &hsudc->ep[ep_idx];
  945. hsep->stopped = 1;
  946. s3c_hsudc_nuke_ep(hsep, -ECONNRESET);
  947. }
  948. s3c_hsudc_reconfig(hsudc);
  949. hsudc->ep0state = WAIT_FOR_SETUP;
  950. }
  951. }
  952. if (ep_intr & S3C_EIR_EP0) {
  953. writel(S3C_EIR_EP0, hsudc->regs + S3C_EIR);
  954. set_index(hsudc, 0);
  955. s3c_hsudc_handle_ep0_intr(hsudc);
  956. }
  957. ep_intr >>= 1;
  958. ep_idx = 1;
  959. while (ep_intr) {
  960. if (ep_intr & 1) {
  961. hsep = &hsudc->ep[ep_idx];
  962. set_index(hsudc, ep_idx);
  963. writel(1 << ep_idx, hsudc->regs + S3C_EIR);
  964. if (ep_is_in(hsep))
  965. s3c_hsudc_epin_intr(hsudc, ep_idx);
  966. else
  967. s3c_hsudc_epout_intr(hsudc, ep_idx);
  968. }
  969. ep_intr >>= 1;
  970. ep_idx++;
  971. }
  972. spin_unlock(&hsudc->lock);
  973. return IRQ_HANDLED;
  974. }
  975. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  976. int (*bind)(struct usb_gadget *))
  977. {
  978. struct s3c_hsudc *hsudc = the_controller;
  979. int ret;
  980. if (!driver
  981. || (driver->speed != USB_SPEED_FULL &&
  982. driver->speed != USB_SPEED_HIGH)
  983. || !bind
  984. || !driver->unbind || !driver->disconnect || !driver->setup)
  985. return -EINVAL;
  986. if (!hsudc)
  987. return -ENODEV;
  988. if (hsudc->driver)
  989. return -EBUSY;
  990. hsudc->driver = driver;
  991. hsudc->gadget.dev.driver = &driver->driver;
  992. hsudc->gadget.speed = USB_SPEED_UNKNOWN;
  993. ret = device_add(&hsudc->gadget.dev);
  994. if (ret) {
  995. dev_err(hsudc->dev, "failed to probe gadget device");
  996. return ret;
  997. }
  998. ret = bind(&hsudc->gadget);
  999. if (ret) {
  1000. dev_err(hsudc->dev, "%s: bind failed\n", hsudc->gadget.name);
  1001. device_del(&hsudc->gadget.dev);
  1002. hsudc->driver = NULL;
  1003. hsudc->gadget.dev.driver = NULL;
  1004. return ret;
  1005. }
  1006. enable_irq(hsudc->irq);
  1007. dev_info(hsudc->dev, "bound driver %s\n", driver->driver.name);
  1008. s3c_hsudc_reconfig(hsudc);
  1009. s3c_hsudc_init_phy();
  1010. if (hsudc->pd->gpio_init)
  1011. hsudc->pd->gpio_init();
  1012. return 0;
  1013. }
  1014. EXPORT_SYMBOL(usb_gadget_probe_driver);
  1015. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1016. {
  1017. struct s3c_hsudc *hsudc = the_controller;
  1018. unsigned long flags;
  1019. if (!hsudc)
  1020. return -ENODEV;
  1021. if (!driver || driver != hsudc->driver || !driver->unbind)
  1022. return -EINVAL;
  1023. spin_lock_irqsave(&hsudc->lock, flags);
  1024. hsudc->driver = 0;
  1025. s3c_hsudc_uninit_phy();
  1026. if (hsudc->pd->gpio_uninit)
  1027. hsudc->pd->gpio_uninit();
  1028. s3c_hsudc_stop_activity(hsudc, driver);
  1029. spin_unlock_irqrestore(&hsudc->lock, flags);
  1030. driver->unbind(&hsudc->gadget);
  1031. device_del(&hsudc->gadget.dev);
  1032. disable_irq(hsudc->irq);
  1033. dev_info(hsudc->dev, "unregistered gadget driver '%s'\n",
  1034. driver->driver.name);
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1038. static inline u32 s3c_hsudc_read_frameno(struct s3c_hsudc *hsudc)
  1039. {
  1040. return readl(hsudc->regs + S3C_FNR) & 0x3FF;
  1041. }
  1042. static int s3c_hsudc_gadget_getframe(struct usb_gadget *gadget)
  1043. {
  1044. return s3c_hsudc_read_frameno(to_hsudc(gadget));
  1045. }
  1046. static struct usb_gadget_ops s3c_hsudc_gadget_ops = {
  1047. .get_frame = s3c_hsudc_gadget_getframe,
  1048. };
  1049. static int s3c_hsudc_probe(struct platform_device *pdev)
  1050. {
  1051. struct device *dev = &pdev->dev;
  1052. struct resource *res;
  1053. struct s3c_hsudc *hsudc;
  1054. struct s3c24xx_hsudc_platdata *pd = pdev->dev.platform_data;
  1055. int ret;
  1056. hsudc = kzalloc(sizeof(struct s3c_hsudc) +
  1057. sizeof(struct s3c_hsudc_ep) * pd->epnum,
  1058. GFP_KERNEL);
  1059. if (!hsudc) {
  1060. dev_err(dev, "cannot allocate memory\n");
  1061. return -ENOMEM;
  1062. }
  1063. the_controller = hsudc;
  1064. platform_set_drvdata(pdev, dev);
  1065. hsudc->dev = dev;
  1066. hsudc->pd = pdev->dev.platform_data;
  1067. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1068. if (!res) {
  1069. dev_err(dev, "unable to obtain driver resource data\n");
  1070. ret = -ENODEV;
  1071. goto err_res;
  1072. }
  1073. hsudc->mem_rsrc = request_mem_region(res->start, resource_size(res),
  1074. dev_name(&pdev->dev));
  1075. if (!hsudc->mem_rsrc) {
  1076. dev_err(dev, "failed to reserve register area\n");
  1077. ret = -ENODEV;
  1078. goto err_res;
  1079. }
  1080. hsudc->regs = ioremap(res->start, resource_size(res));
  1081. if (!hsudc->regs) {
  1082. dev_err(dev, "error mapping device register area\n");
  1083. ret = -EBUSY;
  1084. goto err_remap;
  1085. }
  1086. ret = platform_get_irq(pdev, 0);
  1087. if (ret < 0) {
  1088. dev_err(dev, "unable to obtain IRQ number\n");
  1089. goto err_irq;
  1090. }
  1091. hsudc->irq = ret;
  1092. ret = request_irq(hsudc->irq, s3c_hsudc_irq, 0, driver_name, hsudc);
  1093. if (ret < 0) {
  1094. dev_err(dev, "irq request failed\n");
  1095. goto err_irq;
  1096. }
  1097. spin_lock_init(&hsudc->lock);
  1098. device_initialize(&hsudc->gadget.dev);
  1099. dev_set_name(&hsudc->gadget.dev, "gadget");
  1100. hsudc->gadget.is_dualspeed = 1;
  1101. hsudc->gadget.ops = &s3c_hsudc_gadget_ops;
  1102. hsudc->gadget.name = dev_name(dev);
  1103. hsudc->gadget.dev.parent = dev;
  1104. hsudc->gadget.dev.dma_mask = dev->dma_mask;
  1105. hsudc->gadget.ep0 = &hsudc->ep[0].ep;
  1106. hsudc->gadget.is_otg = 0;
  1107. hsudc->gadget.is_a_peripheral = 0;
  1108. s3c_hsudc_setup_ep(hsudc);
  1109. hsudc->uclk = clk_get(&pdev->dev, "usb-device");
  1110. if (IS_ERR(hsudc->uclk)) {
  1111. dev_err(dev, "failed to find usb-device clock source\n");
  1112. return PTR_ERR(hsudc->uclk);
  1113. }
  1114. clk_enable(hsudc->uclk);
  1115. local_irq_disable();
  1116. disable_irq(hsudc->irq);
  1117. local_irq_enable();
  1118. return 0;
  1119. err_irq:
  1120. iounmap(hsudc->regs);
  1121. err_remap:
  1122. release_resource(hsudc->mem_rsrc);
  1123. kfree(hsudc->mem_rsrc);
  1124. err_res:
  1125. kfree(hsudc);
  1126. return ret;
  1127. }
  1128. static struct platform_driver s3c_hsudc_driver = {
  1129. .driver = {
  1130. .owner = THIS_MODULE,
  1131. .name = "s3c-hsudc",
  1132. },
  1133. .probe = s3c_hsudc_probe,
  1134. };
  1135. static int __init s3c_hsudc_modinit(void)
  1136. {
  1137. return platform_driver_register(&s3c_hsudc_driver);
  1138. }
  1139. static void __exit s3c_hsudc_modexit(void)
  1140. {
  1141. platform_driver_unregister(&s3c_hsudc_driver);
  1142. }
  1143. module_init(s3c_hsudc_modinit);
  1144. module_exit(s3c_hsudc_modexit);
  1145. MODULE_DESCRIPTION("Samsung S3C24XX USB high-speed controller driver");
  1146. MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
  1147. MODULE_LICENSE("GPL");