zaurus.c 7.5 KB

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