omap_l4.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 = g_malloc0(
  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 = g_malloc0(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 = g_malloc0(sizeof(void *) * L4_PAGES);
  116. omap_l4_io_readh_fn = g_malloc0(sizeof(void *) * L4_PAGES);
  117. omap_l4_io_readw_fn = g_malloc0(sizeof(void *) * L4_PAGES);
  118. omap_l4_io_writeb_fn = g_malloc0(sizeof(void *) * L4_PAGES);
  119. omap_l4_io_writeh_fn = g_malloc0(sizeof(void *) * L4_PAGES);
  120. omap_l4_io_writew_fn = g_malloc0(sizeof(void *) * L4_PAGES);
  121. omap_l4_io_opaque = g_malloc0(sizeof(void *) * L4_PAGES);
  122. #endif
  123. return bus;
  124. }
  125. target_phys_addr_t omap_l4_region_base(struct omap_target_agent_s *ta,
  126. int region)
  127. {
  128. return ta->bus->base + ta->start[region].offset;
  129. }
  130. static uint32_t omap_l4ta_read(void *opaque, target_phys_addr_t addr)
  131. {
  132. struct omap_target_agent_s *s = (struct omap_target_agent_s *) opaque;
  133. switch (addr) {
  134. case 0x00: /* COMPONENT */
  135. return s->component;
  136. case 0x20: /* AGENT_CONTROL */
  137. return s->control;
  138. case 0x28: /* AGENT_STATUS */
  139. return s->status;
  140. }
  141. OMAP_BAD_REG(addr);
  142. return 0;
  143. }
  144. static void omap_l4ta_write(void *opaque, target_phys_addr_t addr,
  145. uint32_t value)
  146. {
  147. struct omap_target_agent_s *s = (struct omap_target_agent_s *) opaque;
  148. switch (addr) {
  149. case 0x00: /* COMPONENT */
  150. case 0x28: /* AGENT_STATUS */
  151. OMAP_RO_REG(addr);
  152. break;
  153. case 0x20: /* AGENT_CONTROL */
  154. s->control = value & 0x01000700;
  155. if (value & 1) /* OCP_RESET */
  156. s->status &= ~1; /* REQ_TIMEOUT */
  157. break;
  158. default:
  159. OMAP_BAD_REG(addr);
  160. }
  161. }
  162. static CPUReadMemoryFunc * const omap_l4ta_readfn[] = {
  163. omap_badwidth_read16,
  164. omap_l4ta_read,
  165. omap_badwidth_read16,
  166. };
  167. static CPUWriteMemoryFunc * const omap_l4ta_writefn[] = {
  168. omap_badwidth_write32,
  169. omap_badwidth_write32,
  170. omap_l4ta_write,
  171. };
  172. struct omap_target_agent_s *omap_l4ta_get(struct omap_l4_s *bus,
  173. const struct omap_l4_region_s *regions,
  174. const struct omap_l4_agent_info_s *agents,
  175. int cs)
  176. {
  177. int i, iomemtype;
  178. struct omap_target_agent_s *ta = NULL;
  179. const struct omap_l4_agent_info_s *info = NULL;
  180. for (i = 0; i < bus->ta_num; i ++)
  181. if (agents[i].ta == cs) {
  182. ta = &bus->ta[i];
  183. info = &agents[i];
  184. break;
  185. }
  186. if (!ta) {
  187. fprintf(stderr, "%s: bad target agent (%i)\n", __FUNCTION__, cs);
  188. exit(-1);
  189. }
  190. ta->bus = bus;
  191. ta->start = &regions[info->region];
  192. ta->regions = info->regions;
  193. ta->component = ('Q' << 24) | ('E' << 16) | ('M' << 8) | ('U' << 0);
  194. ta->status = 0x00000000;
  195. ta->control = 0x00000200; /* XXX 01000200 for L4TAO */
  196. iomemtype = l4_register_io_memory(omap_l4ta_readfn,
  197. omap_l4ta_writefn, ta);
  198. ta->base = omap_l4_attach(ta, info->ta_region, iomemtype);
  199. return ta;
  200. }
  201. target_phys_addr_t omap_l4_attach(struct omap_target_agent_s *ta, int region,
  202. int iotype)
  203. {
  204. target_phys_addr_t base;
  205. ssize_t size;
  206. #ifdef L4_MUX_HACK
  207. int i;
  208. #endif
  209. if (region < 0 || region >= ta->regions) {
  210. fprintf(stderr, "%s: bad io region (%i)\n", __FUNCTION__, region);
  211. exit(-1);
  212. }
  213. base = ta->bus->base + ta->start[region].offset;
  214. size = ta->start[region].size;
  215. if (iotype) {
  216. #ifndef L4_MUX_HACK
  217. cpu_register_physical_memory(base, size, iotype);
  218. #else
  219. cpu_register_physical_memory(base, size, omap_cpu_io_entry);
  220. i = (base - ta->bus->base) / TARGET_PAGE_SIZE;
  221. for (; size > 0; size -= TARGET_PAGE_SIZE, i ++) {
  222. omap_l4_io_readb_fn[i] = omap_l4_io_entry[iotype].mem_read[0];
  223. omap_l4_io_readh_fn[i] = omap_l4_io_entry[iotype].mem_read[1];
  224. omap_l4_io_readw_fn[i] = omap_l4_io_entry[iotype].mem_read[2];
  225. omap_l4_io_writeb_fn[i] = omap_l4_io_entry[iotype].mem_write[0];
  226. omap_l4_io_writeh_fn[i] = omap_l4_io_entry[iotype].mem_write[1];
  227. omap_l4_io_writew_fn[i] = omap_l4_io_entry[iotype].mem_write[2];
  228. omap_l4_io_opaque[i] = omap_l4_io_entry[iotype].opaque;
  229. }
  230. #endif
  231. }
  232. return base;
  233. }