2
0

omap_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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, hwaddr 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. /* fall through */
  143. case 0x54: ch ++;
  144. /* fall through */
  145. case 0x40: ch ++;
  146. /* fall through */
  147. case 0x2c: /* MCSPI_CHCONF */
  148. return s->ch[ch].config;
  149. case 0x6c: ch ++;
  150. /* fall through */
  151. case 0x58: ch ++;
  152. /* fall through */
  153. case 0x44: ch ++;
  154. /* fall through */
  155. case 0x30: /* MCSPI_CHSTAT */
  156. return s->ch[ch].status;
  157. case 0x70: ch ++;
  158. /* fall through */
  159. case 0x5c: ch ++;
  160. /* fall through */
  161. case 0x48: ch ++;
  162. /* fall through */
  163. case 0x34: /* MCSPI_CHCTRL */
  164. return s->ch[ch].control;
  165. case 0x74: ch ++;
  166. /* fall through */
  167. case 0x60: ch ++;
  168. /* fall through */
  169. case 0x4c: ch ++;
  170. /* fall through */
  171. case 0x38: /* MCSPI_TX */
  172. return s->ch[ch].tx;
  173. case 0x78: ch ++;
  174. /* fall through */
  175. case 0x64: ch ++;
  176. /* fall through */
  177. case 0x50: ch ++;
  178. /* fall through */
  179. case 0x3c: /* MCSPI_RX */
  180. s->ch[ch].status &= ~(1 << 0); /* RXS */
  181. ret = s->ch[ch].rx;
  182. omap_mcspi_transfer_run(s, ch);
  183. return ret;
  184. }
  185. OMAP_BAD_REG(addr);
  186. return 0;
  187. }
  188. static void omap_mcspi_write(void *opaque, hwaddr addr,
  189. uint64_t value, unsigned size)
  190. {
  191. struct omap_mcspi_s *s = (struct omap_mcspi_s *) opaque;
  192. int ch = 0;
  193. if (size != 4) {
  194. return omap_badwidth_write32(opaque, addr, value);
  195. }
  196. switch (addr) {
  197. case 0x00: /* MCSPI_REVISION */
  198. case 0x14: /* MCSPI_SYSSTATUS */
  199. case 0x30: /* MCSPI_CHSTAT0 */
  200. case 0x3c: /* MCSPI_RX0 */
  201. case 0x44: /* MCSPI_CHSTAT1 */
  202. case 0x50: /* MCSPI_RX1 */
  203. case 0x58: /* MCSPI_CHSTAT2 */
  204. case 0x64: /* MCSPI_RX2 */
  205. case 0x6c: /* MCSPI_CHSTAT3 */
  206. case 0x78: /* MCSPI_RX3 */
  207. OMAP_RO_REG(addr);
  208. return;
  209. case 0x10: /* MCSPI_SYSCONFIG */
  210. if (value & (1 << 1)) /* SOFTRESET */
  211. omap_mcspi_reset(s);
  212. s->sysconfig = value & 0x31d;
  213. break;
  214. case 0x18: /* MCSPI_IRQSTATUS */
  215. if (!((s->control & (1 << 3)) && (s->systest & (1 << 11)))) {
  216. s->irqst &= ~value;
  217. omap_mcspi_interrupt_update(s);
  218. }
  219. break;
  220. case 0x1c: /* MCSPI_IRQENABLE */
  221. s->irqen = value & 0x1777f;
  222. omap_mcspi_interrupt_update(s);
  223. break;
  224. case 0x20: /* MCSPI_WAKEUPENABLE */
  225. s->wken = value & 1;
  226. break;
  227. case 0x24: /* MCSPI_SYST */
  228. if (s->control & (1 << 3)) /* SYSTEM_TEST */
  229. if (value & (1 << 11)) { /* SSB */
  230. s->irqst |= 0x1777f;
  231. omap_mcspi_interrupt_update(s);
  232. }
  233. s->systest = value & 0xfff;
  234. break;
  235. case 0x28: /* MCSPI_MODULCTRL */
  236. if (value & (1 << 3)) /* SYSTEM_TEST */
  237. if (s->systest & (1 << 11)) { /* SSB */
  238. s->irqst |= 0x1777f;
  239. omap_mcspi_interrupt_update(s);
  240. }
  241. s->control = value & 0xf;
  242. break;
  243. case 0x68: ch ++;
  244. /* fall through */
  245. case 0x54: ch ++;
  246. /* fall through */
  247. case 0x40: ch ++;
  248. /* fall through */
  249. case 0x2c: /* MCSPI_CHCONF */
  250. if ((value ^ s->ch[ch].config) & (3 << 14)) /* DMAR | DMAW */
  251. omap_mcspi_dmarequest_update(s->ch + ch);
  252. if (((value >> 12) & 3) == 3) /* TRM */
  253. fprintf(stderr, "%s: invalid TRM value (3)\n", __FUNCTION__);
  254. if (((value >> 7) & 0x1f) < 3) /* WL */
  255. fprintf(stderr, "%s: invalid WL value (%" PRIx64 ")\n",
  256. __FUNCTION__, (value >> 7) & 0x1f);
  257. s->ch[ch].config = value & 0x7fffff;
  258. break;
  259. case 0x70: ch ++;
  260. /* fall through */
  261. case 0x5c: ch ++;
  262. /* fall through */
  263. case 0x48: ch ++;
  264. /* fall through */
  265. case 0x34: /* MCSPI_CHCTRL */
  266. if (value & ~s->ch[ch].control & 1) { /* EN */
  267. s->ch[ch].control |= 1;
  268. omap_mcspi_transfer_run(s, ch);
  269. } else
  270. s->ch[ch].control = value & 1;
  271. break;
  272. case 0x74: ch ++;
  273. /* fall through */
  274. case 0x60: ch ++;
  275. /* fall through */
  276. case 0x4c: ch ++;
  277. /* fall through */
  278. case 0x38: /* MCSPI_TX */
  279. s->ch[ch].tx = value;
  280. s->ch[ch].status &= ~(1 << 1); /* TXS */
  281. omap_mcspi_transfer_run(s, ch);
  282. break;
  283. default:
  284. OMAP_BAD_REG(addr);
  285. return;
  286. }
  287. }
  288. static const MemoryRegionOps omap_mcspi_ops = {
  289. .read = omap_mcspi_read,
  290. .write = omap_mcspi_write,
  291. .endianness = DEVICE_NATIVE_ENDIAN,
  292. };
  293. struct omap_mcspi_s *omap_mcspi_init(struct omap_target_agent_s *ta, int chnum,
  294. qemu_irq irq, qemu_irq *drq, omap_clk fclk, omap_clk iclk)
  295. {
  296. struct omap_mcspi_s *s = (struct omap_mcspi_s *)
  297. g_malloc0(sizeof(struct omap_mcspi_s));
  298. struct omap_mcspi_ch_s *ch = s->ch;
  299. s->irq = irq;
  300. s->chnum = chnum;
  301. while (chnum --) {
  302. ch->txdrq = *drq ++;
  303. ch->rxdrq = *drq ++;
  304. ch ++;
  305. }
  306. omap_mcspi_reset(s);
  307. memory_region_init_io(&s->iomem, &omap_mcspi_ops, s, "omap.mcspi",
  308. omap_l4_region_size(ta, 0));
  309. omap_l4_attach(ta, 0, &s->iomem);
  310. return s;
  311. }
  312. void omap_mcspi_attach(struct omap_mcspi_s *s,
  313. uint32_t (*txrx)(void *opaque, uint32_t, int), void *opaque,
  314. int chipselect)
  315. {
  316. if (chipselect < 0 || chipselect >= s->chnum)
  317. hw_error("%s: Bad chipselect %i\n", __FUNCTION__, chipselect);
  318. s->ch[chipselect].txrx = txrx;
  319. s->ch[chipselect].opaque = opaque;
  320. }