dm365.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * File : dm365.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-11-13 weety first version
  23. */
  24. #include <edma.h>
  25. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  26. static rt_uint32_t commonrate;
  27. static rt_uint32_t div_by_four;
  28. static rt_uint32_t div_by_six;
  29. static rt_uint32_t armrate;
  30. static rt_uint32_t fixedrate;
  31. static rt_uint32_t ddrrate;
  32. static rt_uint32_t voicerate;
  33. static rt_uint32_t mmcsdrate;
  34. static rt_uint32_t vpssrate, vencrate_sd, vencrate_hd;
  35. /* Four Transfer Controllers on DM365 */
  36. static const rt_int8_t
  37. dm365_queue_tc_mapping[][2] = {
  38. /* {event queue no, TC no} */
  39. {0, 0},
  40. {1, 1},
  41. {2, 2},
  42. {3, 3},
  43. {-1, -1},
  44. };
  45. static const rt_int8_t
  46. dm365_queue_priority_mapping[][2] = {
  47. /* {event queue no, Priority} */
  48. {0, 7},
  49. {1, 7},
  50. {2, 7},
  51. {3, 0},
  52. {-1, -1},
  53. };
  54. static struct edma_soc_info edma_cc0_info = {
  55. .n_channel = 64,
  56. .n_region = 4,
  57. .n_slot = 256,
  58. .n_tc = 4,
  59. .n_cc = 1,
  60. .queue_tc_mapping = dm365_queue_tc_mapping,
  61. .queue_priority_mapping = dm365_queue_priority_mapping,
  62. .default_queue = EVENTQ_3,
  63. };
  64. static struct edma_soc_info *dm365_edma_info[EDMA_MAX_CC] = {
  65. &edma_cc0_info,
  66. };
  67. static rt_list_t clocks;
  68. struct clk {
  69. char name[32];
  70. rt_uint32_t *rate_hz;
  71. struct clk *parent;
  72. rt_list_t node;
  73. };
  74. static struct clk davinci_dm365_clks[] = {
  75. {
  76. .name = "ARMCLK",
  77. .rate_hz = &armrate,
  78. },
  79. {
  80. .name = "UART0",
  81. .rate_hz = &fixedrate,
  82. },
  83. {
  84. .name = "UART1",
  85. .rate_hz = &commonrate,
  86. },
  87. {
  88. .name = "HPI",
  89. .rate_hz = &commonrate,
  90. },
  91. {
  92. .name = "EMACCLK",
  93. .rate_hz = &commonrate,
  94. },
  95. {
  96. .name = "I2CCLK",
  97. .rate_hz = &fixedrate,
  98. },
  99. {
  100. .name = "McBSPCLK",
  101. .rate_hz = &commonrate,
  102. },
  103. {
  104. .name = "MMCSDCLK0",
  105. .rate_hz = &mmcsdrate,
  106. },
  107. {
  108. .name = "MMCSDCLK1",
  109. .rate_hz = &mmcsdrate,
  110. },
  111. {
  112. .name = "SPICLK",
  113. .rate_hz = &commonrate,
  114. },
  115. {
  116. .name = "gpio",
  117. .rate_hz = &commonrate,
  118. },
  119. {
  120. .name = "AEMIFCLK",
  121. .rate_hz = &commonrate,
  122. },
  123. {
  124. .name = "PWM0_CLK",
  125. .rate_hz = &fixedrate,
  126. },
  127. {
  128. .name = "PWM1_CLK",
  129. .rate_hz = &fixedrate,
  130. },
  131. {
  132. .name = "PWM2_CLK",
  133. .rate_hz = &fixedrate,
  134. },
  135. {
  136. .name = "PWM3_CLK",
  137. .rate_hz = &fixedrate,
  138. },
  139. {
  140. .name = "USBCLK",
  141. .rate_hz = &fixedrate,
  142. },
  143. {
  144. .name = "VOICECODEC_CLK",
  145. .rate_hz = &voicerate,
  146. },
  147. {
  148. .name = "RTC_CLK",
  149. .rate_hz = &fixedrate,
  150. },
  151. {
  152. .name = "KEYSCAN_CLK",
  153. .rate_hz = &fixedrate,
  154. },
  155. {
  156. .name = "ADCIF_CLK",
  157. .rate_hz = &fixedrate,
  158. },
  159. };
  160. /* clocks cannot be de-registered no refcounting necessary */
  161. struct clk *clk_get(const char *id)
  162. {
  163. struct clk *clk;
  164. rt_list_t *list;
  165. for (list = (&clocks)->next; list != &clocks; list = list->next)
  166. {
  167. clk = (struct clk *)rt_list_entry(list, struct clk, node);
  168. if (rt_strcmp(id, clk->name) == 0)
  169. return clk;
  170. }
  171. return RT_NULL;
  172. }
  173. rt_uint32_t clk_get_rate(struct clk *clk)
  174. {
  175. rt_uint32_t flags;
  176. rt_uint32_t *rate;
  177. for (;;) {
  178. rate = clk->rate_hz;
  179. if (rate || !clk->parent)
  180. break;
  181. clk = clk->parent;
  182. }
  183. return *rate;
  184. }
  185. void clk_register(struct clk *clk)
  186. {
  187. rt_list_insert_after(&clocks, &clk->node);
  188. }
  189. int davinci_register_clks(struct clk *clk_list, int num_clks)
  190. {
  191. struct clk *clkp;
  192. int i;
  193. for (i = 0, clkp = clk_list; i < num_clks; i++, clkp++)
  194. {
  195. //rt_kprintf("1:%s\n", clkp->name);
  196. clk_register(clkp);
  197. //rt_kprintf("2:%s\n", clkp->name);
  198. }
  199. return 0;
  200. }
  201. /* PLL/Reset register offsets */
  202. #define PLLM 0x110
  203. #define PREDIV 0x114
  204. #define PLLDIV2 0x11C
  205. #define POSTDIV 0x128
  206. #define PLLDIV4 0x160
  207. #define PLLDIV5 0x164
  208. #define PLLDIV6 0x168
  209. #define PLLDIV7 0x16C
  210. #define PLLDIV8 0x170
  211. int davinci_clk_init(void)
  212. {
  213. struct clk *clk_list;
  214. int num_clks;
  215. rt_uint32_t pll0_mult, pll1_mult;
  216. unsigned long prediv, postdiv;
  217. unsigned long pll_rate;
  218. unsigned long pll_div2, pll_div4, pll_div5,
  219. pll_div6, pll_div7, pll_div8;
  220. rt_list_init(&clocks);
  221. //davinci_psc_register(davinci_psc_base, 1);
  222. pll0_mult = davinci_readl(DAVINCI_PLL_CNTRL0_BASE + PLLM);
  223. pll1_mult = davinci_readl(DAVINCI_PLL_CNTRL1_BASE + PLLM);
  224. commonrate = ((pll0_mult + 1) * 27000000) / 6;
  225. armrate = ((pll0_mult + 1) * 27000000) / 2;
  226. fixedrate = 24000000;
  227. /* Read PLL0 configuration */
  228. prediv = (davinci_readl(DAVINCI_PLL_CNTRL0_BASE + PREDIV) &
  229. 0x1f) + 1;
  230. postdiv = (davinci_readl(DAVINCI_PLL_CNTRL0_BASE + POSTDIV) &
  231. 0x1f) + 1;
  232. /* PLL0 dividers */
  233. pll_div4 = (davinci_readl(DAVINCI_PLL_CNTRL0_BASE + PLLDIV4) &
  234. 0x1f) + 1; /* EDMA, EMAC, config, common */
  235. pll_div5 = (davinci_readl(DAVINCI_PLL_CNTRL0_BASE + PLLDIV5) &
  236. 0x1f) + 1; /* VPSS */
  237. pll_div6 = (davinci_readl(DAVINCI_PLL_CNTRL0_BASE + PLLDIV6) &
  238. 0x1f) + 1; /* VENC */
  239. pll_div7 = (davinci_readl(DAVINCI_PLL_CNTRL0_BASE + PLLDIV7) &
  240. 0x1f) + 1; /* DDR */
  241. pll_div8 = (davinci_readl(DAVINCI_PLL_CNTRL0_BASE + PLLDIV8) &
  242. 0x1f) + 1; /* MMC/SD */
  243. pll_rate = ((fixedrate / prediv) * (2 * pll0_mult)) / postdiv;
  244. commonrate = pll_rate / pll_div4; /* 486/4 = 121.5MHz */
  245. vpssrate = pll_rate / pll_div5; /* 486/2 = 243MHz */
  246. vencrate_sd = pll_rate / pll_div6; /* 486/18 = 27MHz */
  247. ddrrate = pll_rate / pll_div7; /* 486/2 = 243MHz */
  248. mmcsdrate = pll_rate / pll_div8; /* 486/4 = 121.5MHz */
  249. rt_kprintf(
  250. "PLL0: fixedrate: %d, commonrate: %d, vpssrate: %d\n",
  251. fixedrate, commonrate, vpssrate);
  252. rt_kprintf(
  253. "PLL0: vencrate_sd: %d, ddrrate: %d mmcsdrate: %d\n",
  254. vencrate_sd, (ddrrate/2), mmcsdrate);
  255. /* Read PLL1 configuration */
  256. prediv = (davinci_readl(DAVINCI_PLL_CNTRL1_BASE + PREDIV) &
  257. 0x1f) + 1;
  258. postdiv = (davinci_readl(DAVINCI_PLL_CNTRL1_BASE + POSTDIV) &
  259. 0x1f) + 1;
  260. pll_rate = ((fixedrate / prediv) * (2 * pll1_mult)) / postdiv;
  261. /* PLL1 dividers */
  262. pll_div2 = (davinci_readl(DAVINCI_PLL_CNTRL1_BASE + PLLDIV2) &
  263. 0x1f) + 1; /* ARM */
  264. pll_div4 = (davinci_readl(DAVINCI_PLL_CNTRL1_BASE + PLLDIV4) &
  265. 0x1f) + 1; /* VOICE */
  266. pll_div5 = (davinci_readl(DAVINCI_PLL_CNTRL1_BASE + PLLDIV5) &
  267. 0x1f) + 1; /* VENC */
  268. armrate = pll_rate / pll_div2; /* 594/2 = 297MHz */
  269. voicerate = pll_rate / pll_div4; /* 594/6 = 99MHz */
  270. vencrate_hd = pll_rate / pll_div5; /* 594/8 = 74.25MHz */
  271. rt_kprintf(
  272. "PLL1: armrate: %d, voicerate: %d, vencrate_hd: %d\n",
  273. armrate, voicerate, vencrate_hd);
  274. clk_list = davinci_dm365_clks;
  275. num_clks = ARRAY_SIZE(davinci_dm365_clks);
  276. return davinci_register_clks(clk_list, num_clks);
  277. }
  278. int platform_init(void)
  279. {
  280. edma_init(dm365_edma_info);
  281. }
  282. INIT_BOARD_EXPORT(platform_init);
  283. /* Reset board using the watchdog timer */
  284. void reset_system(void)
  285. {
  286. rt_uint32_t tgcr, wdtcr;
  287. rt_uint32_t base = DAVINCI_WDOG_BASE;
  288. /* Disable, internal clock source */
  289. davinci_writel(0, base + TCR);
  290. /* Reset timer, set mode to 64-bit watchdog, and unreset */
  291. davinci_writel(0, base + TGCR);
  292. tgcr = (TGCR_TIMMODE_64BIT_WDOG << TGCR_TIMMODE_SHIFT) |
  293. (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
  294. (TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
  295. davinci_writel(tgcr, base + TGCR);
  296. /* Clear counter and period regs */
  297. davinci_writel(0, base + TIM12);
  298. davinci_writel(0, base + TIM34);
  299. davinci_writel(0, base + PRD12);
  300. davinci_writel(0, base + PRD34);
  301. /* Enable periodic mode */
  302. davinci_writel(TCR_ENAMODE_PERIODIC << ENAMODE12_SHIFT, base + TCR);
  303. /* Put watchdog in pre-active state */
  304. wdtcr = (WDTCR_WDKEY_SEQ0 << WDTCR_WDKEY_SHIFT) |
  305. (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
  306. davinci_writel(wdtcr, base + WDTCR);
  307. /* Put watchdog in active state */
  308. wdtcr = (WDTCR_WDKEY_SEQ1 << WDTCR_WDKEY_SHIFT) |
  309. (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
  310. davinci_writel(wdtcr, base + WDTCR);
  311. /*
  312. * Write an invalid value to the WDKEY field to trigger
  313. * a watchdog reset.
  314. */
  315. wdtcr = 0xDEADBEEF;
  316. davinci_writel(wdtcr, base + WDTCR);
  317. }