mt_usb.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329
  1. /*
  2. * Copyright (c) 2012 MediaTek Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in
  11. * the documentation and/or other materials provided with the
  12. * distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  17. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  18. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  20. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  21. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  22. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  24. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include <sys/types.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <debug.h>
  31. #include <reg.h>
  32. #include <platform/bitops.h>
  33. #include <platform/mt_irq.h>
  34. #include <platform/mt_reg_base.h>
  35. #include <platform/mt_usb.h>
  36. #include <platform/mt_typedefs.h>
  37. #include <platform/timer.h>
  38. #include <kernel/thread.h>
  39. #include <platform/sec_devinfo.h>
  40. #include <dev/udc.h>
  41. #ifdef MACH_FPGA
  42. #define CFG_FPGA_PLATFORM (1)
  43. #else
  44. #define DBG_PHY_CALIBRATION 1
  45. #endif
  46. #define USB_DOUBLE_BUF
  47. #define USB_GINTR
  48. #ifdef USB_DEBUG
  49. /* DEBUG INFO Sections */
  50. #define DBG_USB_DUMP_DESC 0
  51. #define DBG_USB_DUMP_DATA 0
  52. #define DBG_USB_DUMP_SETUP 1
  53. #define DBG_USB_FIFO 0
  54. #define DBG_USB_GENERAL 1
  55. #define DBG_PHY_CALIBRATION 0
  56. #endif
  57. #define DBG_C(x...) dprintf(CRITICAL, x)
  58. #define DBG_I(x...) dprintf(INFO, x)
  59. #define DBG_S(x...) dprintf(SPEW, x)
  60. #if DBG_USB_GENERAL
  61. #define DBG_IRQ(x...) dprintf(INFO, x)
  62. #else
  63. #define DBG_IRQ(x...) do{} while(0)
  64. #endif
  65. /* bits used in all the endpoint status registers */
  66. #define EPT_TX(n) (1 << ((n) + 16))
  67. #define EPT_RX(n) (1 << (n))
  68. /* udc.h wrapper for usbdcore */
  69. static unsigned char usb_config_value = 0;
  70. EP0_STATE ep0_state = EP0_IDLE;
  71. int set_address = 0;
  72. u32 fifo_addr = FIFO_ADDR_START;
  73. #define EP0 0
  74. /* USB transfer directions */
  75. #define USB_DIR_IN DEVICE_WRITE /* val: 0x80 */
  76. #define USB_DIR_OUT DEVICE_READ /* val: 0x00 */
  77. #define EP0_MAX_PACKET_SIZE 64
  78. /* Request types */
  79. #define USB_TYPE_STANDARD (0x00 << 5)
  80. #define USB_TYPE_CLASS (0x01 << 5)
  81. #define USB_TYPE_VENDOR (0x02 << 5)
  82. #define USB_TYPE_RESERVED (0x03 << 5)
  83. /* values used in GET_STATUS requests */
  84. #define USB_STAT_SELFPOWERED 0x01
  85. /* USB recipients */
  86. #define USB_RECIP_DEVICE 0x00
  87. #define USB_RECIP_INTERFACE 0x01
  88. #define USB_RECIP_ENDPOINT 0x02
  89. #define USB_RECIP_OTHER 0x03
  90. /* Endpoints */
  91. #define USB_EP_NUM_MASK 0x0f /* in bEndpointAddress */
  92. #define USB_EP_DIR_MASK 0x80
  93. #define USB_TYPE_MASK 0x60
  94. #define USB_RECIP_MASK 0x1f
  95. #if CFG_FPGA_PLATFORM
  96. /* for usb phy */
  97. #include <platform/mt_i2c.h>
  98. #endif
  99. #define URB_BUF_SIZE 512
  100. struct urb {
  101. struct udc_endpoint *endpoint;
  102. struct udc_device *device;
  103. struct setup_packet device_request;
  104. u8 *buffer;
  105. unsigned int actual_length;
  106. };
  107. static struct udc_endpoint *ep0in, *ep0out;
  108. static struct udc_request *ep0req;
  109. static unsigned char ep0_buf[4096] __attribute__((aligned(32)));
  110. struct urb mt_ep0_urb;
  111. struct urb mt_tx_urb;
  112. struct urb mt_rx_urb;
  113. struct urb *ep0_urb = &mt_ep0_urb;
  114. struct urb *tx_urb = &mt_tx_urb;
  115. struct urb *rx_urb = &mt_rx_urb;
  116. /* endpoint data - mt_ep */
  117. struct udc_endpoint {
  118. /* rx side */
  119. struct urb *rcv_urb; /* active urb */
  120. /* tx side */
  121. struct urb *tx_urb; /* active urb */
  122. /* info from hsusb */
  123. struct udc_request *req;
  124. unsigned int bit; /* EPT_TX/EPT_RX */
  125. unsigned char num;
  126. unsigned char in;
  127. unsigned short maxpkt;
  128. int status; /* status for error handling */
  129. unsigned int sent; /* data already sent */
  130. unsigned int last; /* data sent in last packet XXX do we need this */
  131. unsigned char mode; /* double buffer */
  132. };
  133. /* from mt_usbtty.h */
  134. #define NUM_ENDPOINTS 3
  135. /* origin endpoint_array */
  136. struct udc_endpoint ep_list[NUM_ENDPOINTS + 1]; /* one extra for control endpoint */
  137. static int usb_online = 0;
  138. static u8 dev_address = 0;
  139. static struct udc_device *the_device;
  140. static struct udc_gadget *the_gadget;
  141. /* end from hsusb.c */
  142. /* declare ept_complete handle */
  143. static void handle_ept_complete(struct udc_endpoint *ept);
  144. /* usb phy bring up */
  145. #if CFG_FPGA_PLATFORM
  146. #define USB_I2C_ID I2C0 /* 0 - 6 */
  147. #define PATH_NORMAL 0
  148. #define PATH_PMIC 1
  149. U32 usb_i2c_read8 (U8 addr, U8 *dataBuffer)
  150. {
  151. U32 ret_code = I2C_OK;
  152. // U8 write_data = addr;
  153. U16 len;
  154. static struct mt_i2c_t i2c;
  155. i2c.id = USB_I2C_ID;
  156. i2c.addr = 0x60; /* i2c API will shift-left this value to 0xc0 */
  157. i2c.mode = ST_MODE;
  158. i2c.speed = 100;
  159. i2c.dma_en = 0;
  160. len = 1;
  161. /* set register command */
  162. #if 0
  163. ret_code = i2c_write(&i2c, &write_data, len);
  164. if (ret_code != len)
  165. return ret_code;
  166. ret_code = i2c_read(&i2c, dataBuffer, len);
  167. #else
  168. ret_code = i2c_write_read(&i2c, dataBuffer, len, len);
  169. #endif
  170. return ret_code;
  171. }
  172. U32 usb_i2c_write8(U8 addr, U8 value)
  173. {
  174. U32 ret_code = I2C_OK;
  175. U8 write_data[2];
  176. U16 len;
  177. static struct mt_i2c_t i2c;
  178. write_data[0]= addr;
  179. write_data[1] = value;
  180. i2c.id = USB_I2C_ID;
  181. i2c.addr = 0x60; /* i2c API will shift-left this value to 0xc0 */
  182. i2c.mode = ST_MODE;
  183. i2c.speed = 100;
  184. i2c.dma_en = 0;
  185. len = 2;
  186. ret_code = i2c_write(&i2c, write_data, len);
  187. return ret_code;
  188. }
  189. #define USBPHY_I2C_READ8(addr, buffer) usb_i2c_read8(addr, buffer)
  190. #define USBPHY_I2C_WRITE8(addr, value) usb_i2c_write8(addr, value)
  191. #endif
  192. /* use mt_typedefs.h */
  193. #define USBPHY_READ8(offset) readb(USB20_PHY_BASE+offset)
  194. #define USBPHY_WRITE8(offset, value) writeb(value, USB20_PHY_BASE+offset)
  195. #define USBPHY_SET8(offset, mask) USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) | (mask))
  196. #define USBPHY_CLR8(offset, mask) USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) & (~mask))
  197. #define USB11PHY_READ8(offset) readb(USB11_PHY_BASE+offset)
  198. #define USB11PHY_WRITE8(offset, value) writeb(value, USB11_PHY_BASE+offset)
  199. #define USB11PHY_SET8(offset, mask) USB11PHY_WRITE8(offset, (USB11PHY_READ8(offset)) | (mask))
  200. #define USB11PHY_CLR8(offset, mask) USB11PHY_WRITE8(offset, (USB11PHY_READ8(offset)) & (~mask))
  201. #if CFG_FPGA_PLATFORM
  202. void mt_usb_phy_poweron(void)
  203. {
  204. #define PHY_DRIVING 0x3
  205. #if defined(USB_PHY_DRIVING_TUNING)
  206. UINT8 usbreg8;
  207. unsigned int i;
  208. #endif
  209. /* force_suspendm = 0 */
  210. USBPHY_CLR8(0x6a, 0x04);
  211. USBPHY_I2C_WRITE8(0xff, 0x00);
  212. USBPHY_I2C_WRITE8(0x61, 0x04);
  213. USBPHY_I2C_WRITE8(0x68, 0x00);
  214. USBPHY_I2C_WRITE8(0x6a, 0x00);
  215. USBPHY_I2C_WRITE8(0x00, 0x6e);
  216. USBPHY_I2C_WRITE8(0x1b, 0x0c);
  217. USBPHY_I2C_WRITE8(0x08, 0x44);
  218. USBPHY_I2C_WRITE8(0x11, 0x55);
  219. USBPHY_I2C_WRITE8(0x1a, 0x68);
  220. #if defined(USB_PHY_DRIVING_TUNING)
  221. /* driving tuning */
  222. USBPHY_I2C_READ8(0xab, &usbreg8);
  223. usbreg8 &= ~0x3;
  224. usbreg8 |= PHY_DRIVING;
  225. USBPHY_I2C_WRITE8(0xab, usbreg8);
  226. for(i = 0; i < 16; i++)
  227. {
  228. USBPHY_I2C_READ8((0x92+i), &usbreg8);
  229. usbreg8 &= ~0x3;
  230. usbreg8 |= PHY_DRIVING;
  231. USBPHY_I2C_WRITE8((0x92+i), usbreg8);
  232. }
  233. USBPHY_I2C_READ8(0xbc, &usbreg8);
  234. usbreg8 &= ~0x3;
  235. usbreg8 |= PHY_DRIVING;
  236. USBPHY_I2C_WRITE8(0xbc, usbreg8);
  237. USBPHY_I2C_READ8(0xbe, &usbreg8);
  238. usbreg8 &= ~0x3;
  239. usbreg8 |= PHY_DRIVING;
  240. USBPHY_I2C_WRITE8(0xbe, usbreg8);
  241. USBPHY_I2C_READ8(0xbf, &usbreg8);
  242. usbreg8 &= ~0x3;
  243. usbreg8 |= PHY_DRIVING;
  244. USBPHY_I2C_WRITE8(0xbf, usbreg8);
  245. USBPHY_I2C_READ8(0xcd, &usbreg8);
  246. usbreg8 &= ~0x3;
  247. usbreg8 |= PHY_DRIVING;
  248. USBPHY_I2C_WRITE8(0xcd, usbreg8);
  249. USBPHY_I2C_READ8(0xf1, &usbreg8);
  250. usbreg8 &= ~0x3;
  251. usbreg8 |= PHY_DRIVING;
  252. USBPHY_I2C_WRITE8(0xf1, usbreg8);
  253. USBPHY_I2C_READ8(0xa7, &usbreg8);
  254. usbreg8 &= ~0x3;
  255. usbreg8 |= PHY_DRIVING;
  256. USBPHY_I2C_WRITE8(0xa7, usbreg8);
  257. USBPHY_I2C_READ8(0xa8, &usbreg8);
  258. usbreg8 &= ~0x3;
  259. usbreg8 |= PHY_DRIVING;
  260. USBPHY_I2C_WRITE8(0xa8, usbreg8);
  261. #endif
  262. udelay(800);
  263. }
  264. void mt_usb_phy_savecurrent (void)
  265. {
  266. /* no need */
  267. }
  268. void mt_usb_phy_recover (void)
  269. {
  270. /* no need */
  271. }
  272. void mt_usb11_phy_savecurrent(void)
  273. {
  274. /* no need */
  275. }
  276. void Charger_Detect_Init(void)
  277. {
  278. /* no need */
  279. }
  280. void Charger_Detect_Release(void)
  281. {
  282. /* no need */
  283. }
  284. #else
  285. void mt_usb_phy_poweron(void)
  286. {
  287. /*
  288. * swtich to USB function.
  289. * (system register, force ip into usb mode).
  290. */
  291. USBPHY_CLR8(0x6b, 0x04);
  292. USBPHY_CLR8(0x6e, 0x01);
  293. USBPHY_CLR8(0x21, 0x03);
  294. /* RG_USB20_BC11_SW_EN = 1'b0 */
  295. USBPHY_SET8(0x22, 0x04);
  296. USBPHY_CLR8(0x1a, 0x80);
  297. /* RG_USB20_DP_100K_EN = 1'b0 */
  298. /* RG_USB20_DP_100K_EN = 1'b0 */
  299. USBPHY_CLR8(0x22, 0x03);
  300. /*OTG enable*/
  301. USBPHY_SET8(0x20, 0x10);
  302. /* release force suspendm */
  303. USBPHY_CLR8(0x6a, 0x04);
  304. udelay(800);
  305. /* force enter device mode */
  306. USBPHY_CLR8(0x6c, 0x10);
  307. USBPHY_SET8(0x6c, 0x2E);
  308. USBPHY_SET8(0x6d, 0x3E);
  309. return;
  310. }
  311. void mt_usb_phy_savecurrent(void)
  312. {
  313. /*
  314. * swtich to USB function.
  315. * (system register, force ip into usb mode).
  316. */
  317. USBPHY_CLR8(0x6b, 0x04);
  318. USBPHY_CLR8(0x6e, 0x01);
  319. USBPHY_CLR8(0x21, 0x03);
  320. /* release force suspendm */
  321. USBPHY_CLR8(0x6a, 0x04);
  322. USBPHY_SET8(0x68, 0x04);
  323. /* RG_DPPULLDOWN./RG_DMPULLDOWN. */
  324. USBPHY_SET8(0x68, 0xc0);
  325. /* RG_XCVRSEL[1:0] = 2'b01 */
  326. USBPHY_CLR8(0x68, 0x30);
  327. USBPHY_SET8(0x68, 0x10);
  328. /* RG_TERMSEL = 1'b1 */
  329. USBPHY_SET8(0x68, 0x04);
  330. /* RG_DATAIN[3:0] = 4'b0000 */
  331. USBPHY_CLR8(0x69, 0x3c);
  332. /*
  333. * force_dp_pulldown, force_dm_pulldown,
  334. * force_xcversel, force_termsel.
  335. */
  336. USBPHY_SET8(0x6a, 0xba);
  337. /* RG_USB20_BC11_SW_EN = 1'b0 */
  338. USBPHY_CLR8(0x1a, 0x80);
  339. /* RG_USB20_OTG_VBUSSCMP_EN = 1'b0 */
  340. USBPHY_CLR8(0x1a, 0x10);
  341. udelay(800);
  342. USBPHY_CLR8(0x6a, 0x04);
  343. /* rg_usb20_pll_stable = 1 */
  344. //USBPHY_SET8(0x63, 0x02);
  345. udelay(1);
  346. /* force suspendm = 1 */
  347. //USBPHY_SET8(0x6a, 0x04);
  348. //udelay(1);
  349. return;
  350. }
  351. void mt_usb_phy_recover(void)
  352. {
  353. /* clean PUPD_BIST_EN */
  354. /* PUPD_BIST_EN = 1'b0 */
  355. /* PMIC will use it to detect charger type */
  356. USBPHY_CLR8(0x1d, 0x10);
  357. /* force_uart_en = 1'b0 */
  358. USBPHY_CLR8(0x6b, 0x04);
  359. /* RG_UART_EN = 1'b0 */
  360. USBPHY_CLR8(0x6e, 0x01);
  361. /* force_uart_en = 1'b0 */
  362. USBPHY_CLR8(0x6a, 0x04);
  363. USBPHY_CLR8(0x21, 0x03);
  364. USBPHY_CLR8(0x68, 0xf4);
  365. /* RG_DATAIN[3:0] = 4'b0000 */
  366. USBPHY_CLR8(0x69, 0x3c);
  367. USBPHY_CLR8(0x6a, 0xba);
  368. /* RG_USB20_BC11_SW_EN = 1'b0 */
  369. USBPHY_CLR8(0x1a, 0x80);
  370. /* RG_USB20_OTG_VBUSSCMP_EN = 1'b1 */
  371. USBPHY_SET8(0x1a, 0x10);
  372. udelay(800);
  373. /* force enter device mode */
  374. //USBPHY_CLR8(0x6c, 0x10);
  375. //USBPHY_SET8(0x6c, 0x2E);
  376. //USBPHY_SET8(0x6d, 0x3E);
  377. }
  378. void mt_usb11_phy_savecurrent(void)
  379. {
  380. #if 0
  381. USB11PHY_SET8(0xca, 0x10);
  382. USB11PHY_SET8(0xcb, 0x3c);
  383. USB11PHY_CLR8(0xc1, 0x08);
  384. USB11PHY_CLR8(0xc7, 0x06);
  385. USB11PHY_SET8(0xc6, 0x06);
  386. #endif
  387. }
  388. void Charger_Detect_Init(void)
  389. {
  390. /* RG_USB20_BC11_SW_EN = 1'b1 */
  391. USBPHY_SET8(0x1a, 0x80);
  392. }
  393. void Charger_Detect_Release(void)
  394. {
  395. /* RG_USB20_BC11_SW_EN = 1'b0 */
  396. USBPHY_CLR8(0x1a, 0x80);
  397. }
  398. #endif
  399. /* usb phy bring up end */
  400. #if !CFG_FPGA_PLATFORM
  401. //ALPS00427972, implement the analog register formula
  402. void mt_usb_phy_calibraion (int case_set, int input_reg)
  403. {
  404. #if 0
  405. int temp_added=0;
  406. int temp_test=0;
  407. int temp_mask;
  408. #if DBG_PHY_CALIBRATION
  409. DBG_I("%s: case_set %d, input_reg = 0x%x \n", __func__, case_set, input_reg);
  410. #endif
  411. switch(case_set)
  412. {
  413. case 1:
  414. //case 1
  415. //If M_HW_RES3[15:13] !=0
  416. //RG_USB20_TERM_VREF_SEL[2:0] <= RG_USB20_TERM_VREF_SEL[2:0] + M_HW_RES3[15:13]
  417. temp_mask = 0x07;
  418. temp_test = USBPHY_READ8(0x05);
  419. #if DBG_PHY_CALIBRATION
  420. DBG_I("%s: temp_test = 0x%x \n", __func__, temp_test);
  421. #endif
  422. temp_added = (USBPHY_READ8(0x05)& temp_mask) + input_reg;
  423. #if DBG_PHY_CALIBRATION
  424. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  425. #endif
  426. temp_added &= 0x07;
  427. #if DBG_PHY_CALIBRATION
  428. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  429. #endif
  430. USBPHY_CLR8(0x05, temp_mask);
  431. USBPHY_SET8(0x05, temp_added);
  432. temp_test = USBPHY_READ8(0x05);
  433. #if DBG_PHY_CALIBRATION
  434. DBG_I("%s: final temp_test = 0x%x \n", __func__, temp_test);
  435. #endif
  436. break;
  437. case 2:
  438. //case 2
  439. //If M_HW_RES3[12:10] !=0
  440. //RG_USB20_CLKREF_REF[2:0]<= RG_USB20_CLKREF_REF[2:0]+ M_HW_RES3[12:10]
  441. temp_mask = 0x07;
  442. temp_test = USBPHY_READ8(0x07);
  443. #if DBG_PHY_CALIBRATION
  444. DBG_I("%s: temp_test = 0x%x \n", __func__, temp_test);
  445. #endif
  446. temp_added = (USBPHY_READ8(0x07)& temp_mask) + input_reg;
  447. #if DBG_PHY_CALIBRATION
  448. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  449. #endif
  450. temp_added &= 0x07;
  451. #if DBG_PHY_CALIBRATION
  452. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  453. #endif
  454. USBPHY_CLR8(0x07, temp_mask);
  455. USBPHY_SET8(0x07, temp_added);
  456. temp_test = USBPHY_READ8(0x07);
  457. #if DBG_PHY_CALIBRATION
  458. DBG_I("%s: final temp_test = 0x%x \n", __func__, temp_test);
  459. #endif
  460. break;
  461. case 3:
  462. //case 3
  463. //If M_HW_RES3[9:7] !=0
  464. //RG_USB20_VRT_VREF_SEL[2:0]<=RG_USB20_VRT_VREF_SEL[2:0]+ M_HW_RES3[9:7]
  465. temp_mask = 0x70;
  466. temp_test = USBPHY_READ8(0x05);
  467. #if DBG_PHY_CALIBRATION
  468. DBG_I("%s: temp_test = 0x%x \n", __func__, temp_test);
  469. #endif
  470. temp_added = (USBPHY_READ8(0x05)& temp_mask) >> 4;
  471. #if DBG_PHY_CALIBRATION
  472. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  473. #endif
  474. temp_added += input_reg;
  475. #if DBG_PHY_CALIBRATION
  476. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  477. #endif
  478. temp_added &= 0x07;
  479. #if DBG_PHY_CALIBRATION
  480. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  481. #endif
  482. USBPHY_CLR8(0x05, temp_mask);
  483. USBPHY_SET8(0x05, temp_added<<4);
  484. temp_test = USBPHY_READ8(0x05);
  485. #if DBG_PHY_CALIBRATION
  486. DBG_I("%s: final temp_test = 0x%x \n", __func__, temp_test);
  487. #endif
  488. break;
  489. }
  490. #endif
  491. }
  492. //ALPS00427972, implement the analog register formula
  493. #endif
  494. void board_usb_init(void)
  495. {
  496. mt_usb_phy_poweron();
  497. }
  498. struct udc_descriptor {
  499. struct udc_descriptor *next;
  500. unsigned short tag; /* ((TYPE << 8) | NUM) */
  501. unsigned short len; /* total length */
  502. unsigned char data[0];
  503. };
  504. #if DBG_USB_DUMP_SETUP
  505. static void dump_setup_packet(char *str, struct setup_packet *sp) {
  506. DBG_I("\n");
  507. DBG_I(str);
  508. DBG_I(" bmRequestType = %x\n", sp->type);
  509. DBG_I(" bRequest = %x\n", sp->request);
  510. DBG_I(" wValue = %x\n", sp->value);
  511. DBG_I(" wIndex = %x\n", sp->index);
  512. DBG_I(" wLength = %x\n", sp->length);
  513. }
  514. #else
  515. static void dump_setup_packet(char *str, struct setup_packet *sp) {}
  516. #endif
  517. static void copy_desc(struct urb *urb, void *data, int length) {
  518. #if DBG_USB_FIFO
  519. DBG_I("%s: urb: %x, data %x, length: %d, actual_length: %d\n",
  520. __func__, urb->buffer, data, length, urb->actual_length);
  521. #endif
  522. //memcpy(urb->buffer + urb->actual_length, data, length);
  523. memcpy(urb->buffer, data, length);
  524. //urb->actual_length += length;
  525. urb->actual_length = length;
  526. #if DBG_USB_FIFO
  527. DBG_I("%s: urb: %x, data %x, length: %d, actual_length: %d\n",
  528. __func__, urb, data, length, urb->actual_length);
  529. #endif
  530. }
  531. struct udc_descriptor *udc_descriptor_alloc(unsigned type, unsigned num,
  532. unsigned len)
  533. {
  534. struct udc_descriptor *desc;
  535. if ((len > 255) || (len < 2) || (num > 255) || (type > 255))
  536. return 0;
  537. if (!(desc = malloc(sizeof(struct udc_descriptor) + len)))
  538. return 0;
  539. desc->next = 0;
  540. desc->tag = (type << 8) | num;
  541. desc->len = len;
  542. desc->data[0] = len;
  543. desc->data[1] = type;
  544. return desc;
  545. }
  546. static struct udc_descriptor *desc_list = 0;
  547. static unsigned next_string_id = 1;
  548. void udc_descriptor_register(struct udc_descriptor *desc) {
  549. desc->next = desc_list;
  550. desc_list = desc;
  551. }
  552. unsigned udc_string_desc_alloc(const char *str)
  553. {
  554. unsigned len;
  555. struct udc_descriptor *desc;
  556. unsigned char *data;
  557. if (next_string_id > 255)
  558. return 0;
  559. if (!str)
  560. return 0;
  561. len = strlen(str);
  562. desc = udc_descriptor_alloc(TYPE_STRING, next_string_id, len * 2 + 2);
  563. if (!desc)
  564. return 0;
  565. next_string_id++;
  566. /* expand ascii string to utf16 */
  567. data = desc->data + 2;
  568. while (len-- > 0) {
  569. *data++ = *str++;
  570. *data++ = 0;
  571. }
  572. udc_descriptor_register(desc);
  573. return desc->tag & 0xff;
  574. }
  575. static int mt_read_fifo(struct udc_endpoint *endpoint) {
  576. struct urb *urb = endpoint->rcv_urb;
  577. int len = 0, count = 0;
  578. int ep_num = endpoint->num;
  579. int index;
  580. unsigned char *cp;
  581. u32 *wp;
  582. #if !CFG_FPGA_PLATFORM
  583. u16 dma_cntl = 0;
  584. #endif
  585. if (ep_num == EP0)
  586. urb = ep0_urb;
  587. if (urb) {
  588. index = readb(INDEX);
  589. writeb(ep_num, INDEX);
  590. cp = (u8 *) (urb->buffer + urb->actual_length);
  591. wp = (u32 *) cp;
  592. #if DBG_USB_FIFO
  593. DBG_I("%s: ep_num: %d, urb: %x, urb->buffer: %x, urb->actual_length = %d\n",
  594. __func__, ep_num, urb, urb->buffer, urb->actual_length);
  595. #endif
  596. count = len = readw(IECSR + RXCOUNT);
  597. if (ep_num != 0) {
  598. #if DBG_USB_FIFO
  599. DBG_I("%s: ep_num: %d count = %d\n",
  600. __func__, ep_num, count);
  601. #endif
  602. }
  603. /* FIX: DMA has problem write now */
  604. #if !CFG_FPGA_PLATFORM
  605. arch_clean_invalidate_cache_range((addr_t) cp, count);
  606. if (ep_num != 0) {
  607. writel((u32)wp, USB_DMA_ADDR (ep_num));
  608. writel(count, USB_DMA_COUNT (ep_num));
  609. dma_cntl =
  610. USB_DMA_BURST_MODE_3 | (ep_num << USB_DMA_ENDPNT_OFFSET) |
  611. USB_DMA_EN;
  612. writew(dma_cntl, USB_DMA_CNTL (ep_num));
  613. while (readw(USB_DMA_CNTL (ep_num)) & USB_DMA_EN);
  614. }
  615. else
  616. #endif
  617. {
  618. while (len > 0) {
  619. if (len >= 4) {
  620. *wp++ = readl(FIFO(ep_num));
  621. cp = (unsigned char *) wp;
  622. //DBG_I("USB READ FIFO: wp = %lu, cp = %lu\n", wp, cp);
  623. len -= 4;
  624. } else {
  625. *cp++ = readb(FIFO(ep_num));
  626. //DBG_I("USB READ FIFO: wp = %lu, cp = %lu\n", wp, cp);
  627. len--;
  628. }
  629. }
  630. }
  631. #if DBG_USB_DUMP_DATA
  632. if (ep_num != 0) {
  633. DBG_I("%s: &urb->buffer: %x\n", __func__, urb->buffer);
  634. DBG_I("[USB] dump data:\n");
  635. hexdump8(urb->buffer, count);
  636. }
  637. #endif
  638. urb->actual_length += count;
  639. writeb(index, INDEX);
  640. }
  641. return count;
  642. }
  643. static int mt_write_fifo(struct udc_endpoint *endpoint)
  644. {
  645. struct urb *urb = endpoint->tx_urb;
  646. int last = 0, count = 0;
  647. int ep_num = endpoint->num;
  648. int index;
  649. unsigned char *cp = NULL;
  650. #ifdef USB_TX_DMA_MODE_0
  651. u32 *wp;
  652. u16 dma_cntl = 0;
  653. #endif
  654. if (ep_num == EP0)
  655. urb = ep0_urb;
  656. if (urb) {
  657. index = readb(INDEX);
  658. writeb(ep_num, INDEX);
  659. #if DBG_USB_DUMP_DESC
  660. DBG_I("%s: dump desc\n", __func__);
  661. hexdump8(urb->buffer, urb->actual_length);
  662. #endif
  663. #if DBG_USB_FIFO
  664. DBG_I("%s: ep_num: %d urb: %x, actual_length: %d\n",
  665. __func__, ep_num, urb, urb->actual_length);
  666. DBG_I("%s: sent: %d, tx_pkt_size: %d\n", __func__, endpoint->sent, endpoint->maxpkt);
  667. #endif
  668. count = last = MIN (urb->actual_length - endpoint->sent, endpoint->maxpkt);
  669. //count = last = urb->actual_length;
  670. #if DBG_USB_FIFO
  671. DBG_I("%s: count: %d\n", __func__, count);
  672. DBG_I("%s: urb->actual_length = %d\n", __func__, urb->actual_length);
  673. DBG_I("%s: endpoint->sent = %d\n", __func__, endpoint->sent);
  674. #endif
  675. if (count < 0) {
  676. DBG_C("%s: something is wrong, count < 0", __func__);
  677. }
  678. if (count) {
  679. cp = urb->buffer + endpoint->sent;
  680. #ifdef USB_TX_DMA_MODE_0
  681. wp = (u32 *)cp;
  682. arch_clean_invalidate_cache_range((addr_t) cp, count);
  683. if (ep_num != 0) {
  684. writel(wp, USB_DMA_ADDR(ep_num));
  685. writel(count, USB_DMA_COUNT(ep_num));
  686. dma_cntl =
  687. USB_DMA_BURST_MODE_3 | (ep_num << USB_DMA_ENDPNT_OFFSET) |
  688. USB_DMA_EN | USB_DMA_DIR;
  689. writew(dma_cntl, USB_DMA_CNTL(ep_num));
  690. while (readw(USB_DMA_CNTL (ep_num)) & USB_DMA_EN);
  691. } else
  692. #endif
  693. {
  694. //DBG("---------write USB fifo---------\n");
  695. while (count > 0) {
  696. //hexdump8(cp, 1);
  697. writeb(*cp, FIFO (ep_num));
  698. cp++;
  699. count--;
  700. }
  701. }
  702. }
  703. endpoint->last = last;
  704. endpoint->sent += last;
  705. writeb(index, INDEX);
  706. }
  707. return last;
  708. }
  709. static struct udc_endpoint * mt_find_ep(int ep_num, u8 dir) {
  710. int i;
  711. u8 in = 0;
  712. /* convert dir to in */
  713. if (dir == USB_DIR_IN) /* dir == USB_DIR_IN */
  714. in = 1;
  715. /* for (i = 0; i < udc_device->max_endpoints; i++) */
  716. /* for (i = 0; i < the_gadget->ifc_endpoints; i++) */
  717. for (i = 0; i < MT_EP_NUM; i++) {
  718. if ((ep_list[i].num == ep_num) && (ep_list[i].in == in)) {
  719. #if DBG_USB_GENERAL
  720. DBG_I("%s: find ep!\n", __func__);
  721. #endif
  722. return &ep_list[i];
  723. }
  724. }
  725. return NULL;
  726. }
  727. static void mt_udc_flush_fifo(u8 ep_num, u8 dir)
  728. {
  729. u16 tmpReg16;
  730. u8 index;
  731. struct udc_endpoint *endpoint;
  732. index = readb(INDEX);
  733. writeb(ep_num, INDEX);
  734. if (ep_num == 0) {
  735. tmpReg16 = readw(IECSR + CSR0);
  736. tmpReg16 |= EP0_FLUSH_FIFO;
  737. writew(tmpReg16, IECSR + CSR0);
  738. writew(tmpReg16, IECSR + CSR0);
  739. } else {
  740. endpoint = mt_find_ep(ep_num, dir);
  741. if (endpoint->in == 0) { /* USB_DIR_OUT */
  742. tmpReg16 = readw(IECSR + RXCSR);
  743. tmpReg16 |= EPX_RX_FLUSHFIFO;
  744. writew(tmpReg16, IECSR + RXCSR);
  745. writew(tmpReg16, IECSR + RXCSR);
  746. } else {
  747. tmpReg16 = readw(IECSR + TXCSR);
  748. tmpReg16 |= EPX_TX_FLUSHFIFO;
  749. writew(tmpReg16, IECSR + TXCSR);
  750. writew(tmpReg16, IECSR + TXCSR);
  751. }
  752. }
  753. /* recover index register */
  754. writeb(index, INDEX);
  755. }
  756. /* the endpoint does not support the received command, stall it!! */
  757. static void udc_stall_ep(unsigned int ep_num, u8 dir) {
  758. struct udc_endpoint *endpoint = mt_find_ep(ep_num, dir);
  759. u8 index;
  760. u16 csr;
  761. DBG_C("[USB] %s\n", __func__);
  762. index = readb(INDEX);
  763. writeb(ep_num, INDEX);
  764. if (ep_num == 0) {
  765. csr = readw(IECSR + CSR0);
  766. csr |= EP0_SENDSTALL;
  767. writew(csr, IECSR + CSR0);
  768. mt_udc_flush_fifo(ep_num, USB_DIR_OUT);
  769. } else {
  770. if (endpoint->in == 0) { /* USB_DIR_OUT */
  771. csr = readb(IECSR + RXCSR);
  772. csr |= EPX_RX_SENDSTALL;
  773. writew(csr, IECSR + RXCSR);
  774. mt_udc_flush_fifo(ep_num, USB_DIR_OUT);
  775. } else {
  776. csr = readb(IECSR + TXCSR);
  777. csr |= EPX_TX_SENDSTALL;
  778. writew(csr, IECSR + TXCSR);
  779. mt_udc_flush_fifo(ep_num, USB_DIR_IN);
  780. }
  781. }
  782. //mt_udc_flush_fifo (ep_num, USB_DIR_OUT);
  783. //mt_udc_flush_fifo (ep_num, USB_DIR_IN);
  784. ep0_state = EP0_IDLE;
  785. writeb(index, INDEX);
  786. return;
  787. }
  788. /*
  789. * If abnormal DATA transfer happened, like USB unplugged,
  790. * we cannot fix this after mt_udc_reset().
  791. * Because sometimes there will come reset twice.
  792. */
  793. static void mt_udc_suspend(void) {
  794. /* handle abnormal DATA transfer if we had any */
  795. struct udc_endpoint *endpoint;
  796. int i;
  797. /* deal with flags */
  798. usb_online = 0;
  799. usb_config_value = 0;
  800. the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE);
  801. /* error out any pending reqs */
  802. for (i = 1; i < MT_EP_NUM; i++) {
  803. /* ensure that ept_complete considers
  804. * this to be an error state
  805. */
  806. #if DBG_USB_GENERAL
  807. DBG_I("%s: ep: %i, in: %s, req: %x\n",
  808. __func__, ep_list[i].num, ep_list[i].in ? "IN" : "OUT", ep_list[i].req);
  809. #endif
  810. if ((ep_list[i].req && (ep_list[i].in == 0)) || /* USB_DIR_OUT */
  811. (ep_list[i].req && (ep_list[i].in == 1))) { /* USB_DIR_IN */
  812. ep_list[i].status = -1; /* HALT */
  813. endpoint = &ep_list[i];
  814. handle_ept_complete(endpoint);
  815. }
  816. }
  817. }
  818. static void mt_udc_rxtxmap_recover(void) {
  819. int i;
  820. for (i = 1; i < MT_EP_NUM; i++) {
  821. if (ep_list[i].num != 0) { /* allocated */
  822. writeb(ep_list[i].num, INDEX);
  823. if (ep_list[i].in == 0) /* USB_DIR_OUT */
  824. writel(ep_list[i].maxpkt, (IECSR + RXMAP));
  825. else
  826. writel(ep_list[i].maxpkt, (IECSR + TXMAP));
  827. }
  828. }
  829. }
  830. static void mt_udc_reset(void) {
  831. /* MUSBHDRC automatically does the following when reset signal is detected */
  832. /* 1. Sets FAddr to 0
  833. * 2. Sets Index to 0
  834. * 3. Flush all endpoint FIFOs
  835. * 4. Clears all control/status registers
  836. * 5. Enables all endpoint interrupts
  837. * 6. Generates a Rest interrupt
  838. */
  839. DBG_I("[USB] %s\n", __func__);
  840. /* disable all endpoint interrupts */
  841. writeb(0, INTRTXE);
  842. writeb(0, INTRRXE);
  843. writeb(0, INTRUSBE);
  844. writew(SWRST_SWRST | SWRST_DISUSBRESET, SWRST);
  845. dev_address = 0;
  846. /* flush FIFO */
  847. mt_udc_flush_fifo(0, USB_DIR_OUT);
  848. mt_udc_flush_fifo(1, USB_DIR_OUT);
  849. mt_udc_flush_fifo(1, USB_DIR_IN);
  850. //mt_udc_flush_fifo (2, USB_DIR_IN);
  851. /* detect USB speed */
  852. if (readb(POWER) & PWR_HS_MODE) {
  853. DBG_I("[USB] USB High Speed\n");
  854. // enable_highspeed();
  855. } else {
  856. DBG_I("[USB] USB Full Speed\n");
  857. }
  858. /* restore RXMAP and TXMAP if the endpoint has been configured */
  859. mt_udc_rxtxmap_recover();
  860. /* enable suspend */
  861. writeb((INTRUSB_SUSPEND | INTRUSB_RESUME | INTRUSB_RESET |INTRUSB_DISCON), INTRUSBE);
  862. }
  863. static void mt_udc_ep0_write(void) {
  864. struct udc_endpoint *endpoint = &ep_list[EP0];
  865. int count = 0;
  866. u16 csr0 = 0;
  867. u8 index = 0;
  868. index = readb(INDEX);
  869. writeb(0, INDEX);
  870. csr0 = readw(IECSR + CSR0);
  871. if (csr0 & EP0_TXPKTRDY) {
  872. DBG_I("mt_udc_ep0_write: ep0 is not ready to be written\n");
  873. return;
  874. }
  875. count = mt_write_fifo(endpoint);
  876. #if DBG_USB_GENERAL
  877. DBG_I("%s: count = %d\n", __func__, count);
  878. #endif
  879. if (count < EP0_MAX_PACKET_SIZE) {
  880. /* last packet */
  881. csr0 |= (EP0_TXPKTRDY | EP0_DATAEND);
  882. ep0_urb->actual_length = 0;
  883. endpoint->sent = 0;
  884. ep0_state = EP0_IDLE;
  885. } else {
  886. /* more packets are waiting to be transferred */
  887. csr0 |= EP0_TXPKTRDY;
  888. }
  889. writew(csr0, IECSR + CSR0);
  890. writeb(index, INDEX);
  891. return;
  892. }
  893. static void mt_udc_ep0_read(void) {
  894. struct udc_endpoint *endpoint = &ep_list[EP0];
  895. int count = 0;
  896. u16 csr0 = 0;
  897. u8 index = 0;
  898. index = readb(INDEX);
  899. writeb(EP0, INDEX);
  900. csr0 = readw(IECSR + CSR0);
  901. /* erroneous ep0 interrupt */
  902. if (!(csr0 & EP0_RXPKTRDY)) {
  903. return;
  904. }
  905. count = mt_read_fifo(endpoint);
  906. if (count <= EP0_MAX_PACKET_SIZE) {
  907. /* last packet */
  908. csr0 |= (EP0_SERVICED_RXPKTRDY | EP0_DATAEND);
  909. ep0_state = EP0_IDLE;
  910. } else {
  911. /* more packets are waiting to be transferred */
  912. csr0 |= EP0_SERVICED_RXPKTRDY;
  913. }
  914. writew(csr0, IECSR + CSR0);
  915. writeb(index, INDEX);
  916. return;
  917. }
  918. static int ep0_standard_setup(struct urb *urb) {
  919. struct setup_packet *request;
  920. struct udc_descriptor *desc;
  921. //struct udc_device *device;
  922. u8 *cp = urb->buffer;
  923. #if 0
  924. if (!urb || !urb->device) {
  925. DBG ("\n!urb || !urb->device\n");
  926. return FALSE;
  927. }
  928. #endif
  929. request = &urb->device_request;
  930. //device = urb->device;
  931. dump_setup_packet("[USB] Device Request\n", request);
  932. if ((request->type & USB_TYPE_MASK) != 0) {
  933. return FALSE; /* Class-specific requests are handled elsewhere */
  934. }
  935. /* handle all requests that return data (direction bit set on bm RequestType) */
  936. if ((request->type & USB_EP_DIR_MASK)) {
  937. /* send the descriptor */
  938. ep0_state = EP0_TX;
  939. switch (request->request) {
  940. /* data stage: from device to host */
  941. case GET_STATUS:
  942. #if DBG_USB_GENERAL
  943. DBG_I("GET_STATUS\n");
  944. #endif
  945. urb->actual_length = 2;
  946. cp[0] = cp[1] = 0;
  947. switch (request->type & USB_RECIP_MASK) {
  948. case USB_RECIP_DEVICE:
  949. cp[0] = USB_STAT_SELFPOWERED;
  950. break;
  951. case USB_RECIP_OTHER:
  952. urb->actual_length = 0;
  953. break;
  954. default:
  955. break;
  956. }
  957. return 0;
  958. case GET_DESCRIPTOR:
  959. #if DBG_USB_GENERAL
  960. DBG_I("GET_DESCRIPTOR\n");
  961. #endif
  962. /* usb_highspeed? */
  963. for (desc = desc_list; desc; desc = desc->next) {
  964. #if DBG_USB_DUMP_DESC
  965. DBG_I("desc->tag: %x: request->value: %x\n", desc->tag, request->value);
  966. #endif
  967. if (desc->tag == request->value) {
  968. #if DBG_USB_DUMP_DESC
  969. DBG_I("Find packet!\n");
  970. #endif
  971. unsigned len = desc->len;
  972. if (len > request->length)
  973. len = request->length;
  974. #if DBG_USB_GENERAL
  975. DBG_I("%s: urb: %x, cp: %p\n", __func__, urb, cp);
  976. #endif
  977. copy_desc(urb, desc->data, len);
  978. return 0;
  979. }
  980. }
  981. /* descriptor lookup failed */
  982. return FALSE;
  983. case GET_CONFIGURATION:
  984. #if DBG_USB_GENERAL
  985. DBG_I("GET_CONFIGURATION\n");
  986. DBG_I("USB_EP_DIR_MASK\n");
  987. #endif
  988. #if 0
  989. urb->actual_length = 1;
  990. ((char *) urb->buffer)[0] = device->configuration;
  991. #endif
  992. // return 0;
  993. break;
  994. case GET_INTERFACE:
  995. #if DBG_USB_GENERAL
  996. DBG_I("GET_INTERFACE\n");
  997. #endif
  998. #if 0
  999. urb->actual_length = 1;
  1000. ((char *) urb->buffer)[0] = device->alternate;
  1001. return 0;
  1002. #endif
  1003. default:
  1004. DBG_C("Unsupported command with TX data stage\n");
  1005. break;
  1006. }
  1007. } else {
  1008. switch (request->request) {
  1009. case SET_ADDRESS:
  1010. #if DBG_USB_GENERAL
  1011. DBG_I("SET_ADDRESS\n");
  1012. #endif
  1013. dev_address = (request->value);
  1014. set_address = 1;
  1015. return 0;
  1016. case SET_CONFIGURATION:
  1017. #if DBG_USB_GENERAL
  1018. DBG_I("SET_CONFIGURATION\n");
  1019. #endif
  1020. #if 0
  1021. device->configuration = (request->value) & 0x7f;
  1022. device->interface = device->alternate = 0;
  1023. #endif
  1024. if (request->value == 1) {
  1025. usb_config_value = 1;
  1026. the_gadget->notify(the_gadget, UDC_EVENT_ONLINE);
  1027. } else {
  1028. usb_config_value = 0;
  1029. the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE);
  1030. }
  1031. usb_online = request->value ? 1 : 0;
  1032. //usb_status(request->value ? 1 : 0, usb_highspeed);
  1033. return 0;
  1034. default:
  1035. DBG_C("Unsupported command with RX data stage\n");
  1036. break;
  1037. }
  1038. }
  1039. return FALSE;
  1040. }
  1041. static void mt_udc_ep0_setup(void) {
  1042. struct udc_endpoint *endpoint = &ep_list[0];
  1043. u8 index;
  1044. u8 stall = 0;
  1045. u16 csr0;
  1046. struct setup_packet *request;
  1047. #ifdef USB_DEBUG
  1048. u16 count;
  1049. #endif
  1050. index = readb(INDEX);
  1051. writeb(0, INDEX);
  1052. /* Read control status register for endpiont 0 */
  1053. csr0 = readw(IECSR + CSR0);
  1054. /* check whether RxPktRdy is set? */
  1055. if (!(csr0 & EP0_RXPKTRDY))
  1056. return;
  1057. /* unload fifo */
  1058. ep0_urb->actual_length = 0;
  1059. #ifndef USB_DEBUG
  1060. mt_read_fifo(endpoint);
  1061. #else
  1062. count = mt_read_fifo(endpoint);
  1063. #if DBG_USB_FIFO
  1064. DBG_I("%s: mt_read_fifo count = %d\n", __func__, count);
  1065. #endif
  1066. #endif
  1067. /* decode command */
  1068. request = &ep0_urb->device_request;
  1069. memcpy(request, ep0_urb->buffer, sizeof(struct setup_packet));
  1070. if (((request->type) & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1071. #if DBG_USB_GENERAL
  1072. DBG_I("[USB] Standard Request\n");
  1073. #endif
  1074. stall = ep0_standard_setup(ep0_urb);
  1075. if (stall) {
  1076. dump_setup_packet("[USB] STANDARD REQUEST NOT SUPPORTED\n", request);
  1077. }
  1078. } else if (((request->type) & USB_TYPE_MASK) == USB_TYPE_CLASS) {
  1079. #if DBG_USB_GENERAL
  1080. DBG_I("[USB] Class-Specific Request\n");
  1081. #endif
  1082. // stall = ep0_class_setup(ep0_urb);
  1083. if (stall) {
  1084. dump_setup_packet("[USB] CLASS REQUEST NOT SUPPORTED\n", request);
  1085. }
  1086. } else if (((request->type) & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
  1087. #if DBG_USB_GENERAL
  1088. DBG_I("[USB] Vendor-Specific Request\n");
  1089. /* do nothing now */
  1090. DBG_I("[USB] ALL VENDOR-SPECIFIC REQUESTS ARE NOT SUPPORTED!!\n");
  1091. #endif
  1092. }
  1093. if (stall) {
  1094. /* the received command is not supported */
  1095. udc_stall_ep(0, USB_DIR_OUT);
  1096. return;
  1097. }
  1098. switch (ep0_state) {
  1099. case EP0_TX:
  1100. /* data stage: from device to host */
  1101. #if DBG_USB_GENERAL
  1102. DBG_I("%s: EP0_TX\n", __func__);
  1103. #endif
  1104. csr0 = readw(IECSR + CSR0);
  1105. csr0 |= (EP0_SERVICED_RXPKTRDY);
  1106. writew(csr0, IECSR + CSR0);
  1107. mt_udc_ep0_write();
  1108. break;
  1109. case EP0_RX:
  1110. /* data stage: from host to device */
  1111. #if DBG_USB_GENERAL
  1112. DBG_I("%s: EP0_RX\n", __func__);
  1113. #endif
  1114. csr0 = readw(IECSR + CSR0);
  1115. csr0 |= (EP0_SERVICED_RXPKTRDY);
  1116. writew(csr0, IECSR + CSR0);
  1117. break;
  1118. case EP0_IDLE:
  1119. /* no data stage */
  1120. #if DBG_USB_GENERAL
  1121. DBG_I("%s: EP0_IDLE\n", __func__);
  1122. #endif
  1123. csr0 = readw(IECSR + CSR0);
  1124. csr0 |= (EP0_SERVICED_RXPKTRDY | EP0_DATAEND);
  1125. writew(csr0, IECSR + CSR0);
  1126. writew(csr0, IECSR + CSR0);
  1127. break;
  1128. default:
  1129. break;
  1130. }
  1131. writeb(index, INDEX);
  1132. return;
  1133. }
  1134. static void mt_udc_ep0_handler(void) {
  1135. u16 csr0;
  1136. u8 index = 0;
  1137. index = readb(INDEX);
  1138. writeb(0, INDEX);
  1139. csr0 = readw(IECSR + CSR0);
  1140. if (csr0 & EP0_SENTSTALL) {
  1141. #if DBG_USB_GENERAL
  1142. DBG_I("USB: [EP0] SENTSTALL\n");
  1143. #endif
  1144. /* needs implementation for exception handling here */
  1145. ep0_state = EP0_IDLE;
  1146. }
  1147. if (csr0 & EP0_SETUPEND) {
  1148. #if DBG_USB_GENERAL
  1149. DBG_I("USB: [EP0] SETUPEND\n");
  1150. #endif
  1151. csr0 |= EP0_SERVICE_SETUP_END;
  1152. writew(csr0, IECSR + CSR0);
  1153. ep0_state = EP0_IDLE;
  1154. }
  1155. switch (ep0_state) {
  1156. case EP0_IDLE:
  1157. #if DBG_USB_GENERAL
  1158. DBG_I("%s: EP0_IDLE\n", __func__);
  1159. #endif
  1160. if (set_address) {
  1161. writeb(dev_address, FADDR);
  1162. set_address = 0;
  1163. }
  1164. mt_udc_ep0_setup();
  1165. break;
  1166. case EP0_TX:
  1167. #if DBG_USB_GENERAL
  1168. DBG_I("%s: EP0_TX\n", __func__);
  1169. #endif
  1170. mt_udc_ep0_write();
  1171. break;
  1172. case EP0_RX:
  1173. #if DBG_USB_GENERAL
  1174. DBG_I("%s: EP0_RX\n", __func__);
  1175. #endif
  1176. mt_udc_ep0_read();
  1177. break;
  1178. default:
  1179. break;
  1180. }
  1181. writeb(index, INDEX);
  1182. return;
  1183. }
  1184. /*
  1185. * udc_setup_ep - setup endpoint
  1186. *
  1187. * Associate a physical endpoint with endpoint_instance and initialize FIFO
  1188. */
  1189. void mt_setup_ep(unsigned int ep, struct udc_endpoint *endpoint) {
  1190. u8 index;
  1191. u16 csr;
  1192. u16 csr0;
  1193. u16 max_packet_size;
  1194. u8 fifosz = 0;
  1195. /* EP table records in bits hence bit 1 is ep0 */
  1196. index = readb(INDEX);
  1197. writeb(ep, INDEX);
  1198. if (ep == EP0) {
  1199. /* Read control status register for endpiont 0 */
  1200. csr0 = readw(IECSR + CSR0);
  1201. /* check whether RxPktRdy is set? */
  1202. if (!(csr0 & EP0_RXPKTRDY))
  1203. return;
  1204. }
  1205. /* Configure endpoint fifo */
  1206. /* Set fifo address, fifo size, and fifo max packet size */
  1207. #if DBG_USB_GENERAL
  1208. DBG_I("%s: endpoint->in: %d, maxpkt: %d\n",
  1209. __func__, endpoint->in, endpoint->maxpkt);
  1210. #endif
  1211. if (endpoint->in == 0) { /* USB_DIR_OUT */
  1212. /* Clear data toggle to 0 */
  1213. csr = readw(IECSR + RXCSR);
  1214. /* pangyen 20090911 */
  1215. csr |= EPX_RX_CLRDATATOG | EPX_RX_FLUSHFIFO;
  1216. writew(csr, IECSR + RXCSR);
  1217. /* Set fifo address */
  1218. writew(fifo_addr >> 3, RXFIFOADD);
  1219. /* Set fifo max packet size */
  1220. max_packet_size = endpoint->maxpkt;
  1221. writew(max_packet_size, IECSR + RXMAP);
  1222. /* Set fifo size (double buffering is currently not enabled) */
  1223. switch (max_packet_size) {
  1224. case 8:
  1225. case 16:
  1226. case 32:
  1227. case 64:
  1228. case 128:
  1229. case 256:
  1230. case 512:
  1231. case 1024:
  1232. case 2048:
  1233. if (endpoint->mode == DOUBLE_BUF)
  1234. fifosz |= FIFOSZ_DPB;
  1235. case 4096:
  1236. fifosz |= uffs(max_packet_size >> 4);
  1237. writeb(fifosz, RXFIFOSZ);
  1238. break;
  1239. case 3072:
  1240. fifosz = uffs(4096 >> 4);
  1241. writeb(fifosz, RXFIFOSZ);
  1242. break;
  1243. default:
  1244. DBG_C("The max_packet_size for ep %d is not supported\n", ep);
  1245. }
  1246. } else {
  1247. /* Clear data toggle to 0 */
  1248. csr = readw(IECSR + TXCSR);
  1249. /* pangyen 20090911 */
  1250. csr |= EPX_TX_CLRDATATOG | EPX_TX_FLUSHFIFO;
  1251. writew(csr, IECSR + TXCSR);
  1252. /* Set fifo address */
  1253. writew(fifo_addr >> 3, TXFIFOADD);
  1254. /* Set fifo max packet size */
  1255. max_packet_size = endpoint->maxpkt;
  1256. writew(max_packet_size, IECSR + TXMAP);
  1257. /* Set fifo size(double buffering is currently not enabled) */
  1258. switch (max_packet_size) {
  1259. case 8:
  1260. case 16:
  1261. case 32:
  1262. case 64:
  1263. case 128:
  1264. case 256:
  1265. case 512:
  1266. case 1024:
  1267. case 2048:
  1268. if (endpoint->mode == DOUBLE_BUF)
  1269. fifosz |= FIFOSZ_DPB;
  1270. case 4096:
  1271. fifosz |= uffs(max_packet_size >> 4);
  1272. writeb(fifosz, TXFIFOSZ);
  1273. break;
  1274. case 3072:
  1275. fifosz = uffs(4096 >> 4);
  1276. writeb(fifosz, TXFIFOSZ);
  1277. break;
  1278. default:
  1279. DBG_C("The max_packet_size for ep %d is not supported\n", ep);
  1280. }
  1281. }
  1282. if (endpoint->mode == DOUBLE_BUF)
  1283. fifo_addr += (max_packet_size << 1);
  1284. else
  1285. fifo_addr += max_packet_size;
  1286. /* recover INDEX register */
  1287. writeb(index, INDEX);
  1288. }
  1289. struct udc_endpoint *_udc_endpoint_alloc(unsigned char num, unsigned char in,
  1290. unsigned short max_pkt) {
  1291. int i;
  1292. /*
  1293. * find an unused slot in ep_list from EP1 to MAX_EP
  1294. * for example, EP1 will use 2 slot one for IN and the other for OUT
  1295. */
  1296. if (num != EP0) {
  1297. for (i = 1; i < MT_EP_NUM; i++) {
  1298. if (ep_list[i].num == 0) /* usable */
  1299. break;
  1300. }
  1301. if (i == MT_EP_NUM) /* ep has been exhausted. */
  1302. return NULL;
  1303. if (in) { /* usb EP1 tx */
  1304. ep_list[i].tx_urb = tx_urb;
  1305. #ifdef USB_DOUBLE_BUF
  1306. ep_list[i].mode = DOUBLE_BUF;
  1307. #endif
  1308. } else { /* usb EP1 rx */
  1309. ep_list[i].rcv_urb = rx_urb;
  1310. #ifdef USB_DOUBLE_BUF
  1311. ep_list[i].mode = DOUBLE_BUF;
  1312. #endif
  1313. }
  1314. } else {
  1315. i = EP0; /* EP0 */
  1316. }
  1317. ep_list[i].maxpkt = max_pkt;
  1318. ep_list[i].num = num;
  1319. ep_list[i].in = in;
  1320. ep_list[i].req = NULL;
  1321. /* store EPT_TX/RX info */
  1322. if (ep_list[i].in) {
  1323. ep_list[i].bit = EPT_TX(num);
  1324. } else {
  1325. ep_list[i].bit = EPT_RX(num);
  1326. }
  1327. /* write parameters to this ep (write to hardware) */
  1328. mt_setup_ep(num, &ep_list[i]);
  1329. DBG_I("[USB] ept%d %s @%p/%p max=%d bit=%x\n",
  1330. num, in ? "in" : "out", &ep_list[i], &ep_list, max_pkt, ep_list[i].bit);
  1331. return &ep_list[i];
  1332. }
  1333. #define SETUP(type,request) (((type) << 8) | (request))
  1334. static unsigned long ept_alloc_table = EPT_TX(0) | EPT_RX(0);
  1335. struct udc_endpoint *udc_endpoint_alloc(unsigned type, unsigned maxpkt)
  1336. {
  1337. struct udc_endpoint *ept;
  1338. unsigned n;
  1339. unsigned in;
  1340. if (type == UDC_TYPE_BULK_IN) {
  1341. in = 1;
  1342. } else if (type == UDC_TYPE_BULK_OUT) {
  1343. in = 0;
  1344. } else {
  1345. return 0;
  1346. }
  1347. /* udc_endpoint_alloc is used for EPx except EP0 */
  1348. for (n = 1; n < 16; n++) {
  1349. unsigned long bit = in ? EPT_TX(n) : EPT_RX(n);
  1350. if (ept_alloc_table & bit)
  1351. continue;
  1352. ept = _udc_endpoint_alloc(n, in, maxpkt);
  1353. if (ept)
  1354. ept_alloc_table |= bit;
  1355. return ept;
  1356. }
  1357. return 0;
  1358. }
  1359. static void handle_ept_complete(struct udc_endpoint *ept)
  1360. {
  1361. unsigned int actual;
  1362. int status;
  1363. struct udc_request *req;
  1364. req = ept->req;
  1365. if (req) {
  1366. #if DBG_USB_GENERAL
  1367. DBG_I("%s: req: %x: req->length: %d: status: %d\n", __func__, req, req->length, ept->status);
  1368. #endif
  1369. /* release this request for processing next */
  1370. ept->req = NULL;
  1371. if (ept->status == -1) {
  1372. actual = 0;
  1373. status = -1;
  1374. DBG_C("%s: EP%d/%s FAIL status: %x\n",
  1375. __func__, ept->num, ept->in ? "in" : "out", status);
  1376. } else {
  1377. actual = req->length;
  1378. status = 0;
  1379. }
  1380. if (req->complete)
  1381. req->complete(req, actual, status);
  1382. }
  1383. }
  1384. static void mt_udc_epx_handler(u8 ep_num, u8 dir)
  1385. {
  1386. u8 index;
  1387. u16 csr;
  1388. u32 count;
  1389. struct udc_endpoint *endpoint;
  1390. struct urb *urb;
  1391. struct udc_request *req; /* for event signaling */
  1392. u8 intrrxe;
  1393. endpoint = mt_find_ep(ep_num, dir);
  1394. index = readb(INDEX);
  1395. writeb(ep_num, INDEX);
  1396. #if DBG_USB_GENERAL
  1397. DBG_I("EP%d Interrupt\n", ep_num);
  1398. DBG_I("dir: %x\n", dir);
  1399. #endif
  1400. switch (dir) {
  1401. case USB_DIR_OUT:
  1402. /* transfer direction is from host to device */
  1403. /* from the view of usb device, it's RX */
  1404. csr = readw(IECSR + RXCSR);
  1405. if (csr & EPX_RX_SENTSTALL) {
  1406. DBG_C("EP %d(RX): STALL\n", ep_num);
  1407. /* exception handling: implement this!! */
  1408. return;
  1409. }
  1410. if (!(csr & EPX_RX_RXPKTRDY)) {
  1411. #if DBG_USB_GENERAL
  1412. DBG_I("EP %d: ERRONEOUS INTERRUPT\n", ep_num); // normal
  1413. #endif
  1414. return;
  1415. }
  1416. //DBG_C("mt_read_fifo, start\n");
  1417. count = mt_read_fifo(endpoint);
  1418. //DBG_C("mt_read_fifo, end\n");
  1419. #if DBG_USB_GENERAL
  1420. DBG_I("EP%d(RX), count = %d\n", ep_num, count);
  1421. #endif
  1422. csr &= ~EPX_RX_RXPKTRDY;
  1423. writew(csr, IECSR + RXCSR);
  1424. if (readw(IECSR + RXCSR) & EPX_RX_RXPKTRDY) {
  1425. #if DBG_USB_GENERAL
  1426. DBG_I("%s: rxpktrdy clear failed\n", __func__);
  1427. #endif
  1428. }
  1429. /* do signaling */
  1430. req = endpoint->req;
  1431. /* workaround: if req->lenth == 64 bytes (not huge data transmission)
  1432. * do normal return */
  1433. #if DBG_USB_GENERAL
  1434. DBG_I("%s: req->length: %x, endpoint->rcv_urb->actual_length: %x\n",
  1435. __func__, req->length, endpoint->rcv_urb->actual_length);
  1436. #endif
  1437. /* Deal with FASTBOOT command */
  1438. if ((req->length >= endpoint->rcv_urb->actual_length) && req->length == 64) {
  1439. req->length = count;
  1440. /* mask EPx INTRRXE */
  1441. /* The buffer is passed from the AP caller.
  1442. * It happens that AP is dealing with the buffer filled data by driver,
  1443. * but the driver is still receiving the next data packets onto the buffer.
  1444. * Data corrupted happens if the every request use the same buffer.
  1445. * Mask the EPx to ensure that AP and driver are not accessing the buffer parallely.
  1446. */
  1447. intrrxe = readb(INTRRXE);
  1448. writeb((intrrxe &= ~(1 << ep_num)), INTRRXE);
  1449. }
  1450. /* Deal with DATA transfer */
  1451. if ((req->length == endpoint->rcv_urb->actual_length) ||
  1452. ((req->length >= endpoint->rcv_urb->actual_length) && req->length == 64)) {
  1453. handle_ept_complete(endpoint);
  1454. /* mask EPx INTRRXE */
  1455. /* The buffer is passed from the AP caller.
  1456. * It happens that AP is dealing with the buffer filled data by driver,
  1457. * but the driver is still receiving the next data packets onto the buffer.
  1458. * Data corrupted happens if the every request use the same buffer.
  1459. * Mask the EPx to ensure that AP and driver are not accessing the buffer parallely.
  1460. */
  1461. intrrxe = readb(INTRRXE);
  1462. writeb((intrrxe &= ~(1 << ep_num)), INTRRXE);
  1463. }
  1464. break;
  1465. case USB_DIR_IN:
  1466. /* transfer direction is from device to host */
  1467. /* from the view of usb device, it's tx */
  1468. csr = readw(IECSR + TXCSR);
  1469. if (csr & EPX_TX_SENTSTALL) {
  1470. DBG_C("EP %d(TX): STALL\n", ep_num);
  1471. endpoint->status = -1;
  1472. handle_ept_complete(endpoint);
  1473. /* exception handling: implement this!! */
  1474. return;
  1475. }
  1476. if (csr & EPX_TX_TXPKTRDY) {
  1477. DBG_C
  1478. ("mt_udc_epx_handler: ep%d is not ready to be written\n",
  1479. ep_num);
  1480. return;
  1481. }
  1482. urb = endpoint->tx_urb;
  1483. if (endpoint->sent == urb->actual_length) {
  1484. /* do signaling */
  1485. handle_ept_complete(endpoint);
  1486. break;
  1487. }
  1488. /* send next packet of the same urb */
  1489. count = mt_write_fifo(endpoint);
  1490. #if DBG_USB_GENERAL
  1491. DBG_I("EP%d(TX), count = %d\n", ep_num, endpoint->sent);
  1492. #endif
  1493. if (count != 0) {
  1494. /* not the interrupt generated by the last tx packet of the transfer */
  1495. csr |= EPX_TX_TXPKTRDY;
  1496. writew(csr, IECSR + TXCSR);
  1497. }
  1498. break;
  1499. default:
  1500. break;
  1501. }
  1502. writeb(index, INDEX);
  1503. return;
  1504. }
  1505. void mt_udc_irq(u8 intrtx, u8 intrrx, u8 intrusb) {
  1506. int i;
  1507. DBG_IRQ("[USB] INTERRUPT\n");
  1508. if (intrusb) {
  1509. if (intrusb & INTRUSB_RESUME) {
  1510. DBG_IRQ("[USB] INTRUSB: RESUME\n");
  1511. }
  1512. if (intrusb & INTRUSB_SESS_REQ) {
  1513. DBG_IRQ("[USB] INTRUSB: SESSION REQUEST\n");
  1514. }
  1515. if (intrusb & INTRUSB_VBUS_ERROR) {
  1516. DBG_IRQ("[USB] INTRUSB: VBUS ERROR\n");
  1517. }
  1518. if (intrusb & INTRUSB_SUSPEND) {
  1519. DBG_IRQ("[USB] INTRUSB: SUSPEND\n");
  1520. mt_udc_suspend();
  1521. }
  1522. if (intrusb & INTRUSB_CONN) {
  1523. DBG_IRQ("[USB] INTRUSB: CONNECT\n");
  1524. }
  1525. if (intrusb & INTRUSB_DISCON) {
  1526. DBG_IRQ("[USB] INTRUSB: DISCONNECT\n");
  1527. }
  1528. if (intrusb & INTRUSB_RESET) {
  1529. DBG_IRQ("[USB] INTRUSB: RESET\n");
  1530. mt_udc_reset();
  1531. }
  1532. if (intrusb & INTRUSB_SOF) {
  1533. DBG_IRQ("[USB] INTRUSB: SOF\n");
  1534. }
  1535. }
  1536. /* endpoint 0 interrupt? */
  1537. if (intrtx & EPMASK (0)) {
  1538. mt_udc_ep0_handler();
  1539. intrtx &= ~0x1;
  1540. }
  1541. if (intrtx) {
  1542. for (i = 1; i < MT_EP_NUM; i++) {
  1543. if (intrtx & EPMASK (i)) {
  1544. mt_udc_epx_handler(i, USB_DIR_IN);
  1545. }
  1546. }
  1547. }
  1548. if (intrrx) {
  1549. for (i = 1; i < MT_EP_NUM; i++) {
  1550. if (intrrx & EPMASK (i)) {
  1551. mt_udc_epx_handler(i, USB_DIR_OUT);
  1552. }
  1553. }
  1554. }
  1555. }
  1556. void service_interrupts(void)
  1557. {
  1558. volatile u8 intrtx, intrrx, intrusb;
  1559. /* polling interrupt status for incoming interrupts and service it */
  1560. intrtx = readb(INTRTX) & readb(INTRTXE);
  1561. intrrx = readb(INTRRX) & readb(INTRRXE);
  1562. intrusb = readb(INTRUSB) & readb(INTRUSBE);
  1563. writeb(intrtx, INTRTX);
  1564. writeb(intrrx, INTRRX);
  1565. writeb(intrusb, INTRUSB);
  1566. intrusb &= ~INTRUSB_SOF;
  1567. if (intrtx | intrrx | intrusb) {
  1568. mt_udc_irq(intrtx, intrrx, intrusb);
  1569. }
  1570. }
  1571. void lk_usb_scheduler(void)
  1572. {
  1573. mt_irq_ack(MT_USB0_IRQ_ID);
  1574. service_interrupts();
  1575. #if 0
  1576. static enum handler_return ret;
  1577. ret = INT_RESCHEDULE;
  1578. if (ret == INT_RESCHEDULE) {
  1579. thread_preempt();
  1580. }
  1581. #endif
  1582. return;
  1583. }
  1584. int mt_usb_irq_init(void) {
  1585. /* disable all endpoint interrupts */
  1586. writeb(0, INTRTXE);
  1587. writeb(0, INTRRXE);
  1588. writeb(0, INTRUSBE);
  1589. /* 2. Ack all gpt irq if needed */
  1590. //writel(0x3F, GPT_IRQ_ACK);
  1591. /* 3. Register usb irq */
  1592. mt_irq_set_sens(MT_USB0_IRQ_ID, MT65xx_LEVEL_SENSITIVE);
  1593. mt_irq_set_polarity(MT_USB0_IRQ_ID, MT65xx_POLARITY_LOW);
  1594. return 0;
  1595. }
  1596. /* Turn on the USB connection by enabling the pullup resistor */
  1597. void mt_usb_connect_internal(void)
  1598. {
  1599. u8 tmpReg8;
  1600. /* connect */
  1601. tmpReg8 = readb(POWER);
  1602. tmpReg8 |= PWR_SOFT_CONN;
  1603. tmpReg8 |= PWR_ENABLE_SUSPENDM;
  1604. #ifdef USB_FORCE_FULL_SPEED
  1605. tmpReg8 &= ~PWR_HS_ENAB;
  1606. #else
  1607. tmpReg8 |= PWR_HS_ENAB;
  1608. #endif
  1609. writeb(tmpReg8, POWER);
  1610. }
  1611. /* Turn off the USB connection by disabling the pullup resistor */
  1612. void mt_usb_disconnect_internal(void)
  1613. {
  1614. u8 tmpReg8;
  1615. /* connect */
  1616. tmpReg8 = readb(POWER);
  1617. tmpReg8 &= ~PWR_SOFT_CONN;
  1618. writeb(tmpReg8, POWER);
  1619. }
  1620. int udc_init(struct udc_device *dev)
  1621. {
  1622. struct udc_descriptor *desc = NULL;
  1623. #ifdef USB_GINTR
  1624. #ifdef USB_HSDMA_ISR
  1625. u32 usb_dmaintr;
  1626. #endif
  1627. u32 usb_l1intm;
  1628. #endif
  1629. DBG_I("[USB] %s:\n", __func__);
  1630. DBG_I("[USB] ep0_urb: %p\n", ep0_urb);
  1631. /* RESET */
  1632. /*
  1633. * mt_usb_disconnect_internal can be removed:
  1634. * if we call udc_stop() in cmd_reboot and cmd_reboot_bootloader.
  1635. */
  1636. //mt_usb_disconnect_internal();
  1637. thread_sleep(20);
  1638. #if 0 /* Wait for HW designer's instruction (depends on tapeout process) */
  1639. //#if !CFG_FPGA_PLATFORM
  1640. //ALPS00427972, implement the analog register formula
  1641. //Set the calibration after power on
  1642. //Add here for eFuse, chip version checking -> analog register calibration
  1643. int input_reg = INREG16(M_HW_RES3);
  1644. #if DBG_PHY_CALIBRATION
  1645. DBG_I("%s: input_reg = 0x%x \n", __func__, input_reg);
  1646. #endif
  1647. int term_vref = (input_reg & RG_USB20_TERM_VREF_SEL_MASK) >> 13; //0xE000 //0b 1110,0000,0000,0000 15~13
  1648. int clkref = (input_reg & RG_USB20_CLKREF_REF_MASK) >> 10; //0x1C00 //0b 0001,1100,0000,0000 12~10
  1649. int vrt_vref = (input_reg & RG_USB20_VRT_VREF_SEL_MASK) >> 7; //0x0380 //0b 0000,0011,1000,0000 9~7
  1650. #if DBG_PHY_CALIBRATION
  1651. DBG_I("%s: term_vref = 0x%x, clkref = 0x%x, vrt_vref = 0x%x,\n", __func__, term_vref, clkref, vrt_vref);
  1652. #endif
  1653. if (term_vref)
  1654. mt_usb_phy_calibraion(1, term_vref);
  1655. if (clkref)
  1656. mt_usb_phy_calibraion(2, clkref);
  1657. if (vrt_vref)
  1658. mt_usb_phy_calibraion(3, vrt_vref);
  1659. //ALPS00427972, implement the analog register formula
  1660. //#endif
  1661. #endif
  1662. #if 0
  1663. if (get_devinfo_with_index(14) & (0x01<<22))
  1664. {
  1665. USBPHY_CLR8(0x00, 0x20);
  1666. }
  1667. if (get_devinfo_with_index(14) & (0x07<<19))
  1668. {
  1669. //RG_USB20_VRT_VREF_SEL[2:0]=5 (ori:4) (0x11110804[14:12])
  1670. USBPHY_CLR8(0x05, 0x70);
  1671. USBPHY_SET8(0x05, ((get_devinfo_with_index(7)>>19)<<4) & (0x70));
  1672. }
  1673. #endif
  1674. /* usb phy init */
  1675. board_usb_init();
  1676. mt_usb_phy_recover();
  1677. thread_sleep(20);
  1678. /* allocate ep0 */
  1679. ep0out = _udc_endpoint_alloc(EP0, 0, EP0_MAX_PACKET_SIZE);
  1680. ep0in = _udc_endpoint_alloc(EP0, 1, EP0_MAX_PACKET_SIZE);
  1681. ep0req = udc_request_alloc();
  1682. ep0req->buf = malloc(4096);
  1683. ep0_urb->buffer = ep0_buf;
  1684. {
  1685. /* create and register a language table descriptor */
  1686. /* language 0x0409 is US English */
  1687. desc = udc_descriptor_alloc(TYPE_STRING, EP0, 4);
  1688. desc->data[2] = 0x09;
  1689. desc->data[3] = 0x04;
  1690. udc_descriptor_register(desc);
  1691. }
  1692. #ifdef USB_HSDMA_ISR
  1693. /* setting HSDMA interrupt register */
  1694. usb_dmaintr = (0xff | 0xff << USB_DMA_INTR_UNMASK_SET_OFFSET);
  1695. writel(usb_dmaintr, USB_DMA_INTR);
  1696. #endif
  1697. #ifdef USB_GINTR
  1698. usb_l1intm = (TX_INT_STATUS | RX_INT_STATUS | USBCOM_INT_STATUS | DMA_INT_STATUS);
  1699. writel(usb_l1intm, USB_L1INTM);
  1700. #endif
  1701. the_device = dev;
  1702. return 0;
  1703. }
  1704. void udc_endpoint_free(struct udc_endpoint *ept)
  1705. {
  1706. /* todo */
  1707. }
  1708. struct udc_request *udc_request_alloc(void)
  1709. {
  1710. struct udc_request *req;
  1711. req = malloc(sizeof(*req));
  1712. req->buf = NULL;
  1713. req->length = 0;
  1714. return req;
  1715. }
  1716. void udc_request_free(struct udc_request *req)
  1717. {
  1718. free(req);
  1719. }
  1720. /* Called to start packet transmission. */
  1721. /* It must be applied in udc_request_queue when polling mode is used.
  1722. * (When USB_GINTR is undefined).
  1723. * If interrupt mode is used, you can use
  1724. * mt_udc_epx_handler(ept->num, USB_DIR_IN); to replace mt_ep_write make ISR
  1725. * do it for you.
  1726. */
  1727. static int mt_ep_write(struct udc_endpoint *endpoint)
  1728. {
  1729. int ep_num = endpoint->num;
  1730. int count;
  1731. u8 index;
  1732. u16 csr;
  1733. index = readb(INDEX);
  1734. writeb(ep_num, INDEX);
  1735. /* udc_endpoint_write: cannot write ep0 */
  1736. if (ep_num == 0)
  1737. return FALSE;
  1738. /* udc_endpoint_write: cannot write USB_DIR_OUT */
  1739. if (endpoint->in == 0)
  1740. return FALSE;
  1741. csr = readw(IECSR + TXCSR);
  1742. if (csr & EPX_TX_TXPKTRDY) {
  1743. #if DBG_USB_GENERAL
  1744. DBG_I("[USB]: udc_endpoint_write: ep%d is not ready to be written\n",
  1745. ep_num);
  1746. #endif
  1747. return FALSE;
  1748. }
  1749. count = mt_write_fifo(endpoint);
  1750. csr |= EPX_TX_TXPKTRDY;
  1751. writew(csr, IECSR + TXCSR);
  1752. writeb(index, INDEX);
  1753. return count;
  1754. }
  1755. int udc_request_queue(struct udc_endpoint *ept, struct udc_request *req) {
  1756. u8 intrrxe;
  1757. #if DBG_USB_GENERAL
  1758. DBG_I("[USB] %s: ept%d %s queue req=%p, req->length=%x\n",
  1759. __func__, ept->num, ept->in ? "in" : "out", req, req->length);
  1760. DBG_I("[USB] %s: ept%d: %x, ept->in: %s, ept->rcv_urb->buffer: %x, req->buf: %x\n",
  1761. __func__, ept->num, ept, ept->in ? "IN" : "OUT" , ept->rcv_urb->buffer, req->buf);
  1762. #endif
  1763. enter_critical_section();
  1764. ept->req = req;
  1765. ept->status = 0; /* ACTIVE */
  1766. ept->sent = 0;
  1767. ept->last = 0;
  1768. /* read */
  1769. if (!ept->in) {
  1770. ept->rcv_urb->buffer = req->buf;
  1771. ept->rcv_urb->actual_length = 0;
  1772. /* unmask EPx INTRRXE */
  1773. /*
  1774. * To avoid the parallely access the buffer,
  1775. * it is umasked here and umask at complete.
  1776. */
  1777. intrrxe = readb(INTRRXE);
  1778. intrrxe |= (1 << ept->num);
  1779. writeb(intrrxe, INTRRXE);
  1780. }
  1781. /* write */
  1782. if (ept->in) {
  1783. ept->tx_urb->buffer = req->buf;
  1784. ept->tx_urb->actual_length = req->length;
  1785. mt_ep_write(ept);
  1786. }
  1787. exit_critical_section();
  1788. return 0;
  1789. }
  1790. #if 0
  1791. enum handler_return udc_interrupt(void *arg)
  1792. {
  1793. struct udc_endpoint *ept;
  1794. unsigned ret;
  1795. return ret;
  1796. }
  1797. #endif
  1798. int udc_register_gadget(struct udc_gadget *gadget)
  1799. {
  1800. if (the_gadget) {
  1801. DBG_C("only one gadget supported\n");
  1802. return FALSE;
  1803. }
  1804. the_gadget = gadget;
  1805. return 0;
  1806. }
  1807. static void udc_ept_desc_fill(struct udc_endpoint *ept, unsigned char *data)
  1808. {
  1809. data[0] = 7;
  1810. data[1] = TYPE_ENDPOINT;
  1811. data[2] = ept->num | (ept->in ? USB_DIR_IN : USB_DIR_OUT);
  1812. data[3] = 0x02; /* bulk -- the only kind we support */
  1813. data[4] = ept->maxpkt;
  1814. data[5] = ept->maxpkt >> 8;
  1815. data[6] = ept->in ? 0x00 : 0x01;
  1816. }
  1817. static unsigned udc_ifc_desc_size(struct udc_gadget *g)
  1818. {
  1819. return 9 + g->ifc_endpoints * 7;
  1820. }
  1821. static void udc_ifc_desc_fill(struct udc_gadget *g, unsigned char *data)
  1822. {
  1823. unsigned n;
  1824. data[0] = 0x09;
  1825. data[1] = TYPE_INTERFACE;
  1826. data[2] = 0x00; /* ifc number */
  1827. data[3] = 0x00; /* alt number */
  1828. data[4] = g->ifc_endpoints;
  1829. data[5] = g->ifc_class;
  1830. data[6] = g->ifc_subclass;
  1831. data[7] = g->ifc_protocol;
  1832. data[8] = udc_string_desc_alloc(g->ifc_string);
  1833. data += 9;
  1834. for (n = 0; n < g->ifc_endpoints; n++) {
  1835. udc_ept_desc_fill(g->ept[n], data);
  1836. data += 7;
  1837. }
  1838. }
  1839. int udc_start(void)
  1840. {
  1841. struct udc_descriptor *desc;
  1842. unsigned char *data;
  1843. unsigned size;
  1844. DBG_C("[USB] %s\n", __func__);
  1845. if (!the_device) {
  1846. DBG_C("udc cannot start before init\n");
  1847. return FALSE;
  1848. }
  1849. if (!the_gadget) {
  1850. DBG_C("udc has no gadget registered\n");
  1851. return FALSE;
  1852. }
  1853. /* create our device descriptor */
  1854. desc = udc_descriptor_alloc(TYPE_DEVICE, EP0, 18);
  1855. data = desc->data;
  1856. data[2] = 0x00; /* usb spec minor rev */
  1857. data[3] = 0x02; /* usb spec major rev */
  1858. data[4] = 0x00; /* class */
  1859. data[5] = 0x00; /* subclass */
  1860. data[6] = 0x00; /* protocol */
  1861. data[7] = 0x40; /* max packet size on ept 0 */
  1862. memcpy(data + 8, &the_device->vendor_id, sizeof(short));
  1863. memcpy(data + 10, &the_device->product_id, sizeof(short));
  1864. memcpy(data + 12, &the_device->version_id, sizeof(short));
  1865. data[14] = udc_string_desc_alloc(the_device->manufacturer);
  1866. data[15] = udc_string_desc_alloc(the_device->product);
  1867. data[16] = udc_string_desc_alloc(the_device->serialno);
  1868. data[17] = 1; /* number of configurations */
  1869. udc_descriptor_register(desc);
  1870. /* create our configuration descriptor */
  1871. size = 9 + udc_ifc_desc_size(the_gadget);
  1872. desc = udc_descriptor_alloc(TYPE_CONFIGURATION, EP0, size);
  1873. data = desc->data;
  1874. data[0] = 0x09;
  1875. data[2] = size;
  1876. data[3] = size >> 8;
  1877. data[4] = 0x01; /* number of interfaces */
  1878. data[5] = 0x01; /* configuration value */
  1879. data[6] = 0x00; /* configuration string */
  1880. data[7] = 0x80; /* attributes */
  1881. data[8] = 0x80; /* max power (250ma) -- todo fix this */
  1882. udc_ifc_desc_fill(the_gadget, data + 9);
  1883. udc_descriptor_register(desc);
  1884. #if DBG_USB_DUMP_DESC
  1885. DBG_I("%s: dump desc_list\n", __func__);
  1886. for (desc = desc_list; desc; desc = desc->next) {
  1887. DBG_I("tag: %04x\n", desc->tag);
  1888. DBG_I("len: %d\n", desc->len);
  1889. DBG_I("data:");
  1890. hexdump8(desc->data, desc->len);
  1891. }
  1892. #endif
  1893. /* register interrupt handler */
  1894. mt_usb_irq_init();
  1895. /* go to RUN mode */
  1896. mt_usb_phy_recover();
  1897. /* clear INTRTX, INTRRX and INTRUSB */
  1898. writew(0xffff, INTRTX); /* writew */
  1899. writew(0xffff, INTRRX); /* writew */
  1900. writeb(0xff, INTRUSB); /* writeb */
  1901. /* unmask usb irq */
  1902. #ifdef USB_GINTR
  1903. mt_irq_unmask(MT_USB0_IRQ_ID);
  1904. #endif
  1905. writeb((INTRUSB_SUSPEND | INTRUSB_RESUME | INTRUSB_RESET |INTRUSB_DISCON), INTRUSBE);
  1906. /* enable the pullup resistor */
  1907. mt_usb_connect_internal();
  1908. while (1) {
  1909. #ifdef USB_GINTR
  1910. thread_sleep(1);
  1911. #else
  1912. service_interrupts();
  1913. #endif
  1914. }
  1915. return 0;
  1916. }
  1917. int udc_stop(void)
  1918. {
  1919. thread_sleep(10);
  1920. mt_usb_disconnect_internal();
  1921. mt_usb_phy_savecurrent();
  1922. return 0;
  1923. }