2
0

escc.c 25 KB

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