usbdcore_omap1510.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. /*
  2. * (C) Copyright 2003
  3. * Gerry Hamel, geh@ti.com, Texas Instruments
  4. *
  5. * Based on
  6. * linux/drivers/usb/device/bi/omap.c
  7. * TI OMAP1510 USB bus interface driver
  8. *
  9. * Author: MontaVista Software, Inc.
  10. * source@mvista.com
  11. * (C) Copyright 2002
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. #include <common.h>
  29. #if defined(CONFIG_OMAP1510) && defined(CONFIG_USB_DEVICE)
  30. #include <asm/io.h>
  31. #include <i2c.h>
  32. #include "usbdcore.h"
  33. #include "usbdcore_omap1510.h"
  34. #include "usbdcore_ep0.h"
  35. #define UDC_MAX_ENDPOINTS 31 /* Number of endpoints on this UDC */
  36. /* Some kind of debugging output... */
  37. #if 1
  38. #define UDCDBG(str)
  39. #define UDCDBGA(fmt,args...)
  40. #else /* The bugs still exists... */
  41. #define UDCDBG(str) serial_printf("[%s] %s:%d: " str "\n", __FILE__,__FUNCTION__,__LINE__)
  42. #define UDCDBGA(fmt,args...) serial_printf("[%s] %s:%d: " fmt "\n", __FILE__,__FUNCTION__,__LINE__, ##args)
  43. #endif
  44. #if 1
  45. #define UDCREG(name)
  46. #define UDCREGL(name)
  47. #else /* The bugs still exists... */
  48. #define UDCREG(name) serial_printf("%s():%d: %s[%08x]=%.4x\n",__FUNCTION__,__LINE__, (#name), name, inw(name)) /* For 16-bit regs */
  49. #define UDCREGL(name) serial_printf("%s():%d: %s[%08x]=%.8x\n",__FUNCTION__,__LINE__, (#name), name, inl(name)) /* For 32-bit regs */
  50. #endif
  51. static struct urb *ep0_urb = NULL;
  52. static struct usb_device_instance *udc_device; /* Used in interrupt handler */
  53. static u16 udc_devstat = 0; /* UDC status (DEVSTAT) */
  54. static u32 udc_interrupts = 0;
  55. static void udc_stall_ep (unsigned int ep_addr);
  56. static struct usb_endpoint_instance *omap1510_find_ep (int ep)
  57. {
  58. int i;
  59. for (i = 0; i < udc_device->bus->max_endpoints; i++) {
  60. if (udc_device->bus->endpoint_array[i].endpoint_address == ep)
  61. return &udc_device->bus->endpoint_array[i];
  62. }
  63. return NULL;
  64. }
  65. /* ************************************************************************** */
  66. /* IO
  67. */
  68. /*
  69. * omap1510_prepare_endpoint_for_rx
  70. *
  71. * This function implements TRM Figure 14-11.
  72. *
  73. * The endpoint to prepare for transfer is specified as a physical endpoint
  74. * number. For OUT (rx) endpoints 1 through 15, the corresponding endpoint
  75. * configuration register is checked to see if the endpoint is ISO or not.
  76. * If the OUT endpoint is valid and is non-ISO then its FIFO is enabled.
  77. * No action is taken for endpoint 0 or for IN (tx) endpoints 16 through 30.
  78. */
  79. static void omap1510_prepare_endpoint_for_rx (int ep_addr)
  80. {
  81. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  82. UDCDBGA ("omap1510_prepare_endpoint %x", ep_addr);
  83. if (((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)) {
  84. if ((inw (UDC_EP_RX (ep_num)) &
  85. (UDC_EPn_RX_Valid | UDC_EPn_RX_Iso)) ==
  86. UDC_EPn_RX_Valid) {
  87. /* rx endpoint is valid, non-ISO, so enable its FIFO */
  88. outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
  89. outw (UDC_Set_FIFO_En, UDC_CTRL);
  90. outw (0, UDC_EP_NUM);
  91. }
  92. }
  93. }
  94. /* omap1510_configure_endpoints
  95. *
  96. * This function implements TRM Figure 14-10.
  97. */
  98. static void omap1510_configure_endpoints (struct usb_device_instance *device)
  99. {
  100. int ep;
  101. struct usb_bus_instance *bus;
  102. struct usb_endpoint_instance *endpoint;
  103. unsigned short ep_ptr;
  104. unsigned short ep_size;
  105. unsigned short ep_isoc;
  106. unsigned short ep_doublebuffer;
  107. int ep_addr;
  108. int packet_size;
  109. int buffer_size;
  110. int attributes;
  111. bus = device->bus;
  112. /* There is a dedicated 2048 byte buffer for USB packets that may be
  113. * arbitrarily partitioned among the endpoints on 8-byte boundaries.
  114. * The first 8 bytes are reserved for receiving setup packets on
  115. * endpoint 0.
  116. */
  117. ep_ptr = 8; /* reserve the first 8 bytes for the setup fifo */
  118. for (ep = 0; ep < bus->max_endpoints; ep++) {
  119. endpoint = bus->endpoint_array + ep;
  120. ep_addr = endpoint->endpoint_address;
  121. if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  122. /* IN endpoint */
  123. packet_size = endpoint->tx_packetSize;
  124. attributes = endpoint->tx_attributes;
  125. } else {
  126. /* OUT endpoint */
  127. packet_size = endpoint->rcv_packetSize;
  128. attributes = endpoint->rcv_attributes;
  129. }
  130. switch (packet_size) {
  131. case 0:
  132. ep_size = 0;
  133. break;
  134. case 8:
  135. ep_size = 0;
  136. break;
  137. case 16:
  138. ep_size = 1;
  139. break;
  140. case 32:
  141. ep_size = 2;
  142. break;
  143. case 64:
  144. ep_size = 3;
  145. break;
  146. case 128:
  147. ep_size = 4;
  148. break;
  149. case 256:
  150. ep_size = 5;
  151. break;
  152. case 512:
  153. ep_size = 6;
  154. break;
  155. default:
  156. UDCDBGA ("ep 0x%02x has bad packet size %d",
  157. ep_addr, packet_size);
  158. packet_size = 0;
  159. ep_size = 0;
  160. break;
  161. }
  162. switch (attributes & USB_ENDPOINT_XFERTYPE_MASK) {
  163. case USB_ENDPOINT_XFER_CONTROL:
  164. case USB_ENDPOINT_XFER_BULK:
  165. case USB_ENDPOINT_XFER_INT:
  166. default:
  167. /* A non-isochronous endpoint may optionally be
  168. * double-buffered. For now we disable
  169. * double-buffering.
  170. */
  171. ep_doublebuffer = 0;
  172. ep_isoc = 0;
  173. if (packet_size > 64)
  174. packet_size = 0;
  175. if (!ep || !ep_doublebuffer)
  176. buffer_size = packet_size;
  177. else
  178. buffer_size = packet_size * 2;
  179. break;
  180. case USB_ENDPOINT_XFER_ISOC:
  181. /* Isochronous endpoints are always double-
  182. * buffered, but the double-buffering bit
  183. * in the endpoint configuration register
  184. * becomes the msb of the endpoint size so we
  185. * set the double-buffering flag to zero.
  186. */
  187. ep_doublebuffer = 0;
  188. ep_isoc = 1;
  189. buffer_size = packet_size * 2;
  190. break;
  191. }
  192. /* check to see if our packet buffer RAM is exhausted */
  193. if ((ep_ptr + buffer_size) > 2048) {
  194. UDCDBGA ("out of packet RAM for ep 0x%02x buf size %d", ep_addr, buffer_size);
  195. buffer_size = packet_size = 0;
  196. }
  197. /* force a default configuration for endpoint 0 since it is
  198. * always enabled
  199. */
  200. if (!ep && ((packet_size < 8) || (packet_size > 64))) {
  201. buffer_size = packet_size = 64;
  202. ep_size = 3;
  203. }
  204. if (!ep) {
  205. /* configure endpoint 0 */
  206. outw ((ep_size << 12) | (ep_ptr >> 3), UDC_EP0);
  207. /*UDCDBGA("ep 0 buffer offset 0x%03x packet size 0x%03x", */
  208. /* ep_ptr, packet_size); */
  209. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  210. /* IN endpoint */
  211. if (packet_size) {
  212. outw ((1 << 15) | (ep_doublebuffer << 14) |
  213. (ep_size << 12) | (ep_isoc << 11) |
  214. (ep_ptr >> 3),
  215. UDC_EP_TX (ep_addr &
  216. USB_ENDPOINT_NUMBER_MASK));
  217. UDCDBGA ("IN ep %d buffer offset 0x%03x"
  218. " packet size 0x%03x",
  219. ep_addr & USB_ENDPOINT_NUMBER_MASK,
  220. ep_ptr, packet_size);
  221. } else {
  222. outw (0,
  223. UDC_EP_TX (ep_addr &
  224. USB_ENDPOINT_NUMBER_MASK));
  225. }
  226. } else {
  227. /* OUT endpoint */
  228. if (packet_size) {
  229. outw ((1 << 15) | (ep_doublebuffer << 14) |
  230. (ep_size << 12) | (ep_isoc << 11) |
  231. (ep_ptr >> 3),
  232. UDC_EP_RX (ep_addr &
  233. USB_ENDPOINT_NUMBER_MASK));
  234. UDCDBGA ("OUT ep %d buffer offset 0x%03x"
  235. " packet size 0x%03x",
  236. ep_addr & USB_ENDPOINT_NUMBER_MASK,
  237. ep_ptr, packet_size);
  238. } else {
  239. outw (0,
  240. UDC_EP_RX (ep_addr &
  241. USB_ENDPOINT_NUMBER_MASK));
  242. }
  243. }
  244. ep_ptr += buffer_size;
  245. }
  246. }
  247. /* omap1510_deconfigure_device
  248. *
  249. * This function balances omap1510_configure_device.
  250. */
  251. static void omap1510_deconfigure_device (void)
  252. {
  253. int epnum;
  254. UDCDBG ("clear Cfg_Lock");
  255. outw (inw (UDC_SYSCON1) & ~UDC_Cfg_Lock, UDC_SYSCON1);
  256. UDCREG (UDC_SYSCON1);
  257. /* deconfigure all endpoints */
  258. for (epnum = 1; epnum <= 15; epnum++) {
  259. outw (0, UDC_EP_RX (epnum));
  260. outw (0, UDC_EP_TX (epnum));
  261. }
  262. }
  263. /* omap1510_configure_device
  264. *
  265. * This function implements TRM Figure 14-9.
  266. */
  267. static void omap1510_configure_device (struct usb_device_instance *device)
  268. {
  269. omap1510_configure_endpoints (device);
  270. /* Figure 14-9 indicates we should enable interrupts here, but we have
  271. * other routines (udc_all_interrupts, udc_suspended_interrupts) to
  272. * do that.
  273. */
  274. UDCDBG ("set Cfg_Lock");
  275. outw (inw (UDC_SYSCON1) | UDC_Cfg_Lock, UDC_SYSCON1);
  276. UDCREG (UDC_SYSCON1);
  277. }
  278. /* omap1510_write_noniso_tx_fifo
  279. *
  280. * This function implements TRM Figure 14-30.
  281. *
  282. * If the endpoint has an active tx_urb, then the next packet of data from the
  283. * URB is written to the tx FIFO. The total amount of data in the urb is given
  284. * by urb->actual_length. The maximum amount of data that can be sent in any
  285. * one packet is given by endpoint->tx_packetSize. The number of data bytes
  286. * from this URB that have already been transmitted is given by endpoint->sent.
  287. * endpoint->last is updated by this routine with the number of data bytes
  288. * transmitted in this packet.
  289. *
  290. * In accordance with Figure 14-30, the EP_NUM register must already have been
  291. * written with the value to select the appropriate tx FIFO before this routine
  292. * is called.
  293. */
  294. static void omap1510_write_noniso_tx_fifo (struct usb_endpoint_instance
  295. *endpoint)
  296. {
  297. struct urb *urb = endpoint->tx_urb;
  298. if (urb) {
  299. unsigned int last, i;
  300. UDCDBGA ("urb->buffer %p, buffer_length %d, actual_length %d",
  301. urb->buffer, urb->buffer_length, urb->actual_length);
  302. if ((last =
  303. MIN (urb->actual_length - endpoint->sent,
  304. endpoint->tx_packetSize))) {
  305. u8 *cp = urb->buffer + endpoint->sent;
  306. UDCDBGA ("endpoint->sent %d, tx_packetSize %d, last %d", endpoint->sent, endpoint->tx_packetSize, last);
  307. if (((u32) cp & 1) == 0) { /* word aligned? */
  308. outsw (UDC_DATA, cp, last >> 1);
  309. } else { /* byte aligned. */
  310. for (i = 0; i < (last >> 1); i++) {
  311. u16 w = ((u16) cp[2 * i + 1] << 8) |
  312. (u16) cp[2 * i];
  313. outw (w, UDC_DATA);
  314. }
  315. }
  316. if (last & 1) {
  317. outb (*(cp + last - 1), UDC_DATA);
  318. }
  319. }
  320. endpoint->last = last;
  321. }
  322. }
  323. /* omap1510_read_noniso_rx_fifo
  324. *
  325. * This function implements TRM Figure 14-28.
  326. *
  327. * If the endpoint has an active rcv_urb, then the next packet of data is read
  328. * from the rcv FIFO and written to rcv_urb->buffer at offset
  329. * rcv_urb->actual_length to append the packet data to the data from any
  330. * previous packets for this transfer. We assume that there is sufficient room
  331. * left in the buffer to hold an entire packet of data.
  332. *
  333. * The return value is the number of bytes read from the FIFO for this packet.
  334. *
  335. * In accordance with Figure 14-28, the EP_NUM register must already have been
  336. * written with the value to select the appropriate rcv FIFO before this routine
  337. * is called.
  338. */
  339. static int omap1510_read_noniso_rx_fifo (struct usb_endpoint_instance
  340. *endpoint)
  341. {
  342. struct urb *urb = endpoint->rcv_urb;
  343. int len = 0;
  344. if (urb) {
  345. len = inw (UDC_RXFSTAT);
  346. if (len) {
  347. unsigned char *cp = urb->buffer + urb->actual_length;
  348. insw (UDC_DATA, cp, len >> 1);
  349. if (len & 1)
  350. *(cp + len - 1) = inb (UDC_DATA);
  351. }
  352. }
  353. return len;
  354. }
  355. /* omap1510_prepare_for_control_write_status
  356. *
  357. * This function implements TRM Figure 14-17.
  358. *
  359. * We have to deal here with non-autodecoded control writes that haven't already
  360. * been dealt with by ep0_recv_setup. The non-autodecoded standard control
  361. * write requests are: set/clear endpoint feature, set configuration, set
  362. * interface, and set descriptor. ep0_recv_setup handles set/clear requests for
  363. * ENDPOINT_HALT by halting the endpoint for a set request and resetting the
  364. * endpoint for a clear request. ep0_recv_setup returns an error for
  365. * SET_DESCRIPTOR requests which causes them to be terminated with a stall by
  366. * the setup handler. A SET_INTERFACE request is handled by ep0_recv_setup by
  367. * generating a DEVICE_SET_INTERFACE event. This leaves only the
  368. * SET_CONFIGURATION event for us to deal with here.
  369. *
  370. */
  371. static void omap1510_prepare_for_control_write_status (struct urb *urb)
  372. {
  373. struct usb_device_request *request = &urb->device_request;;
  374. /* check for a SET_CONFIGURATION request */
  375. if (request->bRequest == USB_REQ_SET_CONFIGURATION) {
  376. int configuration = le16_to_cpu (request->wValue) & 0xff;
  377. unsigned short devstat = inw (UDC_DEVSTAT);
  378. if ((devstat & (UDC_ADD | UDC_CFG)) == UDC_ADD) {
  379. /* device is currently in ADDRESSED state */
  380. if (configuration) {
  381. /* Assume the specified non-zero configuration
  382. * value is valid and switch to the CONFIGURED
  383. * state.
  384. */
  385. outw (UDC_Dev_Cfg, UDC_SYSCON2);
  386. }
  387. } else if ((devstat & UDC_CFG) == UDC_CFG) {
  388. /* device is currently in CONFIGURED state */
  389. if (!configuration) {
  390. /* Switch to ADDRESSED state. */
  391. outw (UDC_Clr_Cfg, UDC_SYSCON2);
  392. }
  393. }
  394. }
  395. /* select EP0 tx FIFO */
  396. outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
  397. /* clear endpoint (no data bytes in status stage) */
  398. outw (UDC_Clr_EP, UDC_CTRL);
  399. /* enable the EP0 tx FIFO */
  400. outw (UDC_Set_FIFO_En, UDC_CTRL);
  401. /* deselect the endpoint */
  402. outw (UDC_EP_Dir, UDC_EP_NUM);
  403. }
  404. /* udc_state_transition_up
  405. * udc_state_transition_down
  406. *
  407. * Helper functions to implement device state changes. The device states and
  408. * the events that transition between them are:
  409. *
  410. * STATE_ATTACHED
  411. * || /\
  412. * \/ ||
  413. * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
  414. * || /\
  415. * \/ ||
  416. * STATE_POWERED
  417. * || /\
  418. * \/ ||
  419. * DEVICE_RESET DEVICE_POWER_INTERRUPTION
  420. * || /\
  421. * \/ ||
  422. * STATE_DEFAULT
  423. * || /\
  424. * \/ ||
  425. * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
  426. * || /\
  427. * \/ ||
  428. * STATE_ADDRESSED
  429. * || /\
  430. * \/ ||
  431. * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
  432. * || /\
  433. * \/ ||
  434. * STATE_CONFIGURED
  435. *
  436. * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED
  437. * to STATE_CONFIGURED) from the specified initial state to the specified final
  438. * state, passing through each intermediate state on the way. If the initial
  439. * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
  440. * no state transitions will take place.
  441. *
  442. * udc_state_transition_down transitions down (in the direction from
  443. * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
  444. * specified final state, passing through each intermediate state on the way.
  445. * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
  446. * state, then no state transitions will take place.
  447. *
  448. * These functions must only be called with interrupts disabled.
  449. */
  450. static void udc_state_transition_up (usb_device_state_t initial,
  451. usb_device_state_t final)
  452. {
  453. if (initial < final) {
  454. switch (initial) {
  455. case STATE_ATTACHED:
  456. usbd_device_event_irq (udc_device,
  457. DEVICE_HUB_CONFIGURED, 0);
  458. if (final == STATE_POWERED)
  459. break;
  460. case STATE_POWERED:
  461. usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
  462. if (final == STATE_DEFAULT)
  463. break;
  464. case STATE_DEFAULT:
  465. usbd_device_event_irq (udc_device,
  466. DEVICE_ADDRESS_ASSIGNED, 0);
  467. if (final == STATE_ADDRESSED)
  468. break;
  469. case STATE_ADDRESSED:
  470. usbd_device_event_irq (udc_device, DEVICE_CONFIGURED,
  471. 0);
  472. case STATE_CONFIGURED:
  473. break;
  474. default:
  475. break;
  476. }
  477. }
  478. }
  479. static void udc_state_transition_down (usb_device_state_t initial,
  480. usb_device_state_t final)
  481. {
  482. if (initial > final) {
  483. switch (initial) {
  484. case STATE_CONFIGURED:
  485. usbd_device_event_irq (udc_device, DEVICE_DE_CONFIGURED, 0);
  486. if (final == STATE_ADDRESSED)
  487. break;
  488. case STATE_ADDRESSED:
  489. usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
  490. if (final == STATE_DEFAULT)
  491. break;
  492. case STATE_DEFAULT:
  493. usbd_device_event_irq (udc_device, DEVICE_POWER_INTERRUPTION, 0);
  494. if (final == STATE_POWERED)
  495. break;
  496. case STATE_POWERED:
  497. usbd_device_event_irq (udc_device, DEVICE_HUB_RESET, 0);
  498. case STATE_ATTACHED:
  499. break;
  500. default:
  501. break;
  502. }
  503. }
  504. }
  505. /* Handle all device state changes.
  506. * This function implements TRM Figure 14-21.
  507. */
  508. static void omap1510_udc_state_changed (void)
  509. {
  510. u16 bits;
  511. u16 devstat = inw (UDC_DEVSTAT);
  512. UDCDBGA ("state changed, devstat %x, old %x", devstat, udc_devstat);
  513. bits = devstat ^ udc_devstat;
  514. if (bits) {
  515. if (bits & UDC_ATT) {
  516. if (devstat & UDC_ATT) {
  517. UDCDBG ("device attached and powered");
  518. udc_state_transition_up (udc_device->device_state, STATE_POWERED);
  519. } else {
  520. UDCDBG ("device detached or unpowered");
  521. udc_state_transition_down (udc_device->device_state, STATE_ATTACHED);
  522. }
  523. }
  524. if (bits & UDC_USB_Reset) {
  525. if (devstat & UDC_USB_Reset) {
  526. UDCDBG ("device reset in progess");
  527. udc_state_transition_down (udc_device->device_state, STATE_POWERED);
  528. } else {
  529. UDCDBG ("device reset completed");
  530. }
  531. }
  532. if (bits & UDC_DEF) {
  533. if (devstat & UDC_DEF) {
  534. UDCDBG ("device entering default state");
  535. udc_state_transition_up (udc_device->device_state, STATE_DEFAULT);
  536. } else {
  537. UDCDBG ("device leaving default state");
  538. udc_state_transition_down (udc_device->device_state, STATE_POWERED);
  539. }
  540. }
  541. if (bits & UDC_SUS) {
  542. if (devstat & UDC_SUS) {
  543. UDCDBG ("entering suspended state");
  544. usbd_device_event_irq (udc_device, DEVICE_BUS_INACTIVE, 0);
  545. } else {
  546. UDCDBG ("leaving suspended state");
  547. usbd_device_event_irq (udc_device, DEVICE_BUS_ACTIVITY, 0);
  548. }
  549. }
  550. if (bits & UDC_R_WK_OK) {
  551. UDCDBGA ("remote wakeup %s", (devstat & UDC_R_WK_OK)
  552. ? "enabled" : "disabled");
  553. }
  554. if (bits & UDC_ADD) {
  555. if (devstat & UDC_ADD) {
  556. UDCDBG ("default -> addressed");
  557. udc_state_transition_up (udc_device->device_state, STATE_ADDRESSED);
  558. } else {
  559. UDCDBG ("addressed -> default");
  560. udc_state_transition_down (udc_device->device_state, STATE_DEFAULT);
  561. }
  562. }
  563. if (bits & UDC_CFG) {
  564. if (devstat & UDC_CFG) {
  565. UDCDBG ("device configured");
  566. /* The ep0_recv_setup function generates the
  567. * DEVICE_CONFIGURED event when a
  568. * USB_REQ_SET_CONFIGURATION setup packet is
  569. * received, so we should already be in the
  570. * state STATE_CONFIGURED.
  571. */
  572. udc_state_transition_up (udc_device->device_state, STATE_CONFIGURED);
  573. } else {
  574. UDCDBG ("device deconfigured");
  575. udc_state_transition_down (udc_device->device_state, STATE_ADDRESSED);
  576. }
  577. }
  578. }
  579. /* Clear interrupt source */
  580. outw (UDC_DS_Chg, UDC_IRQ_SRC);
  581. /* Save current DEVSTAT */
  582. udc_devstat = devstat;
  583. }
  584. /* Handle SETUP USB interrupt.
  585. * This function implements TRM Figure 14-14.
  586. */
  587. static void omap1510_udc_setup (struct usb_endpoint_instance *endpoint)
  588. {
  589. UDCDBG ("-> Entering device setup");
  590. do {
  591. const int setup_pktsize = 8;
  592. unsigned char *datap =
  593. (unsigned char *) &ep0_urb->device_request;
  594. /* Gain access to EP 0 setup FIFO */
  595. outw (UDC_Setup_Sel, UDC_EP_NUM);
  596. /* Read control request data */
  597. insb (UDC_DATA, datap, setup_pktsize);
  598. UDCDBGA ("EP0 setup read [%x %x %x %x %x %x %x %x]",
  599. *(datap + 0), *(datap + 1), *(datap + 2),
  600. *(datap + 3), *(datap + 4), *(datap + 5),
  601. *(datap + 6), *(datap + 7));
  602. /* Reset EP0 setup FIFO */
  603. outw (0, UDC_EP_NUM);
  604. } while (inw (UDC_IRQ_SRC) & UDC_Setup);
  605. /* Try to process setup packet */
  606. if (ep0_recv_setup (ep0_urb)) {
  607. /* Not a setup packet, stall next EP0 transaction */
  608. udc_stall_ep (0);
  609. UDCDBG ("can't parse setup packet, still waiting for setup");
  610. return;
  611. }
  612. /* Check direction */
  613. if ((ep0_urb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
  614. == USB_REQ_HOST2DEVICE) {
  615. UDCDBG ("control write on EP0");
  616. if (le16_to_cpu (ep0_urb->device_request.wLength)) {
  617. /* We don't support control write data stages.
  618. * The only standard control write request with a data
  619. * stage is SET_DESCRIPTOR, and ep0_recv_setup doesn't
  620. * support that so we just stall those requests. A
  621. * function driver might support a non-standard
  622. * write request with a data stage, but it isn't
  623. * obvious what we would do with the data if we read it
  624. * so we'll just stall it. It seems like the API isn't
  625. * quite right here.
  626. */
  627. #if 0
  628. /* Here is what we would do if we did support control
  629. * write data stages.
  630. */
  631. ep0_urb->actual_length = 0;
  632. outw (0, UDC_EP_NUM);
  633. /* enable the EP0 rx FIFO */
  634. outw (UDC_Set_FIFO_En, UDC_CTRL);
  635. #else
  636. /* Stall this request */
  637. UDCDBG ("Stalling unsupported EP0 control write data "
  638. "stage.");
  639. udc_stall_ep (0);
  640. #endif
  641. } else {
  642. omap1510_prepare_for_control_write_status (ep0_urb);
  643. }
  644. } else {
  645. UDCDBG ("control read on EP0");
  646. /* The ep0_recv_setup function has already placed our response
  647. * packet data in ep0_urb->buffer and the packet length in
  648. * ep0_urb->actual_length.
  649. */
  650. endpoint->tx_urb = ep0_urb;
  651. endpoint->sent = 0;
  652. /* select the EP0 tx FIFO */
  653. outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
  654. /* Write packet data to the FIFO. omap1510_write_noniso_tx_fifo
  655. * will update endpoint->last with the number of bytes written
  656. * to the FIFO.
  657. */
  658. omap1510_write_noniso_tx_fifo (endpoint);
  659. /* enable the FIFO to start the packet transmission */
  660. outw (UDC_Set_FIFO_En, UDC_CTRL);
  661. /* deselect the EP0 tx FIFO */
  662. outw (UDC_EP_Dir, UDC_EP_NUM);
  663. }
  664. UDCDBG ("<- Leaving device setup");
  665. }
  666. /* Handle endpoint 0 RX interrupt
  667. * This routine implements TRM Figure 14-16.
  668. */
  669. static void omap1510_udc_ep0_rx (struct usb_endpoint_instance *endpoint)
  670. {
  671. unsigned short status;
  672. UDCDBG ("RX on EP0");
  673. /* select EP0 rx FIFO */
  674. outw (UDC_EP_Sel, UDC_EP_NUM);
  675. status = inw (UDC_STAT_FLG);
  676. if (status & UDC_ACK) {
  677. /* Check direction */
  678. if ((ep0_urb->device_request.bmRequestType
  679. & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
  680. /* This rx interrupt must be for a control write data
  681. * stage packet.
  682. *
  683. * We don't support control write data stages.
  684. * We should never end up here.
  685. */
  686. /* clear the EP0 rx FIFO */
  687. outw (UDC_Clr_EP, UDC_CTRL);
  688. /* deselect the EP0 rx FIFO */
  689. outw (0, UDC_EP_NUM);
  690. UDCDBG ("Stalling unexpected EP0 control write "
  691. "data stage packet");
  692. udc_stall_ep (0);
  693. } else {
  694. /* This rx interrupt must be for a control read status
  695. * stage packet.
  696. */
  697. UDCDBG ("ACK on EP0 control read status stage packet");
  698. /* deselect EP0 rx FIFO */
  699. outw (0, UDC_EP_NUM);
  700. }
  701. } else if (status & UDC_STALL) {
  702. UDCDBG ("EP0 stall during RX");
  703. /* deselect EP0 rx FIFO */
  704. outw (0, UDC_EP_NUM);
  705. } else {
  706. /* deselect EP0 rx FIFO */
  707. outw (0, UDC_EP_NUM);
  708. }
  709. }
  710. /* Handle endpoint 0 TX interrupt
  711. * This routine implements TRM Figure 14-18.
  712. */
  713. static void omap1510_udc_ep0_tx (struct usb_endpoint_instance *endpoint)
  714. {
  715. unsigned short status;
  716. struct usb_device_request *request = &ep0_urb->device_request;
  717. UDCDBG ("TX on EP0");
  718. /* select EP0 TX FIFO */
  719. outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
  720. status = inw (UDC_STAT_FLG);
  721. if (status & UDC_ACK) {
  722. /* Check direction */
  723. if ((request->bmRequestType & USB_REQ_DIRECTION_MASK) ==
  724. USB_REQ_HOST2DEVICE) {
  725. /* This tx interrupt must be for a control write status
  726. * stage packet.
  727. */
  728. UDCDBG ("ACK on EP0 control write status stage packet");
  729. /* deselect EP0 TX FIFO */
  730. outw (UDC_EP_Dir, UDC_EP_NUM);
  731. } else {
  732. /* This tx interrupt must be for a control read data
  733. * stage packet.
  734. */
  735. int wLength = le16_to_cpu (request->wLength);
  736. /* Update our count of bytes sent so far in this
  737. * transfer.
  738. */
  739. endpoint->sent += endpoint->last;
  740. /* We are finished with this transfer if we have sent
  741. * all of the bytes in our tx urb (urb->actual_length)
  742. * unless we need a zero-length terminating packet. We
  743. * need a zero-length terminating packet if we returned
  744. * fewer bytes than were requested (wLength) by the host,
  745. * and the number of bytes we returned is an exact
  746. * multiple of the packet size endpoint->tx_packetSize.
  747. */
  748. if ((endpoint->sent == ep0_urb->actual_length)
  749. && ((ep0_urb->actual_length == wLength)
  750. || (endpoint->last !=
  751. endpoint->tx_packetSize))) {
  752. /* Done with control read data stage. */
  753. UDCDBG ("control read data stage complete");
  754. /* deselect EP0 TX FIFO */
  755. outw (UDC_EP_Dir, UDC_EP_NUM);
  756. /* select EP0 RX FIFO to prepare for control
  757. * read status stage.
  758. */
  759. outw (UDC_EP_Sel, UDC_EP_NUM);
  760. /* clear the EP0 RX FIFO */
  761. outw (UDC_Clr_EP, UDC_CTRL);
  762. /* enable the EP0 RX FIFO */
  763. outw (UDC_Set_FIFO_En, UDC_CTRL);
  764. /* deselect the EP0 RX FIFO */
  765. outw (0, UDC_EP_NUM);
  766. } else {
  767. /* We still have another packet of data to send
  768. * in this control read data stage or else we
  769. * need a zero-length terminating packet.
  770. */
  771. UDCDBG ("ACK control read data stage packet");
  772. omap1510_write_noniso_tx_fifo (endpoint);
  773. /* enable the EP0 tx FIFO to start transmission */
  774. outw (UDC_Set_FIFO_En, UDC_CTRL);
  775. /* deselect EP0 TX FIFO */
  776. outw (UDC_EP_Dir, UDC_EP_NUM);
  777. }
  778. }
  779. } else if (status & UDC_STALL) {
  780. UDCDBG ("EP0 stall during TX");
  781. /* deselect EP0 TX FIFO */
  782. outw (UDC_EP_Dir, UDC_EP_NUM);
  783. } else {
  784. /* deselect EP0 TX FIFO */
  785. outw (UDC_EP_Dir, UDC_EP_NUM);
  786. }
  787. }
  788. /* Handle RX transaction on non-ISO endpoint.
  789. * This function implements TRM Figure 14-27.
  790. * The ep argument is a physical endpoint number for a non-ISO OUT endpoint
  791. * in the range 1 to 15.
  792. */
  793. static void omap1510_udc_epn_rx (int ep)
  794. {
  795. unsigned short status;
  796. /* Check endpoint status */
  797. status = inw (UDC_STAT_FLG);
  798. if (status & UDC_ACK) {
  799. int nbytes;
  800. struct usb_endpoint_instance *endpoint =
  801. omap1510_find_ep (ep);
  802. nbytes = omap1510_read_noniso_rx_fifo (endpoint);
  803. usbd_rcv_complete (endpoint, nbytes, 0);
  804. /* enable rx FIFO to prepare for next packet */
  805. outw (UDC_Set_FIFO_En, UDC_CTRL);
  806. } else if (status & UDC_STALL) {
  807. UDCDBGA ("STALL on RX endpoint %d", ep);
  808. } else if (status & UDC_NAK) {
  809. UDCDBGA ("NAK on RX ep %d", ep);
  810. } else {
  811. serial_printf ("omap-bi: RX on ep %d with status %x", ep,
  812. status);
  813. }
  814. }
  815. /* Handle TX transaction on non-ISO endpoint.
  816. * This function implements TRM Figure 14-29.
  817. * The ep argument is a physical endpoint number for a non-ISO IN endpoint
  818. * in the range 16 to 30.
  819. */
  820. static void omap1510_udc_epn_tx (int ep)
  821. {
  822. unsigned short status;
  823. /*serial_printf("omap1510_udc_epn_tx( %x )\n",ep); */
  824. /* Check endpoint status */
  825. status = inw (UDC_STAT_FLG);
  826. if (status & UDC_ACK) {
  827. struct usb_endpoint_instance *endpoint =
  828. omap1510_find_ep (ep);
  829. /* We need to transmit a terminating zero-length packet now if
  830. * we have sent all of the data in this URB and the transfer
  831. * size was an exact multiple of the packet size.
  832. */
  833. if (endpoint->tx_urb
  834. && (endpoint->last == endpoint->tx_packetSize)
  835. && (endpoint->tx_urb->actual_length - endpoint->sent -
  836. endpoint->last == 0)) {
  837. /* Prepare to transmit a zero-length packet. */
  838. endpoint->sent += endpoint->last;
  839. /* write 0 bytes of data to FIFO */
  840. omap1510_write_noniso_tx_fifo (endpoint);
  841. /* enable tx FIFO to start transmission */
  842. outw (UDC_Set_FIFO_En, UDC_CTRL);
  843. } else if (endpoint->tx_urb
  844. && endpoint->tx_urb->actual_length) {
  845. /* retire the data that was just sent */
  846. usbd_tx_complete (endpoint);
  847. /* Check to see if we have more data ready to transmit
  848. * now.
  849. */
  850. if (endpoint->tx_urb
  851. && endpoint->tx_urb->actual_length) {
  852. /* write data to FIFO */
  853. omap1510_write_noniso_tx_fifo (endpoint);
  854. /* enable tx FIFO to start transmission */
  855. outw (UDC_Set_FIFO_En, UDC_CTRL);
  856. }
  857. }
  858. } else if (status & UDC_STALL) {
  859. UDCDBGA ("STALL on TX endpoint %d", ep);
  860. } else if (status & UDC_NAK) {
  861. UDCDBGA ("NAK on TX endpoint %d", ep);
  862. } else {
  863. /*serial_printf("omap-bi: TX on ep %d with status %x\n", ep, status); */
  864. }
  865. }
  866. /*
  867. -------------------------------------------------------------------------------
  868. */
  869. /* Handle general USB interrupts and dispatch according to type.
  870. * This function implements TRM Figure 14-13.
  871. */
  872. void omap1510_udc_irq (void)
  873. {
  874. u16 irq_src = inw (UDC_IRQ_SRC);
  875. int valid_irq = 0;
  876. if (!(irq_src & ~UDC_SOF_Flg)) /* ignore SOF interrupts ) */
  877. return;
  878. UDCDBGA ("< IRQ #%d start >- %x", udc_interrupts, irq_src);
  879. /*serial_printf("< IRQ #%d start >- %x\n", udc_interrupts, irq_src); */
  880. if (irq_src & UDC_DS_Chg) {
  881. /* Device status changed */
  882. omap1510_udc_state_changed ();
  883. valid_irq++;
  884. }
  885. if (irq_src & UDC_EP0_RX) {
  886. /* Endpoint 0 receive */
  887. outw (UDC_EP0_RX, UDC_IRQ_SRC); /* ack interrupt */
  888. omap1510_udc_ep0_rx (udc_device->bus->endpoint_array + 0);
  889. valid_irq++;
  890. }
  891. if (irq_src & UDC_EP0_TX) {
  892. /* Endpoint 0 transmit */
  893. outw (UDC_EP0_TX, UDC_IRQ_SRC); /* ack interrupt */
  894. omap1510_udc_ep0_tx (udc_device->bus->endpoint_array + 0);
  895. valid_irq++;
  896. }
  897. if (irq_src & UDC_Setup) {
  898. /* Device setup */
  899. omap1510_udc_setup (udc_device->bus->endpoint_array + 0);
  900. valid_irq++;
  901. }
  902. /*if (!valid_irq) */
  903. /* serial_printf("unknown interrupt, IRQ_SRC %.4x\n", irq_src); */
  904. UDCDBGA ("< IRQ #%d end >", udc_interrupts);
  905. udc_interrupts++;
  906. }
  907. /* This function implements TRM Figure 14-26. */
  908. void omap1510_udc_noniso_irq (void)
  909. {
  910. unsigned short epnum;
  911. unsigned short irq_src = inw (UDC_IRQ_SRC);
  912. int valid_irq = 0;
  913. if (!(irq_src & (UDC_EPn_RX | UDC_EPn_TX)))
  914. return;
  915. UDCDBGA ("non-ISO IRQ, IRQ_SRC %x", inw (UDC_IRQ_SRC));
  916. if (irq_src & UDC_EPn_RX) { /* Endpoint N OUT transaction */
  917. /* Determine the endpoint number for this interrupt */
  918. epnum = (inw (UDC_EPN_STAT) & 0x0f00) >> 8;
  919. UDCDBGA ("RX on ep %x", epnum);
  920. /* acknowledge interrupt */
  921. outw (UDC_EPn_RX, UDC_IRQ_SRC);
  922. if (epnum) {
  923. /* select the endpoint FIFO */
  924. outw (UDC_EP_Sel | epnum, UDC_EP_NUM);
  925. omap1510_udc_epn_rx (epnum);
  926. /* deselect the endpoint FIFO */
  927. outw (epnum, UDC_EP_NUM);
  928. }
  929. valid_irq++;
  930. }
  931. if (irq_src & UDC_EPn_TX) { /* Endpoint N IN transaction */
  932. /* Determine the endpoint number for this interrupt */
  933. epnum = (inw (UDC_EPN_STAT) & 0x000f) | USB_DIR_IN;
  934. UDCDBGA ("TX on ep %x", epnum);
  935. /* acknowledge interrupt */
  936. outw (UDC_EPn_TX, UDC_IRQ_SRC);
  937. if (epnum) {
  938. /* select the endpoint FIFO */
  939. outw (UDC_EP_Sel | UDC_EP_Dir | epnum, UDC_EP_NUM);
  940. omap1510_udc_epn_tx (epnum);
  941. /* deselect the endpoint FIFO */
  942. outw (UDC_EP_Dir | epnum, UDC_EP_NUM);
  943. }
  944. valid_irq++;
  945. }
  946. if (!valid_irq)
  947. serial_printf (": unknown non-ISO interrupt, IRQ_SRC %.4x\n",
  948. irq_src);
  949. }
  950. /*
  951. -------------------------------------------------------------------------------
  952. */
  953. /*
  954. * Start of public functions.
  955. */
  956. /* Called to start packet transmission. */
  957. void udc_endpoint_write (struct usb_endpoint_instance *endpoint)
  958. {
  959. unsigned short epnum =
  960. endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
  961. UDCDBGA ("Starting transmit on ep %x", epnum);
  962. if (endpoint->tx_urb) {
  963. /* select the endpoint FIFO */
  964. outw (UDC_EP_Sel | UDC_EP_Dir | epnum, UDC_EP_NUM);
  965. /* write data to FIFO */
  966. omap1510_write_noniso_tx_fifo (endpoint);
  967. /* enable tx FIFO to start transmission */
  968. outw (UDC_Set_FIFO_En, UDC_CTRL);
  969. /* deselect the endpoint FIFO */
  970. outw (UDC_EP_Dir | epnum, UDC_EP_NUM);
  971. }
  972. }
  973. /* Start to initialize h/w stuff */
  974. int udc_init (void)
  975. {
  976. u16 udc_rev;
  977. uchar wert;
  978. udc_device = NULL;
  979. UDCDBG ("starting");
  980. /* Check peripheral reset. Must be 1 to make sure
  981. MPU TIPB peripheral reset is inactive */
  982. UDCREG (ARM_RSTCT2);
  983. /* Set and check clock control.
  984. * We might ought to be using the clock control API to do
  985. * this instead of fiddling with the clock registers directly
  986. * here.
  987. */
  988. outw ((1 << 4) | (1 << 5), CLOCK_CTRL);
  989. UDCREG (CLOCK_CTRL);
  990. /* Set and check APLL */
  991. outw (0x0008, APLL_CTRL);
  992. UDCREG (APLL_CTRL);
  993. /* Set and check DPLL */
  994. outw (0x2210, DPLL_CTRL);
  995. UDCREG (DPLL_CTRL);
  996. /* Set and check SOFT */
  997. outw ((1 << 4) | (1 << 3) | 1, SOFT_REQ);
  998. /* Short delay to wait for DPLL */
  999. udelay (1000);
  1000. /* Print banner with device revision */
  1001. udc_rev = inw (UDC_REV) & 0xff;
  1002. printf ("USB: TI OMAP1510 USB function module rev %d.%d\n",
  1003. udc_rev >> 4, udc_rev & 0xf);
  1004. /* Configure some Sofia Unit registers
  1005. */
  1006. i2c_read (0x32, 0x04, 1, &wert, 1); /* SOF_Control_CONT */
  1007. wert |= 0x04; /* SOF_ContCONT_dis_usb_det */
  1008. i2c_write (0x32, 0x04, 1, &wert, 1);
  1009. i2c_read (0x32, 0x03, 1, &wert, 1); /* SOF_Control */
  1010. wert |= 0x01; /* SOF_Cont_usb_en */
  1011. i2c_write (0x32, 0x03, 1, &wert, 1);
  1012. /* The VBUS_MODE bit selects whether VBUS detection is done via
  1013. * software (1) or hardware (0). When software detection is
  1014. * selected, VBUS_CTRL selects whether USB is not connected (0)
  1015. * or connected (1).
  1016. */
  1017. outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
  1018. outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
  1019. UDCREGL (FUNC_MUX_CTRL_0);
  1020. /*
  1021. * At this point, device is ready for configuration...
  1022. */
  1023. UDCDBG ("disable USB interrupts");
  1024. outw (0, UDC_IRQ_EN);
  1025. UDCREG (UDC_IRQ_EN);
  1026. UDCDBG ("disable USB DMA");
  1027. outw (0, UDC_DMA_IRQ_EN);
  1028. UDCREG (UDC_DMA_IRQ_EN);
  1029. UDCDBG ("initialize SYSCON1");
  1030. outw (UDC_Self_Pwr | UDC_Pullup_En, UDC_SYSCON1);
  1031. UDCREG (UDC_SYSCON1);
  1032. return 0;
  1033. }
  1034. /* Stall endpoint */
  1035. static void udc_stall_ep (unsigned int ep_addr)
  1036. {
  1037. /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
  1038. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  1039. UDCDBGA ("stall ep_addr %d", ep_addr);
  1040. /* REVISIT?
  1041. * The OMAP TRM section 14.2.4.2 says we must check that the FIFO
  1042. * is empty before halting the endpoint. The current implementation
  1043. * doesn't check that the FIFO is empty.
  1044. */
  1045. if (!ep_num) {
  1046. outw (UDC_Stall_Cmd, UDC_SYSCON2);
  1047. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1048. if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
  1049. /* we have a valid rx endpoint, so halt it */
  1050. outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
  1051. outw (UDC_Set_Halt, UDC_CTRL);
  1052. outw (ep_num, UDC_EP_NUM);
  1053. }
  1054. } else {
  1055. if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
  1056. /* we have a valid tx endpoint, so halt it */
  1057. outw (UDC_EP_Sel | UDC_EP_Dir | ep_num, UDC_EP_NUM);
  1058. outw (UDC_Set_Halt, UDC_CTRL);
  1059. outw (ep_num, UDC_EP_NUM);
  1060. }
  1061. }
  1062. }
  1063. /* Reset endpoint */
  1064. #if 0
  1065. static void udc_reset_ep (unsigned int ep_addr)
  1066. {
  1067. /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
  1068. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  1069. UDCDBGA ("reset ep_addr %d", ep_addr);
  1070. if (!ep_num) {
  1071. /* control endpoint 0 can't be reset */
  1072. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1073. UDCDBGA ("UDC_EP_RX(%d) = 0x%04x", ep_num,
  1074. inw (UDC_EP_RX (ep_num)));
  1075. if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
  1076. /* we have a valid rx endpoint, so reset it */
  1077. outw (ep_num | UDC_EP_Sel, UDC_EP_NUM);
  1078. outw (UDC_Reset_EP, UDC_CTRL);
  1079. outw (ep_num, UDC_EP_NUM);
  1080. UDCDBGA ("OUT endpoint %d reset", ep_num);
  1081. }
  1082. } else {
  1083. UDCDBGA ("UDC_EP_TX(%d) = 0x%04x", ep_num,
  1084. inw (UDC_EP_TX (ep_num)));
  1085. /* Resetting of tx endpoints seems to be causing the USB function
  1086. * module to fail, which causes problems when the driver is
  1087. * uninstalled. We'll skip resetting tx endpoints for now until
  1088. * we figure out what the problem is.
  1089. */
  1090. #if 0
  1091. if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
  1092. /* we have a valid tx endpoint, so reset it */
  1093. outw (ep_num | UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
  1094. outw (UDC_Reset_EP, UDC_CTRL);
  1095. outw (ep_num | UDC_EP_Dir, UDC_EP_NUM);
  1096. UDCDBGA ("IN endpoint %d reset", ep_num);
  1097. }
  1098. #endif
  1099. }
  1100. }
  1101. #endif
  1102. /* ************************************************************************** */
  1103. /**
  1104. * udc_check_ep - check logical endpoint
  1105. *
  1106. * Return physical endpoint number to use for this logical endpoint or zero if not valid.
  1107. */
  1108. #if 0
  1109. int udc_check_ep (int logical_endpoint, int packetsize)
  1110. {
  1111. if ((logical_endpoint == 0x80) ||
  1112. ((logical_endpoint & 0x8f) != logical_endpoint)) {
  1113. return 0;
  1114. }
  1115. switch (packetsize) {
  1116. case 8:
  1117. case 16:
  1118. case 32:
  1119. case 64:
  1120. case 128:
  1121. case 256:
  1122. case 512:
  1123. break;
  1124. default:
  1125. return 0;
  1126. }
  1127. return EP_ADDR_TO_PHYS_EP (logical_endpoint);
  1128. }
  1129. #endif
  1130. /*
  1131. * udc_setup_ep - setup endpoint
  1132. *
  1133. * Associate a physical endpoint with endpoint_instance
  1134. */
  1135. void udc_setup_ep (struct usb_device_instance *device,
  1136. unsigned int ep, struct usb_endpoint_instance *endpoint)
  1137. {
  1138. UDCDBGA ("setting up endpoint addr %x", endpoint->endpoint_address);
  1139. /* This routine gets called by bi_modinit for endpoint 0 and from
  1140. * bi_config for all of the other endpoints. bi_config gets called
  1141. * during the DEVICE_CREATE, DEVICE_CONFIGURED, and
  1142. * DEVICE_SET_INTERFACE events. We need to reconfigure the OMAP packet
  1143. * RAM after bi_config scans the selected device configuration and
  1144. * initializes the endpoint structures, but before this routine enables
  1145. * the OUT endpoint FIFOs. Since bi_config calls this routine in a
  1146. * loop for endpoints 1 through UDC_MAX_ENDPOINTS, we reconfigure our
  1147. * packet RAM here when ep==1.
  1148. * I really hate to do this here, but it seems like the API exported
  1149. * by the USB bus interface controller driver to the usbd-bi module
  1150. * isn't quite right so there is no good place to do this.
  1151. */
  1152. if (ep == 1) {
  1153. omap1510_deconfigure_device ();
  1154. omap1510_configure_device (device);
  1155. }
  1156. if (endpoint && (ep < UDC_MAX_ENDPOINTS)) {
  1157. int ep_addr = endpoint->endpoint_address;
  1158. if (!ep_addr) {
  1159. /* nothing to do for endpoint 0 */
  1160. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  1161. /* nothing to do for IN (tx) endpoints */
  1162. } else { /* OUT (rx) endpoint */
  1163. if (endpoint->rcv_packetSize) {
  1164. /*struct urb* urb = &(urb_out_array[ep&0xFF]); */
  1165. /*urb->endpoint = endpoint; */
  1166. /*urb->device = device; */
  1167. /*urb->buffer_length = sizeof(urb->buffer); */
  1168. /*endpoint->rcv_urb = urb; */
  1169. omap1510_prepare_endpoint_for_rx (ep_addr);
  1170. }
  1171. }
  1172. }
  1173. }
  1174. /**
  1175. * udc_disable_ep - disable endpoint
  1176. * @ep:
  1177. *
  1178. * Disable specified endpoint
  1179. */
  1180. #if 0
  1181. void udc_disable_ep (unsigned int ep_addr)
  1182. {
  1183. /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
  1184. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  1185. struct usb_endpoint_instance *endpoint = omap1510_find_ep (ep_addr); /*udc_device->bus->endpoint_array + ep; */
  1186. UDCDBGA ("disable ep_addr %d", ep_addr);
  1187. if (!ep_num) {
  1188. /* nothing to do for endpoint 0 */ ;
  1189. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  1190. if (endpoint->tx_packetSize) {
  1191. /* we have a valid tx endpoint */
  1192. /*usbd_flush_tx(endpoint); */
  1193. endpoint->tx_urb = NULL;
  1194. }
  1195. } else {
  1196. if (endpoint->rcv_packetSize) {
  1197. /* we have a valid rx endpoint */
  1198. /*usbd_flush_rcv(endpoint); */
  1199. endpoint->rcv_urb = NULL;
  1200. }
  1201. }
  1202. }
  1203. #endif
  1204. /* ************************************************************************** */
  1205. /**
  1206. * udc_connected - is the USB cable connected
  1207. *
  1208. * Return non-zero if cable is connected.
  1209. */
  1210. #if 0
  1211. int udc_connected (void)
  1212. {
  1213. return ((inw (UDC_DEVSTAT) & UDC_ATT) == UDC_ATT);
  1214. }
  1215. #endif
  1216. /* Turn on the USB connection by enabling the pullup resistor */
  1217. void udc_connect (void)
  1218. {
  1219. UDCDBG ("connect, enable Pullup");
  1220. outl (0x00000018, FUNC_MUX_CTRL_D);
  1221. }
  1222. /* Turn off the USB connection by disabling the pullup resistor */
  1223. void udc_disconnect (void)
  1224. {
  1225. UDCDBG ("disconnect, disable Pullup");
  1226. outl (0x00000000, FUNC_MUX_CTRL_D);
  1227. }
  1228. /* ************************************************************************** */
  1229. /*
  1230. * udc_disable_interrupts - disable interrupts
  1231. * switch off interrupts
  1232. */
  1233. #if 0
  1234. void udc_disable_interrupts (struct usb_device_instance *device)
  1235. {
  1236. UDCDBG ("disabling all interrupts");
  1237. outw (0, UDC_IRQ_EN);
  1238. }
  1239. #endif
  1240. /* ************************************************************************** */
  1241. /**
  1242. * udc_ep0_packetsize - return ep0 packetsize
  1243. */
  1244. #if 0
  1245. int udc_ep0_packetsize (void)
  1246. {
  1247. return EP0_PACKETSIZE;
  1248. }
  1249. #endif
  1250. /* Switch on the UDC */
  1251. void udc_enable (struct usb_device_instance *device)
  1252. {
  1253. UDCDBGA ("enable device %p, status %d", device, device->status);
  1254. /* initialize driver state variables */
  1255. udc_devstat = 0;
  1256. /* Save the device structure pointer */
  1257. udc_device = device;
  1258. /* Setup ep0 urb */
  1259. if (!ep0_urb) {
  1260. ep0_urb =
  1261. usbd_alloc_urb (udc_device,
  1262. udc_device->bus->endpoint_array);
  1263. } else {
  1264. serial_printf ("udc_enable: ep0_urb already allocated %p\n",
  1265. ep0_urb);
  1266. }
  1267. UDCDBG ("Check clock status");
  1268. UDCREG (STATUS_REQ);
  1269. /* The VBUS_MODE bit selects whether VBUS detection is done via
  1270. * software (1) or hardware (0). When software detection is
  1271. * selected, VBUS_CTRL selects whether USB is not connected (0)
  1272. * or connected (1).
  1273. */
  1274. outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_CTRL | UDC_VBUS_MODE,
  1275. FUNC_MUX_CTRL_0);
  1276. UDCREGL (FUNC_MUX_CTRL_0);
  1277. omap1510_configure_device (device);
  1278. }
  1279. /* Switch off the UDC */
  1280. void udc_disable (void)
  1281. {
  1282. UDCDBG ("disable UDC");
  1283. omap1510_deconfigure_device ();
  1284. /* The VBUS_MODE bit selects whether VBUS detection is done via
  1285. * software (1) or hardware (0). When software detection is
  1286. * selected, VBUS_CTRL selects whether USB is not connected (0)
  1287. * or connected (1).
  1288. */
  1289. outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
  1290. outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
  1291. UDCREGL (FUNC_MUX_CTRL_0);
  1292. /* Free ep0 URB */
  1293. if (ep0_urb) {
  1294. /*usbd_dealloc_urb(ep0_urb); */
  1295. ep0_urb = NULL;
  1296. }
  1297. /* Reset device pointer.
  1298. * We ought to do this here to balance the initialization of udc_device
  1299. * in udc_enable, but some of our other exported functions get called
  1300. * by the bus interface driver after udc_disable, so we have to hang on
  1301. * to the device pointer to avoid a null pointer dereference. */
  1302. /* udc_device = NULL; */
  1303. }
  1304. /**
  1305. * udc_startup - allow udc code to do any additional startup
  1306. */
  1307. void udc_startup_events (struct usb_device_instance *device)
  1308. {
  1309. /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
  1310. usbd_device_event_irq (device, DEVICE_INIT, 0);
  1311. /* The DEVICE_CREATE event puts the USB device in the state
  1312. * STATE_ATTACHED.
  1313. */
  1314. usbd_device_event_irq (device, DEVICE_CREATE, 0);
  1315. /* Some USB controller driver implementations signal
  1316. * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
  1317. * DEVICE_HUB_CONFIGURED causes a transition to the state STATE_POWERED,
  1318. * and DEVICE_RESET causes a transition to the state STATE_DEFAULT.
  1319. * The OMAP USB client controller has the capability to detect when the
  1320. * USB cable is connected to a powered USB bus via the ATT bit in the
  1321. * DEVSTAT register, so we will defer the DEVICE_HUB_CONFIGURED and
  1322. * DEVICE_RESET events until later.
  1323. */
  1324. udc_enable (device);
  1325. }
  1326. #endif