early_printk.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifdef CONFIG_SOC_RT288X
  12. #define EARLY_UART_BASE 0x300c00
  13. #define CHIPID_BASE 0x300004
  14. #elif defined(CONFIG_SOC_MT7621)
  15. #define EARLY_UART_BASE 0x1E000c00
  16. #define CHIPID_BASE 0x1E000004
  17. #else
  18. #define EARLY_UART_BASE 0x10000c00
  19. #define CHIPID_BASE 0x10000004
  20. #endif
  21. #define MT7628_CHIP_NAME1 0x20203832
  22. #define UART_REG_TX 0x04
  23. #define UART_REG_LCR 0x0c
  24. #define UART_REG_LSR 0x14
  25. #define UART_REG_LSR_RT2880 0x1c
  26. static __iomem void *uart_membase = (__iomem void *) KSEG1ADDR(EARLY_UART_BASE);
  27. static __iomem void *chipid_membase = (__iomem void *) KSEG1ADDR(CHIPID_BASE);
  28. static int init_complete;
  29. static inline void uart_w32(u32 val, unsigned reg)
  30. {
  31. __raw_writel(val, uart_membase + reg);
  32. }
  33. static inline u32 uart_r32(unsigned reg)
  34. {
  35. return __raw_readl(uart_membase + reg);
  36. }
  37. static inline int soc_is_mt7628(void)
  38. {
  39. return IS_ENABLED(CONFIG_SOC_MT7620) &&
  40. (__raw_readl(chipid_membase) == MT7628_CHIP_NAME1);
  41. }
  42. static void find_uart_base(void)
  43. {
  44. int i;
  45. if (!soc_is_mt7628())
  46. return;
  47. for (i = 0; i < 3; i++) {
  48. u32 reg = uart_r32(UART_REG_LCR + (0x100 * i));
  49. if (!reg)
  50. continue;
  51. uart_membase = (__iomem void *) KSEG1ADDR(EARLY_UART_BASE +
  52. (0x100 * i));
  53. break;
  54. }
  55. }
  56. void prom_putchar(unsigned char ch)
  57. {
  58. if (!init_complete) {
  59. find_uart_base();
  60. init_complete = 1;
  61. }
  62. if (IS_ENABLED(CONFIG_SOC_MT7621) || soc_is_mt7628()) {
  63. uart_w32(ch, UART_TX);
  64. while ((uart_r32(UART_REG_LSR) & UART_LSR_THRE) == 0)
  65. ;
  66. } else {
  67. while ((uart_r32(UART_REG_LSR_RT2880) & UART_LSR_THRE) == 0)
  68. ;
  69. uart_w32(ch, UART_REG_TX);
  70. while ((uart_r32(UART_REG_LSR_RT2880) & UART_LSR_THRE) == 0)
  71. ;
  72. }
  73. }