pipe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /*
  2. * Renesas USB driver
  3. *
  4. * Copyright (C) 2011 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. *
  16. */
  17. #include <linux/delay.h>
  18. #include <linux/slab.h>
  19. #include "common.h"
  20. #include "pipe.h"
  21. /*
  22. * macros
  23. */
  24. #define usbhsp_addr_offset(p) ((usbhs_pipe_number(p) - 1) * 2)
  25. #define usbhsp_flags_set(p, f) ((p)->flags |= USBHS_PIPE_FLAGS_##f)
  26. #define usbhsp_flags_clr(p, f) ((p)->flags &= ~USBHS_PIPE_FLAGS_##f)
  27. #define usbhsp_flags_has(p, f) ((p)->flags & USBHS_PIPE_FLAGS_##f)
  28. #define usbhsp_flags_init(p) do {(p)->flags = 0; } while (0)
  29. /*
  30. * for debug
  31. */
  32. static char *usbhsp_pipe_name[] = {
  33. [USB_ENDPOINT_XFER_CONTROL] = "DCP",
  34. [USB_ENDPOINT_XFER_BULK] = "BULK",
  35. [USB_ENDPOINT_XFER_INT] = "INT",
  36. [USB_ENDPOINT_XFER_ISOC] = "ISO",
  37. };
  38. char *usbhs_pipe_name(struct usbhs_pipe *pipe)
  39. {
  40. return usbhsp_pipe_name[usbhs_pipe_type(pipe)];
  41. }
  42. /*
  43. * DCPCTR/PIPEnCTR functions
  44. */
  45. static void usbhsp_pipectrl_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
  46. {
  47. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  48. int offset = usbhsp_addr_offset(pipe);
  49. if (usbhs_pipe_is_dcp(pipe))
  50. usbhs_bset(priv, DCPCTR, mask, val);
  51. else
  52. usbhs_bset(priv, PIPEnCTR + offset, mask, val);
  53. }
  54. static u16 usbhsp_pipectrl_get(struct usbhs_pipe *pipe)
  55. {
  56. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  57. int offset = usbhsp_addr_offset(pipe);
  58. if (usbhs_pipe_is_dcp(pipe))
  59. return usbhs_read(priv, DCPCTR);
  60. else
  61. return usbhs_read(priv, PIPEnCTR + offset);
  62. }
  63. /*
  64. * DCP/PIPE functions
  65. */
  66. static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe,
  67. u16 dcp_reg, u16 pipe_reg,
  68. u16 mask, u16 val)
  69. {
  70. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  71. if (usbhs_pipe_is_dcp(pipe))
  72. usbhs_bset(priv, dcp_reg, mask, val);
  73. else
  74. usbhs_bset(priv, pipe_reg, mask, val);
  75. }
  76. static u16 __usbhsp_pipe_xxx_get(struct usbhs_pipe *pipe,
  77. u16 dcp_reg, u16 pipe_reg)
  78. {
  79. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  80. if (usbhs_pipe_is_dcp(pipe))
  81. return usbhs_read(priv, dcp_reg);
  82. else
  83. return usbhs_read(priv, pipe_reg);
  84. }
  85. /*
  86. * DCPCFG/PIPECFG functions
  87. */
  88. static void usbhsp_pipe_cfg_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
  89. {
  90. __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val);
  91. }
  92. static u16 usbhsp_pipe_cfg_get(struct usbhs_pipe *pipe)
  93. {
  94. return __usbhsp_pipe_xxx_get(pipe, DCPCFG, PIPECFG);
  95. }
  96. /*
  97. * PIPEnTRN/PIPEnTRE functions
  98. */
  99. static void usbhsp_pipe_trn_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
  100. {
  101. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  102. struct device *dev = usbhs_priv_to_dev(priv);
  103. int num = usbhs_pipe_number(pipe);
  104. u16 reg;
  105. /*
  106. * It is impossible to calculate address,
  107. * since PIPEnTRN addresses were mapped randomly.
  108. */
  109. #define CASE_PIPExTRN(a) \
  110. case 0x ## a: \
  111. reg = PIPE ## a ## TRN; \
  112. break;
  113. switch (num) {
  114. CASE_PIPExTRN(1);
  115. CASE_PIPExTRN(2);
  116. CASE_PIPExTRN(3);
  117. CASE_PIPExTRN(4);
  118. CASE_PIPExTRN(5);
  119. CASE_PIPExTRN(B);
  120. CASE_PIPExTRN(C);
  121. CASE_PIPExTRN(D);
  122. CASE_PIPExTRN(E);
  123. CASE_PIPExTRN(F);
  124. CASE_PIPExTRN(9);
  125. CASE_PIPExTRN(A);
  126. default:
  127. dev_err(dev, "unknown pipe (%d)\n", num);
  128. return;
  129. }
  130. __usbhsp_pipe_xxx_set(pipe, 0, reg, mask, val);
  131. }
  132. static void usbhsp_pipe_tre_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
  133. {
  134. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  135. struct device *dev = usbhs_priv_to_dev(priv);
  136. int num = usbhs_pipe_number(pipe);
  137. u16 reg;
  138. /*
  139. * It is impossible to calculate address,
  140. * since PIPEnTRE addresses were mapped randomly.
  141. */
  142. #define CASE_PIPExTRE(a) \
  143. case 0x ## a: \
  144. reg = PIPE ## a ## TRE; \
  145. break;
  146. switch (num) {
  147. CASE_PIPExTRE(1);
  148. CASE_PIPExTRE(2);
  149. CASE_PIPExTRE(3);
  150. CASE_PIPExTRE(4);
  151. CASE_PIPExTRE(5);
  152. CASE_PIPExTRE(B);
  153. CASE_PIPExTRE(C);
  154. CASE_PIPExTRE(D);
  155. CASE_PIPExTRE(E);
  156. CASE_PIPExTRE(F);
  157. CASE_PIPExTRE(9);
  158. CASE_PIPExTRE(A);
  159. default:
  160. dev_err(dev, "unknown pipe (%d)\n", num);
  161. return;
  162. }
  163. __usbhsp_pipe_xxx_set(pipe, 0, reg, mask, val);
  164. }
  165. /*
  166. * PIPEBUF
  167. */
  168. static void usbhsp_pipe_buf_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
  169. {
  170. if (usbhs_pipe_is_dcp(pipe))
  171. return;
  172. __usbhsp_pipe_xxx_set(pipe, 0, PIPEBUF, mask, val);
  173. }
  174. /*
  175. * DCPMAXP/PIPEMAXP
  176. */
  177. static void usbhsp_pipe_maxp_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
  178. {
  179. __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val);
  180. }
  181. /*
  182. * pipe control functions
  183. */
  184. static void usbhsp_pipe_select(struct usbhs_pipe *pipe)
  185. {
  186. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  187. /*
  188. * On pipe, this is necessary before
  189. * accesses to below registers.
  190. *
  191. * PIPESEL : usbhsp_pipe_select
  192. * PIPECFG : usbhsp_pipe_cfg_xxx
  193. * PIPEBUF : usbhsp_pipe_buf_xxx
  194. * PIPEMAXP : usbhsp_pipe_maxp_xxx
  195. * PIPEPERI
  196. */
  197. /*
  198. * if pipe is dcp, no pipe is selected.
  199. * it is no problem, because dcp have its register
  200. */
  201. usbhs_write(priv, PIPESEL, 0xF & usbhs_pipe_number(pipe));
  202. }
  203. static int usbhsp_pipe_barrier(struct usbhs_pipe *pipe)
  204. {
  205. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  206. int timeout = 1024;
  207. u16 val;
  208. /*
  209. * make sure....
  210. *
  211. * Modify these bits when CSSTS = 0, PID = NAK, and no pipe number is
  212. * specified by the CURPIPE bits.
  213. * When changing the setting of this bit after changing
  214. * the PID bits for the selected pipe from BUF to NAK,
  215. * check that CSSTS = 0 and PBUSY = 0.
  216. */
  217. /*
  218. * CURPIPE bit = 0
  219. *
  220. * see also
  221. * "Operation"
  222. * - "Pipe Control"
  223. * - "Pipe Control Registers Switching Procedure"
  224. */
  225. usbhs_write(priv, CFIFOSEL, 0);
  226. usbhs_pipe_disable(pipe);
  227. do {
  228. val = usbhsp_pipectrl_get(pipe);
  229. val &= CSSTS | PID_MASK;
  230. if (!val)
  231. return 0;
  232. udelay(10);
  233. } while (timeout--);
  234. return -EBUSY;
  235. }
  236. int usbhs_pipe_is_accessible(struct usbhs_pipe *pipe)
  237. {
  238. u16 val;
  239. val = usbhsp_pipectrl_get(pipe);
  240. if (val & BSTS)
  241. return 0;
  242. return -EBUSY;
  243. }
  244. /*
  245. * PID ctrl
  246. */
  247. static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe)
  248. {
  249. u16 pid = usbhsp_pipectrl_get(pipe);
  250. pid &= PID_MASK;
  251. /*
  252. * see
  253. * "Pipe n Control Register" - "PID"
  254. */
  255. switch (pid) {
  256. case PID_STALL11:
  257. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
  258. /* fall-through */
  259. case PID_STALL10:
  260. usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
  261. }
  262. }
  263. void usbhs_pipe_disable(struct usbhs_pipe *pipe)
  264. {
  265. int timeout = 1024;
  266. u16 val;
  267. /* see "Pipe n Control Register" - "PID" */
  268. __usbhsp_pid_try_nak_if_stall(pipe);
  269. usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
  270. do {
  271. val = usbhsp_pipectrl_get(pipe);
  272. val &= PBUSY;
  273. if (!val)
  274. break;
  275. udelay(10);
  276. } while (timeout--);
  277. }
  278. void usbhs_pipe_enable(struct usbhs_pipe *pipe)
  279. {
  280. /* see "Pipe n Control Register" - "PID" */
  281. __usbhsp_pid_try_nak_if_stall(pipe);
  282. usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF);
  283. }
  284. void usbhs_pipe_stall(struct usbhs_pipe *pipe)
  285. {
  286. u16 pid = usbhsp_pipectrl_get(pipe);
  287. pid &= PID_MASK;
  288. /*
  289. * see
  290. * "Pipe n Control Register" - "PID"
  291. */
  292. switch (pid) {
  293. case PID_NAK:
  294. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
  295. break;
  296. case PID_BUF:
  297. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11);
  298. break;
  299. }
  300. }
  301. int usbhs_pipe_is_stall(struct usbhs_pipe *pipe)
  302. {
  303. u16 pid = usbhsp_pipectrl_get(pipe) & PID_MASK;
  304. return (int)(pid == PID_STALL10 || pid == PID_STALL11);
  305. }
  306. void usbhs_pipe_set_trans_count_if_bulk(struct usbhs_pipe *pipe, int len)
  307. {
  308. if (!usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  309. return;
  310. /*
  311. * clear and disable transfer counter for IN/OUT pipe
  312. */
  313. usbhsp_pipe_tre_set(pipe, TRCLR | TRENB, TRCLR);
  314. /*
  315. * Only IN direction bulk pipe can use transfer count.
  316. * Without using this function,
  317. * received data will break if it was large data size.
  318. * see PIPEnTRN/PIPEnTRE for detail
  319. */
  320. if (usbhs_pipe_is_dir_in(pipe)) {
  321. int maxp = usbhs_pipe_get_maxpacket(pipe);
  322. usbhsp_pipe_trn_set(pipe, 0xffff, DIV_ROUND_UP(len, maxp));
  323. usbhsp_pipe_tre_set(pipe, TRENB, TRENB); /* enable */
  324. }
  325. }
  326. /*
  327. * pipe setup
  328. */
  329. static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe)
  330. {
  331. /*
  332. * only ISO / BULK pipe can use double buffer
  333. */
  334. if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) ||
  335. usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
  336. return 1;
  337. return 0;
  338. }
  339. static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
  340. int is_host,
  341. int dir_in)
  342. {
  343. u16 type = 0;
  344. u16 bfre = 0;
  345. u16 dblb = 0;
  346. u16 cntmd = 0;
  347. u16 dir = 0;
  348. u16 epnum = 0;
  349. u16 shtnak = 0;
  350. u16 type_array[] = {
  351. [USB_ENDPOINT_XFER_BULK] = TYPE_BULK,
  352. [USB_ENDPOINT_XFER_INT] = TYPE_INT,
  353. [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO,
  354. };
  355. int is_double = usbhsp_possible_double_buffer(pipe);
  356. if (usbhs_pipe_is_dcp(pipe))
  357. return -EINVAL;
  358. /*
  359. * PIPECFG
  360. *
  361. * see
  362. * - "Register Descriptions" - "PIPECFG" register
  363. * - "Features" - "Pipe configuration"
  364. * - "Operation" - "Pipe Control"
  365. */
  366. /* TYPE */
  367. type = type_array[usbhs_pipe_type(pipe)];
  368. /* BFRE */
  369. if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
  370. usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  371. bfre = 0; /* FIXME */
  372. /* DBLB */
  373. if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
  374. usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  375. dblb = (is_double) ? DBLB : 0;
  376. /* CNTMD */
  377. if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  378. cntmd = 0; /* FIXME */
  379. /* DIR */
  380. if (dir_in)
  381. usbhsp_flags_set(pipe, IS_DIR_HOST);
  382. if (!!is_host ^ !!dir_in)
  383. dir |= DIR_OUT;
  384. if (!dir)
  385. usbhsp_flags_set(pipe, IS_DIR_IN);
  386. /* SHTNAK */
  387. if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) &&
  388. !dir)
  389. shtnak = SHTNAK;
  390. /* EPNUM */
  391. epnum = 0; /* see usbhs_pipe_config_update() */
  392. return type |
  393. bfre |
  394. dblb |
  395. cntmd |
  396. dir |
  397. shtnak |
  398. epnum;
  399. }
  400. static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe)
  401. {
  402. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  403. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  404. struct device *dev = usbhs_priv_to_dev(priv);
  405. int pipe_num = usbhs_pipe_number(pipe);
  406. int is_double = usbhsp_possible_double_buffer(pipe);
  407. u16 buff_size;
  408. u16 bufnmb;
  409. u16 bufnmb_cnt;
  410. /*
  411. * PIPEBUF
  412. *
  413. * see
  414. * - "Register Descriptions" - "PIPEBUF" register
  415. * - "Features" - "Pipe configuration"
  416. * - "Operation" - "FIFO Buffer Memory"
  417. * - "Operation" - "Pipe Control"
  418. *
  419. * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724)
  420. *
  421. * BUFNMB: PIPE
  422. * 0: pipe0 (DCP 256byte)
  423. * 1: -
  424. * 2: -
  425. * 3: -
  426. * 4: pipe6 (INT 64byte)
  427. * 5: pipe7 (INT 64byte)
  428. * 6: pipe8 (INT 64byte)
  429. * 7: pipe9 (INT 64byte)
  430. * 8 - xx: free (for BULK, ISOC)
  431. */
  432. /*
  433. * FIXME
  434. *
  435. * it doesn't have good buffer allocator
  436. *
  437. * DCP : 256 byte
  438. * BULK: 512 byte
  439. * INT : 64 byte
  440. * ISOC: 512 byte
  441. */
  442. if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_CONTROL))
  443. buff_size = 256;
  444. else if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
  445. buff_size = 64;
  446. else
  447. buff_size = 512;
  448. /* change buff_size to register value */
  449. bufnmb_cnt = (buff_size / 64) - 1;
  450. /* BUFNMB has been reserved for INT pipe
  451. * see above */
  452. if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT)) {
  453. bufnmb = pipe_num - 2;
  454. } else {
  455. bufnmb = info->bufnmb_last;
  456. info->bufnmb_last += bufnmb_cnt + 1;
  457. /*
  458. * double buffer
  459. */
  460. if (is_double)
  461. info->bufnmb_last += bufnmb_cnt + 1;
  462. }
  463. dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n",
  464. pipe_num, buff_size, bufnmb);
  465. return (0x1f & bufnmb_cnt) << 10 |
  466. (0xff & bufnmb) << 0;
  467. }
  468. void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 devsel,
  469. u16 epnum, u16 maxp)
  470. {
  471. if (devsel > 0xA) {
  472. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  473. struct device *dev = usbhs_priv_to_dev(priv);
  474. dev_err(dev, "devsel error %d\n", devsel);
  475. devsel = 0;
  476. }
  477. usbhsp_pipe_barrier(pipe);
  478. pipe->maxp = maxp;
  479. usbhsp_pipe_select(pipe);
  480. usbhsp_pipe_maxp_set(pipe, 0xFFFF,
  481. (devsel << 12) |
  482. maxp);
  483. if (!usbhs_pipe_is_dcp(pipe))
  484. usbhsp_pipe_cfg_set(pipe, 0x000F, epnum);
  485. }
  486. /*
  487. * pipe control
  488. */
  489. int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe)
  490. {
  491. /*
  492. * see
  493. * usbhs_pipe_config_update()
  494. * usbhs_dcp_malloc()
  495. */
  496. return pipe->maxp;
  497. }
  498. int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe)
  499. {
  500. return usbhsp_flags_has(pipe, IS_DIR_IN);
  501. }
  502. int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe)
  503. {
  504. return usbhsp_flags_has(pipe, IS_DIR_HOST);
  505. }
  506. int usbhs_pipe_is_running(struct usbhs_pipe *pipe)
  507. {
  508. return usbhsp_flags_has(pipe, IS_RUNNING);
  509. }
  510. void usbhs_pipe_running(struct usbhs_pipe *pipe, int running)
  511. {
  512. if (running)
  513. usbhsp_flags_set(pipe, IS_RUNNING);
  514. else
  515. usbhsp_flags_clr(pipe, IS_RUNNING);
  516. }
  517. void usbhs_pipe_data_sequence(struct usbhs_pipe *pipe, int sequence)
  518. {
  519. u16 mask = (SQCLR | SQSET);
  520. u16 val;
  521. /*
  522. * sequence
  523. * 0 : data0
  524. * 1 : data1
  525. * -1 : no change
  526. */
  527. switch (sequence) {
  528. case 0:
  529. val = SQCLR;
  530. break;
  531. case 1:
  532. val = SQSET;
  533. break;
  534. default:
  535. return;
  536. }
  537. usbhsp_pipectrl_set(pipe, mask, val);
  538. }
  539. static int usbhs_pipe_get_data_sequence(struct usbhs_pipe *pipe)
  540. {
  541. return !!(usbhsp_pipectrl_get(pipe) & SQMON);
  542. }
  543. void usbhs_pipe_clear(struct usbhs_pipe *pipe)
  544. {
  545. if (usbhs_pipe_is_dcp(pipe)) {
  546. usbhs_fifo_clear_dcp(pipe);
  547. } else {
  548. usbhsp_pipectrl_set(pipe, ACLRM, ACLRM);
  549. usbhsp_pipectrl_set(pipe, ACLRM, 0);
  550. }
  551. }
  552. void usbhs_pipe_config_change_bfre(struct usbhs_pipe *pipe, int enable)
  553. {
  554. int sequence;
  555. if (usbhs_pipe_is_dcp(pipe))
  556. return;
  557. usbhsp_pipe_select(pipe);
  558. /* check if the driver needs to change the BFRE value */
  559. if (!(enable ^ !!(usbhsp_pipe_cfg_get(pipe) & BFRE)))
  560. return;
  561. sequence = usbhs_pipe_get_data_sequence(pipe);
  562. usbhsp_pipe_cfg_set(pipe, BFRE, enable ? BFRE : 0);
  563. usbhs_pipe_clear(pipe);
  564. usbhs_pipe_data_sequence(pipe, sequence);
  565. }
  566. static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
  567. {
  568. struct usbhs_pipe *pos, *pipe;
  569. int i;
  570. /*
  571. * find target pipe
  572. */
  573. pipe = NULL;
  574. usbhs_for_each_pipe_with_dcp(pos, priv, i) {
  575. if (!usbhs_pipe_type_is(pos, type))
  576. continue;
  577. if (usbhsp_flags_has(pos, IS_USED))
  578. continue;
  579. pipe = pos;
  580. break;
  581. }
  582. if (!pipe)
  583. return NULL;
  584. /*
  585. * initialize pipe flags
  586. */
  587. usbhsp_flags_init(pipe);
  588. usbhsp_flags_set(pipe, IS_USED);
  589. return pipe;
  590. }
  591. static void usbhsp_put_pipe(struct usbhs_pipe *pipe)
  592. {
  593. usbhsp_flags_init(pipe);
  594. }
  595. void usbhs_pipe_init(struct usbhs_priv *priv,
  596. int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map))
  597. {
  598. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  599. struct usbhs_pipe *pipe;
  600. int i;
  601. /*
  602. * FIXME
  603. *
  604. * driver needs good allocator.
  605. *
  606. * find first free buffer area (BULK, ISOC)
  607. * (DCP, INT area is fixed)
  608. *
  609. * buffer number 0 - 3 have been reserved for DCP
  610. * see
  611. * usbhsp_to_bufnmb
  612. */
  613. info->bufnmb_last = 4;
  614. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  615. if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
  616. info->bufnmb_last++;
  617. usbhsp_flags_init(pipe);
  618. pipe->fifo = NULL;
  619. pipe->mod_private = NULL;
  620. INIT_LIST_HEAD(&pipe->list);
  621. /* pipe force init */
  622. usbhs_pipe_clear(pipe);
  623. }
  624. info->dma_map_ctrl = dma_map_ctrl;
  625. }
  626. struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
  627. int endpoint_type,
  628. int dir_in)
  629. {
  630. struct device *dev = usbhs_priv_to_dev(priv);
  631. struct usbhs_pipe *pipe;
  632. int is_host = usbhs_mod_is_host(priv);
  633. int ret;
  634. u16 pipecfg, pipebuf;
  635. pipe = usbhsp_get_pipe(priv, endpoint_type);
  636. if (!pipe) {
  637. dev_err(dev, "can't get pipe (%s)\n",
  638. usbhsp_pipe_name[endpoint_type]);
  639. return NULL;
  640. }
  641. INIT_LIST_HEAD(&pipe->list);
  642. usbhs_pipe_disable(pipe);
  643. /* make sure pipe is not busy */
  644. ret = usbhsp_pipe_barrier(pipe);
  645. if (ret < 0) {
  646. dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe));
  647. return NULL;
  648. }
  649. pipecfg = usbhsp_setup_pipecfg(pipe, is_host, dir_in);
  650. pipebuf = usbhsp_setup_pipebuff(pipe);
  651. usbhsp_pipe_select(pipe);
  652. usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg);
  653. usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf);
  654. usbhs_pipe_clear(pipe);
  655. usbhs_pipe_sequence_data0(pipe);
  656. dev_dbg(dev, "enable pipe %d : %s (%s)\n",
  657. usbhs_pipe_number(pipe),
  658. usbhs_pipe_name(pipe),
  659. usbhs_pipe_is_dir_in(pipe) ? "in" : "out");
  660. /*
  661. * epnum / maxp are still not set to this pipe.
  662. * call usbhs_pipe_config_update() after this function !!
  663. */
  664. return pipe;
  665. }
  666. void usbhs_pipe_free(struct usbhs_pipe *pipe)
  667. {
  668. usbhsp_put_pipe(pipe);
  669. }
  670. void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo)
  671. {
  672. if (pipe->fifo)
  673. pipe->fifo->pipe = NULL;
  674. pipe->fifo = fifo;
  675. if (fifo)
  676. fifo->pipe = pipe;
  677. }
  678. /*
  679. * dcp control
  680. */
  681. struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv)
  682. {
  683. struct usbhs_pipe *pipe;
  684. pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL);
  685. if (!pipe)
  686. return NULL;
  687. INIT_LIST_HEAD(&pipe->list);
  688. /*
  689. * call usbhs_pipe_config_update() after this function !!
  690. */
  691. return pipe;
  692. }
  693. void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe)
  694. {
  695. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  696. WARN_ON(!usbhs_pipe_is_dcp(pipe));
  697. usbhs_pipe_enable(pipe);
  698. if (!usbhs_mod_is_host(priv)) /* funconly */
  699. usbhsp_pipectrl_set(pipe, CCPL, CCPL);
  700. }
  701. void usbhs_dcp_dir_for_host(struct usbhs_pipe *pipe, int dir_out)
  702. {
  703. usbhsp_pipe_cfg_set(pipe, DIR_OUT,
  704. dir_out ? DIR_OUT : 0);
  705. }
  706. /*
  707. * pipe module function
  708. */
  709. int usbhs_pipe_probe(struct usbhs_priv *priv)
  710. {
  711. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  712. struct usbhs_pipe *pipe;
  713. struct device *dev = usbhs_priv_to_dev(priv);
  714. u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
  715. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  716. int i;
  717. /* This driver expects 1st pipe is DCP */
  718. if (pipe_type[0] != USB_ENDPOINT_XFER_CONTROL) {
  719. dev_err(dev, "1st PIPE is not DCP\n");
  720. return -EINVAL;
  721. }
  722. info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL);
  723. if (!info->pipe) {
  724. dev_err(dev, "Could not allocate pipe\n");
  725. return -ENOMEM;
  726. }
  727. info->size = pipe_size;
  728. /*
  729. * init pipe
  730. */
  731. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  732. pipe->priv = priv;
  733. usbhs_pipe_type(pipe) =
  734. pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK;
  735. dev_dbg(dev, "pipe %x\t: %s\n",
  736. i, usbhsp_pipe_name[pipe_type[i]]);
  737. }
  738. return 0;
  739. }
  740. void usbhs_pipe_remove(struct usbhs_priv *priv)
  741. {
  742. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  743. kfree(info->pipe);
  744. }