pipe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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/io.h>
  19. #include <linux/slab.h>
  20. #include "./common.h"
  21. #include "./pipe.h"
  22. /*
  23. * macros
  24. */
  25. #define usbhsp_priv_to_pipeinfo(pr) (&(pr)->pipe_info)
  26. #define usbhsp_pipe_to_priv(p) ((p)->priv)
  27. #define usbhsp_addr_offset(p) ((usbhs_pipe_number(p) - 1) * 2)
  28. #define usbhsp_is_dcp(p) ((p)->priv->pipe_info.pipe == (p))
  29. #define usbhsp_flags_set(p, f) ((p)->flags |= USBHS_PIPE_FLAGS_##f)
  30. #define usbhsp_flags_clr(p, f) ((p)->flags &= ~USBHS_PIPE_FLAGS_##f)
  31. #define usbhsp_flags_has(p, f) ((p)->flags & USBHS_PIPE_FLAGS_##f)
  32. #define usbhsp_flags_init(p) do {(p)->flags = 0; } while (0)
  33. #define usbhsp_type(p) ((p)->pipe_type)
  34. #define usbhsp_type_is(p, t) ((p)->pipe_type == t)
  35. /*
  36. * for debug
  37. */
  38. static char *usbhsp_pipe_name[] = {
  39. [USB_ENDPOINT_XFER_CONTROL] = "DCP",
  40. [USB_ENDPOINT_XFER_BULK] = "BULK",
  41. [USB_ENDPOINT_XFER_INT] = "INT",
  42. [USB_ENDPOINT_XFER_ISOC] = "ISO",
  43. };
  44. /*
  45. * usb request functions
  46. */
  47. void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
  48. {
  49. u16 val;
  50. val = usbhs_read(priv, USBREQ);
  51. req->bRequest = (val >> 8) & 0xFF;
  52. req->bRequestType = (val >> 0) & 0xFF;
  53. req->wValue = usbhs_read(priv, USBVAL);
  54. req->wIndex = usbhs_read(priv, USBINDX);
  55. req->wLength = usbhs_read(priv, USBLENG);
  56. }
  57. void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
  58. {
  59. usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType);
  60. usbhs_write(priv, USBVAL, req->wValue);
  61. usbhs_write(priv, USBINDX, req->wIndex);
  62. usbhs_write(priv, USBLENG, req->wLength);
  63. }
  64. /*
  65. * DCPCTR/PIPEnCTR functions
  66. */
  67. static void usbhsp_pipectrl_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
  68. {
  69. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  70. int offset = usbhsp_addr_offset(pipe);
  71. if (usbhsp_is_dcp(pipe))
  72. usbhs_bset(priv, DCPCTR, mask, val);
  73. else
  74. usbhs_bset(priv, PIPEnCTR + offset, mask, val);
  75. }
  76. static u16 usbhsp_pipectrl_get(struct usbhs_pipe *pipe)
  77. {
  78. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  79. int offset = usbhsp_addr_offset(pipe);
  80. if (usbhsp_is_dcp(pipe))
  81. return usbhs_read(priv, DCPCTR);
  82. else
  83. return usbhs_read(priv, PIPEnCTR + offset);
  84. }
  85. /*
  86. * DCP/PIPE functions
  87. */
  88. static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe,
  89. u16 dcp_reg, u16 pipe_reg,
  90. u16 mask, u16 val)
  91. {
  92. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  93. if (usbhsp_is_dcp(pipe))
  94. usbhs_bset(priv, dcp_reg, mask, val);
  95. else
  96. usbhs_bset(priv, pipe_reg, mask, val);
  97. }
  98. static u16 __usbhsp_pipe_xxx_get(struct usbhs_pipe *pipe,
  99. u16 dcp_reg, u16 pipe_reg)
  100. {
  101. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  102. if (usbhsp_is_dcp(pipe))
  103. return usbhs_read(priv, dcp_reg);
  104. else
  105. return usbhs_read(priv, pipe_reg);
  106. }
  107. /*
  108. * DCPCFG/PIPECFG functions
  109. */
  110. static void usbhsp_pipe_cfg_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
  111. {
  112. __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val);
  113. }
  114. /*
  115. * PIPEBUF
  116. */
  117. static void usbhsp_pipe_buf_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
  118. {
  119. if (usbhsp_is_dcp(pipe))
  120. return;
  121. __usbhsp_pipe_xxx_set(pipe, 0, PIPEBUF, mask, val);
  122. }
  123. /*
  124. * DCPMAXP/PIPEMAXP
  125. */
  126. static void usbhsp_pipe_maxp_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
  127. {
  128. __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val);
  129. }
  130. static u16 usbhsp_pipe_maxp_get(struct usbhs_pipe *pipe)
  131. {
  132. return __usbhsp_pipe_xxx_get(pipe, DCPMAXP, PIPEMAXP);
  133. }
  134. /*
  135. * pipe control functions
  136. */
  137. static void usbhsp_pipe_select(struct usbhs_pipe *pipe)
  138. {
  139. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  140. /*
  141. * On pipe, this is necessary before
  142. * accesses to below registers.
  143. *
  144. * PIPESEL : usbhsp_pipe_select
  145. * PIPECFG : usbhsp_pipe_cfg_xxx
  146. * PIPEBUF : usbhsp_pipe_buf_xxx
  147. * PIPEMAXP : usbhsp_pipe_maxp_xxx
  148. * PIPEPERI
  149. */
  150. /*
  151. * if pipe is dcp, no pipe is selected.
  152. * it is no problem, because dcp have its register
  153. */
  154. usbhs_write(priv, PIPESEL, 0xF & usbhs_pipe_number(pipe));
  155. }
  156. static int usbhsp_pipe_barrier(struct usbhs_pipe *pipe)
  157. {
  158. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  159. struct device *dev = usbhs_priv_to_dev(priv);
  160. int timeout = 1024;
  161. u16 val;
  162. /*
  163. * make sure....
  164. *
  165. * Modify these bits when CSSTS = 0, PID = NAK, and no pipe number is
  166. * specified by the CURPIPE bits.
  167. * When changing the setting of this bit after changing
  168. * the PID bits for the selected pipe from BUF to NAK,
  169. * check that CSSTS = 0 and PBUSY = 0.
  170. */
  171. /*
  172. * CURPIPE bit = 0
  173. *
  174. * see also
  175. * "Operation"
  176. * - "Pipe Control"
  177. * - "Pipe Control Registers Switching Procedure"
  178. */
  179. usbhs_write(priv, CFIFOSEL, 0);
  180. do {
  181. val = usbhsp_pipectrl_get(pipe);
  182. val &= CSSTS | PID_MASK;
  183. if (!val)
  184. return 0;
  185. udelay(10);
  186. } while (timeout--);
  187. /*
  188. * force NAK
  189. */
  190. timeout = 1024;
  191. usbhs_fifo_disable(pipe);
  192. do {
  193. val = usbhsp_pipectrl_get(pipe);
  194. val &= PBUSY;
  195. if (!val)
  196. return 0;
  197. } while (timeout--);
  198. dev_err(dev, "pipe barrier failed\n");
  199. return -EBUSY;
  200. }
  201. static int usbhsp_pipe_is_accessible(struct usbhs_pipe *pipe)
  202. {
  203. u16 val;
  204. val = usbhsp_pipectrl_get(pipe);
  205. if (val & BSTS)
  206. return 0;
  207. return -EBUSY;
  208. }
  209. /*
  210. * PID ctrl
  211. */
  212. static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe)
  213. {
  214. u16 pid = usbhsp_pipectrl_get(pipe);
  215. pid &= PID_MASK;
  216. /*
  217. * see
  218. * "Pipe n Control Register" - "PID"
  219. */
  220. switch (pid) {
  221. case PID_STALL11:
  222. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
  223. /* fall-through */
  224. case PID_STALL10:
  225. usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
  226. }
  227. }
  228. void usbhs_fifo_disable(struct usbhs_pipe *pipe)
  229. {
  230. /* see "Pipe n Control Register" - "PID" */
  231. __usbhsp_pid_try_nak_if_stall(pipe);
  232. usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
  233. }
  234. void usbhs_fifo_enable(struct usbhs_pipe *pipe)
  235. {
  236. /* see "Pipe n Control Register" - "PID" */
  237. __usbhsp_pid_try_nak_if_stall(pipe);
  238. usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF);
  239. }
  240. void usbhs_fifo_stall(struct usbhs_pipe *pipe)
  241. {
  242. u16 pid = usbhsp_pipectrl_get(pipe);
  243. pid &= PID_MASK;
  244. /*
  245. * see
  246. * "Pipe n Control Register" - "PID"
  247. */
  248. switch (pid) {
  249. case PID_NAK:
  250. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
  251. break;
  252. case PID_BUF:
  253. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11);
  254. break;
  255. }
  256. }
  257. /*
  258. * CFIFO ctrl
  259. */
  260. void usbhs_fifo_send_terminator(struct usbhs_pipe *pipe)
  261. {
  262. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  263. usbhs_bset(priv, CFIFOCTR, BVAL, BVAL);
  264. }
  265. static void usbhsp_fifo_clear(struct usbhs_pipe *pipe)
  266. {
  267. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  268. usbhs_write(priv, CFIFOCTR, BCLR);
  269. }
  270. static int usbhsp_fifo_barrier(struct usbhs_priv *priv)
  271. {
  272. int timeout = 1024;
  273. do {
  274. /* The FIFO port is accessible */
  275. if (usbhs_read(priv, CFIFOCTR) & FRDY)
  276. return 0;
  277. udelay(10);
  278. } while (timeout--);
  279. return -EBUSY;
  280. }
  281. static int usbhsp_fifo_rcv_len(struct usbhs_priv *priv)
  282. {
  283. return usbhs_read(priv, CFIFOCTR) & DTLN_MASK;
  284. }
  285. static int usbhsp_fifo_select(struct usbhs_pipe *pipe, int write)
  286. {
  287. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  288. struct device *dev = usbhs_priv_to_dev(priv);
  289. int timeout = 1024;
  290. u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
  291. u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
  292. if (usbhsp_is_dcp(pipe))
  293. base |= (1 == write) << 5; /* ISEL */
  294. /* "base" will be used below */
  295. usbhs_write(priv, CFIFOSEL, base | MBW_32);
  296. /* check ISEL and CURPIPE value */
  297. while (timeout--) {
  298. if (base == (mask & usbhs_read(priv, CFIFOSEL)))
  299. return 0;
  300. udelay(10);
  301. }
  302. dev_err(dev, "fifo select error\n");
  303. return -EIO;
  304. }
  305. int usbhs_fifo_prepare_write(struct usbhs_pipe *pipe)
  306. {
  307. return usbhsp_fifo_select(pipe, 1);
  308. }
  309. int usbhs_fifo_write(struct usbhs_pipe *pipe, u8 *buf, int len)
  310. {
  311. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  312. void __iomem *addr = priv->base + CFIFO;
  313. int maxp = usbhs_pipe_get_maxpacket(pipe);
  314. int total_len;
  315. int i, ret;
  316. ret = usbhsp_pipe_is_accessible(pipe);
  317. if (ret < 0)
  318. return ret;
  319. ret = usbhsp_fifo_select(pipe, 1);
  320. if (ret < 0)
  321. return ret;
  322. ret = usbhsp_fifo_barrier(priv);
  323. if (ret < 0)
  324. return ret;
  325. len = min(len, maxp);
  326. total_len = len;
  327. /*
  328. * FIXME
  329. *
  330. * 32-bit access only
  331. */
  332. if (len >= 4 &&
  333. !((unsigned long)buf & 0x03)) {
  334. iowrite32_rep(addr, buf, len / 4);
  335. len %= 4;
  336. buf += total_len - len;
  337. }
  338. /* the rest operation */
  339. for (i = 0; i < len; i++)
  340. iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
  341. if (total_len < maxp)
  342. usbhs_fifo_send_terminator(pipe);
  343. return total_len;
  344. }
  345. int usbhs_fifo_prepare_read(struct usbhs_pipe *pipe)
  346. {
  347. int ret;
  348. /*
  349. * select pipe and enable it to prepare packet receive
  350. */
  351. ret = usbhsp_fifo_select(pipe, 0);
  352. if (ret < 0)
  353. return ret;
  354. usbhs_fifo_enable(pipe);
  355. return ret;
  356. }
  357. int usbhs_fifo_read(struct usbhs_pipe *pipe, u8 *buf, int len)
  358. {
  359. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  360. void __iomem *addr = priv->base + CFIFO;
  361. int rcv_len;
  362. int i, ret;
  363. int total_len;
  364. u32 data = 0;
  365. ret = usbhsp_fifo_select(pipe, 0);
  366. if (ret < 0)
  367. return ret;
  368. ret = usbhsp_fifo_barrier(priv);
  369. if (ret < 0)
  370. return ret;
  371. rcv_len = usbhsp_fifo_rcv_len(priv);
  372. /*
  373. * Buffer clear if Zero-Length packet
  374. *
  375. * see
  376. * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
  377. */
  378. if (0 == rcv_len) {
  379. usbhsp_fifo_clear(pipe);
  380. return 0;
  381. }
  382. len = min(rcv_len, len);
  383. total_len = len;
  384. /*
  385. * FIXME
  386. *
  387. * 32-bit access only
  388. */
  389. if (len >= 4 &&
  390. !((unsigned long)buf & 0x03)) {
  391. ioread32_rep(addr, buf, len / 4);
  392. len %= 4;
  393. buf += rcv_len - len;
  394. }
  395. /* the rest operation */
  396. for (i = 0; i < len; i++) {
  397. if (!(i & 0x03))
  398. data = ioread32(addr);
  399. buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
  400. }
  401. return total_len;
  402. }
  403. /*
  404. * pipe setup
  405. */
  406. static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe)
  407. {
  408. /*
  409. * only ISO / BULK pipe can use double buffer
  410. */
  411. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) ||
  412. usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
  413. return 1;
  414. return 0;
  415. }
  416. static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
  417. const struct usb_endpoint_descriptor *desc,
  418. int is_host)
  419. {
  420. u16 type = 0;
  421. u16 bfre = 0;
  422. u16 dblb = 0;
  423. u16 cntmd = 0;
  424. u16 dir = 0;
  425. u16 epnum = 0;
  426. u16 shtnak = 0;
  427. u16 type_array[] = {
  428. [USB_ENDPOINT_XFER_BULK] = TYPE_BULK,
  429. [USB_ENDPOINT_XFER_INT] = TYPE_INT,
  430. [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO,
  431. };
  432. int is_double = usbhsp_possible_double_buffer(pipe);
  433. if (usbhsp_is_dcp(pipe))
  434. return -EINVAL;
  435. /*
  436. * PIPECFG
  437. *
  438. * see
  439. * - "Register Descriptions" - "PIPECFG" register
  440. * - "Features" - "Pipe configuration"
  441. * - "Operation" - "Pipe Control"
  442. */
  443. /* TYPE */
  444. type = type_array[usbhsp_type(pipe)];
  445. /* BFRE */
  446. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
  447. usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  448. bfre = 0; /* FIXME */
  449. /* DBLB */
  450. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
  451. usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  452. dblb = (is_double) ? DBLB : 0;
  453. /* CNTMD */
  454. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  455. cntmd = 0; /* FIXME */
  456. /* DIR */
  457. if (usb_endpoint_dir_in(desc))
  458. usbhsp_flags_set(pipe, IS_DIR_IN);
  459. if ((is_host && usb_endpoint_dir_out(desc)) ||
  460. (!is_host && usb_endpoint_dir_in(desc)))
  461. dir |= DIR_OUT;
  462. /* SHTNAK */
  463. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) &&
  464. !dir)
  465. shtnak = SHTNAK;
  466. /* EPNUM */
  467. epnum = 0xF & usb_endpoint_num(desc);
  468. return type |
  469. bfre |
  470. dblb |
  471. cntmd |
  472. dir |
  473. shtnak |
  474. epnum;
  475. }
  476. static u16 usbhsp_setup_pipemaxp(struct usbhs_pipe *pipe,
  477. const struct usb_endpoint_descriptor *desc,
  478. int is_host)
  479. {
  480. /* host should set DEVSEL */
  481. /* reutn MXPS */
  482. return PIPE_MAXP_MASK & le16_to_cpu(desc->wMaxPacketSize);
  483. }
  484. static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe,
  485. const struct usb_endpoint_descriptor *desc,
  486. int is_host)
  487. {
  488. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  489. struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv);
  490. struct device *dev = usbhs_priv_to_dev(priv);
  491. int pipe_num = usbhs_pipe_number(pipe);
  492. int is_double = usbhsp_possible_double_buffer(pipe);
  493. u16 buff_size;
  494. u16 bufnmb;
  495. u16 bufnmb_cnt;
  496. /*
  497. * PIPEBUF
  498. *
  499. * see
  500. * - "Register Descriptions" - "PIPEBUF" register
  501. * - "Features" - "Pipe configuration"
  502. * - "Operation" - "FIFO Buffer Memory"
  503. * - "Operation" - "Pipe Control"
  504. *
  505. * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724)
  506. *
  507. * BUFNMB: PIPE
  508. * 0: pipe0 (DCP 256byte)
  509. * 1: -
  510. * 2: -
  511. * 3: -
  512. * 4: pipe6 (INT 64byte)
  513. * 5: pipe7 (INT 64byte)
  514. * 6: pipe8 (INT 64byte)
  515. * 7: pipe9 (INT 64byte)
  516. * 8 - xx: free (for BULK, ISOC)
  517. */
  518. /*
  519. * FIXME
  520. *
  521. * it doesn't have good buffer allocator
  522. *
  523. * DCP : 256 byte
  524. * BULK: 512 byte
  525. * INT : 64 byte
  526. * ISOC: 512 byte
  527. */
  528. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_CONTROL))
  529. buff_size = 256;
  530. else if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT))
  531. buff_size = 64;
  532. else
  533. buff_size = 512;
  534. /* change buff_size to register value */
  535. bufnmb_cnt = (buff_size / 64) - 1;
  536. /* BUFNMB has been reserved for INT pipe
  537. * see above */
  538. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT)) {
  539. bufnmb = pipe_num - 2;
  540. } else {
  541. bufnmb = info->bufnmb_last;
  542. info->bufnmb_last += bufnmb_cnt + 1;
  543. /*
  544. * double buffer
  545. */
  546. if (is_double)
  547. info->bufnmb_last += bufnmb_cnt + 1;
  548. }
  549. dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n",
  550. pipe_num, buff_size, bufnmb);
  551. return (0x1f & bufnmb_cnt) << 10 |
  552. (0xff & bufnmb) << 0;
  553. }
  554. /*
  555. * pipe control
  556. */
  557. int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe)
  558. {
  559. u16 mask = usbhsp_is_dcp(pipe) ? DCP_MAXP_MASK : PIPE_MAXP_MASK;
  560. usbhsp_pipe_select(pipe);
  561. return (int)(usbhsp_pipe_maxp_get(pipe) & mask);
  562. }
  563. int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe)
  564. {
  565. return usbhsp_flags_has(pipe, IS_DIR_IN);
  566. }
  567. void usbhs_pipe_clear_sequence(struct usbhs_pipe *pipe)
  568. {
  569. usbhsp_pipectrl_set(pipe, SQCLR, SQCLR);
  570. }
  571. static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
  572. {
  573. struct usbhs_pipe *pos, *pipe;
  574. int i;
  575. /*
  576. * find target pipe
  577. */
  578. pipe = NULL;
  579. usbhs_for_each_pipe_with_dcp(pos, priv, i) {
  580. if (!usbhsp_type_is(pos, type))
  581. continue;
  582. if (usbhsp_flags_has(pos, IS_USED))
  583. continue;
  584. pipe = pos;
  585. break;
  586. }
  587. if (!pipe)
  588. return NULL;
  589. /*
  590. * initialize pipe flags
  591. */
  592. usbhsp_flags_init(pipe);
  593. usbhsp_flags_set(pipe, IS_USED);
  594. return pipe;
  595. }
  596. void usbhs_pipe_init(struct usbhs_priv *priv)
  597. {
  598. struct usbhs_pipe_info *info = usbhsp_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 (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT))
  616. info->bufnmb_last++;
  617. usbhsp_flags_init(pipe);
  618. pipe->mod_private = NULL;
  619. usbhsp_fifo_clear(pipe);
  620. }
  621. }
  622. struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
  623. const struct usb_endpoint_descriptor *desc)
  624. {
  625. struct device *dev = usbhs_priv_to_dev(priv);
  626. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  627. struct usbhs_pipe *pipe;
  628. int is_host = usbhs_mod_is_host(priv, mod);
  629. int ret;
  630. u16 pipecfg, pipebuf, pipemaxp;
  631. pipe = usbhsp_get_pipe(priv, usb_endpoint_type(desc));
  632. if (!pipe) {
  633. dev_err(dev, "can't get pipe (%s)\n",
  634. usbhsp_pipe_name[usb_endpoint_type(desc)]);
  635. return NULL;
  636. }
  637. usbhs_fifo_disable(pipe);
  638. /* make sure pipe is not busy */
  639. ret = usbhsp_pipe_barrier(pipe);
  640. if (ret < 0) {
  641. dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe));
  642. return NULL;
  643. }
  644. pipecfg = usbhsp_setup_pipecfg(pipe, desc, is_host);
  645. pipebuf = usbhsp_setup_pipebuff(pipe, desc, is_host);
  646. pipemaxp = usbhsp_setup_pipemaxp(pipe, desc, is_host);
  647. /* buffer clear
  648. * see PIPECFG :: BFRE */
  649. usbhsp_pipectrl_set(pipe, ACLRM, ACLRM);
  650. usbhsp_pipectrl_set(pipe, ACLRM, 0);
  651. usbhsp_pipe_select(pipe);
  652. usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg);
  653. usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf);
  654. usbhsp_pipe_maxp_set(pipe, 0xFFFF, pipemaxp);
  655. usbhs_pipe_clear_sequence(pipe);
  656. dev_dbg(dev, "enable pipe %d : %s (%s)\n",
  657. usbhs_pipe_number(pipe),
  658. usbhsp_pipe_name[usb_endpoint_type(desc)],
  659. usbhs_pipe_is_dir_in(pipe) ? "in" : "out");
  660. return pipe;
  661. }
  662. /*
  663. * dcp control
  664. */
  665. struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv)
  666. {
  667. struct usbhs_pipe *pipe;
  668. pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL);
  669. if (!pipe)
  670. return NULL;
  671. /*
  672. * dcpcfg : default
  673. * dcpmaxp : default
  674. * pipebuf : nothing to do
  675. */
  676. usbhsp_pipe_select(pipe);
  677. usbhs_pipe_clear_sequence(pipe);
  678. return pipe;
  679. }
  680. void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe)
  681. {
  682. WARN_ON(!usbhsp_is_dcp(pipe));
  683. usbhs_fifo_enable(pipe);
  684. usbhsp_pipectrl_set(pipe, CCPL, CCPL);
  685. }
  686. /*
  687. * pipe module function
  688. */
  689. int usbhs_pipe_probe(struct usbhs_priv *priv)
  690. {
  691. struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv);
  692. struct usbhs_pipe *pipe;
  693. struct device *dev = usbhs_priv_to_dev(priv);
  694. u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
  695. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  696. int i;
  697. /* This driver expects 1st pipe is DCP */
  698. if (pipe_type[0] != USB_ENDPOINT_XFER_CONTROL) {
  699. dev_err(dev, "1st PIPE is not DCP\n");
  700. return -EINVAL;
  701. }
  702. info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL);
  703. if (!info->pipe) {
  704. dev_err(dev, "Could not allocate pipe\n");
  705. return -ENOMEM;
  706. }
  707. info->size = pipe_size;
  708. /*
  709. * init pipe
  710. */
  711. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  712. pipe->priv = priv;
  713. usbhsp_type(pipe) = pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK;
  714. dev_dbg(dev, "pipe %x\t: %s\n",
  715. i, usbhsp_pipe_name[pipe_type[i]]);
  716. }
  717. return 0;
  718. }
  719. void usbhs_pipe_remove(struct usbhs_priv *priv)
  720. {
  721. struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv);
  722. kfree(info->pipe);
  723. }