2
0

omap_gpmc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * TI OMAP general purpose memory controller emulation.
  3. *
  4. * Copyright (C) 2007-2009 Nokia Corporation
  5. * Original code written by Andrzej Zaborowski <andrew@openedhand.com>
  6. * Enhancements for OMAP3 and NAND support written by Juha Riihimäki
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 or
  11. * (at your option) any later version of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "hw.h"
  22. #include "flash.h"
  23. #include "omap.h"
  24. /* General-Purpose Memory Controller */
  25. struct omap_gpmc_s {
  26. qemu_irq irq;
  27. uint8_t sysconfig;
  28. uint16_t irqst;
  29. uint16_t irqen;
  30. uint16_t timeout;
  31. uint16_t config;
  32. uint32_t prefconfig[2];
  33. int prefcontrol;
  34. int preffifo;
  35. int prefcount;
  36. struct omap_gpmc_cs_file_s {
  37. uint32_t config[7];
  38. target_phys_addr_t base;
  39. size_t size;
  40. int iomemtype;
  41. void (*base_update)(void *opaque, target_phys_addr_t new);
  42. void (*unmap)(void *opaque);
  43. void *opaque;
  44. } cs_file[8];
  45. int ecc_cs;
  46. int ecc_ptr;
  47. uint32_t ecc_cfg;
  48. ECCState ecc[9];
  49. };
  50. static void omap_gpmc_int_update(struct omap_gpmc_s *s)
  51. {
  52. qemu_set_irq(s->irq, s->irqen & s->irqst);
  53. }
  54. static void omap_gpmc_cs_map(struct omap_gpmc_cs_file_s *f, int base, int mask)
  55. {
  56. /* TODO: check for overlapping regions and report access errors */
  57. if ((mask != 0x8 && mask != 0xc && mask != 0xe && mask != 0xf) ||
  58. (base < 0 || base >= 0x40) ||
  59. (base & 0x0f & ~mask)) {
  60. fprintf(stderr, "%s: wrong cs address mapping/decoding!\n",
  61. __FUNCTION__);
  62. return;
  63. }
  64. if (!f->opaque)
  65. return;
  66. f->base = base << 24;
  67. f->size = (0x0fffffff & ~(mask << 24)) + 1;
  68. /* TODO: rather than setting the size of the mapping (which should be
  69. * constant), the mask should cause wrapping of the address space, so
  70. * that the same memory becomes accessible at every <i>size</i> bytes
  71. * starting from <i>base</i>. */
  72. if (f->iomemtype)
  73. cpu_register_physical_memory(f->base, f->size, f->iomemtype);
  74. if (f->base_update)
  75. f->base_update(f->opaque, f->base);
  76. }
  77. static void omap_gpmc_cs_unmap(struct omap_gpmc_cs_file_s *f)
  78. {
  79. if (f->size) {
  80. if (f->unmap)
  81. f->unmap(f->opaque);
  82. if (f->iomemtype)
  83. cpu_register_physical_memory(f->base, f->size, IO_MEM_UNASSIGNED);
  84. f->base = 0;
  85. f->size = 0;
  86. }
  87. }
  88. void omap_gpmc_reset(struct omap_gpmc_s *s)
  89. {
  90. int i;
  91. s->sysconfig = 0;
  92. s->irqst = 0;
  93. s->irqen = 0;
  94. omap_gpmc_int_update(s);
  95. s->timeout = 0;
  96. s->config = 0xa00;
  97. s->prefconfig[0] = 0x00004000;
  98. s->prefconfig[1] = 0x00000000;
  99. s->prefcontrol = 0;
  100. s->preffifo = 0;
  101. s->prefcount = 0;
  102. for (i = 0; i < 8; i ++) {
  103. if (s->cs_file[i].config[6] & (1 << 6)) /* CSVALID */
  104. omap_gpmc_cs_unmap(s->cs_file + i);
  105. s->cs_file[i].config[0] = i ? 1 << 12 : 0;
  106. s->cs_file[i].config[1] = 0x101001;
  107. s->cs_file[i].config[2] = 0x020201;
  108. s->cs_file[i].config[3] = 0x10031003;
  109. s->cs_file[i].config[4] = 0x10f1111;
  110. s->cs_file[i].config[5] = 0;
  111. s->cs_file[i].config[6] = 0xf00 | (i ? 0 : 1 << 6);
  112. if (s->cs_file[i].config[6] & (1 << 6)) /* CSVALID */
  113. omap_gpmc_cs_map(&s->cs_file[i],
  114. s->cs_file[i].config[6] & 0x1f, /* MASKADDR */
  115. (s->cs_file[i].config[6] >> 8 & 0xf)); /* BASEADDR */
  116. }
  117. omap_gpmc_cs_map(s->cs_file, 0, 0xf);
  118. s->ecc_cs = 0;
  119. s->ecc_ptr = 0;
  120. s->ecc_cfg = 0x3fcff000;
  121. for (i = 0; i < 9; i ++)
  122. ecc_reset(&s->ecc[i]);
  123. }
  124. static uint32_t omap_gpmc_read(void *opaque, target_phys_addr_t addr)
  125. {
  126. struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
  127. int cs;
  128. struct omap_gpmc_cs_file_s *f;
  129. switch (addr) {
  130. case 0x000: /* GPMC_REVISION */
  131. return 0x20;
  132. case 0x010: /* GPMC_SYSCONFIG */
  133. return s->sysconfig;
  134. case 0x014: /* GPMC_SYSSTATUS */
  135. return 1; /* RESETDONE */
  136. case 0x018: /* GPMC_IRQSTATUS */
  137. return s->irqst;
  138. case 0x01c: /* GPMC_IRQENABLE */
  139. return s->irqen;
  140. case 0x040: /* GPMC_TIMEOUT_CONTROL */
  141. return s->timeout;
  142. case 0x044: /* GPMC_ERR_ADDRESS */
  143. case 0x048: /* GPMC_ERR_TYPE */
  144. return 0;
  145. case 0x050: /* GPMC_CONFIG */
  146. return s->config;
  147. case 0x054: /* GPMC_STATUS */
  148. return 0x001;
  149. case 0x060 ... 0x1d4:
  150. cs = (addr - 0x060) / 0x30;
  151. addr -= cs * 0x30;
  152. f = s->cs_file + cs;
  153. switch (addr) {
  154. case 0x60: /* GPMC_CONFIG1 */
  155. return f->config[0];
  156. case 0x64: /* GPMC_CONFIG2 */
  157. return f->config[1];
  158. case 0x68: /* GPMC_CONFIG3 */
  159. return f->config[2];
  160. case 0x6c: /* GPMC_CONFIG4 */
  161. return f->config[3];
  162. case 0x70: /* GPMC_CONFIG5 */
  163. return f->config[4];
  164. case 0x74: /* GPMC_CONFIG6 */
  165. return f->config[5];
  166. case 0x78: /* GPMC_CONFIG7 */
  167. return f->config[6];
  168. case 0x84: /* GPMC_NAND_DATA */
  169. return 0;
  170. }
  171. break;
  172. case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
  173. return s->prefconfig[0];
  174. case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
  175. return s->prefconfig[1];
  176. case 0x1ec: /* GPMC_PREFETCH_CONTROL */
  177. return s->prefcontrol;
  178. case 0x1f0: /* GPMC_PREFETCH_STATUS */
  179. return (s->preffifo << 24) |
  180. ((s->preffifo >
  181. ((s->prefconfig[0] >> 8) & 0x7f) ? 1 : 0) << 16) |
  182. s->prefcount;
  183. case 0x1f4: /* GPMC_ECC_CONFIG */
  184. return s->ecc_cs;
  185. case 0x1f8: /* GPMC_ECC_CONTROL */
  186. return s->ecc_ptr;
  187. case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
  188. return s->ecc_cfg;
  189. case 0x200 ... 0x220: /* GPMC_ECC_RESULT */
  190. cs = (addr & 0x1f) >> 2;
  191. /* TODO: check correctness */
  192. return
  193. ((s->ecc[cs].cp & 0x07) << 0) |
  194. ((s->ecc[cs].cp & 0x38) << 13) |
  195. ((s->ecc[cs].lp[0] & 0x1ff) << 3) |
  196. ((s->ecc[cs].lp[1] & 0x1ff) << 19);
  197. case 0x230: /* GPMC_TESTMODE_CTRL */
  198. return 0;
  199. case 0x234: /* GPMC_PSA_LSB */
  200. case 0x238: /* GPMC_PSA_MSB */
  201. return 0x00000000;
  202. }
  203. OMAP_BAD_REG(addr);
  204. return 0;
  205. }
  206. static void omap_gpmc_write(void *opaque, target_phys_addr_t addr,
  207. uint32_t value)
  208. {
  209. struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
  210. int cs;
  211. struct omap_gpmc_cs_file_s *f;
  212. switch (addr) {
  213. case 0x000: /* GPMC_REVISION */
  214. case 0x014: /* GPMC_SYSSTATUS */
  215. case 0x054: /* GPMC_STATUS */
  216. case 0x1f0: /* GPMC_PREFETCH_STATUS */
  217. case 0x200 ... 0x220: /* GPMC_ECC_RESULT */
  218. case 0x234: /* GPMC_PSA_LSB */
  219. case 0x238: /* GPMC_PSA_MSB */
  220. OMAP_RO_REG(addr);
  221. break;
  222. case 0x010: /* GPMC_SYSCONFIG */
  223. if ((value >> 3) == 0x3)
  224. fprintf(stderr, "%s: bad SDRAM idle mode %i\n",
  225. __FUNCTION__, value >> 3);
  226. if (value & 2)
  227. omap_gpmc_reset(s);
  228. s->sysconfig = value & 0x19;
  229. break;
  230. case 0x018: /* GPMC_IRQSTATUS */
  231. s->irqen = ~value;
  232. omap_gpmc_int_update(s);
  233. break;
  234. case 0x01c: /* GPMC_IRQENABLE */
  235. s->irqen = value & 0xf03;
  236. omap_gpmc_int_update(s);
  237. break;
  238. case 0x040: /* GPMC_TIMEOUT_CONTROL */
  239. s->timeout = value & 0x1ff1;
  240. break;
  241. case 0x044: /* GPMC_ERR_ADDRESS */
  242. case 0x048: /* GPMC_ERR_TYPE */
  243. break;
  244. case 0x050: /* GPMC_CONFIG */
  245. s->config = value & 0xf13;
  246. break;
  247. case 0x060 ... 0x1d4:
  248. cs = (addr - 0x060) / 0x30;
  249. addr -= cs * 0x30;
  250. f = s->cs_file + cs;
  251. switch (addr) {
  252. case 0x60: /* GPMC_CONFIG1 */
  253. f->config[0] = value & 0xffef3e13;
  254. break;
  255. case 0x64: /* GPMC_CONFIG2 */
  256. f->config[1] = value & 0x001f1f8f;
  257. break;
  258. case 0x68: /* GPMC_CONFIG3 */
  259. f->config[2] = value & 0x001f1f8f;
  260. break;
  261. case 0x6c: /* GPMC_CONFIG4 */
  262. f->config[3] = value & 0x1f8f1f8f;
  263. break;
  264. case 0x70: /* GPMC_CONFIG5 */
  265. f->config[4] = value & 0x0f1f1f1f;
  266. break;
  267. case 0x74: /* GPMC_CONFIG6 */
  268. f->config[5] = value & 0x00000fcf;
  269. break;
  270. case 0x78: /* GPMC_CONFIG7 */
  271. if ((f->config[6] ^ value) & 0xf7f) {
  272. if (f->config[6] & (1 << 6)) /* CSVALID */
  273. omap_gpmc_cs_unmap(f);
  274. if (value & (1 << 6)) /* CSVALID */
  275. omap_gpmc_cs_map(f, value & 0x1f, /* MASKADDR */
  276. (value >> 8 & 0xf)); /* BASEADDR */
  277. }
  278. f->config[6] = value & 0x00000f7f;
  279. break;
  280. case 0x7c: /* GPMC_NAND_COMMAND */
  281. case 0x80: /* GPMC_NAND_ADDRESS */
  282. case 0x84: /* GPMC_NAND_DATA */
  283. break;
  284. default:
  285. goto bad_reg;
  286. }
  287. break;
  288. case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
  289. s->prefconfig[0] = value & 0x7f8f7fbf;
  290. /* TODO: update interrupts, fifos, dmas */
  291. break;
  292. case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
  293. s->prefconfig[1] = value & 0x3fff;
  294. break;
  295. case 0x1ec: /* GPMC_PREFETCH_CONTROL */
  296. s->prefcontrol = value & 1;
  297. if (s->prefcontrol) {
  298. if (s->prefconfig[0] & 1)
  299. s->preffifo = 0x40;
  300. else
  301. s->preffifo = 0x00;
  302. }
  303. /* TODO: start */
  304. break;
  305. case 0x1f4: /* GPMC_ECC_CONFIG */
  306. s->ecc_cs = 0x8f;
  307. break;
  308. case 0x1f8: /* GPMC_ECC_CONTROL */
  309. if (value & (1 << 8))
  310. for (cs = 0; cs < 9; cs ++)
  311. ecc_reset(&s->ecc[cs]);
  312. s->ecc_ptr = value & 0xf;
  313. if (s->ecc_ptr == 0 || s->ecc_ptr > 9) {
  314. s->ecc_ptr = 0;
  315. s->ecc_cs &= ~1;
  316. }
  317. break;
  318. case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
  319. s->ecc_cfg = value & 0x3fcff1ff;
  320. break;
  321. case 0x230: /* GPMC_TESTMODE_CTRL */
  322. if (value & 7)
  323. fprintf(stderr, "%s: test mode enable attempt\n", __FUNCTION__);
  324. break;
  325. default:
  326. bad_reg:
  327. OMAP_BAD_REG(addr);
  328. return;
  329. }
  330. }
  331. static CPUReadMemoryFunc * const omap_gpmc_readfn[] = {
  332. omap_badwidth_read32, /* TODO */
  333. omap_badwidth_read32, /* TODO */
  334. omap_gpmc_read,
  335. };
  336. static CPUWriteMemoryFunc * const omap_gpmc_writefn[] = {
  337. omap_badwidth_write32, /* TODO */
  338. omap_badwidth_write32, /* TODO */
  339. omap_gpmc_write,
  340. };
  341. struct omap_gpmc_s *omap_gpmc_init(target_phys_addr_t base, qemu_irq irq)
  342. {
  343. int iomemtype;
  344. struct omap_gpmc_s *s = (struct omap_gpmc_s *)
  345. qemu_mallocz(sizeof(struct omap_gpmc_s));
  346. omap_gpmc_reset(s);
  347. iomemtype = cpu_register_io_memory(omap_gpmc_readfn,
  348. omap_gpmc_writefn, s, DEVICE_NATIVE_ENDIAN);
  349. cpu_register_physical_memory(base, 0x1000, iomemtype);
  350. return s;
  351. }
  352. void omap_gpmc_attach(struct omap_gpmc_s *s, int cs, int iomemtype,
  353. void (*base_upd)(void *opaque, target_phys_addr_t new),
  354. void (*unmap)(void *opaque), void *opaque)
  355. {
  356. struct omap_gpmc_cs_file_s *f;
  357. if (cs < 0 || cs >= 8) {
  358. fprintf(stderr, "%s: bad chip-select %i\n", __FUNCTION__, cs);
  359. exit(-1);
  360. }
  361. f = &s->cs_file[cs];
  362. f->iomemtype = iomemtype;
  363. f->base_update = base_upd;
  364. f->unmap = unmap;
  365. f->opaque = opaque;
  366. if (f->config[6] & (1 << 6)) /* CSVALID */
  367. omap_gpmc_cs_map(f, f->config[6] & 0x1f, /* MASKADDR */
  368. (f->config[6] >> 8 & 0xf)); /* BASEADDR */
  369. }