early_printk_mrst.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. {
  99. static char line[1024];
  100. size_t len;
  101. /* When run to this, we'd better re-init the HW */
  102. mrst_early_console_init();
  103. while (kmsg_dump_get_line(dumper, true, line, sizeof(line), &len))
  104. early_mrst_console.write(&early_mrst_console, line, len);
  105. }
  106. /* Set the ratio rate to 115200, 8n1, IRQ disabled */
  107. static void max3110_write_config(void)
  108. {
  109. u16 config;
  110. config = 0xc001;
  111. dw_writel(pspi, dr, config);
  112. }
  113. /* Translate char to a eligible word and send to max3110 */
  114. static void max3110_write_data(char c)
  115. {
  116. u16 data;
  117. data = 0x8000 | c;
  118. dw_writel(pspi, dr, data);
  119. }
  120. void mrst_early_console_init(void)
  121. {
  122. u32 ctrlr0 = 0;
  123. u32 spi0_cdiv;
  124. u32 freq; /* Freqency info only need be searched once */
  125. /* Base clk is 100 MHz, the actual clk = 100M / (clk_divider + 1) */
  126. pclk_spi0 = (void *)set_fixmap_offset_nocache(FIX_EARLYCON_MEM_BASE,
  127. MRST_CLK_SPI0_REG);
  128. spi0_cdiv = ((*pclk_spi0) & 0xe00) >> 9;
  129. freq = 100000000 / (spi0_cdiv + 1);
  130. if (mrst_identify_cpu() == MRST_CPU_CHIP_PENWELL)
  131. mrst_spi_paddr = MRST_REGBASE_SPI1;
  132. pspi = (void *)set_fixmap_offset_nocache(FIX_EARLYCON_MEM_BASE,
  133. mrst_spi_paddr);
  134. /* Disable SPI controller */
  135. dw_writel(pspi, ssienr, 0);
  136. /* Set control param, 8 bits, transmit only mode */
  137. ctrlr0 = dw_readl(pspi, ctrl0);
  138. ctrlr0 &= 0xfcc0;
  139. ctrlr0 |= 0xf | (SPI_FRF_SPI << SPI_FRF_OFFSET)
  140. | (SPI_TMOD_TO << SPI_TMOD_OFFSET);
  141. dw_writel(pspi, ctrl0, ctrlr0);
  142. /*
  143. * Change the spi0 clk to comply with 115200 bps, use 100000 to
  144. * calculate the clk dividor to make the clock a little slower
  145. * than real baud rate.
  146. */
  147. dw_writel(pspi, baudr, freq/100000);
  148. /* Disable all INT for early phase */
  149. dw_writel(pspi, imr, 0x0);
  150. /* Set the cs to spi-uart */
  151. dw_writel(pspi, ser, 0x2);
  152. /* Enable the HW, the last step for HW init */
  153. dw_writel(pspi, ssienr, 0x1);
  154. /* Set the default configuration */
  155. max3110_write_config();
  156. /* Register the kmsg dumper */
  157. if (!dumper_registered) {
  158. dw_dumper.dump = dw_kmsg_dump;
  159. kmsg_dump_register(&dw_dumper);
  160. dumper_registered = 1;
  161. }
  162. }
  163. /* Slave select should be called in the read/write function */
  164. static void early_mrst_spi_putc(char c)
  165. {
  166. unsigned int timeout;
  167. u32 sr;
  168. timeout = MRST_SPI_TIMEOUT;
  169. /* Early putc needs to make sure the TX FIFO is not full */
  170. while (--timeout) {
  171. sr = dw_readl(pspi, sr);
  172. if (!(sr & SR_TF_NOT_FULL))
  173. cpu_relax();
  174. else
  175. break;
  176. }
  177. if (!timeout)
  178. pr_warning("MRST earlycon: timed out\n");
  179. else
  180. max3110_write_data(c);
  181. }
  182. /* Early SPI only uses polling mode */
  183. static void early_mrst_spi_write(struct console *con, const char *str, unsigned n)
  184. {
  185. int i;
  186. for (i = 0; i < n && *str; i++) {
  187. if (*str == '\n')
  188. early_mrst_spi_putc('\r');
  189. early_mrst_spi_putc(*str);
  190. str++;
  191. }
  192. }
  193. struct console early_mrst_console = {
  194. .name = "earlymrst",
  195. .write = early_mrst_spi_write,
  196. .flags = CON_PRINTBUFFER,
  197. .index = -1,
  198. };
  199. /*
  200. * Following is the early console based on Medfield HSU (High
  201. * Speed UART) device.
  202. */
  203. #define HSU_PORT_BASE 0xffa28080
  204. static void __iomem *phsu;
  205. void hsu_early_console_init(const char *s)
  206. {
  207. unsigned long paddr, port = 0;
  208. u8 lcr;
  209. /*
  210. * Select the early HSU console port if specified by user in the
  211. * kernel command line.
  212. */
  213. if (*s && !kstrtoul(s, 10, &port))
  214. port = clamp_val(port, 0, 2);
  215. paddr = HSU_PORT_BASE + port * 0x80;
  216. phsu = (void *)set_fixmap_offset_nocache(FIX_EARLYCON_MEM_BASE, paddr);
  217. /* Disable FIFO */
  218. writeb(0x0, phsu + UART_FCR);
  219. /* Set to default 115200 bps, 8n1 */
  220. lcr = readb(phsu + UART_LCR);
  221. writeb((0x80 | lcr), phsu + UART_LCR);
  222. writeb(0x18, phsu + UART_DLL);
  223. writeb(lcr, phsu + UART_LCR);
  224. writel(0x3600, phsu + UART_MUL*4);
  225. writeb(0x8, phsu + UART_MCR);
  226. writeb(0x7, phsu + UART_FCR);
  227. writeb(0x3, phsu + UART_LCR);
  228. /* Clear IRQ status */
  229. readb(phsu + UART_LSR);
  230. readb(phsu + UART_RX);
  231. readb(phsu + UART_IIR);
  232. readb(phsu + UART_MSR);
  233. /* Enable FIFO */
  234. writeb(0x7, phsu + UART_FCR);
  235. }
  236. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  237. static void early_hsu_putc(char ch)
  238. {
  239. unsigned int timeout = 10000; /* 10ms */
  240. u8 status;
  241. while (--timeout) {
  242. status = readb(phsu + UART_LSR);
  243. if (status & BOTH_EMPTY)
  244. break;
  245. udelay(1);
  246. }
  247. /* Only write the char when there was no timeout */
  248. if (timeout)
  249. writeb(ch, phsu + UART_TX);
  250. }
  251. static void early_hsu_write(struct console *con, const char *str, unsigned n)
  252. {
  253. int i;
  254. for (i = 0; i < n && *str; i++) {
  255. if (*str == '\n')
  256. early_hsu_putc('\r');
  257. early_hsu_putc(*str);
  258. str++;
  259. }
  260. }
  261. struct console early_hsu_console = {
  262. .name = "earlyhsu",
  263. .write = early_hsu_write,
  264. .flags = CON_PRINTBUFFER,
  265. .index = -1,
  266. };