usbdcore_omap1510.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  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 "usbdcore.h"
  34. #include "usbdcore_omap1510.h"
  35. #include "usbdcore_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. void 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. }
  975. /* Start to initialize h/w stuff */
  976. int udc_init (void)
  977. {
  978. u16 udc_rev;
  979. uchar value;
  980. ulong gpio;
  981. int i;
  982. /* Let the device settle down before we start */
  983. for (i = 0; i < UDC_INIT_MDELAY; i++) udelay(1000);
  984. udc_device = NULL;
  985. UDCDBG ("starting");
  986. /* Check peripheral reset. Must be 1 to make sure
  987. MPU TIPB peripheral reset is inactive */
  988. UDCREG (ARM_RSTCT2);
  989. /* Set and check clock control.
  990. * We might ought to be using the clock control API to do
  991. * this instead of fiddling with the clock registers directly
  992. * here.
  993. */
  994. outw ((1 << 4) | (1 << 5), CLOCK_CTRL);
  995. UDCREG (CLOCK_CTRL);
  996. #ifdef CONFIG_OMAP1510
  997. /* This code was originally implemented for OMAP1510 and
  998. * therefore is only applicable for OMAP1510 boards. For
  999. * OMAP5912 or OMAP16xx the register APLL_CTRL does not
  1000. * exist and DPLL_CTRL is already configured.
  1001. */
  1002. /* Set and check APLL */
  1003. outw (0x0008, APLL_CTRL);
  1004. UDCREG (APLL_CTRL);
  1005. /* Set and check DPLL */
  1006. outw (0x2210, DPLL_CTRL);
  1007. UDCREG (DPLL_CTRL);
  1008. #endif
  1009. /* Set and check SOFT
  1010. * The below line of code has been changed to perform a
  1011. * read-modify-write instead of a simple write for
  1012. * configuring the SOFT_REQ register. This allows the code
  1013. * to be compatible with OMAP5912 and OMAP16xx devices
  1014. */
  1015. outw ((1 << 4) | (1 << 3) | 1 | (inw(SOFT_REQ)), SOFT_REQ);
  1016. /* Short delay to wait for DPLL */
  1017. udelay (1000);
  1018. /* Print banner with device revision */
  1019. udc_rev = inw (UDC_REV) & 0xff;
  1020. #ifdef CONFIG_OMAP1510
  1021. printf ("USB: TI OMAP1510 USB function module rev %d.%d\n",
  1022. udc_rev >> 4, udc_rev & 0xf);
  1023. #endif
  1024. #ifdef CONFIG_OMAP1610
  1025. printf ("USB: TI OMAP5912 USB function module rev %d.%d\n",
  1026. udc_rev >> 4, udc_rev & 0xf);
  1027. #endif
  1028. #ifdef CONFIG_OMAP_SX1
  1029. i2c_read (0x32, 0x04, 1, &value, 1);
  1030. value |= 0x04;
  1031. i2c_write (0x32, 0x04, 1, &value, 1);
  1032. i2c_read (0x32, 0x03, 1, &value, 1);
  1033. value |= 0x01;
  1034. i2c_write (0x32, 0x03, 1, &value, 1);
  1035. gpio = inl(GPIO_PIN_CONTROL_REG);
  1036. gpio |= 0x0002; /* A_IRDA_OFF */
  1037. gpio |= 0x0800; /* A_SWITCH */
  1038. gpio |= 0x8000; /* A_USB_ON */
  1039. outl (gpio, GPIO_PIN_CONTROL_REG);
  1040. gpio = inl(GPIO_DIR_CONTROL_REG);
  1041. gpio &= ~0x0002; /* A_IRDA_OFF */
  1042. gpio &= ~0x0800; /* A_SWITCH */
  1043. gpio &= ~0x8000; /* A_USB_ON */
  1044. outl (gpio, GPIO_DIR_CONTROL_REG);
  1045. gpio = inl(GPIO_DATA_OUTPUT_REG);
  1046. gpio |= 0x0002; /* A_IRDA_OFF */
  1047. gpio &= ~0x0800; /* A_SWITCH */
  1048. gpio &= ~0x8000; /* A_USB_ON */
  1049. outl (gpio, GPIO_DATA_OUTPUT_REG);
  1050. #endif
  1051. /* The VBUS_MODE bit selects whether VBUS detection is done via
  1052. * software (1) or hardware (0). When software detection is
  1053. * selected, VBUS_CTRL selects whether USB is not connected (0)
  1054. * or connected (1).
  1055. */
  1056. outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
  1057. outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
  1058. UDCREGL (FUNC_MUX_CTRL_0);
  1059. /*
  1060. * At this point, device is ready for configuration...
  1061. */
  1062. UDCDBG ("disable USB interrupts");
  1063. outw (0, UDC_IRQ_EN);
  1064. UDCREG (UDC_IRQ_EN);
  1065. UDCDBG ("disable USB DMA");
  1066. outw (0, UDC_DMA_IRQ_EN);
  1067. UDCREG (UDC_DMA_IRQ_EN);
  1068. UDCDBG ("initialize SYSCON1");
  1069. outw (UDC_Self_Pwr | UDC_Pullup_En, UDC_SYSCON1);
  1070. UDCREG (UDC_SYSCON1);
  1071. return 0;
  1072. }
  1073. /* Stall endpoint */
  1074. static void udc_stall_ep (unsigned int ep_addr)
  1075. {
  1076. /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
  1077. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  1078. UDCDBGA ("stall ep_addr %d", ep_addr);
  1079. /* REVISIT?
  1080. * The OMAP TRM section 14.2.4.2 says we must check that the FIFO
  1081. * is empty before halting the endpoint. The current implementation
  1082. * doesn't check that the FIFO is empty.
  1083. */
  1084. if (!ep_num) {
  1085. outw (UDC_Stall_Cmd, UDC_SYSCON2);
  1086. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1087. if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
  1088. /* we have a valid rx endpoint, so halt it */
  1089. outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
  1090. outw (UDC_Set_Halt, UDC_CTRL);
  1091. outw (ep_num, UDC_EP_NUM);
  1092. }
  1093. } else {
  1094. if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
  1095. /* we have a valid tx endpoint, so halt it */
  1096. outw (UDC_EP_Sel | UDC_EP_Dir | ep_num, UDC_EP_NUM);
  1097. outw (UDC_Set_Halt, UDC_CTRL);
  1098. outw (ep_num, UDC_EP_NUM);
  1099. }
  1100. }
  1101. }
  1102. /* Reset endpoint */
  1103. #if 0
  1104. static void udc_reset_ep (unsigned int ep_addr)
  1105. {
  1106. /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
  1107. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  1108. UDCDBGA ("reset ep_addr %d", ep_addr);
  1109. if (!ep_num) {
  1110. /* control endpoint 0 can't be reset */
  1111. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1112. UDCDBGA ("UDC_EP_RX(%d) = 0x%04x", ep_num,
  1113. inw (UDC_EP_RX (ep_num)));
  1114. if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
  1115. /* we have a valid rx endpoint, so reset it */
  1116. outw (ep_num | UDC_EP_Sel, UDC_EP_NUM);
  1117. outw (UDC_Reset_EP, UDC_CTRL);
  1118. outw (ep_num, UDC_EP_NUM);
  1119. UDCDBGA ("OUT endpoint %d reset", ep_num);
  1120. }
  1121. } else {
  1122. UDCDBGA ("UDC_EP_TX(%d) = 0x%04x", ep_num,
  1123. inw (UDC_EP_TX (ep_num)));
  1124. /* Resetting of tx endpoints seems to be causing the USB function
  1125. * module to fail, which causes problems when the driver is
  1126. * uninstalled. We'll skip resetting tx endpoints for now until
  1127. * we figure out what the problem is.
  1128. */
  1129. #if 0
  1130. if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
  1131. /* we have a valid tx endpoint, so reset it */
  1132. outw (ep_num | UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
  1133. outw (UDC_Reset_EP, UDC_CTRL);
  1134. outw (ep_num | UDC_EP_Dir, UDC_EP_NUM);
  1135. UDCDBGA ("IN endpoint %d reset", ep_num);
  1136. }
  1137. #endif
  1138. }
  1139. }
  1140. #endif
  1141. /* ************************************************************************** */
  1142. /**
  1143. * udc_check_ep - check logical endpoint
  1144. *
  1145. * Return physical endpoint number to use for this logical endpoint or zero if not valid.
  1146. */
  1147. #if 0
  1148. int udc_check_ep (int logical_endpoint, int packetsize)
  1149. {
  1150. if ((logical_endpoint == 0x80) ||
  1151. ((logical_endpoint & 0x8f) != logical_endpoint)) {
  1152. return 0;
  1153. }
  1154. switch (packetsize) {
  1155. case 8:
  1156. case 16:
  1157. case 32:
  1158. case 64:
  1159. case 128:
  1160. case 256:
  1161. case 512:
  1162. break;
  1163. default:
  1164. return 0;
  1165. }
  1166. return EP_ADDR_TO_PHYS_EP (logical_endpoint);
  1167. }
  1168. #endif
  1169. /*
  1170. * udc_setup_ep - setup endpoint
  1171. *
  1172. * Associate a physical endpoint with endpoint_instance
  1173. */
  1174. void udc_setup_ep (struct usb_device_instance *device,
  1175. unsigned int ep, struct usb_endpoint_instance *endpoint)
  1176. {
  1177. UDCDBGA ("setting up endpoint addr %x", endpoint->endpoint_address);
  1178. /* This routine gets called by bi_modinit for endpoint 0 and from
  1179. * bi_config for all of the other endpoints. bi_config gets called
  1180. * during the DEVICE_CREATE, DEVICE_CONFIGURED, and
  1181. * DEVICE_SET_INTERFACE events. We need to reconfigure the OMAP packet
  1182. * RAM after bi_config scans the selected device configuration and
  1183. * initializes the endpoint structures, but before this routine enables
  1184. * the OUT endpoint FIFOs. Since bi_config calls this routine in a
  1185. * loop for endpoints 1 through UDC_MAX_ENDPOINTS, we reconfigure our
  1186. * packet RAM here when ep==1.
  1187. * I really hate to do this here, but it seems like the API exported
  1188. * by the USB bus interface controller driver to the usbd-bi module
  1189. * isn't quite right so there is no good place to do this.
  1190. */
  1191. if (ep == 1) {
  1192. omap1510_deconfigure_device ();
  1193. omap1510_configure_device (device);
  1194. }
  1195. if (endpoint && (ep < UDC_MAX_ENDPOINTS)) {
  1196. int ep_addr = endpoint->endpoint_address;
  1197. if (!ep_addr) {
  1198. /* nothing to do for endpoint 0 */
  1199. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  1200. /* nothing to do for IN (tx) endpoints */
  1201. } else { /* OUT (rx) endpoint */
  1202. if (endpoint->rcv_packetSize) {
  1203. /*struct urb* urb = &(urb_out_array[ep&0xFF]); */
  1204. /*urb->endpoint = endpoint; */
  1205. /*urb->device = device; */
  1206. /*urb->buffer_length = sizeof(urb->buffer); */
  1207. /*endpoint->rcv_urb = urb; */
  1208. omap1510_prepare_endpoint_for_rx (ep_addr);
  1209. }
  1210. }
  1211. }
  1212. }
  1213. /**
  1214. * udc_disable_ep - disable endpoint
  1215. * @ep:
  1216. *
  1217. * Disable specified endpoint
  1218. */
  1219. #if 0
  1220. void udc_disable_ep (unsigned int ep_addr)
  1221. {
  1222. /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
  1223. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  1224. struct usb_endpoint_instance *endpoint = omap1510_find_ep (ep_addr); /*udc_device->bus->endpoint_array + ep; */
  1225. UDCDBGA ("disable ep_addr %d", ep_addr);
  1226. if (!ep_num) {
  1227. /* nothing to do for endpoint 0 */ ;
  1228. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  1229. if (endpoint->tx_packetSize) {
  1230. /* we have a valid tx endpoint */
  1231. /*usbd_flush_tx(endpoint); */
  1232. endpoint->tx_urb = NULL;
  1233. }
  1234. } else {
  1235. if (endpoint->rcv_packetSize) {
  1236. /* we have a valid rx endpoint */
  1237. /*usbd_flush_rcv(endpoint); */
  1238. endpoint->rcv_urb = NULL;
  1239. }
  1240. }
  1241. }
  1242. #endif
  1243. /* ************************************************************************** */
  1244. /**
  1245. * udc_connected - is the USB cable connected
  1246. *
  1247. * Return non-zero if cable is connected.
  1248. */
  1249. #if 0
  1250. int udc_connected (void)
  1251. {
  1252. return ((inw (UDC_DEVSTAT) & UDC_ATT) == UDC_ATT);
  1253. }
  1254. #endif
  1255. /* Turn on the USB connection by enabling the pullup resistor */
  1256. void udc_connect (void)
  1257. {
  1258. UDCDBG ("connect, enable Pullup");
  1259. outl (0x00000018, FUNC_MUX_CTRL_D);
  1260. }
  1261. /* Turn off the USB connection by disabling the pullup resistor */
  1262. void udc_disconnect (void)
  1263. {
  1264. UDCDBG ("disconnect, disable Pullup");
  1265. outl (0x00000000, FUNC_MUX_CTRL_D);
  1266. }
  1267. /* ************************************************************************** */
  1268. /*
  1269. * udc_disable_interrupts - disable interrupts
  1270. * switch off interrupts
  1271. */
  1272. #if 0
  1273. void udc_disable_interrupts (struct usb_device_instance *device)
  1274. {
  1275. UDCDBG ("disabling all interrupts");
  1276. outw (0, UDC_IRQ_EN);
  1277. }
  1278. #endif
  1279. /* ************************************************************************** */
  1280. /**
  1281. * udc_ep0_packetsize - return ep0 packetsize
  1282. */
  1283. #if 0
  1284. int udc_ep0_packetsize (void)
  1285. {
  1286. return EP0_PACKETSIZE;
  1287. }
  1288. #endif
  1289. /* Switch on the UDC */
  1290. void udc_enable (struct usb_device_instance *device)
  1291. {
  1292. UDCDBGA ("enable device %p, status %d", device, device->status);
  1293. /* initialize driver state variables */
  1294. udc_devstat = 0;
  1295. /* Save the device structure pointer */
  1296. udc_device = device;
  1297. /* Setup ep0 urb */
  1298. if (!ep0_urb) {
  1299. ep0_urb =
  1300. usbd_alloc_urb (udc_device,
  1301. udc_device->bus->endpoint_array);
  1302. } else {
  1303. serial_printf ("udc_enable: ep0_urb already allocated %p\n",
  1304. ep0_urb);
  1305. }
  1306. UDCDBG ("Check clock status");
  1307. UDCREG (STATUS_REQ);
  1308. /* The VBUS_MODE bit selects whether VBUS detection is done via
  1309. * software (1) or hardware (0). When software detection is
  1310. * selected, VBUS_CTRL selects whether USB is not connected (0)
  1311. * or connected (1).
  1312. */
  1313. outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_CTRL | UDC_VBUS_MODE,
  1314. FUNC_MUX_CTRL_0);
  1315. UDCREGL (FUNC_MUX_CTRL_0);
  1316. omap1510_configure_device (device);
  1317. }
  1318. /* Switch off the UDC */
  1319. void udc_disable (void)
  1320. {
  1321. UDCDBG ("disable UDC");
  1322. omap1510_deconfigure_device ();
  1323. /* The VBUS_MODE bit selects whether VBUS detection is done via
  1324. * software (1) or hardware (0). When software detection is
  1325. * selected, VBUS_CTRL selects whether USB is not connected (0)
  1326. * or connected (1).
  1327. */
  1328. outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
  1329. outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
  1330. UDCREGL (FUNC_MUX_CTRL_0);
  1331. /* Free ep0 URB */
  1332. if (ep0_urb) {
  1333. /*usbd_dealloc_urb(ep0_urb); */
  1334. ep0_urb = NULL;
  1335. }
  1336. /* Reset device pointer.
  1337. * We ought to do this here to balance the initialization of udc_device
  1338. * in udc_enable, but some of our other exported functions get called
  1339. * by the bus interface driver after udc_disable, so we have to hang on
  1340. * to the device pointer to avoid a null pointer dereference. */
  1341. /* udc_device = NULL; */
  1342. }
  1343. /**
  1344. * udc_startup - allow udc code to do any additional startup
  1345. */
  1346. void udc_startup_events (struct usb_device_instance *device)
  1347. {
  1348. /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
  1349. usbd_device_event_irq (device, DEVICE_INIT, 0);
  1350. /* The DEVICE_CREATE event puts the USB device in the state
  1351. * STATE_ATTACHED.
  1352. */
  1353. usbd_device_event_irq (device, DEVICE_CREATE, 0);
  1354. /* Some USB controller driver implementations signal
  1355. * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
  1356. * DEVICE_HUB_CONFIGURED causes a transition to the state STATE_POWERED,
  1357. * and DEVICE_RESET causes a transition to the state STATE_DEFAULT.
  1358. * The OMAP USB client controller has the capability to detect when the
  1359. * USB cable is connected to a powered USB bus via the ATT bit in the
  1360. * DEVSTAT register, so we will defer the DEVICE_HUB_CONFIGURED and
  1361. * DEVICE_RESET events until later.
  1362. */
  1363. udc_enable (device);
  1364. }
  1365. /**
  1366. * udc_irq - do pseudo interrupts
  1367. */
  1368. void udc_irq(void)
  1369. {
  1370. /* Loop while we have interrupts.
  1371. * If we don't do this, the input chain
  1372. * polling delay is likely to miss
  1373. * host requests.
  1374. */
  1375. while (inw (UDC_IRQ_SRC) & ~UDC_SOF_Flg) {
  1376. /* Handle any new IRQs */
  1377. omap1510_udc_irq ();
  1378. omap1510_udc_noniso_irq ();
  1379. }
  1380. }
  1381. /* Flow control */
  1382. void udc_set_nak(int epid)
  1383. {
  1384. /* TODO: implement this functionality in omap1510 */
  1385. }
  1386. void udc_unset_nak (int epid)
  1387. {
  1388. /* TODO: implement this functionality in omap1510 */
  1389. }