serial.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * arch/arm/mach-lpc32xx/serial.c
  3. *
  4. * Author: Kevin Wells <kevin.wells@nxp.com>
  5. *
  6. * Copyright (C) 2010 NXP Semiconductors
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/serial.h>
  21. #include <linux/serial_core.h>
  22. #include <linux/serial_reg.h>
  23. #include <linux/serial_8250.h>
  24. #include <linux/clk.h>
  25. #include <linux/io.h>
  26. #include <mach/hardware.h>
  27. #include <mach/platform.h>
  28. #include "common.h"
  29. #define LPC32XX_SUART_FIFO_SIZE 64
  30. /* Standard 8250/16550 compatible serial ports */
  31. static struct plat_serial8250_port serial_std_platform_data[] = {
  32. #ifdef CONFIG_ARCH_LPC32XX_UART5_SELECT
  33. {
  34. .membase = io_p2v(LPC32XX_UART5_BASE),
  35. .mapbase = LPC32XX_UART5_BASE,
  36. .irq = IRQ_LPC32XX_UART_IIR5,
  37. .uartclk = LPC32XX_MAIN_OSC_FREQ,
  38. .regshift = 2,
  39. .iotype = UPIO_MEM32,
  40. .flags = UPF_BOOT_AUTOCONF | UPF_BUGGY_UART |
  41. UPF_SKIP_TEST,
  42. },
  43. #endif
  44. #ifdef CONFIG_ARCH_LPC32XX_UART3_SELECT
  45. {
  46. .membase = io_p2v(LPC32XX_UART3_BASE),
  47. .mapbase = LPC32XX_UART3_BASE,
  48. .irq = IRQ_LPC32XX_UART_IIR3,
  49. .uartclk = LPC32XX_MAIN_OSC_FREQ,
  50. .regshift = 2,
  51. .iotype = UPIO_MEM32,
  52. .flags = UPF_BOOT_AUTOCONF | UPF_BUGGY_UART |
  53. UPF_SKIP_TEST,
  54. },
  55. #endif
  56. #ifdef CONFIG_ARCH_LPC32XX_UART4_SELECT
  57. {
  58. .membase = io_p2v(LPC32XX_UART4_BASE),
  59. .mapbase = LPC32XX_UART4_BASE,
  60. .irq = IRQ_LPC32XX_UART_IIR4,
  61. .uartclk = LPC32XX_MAIN_OSC_FREQ,
  62. .regshift = 2,
  63. .iotype = UPIO_MEM32,
  64. .flags = UPF_BOOT_AUTOCONF | UPF_BUGGY_UART |
  65. UPF_SKIP_TEST,
  66. },
  67. #endif
  68. #ifdef CONFIG_ARCH_LPC32XX_UART6_SELECT
  69. {
  70. .membase = io_p2v(LPC32XX_UART6_BASE),
  71. .mapbase = LPC32XX_UART6_BASE,
  72. .irq = IRQ_LPC32XX_UART_IIR6,
  73. .uartclk = LPC32XX_MAIN_OSC_FREQ,
  74. .regshift = 2,
  75. .iotype = UPIO_MEM32,
  76. .flags = UPF_BOOT_AUTOCONF | UPF_BUGGY_UART |
  77. UPF_SKIP_TEST,
  78. },
  79. #endif
  80. { },
  81. };
  82. struct uartinit {
  83. char *uart_ck_name;
  84. u32 ck_mode_mask;
  85. void __iomem *pdiv_clk_reg;
  86. resource_size_t mapbase;
  87. };
  88. static struct uartinit uartinit_data[] __initdata = {
  89. #ifdef CONFIG_ARCH_LPC32XX_UART5_SELECT
  90. {
  91. .uart_ck_name = "uart5_ck",
  92. .ck_mode_mask =
  93. LPC32XX_UART_CLKMODE_LOAD(LPC32XX_UART_CLKMODE_ON, 5),
  94. .pdiv_clk_reg = LPC32XX_CLKPWR_UART5_CLK_CTRL,
  95. .mapbase = LPC32XX_UART5_BASE,
  96. },
  97. #endif
  98. #ifdef CONFIG_ARCH_LPC32XX_UART3_SELECT
  99. {
  100. .uart_ck_name = "uart3_ck",
  101. .ck_mode_mask =
  102. LPC32XX_UART_CLKMODE_LOAD(LPC32XX_UART_CLKMODE_ON, 3),
  103. .pdiv_clk_reg = LPC32XX_CLKPWR_UART3_CLK_CTRL,
  104. .mapbase = LPC32XX_UART3_BASE,
  105. },
  106. #endif
  107. #ifdef CONFIG_ARCH_LPC32XX_UART4_SELECT
  108. {
  109. .uart_ck_name = "uart4_ck",
  110. .ck_mode_mask =
  111. LPC32XX_UART_CLKMODE_LOAD(LPC32XX_UART_CLKMODE_ON, 4),
  112. .pdiv_clk_reg = LPC32XX_CLKPWR_UART4_CLK_CTRL,
  113. .mapbase = LPC32XX_UART4_BASE,
  114. },
  115. #endif
  116. #ifdef CONFIG_ARCH_LPC32XX_UART6_SELECT
  117. {
  118. .uart_ck_name = "uart6_ck",
  119. .ck_mode_mask =
  120. LPC32XX_UART_CLKMODE_LOAD(LPC32XX_UART_CLKMODE_ON, 6),
  121. .pdiv_clk_reg = LPC32XX_CLKPWR_UART6_CLK_CTRL,
  122. .mapbase = LPC32XX_UART6_BASE,
  123. },
  124. #endif
  125. };
  126. static struct platform_device serial_std_platform_device = {
  127. .name = "serial8250",
  128. .id = 0,
  129. .dev = {
  130. .platform_data = serial_std_platform_data,
  131. },
  132. };
  133. static struct platform_device *lpc32xx_serial_devs[] __initdata = {
  134. &serial_std_platform_device,
  135. };
  136. void __init lpc32xx_serial_init(void)
  137. {
  138. u32 tmp, clkmodes = 0;
  139. struct clk *clk;
  140. unsigned int puart;
  141. int i, j;
  142. /* UART clocks are off, let clock driver manage them */
  143. __raw_writel(0, LPC32XX_CLKPWR_UART_CLK_CTRL);
  144. for (i = 0; i < ARRAY_SIZE(uartinit_data); i++) {
  145. clk = clk_get(NULL, uartinit_data[i].uart_ck_name);
  146. if (!IS_ERR(clk)) {
  147. clk_enable(clk);
  148. serial_std_platform_data[i].uartclk =
  149. clk_get_rate(clk);
  150. }
  151. /* Fall back on main osc rate if clock rate return fails */
  152. if (serial_std_platform_data[i].uartclk == 0)
  153. serial_std_platform_data[i].uartclk =
  154. LPC32XX_MAIN_OSC_FREQ;
  155. /* Setup UART clock modes for all UARTs, disable autoclock */
  156. clkmodes |= uartinit_data[i].ck_mode_mask;
  157. /* pre-UART clock divider set to 1 */
  158. __raw_writel(0x0101, uartinit_data[i].pdiv_clk_reg);
  159. /*
  160. * Force a flush of the RX FIFOs to work around a
  161. * HW bug
  162. */
  163. puart = uartinit_data[i].mapbase;
  164. __raw_writel(0xC1, LPC32XX_UART_IIR_FCR(puart));
  165. __raw_writel(0x00, LPC32XX_UART_DLL_FIFO(puart));
  166. j = LPC32XX_SUART_FIFO_SIZE;
  167. while (j--)
  168. tmp = __raw_readl(
  169. LPC32XX_UART_DLL_FIFO(puart));
  170. __raw_writel(0, LPC32XX_UART_IIR_FCR(puart));
  171. }
  172. /* This needs to be done after all UART clocks are setup */
  173. __raw_writel(clkmodes, LPC32XX_UARTCTL_CLKMODE);
  174. for (i = 0; i < ARRAY_SIZE(uartinit_data); i++) {
  175. /* Force a flush of the RX FIFOs to work around a HW bug */
  176. puart = serial_std_platform_data[i].mapbase;
  177. __raw_writel(0xC1, LPC32XX_UART_IIR_FCR(puart));
  178. __raw_writel(0x00, LPC32XX_UART_DLL_FIFO(puart));
  179. j = LPC32XX_SUART_FIFO_SIZE;
  180. while (j--)
  181. tmp = __raw_readl(LPC32XX_UART_DLL_FIFO(puart));
  182. __raw_writel(0, LPC32XX_UART_IIR_FCR(puart));
  183. }
  184. /* Disable UART5->USB transparent mode or USB won't work */
  185. tmp = __raw_readl(LPC32XX_UARTCTL_CTRL);
  186. tmp &= ~LPC32XX_UART_U5_ROUTE_TO_USB;
  187. __raw_writel(tmp, LPC32XX_UARTCTL_CTRL);
  188. platform_add_devices(lpc32xx_serial_devs,
  189. ARRAY_SIZE(lpc32xx_serial_devs));
  190. }