s3c-hsudc.c 36 KB

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