isp116x-hcd.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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/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. int got_rhsc = 0; /* root hub status change */
  109. struct usb_device *devgone; /* device which was disconnected */
  110. int rh_devnum = 0; /* 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. if (cc == TD_DATAUNDERRUN) { /* underrun is no error... */
  466. DBG("allowed data underrun");
  467. cc = TD_CC_NOERROR;
  468. }
  469. if (cc != TD_CC_NOERROR && ret == TD_CC_NOERROR)
  470. ret = cc;
  471. }
  472. DBG("--- unpack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
  473. return ret;
  474. }
  475. /* Interrupt handling
  476. */
  477. static int isp116x_interrupt(struct isp116x *isp116x)
  478. {
  479. u16 irqstat;
  480. u32 intstat;
  481. int ret = 0;
  482. isp116x_write_reg16(isp116x, HCuPINTENB, 0);
  483. irqstat = isp116x_read_reg16(isp116x, HCuPINT);
  484. isp116x_write_reg16(isp116x, HCuPINT, irqstat);
  485. DBG(">>>>>> irqstat %x <<<<<<", irqstat);
  486. if (irqstat & HCuPINT_ATL) {
  487. DBG(">>>>>> HCuPINT_ATL <<<<<<");
  488. udelay(500);
  489. ret = 1;
  490. }
  491. if (irqstat & HCuPINT_OPR) {
  492. intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
  493. isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
  494. DBG(">>>>>> HCuPINT_OPR %x <<<<<<", intstat);
  495. if (intstat & HCINT_UE) {
  496. ERR("unrecoverable error, controller disabled");
  497. /* FIXME: be optimistic, hope that bug won't repeat
  498. * often. Make some non-interrupt context restart the
  499. * controller. Count and limit the retries though;
  500. * either hardware or software errors can go forever...
  501. */
  502. isp116x_reset(isp116x);
  503. ret = -1;
  504. return -1;
  505. }
  506. if (intstat & HCINT_RHSC) {
  507. got_rhsc = 1;
  508. ret = 1;
  509. /* When root hub or any of its ports is going
  510. to come out of suspend, it may take more
  511. than 10ms for status bits to stabilize. */
  512. wait_ms(20);
  513. }
  514. if (intstat & HCINT_SO) {
  515. ERR("schedule overrun");
  516. ret = -1;
  517. }
  518. irqstat &= ~HCuPINT_OPR;
  519. }
  520. return ret;
  521. }
  522. #define PTD_NUM 64 /* it should be enougth... */
  523. struct ptd ptd[PTD_NUM];
  524. static inline int max_transfer_len(struct usb_device *dev, unsigned long pipe)
  525. {
  526. return min(PTD_NUM * usb_maxpacket(dev, pipe), PTD_NUM * 16);
  527. }
  528. /* Do an USB transfer
  529. */
  530. static int isp116x_submit_job(struct usb_device *dev, unsigned long pipe,
  531. int dir, void *buffer, int len)
  532. {
  533. struct isp116x *isp116x = &isp116x_dev;
  534. int type = usb_pipetype(pipe);
  535. int epnum = usb_pipeendpoint(pipe);
  536. int max = usb_maxpacket(dev, pipe);
  537. int dir_out = usb_pipeout(pipe);
  538. int speed_low = usb_pipeslow(pipe);
  539. int i, done, stat, timeout, cc;
  540. int retries = 10;
  541. DBG("------------------------------------------------");
  542. dump_msg(dev, pipe, buffer, len, "SUBMIT");
  543. DBG("------------------------------------------------");
  544. if (isp116x->disabled) {
  545. ERR("EPIPE");
  546. dev->status = USB_ST_CRC_ERR;
  547. return -1;
  548. }
  549. /* device pulled? Shortcut the action. */
  550. if (devgone == dev) {
  551. ERR("ENODEV");
  552. dev->status = USB_ST_CRC_ERR;
  553. return USB_ST_CRC_ERR;
  554. }
  555. if (!max) {
  556. ERR("pipesize for pipe %lx is zero", pipe);
  557. dev->status = USB_ST_CRC_ERR;
  558. return -1;
  559. }
  560. if (type == PIPE_ISOCHRONOUS) {
  561. ERR("isochronous transfers not supported");
  562. dev->status = USB_ST_CRC_ERR;
  563. return -1;
  564. }
  565. /* FIFO not empty? */
  566. if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL) {
  567. ERR("****** FIFO not empty! ******");
  568. dev->status = USB_ST_BUF_ERR;
  569. return -1;
  570. }
  571. retry:
  572. isp116x_write_reg32(isp116x, HCINTSTAT, 0xff);
  573. /* Prepare the PTD data */
  574. done = 0;
  575. i = 0;
  576. do {
  577. ptd[i].count = PTD_CC_MSK | PTD_ACTIVE_MSK |
  578. PTD_TOGGLE(usb_gettoggle(dev, epnum, dir_out));
  579. ptd[i].mps = PTD_MPS(max) | PTD_SPD(speed_low) | PTD_EP(epnum);
  580. ptd[i].len = PTD_LEN(max > len - done ? len - done : max) |
  581. PTD_DIR(dir);
  582. ptd[i].faddr = PTD_FA(usb_pipedevice(pipe));
  583. usb_dotoggle(dev, epnum, dir_out);
  584. done += PTD_GET_LEN(&ptd[i]);
  585. i++;
  586. if (i >= PTD_NUM) {
  587. ERR("****** Cannot pack buffer! ******");
  588. dev->status = USB_ST_BUF_ERR;
  589. return -1;
  590. }
  591. } while (done < len);
  592. ptd[i - 1].mps |= PTD_LAST_MSK;
  593. /* Pack data into FIFO ram */
  594. pack_fifo(isp116x, dev, pipe, ptd, i, 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, i, buffer, len);
  647. /* Mmm... sometime we get 0x0f as cc which is a non sense!
  648. * Just retry the transfer...
  649. */
  650. if (cc == 0x0f && retries-- > 0) {
  651. usb_dotoggle(dev, epnum, dir_out);
  652. goto retry;
  653. }
  654. if (cc != TD_CC_NOERROR) {
  655. DBG("****** completition code error %x ******", cc);
  656. switch (cc) {
  657. case TD_CC_BITSTUFFING:
  658. dev->status = USB_ST_BIT_ERR;
  659. break;
  660. case TD_CC_STALL:
  661. dev->status = USB_ST_STALLED;
  662. break;
  663. case TD_BUFFEROVERRUN:
  664. case TD_BUFFERUNDERRUN:
  665. dev->status = USB_ST_BUF_ERR;
  666. break;
  667. default:
  668. dev->status = USB_ST_CRC_ERR;
  669. }
  670. return -cc;
  671. }
  672. dump_msg(dev, pipe, buffer, len, "SUBMIT(ret)");
  673. dev->status = 0;
  674. return done;
  675. }
  676. /* Adapted from au1x00_usb_ohci.c
  677. */
  678. static int isp116x_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
  679. void *buffer, int transfer_len,
  680. struct devrequest *cmd)
  681. {
  682. struct isp116x *isp116x = &isp116x_dev;
  683. u32 tmp = 0;
  684. int leni = transfer_len;
  685. int len = 0;
  686. int stat = 0;
  687. u32 datab[4];
  688. u8 *data_buf = (u8 *) datab;
  689. u16 bmRType_bReq;
  690. u16 wValue;
  691. u16 wIndex;
  692. u16 wLength;
  693. if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
  694. INFO("Root-Hub submit IRQ: NOT implemented");
  695. return 0;
  696. }
  697. bmRType_bReq = cmd->requesttype | (cmd->request << 8);
  698. wValue = swap_16(cmd->value);
  699. wIndex = swap_16(cmd->index);
  700. wLength = swap_16(cmd->length);
  701. DBG("--- HUB ----------------------------------------");
  702. DBG("submit rh urb, req=%x val=%#x index=%#x len=%d",
  703. bmRType_bReq, wValue, wIndex, wLength);
  704. dump_msg(dev, pipe, buffer, transfer_len, "RH");
  705. DBG("------------------------------------------------");
  706. switch (bmRType_bReq) {
  707. case RH_GET_STATUS:
  708. DBG("RH_GET_STATUS");
  709. *(__u16 *) data_buf = swap_16(1);
  710. len = 2;
  711. break;
  712. case RH_GET_STATUS | RH_INTERFACE:
  713. DBG("RH_GET_STATUS | RH_INTERFACE");
  714. *(__u16 *) data_buf = swap_16(0);
  715. len = 2;
  716. break;
  717. case RH_GET_STATUS | RH_ENDPOINT:
  718. DBG("RH_GET_STATUS | RH_ENDPOINT");
  719. *(__u16 *) data_buf = swap_16(0);
  720. len = 2;
  721. break;
  722. case RH_GET_STATUS | RH_CLASS:
  723. DBG("RH_GET_STATUS | RH_CLASS");
  724. tmp = isp116x_read_reg32(isp116x, HCRHSTATUS);
  725. *(__u32 *) data_buf = swap_32(tmp & ~(RH_HS_CRWE | RH_HS_DRWE));
  726. len = 4;
  727. break;
  728. case RH_GET_STATUS | RH_OTHER | RH_CLASS:
  729. DBG("RH_GET_STATUS | RH_OTHER | RH_CLASS");
  730. tmp = isp116x_read_reg32(isp116x, HCRHPORT1 + wIndex - 1);
  731. *(__u32 *) data_buf = swap_32(tmp);
  732. isp116x_show_regs(isp116x);
  733. len = 4;
  734. break;
  735. case RH_CLEAR_FEATURE | RH_ENDPOINT:
  736. DBG("RH_CLEAR_FEATURE | RH_ENDPOINT");
  737. switch (wValue) {
  738. case RH_ENDPOINT_STALL:
  739. DBG("C_HUB_ENDPOINT_STALL");
  740. len = 0;
  741. break;
  742. }
  743. break;
  744. case RH_CLEAR_FEATURE | RH_CLASS:
  745. DBG("RH_CLEAR_FEATURE | RH_CLASS");
  746. switch (wValue) {
  747. case RH_C_HUB_LOCAL_POWER:
  748. DBG("C_HUB_LOCAL_POWER");
  749. len = 0;
  750. break;
  751. case RH_C_HUB_OVER_CURRENT:
  752. DBG("C_HUB_OVER_CURRENT");
  753. isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
  754. len = 0;
  755. break;
  756. }
  757. break;
  758. case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
  759. DBG("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS");
  760. switch (wValue) {
  761. case RH_PORT_ENABLE:
  762. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  763. RH_PS_CCS);
  764. len = 0;
  765. break;
  766. case RH_PORT_SUSPEND:
  767. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  768. RH_PS_POCI);
  769. len = 0;
  770. break;
  771. case RH_PORT_POWER:
  772. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  773. RH_PS_LSDA);
  774. len = 0;
  775. break;
  776. case RH_C_PORT_CONNECTION:
  777. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  778. RH_PS_CSC);
  779. len = 0;
  780. break;
  781. case RH_C_PORT_ENABLE:
  782. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  783. RH_PS_PESC);
  784. len = 0;
  785. break;
  786. case RH_C_PORT_SUSPEND:
  787. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  788. RH_PS_PSSC);
  789. len = 0;
  790. break;
  791. case RH_C_PORT_OVER_CURRENT:
  792. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  793. RH_PS_POCI);
  794. len = 0;
  795. break;
  796. case RH_C_PORT_RESET:
  797. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  798. RH_PS_PRSC);
  799. len = 0;
  800. break;
  801. default:
  802. ERR("invalid wValue");
  803. stat = USB_ST_STALLED;
  804. }
  805. isp116x_show_regs(isp116x);
  806. break;
  807. case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
  808. DBG("RH_SET_FEATURE | RH_OTHER | RH_CLASS");
  809. switch (wValue) {
  810. case RH_PORT_SUSPEND:
  811. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  812. RH_PS_PSS);
  813. len = 0;
  814. break;
  815. case RH_PORT_RESET:
  816. /* Spin until any current reset finishes */
  817. while (1) {
  818. tmp =
  819. isp116x_read_reg32(isp116x,
  820. HCRHPORT1 + wIndex - 1);
  821. if (!(tmp & RH_PS_PRS))
  822. break;
  823. wait_ms(1);
  824. }
  825. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  826. RH_PS_PRS);
  827. wait_ms(10);
  828. len = 0;
  829. break;
  830. case RH_PORT_POWER:
  831. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  832. RH_PS_PPS);
  833. len = 0;
  834. break;
  835. case RH_PORT_ENABLE:
  836. isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
  837. RH_PS_PES);
  838. len = 0;
  839. break;
  840. default:
  841. ERR("invalid wValue");
  842. stat = USB_ST_STALLED;
  843. }
  844. isp116x_show_regs(isp116x);
  845. break;
  846. case RH_SET_ADDRESS:
  847. DBG("RH_SET_ADDRESS");
  848. rh_devnum = wValue;
  849. len = 0;
  850. break;
  851. case RH_GET_DESCRIPTOR:
  852. DBG("RH_GET_DESCRIPTOR: %x, %d", wValue, wLength);
  853. switch (wValue) {
  854. case (USB_DT_DEVICE << 8): /* device descriptor */
  855. len = min_t(unsigned int,
  856. leni, min_t(unsigned int,
  857. sizeof(root_hub_dev_des),
  858. wLength));
  859. data_buf = root_hub_dev_des;
  860. break;
  861. case (USB_DT_CONFIG << 8): /* configuration descriptor */
  862. len = min_t(unsigned int,
  863. leni, min_t(unsigned int,
  864. sizeof(root_hub_config_des),
  865. wLength));
  866. data_buf = root_hub_config_des;
  867. break;
  868. case ((USB_DT_STRING << 8) | 0x00): /* string 0 descriptors */
  869. len = min_t(unsigned int,
  870. leni, min_t(unsigned int,
  871. sizeof(root_hub_str_index0),
  872. wLength));
  873. data_buf = root_hub_str_index0;
  874. break;
  875. case ((USB_DT_STRING << 8) | 0x01): /* string 1 descriptors */
  876. len = min_t(unsigned int,
  877. leni, min_t(unsigned int,
  878. sizeof(root_hub_str_index1),
  879. wLength));
  880. data_buf = root_hub_str_index1;
  881. break;
  882. default:
  883. ERR("invalid wValue");
  884. stat = USB_ST_STALLED;
  885. }
  886. break;
  887. case RH_GET_DESCRIPTOR | RH_CLASS:
  888. DBG("RH_GET_DESCRIPTOR | RH_CLASS");
  889. tmp = isp116x_read_reg32(isp116x, HCRHDESCA);
  890. data_buf[0] = 0x09; /* min length; */
  891. data_buf[1] = 0x29;
  892. data_buf[2] = tmp & RH_A_NDP;
  893. data_buf[3] = 0;
  894. if (tmp & RH_A_PSM) /* per-port power switching? */
  895. data_buf[3] |= 0x01;
  896. if (tmp & RH_A_NOCP) /* no overcurrent reporting? */
  897. data_buf[3] |= 0x10;
  898. else if (tmp & RH_A_OCPM) /* per-port overcurrent rep? */
  899. data_buf[3] |= 0x08;
  900. /* Corresponds to data_buf[4-7] */
  901. datab[1] = 0;
  902. data_buf[5] = (tmp & RH_A_POTPGT) >> 24;
  903. tmp = isp116x_read_reg32(isp116x, HCRHDESCB);
  904. data_buf[7] = tmp & RH_B_DR;
  905. if (data_buf[2] < 7)
  906. data_buf[8] = 0xff;
  907. else {
  908. data_buf[0] += 2;
  909. data_buf[8] = (tmp & RH_B_DR) >> 8;
  910. data_buf[10] = data_buf[9] = 0xff;
  911. }
  912. len = min_t(unsigned int, leni,
  913. min_t(unsigned int, data_buf[0], wLength));
  914. break;
  915. case RH_GET_CONFIGURATION:
  916. DBG("RH_GET_CONFIGURATION");
  917. *(__u8 *) data_buf = 0x01;
  918. len = 1;
  919. break;
  920. case RH_SET_CONFIGURATION:
  921. DBG("RH_SET_CONFIGURATION");
  922. isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPSC);
  923. len = 0;
  924. break;
  925. default:
  926. ERR("*** *** *** unsupported root hub command *** *** ***");
  927. stat = USB_ST_STALLED;
  928. }
  929. len = min_t(int, len, leni);
  930. if (buffer != data_buf)
  931. memcpy(buffer, data_buf, len);
  932. dev->act_len = len;
  933. dev->status = stat;
  934. DBG("dev act_len %d, status %d", dev->act_len, dev->status);
  935. dump_msg(dev, pipe, buffer, transfer_len, "RH(ret)");
  936. return stat;
  937. }
  938. /* --- Transfer functions -------------------------------------------------- */
  939. int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  940. int len, int interval)
  941. {
  942. DBG("dev=%p pipe=%#lx buf=%p size=%d int=%d",
  943. dev, pipe, buffer, len, interval);
  944. return -1;
  945. }
  946. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  947. int len, struct devrequest *setup)
  948. {
  949. int devnum = usb_pipedevice(pipe);
  950. int epnum = usb_pipeendpoint(pipe);
  951. int max = max_transfer_len(dev, pipe);
  952. int dir_in = usb_pipein(pipe);
  953. int done, ret;
  954. /* Control message is for the HUB? */
  955. if (devnum == rh_devnum)
  956. return isp116x_submit_rh_msg(dev, pipe, buffer, len, setup);
  957. /* Ok, no HUB message so send the message to the device */
  958. /* Setup phase */
  959. DBG("--- SETUP PHASE --------------------------------");
  960. usb_settoggle(dev, epnum, 1, 0);
  961. ret = isp116x_submit_job(dev, pipe,
  962. PTD_DIR_SETUP,
  963. setup, sizeof(struct devrequest));
  964. if (ret < 0) {
  965. DBG("control setup phase error (ret = %d", ret);
  966. return -1;
  967. }
  968. /* Data phase */
  969. DBG("--- DATA PHASE ---------------------------------");
  970. done = 0;
  971. usb_settoggle(dev, epnum, !dir_in, 1);
  972. while (done < len) {
  973. ret = isp116x_submit_job(dev, pipe,
  974. dir_in ? PTD_DIR_IN : PTD_DIR_OUT,
  975. (__u8 *) buffer + done,
  976. max > len - done ? len - done : max);
  977. if (ret < 0) {
  978. DBG("control data phase error (ret = %d)", ret);
  979. return -1;
  980. }
  981. done += ret;
  982. if (dir_in && ret < max) /* short packet */
  983. break;
  984. }
  985. /* Status phase */
  986. DBG("--- STATUS PHASE -------------------------------");
  987. usb_settoggle(dev, epnum, !dir_in, 1);
  988. ret = isp116x_submit_job(dev, pipe,
  989. !dir_in ? PTD_DIR_IN : PTD_DIR_OUT, NULL, 0);
  990. if (ret < 0) {
  991. DBG("control status phase error (ret = %d", ret);
  992. return -1;
  993. }
  994. dev->act_len = done;
  995. dump_msg(dev, pipe, buffer, len, "DEV(ret)");
  996. return done;
  997. }
  998. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  999. int len)
  1000. {
  1001. int dir_out = usb_pipeout(pipe);
  1002. int max = max_transfer_len(dev, pipe);
  1003. int done, ret;
  1004. DBG("--- BULK ---------------------------------------");
  1005. DBG("dev=%ld pipe=%ld buf=%p size=%d dir_out=%d",
  1006. usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);
  1007. done = 0;
  1008. while (done < len) {
  1009. ret = isp116x_submit_job(dev, pipe,
  1010. !dir_out ? PTD_DIR_IN : PTD_DIR_OUT,
  1011. (__u8 *) buffer + done,
  1012. max > len - done ? len - done : max);
  1013. if (ret < 0) {
  1014. DBG("error on bulk message (ret = %d)", ret);
  1015. return -1;
  1016. }
  1017. done += ret;
  1018. if (!dir_out && ret < max) /* short packet */
  1019. break;
  1020. }
  1021. dev->act_len = done;
  1022. return 0;
  1023. }
  1024. /* --- Basic functions ----------------------------------------------------- */
  1025. static int isp116x_sw_reset(struct isp116x *isp116x)
  1026. {
  1027. int retries = 15;
  1028. int ret = 0;
  1029. DBG("");
  1030. isp116x->disabled = 1;
  1031. isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
  1032. isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
  1033. while (--retries) {
  1034. /* It usually resets within 1 ms */
  1035. wait_ms(1);
  1036. if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
  1037. break;
  1038. }
  1039. if (!retries) {
  1040. ERR("software reset timeout");
  1041. ret = -1;
  1042. }
  1043. return ret;
  1044. }
  1045. static int isp116x_reset(struct isp116x *isp116x)
  1046. {
  1047. unsigned long t;
  1048. u16 clkrdy = 0;
  1049. int ret, timeout = 15 /* ms */ ;
  1050. DBG("");
  1051. ret = isp116x_sw_reset(isp116x);
  1052. if (ret)
  1053. return ret;
  1054. for (t = 0; t < timeout; t++) {
  1055. clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
  1056. if (clkrdy)
  1057. break;
  1058. wait_ms(1);
  1059. }
  1060. if (!clkrdy) {
  1061. ERR("clock not ready after %dms", timeout);
  1062. /* After sw_reset the clock won't report to be ready, if
  1063. H_WAKEUP pin is high. */
  1064. ERR("please make sure that the H_WAKEUP pin is pulled low!");
  1065. ret = -1;
  1066. }
  1067. return ret;
  1068. }
  1069. static void isp116x_stop(struct isp116x *isp116x)
  1070. {
  1071. u32 val;
  1072. DBG("");
  1073. isp116x_write_reg16(isp116x, HCuPINTENB, 0);
  1074. /* Switch off ports' power, some devices don't come up
  1075. after next 'start' without this */
  1076. val = isp116x_read_reg32(isp116x, HCRHDESCA);
  1077. val &= ~(RH_A_NPS | RH_A_PSM);
  1078. isp116x_write_reg32(isp116x, HCRHDESCA, val);
  1079. isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);
  1080. isp116x_sw_reset(isp116x);
  1081. }
  1082. /*
  1083. * Configure the chip. The chip must be successfully reset by now.
  1084. */
  1085. static int isp116x_start(struct isp116x *isp116x)
  1086. {
  1087. struct isp116x_platform_data *board = isp116x->board;
  1088. u32 val;
  1089. DBG("");
  1090. /* Clear interrupt status and disable all interrupt sources */
  1091. isp116x_write_reg16(isp116x, HCuPINT, 0xff);
  1092. isp116x_write_reg16(isp116x, HCuPINTENB, 0);
  1093. isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
  1094. isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);
  1095. /* Hardware configuration */
  1096. val = HCHWCFG_DBWIDTH(1);
  1097. if (board->sel15Kres)
  1098. val |= HCHWCFG_15KRSEL;
  1099. /* Remote wakeup won't work without working clock */
  1100. if (board->remote_wakeup_enable)
  1101. val |= HCHWCFG_CLKNOTSTOP;
  1102. if (board->oc_enable)
  1103. val |= HCHWCFG_ANALOG_OC;
  1104. isp116x_write_reg16(isp116x, HCHWCFG, val);
  1105. /* --- Root hub configuration */
  1106. val = (25 << 24) & RH_A_POTPGT;
  1107. /* AN10003_1.pdf recommends RH_A_NPS (no power switching) to
  1108. be always set. Yet, instead, we request individual port
  1109. power switching. */
  1110. val |= RH_A_PSM;
  1111. /* Report overcurrent per port */
  1112. val |= RH_A_OCPM;
  1113. isp116x_write_reg32(isp116x, HCRHDESCA, val);
  1114. isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
  1115. val = RH_B_PPCM;
  1116. isp116x_write_reg32(isp116x, HCRHDESCB, val);
  1117. isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
  1118. val = 0;
  1119. if (board->remote_wakeup_enable)
  1120. val |= RH_HS_DRWE;
  1121. isp116x_write_reg32(isp116x, HCRHSTATUS, val);
  1122. isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
  1123. isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);
  1124. /* Go operational */
  1125. val = HCCONTROL_USB_OPER;
  1126. if (board->remote_wakeup_enable)
  1127. val |= HCCONTROL_RWE;
  1128. isp116x_write_reg32(isp116x, HCCONTROL, val);
  1129. /* Disable ports to avoid race in device enumeration */
  1130. isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
  1131. isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);
  1132. isp116x_show_regs(isp116x);
  1133. isp116x->disabled = 0;
  1134. return 0;
  1135. }
  1136. /* --- Init functions ------------------------------------------------------ */
  1137. int isp116x_check_id(struct isp116x *isp116x)
  1138. {
  1139. int val;
  1140. val = isp116x_read_reg16(isp116x, HCCHIPID);
  1141. if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
  1142. ERR("invalid chip ID %04x", val);
  1143. return -1;
  1144. }
  1145. return 0;
  1146. }
  1147. int usb_lowlevel_init(void)
  1148. {
  1149. struct isp116x *isp116x = &isp116x_dev;
  1150. DBG("");
  1151. /* Init device registers addr */
  1152. isp116x->addr_reg = (u16 *) ISP116X_HCD_ADDR;
  1153. isp116x->data_reg = (u16 *) ISP116X_HCD_DATA;
  1154. /* Setup specific board settings */
  1155. #ifdef ISP116X_HCD_SEL15kRES
  1156. isp116x_board.sel15Kres = 1;
  1157. #endif
  1158. #ifdef ISP116X_HCD_OC_ENABLE
  1159. isp116x_board.oc_enable = 1;
  1160. #endif
  1161. #ifdef ISP116X_HCD_REMOTE_WAKEUP_ENABLE
  1162. isp116x_board.remote_wakeup_enable = 1;
  1163. #endif
  1164. isp116x->board = &isp116x_board;
  1165. /* Try to get ISP116x silicon chip ID */
  1166. if (isp116x_check_id(isp116x) < 0)
  1167. return -1;
  1168. isp116x->disabled = 1;
  1169. isp116x->sleeping = 0;
  1170. isp116x_reset(isp116x);
  1171. isp116x_start(isp116x);
  1172. return 0;
  1173. }
  1174. int usb_lowlevel_stop(void)
  1175. {
  1176. struct isp116x *isp116x = &isp116x_dev;
  1177. DBG("");
  1178. if (!isp116x->disabled)
  1179. isp116x_stop(isp116x);
  1180. return 0;
  1181. }
  1182. #endif /* CONFIG_USB_ISP116X_HCD */