ucc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * arch/powerpc/sysdev/qe_lib/ucc.c
  3. *
  4. * QE UCC API Set - UCC specific routines implementations.
  5. *
  6. * Copyright (C) 2006 Freescale Semiconductor, Inc. All rights reserved.
  7. *
  8. * Authors: Shlomi Gridish <gridish@freescale.com>
  9. * Li Yang <leoli@freescale.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/stddef.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/export.h>
  21. #include <asm/irq.h>
  22. #include <asm/io.h>
  23. #include <soc/fsl/qe/immap_qe.h>
  24. #include <soc/fsl/qe/qe.h>
  25. #include <soc/fsl/qe/ucc.h>
  26. #define UCC_TDM_NUM 8
  27. #define RX_SYNC_SHIFT_BASE 30
  28. #define TX_SYNC_SHIFT_BASE 14
  29. #define RX_CLK_SHIFT_BASE 28
  30. #define TX_CLK_SHIFT_BASE 12
  31. int ucc_set_qe_mux_mii_mng(unsigned int ucc_num)
  32. {
  33. unsigned long flags;
  34. if (ucc_num > UCC_MAX_NUM - 1)
  35. return -EINVAL;
  36. spin_lock_irqsave(&cmxgcr_lock, flags);
  37. clrsetbits_be32(&qe_immr->qmx.cmxgcr, QE_CMXGCR_MII_ENET_MNG,
  38. ucc_num << QE_CMXGCR_MII_ENET_MNG_SHIFT);
  39. spin_unlock_irqrestore(&cmxgcr_lock, flags);
  40. return 0;
  41. }
  42. EXPORT_SYMBOL(ucc_set_qe_mux_mii_mng);
  43. /* Configure the UCC to either Slow or Fast.
  44. *
  45. * A given UCC can be figured to support either "slow" devices (e.g. UART)
  46. * or "fast" devices (e.g. Ethernet).
  47. *
  48. * 'ucc_num' is the UCC number, from 0 - 7.
  49. *
  50. * This function also sets the UCC_GUEMR_SET_RESERVED3 bit because that bit
  51. * must always be set to 1.
  52. */
  53. int ucc_set_type(unsigned int ucc_num, enum ucc_speed_type speed)
  54. {
  55. u8 __iomem *guemr;
  56. /* The GUEMR register is at the same location for both slow and fast
  57. devices, so we just use uccX.slow.guemr. */
  58. switch (ucc_num) {
  59. case 0: guemr = &qe_immr->ucc1.slow.guemr;
  60. break;
  61. case 1: guemr = &qe_immr->ucc2.slow.guemr;
  62. break;
  63. case 2: guemr = &qe_immr->ucc3.slow.guemr;
  64. break;
  65. case 3: guemr = &qe_immr->ucc4.slow.guemr;
  66. break;
  67. case 4: guemr = &qe_immr->ucc5.slow.guemr;
  68. break;
  69. case 5: guemr = &qe_immr->ucc6.slow.guemr;
  70. break;
  71. case 6: guemr = &qe_immr->ucc7.slow.guemr;
  72. break;
  73. case 7: guemr = &qe_immr->ucc8.slow.guemr;
  74. break;
  75. default:
  76. return -EINVAL;
  77. }
  78. clrsetbits_8(guemr, UCC_GUEMR_MODE_MASK,
  79. UCC_GUEMR_SET_RESERVED3 | speed);
  80. return 0;
  81. }
  82. static void get_cmxucr_reg(unsigned int ucc_num, __be32 __iomem **cmxucr,
  83. unsigned int *reg_num, unsigned int *shift)
  84. {
  85. unsigned int cmx = ((ucc_num & 1) << 1) + (ucc_num > 3);
  86. *reg_num = cmx + 1;
  87. *cmxucr = &qe_immr->qmx.cmxucr[cmx];
  88. *shift = 16 - 8 * (ucc_num & 2);
  89. }
  90. int ucc_mux_set_grant_tsa_bkpt(unsigned int ucc_num, int set, u32 mask)
  91. {
  92. __be32 __iomem *cmxucr;
  93. unsigned int reg_num;
  94. unsigned int shift;
  95. /* check if the UCC number is in range. */
  96. if (ucc_num > UCC_MAX_NUM - 1)
  97. return -EINVAL;
  98. get_cmxucr_reg(ucc_num, &cmxucr, &reg_num, &shift);
  99. if (set)
  100. setbits32(cmxucr, mask << shift);
  101. else
  102. clrbits32(cmxucr, mask << shift);
  103. return 0;
  104. }
  105. int ucc_set_qe_mux_rxtx(unsigned int ucc_num, enum qe_clock clock,
  106. enum comm_dir mode)
  107. {
  108. __be32 __iomem *cmxucr;
  109. unsigned int reg_num;
  110. unsigned int shift;
  111. u32 clock_bits = 0;
  112. /* check if the UCC number is in range. */
  113. if (ucc_num > UCC_MAX_NUM - 1)
  114. return -EINVAL;
  115. /* The communications direction must be RX or TX */
  116. if (!((mode == COMM_DIR_RX) || (mode == COMM_DIR_TX)))
  117. return -EINVAL;
  118. get_cmxucr_reg(ucc_num, &cmxucr, &reg_num, &shift);
  119. switch (reg_num) {
  120. case 1:
  121. switch (clock) {
  122. case QE_BRG1: clock_bits = 1; break;
  123. case QE_BRG2: clock_bits = 2; break;
  124. case QE_BRG7: clock_bits = 3; break;
  125. case QE_BRG8: clock_bits = 4; break;
  126. case QE_CLK9: clock_bits = 5; break;
  127. case QE_CLK10: clock_bits = 6; break;
  128. case QE_CLK11: clock_bits = 7; break;
  129. case QE_CLK12: clock_bits = 8; break;
  130. case QE_CLK15: clock_bits = 9; break;
  131. case QE_CLK16: clock_bits = 10; break;
  132. default: break;
  133. }
  134. break;
  135. case 2:
  136. switch (clock) {
  137. case QE_BRG5: clock_bits = 1; break;
  138. case QE_BRG6: clock_bits = 2; break;
  139. case QE_BRG7: clock_bits = 3; break;
  140. case QE_BRG8: clock_bits = 4; break;
  141. case QE_CLK13: clock_bits = 5; break;
  142. case QE_CLK14: clock_bits = 6; break;
  143. case QE_CLK19: clock_bits = 7; break;
  144. case QE_CLK20: clock_bits = 8; break;
  145. case QE_CLK15: clock_bits = 9; break;
  146. case QE_CLK16: clock_bits = 10; break;
  147. default: break;
  148. }
  149. break;
  150. case 3:
  151. switch (clock) {
  152. case QE_BRG9: clock_bits = 1; break;
  153. case QE_BRG10: clock_bits = 2; break;
  154. case QE_BRG15: clock_bits = 3; break;
  155. case QE_BRG16: clock_bits = 4; break;
  156. case QE_CLK3: clock_bits = 5; break;
  157. case QE_CLK4: clock_bits = 6; break;
  158. case QE_CLK17: clock_bits = 7; break;
  159. case QE_CLK18: clock_bits = 8; break;
  160. case QE_CLK7: clock_bits = 9; break;
  161. case QE_CLK8: clock_bits = 10; break;
  162. case QE_CLK16: clock_bits = 11; break;
  163. default: break;
  164. }
  165. break;
  166. case 4:
  167. switch (clock) {
  168. case QE_BRG13: clock_bits = 1; break;
  169. case QE_BRG14: clock_bits = 2; break;
  170. case QE_BRG15: clock_bits = 3; break;
  171. case QE_BRG16: clock_bits = 4; break;
  172. case QE_CLK5: clock_bits = 5; break;
  173. case QE_CLK6: clock_bits = 6; break;
  174. case QE_CLK21: clock_bits = 7; break;
  175. case QE_CLK22: clock_bits = 8; break;
  176. case QE_CLK7: clock_bits = 9; break;
  177. case QE_CLK8: clock_bits = 10; break;
  178. case QE_CLK16: clock_bits = 11; break;
  179. default: break;
  180. }
  181. break;
  182. default: break;
  183. }
  184. /* Check for invalid combination of clock and UCC number */
  185. if (!clock_bits)
  186. return -ENOENT;
  187. if (mode == COMM_DIR_RX)
  188. shift += 4;
  189. clrsetbits_be32(cmxucr, QE_CMXUCR_TX_CLK_SRC_MASK << shift,
  190. clock_bits << shift);
  191. return 0;
  192. }
  193. static int ucc_get_tdm_common_clk(u32 tdm_num, enum qe_clock clock)
  194. {
  195. int clock_bits = -EINVAL;
  196. /*
  197. * for TDM[0, 1, 2, 3], TX and RX use common
  198. * clock source BRG3,4 and CLK1,2
  199. * for TDM[4, 5, 6, 7], TX and RX use common
  200. * clock source BRG12,13 and CLK23,24
  201. */
  202. switch (tdm_num) {
  203. case 0:
  204. case 1:
  205. case 2:
  206. case 3:
  207. switch (clock) {
  208. case QE_BRG3:
  209. clock_bits = 1;
  210. break;
  211. case QE_BRG4:
  212. clock_bits = 2;
  213. break;
  214. case QE_CLK1:
  215. clock_bits = 4;
  216. break;
  217. case QE_CLK2:
  218. clock_bits = 5;
  219. break;
  220. default:
  221. break;
  222. }
  223. break;
  224. case 4:
  225. case 5:
  226. case 6:
  227. case 7:
  228. switch (clock) {
  229. case QE_BRG12:
  230. clock_bits = 1;
  231. break;
  232. case QE_BRG13:
  233. clock_bits = 2;
  234. break;
  235. case QE_CLK23:
  236. clock_bits = 4;
  237. break;
  238. case QE_CLK24:
  239. clock_bits = 5;
  240. break;
  241. default:
  242. break;
  243. }
  244. break;
  245. default:
  246. break;
  247. }
  248. return clock_bits;
  249. }
  250. static int ucc_get_tdm_rx_clk(u32 tdm_num, enum qe_clock clock)
  251. {
  252. int clock_bits = -EINVAL;
  253. switch (tdm_num) {
  254. case 0:
  255. switch (clock) {
  256. case QE_CLK3:
  257. clock_bits = 6;
  258. break;
  259. case QE_CLK8:
  260. clock_bits = 7;
  261. break;
  262. default:
  263. break;
  264. }
  265. break;
  266. case 1:
  267. switch (clock) {
  268. case QE_CLK5:
  269. clock_bits = 6;
  270. break;
  271. case QE_CLK10:
  272. clock_bits = 7;
  273. break;
  274. default:
  275. break;
  276. }
  277. break;
  278. case 2:
  279. switch (clock) {
  280. case QE_CLK7:
  281. clock_bits = 6;
  282. break;
  283. case QE_CLK12:
  284. clock_bits = 7;
  285. break;
  286. default:
  287. break;
  288. }
  289. break;
  290. case 3:
  291. switch (clock) {
  292. case QE_CLK9:
  293. clock_bits = 6;
  294. break;
  295. case QE_CLK14:
  296. clock_bits = 7;
  297. break;
  298. default:
  299. break;
  300. }
  301. break;
  302. case 4:
  303. switch (clock) {
  304. case QE_CLK11:
  305. clock_bits = 6;
  306. break;
  307. case QE_CLK16:
  308. clock_bits = 7;
  309. break;
  310. default:
  311. break;
  312. }
  313. break;
  314. case 5:
  315. switch (clock) {
  316. case QE_CLK13:
  317. clock_bits = 6;
  318. break;
  319. case QE_CLK18:
  320. clock_bits = 7;
  321. break;
  322. default:
  323. break;
  324. }
  325. break;
  326. case 6:
  327. switch (clock) {
  328. case QE_CLK15:
  329. clock_bits = 6;
  330. break;
  331. case QE_CLK20:
  332. clock_bits = 7;
  333. break;
  334. default:
  335. break;
  336. }
  337. break;
  338. case 7:
  339. switch (clock) {
  340. case QE_CLK17:
  341. clock_bits = 6;
  342. break;
  343. case QE_CLK22:
  344. clock_bits = 7;
  345. break;
  346. default:
  347. break;
  348. }
  349. break;
  350. }
  351. return clock_bits;
  352. }
  353. static int ucc_get_tdm_tx_clk(u32 tdm_num, enum qe_clock clock)
  354. {
  355. int clock_bits = -EINVAL;
  356. switch (tdm_num) {
  357. case 0:
  358. switch (clock) {
  359. case QE_CLK4:
  360. clock_bits = 6;
  361. break;
  362. case QE_CLK9:
  363. clock_bits = 7;
  364. break;
  365. default:
  366. break;
  367. }
  368. break;
  369. case 1:
  370. switch (clock) {
  371. case QE_CLK6:
  372. clock_bits = 6;
  373. break;
  374. case QE_CLK11:
  375. clock_bits = 7;
  376. break;
  377. default:
  378. break;
  379. }
  380. break;
  381. case 2:
  382. switch (clock) {
  383. case QE_CLK8:
  384. clock_bits = 6;
  385. break;
  386. case QE_CLK13:
  387. clock_bits = 7;
  388. break;
  389. default:
  390. break;
  391. }
  392. break;
  393. case 3:
  394. switch (clock) {
  395. case QE_CLK10:
  396. clock_bits = 6;
  397. break;
  398. case QE_CLK15:
  399. clock_bits = 7;
  400. break;
  401. default:
  402. break;
  403. }
  404. break;
  405. case 4:
  406. switch (clock) {
  407. case QE_CLK12:
  408. clock_bits = 6;
  409. break;
  410. case QE_CLK17:
  411. clock_bits = 7;
  412. break;
  413. default:
  414. break;
  415. }
  416. break;
  417. case 5:
  418. switch (clock) {
  419. case QE_CLK14:
  420. clock_bits = 6;
  421. break;
  422. case QE_CLK19:
  423. clock_bits = 7;
  424. break;
  425. default:
  426. break;
  427. }
  428. break;
  429. case 6:
  430. switch (clock) {
  431. case QE_CLK16:
  432. clock_bits = 6;
  433. break;
  434. case QE_CLK21:
  435. clock_bits = 7;
  436. break;
  437. default:
  438. break;
  439. }
  440. break;
  441. case 7:
  442. switch (clock) {
  443. case QE_CLK18:
  444. clock_bits = 6;
  445. break;
  446. case QE_CLK3:
  447. clock_bits = 7;
  448. break;
  449. default:
  450. break;
  451. }
  452. break;
  453. }
  454. return clock_bits;
  455. }
  456. /* tdm_num: TDM A-H port num is 0-7 */
  457. static int ucc_get_tdm_rxtx_clk(enum comm_dir mode, u32 tdm_num,
  458. enum qe_clock clock)
  459. {
  460. int clock_bits;
  461. clock_bits = ucc_get_tdm_common_clk(tdm_num, clock);
  462. if (clock_bits > 0)
  463. return clock_bits;
  464. if (mode == COMM_DIR_RX)
  465. clock_bits = ucc_get_tdm_rx_clk(tdm_num, clock);
  466. if (mode == COMM_DIR_TX)
  467. clock_bits = ucc_get_tdm_tx_clk(tdm_num, clock);
  468. return clock_bits;
  469. }
  470. static u32 ucc_get_tdm_clk_shift(enum comm_dir mode, u32 tdm_num)
  471. {
  472. u32 shift;
  473. shift = (mode == COMM_DIR_RX) ? RX_CLK_SHIFT_BASE : TX_CLK_SHIFT_BASE;
  474. if (tdm_num < 4)
  475. shift -= tdm_num * 4;
  476. else
  477. shift -= (tdm_num - 4) * 4;
  478. return shift;
  479. }
  480. int ucc_set_tdm_rxtx_clk(u32 tdm_num, enum qe_clock clock,
  481. enum comm_dir mode)
  482. {
  483. int clock_bits;
  484. u32 shift;
  485. struct qe_mux __iomem *qe_mux_reg;
  486. __be32 __iomem *cmxs1cr;
  487. qe_mux_reg = &qe_immr->qmx;
  488. if (tdm_num > 7 || tdm_num < 0)
  489. return -EINVAL;
  490. /* The communications direction must be RX or TX */
  491. if (mode != COMM_DIR_RX && mode != COMM_DIR_TX)
  492. return -EINVAL;
  493. clock_bits = ucc_get_tdm_rxtx_clk(mode, tdm_num, clock);
  494. if (clock_bits < 0)
  495. return -EINVAL;
  496. shift = ucc_get_tdm_clk_shift(mode, tdm_num);
  497. cmxs1cr = (tdm_num < 4) ? &qe_mux_reg->cmxsi1cr_l :
  498. &qe_mux_reg->cmxsi1cr_h;
  499. qe_clrsetbits32(cmxs1cr, QE_CMXUCR_TX_CLK_SRC_MASK << shift,
  500. clock_bits << shift);
  501. return 0;
  502. }
  503. static int ucc_get_tdm_sync_source(u32 tdm_num, enum qe_clock clock,
  504. enum comm_dir mode)
  505. {
  506. int source = -EINVAL;
  507. if (mode == COMM_DIR_RX && clock == QE_RSYNC_PIN) {
  508. source = 0;
  509. return source;
  510. }
  511. if (mode == COMM_DIR_TX && clock == QE_TSYNC_PIN) {
  512. source = 0;
  513. return source;
  514. }
  515. switch (tdm_num) {
  516. case 0:
  517. case 1:
  518. switch (clock) {
  519. case QE_BRG9:
  520. source = 1;
  521. break;
  522. case QE_BRG10:
  523. source = 2;
  524. break;
  525. default:
  526. break;
  527. }
  528. break;
  529. case 2:
  530. case 3:
  531. switch (clock) {
  532. case QE_BRG9:
  533. source = 1;
  534. break;
  535. case QE_BRG11:
  536. source = 2;
  537. break;
  538. default:
  539. break;
  540. }
  541. break;
  542. case 4:
  543. case 5:
  544. switch (clock) {
  545. case QE_BRG13:
  546. source = 1;
  547. break;
  548. case QE_BRG14:
  549. source = 2;
  550. break;
  551. default:
  552. break;
  553. }
  554. break;
  555. case 6:
  556. case 7:
  557. switch (clock) {
  558. case QE_BRG13:
  559. source = 1;
  560. break;
  561. case QE_BRG15:
  562. source = 2;
  563. break;
  564. default:
  565. break;
  566. }
  567. break;
  568. }
  569. return source;
  570. }
  571. static u32 ucc_get_tdm_sync_shift(enum comm_dir mode, u32 tdm_num)
  572. {
  573. u32 shift;
  574. shift = (mode == COMM_DIR_RX) ? RX_SYNC_SHIFT_BASE : RX_SYNC_SHIFT_BASE;
  575. shift -= tdm_num * 2;
  576. return shift;
  577. }
  578. int ucc_set_tdm_rxtx_sync(u32 tdm_num, enum qe_clock clock,
  579. enum comm_dir mode)
  580. {
  581. int source;
  582. u32 shift;
  583. struct qe_mux *qe_mux_reg;
  584. qe_mux_reg = &qe_immr->qmx;
  585. if (tdm_num >= UCC_TDM_NUM)
  586. return -EINVAL;
  587. /* The communications direction must be RX or TX */
  588. if (mode != COMM_DIR_RX && mode != COMM_DIR_TX)
  589. return -EINVAL;
  590. source = ucc_get_tdm_sync_source(tdm_num, clock, mode);
  591. if (source < 0)
  592. return -EINVAL;
  593. shift = ucc_get_tdm_sync_shift(mode, tdm_num);
  594. qe_clrsetbits32(&qe_mux_reg->cmxsi1syr,
  595. QE_CMXUCR_TX_CLK_SRC_MASK << shift,
  596. source << shift);
  597. return 0;
  598. }