axis_dev88.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 <time.h>
  25. #include <sys/time.h>
  26. #include "hw.h"
  27. #include "net.h"
  28. #include "flash.h"
  29. #include "sysemu.h"
  30. #include "devices.h"
  31. #include "boards.h"
  32. #include "etraxfs.h"
  33. #define D(x)
  34. #define DNAND(x)
  35. struct nand_state_t
  36. {
  37. struct nand_flash_s *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 *nand_read[] = {
  67. &nand_readl,
  68. &nand_readl,
  69. &nand_readl,
  70. };
  71. static CPUWriteMemoryFunc *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 *gpio_read[] = {
  204. NULL, NULL,
  205. &gpio_readl,
  206. };
  207. static CPUWriteMemoryFunc *gpio_write[] = {
  208. NULL, NULL,
  209. &gpio_writel,
  210. };
  211. #define INTMEM_SIZE (128 * 1024)
  212. static uint32_t bootstrap_pc;
  213. static void main_cpu_reset(void *opaque)
  214. {
  215. CPUState *env = opaque;
  216. cpu_reset(env);
  217. env->pc = bootstrap_pc;
  218. }
  219. static
  220. void axisdev88_init (ram_addr_t ram_size, int vga_ram_size,
  221. const char *boot_device,
  222. const char *kernel_filename, const char *kernel_cmdline,
  223. const char *initrd_filename, const char *cpu_model)
  224. {
  225. CPUState *env;
  226. struct etraxfs_pic *pic;
  227. void *etraxfs_dmac;
  228. struct etraxfs_dma_client *eth[2] = {NULL, NULL};
  229. int kernel_size;
  230. int i;
  231. int nand_regs;
  232. int gpio_regs;
  233. ram_addr_t phys_ram;
  234. ram_addr_t phys_intmem;
  235. /* init CPUs */
  236. if (cpu_model == NULL) {
  237. cpu_model = "crisv32";
  238. }
  239. env = cpu_init(cpu_model);
  240. qemu_register_reset(main_cpu_reset, env);
  241. /* allocate RAM */
  242. phys_ram = qemu_ram_alloc(ram_size);
  243. cpu_register_physical_memory(0x40000000, ram_size, phys_ram | IO_MEM_RAM);
  244. /* The ETRAX-FS has 128Kb on chip ram, the docs refer to it as the
  245. internal memory. */
  246. phys_intmem = qemu_ram_alloc(INTMEM_SIZE);
  247. cpu_register_physical_memory(0x38000000, INTMEM_SIZE,
  248. phys_intmem | IO_MEM_RAM);
  249. /* Attach a NAND flash to CS1. */
  250. nand_state.nand = nand_init(NAND_MFR_STMICRO, 0x39);
  251. nand_regs = cpu_register_io_memory(0, nand_read, nand_write, &nand_state);
  252. cpu_register_physical_memory(0x10000000, 0x05000000, nand_regs);
  253. gpio_state.nand = &nand_state;
  254. gpio_regs = cpu_register_io_memory(0, gpio_read, gpio_write, &gpio_state);
  255. cpu_register_physical_memory(0x3001a000, 0x5c, gpio_regs);
  256. pic = etraxfs_pic_init(env, 0x3001c000);
  257. etraxfs_dmac = etraxfs_dmac_init(env, 0x30000000, 10);
  258. for (i = 0; i < 10; i++) {
  259. /* On ETRAX, odd numbered channels are inputs. */
  260. etraxfs_dmac_connect(etraxfs_dmac, i, pic->irq + 7 + i, i & 1);
  261. }
  262. /* Add the two ethernet blocks. */
  263. eth[0] = etraxfs_eth_init(&nd_table[0], env, pic->irq + 25, 0x30034000, 1);
  264. if (nb_nics > 1)
  265. eth[1] = etraxfs_eth_init(&nd_table[1], env,
  266. pic->irq + 26, 0x30036000, 2);
  267. /* The DMA Connector block is missing, hardwire things for now. */
  268. etraxfs_dmac_connect_client(etraxfs_dmac, 0, eth[0]);
  269. etraxfs_dmac_connect_client(etraxfs_dmac, 1, eth[0] + 1);
  270. if (eth[1]) {
  271. etraxfs_dmac_connect_client(etraxfs_dmac, 6, eth[1]);
  272. etraxfs_dmac_connect_client(etraxfs_dmac, 7, eth[1] + 1);
  273. }
  274. /* 2 timers. */
  275. etraxfs_timer_init(env, pic->irq + 0x1b, pic->nmi + 1, 0x3001e000);
  276. etraxfs_timer_init(env, pic->irq + 0x1b, pic->nmi + 1, 0x3005e000);
  277. for (i = 0; i < 4; i++) {
  278. if (serial_hds[i]) {
  279. etraxfs_ser_init(env, pic->irq + 0x14 + i,
  280. serial_hds[i], 0x30026000 + i * 0x2000);
  281. }
  282. }
  283. if (kernel_filename) {
  284. uint64_t entry, high;
  285. int kcmdline_len;
  286. /* Boots a kernel elf binary, os/linux-2.6/vmlinux from the axis
  287. devboard SDK. */
  288. kernel_size = load_elf(kernel_filename, -0x80000000LL,
  289. &entry, NULL, &high);
  290. bootstrap_pc = entry;
  291. if (kernel_size < 0) {
  292. /* Takes a kimage from the axis devboard SDK. */
  293. kernel_size = load_image(kernel_filename, phys_ram_base + 0x4000);
  294. bootstrap_pc = 0x40004000;
  295. env->regs[9] = 0x40004000 + kernel_size;
  296. }
  297. env->regs[8] = 0x56902387; /* RAM init magic. */
  298. if (kernel_cmdline && (kcmdline_len = strlen(kernel_cmdline))) {
  299. if (kcmdline_len > 256) {
  300. fprintf(stderr, "Too long CRIS kernel cmdline (max 256)\n");
  301. exit(1);
  302. }
  303. /* Let the kernel know we are modifying the cmdline. */
  304. env->regs[10] = 0x87109563;
  305. env->regs[11] = 0x40000000;
  306. pstrcpy_targphys(env->regs[11], 256, kernel_cmdline);
  307. }
  308. }
  309. env->pc = bootstrap_pc;
  310. printf ("pc =%x\n", env->pc);
  311. printf ("ram size =%ld\n", ram_size);
  312. }
  313. QEMUMachine axisdev88_machine = {
  314. .name = "axis-dev88",
  315. .desc = "AXIS devboard 88",
  316. .init = axisdev88_init,
  317. .ram_require = 0x8000000,
  318. };