zaurus.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "qemu/osdep.h"
  19. #include "hw/irq.h"
  20. #include "hw/arm/sharpsl.h"
  21. #include "hw/sysbus.h"
  22. #include "migration/vmstate.h"
  23. #include "qemu/module.h"
  24. #include "qemu/log.h"
  25. #include "qom/object.h"
  26. /* SCOOP devices */
  27. #define TYPE_SCOOP "scoop"
  28. OBJECT_DECLARE_SIMPLE_TYPE(ScoopInfo, SCOOP)
  29. struct ScoopInfo {
  30. SysBusDevice parent_obj;
  31. qemu_irq handler[16];
  32. MemoryRegion iomem;
  33. uint16_t status;
  34. uint16_t power;
  35. uint32_t gpio_level;
  36. uint32_t gpio_dir;
  37. uint32_t prev_level;
  38. uint16_t mcr;
  39. uint16_t cdr;
  40. uint16_t ccr;
  41. uint16_t irr;
  42. uint16_t imr;
  43. uint16_t isr;
  44. };
  45. #define SCOOP_MCR 0x00
  46. #define SCOOP_CDR 0x04
  47. #define SCOOP_CSR 0x08
  48. #define SCOOP_CPR 0x0c
  49. #define SCOOP_CCR 0x10
  50. #define SCOOP_IRR_IRM 0x14
  51. #define SCOOP_IMR 0x18
  52. #define SCOOP_ISR 0x1c
  53. #define SCOOP_GPCR 0x20
  54. #define SCOOP_GPWR 0x24
  55. #define SCOOP_GPRR 0x28
  56. static inline void scoop_gpio_handler_update(ScoopInfo *s) {
  57. uint32_t level, diff;
  58. int bit;
  59. level = s->gpio_level & s->gpio_dir;
  60. for (diff = s->prev_level ^ level; diff; diff ^= 1 << bit) {
  61. bit = ctz32(diff);
  62. qemu_set_irq(s->handler[bit], (level >> bit) & 1);
  63. }
  64. s->prev_level = level;
  65. }
  66. static uint64_t scoop_read(void *opaque, hwaddr addr,
  67. unsigned size)
  68. {
  69. ScoopInfo *s = (ScoopInfo *) opaque;
  70. switch (addr & 0x3f) {
  71. case SCOOP_MCR:
  72. return s->mcr;
  73. case SCOOP_CDR:
  74. return s->cdr;
  75. case SCOOP_CSR:
  76. return s->status;
  77. case SCOOP_CPR:
  78. return s->power;
  79. case SCOOP_CCR:
  80. return s->ccr;
  81. case SCOOP_IRR_IRM:
  82. return s->irr;
  83. case SCOOP_IMR:
  84. return s->imr;
  85. case SCOOP_ISR:
  86. return s->isr;
  87. case SCOOP_GPCR:
  88. return s->gpio_dir;
  89. case SCOOP_GPWR:
  90. case SCOOP_GPRR:
  91. return s->gpio_level;
  92. default:
  93. qemu_log_mask(LOG_GUEST_ERROR,
  94. "scoop_read: bad register offset 0x%02" HWADDR_PRIx "\n",
  95. addr);
  96. }
  97. return 0;
  98. }
  99. static void scoop_write(void *opaque, hwaddr addr,
  100. uint64_t value, unsigned size)
  101. {
  102. ScoopInfo *s = (ScoopInfo *) opaque;
  103. value &= 0xffff;
  104. switch (addr & 0x3f) {
  105. case SCOOP_MCR:
  106. s->mcr = value;
  107. break;
  108. case SCOOP_CDR:
  109. s->cdr = value;
  110. break;
  111. case SCOOP_CPR:
  112. s->power = value;
  113. if (value & 0x80)
  114. s->power |= 0x8040;
  115. break;
  116. case SCOOP_CCR:
  117. s->ccr = value;
  118. break;
  119. case SCOOP_IRR_IRM:
  120. s->irr = value;
  121. break;
  122. case SCOOP_IMR:
  123. s->imr = value;
  124. break;
  125. case SCOOP_ISR:
  126. s->isr = value;
  127. break;
  128. case SCOOP_GPCR:
  129. s->gpio_dir = value;
  130. scoop_gpio_handler_update(s);
  131. break;
  132. case SCOOP_GPWR:
  133. case SCOOP_GPRR: /* GPRR is probably R/O in real HW */
  134. s->gpio_level = value & s->gpio_dir;
  135. scoop_gpio_handler_update(s);
  136. break;
  137. default:
  138. qemu_log_mask(LOG_GUEST_ERROR,
  139. "scoop_write: bad register offset 0x%02" HWADDR_PRIx "\n",
  140. addr);
  141. }
  142. }
  143. static const MemoryRegionOps scoop_ops = {
  144. .read = scoop_read,
  145. .write = scoop_write,
  146. .endianness = DEVICE_NATIVE_ENDIAN,
  147. };
  148. static void scoop_gpio_set(void *opaque, int line, int level)
  149. {
  150. ScoopInfo *s = (ScoopInfo *) opaque;
  151. if (level)
  152. s->gpio_level |= (1 << line);
  153. else
  154. s->gpio_level &= ~(1 << line);
  155. }
  156. static void scoop_init(Object *obj)
  157. {
  158. DeviceState *dev = DEVICE(obj);
  159. ScoopInfo *s = SCOOP(obj);
  160. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  161. s->status = 0x02;
  162. qdev_init_gpio_out(dev, s->handler, 16);
  163. qdev_init_gpio_in(dev, scoop_gpio_set, 16);
  164. memory_region_init_io(&s->iomem, obj, &scoop_ops, s, "scoop", 0x1000);
  165. sysbus_init_mmio(sbd, &s->iomem);
  166. }
  167. static int scoop_post_load(void *opaque, int version_id)
  168. {
  169. ScoopInfo *s = (ScoopInfo *) opaque;
  170. int i;
  171. uint32_t level;
  172. level = s->gpio_level & s->gpio_dir;
  173. for (i = 0; i < 16; i++) {
  174. qemu_set_irq(s->handler[i], (level >> i) & 1);
  175. }
  176. s->prev_level = level;
  177. return 0;
  178. }
  179. static bool is_version_0 (void *opaque, int version_id)
  180. {
  181. return version_id == 0;
  182. }
  183. static bool vmstate_scoop_validate(void *opaque, int version_id)
  184. {
  185. ScoopInfo *s = opaque;
  186. return !(s->prev_level & 0xffff0000) &&
  187. !(s->gpio_level & 0xffff0000) &&
  188. !(s->gpio_dir & 0xffff0000);
  189. }
  190. static const VMStateDescription vmstate_scoop_regs = {
  191. .name = "scoop",
  192. .version_id = 1,
  193. .minimum_version_id = 0,
  194. .post_load = scoop_post_load,
  195. .fields = (VMStateField[]) {
  196. VMSTATE_UINT16(status, ScoopInfo),
  197. VMSTATE_UINT16(power, ScoopInfo),
  198. VMSTATE_UINT32(gpio_level, ScoopInfo),
  199. VMSTATE_UINT32(gpio_dir, ScoopInfo),
  200. VMSTATE_UINT32(prev_level, ScoopInfo),
  201. VMSTATE_VALIDATE("irq levels are 16 bit", vmstate_scoop_validate),
  202. VMSTATE_UINT16(mcr, ScoopInfo),
  203. VMSTATE_UINT16(cdr, ScoopInfo),
  204. VMSTATE_UINT16(ccr, ScoopInfo),
  205. VMSTATE_UINT16(irr, ScoopInfo),
  206. VMSTATE_UINT16(imr, ScoopInfo),
  207. VMSTATE_UINT16(isr, ScoopInfo),
  208. VMSTATE_UNUSED_TEST(is_version_0, 2),
  209. VMSTATE_END_OF_LIST(),
  210. },
  211. };
  212. static void scoop_sysbus_class_init(ObjectClass *klass, void *data)
  213. {
  214. DeviceClass *dc = DEVICE_CLASS(klass);
  215. dc->desc = "Scoop2 Sharp custom ASIC";
  216. dc->vmsd = &vmstate_scoop_regs;
  217. }
  218. static const TypeInfo scoop_sysbus_info = {
  219. .name = TYPE_SCOOP,
  220. .parent = TYPE_SYS_BUS_DEVICE,
  221. .instance_size = sizeof(ScoopInfo),
  222. .instance_init = scoop_init,
  223. .class_init = scoop_sysbus_class_init,
  224. };
  225. static void scoop_register_types(void)
  226. {
  227. type_register_static(&scoop_sysbus_info);
  228. }
  229. type_init(scoop_register_types)
  230. /* Write the bootloader parameters memory area. */
  231. #define MAGIC_CHG(a, b, c, d) ((d << 24) | (c << 16) | (b << 8) | a)
  232. static struct QEMU_PACKED sl_param_info {
  233. uint32_t comadj_keyword;
  234. int32_t comadj;
  235. uint32_t uuid_keyword;
  236. char uuid[16];
  237. uint32_t touch_keyword;
  238. int32_t touch_xp;
  239. int32_t touch_yp;
  240. int32_t touch_xd;
  241. int32_t touch_yd;
  242. uint32_t adadj_keyword;
  243. int32_t adadj;
  244. uint32_t phad_keyword;
  245. int32_t phadadj;
  246. } zaurus_bootparam = {
  247. .comadj_keyword = MAGIC_CHG('C', 'M', 'A', 'D'),
  248. .comadj = 125,
  249. .uuid_keyword = MAGIC_CHG('U', 'U', 'I', 'D'),
  250. .uuid = { -1 },
  251. .touch_keyword = MAGIC_CHG('T', 'U', 'C', 'H'),
  252. .touch_xp = -1,
  253. .adadj_keyword = MAGIC_CHG('B', 'V', 'A', 'D'),
  254. .adadj = -1,
  255. .phad_keyword = MAGIC_CHG('P', 'H', 'A', 'D'),
  256. .phadadj = 0x01,
  257. };
  258. void sl_bootparam_write(hwaddr ptr)
  259. {
  260. cpu_physical_memory_write(ptr, &zaurus_bootparam,
  261. sizeof(struct sl_param_info));
  262. }