omap_uart.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * TI OMAP processors UART emulation.
  3. *
  4. * Copyright (C) 2006-2008 Andrzej Zaborowski <balrog@zabor.org>
  5. * Copyright (C) 2007-2009 Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 or
  10. * (at your option) version 3 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu-char.h"
  21. #include "hw.h"
  22. #include "omap.h"
  23. /* We use pc-style serial ports. */
  24. #include "pc.h"
  25. #include "exec-memory.h"
  26. /* UARTs */
  27. struct omap_uart_s {
  28. target_phys_addr_t base;
  29. SerialState *serial; /* TODO */
  30. struct omap_target_agent_s *ta;
  31. omap_clk fclk;
  32. qemu_irq irq;
  33. uint8_t eblr;
  34. uint8_t syscontrol;
  35. uint8_t wkup;
  36. uint8_t cfps;
  37. uint8_t mdr[2];
  38. uint8_t scr;
  39. uint8_t clksel;
  40. };
  41. void omap_uart_reset(struct omap_uart_s *s)
  42. {
  43. s->eblr = 0x00;
  44. s->syscontrol = 0;
  45. s->wkup = 0x3f;
  46. s->cfps = 0x69;
  47. s->clksel = 0;
  48. }
  49. struct omap_uart_s *omap_uart_init(target_phys_addr_t base,
  50. qemu_irq irq, omap_clk fclk, omap_clk iclk,
  51. qemu_irq txdma, qemu_irq rxdma,
  52. const char *label, CharDriverState *chr)
  53. {
  54. struct omap_uart_s *s = (struct omap_uart_s *)
  55. g_malloc0(sizeof(struct omap_uart_s));
  56. s->base = base;
  57. s->fclk = fclk;
  58. s->irq = irq;
  59. s->serial = serial_mm_init(get_system_memory(), base, 2, irq,
  60. omap_clk_getrate(fclk)/16,
  61. chr ?: qemu_chr_new(label, "null", NULL),
  62. DEVICE_NATIVE_ENDIAN);
  63. return s;
  64. }
  65. static uint32_t omap_uart_read(void *opaque, target_phys_addr_t addr)
  66. {
  67. struct omap_uart_s *s = (struct omap_uart_s *) opaque;
  68. addr &= 0xff;
  69. switch (addr) {
  70. case 0x20: /* MDR1 */
  71. return s->mdr[0];
  72. case 0x24: /* MDR2 */
  73. return s->mdr[1];
  74. case 0x40: /* SCR */
  75. return s->scr;
  76. case 0x44: /* SSR */
  77. return 0x0;
  78. case 0x48: /* EBLR (OMAP2) */
  79. return s->eblr;
  80. case 0x4C: /* OSC_12M_SEL (OMAP1) */
  81. return s->clksel;
  82. case 0x50: /* MVR */
  83. return 0x30;
  84. case 0x54: /* SYSC (OMAP2) */
  85. return s->syscontrol;
  86. case 0x58: /* SYSS (OMAP2) */
  87. return 1;
  88. case 0x5c: /* WER (OMAP2) */
  89. return s->wkup;
  90. case 0x60: /* CFPS (OMAP2) */
  91. return s->cfps;
  92. }
  93. OMAP_BAD_REG(addr);
  94. return 0;
  95. }
  96. static void omap_uart_write(void *opaque, target_phys_addr_t addr,
  97. uint32_t value)
  98. {
  99. struct omap_uart_s *s = (struct omap_uart_s *) opaque;
  100. addr &= 0xff;
  101. switch (addr) {
  102. case 0x20: /* MDR1 */
  103. s->mdr[0] = value & 0x7f;
  104. break;
  105. case 0x24: /* MDR2 */
  106. s->mdr[1] = value & 0xff;
  107. break;
  108. case 0x40: /* SCR */
  109. s->scr = value & 0xff;
  110. break;
  111. case 0x48: /* EBLR (OMAP2) */
  112. s->eblr = value & 0xff;
  113. break;
  114. case 0x4C: /* OSC_12M_SEL (OMAP1) */
  115. s->clksel = value & 1;
  116. break;
  117. case 0x44: /* SSR */
  118. case 0x50: /* MVR */
  119. case 0x58: /* SYSS (OMAP2) */
  120. OMAP_RO_REG(addr);
  121. break;
  122. case 0x54: /* SYSC (OMAP2) */
  123. s->syscontrol = value & 0x1d;
  124. if (value & 2)
  125. omap_uart_reset(s);
  126. break;
  127. case 0x5c: /* WER (OMAP2) */
  128. s->wkup = value & 0x7f;
  129. break;
  130. case 0x60: /* CFPS (OMAP2) */
  131. s->cfps = value & 0xff;
  132. break;
  133. default:
  134. OMAP_BAD_REG(addr);
  135. }
  136. }
  137. static CPUReadMemoryFunc * const omap_uart_readfn[] = {
  138. omap_uart_read,
  139. omap_uart_read,
  140. omap_badwidth_read8,
  141. };
  142. static CPUWriteMemoryFunc * const omap_uart_writefn[] = {
  143. omap_uart_write,
  144. omap_uart_write,
  145. omap_badwidth_write8,
  146. };
  147. struct omap_uart_s *omap2_uart_init(struct omap_target_agent_s *ta,
  148. qemu_irq irq, omap_clk fclk, omap_clk iclk,
  149. qemu_irq txdma, qemu_irq rxdma,
  150. const char *label, CharDriverState *chr)
  151. {
  152. target_phys_addr_t base = omap_l4_attach(ta, 0, 0);
  153. struct omap_uart_s *s = omap_uart_init(base, irq,
  154. fclk, iclk, txdma, rxdma, label, chr);
  155. int iomemtype = cpu_register_io_memory(omap_uart_readfn,
  156. omap_uart_writefn, s, DEVICE_NATIVE_ENDIAN);
  157. s->ta = ta;
  158. cpu_register_physical_memory(base + 0x20, 0x100, iomemtype);
  159. return s;
  160. }
  161. void omap_uart_attach(struct omap_uart_s *s, CharDriverState *chr)
  162. {
  163. /* TODO: Should reuse or destroy current s->serial */
  164. s->serial = serial_mm_init(get_system_memory(), s->base, 2, s->irq,
  165. omap_clk_getrate(s->fclk) / 16,
  166. chr ?: qemu_chr_new("null", "null", NULL),
  167. DEVICE_NATIVE_ENDIAN);
  168. }