pxa27x_udc.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /* $OpenBSD: pxa27x_udc.c,v 1.31 2015/05/15 13:32:08 jsg Exp $ */
  2. /*
  3. * Copyright (c) 2007 Dale Rahn <drahn@openbsd.org>
  4. * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
  5. * Copyright (c) 2005 David Gwynne <dlg@openbsd.org>
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #include <sys/param.h>
  20. #include <sys/systm.h>
  21. #include <sys/device.h>
  22. #include <sys/kernel.h>
  23. #include <sys/malloc.h>
  24. #include <sys/timeout.h>
  25. #include <machine/intr.h>
  26. #include <machine/bus.h>
  27. #include <dev/usb/usb.h>
  28. #include <dev/usb/usbdi.h>
  29. #include <dev/usb/usbdivar.h>
  30. #include <dev/usb/usbf.h>
  31. #include <dev/usb/usbfvar.h>
  32. #include <arm/xscale/pxa2x0reg.h>
  33. #include <arm/xscale/pxa2x0var.h>
  34. #include <arm/xscale/pxa2x0_gpio.h>
  35. #include <arm/xscale/pxa27x_udc.h>
  36. #include "usbf.h"
  37. struct pxaudc_xfer {
  38. struct usbf_xfer xfer;
  39. u_int16_t frmlen;
  40. };
  41. struct pxaudc_pipe {
  42. struct usbf_pipe pipe;
  43. // LIST_ENTRY(pxaudc_pipe) list;
  44. };
  45. void pxaudc_enable(struct pxaudc_softc *);
  46. void pxaudc_disable(struct pxaudc_softc *);
  47. void pxaudc_read_ep0(struct pxaudc_softc *, struct usbf_xfer *);
  48. void pxaudc_read_epN(struct pxaudc_softc *sc, int ep);
  49. void pxaudc_write_ep0(struct pxaudc_softc *, struct usbf_xfer *);
  50. void pxaudc_write(struct pxaudc_softc *, struct usbf_xfer *);
  51. void pxaudc_write_epN(struct pxaudc_softc *sc, int ep);
  52. int pxaudc_connect_intr(void *);
  53. int pxaudc_intr(void *);
  54. void pxaudc_intr1(struct pxaudc_softc *);
  55. void pxaudc_ep0_intr(struct pxaudc_softc *);
  56. void pxaudc_epN_intr(struct pxaudc_softc *sc, int ep, int isr);
  57. usbf_status pxaudc_open(struct usbf_pipe *);
  58. void pxaudc_softintr(void *);
  59. usbf_status pxaudc_allocm(struct usbf_bus *, struct usb_dma *, u_int32_t);
  60. void pxaudc_freem(struct usbf_bus *, struct usb_dma *);
  61. struct usbf_xfer *pxaudc_allocx(struct usbf_bus *);
  62. void pxaudc_freex(struct usbf_bus *, struct usbf_xfer *);
  63. usbf_status pxaudc_ctrl_transfer(struct usbf_xfer *);
  64. usbf_status pxaudc_ctrl_start(struct usbf_xfer *);
  65. void pxaudc_ctrl_abort(struct usbf_xfer *);
  66. void pxaudc_ctrl_done(struct usbf_xfer *);
  67. void pxaudc_ctrl_close(struct usbf_pipe *);
  68. usbf_status pxaudc_bulk_transfer(struct usbf_xfer *);
  69. usbf_status pxaudc_bulk_start(struct usbf_xfer *);
  70. void pxaudc_bulk_abort(struct usbf_xfer *);
  71. void pxaudc_bulk_done(struct usbf_xfer *);
  72. void pxaudc_bulk_close(struct usbf_pipe *);
  73. struct cfdriver pxaudc_cd = {
  74. NULL, "pxaudc", DV_DULL
  75. };
  76. #if NUSBF > 0
  77. struct usbf_bus_methods pxaudc_bus_methods = {
  78. pxaudc_open,
  79. pxaudc_softintr,
  80. pxaudc_allocm,
  81. pxaudc_freem,
  82. pxaudc_allocx,
  83. pxaudc_freex
  84. };
  85. struct usbf_pipe_methods pxaudc_ctrl_methods = {
  86. pxaudc_ctrl_transfer,
  87. pxaudc_ctrl_start,
  88. pxaudc_ctrl_abort,
  89. pxaudc_ctrl_done,
  90. pxaudc_ctrl_close
  91. };
  92. struct usbf_pipe_methods pxaudc_bulk_methods = {
  93. pxaudc_bulk_transfer,
  94. pxaudc_bulk_start,
  95. pxaudc_bulk_abort,
  96. pxaudc_bulk_done,
  97. pxaudc_bulk_close
  98. };
  99. #endif /* NUSBF > 0 */
  100. #define DEVNAME(sc) ((sc)->sc_bus.bdev.dv_xname)
  101. #define CSR_READ_4(sc, reg) \
  102. bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
  103. #define CSR_WRITE_4(sc, reg, val) \
  104. bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
  105. #define CSR_WRITE_1(sc, reg, val) \
  106. bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
  107. #define CSR_SET_4(sc, reg, val) \
  108. CSR_WRITE_4((sc), (reg), CSR_READ_4((sc), (reg)) | (val))
  109. #define CSR_CLR_4(sc, reg, val) \
  110. CSR_WRITE_4((sc), (reg), CSR_READ_4((sc), (reg)) & ~(val))
  111. #ifndef PXAUDC_DEBUG
  112. #define DPRINTF(l, x) do {} while (0)
  113. #else
  114. int pxaudcdebug = 0;
  115. #define DPRINTF(l, x) if ((l) <= pxaudcdebug) printf x; else {}
  116. #endif
  117. int
  118. pxaudc_match(void)
  119. {
  120. if ((cputype & ~CPU_ID_XSCALE_COREREV_MASK) != CPU_ID_PXA27X)
  121. return (0);
  122. return (1);
  123. }
  124. void
  125. pxaudc_attach(struct pxaudc_softc *sc, void *aux)
  126. {
  127. struct pxaip_attach_args *pxa = aux;
  128. #if NUSBF > 0
  129. int i;
  130. #endif
  131. sc->sc_iot = pxa->pxa_iot;
  132. if (bus_space_map(sc->sc_iot, PXA2X0_USBDC_BASE, PXA2X0_USBDC_SIZE, 0,
  133. &sc->sc_ioh)) {
  134. printf(": cannot map mem space\n");
  135. return;
  136. }
  137. sc->sc_size = PXA2X0_USBDC_SIZE;
  138. printf(": USB Device Controller\n");
  139. bus_space_barrier(sc->sc_iot, sc->sc_ioh, 0, sc->sc_size,
  140. BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
  141. /* Set up GPIO pins and disable the controller. */
  142. pxaudc_disable(sc);
  143. #if NUSBF > 0
  144. /* Establish USB device interrupt. */
  145. sc->sc_ih = pxa2x0_intr_establish(PXA2X0_INT_USB, IPL_USB,
  146. pxaudc_intr, sc, DEVNAME(sc));
  147. if (sc->sc_ih == NULL) {
  148. printf(": unable to establish interrupt\n");
  149. bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
  150. sc->sc_size = 0;
  151. return;
  152. }
  153. /* Establish device connect interrupt. */
  154. #if 0
  155. sc->sc_conn_ih = pxa2x0_gpio_intr_establish(PXA_USB_DEVICE_PIN, /* XXX */
  156. IST_EDGE_BOTH, IPL_USB, pxaudc_connect_intr, sc, "usbc");
  157. #endif
  158. sc->sc_conn_ih = pxa2x0_gpio_intr_establish(sc->sc_gpio_detect,
  159. IST_EDGE_BOTH, IPL_USB, pxaudc_connect_intr, sc, "usbc");
  160. if (sc->sc_conn_ih == NULL) {
  161. printf(": unable to establish connect interrupt\n");
  162. pxa2x0_intr_disestablish(sc->sc_ih);
  163. bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
  164. sc->sc_ioh = 0;
  165. sc->sc_size = 0;
  166. return;
  167. }
  168. /* Set up the bus struct. */
  169. sc->sc_bus.methods = &pxaudc_bus_methods;
  170. sc->sc_bus.pipe_size = sizeof(struct pxaudc_pipe);
  171. sc->sc_bus.ep0_maxp = PXAUDC_EP0MAXP;
  172. sc->sc_bus.usbrev = USBREV_1_1;
  173. sc->sc_bus.dmatag = pxa->pxa_dmat;
  174. sc->sc_npipe = 0; /* ep0 is always there. */
  175. sc->sc_ep_map[0] = 0;
  176. /* 16 == max logical endpoints */
  177. for (i = 1; i < 16; i++) {
  178. sc->sc_ep_map[i] = -1;
  179. }
  180. /* Attach logical device and function. */
  181. (void)config_found((struct device *)sc, &sc->sc_bus, NULL);
  182. /* Enable the controller unless we're now acting as a host. */
  183. if (!sc->sc_is_host())
  184. pxaudc_enable(sc);
  185. #endif
  186. }
  187. int
  188. pxaudc_detach(struct pxaudc_softc *sc, int flags)
  189. {
  190. if (sc->sc_conn_ih != NULL)
  191. pxa2x0_gpio_intr_disestablish(sc->sc_conn_ih);
  192. if (sc->sc_ih != NULL)
  193. pxa2x0_intr_disestablish(sc->sc_ih);
  194. if (sc->sc_size) {
  195. bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
  196. sc->sc_size = 0;
  197. }
  198. return (0);
  199. }
  200. int
  201. pxaudc_activate(struct pxaudc_softc *self, int act)
  202. {
  203. struct pxaudc_softc *sc = (struct pxaudc_softc *)self;
  204. switch (act) {
  205. case DVACT_SUSPEND:
  206. pxaudc_disable(sc);
  207. break;
  208. case DVACT_RESUME:
  209. pxaudc_enable(sc);
  210. break;
  211. }
  212. return 0;
  213. }
  214. /*
  215. * Register manipulation
  216. */
  217. #if 0
  218. static void
  219. pxaudc_dump_regs(struct pxaudc_softc *sc)
  220. {
  221. printf("UDCCR\t%b\n", CSR_READ_4(sc, USBDC_UDCCR),
  222. USBDC_UDCCR_BITS);
  223. printf("UDCICR0\t%b\n", CSR_READ_4(sc, USBDC_UDCICR0),
  224. USBDC_UDCISR0_BITS);
  225. printf("UDCICR1\t%b\n", CSR_READ_4(sc, USBDC_UDCICR1),
  226. USBDC_UDCISR1_BITS);
  227. printf("OTGICR\t%b\n", CSR_READ_4(sc, USBDC_UDCOTGICR),
  228. USBDC_UDCOTGISR_BITS);
  229. }
  230. #endif
  231. void
  232. pxaudc_enable(struct pxaudc_softc *sc)
  233. {
  234. int i;
  235. DPRINTF(10,("pxaudc_enable\n"));
  236. /* Start the clocks. */
  237. pxa2x0_clkman_config(CKEN_USBDC, 1);
  238. #if 0
  239. /* Configure Port 2 for USB device. */
  240. CSR_WRITE_4(sc, USBDC_UP2OCR, USBDC_UP2OCR_DMPUE |
  241. USBDC_UP2OCR_DPPUE | USBDC_UP2OCR_HXOE);
  242. #else
  243. /* Configure Port 2 for USB device. */
  244. CSR_WRITE_4(sc, USBDC_UP2OCR, USBDC_UP2OCR_DPPUE | USBDC_UP2OCR_HXOE);
  245. #endif
  246. CSR_SET_4(sc, USBDC_UDCCR, 0);
  247. sc->sc_icr0 = 0;
  248. sc->sc_icr1 = 0;
  249. for (i = 1; i < PXAUDC_NEP; i++)
  250. CSR_WRITE_4(sc, USBDC_UDCECR(i), 0); /* disable endpoints */
  251. for (i = 1; i < sc->sc_npipe; i++) {
  252. if (sc->sc_pipe[i] != NULL) {
  253. struct usbf_endpoint *ep =
  254. sc->sc_pipe[i]->pipe.endpoint;
  255. u_int32_t cr;
  256. int dir = usbf_endpoint_dir(ep);
  257. usb_endpoint_descriptor_t *ed = ep->edesc;
  258. if (i < 16)
  259. sc->sc_icr0 |= USBDC_UDCICR0_IE(i);
  260. else
  261. sc->sc_icr1 |= USBDC_UDCICR1_IE(i-16);
  262. cr = USBDC_UDCECR_EE | USBDC_UDCECR_DE;
  263. cr |= USBDC_UDCECR_ENs(
  264. UE_GET_ADDR(ed->bEndpointAddress));
  265. cr |= USBDC_UDCECR_MPSs(UGETW(ed->wMaxPacketSize));
  266. cr |= USBDC_UDCECR_ETs(ed->bmAttributes & UE_XFERTYPE);
  267. if (dir == UE_DIR_IN)
  268. cr |= USBDC_UDCECR_ED;
  269. /* XXX - until pipe has cn/in/ain */
  270. cr |= USBDC_UDCECR_AISNs(0) | USBDC_UDCECR_INs(0) |
  271. USBDC_UDCECR_CNs(1);
  272. CSR_WRITE_4(sc, USBDC_UDCECR(i), cr);
  273. /* clear old status */
  274. CSR_WRITE_4(sc, USBDC_UDCCSR(1),
  275. USBDC_UDCCSR_PC | USBDC_UDCCSR_TRN |
  276. USBDC_UDCCSR_SST | USBDC_UDCCSR_FEF);
  277. }
  278. }
  279. CSR_WRITE_4(sc, USBDC_UDCISR0, 0xffffffff); /* clear all */
  280. CSR_WRITE_4(sc, USBDC_UDCISR1, 0xffffffff); /* clear all */
  281. CSR_SET_4(sc, USBDC_UDCCSR0, USBDC_UDCCSR0_ACM);
  282. /* Enable interrupts for configured endpoints. */
  283. CSR_WRITE_4(sc, USBDC_UDCICR0, USBDC_UDCICR0_IE(0) |
  284. sc->sc_icr0);
  285. CSR_WRITE_4(sc, USBDC_UDCICR1, USBDC_UDCICR1_IERS |
  286. USBDC_UDCICR1_IESU | USBDC_UDCICR1_IERU |
  287. USBDC_UDCICR1_IECC | sc->sc_icr1);
  288. /* Enable the controller. */
  289. CSR_CLR_4(sc, USBDC_UDCCR, USBDC_UDCCR_EMCE);
  290. CSR_SET_4(sc, USBDC_UDCCR, USBDC_UDCCR_UDE);
  291. /* Enable USB client on port 2. */
  292. pxa2x0_gpio_clear_bit(37); /* USB_P2_8 */
  293. }
  294. void
  295. pxaudc_disable(struct pxaudc_softc *sc)
  296. {
  297. DPRINTF(10,("pxaudc_disable\n"));
  298. /* Disable the controller. */
  299. CSR_CLR_4(sc, USBDC_UDCCR, USBDC_UDCCR_UDE);
  300. /* Disable all interrupts. */
  301. CSR_WRITE_4(sc, USBDC_UDCICR0, 0);
  302. CSR_WRITE_4(sc, USBDC_UDCICR1, 0);
  303. CSR_WRITE_4(sc, USBDC_UDCOTGICR, 0);
  304. /* Set Port 2 output to "Non-OTG Host with Differential Port". */
  305. CSR_WRITE_4(sc, USBDC_UP2OCR, USBDC_UP2OCR_HXS | USBDC_UP2OCR_HXOE);
  306. /* Set "Host Port 2 Transceiver D­ Pull Down Enable". */
  307. CSR_SET_4(sc, USBDC_UP2OCR, USBDC_UP2OCR_DMPDE);
  308. /* Stop the clocks. */
  309. pxa2x0_clkman_config(CKEN_USBDC, 0);
  310. /* Enable USB host on port 2. */
  311. pxa2x0_gpio_set_bit(37); /* USB_P2_8 */
  312. }
  313. #if NUSBF > 0
  314. /*
  315. * Endpoint FIFO handling
  316. */
  317. void
  318. pxaudc_read_ep0(struct pxaudc_softc *sc, struct usbf_xfer *xfer)
  319. {
  320. size_t len;
  321. u_int8_t *p;
  322. xfer->actlen = CSR_READ_4(sc, USBDC_UDCBCR(0));
  323. len = MIN(xfer->actlen, xfer->length);
  324. p = xfer->buffer;
  325. while (CSR_READ_4(sc, USBDC_UDCCSR0) & USBDC_UDCCSR0_RNE) {
  326. u_int32_t v = CSR_READ_4(sc, USBDC_UDCDR(0));
  327. if (len > 0) {
  328. if (((unsigned)p & 0x3) == 0)
  329. *(u_int32_t *)p = v;
  330. else {
  331. *(p+0) = v & 0xff;
  332. *(p+1) = (v >> 8) & 0xff;
  333. *(p+2) = (v >> 16) & 0xff;
  334. *(p+3) = (v >> 24) & 0xff;
  335. }
  336. p += 4;
  337. len -= 4;
  338. }
  339. }
  340. CSR_WRITE_4(sc, USBDC_UDCCSR0, USBDC_UDCCSR0_SA | USBDC_UDCCSR0_OPC);
  341. xfer->status = USBF_NORMAL_COMPLETION;
  342. usbf_transfer_complete(xfer);
  343. }
  344. void
  345. pxaudc_read_epN(struct pxaudc_softc *sc, int ep)
  346. {
  347. size_t len, tlen;
  348. u_int8_t *p;
  349. struct pxaudc_pipe *ppipe;
  350. struct usbf_pipe *pipe = NULL;
  351. struct usbf_xfer *xfer = NULL;
  352. int count;
  353. u_int32_t csr;
  354. ppipe = sc->sc_pipe[ep];
  355. if (ppipe == NULL) {
  356. return;
  357. }
  358. pipe = &ppipe->pipe;
  359. again:
  360. xfer = SIMPLEQ_FIRST(&pipe->queue);
  361. if (xfer == NULL) {
  362. printf("pxaudc_read_epN: ep %d, no xfer\n", ep);
  363. return;
  364. }
  365. count = CSR_READ_4(sc, USBDC_UDCBCR(ep));
  366. tlen = len = MIN(count, xfer->length - xfer->actlen);
  367. p = xfer->buffer + xfer->actlen;
  368. csr = CSR_READ_4(sc, USBDC_UDCCSR(ep));
  369. if ((csr & USBDC_UDCCSR_PC) && count == 0)
  370. {
  371. #ifdef DEBUG_RX
  372. printf("trans1 complete\n");
  373. #endif
  374. xfer->status = USBF_NORMAL_COMPLETION;
  375. usbf_transfer_complete(xfer);
  376. CSR_SET_4(sc, USBDC_UDCCSR(ep), USBDC_UDCCSR_PC);
  377. return;
  378. }
  379. #ifdef DEBUG_RX
  380. printf("reading data from endpoint %x, len %x csr %x",
  381. ep, count, csr);
  382. #endif
  383. while (CSR_READ_4(sc, USBDC_UDCCSR(ep)) & USBDC_UDCCSR_BNE) {
  384. u_int32_t v = CSR_READ_4(sc, USBDC_UDCDR(ep));
  385. /* double buffering? */
  386. if (len > 0) {
  387. if (((unsigned)p & 0x3) == 0)
  388. *(u_int32_t *)p = v;
  389. else {
  390. *(p+0) = v & 0xff;
  391. *(p+1) = (v >> 8) & 0xff;
  392. *(p+2) = (v >> 16) & 0xff;
  393. *(p+3) = (v >> 24) & 0xff;
  394. }
  395. p += 4;
  396. len -= 4;
  397. xfer->actlen += 4;
  398. }
  399. count -= 4;
  400. }
  401. CSR_SET_4(sc, USBDC_UDCCSR(ep), USBDC_UDCCSR_PC);
  402. if (xfer->length == xfer->actlen || (tlen == 0 && xfer->actlen != 0) ||
  403. csr & USBDC_UDCCSR_SP) {
  404. #ifdef DEBUG_RX
  405. printf("trans2 complete\n");
  406. #endif
  407. xfer->status = USBF_NORMAL_COMPLETION;
  408. usbf_transfer_complete(xfer);
  409. }
  410. csr = CSR_READ_4(sc, USBDC_UDCCSR(ep));
  411. #ifdef DEBUG_RX
  412. printf("csr now %x len %x\n",
  413. csr, CSR_READ_4(sc, USBDC_UDCBCR(ep)));
  414. #endif
  415. if (csr & USBDC_UDCCSR_PC)
  416. goto again;
  417. }
  418. void
  419. pxaudc_write_ep0(struct pxaudc_softc *sc, struct usbf_xfer *xfer)
  420. {
  421. struct pxaudc_xfer *lxfer = (struct pxaudc_xfer *)xfer;
  422. u_int32_t len;
  423. u_int8_t *p;
  424. if (lxfer->frmlen > 0) {
  425. xfer->actlen += lxfer->frmlen;
  426. lxfer->frmlen = 0;
  427. }
  428. DPRINTF(11,("%s: ep0 ctrl-in, xfer=%p, len=%u, actlen=%u\n",
  429. DEVNAME(sc), xfer, xfer->length, xfer->actlen));
  430. if (xfer->actlen >= xfer->length) {
  431. sc->sc_ep0state = EP0_SETUP;
  432. usbf_transfer_complete(xfer);
  433. return;
  434. }
  435. sc->sc_ep0state = EP0_IN;
  436. p = (u_char *)xfer->buffer + xfer->actlen;
  437. len = xfer->length - xfer->actlen;
  438. len = MIN(len, PXAUDC_EP0MAXP);
  439. lxfer->frmlen = len;
  440. while (len >= 4) {
  441. u_int32_t v;
  442. if (((unsigned)p & 0x3) == 0)
  443. v = *(u_int32_t *)p;
  444. else {
  445. v = *(p+0);
  446. v |= *(p+1) << 8;
  447. v |= *(p+2) << 16;
  448. v |= *(p+3) << 24;
  449. }
  450. CSR_WRITE_4(sc, USBDC_UDCDR(0), v);
  451. len -= 4;
  452. p += 4;
  453. }
  454. while (len > 0) {
  455. CSR_WRITE_1(sc, USBDC_UDCDR(0), *p);
  456. len--;
  457. p++;
  458. }
  459. /* (12.6.7) Set IPR only for short packets. */
  460. if (lxfer->frmlen < PXAUDC_EP0MAXP)
  461. CSR_SET_4(sc, USBDC_UDCCSR0, USBDC_UDCCSR0_IPR);
  462. }
  463. void
  464. pxaudc_write(struct pxaudc_softc *sc, struct usbf_xfer *xfer)
  465. {
  466. u_int8_t *p;
  467. int ep = sc->sc_ep_map[usbf_endpoint_index(xfer->pipe->endpoint)];
  468. int tlen = 0;
  469. int maxp = UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize);
  470. u_int32_t csr, csr_o;
  471. #ifdef DEBUG_TX_PKT
  472. if (xfer->actlen == 0)
  473. printf("new packet len %x\n", xfer->length);
  474. #endif
  475. #ifdef DEBUG_TX
  476. printf("writing data to endpoint %x, xlen %x xact %x\n",
  477. ep, xfer->length, xfer->actlen);
  478. #endif
  479. if (xfer->actlen == xfer->length) {
  480. /*
  481. * If the packet size is wMaxPacketSize byte multiple
  482. * send a zero packet to indicate termiation.
  483. */
  484. if ((xfer->actlen % maxp) == 0 &&
  485. xfer->status != USBF_NORMAL_COMPLETION &&
  486. xfer->flags & USBD_FORCE_SHORT_XFER) {
  487. if (CSR_READ_4(sc, USBDC_UDCCSR(ep))
  488. & USBDC_UDCCSR_BNF) {
  489. CSR_SET_4(sc, USBDC_UDCCSR(ep),
  490. USBDC_UDCCSR_SP);
  491. /*
  492. * if we send a zero packet, we are 'done', but
  493. * dont to usbf_transfer_complete() just yet
  494. * because the short packet will cause another
  495. * interrupt.
  496. */
  497. xfer->status = USBF_NORMAL_COMPLETION;
  498. return;
  499. } else {
  500. printf("fifo full when trying to set short packet\n");
  501. }
  502. }
  503. xfer->status = USBF_NORMAL_COMPLETION;
  504. #ifdef DEBUG_TX_PKT
  505. printf("packet complete %x\n", xfer->actlen);
  506. #endif
  507. usbf_transfer_complete(xfer);
  508. return;
  509. }
  510. p = xfer->buffer + xfer->actlen;
  511. csr_o = 0;
  512. csr = CSR_READ_4(sc, USBDC_UDCCSR(ep));
  513. if (csr & USBDC_UDCCSR_PC)
  514. csr_o |= USBDC_UDCCSR_PC;
  515. if (csr & USBDC_UDCCSR_TRN)
  516. csr_o |= USBDC_UDCCSR_TRN;
  517. if (csr & USBDC_UDCCSR_SST)
  518. csr_o |= USBDC_UDCCSR_SST;
  519. if (csr_o != 0)
  520. CSR_WRITE_4(sc, USBDC_UDCCSR(ep), csr_o);
  521. while (CSR_READ_4(sc, USBDC_UDCCSR(ep)) & USBDC_UDCCSR_BNF) {
  522. u_int32_t v;
  523. if (xfer->length - xfer->actlen < 4) {
  524. while (xfer->actlen < xfer->length) {
  525. CSR_WRITE_1(sc, USBDC_UDCDR(ep), *p);
  526. p++;
  527. xfer->actlen++;
  528. tlen++;
  529. }
  530. break;
  531. }
  532. if (((unsigned)p & 0x3) == 0)
  533. v = *(u_int32_t *)p;
  534. else {
  535. v = *(p+0);
  536. v |= *(p+1) << 8;
  537. v |= *(p+2) << 16;
  538. v |= *(p+3) << 24;
  539. }
  540. CSR_WRITE_4(sc, USBDC_UDCDR(ep), v);
  541. p += 4;
  542. xfer->actlen += 4;
  543. tlen += 4;
  544. }
  545. #ifdef DEBUG_TX
  546. printf(" wrote tlen %x %x\n", tlen, xfer->actlen);
  547. if (xfer->actlen == 0) {
  548. printf("whoa, write_ep called, but no free space\n");
  549. }
  550. #endif
  551. if (xfer->actlen == xfer->length) {
  552. if ((xfer->actlen % maxp) != 0) {
  553. if (xfer->flags & USBD_FORCE_SHORT_XFER) {
  554. CSR_SET_4(sc, USBDC_UDCCSR(ep), USBDC_UDCCSR_SP);
  555. #ifdef DEBUG_TX
  556. printf("setting short packet on %x csr\n", ep,
  557. CSR_READ_4(sc, USBDC_UDCCSR(ep)));
  558. #endif
  559. } else {
  560. /* fill buffer to maxpacket size??? */
  561. }
  562. }
  563. }
  564. }
  565. /*
  566. * Interrupt handling
  567. */
  568. int
  569. pxaudc_connect_intr(void *v)
  570. {
  571. struct pxaudc_softc *sc = v;
  572. DPRINTF(10,("pxaudc_connect_intr: connect=%d\n",
  573. pxa2x0_gpio_get_bit(sc->sc_gpio_detect)));
  574. /* XXX only set a flag here */
  575. if (sc->sc_is_host()) {
  576. #if 0
  577. printf("%s:switching to host\n", sc->sc_bus.bdev.dv_xname);
  578. #endif
  579. pxaudc_disable(sc);
  580. } else {
  581. #if 0
  582. printf("%s:switching to client\n", sc->sc_bus.bdev.dv_xname);
  583. #endif
  584. pxaudc_enable(sc);
  585. }
  586. /* Claim this interrupt. */
  587. return 1;
  588. }
  589. int
  590. pxaudc_intr(void *v)
  591. {
  592. struct pxaudc_softc *sc = v;
  593. u_int32_t isr0, isr1, otgisr;
  594. isr0 = CSR_READ_4(sc, USBDC_UDCISR0);
  595. isr1 = CSR_READ_4(sc, USBDC_UDCISR1);
  596. otgisr = CSR_READ_4(sc, USBDC_UDCOTGISR);
  597. DPRINTF(10,("pxaudc_intr: isr0=%b, isr1=%b, otgisr=%b\n",
  598. isr0, USBDC_UDCISR0_BITS, isr1, USBDC_UDCISR1_BITS,
  599. otgisr, USBDC_UDCOTGISR_BITS));
  600. if (isr0 || isr1 || otgisr) {
  601. sc->sc_isr0 |= isr0;
  602. sc->sc_isr1 |= isr1;
  603. sc->sc_otgisr |= otgisr;
  604. //usbf_schedsoftintr(&sc->sc_bus);
  605. pxaudc_intr1(sc); /* XXX */
  606. }
  607. CSR_WRITE_4(sc, USBDC_UDCISR0, isr0);
  608. CSR_WRITE_4(sc, USBDC_UDCISR1, isr1);
  609. CSR_WRITE_4(sc, USBDC_UDCOTGISR, otgisr);
  610. /* Claim this interrupt. */
  611. return 1;
  612. }
  613. u_int32_t csr1, csr2;
  614. void
  615. pxaudc_intr1(struct pxaudc_softc *sc)
  616. {
  617. u_int32_t isr0, isr1, otgisr;
  618. int i;
  619. //int s;
  620. //s = splhardusb();
  621. isr0 = sc->sc_isr0;
  622. isr1 = sc->sc_isr1;
  623. otgisr = sc->sc_otgisr;
  624. sc->sc_isr0 = 0;
  625. sc->sc_isr1 = 0;
  626. sc->sc_otgisr = 0;
  627. //splx(s);
  628. sc->sc_bus.intr_context++;
  629. if (isr1 & USBDC_UDCISR1_IRCC) {
  630. u_int32_t ccr;
  631. CSR_SET_4(sc, USBDC_UDCCR, USBDC_UDCCR_SMAC);
  632. /* wait for reconfig to finish (SMAC auto clears) */
  633. while (CSR_READ_4(sc, USBDC_UDCCR) & USBDC_UDCCR_SMAC)
  634. delay(10);
  635. ccr = CSR_READ_4(sc, USBDC_UDCCR);
  636. sc->sc_cn = USBDC_UDCCR_ACNr(ccr);
  637. sc->sc_in = USBDC_UDCCR_AINr(ccr);
  638. sc->sc_isn = USBDC_UDCCR_AAISNr(ccr);
  639. goto ret;
  640. }
  641. #if 0
  642. printf("pxaudc_intr: isr0=%b, isr1=%b, otgisr=%b\n",
  643. isr0, USBDC_UDCISR0_BITS, isr1, USBDC_UDCISR1_BITS,
  644. otgisr, USBDC_UDCOTGISR_BITS);
  645. #endif
  646. /* Handle USB RESET condition. */
  647. if (isr1 & USBDC_UDCISR1_IRRS) {
  648. sc->sc_ep0state = EP0_SETUP;
  649. usbf_host_reset(&sc->sc_bus);
  650. /* Discard all other interrupts. */
  651. goto ret;
  652. }
  653. /* Service control pipe interrupts. */
  654. if (isr0 & USBDC_UDCISR0_IR(0))
  655. pxaudc_ep0_intr(sc);
  656. for (i = 1; i < 24; i++) {
  657. if (i < 16) {
  658. if (USBDC_UDCISR0_IRs(isr0,i))
  659. pxaudc_epN_intr(sc, i,
  660. USBDC_UDCISR0_IRs(isr0,i));
  661. } else {
  662. if (USBDC_UDCISR1_IRs(isr1,i))
  663. pxaudc_epN_intr(sc, i,
  664. USBDC_UDCISR1_IRs(isr1,i));
  665. }
  666. }
  667. if (isr1 & USBDC_UDCISR1_IRSU) {
  668. /* suspend ?? */
  669. }
  670. if (isr1 & USBDC_UDCISR1_IRRU) {
  671. /* resume ?? */
  672. }
  673. ret:
  674. sc->sc_bus.intr_context--;
  675. }
  676. void
  677. pxaudc_epN_intr(struct pxaudc_softc *sc, int ep, int isr)
  678. {
  679. struct pxaudc_pipe *ppipe;
  680. struct usbf_pipe *pipe;
  681. int dir;
  682. /* should not occur before device is configured */
  683. if (sc->sc_cn == 0)
  684. return;
  685. if (isr & 2)
  686. printf("ep%d: fifo error\n", ep); /* XXX */
  687. /* faster method of determining direction? */
  688. ppipe = sc->sc_pipe[ep];
  689. if (ppipe == NULL)
  690. return;
  691. pipe = &ppipe->pipe;
  692. dir = usbf_endpoint_dir(pipe->endpoint);
  693. if (dir == UE_DIR_IN) {
  694. pxaudc_write_epN(sc, ep);
  695. } else {
  696. pxaudc_read_epN(sc, ep);
  697. }
  698. }
  699. void
  700. pxaudc_write_epN(struct pxaudc_softc *sc, int ep)
  701. {
  702. struct pxaudc_pipe *ppipe;
  703. struct usbf_pipe *pipe = NULL;
  704. struct usbf_xfer *xfer = NULL;
  705. ppipe = sc->sc_pipe[ep];
  706. if (ppipe == NULL) {
  707. return;
  708. }
  709. pipe = &ppipe->pipe;
  710. xfer = SIMPLEQ_FIRST(&pipe->queue);
  711. if (xfer != NULL)
  712. pxaudc_write(sc, xfer);
  713. }
  714. void
  715. pxaudc_ep0_intr(struct pxaudc_softc *sc)
  716. {
  717. struct pxaudc_pipe *ppipe;
  718. struct usbf_pipe *pipe = NULL;
  719. struct usbf_xfer *xfer = NULL;
  720. u_int32_t csr0;
  721. csr0 = CSR_READ_4(sc, USBDC_UDCCSR0);
  722. DPRINTF(10,("pxaudc_ep0_intr: csr0=%b\n", csr0, USBDC_UDCCSR0_BITS));
  723. delay (25);
  724. ppipe = sc->sc_pipe[0];
  725. if (ppipe != NULL) {
  726. pipe = &ppipe->pipe;
  727. xfer = SIMPLEQ_FIRST(&pipe->queue);
  728. }
  729. if (sc->sc_ep0state == EP0_SETUP && (csr0 & USBDC_UDCCSR0_OPC)) {
  730. if (pipe == NULL) {
  731. DPRINTF(10,("pxaudc_ep0_intr: no control pipe\n"));
  732. return;
  733. }
  734. if (xfer == NULL) {
  735. DPRINTF(10,("pxaudc_ep0_intr: no xfer\n"));
  736. return;
  737. }
  738. pxaudc_read_ep0(sc, xfer);
  739. } else if (sc->sc_ep0state == EP0_IN &&
  740. (csr0 & USBDC_UDCCSR0_IPR) == 0 && xfer) {
  741. pxaudc_write_ep0(sc, xfer);
  742. }
  743. }
  744. /*
  745. * Bus methods
  746. */
  747. usbf_status
  748. pxaudc_open(struct usbf_pipe *pipe)
  749. {
  750. struct pxaudc_softc *sc = (struct pxaudc_softc *)pipe->device->bus;
  751. struct pxaudc_pipe *ppipe = (struct pxaudc_pipe *)pipe;
  752. int ep_idx;
  753. int s;
  754. ep_idx = usbf_endpoint_index(pipe->endpoint);
  755. if (ep_idx >= PXAUDC_NEP)
  756. return USBF_BAD_ADDRESS;
  757. DPRINTF(10,("pxaudc_open\n"));
  758. s = splhardusb();
  759. switch (usbf_endpoint_type(pipe->endpoint)) {
  760. case UE_CONTROL:
  761. pipe->methods = &pxaudc_ctrl_methods;
  762. break;
  763. case UE_BULK:
  764. pipe->methods = &pxaudc_bulk_methods;
  765. break;
  766. case UE_ISOCHRONOUS:
  767. case UE_INTERRUPT:
  768. default:
  769. /* XXX */
  770. splx(s);
  771. return USBF_BAD_ADDRESS;
  772. }
  773. if (ep_idx != 0 && sc->sc_ep_map[ep_idx] != -1) {
  774. printf("endpoint %d already used by %c",
  775. ep_idx, '@'+ sc->sc_ep_map[0]);
  776. splx(s);
  777. return USBF_BAD_ADDRESS;
  778. }
  779. sc->sc_ep_map[ep_idx] = sc->sc_npipe;
  780. sc->sc_pipe[sc->sc_npipe] = ppipe;
  781. sc->sc_npipe++;
  782. splx(s);
  783. return USBF_NORMAL_COMPLETION;
  784. }
  785. void
  786. pxaudc_softintr(void *v)
  787. {
  788. struct pxaudc_softc *sc = v;
  789. pxaudc_intr1(sc);
  790. }
  791. usbf_status
  792. pxaudc_allocm(struct usbf_bus *bus, struct usb_dma *dmap, u_int32_t size)
  793. {
  794. return usbf_allocmem(bus, size, 0, dmap);
  795. }
  796. void
  797. pxaudc_freem(struct usbf_bus *bus, struct usb_dma *dmap)
  798. {
  799. usbf_freemem(bus, dmap);
  800. }
  801. struct usbf_xfer *
  802. pxaudc_allocx(struct usbf_bus *bus)
  803. {
  804. struct pxaudc_softc *sc = (struct pxaudc_softc *)bus;
  805. struct usbf_xfer *xfer;
  806. xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
  807. if (xfer != NULL)
  808. SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
  809. else
  810. xfer = malloc(sizeof(struct pxaudc_xfer), M_USB, M_NOWAIT);
  811. if (xfer != NULL)
  812. bzero(xfer, sizeof(struct pxaudc_xfer));
  813. return xfer;
  814. }
  815. void
  816. pxaudc_freex(struct usbf_bus *bus, struct usbf_xfer *xfer)
  817. {
  818. struct pxaudc_softc *sc = (struct pxaudc_softc *)bus;
  819. SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
  820. }
  821. /*
  822. * Control pipe methods
  823. */
  824. usbf_status
  825. pxaudc_ctrl_transfer(struct usbf_xfer *xfer)
  826. {
  827. usbf_status err;
  828. /* Insert last in queue. */
  829. err = usbf_insert_transfer(xfer);
  830. if (err)
  831. return err;
  832. /*
  833. * Pipe isn't running (otherwise err would be USBF_IN_PROGRESS),
  834. * so start first.
  835. */
  836. return pxaudc_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
  837. }
  838. usbf_status
  839. pxaudc_ctrl_start(struct usbf_xfer *xfer)
  840. {
  841. struct usbf_pipe *pipe = xfer->pipe;
  842. struct pxaudc_softc *sc = (struct pxaudc_softc *)pipe->device->bus;
  843. int iswrite = !(xfer->rqflags & URQ_REQUEST);
  844. int s;
  845. s = splusb();
  846. xfer->status = USBF_IN_PROGRESS;
  847. if (iswrite)
  848. pxaudc_write_ep0(sc, xfer);
  849. else {
  850. /* XXX boring message, this case is normally reached if
  851. * XXX the xfer for a device request is being queued. */
  852. DPRINTF(10,("%s: ep[%x] ctrl-out, xfer=%p, len=%u, "
  853. "actlen=%u\n", DEVNAME(sc),
  854. usbf_endpoint_address(xfer->pipe->endpoint),
  855. xfer, xfer->length,
  856. xfer->actlen));
  857. }
  858. splx(s);
  859. return USBF_IN_PROGRESS;
  860. }
  861. /* (also used by bulk pipes) */
  862. void
  863. pxaudc_ctrl_abort(struct usbf_xfer *xfer)
  864. {
  865. int s;
  866. #ifdef PXAUDC_DEBUG
  867. struct usbf_pipe *pipe = xfer->pipe;
  868. struct pxaudc_softc *sc = (struct pxaudc_softc *)pipe->device->bus;
  869. int index = usbf_endpoint_index(pipe->endpoint);
  870. int dir = usbf_endpoint_dir(pipe->endpoint);
  871. int type = usbf_endpoint_type(pipe->endpoint);
  872. #endif
  873. DPRINTF(10,("%s: ep%d %s-%s abort, xfer=%p\n", DEVNAME(sc), index,
  874. type == UE_CONTROL ? "ctrl" : "bulk", dir == UE_DIR_IN ?
  875. "in" : "out", xfer));
  876. /*
  877. * Step 1: Make soft interrupt routine and hardware ignore the xfer.
  878. */
  879. s = splusb();
  880. xfer->status = USBF_CANCELLED;
  881. timeout_del(&xfer->timeout_handle);
  882. splx(s);
  883. /*
  884. * Step 2: Make sure hardware has finished any possible use of the
  885. * xfer and the soft interrupt routine has run.
  886. */
  887. s = splusb();
  888. /* XXX this does not seem right, what if there
  889. * XXX are two xfers in the FIFO and we only want to
  890. * XXX ignore one? */
  891. #ifdef notyet
  892. pxaudc_flush(sc, usbf_endpoint_address(pipe->endpoint));
  893. #endif
  894. /* XXX we're not doing DMA and the soft interrupt routine does not
  895. XXX need to clean up anything. */
  896. splx(s);
  897. /*
  898. * Step 3: Execute callback.
  899. */
  900. s = splusb();
  901. usbf_transfer_complete(xfer);
  902. splx(s);
  903. }
  904. void
  905. pxaudc_ctrl_done(struct usbf_xfer *xfer)
  906. {
  907. }
  908. void
  909. pxaudc_ctrl_close(struct usbf_pipe *pipe)
  910. {
  911. /* XXX */
  912. }
  913. /*
  914. * Bulk pipe methods
  915. */
  916. usbf_status
  917. pxaudc_bulk_transfer(struct usbf_xfer *xfer)
  918. {
  919. usbf_status err;
  920. /* Insert last in queue. */
  921. err = usbf_insert_transfer(xfer);
  922. if (err)
  923. return err;
  924. /*
  925. * Pipe isn't running (otherwise err would be USBF_IN_PROGRESS),
  926. * so start first.
  927. */
  928. return pxaudc_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
  929. }
  930. usbf_status
  931. pxaudc_bulk_start(struct usbf_xfer *xfer)
  932. {
  933. struct usbf_pipe *pipe = xfer->pipe;
  934. struct pxaudc_softc *sc = (struct pxaudc_softc *)pipe->device->bus;
  935. int iswrite = (usbf_endpoint_dir(pipe->endpoint) == UE_DIR_IN);
  936. int s;
  937. DPRINTF(0,("%s: ep%d bulk-%s start, xfer=%p, len=%u\n", DEVNAME(sc),
  938. usbf_endpoint_index(pipe->endpoint), iswrite ? "in" : "out",
  939. xfer, xfer->length));
  940. s = splusb();
  941. xfer->status = USBF_IN_PROGRESS;
  942. if (iswrite)
  943. pxaudc_write(sc, xfer);
  944. else {
  945. /* enable interrupt */
  946. }
  947. splx(s);
  948. return USBF_IN_PROGRESS;
  949. }
  950. void
  951. pxaudc_bulk_abort(struct usbf_xfer *xfer)
  952. {
  953. pxaudc_ctrl_abort(xfer);
  954. }
  955. void
  956. pxaudc_bulk_done(struct usbf_xfer *xfer)
  957. {
  958. #if 0
  959. int ep = usbf_endpoint_address(xfer->pipe->endpoint);
  960. struct usbf_pipe *pipe = xfer->pipe;
  961. struct pxaudc_softc *sc = (struct pxaudc_softc *)pipe->device->bus;
  962. #endif
  963. }
  964. void
  965. pxaudc_bulk_close(struct usbf_pipe *pipe)
  966. {
  967. /* XXX */
  968. }
  969. #endif /* NUSBF > 0 */