usbdcore_omap1510.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. /*
  2. * (C) Copyright 2003
  3. * Gerry Hamel, geh@ti.com, Texas Instruments
  4. *
  5. * Based on
  6. * linux/drivers/usb/device/bi/omap.c
  7. * TI OMAP1510 USB bus interface driver
  8. *
  9. * Author: MontaVista Software, Inc.
  10. * source@mvista.com
  11. * (C) Copyright 2002
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. #include <common.h>
  29. #if defined(CONFIG_OMAP1510) && defined(CONFIG_USB_DEVICE)
  30. #include <asm/io.h>
  31. #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. /* Set and check APLL */
  998. outw (0x0008, APLL_CTRL);
  999. UDCREG (APLL_CTRL);
  1000. /* Set and check DPLL */
  1001. outw (0x2210, DPLL_CTRL);
  1002. UDCREG (DPLL_CTRL);
  1003. /* Set and check SOFT */
  1004. outw ((1 << 4) | (1 << 3) | 1, SOFT_REQ);
  1005. /* Short delay to wait for DPLL */
  1006. udelay (1000);
  1007. /* Print banner with device revision */
  1008. udc_rev = inw (UDC_REV) & 0xff;
  1009. printf ("USB: TI OMAP1510 USB function module rev %d.%d\n",
  1010. udc_rev >> 4, udc_rev & 0xf);
  1011. #ifdef CONFIG_OMAP_SX1
  1012. i2c_read (0x32, 0x04, 1, &value, 1);
  1013. value |= 0x04;
  1014. i2c_write (0x32, 0x04, 1, &value, 1);
  1015. i2c_read (0x32, 0x03, 1, &value, 1);
  1016. value |= 0x01;
  1017. i2c_write (0x32, 0x03, 1, &value, 1);
  1018. gpio = inl(GPIO_PIN_CONTROL_REG);
  1019. gpio |= 0x0002; /* A_IRDA_OFF */
  1020. gpio |= 0x0800; /* A_SWITCH */
  1021. gpio |= 0x8000; /* A_USB_ON */
  1022. outl (gpio, GPIO_PIN_CONTROL_REG);
  1023. gpio = inl(GPIO_DIR_CONTROL_REG);
  1024. gpio &= ~0x0002; /* A_IRDA_OFF */
  1025. gpio &= ~0x0800; /* A_SWITCH */
  1026. gpio &= ~0x8000; /* A_USB_ON */
  1027. outl (gpio, GPIO_DIR_CONTROL_REG);
  1028. gpio = inl(GPIO_DATA_OUTPUT_REG);
  1029. gpio |= 0x0002; /* A_IRDA_OFF */
  1030. gpio &= ~0x0800; /* A_SWITCH */
  1031. gpio &= ~0x8000; /* A_USB_ON */
  1032. outl (gpio, GPIO_DATA_OUTPUT_REG);
  1033. #endif
  1034. /* The VBUS_MODE bit selects whether VBUS detection is done via
  1035. * software (1) or hardware (0). When software detection is
  1036. * selected, VBUS_CTRL selects whether USB is not connected (0)
  1037. * or connected (1).
  1038. */
  1039. outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
  1040. outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
  1041. UDCREGL (FUNC_MUX_CTRL_0);
  1042. /*
  1043. * At this point, device is ready for configuration...
  1044. */
  1045. UDCDBG ("disable USB interrupts");
  1046. outw (0, UDC_IRQ_EN);
  1047. UDCREG (UDC_IRQ_EN);
  1048. UDCDBG ("disable USB DMA");
  1049. outw (0, UDC_DMA_IRQ_EN);
  1050. UDCREG (UDC_DMA_IRQ_EN);
  1051. UDCDBG ("initialize SYSCON1");
  1052. outw (UDC_Self_Pwr | UDC_Pullup_En, UDC_SYSCON1);
  1053. UDCREG (UDC_SYSCON1);
  1054. return 0;
  1055. }
  1056. /* Stall endpoint */
  1057. static void udc_stall_ep (unsigned int ep_addr)
  1058. {
  1059. /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
  1060. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  1061. UDCDBGA ("stall ep_addr %d", ep_addr);
  1062. /* REVISIT?
  1063. * The OMAP TRM section 14.2.4.2 says we must check that the FIFO
  1064. * is empty before halting the endpoint. The current implementation
  1065. * doesn't check that the FIFO is empty.
  1066. */
  1067. if (!ep_num) {
  1068. outw (UDC_Stall_Cmd, UDC_SYSCON2);
  1069. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1070. if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
  1071. /* we have a valid rx endpoint, so halt it */
  1072. outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
  1073. outw (UDC_Set_Halt, UDC_CTRL);
  1074. outw (ep_num, UDC_EP_NUM);
  1075. }
  1076. } else {
  1077. if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
  1078. /* we have a valid tx endpoint, so halt it */
  1079. outw (UDC_EP_Sel | UDC_EP_Dir | ep_num, UDC_EP_NUM);
  1080. outw (UDC_Set_Halt, UDC_CTRL);
  1081. outw (ep_num, UDC_EP_NUM);
  1082. }
  1083. }
  1084. }
  1085. /* Reset endpoint */
  1086. #if 0
  1087. static void udc_reset_ep (unsigned int ep_addr)
  1088. {
  1089. /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
  1090. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  1091. UDCDBGA ("reset ep_addr %d", ep_addr);
  1092. if (!ep_num) {
  1093. /* control endpoint 0 can't be reset */
  1094. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1095. UDCDBGA ("UDC_EP_RX(%d) = 0x%04x", ep_num,
  1096. inw (UDC_EP_RX (ep_num)));
  1097. if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
  1098. /* we have a valid rx endpoint, so reset it */
  1099. outw (ep_num | UDC_EP_Sel, UDC_EP_NUM);
  1100. outw (UDC_Reset_EP, UDC_CTRL);
  1101. outw (ep_num, UDC_EP_NUM);
  1102. UDCDBGA ("OUT endpoint %d reset", ep_num);
  1103. }
  1104. } else {
  1105. UDCDBGA ("UDC_EP_TX(%d) = 0x%04x", ep_num,
  1106. inw (UDC_EP_TX (ep_num)));
  1107. /* Resetting of tx endpoints seems to be causing the USB function
  1108. * module to fail, which causes problems when the driver is
  1109. * uninstalled. We'll skip resetting tx endpoints for now until
  1110. * we figure out what the problem is.
  1111. */
  1112. #if 0
  1113. if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
  1114. /* we have a valid tx endpoint, so reset it */
  1115. outw (ep_num | UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
  1116. outw (UDC_Reset_EP, UDC_CTRL);
  1117. outw (ep_num | UDC_EP_Dir, UDC_EP_NUM);
  1118. UDCDBGA ("IN endpoint %d reset", ep_num);
  1119. }
  1120. #endif
  1121. }
  1122. }
  1123. #endif
  1124. /* ************************************************************************** */
  1125. /**
  1126. * udc_check_ep - check logical endpoint
  1127. *
  1128. * Return physical endpoint number to use for this logical endpoint or zero if not valid.
  1129. */
  1130. #if 0
  1131. int udc_check_ep (int logical_endpoint, int packetsize)
  1132. {
  1133. if ((logical_endpoint == 0x80) ||
  1134. ((logical_endpoint & 0x8f) != logical_endpoint)) {
  1135. return 0;
  1136. }
  1137. switch (packetsize) {
  1138. case 8:
  1139. case 16:
  1140. case 32:
  1141. case 64:
  1142. case 128:
  1143. case 256:
  1144. case 512:
  1145. break;
  1146. default:
  1147. return 0;
  1148. }
  1149. return EP_ADDR_TO_PHYS_EP (logical_endpoint);
  1150. }
  1151. #endif
  1152. /*
  1153. * udc_setup_ep - setup endpoint
  1154. *
  1155. * Associate a physical endpoint with endpoint_instance
  1156. */
  1157. void udc_setup_ep (struct usb_device_instance *device,
  1158. unsigned int ep, struct usb_endpoint_instance *endpoint)
  1159. {
  1160. UDCDBGA ("setting up endpoint addr %x", endpoint->endpoint_address);
  1161. /* This routine gets called by bi_modinit for endpoint 0 and from
  1162. * bi_config for all of the other endpoints. bi_config gets called
  1163. * during the DEVICE_CREATE, DEVICE_CONFIGURED, and
  1164. * DEVICE_SET_INTERFACE events. We need to reconfigure the OMAP packet
  1165. * RAM after bi_config scans the selected device configuration and
  1166. * initializes the endpoint structures, but before this routine enables
  1167. * the OUT endpoint FIFOs. Since bi_config calls this routine in a
  1168. * loop for endpoints 1 through UDC_MAX_ENDPOINTS, we reconfigure our
  1169. * packet RAM here when ep==1.
  1170. * I really hate to do this here, but it seems like the API exported
  1171. * by the USB bus interface controller driver to the usbd-bi module
  1172. * isn't quite right so there is no good place to do this.
  1173. */
  1174. if (ep == 1) {
  1175. omap1510_deconfigure_device ();
  1176. omap1510_configure_device (device);
  1177. }
  1178. if (endpoint && (ep < UDC_MAX_ENDPOINTS)) {
  1179. int ep_addr = endpoint->endpoint_address;
  1180. if (!ep_addr) {
  1181. /* nothing to do for endpoint 0 */
  1182. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  1183. /* nothing to do for IN (tx) endpoints */
  1184. } else { /* OUT (rx) endpoint */
  1185. if (endpoint->rcv_packetSize) {
  1186. /*struct urb* urb = &(urb_out_array[ep&0xFF]); */
  1187. /*urb->endpoint = endpoint; */
  1188. /*urb->device = device; */
  1189. /*urb->buffer_length = sizeof(urb->buffer); */
  1190. /*endpoint->rcv_urb = urb; */
  1191. omap1510_prepare_endpoint_for_rx (ep_addr);
  1192. }
  1193. }
  1194. }
  1195. }
  1196. /**
  1197. * udc_disable_ep - disable endpoint
  1198. * @ep:
  1199. *
  1200. * Disable specified endpoint
  1201. */
  1202. #if 0
  1203. void udc_disable_ep (unsigned int ep_addr)
  1204. {
  1205. /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
  1206. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  1207. struct usb_endpoint_instance *endpoint = omap1510_find_ep (ep_addr); /*udc_device->bus->endpoint_array + ep; */
  1208. UDCDBGA ("disable ep_addr %d", ep_addr);
  1209. if (!ep_num) {
  1210. /* nothing to do for endpoint 0 */ ;
  1211. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  1212. if (endpoint->tx_packetSize) {
  1213. /* we have a valid tx endpoint */
  1214. /*usbd_flush_tx(endpoint); */
  1215. endpoint->tx_urb = NULL;
  1216. }
  1217. } else {
  1218. if (endpoint->rcv_packetSize) {
  1219. /* we have a valid rx endpoint */
  1220. /*usbd_flush_rcv(endpoint); */
  1221. endpoint->rcv_urb = NULL;
  1222. }
  1223. }
  1224. }
  1225. #endif
  1226. /* ************************************************************************** */
  1227. /**
  1228. * udc_connected - is the USB cable connected
  1229. *
  1230. * Return non-zero if cable is connected.
  1231. */
  1232. #if 0
  1233. int udc_connected (void)
  1234. {
  1235. return ((inw (UDC_DEVSTAT) & UDC_ATT) == UDC_ATT);
  1236. }
  1237. #endif
  1238. /* Turn on the USB connection by enabling the pullup resistor */
  1239. void udc_connect (void)
  1240. {
  1241. UDCDBG ("connect, enable Pullup");
  1242. outl (0x00000018, FUNC_MUX_CTRL_D);
  1243. }
  1244. /* Turn off the USB connection by disabling the pullup resistor */
  1245. void udc_disconnect (void)
  1246. {
  1247. UDCDBG ("disconnect, disable Pullup");
  1248. outl (0x00000000, FUNC_MUX_CTRL_D);
  1249. }
  1250. /* ************************************************************************** */
  1251. /*
  1252. * udc_disable_interrupts - disable interrupts
  1253. * switch off interrupts
  1254. */
  1255. #if 0
  1256. void udc_disable_interrupts (struct usb_device_instance *device)
  1257. {
  1258. UDCDBG ("disabling all interrupts");
  1259. outw (0, UDC_IRQ_EN);
  1260. }
  1261. #endif
  1262. /* ************************************************************************** */
  1263. /**
  1264. * udc_ep0_packetsize - return ep0 packetsize
  1265. */
  1266. #if 0
  1267. int udc_ep0_packetsize (void)
  1268. {
  1269. return EP0_PACKETSIZE;
  1270. }
  1271. #endif
  1272. /* Switch on the UDC */
  1273. void udc_enable (struct usb_device_instance *device)
  1274. {
  1275. UDCDBGA ("enable device %p, status %d", device, device->status);
  1276. /* initialize driver state variables */
  1277. udc_devstat = 0;
  1278. /* Save the device structure pointer */
  1279. udc_device = device;
  1280. /* Setup ep0 urb */
  1281. if (!ep0_urb) {
  1282. ep0_urb =
  1283. usbd_alloc_urb (udc_device,
  1284. udc_device->bus->endpoint_array);
  1285. } else {
  1286. serial_printf ("udc_enable: ep0_urb already allocated %p\n",
  1287. ep0_urb);
  1288. }
  1289. UDCDBG ("Check clock status");
  1290. UDCREG (STATUS_REQ);
  1291. /* The VBUS_MODE bit selects whether VBUS detection is done via
  1292. * software (1) or hardware (0). When software detection is
  1293. * selected, VBUS_CTRL selects whether USB is not connected (0)
  1294. * or connected (1).
  1295. */
  1296. outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_CTRL | UDC_VBUS_MODE,
  1297. FUNC_MUX_CTRL_0);
  1298. UDCREGL (FUNC_MUX_CTRL_0);
  1299. omap1510_configure_device (device);
  1300. }
  1301. /* Switch off the UDC */
  1302. void udc_disable (void)
  1303. {
  1304. UDCDBG ("disable UDC");
  1305. omap1510_deconfigure_device ();
  1306. /* The VBUS_MODE bit selects whether VBUS detection is done via
  1307. * software (1) or hardware (0). When software detection is
  1308. * selected, VBUS_CTRL selects whether USB is not connected (0)
  1309. * or connected (1).
  1310. */
  1311. outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
  1312. outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
  1313. UDCREGL (FUNC_MUX_CTRL_0);
  1314. /* Free ep0 URB */
  1315. if (ep0_urb) {
  1316. /*usbd_dealloc_urb(ep0_urb); */
  1317. ep0_urb = NULL;
  1318. }
  1319. /* Reset device pointer.
  1320. * We ought to do this here to balance the initialization of udc_device
  1321. * in udc_enable, but some of our other exported functions get called
  1322. * by the bus interface driver after udc_disable, so we have to hang on
  1323. * to the device pointer to avoid a null pointer dereference. */
  1324. /* udc_device = NULL; */
  1325. }
  1326. /**
  1327. * udc_startup - allow udc code to do any additional startup
  1328. */
  1329. void udc_startup_events (struct usb_device_instance *device)
  1330. {
  1331. /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
  1332. usbd_device_event_irq (device, DEVICE_INIT, 0);
  1333. /* The DEVICE_CREATE event puts the USB device in the state
  1334. * STATE_ATTACHED.
  1335. */
  1336. usbd_device_event_irq (device, DEVICE_CREATE, 0);
  1337. /* Some USB controller driver implementations signal
  1338. * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
  1339. * DEVICE_HUB_CONFIGURED causes a transition to the state STATE_POWERED,
  1340. * and DEVICE_RESET causes a transition to the state STATE_DEFAULT.
  1341. * The OMAP USB client controller has the capability to detect when the
  1342. * USB cable is connected to a powered USB bus via the ATT bit in the
  1343. * DEVSTAT register, so we will defer the DEVICE_HUB_CONFIGURED and
  1344. * DEVICE_RESET events until later.
  1345. */
  1346. udc_enable (device);
  1347. }
  1348. #endif