s3c-hsudc.c 37 KB

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