escc.c 26 KB

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