s3c-hsudc.c 34 KB

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