pipe.c 17 KB

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