early_printk_mrst.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * early_printk_mrst.c - early consoles for Intel MID platforms
  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. /*
  12. * This file implements two early consoles named mrst and hsu.
  13. * mrst is based on Maxim3110 spi-uart device, it exists in both
  14. * Moorestown and Medfield platforms, while hsu is based on a High
  15. * Speed UART device which only exists in the Medfield platform
  16. */
  17. #include <linux/serial_reg.h>
  18. #include <linux/serial_mfd.h>
  19. #include <linux/kmsg_dump.h>
  20. #include <linux/console.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/init.h>
  24. #include <linux/io.h>
  25. #include <asm/fixmap.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/mrst.h>
  28. #define MRST_SPI_TIMEOUT 0x200000
  29. #define MRST_REGBASE_SPI0 0xff128000
  30. #define MRST_REGBASE_SPI1 0xff128400
  31. #define MRST_CLK_SPI0_REG 0xff11d86c
  32. /* Bit fields in CTRLR0 */
  33. #define SPI_DFS_OFFSET 0
  34. #define SPI_FRF_OFFSET 4
  35. #define SPI_FRF_SPI 0x0
  36. #define SPI_FRF_SSP 0x1
  37. #define SPI_FRF_MICROWIRE 0x2
  38. #define SPI_FRF_RESV 0x3
  39. #define SPI_MODE_OFFSET 6
  40. #define SPI_SCPH_OFFSET 6
  41. #define SPI_SCOL_OFFSET 7
  42. #define SPI_TMOD_OFFSET 8
  43. #define SPI_TMOD_TR 0x0 /* xmit & recv */
  44. #define SPI_TMOD_TO 0x1 /* xmit only */
  45. #define SPI_TMOD_RO 0x2 /* recv only */
  46. #define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */
  47. #define SPI_SLVOE_OFFSET 10
  48. #define SPI_SRL_OFFSET 11
  49. #define SPI_CFS_OFFSET 12
  50. /* Bit fields in SR, 7 bits */
  51. #define SR_MASK 0x7f /* cover 7 bits */
  52. #define SR_BUSY (1 << 0)
  53. #define SR_TF_NOT_FULL (1 << 1)
  54. #define SR_TF_EMPT (1 << 2)
  55. #define SR_RF_NOT_EMPT (1 << 3)
  56. #define SR_RF_FULL (1 << 4)
  57. #define SR_TX_ERR (1 << 5)
  58. #define SR_DCOL (1 << 6)
  59. struct dw_spi_reg {
  60. u32 ctrl0;
  61. u32 ctrl1;
  62. u32 ssienr;
  63. u32 mwcr;
  64. u32 ser;
  65. u32 baudr;
  66. u32 txfltr;
  67. u32 rxfltr;
  68. u32 txflr;
  69. u32 rxflr;
  70. u32 sr;
  71. u32 imr;
  72. u32 isr;
  73. u32 risr;
  74. u32 txoicr;
  75. u32 rxoicr;
  76. u32 rxuicr;
  77. u32 msticr;
  78. u32 icr;
  79. u32 dmacr;
  80. u32 dmatdlr;
  81. u32 dmardlr;
  82. u32 idr;
  83. u32 version;
  84. /* Currently operates as 32 bits, though only the low 16 bits matter */
  85. u32 dr;
  86. } __packed;
  87. #define dw_readl(dw, name) __raw_readl(&(dw)->name)
  88. #define dw_writel(dw, name, val) __raw_writel((val), &(dw)->name)
  89. /* Default use SPI0 register for mrst, we will detect Penwell and use SPI1 */
  90. static unsigned long mrst_spi_paddr = MRST_REGBASE_SPI0;
  91. static u32 *pclk_spi0;
  92. /* Always contains an accessible address, start with 0 */
  93. static struct dw_spi_reg *pspi;
  94. static struct kmsg_dumper dw_dumper;
  95. static int dumper_registered;
  96. static void dw_kmsg_dump(struct kmsg_dumper *dumper,
  97. enum kmsg_dump_reason reason,
  98. const char *s1, unsigned long l1,
  99. const char *s2, unsigned long l2)
  100. {
  101. int i;
  102. /* When run to this, we'd better re-init the HW */
  103. mrst_early_console_init();
  104. for (i = 0; i < l1; i++)
  105. early_mrst_console.write(&early_mrst_console, s1 + i, 1);
  106. for (i = 0; i < l2; i++)
  107. early_mrst_console.write(&early_mrst_console, s2 + i, 1);
  108. }
  109. /* Set the ratio rate to 115200, 8n1, IRQ disabled */
  110. static void max3110_write_config(void)
  111. {
  112. u16 config;
  113. config = 0xc001;
  114. dw_writel(pspi, dr, config);
  115. }
  116. /* Translate char to a eligible word and send to max3110 */
  117. static void max3110_write_data(char c)
  118. {
  119. u16 data;
  120. data = 0x8000 | c;
  121. dw_writel(pspi, dr, data);
  122. }
  123. void mrst_early_console_init(void)
  124. {
  125. u32 ctrlr0 = 0;
  126. u32 spi0_cdiv;
  127. u32 freq; /* Freqency info only need be searched once */
  128. /* Base clk is 100 MHz, the actual clk = 100M / (clk_divider + 1) */
  129. pclk_spi0 = (void *)set_fixmap_offset_nocache(FIX_EARLYCON_MEM_BASE,
  130. MRST_CLK_SPI0_REG);
  131. spi0_cdiv = ((*pclk_spi0) & 0xe00) >> 9;
  132. freq = 100000000 / (spi0_cdiv + 1);
  133. if (mrst_identify_cpu() == MRST_CPU_CHIP_PENWELL)
  134. mrst_spi_paddr = MRST_REGBASE_SPI1;
  135. pspi = (void *)set_fixmap_offset_nocache(FIX_EARLYCON_MEM_BASE,
  136. mrst_spi_paddr);
  137. /* Disable SPI controller */
  138. dw_writel(pspi, ssienr, 0);
  139. /* Set control param, 8 bits, transmit only mode */
  140. ctrlr0 = dw_readl(pspi, ctrl0);
  141. ctrlr0 &= 0xfcc0;
  142. ctrlr0 |= 0xf | (SPI_FRF_SPI << SPI_FRF_OFFSET)
  143. | (SPI_TMOD_TO << SPI_TMOD_OFFSET);
  144. dw_writel(pspi, ctrl0, ctrlr0);
  145. /*
  146. * Change the spi0 clk to comply with 115200 bps, use 100000 to
  147. * calculate the clk dividor to make the clock a little slower
  148. * than real baud rate.
  149. */
  150. dw_writel(pspi, baudr, freq/100000);
  151. /* Disable all INT for early phase */
  152. dw_writel(pspi, imr, 0x0);
  153. /* Set the cs to spi-uart */
  154. dw_writel(pspi, ser, 0x2);
  155. /* Enable the HW, the last step for HW init */
  156. dw_writel(pspi, ssienr, 0x1);
  157. /* Set the default configuration */
  158. max3110_write_config();
  159. /* Register the kmsg dumper */
  160. if (!dumper_registered) {
  161. dw_dumper.dump = dw_kmsg_dump;
  162. kmsg_dump_register(&dw_dumper);
  163. dumper_registered = 1;
  164. }
  165. }
  166. /* Slave select should be called in the read/write function */
  167. static void early_mrst_spi_putc(char c)
  168. {
  169. unsigned int timeout;
  170. u32 sr;
  171. timeout = MRST_SPI_TIMEOUT;
  172. /* Early putc needs to make sure the TX FIFO is not full */
  173. while (--timeout) {
  174. sr = dw_readl(pspi, sr);
  175. if (!(sr & SR_TF_NOT_FULL))
  176. cpu_relax();
  177. else
  178. break;
  179. }
  180. if (!timeout)
  181. pr_warning("MRST earlycon: timed out\n");
  182. else
  183. max3110_write_data(c);
  184. }
  185. /* Early SPI only uses polling mode */
  186. static void early_mrst_spi_write(struct console *con, const char *str, unsigned n)
  187. {
  188. int i;
  189. for (i = 0; i < n && *str; i++) {
  190. if (*str == '\n')
  191. early_mrst_spi_putc('\r');
  192. early_mrst_spi_putc(*str);
  193. str++;
  194. }
  195. }
  196. struct console early_mrst_console = {
  197. .name = "earlymrst",
  198. .write = early_mrst_spi_write,
  199. .flags = CON_PRINTBUFFER,
  200. .index = -1,
  201. };
  202. /*
  203. * Following is the early console based on Medfield HSU (High
  204. * Speed UART) device.
  205. */
  206. #define HSU_PORT2_PADDR 0xffa28180
  207. static void __iomem *phsu;
  208. void hsu_early_console_init(void)
  209. {
  210. u8 lcr;
  211. phsu = (void *)set_fixmap_offset_nocache(FIX_EARLYCON_MEM_BASE,
  212. HSU_PORT2_PADDR);
  213. /* Disable FIFO */
  214. writeb(0x0, phsu + UART_FCR);
  215. /* Set to default 115200 bps, 8n1 */
  216. lcr = readb(phsu + UART_LCR);
  217. writeb((0x80 | lcr), phsu + UART_LCR);
  218. writeb(0x18, phsu + UART_DLL);
  219. writeb(lcr, phsu + UART_LCR);
  220. writel(0x3600, phsu + UART_MUL*4);
  221. writeb(0x8, phsu + UART_MCR);
  222. writeb(0x7, phsu + UART_FCR);
  223. writeb(0x3, phsu + UART_LCR);
  224. /* Clear IRQ status */
  225. readb(phsu + UART_LSR);
  226. readb(phsu + UART_RX);
  227. readb(phsu + UART_IIR);
  228. readb(phsu + UART_MSR);
  229. /* Enable FIFO */
  230. writeb(0x7, phsu + UART_FCR);
  231. }
  232. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  233. static void early_hsu_putc(char ch)
  234. {
  235. unsigned int timeout = 10000; /* 10ms */
  236. u8 status;
  237. while (--timeout) {
  238. status = readb(phsu + UART_LSR);
  239. if (status & BOTH_EMPTY)
  240. break;
  241. udelay(1);
  242. }
  243. /* Only write the char when there was no timeout */
  244. if (timeout)
  245. writeb(ch, phsu + UART_TX);
  246. }
  247. static void early_hsu_write(struct console *con, const char *str, unsigned n)
  248. {
  249. int i;
  250. for (i = 0; i < n && *str; i++) {
  251. if (*str == '\n')
  252. early_hsu_putc('\r');
  253. early_hsu_putc(*str);
  254. str++;
  255. }
  256. }
  257. struct console early_hsu_console = {
  258. .name = "earlyhsu",
  259. .write = early_hsu_write,
  260. .flags = CON_PRINTBUFFER,
  261. .index = -1,
  262. };