s3c-hsudc.c 37 KB

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