s3c-hsudc.c 36 KB

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