ar71xx_serial.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * (C) Copyright 2010
  3. * Michael Kurz <michi.kurz@googlemail.com>.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <asm/addrspace.h>
  24. #include <asm/types.h>
  25. #include <config.h>
  26. #include <asm/ar71xx.h>
  27. #define REG_SIZE 4
  28. /* === END OF CONFIG === */
  29. /* register offset */
  30. #define OFS_RCV_BUFFER (0*REG_SIZE)
  31. #define OFS_TRANS_HOLD (0*REG_SIZE)
  32. #define OFS_SEND_BUFFER (0*REG_SIZE)
  33. #define OFS_INTR_ENABLE (1*REG_SIZE)
  34. #define OFS_INTR_ID (2*REG_SIZE)
  35. #define OFS_DATA_FORMAT (3*REG_SIZE)
  36. #define OFS_LINE_CONTROL (3*REG_SIZE)
  37. #define OFS_MODEM_CONTROL (4*REG_SIZE)
  38. #define OFS_RS232_OUTPUT (4*REG_SIZE)
  39. #define OFS_LINE_STATUS (5*REG_SIZE)
  40. #define OFS_MODEM_STATUS (6*REG_SIZE)
  41. #define OFS_RS232_INPUT (6*REG_SIZE)
  42. #define OFS_SCRATCH_PAD (7*REG_SIZE)
  43. #define OFS_DIVISOR_LSB (0*REG_SIZE)
  44. #define OFS_DIVISOR_MSB (1*REG_SIZE)
  45. #define UART16550_READ(y) readl(KSEG1ADDR(AR71XX_UART_BASE+y))
  46. #define UART16550_WRITE(x, z) writel(z, KSEG1ADDR((AR71XX_UART_BASE+x)))
  47. void
  48. ar71xx_sys_frequency(u32 *cpu_freq, u32 *ddr_freq, u32 *ahb_freq)
  49. {
  50. #ifndef CONFIG_AR91XX
  51. u32 pll, pll_div, cpu_div, ahb_div, ddr_div, freq;
  52. pll = readl(KSEG1ADDR(AR71XX_PLL_REG_CPU_CONFIG + AR71XX_PLL_BASE));
  53. pll_div =
  54. ((pll & AR71XX_PLL_DIV_MASK) >> AR71XX_PLL_DIV_SHIFT) + 1;
  55. cpu_div =
  56. ((pll & AR71XX_CPU_DIV_MASK) >> AR71XX_CPU_DIV_SHIFT) + 1;
  57. ddr_div =
  58. ((pll & AR71XX_DDR_DIV_MASK) >> AR71XX_DDR_DIV_SHIFT) + 1;
  59. ahb_div =
  60. (((pll & AR71XX_AHB_DIV_MASK) >> AR71XX_AHB_DIV_SHIFT) + 1)*2;
  61. freq = pll_div * 40000000;
  62. if (cpu_freq)
  63. *cpu_freq = freq/cpu_div;
  64. if (ddr_freq)
  65. *ddr_freq = freq/ddr_div;
  66. if (ahb_freq)
  67. *ahb_freq = (freq/cpu_div)/ahb_div;
  68. #else
  69. u32 pll, pll_div, ahb_div, ddr_div, freq;
  70. pll = readl(KSEG1ADDR(AR91XX_PLL_REG_CPU_CONFIG + AR71XX_PLL_BASE));
  71. pll_div =
  72. ((pll & AR91XX_PLL_DIV_MASK) >> AR91XX_PLL_DIV_SHIFT);
  73. ddr_div =
  74. ((pll & AR91XX_DDR_DIV_MASK) >> AR91XX_DDR_DIV_SHIFT) + 1;
  75. ahb_div =
  76. (((pll & AR91XX_AHB_DIV_MASK) >> AR91XX_AHB_DIV_SHIFT) + 1)*2;
  77. freq = pll_div * 5000000;
  78. if (cpu_freq)
  79. *cpu_freq = freq;
  80. if (ddr_freq)
  81. *ddr_freq = freq/ddr_div;
  82. if (ahb_freq)
  83. *ahb_freq = freq/ahb_div;
  84. #endif
  85. }
  86. int serial_init(void)
  87. {
  88. u32 div;
  89. u32 ahb_freq = 100000000;
  90. ar71xx_sys_frequency (0, 0, &ahb_freq);
  91. div = ahb_freq/(16 * CONFIG_BAUDRATE);
  92. // enable uart pins
  93. #ifndef CONFIG_AR91XX
  94. writel(AR71XX_GPIO_FUNC_UART_EN, KSEG1ADDR(AR71XX_GPIO_BASE + GPIO_REG_FUNC));
  95. #else
  96. writel(AR91XX_GPIO_FUNC_UART_EN, KSEG1ADDR(AR71XX_GPIO_BASE + GPIO_REG_FUNC));
  97. #endif
  98. /* set DIAB bit */
  99. UART16550_WRITE(OFS_LINE_CONTROL, 0x80);
  100. /* set divisor */
  101. UART16550_WRITE(OFS_DIVISOR_LSB, (div & 0xff));
  102. UART16550_WRITE(OFS_DIVISOR_MSB, ((div >> 8) & 0xff));
  103. /* clear DIAB bit*/
  104. UART16550_WRITE(OFS_LINE_CONTROL, 0x00);
  105. /* set data format */
  106. UART16550_WRITE(OFS_DATA_FORMAT, 0x3);
  107. UART16550_WRITE(OFS_INTR_ENABLE, 0);
  108. return 0;
  109. }
  110. int serial_tstc (void)
  111. {
  112. return(UART16550_READ(OFS_LINE_STATUS) & 0x1);
  113. }
  114. int serial_getc(void)
  115. {
  116. while(!serial_tstc());
  117. return UART16550_READ(OFS_RCV_BUFFER);
  118. }
  119. void serial_putc(const char byte)
  120. {
  121. if (byte == '\n') serial_putc ('\r');
  122. while (((UART16550_READ(OFS_LINE_STATUS)) & 0x20) == 0x0);
  123. UART16550_WRITE(OFS_SEND_BUFFER, byte);
  124. }
  125. void serial_setbrg (void)
  126. {
  127. }
  128. void serial_puts (const char *s)
  129. {
  130. while (*s)
  131. {
  132. serial_putc (*s++);
  133. }
  134. }