qe.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2006 Freescale Semiconductor, Inc.
  3. *
  4. * Dave Liu <daveliu@freescale.com>
  5. * based on source code of Shlomi Gridish
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include "common.h"
  23. #include "asm/errno.h"
  24. #include "asm/io.h"
  25. #include "asm/immap_qe.h"
  26. #include "qe.h"
  27. #if defined(CONFIG_QE)
  28. qe_map_t *qe_immr = NULL;
  29. static qe_snum_t snums[QE_NUM_OF_SNUM];
  30. DECLARE_GLOBAL_DATA_PTR;
  31. void qe_issue_cmd(uint cmd, uint sbc, u8 mcn, u32 cmd_data)
  32. {
  33. u32 cecr;
  34. if (cmd == QE_RESET) {
  35. out_be32(&qe_immr->cp.cecr,(u32) (cmd | QE_CR_FLG));
  36. } else {
  37. out_be32(&qe_immr->cp.cecdr, cmd_data);
  38. out_be32(&qe_immr->cp.cecr, (sbc | QE_CR_FLG |
  39. ((u32) mcn<<QE_CR_PROTOCOL_SHIFT) | cmd));
  40. }
  41. /* Wait for the QE_CR_FLG to clear */
  42. do {
  43. cecr = in_be32(&qe_immr->cp.cecr);
  44. } while (cecr & QE_CR_FLG);
  45. return;
  46. }
  47. uint qe_muram_alloc(uint size, uint align)
  48. {
  49. uint retloc;
  50. uint align_mask, off;
  51. uint savebase;
  52. align_mask = align - 1;
  53. savebase = gd->mp_alloc_base;
  54. if ((off = (gd->mp_alloc_base & align_mask)) != 0)
  55. gd->mp_alloc_base += (align - off);
  56. if ((off = size & align_mask) != 0)
  57. size += (align - off);
  58. if ((gd->mp_alloc_base + size) >= gd->mp_alloc_top) {
  59. gd->mp_alloc_base = savebase;
  60. printf("%s: ran out of ram.\n", __FUNCTION__);
  61. }
  62. retloc = gd->mp_alloc_base;
  63. gd->mp_alloc_base += size;
  64. memset((void *)&qe_immr->muram[retloc], 0, size);
  65. __asm__ __volatile__("sync");
  66. return retloc;
  67. }
  68. void *qe_muram_addr(uint offset)
  69. {
  70. return (void *)&qe_immr->muram[offset];
  71. }
  72. static void qe_sdma_init(void)
  73. {
  74. volatile sdma_t *p;
  75. uint sdma_buffer_base;
  76. p = (volatile sdma_t *)&qe_immr->sdma;
  77. /* All of DMA transaction in bus 1 */
  78. out_be32(&p->sdaqr, 0);
  79. out_be32(&p->sdaqmr, 0);
  80. /* Allocate 2KB temporary buffer for sdma */
  81. sdma_buffer_base = qe_muram_alloc(2048, 4096);
  82. out_be32(&p->sdwbcr, sdma_buffer_base & QE_SDEBCR_BA_MASK);
  83. /* Clear sdma status */
  84. out_be32(&p->sdsr, 0x03000000);
  85. /* Enable global mode on bus 1, and 2KB buffer size */
  86. out_be32(&p->sdmr, QE_SDMR_GLB_1_MSK | (0x3 << QE_SDMR_CEN_SHIFT));
  87. }
  88. static u8 thread_snum[QE_NUM_OF_SNUM] = {
  89. 0x04, 0x05, 0x0c, 0x0d,
  90. 0x14, 0x15, 0x1c, 0x1d,
  91. 0x24, 0x25, 0x2c, 0x2d,
  92. 0x34, 0x35, 0x88, 0x89,
  93. 0x98, 0x99, 0xa8, 0xa9,
  94. 0xb8, 0xb9, 0xc8, 0xc9,
  95. 0xd8, 0xd9, 0xe8, 0xe9
  96. };
  97. static void qe_snums_init(void)
  98. {
  99. int i;
  100. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  101. snums[i].state = QE_SNUM_STATE_FREE;
  102. snums[i].num = thread_snum[i];
  103. }
  104. }
  105. int qe_get_snum(void)
  106. {
  107. int snum = -EBUSY;
  108. int i;
  109. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  110. if (snums[i].state == QE_SNUM_STATE_FREE) {
  111. snums[i].state = QE_SNUM_STATE_USED;
  112. snum = snums[i].num;
  113. break;
  114. }
  115. }
  116. return snum;
  117. }
  118. void qe_put_snum(u8 snum)
  119. {
  120. int i;
  121. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  122. if (snums[i].num == snum) {
  123. snums[i].state = QE_SNUM_STATE_FREE;
  124. break;
  125. }
  126. }
  127. }
  128. void qe_init(uint qe_base)
  129. {
  130. /* Init the QE IMMR base */
  131. qe_immr = (qe_map_t *)qe_base;
  132. gd->mp_alloc_base = QE_DATAONLY_BASE;
  133. gd->mp_alloc_top = gd->mp_alloc_base + QE_DATAONLY_SIZE;
  134. qe_sdma_init();
  135. qe_snums_init();
  136. }
  137. void qe_reset(void)
  138. {
  139. qe_issue_cmd(QE_RESET, QE_CR_SUBBLOCK_INVALID,
  140. (u8) QE_CR_PROTOCOL_UNSPECIFIED, 0);
  141. }
  142. void qe_assign_page(uint snum, uint para_ram_base)
  143. {
  144. u32 cecr;
  145. out_be32(&qe_immr->cp.cecdr, para_ram_base);
  146. out_be32(&qe_immr->cp.cecr, ((u32) snum<<QE_CR_ASSIGN_PAGE_SNUM_SHIFT)
  147. | QE_CR_FLG | QE_ASSIGN_PAGE);
  148. /* Wait for the QE_CR_FLG to clear */
  149. do {
  150. cecr = in_be32(&qe_immr->cp.cecr);
  151. } while (cecr & QE_CR_FLG );
  152. return;
  153. }
  154. /*
  155. * brg: 0~15 as BRG1~BRG16
  156. rate: baud rate
  157. * BRG input clock comes from the BRGCLK (internal clock generated from
  158. the QE clock, it is one-half of the QE clock), If need the clock source
  159. from CLKn pin, we have te change the function.
  160. */
  161. #define BRG_CLK (gd->brg_clk)
  162. int qe_set_brg(uint brg, uint rate)
  163. {
  164. volatile uint *bp;
  165. u32 divisor;
  166. int div16 = 0;
  167. if (brg >= QE_NUM_OF_BRGS)
  168. return -EINVAL;
  169. bp = (uint *)&qe_immr->brg.brgc1;
  170. bp += brg;
  171. divisor = (BRG_CLK / rate);
  172. if (divisor > QE_BRGC_DIVISOR_MAX + 1) {
  173. div16 = 1;
  174. divisor /= 16;
  175. }
  176. *bp = ((divisor - 1) << QE_BRGC_DIVISOR_SHIFT) | QE_BRGC_ENABLE;
  177. __asm__ __volatile__("sync");
  178. if (div16) {
  179. *bp |= QE_BRGC_DIV16;
  180. __asm__ __volatile__("sync");
  181. }
  182. return 0;
  183. }
  184. /* Set ethernet MII clock master
  185. */
  186. int qe_set_mii_clk_src(int ucc_num)
  187. {
  188. u32 cmxgcr;
  189. /* check if the UCC number is in range. */
  190. if ((ucc_num > UCC_MAX_NUM - 1) || (ucc_num < 0)) {
  191. printf("%s: ucc num not in ranges\n", __FUNCTION__);
  192. return -EINVAL;
  193. }
  194. cmxgcr = in_be32(&qe_immr->qmx.cmxgcr);
  195. cmxgcr &= ~QE_CMXGCR_MII_ENET_MNG_MASK;
  196. cmxgcr |= (ucc_num <<QE_CMXGCR_MII_ENET_MNG_SHIFT);
  197. out_be32(&qe_immr->qmx.cmxgcr, cmxgcr);
  198. return 0;
  199. }
  200. #endif /* CONFIG_QE */