escc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. * QEMU ESCC (Z8030/Z8530/Z85C30/SCC/ESCC) serial port emulation
  3. *
  4. * Copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "hw/irq.h"
  26. #include "hw/qdev-properties.h"
  27. #include "hw/sysbus.h"
  28. #include "migration/vmstate.h"
  29. #include "qemu/module.h"
  30. #include "hw/char/escc.h"
  31. #include "ui/console.h"
  32. #include "trace.h"
  33. /*
  34. * Chipset docs:
  35. * "Z80C30/Z85C30/Z80230/Z85230/Z85233 SCC/ESCC User Manual",
  36. * http://www.zilog.com/docs/serial/scc_escc_um.pdf
  37. *
  38. * On Sparc32 this is the serial port, mouse and keyboard part of chip STP2001
  39. * (Slave I/O), also produced as NCR89C105. See
  40. * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
  41. *
  42. * The serial ports implement full AMD AM8530 or Zilog Z8530 chips,
  43. * mouse and keyboard ports don't implement all functions and they are
  44. * only asynchronous. There is no DMA.
  45. *
  46. * Z85C30 is also used on PowerMacs and m68k Macs.
  47. *
  48. * There are some small differences between Sparc version (sunzilog)
  49. * and PowerMac (pmac):
  50. * Offset between control and data registers
  51. * There is some kind of lockup bug, but we can ignore it
  52. * CTS is inverted
  53. * DMA on pmac using DBDMA chip
  54. * pmac can do IRDA and faster rates, sunzilog can only do 38400
  55. * pmac baud rate generator clock is 3.6864 MHz, sunzilog 4.9152 MHz
  56. *
  57. * Linux driver for m68k Macs is the same as for PowerMac (pmac_zilog),
  58. * but registers are grouped by type and not by channel:
  59. * channel is selected by bit 0 of the address (instead of bit 1)
  60. * and register is selected by bit 1 of the address (instead of bit 0).
  61. */
  62. /*
  63. * Modifications:
  64. * 2006-Aug-10 Igor Kovalenko : Renamed KBDQueue to SERIOQueue, implemented
  65. * serial mouse queue.
  66. * Implemented serial mouse protocol.
  67. *
  68. * 2010-May-23 Artyom Tarasenko: Reworked IUS logic
  69. */
  70. #define CHN_C(s) ((s)->chn == escc_chn_b ? 'b' : 'a')
  71. #define SERIAL_CTRL 0
  72. #define SERIAL_DATA 1
  73. #define W_CMD 0
  74. #define CMD_PTR_MASK 0x07
  75. #define CMD_CMD_MASK 0x38
  76. #define CMD_HI 0x08
  77. #define CMD_CLR_TXINT 0x28
  78. #define CMD_CLR_IUS 0x38
  79. #define W_INTR 1
  80. #define INTR_INTALL 0x01
  81. #define INTR_TXINT 0x02
  82. #define INTR_RXMODEMSK 0x18
  83. #define INTR_RXINT1ST 0x08
  84. #define INTR_RXINTALL 0x10
  85. #define W_IVEC 2
  86. #define W_RXCTRL 3
  87. #define RXCTRL_RXEN 0x01
  88. #define W_TXCTRL1 4
  89. #define TXCTRL1_PAREN 0x01
  90. #define TXCTRL1_PAREV 0x02
  91. #define TXCTRL1_1STOP 0x04
  92. #define TXCTRL1_1HSTOP 0x08
  93. #define TXCTRL1_2STOP 0x0c
  94. #define TXCTRL1_STPMSK 0x0c
  95. #define TXCTRL1_CLK1X 0x00
  96. #define TXCTRL1_CLK16X 0x40
  97. #define TXCTRL1_CLK32X 0x80
  98. #define TXCTRL1_CLK64X 0xc0
  99. #define TXCTRL1_CLKMSK 0xc0
  100. #define W_TXCTRL2 5
  101. #define TXCTRL2_TXEN 0x08
  102. #define TXCTRL2_BITMSK 0x60
  103. #define TXCTRL2_5BITS 0x00
  104. #define TXCTRL2_7BITS 0x20
  105. #define TXCTRL2_6BITS 0x40
  106. #define TXCTRL2_8BITS 0x60
  107. #define W_SYNC1 6
  108. #define W_SYNC2 7
  109. #define W_TXBUF 8
  110. #define W_MINTR 9
  111. #define MINTR_STATUSHI 0x10
  112. #define MINTR_RST_MASK 0xc0
  113. #define MINTR_RST_B 0x40
  114. #define MINTR_RST_A 0x80
  115. #define MINTR_RST_ALL 0xc0
  116. #define W_MISC1 10
  117. #define W_CLOCK 11
  118. #define CLOCK_TRXC 0x08
  119. #define W_BRGLO 12
  120. #define W_BRGHI 13
  121. #define W_MISC2 14
  122. #define MISC2_PLLDIS 0x30
  123. #define W_EXTINT 15
  124. #define EXTINT_DCD 0x08
  125. #define EXTINT_SYNCINT 0x10
  126. #define EXTINT_CTSINT 0x20
  127. #define EXTINT_TXUNDRN 0x40
  128. #define EXTINT_BRKINT 0x80
  129. #define R_STATUS 0
  130. #define STATUS_RXAV 0x01
  131. #define STATUS_ZERO 0x02
  132. #define STATUS_TXEMPTY 0x04
  133. #define STATUS_DCD 0x08
  134. #define STATUS_SYNC 0x10
  135. #define STATUS_CTS 0x20
  136. #define STATUS_TXUNDRN 0x40
  137. #define STATUS_BRK 0x80
  138. #define R_SPEC 1
  139. #define SPEC_ALLSENT 0x01
  140. #define SPEC_BITS8 0x06
  141. #define R_IVEC 2
  142. #define IVEC_TXINTB 0x00
  143. #define IVEC_LONOINT 0x06
  144. #define IVEC_LORXINTA 0x0c
  145. #define IVEC_LORXINTB 0x04
  146. #define IVEC_LOTXINTA 0x08
  147. #define IVEC_HINOINT 0x60
  148. #define IVEC_HIRXINTA 0x30
  149. #define IVEC_HIRXINTB 0x20
  150. #define IVEC_HITXINTA 0x10
  151. #define R_INTR 3
  152. #define INTR_EXTINTB 0x01
  153. #define INTR_TXINTB 0x02
  154. #define INTR_RXINTB 0x04
  155. #define INTR_EXTINTA 0x08
  156. #define INTR_TXINTA 0x10
  157. #define INTR_RXINTA 0x20
  158. #define R_IPEN 4
  159. #define R_TXCTRL1 5
  160. #define R_TXCTRL2 6
  161. #define R_BC 7
  162. #define R_RXBUF 8
  163. #define R_RXCTRL 9
  164. #define R_MISC 10
  165. #define R_MISC1 11
  166. #define R_BRGLO 12
  167. #define R_BRGHI 13
  168. #define R_MISC1I 14
  169. #define R_EXTINT 15
  170. static void handle_kbd_command(ESCCChannelState *s, int val);
  171. static int serial_can_receive(void *opaque);
  172. static void serial_receive_byte(ESCCChannelState *s, int ch);
  173. static int reg_shift(ESCCState *s)
  174. {
  175. return s->bit_swap ? s->it_shift + 1 : s->it_shift;
  176. }
  177. static int chn_shift(ESCCState *s)
  178. {
  179. return s->bit_swap ? s->it_shift : s->it_shift + 1;
  180. }
  181. static void clear_queue(void *opaque)
  182. {
  183. ESCCChannelState *s = opaque;
  184. ESCCSERIOQueue *q = &s->queue;
  185. q->rptr = q->wptr = q->count = 0;
  186. }
  187. static void put_queue(void *opaque, int b)
  188. {
  189. ESCCChannelState *s = opaque;
  190. ESCCSERIOQueue *q = &s->queue;
  191. trace_escc_put_queue(CHN_C(s), b);
  192. if (q->count >= ESCC_SERIO_QUEUE_SIZE) {
  193. return;
  194. }
  195. q->data[q->wptr] = b;
  196. if (++q->wptr == ESCC_SERIO_QUEUE_SIZE) {
  197. q->wptr = 0;
  198. }
  199. q->count++;
  200. serial_receive_byte(s, 0);
  201. }
  202. static uint32_t get_queue(void *opaque)
  203. {
  204. ESCCChannelState *s = opaque;
  205. ESCCSERIOQueue *q = &s->queue;
  206. int val;
  207. if (q->count == 0) {
  208. return 0;
  209. } else {
  210. val = q->data[q->rptr];
  211. if (++q->rptr == ESCC_SERIO_QUEUE_SIZE) {
  212. q->rptr = 0;
  213. }
  214. q->count--;
  215. }
  216. trace_escc_get_queue(CHN_C(s), val);
  217. if (q->count > 0)
  218. serial_receive_byte(s, 0);
  219. return val;
  220. }
  221. static int escc_update_irq_chn(ESCCChannelState *s)
  222. {
  223. if ((((s->wregs[W_INTR] & INTR_TXINT) && (s->txint == 1)) ||
  224. // tx ints enabled, pending
  225. ((((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINT1ST) ||
  226. ((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINTALL)) &&
  227. s->rxint == 1) || // rx ints enabled, pending
  228. ((s->wregs[W_EXTINT] & EXTINT_BRKINT) &&
  229. (s->rregs[R_STATUS] & STATUS_BRK)))) { // break int e&p
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. static void escc_update_irq(ESCCChannelState *s)
  235. {
  236. int irq;
  237. irq = escc_update_irq_chn(s);
  238. irq |= escc_update_irq_chn(s->otherchn);
  239. trace_escc_update_irq(irq);
  240. qemu_set_irq(s->irq, irq);
  241. }
  242. static void escc_reset_chn(ESCCChannelState *s)
  243. {
  244. int i;
  245. s->reg = 0;
  246. for (i = 0; i < ESCC_SERIAL_REGS; i++) {
  247. s->rregs[i] = 0;
  248. s->wregs[i] = 0;
  249. }
  250. s->wregs[W_TXCTRL1] = TXCTRL1_1STOP; // 1X divisor, 1 stop bit, no parity
  251. s->wregs[W_MINTR] = MINTR_RST_ALL;
  252. s->wregs[W_CLOCK] = CLOCK_TRXC; // Synch mode tx clock = TRxC
  253. s->wregs[W_MISC2] = MISC2_PLLDIS; // PLL disabled
  254. s->wregs[W_EXTINT] = EXTINT_DCD | EXTINT_SYNCINT | EXTINT_CTSINT |
  255. EXTINT_TXUNDRN | EXTINT_BRKINT; // Enable most interrupts
  256. if (s->disabled)
  257. s->rregs[R_STATUS] = STATUS_TXEMPTY | STATUS_DCD | STATUS_SYNC |
  258. STATUS_CTS | STATUS_TXUNDRN;
  259. else
  260. s->rregs[R_STATUS] = STATUS_TXEMPTY | STATUS_TXUNDRN;
  261. s->rregs[R_SPEC] = SPEC_BITS8 | SPEC_ALLSENT;
  262. s->rx = s->tx = 0;
  263. s->rxint = s->txint = 0;
  264. s->rxint_under_svc = s->txint_under_svc = 0;
  265. s->e0_mode = s->led_mode = s->caps_lock_mode = s->num_lock_mode = 0;
  266. clear_queue(s);
  267. }
  268. static void escc_reset(DeviceState *d)
  269. {
  270. ESCCState *s = ESCC(d);
  271. escc_reset_chn(&s->chn[0]);
  272. escc_reset_chn(&s->chn[1]);
  273. }
  274. static inline void set_rxint(ESCCChannelState *s)
  275. {
  276. s->rxint = 1;
  277. /* XXX: missing daisy chainnig: escc_chn_b rx should have a lower priority
  278. than chn_a rx/tx/special_condition service*/
  279. s->rxint_under_svc = 1;
  280. if (s->chn == escc_chn_a) {
  281. s->rregs[R_INTR] |= INTR_RXINTA;
  282. if (s->wregs[W_MINTR] & MINTR_STATUSHI)
  283. s->otherchn->rregs[R_IVEC] = IVEC_HIRXINTA;
  284. else
  285. s->otherchn->rregs[R_IVEC] = IVEC_LORXINTA;
  286. } else {
  287. s->otherchn->rregs[R_INTR] |= INTR_RXINTB;
  288. if (s->wregs[W_MINTR] & MINTR_STATUSHI)
  289. s->rregs[R_IVEC] = IVEC_HIRXINTB;
  290. else
  291. s->rregs[R_IVEC] = IVEC_LORXINTB;
  292. }
  293. escc_update_irq(s);
  294. }
  295. static inline void set_txint(ESCCChannelState *s)
  296. {
  297. s->txint = 1;
  298. if (!s->rxint_under_svc) {
  299. s->txint_under_svc = 1;
  300. if (s->chn == escc_chn_a) {
  301. if (s->wregs[W_INTR] & INTR_TXINT) {
  302. s->rregs[R_INTR] |= INTR_TXINTA;
  303. }
  304. if (s->wregs[W_MINTR] & MINTR_STATUSHI)
  305. s->otherchn->rregs[R_IVEC] = IVEC_HITXINTA;
  306. else
  307. s->otherchn->rregs[R_IVEC] = IVEC_LOTXINTA;
  308. } else {
  309. s->rregs[R_IVEC] = IVEC_TXINTB;
  310. if (s->wregs[W_INTR] & INTR_TXINT) {
  311. s->otherchn->rregs[R_INTR] |= INTR_TXINTB;
  312. }
  313. }
  314. escc_update_irq(s);
  315. }
  316. }
  317. static inline void clr_rxint(ESCCChannelState *s)
  318. {
  319. s->rxint = 0;
  320. s->rxint_under_svc = 0;
  321. if (s->chn == escc_chn_a) {
  322. if (s->wregs[W_MINTR] & MINTR_STATUSHI)
  323. s->otherchn->rregs[R_IVEC] = IVEC_HINOINT;
  324. else
  325. s->otherchn->rregs[R_IVEC] = IVEC_LONOINT;
  326. s->rregs[R_INTR] &= ~INTR_RXINTA;
  327. } else {
  328. if (s->wregs[W_MINTR] & MINTR_STATUSHI)
  329. s->rregs[R_IVEC] = IVEC_HINOINT;
  330. else
  331. s->rregs[R_IVEC] = IVEC_LONOINT;
  332. s->otherchn->rregs[R_INTR] &= ~INTR_RXINTB;
  333. }
  334. if (s->txint)
  335. set_txint(s);
  336. escc_update_irq(s);
  337. }
  338. static inline void clr_txint(ESCCChannelState *s)
  339. {
  340. s->txint = 0;
  341. s->txint_under_svc = 0;
  342. if (s->chn == escc_chn_a) {
  343. if (s->wregs[W_MINTR] & MINTR_STATUSHI)
  344. s->otherchn->rregs[R_IVEC] = IVEC_HINOINT;
  345. else
  346. s->otherchn->rregs[R_IVEC] = IVEC_LONOINT;
  347. s->rregs[R_INTR] &= ~INTR_TXINTA;
  348. } else {
  349. s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB;
  350. if (s->wregs[W_MINTR] & MINTR_STATUSHI)
  351. s->rregs[R_IVEC] = IVEC_HINOINT;
  352. else
  353. s->rregs[R_IVEC] = IVEC_LONOINT;
  354. s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB;
  355. }
  356. if (s->rxint)
  357. set_rxint(s);
  358. escc_update_irq(s);
  359. }
  360. static void escc_update_parameters(ESCCChannelState *s)
  361. {
  362. int speed, parity, data_bits, stop_bits;
  363. QEMUSerialSetParams ssp;
  364. if (!qemu_chr_fe_backend_connected(&s->chr) || s->type != escc_serial)
  365. return;
  366. if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREN) {
  367. if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREV)
  368. parity = 'E';
  369. else
  370. parity = 'O';
  371. } else {
  372. parity = 'N';
  373. }
  374. if ((s->wregs[W_TXCTRL1] & TXCTRL1_STPMSK) == TXCTRL1_2STOP)
  375. stop_bits = 2;
  376. else
  377. stop_bits = 1;
  378. switch (s->wregs[W_TXCTRL2] & TXCTRL2_BITMSK) {
  379. case TXCTRL2_5BITS:
  380. data_bits = 5;
  381. break;
  382. case TXCTRL2_7BITS:
  383. data_bits = 7;
  384. break;
  385. case TXCTRL2_6BITS:
  386. data_bits = 6;
  387. break;
  388. default:
  389. case TXCTRL2_8BITS:
  390. data_bits = 8;
  391. break;
  392. }
  393. speed = s->clock / ((s->wregs[W_BRGLO] | (s->wregs[W_BRGHI] << 8)) + 2);
  394. switch (s->wregs[W_TXCTRL1] & TXCTRL1_CLKMSK) {
  395. case TXCTRL1_CLK1X:
  396. break;
  397. case TXCTRL1_CLK16X:
  398. speed /= 16;
  399. break;
  400. case TXCTRL1_CLK32X:
  401. speed /= 32;
  402. break;
  403. default:
  404. case TXCTRL1_CLK64X:
  405. speed /= 64;
  406. break;
  407. }
  408. ssp.speed = speed;
  409. ssp.parity = parity;
  410. ssp.data_bits = data_bits;
  411. ssp.stop_bits = stop_bits;
  412. trace_escc_update_parameters(CHN_C(s), speed, parity, data_bits, stop_bits);
  413. qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
  414. }
  415. static void escc_mem_write(void *opaque, hwaddr addr,
  416. uint64_t val, unsigned size)
  417. {
  418. ESCCState *serial = opaque;
  419. ESCCChannelState *s;
  420. uint32_t saddr;
  421. int newreg, channel;
  422. val &= 0xff;
  423. saddr = (addr >> reg_shift(serial)) & 1;
  424. channel = (addr >> chn_shift(serial)) & 1;
  425. s = &serial->chn[channel];
  426. switch (saddr) {
  427. case SERIAL_CTRL:
  428. trace_escc_mem_writeb_ctrl(CHN_C(s), s->reg, val & 0xff);
  429. newreg = 0;
  430. switch (s->reg) {
  431. case W_CMD:
  432. newreg = val & CMD_PTR_MASK;
  433. val &= CMD_CMD_MASK;
  434. switch (val) {
  435. case CMD_HI:
  436. newreg |= CMD_HI;
  437. break;
  438. case CMD_CLR_TXINT:
  439. clr_txint(s);
  440. break;
  441. case CMD_CLR_IUS:
  442. if (s->rxint_under_svc) {
  443. s->rxint_under_svc = 0;
  444. if (s->txint) {
  445. set_txint(s);
  446. }
  447. } else if (s->txint_under_svc) {
  448. s->txint_under_svc = 0;
  449. }
  450. escc_update_irq(s);
  451. break;
  452. default:
  453. break;
  454. }
  455. break;
  456. case W_INTR ... W_RXCTRL:
  457. case W_SYNC1 ... W_TXBUF:
  458. case W_MISC1 ... W_CLOCK:
  459. case W_MISC2 ... W_EXTINT:
  460. s->wregs[s->reg] = val;
  461. break;
  462. case W_TXCTRL1:
  463. case W_TXCTRL2:
  464. s->wregs[s->reg] = val;
  465. escc_update_parameters(s);
  466. break;
  467. case W_BRGLO:
  468. case W_BRGHI:
  469. s->wregs[s->reg] = val;
  470. s->rregs[s->reg] = val;
  471. escc_update_parameters(s);
  472. break;
  473. case W_MINTR:
  474. switch (val & MINTR_RST_MASK) {
  475. case 0:
  476. default:
  477. break;
  478. case MINTR_RST_B:
  479. escc_reset_chn(&serial->chn[0]);
  480. return;
  481. case MINTR_RST_A:
  482. escc_reset_chn(&serial->chn[1]);
  483. return;
  484. case MINTR_RST_ALL:
  485. escc_reset(DEVICE(serial));
  486. return;
  487. }
  488. break;
  489. default:
  490. break;
  491. }
  492. if (s->reg == 0)
  493. s->reg = newreg;
  494. else
  495. s->reg = 0;
  496. break;
  497. case SERIAL_DATA:
  498. trace_escc_mem_writeb_data(CHN_C(s), val);
  499. /*
  500. * Lower the irq when data is written to the Tx buffer and no other
  501. * interrupts are currently pending. The irq will be raised again once
  502. * the Tx buffer becomes empty below.
  503. */
  504. s->txint = 0;
  505. escc_update_irq(s);
  506. s->tx = val;
  507. if (s->wregs[W_TXCTRL2] & TXCTRL2_TXEN) { // tx enabled
  508. if (qemu_chr_fe_backend_connected(&s->chr)) {
  509. /* XXX this blocks entire thread. Rewrite to use
  510. * qemu_chr_fe_write and background I/O callbacks */
  511. qemu_chr_fe_write_all(&s->chr, &s->tx, 1);
  512. } else if (s->type == escc_kbd && !s->disabled) {
  513. handle_kbd_command(s, val);
  514. }
  515. }
  516. s->rregs[R_STATUS] |= STATUS_TXEMPTY; // Tx buffer empty
  517. s->rregs[R_SPEC] |= SPEC_ALLSENT; // All sent
  518. set_txint(s);
  519. break;
  520. default:
  521. break;
  522. }
  523. }
  524. static uint64_t escc_mem_read(void *opaque, hwaddr addr,
  525. unsigned size)
  526. {
  527. ESCCState *serial = opaque;
  528. ESCCChannelState *s;
  529. uint32_t saddr;
  530. uint32_t ret;
  531. int channel;
  532. saddr = (addr >> reg_shift(serial)) & 1;
  533. channel = (addr >> chn_shift(serial)) & 1;
  534. s = &serial->chn[channel];
  535. switch (saddr) {
  536. case SERIAL_CTRL:
  537. trace_escc_mem_readb_ctrl(CHN_C(s), s->reg, s->rregs[s->reg]);
  538. ret = s->rregs[s->reg];
  539. s->reg = 0;
  540. return ret;
  541. case SERIAL_DATA:
  542. s->rregs[R_STATUS] &= ~STATUS_RXAV;
  543. clr_rxint(s);
  544. if (s->type == escc_kbd || s->type == escc_mouse) {
  545. ret = get_queue(s);
  546. } else {
  547. ret = s->rx;
  548. }
  549. trace_escc_mem_readb_data(CHN_C(s), ret);
  550. qemu_chr_fe_accept_input(&s->chr);
  551. return ret;
  552. default:
  553. break;
  554. }
  555. return 0;
  556. }
  557. static const MemoryRegionOps escc_mem_ops = {
  558. .read = escc_mem_read,
  559. .write = escc_mem_write,
  560. .endianness = DEVICE_NATIVE_ENDIAN,
  561. .valid = {
  562. .min_access_size = 1,
  563. .max_access_size = 1,
  564. },
  565. };
  566. static int serial_can_receive(void *opaque)
  567. {
  568. ESCCChannelState *s = opaque;
  569. int ret;
  570. if (((s->wregs[W_RXCTRL] & RXCTRL_RXEN) == 0) // Rx not enabled
  571. || ((s->rregs[R_STATUS] & STATUS_RXAV) == STATUS_RXAV))
  572. // char already available
  573. ret = 0;
  574. else
  575. ret = 1;
  576. return ret;
  577. }
  578. static void serial_receive_byte(ESCCChannelState *s, int ch)
  579. {
  580. trace_escc_serial_receive_byte(CHN_C(s), ch);
  581. s->rregs[R_STATUS] |= STATUS_RXAV;
  582. s->rx = ch;
  583. set_rxint(s);
  584. }
  585. static void serial_receive_break(ESCCChannelState *s)
  586. {
  587. s->rregs[R_STATUS] |= STATUS_BRK;
  588. escc_update_irq(s);
  589. }
  590. static void serial_receive1(void *opaque, const uint8_t *buf, int size)
  591. {
  592. ESCCChannelState *s = opaque;
  593. serial_receive_byte(s, buf[0]);
  594. }
  595. static void serial_event(void *opaque, int event)
  596. {
  597. ESCCChannelState *s = opaque;
  598. if (event == CHR_EVENT_BREAK)
  599. serial_receive_break(s);
  600. }
  601. static const VMStateDescription vmstate_escc_chn = {
  602. .name ="escc_chn",
  603. .version_id = 2,
  604. .minimum_version_id = 1,
  605. .fields = (VMStateField[]) {
  606. VMSTATE_UINT32(vmstate_dummy, ESCCChannelState),
  607. VMSTATE_UINT32(reg, ESCCChannelState),
  608. VMSTATE_UINT32(rxint, ESCCChannelState),
  609. VMSTATE_UINT32(txint, ESCCChannelState),
  610. VMSTATE_UINT32(rxint_under_svc, ESCCChannelState),
  611. VMSTATE_UINT32(txint_under_svc, ESCCChannelState),
  612. VMSTATE_UINT8(rx, ESCCChannelState),
  613. VMSTATE_UINT8(tx, ESCCChannelState),
  614. VMSTATE_BUFFER(wregs, ESCCChannelState),
  615. VMSTATE_BUFFER(rregs, ESCCChannelState),
  616. VMSTATE_END_OF_LIST()
  617. }
  618. };
  619. static const VMStateDescription vmstate_escc = {
  620. .name ="escc",
  621. .version_id = 2,
  622. .minimum_version_id = 1,
  623. .fields = (VMStateField[]) {
  624. VMSTATE_STRUCT_ARRAY(chn, ESCCState, 2, 2, vmstate_escc_chn,
  625. ESCCChannelState),
  626. VMSTATE_END_OF_LIST()
  627. }
  628. };
  629. static void sunkbd_handle_event(DeviceState *dev, QemuConsole *src,
  630. InputEvent *evt)
  631. {
  632. ESCCChannelState *s = (ESCCChannelState *)dev;
  633. int qcode, keycode;
  634. InputKeyEvent *key;
  635. assert(evt->type == INPUT_EVENT_KIND_KEY);
  636. key = evt->u.key.data;
  637. qcode = qemu_input_key_value_to_qcode(key->key);
  638. trace_escc_sunkbd_event_in(qcode, QKeyCode_str(qcode),
  639. key->down);
  640. if (qcode == Q_KEY_CODE_CAPS_LOCK) {
  641. if (key->down) {
  642. s->caps_lock_mode ^= 1;
  643. if (s->caps_lock_mode == 2) {
  644. return; /* Drop second press */
  645. }
  646. } else {
  647. s->caps_lock_mode ^= 2;
  648. if (s->caps_lock_mode == 3) {
  649. return; /* Drop first release */
  650. }
  651. }
  652. }
  653. if (qcode == Q_KEY_CODE_NUM_LOCK) {
  654. if (key->down) {
  655. s->num_lock_mode ^= 1;
  656. if (s->num_lock_mode == 2) {
  657. return; /* Drop second press */
  658. }
  659. } else {
  660. s->num_lock_mode ^= 2;
  661. if (s->num_lock_mode == 3) {
  662. return; /* Drop first release */
  663. }
  664. }
  665. }
  666. if (qcode > qemu_input_map_qcode_to_sun_len) {
  667. return;
  668. }
  669. keycode = qemu_input_map_qcode_to_sun[qcode];
  670. if (!key->down) {
  671. keycode |= 0x80;
  672. }
  673. trace_escc_sunkbd_event_out(keycode);
  674. put_queue(s, keycode);
  675. }
  676. static QemuInputHandler sunkbd_handler = {
  677. .name = "sun keyboard",
  678. .mask = INPUT_EVENT_MASK_KEY,
  679. .event = sunkbd_handle_event,
  680. };
  681. static void handle_kbd_command(ESCCChannelState *s, int val)
  682. {
  683. trace_escc_kbd_command(val);
  684. if (s->led_mode) { // Ignore led byte
  685. s->led_mode = 0;
  686. return;
  687. }
  688. switch (val) {
  689. case 1: // Reset, return type code
  690. clear_queue(s);
  691. put_queue(s, 0xff);
  692. put_queue(s, 4); // Type 4
  693. put_queue(s, 0x7f);
  694. break;
  695. case 0xe: // Set leds
  696. s->led_mode = 1;
  697. break;
  698. case 7: // Query layout
  699. case 0xf:
  700. clear_queue(s);
  701. put_queue(s, 0xfe);
  702. put_queue(s, 0x21); /* en-us layout */
  703. break;
  704. default:
  705. break;
  706. }
  707. }
  708. static void sunmouse_event(void *opaque,
  709. int dx, int dy, int dz, int buttons_state)
  710. {
  711. ESCCChannelState *s = opaque;
  712. int ch;
  713. trace_escc_sunmouse_event(dx, dy, buttons_state);
  714. ch = 0x80 | 0x7; /* protocol start byte, no buttons pressed */
  715. if (buttons_state & MOUSE_EVENT_LBUTTON)
  716. ch ^= 0x4;
  717. if (buttons_state & MOUSE_EVENT_MBUTTON)
  718. ch ^= 0x2;
  719. if (buttons_state & MOUSE_EVENT_RBUTTON)
  720. ch ^= 0x1;
  721. put_queue(s, ch);
  722. ch = dx;
  723. if (ch > 127)
  724. ch = 127;
  725. else if (ch < -127)
  726. ch = -127;
  727. put_queue(s, ch & 0xff);
  728. ch = -dy;
  729. if (ch > 127)
  730. ch = 127;
  731. else if (ch < -127)
  732. ch = -127;
  733. put_queue(s, ch & 0xff);
  734. // MSC protocol specify two extra motion bytes
  735. put_queue(s, 0);
  736. put_queue(s, 0);
  737. }
  738. static void escc_init1(Object *obj)
  739. {
  740. ESCCState *s = ESCC(obj);
  741. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  742. unsigned int i;
  743. for (i = 0; i < 2; i++) {
  744. sysbus_init_irq(dev, &s->chn[i].irq);
  745. s->chn[i].chn = 1 - i;
  746. }
  747. s->chn[0].otherchn = &s->chn[1];
  748. s->chn[1].otherchn = &s->chn[0];
  749. sysbus_init_mmio(dev, &s->mmio);
  750. }
  751. static void escc_realize(DeviceState *dev, Error **errp)
  752. {
  753. ESCCState *s = ESCC(dev);
  754. unsigned int i;
  755. s->chn[0].disabled = s->disabled;
  756. s->chn[1].disabled = s->disabled;
  757. memory_region_init_io(&s->mmio, OBJECT(dev), &escc_mem_ops, s, "escc",
  758. ESCC_SIZE << s->it_shift);
  759. for (i = 0; i < 2; i++) {
  760. if (qemu_chr_fe_backend_connected(&s->chn[i].chr)) {
  761. s->chn[i].clock = s->frequency / 2;
  762. qemu_chr_fe_set_handlers(&s->chn[i].chr, serial_can_receive,
  763. serial_receive1, serial_event, NULL,
  764. &s->chn[i], NULL, true);
  765. }
  766. }
  767. if (s->chn[0].type == escc_mouse) {
  768. qemu_add_mouse_event_handler(sunmouse_event, &s->chn[0], 0,
  769. "QEMU Sun Mouse");
  770. }
  771. if (s->chn[1].type == escc_kbd) {
  772. s->chn[1].hs = qemu_input_handler_register((DeviceState *)(&s->chn[1]),
  773. &sunkbd_handler);
  774. }
  775. }
  776. static Property escc_properties[] = {
  777. DEFINE_PROP_UINT32("frequency", ESCCState, frequency, 0),
  778. DEFINE_PROP_UINT32("it_shift", ESCCState, it_shift, 0),
  779. DEFINE_PROP_BOOL("bit_swap", ESCCState, bit_swap, false),
  780. DEFINE_PROP_UINT32("disabled", ESCCState, disabled, 0),
  781. DEFINE_PROP_UINT32("chnBtype", ESCCState, chn[0].type, 0),
  782. DEFINE_PROP_UINT32("chnAtype", ESCCState, chn[1].type, 0),
  783. DEFINE_PROP_CHR("chrB", ESCCState, chn[0].chr),
  784. DEFINE_PROP_CHR("chrA", ESCCState, chn[1].chr),
  785. DEFINE_PROP_END_OF_LIST(),
  786. };
  787. static void escc_class_init(ObjectClass *klass, void *data)
  788. {
  789. DeviceClass *dc = DEVICE_CLASS(klass);
  790. dc->reset = escc_reset;
  791. dc->realize = escc_realize;
  792. dc->vmsd = &vmstate_escc;
  793. dc->props = escc_properties;
  794. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  795. }
  796. static const TypeInfo escc_info = {
  797. .name = TYPE_ESCC,
  798. .parent = TYPE_SYS_BUS_DEVICE,
  799. .instance_size = sizeof(ESCCState),
  800. .instance_init = escc_init1,
  801. .class_init = escc_class_init,
  802. };
  803. static void escc_register_types(void)
  804. {
  805. type_register_static(&escc_info);
  806. }
  807. type_init(escc_register_types)