s3c-hsudc.c 36 KB

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