early_printk.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
  7. */
  8. #include <linux/io.h>
  9. #include <linux/serial_reg.h>
  10. #include <asm/addrspace.h>
  11. #include <asm/setup.h>
  12. #ifdef CONFIG_SOC_RT288X
  13. #define EARLY_UART_BASE 0x300c00
  14. #define CHIPID_BASE 0x300004
  15. #elif defined(CONFIG_SOC_MT7621)
  16. #define EARLY_UART_BASE 0x1E000c00
  17. #define CHIPID_BASE 0x1E000004
  18. #else
  19. #define EARLY_UART_BASE 0x10000c00
  20. #define CHIPID_BASE 0x10000004
  21. #endif
  22. #define MT7628_CHIP_NAME1 0x20203832
  23. #define UART_REG_TX 0x04
  24. #define UART_REG_LCR 0x0c
  25. #define UART_REG_LSR 0x14
  26. #define UART_REG_LSR_RT2880 0x1c
  27. static __iomem void *uart_membase = (__iomem void *) KSEG1ADDR(EARLY_UART_BASE);
  28. static __iomem void *chipid_membase = (__iomem void *) KSEG1ADDR(CHIPID_BASE);
  29. static int init_complete;
  30. static inline void uart_w32(u32 val, unsigned reg)
  31. {
  32. __raw_writel(val, uart_membase + reg);
  33. }
  34. static inline u32 uart_r32(unsigned reg)
  35. {
  36. return __raw_readl(uart_membase + reg);
  37. }
  38. static inline int soc_is_mt7628(void)
  39. {
  40. return IS_ENABLED(CONFIG_SOC_MT7620) &&
  41. (__raw_readl(chipid_membase) == MT7628_CHIP_NAME1);
  42. }
  43. static void find_uart_base(void)
  44. {
  45. int i;
  46. if (!soc_is_mt7628())
  47. return;
  48. for (i = 0; i < 3; i++) {
  49. u32 reg = uart_r32(UART_REG_LCR + (0x100 * i));
  50. if (!reg)
  51. continue;
  52. uart_membase = (__iomem void *) KSEG1ADDR(EARLY_UART_BASE +
  53. (0x100 * i));
  54. break;
  55. }
  56. }
  57. void prom_putchar(char ch)
  58. {
  59. if (!init_complete) {
  60. find_uart_base();
  61. init_complete = 1;
  62. }
  63. if (IS_ENABLED(CONFIG_SOC_MT7621) || soc_is_mt7628()) {
  64. uart_w32((unsigned char)ch, UART_TX);
  65. while ((uart_r32(UART_REG_LSR) & UART_LSR_THRE) == 0)
  66. ;
  67. } else {
  68. while ((uart_r32(UART_REG_LSR_RT2880) & UART_LSR_THRE) == 0)
  69. ;
  70. uart_w32((unsigned char)ch, UART_REG_TX);
  71. while ((uart_r32(UART_REG_LSR_RT2880) & UART_LSR_THRE) == 0)
  72. ;
  73. }
  74. }