usbdcore_omap1510.c 43 KB

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