xilinx_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * QEMU model of the Xilinx SPI Controller
  3. *
  4. * Copyright (C) 2010 Edgar E. Iglesias.
  5. * Copyright (C) 2012 Peter A. G. Crosthwaite <peter.crosthwaite@petalogix.com>
  6. * Copyright (C) 2012 PetaLogix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "qapi/error.h"
  28. #include "hw/sysbus.h"
  29. #include "migration/vmstate.h"
  30. #include "qemu/module.h"
  31. #include "qemu/fifo8.h"
  32. #include "hw/irq.h"
  33. #include "hw/qdev-properties.h"
  34. #include "hw/qdev-properties-system.h"
  35. #include "hw/ssi/ssi.h"
  36. #include "qom/object.h"
  37. #ifdef XILINX_SPI_ERR_DEBUG
  38. #define DB_PRINT(...) do { \
  39. fprintf(stderr, ": %s: ", __func__); \
  40. fprintf(stderr, ## __VA_ARGS__); \
  41. } while (0)
  42. #else
  43. #define DB_PRINT(...)
  44. #endif
  45. #define R_DGIER (0x1c / 4)
  46. #define R_DGIER_IE (1 << 31)
  47. #define R_IPISR (0x20 / 4)
  48. #define IRQ_DRR_NOT_EMPTY (1 << (31 - 23))
  49. #define IRQ_DRR_OVERRUN (1 << (31 - 26))
  50. #define IRQ_DRR_FULL (1 << (31 - 27))
  51. #define IRQ_TX_FF_HALF_EMPTY (1 << 6)
  52. #define IRQ_DTR_UNDERRUN (1 << 3)
  53. #define IRQ_DTR_EMPTY (1 << (31 - 29))
  54. #define R_IPIER (0x28 / 4)
  55. #define R_SRR (0x40 / 4)
  56. #define R_SPICR (0x60 / 4)
  57. #define R_SPICR_TXFF_RST (1 << 5)
  58. #define R_SPICR_RXFF_RST (1 << 6)
  59. #define R_SPICR_MTI (1 << 8)
  60. #define R_SPISR (0x64 / 4)
  61. #define SR_TX_FULL (1 << 3)
  62. #define SR_TX_EMPTY (1 << 2)
  63. #define SR_RX_FULL (1 << 1)
  64. #define SR_RX_EMPTY (1 << 0)
  65. #define R_SPIDTR (0x68 / 4)
  66. #define R_SPIDRR (0x6C / 4)
  67. #define R_SPISSR (0x70 / 4)
  68. #define R_TX_FF_OCY (0x74 / 4)
  69. #define R_RX_FF_OCY (0x78 / 4)
  70. #define R_MAX (0x7C / 4)
  71. #define FIFO_CAPACITY 256
  72. #define TYPE_XILINX_SPI "xlnx.xps-spi"
  73. OBJECT_DECLARE_SIMPLE_TYPE(XilinxSPI, XILINX_SPI)
  74. struct XilinxSPI {
  75. SysBusDevice parent_obj;
  76. EndianMode model_endianness;
  77. MemoryRegion mmio;
  78. qemu_irq irq;
  79. int irqline;
  80. uint8_t num_cs;
  81. qemu_irq *cs_lines;
  82. SSIBus *spi;
  83. Fifo8 rx_fifo;
  84. Fifo8 tx_fifo;
  85. uint32_t regs[R_MAX];
  86. };
  87. static void txfifo_reset(XilinxSPI *s)
  88. {
  89. fifo8_reset(&s->tx_fifo);
  90. s->regs[R_SPISR] &= ~SR_TX_FULL;
  91. s->regs[R_SPISR] |= SR_TX_EMPTY;
  92. }
  93. static void rxfifo_reset(XilinxSPI *s)
  94. {
  95. fifo8_reset(&s->rx_fifo);
  96. s->regs[R_SPISR] |= SR_RX_EMPTY;
  97. s->regs[R_SPISR] &= ~SR_RX_FULL;
  98. }
  99. static void xlx_spi_update_cs(XilinxSPI *s)
  100. {
  101. int i;
  102. for (i = 0; i < s->num_cs; ++i) {
  103. qemu_set_irq(s->cs_lines[i], !(~s->regs[R_SPISSR] & 1 << i));
  104. }
  105. }
  106. static void xlx_spi_update_irq(XilinxSPI *s)
  107. {
  108. uint32_t pending;
  109. s->regs[R_IPISR] |=
  110. (!fifo8_is_empty(&s->rx_fifo) ? IRQ_DRR_NOT_EMPTY : 0) |
  111. (fifo8_is_full(&s->rx_fifo) ? IRQ_DRR_FULL : 0);
  112. pending = s->regs[R_IPISR] & s->regs[R_IPIER];
  113. pending = pending && (s->regs[R_DGIER] & R_DGIER_IE);
  114. pending = !!pending;
  115. /* This call lies right in the data paths so don't call the
  116. irq chain unless things really changed. */
  117. if (pending != s->irqline) {
  118. s->irqline = pending;
  119. DB_PRINT("irq_change of state %u ISR:%x IER:%X\n",
  120. pending, s->regs[R_IPISR], s->regs[R_IPIER]);
  121. qemu_set_irq(s->irq, pending);
  122. }
  123. }
  124. static void xlx_spi_do_reset(XilinxSPI *s)
  125. {
  126. memset(s->regs, 0, sizeof s->regs);
  127. rxfifo_reset(s);
  128. txfifo_reset(s);
  129. s->regs[R_SPISSR] = ~0;
  130. s->regs[R_SPICR] = R_SPICR_MTI;
  131. xlx_spi_update_irq(s);
  132. xlx_spi_update_cs(s);
  133. }
  134. static void xlx_spi_reset(DeviceState *d)
  135. {
  136. xlx_spi_do_reset(XILINX_SPI(d));
  137. }
  138. static inline int spi_master_enabled(XilinxSPI *s)
  139. {
  140. return !(s->regs[R_SPICR] & R_SPICR_MTI);
  141. }
  142. static void spi_flush_txfifo(XilinxSPI *s)
  143. {
  144. uint32_t tx;
  145. uint32_t rx;
  146. while (!fifo8_is_empty(&s->tx_fifo)) {
  147. tx = (uint32_t)fifo8_pop(&s->tx_fifo);
  148. DB_PRINT("data tx:%x\n", tx);
  149. rx = ssi_transfer(s->spi, tx);
  150. DB_PRINT("data rx:%x\n", rx);
  151. if (fifo8_is_full(&s->rx_fifo)) {
  152. s->regs[R_IPISR] |= IRQ_DRR_OVERRUN;
  153. } else {
  154. fifo8_push(&s->rx_fifo, (uint8_t)rx);
  155. if (fifo8_is_full(&s->rx_fifo)) {
  156. s->regs[R_SPISR] |= SR_RX_FULL;
  157. s->regs[R_IPISR] |= IRQ_DRR_FULL;
  158. }
  159. }
  160. s->regs[R_SPISR] &= ~SR_RX_EMPTY;
  161. s->regs[R_SPISR] &= ~SR_TX_FULL;
  162. s->regs[R_SPISR] |= SR_TX_EMPTY;
  163. s->regs[R_IPISR] |= IRQ_DTR_EMPTY;
  164. s->regs[R_IPISR] |= IRQ_DRR_NOT_EMPTY;
  165. }
  166. }
  167. static uint64_t
  168. spi_read(void *opaque, hwaddr addr, unsigned int size)
  169. {
  170. XilinxSPI *s = opaque;
  171. uint32_t r = 0;
  172. addr >>= 2;
  173. switch (addr) {
  174. case R_SPIDRR:
  175. if (fifo8_is_empty(&s->rx_fifo)) {
  176. DB_PRINT("Read from empty FIFO!\n");
  177. return 0xdeadbeef;
  178. }
  179. s->regs[R_SPISR] &= ~SR_RX_FULL;
  180. r = fifo8_pop(&s->rx_fifo);
  181. if (fifo8_is_empty(&s->rx_fifo)) {
  182. s->regs[R_SPISR] |= SR_RX_EMPTY;
  183. }
  184. break;
  185. case R_SPISR:
  186. r = s->regs[addr];
  187. break;
  188. default:
  189. if (addr < ARRAY_SIZE(s->regs)) {
  190. r = s->regs[addr];
  191. }
  192. break;
  193. }
  194. DB_PRINT("addr=" HWADDR_FMT_plx " = %x\n", addr * 4, r);
  195. xlx_spi_update_irq(s);
  196. return r;
  197. }
  198. static void
  199. spi_write(void *opaque, hwaddr addr,
  200. uint64_t val64, unsigned int size)
  201. {
  202. XilinxSPI *s = opaque;
  203. uint32_t value = val64;
  204. DB_PRINT("addr=" HWADDR_FMT_plx " = %x\n", addr, value);
  205. addr >>= 2;
  206. switch (addr) {
  207. case R_SRR:
  208. if (value != 0xa) {
  209. DB_PRINT("Invalid write to SRR %x\n", value);
  210. } else {
  211. xlx_spi_do_reset(s);
  212. }
  213. break;
  214. case R_SPIDTR:
  215. s->regs[R_SPISR] &= ~SR_TX_EMPTY;
  216. fifo8_push(&s->tx_fifo, (uint8_t)value);
  217. if (fifo8_is_full(&s->tx_fifo)) {
  218. s->regs[R_SPISR] |= SR_TX_FULL;
  219. }
  220. if (!spi_master_enabled(s)) {
  221. goto done;
  222. } else {
  223. DB_PRINT("DTR and master enabled\n");
  224. }
  225. spi_flush_txfifo(s);
  226. break;
  227. case R_SPISR:
  228. DB_PRINT("Invalid write to SPISR %x\n", value);
  229. break;
  230. case R_IPISR:
  231. /* Toggle the bits. */
  232. s->regs[addr] ^= value;
  233. break;
  234. /* Slave Select Register. */
  235. case R_SPISSR:
  236. s->regs[addr] = value;
  237. xlx_spi_update_cs(s);
  238. break;
  239. case R_SPICR:
  240. /* FIXME: reset irq and sr state to empty queues. */
  241. if (value & R_SPICR_RXFF_RST) {
  242. rxfifo_reset(s);
  243. }
  244. if (value & R_SPICR_TXFF_RST) {
  245. txfifo_reset(s);
  246. }
  247. value &= ~(R_SPICR_RXFF_RST | R_SPICR_TXFF_RST);
  248. s->regs[addr] = value;
  249. if (!(value & R_SPICR_MTI)) {
  250. spi_flush_txfifo(s);
  251. }
  252. break;
  253. default:
  254. if (addr < ARRAY_SIZE(s->regs)) {
  255. s->regs[addr] = value;
  256. }
  257. break;
  258. }
  259. done:
  260. xlx_spi_update_irq(s);
  261. }
  262. static const MemoryRegionOps spi_ops[2] = {
  263. [0 ... 1] = {
  264. .read = spi_read,
  265. .write = spi_write,
  266. .valid = {
  267. .min_access_size = 4,
  268. .max_access_size = 4,
  269. },
  270. },
  271. [0].endianness = DEVICE_LITTLE_ENDIAN,
  272. [1].endianness = DEVICE_BIG_ENDIAN,
  273. };
  274. static void xilinx_spi_realize(DeviceState *dev, Error **errp)
  275. {
  276. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  277. XilinxSPI *s = XILINX_SPI(dev);
  278. int i;
  279. if (s->model_endianness == ENDIAN_MODE_UNSPECIFIED) {
  280. error_setg(errp, TYPE_XILINX_SPI " property 'endianness'"
  281. " must be set to 'big' or 'little'");
  282. return;
  283. }
  284. DB_PRINT("\n");
  285. s->spi = ssi_create_bus(dev, "spi");
  286. sysbus_init_irq(sbd, &s->irq);
  287. s->cs_lines = g_new0(qemu_irq, s->num_cs);
  288. for (i = 0; i < s->num_cs; ++i) {
  289. sysbus_init_irq(sbd, &s->cs_lines[i]);
  290. }
  291. memory_region_init_io(&s->mmio, OBJECT(s),
  292. &spi_ops[s->model_endianness == ENDIAN_MODE_BIG], s,
  293. "xilinx-spi", R_MAX * 4);
  294. sysbus_init_mmio(sbd, &s->mmio);
  295. s->irqline = -1;
  296. fifo8_create(&s->tx_fifo, FIFO_CAPACITY);
  297. fifo8_create(&s->rx_fifo, FIFO_CAPACITY);
  298. }
  299. static const VMStateDescription vmstate_xilinx_spi = {
  300. .name = "xilinx_spi",
  301. .version_id = 1,
  302. .minimum_version_id = 1,
  303. .fields = (const VMStateField[]) {
  304. VMSTATE_FIFO8(tx_fifo, XilinxSPI),
  305. VMSTATE_FIFO8(rx_fifo, XilinxSPI),
  306. VMSTATE_UINT32_ARRAY(regs, XilinxSPI, R_MAX),
  307. VMSTATE_END_OF_LIST()
  308. }
  309. };
  310. static const Property xilinx_spi_properties[] = {
  311. DEFINE_PROP_ENDIAN_NODEFAULT("endianness", XilinxSPI, model_endianness),
  312. DEFINE_PROP_UINT8("num-ss-bits", XilinxSPI, num_cs, 1),
  313. };
  314. static void xilinx_spi_class_init(ObjectClass *klass, void *data)
  315. {
  316. DeviceClass *dc = DEVICE_CLASS(klass);
  317. dc->realize = xilinx_spi_realize;
  318. device_class_set_legacy_reset(dc, xlx_spi_reset);
  319. device_class_set_props(dc, xilinx_spi_properties);
  320. dc->vmsd = &vmstate_xilinx_spi;
  321. }
  322. static const TypeInfo xilinx_spi_info = {
  323. .name = TYPE_XILINX_SPI,
  324. .parent = TYPE_SYS_BUS_DEVICE,
  325. .instance_size = sizeof(XilinxSPI),
  326. .class_init = xilinx_spi_class_init,
  327. };
  328. static void xilinx_spi_register_types(void)
  329. {
  330. type_register_static(&xilinx_spi_info);
  331. }
  332. type_init(xilinx_spi_register_types)