pipe.c 17 KB

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