pipe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. int timeout = 1024;
  160. u16 val;
  161. /*
  162. * make sure....
  163. *
  164. * Modify these bits when CSSTS = 0, PID = NAK, and no pipe number is
  165. * specified by the CURPIPE bits.
  166. * When changing the setting of this bit after changing
  167. * the PID bits for the selected pipe from BUF to NAK,
  168. * check that CSSTS = 0 and PBUSY = 0.
  169. */
  170. /*
  171. * CURPIPE bit = 0
  172. *
  173. * see also
  174. * "Operation"
  175. * - "Pipe Control"
  176. * - "Pipe Control Registers Switching Procedure"
  177. */
  178. usbhs_write(priv, CFIFOSEL, 0);
  179. usbhs_fifo_disable(pipe);
  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. return -EBUSY;
  188. }
  189. static int usbhsp_pipe_is_accessible(struct usbhs_pipe *pipe)
  190. {
  191. u16 val;
  192. val = usbhsp_pipectrl_get(pipe);
  193. if (val & BSTS)
  194. return 0;
  195. return -EBUSY;
  196. }
  197. /*
  198. * PID ctrl
  199. */
  200. static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe)
  201. {
  202. u16 pid = usbhsp_pipectrl_get(pipe);
  203. pid &= PID_MASK;
  204. /*
  205. * see
  206. * "Pipe n Control Register" - "PID"
  207. */
  208. switch (pid) {
  209. case PID_STALL11:
  210. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
  211. /* fall-through */
  212. case PID_STALL10:
  213. usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
  214. }
  215. }
  216. void usbhs_fifo_disable(struct usbhs_pipe *pipe)
  217. {
  218. int timeout = 1024;
  219. u16 val;
  220. /* see "Pipe n Control Register" - "PID" */
  221. __usbhsp_pid_try_nak_if_stall(pipe);
  222. usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
  223. do {
  224. val = usbhsp_pipectrl_get(pipe);
  225. val &= PBUSY;
  226. if (!val)
  227. break;
  228. udelay(10);
  229. } while (timeout--);
  230. }
  231. void usbhs_fifo_enable(struct usbhs_pipe *pipe)
  232. {
  233. /* see "Pipe n Control Register" - "PID" */
  234. __usbhsp_pid_try_nak_if_stall(pipe);
  235. usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF);
  236. }
  237. void usbhs_fifo_stall(struct usbhs_pipe *pipe)
  238. {
  239. u16 pid = usbhsp_pipectrl_get(pipe);
  240. pid &= PID_MASK;
  241. /*
  242. * see
  243. * "Pipe n Control Register" - "PID"
  244. */
  245. switch (pid) {
  246. case PID_NAK:
  247. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
  248. break;
  249. case PID_BUF:
  250. usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11);
  251. break;
  252. }
  253. }
  254. /*
  255. * CFIFO ctrl
  256. */
  257. void usbhs_fifo_send_terminator(struct usbhs_pipe *pipe)
  258. {
  259. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  260. usbhs_bset(priv, CFIFOCTR, BVAL, BVAL);
  261. }
  262. static void usbhsp_fifo_clear(struct usbhs_pipe *pipe)
  263. {
  264. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  265. usbhs_write(priv, CFIFOCTR, BCLR);
  266. }
  267. static int usbhsp_fifo_barrier(struct usbhs_priv *priv)
  268. {
  269. int timeout = 1024;
  270. do {
  271. /* The FIFO port is accessible */
  272. if (usbhs_read(priv, CFIFOCTR) & FRDY)
  273. return 0;
  274. udelay(10);
  275. } while (timeout--);
  276. return -EBUSY;
  277. }
  278. static int usbhsp_fifo_rcv_len(struct usbhs_priv *priv)
  279. {
  280. return usbhs_read(priv, CFIFOCTR) & DTLN_MASK;
  281. }
  282. static int usbhsp_fifo_select(struct usbhs_pipe *pipe, int write)
  283. {
  284. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  285. struct device *dev = usbhs_priv_to_dev(priv);
  286. int timeout = 1024;
  287. u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
  288. u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
  289. if (usbhsp_is_dcp(pipe))
  290. base |= (1 == write) << 5; /* ISEL */
  291. /* "base" will be used below */
  292. usbhs_write(priv, CFIFOSEL, base | MBW_32);
  293. /* check ISEL and CURPIPE value */
  294. while (timeout--) {
  295. if (base == (mask & usbhs_read(priv, CFIFOSEL)))
  296. return 0;
  297. udelay(10);
  298. }
  299. dev_err(dev, "fifo select error\n");
  300. return -EIO;
  301. }
  302. int usbhs_fifo_prepare_write(struct usbhs_pipe *pipe)
  303. {
  304. return usbhsp_fifo_select(pipe, 1);
  305. }
  306. int usbhs_fifo_write(struct usbhs_pipe *pipe, u8 *buf, int len)
  307. {
  308. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  309. void __iomem *addr = priv->base + CFIFO;
  310. int maxp = usbhs_pipe_get_maxpacket(pipe);
  311. int total_len;
  312. int i, ret;
  313. ret = usbhsp_pipe_is_accessible(pipe);
  314. if (ret < 0)
  315. return ret;
  316. ret = usbhsp_fifo_select(pipe, 1);
  317. if (ret < 0)
  318. return ret;
  319. ret = usbhsp_fifo_barrier(priv);
  320. if (ret < 0)
  321. return ret;
  322. len = min(len, maxp);
  323. total_len = len;
  324. /*
  325. * FIXME
  326. *
  327. * 32-bit access only
  328. */
  329. if (len >= 4 &&
  330. !((unsigned long)buf & 0x03)) {
  331. iowrite32_rep(addr, buf, len / 4);
  332. len %= 4;
  333. buf += total_len - len;
  334. }
  335. /* the rest operation */
  336. for (i = 0; i < len; i++)
  337. iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
  338. if (total_len < maxp)
  339. usbhs_fifo_send_terminator(pipe);
  340. return total_len;
  341. }
  342. int usbhs_fifo_prepare_read(struct usbhs_pipe *pipe)
  343. {
  344. int ret;
  345. /*
  346. * select pipe and enable it to prepare packet receive
  347. */
  348. ret = usbhsp_fifo_select(pipe, 0);
  349. if (ret < 0)
  350. return ret;
  351. usbhs_fifo_enable(pipe);
  352. return ret;
  353. }
  354. int usbhs_fifo_read(struct usbhs_pipe *pipe, u8 *buf, int len)
  355. {
  356. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  357. void __iomem *addr = priv->base + CFIFO;
  358. int rcv_len;
  359. int i, ret;
  360. int total_len;
  361. u32 data = 0;
  362. ret = usbhsp_fifo_select(pipe, 0);
  363. if (ret < 0)
  364. return ret;
  365. ret = usbhsp_fifo_barrier(priv);
  366. if (ret < 0)
  367. return ret;
  368. rcv_len = usbhsp_fifo_rcv_len(priv);
  369. /*
  370. * Buffer clear if Zero-Length packet
  371. *
  372. * see
  373. * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
  374. */
  375. if (0 == rcv_len) {
  376. usbhsp_fifo_clear(pipe);
  377. return 0;
  378. }
  379. len = min(rcv_len, len);
  380. total_len = len;
  381. /*
  382. * FIXME
  383. *
  384. * 32-bit access only
  385. */
  386. if (len >= 4 &&
  387. !((unsigned long)buf & 0x03)) {
  388. ioread32_rep(addr, buf, len / 4);
  389. len %= 4;
  390. buf += rcv_len - len;
  391. }
  392. /* the rest operation */
  393. for (i = 0; i < len; i++) {
  394. if (!(i & 0x03))
  395. data = ioread32(addr);
  396. buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
  397. }
  398. return total_len;
  399. }
  400. /*
  401. * pipe setup
  402. */
  403. static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe)
  404. {
  405. /*
  406. * only ISO / BULK pipe can use double buffer
  407. */
  408. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) ||
  409. usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
  410. return 1;
  411. return 0;
  412. }
  413. static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
  414. const struct usb_endpoint_descriptor *desc,
  415. int is_host)
  416. {
  417. u16 type = 0;
  418. u16 bfre = 0;
  419. u16 dblb = 0;
  420. u16 cntmd = 0;
  421. u16 dir = 0;
  422. u16 epnum = 0;
  423. u16 shtnak = 0;
  424. u16 type_array[] = {
  425. [USB_ENDPOINT_XFER_BULK] = TYPE_BULK,
  426. [USB_ENDPOINT_XFER_INT] = TYPE_INT,
  427. [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO,
  428. };
  429. int is_double = usbhsp_possible_double_buffer(pipe);
  430. if (usbhsp_is_dcp(pipe))
  431. return -EINVAL;
  432. /*
  433. * PIPECFG
  434. *
  435. * see
  436. * - "Register Descriptions" - "PIPECFG" register
  437. * - "Features" - "Pipe configuration"
  438. * - "Operation" - "Pipe Control"
  439. */
  440. /* TYPE */
  441. type = type_array[usbhsp_type(pipe)];
  442. /* BFRE */
  443. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
  444. usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  445. bfre = 0; /* FIXME */
  446. /* DBLB */
  447. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
  448. usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  449. dblb = (is_double) ? DBLB : 0;
  450. /* CNTMD */
  451. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
  452. cntmd = 0; /* FIXME */
  453. /* DIR */
  454. if (usb_endpoint_dir_in(desc))
  455. usbhsp_flags_set(pipe, IS_DIR_IN);
  456. if ((is_host && usb_endpoint_dir_out(desc)) ||
  457. (!is_host && usb_endpoint_dir_in(desc)))
  458. dir |= DIR_OUT;
  459. /* SHTNAK */
  460. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) &&
  461. !dir)
  462. shtnak = SHTNAK;
  463. /* EPNUM */
  464. epnum = 0xF & usb_endpoint_num(desc);
  465. return type |
  466. bfre |
  467. dblb |
  468. cntmd |
  469. dir |
  470. shtnak |
  471. epnum;
  472. }
  473. static u16 usbhsp_setup_pipemaxp(struct usbhs_pipe *pipe,
  474. const struct usb_endpoint_descriptor *desc,
  475. int is_host)
  476. {
  477. /* host should set DEVSEL */
  478. /* reutn MXPS */
  479. return PIPE_MAXP_MASK & le16_to_cpu(desc->wMaxPacketSize);
  480. }
  481. static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe,
  482. const struct usb_endpoint_descriptor *desc,
  483. int is_host)
  484. {
  485. struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
  486. struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv);
  487. struct device *dev = usbhs_priv_to_dev(priv);
  488. int pipe_num = usbhs_pipe_number(pipe);
  489. int is_double = usbhsp_possible_double_buffer(pipe);
  490. u16 buff_size;
  491. u16 bufnmb;
  492. u16 bufnmb_cnt;
  493. /*
  494. * PIPEBUF
  495. *
  496. * see
  497. * - "Register Descriptions" - "PIPEBUF" register
  498. * - "Features" - "Pipe configuration"
  499. * - "Operation" - "FIFO Buffer Memory"
  500. * - "Operation" - "Pipe Control"
  501. *
  502. * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724)
  503. *
  504. * BUFNMB: PIPE
  505. * 0: pipe0 (DCP 256byte)
  506. * 1: -
  507. * 2: -
  508. * 3: -
  509. * 4: pipe6 (INT 64byte)
  510. * 5: pipe7 (INT 64byte)
  511. * 6: pipe8 (INT 64byte)
  512. * 7: pipe9 (INT 64byte)
  513. * 8 - xx: free (for BULK, ISOC)
  514. */
  515. /*
  516. * FIXME
  517. *
  518. * it doesn't have good buffer allocator
  519. *
  520. * DCP : 256 byte
  521. * BULK: 512 byte
  522. * INT : 64 byte
  523. * ISOC: 512 byte
  524. */
  525. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_CONTROL))
  526. buff_size = 256;
  527. else if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT))
  528. buff_size = 64;
  529. else
  530. buff_size = 512;
  531. /* change buff_size to register value */
  532. bufnmb_cnt = (buff_size / 64) - 1;
  533. /* BUFNMB has been reserved for INT pipe
  534. * see above */
  535. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT)) {
  536. bufnmb = pipe_num - 2;
  537. } else {
  538. bufnmb = info->bufnmb_last;
  539. info->bufnmb_last += bufnmb_cnt + 1;
  540. /*
  541. * double buffer
  542. */
  543. if (is_double)
  544. info->bufnmb_last += bufnmb_cnt + 1;
  545. }
  546. dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n",
  547. pipe_num, buff_size, bufnmb);
  548. return (0x1f & bufnmb_cnt) << 10 |
  549. (0xff & bufnmb) << 0;
  550. }
  551. /*
  552. * pipe control
  553. */
  554. int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe)
  555. {
  556. u16 mask = usbhsp_is_dcp(pipe) ? DCP_MAXP_MASK : PIPE_MAXP_MASK;
  557. usbhsp_pipe_select(pipe);
  558. return (int)(usbhsp_pipe_maxp_get(pipe) & mask);
  559. }
  560. int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe)
  561. {
  562. return usbhsp_flags_has(pipe, IS_DIR_IN);
  563. }
  564. void usbhs_pipe_clear_sequence(struct usbhs_pipe *pipe)
  565. {
  566. usbhsp_pipectrl_set(pipe, SQCLR, SQCLR);
  567. }
  568. static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
  569. {
  570. struct usbhs_pipe *pos, *pipe;
  571. int i;
  572. /*
  573. * find target pipe
  574. */
  575. pipe = NULL;
  576. usbhs_for_each_pipe_with_dcp(pos, priv, i) {
  577. if (!usbhsp_type_is(pos, type))
  578. continue;
  579. if (usbhsp_flags_has(pos, IS_USED))
  580. continue;
  581. pipe = pos;
  582. break;
  583. }
  584. if (!pipe)
  585. return NULL;
  586. /*
  587. * initialize pipe flags
  588. */
  589. usbhsp_flags_init(pipe);
  590. usbhsp_flags_set(pipe, IS_USED);
  591. return pipe;
  592. }
  593. void usbhs_pipe_init(struct usbhs_priv *priv)
  594. {
  595. struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv);
  596. struct usbhs_pipe *pipe;
  597. int i;
  598. /*
  599. * FIXME
  600. *
  601. * driver needs good allocator.
  602. *
  603. * find first free buffer area (BULK, ISOC)
  604. * (DCP, INT area is fixed)
  605. *
  606. * buffer number 0 - 3 have been reserved for DCP
  607. * see
  608. * usbhsp_to_bufnmb
  609. */
  610. info->bufnmb_last = 4;
  611. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  612. if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT))
  613. info->bufnmb_last++;
  614. usbhsp_flags_init(pipe);
  615. pipe->mod_private = NULL;
  616. usbhsp_fifo_clear(pipe);
  617. }
  618. }
  619. struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
  620. const struct usb_endpoint_descriptor *desc)
  621. {
  622. struct device *dev = usbhs_priv_to_dev(priv);
  623. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  624. struct usbhs_pipe *pipe;
  625. int is_host = usbhs_mod_is_host(priv, mod);
  626. int ret;
  627. u16 pipecfg, pipebuf, pipemaxp;
  628. pipe = usbhsp_get_pipe(priv, usb_endpoint_type(desc));
  629. if (!pipe) {
  630. dev_err(dev, "can't get pipe (%s)\n",
  631. usbhsp_pipe_name[usb_endpoint_type(desc)]);
  632. return NULL;
  633. }
  634. usbhs_fifo_disable(pipe);
  635. /* make sure pipe is not busy */
  636. ret = usbhsp_pipe_barrier(pipe);
  637. if (ret < 0) {
  638. dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe));
  639. return NULL;
  640. }
  641. pipecfg = usbhsp_setup_pipecfg(pipe, desc, is_host);
  642. pipebuf = usbhsp_setup_pipebuff(pipe, desc, is_host);
  643. pipemaxp = usbhsp_setup_pipemaxp(pipe, desc, is_host);
  644. /* buffer clear
  645. * see PIPECFG :: BFRE */
  646. usbhsp_pipectrl_set(pipe, ACLRM, ACLRM);
  647. usbhsp_pipectrl_set(pipe, ACLRM, 0);
  648. usbhsp_pipe_select(pipe);
  649. usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg);
  650. usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf);
  651. usbhsp_pipe_maxp_set(pipe, 0xFFFF, pipemaxp);
  652. usbhs_pipe_clear_sequence(pipe);
  653. dev_dbg(dev, "enable pipe %d : %s (%s)\n",
  654. usbhs_pipe_number(pipe),
  655. usbhsp_pipe_name[usb_endpoint_type(desc)],
  656. usbhs_pipe_is_dir_in(pipe) ? "in" : "out");
  657. return pipe;
  658. }
  659. /*
  660. * dcp control
  661. */
  662. struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv)
  663. {
  664. struct usbhs_pipe *pipe;
  665. pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL);
  666. if (!pipe)
  667. return NULL;
  668. /*
  669. * dcpcfg : default
  670. * dcpmaxp : default
  671. * pipebuf : nothing to do
  672. */
  673. usbhsp_pipe_select(pipe);
  674. usbhs_pipe_clear_sequence(pipe);
  675. return pipe;
  676. }
  677. void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe)
  678. {
  679. WARN_ON(!usbhsp_is_dcp(pipe));
  680. usbhs_fifo_enable(pipe);
  681. usbhsp_pipectrl_set(pipe, CCPL, CCPL);
  682. }
  683. /*
  684. * pipe module function
  685. */
  686. int usbhs_pipe_probe(struct usbhs_priv *priv)
  687. {
  688. struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv);
  689. struct usbhs_pipe *pipe;
  690. struct device *dev = usbhs_priv_to_dev(priv);
  691. u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
  692. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  693. int i;
  694. /* This driver expects 1st pipe is DCP */
  695. if (pipe_type[0] != USB_ENDPOINT_XFER_CONTROL) {
  696. dev_err(dev, "1st PIPE is not DCP\n");
  697. return -EINVAL;
  698. }
  699. info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL);
  700. if (!info->pipe) {
  701. dev_err(dev, "Could not allocate pipe\n");
  702. return -ENOMEM;
  703. }
  704. info->size = pipe_size;
  705. /*
  706. * init pipe
  707. */
  708. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  709. pipe->priv = priv;
  710. usbhsp_type(pipe) = pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK;
  711. dev_dbg(dev, "pipe %x\t: %s\n",
  712. i, usbhsp_pipe_name[pipe_type[i]]);
  713. }
  714. return 0;
  715. }
  716. void usbhs_pipe_remove(struct usbhs_priv *priv)
  717. {
  718. struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv);
  719. kfree(info->pipe);
  720. }