pipe.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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. /*
  241. * PID ctrl
  242. */
  243. static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe)
  244. {
  245. u16 pid = usbhsp_pipectrl_get(pipe);
  246. pid &= PID_MASK;
  247. /*
  248. * see
  249. * "Pipe n Control Register" - "PID"
  250. */
  251. switch (pid) {
  252. case PID_STALL11:
  253. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
  254. /* fall-through */
  255. case PID_STALL10:
  256. usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
  257. }
  258. }
  259. void usbhs_pipe_disable(struct usbhs_pipe *pipe)
  260. {
  261. int timeout = 1024;
  262. u16 val;
  263. /* see "Pipe n Control Register" - "PID" */
  264. __usbhsp_pid_try_nak_if_stall(pipe);
  265. usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
  266. do {
  267. val = usbhsp_pipectrl_get(pipe);
  268. val &= PBUSY;
  269. if (!val)
  270. break;
  271. udelay(10);
  272. } while (timeout--);
  273. }
  274. void usbhs_pipe_enable(struct usbhs_pipe *pipe)
  275. {
  276. /* see "Pipe n Control Register" - "PID" */
  277. __usbhsp_pid_try_nak_if_stall(pipe);
  278. usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF);
  279. }
  280. void usbhs_pipe_stall(struct usbhs_pipe *pipe)
  281. {
  282. u16 pid = usbhsp_pipectrl_get(pipe);
  283. pid &= PID_MASK;
  284. /*
  285. * see
  286. * "Pipe n Control Register" - "PID"
  287. */
  288. switch (pid) {
  289. case PID_NAK:
  290. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
  291. break;
  292. case PID_BUF:
  293. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11);
  294. break;
  295. }
  296. }
  297. int usbhs_pipe_is_stall(struct usbhs_pipe *pipe)
  298. {
  299. u16 pid = usbhsp_pipectrl_get(pipe) & PID_MASK;
  300. return (int)(pid == PID_STALL10 || pid == PID_STALL11);
  301. }
  302. void usbhs_pipe_set_trans_count_if_bulk(struct usbhs_pipe *pipe, int len)
  303. {
  304. if (!usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  305. return;
  306. /*
  307. * clear and disable transfer counter for IN/OUT pipe
  308. */
  309. usbhsp_pipe_tre_set(pipe, TRCLR | TRENB, TRCLR);
  310. /*
  311. * Only IN direction bulk pipe can use transfer count.
  312. * Without using this function,
  313. * received data will break if it was large data size.
  314. * see PIPEnTRN/PIPEnTRE for detail
  315. */
  316. if (usbhs_pipe_is_dir_in(pipe)) {
  317. int maxp = usbhs_pipe_get_maxpacket(pipe);
  318. usbhsp_pipe_trn_set(pipe, 0xffff, DIV_ROUND_UP(len, maxp));
  319. usbhsp_pipe_tre_set(pipe, TRENB, TRENB); /* enable */
  320. }
  321. }
  322. /*
  323. * pipe setup
  324. */
  325. static int usbhsp_setup_pipecfg(struct usbhs_pipe *pipe, int is_host,
  326. int dir_in, u16 *pipecfg)
  327. {
  328. u16 type = 0;
  329. u16 bfre = 0;
  330. u16 dblb = 0;
  331. u16 cntmd = 0;
  332. u16 dir = 0;
  333. u16 epnum = 0;
  334. u16 shtnak = 0;
  335. static const u16 type_array[] = {
  336. [USB_ENDPOINT_XFER_BULK] = TYPE_BULK,
  337. [USB_ENDPOINT_XFER_INT] = TYPE_INT,
  338. [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO,
  339. };
  340. if (usbhs_pipe_is_dcp(pipe))
  341. return -EINVAL;
  342. /*
  343. * PIPECFG
  344. *
  345. * see
  346. * - "Register Descriptions" - "PIPECFG" register
  347. * - "Features" - "Pipe configuration"
  348. * - "Operation" - "Pipe Control"
  349. */
  350. /* TYPE */
  351. type = type_array[usbhs_pipe_type(pipe)];
  352. /* BFRE */
  353. if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
  354. usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  355. bfre = 0; /* FIXME */
  356. /* DBLB: see usbhs_pipe_config_update() */
  357. /* CNTMD */
  358. if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  359. cntmd = 0; /* FIXME */
  360. /* DIR */
  361. if (dir_in)
  362. usbhsp_flags_set(pipe, IS_DIR_HOST);
  363. if (!!is_host ^ !!dir_in)
  364. dir |= DIR_OUT;
  365. if (!dir)
  366. usbhsp_flags_set(pipe, IS_DIR_IN);
  367. /* SHTNAK */
  368. if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) &&
  369. !dir)
  370. shtnak = SHTNAK;
  371. /* EPNUM */
  372. epnum = 0; /* see usbhs_pipe_config_update() */
  373. *pipecfg = type |
  374. bfre |
  375. dblb |
  376. cntmd |
  377. dir |
  378. shtnak |
  379. epnum;
  380. return 0;
  381. }
  382. static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe)
  383. {
  384. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  385. struct device *dev = usbhs_priv_to_dev(priv);
  386. int pipe_num = usbhs_pipe_number(pipe);
  387. u16 buff_size;
  388. u16 bufnmb;
  389. u16 bufnmb_cnt;
  390. struct renesas_usbhs_driver_pipe_config *pipe_config =
  391. usbhsp_get_pipe_config(priv, pipe_num);
  392. /*
  393. * PIPEBUF
  394. *
  395. * see
  396. * - "Register Descriptions" - "PIPEBUF" register
  397. * - "Features" - "Pipe configuration"
  398. * - "Operation" - "FIFO Buffer Memory"
  399. * - "Operation" - "Pipe Control"
  400. */
  401. buff_size = pipe_config->bufsize;
  402. bufnmb = pipe_config->bufnum;
  403. /* change buff_size to register value */
  404. bufnmb_cnt = (buff_size / 64) - 1;
  405. dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n",
  406. pipe_num, buff_size, bufnmb);
  407. return (0x1f & bufnmb_cnt) << 10 |
  408. (0xff & bufnmb) << 0;
  409. }
  410. void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 devsel,
  411. u16 epnum, u16 maxp)
  412. {
  413. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  414. int pipe_num = usbhs_pipe_number(pipe);
  415. struct renesas_usbhs_driver_pipe_config *pipe_config =
  416. usbhsp_get_pipe_config(priv, pipe_num);
  417. u16 dblb = pipe_config->double_buf ? DBLB : 0;
  418. if (devsel > 0xA) {
  419. struct device *dev = usbhs_priv_to_dev(priv);
  420. dev_err(dev, "devsel error %d\n", devsel);
  421. devsel = 0;
  422. }
  423. usbhsp_pipe_barrier(pipe);
  424. pipe->maxp = maxp;
  425. usbhsp_pipe_select(pipe);
  426. usbhsp_pipe_maxp_set(pipe, 0xFFFF,
  427. (devsel << 12) |
  428. maxp);
  429. if (!usbhs_pipe_is_dcp(pipe))
  430. usbhsp_pipe_cfg_set(pipe, 0x000F | DBLB, epnum | dblb);
  431. }
  432. /*
  433. * pipe control
  434. */
  435. int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe)
  436. {
  437. /*
  438. * see
  439. * usbhs_pipe_config_update()
  440. * usbhs_dcp_malloc()
  441. */
  442. return pipe->maxp;
  443. }
  444. int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe)
  445. {
  446. return usbhsp_flags_has(pipe, IS_DIR_IN);
  447. }
  448. int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe)
  449. {
  450. return usbhsp_flags_has(pipe, IS_DIR_HOST);
  451. }
  452. int usbhs_pipe_is_running(struct usbhs_pipe *pipe)
  453. {
  454. return usbhsp_flags_has(pipe, IS_RUNNING);
  455. }
  456. void usbhs_pipe_running(struct usbhs_pipe *pipe, int running)
  457. {
  458. if (running)
  459. usbhsp_flags_set(pipe, IS_RUNNING);
  460. else
  461. usbhsp_flags_clr(pipe, IS_RUNNING);
  462. }
  463. void usbhs_pipe_data_sequence(struct usbhs_pipe *pipe, int sequence)
  464. {
  465. u16 mask = (SQCLR | SQSET);
  466. u16 val;
  467. /*
  468. * sequence
  469. * 0 : data0
  470. * 1 : data1
  471. * -1 : no change
  472. */
  473. switch (sequence) {
  474. case 0:
  475. val = SQCLR;
  476. break;
  477. case 1:
  478. val = SQSET;
  479. break;
  480. default:
  481. return;
  482. }
  483. usbhsp_pipectrl_set(pipe, mask, val);
  484. }
  485. static int usbhs_pipe_get_data_sequence(struct usbhs_pipe *pipe)
  486. {
  487. return !!(usbhsp_pipectrl_get(pipe) & SQMON);
  488. }
  489. void usbhs_pipe_clear(struct usbhs_pipe *pipe)
  490. {
  491. if (usbhs_pipe_is_dcp(pipe)) {
  492. usbhs_fifo_clear_dcp(pipe);
  493. } else {
  494. usbhsp_pipectrl_set(pipe, ACLRM, ACLRM);
  495. usbhsp_pipectrl_set(pipe, ACLRM, 0);
  496. }
  497. }
  498. /* Should call usbhsp_pipe_select() before */
  499. void usbhs_pipe_clear_without_sequence(struct usbhs_pipe *pipe,
  500. int needs_bfre, int bfre_enable)
  501. {
  502. int sequence;
  503. usbhsp_pipe_select(pipe);
  504. sequence = usbhs_pipe_get_data_sequence(pipe);
  505. if (needs_bfre)
  506. usbhsp_pipe_cfg_set(pipe, BFRE, bfre_enable ? BFRE : 0);
  507. usbhs_pipe_clear(pipe);
  508. usbhs_pipe_data_sequence(pipe, sequence);
  509. }
  510. void usbhs_pipe_config_change_bfre(struct usbhs_pipe *pipe, int enable)
  511. {
  512. if (usbhs_pipe_is_dcp(pipe))
  513. return;
  514. usbhsp_pipe_select(pipe);
  515. /* check if the driver needs to change the BFRE value */
  516. if (!(enable ^ !!(usbhsp_pipe_cfg_get(pipe) & BFRE)))
  517. return;
  518. usbhs_pipe_clear_without_sequence(pipe, 1, enable);
  519. }
  520. static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
  521. {
  522. struct usbhs_pipe *pos, *pipe;
  523. int i;
  524. /*
  525. * find target pipe
  526. */
  527. pipe = NULL;
  528. usbhs_for_each_pipe_with_dcp(pos, priv, i) {
  529. if (!usbhs_pipe_type_is(pos, type))
  530. continue;
  531. if (usbhsp_flags_has(pos, IS_USED))
  532. continue;
  533. pipe = pos;
  534. break;
  535. }
  536. if (!pipe)
  537. return NULL;
  538. /*
  539. * initialize pipe flags
  540. */
  541. usbhsp_flags_init(pipe);
  542. usbhsp_flags_set(pipe, IS_USED);
  543. return pipe;
  544. }
  545. static void usbhsp_put_pipe(struct usbhs_pipe *pipe)
  546. {
  547. usbhsp_flags_init(pipe);
  548. }
  549. void usbhs_pipe_init(struct usbhs_priv *priv,
  550. int (*dma_map_ctrl)(struct device *dma_dev,
  551. struct usbhs_pkt *pkt, int map))
  552. {
  553. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  554. struct usbhs_pipe *pipe;
  555. int i;
  556. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  557. usbhsp_flags_init(pipe);
  558. pipe->fifo = NULL;
  559. pipe->mod_private = NULL;
  560. INIT_LIST_HEAD(&pipe->list);
  561. /* pipe force init */
  562. usbhs_pipe_clear(pipe);
  563. }
  564. info->dma_map_ctrl = dma_map_ctrl;
  565. }
  566. struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
  567. int endpoint_type,
  568. int dir_in)
  569. {
  570. struct device *dev = usbhs_priv_to_dev(priv);
  571. struct usbhs_pipe *pipe;
  572. int is_host = usbhs_mod_is_host(priv);
  573. int ret;
  574. u16 pipecfg, pipebuf;
  575. pipe = usbhsp_get_pipe(priv, endpoint_type);
  576. if (!pipe) {
  577. dev_err(dev, "can't get pipe (%s)\n",
  578. usbhsp_pipe_name[endpoint_type]);
  579. return NULL;
  580. }
  581. INIT_LIST_HEAD(&pipe->list);
  582. usbhs_pipe_disable(pipe);
  583. /* make sure pipe is not busy */
  584. ret = usbhsp_pipe_barrier(pipe);
  585. if (ret < 0) {
  586. dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe));
  587. return NULL;
  588. }
  589. if (usbhsp_setup_pipecfg(pipe, is_host, dir_in, &pipecfg)) {
  590. dev_err(dev, "can't setup pipe\n");
  591. return NULL;
  592. }
  593. pipebuf = usbhsp_setup_pipebuff(pipe);
  594. usbhsp_pipe_select(pipe);
  595. usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg);
  596. usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf);
  597. usbhs_pipe_clear(pipe);
  598. usbhs_pipe_sequence_data0(pipe);
  599. dev_dbg(dev, "enable pipe %d : %s (%s)\n",
  600. usbhs_pipe_number(pipe),
  601. usbhs_pipe_name(pipe),
  602. usbhs_pipe_is_dir_in(pipe) ? "in" : "out");
  603. /*
  604. * epnum / maxp are still not set to this pipe.
  605. * call usbhs_pipe_config_update() after this function !!
  606. */
  607. return pipe;
  608. }
  609. void usbhs_pipe_free(struct usbhs_pipe *pipe)
  610. {
  611. usbhsp_put_pipe(pipe);
  612. }
  613. void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo)
  614. {
  615. if (pipe->fifo)
  616. pipe->fifo->pipe = NULL;
  617. pipe->fifo = fifo;
  618. if (fifo)
  619. fifo->pipe = pipe;
  620. }
  621. /*
  622. * dcp control
  623. */
  624. struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv)
  625. {
  626. struct usbhs_pipe *pipe;
  627. pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL);
  628. if (!pipe)
  629. return NULL;
  630. INIT_LIST_HEAD(&pipe->list);
  631. /*
  632. * call usbhs_pipe_config_update() after this function !!
  633. */
  634. return pipe;
  635. }
  636. void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe)
  637. {
  638. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  639. WARN_ON(!usbhs_pipe_is_dcp(pipe));
  640. usbhs_pipe_enable(pipe);
  641. if (!usbhs_mod_is_host(priv)) /* funconly */
  642. usbhsp_pipectrl_set(pipe, CCPL, CCPL);
  643. }
  644. void usbhs_dcp_dir_for_host(struct usbhs_pipe *pipe, int dir_out)
  645. {
  646. usbhsp_pipe_cfg_set(pipe, DIR_OUT,
  647. dir_out ? DIR_OUT : 0);
  648. }
  649. /*
  650. * pipe module function
  651. */
  652. int usbhs_pipe_probe(struct usbhs_priv *priv)
  653. {
  654. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  655. struct usbhs_pipe *pipe;
  656. struct device *dev = usbhs_priv_to_dev(priv);
  657. struct renesas_usbhs_driver_pipe_config *pipe_configs =
  658. usbhs_get_dparam(priv, pipe_configs);
  659. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  660. int i;
  661. /* This driver expects 1st pipe is DCP */
  662. if (pipe_configs[0].type != USB_ENDPOINT_XFER_CONTROL) {
  663. dev_err(dev, "1st PIPE is not DCP\n");
  664. return -EINVAL;
  665. }
  666. info->pipe = kcalloc(pipe_size, sizeof(struct usbhs_pipe),
  667. GFP_KERNEL);
  668. if (!info->pipe)
  669. return -ENOMEM;
  670. info->size = pipe_size;
  671. /*
  672. * init pipe
  673. */
  674. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  675. pipe->priv = priv;
  676. usbhs_pipe_type(pipe) =
  677. pipe_configs[i].type & USB_ENDPOINT_XFERTYPE_MASK;
  678. dev_dbg(dev, "pipe %x\t: %s\n",
  679. i, usbhsp_pipe_name[pipe_configs[i].type]);
  680. }
  681. return 0;
  682. }
  683. void usbhs_pipe_remove(struct usbhs_priv *priv)
  684. {
  685. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  686. kfree(info->pipe);
  687. }