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. #define D(x)
  34. #define DNAND(x)
  35. struct nand_state_t
  36. {
  37. DeviceState *nand;
  38. unsigned int rdy:1;
  39. unsigned int ale:1;
  40. unsigned int cle:1;
  41. unsigned int ce:1;
  42. };
  43. static struct nand_state_t nand_state;
  44. static uint32_t nand_readl (void *opaque, target_phys_addr_t addr)
  45. {
  46. struct nand_state_t *s = opaque;
  47. uint32_t r;
  48. int rdy;
  49. r = nand_getio(s->nand);
  50. nand_getpins(s->nand, &rdy);
  51. s->rdy = rdy;
  52. DNAND(printf("%s addr=%x r=%x\n", __func__, addr, r));
  53. return r;
  54. }
  55. static void
  56. nand_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
  57. {
  58. struct nand_state_t *s = opaque;
  59. int rdy;
  60. DNAND(printf("%s addr=%x v=%x\n", __func__, addr, value));
  61. nand_setpins(s->nand, s->cle, s->ale, s->ce, 1, 0);
  62. nand_setio(s->nand, value);
  63. nand_getpins(s->nand, &rdy);
  64. s->rdy = rdy;
  65. }
  66. static CPUReadMemoryFunc * const nand_read[] = {
  67. &nand_readl,
  68. &nand_readl,
  69. &nand_readl,
  70. };
  71. static CPUWriteMemoryFunc * const nand_write[] = {
  72. &nand_writel,
  73. &nand_writel,
  74. &nand_writel,
  75. };
  76. struct tempsensor_t
  77. {
  78. unsigned int shiftreg;
  79. unsigned int count;
  80. enum {
  81. ST_OUT, ST_IN, ST_Z
  82. } state;
  83. uint16_t regs[3];
  84. };
  85. static void tempsensor_clkedge(struct tempsensor_t *s,
  86. unsigned int clk, unsigned int data_in)
  87. {
  88. D(printf("%s clk=%d state=%d sr=%x\n", __func__,
  89. clk, s->state, s->shiftreg));
  90. if (s->count == 0) {
  91. s->count = 16;
  92. s->state = ST_OUT;
  93. }
  94. switch (s->state) {
  95. case ST_OUT:
  96. /* Output reg is clocked at negedge. */
  97. if (!clk) {
  98. s->count--;
  99. s->shiftreg <<= 1;
  100. if (s->count == 0) {
  101. s->shiftreg = 0;
  102. s->state = ST_IN;
  103. s->count = 16;
  104. }
  105. }
  106. break;
  107. case ST_Z:
  108. if (clk) {
  109. s->count--;
  110. if (s->count == 0) {
  111. s->shiftreg = 0;
  112. s->state = ST_OUT;
  113. s->count = 16;
  114. }
  115. }
  116. break;
  117. case ST_IN:
  118. /* Indata is sampled at posedge. */
  119. if (clk) {
  120. s->count--;
  121. s->shiftreg <<= 1;
  122. s->shiftreg |= data_in & 1;
  123. if (s->count == 0) {
  124. D(printf("%s cfgreg=%x\n", __func__, s->shiftreg));
  125. s->regs[0] = s->shiftreg;
  126. s->state = ST_OUT;
  127. s->count = 16;
  128. if ((s->regs[0] & 0xff) == 0) {
  129. /* 25 degrees celcius. */
  130. s->shiftreg = 0x0b9f;
  131. } else if ((s->regs[0] & 0xff) == 0xff) {
  132. /* Sensor ID, 0x8100 LM70. */
  133. s->shiftreg = 0x8100;
  134. } else
  135. printf("Invalid tempsens state %x\n", s->regs[0]);
  136. }
  137. }
  138. break;
  139. }
  140. }
  141. #define RW_PA_DOUT 0x00
  142. #define R_PA_DIN 0x01
  143. #define RW_PA_OE 0x02
  144. #define RW_PD_DOUT 0x10
  145. #define R_PD_DIN 0x11
  146. #define RW_PD_OE 0x12
  147. static struct gpio_state_t
  148. {
  149. struct nand_state_t *nand;
  150. struct tempsensor_t tempsensor;
  151. uint32_t regs[0x5c / 4];
  152. } gpio_state;
  153. static uint32_t gpio_readl (void *opaque, target_phys_addr_t addr)
  154. {
  155. struct gpio_state_t *s = opaque;
  156. uint32_t r = 0;
  157. addr >>= 2;
  158. switch (addr)
  159. {
  160. case R_PA_DIN:
  161. r = s->regs[RW_PA_DOUT] & s->regs[RW_PA_OE];
  162. /* Encode pins from the nand. */
  163. r |= s->nand->rdy << 7;
  164. break;
  165. case R_PD_DIN:
  166. r = s->regs[RW_PD_DOUT] & s->regs[RW_PD_OE];
  167. /* Encode temp sensor pins. */
  168. r |= (!!(s->tempsensor.shiftreg & 0x10000)) << 4;
  169. break;
  170. default:
  171. r = s->regs[addr];
  172. break;
  173. }
  174. return r;
  175. D(printf("%s %x=%x\n", __func__, addr, r));
  176. }
  177. static void gpio_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
  178. {
  179. struct gpio_state_t *s = opaque;
  180. D(printf("%s %x=%x\n", __func__, addr, value));
  181. addr >>= 2;
  182. switch (addr)
  183. {
  184. case RW_PA_DOUT:
  185. /* Decode nand pins. */
  186. s->nand->ale = !!(value & (1 << 6));
  187. s->nand->cle = !!(value & (1 << 5));
  188. s->nand->ce = !!(value & (1 << 4));
  189. s->regs[addr] = value;
  190. break;
  191. case RW_PD_DOUT:
  192. /* Temp sensor clk. */
  193. if ((s->regs[addr] ^ value) & 2)
  194. tempsensor_clkedge(&s->tempsensor, !!(value & 2),
  195. !!(value & 16));
  196. s->regs[addr] = value;
  197. break;
  198. default:
  199. s->regs[addr] = value;
  200. break;
  201. }
  202. }
  203. static CPUReadMemoryFunc * const gpio_read[] = {
  204. NULL, NULL,
  205. &gpio_readl,
  206. };
  207. static CPUWriteMemoryFunc * const gpio_write[] = {
  208. NULL, NULL,
  209. &gpio_writel,
  210. };
  211. #define INTMEM_SIZE (128 * 1024)
  212. static struct cris_load_info li;
  213. static
  214. void axisdev88_init (ram_addr_t ram_size,
  215. const char *boot_device,
  216. const char *kernel_filename, const char *kernel_cmdline,
  217. const char *initrd_filename, const char *cpu_model)
  218. {
  219. CPUState *env;
  220. DeviceState *dev;
  221. SysBusDevice *s;
  222. DriveInfo *nand;
  223. qemu_irq irq[30], nmi[2], *cpu_irq;
  224. void *etraxfs_dmac;
  225. struct etraxfs_dma_client *dma_eth;
  226. int i;
  227. int nand_regs;
  228. int gpio_regs;
  229. ram_addr_t phys_ram;
  230. ram_addr_t phys_intmem;
  231. /* init CPUs */
  232. if (cpu_model == NULL) {
  233. cpu_model = "crisv32";
  234. }
  235. env = cpu_init(cpu_model);
  236. /* allocate RAM */
  237. phys_ram = qemu_ram_alloc(NULL, "axisdev88.ram", ram_size);
  238. cpu_register_physical_memory(0x40000000, ram_size, phys_ram | IO_MEM_RAM);
  239. /* The ETRAX-FS has 128Kb on chip ram, the docs refer to it as the
  240. internal memory. */
  241. phys_intmem = qemu_ram_alloc(NULL, "axisdev88.chipram", INTMEM_SIZE);
  242. cpu_register_physical_memory(0x38000000, INTMEM_SIZE,
  243. phys_intmem | IO_MEM_RAM);
  244. /* Attach a NAND flash to CS1. */
  245. nand = drive_get(IF_MTD, 0, 0);
  246. nand_state.nand = nand_init(nand ? nand->bdrv : NULL,
  247. NAND_MFR_STMICRO, 0x39);
  248. nand_regs = cpu_register_io_memory(nand_read, nand_write, &nand_state,
  249. DEVICE_NATIVE_ENDIAN);
  250. cpu_register_physical_memory(0x10000000, 0x05000000, nand_regs);
  251. gpio_state.nand = &nand_state;
  252. gpio_regs = cpu_register_io_memory(gpio_read, gpio_write, &gpio_state,
  253. DEVICE_NATIVE_ENDIAN);
  254. cpu_register_physical_memory(0x3001a000, 0x5c, gpio_regs);
  255. cpu_irq = cris_pic_init_cpu(env);
  256. dev = qdev_create(NULL, "etraxfs,pic");
  257. /* FIXME: Is there a proper way to signal vectors to the CPU core? */
  258. qdev_prop_set_ptr(dev, "interrupt_vector", &env->interrupt_vector);
  259. qdev_init_nofail(dev);
  260. s = sysbus_from_qdev(dev);
  261. sysbus_mmio_map(s, 0, 0x3001c000);
  262. sysbus_connect_irq(s, 0, cpu_irq[0]);
  263. sysbus_connect_irq(s, 1, cpu_irq[1]);
  264. for (i = 0; i < 30; i++) {
  265. irq[i] = qdev_get_gpio_in(dev, i);
  266. }
  267. nmi[0] = qdev_get_gpio_in(dev, 30);
  268. nmi[1] = qdev_get_gpio_in(dev, 31);
  269. etraxfs_dmac = etraxfs_dmac_init(0x30000000, 10);
  270. for (i = 0; i < 10; i++) {
  271. /* On ETRAX, odd numbered channels are inputs. */
  272. etraxfs_dmac_connect(etraxfs_dmac, i, irq + 7 + i, i & 1);
  273. }
  274. /* Add the two ethernet blocks. */
  275. dma_eth = g_malloc0(sizeof dma_eth[0] * 4); /* Allocate 4 channels. */
  276. etraxfs_eth_init(&nd_table[0], 0x30034000, 1, &dma_eth[0], &dma_eth[1]);
  277. if (nb_nics > 1) {
  278. etraxfs_eth_init(&nd_table[1], 0x30036000, 2, &dma_eth[2], &dma_eth[3]);
  279. }
  280. /* The DMA Connector block is missing, hardwire things for now. */
  281. etraxfs_dmac_connect_client(etraxfs_dmac, 0, &dma_eth[0]);
  282. etraxfs_dmac_connect_client(etraxfs_dmac, 1, &dma_eth[1]);
  283. if (nb_nics > 1) {
  284. etraxfs_dmac_connect_client(etraxfs_dmac, 6, &dma_eth[2]);
  285. etraxfs_dmac_connect_client(etraxfs_dmac, 7, &dma_eth[3]);
  286. }
  287. /* 2 timers. */
  288. sysbus_create_varargs("etraxfs,timer", 0x3001e000, irq[0x1b], nmi[1], NULL);
  289. sysbus_create_varargs("etraxfs,timer", 0x3005e000, irq[0x1b], nmi[1], NULL);
  290. for (i = 0; i < 4; i++) {
  291. sysbus_create_simple("etraxfs,serial", 0x30026000 + i * 0x2000,
  292. irq[0x14 + i]);
  293. }
  294. if (!kernel_filename) {
  295. fprintf(stderr, "Kernel image must be specified\n");
  296. exit(1);
  297. }
  298. li.image_filename = kernel_filename;
  299. li.cmdline = kernel_cmdline;
  300. cris_load_image(env, &li);
  301. }
  302. static QEMUMachine axisdev88_machine = {
  303. .name = "axis-dev88",
  304. .desc = "AXIS devboard 88",
  305. .init = axisdev88_init,
  306. .is_default = 1,
  307. };
  308. static void axisdev88_machine_init(void)
  309. {
  310. qemu_register_machine(&axisdev88_machine);
  311. }
  312. machine_init(axisdev88_machine_init);