omap_spi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * TI OMAP processor's Multichannel SPI emulation.
  3. *
  4. * Copyright (C) 2007-2009 Nokia Corporation
  5. *
  6. * Original code for OMAP2 by Andrzej Zaborowski <andrew@openedhand.com>
  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, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include "hw.h"
  23. #include "omap.h"
  24. /* Multichannel SPI */
  25. struct omap_mcspi_s {
  26. MemoryRegion iomem;
  27. qemu_irq irq;
  28. int chnum;
  29. uint32_t sysconfig;
  30. uint32_t systest;
  31. uint32_t irqst;
  32. uint32_t irqen;
  33. uint32_t wken;
  34. uint32_t control;
  35. struct omap_mcspi_ch_s {
  36. qemu_irq txdrq;
  37. qemu_irq rxdrq;
  38. uint32_t (*txrx)(void *opaque, uint32_t, int);
  39. void *opaque;
  40. uint32_t tx;
  41. uint32_t rx;
  42. uint32_t config;
  43. uint32_t status;
  44. uint32_t control;
  45. } ch[4];
  46. };
  47. static inline void omap_mcspi_interrupt_update(struct omap_mcspi_s *s)
  48. {
  49. qemu_set_irq(s->irq, s->irqst & s->irqen);
  50. }
  51. static inline void omap_mcspi_dmarequest_update(struct omap_mcspi_ch_s *ch)
  52. {
  53. qemu_set_irq(ch->txdrq,
  54. (ch->control & 1) && /* EN */
  55. (ch->config & (1 << 14)) && /* DMAW */
  56. (ch->status & (1 << 1)) && /* TXS */
  57. ((ch->config >> 12) & 3) != 1); /* TRM */
  58. qemu_set_irq(ch->rxdrq,
  59. (ch->control & 1) && /* EN */
  60. (ch->config & (1 << 15)) && /* DMAW */
  61. (ch->status & (1 << 0)) && /* RXS */
  62. ((ch->config >> 12) & 3) != 2); /* TRM */
  63. }
  64. static void omap_mcspi_transfer_run(struct omap_mcspi_s *s, int chnum)
  65. {
  66. struct omap_mcspi_ch_s *ch = s->ch + chnum;
  67. if (!(ch->control & 1)) /* EN */
  68. return;
  69. if ((ch->status & (1 << 0)) && /* RXS */
  70. ((ch->config >> 12) & 3) != 2 && /* TRM */
  71. !(ch->config & (1 << 19))) /* TURBO */
  72. goto intr_update;
  73. if ((ch->status & (1 << 1)) && /* TXS */
  74. ((ch->config >> 12) & 3) != 1) /* TRM */
  75. goto intr_update;
  76. if (!(s->control & 1) || /* SINGLE */
  77. (ch->config & (1 << 20))) { /* FORCE */
  78. if (ch->txrx)
  79. ch->rx = ch->txrx(ch->opaque, ch->tx, /* WL */
  80. 1 + (0x1f & (ch->config >> 7)));
  81. }
  82. ch->tx = 0;
  83. ch->status |= 1 << 2; /* EOT */
  84. ch->status |= 1 << 1; /* TXS */
  85. if (((ch->config >> 12) & 3) != 2) /* TRM */
  86. ch->status |= 1 << 0; /* RXS */
  87. intr_update:
  88. if ((ch->status & (1 << 0)) && /* RXS */
  89. ((ch->config >> 12) & 3) != 2 && /* TRM */
  90. !(ch->config & (1 << 19))) /* TURBO */
  91. s->irqst |= 1 << (2 + 4 * chnum); /* RX_FULL */
  92. if ((ch->status & (1 << 1)) && /* TXS */
  93. ((ch->config >> 12) & 3) != 1) /* TRM */
  94. s->irqst |= 1 << (0 + 4 * chnum); /* TX_EMPTY */
  95. omap_mcspi_interrupt_update(s);
  96. omap_mcspi_dmarequest_update(ch);
  97. }
  98. void omap_mcspi_reset(struct omap_mcspi_s *s)
  99. {
  100. int ch;
  101. s->sysconfig = 0;
  102. s->systest = 0;
  103. s->irqst = 0;
  104. s->irqen = 0;
  105. s->wken = 0;
  106. s->control = 4;
  107. for (ch = 0; ch < 4; ch ++) {
  108. s->ch[ch].config = 0x060000;
  109. s->ch[ch].status = 2; /* TXS */
  110. s->ch[ch].control = 0;
  111. omap_mcspi_dmarequest_update(s->ch + ch);
  112. }
  113. omap_mcspi_interrupt_update(s);
  114. }
  115. static uint64_t omap_mcspi_read(void *opaque, target_phys_addr_t addr,
  116. unsigned size)
  117. {
  118. struct omap_mcspi_s *s = (struct omap_mcspi_s *) opaque;
  119. int ch = 0;
  120. uint32_t ret;
  121. if (size != 4) {
  122. return omap_badwidth_read32(opaque, addr);
  123. }
  124. switch (addr) {
  125. case 0x00: /* MCSPI_REVISION */
  126. return 0x91;
  127. case 0x10: /* MCSPI_SYSCONFIG */
  128. return s->sysconfig;
  129. case 0x14: /* MCSPI_SYSSTATUS */
  130. return 1; /* RESETDONE */
  131. case 0x18: /* MCSPI_IRQSTATUS */
  132. return s->irqst;
  133. case 0x1c: /* MCSPI_IRQENABLE */
  134. return s->irqen;
  135. case 0x20: /* MCSPI_WAKEUPENABLE */
  136. return s->wken;
  137. case 0x24: /* MCSPI_SYST */
  138. return s->systest;
  139. case 0x28: /* MCSPI_MODULCTRL */
  140. return s->control;
  141. case 0x68: ch ++;
  142. case 0x54: ch ++;
  143. case 0x40: ch ++;
  144. case 0x2c: /* MCSPI_CHCONF */
  145. return s->ch[ch].config;
  146. case 0x6c: ch ++;
  147. case 0x58: ch ++;
  148. case 0x44: ch ++;
  149. case 0x30: /* MCSPI_CHSTAT */
  150. return s->ch[ch].status;
  151. case 0x70: ch ++;
  152. case 0x5c: ch ++;
  153. case 0x48: ch ++;
  154. case 0x34: /* MCSPI_CHCTRL */
  155. return s->ch[ch].control;
  156. case 0x74: ch ++;
  157. case 0x60: ch ++;
  158. case 0x4c: ch ++;
  159. case 0x38: /* MCSPI_TX */
  160. return s->ch[ch].tx;
  161. case 0x78: ch ++;
  162. case 0x64: ch ++;
  163. case 0x50: ch ++;
  164. case 0x3c: /* MCSPI_RX */
  165. s->ch[ch].status &= ~(1 << 0); /* RXS */
  166. ret = s->ch[ch].rx;
  167. omap_mcspi_transfer_run(s, ch);
  168. return ret;
  169. }
  170. OMAP_BAD_REG(addr);
  171. return 0;
  172. }
  173. static void omap_mcspi_write(void *opaque, target_phys_addr_t addr,
  174. uint64_t value, unsigned size)
  175. {
  176. struct omap_mcspi_s *s = (struct omap_mcspi_s *) opaque;
  177. int ch = 0;
  178. if (size != 4) {
  179. return omap_badwidth_write32(opaque, addr, value);
  180. }
  181. switch (addr) {
  182. case 0x00: /* MCSPI_REVISION */
  183. case 0x14: /* MCSPI_SYSSTATUS */
  184. case 0x30: /* MCSPI_CHSTAT0 */
  185. case 0x3c: /* MCSPI_RX0 */
  186. case 0x44: /* MCSPI_CHSTAT1 */
  187. case 0x50: /* MCSPI_RX1 */
  188. case 0x58: /* MCSPI_CHSTAT2 */
  189. case 0x64: /* MCSPI_RX2 */
  190. case 0x6c: /* MCSPI_CHSTAT3 */
  191. case 0x78: /* MCSPI_RX3 */
  192. OMAP_RO_REG(addr);
  193. return;
  194. case 0x10: /* MCSPI_SYSCONFIG */
  195. if (value & (1 << 1)) /* SOFTRESET */
  196. omap_mcspi_reset(s);
  197. s->sysconfig = value & 0x31d;
  198. break;
  199. case 0x18: /* MCSPI_IRQSTATUS */
  200. if (!((s->control & (1 << 3)) && (s->systest & (1 << 11)))) {
  201. s->irqst &= ~value;
  202. omap_mcspi_interrupt_update(s);
  203. }
  204. break;
  205. case 0x1c: /* MCSPI_IRQENABLE */
  206. s->irqen = value & 0x1777f;
  207. omap_mcspi_interrupt_update(s);
  208. break;
  209. case 0x20: /* MCSPI_WAKEUPENABLE */
  210. s->wken = value & 1;
  211. break;
  212. case 0x24: /* MCSPI_SYST */
  213. if (s->control & (1 << 3)) /* SYSTEM_TEST */
  214. if (value & (1 << 11)) { /* SSB */
  215. s->irqst |= 0x1777f;
  216. omap_mcspi_interrupt_update(s);
  217. }
  218. s->systest = value & 0xfff;
  219. break;
  220. case 0x28: /* MCSPI_MODULCTRL */
  221. if (value & (1 << 3)) /* SYSTEM_TEST */
  222. if (s->systest & (1 << 11)) { /* SSB */
  223. s->irqst |= 0x1777f;
  224. omap_mcspi_interrupt_update(s);
  225. }
  226. s->control = value & 0xf;
  227. break;
  228. case 0x68: ch ++;
  229. case 0x54: ch ++;
  230. case 0x40: ch ++;
  231. case 0x2c: /* MCSPI_CHCONF */
  232. if ((value ^ s->ch[ch].config) & (3 << 14)) /* DMAR | DMAW */
  233. omap_mcspi_dmarequest_update(s->ch + ch);
  234. if (((value >> 12) & 3) == 3) /* TRM */
  235. fprintf(stderr, "%s: invalid TRM value (3)\n", __FUNCTION__);
  236. if (((value >> 7) & 0x1f) < 3) /* WL */
  237. fprintf(stderr, "%s: invalid WL value (%" PRIx64 ")\n",
  238. __FUNCTION__, (value >> 7) & 0x1f);
  239. s->ch[ch].config = value & 0x7fffff;
  240. break;
  241. case 0x70: ch ++;
  242. case 0x5c: ch ++;
  243. case 0x48: ch ++;
  244. case 0x34: /* MCSPI_CHCTRL */
  245. if (value & ~s->ch[ch].control & 1) { /* EN */
  246. s->ch[ch].control |= 1;
  247. omap_mcspi_transfer_run(s, ch);
  248. } else
  249. s->ch[ch].control = value & 1;
  250. break;
  251. case 0x74: ch ++;
  252. case 0x60: ch ++;
  253. case 0x4c: ch ++;
  254. case 0x38: /* MCSPI_TX */
  255. s->ch[ch].tx = value;
  256. s->ch[ch].status &= ~(1 << 1); /* TXS */
  257. omap_mcspi_transfer_run(s, ch);
  258. break;
  259. default:
  260. OMAP_BAD_REG(addr);
  261. return;
  262. }
  263. }
  264. static const MemoryRegionOps omap_mcspi_ops = {
  265. .read = omap_mcspi_read,
  266. .write = omap_mcspi_write,
  267. .endianness = DEVICE_NATIVE_ENDIAN,
  268. };
  269. struct omap_mcspi_s *omap_mcspi_init(struct omap_target_agent_s *ta, int chnum,
  270. qemu_irq irq, qemu_irq *drq, omap_clk fclk, omap_clk iclk)
  271. {
  272. struct omap_mcspi_s *s = (struct omap_mcspi_s *)
  273. g_malloc0(sizeof(struct omap_mcspi_s));
  274. struct omap_mcspi_ch_s *ch = s->ch;
  275. s->irq = irq;
  276. s->chnum = chnum;
  277. while (chnum --) {
  278. ch->txdrq = *drq ++;
  279. ch->rxdrq = *drq ++;
  280. ch ++;
  281. }
  282. omap_mcspi_reset(s);
  283. memory_region_init_io(&s->iomem, &omap_mcspi_ops, s, "omap.mcspi",
  284. omap_l4_region_size(ta, 0));
  285. omap_l4_attach(ta, 0, &s->iomem);
  286. return s;
  287. }
  288. void omap_mcspi_attach(struct omap_mcspi_s *s,
  289. uint32_t (*txrx)(void *opaque, uint32_t, int), void *opaque,
  290. int chipselect)
  291. {
  292. if (chipselect < 0 || chipselect >= s->chnum)
  293. hw_error("%s: Bad chipselect %i\n", __FUNCTION__, chipselect);
  294. s->ch[chipselect].txrx = txrx;
  295. s->ch[chipselect].opaque = opaque;
  296. }