early_printk_mrst.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * early_printk_mrst.c - spi-uart early printk for Intel Moorestown platform
  3. *
  4. * Copyright (c) 2008-2010, Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; version 2
  9. * of the License.
  10. */
  11. #include <linux/kmsg_dump.h>
  12. #include <linux/console.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <asm/fixmap.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/mrst.h>
  19. #define MRST_SPI_TIMEOUT 0x200000
  20. #define MRST_REGBASE_SPI0 0xff128000
  21. #define MRST_REGBASE_SPI1 0xff128400
  22. #define MRST_CLK_SPI0_REG 0xff11d86c
  23. /* Bit fields in CTRLR0 */
  24. #define SPI_DFS_OFFSET 0
  25. #define SPI_FRF_OFFSET 4
  26. #define SPI_FRF_SPI 0x0
  27. #define SPI_FRF_SSP 0x1
  28. #define SPI_FRF_MICROWIRE 0x2
  29. #define SPI_FRF_RESV 0x3
  30. #define SPI_MODE_OFFSET 6
  31. #define SPI_SCPH_OFFSET 6
  32. #define SPI_SCOL_OFFSET 7
  33. #define SPI_TMOD_OFFSET 8
  34. #define SPI_TMOD_TR 0x0 /* xmit & recv */
  35. #define SPI_TMOD_TO 0x1 /* xmit only */
  36. #define SPI_TMOD_RO 0x2 /* recv only */
  37. #define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */
  38. #define SPI_SLVOE_OFFSET 10
  39. #define SPI_SRL_OFFSET 11
  40. #define SPI_CFS_OFFSET 12
  41. /* Bit fields in SR, 7 bits */
  42. #define SR_MASK 0x7f /* cover 7 bits */
  43. #define SR_BUSY (1 << 0)
  44. #define SR_TF_NOT_FULL (1 << 1)
  45. #define SR_TF_EMPT (1 << 2)
  46. #define SR_RF_NOT_EMPT (1 << 3)
  47. #define SR_RF_FULL (1 << 4)
  48. #define SR_TX_ERR (1 << 5)
  49. #define SR_DCOL (1 << 6)
  50. struct dw_spi_reg {
  51. u32 ctrl0;
  52. u32 ctrl1;
  53. u32 ssienr;
  54. u32 mwcr;
  55. u32 ser;
  56. u32 baudr;
  57. u32 txfltr;
  58. u32 rxfltr;
  59. u32 txflr;
  60. u32 rxflr;
  61. u32 sr;
  62. u32 imr;
  63. u32 isr;
  64. u32 risr;
  65. u32 txoicr;
  66. u32 rxoicr;
  67. u32 rxuicr;
  68. u32 msticr;
  69. u32 icr;
  70. u32 dmacr;
  71. u32 dmatdlr;
  72. u32 dmardlr;
  73. u32 idr;
  74. u32 version;
  75. /* Currently operates as 32 bits, though only the low 16 bits matter */
  76. u32 dr;
  77. } __packed;
  78. #define dw_readl(dw, name) __raw_readl(&(dw)->name)
  79. #define dw_writel(dw, name, val) __raw_writel((val), &(dw)->name)
  80. /* Default use SPI0 register for mrst, we will detect Penwell and use SPI1 */
  81. static unsigned long mrst_spi_paddr = MRST_REGBASE_SPI0;
  82. static u32 *pclk_spi0;
  83. /* Always contains an accessable address, start with 0 */
  84. static struct dw_spi_reg *pspi;
  85. static struct kmsg_dumper dw_dumper;
  86. static int dumper_registered;
  87. static void dw_kmsg_dump(struct kmsg_dumper *dumper,
  88. enum kmsg_dump_reason reason,
  89. const char *s1, unsigned long l1,
  90. const char *s2, unsigned long l2)
  91. {
  92. int i;
  93. /* When run to this, we'd better re-init the HW */
  94. mrst_early_console_init();
  95. for (i = 0; i < l1; i++)
  96. early_mrst_console.write(&early_mrst_console, s1 + i, 1);
  97. for (i = 0; i < l2; i++)
  98. early_mrst_console.write(&early_mrst_console, s2 + i, 1);
  99. }
  100. /* Set the ratio rate to 115200, 8n1, IRQ disabled */
  101. static void max3110_write_config(void)
  102. {
  103. u16 config;
  104. config = 0xc001;
  105. dw_writel(pspi, dr, config);
  106. }
  107. /* Translate char to a eligible word and send to max3110 */
  108. static void max3110_write_data(char c)
  109. {
  110. u16 data;
  111. data = 0x8000 | c;
  112. dw_writel(pspi, dr, data);
  113. }
  114. void mrst_early_console_init(void)
  115. {
  116. u32 ctrlr0 = 0;
  117. u32 spi0_cdiv;
  118. u32 freq; /* Freqency info only need be searched once */
  119. /* Base clk is 100 MHz, the actual clk = 100M / (clk_divider + 1) */
  120. pclk_spi0 = (void *)set_fixmap_offset_nocache(FIX_EARLYCON_MEM_BASE,
  121. MRST_CLK_SPI0_REG);
  122. spi0_cdiv = ((*pclk_spi0) & 0xe00) >> 9;
  123. freq = 100000000 / (spi0_cdiv + 1);
  124. if (mrst_identify_cpu() == MRST_CPU_CHIP_PENWELL)
  125. mrst_spi_paddr = MRST_REGBASE_SPI1;
  126. pspi = (void *)set_fixmap_offset_nocache(FIX_EARLYCON_MEM_BASE,
  127. mrst_spi_paddr);
  128. /* Disable SPI controller */
  129. dw_writel(pspi, ssienr, 0);
  130. /* Set control param, 8 bits, transmit only mode */
  131. ctrlr0 = dw_readl(pspi, ctrl0);
  132. ctrlr0 &= 0xfcc0;
  133. ctrlr0 |= 0xf | (SPI_FRF_SPI << SPI_FRF_OFFSET)
  134. | (SPI_TMOD_TO << SPI_TMOD_OFFSET);
  135. dw_writel(pspi, ctrl0, ctrlr0);
  136. /*
  137. * Change the spi0 clk to comply with 115200 bps, use 100000 to
  138. * calculate the clk dividor to make the clock a little slower
  139. * than real baud rate.
  140. */
  141. dw_writel(pspi, baudr, freq/100000);
  142. /* Disable all INT for early phase */
  143. dw_writel(pspi, imr, 0x0);
  144. /* Set the cs to spi-uart */
  145. dw_writel(pspi, ser, 0x2);
  146. /* Enable the HW, the last step for HW init */
  147. dw_writel(pspi, ssienr, 0x1);
  148. /* Set the default configuration */
  149. max3110_write_config();
  150. /* Register the kmsg dumper */
  151. if (!dumper_registered) {
  152. dw_dumper.dump = dw_kmsg_dump;
  153. kmsg_dump_register(&dw_dumper);
  154. dumper_registered = 1;
  155. }
  156. }
  157. /* Slave select should be called in the read/write function */
  158. static void early_mrst_spi_putc(char c)
  159. {
  160. unsigned int timeout;
  161. u32 sr;
  162. timeout = MRST_SPI_TIMEOUT;
  163. /* Early putc needs to make sure the TX FIFO is not full */
  164. while (--timeout) {
  165. sr = dw_readl(pspi, sr);
  166. if (!(sr & SR_TF_NOT_FULL))
  167. cpu_relax();
  168. else
  169. break;
  170. }
  171. if (!timeout)
  172. pr_warning("MRST earlycon: timed out\n");
  173. else
  174. max3110_write_data(c);
  175. }
  176. /* Early SPI only uses polling mode */
  177. static void early_mrst_spi_write(struct console *con, const char *str, unsigned n)
  178. {
  179. int i;
  180. for (i = 0; i < n && *str; i++) {
  181. if (*str == '\n')
  182. early_mrst_spi_putc('\r');
  183. early_mrst_spi_putc(*str);
  184. str++;
  185. }
  186. }
  187. struct console early_mrst_console = {
  188. .name = "earlymrst",
  189. .write = early_mrst_spi_write,
  190. .flags = CON_PRINTBUFFER,
  191. .index = -1,
  192. };