dv-mn103iop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /* This file is part of the program GDB, the GNU debugger.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include "sim-main.h"
  18. #include "hw-main.h"
  19. /* DEVICE
  20. mn103iop - mn103002 I/O ports 0-3.
  21. DESCRIPTION
  22. Implements the mn103002 i/o ports as described in the mn103002 user guide.
  23. PROPERTIES
  24. reg = <ioport-addr> <ioport-size> ...
  25. BUGS
  26. */
  27. /* The I/O ports' registers' address block */
  28. struct mn103iop_block {
  29. unsigned_word base;
  30. unsigned_word bound;
  31. };
  32. enum io_port_register_types {
  33. P0OUT,
  34. P1OUT,
  35. P2OUT,
  36. P3OUT,
  37. P0MD,
  38. P1MD,
  39. P2MD,
  40. P3MD,
  41. P2SS,
  42. P4SS,
  43. P0DIR,
  44. P1DIR,
  45. P2DIR,
  46. P3DIR,
  47. P0IN,
  48. P1IN,
  49. P2IN,
  50. P3IN,
  51. };
  52. #define NR_PORTS 4
  53. enum {
  54. OUTPUT_BLOCK,
  55. MODE_BLOCK,
  56. DED_CTRL_BLOCK,
  57. CTRL_BLOCK,
  58. PIN_BLOCK,
  59. NR_BLOCKS
  60. };
  61. typedef struct _mn10300_ioport {
  62. uint8_t output, output_mode, control, pin;
  63. struct hw_event *event;
  64. } mn10300_ioport;
  65. struct mn103iop {
  66. struct mn103iop_block block[NR_BLOCKS];
  67. mn10300_ioport port[NR_PORTS];
  68. uint8_t p2ss, p4ss;
  69. };
  70. /* Finish off the partially created hw device. Attach our local
  71. callbacks. Wire up our port names etc */
  72. static hw_io_read_buffer_method mn103iop_io_read_buffer;
  73. static hw_io_write_buffer_method mn103iop_io_write_buffer;
  74. static void
  75. attach_mn103iop_regs (struct hw *me,
  76. struct mn103iop *io_port)
  77. {
  78. int i;
  79. unsigned_word attach_address;
  80. int attach_space;
  81. unsigned attach_size;
  82. reg_property_spec reg;
  83. if (hw_find_property (me, "reg") == NULL)
  84. hw_abort (me, "Missing \"reg\" property");
  85. for (i=0; i < NR_BLOCKS; ++i )
  86. {
  87. if (!hw_find_reg_array_property (me, "reg", i, &reg))
  88. hw_abort (me, "\"reg\" property must contain five addr/size entries");
  89. hw_unit_address_to_attach_address (hw_parent (me),
  90. &reg.address,
  91. &attach_space,
  92. &attach_address,
  93. me);
  94. io_port->block[i].base = attach_address;
  95. hw_unit_size_to_attach_size (hw_parent (me),
  96. &reg.size,
  97. &attach_size, me);
  98. io_port->block[i].bound = attach_address + (attach_size - 1);
  99. hw_attach_address (hw_parent (me),
  100. 0,
  101. attach_space, attach_address, attach_size,
  102. me);
  103. }
  104. }
  105. static void
  106. mn103iop_finish (struct hw *me)
  107. {
  108. struct mn103iop *io_port;
  109. int i;
  110. io_port = HW_ZALLOC (me, struct mn103iop);
  111. set_hw_data (me, io_port);
  112. set_hw_io_read_buffer (me, mn103iop_io_read_buffer);
  113. set_hw_io_write_buffer (me, mn103iop_io_write_buffer);
  114. /* Attach ourself to our parent bus */
  115. attach_mn103iop_regs (me, io_port);
  116. /* Initialize the i/o port registers. */
  117. for ( i=0; i<NR_PORTS; ++i )
  118. {
  119. io_port->port[i].output = 0;
  120. io_port->port[i].output_mode = 0;
  121. io_port->port[i].control = 0;
  122. io_port->port[i].pin = 0;
  123. }
  124. io_port->port[2].output_mode = 0xff;
  125. io_port->p2ss = 0;
  126. io_port->p4ss = 0x0f;
  127. }
  128. /* read and write */
  129. static int
  130. decode_addr (struct hw *me,
  131. struct mn103iop *io_port,
  132. unsigned_word address)
  133. {
  134. unsigned_word offset;
  135. offset = address - io_port->block[0].base;
  136. switch (offset)
  137. {
  138. case 0x00: return P0OUT;
  139. case 0x01: return P1OUT;
  140. case 0x04: return P2OUT;
  141. case 0x05: return P3OUT;
  142. case 0x20: return P0MD;
  143. case 0x21: return P1MD;
  144. case 0x24: return P2MD;
  145. case 0x25: return P3MD;
  146. case 0x44: return P2SS;
  147. case 0x48: return P4SS;
  148. case 0x60: return P0DIR;
  149. case 0x61: return P1DIR;
  150. case 0x64: return P2DIR;
  151. case 0x65: return P3DIR;
  152. case 0x80: return P0IN;
  153. case 0x81: return P1IN;
  154. case 0x84: return P2IN;
  155. case 0x85: return P3IN;
  156. default:
  157. {
  158. hw_abort (me, "bad address");
  159. return -1;
  160. }
  161. }
  162. }
  163. static void
  164. read_output_reg (struct hw *me,
  165. struct mn103iop *io_port,
  166. unsigned_word io_port_reg,
  167. const void *dest,
  168. unsigned nr_bytes)
  169. {
  170. if ( nr_bytes == 1 )
  171. {
  172. *(uint8_t *)dest = io_port->port[io_port_reg].output;
  173. }
  174. else
  175. {
  176. hw_abort (me, "bad read size of %d bytes from P%dOUT.", nr_bytes,
  177. io_port_reg);
  178. }
  179. }
  180. static void
  181. read_output_mode_reg (struct hw *me,
  182. struct mn103iop *io_port,
  183. unsigned_word io_port_reg,
  184. const void *dest,
  185. unsigned nr_bytes)
  186. {
  187. if ( nr_bytes == 1 )
  188. {
  189. /* check if there are fields which can't be written and
  190. take appropriate action depending what bits are set */
  191. *(uint8_t *)dest = io_port->port[io_port_reg].output_mode;
  192. }
  193. else
  194. {
  195. hw_abort (me, "bad read size of %d bytes to P%dMD.", nr_bytes,
  196. io_port_reg);
  197. }
  198. }
  199. static void
  200. read_control_reg (struct hw *me,
  201. struct mn103iop *io_port,
  202. unsigned_word io_port_reg,
  203. const void *dest,
  204. unsigned nr_bytes)
  205. {
  206. if ( nr_bytes == 1 )
  207. {
  208. *(uint8_t *)dest = io_port->port[io_port_reg].control;
  209. }
  210. else
  211. {
  212. hw_abort (me, "bad read size of %d bytes to P%dDIR.", nr_bytes,
  213. io_port_reg);
  214. }
  215. }
  216. static void
  217. read_pin_reg (struct hw *me,
  218. struct mn103iop *io_port,
  219. unsigned_word io_port_reg,
  220. const void *dest,
  221. unsigned nr_bytes)
  222. {
  223. if ( nr_bytes == 1 )
  224. {
  225. *(uint8_t *)dest = io_port->port[io_port_reg].pin;
  226. }
  227. else
  228. {
  229. hw_abort (me, "bad read size of %d bytes to P%dIN.", nr_bytes,
  230. io_port_reg);
  231. }
  232. }
  233. static void
  234. read_dedicated_control_reg (struct hw *me,
  235. struct mn103iop *io_port,
  236. unsigned_word io_port_reg,
  237. const void *dest,
  238. unsigned nr_bytes)
  239. {
  240. if ( nr_bytes == 1 )
  241. {
  242. /* select on io_port_reg: */
  243. if ( io_port_reg == P2SS )
  244. {
  245. *(uint8_t *)dest = io_port->p2ss;
  246. }
  247. else
  248. {
  249. *(uint8_t *)dest = io_port->p4ss;
  250. }
  251. }
  252. else
  253. {
  254. hw_abort (me, "bad read size of %d bytes to PSS.", nr_bytes);
  255. }
  256. }
  257. static unsigned
  258. mn103iop_io_read_buffer (struct hw *me,
  259. void *dest,
  260. int space,
  261. unsigned_word base,
  262. unsigned nr_bytes)
  263. {
  264. struct mn103iop *io_port = hw_data (me);
  265. enum io_port_register_types io_port_reg;
  266. HW_TRACE ((me, "read 0x%08lx %d", (long) base, (int) nr_bytes));
  267. io_port_reg = decode_addr (me, io_port, base);
  268. switch (io_port_reg)
  269. {
  270. /* Port output registers */
  271. case P0OUT:
  272. case P1OUT:
  273. case P2OUT:
  274. case P3OUT:
  275. read_output_reg(me, io_port, io_port_reg-P0OUT, dest, nr_bytes);
  276. break;
  277. /* Port output mode registers */
  278. case P0MD:
  279. case P1MD:
  280. case P2MD:
  281. case P3MD:
  282. read_output_mode_reg(me, io_port, io_port_reg-P0MD, dest, nr_bytes);
  283. break;
  284. /* Port control registers */
  285. case P0DIR:
  286. case P1DIR:
  287. case P2DIR:
  288. case P3DIR:
  289. read_control_reg(me, io_port, io_port_reg-P0DIR, dest, nr_bytes);
  290. break;
  291. /* Port pin registers */
  292. case P0IN:
  293. case P1IN:
  294. case P2IN:
  295. read_pin_reg(me, io_port, io_port_reg-P0IN, dest, nr_bytes);
  296. break;
  297. case P2SS:
  298. case P4SS:
  299. read_dedicated_control_reg(me, io_port, io_port_reg, dest, nr_bytes);
  300. break;
  301. default:
  302. hw_abort(me, "invalid address");
  303. }
  304. return nr_bytes;
  305. }
  306. static void
  307. write_output_reg (struct hw *me,
  308. struct mn103iop *io_port,
  309. unsigned_word io_port_reg,
  310. const void *source,
  311. unsigned nr_bytes)
  312. {
  313. uint8_t buf = *(uint8_t *)source;
  314. if ( nr_bytes == 1 )
  315. {
  316. if ( io_port_reg == 3 && (buf & 0xfc) != 0 )
  317. {
  318. hw_abort(me, "Cannot write to read-only bits of P3OUT.");
  319. }
  320. else
  321. {
  322. io_port->port[io_port_reg].output = buf;
  323. }
  324. }
  325. else
  326. {
  327. hw_abort (me, "bad read size of %d bytes from P%dOUT.", nr_bytes,
  328. io_port_reg);
  329. }
  330. }
  331. static void
  332. write_output_mode_reg (struct hw *me,
  333. struct mn103iop *io_port,
  334. unsigned_word io_port_reg,
  335. const void *source,
  336. unsigned nr_bytes)
  337. {
  338. uint8_t buf = *(uint8_t *)source;
  339. if ( nr_bytes == 1 )
  340. {
  341. /* check if there are fields which can't be written and
  342. take appropriate action depending what bits are set */
  343. if ( ( io_port_reg == 3 && (buf & 0xfc) != 0 )
  344. || ( (io_port_reg == 0 || io_port_reg == 1) && (buf & 0xfe) != 0 ) )
  345. {
  346. hw_abort(me, "Cannot write to read-only bits of output mode register.");
  347. }
  348. else
  349. {
  350. io_port->port[io_port_reg].output_mode = buf;
  351. }
  352. }
  353. else
  354. {
  355. hw_abort (me, "bad write size of %d bytes to P%dMD.", nr_bytes,
  356. io_port_reg);
  357. }
  358. }
  359. static void
  360. write_control_reg (struct hw *me,
  361. struct mn103iop *io_port,
  362. unsigned_word io_port_reg,
  363. const void *source,
  364. unsigned nr_bytes)
  365. {
  366. uint8_t buf = *(uint8_t *)source;
  367. if ( nr_bytes == 1 )
  368. {
  369. if ( io_port_reg == 3 && (buf & 0xfc) != 0 )
  370. {
  371. hw_abort(me, "Cannot write to read-only bits of P3DIR.");
  372. }
  373. else
  374. {
  375. io_port->port[io_port_reg].control = buf;
  376. }
  377. }
  378. else
  379. {
  380. hw_abort (me, "bad write size of %d bytes to P%dDIR.", nr_bytes,
  381. io_port_reg);
  382. }
  383. }
  384. static void
  385. write_dedicated_control_reg (struct hw *me,
  386. struct mn103iop *io_port,
  387. unsigned_word io_port_reg,
  388. const void *source,
  389. unsigned nr_bytes)
  390. {
  391. uint8_t buf = *(uint8_t *)source;
  392. if ( nr_bytes == 1 )
  393. {
  394. /* select on io_port_reg: */
  395. if ( io_port_reg == P2SS )
  396. {
  397. if ( (buf & 0xfc) != 0 )
  398. {
  399. hw_abort(me, "Cannot write to read-only bits in p2ss.");
  400. }
  401. else
  402. {
  403. io_port->p2ss = buf;
  404. }
  405. }
  406. else
  407. {
  408. if ( (buf & 0xf0) != 0 )
  409. {
  410. hw_abort(me, "Cannot write to read-only bits in p4ss.");
  411. }
  412. else
  413. {
  414. io_port->p4ss = buf;
  415. }
  416. }
  417. }
  418. else
  419. {
  420. hw_abort (me, "bad write size of %d bytes to PSS.", nr_bytes);
  421. }
  422. }
  423. static unsigned
  424. mn103iop_io_write_buffer (struct hw *me,
  425. const void *source,
  426. int space,
  427. unsigned_word base,
  428. unsigned nr_bytes)
  429. {
  430. struct mn103iop *io_port = hw_data (me);
  431. enum io_port_register_types io_port_reg;
  432. HW_TRACE ((me, "write 0x%08lx %d", (long) base, (int) nr_bytes));
  433. io_port_reg = decode_addr (me, io_port, base);
  434. switch (io_port_reg)
  435. {
  436. /* Port output registers */
  437. case P0OUT:
  438. case P1OUT:
  439. case P2OUT:
  440. case P3OUT:
  441. write_output_reg(me, io_port, io_port_reg-P0OUT, source, nr_bytes);
  442. break;
  443. /* Port output mode registers */
  444. case P0MD:
  445. case P1MD:
  446. case P2MD:
  447. case P3MD:
  448. write_output_mode_reg(me, io_port, io_port_reg-P0MD, source, nr_bytes);
  449. break;
  450. /* Port control registers */
  451. case P0DIR:
  452. case P1DIR:
  453. case P2DIR:
  454. case P3DIR:
  455. write_control_reg(me, io_port, io_port_reg-P0DIR, source, nr_bytes);
  456. break;
  457. /* Port pin registers */
  458. case P0IN:
  459. case P1IN:
  460. case P2IN:
  461. hw_abort(me, "Cannot write to pin register.");
  462. break;
  463. case P2SS:
  464. case P4SS:
  465. write_dedicated_control_reg(me, io_port, io_port_reg, source, nr_bytes);
  466. break;
  467. default:
  468. hw_abort(me, "invalid address");
  469. }
  470. return nr_bytes;
  471. }
  472. const struct hw_descriptor dv_mn103iop_descriptor[] = {
  473. { "mn103iop", mn103iop_finish, },
  474. { NULL },
  475. };