omap_l4.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * TI OMAP L4 interconnect emulation.
  3. *
  4. * Copyright (C) 2007-2009 Nokia Corporation
  5. * Written by Andrzej Zaborowski <andrew@openedhand.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 or
  10. * (at your option) any later version of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "hw.h"
  21. #include "omap.h"
  22. #ifdef L4_MUX_HACK
  23. static int omap_l4_io_entries;
  24. static int omap_cpu_io_entry;
  25. static struct omap_l4_entry {
  26. CPUReadMemoryFunc * const *mem_read;
  27. CPUWriteMemoryFunc * const *mem_write;
  28. void *opaque;
  29. } *omap_l4_io_entry;
  30. static CPUReadMemoryFunc * const *omap_l4_io_readb_fn;
  31. static CPUReadMemoryFunc * const *omap_l4_io_readh_fn;
  32. static CPUReadMemoryFunc * const *omap_l4_io_readw_fn;
  33. static CPUWriteMemoryFunc * const *omap_l4_io_writeb_fn;
  34. static CPUWriteMemoryFunc * const *omap_l4_io_writeh_fn;
  35. static CPUWriteMemoryFunc * const *omap_l4_io_writew_fn;
  36. static void **omap_l4_io_opaque;
  37. int l4_register_io_memory(CPUReadMemoryFunc * const *mem_read,
  38. CPUWriteMemoryFunc * const *mem_write, void *opaque)
  39. {
  40. omap_l4_io_entry[omap_l4_io_entries].mem_read = mem_read;
  41. omap_l4_io_entry[omap_l4_io_entries].mem_write = mem_write;
  42. omap_l4_io_entry[omap_l4_io_entries].opaque = opaque;
  43. return omap_l4_io_entries ++;
  44. }
  45. static uint32_t omap_l4_io_readb(void *opaque, target_phys_addr_t addr)
  46. {
  47. unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
  48. return omap_l4_io_readb_fn[i](omap_l4_io_opaque[i], addr);
  49. }
  50. static uint32_t omap_l4_io_readh(void *opaque, target_phys_addr_t addr)
  51. {
  52. unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
  53. return omap_l4_io_readh_fn[i](omap_l4_io_opaque[i], addr);
  54. }
  55. static uint32_t omap_l4_io_readw(void *opaque, target_phys_addr_t addr)
  56. {
  57. unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
  58. return omap_l4_io_readw_fn[i](omap_l4_io_opaque[i], addr);
  59. }
  60. static void omap_l4_io_writeb(void *opaque, target_phys_addr_t addr,
  61. uint32_t value)
  62. {
  63. unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
  64. return omap_l4_io_writeb_fn[i](omap_l4_io_opaque[i], addr, value);
  65. }
  66. static void omap_l4_io_writeh(void *opaque, target_phys_addr_t addr,
  67. uint32_t value)
  68. {
  69. unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
  70. return omap_l4_io_writeh_fn[i](omap_l4_io_opaque[i], addr, value);
  71. }
  72. static void omap_l4_io_writew(void *opaque, target_phys_addr_t addr,
  73. uint32_t value)
  74. {
  75. unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
  76. return omap_l4_io_writew_fn[i](omap_l4_io_opaque[i], addr, value);
  77. }
  78. static CPUReadMemoryFunc * const omap_l4_io_readfn[] = {
  79. omap_l4_io_readb,
  80. omap_l4_io_readh,
  81. omap_l4_io_readw,
  82. };
  83. static CPUWriteMemoryFunc * const omap_l4_io_writefn[] = {
  84. omap_l4_io_writeb,
  85. omap_l4_io_writeh,
  86. omap_l4_io_writew,
  87. };
  88. #else
  89. int l4_register_io_memory(CPUReadMemoryFunc * const *mem_read,
  90. CPUWriteMemoryFunc * const *mem_write,
  91. void *opaque)
  92. {
  93. return cpu_register_io_memory(mem_read, mem_write, opaque,
  94. DEVICE_NATIVE_ENDIAN);
  95. }
  96. #endif
  97. struct omap_l4_s {
  98. target_phys_addr_t base;
  99. int ta_num;
  100. struct omap_target_agent_s ta[0];
  101. };
  102. struct omap_l4_s *omap_l4_init(target_phys_addr_t base, int ta_num)
  103. {
  104. struct omap_l4_s *bus = qemu_mallocz(
  105. sizeof(*bus) + ta_num * sizeof(*bus->ta));
  106. bus->ta_num = ta_num;
  107. bus->base = base;
  108. #ifdef L4_MUX_HACK
  109. omap_l4_io_entries = 1;
  110. omap_l4_io_entry = qemu_mallocz(125 * sizeof(*omap_l4_io_entry));
  111. omap_cpu_io_entry =
  112. cpu_register_io_memory(omap_l4_io_readfn,
  113. omap_l4_io_writefn, bus, DEVICE_NATIVE_ENDIAN);
  114. # define L4_PAGES (0xb4000 / TARGET_PAGE_SIZE)
  115. omap_l4_io_readb_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
  116. omap_l4_io_readh_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
  117. omap_l4_io_readw_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
  118. omap_l4_io_writeb_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
  119. omap_l4_io_writeh_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
  120. omap_l4_io_writew_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
  121. omap_l4_io_opaque = qemu_mallocz(sizeof(void *) * L4_PAGES);
  122. #endif
  123. return bus;
  124. }
  125. static uint32_t omap_l4ta_read(void *opaque, target_phys_addr_t addr)
  126. {
  127. struct omap_target_agent_s *s = (struct omap_target_agent_s *) opaque;
  128. switch (addr) {
  129. case 0x00: /* COMPONENT */
  130. return s->component;
  131. case 0x20: /* AGENT_CONTROL */
  132. return s->control;
  133. case 0x28: /* AGENT_STATUS */
  134. return s->status;
  135. }
  136. OMAP_BAD_REG(addr);
  137. return 0;
  138. }
  139. static void omap_l4ta_write(void *opaque, target_phys_addr_t addr,
  140. uint32_t value)
  141. {
  142. struct omap_target_agent_s *s = (struct omap_target_agent_s *) opaque;
  143. switch (addr) {
  144. case 0x00: /* COMPONENT */
  145. case 0x28: /* AGENT_STATUS */
  146. OMAP_RO_REG(addr);
  147. break;
  148. case 0x20: /* AGENT_CONTROL */
  149. s->control = value & 0x01000700;
  150. if (value & 1) /* OCP_RESET */
  151. s->status &= ~1; /* REQ_TIMEOUT */
  152. break;
  153. default:
  154. OMAP_BAD_REG(addr);
  155. }
  156. }
  157. static CPUReadMemoryFunc * const omap_l4ta_readfn[] = {
  158. omap_badwidth_read16,
  159. omap_l4ta_read,
  160. omap_badwidth_read16,
  161. };
  162. static CPUWriteMemoryFunc * const omap_l4ta_writefn[] = {
  163. omap_badwidth_write32,
  164. omap_badwidth_write32,
  165. omap_l4ta_write,
  166. };
  167. struct omap_target_agent_s *omap_l4ta_get(struct omap_l4_s *bus,
  168. const struct omap_l4_region_s *regions,
  169. const struct omap_l4_agent_info_s *agents,
  170. int cs)
  171. {
  172. int i, iomemtype;
  173. struct omap_target_agent_s *ta = NULL;
  174. const struct omap_l4_agent_info_s *info = NULL;
  175. for (i = 0; i < bus->ta_num; i ++)
  176. if (agents[i].ta == cs) {
  177. ta = &bus->ta[i];
  178. info = &agents[i];
  179. break;
  180. }
  181. if (!ta) {
  182. fprintf(stderr, "%s: bad target agent (%i)\n", __FUNCTION__, cs);
  183. exit(-1);
  184. }
  185. ta->bus = bus;
  186. ta->start = &regions[info->region];
  187. ta->regions = info->regions;
  188. ta->component = ('Q' << 24) | ('E' << 16) | ('M' << 8) | ('U' << 0);
  189. ta->status = 0x00000000;
  190. ta->control = 0x00000200; /* XXX 01000200 for L4TAO */
  191. iomemtype = l4_register_io_memory(omap_l4ta_readfn,
  192. omap_l4ta_writefn, ta);
  193. ta->base = omap_l4_attach(ta, info->ta_region, iomemtype);
  194. return ta;
  195. }
  196. target_phys_addr_t omap_l4_attach(struct omap_target_agent_s *ta, int region,
  197. int iotype)
  198. {
  199. target_phys_addr_t base;
  200. ssize_t size;
  201. #ifdef L4_MUX_HACK
  202. int i;
  203. #endif
  204. if (region < 0 || region >= ta->regions) {
  205. fprintf(stderr, "%s: bad io region (%i)\n", __FUNCTION__, region);
  206. exit(-1);
  207. }
  208. base = ta->bus->base + ta->start[region].offset;
  209. size = ta->start[region].size;
  210. if (iotype) {
  211. #ifndef L4_MUX_HACK
  212. cpu_register_physical_memory(base, size, iotype);
  213. #else
  214. cpu_register_physical_memory(base, size, omap_cpu_io_entry);
  215. i = (base - ta->bus->base) / TARGET_PAGE_SIZE;
  216. for (; size > 0; size -= TARGET_PAGE_SIZE, i ++) {
  217. omap_l4_io_readb_fn[i] = omap_l4_io_entry[iotype].mem_read[0];
  218. omap_l4_io_readh_fn[i] = omap_l4_io_entry[iotype].mem_read[1];
  219. omap_l4_io_readw_fn[i] = omap_l4_io_entry[iotype].mem_read[2];
  220. omap_l4_io_writeb_fn[i] = omap_l4_io_entry[iotype].mem_write[0];
  221. omap_l4_io_writeh_fn[i] = omap_l4_io_entry[iotype].mem_write[1];
  222. omap_l4_io_writew_fn[i] = omap_l4_io_entry[iotype].mem_write[2];
  223. omap_l4_io_opaque[i] = omap_l4_io_entry[iotype].opaque;
  224. }
  225. #endif
  226. }
  227. return base;
  228. }