zaurus.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.h"
  19. #include "sharpsl.h"
  20. #include "sysbus.h"
  21. #undef REG_FMT
  22. #define REG_FMT "0x%02lx"
  23. /* SCOOP devices */
  24. typedef struct ScoopInfo ScoopInfo;
  25. struct ScoopInfo {
  26. SysBusDevice busdev;
  27. qemu_irq handler[16];
  28. uint16_t status;
  29. uint16_t power;
  30. uint32_t gpio_level;
  31. uint32_t gpio_dir;
  32. uint32_t prev_level;
  33. uint16_t mcr;
  34. uint16_t cdr;
  35. uint16_t ccr;
  36. uint16_t irr;
  37. uint16_t imr;
  38. uint16_t isr;
  39. };
  40. #define SCOOP_MCR 0x00
  41. #define SCOOP_CDR 0x04
  42. #define SCOOP_CSR 0x08
  43. #define SCOOP_CPR 0x0c
  44. #define SCOOP_CCR 0x10
  45. #define SCOOP_IRR_IRM 0x14
  46. #define SCOOP_IMR 0x18
  47. #define SCOOP_ISR 0x1c
  48. #define SCOOP_GPCR 0x20
  49. #define SCOOP_GPWR 0x24
  50. #define SCOOP_GPRR 0x28
  51. static inline void scoop_gpio_handler_update(ScoopInfo *s) {
  52. uint32_t level, diff;
  53. int bit;
  54. level = s->gpio_level & s->gpio_dir;
  55. for (diff = s->prev_level ^ level; diff; diff ^= 1 << bit) {
  56. bit = ffs(diff) - 1;
  57. qemu_set_irq(s->handler[bit], (level >> bit) & 1);
  58. }
  59. s->prev_level = level;
  60. }
  61. static uint32_t scoop_readb(void *opaque, target_phys_addr_t addr)
  62. {
  63. ScoopInfo *s = (ScoopInfo *) opaque;
  64. switch (addr & 0x3f) {
  65. case SCOOP_MCR:
  66. return s->mcr;
  67. case SCOOP_CDR:
  68. return s->cdr;
  69. case SCOOP_CSR:
  70. return s->status;
  71. case SCOOP_CPR:
  72. return s->power;
  73. case SCOOP_CCR:
  74. return s->ccr;
  75. case SCOOP_IRR_IRM:
  76. return s->irr;
  77. case SCOOP_IMR:
  78. return s->imr;
  79. case SCOOP_ISR:
  80. return s->isr;
  81. case SCOOP_GPCR:
  82. return s->gpio_dir;
  83. case SCOOP_GPWR:
  84. case SCOOP_GPRR:
  85. return s->gpio_level;
  86. default:
  87. zaurus_printf("Bad register offset " REG_FMT "\n", (unsigned long)addr);
  88. }
  89. return 0;
  90. }
  91. static void scoop_writeb(void *opaque, target_phys_addr_t addr, uint32_t value)
  92. {
  93. ScoopInfo *s = (ScoopInfo *) opaque;
  94. value &= 0xffff;
  95. switch (addr & 0x3f) {
  96. case SCOOP_MCR:
  97. s->mcr = value;
  98. break;
  99. case SCOOP_CDR:
  100. s->cdr = value;
  101. break;
  102. case SCOOP_CPR:
  103. s->power = value;
  104. if (value & 0x80)
  105. s->power |= 0x8040;
  106. break;
  107. case SCOOP_CCR:
  108. s->ccr = value;
  109. break;
  110. case SCOOP_IRR_IRM:
  111. s->irr = value;
  112. break;
  113. case SCOOP_IMR:
  114. s->imr = value;
  115. break;
  116. case SCOOP_ISR:
  117. s->isr = value;
  118. break;
  119. case SCOOP_GPCR:
  120. s->gpio_dir = value;
  121. scoop_gpio_handler_update(s);
  122. break;
  123. case SCOOP_GPWR:
  124. case SCOOP_GPRR: /* GPRR is probably R/O in real HW */
  125. s->gpio_level = value & s->gpio_dir;
  126. scoop_gpio_handler_update(s);
  127. break;
  128. default:
  129. zaurus_printf("Bad register offset " REG_FMT "\n", (unsigned long)addr);
  130. }
  131. }
  132. static CPUReadMemoryFunc * const scoop_readfn[] = {
  133. scoop_readb,
  134. scoop_readb,
  135. scoop_readb,
  136. };
  137. static CPUWriteMemoryFunc * const scoop_writefn[] = {
  138. scoop_writeb,
  139. scoop_writeb,
  140. scoop_writeb,
  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 *dev)
  151. {
  152. ScoopInfo *s = FROM_SYSBUS(ScoopInfo, dev);
  153. int iomemtype;
  154. s->status = 0x02;
  155. qdev_init_gpio_out(&s->busdev.qdev, s->handler, 16);
  156. qdev_init_gpio_in(&s->busdev.qdev, scoop_gpio_set, 16);
  157. iomemtype = cpu_register_io_memory(scoop_readfn,
  158. scoop_writefn, s, DEVICE_NATIVE_ENDIAN);
  159. sysbus_init_mmio(dev, 0x1000, iomemtype);
  160. return 0;
  161. }
  162. static int scoop_post_load(void *opaque, int version_id)
  163. {
  164. ScoopInfo *s = (ScoopInfo *) opaque;
  165. int i;
  166. uint32_t level;
  167. level = s->gpio_level & s->gpio_dir;
  168. for (i = 0; i < 16; i++) {
  169. qemu_set_irq(s->handler[i], (level >> i) & 1);
  170. }
  171. s->prev_level = level;
  172. return 0;
  173. }
  174. static bool is_version_0 (void *opaque, int version_id)
  175. {
  176. return version_id == 0;
  177. }
  178. static const VMStateDescription vmstate_scoop_regs = {
  179. .name = "scoop",
  180. .version_id = 1,
  181. .minimum_version_id = 0,
  182. .minimum_version_id_old = 0,
  183. .post_load = scoop_post_load,
  184. .fields = (VMStateField []) {
  185. VMSTATE_UINT16(status, ScoopInfo),
  186. VMSTATE_UINT16(power, ScoopInfo),
  187. VMSTATE_UINT32(gpio_level, ScoopInfo),
  188. VMSTATE_UINT32(gpio_dir, ScoopInfo),
  189. VMSTATE_UINT32(prev_level, ScoopInfo),
  190. VMSTATE_UINT16(mcr, ScoopInfo),
  191. VMSTATE_UINT16(cdr, ScoopInfo),
  192. VMSTATE_UINT16(ccr, ScoopInfo),
  193. VMSTATE_UINT16(irr, ScoopInfo),
  194. VMSTATE_UINT16(imr, ScoopInfo),
  195. VMSTATE_UINT16(isr, ScoopInfo),
  196. VMSTATE_UNUSED_TEST(is_version_0, 2),
  197. VMSTATE_END_OF_LIST(),
  198. },
  199. };
  200. static SysBusDeviceInfo scoop_sysbus_info = {
  201. .init = scoop_init,
  202. .qdev.name = "scoop",
  203. .qdev.desc = "Scoop2 Sharp custom ASIC",
  204. .qdev.size = sizeof(ScoopInfo),
  205. .qdev.vmsd = &vmstate_scoop_regs,
  206. .qdev.props = (Property[]) {
  207. DEFINE_PROP_END_OF_LIST(),
  208. }
  209. };
  210. static void scoop_register(void)
  211. {
  212. sysbus_register_withprop(&scoop_sysbus_info);
  213. }
  214. device_init(scoop_register);
  215. /* Write the bootloader parameters memory area. */
  216. #define MAGIC_CHG(a, b, c, d) ((d << 24) | (c << 16) | (b << 8) | a)
  217. static struct __attribute__ ((__packed__)) sl_param_info {
  218. uint32_t comadj_keyword;
  219. int32_t comadj;
  220. uint32_t uuid_keyword;
  221. char uuid[16];
  222. uint32_t touch_keyword;
  223. int32_t touch_xp;
  224. int32_t touch_yp;
  225. int32_t touch_xd;
  226. int32_t touch_yd;
  227. uint32_t adadj_keyword;
  228. int32_t adadj;
  229. uint32_t phad_keyword;
  230. int32_t phadadj;
  231. } zaurus_bootparam = {
  232. .comadj_keyword = MAGIC_CHG('C', 'M', 'A', 'D'),
  233. .comadj = 125,
  234. .uuid_keyword = MAGIC_CHG('U', 'U', 'I', 'D'),
  235. .uuid = { -1 },
  236. .touch_keyword = MAGIC_CHG('T', 'U', 'C', 'H'),
  237. .touch_xp = -1,
  238. .adadj_keyword = MAGIC_CHG('B', 'V', 'A', 'D'),
  239. .adadj = -1,
  240. .phad_keyword = MAGIC_CHG('P', 'H', 'A', 'D'),
  241. .phadadj = 0x01,
  242. };
  243. void sl_bootparam_write(target_phys_addr_t ptr)
  244. {
  245. cpu_physical_memory_write(ptr, (void *)&zaurus_bootparam,
  246. sizeof(struct sl_param_info));
  247. }