2
0

zaurus.c 7.6 KB

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