s3c-hsudc.c 36 KB

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