isp116x-hcd.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  1. /*
  2. * ISP116x HCD (Host Controller Driver) for u-boot.
  3. *
  4. * Copyright (C) 2006-2007 Rodolfo Giometti <giometti@linux.it>
  5. * Copyright (C) 2006-2007 Eurotech S.p.A. <info@eurotech.it>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. *
  22. *
  23. * Derived in part from the SL811 HCD driver "u-boot/drivers/usb/sl811_usb.c"
  24. * (original copyright message follows):
  25. *
  26. * (C) Copyright 2004
  27. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  28. *
  29. * This code is based on linux driver for sl811hs chip, source at
  30. * drivers/usb/host/sl811.c:
  31. *
  32. * SL811 Host Controller Interface driver for USB.
  33. *
  34. * Copyright (c) 2003/06, Courage Co., Ltd.
  35. *
  36. * Based on:
  37. * 1.uhci.c by Linus Torvalds, Johannes Erdfelt, Randy Dunlap,
  38. * Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber,
  39. * Adam Richter, Gregory P. Smith;
  40. * 2.Original SL811 driver (hc_sl811.o) by Pei Liu <pbl@cypress.com>
  41. * 3.Rewrited as sl811.o by Yin Aihua <yinah:couragetech.com.cn>
  42. *
  43. * [[GNU/GPL disclaimer]]
  44. *
  45. * and in part from AU1x00 OHCI HCD driver "u-boot/cpu/mips/au1x00_usb_ohci.c"
  46. * (original copyright message follows):
  47. *
  48. * URB OHCI HCD (Host Controller Driver) for USB on the AU1x00.
  49. *
  50. * (C) Copyright 2003
  51. * Gary Jennejohn, DENX Software Engineering <gj@denx.de>
  52. *
  53. * [[GNU/GPL disclaimer]]
  54. *
  55. * Note: Part of this code has been derived from linux
  56. */
  57. #include <common.h>
  58. #ifdef CONFIG_USB_ISP116X_HCD
  59. #include <asm/io.h>
  60. #include <usb.h>
  61. #include <malloc.h>
  62. #include <linux/list.h>
  63. /*
  64. * ISP116x chips require certain delays between accesses to its
  65. * registers. The following timing options exist.
  66. *
  67. * 1. Configure your memory controller (the best)
  68. * 2. Use ndelay (easiest, poorest). For that, enable the following macro.
  69. *
  70. * Value is in microseconds.
  71. */
  72. #ifdef ISP116X_HCD_USE_UDELAY
  73. #define UDELAY 1
  74. #endif
  75. /*
  76. * On some (slowly?) machines an extra delay after data packing into
  77. * controller's FIFOs is required, * otherwise you may get the following
  78. * error:
  79. *
  80. * uboot> usb start
  81. * (Re)start USB...
  82. * USB: scanning bus for devices... isp116x: isp116x_submit_job: CTL:TIMEOUT
  83. * isp116x: isp116x_submit_job: ****** FIFO not ready! ******
  84. *
  85. * USB device not responding, giving up (status=4)
  86. * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
  87. * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
  88. * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
  89. * 3 USB Device(s) found
  90. * scanning bus for storage devices... 0 Storage Device(s) found
  91. *
  92. * Value is in milliseconds.
  93. */
  94. #ifdef ISP116X_HCD_USE_EXTRA_DELAY
  95. #define EXTRA_DELAY 2
  96. #endif
  97. /*
  98. * Enable the following defines if you wish enable debugging messages.
  99. */
  100. #undef DEBUG /* enable debugging messages */
  101. #undef TRACE /* enable tracing code */
  102. #undef VERBOSE /* verbose debugging messages */
  103. #include "isp116x.h"
  104. #define DRIVER_VERSION "08 Jan 2007"
  105. static const char hcd_name[] = "isp116x-hcd";
  106. struct isp116x isp116x_dev;
  107. struct isp116x_platform_data isp116x_board;
  108. static int got_rhsc; /* root hub status change */
  109. struct usb_device *devgone; /* device which was disconnected */
  110. static int rh_devnum; /* address of Root Hub endpoint */
  111. /* ------------------------------------------------------------------------- */
  112. #define ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
  113. #define min_t(type,x,y) \
  114. ({ type __x = (x); type __y = (y); __x < __y ? __x : __y; })
  115. /* ------------------------------------------------------------------------- */
  116. static int isp116x_reset(struct isp116x *isp116x);
  117. /* --- Debugging functions ------------------------------------------------- */
  118. #define isp116x_show_reg(d, r) { \
  119. if ((r) < 0x20) { \
  120. DBG("%-12s[%02x]: %08x", #r, \
  121. r, isp116x_read_reg32(d, r)); \
  122. } else { \
  123. DBG("%-12s[%02x]: %04x", #r, \
  124. r, isp116x_read_reg16(d, r)); \
  125. } \
  126. }
  127. #define isp116x_show_regs(d) { \
  128. isp116x_show_reg(d, HCREVISION); \
  129. isp116x_show_reg(d, HCCONTROL); \
  130. isp116x_show_reg(d, HCCMDSTAT); \
  131. isp116x_show_reg(d, HCINTSTAT); \
  132. isp116x_show_reg(d, HCINTENB); \
  133. isp116x_show_reg(d, HCFMINTVL); \
  134. isp116x_show_reg(d, HCFMREM); \
  135. isp116x_show_reg(d, HCFMNUM); \
  136. isp116x_show_reg(d, HCLSTHRESH); \
  137. isp116x_show_reg(d, HCRHDESCA); \
  138. isp116x_show_reg(d, HCRHDESCB); \
  139. isp116x_show_reg(d, HCRHSTATUS); \
  140. isp116x_show_reg(d, HCRHPORT1); \
  141. isp116x_show_reg(d, HCRHPORT2); \
  142. isp116x_show_reg(d, HCHWCFG); \
  143. isp116x_show_reg(d, HCDMACFG); \
  144. isp116x_show_reg(d, HCXFERCTR); \
  145. isp116x_show_reg(d, HCuPINT); \
  146. isp116x_show_reg(d, HCuPINTENB); \
  147. isp116x_show_reg(d, HCCHIPID); \
  148. isp116x_show_reg(d, HCSCRATCH); \
  149. isp116x_show_reg(d, HCITLBUFLEN); \
  150. isp116x_show_reg(d, HCATLBUFLEN); \
  151. isp116x_show_reg(d, HCBUFSTAT); \
  152. isp116x_show_reg(d, HCRDITL0LEN); \
  153. isp116x_show_reg(d, HCRDITL1LEN); \
  154. }
  155. #if defined(TRACE)
  156. static int isp116x_get_current_frame_number(struct usb_device *usb_dev)
  157. {
  158. struct isp116x *isp116x = &isp116x_dev;
  159. return isp116x_read_reg32(isp116x, HCFMNUM);
  160. }
  161. static void dump_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  162. int len, char *str)
  163. {
  164. #if defined(VERBOSE)
  165. int i;
  166. #endif
  167. DBG("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d stat:%#lx",
  168. str,
  169. isp116x_get_current_frame_number(dev),
  170. usb_pipedevice(pipe),
  171. usb_pipeendpoint(pipe),
  172. usb_pipeout(pipe) ? 'O' : 'I',
  173. usb_pipetype(pipe) < 2 ?
  174. (usb_pipeint(pipe) ?
  175. "INTR" : "ISOC") :
  176. (usb_pipecontrol(pipe) ? "CTRL" : "BULK"), len, dev->status);
  177. #if defined(VERBOSE)
  178. if (len > 0 && buffer) {
  179. printf(__FILE__ ": data(%d):", len);
  180. for (i = 0; i < 16 && i < len; i++)
  181. printf(" %02x", ((__u8 *) buffer)[i]);
  182. printf("%s\n", i < len ? "..." : "");
  183. }
  184. #endif
  185. }
  186. #define PTD_DIR_STR(ptd) ({char __c; \
  187. switch(PTD_GET_DIR(ptd)){ \
  188. case 0: __c = 's'; break; \
  189. case 1: __c = 'o'; break; \
  190. default: __c = 'i'; break; \
  191. }; __c;})
  192. /*
  193. Dump PTD info. The code documents the format
  194. perfectly, right :)
  195. */
  196. static inline void dump_ptd(struct ptd *ptd)
  197. {
  198. #if defined(VERBOSE)
  199. int k;
  200. #endif
  201. DBG("PTD(ext) : cc:%x %d%c%d %d,%d,%d t:%x %x%x%x",
  202. PTD_GET_CC(ptd),
  203. PTD_GET_FA(ptd), PTD_DIR_STR(ptd), PTD_GET_EP(ptd),
  204. PTD_GET_COUNT(ptd), PTD_GET_LEN(ptd), PTD_GET_MPS(ptd),
  205. PTD_GET_TOGGLE(ptd),
  206. PTD_GET_ACTIVE(ptd), PTD_GET_SPD(ptd), PTD_GET_LAST(ptd));
  207. #if defined(VERBOSE)
  208. printf("isp116x: %s: PTD(byte): ", __FUNCTION__);
  209. for (k = 0; k < sizeof(struct ptd); ++k)
  210. printf("%02x ", ((u8 *) ptd)[k]);
  211. printf("\n");
  212. #endif
  213. }
  214. static inline void dump_ptd_data(struct ptd *ptd, u8 * buf, int type)
  215. {
  216. #if defined(VERBOSE)
  217. int k;
  218. if (type == 0 /* 0ut data */ ) {
  219. printf("isp116x: %s: out data: ", __FUNCTION__);
  220. for (k = 0; k < PTD_GET_LEN(ptd); ++k)
  221. printf("%02x ", ((u8 *) buf)[k]);
  222. printf("\n");
  223. }
  224. if (type == 1 /* 1n data */ ) {
  225. printf("isp116x: %s: in data: ", __FUNCTION__);
  226. for (k = 0; k < PTD_GET_COUNT(ptd); ++k)
  227. printf("%02x ", ((u8 *) buf)[k]);
  228. printf("\n");
  229. }
  230. if (PTD_GET_LAST(ptd))
  231. DBG("--- last PTD ---");
  232. #endif
  233. }
  234. #else
  235. #define dump_msg(dev, pipe, buffer, len, str) do { } while (0)
  236. #define dump_pkt(dev, pipe, buffer, len, setup, str, small) do {} while (0)
  237. #define dump_ptd(ptd) do {} while (0)
  238. #define dump_ptd_data(ptd, buf, type) do {} while (0)
  239. #endif
  240. /* --- Virtual Root Hub ---------------------------------------------------- */
  241. /* Device descriptor */
  242. static __u8 root_hub_dev_des[] = {
  243. 0x12, /* __u8 bLength; */
  244. 0x01, /* __u8 bDescriptorType; Device */
  245. 0x10, /* __u16 bcdUSB; v1.1 */
  246. 0x01,
  247. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  248. 0x00, /* __u8 bDeviceSubClass; */
  249. 0x00, /* __u8 bDeviceProtocol; */
  250. 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
  251. 0x00, /* __u16 idVendor; */
  252. 0x00,
  253. 0x00, /* __u16 idProduct; */
  254. 0x00,
  255. 0x00, /* __u16 bcdDevice; */
  256. 0x00,
  257. 0x00, /* __u8 iManufacturer; */
  258. 0x01, /* __u8 iProduct; */
  259. 0x00, /* __u8 iSerialNumber; */
  260. 0x01 /* __u8 bNumConfigurations; */
  261. };
  262. /* Configuration descriptor */
  263. static __u8 root_hub_config_des[] = {
  264. 0x09, /* __u8 bLength; */
  265. 0x02, /* __u8 bDescriptorType; Configuration */
  266. 0x19, /* __u16 wTotalLength; */
  267. 0x00,
  268. 0x01, /* __u8 bNumInterfaces; */
  269. 0x01, /* __u8 bConfigurationValue; */
  270. 0x00, /* __u8 iConfiguration; */
  271. 0x40, /* __u8 bmAttributes;
  272. Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
  273. 0x00, /* __u8 MaxPower; */
  274. /* interface */
  275. 0x09, /* __u8 if_bLength; */
  276. 0x04, /* __u8 if_bDescriptorType; Interface */
  277. 0x00, /* __u8 if_bInterfaceNumber; */
  278. 0x00, /* __u8 if_bAlternateSetting; */
  279. 0x01, /* __u8 if_bNumEndpoints; */
  280. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  281. 0x00, /* __u8 if_bInterfaceSubClass; */
  282. 0x00, /* __u8 if_bInterfaceProtocol; */
  283. 0x00, /* __u8 if_iInterface; */
  284. /* endpoint */
  285. 0x07, /* __u8 ep_bLength; */
  286. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  287. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  288. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  289. 0x00, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
  290. 0x02,
  291. 0xff /* __u8 ep_bInterval; 255 ms */
  292. };
  293. static unsigned char root_hub_str_index0[] = {
  294. 0x04, /* __u8 bLength; */
  295. 0x03, /* __u8 bDescriptorType; String-descriptor */
  296. 0x09, /* __u8 lang ID */
  297. 0x04, /* __u8 lang ID */
  298. };
  299. static unsigned char root_hub_str_index1[] = {
  300. 0x22, /* __u8 bLength; */
  301. 0x03, /* __u8 bDescriptorType; String-descriptor */
  302. 'I', /* __u8 Unicode */
  303. 0, /* __u8 Unicode */
  304. 'S', /* __u8 Unicode */
  305. 0, /* __u8 Unicode */
  306. 'P', /* __u8 Unicode */
  307. 0, /* __u8 Unicode */
  308. '1', /* __u8 Unicode */
  309. 0, /* __u8 Unicode */
  310. '1', /* __u8 Unicode */
  311. 0, /* __u8 Unicode */
  312. '6', /* __u8 Unicode */
  313. 0, /* __u8 Unicode */
  314. 'x', /* __u8 Unicode */
  315. 0, /* __u8 Unicode */
  316. ' ', /* __u8 Unicode */
  317. 0, /* __u8 Unicode */
  318. 'R', /* __u8 Unicode */
  319. 0, /* __u8 Unicode */
  320. 'o', /* __u8 Unicode */
  321. 0, /* __u8 Unicode */
  322. 'o', /* __u8 Unicode */
  323. 0, /* __u8 Unicode */
  324. 't', /* __u8 Unicode */
  325. 0, /* __u8 Unicode */
  326. ' ', /* __u8 Unicode */
  327. 0, /* __u8 Unicode */
  328. 'H', /* __u8 Unicode */
  329. 0, /* __u8 Unicode */
  330. 'u', /* __u8 Unicode */
  331. 0, /* __u8 Unicode */
  332. 'b', /* __u8 Unicode */
  333. 0, /* __u8 Unicode */
  334. };
  335. /*
  336. * Hub class-specific descriptor is constructed dynamically
  337. */
  338. /* --- Virtual root hub management functions ------------------------------- */
  339. static int rh_check_port_status(struct isp116x *isp116x)
  340. {
  341. u32 temp, ndp, i;
  342. int res;
  343. res = -1;
  344. temp = isp116x_read_reg32(isp116x, HCRHSTATUS);
  345. ndp = (temp & RH_A_NDP);
  346. for (i = 0; i < ndp; i++) {
  347. temp = isp116x_read_reg32(isp116x, HCRHPORT1 + i);
  348. /* check for a device disconnect */
  349. if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
  350. (RH_PS_PESC | RH_PS_CSC)) && ((temp & RH_PS_CCS) == 0)) {
  351. res = i;
  352. break;
  353. }
  354. }
  355. return res;
  356. }
  357. /* --- HC management functions --------------------------------------------- */
  358. /* Write len bytes to fifo, pad till 32-bit boundary
  359. */
  360. static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
  361. {
  362. u8 *dp = (u8 *) buf;
  363. u16 *dp2 = (u16 *) buf;
  364. u16 w;
  365. int quot = len % 4;
  366. if ((unsigned long)dp2 & 1) {
  367. /* not aligned */
  368. for (; len > 1; len -= 2) {
  369. w = *dp++;
  370. w |= *dp++ << 8;
  371. isp116x_raw_write_data16(isp116x, w);
  372. }
  373. if (len)
  374. isp116x_write_data16(isp116x, (u16) * dp);
  375. } else {
  376. /* aligned */
  377. for (; len > 1; len -= 2)
  378. isp116x_raw_write_data16(isp116x, *dp2++);
  379. if (len)
  380. isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2));
  381. }
  382. if (quot == 1 || quot == 2)
  383. isp116x_raw_write_data16(isp116x, 0);
  384. }
  385. /* Read len bytes from fifo and then read till 32-bit boundary
  386. */
  387. static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
  388. {
  389. u8 *dp = (u8 *) buf;
  390. u16 *dp2 = (u16 *) buf;
  391. u16 w;
  392. int quot = len % 4;
  393. if ((unsigned long)dp2 & 1) {
  394. /* not aligned */
  395. for (; len > 1; len -= 2) {
  396. w = isp116x_raw_read_data16(isp116x);
  397. *dp++ = w & 0xff;
  398. *dp++ = (w >> 8) & 0xff;
  399. }
  400. if (len)
  401. *dp = 0xff & isp116x_read_data16(isp116x);
  402. } else {
  403. /* aligned */
  404. for (; len > 1; len -= 2)
  405. *dp2++ = isp116x_raw_read_data16(isp116x);
  406. if (len)
  407. *(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
  408. }
  409. if (quot == 1 || quot == 2)
  410. isp116x_raw_read_data16(isp116x);
  411. }
  412. /* Write PTD's and data for scheduled transfers into the fifo ram.
  413. * Fifo must be empty and ready */
  414. static void pack_fifo(struct isp116x *isp116x, struct usb_device *dev,
  415. unsigned long pipe, struct ptd *ptd, int n, void *data,
  416. int len)
  417. {
  418. int buflen = n * sizeof(struct ptd) + len;
  419. int i, done;
  420. DBG("--- pack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
  421. isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
  422. isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
  423. isp116x_write_addr(isp116x, HCATLPORT | ISP116x_WRITE_OFFSET);
  424. done = 0;
  425. for (i = 0; i < n; i++) {
  426. DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
  427. dump_ptd(&ptd[i]);
  428. isp116x_write_data16(isp116x, ptd[i].count);
  429. isp116x_write_data16(isp116x, ptd[i].mps);
  430. isp116x_write_data16(isp116x, ptd[i].len);
  431. isp116x_write_data16(isp116x, ptd[i].faddr);
  432. dump_ptd_data(&ptd[i], (__u8 *) data + done, 0);
  433. write_ptddata_to_fifo(isp116x,
  434. (__u8 *) data + done,
  435. PTD_GET_LEN(&ptd[i]));
  436. done += PTD_GET_LEN(&ptd[i]);
  437. }
  438. }
  439. /* Read the processed PTD's and data from fifo ram back to URBs' buffers.
  440. * Fifo must be full and done */
  441. static int unpack_fifo(struct isp116x *isp116x, struct usb_device *dev,
  442. unsigned long pipe, struct ptd *ptd, int n, void *data,
  443. int len)
  444. {
  445. int buflen = n * sizeof(struct ptd) + len;
  446. int i, done, cc, ret;
  447. isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
  448. isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
  449. isp116x_write_addr(isp116x, HCATLPORT);
  450. ret = TD_CC_NOERROR;
  451. done = 0;
  452. for (i = 0; i < n; i++) {
  453. DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
  454. ptd[i].count = isp116x_read_data16(isp116x);
  455. ptd[i].mps = isp116x_read_data16(isp116x);
  456. ptd[i].len = isp116x_read_data16(isp116x);
  457. ptd[i].faddr = isp116x_read_data16(isp116x);
  458. dump_ptd(&ptd[i]);
  459. read_ptddata_from_fifo(isp116x,
  460. (__u8 *) data + done,
  461. PTD_GET_LEN(&ptd[i]));
  462. dump_ptd_data(&ptd[i], (__u8 *) data + done, 1);
  463. done += PTD_GET_LEN(&ptd[i]);
  464. cc = PTD_GET_CC(&ptd[i]);
  465. /* Data underrun means basically that we had more buffer space than
  466. * the function had data. It is perfectly normal but upper levels have
  467. * to know how much we actually transferred.
  468. */
  469. if (cc == TD_NOTACCESSED ||
  470. (cc != TD_CC_NOERROR && (ret == TD_CC_NOERROR || ret == TD_DATAUNDERRUN)))
  471. ret = cc;
  472. }
  473. DBG("--- unpack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
  474. return ret;
  475. }
  476. /* Interrupt handling
  477. */
  478. static int isp116x_interrupt(struct isp116x *isp116x)
  479. {
  480. u16 irqstat;
  481. u32 intstat;
  482. int ret = 0;
  483. isp116x_write_reg16(isp116x, HCuPINTENB, 0);
  484. irqstat = isp116x_read_reg16(isp116x, HCuPINT);
  485. isp116x_write_reg16(isp116x, HCuPINT, irqstat);
  486. DBG(">>>>>> irqstat %x <<<<<<", irqstat);
  487. if (irqstat & HCuPINT_ATL) {
  488. DBG(">>>>>> HCuPINT_ATL <<<<<<");
  489. udelay(500);
  490. ret = 1;
  491. }
  492. if (irqstat & HCuPINT_OPR) {
  493. intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
  494. isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
  495. DBG(">>>>>> HCuPINT_OPR %x <<<<<<", intstat);
  496. if (intstat & HCINT_UE) {
  497. ERR("unrecoverable error, controller disabled");
  498. /* FIXME: be optimistic, hope that bug won't repeat
  499. * often. Make some non-interrupt context restart the
  500. * controller. Count and limit the retries though;
  501. * either hardware or software errors can go forever...
  502. */
  503. isp116x_reset(isp116x);
  504. ret = -1;
  505. return -1;
  506. }
  507. if (intstat & HCINT_RHSC) {
  508. got_rhsc = 1;
  509. ret = 1;
  510. /* When root hub or any of its ports is going
  511. to come out of suspend, it may take more
  512. than 10ms for status bits to stabilize. */
  513. wait_ms(20);
  514. }
  515. if (intstat & HCINT_SO) {
  516. ERR("schedule overrun");
  517. ret = -1;
  518. }
  519. irqstat &= ~HCuPINT_OPR;
  520. }
  521. return ret;
  522. }
  523. /* With one PTD we can transfer almost 1K in one go;
  524. * HC does the splitting into endpoint digestible transactions
  525. */
  526. struct ptd ptd[1];
  527. static inline int max_transfer_len(struct usb_device *dev, unsigned long pipe)
  528. {
  529. unsigned mpck = usb_maxpacket(dev, pipe);
  530. /* One PTD can transfer 1023 bytes but try to always
  531. * transfer multiples of endpoint buffer size
  532. */
  533. return 1023 / mpck * mpck;
  534. }
  535. /* Do an USB transfer
  536. */
  537. static int isp116x_submit_job(struct usb_device *dev, unsigned long pipe,
  538. int dir, void *buffer, int len)
  539. {
  540. struct isp116x *isp116x = &isp116x_dev;
  541. int type = usb_pipetype(pipe);
  542. int epnum = usb_pipeendpoint(pipe);
  543. int max = usb_maxpacket(dev, pipe);
  544. int dir_out = usb_pipeout(pipe);
  545. int speed_low = usb_pipeslow(pipe);
  546. int i, done = 0, stat, timeout, cc;
  547. /* 500 frames or 0.5s timeout when function is busy and NAKs transactions for a while */
  548. int retries = 500;
  549. DBG("------------------------------------------------");
  550. dump_msg(dev, pipe, buffer, len, "SUBMIT");
  551. DBG("------------------------------------------------");
  552. if (len >= 1024) {
  553. ERR("Too big job");
  554. dev->status = USB_ST_CRC_ERR;
  555. return -1;
  556. }
  557. if (isp116x->disabled) {
  558. ERR("EPIPE");
  559. dev->status = USB_ST_CRC_ERR;
  560. return -1;
  561. }
  562. /* device pulled? Shortcut the action. */
  563. if (devgone == dev) {
  564. ERR("ENODEV");
  565. dev->status = USB_ST_CRC_ERR;
  566. return USB_ST_CRC_ERR;
  567. }
  568. if (!max) {
  569. ERR("pipesize for pipe %lx is zero", pipe);
  570. dev->status = USB_ST_CRC_ERR;
  571. return -1;
  572. }
  573. if (type == PIPE_ISOCHRONOUS) {
  574. ERR("isochronous transfers not supported");
  575. dev->status = USB_ST_CRC_ERR;
  576. return -1;
  577. }
  578. /* FIFO not empty? */
  579. if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL) {
  580. ERR("****** FIFO not empty! ******");
  581. dev->status = USB_ST_BUF_ERR;
  582. return -1;
  583. }
  584. retry:
  585. isp116x_write_reg32(isp116x, HCINTSTAT, 0xff);
  586. /* Prepare the PTD data */
  587. ptd->count = PTD_CC_MSK | PTD_ACTIVE_MSK |
  588. PTD_TOGGLE(usb_gettoggle(dev, epnum, dir_out));
  589. ptd->mps = PTD_MPS(max) | PTD_SPD(speed_low) | PTD_EP(epnum) | PTD_LAST_MSK;
  590. ptd->len = PTD_LEN(len) | PTD_DIR(dir);
  591. ptd->faddr = PTD_FA(usb_pipedevice(pipe));
  592. retry_same:
  593. /* Pack data into FIFO ram */
  594. pack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
  595. #ifdef EXTRA_DELAY
  596. wait_ms(EXTRA_DELAY);
  597. #endif
  598. /* Start the data transfer */
  599. /* Allow more time for a BULK device to react - some are slow */
  600. if (usb_pipetype(pipe) == PIPE_BULK)
  601. timeout = 5000;
  602. else
  603. timeout = 100;
  604. /* Wait for it to complete */
  605. for (;;) {
  606. /* Check whether the controller is done */
  607. stat = isp116x_interrupt(isp116x);
  608. if (stat < 0) {
  609. dev->status = USB_ST_CRC_ERR;
  610. break;
  611. }
  612. if (stat > 0)
  613. break;
  614. /* Check the timeout */
  615. if (--timeout)
  616. udelay(1);
  617. else {
  618. ERR("CTL:TIMEOUT ");
  619. stat = USB_ST_CRC_ERR;
  620. break;
  621. }
  622. }
  623. /* We got an Root Hub Status Change interrupt */
  624. if (got_rhsc) {
  625. isp116x_show_regs(isp116x);
  626. got_rhsc = 0;
  627. /* Abuse timeout */
  628. timeout = rh_check_port_status(isp116x);
  629. if (timeout >= 0) {
  630. /*
  631. * FIXME! NOTE! AAAARGH!
  632. * This is potentially dangerous because it assumes
  633. * that only one device is ever plugged in!
  634. */
  635. devgone = dev;
  636. }
  637. }
  638. /* Ok, now we can read transfer status */
  639. /* FIFO not ready? */
  640. if (!(isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_DONE)) {
  641. ERR("****** FIFO not ready! ******");
  642. dev->status = USB_ST_BUF_ERR;
  643. return -1;
  644. }
  645. /* Unpack data from FIFO ram */
  646. cc = unpack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
  647. i = PTD_GET_COUNT(ptd);
  648. done += i;
  649. buffer += i;
  650. len -= i;
  651. /* There was some kind of real problem; Prepare the PTD again
  652. * and retry from the failed transaction on
  653. */
  654. if (cc && cc != TD_NOTACCESSED && cc != TD_DATAUNDERRUN) {
  655. if (retries >= 100) {
  656. retries -= 100;
  657. /* The chip will have toggled the toggle bit for the failed
  658. * transaction too. We have to toggle it back.
  659. */
  660. usb_settoggle(dev, epnum, dir_out, !PTD_GET_TOGGLE(ptd));
  661. goto retry;
  662. }
  663. }
  664. /* "Normal" errors; TD_NOTACCESSED would mean in effect that the function have NAKed
  665. * the transactions from the first on for the whole frame. It may be busy and we retry
  666. * with the same PTD. PTD_ACTIVE (and not TD_NOTACCESSED) would mean that some of the
  667. * PTD didn't make it because the function was busy or the frame ended before the PTD
  668. * finished. We prepare the rest of the data and try again.
  669. */
  670. else if (cc == TD_NOTACCESSED || PTD_GET_ACTIVE(ptd) || (cc != TD_DATAUNDERRUN && PTD_GET_COUNT(ptd) < PTD_GET_LEN(ptd))) {
  671. if (retries) {
  672. --retries;
  673. if (cc == TD_NOTACCESSED && PTD_GET_ACTIVE(ptd) && !PTD_GET_COUNT(ptd)) goto retry_same;
  674. usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
  675. goto retry;
  676. }
  677. }
  678. if (cc != TD_CC_NOERROR && cc != TD_DATAUNDERRUN) {
  679. DBG("****** completition code error %x ******", cc);
  680. switch (cc) {
  681. case TD_CC_BITSTUFFING:
  682. dev->status = USB_ST_BIT_ERR;
  683. break;
  684. case TD_CC_STALL:
  685. dev->status = USB_ST_STALLED;
  686. break;
  687. case TD_BUFFEROVERRUN:
  688. case TD_BUFFERUNDERRUN:
  689. dev->status = USB_ST_BUF_ERR;
  690. break;
  691. default:
  692. dev->status = USB_ST_CRC_ERR;
  693. }
  694. return -cc;
  695. }
  696. else usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
  697. dump_msg(dev, pipe, buffer, len, "SUBMIT(ret)");
  698. dev->status = 0;
  699. return done;
  700. }
  701. /* Adapted from au1x00_usb_ohci.c
  702. */
  703. static int isp116x_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
  704. void *buffer, int transfer_len,
  705. struct devrequest *cmd)
  706. {
  707. struct isp116x *isp116x = &isp116x_dev;
  708. u32 tmp = 0;
  709. int leni = transfer_len;
  710. int len = 0;
  711. int stat = 0;
  712. u32 datab[4];
  713. u8 *data_buf = (u8 *) datab;
  714. u16 bmRType_bReq;
  715. u16 wValue;
  716. u16 wIndex;
  717. u16 wLength;
  718. if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
  719. INFO("Root-Hub submit IRQ: NOT implemented");
  720. return 0;
  721. }
  722. bmRType_bReq = cmd->requesttype | (cmd->request << 8);
  723. wValue = swap_16(cmd->value);
  724. wIndex = swap_16(cmd->index);
  725. wLength = swap_16(cmd->length);
  726. DBG("--- HUB ----------------------------------------");
  727. DBG("submit rh urb, req=%x val=%#x index=%#x len=%d",
  728. bmRType_bReq, wValue, wIndex, wLength);
  729. dump_msg(dev, pipe, buffer, transfer_len, "RH");
  730. DBG("------------------------------------------------");
  731. switch (bmRType_bReq) {
  732. case RH_GET_STATUS:
  733. DBG("RH_GET_STATUS");
  734. *(__u16 *) data_buf = swap_16(1);
  735. len = 2;
  736. break;
  737. case RH_GET_STATUS | RH_INTERFACE:
  738. DBG("RH_GET_STATUS | RH_INTERFACE");
  739. *(__u16 *) data_buf = swap_16(0);
  740. len = 2;
  741. break;
  742. case RH_GET_STATUS | RH_ENDPOINT:
  743. DBG("RH_GET_STATUS | RH_ENDPOINT");
  744. *(__u16 *) data_buf = swap_16(0);
  745. len = 2;
  746. break;
  747. case RH_GET_STATUS | RH_CLASS:
  748. DBG("RH_GET_STATUS | RH_CLASS");
  749. tmp = isp116x_read_reg32(isp116x, HCRHSTATUS);
  750. *(__u32 *) data_buf = swap_32(tmp & ~(RH_HS_CRWE | RH_HS_DRWE));
  751. len = 4;
  752. break;
  753. case RH_GET_STATUS | RH_OTHER | RH_CLASS:
  754. DBG("RH_GET_STATUS | RH_OTHER | RH_CLASS");
  755. tmp = isp116x_read_reg32(isp116x, HCRHPORT1 + wIndex - 1);
  756. *(__u32 *) data_buf = swap_32(tmp);
  757. isp116x_show_regs(isp116x);
  758. len = 4;
  759. break;
  760. case RH_CLEAR_FEATURE | RH_ENDPOINT:
  761. DBG("RH_CLEAR_FEATURE | RH_ENDPOINT");
  762. switch (wValue) {
  763. case RH_ENDPOINT_STALL:
  764. DBG("C_HUB_ENDPOINT_STALL");
  765. len = 0;
  766. break;
  767. }
  768. break;
  769. case RH_CLEAR_FEATURE | RH_CLASS:
  770. DBG("RH_CLEAR_FEATURE | RH_CLASS");
  771. switch (wValue) {
  772. case RH_C_HUB_LOCAL_POWER:
  773. DBG("C_HUB_LOCAL_POWER");
  774. len = 0;
  775. break;
  776. case RH_C_HUB_OVER_CURRENT:
  777. DBG("C_HUB_OVER_CURRENT");
  778. isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
  779. len = 0;
  780. break;
  781. }
  782. break;
  783. case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
  784. DBG("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS");
  785. switch (wValue) {
  786. case RH_PORT_ENABLE:
  787. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  788. RH_PS_CCS);
  789. len = 0;
  790. break;
  791. case RH_PORT_SUSPEND:
  792. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  793. RH_PS_POCI);
  794. len = 0;
  795. break;
  796. case RH_PORT_POWER:
  797. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  798. RH_PS_LSDA);
  799. len = 0;
  800. break;
  801. case RH_C_PORT_CONNECTION:
  802. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  803. RH_PS_CSC);
  804. len = 0;
  805. break;
  806. case RH_C_PORT_ENABLE:
  807. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  808. RH_PS_PESC);
  809. len = 0;
  810. break;
  811. case RH_C_PORT_SUSPEND:
  812. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  813. RH_PS_PSSC);
  814. len = 0;
  815. break;
  816. case RH_C_PORT_OVER_CURRENT:
  817. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  818. RH_PS_POCI);
  819. len = 0;
  820. break;
  821. case RH_C_PORT_RESET:
  822. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  823. RH_PS_PRSC);
  824. len = 0;
  825. break;
  826. default:
  827. ERR("invalid wValue");
  828. stat = USB_ST_STALLED;
  829. }
  830. isp116x_show_regs(isp116x);
  831. break;
  832. case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
  833. DBG("RH_SET_FEATURE | RH_OTHER | RH_CLASS");
  834. switch (wValue) {
  835. case RH_PORT_SUSPEND:
  836. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  837. RH_PS_PSS);
  838. len = 0;
  839. break;
  840. case RH_PORT_RESET:
  841. /* Spin until any current reset finishes */
  842. while (1) {
  843. tmp =
  844. isp116x_read_reg32(isp116x,
  845. HCRHPORT1 + wIndex - 1);
  846. if (!(tmp & RH_PS_PRS))
  847. break;
  848. wait_ms(1);
  849. }
  850. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  851. RH_PS_PRS);
  852. wait_ms(10);
  853. len = 0;
  854. break;
  855. case RH_PORT_POWER:
  856. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  857. RH_PS_PPS);
  858. len = 0;
  859. break;
  860. case RH_PORT_ENABLE:
  861. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  862. RH_PS_PES);
  863. len = 0;
  864. break;
  865. default:
  866. ERR("invalid wValue");
  867. stat = USB_ST_STALLED;
  868. }
  869. isp116x_show_regs(isp116x);
  870. break;
  871. case RH_SET_ADDRESS:
  872. DBG("RH_SET_ADDRESS");
  873. rh_devnum = wValue;
  874. len = 0;
  875. break;
  876. case RH_GET_DESCRIPTOR:
  877. DBG("RH_GET_DESCRIPTOR: %x, %d", wValue, wLength);
  878. switch (wValue) {
  879. case (USB_DT_DEVICE << 8): /* device descriptor */
  880. len = min_t(unsigned int,
  881. leni, min_t(unsigned int,
  882. sizeof(root_hub_dev_des),
  883. wLength));
  884. data_buf = root_hub_dev_des;
  885. break;
  886. case (USB_DT_CONFIG << 8): /* configuration descriptor */
  887. len = min_t(unsigned int,
  888. leni, min_t(unsigned int,
  889. sizeof(root_hub_config_des),
  890. wLength));
  891. data_buf = root_hub_config_des;
  892. break;
  893. case ((USB_DT_STRING << 8) | 0x00): /* string 0 descriptors */
  894. len = min_t(unsigned int,
  895. leni, min_t(unsigned int,
  896. sizeof(root_hub_str_index0),
  897. wLength));
  898. data_buf = root_hub_str_index0;
  899. break;
  900. case ((USB_DT_STRING << 8) | 0x01): /* string 1 descriptors */
  901. len = min_t(unsigned int,
  902. leni, min_t(unsigned int,
  903. sizeof(root_hub_str_index1),
  904. wLength));
  905. data_buf = root_hub_str_index1;
  906. break;
  907. default:
  908. ERR("invalid wValue");
  909. stat = USB_ST_STALLED;
  910. }
  911. break;
  912. case RH_GET_DESCRIPTOR | RH_CLASS:
  913. DBG("RH_GET_DESCRIPTOR | RH_CLASS");
  914. tmp = isp116x_read_reg32(isp116x, HCRHDESCA);
  915. data_buf[0] = 0x09; /* min length; */
  916. data_buf[1] = 0x29;
  917. data_buf[2] = tmp & RH_A_NDP;
  918. data_buf[3] = 0;
  919. if (tmp & RH_A_PSM) /* per-port power switching? */
  920. data_buf[3] |= 0x01;
  921. if (tmp & RH_A_NOCP) /* no overcurrent reporting? */
  922. data_buf[3] |= 0x10;
  923. else if (tmp & RH_A_OCPM) /* per-port overcurrent rep? */
  924. data_buf[3] |= 0x08;
  925. /* Corresponds to data_buf[4-7] */
  926. datab[1] = 0;
  927. data_buf[5] = (tmp & RH_A_POTPGT) >> 24;
  928. tmp = isp116x_read_reg32(isp116x, HCRHDESCB);
  929. data_buf[7] = tmp & RH_B_DR;
  930. if (data_buf[2] < 7)
  931. data_buf[8] = 0xff;
  932. else {
  933. data_buf[0] += 2;
  934. data_buf[8] = (tmp & RH_B_DR) >> 8;
  935. data_buf[10] = data_buf[9] = 0xff;
  936. }
  937. len = min_t(unsigned int, leni,
  938. min_t(unsigned int, data_buf[0], wLength));
  939. break;
  940. case RH_GET_CONFIGURATION:
  941. DBG("RH_GET_CONFIGURATION");
  942. *(__u8 *) data_buf = 0x01;
  943. len = 1;
  944. break;
  945. case RH_SET_CONFIGURATION:
  946. DBG("RH_SET_CONFIGURATION");
  947. isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPSC);
  948. len = 0;
  949. break;
  950. default:
  951. ERR("*** *** *** unsupported root hub command *** *** ***");
  952. stat = USB_ST_STALLED;
  953. }
  954. len = min_t(int, len, leni);
  955. if (buffer != data_buf)
  956. memcpy(buffer, data_buf, len);
  957. dev->act_len = len;
  958. dev->status = stat;
  959. DBG("dev act_len %d, status %d", dev->act_len, dev->status);
  960. dump_msg(dev, pipe, buffer, transfer_len, "RH(ret)");
  961. return stat;
  962. }
  963. /* --- Transfer functions -------------------------------------------------- */
  964. int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  965. int len, int interval)
  966. {
  967. DBG("dev=%p pipe=%#lx buf=%p size=%d int=%d",
  968. dev, pipe, buffer, len, interval);
  969. return -1;
  970. }
  971. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  972. int len, struct devrequest *setup)
  973. {
  974. int devnum = usb_pipedevice(pipe);
  975. int epnum = usb_pipeendpoint(pipe);
  976. int max = max_transfer_len(dev, pipe);
  977. int dir_in = usb_pipein(pipe);
  978. int done, ret;
  979. /* Control message is for the HUB? */
  980. if (devnum == rh_devnum)
  981. return isp116x_submit_rh_msg(dev, pipe, buffer, len, setup);
  982. /* Ok, no HUB message so send the message to the device */
  983. /* Setup phase */
  984. DBG("--- SETUP PHASE --------------------------------");
  985. usb_settoggle(dev, epnum, 1, 0);
  986. ret = isp116x_submit_job(dev, pipe,
  987. PTD_DIR_SETUP,
  988. setup, sizeof(struct devrequest));
  989. if (ret < 0) {
  990. DBG("control setup phase error (ret = %d", ret);
  991. return -1;
  992. }
  993. /* Data phase */
  994. DBG("--- DATA PHASE ---------------------------------");
  995. done = 0;
  996. usb_settoggle(dev, epnum, !dir_in, 1);
  997. while (done < len) {
  998. ret = isp116x_submit_job(dev, pipe,
  999. dir_in ? PTD_DIR_IN : PTD_DIR_OUT,
  1000. (__u8 *) buffer + done,
  1001. max > len - done ? len - done : max);
  1002. if (ret < 0) {
  1003. DBG("control data phase error (ret = %d)", ret);
  1004. return -1;
  1005. }
  1006. done += ret;
  1007. if (dir_in && ret < max) /* short packet */
  1008. break;
  1009. }
  1010. /* Status phase */
  1011. DBG("--- STATUS PHASE -------------------------------");
  1012. usb_settoggle(dev, epnum, !dir_in, 1);
  1013. ret = isp116x_submit_job(dev, pipe,
  1014. !dir_in ? PTD_DIR_IN : PTD_DIR_OUT, NULL, 0);
  1015. if (ret < 0) {
  1016. DBG("control status phase error (ret = %d", ret);
  1017. return -1;
  1018. }
  1019. dev->act_len = done;
  1020. dump_msg(dev, pipe, buffer, len, "DEV(ret)");
  1021. return done;
  1022. }
  1023. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  1024. int len)
  1025. {
  1026. int dir_out = usb_pipeout(pipe);
  1027. int max = max_transfer_len(dev, pipe);
  1028. int done, ret;
  1029. DBG("--- BULK ---------------------------------------");
  1030. DBG("dev=%ld pipe=%ld buf=%p size=%d dir_out=%d",
  1031. usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);
  1032. done = 0;
  1033. while (done < len) {
  1034. ret = isp116x_submit_job(dev, pipe,
  1035. !dir_out ? PTD_DIR_IN : PTD_DIR_OUT,
  1036. (__u8 *) buffer + done,
  1037. max > len - done ? len - done : max);
  1038. if (ret < 0) {
  1039. DBG("error on bulk message (ret = %d)", ret);
  1040. return -1;
  1041. }
  1042. done += ret;
  1043. if (!dir_out && ret < max) /* short packet */
  1044. break;
  1045. }
  1046. dev->act_len = done;
  1047. return 0;
  1048. }
  1049. /* --- Basic functions ----------------------------------------------------- */
  1050. static int isp116x_sw_reset(struct isp116x *isp116x)
  1051. {
  1052. int retries = 15;
  1053. int ret = 0;
  1054. DBG("");
  1055. isp116x->disabled = 1;
  1056. isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
  1057. isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
  1058. while (--retries) {
  1059. /* It usually resets within 1 ms */
  1060. wait_ms(1);
  1061. if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
  1062. break;
  1063. }
  1064. if (!retries) {
  1065. ERR("software reset timeout");
  1066. ret = -1;
  1067. }
  1068. return ret;
  1069. }
  1070. static int isp116x_reset(struct isp116x *isp116x)
  1071. {
  1072. unsigned long t;
  1073. u16 clkrdy = 0;
  1074. int ret, timeout = 15 /* ms */ ;
  1075. DBG("");
  1076. ret = isp116x_sw_reset(isp116x);
  1077. if (ret)
  1078. return ret;
  1079. for (t = 0; t < timeout; t++) {
  1080. clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
  1081. if (clkrdy)
  1082. break;
  1083. wait_ms(1);
  1084. }
  1085. if (!clkrdy) {
  1086. ERR("clock not ready after %dms", timeout);
  1087. /* After sw_reset the clock won't report to be ready, if
  1088. H_WAKEUP pin is high. */
  1089. ERR("please make sure that the H_WAKEUP pin is pulled low!");
  1090. ret = -1;
  1091. }
  1092. return ret;
  1093. }
  1094. static void isp116x_stop(struct isp116x *isp116x)
  1095. {
  1096. u32 val;
  1097. DBG("");
  1098. isp116x_write_reg16(isp116x, HCuPINTENB, 0);
  1099. /* Switch off ports' power, some devices don't come up
  1100. after next 'start' without this */
  1101. val = isp116x_read_reg32(isp116x, HCRHDESCA);
  1102. val &= ~(RH_A_NPS | RH_A_PSM);
  1103. isp116x_write_reg32(isp116x, HCRHDESCA, val);
  1104. isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);
  1105. isp116x_sw_reset(isp116x);
  1106. }
  1107. /*
  1108. * Configure the chip. The chip must be successfully reset by now.
  1109. */
  1110. static int isp116x_start(struct isp116x *isp116x)
  1111. {
  1112. struct isp116x_platform_data *board = isp116x->board;
  1113. u32 val;
  1114. DBG("");
  1115. /* Clear interrupt status and disable all interrupt sources */
  1116. isp116x_write_reg16(isp116x, HCuPINT, 0xff);
  1117. isp116x_write_reg16(isp116x, HCuPINTENB, 0);
  1118. isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
  1119. isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);
  1120. /* Hardware configuration */
  1121. val = HCHWCFG_DBWIDTH(1);
  1122. if (board->sel15Kres)
  1123. val |= HCHWCFG_15KRSEL;
  1124. /* Remote wakeup won't work without working clock */
  1125. if (board->remote_wakeup_enable)
  1126. val |= HCHWCFG_CLKNOTSTOP;
  1127. if (board->oc_enable)
  1128. val |= HCHWCFG_ANALOG_OC;
  1129. isp116x_write_reg16(isp116x, HCHWCFG, val);
  1130. /* --- Root hub configuration */
  1131. val = (25 << 24) & RH_A_POTPGT;
  1132. /* AN10003_1.pdf recommends RH_A_NPS (no power switching) to
  1133. be always set. Yet, instead, we request individual port
  1134. power switching. */
  1135. val |= RH_A_PSM;
  1136. /* Report overcurrent per port */
  1137. val |= RH_A_OCPM;
  1138. isp116x_write_reg32(isp116x, HCRHDESCA, val);
  1139. isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
  1140. val = RH_B_PPCM;
  1141. isp116x_write_reg32(isp116x, HCRHDESCB, val);
  1142. isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
  1143. val = 0;
  1144. if (board->remote_wakeup_enable)
  1145. val |= RH_HS_DRWE;
  1146. isp116x_write_reg32(isp116x, HCRHSTATUS, val);
  1147. isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
  1148. isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);
  1149. /* Go operational */
  1150. val = HCCONTROL_USB_OPER;
  1151. if (board->remote_wakeup_enable)
  1152. val |= HCCONTROL_RWE;
  1153. isp116x_write_reg32(isp116x, HCCONTROL, val);
  1154. /* Disable ports to avoid race in device enumeration */
  1155. isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
  1156. isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);
  1157. isp116x_show_regs(isp116x);
  1158. isp116x->disabled = 0;
  1159. return 0;
  1160. }
  1161. /* --- Init functions ------------------------------------------------------ */
  1162. int isp116x_check_id(struct isp116x *isp116x)
  1163. {
  1164. int val;
  1165. val = isp116x_read_reg16(isp116x, HCCHIPID);
  1166. if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
  1167. ERR("invalid chip ID %04x", val);
  1168. return -1;
  1169. }
  1170. return 0;
  1171. }
  1172. int usb_lowlevel_init(void)
  1173. {
  1174. struct isp116x *isp116x = &isp116x_dev;
  1175. DBG("");
  1176. got_rhsc = rh_devnum = 0;
  1177. /* Init device registers addr */
  1178. isp116x->addr_reg = (u16 *) ISP116X_HCD_ADDR;
  1179. isp116x->data_reg = (u16 *) ISP116X_HCD_DATA;
  1180. /* Setup specific board settings */
  1181. #ifdef ISP116X_HCD_SEL15kRES
  1182. isp116x_board.sel15Kres = 1;
  1183. #endif
  1184. #ifdef ISP116X_HCD_OC_ENABLE
  1185. isp116x_board.oc_enable = 1;
  1186. #endif
  1187. #ifdef ISP116X_HCD_REMOTE_WAKEUP_ENABLE
  1188. isp116x_board.remote_wakeup_enable = 1;
  1189. #endif
  1190. isp116x->board = &isp116x_board;
  1191. /* Try to get ISP116x silicon chip ID */
  1192. if (isp116x_check_id(isp116x) < 0)
  1193. return -1;
  1194. isp116x->disabled = 1;
  1195. isp116x->sleeping = 0;
  1196. isp116x_reset(isp116x);
  1197. isp116x_start(isp116x);
  1198. return 0;
  1199. }
  1200. int usb_lowlevel_stop(void)
  1201. {
  1202. struct isp116x *isp116x = &isp116x_dev;
  1203. DBG("");
  1204. if (!isp116x->disabled)
  1205. isp116x_stop(isp116x);
  1206. return 0;
  1207. }
  1208. #endif /* CONFIG_USB_ISP116X_HCD */