s3c-hsudc.c 36 KB

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