s3c-hsudc.c 37 KB

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