2
0

omap_gpmc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. #include "exec/memory.h"
  25. #include "exec/address-spaces.h"
  26. /* General-Purpose Memory Controller */
  27. struct omap_gpmc_s {
  28. qemu_irq irq;
  29. qemu_irq drq;
  30. MemoryRegion iomem;
  31. int accept_256;
  32. uint8_t revision;
  33. uint8_t sysconfig;
  34. uint16_t irqst;
  35. uint16_t irqen;
  36. uint16_t lastirq;
  37. uint16_t timeout;
  38. uint16_t config;
  39. struct omap_gpmc_cs_file_s {
  40. uint32_t config[7];
  41. MemoryRegion *iomem;
  42. MemoryRegion container;
  43. MemoryRegion nandiomem;
  44. DeviceState *dev;
  45. } cs_file[8];
  46. int ecc_cs;
  47. int ecc_ptr;
  48. uint32_t ecc_cfg;
  49. ECCState ecc[9];
  50. struct prefetch {
  51. uint32_t config1; /* GPMC_PREFETCH_CONFIG1 */
  52. uint32_t transfercount; /* GPMC_PREFETCH_CONFIG2:TRANSFERCOUNT */
  53. int startengine; /* GPMC_PREFETCH_CONTROL:STARTENGINE */
  54. int fifopointer; /* GPMC_PREFETCH_STATUS:FIFOPOINTER */
  55. int count; /* GPMC_PREFETCH_STATUS:COUNTVALUE */
  56. MemoryRegion iomem;
  57. uint8_t fifo[64];
  58. } prefetch;
  59. };
  60. #define OMAP_GPMC_8BIT 0
  61. #define OMAP_GPMC_16BIT 1
  62. #define OMAP_GPMC_NOR 0
  63. #define OMAP_GPMC_NAND 2
  64. static int omap_gpmc_devtype(struct omap_gpmc_cs_file_s *f)
  65. {
  66. return (f->config[0] >> 10) & 3;
  67. }
  68. static int omap_gpmc_devsize(struct omap_gpmc_cs_file_s *f)
  69. {
  70. /* devsize field is really 2 bits but we ignore the high
  71. * bit to ensure consistent behaviour if the guest sets
  72. * it (values 2 and 3 are reserved in the TRM)
  73. */
  74. return (f->config[0] >> 12) & 1;
  75. }
  76. /* Extract the chip-select value from the prefetch config1 register */
  77. static int prefetch_cs(uint32_t config1)
  78. {
  79. return (config1 >> 24) & 7;
  80. }
  81. static int prefetch_threshold(uint32_t config1)
  82. {
  83. return (config1 >> 8) & 0x7f;
  84. }
  85. static void omap_gpmc_int_update(struct omap_gpmc_s *s)
  86. {
  87. /* The TRM is a bit unclear, but it seems to say that
  88. * the TERMINALCOUNTSTATUS bit is set only on the
  89. * transition when the prefetch engine goes from
  90. * active to inactive, whereas the FIFOEVENTSTATUS
  91. * bit is held high as long as the fifo has at
  92. * least THRESHOLD bytes available.
  93. * So we do the latter here, but TERMINALCOUNTSTATUS
  94. * is set elsewhere.
  95. */
  96. if (s->prefetch.fifopointer >= prefetch_threshold(s->prefetch.config1)) {
  97. s->irqst |= 1;
  98. }
  99. if ((s->irqen & s->irqst) != s->lastirq) {
  100. s->lastirq = s->irqen & s->irqst;
  101. qemu_set_irq(s->irq, s->lastirq);
  102. }
  103. }
  104. static void omap_gpmc_dma_update(struct omap_gpmc_s *s, int value)
  105. {
  106. if (s->prefetch.config1 & 4) {
  107. qemu_set_irq(s->drq, value);
  108. }
  109. }
  110. /* Access functions for when a NAND-like device is mapped into memory:
  111. * all addresses in the region behave like accesses to the relevant
  112. * GPMC_NAND_DATA_i register (which is actually implemented to call these)
  113. */
  114. static uint64_t omap_nand_read(void *opaque, hwaddr addr,
  115. unsigned size)
  116. {
  117. struct omap_gpmc_cs_file_s *f = (struct omap_gpmc_cs_file_s *)opaque;
  118. uint64_t v;
  119. nand_setpins(f->dev, 0, 0, 0, 1, 0);
  120. switch (omap_gpmc_devsize(f)) {
  121. case OMAP_GPMC_8BIT:
  122. v = nand_getio(f->dev);
  123. if (size == 1) {
  124. return v;
  125. }
  126. v |= (nand_getio(f->dev) << 8);
  127. if (size == 2) {
  128. return v;
  129. }
  130. v |= (nand_getio(f->dev) << 16);
  131. v |= (nand_getio(f->dev) << 24);
  132. return v;
  133. case OMAP_GPMC_16BIT:
  134. v = nand_getio(f->dev);
  135. if (size == 1) {
  136. /* 8 bit read from 16 bit device : probably a guest bug */
  137. return v & 0xff;
  138. }
  139. if (size == 2) {
  140. return v;
  141. }
  142. v |= (nand_getio(f->dev) << 16);
  143. return v;
  144. default:
  145. abort();
  146. }
  147. }
  148. static void omap_nand_setio(DeviceState *dev, uint64_t value,
  149. int nandsize, int size)
  150. {
  151. /* Write the specified value to the NAND device, respecting
  152. * both size of the NAND device and size of the write access.
  153. */
  154. switch (nandsize) {
  155. case OMAP_GPMC_8BIT:
  156. switch (size) {
  157. case 1:
  158. nand_setio(dev, value & 0xff);
  159. break;
  160. case 2:
  161. nand_setio(dev, value & 0xff);
  162. nand_setio(dev, (value >> 8) & 0xff);
  163. break;
  164. case 4:
  165. default:
  166. nand_setio(dev, value & 0xff);
  167. nand_setio(dev, (value >> 8) & 0xff);
  168. nand_setio(dev, (value >> 16) & 0xff);
  169. nand_setio(dev, (value >> 24) & 0xff);
  170. break;
  171. }
  172. break;
  173. case OMAP_GPMC_16BIT:
  174. switch (size) {
  175. case 1:
  176. /* writing to a 16bit device with 8bit access is probably a guest
  177. * bug; pass the value through anyway.
  178. */
  179. case 2:
  180. nand_setio(dev, value & 0xffff);
  181. break;
  182. case 4:
  183. default:
  184. nand_setio(dev, value & 0xffff);
  185. nand_setio(dev, (value >> 16) & 0xffff);
  186. break;
  187. }
  188. break;
  189. }
  190. }
  191. static void omap_nand_write(void *opaque, hwaddr addr,
  192. uint64_t value, unsigned size)
  193. {
  194. struct omap_gpmc_cs_file_s *f = (struct omap_gpmc_cs_file_s *)opaque;
  195. nand_setpins(f->dev, 0, 0, 0, 1, 0);
  196. omap_nand_setio(f->dev, value, omap_gpmc_devsize(f), size);
  197. }
  198. static const MemoryRegionOps omap_nand_ops = {
  199. .read = omap_nand_read,
  200. .write = omap_nand_write,
  201. .endianness = DEVICE_NATIVE_ENDIAN,
  202. };
  203. static void fill_prefetch_fifo(struct omap_gpmc_s *s)
  204. {
  205. /* Fill the prefetch FIFO by reading data from NAND.
  206. * We do this synchronously, unlike the hardware which
  207. * will do this asynchronously. We refill when the
  208. * FIFO has THRESHOLD bytes free, and we always refill
  209. * as much data as possible starting at the top end
  210. * of the FIFO.
  211. * (We have to refill at THRESHOLD rather than waiting
  212. * for the FIFO to empty to allow for the case where
  213. * the FIFO size isn't an exact multiple of THRESHOLD
  214. * and we're doing DMA transfers.)
  215. * This means we never need to handle wrap-around in
  216. * the fifo-reading code, and the next byte of data
  217. * to read is always fifo[63 - fifopointer].
  218. */
  219. int fptr;
  220. int cs = prefetch_cs(s->prefetch.config1);
  221. int is16bit = (((s->cs_file[cs].config[0] >> 12) & 3) != 0);
  222. int bytes;
  223. /* Don't believe the bit of the OMAP TRM that says that COUNTVALUE
  224. * and TRANSFERCOUNT are in units of 16 bit words for 16 bit NAND.
  225. * Instead believe the bit that says it is always a byte count.
  226. */
  227. bytes = 64 - s->prefetch.fifopointer;
  228. if (bytes > s->prefetch.count) {
  229. bytes = s->prefetch.count;
  230. }
  231. s->prefetch.count -= bytes;
  232. s->prefetch.fifopointer += bytes;
  233. fptr = 64 - s->prefetch.fifopointer;
  234. /* Move the existing data in the FIFO so it sits just
  235. * before what we're about to read in
  236. */
  237. while (fptr < (64 - bytes)) {
  238. s->prefetch.fifo[fptr] = s->prefetch.fifo[fptr + bytes];
  239. fptr++;
  240. }
  241. while (fptr < 64) {
  242. if (is16bit) {
  243. uint32_t v = omap_nand_read(&s->cs_file[cs], 0, 2);
  244. s->prefetch.fifo[fptr++] = v & 0xff;
  245. s->prefetch.fifo[fptr++] = (v >> 8) & 0xff;
  246. } else {
  247. s->prefetch.fifo[fptr++] = omap_nand_read(&s->cs_file[cs], 0, 1);
  248. }
  249. }
  250. if (s->prefetch.startengine && (s->prefetch.count == 0)) {
  251. /* This was the final transfer: raise TERMINALCOUNTSTATUS */
  252. s->irqst |= 2;
  253. s->prefetch.startengine = 0;
  254. }
  255. /* If there are any bytes in the FIFO at this point then
  256. * we must raise a DMA request (either this is a final part
  257. * transfer, or we filled the FIFO in which case we certainly
  258. * have THRESHOLD bytes available)
  259. */
  260. if (s->prefetch.fifopointer != 0) {
  261. omap_gpmc_dma_update(s, 1);
  262. }
  263. omap_gpmc_int_update(s);
  264. }
  265. /* Access functions for a NAND-like device when the prefetch/postwrite
  266. * engine is enabled -- all addresses in the region behave alike:
  267. * data is read or written to the FIFO.
  268. */
  269. static uint64_t omap_gpmc_prefetch_read(void *opaque, hwaddr addr,
  270. unsigned size)
  271. {
  272. struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
  273. uint32_t data;
  274. if (s->prefetch.config1 & 1) {
  275. /* The TRM doesn't define the behaviour if you read from the
  276. * FIFO when the prefetch engine is in write mode. We choose
  277. * to always return zero.
  278. */
  279. return 0;
  280. }
  281. /* Note that trying to read an empty fifo repeats the last byte */
  282. if (s->prefetch.fifopointer) {
  283. s->prefetch.fifopointer--;
  284. }
  285. data = s->prefetch.fifo[63 - s->prefetch.fifopointer];
  286. if (s->prefetch.fifopointer ==
  287. (64 - prefetch_threshold(s->prefetch.config1))) {
  288. /* We've drained THRESHOLD bytes now. So deassert the
  289. * DMA request, then refill the FIFO (which will probably
  290. * assert it again.)
  291. */
  292. omap_gpmc_dma_update(s, 0);
  293. fill_prefetch_fifo(s);
  294. }
  295. omap_gpmc_int_update(s);
  296. return data;
  297. }
  298. static void omap_gpmc_prefetch_write(void *opaque, hwaddr addr,
  299. uint64_t value, unsigned size)
  300. {
  301. struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
  302. int cs = prefetch_cs(s->prefetch.config1);
  303. if ((s->prefetch.config1 & 1) == 0) {
  304. /* The TRM doesn't define the behaviour of writing to the
  305. * FIFO when the prefetch engine is in read mode. We
  306. * choose to ignore the write.
  307. */
  308. return;
  309. }
  310. if (s->prefetch.count == 0) {
  311. /* The TRM doesn't define the behaviour of writing to the
  312. * FIFO if the transfer is complete. We choose to ignore.
  313. */
  314. return;
  315. }
  316. /* The only reason we do any data buffering in postwrite
  317. * mode is if we are talking to a 16 bit NAND device, in
  318. * which case we need to buffer the first byte of the
  319. * 16 bit word until the other byte arrives.
  320. */
  321. int is16bit = (((s->cs_file[cs].config[0] >> 12) & 3) != 0);
  322. if (is16bit) {
  323. /* fifopointer alternates between 64 (waiting for first
  324. * byte of word) and 63 (waiting for second byte)
  325. */
  326. if (s->prefetch.fifopointer == 64) {
  327. s->prefetch.fifo[0] = value;
  328. s->prefetch.fifopointer--;
  329. } else {
  330. value = (value << 8) | s->prefetch.fifo[0];
  331. omap_nand_write(&s->cs_file[cs], 0, value, 2);
  332. s->prefetch.count--;
  333. s->prefetch.fifopointer = 64;
  334. }
  335. } else {
  336. /* Just write the byte : fifopointer remains 64 at all times */
  337. omap_nand_write(&s->cs_file[cs], 0, value, 1);
  338. s->prefetch.count--;
  339. }
  340. if (s->prefetch.count == 0) {
  341. /* Final transfer: raise TERMINALCOUNTSTATUS */
  342. s->irqst |= 2;
  343. s->prefetch.startengine = 0;
  344. }
  345. omap_gpmc_int_update(s);
  346. }
  347. static const MemoryRegionOps omap_prefetch_ops = {
  348. .read = omap_gpmc_prefetch_read,
  349. .write = omap_gpmc_prefetch_write,
  350. .endianness = DEVICE_NATIVE_ENDIAN,
  351. .impl.min_access_size = 1,
  352. .impl.max_access_size = 1,
  353. };
  354. static MemoryRegion *omap_gpmc_cs_memregion(struct omap_gpmc_s *s, int cs)
  355. {
  356. /* Return the MemoryRegion* to map/unmap for this chipselect */
  357. struct omap_gpmc_cs_file_s *f = &s->cs_file[cs];
  358. if (omap_gpmc_devtype(f) == OMAP_GPMC_NOR) {
  359. return f->iomem;
  360. }
  361. if ((s->prefetch.config1 & 0x80) &&
  362. (prefetch_cs(s->prefetch.config1) == cs)) {
  363. /* The prefetch engine is enabled for this CS: map the FIFO */
  364. return &s->prefetch.iomem;
  365. }
  366. return &f->nandiomem;
  367. }
  368. static void omap_gpmc_cs_map(struct omap_gpmc_s *s, int cs)
  369. {
  370. struct omap_gpmc_cs_file_s *f = &s->cs_file[cs];
  371. uint32_t mask = (f->config[6] >> 8) & 0xf;
  372. uint32_t base = f->config[6] & 0x3f;
  373. uint32_t size;
  374. if (!f->iomem && !f->dev) {
  375. return;
  376. }
  377. if (!(f->config[6] & (1 << 6))) {
  378. /* Do nothing unless CSVALID */
  379. return;
  380. }
  381. /* TODO: check for overlapping regions and report access errors */
  382. if (mask != 0x8 && mask != 0xc && mask != 0xe && mask != 0xf
  383. && !(s->accept_256 && !mask)) {
  384. fprintf(stderr, "%s: invalid chip-select mask address (0x%x)\n",
  385. __func__, mask);
  386. }
  387. base <<= 24;
  388. size = (0x0fffffff & ~(mask << 24)) + 1;
  389. /* TODO: rather than setting the size of the mapping (which should be
  390. * constant), the mask should cause wrapping of the address space, so
  391. * that the same memory becomes accessible at every <i>size</i> bytes
  392. * starting from <i>base</i>. */
  393. memory_region_init(&f->container, "omap-gpmc-file", size);
  394. memory_region_add_subregion(&f->container, 0,
  395. omap_gpmc_cs_memregion(s, cs));
  396. memory_region_add_subregion(get_system_memory(), base,
  397. &f->container);
  398. }
  399. static void omap_gpmc_cs_unmap(struct omap_gpmc_s *s, int cs)
  400. {
  401. struct omap_gpmc_cs_file_s *f = &s->cs_file[cs];
  402. if (!(f->config[6] & (1 << 6))) {
  403. /* Do nothing unless CSVALID */
  404. return;
  405. }
  406. if (!f->iomem && !f->dev) {
  407. return;
  408. }
  409. memory_region_del_subregion(get_system_memory(), &f->container);
  410. memory_region_del_subregion(&f->container, omap_gpmc_cs_memregion(s, cs));
  411. memory_region_destroy(&f->container);
  412. }
  413. void omap_gpmc_reset(struct omap_gpmc_s *s)
  414. {
  415. int i;
  416. s->sysconfig = 0;
  417. s->irqst = 0;
  418. s->irqen = 0;
  419. omap_gpmc_int_update(s);
  420. for (i = 0; i < 8; i++) {
  421. /* This has to happen before we change any of the config
  422. * used to determine which memory regions are mapped or unmapped.
  423. */
  424. omap_gpmc_cs_unmap(s, i);
  425. }
  426. s->timeout = 0;
  427. s->config = 0xa00;
  428. s->prefetch.config1 = 0x00004000;
  429. s->prefetch.transfercount = 0x00000000;
  430. s->prefetch.startengine = 0;
  431. s->prefetch.fifopointer = 0;
  432. s->prefetch.count = 0;
  433. for (i = 0; i < 8; i ++) {
  434. s->cs_file[i].config[1] = 0x101001;
  435. s->cs_file[i].config[2] = 0x020201;
  436. s->cs_file[i].config[3] = 0x10031003;
  437. s->cs_file[i].config[4] = 0x10f1111;
  438. s->cs_file[i].config[5] = 0;
  439. s->cs_file[i].config[6] = 0xf00 | (i ? 0 : 1 << 6);
  440. s->cs_file[i].config[6] = 0xf00;
  441. /* In theory we could probe attached devices for some CFG1
  442. * bits here, but we just retain them across resets as they
  443. * were set initially by omap_gpmc_attach().
  444. */
  445. if (i == 0) {
  446. s->cs_file[i].config[0] &= 0x00433e00;
  447. s->cs_file[i].config[6] |= 1 << 6; /* CSVALID */
  448. omap_gpmc_cs_map(s, i);
  449. } else {
  450. s->cs_file[i].config[0] &= 0x00403c00;
  451. }
  452. }
  453. s->ecc_cs = 0;
  454. s->ecc_ptr = 0;
  455. s->ecc_cfg = 0x3fcff000;
  456. for (i = 0; i < 9; i ++)
  457. ecc_reset(&s->ecc[i]);
  458. }
  459. static int gpmc_wordaccess_only(hwaddr addr)
  460. {
  461. /* Return true if the register offset is to a register that
  462. * only permits word width accesses.
  463. * Non-word accesses are only OK for GPMC_NAND_DATA/ADDRESS/COMMAND
  464. * for any chipselect.
  465. */
  466. if (addr >= 0x60 && addr <= 0x1d4) {
  467. int cs = (addr - 0x60) / 0x30;
  468. addr -= cs * 0x30;
  469. if (addr >= 0x7c && addr < 0x88) {
  470. /* GPMC_NAND_COMMAND, GPMC_NAND_ADDRESS, GPMC_NAND_DATA */
  471. return 0;
  472. }
  473. }
  474. return 1;
  475. }
  476. static uint64_t omap_gpmc_read(void *opaque, hwaddr addr,
  477. unsigned size)
  478. {
  479. struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
  480. int cs;
  481. struct omap_gpmc_cs_file_s *f;
  482. if (size != 4 && gpmc_wordaccess_only(addr)) {
  483. return omap_badwidth_read32(opaque, addr);
  484. }
  485. switch (addr) {
  486. case 0x000: /* GPMC_REVISION */
  487. return s->revision;
  488. case 0x010: /* GPMC_SYSCONFIG */
  489. return s->sysconfig;
  490. case 0x014: /* GPMC_SYSSTATUS */
  491. return 1; /* RESETDONE */
  492. case 0x018: /* GPMC_IRQSTATUS */
  493. return s->irqst;
  494. case 0x01c: /* GPMC_IRQENABLE */
  495. return s->irqen;
  496. case 0x040: /* GPMC_TIMEOUT_CONTROL */
  497. return s->timeout;
  498. case 0x044: /* GPMC_ERR_ADDRESS */
  499. case 0x048: /* GPMC_ERR_TYPE */
  500. return 0;
  501. case 0x050: /* GPMC_CONFIG */
  502. return s->config;
  503. case 0x054: /* GPMC_STATUS */
  504. return 0x001;
  505. case 0x060 ... 0x1d4:
  506. cs = (addr - 0x060) / 0x30;
  507. addr -= cs * 0x30;
  508. f = s->cs_file + cs;
  509. switch (addr) {
  510. case 0x60: /* GPMC_CONFIG1 */
  511. return f->config[0];
  512. case 0x64: /* GPMC_CONFIG2 */
  513. return f->config[1];
  514. case 0x68: /* GPMC_CONFIG3 */
  515. return f->config[2];
  516. case 0x6c: /* GPMC_CONFIG4 */
  517. return f->config[3];
  518. case 0x70: /* GPMC_CONFIG5 */
  519. return f->config[4];
  520. case 0x74: /* GPMC_CONFIG6 */
  521. return f->config[5];
  522. case 0x78: /* GPMC_CONFIG7 */
  523. return f->config[6];
  524. case 0x84 ... 0x87: /* GPMC_NAND_DATA */
  525. if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) {
  526. return omap_nand_read(f, 0, size);
  527. }
  528. return 0;
  529. }
  530. break;
  531. case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
  532. return s->prefetch.config1;
  533. case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
  534. return s->prefetch.transfercount;
  535. case 0x1ec: /* GPMC_PREFETCH_CONTROL */
  536. return s->prefetch.startengine;
  537. case 0x1f0: /* GPMC_PREFETCH_STATUS */
  538. /* NB: The OMAP3 TRM is inconsistent about whether the GPMC
  539. * FIFOTHRESHOLDSTATUS bit should be set when
  540. * FIFOPOINTER > FIFOTHRESHOLD or when it is >= FIFOTHRESHOLD.
  541. * Apparently the underlying functional spec from which the TRM was
  542. * created states that the behaviour is ">=", and this also
  543. * makes more conceptual sense.
  544. */
  545. return (s->prefetch.fifopointer << 24) |
  546. ((s->prefetch.fifopointer >=
  547. ((s->prefetch.config1 >> 8) & 0x7f) ? 1 : 0) << 16) |
  548. s->prefetch.count;
  549. case 0x1f4: /* GPMC_ECC_CONFIG */
  550. return s->ecc_cs;
  551. case 0x1f8: /* GPMC_ECC_CONTROL */
  552. return s->ecc_ptr;
  553. case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
  554. return s->ecc_cfg;
  555. case 0x200 ... 0x220: /* GPMC_ECC_RESULT */
  556. cs = (addr & 0x1f) >> 2;
  557. /* TODO: check correctness */
  558. return
  559. ((s->ecc[cs].cp & 0x07) << 0) |
  560. ((s->ecc[cs].cp & 0x38) << 13) |
  561. ((s->ecc[cs].lp[0] & 0x1ff) << 3) |
  562. ((s->ecc[cs].lp[1] & 0x1ff) << 19);
  563. case 0x230: /* GPMC_TESTMODE_CTRL */
  564. return 0;
  565. case 0x234: /* GPMC_PSA_LSB */
  566. case 0x238: /* GPMC_PSA_MSB */
  567. return 0x00000000;
  568. }
  569. OMAP_BAD_REG(addr);
  570. return 0;
  571. }
  572. static void omap_gpmc_write(void *opaque, hwaddr addr,
  573. uint64_t value, unsigned size)
  574. {
  575. struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
  576. int cs;
  577. struct omap_gpmc_cs_file_s *f;
  578. if (size != 4 && gpmc_wordaccess_only(addr)) {
  579. return omap_badwidth_write32(opaque, addr, value);
  580. }
  581. switch (addr) {
  582. case 0x000: /* GPMC_REVISION */
  583. case 0x014: /* GPMC_SYSSTATUS */
  584. case 0x054: /* GPMC_STATUS */
  585. case 0x1f0: /* GPMC_PREFETCH_STATUS */
  586. case 0x200 ... 0x220: /* GPMC_ECC_RESULT */
  587. case 0x234: /* GPMC_PSA_LSB */
  588. case 0x238: /* GPMC_PSA_MSB */
  589. OMAP_RO_REG(addr);
  590. break;
  591. case 0x010: /* GPMC_SYSCONFIG */
  592. if ((value >> 3) == 0x3)
  593. fprintf(stderr, "%s: bad SDRAM idle mode %"PRIi64"\n",
  594. __FUNCTION__, value >> 3);
  595. if (value & 2)
  596. omap_gpmc_reset(s);
  597. s->sysconfig = value & 0x19;
  598. break;
  599. case 0x018: /* GPMC_IRQSTATUS */
  600. s->irqst &= ~value;
  601. omap_gpmc_int_update(s);
  602. break;
  603. case 0x01c: /* GPMC_IRQENABLE */
  604. s->irqen = value & 0xf03;
  605. omap_gpmc_int_update(s);
  606. break;
  607. case 0x040: /* GPMC_TIMEOUT_CONTROL */
  608. s->timeout = value & 0x1ff1;
  609. break;
  610. case 0x044: /* GPMC_ERR_ADDRESS */
  611. case 0x048: /* GPMC_ERR_TYPE */
  612. break;
  613. case 0x050: /* GPMC_CONFIG */
  614. s->config = value & 0xf13;
  615. break;
  616. case 0x060 ... 0x1d4:
  617. cs = (addr - 0x060) / 0x30;
  618. addr -= cs * 0x30;
  619. f = s->cs_file + cs;
  620. switch (addr) {
  621. case 0x60: /* GPMC_CONFIG1 */
  622. f->config[0] = value & 0xffef3e13;
  623. break;
  624. case 0x64: /* GPMC_CONFIG2 */
  625. f->config[1] = value & 0x001f1f8f;
  626. break;
  627. case 0x68: /* GPMC_CONFIG3 */
  628. f->config[2] = value & 0x001f1f8f;
  629. break;
  630. case 0x6c: /* GPMC_CONFIG4 */
  631. f->config[3] = value & 0x1f8f1f8f;
  632. break;
  633. case 0x70: /* GPMC_CONFIG5 */
  634. f->config[4] = value & 0x0f1f1f1f;
  635. break;
  636. case 0x74: /* GPMC_CONFIG6 */
  637. f->config[5] = value & 0x00000fcf;
  638. break;
  639. case 0x78: /* GPMC_CONFIG7 */
  640. if ((f->config[6] ^ value) & 0xf7f) {
  641. omap_gpmc_cs_unmap(s, cs);
  642. f->config[6] = value & 0x00000f7f;
  643. omap_gpmc_cs_map(s, cs);
  644. }
  645. break;
  646. case 0x7c ... 0x7f: /* GPMC_NAND_COMMAND */
  647. if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) {
  648. nand_setpins(f->dev, 1, 0, 0, 1, 0); /* CLE */
  649. omap_nand_setio(f->dev, value, omap_gpmc_devsize(f), size);
  650. }
  651. break;
  652. case 0x80 ... 0x83: /* GPMC_NAND_ADDRESS */
  653. if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) {
  654. nand_setpins(f->dev, 0, 1, 0, 1, 0); /* ALE */
  655. omap_nand_setio(f->dev, value, omap_gpmc_devsize(f), size);
  656. }
  657. break;
  658. case 0x84 ... 0x87: /* GPMC_NAND_DATA */
  659. if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) {
  660. omap_nand_write(f, 0, value, size);
  661. }
  662. break;
  663. default:
  664. goto bad_reg;
  665. }
  666. break;
  667. case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
  668. if (!s->prefetch.startengine) {
  669. uint32_t newconfig1 = value & 0x7f8f7fbf;
  670. uint32_t changed;
  671. changed = newconfig1 ^ s->prefetch.config1;
  672. if (changed & (0x80 | 0x7000000)) {
  673. /* Turning the engine on or off, or mapping it somewhere else.
  674. * cs_map() and cs_unmap() check the prefetch config and
  675. * overall CSVALID bits, so it is sufficient to unmap-and-map
  676. * both the old cs and the new one. Note that we adhere to
  677. * the "unmap/change config/map" order (and not unmap twice
  678. * if newcs == oldcs), otherwise we'll try to delete the wrong
  679. * memory region.
  680. */
  681. int oldcs = prefetch_cs(s->prefetch.config1);
  682. int newcs = prefetch_cs(newconfig1);
  683. omap_gpmc_cs_unmap(s, oldcs);
  684. if (oldcs != newcs) {
  685. omap_gpmc_cs_unmap(s, newcs);
  686. }
  687. s->prefetch.config1 = newconfig1;
  688. omap_gpmc_cs_map(s, oldcs);
  689. if (oldcs != newcs) {
  690. omap_gpmc_cs_map(s, newcs);
  691. }
  692. } else {
  693. s->prefetch.config1 = newconfig1;
  694. }
  695. }
  696. break;
  697. case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
  698. if (!s->prefetch.startengine) {
  699. s->prefetch.transfercount = value & 0x3fff;
  700. }
  701. break;
  702. case 0x1ec: /* GPMC_PREFETCH_CONTROL */
  703. if (s->prefetch.startengine != (value & 1)) {
  704. s->prefetch.startengine = value & 1;
  705. if (s->prefetch.startengine) {
  706. /* Prefetch engine start */
  707. s->prefetch.count = s->prefetch.transfercount;
  708. if (s->prefetch.config1 & 1) {
  709. /* Write */
  710. s->prefetch.fifopointer = 64;
  711. } else {
  712. /* Read */
  713. s->prefetch.fifopointer = 0;
  714. fill_prefetch_fifo(s);
  715. }
  716. } else {
  717. /* Prefetch engine forcibly stopped. The TRM
  718. * doesn't define the behaviour if you do this.
  719. * We clear the prefetch count, which means that
  720. * we permit no more writes, and don't read any
  721. * more data from NAND. The CPU can still drain
  722. * the FIFO of unread data.
  723. */
  724. s->prefetch.count = 0;
  725. }
  726. omap_gpmc_int_update(s);
  727. }
  728. break;
  729. case 0x1f4: /* GPMC_ECC_CONFIG */
  730. s->ecc_cs = 0x8f;
  731. break;
  732. case 0x1f8: /* GPMC_ECC_CONTROL */
  733. if (value & (1 << 8))
  734. for (cs = 0; cs < 9; cs ++)
  735. ecc_reset(&s->ecc[cs]);
  736. s->ecc_ptr = value & 0xf;
  737. if (s->ecc_ptr == 0 || s->ecc_ptr > 9) {
  738. s->ecc_ptr = 0;
  739. s->ecc_cs &= ~1;
  740. }
  741. break;
  742. case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
  743. s->ecc_cfg = value & 0x3fcff1ff;
  744. break;
  745. case 0x230: /* GPMC_TESTMODE_CTRL */
  746. if (value & 7)
  747. fprintf(stderr, "%s: test mode enable attempt\n", __FUNCTION__);
  748. break;
  749. default:
  750. bad_reg:
  751. OMAP_BAD_REG(addr);
  752. return;
  753. }
  754. }
  755. static const MemoryRegionOps omap_gpmc_ops = {
  756. .read = omap_gpmc_read,
  757. .write = omap_gpmc_write,
  758. .endianness = DEVICE_NATIVE_ENDIAN,
  759. };
  760. struct omap_gpmc_s *omap_gpmc_init(struct omap_mpu_state_s *mpu,
  761. hwaddr base,
  762. qemu_irq irq, qemu_irq drq)
  763. {
  764. int cs;
  765. struct omap_gpmc_s *s = (struct omap_gpmc_s *)
  766. g_malloc0(sizeof(struct omap_gpmc_s));
  767. memory_region_init_io(&s->iomem, &omap_gpmc_ops, s, "omap-gpmc", 0x1000);
  768. memory_region_add_subregion(get_system_memory(), base, &s->iomem);
  769. s->irq = irq;
  770. s->drq = drq;
  771. s->accept_256 = cpu_is_omap3630(mpu);
  772. s->revision = cpu_class_omap3(mpu) ? 0x50 : 0x20;
  773. s->lastirq = 0;
  774. omap_gpmc_reset(s);
  775. /* We have to register a different IO memory handler for each
  776. * chip select region in case a NAND device is mapped there. We
  777. * make the region the worst-case size of 256MB and rely on the
  778. * container memory region in cs_map to chop it down to the actual
  779. * guest-requested size.
  780. */
  781. for (cs = 0; cs < 8; cs++) {
  782. memory_region_init_io(&s->cs_file[cs].nandiomem,
  783. &omap_nand_ops,
  784. &s->cs_file[cs],
  785. "omap-nand",
  786. 256 * 1024 * 1024);
  787. }
  788. memory_region_init_io(&s->prefetch.iomem, &omap_prefetch_ops, s,
  789. "omap-gpmc-prefetch", 256 * 1024 * 1024);
  790. return s;
  791. }
  792. void omap_gpmc_attach(struct omap_gpmc_s *s, int cs, MemoryRegion *iomem)
  793. {
  794. struct omap_gpmc_cs_file_s *f;
  795. assert(iomem);
  796. if (cs < 0 || cs >= 8) {
  797. fprintf(stderr, "%s: bad chip-select %i\n", __FUNCTION__, cs);
  798. exit(-1);
  799. }
  800. f = &s->cs_file[cs];
  801. omap_gpmc_cs_unmap(s, cs);
  802. f->config[0] &= ~(0xf << 10);
  803. f->iomem = iomem;
  804. omap_gpmc_cs_map(s, cs);
  805. }
  806. void omap_gpmc_attach_nand(struct omap_gpmc_s *s, int cs, DeviceState *nand)
  807. {
  808. struct omap_gpmc_cs_file_s *f;
  809. assert(nand);
  810. if (cs < 0 || cs >= 8) {
  811. fprintf(stderr, "%s: bad chip-select %i\n", __func__, cs);
  812. exit(-1);
  813. }
  814. f = &s->cs_file[cs];
  815. omap_gpmc_cs_unmap(s, cs);
  816. f->config[0] &= ~(0xf << 10);
  817. f->config[0] |= (OMAP_GPMC_NAND << 10);
  818. f->dev = nand;
  819. if (nand_getbuswidth(f->dev) == 16) {
  820. f->config[0] |= OMAP_GPMC_16BIT << 12;
  821. }
  822. omap_gpmc_cs_map(s, cs);
  823. }