2
0

omap_gpmc.c 28 KB

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