2
0

axis_dev88.c 10 KB

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