omap1510_udc.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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. #include <asm/io.h>
  30. #ifdef CONFIG_OMAP_SX1
  31. #include <i2c.h>
  32. #endif
  33. #include <usbdevice.h>
  34. #include <usb/omap1510_udc.h>
  35. #include "ep0.h"
  36. #define UDC_INIT_MDELAY 80 /* Device settle delay */
  37. #define UDC_MAX_ENDPOINTS 31 /* Number of endpoints on this UDC */
  38. /* Some kind of debugging output... */
  39. #if 1
  40. #define UDCDBG(str)
  41. #define UDCDBGA(fmt,args...)
  42. #else /* The bugs still exists... */
  43. #define UDCDBG(str) serial_printf("[%s] %s:%d: " str "\n", __FILE__,__FUNCTION__,__LINE__)
  44. #define UDCDBGA(fmt,args...) serial_printf("[%s] %s:%d: " fmt "\n", __FILE__,__FUNCTION__,__LINE__, ##args)
  45. #endif
  46. #if 1
  47. #define UDCREG(name)
  48. #define UDCREGL(name)
  49. #else /* The bugs still exists... */
  50. #define UDCREG(name) serial_printf("%s():%d: %s[%08x]=%.4x\n",__FUNCTION__,__LINE__, (#name), name, inw(name)) /* For 16-bit regs */
  51. #define UDCREGL(name) serial_printf("%s():%d: %s[%08x]=%.8x\n",__FUNCTION__,__LINE__, (#name), name, inl(name)) /* For 32-bit regs */
  52. #endif
  53. static struct urb *ep0_urb = NULL;
  54. static struct usb_device_instance *udc_device; /* Used in interrupt handler */
  55. static u16 udc_devstat = 0; /* UDC status (DEVSTAT) */
  56. static u32 udc_interrupts = 0;
  57. static void udc_stall_ep (unsigned int ep_addr);
  58. static struct usb_endpoint_instance *omap1510_find_ep (int ep)
  59. {
  60. int i;
  61. for (i = 0; i < udc_device->bus->max_endpoints; i++) {
  62. if (udc_device->bus->endpoint_array[i].endpoint_address == ep)
  63. return &udc_device->bus->endpoint_array[i];
  64. }
  65. return NULL;
  66. }
  67. /* ************************************************************************** */
  68. /* IO
  69. */
  70. /*
  71. * omap1510_prepare_endpoint_for_rx
  72. *
  73. * This function implements TRM Figure 14-11.
  74. *
  75. * The endpoint to prepare for transfer is specified as a physical endpoint
  76. * number. For OUT (rx) endpoints 1 through 15, the corresponding endpoint
  77. * configuration register is checked to see if the endpoint is ISO or not.
  78. * If the OUT endpoint is valid and is non-ISO then its FIFO is enabled.
  79. * No action is taken for endpoint 0 or for IN (tx) endpoints 16 through 30.
  80. */
  81. static void omap1510_prepare_endpoint_for_rx (int ep_addr)
  82. {
  83. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  84. UDCDBGA ("omap1510_prepare_endpoint %x", ep_addr);
  85. if (((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)) {
  86. if ((inw (UDC_EP_RX (ep_num)) &
  87. (UDC_EPn_RX_Valid | UDC_EPn_RX_Iso)) ==
  88. UDC_EPn_RX_Valid) {
  89. /* rx endpoint is valid, non-ISO, so enable its FIFO */
  90. outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
  91. outw (UDC_Set_FIFO_En, UDC_CTRL);
  92. outw (0, UDC_EP_NUM);
  93. }
  94. }
  95. }
  96. /* omap1510_configure_endpoints
  97. *
  98. * This function implements TRM Figure 14-10.
  99. */
  100. static void omap1510_configure_endpoints (struct usb_device_instance *device)
  101. {
  102. int ep;
  103. struct usb_bus_instance *bus;
  104. struct usb_endpoint_instance *endpoint;
  105. unsigned short ep_ptr;
  106. unsigned short ep_size;
  107. unsigned short ep_isoc;
  108. unsigned short ep_doublebuffer;
  109. int ep_addr;
  110. int packet_size;
  111. int buffer_size;
  112. int attributes;
  113. bus = device->bus;
  114. /* There is a dedicated 2048 byte buffer for USB packets that may be
  115. * arbitrarily partitioned among the endpoints on 8-byte boundaries.
  116. * The first 8 bytes are reserved for receiving setup packets on
  117. * endpoint 0.
  118. */
  119. ep_ptr = 8; /* reserve the first 8 bytes for the setup fifo */
  120. for (ep = 0; ep < bus->max_endpoints; ep++) {
  121. endpoint = bus->endpoint_array + ep;
  122. ep_addr = endpoint->endpoint_address;
  123. if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  124. /* IN endpoint */
  125. packet_size = endpoint->tx_packetSize;
  126. attributes = endpoint->tx_attributes;
  127. } else {
  128. /* OUT endpoint */
  129. packet_size = endpoint->rcv_packetSize;
  130. attributes = endpoint->rcv_attributes;
  131. }
  132. switch (packet_size) {
  133. case 0:
  134. ep_size = 0;
  135. break;
  136. case 8:
  137. ep_size = 0;
  138. break;
  139. case 16:
  140. ep_size = 1;
  141. break;
  142. case 32:
  143. ep_size = 2;
  144. break;
  145. case 64:
  146. ep_size = 3;
  147. break;
  148. case 128:
  149. ep_size = 4;
  150. break;
  151. case 256:
  152. ep_size = 5;
  153. break;
  154. case 512:
  155. ep_size = 6;
  156. break;
  157. default:
  158. UDCDBGA ("ep 0x%02x has bad packet size %d",
  159. ep_addr, packet_size);
  160. packet_size = 0;
  161. ep_size = 0;
  162. break;
  163. }
  164. switch (attributes & USB_ENDPOINT_XFERTYPE_MASK) {
  165. case USB_ENDPOINT_XFER_CONTROL:
  166. case USB_ENDPOINT_XFER_BULK:
  167. case USB_ENDPOINT_XFER_INT:
  168. default:
  169. /* A non-isochronous endpoint may optionally be
  170. * double-buffered. For now we disable
  171. * double-buffering.
  172. */
  173. ep_doublebuffer = 0;
  174. ep_isoc = 0;
  175. if (packet_size > 64)
  176. packet_size = 0;
  177. if (!ep || !ep_doublebuffer)
  178. buffer_size = packet_size;
  179. else
  180. buffer_size = packet_size * 2;
  181. break;
  182. case USB_ENDPOINT_XFER_ISOC:
  183. /* Isochronous endpoints are always double-
  184. * buffered, but the double-buffering bit
  185. * in the endpoint configuration register
  186. * becomes the msb of the endpoint size so we
  187. * set the double-buffering flag to zero.
  188. */
  189. ep_doublebuffer = 0;
  190. ep_isoc = 1;
  191. buffer_size = packet_size * 2;
  192. break;
  193. }
  194. /* check to see if our packet buffer RAM is exhausted */
  195. if ((ep_ptr + buffer_size) > 2048) {
  196. UDCDBGA ("out of packet RAM for ep 0x%02x buf size %d", ep_addr, buffer_size);
  197. buffer_size = packet_size = 0;
  198. }
  199. /* force a default configuration for endpoint 0 since it is
  200. * always enabled
  201. */
  202. if (!ep && ((packet_size < 8) || (packet_size > 64))) {
  203. buffer_size = packet_size = 64;
  204. ep_size = 3;
  205. }
  206. if (!ep) {
  207. /* configure endpoint 0 */
  208. outw ((ep_size << 12) | (ep_ptr >> 3), UDC_EP0);
  209. /*UDCDBGA("ep 0 buffer offset 0x%03x packet size 0x%03x", */
  210. /* ep_ptr, packet_size); */
  211. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  212. /* IN endpoint */
  213. if (packet_size) {
  214. outw ((1 << 15) | (ep_doublebuffer << 14) |
  215. (ep_size << 12) | (ep_isoc << 11) |
  216. (ep_ptr >> 3),
  217. UDC_EP_TX (ep_addr &
  218. USB_ENDPOINT_NUMBER_MASK));
  219. UDCDBGA ("IN ep %d buffer offset 0x%03x"
  220. " packet size 0x%03x",
  221. ep_addr & USB_ENDPOINT_NUMBER_MASK,
  222. ep_ptr, packet_size);
  223. } else {
  224. outw (0,
  225. UDC_EP_TX (ep_addr &
  226. USB_ENDPOINT_NUMBER_MASK));
  227. }
  228. } else {
  229. /* OUT endpoint */
  230. if (packet_size) {
  231. outw ((1 << 15) | (ep_doublebuffer << 14) |
  232. (ep_size << 12) | (ep_isoc << 11) |
  233. (ep_ptr >> 3),
  234. UDC_EP_RX (ep_addr &
  235. USB_ENDPOINT_NUMBER_MASK));
  236. UDCDBGA ("OUT ep %d buffer offset 0x%03x"
  237. " packet size 0x%03x",
  238. ep_addr & USB_ENDPOINT_NUMBER_MASK,
  239. ep_ptr, packet_size);
  240. } else {
  241. outw (0,
  242. UDC_EP_RX (ep_addr &
  243. USB_ENDPOINT_NUMBER_MASK));
  244. }
  245. }
  246. ep_ptr += buffer_size;
  247. }
  248. }
  249. /* omap1510_deconfigure_device
  250. *
  251. * This function balances omap1510_configure_device.
  252. */
  253. static void omap1510_deconfigure_device (void)
  254. {
  255. int epnum;
  256. UDCDBG ("clear Cfg_Lock");
  257. outw (inw (UDC_SYSCON1) & ~UDC_Cfg_Lock, UDC_SYSCON1);
  258. UDCREG (UDC_SYSCON1);
  259. /* deconfigure all endpoints */
  260. for (epnum = 1; epnum <= 15; epnum++) {
  261. outw (0, UDC_EP_RX (epnum));
  262. outw (0, UDC_EP_TX (epnum));
  263. }
  264. }
  265. /* omap1510_configure_device
  266. *
  267. * This function implements TRM Figure 14-9.
  268. */
  269. static void omap1510_configure_device (struct usb_device_instance *device)
  270. {
  271. omap1510_configure_endpoints (device);
  272. /* Figure 14-9 indicates we should enable interrupts here, but we have
  273. * other routines (udc_all_interrupts, udc_suspended_interrupts) to
  274. * do that.
  275. */
  276. UDCDBG ("set Cfg_Lock");
  277. outw (inw (UDC_SYSCON1) | UDC_Cfg_Lock, UDC_SYSCON1);
  278. UDCREG (UDC_SYSCON1);
  279. }
  280. /* omap1510_write_noniso_tx_fifo
  281. *
  282. * This function implements TRM Figure 14-30.
  283. *
  284. * If the endpoint has an active tx_urb, then the next packet of data from the
  285. * URB is written to the tx FIFO. The total amount of data in the urb is given
  286. * by urb->actual_length. The maximum amount of data that can be sent in any
  287. * one packet is given by endpoint->tx_packetSize. The number of data bytes
  288. * from this URB that have already been transmitted is given by endpoint->sent.
  289. * endpoint->last is updated by this routine with the number of data bytes
  290. * transmitted in this packet.
  291. *
  292. * In accordance with Figure 14-30, the EP_NUM register must already have been
  293. * written with the value to select the appropriate tx FIFO before this routine
  294. * is called.
  295. */
  296. static void omap1510_write_noniso_tx_fifo (struct usb_endpoint_instance
  297. *endpoint)
  298. {
  299. struct urb *urb = endpoint->tx_urb;
  300. if (urb) {
  301. unsigned int last, i;
  302. UDCDBGA ("urb->buffer %p, buffer_length %d, actual_length %d",
  303. urb->buffer, urb->buffer_length, urb->actual_length);
  304. if ((last =
  305. MIN (urb->actual_length - endpoint->sent,
  306. endpoint->tx_packetSize))) {
  307. u8 *cp = urb->buffer + endpoint->sent;
  308. UDCDBGA ("endpoint->sent %d, tx_packetSize %d, last %d", endpoint->sent, endpoint->tx_packetSize, last);
  309. if (((u32) cp & 1) == 0) { /* word aligned? */
  310. outsw (UDC_DATA, cp, last >> 1);
  311. } else { /* byte aligned. */
  312. for (i = 0; i < (last >> 1); i++) {
  313. u16 w = ((u16) cp[2 * i + 1] << 8) |
  314. (u16) cp[2 * i];
  315. outw (w, UDC_DATA);
  316. }
  317. }
  318. if (last & 1) {
  319. outb (*(cp + last - 1), UDC_DATA);
  320. }
  321. }
  322. endpoint->last = last;
  323. }
  324. }
  325. /* omap1510_read_noniso_rx_fifo
  326. *
  327. * This function implements TRM Figure 14-28.
  328. *
  329. * If the endpoint has an active rcv_urb, then the next packet of data is read
  330. * from the rcv FIFO and written to rcv_urb->buffer at offset
  331. * rcv_urb->actual_length to append the packet data to the data from any
  332. * previous packets for this transfer. We assume that there is sufficient room
  333. * left in the buffer to hold an entire packet of data.
  334. *
  335. * The return value is the number of bytes read from the FIFO for this packet.
  336. *
  337. * In accordance with Figure 14-28, the EP_NUM register must already have been
  338. * written with the value to select the appropriate rcv FIFO before this routine
  339. * is called.
  340. */
  341. static int omap1510_read_noniso_rx_fifo (struct usb_endpoint_instance
  342. *endpoint)
  343. {
  344. struct urb *urb = endpoint->rcv_urb;
  345. int len = 0;
  346. if (urb) {
  347. len = inw (UDC_RXFSTAT);
  348. if (len) {
  349. unsigned char *cp = urb->buffer + urb->actual_length;
  350. insw (UDC_DATA, cp, len >> 1);
  351. if (len & 1)
  352. *(cp + len - 1) = inb (UDC_DATA);
  353. }
  354. }
  355. return len;
  356. }
  357. /* omap1510_prepare_for_control_write_status
  358. *
  359. * This function implements TRM Figure 14-17.
  360. *
  361. * We have to deal here with non-autodecoded control writes that haven't already
  362. * been dealt with by ep0_recv_setup. The non-autodecoded standard control
  363. * write requests are: set/clear endpoint feature, set configuration, set
  364. * interface, and set descriptor. ep0_recv_setup handles set/clear requests for
  365. * ENDPOINT_HALT by halting the endpoint for a set request and resetting the
  366. * endpoint for a clear request. ep0_recv_setup returns an error for
  367. * SET_DESCRIPTOR requests which causes them to be terminated with a stall by
  368. * the setup handler. A SET_INTERFACE request is handled by ep0_recv_setup by
  369. * generating a DEVICE_SET_INTERFACE event. This leaves only the
  370. * SET_CONFIGURATION event for us to deal with here.
  371. *
  372. */
  373. static void omap1510_prepare_for_control_write_status (struct urb *urb)
  374. {
  375. struct usb_device_request *request = &urb->device_request;;
  376. /* check for a SET_CONFIGURATION request */
  377. if (request->bRequest == USB_REQ_SET_CONFIGURATION) {
  378. int configuration = le16_to_cpu (request->wValue) & 0xff;
  379. unsigned short devstat = inw (UDC_DEVSTAT);
  380. if ((devstat & (UDC_ADD | UDC_CFG)) == UDC_ADD) {
  381. /* device is currently in ADDRESSED state */
  382. if (configuration) {
  383. /* Assume the specified non-zero configuration
  384. * value is valid and switch to the CONFIGURED
  385. * state.
  386. */
  387. outw (UDC_Dev_Cfg, UDC_SYSCON2);
  388. }
  389. } else if ((devstat & UDC_CFG) == UDC_CFG) {
  390. /* device is currently in CONFIGURED state */
  391. if (!configuration) {
  392. /* Switch to ADDRESSED state. */
  393. outw (UDC_Clr_Cfg, UDC_SYSCON2);
  394. }
  395. }
  396. }
  397. /* select EP0 tx FIFO */
  398. outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
  399. /* clear endpoint (no data bytes in status stage) */
  400. outw (UDC_Clr_EP, UDC_CTRL);
  401. /* enable the EP0 tx FIFO */
  402. outw (UDC_Set_FIFO_En, UDC_CTRL);
  403. /* deselect the endpoint */
  404. outw (UDC_EP_Dir, UDC_EP_NUM);
  405. }
  406. /* udc_state_transition_up
  407. * udc_state_transition_down
  408. *
  409. * Helper functions to implement device state changes. The device states and
  410. * the events that transition between them are:
  411. *
  412. * STATE_ATTACHED
  413. * || /\
  414. * \/ ||
  415. * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
  416. * || /\
  417. * \/ ||
  418. * STATE_POWERED
  419. * || /\
  420. * \/ ||
  421. * DEVICE_RESET DEVICE_POWER_INTERRUPTION
  422. * || /\
  423. * \/ ||
  424. * STATE_DEFAULT
  425. * || /\
  426. * \/ ||
  427. * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
  428. * || /\
  429. * \/ ||
  430. * STATE_ADDRESSED
  431. * || /\
  432. * \/ ||
  433. * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
  434. * || /\
  435. * \/ ||
  436. * STATE_CONFIGURED
  437. *
  438. * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED
  439. * to STATE_CONFIGURED) from the specified initial state to the specified final
  440. * state, passing through each intermediate state on the way. If the initial
  441. * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
  442. * no state transitions will take place.
  443. *
  444. * udc_state_transition_down transitions down (in the direction from
  445. * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
  446. * specified final state, passing through each intermediate state on the way.
  447. * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
  448. * state, then no state transitions will take place.
  449. *
  450. * These functions must only be called with interrupts disabled.
  451. */
  452. static void udc_state_transition_up (usb_device_state_t initial,
  453. usb_device_state_t final)
  454. {
  455. if (initial < final) {
  456. switch (initial) {
  457. case STATE_ATTACHED:
  458. usbd_device_event_irq (udc_device,
  459. DEVICE_HUB_CONFIGURED, 0);
  460. if (final == STATE_POWERED)
  461. break;
  462. case STATE_POWERED:
  463. usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
  464. if (final == STATE_DEFAULT)
  465. break;
  466. case STATE_DEFAULT:
  467. usbd_device_event_irq (udc_device,
  468. DEVICE_ADDRESS_ASSIGNED, 0);
  469. if (final == STATE_ADDRESSED)
  470. break;
  471. case STATE_ADDRESSED:
  472. usbd_device_event_irq (udc_device, DEVICE_CONFIGURED,
  473. 0);
  474. case STATE_CONFIGURED:
  475. break;
  476. default:
  477. break;
  478. }
  479. }
  480. }
  481. static void udc_state_transition_down (usb_device_state_t initial,
  482. usb_device_state_t final)
  483. {
  484. if (initial > final) {
  485. switch (initial) {
  486. case STATE_CONFIGURED:
  487. usbd_device_event_irq (udc_device, DEVICE_DE_CONFIGURED, 0);
  488. if (final == STATE_ADDRESSED)
  489. break;
  490. case STATE_ADDRESSED:
  491. usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
  492. if (final == STATE_DEFAULT)
  493. break;
  494. case STATE_DEFAULT:
  495. usbd_device_event_irq (udc_device, DEVICE_POWER_INTERRUPTION, 0);
  496. if (final == STATE_POWERED)
  497. break;
  498. case STATE_POWERED:
  499. usbd_device_event_irq (udc_device, DEVICE_HUB_RESET, 0);
  500. case STATE_ATTACHED:
  501. break;
  502. default:
  503. break;
  504. }
  505. }
  506. }
  507. /* Handle all device state changes.
  508. * This function implements TRM Figure 14-21.
  509. */
  510. static void omap1510_udc_state_changed (void)
  511. {
  512. u16 bits;
  513. u16 devstat = inw (UDC_DEVSTAT);
  514. UDCDBGA ("state changed, devstat %x, old %x", devstat, udc_devstat);
  515. bits = devstat ^ udc_devstat;
  516. if (bits) {
  517. if (bits & UDC_ATT) {
  518. if (devstat & UDC_ATT) {
  519. UDCDBG ("device attached and powered");
  520. udc_state_transition_up (udc_device->device_state, STATE_POWERED);
  521. } else {
  522. UDCDBG ("device detached or unpowered");
  523. udc_state_transition_down (udc_device->device_state, STATE_ATTACHED);
  524. }
  525. }
  526. if (bits & UDC_USB_Reset) {
  527. if (devstat & UDC_USB_Reset) {
  528. UDCDBG ("device reset in progess");
  529. udc_state_transition_down (udc_device->device_state, STATE_POWERED);
  530. } else {
  531. UDCDBG ("device reset completed");
  532. }
  533. }
  534. if (bits & UDC_DEF) {
  535. if (devstat & UDC_DEF) {
  536. UDCDBG ("device entering default state");
  537. udc_state_transition_up (udc_device->device_state, STATE_DEFAULT);
  538. } else {
  539. UDCDBG ("device leaving default state");
  540. udc_state_transition_down (udc_device->device_state, STATE_POWERED);
  541. }
  542. }
  543. if (bits & UDC_SUS) {
  544. if (devstat & UDC_SUS) {
  545. UDCDBG ("entering suspended state");
  546. usbd_device_event_irq (udc_device, DEVICE_BUS_INACTIVE, 0);
  547. } else {
  548. UDCDBG ("leaving suspended state");
  549. usbd_device_event_irq (udc_device, DEVICE_BUS_ACTIVITY, 0);
  550. }
  551. }
  552. if (bits & UDC_R_WK_OK) {
  553. UDCDBGA ("remote wakeup %s", (devstat & UDC_R_WK_OK)
  554. ? "enabled" : "disabled");
  555. }
  556. if (bits & UDC_ADD) {
  557. if (devstat & UDC_ADD) {
  558. UDCDBG ("default -> addressed");
  559. udc_state_transition_up (udc_device->device_state, STATE_ADDRESSED);
  560. } else {
  561. UDCDBG ("addressed -> default");
  562. udc_state_transition_down (udc_device->device_state, STATE_DEFAULT);
  563. }
  564. }
  565. if (bits & UDC_CFG) {
  566. if (devstat & UDC_CFG) {
  567. UDCDBG ("device configured");
  568. /* The ep0_recv_setup function generates the
  569. * DEVICE_CONFIGURED event when a
  570. * USB_REQ_SET_CONFIGURATION setup packet is
  571. * received, so we should already be in the
  572. * state STATE_CONFIGURED.
  573. */
  574. udc_state_transition_up (udc_device->device_state, STATE_CONFIGURED);
  575. } else {
  576. UDCDBG ("device deconfigured");
  577. udc_state_transition_down (udc_device->device_state, STATE_ADDRESSED);
  578. }
  579. }
  580. }
  581. /* Clear interrupt source */
  582. outw (UDC_DS_Chg, UDC_IRQ_SRC);
  583. /* Save current DEVSTAT */
  584. udc_devstat = devstat;
  585. }
  586. /* Handle SETUP USB interrupt.
  587. * This function implements TRM Figure 14-14.
  588. */
  589. static void omap1510_udc_setup (struct usb_endpoint_instance *endpoint)
  590. {
  591. UDCDBG ("-> Entering device setup");
  592. do {
  593. const int setup_pktsize = 8;
  594. unsigned char *datap =
  595. (unsigned char *) &ep0_urb->device_request;
  596. /* Gain access to EP 0 setup FIFO */
  597. outw (UDC_Setup_Sel, UDC_EP_NUM);
  598. /* Read control request data */
  599. insb (UDC_DATA, datap, setup_pktsize);
  600. UDCDBGA ("EP0 setup read [%x %x %x %x %x %x %x %x]",
  601. *(datap + 0), *(datap + 1), *(datap + 2),
  602. *(datap + 3), *(datap + 4), *(datap + 5),
  603. *(datap + 6), *(datap + 7));
  604. /* Reset EP0 setup FIFO */
  605. outw (0, UDC_EP_NUM);
  606. } while (inw (UDC_IRQ_SRC) & UDC_Setup);
  607. /* Try to process setup packet */
  608. if (ep0_recv_setup (ep0_urb)) {
  609. /* Not a setup packet, stall next EP0 transaction */
  610. udc_stall_ep (0);
  611. UDCDBG ("can't parse setup packet, still waiting for setup");
  612. return;
  613. }
  614. /* Check direction */
  615. if ((ep0_urb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
  616. == USB_REQ_HOST2DEVICE) {
  617. UDCDBG ("control write on EP0");
  618. if (le16_to_cpu (ep0_urb->device_request.wLength)) {
  619. /* We don't support control write data stages.
  620. * The only standard control write request with a data
  621. * stage is SET_DESCRIPTOR, and ep0_recv_setup doesn't
  622. * support that so we just stall those requests. A
  623. * function driver might support a non-standard
  624. * write request with a data stage, but it isn't
  625. * obvious what we would do with the data if we read it
  626. * so we'll just stall it. It seems like the API isn't
  627. * quite right here.
  628. */
  629. #if 0
  630. /* Here is what we would do if we did support control
  631. * write data stages.
  632. */
  633. ep0_urb->actual_length = 0;
  634. outw (0, UDC_EP_NUM);
  635. /* enable the EP0 rx FIFO */
  636. outw (UDC_Set_FIFO_En, UDC_CTRL);
  637. #else
  638. /* Stall this request */
  639. UDCDBG ("Stalling unsupported EP0 control write data "
  640. "stage.");
  641. udc_stall_ep (0);
  642. #endif
  643. } else {
  644. omap1510_prepare_for_control_write_status (ep0_urb);
  645. }
  646. } else {
  647. UDCDBG ("control read on EP0");
  648. /* The ep0_recv_setup function has already placed our response
  649. * packet data in ep0_urb->buffer and the packet length in
  650. * ep0_urb->actual_length.
  651. */
  652. endpoint->tx_urb = ep0_urb;
  653. endpoint->sent = 0;
  654. /* select the EP0 tx FIFO */
  655. outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
  656. /* Write packet data to the FIFO. omap1510_write_noniso_tx_fifo
  657. * will update endpoint->last with the number of bytes written
  658. * to the FIFO.
  659. */
  660. omap1510_write_noniso_tx_fifo (endpoint);
  661. /* enable the FIFO to start the packet transmission */
  662. outw (UDC_Set_FIFO_En, UDC_CTRL);
  663. /* deselect the EP0 tx FIFO */
  664. outw (UDC_EP_Dir, UDC_EP_NUM);
  665. }
  666. UDCDBG ("<- Leaving device setup");
  667. }
  668. /* Handle endpoint 0 RX interrupt
  669. * This routine implements TRM Figure 14-16.
  670. */
  671. static void omap1510_udc_ep0_rx (struct usb_endpoint_instance *endpoint)
  672. {
  673. unsigned short status;
  674. UDCDBG ("RX on EP0");
  675. /* select EP0 rx FIFO */
  676. outw (UDC_EP_Sel, UDC_EP_NUM);
  677. status = inw (UDC_STAT_FLG);
  678. if (status & UDC_ACK) {
  679. /* Check direction */
  680. if ((ep0_urb->device_request.bmRequestType
  681. & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
  682. /* This rx interrupt must be for a control write data
  683. * stage packet.
  684. *
  685. * We don't support control write data stages.
  686. * We should never end up here.
  687. */
  688. /* clear the EP0 rx FIFO */
  689. outw (UDC_Clr_EP, UDC_CTRL);
  690. /* deselect the EP0 rx FIFO */
  691. outw (0, UDC_EP_NUM);
  692. UDCDBG ("Stalling unexpected EP0 control write "
  693. "data stage packet");
  694. udc_stall_ep (0);
  695. } else {
  696. /* This rx interrupt must be for a control read status
  697. * stage packet.
  698. */
  699. UDCDBG ("ACK on EP0 control read status stage packet");
  700. /* deselect EP0 rx FIFO */
  701. outw (0, UDC_EP_NUM);
  702. }
  703. } else if (status & UDC_STALL) {
  704. UDCDBG ("EP0 stall during RX");
  705. /* deselect EP0 rx FIFO */
  706. outw (0, UDC_EP_NUM);
  707. } else {
  708. /* deselect EP0 rx FIFO */
  709. outw (0, UDC_EP_NUM);
  710. }
  711. }
  712. /* Handle endpoint 0 TX interrupt
  713. * This routine implements TRM Figure 14-18.
  714. */
  715. static void omap1510_udc_ep0_tx (struct usb_endpoint_instance *endpoint)
  716. {
  717. unsigned short status;
  718. struct usb_device_request *request = &ep0_urb->device_request;
  719. UDCDBG ("TX on EP0");
  720. /* select EP0 TX FIFO */
  721. outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
  722. status = inw (UDC_STAT_FLG);
  723. if (status & UDC_ACK) {
  724. /* Check direction */
  725. if ((request->bmRequestType & USB_REQ_DIRECTION_MASK) ==
  726. USB_REQ_HOST2DEVICE) {
  727. /* This tx interrupt must be for a control write status
  728. * stage packet.
  729. */
  730. UDCDBG ("ACK on EP0 control write status stage packet");
  731. /* deselect EP0 TX FIFO */
  732. outw (UDC_EP_Dir, UDC_EP_NUM);
  733. } else {
  734. /* This tx interrupt must be for a control read data
  735. * stage packet.
  736. */
  737. int wLength = le16_to_cpu (request->wLength);
  738. /* Update our count of bytes sent so far in this
  739. * transfer.
  740. */
  741. endpoint->sent += endpoint->last;
  742. /* We are finished with this transfer if we have sent
  743. * all of the bytes in our tx urb (urb->actual_length)
  744. * unless we need a zero-length terminating packet. We
  745. * need a zero-length terminating packet if we returned
  746. * fewer bytes than were requested (wLength) by the host,
  747. * and the number of bytes we returned is an exact
  748. * multiple of the packet size endpoint->tx_packetSize.
  749. */
  750. if ((endpoint->sent == ep0_urb->actual_length)
  751. && ((ep0_urb->actual_length == wLength)
  752. || (endpoint->last !=
  753. endpoint->tx_packetSize))) {
  754. /* Done with control read data stage. */
  755. UDCDBG ("control read data stage complete");
  756. /* deselect EP0 TX FIFO */
  757. outw (UDC_EP_Dir, UDC_EP_NUM);
  758. /* select EP0 RX FIFO to prepare for control
  759. * read status stage.
  760. */
  761. outw (UDC_EP_Sel, UDC_EP_NUM);
  762. /* clear the EP0 RX FIFO */
  763. outw (UDC_Clr_EP, UDC_CTRL);
  764. /* enable the EP0 RX FIFO */
  765. outw (UDC_Set_FIFO_En, UDC_CTRL);
  766. /* deselect the EP0 RX FIFO */
  767. outw (0, UDC_EP_NUM);
  768. } else {
  769. /* We still have another packet of data to send
  770. * in this control read data stage or else we
  771. * need a zero-length terminating packet.
  772. */
  773. UDCDBG ("ACK control read data stage packet");
  774. omap1510_write_noniso_tx_fifo (endpoint);
  775. /* enable the EP0 tx FIFO to start transmission */
  776. outw (UDC_Set_FIFO_En, UDC_CTRL);
  777. /* deselect EP0 TX FIFO */
  778. outw (UDC_EP_Dir, UDC_EP_NUM);
  779. }
  780. }
  781. } else if (status & UDC_STALL) {
  782. UDCDBG ("EP0 stall during TX");
  783. /* deselect EP0 TX FIFO */
  784. outw (UDC_EP_Dir, UDC_EP_NUM);
  785. } else {
  786. /* deselect EP0 TX FIFO */
  787. outw (UDC_EP_Dir, UDC_EP_NUM);
  788. }
  789. }
  790. /* Handle RX transaction on non-ISO endpoint.
  791. * This function implements TRM Figure 14-27.
  792. * The ep argument is a physical endpoint number for a non-ISO OUT endpoint
  793. * in the range 1 to 15.
  794. */
  795. static void omap1510_udc_epn_rx (int ep)
  796. {
  797. unsigned short status;
  798. /* Check endpoint status */
  799. status = inw (UDC_STAT_FLG);
  800. if (status & UDC_ACK) {
  801. int nbytes;
  802. struct usb_endpoint_instance *endpoint =
  803. omap1510_find_ep (ep);
  804. nbytes = omap1510_read_noniso_rx_fifo (endpoint);
  805. usbd_rcv_complete (endpoint, nbytes, 0);
  806. /* enable rx FIFO to prepare for next packet */
  807. outw (UDC_Set_FIFO_En, UDC_CTRL);
  808. } else if (status & UDC_STALL) {
  809. UDCDBGA ("STALL on RX endpoint %d", ep);
  810. } else if (status & UDC_NAK) {
  811. UDCDBGA ("NAK on RX ep %d", ep);
  812. } else {
  813. serial_printf ("omap-bi: RX on ep %d with status %x", ep,
  814. status);
  815. }
  816. }
  817. /* Handle TX transaction on non-ISO endpoint.
  818. * This function implements TRM Figure 14-29.
  819. * The ep argument is a physical endpoint number for a non-ISO IN endpoint
  820. * in the range 16 to 30.
  821. */
  822. static void omap1510_udc_epn_tx (int ep)
  823. {
  824. unsigned short status;
  825. /*serial_printf("omap1510_udc_epn_tx( %x )\n",ep); */
  826. /* Check endpoint status */
  827. status = inw (UDC_STAT_FLG);
  828. if (status & UDC_ACK) {
  829. struct usb_endpoint_instance *endpoint =
  830. omap1510_find_ep (ep);
  831. /* We need to transmit a terminating zero-length packet now if
  832. * we have sent all of the data in this URB and the transfer
  833. * size was an exact multiple of the packet size.
  834. */
  835. if (endpoint->tx_urb
  836. && (endpoint->last == endpoint->tx_packetSize)
  837. && (endpoint->tx_urb->actual_length - endpoint->sent -
  838. endpoint->last == 0)) {
  839. /* Prepare to transmit a zero-length packet. */
  840. endpoint->sent += endpoint->last;
  841. /* write 0 bytes of data to FIFO */
  842. omap1510_write_noniso_tx_fifo (endpoint);
  843. /* enable tx FIFO to start transmission */
  844. outw (UDC_Set_FIFO_En, UDC_CTRL);
  845. } else if (endpoint->tx_urb
  846. && endpoint->tx_urb->actual_length) {
  847. /* retire the data that was just sent */
  848. usbd_tx_complete (endpoint);
  849. /* Check to see if we have more data ready to transmit
  850. * now.
  851. */
  852. if (endpoint->tx_urb
  853. && endpoint->tx_urb->actual_length) {
  854. /* write data to FIFO */
  855. omap1510_write_noniso_tx_fifo (endpoint);
  856. /* enable tx FIFO to start transmission */
  857. outw (UDC_Set_FIFO_En, UDC_CTRL);
  858. }
  859. }
  860. } else if (status & UDC_STALL) {
  861. UDCDBGA ("STALL on TX endpoint %d", ep);
  862. } else if (status & UDC_NAK) {
  863. UDCDBGA ("NAK on TX endpoint %d", ep);
  864. } else {
  865. /*serial_printf("omap-bi: TX on ep %d with status %x\n", ep, status); */
  866. }
  867. }
  868. /*
  869. -------------------------------------------------------------------------------
  870. */
  871. /* Handle general USB interrupts and dispatch according to type.
  872. * This function implements TRM Figure 14-13.
  873. */
  874. void omap1510_udc_irq (void)
  875. {
  876. u16 irq_src = inw (UDC_IRQ_SRC);
  877. int valid_irq = 0;
  878. if (!(irq_src & ~UDC_SOF_Flg)) /* ignore SOF interrupts ) */
  879. return;
  880. UDCDBGA ("< IRQ #%d start >- %x", udc_interrupts, irq_src);
  881. /*serial_printf("< IRQ #%d start >- %x\n", udc_interrupts, irq_src); */
  882. if (irq_src & UDC_DS_Chg) {
  883. /* Device status changed */
  884. omap1510_udc_state_changed ();
  885. valid_irq++;
  886. }
  887. if (irq_src & UDC_EP0_RX) {
  888. /* Endpoint 0 receive */
  889. outw (UDC_EP0_RX, UDC_IRQ_SRC); /* ack interrupt */
  890. omap1510_udc_ep0_rx (udc_device->bus->endpoint_array + 0);
  891. valid_irq++;
  892. }
  893. if (irq_src & UDC_EP0_TX) {
  894. /* Endpoint 0 transmit */
  895. outw (UDC_EP0_TX, UDC_IRQ_SRC); /* ack interrupt */
  896. omap1510_udc_ep0_tx (udc_device->bus->endpoint_array + 0);
  897. valid_irq++;
  898. }
  899. if (irq_src & UDC_Setup) {
  900. /* Device setup */
  901. omap1510_udc_setup (udc_device->bus->endpoint_array + 0);
  902. valid_irq++;
  903. }
  904. /*if (!valid_irq) */
  905. /* serial_printf("unknown interrupt, IRQ_SRC %.4x\n", irq_src); */
  906. UDCDBGA ("< IRQ #%d end >", udc_interrupts);
  907. udc_interrupts++;
  908. }
  909. /* This function implements TRM Figure 14-26. */
  910. void omap1510_udc_noniso_irq (void)
  911. {
  912. unsigned short epnum;
  913. unsigned short irq_src = inw (UDC_IRQ_SRC);
  914. int valid_irq = 0;
  915. if (!(irq_src & (UDC_EPn_RX | UDC_EPn_TX)))
  916. return;
  917. UDCDBGA ("non-ISO IRQ, IRQ_SRC %x", inw (UDC_IRQ_SRC));
  918. if (irq_src & UDC_EPn_RX) { /* Endpoint N OUT transaction */
  919. /* Determine the endpoint number for this interrupt */
  920. epnum = (inw (UDC_EPN_STAT) & 0x0f00) >> 8;
  921. UDCDBGA ("RX on ep %x", epnum);
  922. /* acknowledge interrupt */
  923. outw (UDC_EPn_RX, UDC_IRQ_SRC);
  924. if (epnum) {
  925. /* select the endpoint FIFO */
  926. outw (UDC_EP_Sel | epnum, UDC_EP_NUM);
  927. omap1510_udc_epn_rx (epnum);
  928. /* deselect the endpoint FIFO */
  929. outw (epnum, UDC_EP_NUM);
  930. }
  931. valid_irq++;
  932. }
  933. if (irq_src & UDC_EPn_TX) { /* Endpoint N IN transaction */
  934. /* Determine the endpoint number for this interrupt */
  935. epnum = (inw (UDC_EPN_STAT) & 0x000f) | USB_DIR_IN;
  936. UDCDBGA ("TX on ep %x", epnum);
  937. /* acknowledge interrupt */
  938. outw (UDC_EPn_TX, UDC_IRQ_SRC);
  939. if (epnum) {
  940. /* select the endpoint FIFO */
  941. outw (UDC_EP_Sel | UDC_EP_Dir | epnum, UDC_EP_NUM);
  942. omap1510_udc_epn_tx (epnum);
  943. /* deselect the endpoint FIFO */
  944. outw (UDC_EP_Dir | epnum, UDC_EP_NUM);
  945. }
  946. valid_irq++;
  947. }
  948. if (!valid_irq)
  949. serial_printf (": unknown non-ISO interrupt, IRQ_SRC %.4x\n",
  950. irq_src);
  951. }
  952. /*
  953. -------------------------------------------------------------------------------
  954. */
  955. /*
  956. * Start of public functions.
  957. */
  958. /* Called to start packet transmission. */
  959. int udc_endpoint_write (struct usb_endpoint_instance *endpoint)
  960. {
  961. unsigned short epnum =
  962. endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
  963. UDCDBGA ("Starting transmit on ep %x", epnum);
  964. if (endpoint->tx_urb) {
  965. /* select the endpoint FIFO */
  966. outw (UDC_EP_Sel | UDC_EP_Dir | epnum, UDC_EP_NUM);
  967. /* write data to FIFO */
  968. omap1510_write_noniso_tx_fifo (endpoint);
  969. /* enable tx FIFO to start transmission */
  970. outw (UDC_Set_FIFO_En, UDC_CTRL);
  971. /* deselect the endpoint FIFO */
  972. outw (UDC_EP_Dir | epnum, UDC_EP_NUM);
  973. }
  974. return 0;
  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. }