4xx_uart.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * (C) Copyright 2000-2006
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2010
  6. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*
  27. * This source code is dual-licensed. You may use it under the terms of the
  28. * GNU General Public License version 2, or under the license below.
  29. *
  30. * This source code has been made available to you by IBM on an AS-IS
  31. * basis. Anyone receiving this source is licensed under IBM
  32. * copyrights to use it in any way he or she deems fit, including
  33. * copying it, modifying it, compiling it, and redistributing it either
  34. * with or without modifications. No license under IBM patents or
  35. * patent applications is to be implied by the copyright license.
  36. *
  37. * Any user of this software should understand that IBM cannot provide
  38. * technical support for this software and will not be responsible for
  39. * any consequences resulting from the use of this software.
  40. *
  41. * Any person who transfers this source code or any derivative work
  42. * must include the IBM copyright notice, this paragraph, and the
  43. * preceding two paragraphs in the transferred software.
  44. *
  45. * COPYRIGHT I B M CORPORATION 1995
  46. * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
  47. */
  48. #include <common.h>
  49. #include <commproc.h>
  50. #include <asm/processor.h>
  51. #include <asm/io.h>
  52. #include <watchdog.h>
  53. #include <asm/ppc4xx.h>
  54. DECLARE_GLOBAL_DATA_PTR;
  55. #if defined(CONFIG_405GP) || defined(CONFIG_405CR) || \
  56. defined(CONFIG_405EP) || defined(CONFIG_405EZ) || \
  57. defined(CONFIG_405EX) || defined(CONFIG_440)
  58. #if defined(CONFIG_440)
  59. #if defined(CONFIG_440GP)
  60. #define CR0_MASK 0x3fff0000
  61. #define CR0_EXTCLK_ENA 0x00600000
  62. #define CR0_UDIV_POS 16
  63. #define UDIV_SUBTRACT 1
  64. #define UART0_SDR CPC0_CR0
  65. #define MFREG(a, d) d = mfdcr(a)
  66. #define MTREG(a, d) mtdcr(a, d)
  67. #else /* #if defined(CONFIG_440GP) */
  68. /* all other 440 PPC's access clock divider via sdr register */
  69. #define CR0_MASK 0xdfffffff
  70. #define CR0_EXTCLK_ENA 0x00800000
  71. #define CR0_UDIV_POS 0
  72. #define UDIV_SUBTRACT 0
  73. #define UART0_SDR SDR0_UART0
  74. #define UART1_SDR SDR0_UART1
  75. #if defined(CONFIG_440EP) || defined(CONFIG_440EPX) || \
  76. defined(CONFIG_440GR) || defined(CONFIG_440GRX) || \
  77. defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
  78. defined(CONFIG_460EX) || defined(CONFIG_460GT)
  79. #define UART2_SDR SDR0_UART2
  80. #endif
  81. #if defined(CONFIG_440EP) || defined(CONFIG_440EPX) || \
  82. defined(CONFIG_440GR) || defined(CONFIG_440GRX) || \
  83. defined(CONFIG_460EX) || defined(CONFIG_460GT)
  84. #define UART3_SDR SDR0_UART3
  85. #endif
  86. #define MFREG(a, d) mfsdr(a, d)
  87. #define MTREG(a, d) mtsdr(a, d)
  88. #endif /* #if defined(CONFIG_440GP) */
  89. #elif defined(CONFIG_405EP) || defined(CONFIG_405EZ)
  90. #define UCR0_MASK 0x0000007f
  91. #define UCR1_MASK 0x00007f00
  92. #define UCR0_UDIV_POS 0
  93. #define UCR1_UDIV_POS 8
  94. #define UDIV_MAX 127
  95. #elif defined(CONFIG_405EX)
  96. #define MFREG(a, d) mfsdr(a, d)
  97. #define MTREG(a, d) mtsdr(a, d)
  98. #define CR0_MASK 0x000000ff
  99. #define CR0_EXTCLK_ENA 0x00800000
  100. #define CR0_UDIV_POS 0
  101. #define UDIV_SUBTRACT 0
  102. #define UART0_SDR SDR0_UART0
  103. #define UART1_SDR SDR0_UART1
  104. #else /* CONFIG_405GP || CONFIG_405CR */
  105. #define CR0_MASK 0x00001fff
  106. #define CR0_EXTCLK_ENA 0x000000c0
  107. #define CR0_UDIV_POS 1
  108. #define UDIV_MAX 32
  109. #endif
  110. #if defined(CONFIG_405EP) && defined(CONFIG_SYS_EXT_SERIAL_CLOCK)
  111. #error "External serial clock not supported on AMCC PPC405EP!"
  112. #endif
  113. #if (defined(CONFIG_405EX) || defined(CONFIG_405EZ) || \
  114. defined(CONFIG_440)) && !defined(CONFIG_SYS_EXT_SERIAL_CLOCK)
  115. /*
  116. * For some SoC's, the cpu clock is on divider chain A, UART on
  117. * divider chain B ... so cpu clock is irrelevant. Get the
  118. * "optimized" values that are subject to the 1/2 opb clock
  119. * constraint.
  120. */
  121. static u16 serial_bdiv(int baudrate, u32 *udiv)
  122. {
  123. sys_info_t sysinfo;
  124. u32 div; /* total divisor udiv * bdiv */
  125. u32 umin; /* minimum udiv */
  126. u16 diff; /* smallest diff */
  127. u16 idiff; /* current diff */
  128. u16 ibdiv; /* current bdiv */
  129. u32 i;
  130. u32 est; /* current estimate */
  131. u32 max;
  132. #if defined(CONFIG_405EZ)
  133. u32 cpr_pllc;
  134. u32 plloutb;
  135. u32 reg;
  136. #endif
  137. get_sys_info(&sysinfo);
  138. #if defined(CONFIG_405EZ)
  139. /* check the pll feedback source */
  140. mfcpr(CPR0_PLLC, cpr_pllc);
  141. plloutb = ((CONFIG_SYS_CLK_FREQ * ((cpr_pllc & PLLC_SRC_MASK) ?
  142. sysinfo.pllFwdDivB : sysinfo.pllFwdDiv) *
  143. sysinfo.pllFbkDiv) / sysinfo.pllFwdDivB);
  144. div = plloutb / (16 * baudrate); /* total divisor */
  145. umin = (plloutb / get_OPB_freq()) << 1; /* 2 x OPB divisor */
  146. max = 256; /* highest possible */
  147. #else /* 405EZ */
  148. div = sysinfo.freqPLB / (16 * baudrate); /* total divisor */
  149. umin = sysinfo.pllOpbDiv << 1; /* 2 x OPB divisor */
  150. max = 32; /* highest possible */
  151. #endif /* 405EZ */
  152. *udiv = diff = max;
  153. /*
  154. * i is the test udiv value -- start with the largest
  155. * possible (max) to minimize serial clock and constrain
  156. * search to umin.
  157. */
  158. for (i = max; i > umin; i--) {
  159. ibdiv = div / i;
  160. est = i * ibdiv;
  161. idiff = (est > div) ? (est - div) : (div - est);
  162. if (idiff == 0) {
  163. *udiv = i;
  164. break; /* can't do better */
  165. } else if (idiff < diff) {
  166. *udiv = i; /* best so far */
  167. diff = idiff; /* update lowest diff*/
  168. }
  169. }
  170. #if defined(CONFIG_405EZ)
  171. mfcpr(CPR0_PERD0, reg);
  172. reg &= ~0x0000ffff;
  173. reg |= ((*udiv - 0) << 8) | (*udiv - 0);
  174. mtcpr(CPR0_PERD0, reg);
  175. #endif
  176. return div / *udiv;
  177. }
  178. #endif /* #if (defined(CONFIG_405EP) ... */
  179. /*
  180. * This function returns the UART clock used by the common
  181. * NS16550 driver. Additionally the SoC internal divisors for
  182. * optimal UART baudrate are configured.
  183. */
  184. int get_serial_clock(void)
  185. {
  186. u32 clk;
  187. u32 udiv;
  188. #if !defined(CONFIG_405EZ)
  189. u32 reg;
  190. #endif
  191. #if !defined(CONFIG_SYS_EXT_SERIAL_CLOCK)
  192. PPC4xx_SYS_INFO sys_info;
  193. #endif
  194. /*
  195. * Programming of the internal divisors is SoC specific.
  196. * Let's handle this in some #ifdef's for the SoC's.
  197. */
  198. #if defined(CONFIG_405CR) || defined(CONFIG_405GP)
  199. reg = mfdcr(CPC0_CR0) & ~CR0_MASK;
  200. #ifdef CONFIG_SYS_EXT_SERIAL_CLOCK
  201. clk = CONFIG_SYS_EXT_SERIAL_CLOCK;
  202. udiv = 1;
  203. reg |= CR0_EXTCLK_ENA;
  204. #else /* CONFIG_SYS_EXT_SERIAL_CLOCK */
  205. clk = gd->cpu_clk;
  206. #ifdef CONFIG_SYS_405_UART_ERRATA_59
  207. udiv = 31; /* Errata 59: stuck at 31 */
  208. #else /* CONFIG_SYS_405_UART_ERRATA_59 */
  209. {
  210. u32 tmp = CONFIG_SYS_BASE_BAUD * 16;
  211. udiv = (clk + tmp / 2) / tmp;
  212. }
  213. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  214. udiv = UDIV_MAX;
  215. #endif /* CONFIG_SYS_405_UART_ERRATA_59 */
  216. #endif /* CONFIG_SYS_EXT_SERIAL_CLOCK */
  217. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  218. mtdcr (CPC0_CR0, reg);
  219. #ifdef CONFIG_SYS_EXT_SERIAL_CLOCK
  220. clk = CONFIG_SYS_EXT_SERIAL_CLOCK;
  221. #else
  222. clk = CONFIG_SYS_BASE_BAUD * 16;
  223. #endif
  224. #endif /* CONFIG_405CR */
  225. #if defined(CONFIG_405EP)
  226. {
  227. u32 tmp = CONFIG_SYS_BASE_BAUD * 16;
  228. reg = mfdcr(CPC0_UCR) & ~(UCR0_MASK | UCR1_MASK);
  229. clk = gd->cpu_clk;
  230. udiv = (clk + tmp / 2) / tmp;
  231. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  232. udiv = UDIV_MAX;
  233. }
  234. reg |= udiv << UCR0_UDIV_POS; /* set the UART divisor */
  235. reg |= udiv << UCR1_UDIV_POS; /* set the UART divisor */
  236. mtdcr(CPC0_UCR, reg);
  237. clk = CONFIG_SYS_BASE_BAUD * 16;
  238. #endif /* CONFIG_405EP */
  239. #if defined(CONFIG_405EX) || defined(CONFIG_440)
  240. MFREG(UART0_SDR, reg);
  241. reg &= ~CR0_MASK;
  242. #ifdef CONFIG_SYS_EXT_SERIAL_CLOCK
  243. reg |= CR0_EXTCLK_ENA;
  244. udiv = 1;
  245. clk = CONFIG_SYS_EXT_SERIAL_CLOCK;
  246. #else /* CONFIG_SYS_EXT_SERIAL_CLOCK */
  247. clk = gd->baudrate * serial_bdiv(gd->baudrate, &udiv) * 16;
  248. #endif /* CONFIG_SYS_EXT_SERIAL_CLOCK */
  249. reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
  250. /*
  251. * Configure input clock to baudrate generator for all
  252. * available serial ports here
  253. */
  254. MTREG(UART0_SDR, reg);
  255. #if defined(UART1_SDR)
  256. MTREG(UART1_SDR, reg);
  257. #endif
  258. #if defined(UART2_SDR)
  259. MTREG(UART2_SDR, reg);
  260. #endif
  261. #if defined(UART3_SDR)
  262. MTREG(UART3_SDR, reg);
  263. #endif
  264. #endif /* CONFIG_405EX ... */
  265. #if defined(CONFIG_405EZ)
  266. clk = gd->baudrate * serial_bdiv(gd->baudrate, &udiv) * 16;
  267. #endif /* CONFIG_405EZ */
  268. /*
  269. * Correct UART frequency in bd-info struct now that
  270. * the UART divisor is available
  271. */
  272. #ifdef CONFIG_SYS_EXT_SERIAL_CLOCK
  273. gd->uart_clk = CONFIG_SYS_EXT_SERIAL_CLOCK;
  274. #else
  275. get_sys_info(&sys_info);
  276. gd->uart_clk = sys_info.freqUART / udiv;
  277. #endif
  278. return clk;
  279. }
  280. #endif /* CONFIG_405GP || CONFIG_405CR */