axis_dev88.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * QEMU model for the AXIS devboard 88.
  3. *
  4. * Copyright (c) 2009 Edgar E. Iglesias, Axis Communications AB.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "sysbus.h"
  25. #include "net.h"
  26. #include "flash.h"
  27. #include "boards.h"
  28. #include "etraxfs.h"
  29. #include "loader.h"
  30. #include "elf.h"
  31. #include "cris-boot.h"
  32. #include "blockdev.h"
  33. #include "exec-memory.h"
  34. #define D(x)
  35. #define DNAND(x)
  36. struct nand_state_t
  37. {
  38. DeviceState *nand;
  39. unsigned int rdy:1;
  40. unsigned int ale:1;
  41. unsigned int cle:1;
  42. unsigned int ce:1;
  43. };
  44. static struct nand_state_t nand_state;
  45. static uint32_t nand_readl (void *opaque, target_phys_addr_t addr)
  46. {
  47. struct nand_state_t *s = opaque;
  48. uint32_t r;
  49. int rdy;
  50. r = nand_getio(s->nand);
  51. nand_getpins(s->nand, &rdy);
  52. s->rdy = rdy;
  53. DNAND(printf("%s addr=%x r=%x\n", __func__, addr, r));
  54. return r;
  55. }
  56. static void
  57. nand_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
  58. {
  59. struct nand_state_t *s = opaque;
  60. int rdy;
  61. DNAND(printf("%s addr=%x v=%x\n", __func__, addr, value));
  62. nand_setpins(s->nand, s->cle, s->ale, s->ce, 1, 0);
  63. nand_setio(s->nand, value);
  64. nand_getpins(s->nand, &rdy);
  65. s->rdy = rdy;
  66. }
  67. static CPUReadMemoryFunc * const nand_read[] = {
  68. &nand_readl,
  69. &nand_readl,
  70. &nand_readl,
  71. };
  72. static CPUWriteMemoryFunc * const nand_write[] = {
  73. &nand_writel,
  74. &nand_writel,
  75. &nand_writel,
  76. };
  77. struct tempsensor_t
  78. {
  79. unsigned int shiftreg;
  80. unsigned int count;
  81. enum {
  82. ST_OUT, ST_IN, ST_Z
  83. } state;
  84. uint16_t regs[3];
  85. };
  86. static void tempsensor_clkedge(struct tempsensor_t *s,
  87. unsigned int clk, unsigned int data_in)
  88. {
  89. D(printf("%s clk=%d state=%d sr=%x\n", __func__,
  90. clk, s->state, s->shiftreg));
  91. if (s->count == 0) {
  92. s->count = 16;
  93. s->state = ST_OUT;
  94. }
  95. switch (s->state) {
  96. case ST_OUT:
  97. /* Output reg is clocked at negedge. */
  98. if (!clk) {
  99. s->count--;
  100. s->shiftreg <<= 1;
  101. if (s->count == 0) {
  102. s->shiftreg = 0;
  103. s->state = ST_IN;
  104. s->count = 16;
  105. }
  106. }
  107. break;
  108. case ST_Z:
  109. if (clk) {
  110. s->count--;
  111. if (s->count == 0) {
  112. s->shiftreg = 0;
  113. s->state = ST_OUT;
  114. s->count = 16;
  115. }
  116. }
  117. break;
  118. case ST_IN:
  119. /* Indata is sampled at posedge. */
  120. if (clk) {
  121. s->count--;
  122. s->shiftreg <<= 1;
  123. s->shiftreg |= data_in & 1;
  124. if (s->count == 0) {
  125. D(printf("%s cfgreg=%x\n", __func__, s->shiftreg));
  126. s->regs[0] = s->shiftreg;
  127. s->state = ST_OUT;
  128. s->count = 16;
  129. if ((s->regs[0] & 0xff) == 0) {
  130. /* 25 degrees celcius. */
  131. s->shiftreg = 0x0b9f;
  132. } else if ((s->regs[0] & 0xff) == 0xff) {
  133. /* Sensor ID, 0x8100 LM70. */
  134. s->shiftreg = 0x8100;
  135. } else
  136. printf("Invalid tempsens state %x\n", s->regs[0]);
  137. }
  138. }
  139. break;
  140. }
  141. }
  142. #define RW_PA_DOUT 0x00
  143. #define R_PA_DIN 0x01
  144. #define RW_PA_OE 0x02
  145. #define RW_PD_DOUT 0x10
  146. #define R_PD_DIN 0x11
  147. #define RW_PD_OE 0x12
  148. static struct gpio_state_t
  149. {
  150. struct nand_state_t *nand;
  151. struct tempsensor_t tempsensor;
  152. uint32_t regs[0x5c / 4];
  153. } gpio_state;
  154. static uint32_t gpio_readl (void *opaque, target_phys_addr_t addr)
  155. {
  156. struct gpio_state_t *s = opaque;
  157. uint32_t r = 0;
  158. addr >>= 2;
  159. switch (addr)
  160. {
  161. case R_PA_DIN:
  162. r = s->regs[RW_PA_DOUT] & s->regs[RW_PA_OE];
  163. /* Encode pins from the nand. */
  164. r |= s->nand->rdy << 7;
  165. break;
  166. case R_PD_DIN:
  167. r = s->regs[RW_PD_DOUT] & s->regs[RW_PD_OE];
  168. /* Encode temp sensor pins. */
  169. r |= (!!(s->tempsensor.shiftreg & 0x10000)) << 4;
  170. break;
  171. default:
  172. r = s->regs[addr];
  173. break;
  174. }
  175. return r;
  176. D(printf("%s %x=%x\n", __func__, addr, r));
  177. }
  178. static void gpio_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
  179. {
  180. struct gpio_state_t *s = opaque;
  181. D(printf("%s %x=%x\n", __func__, addr, value));
  182. addr >>= 2;
  183. switch (addr)
  184. {
  185. case RW_PA_DOUT:
  186. /* Decode nand pins. */
  187. s->nand->ale = !!(value & (1 << 6));
  188. s->nand->cle = !!(value & (1 << 5));
  189. s->nand->ce = !!(value & (1 << 4));
  190. s->regs[addr] = value;
  191. break;
  192. case RW_PD_DOUT:
  193. /* Temp sensor clk. */
  194. if ((s->regs[addr] ^ value) & 2)
  195. tempsensor_clkedge(&s->tempsensor, !!(value & 2),
  196. !!(value & 16));
  197. s->regs[addr] = value;
  198. break;
  199. default:
  200. s->regs[addr] = value;
  201. break;
  202. }
  203. }
  204. static CPUReadMemoryFunc * const gpio_read[] = {
  205. NULL, NULL,
  206. &gpio_readl,
  207. };
  208. static CPUWriteMemoryFunc * const gpio_write[] = {
  209. NULL, NULL,
  210. &gpio_writel,
  211. };
  212. #define INTMEM_SIZE (128 * 1024)
  213. static struct cris_load_info li;
  214. static
  215. void axisdev88_init (ram_addr_t ram_size,
  216. const char *boot_device,
  217. const char *kernel_filename, const char *kernel_cmdline,
  218. const char *initrd_filename, const char *cpu_model)
  219. {
  220. CPUState *env;
  221. DeviceState *dev;
  222. SysBusDevice *s;
  223. DriveInfo *nand;
  224. qemu_irq irq[30], nmi[2], *cpu_irq;
  225. void *etraxfs_dmac;
  226. struct etraxfs_dma_client *dma_eth;
  227. int i;
  228. int nand_regs;
  229. int gpio_regs;
  230. MemoryRegion *address_space_mem = get_system_memory();
  231. MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
  232. MemoryRegion *phys_intmem = g_new(MemoryRegion, 1);
  233. /* init CPUs */
  234. if (cpu_model == NULL) {
  235. cpu_model = "crisv32";
  236. }
  237. env = cpu_init(cpu_model);
  238. /* allocate RAM */
  239. memory_region_init_ram(phys_ram, NULL, "axisdev88.ram", ram_size);
  240. memory_region_add_subregion(address_space_mem, 0x40000000, phys_ram);
  241. /* The ETRAX-FS has 128Kb on chip ram, the docs refer to it as the
  242. internal memory. */
  243. memory_region_init_ram(phys_intmem, NULL, "axisdev88.chipram", INTMEM_SIZE);
  244. memory_region_add_subregion(address_space_mem, 0x38000000, phys_intmem);
  245. /* Attach a NAND flash to CS1. */
  246. nand = drive_get(IF_MTD, 0, 0);
  247. nand_state.nand = nand_init(nand ? nand->bdrv : NULL,
  248. NAND_MFR_STMICRO, 0x39);
  249. nand_regs = cpu_register_io_memory(nand_read, nand_write, &nand_state,
  250. DEVICE_NATIVE_ENDIAN);
  251. cpu_register_physical_memory(0x10000000, 0x05000000, nand_regs);
  252. gpio_state.nand = &nand_state;
  253. gpio_regs = cpu_register_io_memory(gpio_read, gpio_write, &gpio_state,
  254. DEVICE_NATIVE_ENDIAN);
  255. cpu_register_physical_memory(0x3001a000, 0x5c, gpio_regs);
  256. cpu_irq = cris_pic_init_cpu(env);
  257. dev = qdev_create(NULL, "etraxfs,pic");
  258. /* FIXME: Is there a proper way to signal vectors to the CPU core? */
  259. qdev_prop_set_ptr(dev, "interrupt_vector", &env->interrupt_vector);
  260. qdev_init_nofail(dev);
  261. s = sysbus_from_qdev(dev);
  262. sysbus_mmio_map(s, 0, 0x3001c000);
  263. sysbus_connect_irq(s, 0, cpu_irq[0]);
  264. sysbus_connect_irq(s, 1, cpu_irq[1]);
  265. for (i = 0; i < 30; i++) {
  266. irq[i] = qdev_get_gpio_in(dev, i);
  267. }
  268. nmi[0] = qdev_get_gpio_in(dev, 30);
  269. nmi[1] = qdev_get_gpio_in(dev, 31);
  270. etraxfs_dmac = etraxfs_dmac_init(0x30000000, 10);
  271. for (i = 0; i < 10; i++) {
  272. /* On ETRAX, odd numbered channels are inputs. */
  273. etraxfs_dmac_connect(etraxfs_dmac, i, irq + 7 + i, i & 1);
  274. }
  275. /* Add the two ethernet blocks. */
  276. dma_eth = g_malloc0(sizeof dma_eth[0] * 4); /* Allocate 4 channels. */
  277. etraxfs_eth_init(&nd_table[0], 0x30034000, 1, &dma_eth[0], &dma_eth[1]);
  278. if (nb_nics > 1) {
  279. etraxfs_eth_init(&nd_table[1], 0x30036000, 2, &dma_eth[2], &dma_eth[3]);
  280. }
  281. /* The DMA Connector block is missing, hardwire things for now. */
  282. etraxfs_dmac_connect_client(etraxfs_dmac, 0, &dma_eth[0]);
  283. etraxfs_dmac_connect_client(etraxfs_dmac, 1, &dma_eth[1]);
  284. if (nb_nics > 1) {
  285. etraxfs_dmac_connect_client(etraxfs_dmac, 6, &dma_eth[2]);
  286. etraxfs_dmac_connect_client(etraxfs_dmac, 7, &dma_eth[3]);
  287. }
  288. /* 2 timers. */
  289. sysbus_create_varargs("etraxfs,timer", 0x3001e000, irq[0x1b], nmi[1], NULL);
  290. sysbus_create_varargs("etraxfs,timer", 0x3005e000, irq[0x1b], nmi[1], NULL);
  291. for (i = 0; i < 4; i++) {
  292. sysbus_create_simple("etraxfs,serial", 0x30026000 + i * 0x2000,
  293. irq[0x14 + i]);
  294. }
  295. if (!kernel_filename) {
  296. fprintf(stderr, "Kernel image must be specified\n");
  297. exit(1);
  298. }
  299. li.image_filename = kernel_filename;
  300. li.cmdline = kernel_cmdline;
  301. cris_load_image(env, &li);
  302. }
  303. static QEMUMachine axisdev88_machine = {
  304. .name = "axis-dev88",
  305. .desc = "AXIS devboard 88",
  306. .init = axisdev88_init,
  307. .is_default = 1,
  308. };
  309. static void axisdev88_machine_init(void)
  310. {
  311. qemu_register_machine(&axisdev88_machine);
  312. }
  313. machine_init(axisdev88_machine_init);