early_printk.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2011-2012 Gabor Juhos <[email protected]>
  5. */
  6. #include <linux/io.h>
  7. #include <linux/serial_reg.h>
  8. #include <asm/addrspace.h>
  9. #include <asm/setup.h>
  10. #ifdef CONFIG_SOC_RT288X
  11. #define EARLY_UART_BASE 0x300c00
  12. #define CHIPID_BASE 0x300004
  13. #elif defined(CONFIG_SOC_MT7621)
  14. #define EARLY_UART_BASE 0x1E000c00
  15. #define CHIPID_BASE 0x1E000004
  16. #else
  17. #define EARLY_UART_BASE 0x10000c00
  18. #define CHIPID_BASE 0x10000004
  19. #endif
  20. #define MT7628_CHIP_NAME1 0x20203832
  21. #define UART_REG_TX 0x04
  22. #define UART_REG_LCR 0x0c
  23. #define UART_REG_LSR 0x14
  24. #define UART_REG_LSR_RT2880 0x1c
  25. static __iomem void *uart_membase = (__iomem void *) KSEG1ADDR(EARLY_UART_BASE);
  26. static __iomem void *chipid_membase = (__iomem void *) KSEG1ADDR(CHIPID_BASE);
  27. static int init_complete;
  28. static inline void uart_w32(u32 val, unsigned reg)
  29. {
  30. __raw_writel(val, uart_membase + reg);
  31. }
  32. static inline u32 uart_r32(unsigned reg)
  33. {
  34. return __raw_readl(uart_membase + reg);
  35. }
  36. static inline int soc_is_mt7628(void)
  37. {
  38. return IS_ENABLED(CONFIG_SOC_MT7620) &&
  39. (__raw_readl(chipid_membase) == MT7628_CHIP_NAME1);
  40. }
  41. static void find_uart_base(void)
  42. {
  43. int i;
  44. if (!soc_is_mt7628())
  45. return;
  46. for (i = 0; i < 3; i++) {
  47. u32 reg = uart_r32(UART_REG_LCR + (0x100 * i));
  48. if (!reg)
  49. continue;
  50. uart_membase = (__iomem void *) KSEG1ADDR(EARLY_UART_BASE +
  51. (0x100 * i));
  52. break;
  53. }
  54. }
  55. void prom_putchar(char ch)
  56. {
  57. if (!init_complete) {
  58. find_uart_base();
  59. init_complete = 1;
  60. }
  61. if (IS_ENABLED(CONFIG_SOC_MT7621) || soc_is_mt7628()) {
  62. uart_w32((unsigned char)ch, UART_TX);
  63. while ((uart_r32(UART_REG_LSR) & UART_LSR_THRE) == 0)
  64. ;
  65. } else {
  66. while ((uart_r32(UART_REG_LSR_RT2880) & UART_LSR_THRE) == 0)
  67. ;
  68. uart_w32((unsigned char)ch, UART_REG_TX);
  69. while ((uart_r32(UART_REG_LSR_RT2880) & UART_LSR_THRE) == 0)
  70. ;
  71. }
  72. }