zaurus.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2006-2008 Openedhand Ltd.
  3. * Written by Andrzej Zaborowski <balrog@zabor.org>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 or
  8. * (at your option) version 3 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include "hw.h"
  20. #include "pxa.h"
  21. #include "sharpsl.h"
  22. #undef REG_FMT
  23. #if TARGET_PHYS_ADDR_BITS == 32
  24. #define REG_FMT "0x%02x"
  25. #else
  26. #define REG_FMT "0x%02lx"
  27. #endif
  28. /* SCOOP devices */
  29. struct scoop_info_s {
  30. qemu_irq handler[16];
  31. qemu_irq *in;
  32. uint16_t status;
  33. uint16_t power;
  34. uint32_t gpio_level;
  35. uint32_t gpio_dir;
  36. uint32_t prev_level;
  37. uint16_t mcr;
  38. uint16_t cdr;
  39. uint16_t ccr;
  40. uint16_t irr;
  41. uint16_t imr;
  42. uint16_t isr;
  43. };
  44. #define SCOOP_MCR 0x00
  45. #define SCOOP_CDR 0x04
  46. #define SCOOP_CSR 0x08
  47. #define SCOOP_CPR 0x0c
  48. #define SCOOP_CCR 0x10
  49. #define SCOOP_IRR_IRM 0x14
  50. #define SCOOP_IMR 0x18
  51. #define SCOOP_ISR 0x1c
  52. #define SCOOP_GPCR 0x20
  53. #define SCOOP_GPWR 0x24
  54. #define SCOOP_GPRR 0x28
  55. static inline void scoop_gpio_handler_update(struct scoop_info_s *s) {
  56. uint32_t level, diff;
  57. int bit;
  58. level = s->gpio_level & s->gpio_dir;
  59. for (diff = s->prev_level ^ level; diff; diff ^= 1 << bit) {
  60. bit = ffs(diff) - 1;
  61. qemu_set_irq(s->handler[bit], (level >> bit) & 1);
  62. }
  63. s->prev_level = level;
  64. }
  65. static uint32_t scoop_readb(void *opaque, target_phys_addr_t addr)
  66. {
  67. struct scoop_info_s *s = (struct scoop_info_s *) opaque;
  68. switch (addr) {
  69. case SCOOP_MCR:
  70. return s->mcr;
  71. case SCOOP_CDR:
  72. return s->cdr;
  73. case SCOOP_CSR:
  74. return s->status;
  75. case SCOOP_CPR:
  76. return s->power;
  77. case SCOOP_CCR:
  78. return s->ccr;
  79. case SCOOP_IRR_IRM:
  80. return s->irr;
  81. case SCOOP_IMR:
  82. return s->imr;
  83. case SCOOP_ISR:
  84. return s->isr;
  85. case SCOOP_GPCR:
  86. return s->gpio_dir;
  87. case SCOOP_GPWR:
  88. case SCOOP_GPRR:
  89. return s->gpio_level;
  90. default:
  91. zaurus_printf("Bad register offset " REG_FMT "\n", addr);
  92. }
  93. return 0;
  94. }
  95. static void scoop_writeb(void *opaque, target_phys_addr_t addr, uint32_t value)
  96. {
  97. struct scoop_info_s *s = (struct scoop_info_s *) opaque;
  98. value &= 0xffff;
  99. switch (addr) {
  100. case SCOOP_MCR:
  101. s->mcr = value;
  102. break;
  103. case SCOOP_CDR:
  104. s->cdr = value;
  105. break;
  106. case SCOOP_CPR:
  107. s->power = value;
  108. if (value & 0x80)
  109. s->power |= 0x8040;
  110. break;
  111. case SCOOP_CCR:
  112. s->ccr = value;
  113. break;
  114. case SCOOP_IRR_IRM:
  115. s->irr = value;
  116. break;
  117. case SCOOP_IMR:
  118. s->imr = value;
  119. break;
  120. case SCOOP_ISR:
  121. s->isr = value;
  122. break;
  123. case SCOOP_GPCR:
  124. s->gpio_dir = value;
  125. scoop_gpio_handler_update(s);
  126. break;
  127. case SCOOP_GPWR:
  128. case SCOOP_GPRR: /* GPRR is probably R/O in real HW */
  129. s->gpio_level = value & s->gpio_dir;
  130. scoop_gpio_handler_update(s);
  131. break;
  132. default:
  133. zaurus_printf("Bad register offset " REG_FMT "\n", addr);
  134. }
  135. }
  136. static CPUReadMemoryFunc *scoop_readfn[] = {
  137. scoop_readb,
  138. scoop_readb,
  139. scoop_readb,
  140. };
  141. static CPUWriteMemoryFunc *scoop_writefn[] = {
  142. scoop_writeb,
  143. scoop_writeb,
  144. scoop_writeb,
  145. };
  146. void scoop_gpio_set(void *opaque, int line, int level)
  147. {
  148. struct scoop_info_s *s = (struct scoop_info_s *) opaque;
  149. if (level)
  150. s->gpio_level |= (1 << line);
  151. else
  152. s->gpio_level &= ~(1 << line);
  153. }
  154. qemu_irq *scoop_gpio_in_get(struct scoop_info_s *s)
  155. {
  156. return s->in;
  157. }
  158. void scoop_gpio_out_set(struct scoop_info_s *s, int line,
  159. qemu_irq handler) {
  160. if (line >= 16) {
  161. fprintf(stderr, "No GPIO pin %i\n", line);
  162. exit(-1);
  163. }
  164. s->handler[line] = handler;
  165. }
  166. static void scoop_save(QEMUFile *f, void *opaque)
  167. {
  168. struct scoop_info_s *s = (struct scoop_info_s *) opaque;
  169. qemu_put_be16s(f, &s->status);
  170. qemu_put_be16s(f, &s->power);
  171. qemu_put_be32s(f, &s->gpio_level);
  172. qemu_put_be32s(f, &s->gpio_dir);
  173. qemu_put_be32s(f, &s->prev_level);
  174. qemu_put_be16s(f, &s->mcr);
  175. qemu_put_be16s(f, &s->cdr);
  176. qemu_put_be16s(f, &s->ccr);
  177. qemu_put_be16s(f, &s->irr);
  178. qemu_put_be16s(f, &s->imr);
  179. qemu_put_be16s(f, &s->isr);
  180. }
  181. static int scoop_load(QEMUFile *f, void *opaque, int version_id)
  182. {
  183. uint16_t dummy;
  184. struct scoop_info_s *s = (struct scoop_info_s *) opaque;
  185. qemu_get_be16s(f, &s->status);
  186. qemu_get_be16s(f, &s->power);
  187. qemu_get_be32s(f, &s->gpio_level);
  188. qemu_get_be32s(f, &s->gpio_dir);
  189. qemu_get_be32s(f, &s->prev_level);
  190. qemu_get_be16s(f, &s->mcr);
  191. qemu_get_be16s(f, &s->cdr);
  192. qemu_get_be16s(f, &s->ccr);
  193. qemu_get_be16s(f, &s->irr);
  194. qemu_get_be16s(f, &s->imr);
  195. qemu_get_be16s(f, &s->isr);
  196. if (version_id < 1)
  197. qemu_get_be16s(f, &dummy);
  198. return 0;
  199. }
  200. struct scoop_info_s *scoop_init(struct pxa2xx_state_s *cpu,
  201. int instance,
  202. target_phys_addr_t target_base) {
  203. int iomemtype;
  204. struct scoop_info_s *s;
  205. s = (struct scoop_info_s *)
  206. qemu_mallocz(sizeof(struct scoop_info_s));
  207. memset(s, 0, sizeof(struct scoop_info_s));
  208. s->status = 0x02;
  209. s->in = qemu_allocate_irqs(scoop_gpio_set, s, 16);
  210. iomemtype = cpu_register_io_memory(0, scoop_readfn,
  211. scoop_writefn, s);
  212. cpu_register_physical_memory(target_base, 0x1000, iomemtype);
  213. register_savevm("scoop", instance, 1, scoop_save, scoop_load, s);
  214. return s;
  215. }
  216. /* Write the bootloader parameters memory area. */
  217. #define MAGIC_CHG(a, b, c, d) ((d << 24) | (c << 16) | (b << 8) | a)
  218. static struct __attribute__ ((__packed__)) sl_param_info {
  219. uint32_t comadj_keyword;
  220. int32_t comadj;
  221. uint32_t uuid_keyword;
  222. char uuid[16];
  223. uint32_t touch_keyword;
  224. int32_t touch_xp;
  225. int32_t touch_yp;
  226. int32_t touch_xd;
  227. int32_t touch_yd;
  228. uint32_t adadj_keyword;
  229. int32_t adadj;
  230. uint32_t phad_keyword;
  231. int32_t phadadj;
  232. } zaurus_bootparam = {
  233. .comadj_keyword = MAGIC_CHG('C', 'M', 'A', 'D'),
  234. .comadj = 125,
  235. .uuid_keyword = MAGIC_CHG('U', 'U', 'I', 'D'),
  236. .uuid = { -1 },
  237. .touch_keyword = MAGIC_CHG('T', 'U', 'C', 'H'),
  238. .touch_xp = -1,
  239. .adadj_keyword = MAGIC_CHG('B', 'V', 'A', 'D'),
  240. .adadj = -1,
  241. .phad_keyword = MAGIC_CHG('P', 'H', 'A', 'D'),
  242. .phadadj = 0x01,
  243. };
  244. void sl_bootparam_write(uint32_t ptr)
  245. {
  246. memcpy(phys_ram_base + ptr, &zaurus_bootparam,
  247. sizeof(struct sl_param_info));
  248. }