escc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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/qdev-properties-system.h"
  28. #include "hw/sysbus.h"
  29. #include "migration/vmstate.h"
  30. #include "qemu/module.h"
  31. #include "hw/char/escc.h"
  32. #include "ui/console.h"
  33. #include "qemu/cutils.h"
  34. #include "trace.h"
  35. /*
  36. * Chipset docs:
  37. * "Z80C30/Z85C30/Z80230/Z85230/Z85233 SCC/ESCC User Manual",
  38. * http://www.zilog.com/docs/serial/scc_escc_um.pdf
  39. *
  40. * On Sparc32 this is the serial port, mouse and keyboard part of chip STP2001
  41. * (Slave I/O), also produced as NCR89C105. See
  42. * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
  43. *
  44. * The serial ports implement full AMD AM8530 or Zilog Z8530 chips,
  45. * mouse and keyboard ports don't implement all functions and they are
  46. * only asynchronous. There is no DMA.
  47. *
  48. * Z85C30 is also used on PowerMacs and m68k Macs.
  49. *
  50. * There are some small differences between Sparc version (sunzilog)
  51. * and PowerMac (pmac):
  52. * Offset between control and data registers
  53. * There is some kind of lockup bug, but we can ignore it
  54. * CTS is inverted
  55. * DMA on pmac using DBDMA chip
  56. * pmac can do IRDA and faster rates, sunzilog can only do 38400
  57. * pmac baud rate generator clock is 3.6864 MHz, sunzilog 4.9152 MHz
  58. *
  59. * Linux driver for m68k Macs is the same as for PowerMac (pmac_zilog),
  60. * but registers are grouped by type and not by channel:
  61. * channel is selected by bit 0 of the address (instead of bit 1)
  62. * and register is selected by bit 1 of the address (instead of bit 0).
  63. */
  64. /*
  65. * Modifications:
  66. * 2006-Aug-10 Igor Kovalenko : Renamed KBDQueue to SERIOQueue, implemented
  67. * serial mouse queue.
  68. * Implemented serial mouse protocol.
  69. *
  70. * 2010-May-23 Artyom Tarasenko: Reworked IUS logic
  71. */
  72. #define CHN_C(s) ((s)->chn == escc_chn_b ? 'b' : 'a')
  73. #define SERIAL_CTRL 0
  74. #define SERIAL_DATA 1
  75. #define W_CMD 0
  76. #define CMD_PTR_MASK 0x07
  77. #define CMD_CMD_MASK 0x38
  78. #define CMD_HI 0x08
  79. #define CMD_CLR_TXINT 0x28
  80. #define CMD_CLR_IUS 0x38
  81. #define W_INTR 1
  82. #define INTR_INTALL 0x01
  83. #define INTR_TXINT 0x02
  84. #define INTR_PAR_SPEC 0x04
  85. #define INTR_RXMODEMSK 0x18
  86. #define INTR_RXINT1ST 0x08
  87. #define INTR_RXINTALL 0x10
  88. #define INTR_WTRQ_TXRX 0x20
  89. #define W_IVEC 2
  90. #define W_RXCTRL 3
  91. #define RXCTRL_RXEN 0x01
  92. #define RXCTRL_HUNT 0x10
  93. #define W_TXCTRL1 4
  94. #define TXCTRL1_PAREN 0x01
  95. #define TXCTRL1_PAREV 0x02
  96. #define TXCTRL1_1STOP 0x04
  97. #define TXCTRL1_1HSTOP 0x08
  98. #define TXCTRL1_2STOP 0x0c
  99. #define TXCTRL1_STPMSK 0x0c
  100. #define TXCTRL1_CLK1X 0x00
  101. #define TXCTRL1_CLK16X 0x40
  102. #define TXCTRL1_CLK32X 0x80
  103. #define TXCTRL1_CLK64X 0xc0
  104. #define TXCTRL1_CLKMSK 0xc0
  105. #define W_TXCTRL2 5
  106. #define TXCTRL2_TXCRC 0x01
  107. #define TXCTRL2_TXEN 0x08
  108. #define TXCTRL2_BITMSK 0x60
  109. #define TXCTRL2_5BITS 0x00
  110. #define TXCTRL2_7BITS 0x20
  111. #define TXCTRL2_6BITS 0x40
  112. #define TXCTRL2_8BITS 0x60
  113. #define W_SYNC1 6
  114. #define W_SYNC2 7
  115. #define W_TXBUF 8
  116. #define W_MINTR 9
  117. #define MINTR_VIS 0x01
  118. #define MINTR_NV 0x02
  119. #define MINTR_STATUSHI 0x10
  120. #define MINTR_SOFTIACK 0x20
  121. #define MINTR_RST_MASK 0xc0
  122. #define MINTR_RST_B 0x40
  123. #define MINTR_RST_A 0x80
  124. #define MINTR_RST_ALL 0xc0
  125. #define W_MISC1 10
  126. #define MISC1_ENC_MASK 0x60
  127. #define W_CLOCK 11
  128. #define CLOCK_TRXC 0x08
  129. #define W_BRGLO 12
  130. #define W_BRGHI 13
  131. #define W_MISC2 14
  132. #define MISC2_BRG_EN 0x01
  133. #define MISC2_BRG_SRC 0x02
  134. #define MISC2_LCL_LOOP 0x10
  135. #define MISC2_PLLCMD0 0x20
  136. #define MISC2_PLLCMD1 0x40
  137. #define MISC2_PLLCMD2 0x80
  138. #define W_EXTINT 15
  139. #define EXTINT_DCD 0x08
  140. #define EXTINT_SYNCINT 0x10
  141. #define EXTINT_CTSINT 0x20
  142. #define EXTINT_TXUNDRN 0x40
  143. #define EXTINT_BRKINT 0x80
  144. #define R_STATUS 0
  145. #define STATUS_RXAV 0x01
  146. #define STATUS_ZERO 0x02
  147. #define STATUS_TXEMPTY 0x04
  148. #define STATUS_DCD 0x08
  149. #define STATUS_SYNC 0x10
  150. #define STATUS_CTS 0x20
  151. #define STATUS_TXUNDRN 0x40
  152. #define STATUS_BRK 0x80
  153. #define R_SPEC 1
  154. #define SPEC_ALLSENT 0x01
  155. #define SPEC_BITS8 0x06
  156. #define R_IVEC 2
  157. #define IVEC_TXINTB 0x00
  158. #define IVEC_LONOINT 0x06
  159. #define IVEC_LORXINTA 0x0c
  160. #define IVEC_LORXINTB 0x04
  161. #define IVEC_LOTXINTA 0x08
  162. #define IVEC_HINOINT 0x60
  163. #define IVEC_HIRXINTA 0x30
  164. #define IVEC_HIRXINTB 0x20
  165. #define IVEC_HITXINTA 0x10
  166. #define R_INTR 3
  167. #define INTR_EXTINTB 0x01
  168. #define INTR_TXINTB 0x02
  169. #define INTR_RXINTB 0x04
  170. #define INTR_EXTINTA 0x08
  171. #define INTR_TXINTA 0x10
  172. #define INTR_RXINTA 0x20
  173. #define R_IPEN 4
  174. #define R_TXCTRL1 5
  175. #define R_TXCTRL2 6
  176. #define R_BC 7
  177. #define R_RXBUF 8
  178. #define R_RXCTRL 9
  179. #define R_MISC 10
  180. #define MISC_2CLKMISS 0x40
  181. #define R_MISC1 11
  182. #define R_BRGLO 12
  183. #define R_BRGHI 13
  184. #define R_MISC1I 14
  185. #define R_EXTINT 15
  186. static uint8_t sunkbd_layout_dip_switch(const char *sunkbd_layout);
  187. static void handle_kbd_command(ESCCChannelState *s, int val);
  188. static int serial_can_receive(void *opaque);
  189. static void serial_receive_byte(ESCCChannelState *s, int ch);
  190. static int reg_shift(ESCCState *s)
  191. {
  192. return s->bit_swap ? s->it_shift + 1 : s->it_shift;
  193. }
  194. static int chn_shift(ESCCState *s)
  195. {
  196. return s->bit_swap ? s->it_shift : s->it_shift + 1;
  197. }
  198. static void clear_queue(void *opaque)
  199. {
  200. ESCCChannelState *s = opaque;
  201. ESCCSERIOQueue *q = &s->queue;
  202. q->rptr = q->wptr = q->count = 0;
  203. }
  204. static void put_queue(void *opaque, int b)
  205. {
  206. ESCCChannelState *s = opaque;
  207. ESCCSERIOQueue *q = &s->queue;
  208. trace_escc_put_queue(CHN_C(s), b);
  209. if (q->count >= ESCC_SERIO_QUEUE_SIZE) {
  210. return;
  211. }
  212. q->data[q->wptr] = b;
  213. if (++q->wptr == ESCC_SERIO_QUEUE_SIZE) {
  214. q->wptr = 0;
  215. }
  216. q->count++;
  217. serial_receive_byte(s, 0);
  218. }
  219. static uint32_t get_queue(void *opaque)
  220. {
  221. ESCCChannelState *s = opaque;
  222. ESCCSERIOQueue *q = &s->queue;
  223. int val;
  224. if (q->count == 0) {
  225. return 0;
  226. } else {
  227. val = q->data[q->rptr];
  228. if (++q->rptr == ESCC_SERIO_QUEUE_SIZE) {
  229. q->rptr = 0;
  230. }
  231. q->count--;
  232. }
  233. trace_escc_get_queue(CHN_C(s), val);
  234. if (q->count > 0) {
  235. serial_receive_byte(s, 0);
  236. }
  237. return val;
  238. }
  239. static int escc_update_irq_chn(ESCCChannelState *s)
  240. {
  241. if ((((s->wregs[W_INTR] & INTR_TXINT) && (s->txint == 1)) ||
  242. /* tx ints enabled, pending */
  243. ((((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINT1ST) ||
  244. ((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINTALL)) &&
  245. s->rxint == 1) ||
  246. /* rx ints enabled, pending */
  247. ((s->wregs[W_EXTINT] & EXTINT_BRKINT) &&
  248. (s->rregs[R_STATUS] & STATUS_BRK)))) {
  249. /* break int e&p */
  250. return 1;
  251. }
  252. return 0;
  253. }
  254. static void escc_update_irq(ESCCChannelState *s)
  255. {
  256. int irq;
  257. irq = escc_update_irq_chn(s);
  258. irq |= escc_update_irq_chn(s->otherchn);
  259. trace_escc_update_irq(irq);
  260. qemu_set_irq(s->irq, irq);
  261. }
  262. static void escc_reset_chn(ESCCChannelState *s)
  263. {
  264. s->reg = 0;
  265. s->rx = s->tx = 0;
  266. s->rxint = s->txint = 0;
  267. s->rxint_under_svc = s->txint_under_svc = 0;
  268. s->e0_mode = s->led_mode = s->caps_lock_mode = s->num_lock_mode = 0;
  269. s->sunmouse_dx = s->sunmouse_dy = s->sunmouse_buttons = 0;
  270. clear_queue(s);
  271. }
  272. static void escc_soft_reset_chn(ESCCChannelState *s)
  273. {
  274. escc_reset_chn(s);
  275. s->wregs[W_CMD] = 0;
  276. s->wregs[W_INTR] &= INTR_PAR_SPEC | INTR_WTRQ_TXRX;
  277. s->wregs[W_RXCTRL] &= ~RXCTRL_RXEN;
  278. /* 1 stop bit */
  279. s->wregs[W_TXCTRL1] |= TXCTRL1_1STOP;
  280. s->wregs[W_TXCTRL2] &= TXCTRL2_TXCRC | TXCTRL2_8BITS;
  281. s->wregs[W_MINTR] &= ~MINTR_SOFTIACK;
  282. s->wregs[W_MISC1] &= MISC1_ENC_MASK;
  283. /* PLL disabled */
  284. s->wregs[W_MISC2] &= MISC2_BRG_EN | MISC2_BRG_SRC |
  285. MISC2_PLLCMD1 | MISC2_PLLCMD2;
  286. s->wregs[W_MISC2] |= MISC2_PLLCMD0;
  287. /* Enable most interrupts */
  288. s->wregs[W_EXTINT] = EXTINT_DCD | EXTINT_SYNCINT | EXTINT_CTSINT |
  289. EXTINT_TXUNDRN | EXTINT_BRKINT;
  290. s->rregs[R_STATUS] &= STATUS_DCD | STATUS_SYNC | STATUS_CTS | STATUS_BRK;
  291. s->rregs[R_STATUS] |= STATUS_TXEMPTY | STATUS_TXUNDRN;
  292. if (s->disabled) {
  293. s->rregs[R_STATUS] |= STATUS_DCD | STATUS_SYNC | STATUS_CTS;
  294. }
  295. s->rregs[R_SPEC] &= SPEC_ALLSENT;
  296. s->rregs[R_SPEC] |= SPEC_BITS8;
  297. s->rregs[R_INTR] = 0;
  298. s->rregs[R_MISC] &= MISC_2CLKMISS;
  299. }
  300. static void escc_hard_reset_chn(ESCCChannelState *s)
  301. {
  302. escc_soft_reset_chn(s);
  303. /*
  304. * Hard reset is almost identical to soft reset above, except that the
  305. * values of WR9 (W_MINTR), WR10 (W_MISC1), WR11 (W_CLOCK) and WR14
  306. * (W_MISC2) have extra bits forced to 0/1
  307. */
  308. s->wregs[W_MINTR] &= MINTR_VIS | MINTR_NV;
  309. s->wregs[W_MINTR] |= MINTR_RST_B | MINTR_RST_A;
  310. s->wregs[W_MISC1] = 0;
  311. s->wregs[W_CLOCK] = CLOCK_TRXC;
  312. s->wregs[W_MISC2] &= MISC2_PLLCMD1 | MISC2_PLLCMD2;
  313. s->wregs[W_MISC2] |= MISC2_LCL_LOOP | MISC2_PLLCMD0;
  314. }
  315. static void escc_reset(DeviceState *d)
  316. {
  317. ESCCState *s = ESCC(d);
  318. int i, j;
  319. for (i = 0; i < 2; i++) {
  320. ESCCChannelState *cs = &s->chn[i];
  321. /*
  322. * According to the ESCC datasheet "Miscellaneous Questions" section
  323. * on page 384, the values of the ESCC registers are not guaranteed on
  324. * power-on until an explicit hardware or software reset has been
  325. * issued. For now we zero the registers so that a device reset always
  326. * returns the emulated device to a fixed state.
  327. */
  328. for (j = 0; j < ESCC_SERIAL_REGS; j++) {
  329. cs->rregs[j] = 0;
  330. cs->wregs[j] = 0;
  331. }
  332. /*
  333. * ...but there is an exception. The "Transmit Interrupts and Transmit
  334. * Buffer Empty Bit" section on page 50 of the ESCC datasheet says of
  335. * the STATUS_TXEMPTY bit in R_STATUS: "After a hardware reset
  336. * (including a hardware reset by software), or a channel reset, this
  337. * bit is set to 1". The Sun PROM checks this bit early on startup and
  338. * gets stuck in an infinite loop if it is not set.
  339. */
  340. cs->rregs[R_STATUS] |= STATUS_TXEMPTY;
  341. escc_reset_chn(cs);
  342. }
  343. }
  344. static inline void set_rxint(ESCCChannelState *s)
  345. {
  346. s->rxint = 1;
  347. /*
  348. * XXX: missing daisy chaining: escc_chn_b rx should have a lower priority
  349. * than chn_a rx/tx/special_condition service
  350. */
  351. s->rxint_under_svc = 1;
  352. if (s->chn == escc_chn_a) {
  353. s->rregs[R_INTR] |= INTR_RXINTA;
  354. if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
  355. s->otherchn->rregs[R_IVEC] = IVEC_HIRXINTA;
  356. } else {
  357. s->otherchn->rregs[R_IVEC] = IVEC_LORXINTA;
  358. }
  359. } else {
  360. s->otherchn->rregs[R_INTR] |= INTR_RXINTB;
  361. if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
  362. s->rregs[R_IVEC] = IVEC_HIRXINTB;
  363. } else {
  364. s->rregs[R_IVEC] = IVEC_LORXINTB;
  365. }
  366. }
  367. escc_update_irq(s);
  368. }
  369. static inline void set_txint(ESCCChannelState *s)
  370. {
  371. s->txint = 1;
  372. if (!s->rxint_under_svc) {
  373. s->txint_under_svc = 1;
  374. if (s->chn == escc_chn_a) {
  375. if (s->wregs[W_INTR] & INTR_TXINT) {
  376. s->rregs[R_INTR] |= INTR_TXINTA;
  377. }
  378. if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
  379. s->otherchn->rregs[R_IVEC] = IVEC_HITXINTA;
  380. } else {
  381. s->otherchn->rregs[R_IVEC] = IVEC_LOTXINTA;
  382. }
  383. } else {
  384. s->rregs[R_IVEC] = IVEC_TXINTB;
  385. if (s->wregs[W_INTR] & INTR_TXINT) {
  386. s->otherchn->rregs[R_INTR] |= INTR_TXINTB;
  387. }
  388. }
  389. escc_update_irq(s);
  390. }
  391. }
  392. static inline void clr_rxint(ESCCChannelState *s)
  393. {
  394. s->rxint = 0;
  395. s->rxint_under_svc = 0;
  396. if (s->chn == escc_chn_a) {
  397. if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
  398. s->otherchn->rregs[R_IVEC] = IVEC_HINOINT;
  399. } else {
  400. s->otherchn->rregs[R_IVEC] = IVEC_LONOINT;
  401. }
  402. s->rregs[R_INTR] &= ~INTR_RXINTA;
  403. } else {
  404. if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
  405. s->rregs[R_IVEC] = IVEC_HINOINT;
  406. } else {
  407. s->rregs[R_IVEC] = IVEC_LONOINT;
  408. }
  409. s->otherchn->rregs[R_INTR] &= ~INTR_RXINTB;
  410. }
  411. if (s->txint) {
  412. set_txint(s);
  413. }
  414. escc_update_irq(s);
  415. }
  416. static inline void clr_txint(ESCCChannelState *s)
  417. {
  418. s->txint = 0;
  419. s->txint_under_svc = 0;
  420. if (s->chn == escc_chn_a) {
  421. if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
  422. s->otherchn->rregs[R_IVEC] = IVEC_HINOINT;
  423. } else {
  424. s->otherchn->rregs[R_IVEC] = IVEC_LONOINT;
  425. }
  426. s->rregs[R_INTR] &= ~INTR_TXINTA;
  427. } else {
  428. s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB;
  429. if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
  430. s->rregs[R_IVEC] = IVEC_HINOINT;
  431. } else {
  432. s->rregs[R_IVEC] = IVEC_LONOINT;
  433. }
  434. s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB;
  435. }
  436. if (s->rxint) {
  437. set_rxint(s);
  438. }
  439. escc_update_irq(s);
  440. }
  441. static void escc_update_parameters(ESCCChannelState *s)
  442. {
  443. int speed, parity, data_bits, stop_bits;
  444. QEMUSerialSetParams ssp;
  445. if (!qemu_chr_fe_backend_connected(&s->chr) || s->type != escc_serial) {
  446. return;
  447. }
  448. if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREN) {
  449. if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREV) {
  450. parity = 'E';
  451. } else {
  452. parity = 'O';
  453. }
  454. } else {
  455. parity = 'N';
  456. }
  457. if ((s->wregs[W_TXCTRL1] & TXCTRL1_STPMSK) == TXCTRL1_2STOP) {
  458. stop_bits = 2;
  459. } else {
  460. stop_bits = 1;
  461. }
  462. switch (s->wregs[W_TXCTRL2] & TXCTRL2_BITMSK) {
  463. case TXCTRL2_5BITS:
  464. data_bits = 5;
  465. break;
  466. case TXCTRL2_7BITS:
  467. data_bits = 7;
  468. break;
  469. case TXCTRL2_6BITS:
  470. data_bits = 6;
  471. break;
  472. default:
  473. case TXCTRL2_8BITS:
  474. data_bits = 8;
  475. break;
  476. }
  477. speed = s->clock / ((s->wregs[W_BRGLO] | (s->wregs[W_BRGHI] << 8)) + 2);
  478. switch (s->wregs[W_TXCTRL1] & TXCTRL1_CLKMSK) {
  479. case TXCTRL1_CLK1X:
  480. break;
  481. case TXCTRL1_CLK16X:
  482. speed /= 16;
  483. break;
  484. case TXCTRL1_CLK32X:
  485. speed /= 32;
  486. break;
  487. default:
  488. case TXCTRL1_CLK64X:
  489. speed /= 64;
  490. break;
  491. }
  492. ssp.speed = speed;
  493. ssp.parity = parity;
  494. ssp.data_bits = data_bits;
  495. ssp.stop_bits = stop_bits;
  496. trace_escc_update_parameters(CHN_C(s), speed, parity, data_bits, stop_bits);
  497. qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
  498. }
  499. static void escc_mem_write(void *opaque, hwaddr addr,
  500. uint64_t val, unsigned size)
  501. {
  502. ESCCState *serial = opaque;
  503. ESCCChannelState *s;
  504. uint32_t saddr;
  505. int newreg, channel;
  506. val &= 0xff;
  507. saddr = (addr >> reg_shift(serial)) & 1;
  508. channel = (addr >> chn_shift(serial)) & 1;
  509. s = &serial->chn[channel];
  510. switch (saddr) {
  511. case SERIAL_CTRL:
  512. trace_escc_mem_writeb_ctrl(CHN_C(s), s->reg, val & 0xff);
  513. newreg = 0;
  514. switch (s->reg) {
  515. case W_CMD:
  516. newreg = val & CMD_PTR_MASK;
  517. val &= CMD_CMD_MASK;
  518. switch (val) {
  519. case CMD_HI:
  520. newreg |= CMD_HI;
  521. break;
  522. case CMD_CLR_TXINT:
  523. clr_txint(s);
  524. break;
  525. case CMD_CLR_IUS:
  526. if (s->rxint_under_svc) {
  527. s->rxint_under_svc = 0;
  528. if (s->txint) {
  529. set_txint(s);
  530. }
  531. } else if (s->txint_under_svc) {
  532. s->txint_under_svc = 0;
  533. }
  534. escc_update_irq(s);
  535. break;
  536. default:
  537. break;
  538. }
  539. break;
  540. case W_RXCTRL:
  541. s->wregs[s->reg] = val;
  542. if (val & RXCTRL_HUNT) {
  543. s->rregs[R_STATUS] |= STATUS_SYNC;
  544. }
  545. break;
  546. case W_INTR ... W_IVEC:
  547. case W_SYNC1 ... W_TXBUF:
  548. case W_MISC1 ... W_CLOCK:
  549. case W_MISC2 ... W_EXTINT:
  550. s->wregs[s->reg] = val;
  551. break;
  552. case W_TXCTRL1:
  553. s->wregs[s->reg] = val;
  554. /*
  555. * The ESCC datasheet states that SPEC_ALLSENT is always set in
  556. * sync mode, and set in async mode when all characters have
  557. * cleared the transmitter. Since writes to SERIAL_DATA use the
  558. * blocking qemu_chr_fe_write_all() function to write each
  559. * character, the guest can never see the state when async data
  560. * is in the process of being transmitted so we can set this bit
  561. * unconditionally regardless of the state of the W_TXCTRL1 mode
  562. * bits.
  563. */
  564. s->rregs[R_SPEC] |= SPEC_ALLSENT;
  565. escc_update_parameters(s);
  566. break;
  567. case W_TXCTRL2:
  568. s->wregs[s->reg] = val;
  569. escc_update_parameters(s);
  570. break;
  571. case W_BRGLO:
  572. case W_BRGHI:
  573. s->wregs[s->reg] = val;
  574. s->rregs[s->reg] = val;
  575. escc_update_parameters(s);
  576. break;
  577. case W_MINTR:
  578. switch (val & MINTR_RST_MASK) {
  579. case 0:
  580. default:
  581. break;
  582. case MINTR_RST_B:
  583. trace_escc_soft_reset_chn(CHN_C(&serial->chn[0]));
  584. escc_soft_reset_chn(&serial->chn[0]);
  585. return;
  586. case MINTR_RST_A:
  587. trace_escc_soft_reset_chn(CHN_C(&serial->chn[1]));
  588. escc_soft_reset_chn(&serial->chn[1]);
  589. return;
  590. case MINTR_RST_ALL:
  591. trace_escc_hard_reset();
  592. escc_hard_reset_chn(&serial->chn[0]);
  593. escc_hard_reset_chn(&serial->chn[1]);
  594. return;
  595. }
  596. break;
  597. default:
  598. break;
  599. }
  600. if (s->reg == 0) {
  601. s->reg = newreg;
  602. } else {
  603. s->reg = 0;
  604. }
  605. break;
  606. case SERIAL_DATA:
  607. trace_escc_mem_writeb_data(CHN_C(s), val);
  608. /*
  609. * Lower the irq when data is written to the Tx buffer and no other
  610. * interrupts are currently pending. The irq will be raised again once
  611. * the Tx buffer becomes empty below.
  612. */
  613. s->txint = 0;
  614. escc_update_irq(s);
  615. s->tx = val;
  616. if (s->wregs[W_TXCTRL2] & TXCTRL2_TXEN) { /* tx enabled */
  617. if (s->wregs[W_MISC2] & MISC2_LCL_LOOP) {
  618. serial_receive_byte(s, s->tx);
  619. } else if (qemu_chr_fe_backend_connected(&s->chr)) {
  620. /*
  621. * XXX this blocks entire thread. Rewrite to use
  622. * qemu_chr_fe_write and background I/O callbacks
  623. */
  624. qemu_chr_fe_write_all(&s->chr, &s->tx, 1);
  625. } else if (s->type == escc_kbd && !s->disabled) {
  626. handle_kbd_command(s, val);
  627. }
  628. }
  629. s->rregs[R_STATUS] |= STATUS_TXEMPTY; /* Tx buffer empty */
  630. s->rregs[R_SPEC] |= SPEC_ALLSENT; /* All sent */
  631. set_txint(s);
  632. break;
  633. default:
  634. break;
  635. }
  636. }
  637. static uint64_t escc_mem_read(void *opaque, hwaddr addr,
  638. unsigned size)
  639. {
  640. ESCCState *serial = opaque;
  641. ESCCChannelState *s;
  642. uint32_t saddr;
  643. uint32_t ret;
  644. int channel;
  645. saddr = (addr >> reg_shift(serial)) & 1;
  646. channel = (addr >> chn_shift(serial)) & 1;
  647. s = &serial->chn[channel];
  648. switch (saddr) {
  649. case SERIAL_CTRL:
  650. trace_escc_mem_readb_ctrl(CHN_C(s), s->reg, s->rregs[s->reg]);
  651. ret = s->rregs[s->reg];
  652. s->reg = 0;
  653. return ret;
  654. case SERIAL_DATA:
  655. s->rregs[R_STATUS] &= ~STATUS_RXAV;
  656. clr_rxint(s);
  657. if (s->type == escc_kbd || s->type == escc_mouse) {
  658. ret = get_queue(s);
  659. } else {
  660. ret = s->rx;
  661. }
  662. trace_escc_mem_readb_data(CHN_C(s), ret);
  663. qemu_chr_fe_accept_input(&s->chr);
  664. return ret;
  665. default:
  666. break;
  667. }
  668. return 0;
  669. }
  670. static const MemoryRegionOps escc_mem_ops = {
  671. .read = escc_mem_read,
  672. .write = escc_mem_write,
  673. .endianness = DEVICE_NATIVE_ENDIAN,
  674. .valid = {
  675. .min_access_size = 1,
  676. .max_access_size = 1,
  677. },
  678. };
  679. static int serial_can_receive(void *opaque)
  680. {
  681. ESCCChannelState *s = opaque;
  682. int ret;
  683. if (((s->wregs[W_RXCTRL] & RXCTRL_RXEN) == 0) /* Rx not enabled */
  684. || ((s->rregs[R_STATUS] & STATUS_RXAV) == STATUS_RXAV)) {
  685. /* char already available */
  686. ret = 0;
  687. } else {
  688. ret = 1;
  689. }
  690. return ret;
  691. }
  692. static void serial_receive_byte(ESCCChannelState *s, int ch)
  693. {
  694. trace_escc_serial_receive_byte(CHN_C(s), ch);
  695. s->rregs[R_STATUS] |= STATUS_RXAV;
  696. s->rx = ch;
  697. set_rxint(s);
  698. }
  699. static void serial_receive_break(ESCCChannelState *s)
  700. {
  701. s->rregs[R_STATUS] |= STATUS_BRK;
  702. escc_update_irq(s);
  703. }
  704. static void serial_receive1(void *opaque, const uint8_t *buf, int size)
  705. {
  706. ESCCChannelState *s = opaque;
  707. serial_receive_byte(s, buf[0]);
  708. }
  709. static void serial_event(void *opaque, QEMUChrEvent event)
  710. {
  711. ESCCChannelState *s = opaque;
  712. if (event == CHR_EVENT_BREAK) {
  713. serial_receive_break(s);
  714. }
  715. }
  716. static const VMStateDescription vmstate_escc_chn = {
  717. .name = "escc_chn",
  718. .version_id = 2,
  719. .minimum_version_id = 1,
  720. .fields = (const VMStateField[]) {
  721. VMSTATE_UINT32(vmstate_dummy, ESCCChannelState),
  722. VMSTATE_UINT32(reg, ESCCChannelState),
  723. VMSTATE_UINT32(rxint, ESCCChannelState),
  724. VMSTATE_UINT32(txint, ESCCChannelState),
  725. VMSTATE_UINT32(rxint_under_svc, ESCCChannelState),
  726. VMSTATE_UINT32(txint_under_svc, ESCCChannelState),
  727. VMSTATE_UINT8(rx, ESCCChannelState),
  728. VMSTATE_UINT8(tx, ESCCChannelState),
  729. VMSTATE_BUFFER(wregs, ESCCChannelState),
  730. VMSTATE_BUFFER(rregs, ESCCChannelState),
  731. VMSTATE_END_OF_LIST()
  732. }
  733. };
  734. static const VMStateDescription vmstate_escc = {
  735. .name = "escc",
  736. .version_id = 2,
  737. .minimum_version_id = 1,
  738. .fields = (const VMStateField[]) {
  739. VMSTATE_STRUCT_ARRAY(chn, ESCCState, 2, 2, vmstate_escc_chn,
  740. ESCCChannelState),
  741. VMSTATE_END_OF_LIST()
  742. }
  743. };
  744. static void sunkbd_handle_event(DeviceState *dev, QemuConsole *src,
  745. InputEvent *evt)
  746. {
  747. ESCCChannelState *s = (ESCCChannelState *)dev;
  748. int qcode, keycode;
  749. InputKeyEvent *key;
  750. assert(evt->type == INPUT_EVENT_KIND_KEY);
  751. key = evt->u.key.data;
  752. qcode = qemu_input_key_value_to_qcode(key->key);
  753. trace_escc_sunkbd_event_in(qcode, QKeyCode_str(qcode),
  754. key->down);
  755. if (qcode == Q_KEY_CODE_CAPS_LOCK) {
  756. if (key->down) {
  757. s->caps_lock_mode ^= 1;
  758. if (s->caps_lock_mode == 2) {
  759. return; /* Drop second press */
  760. }
  761. } else {
  762. s->caps_lock_mode ^= 2;
  763. if (s->caps_lock_mode == 3) {
  764. return; /* Drop first release */
  765. }
  766. }
  767. }
  768. if (qcode == Q_KEY_CODE_NUM_LOCK) {
  769. if (key->down) {
  770. s->num_lock_mode ^= 1;
  771. if (s->num_lock_mode == 2) {
  772. return; /* Drop second press */
  773. }
  774. } else {
  775. s->num_lock_mode ^= 2;
  776. if (s->num_lock_mode == 3) {
  777. return; /* Drop first release */
  778. }
  779. }
  780. }
  781. if (qcode >= qemu_input_map_qcode_to_sun_len) {
  782. return;
  783. }
  784. keycode = qemu_input_map_qcode_to_sun[qcode];
  785. if (!key->down) {
  786. keycode |= 0x80;
  787. }
  788. trace_escc_sunkbd_event_out(keycode);
  789. put_queue(s, keycode);
  790. }
  791. static const QemuInputHandler sunkbd_handler = {
  792. .name = "sun keyboard",
  793. .mask = INPUT_EVENT_MASK_KEY,
  794. .event = sunkbd_handle_event,
  795. };
  796. static uint8_t sunkbd_layout_dip_switch(const char *kbd_layout)
  797. {
  798. /* Return the value of the dip-switches in a SUN Type 5 keyboard */
  799. static uint8_t ret = 0xff;
  800. if ((ret == 0xff) && kbd_layout) {
  801. int i;
  802. struct layout_values {
  803. const char *lang;
  804. uint8_t dip;
  805. } languages[] =
  806. /*
  807. * Dip values from table 3-16 Layouts for Type 4, 5 and 5c Keyboards
  808. */
  809. {
  810. {"en-us", 0x21}, /* U.S.A. (US5.kt) */
  811. /* 0x22 is some other US (US_UNIX5.kt) */
  812. {"fr", 0x23}, /* France (France5.kt) */
  813. {"da", 0x24}, /* Denmark (Denmark5.kt) */
  814. {"de", 0x25}, /* Germany (Germany5.kt) */
  815. {"it", 0x26}, /* Italy (Italy5.kt) */
  816. {"nl", 0x27}, /* The Netherlands (Netherland5.kt) */
  817. {"no", 0x28}, /* Norway (Norway.kt) */
  818. {"pt", 0x29}, /* Portugal (Portugal5.kt) */
  819. {"es", 0x2a}, /* Spain (Spain5.kt) */
  820. {"sv", 0x2b}, /* Sweden (Sweden5.kt) */
  821. {"fr-ch", 0x2c}, /* Switzerland/French (Switzer_Fr5.kt) */
  822. {"de-ch", 0x2d}, /* Switzerland/German (Switzer_Ge5.kt) */
  823. {"en-gb", 0x2e}, /* Great Britain (UK5.kt) */
  824. {"ko", 0x2f}, /* Korea (Korea5.kt) */
  825. {"tw", 0x30}, /* Taiwan (Taiwan5.kt) */
  826. {"ja", 0x31}, /* Japan (Japan5.kt) */
  827. {"fr-ca", 0x32}, /* Canada/French (Canada_Fr5.kt) */
  828. {"hu", 0x33}, /* Hungary (Hungary5.kt) */
  829. {"pl", 0x34}, /* Poland (Poland5.kt) */
  830. {"cz", 0x35}, /* Czech (Czech5.kt) */
  831. {"ru", 0x36}, /* Russia (Russia5.kt) */
  832. {"lv", 0x37}, /* Latvia (Latvia5.kt) */
  833. {"tr", 0x38}, /* Turkey-Q5 (TurkeyQ5.kt) */
  834. {"gr", 0x39}, /* Greece (Greece5.kt) */
  835. {"ar", 0x3a}, /* Arabic (Arabic5.kt) */
  836. {"lt", 0x3b}, /* Lithuania (Lithuania5.kt) */
  837. {"nl-be", 0x3c}, /* Belgium (Belgian5.kt) */
  838. {"be", 0x3c}, /* Belgium (Belgian5.kt) */
  839. };
  840. for (i = 0;
  841. i < sizeof(languages) / sizeof(struct layout_values);
  842. i++) {
  843. if (!strcmp(kbd_layout, languages[i].lang)) {
  844. ret = languages[i].dip;
  845. return ret;
  846. }
  847. }
  848. /* Found no known language code */
  849. if ((kbd_layout[0] >= '0') && (kbd_layout[0] <= '9')) {
  850. unsigned int tmp;
  851. /* As a fallback we also accept numeric dip switch value */
  852. if (!qemu_strtoui(kbd_layout, NULL, 0, &tmp)) {
  853. ret = tmp;
  854. }
  855. }
  856. }
  857. if (ret == 0xff) {
  858. /* Final fallback if keyboard_layout was not set or recognized */
  859. ret = 0x21; /* en-us layout */
  860. }
  861. return ret;
  862. }
  863. static void handle_kbd_command(ESCCChannelState *s, int val)
  864. {
  865. trace_escc_kbd_command(val);
  866. if (s->led_mode) { /* Ignore led byte */
  867. s->led_mode = 0;
  868. return;
  869. }
  870. switch (val) {
  871. case 1: /* Reset, return type code */
  872. clear_queue(s);
  873. put_queue(s, 0xff);
  874. put_queue(s, 4); /* Type 4 */
  875. put_queue(s, 0x7f);
  876. break;
  877. case 0xe: /* Set leds */
  878. s->led_mode = 1;
  879. break;
  880. case 7: /* Query layout */
  881. case 0xf:
  882. clear_queue(s);
  883. put_queue(s, 0xfe);
  884. put_queue(s, sunkbd_layout_dip_switch(s->sunkbd_layout));
  885. break;
  886. default:
  887. break;
  888. }
  889. }
  890. static void sunmouse_handle_event(DeviceState *dev, QemuConsole *src,
  891. InputEvent *evt)
  892. {
  893. ESCCChannelState *s = (ESCCChannelState *)dev;
  894. InputMoveEvent *move;
  895. InputBtnEvent *btn;
  896. static const int bmap[INPUT_BUTTON__MAX] = {
  897. [INPUT_BUTTON_LEFT] = 0x4,
  898. [INPUT_BUTTON_MIDDLE] = 0x2,
  899. [INPUT_BUTTON_RIGHT] = 0x1,
  900. };
  901. switch (evt->type) {
  902. case INPUT_EVENT_KIND_REL:
  903. move = evt->u.rel.data;
  904. if (move->axis == INPUT_AXIS_X) {
  905. s->sunmouse_dx += move->value;
  906. } else if (move->axis == INPUT_AXIS_Y) {
  907. s->sunmouse_dy -= move->value;
  908. }
  909. break;
  910. case INPUT_EVENT_KIND_BTN:
  911. btn = evt->u.btn.data;
  912. if (bmap[btn->button]) {
  913. if (btn->down) {
  914. s->sunmouse_buttons |= bmap[btn->button];
  915. } else {
  916. s->sunmouse_buttons &= ~bmap[btn->button];
  917. }
  918. /* Indicate we have a supported button event */
  919. s->sunmouse_buttons |= 0x80;
  920. }
  921. break;
  922. default:
  923. /* keep gcc happy */
  924. break;
  925. }
  926. }
  927. static void sunmouse_sync(DeviceState *dev)
  928. {
  929. ESCCChannelState *s = (ESCCChannelState *)dev;
  930. int ch;
  931. if (s->sunmouse_dx == 0 && s->sunmouse_dy == 0 &&
  932. (s->sunmouse_buttons & 0x80) == 0) {
  933. /* Nothing to do after button event filter */
  934. return;
  935. }
  936. /* Clear our button event flag */
  937. s->sunmouse_buttons &= ~0x80;
  938. trace_escc_sunmouse_event(s->sunmouse_dx, s->sunmouse_dy,
  939. s->sunmouse_buttons);
  940. ch = 0x80 | 0x7; /* protocol start byte, no buttons pressed */
  941. ch ^= s->sunmouse_buttons;
  942. put_queue(s, ch);
  943. ch = s->sunmouse_dx;
  944. if (ch > 127) {
  945. ch = 127;
  946. } else if (ch < -127) {
  947. ch = -127;
  948. }
  949. put_queue(s, ch & 0xff);
  950. s->sunmouse_dx -= ch;
  951. ch = s->sunmouse_dy;
  952. if (ch > 127) {
  953. ch = 127;
  954. } else if (ch < -127) {
  955. ch = -127;
  956. }
  957. put_queue(s, ch & 0xff);
  958. s->sunmouse_dy -= ch;
  959. /* MSC protocol specifies two extra motion bytes */
  960. put_queue(s, 0);
  961. put_queue(s, 0);
  962. }
  963. static const QemuInputHandler sunmouse_handler = {
  964. .name = "QEMU Sun Mouse",
  965. .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
  966. .event = sunmouse_handle_event,
  967. .sync = sunmouse_sync,
  968. };
  969. static void escc_init1(Object *obj)
  970. {
  971. ESCCState *s = ESCC(obj);
  972. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  973. unsigned int i;
  974. for (i = 0; i < 2; i++) {
  975. sysbus_init_irq(dev, &s->chn[i].irq);
  976. s->chn[i].chn = 1 - i;
  977. }
  978. s->chn[0].otherchn = &s->chn[1];
  979. s->chn[1].otherchn = &s->chn[0];
  980. sysbus_init_mmio(dev, &s->mmio);
  981. }
  982. static void escc_realize(DeviceState *dev, Error **errp)
  983. {
  984. ESCCState *s = ESCC(dev);
  985. unsigned int i;
  986. s->chn[0].disabled = s->disabled;
  987. s->chn[1].disabled = s->disabled;
  988. memory_region_init_io(&s->mmio, OBJECT(dev), &escc_mem_ops, s, "escc",
  989. ESCC_SIZE << s->it_shift);
  990. for (i = 0; i < 2; i++) {
  991. if (qemu_chr_fe_backend_connected(&s->chn[i].chr)) {
  992. s->chn[i].clock = s->frequency / 2;
  993. qemu_chr_fe_set_handlers(&s->chn[i].chr, serial_can_receive,
  994. serial_receive1, serial_event, NULL,
  995. &s->chn[i], NULL, true);
  996. }
  997. }
  998. if (s->chn[0].type == escc_mouse) {
  999. s->chn[0].hs = qemu_input_handler_register((DeviceState *)(&s->chn[0]),
  1000. &sunmouse_handler);
  1001. }
  1002. if (s->chn[1].type == escc_kbd) {
  1003. s->chn[1].hs = qemu_input_handler_register((DeviceState *)(&s->chn[1]),
  1004. &sunkbd_handler);
  1005. }
  1006. }
  1007. static const Property escc_properties[] = {
  1008. DEFINE_PROP_UINT32("frequency", ESCCState, frequency, 0),
  1009. DEFINE_PROP_UINT32("it_shift", ESCCState, it_shift, 0),
  1010. DEFINE_PROP_BOOL("bit_swap", ESCCState, bit_swap, false),
  1011. DEFINE_PROP_UINT32("disabled", ESCCState, disabled, 0),
  1012. DEFINE_PROP_UINT32("chnBtype", ESCCState, chn[0].type, 0),
  1013. DEFINE_PROP_UINT32("chnAtype", ESCCState, chn[1].type, 0),
  1014. DEFINE_PROP_CHR("chrB", ESCCState, chn[0].chr),
  1015. DEFINE_PROP_CHR("chrA", ESCCState, chn[1].chr),
  1016. DEFINE_PROP_STRING("chnA-sunkbd-layout", ESCCState, chn[1].sunkbd_layout),
  1017. };
  1018. static void escc_class_init(ObjectClass *klass, void *data)
  1019. {
  1020. DeviceClass *dc = DEVICE_CLASS(klass);
  1021. device_class_set_legacy_reset(dc, escc_reset);
  1022. dc->realize = escc_realize;
  1023. dc->vmsd = &vmstate_escc;
  1024. device_class_set_props(dc, escc_properties);
  1025. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  1026. }
  1027. static const TypeInfo escc_info = {
  1028. .name = TYPE_ESCC,
  1029. .parent = TYPE_SYS_BUS_DEVICE,
  1030. .instance_size = sizeof(ESCCState),
  1031. .instance_init = escc_init1,
  1032. .class_init = escc_class_init,
  1033. };
  1034. static void escc_register_types(void)
  1035. {
  1036. type_register_static(&escc_info);
  1037. }
  1038. type_init(escc_register_types)