2
0

omap_spi.c 9.5 KB

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