tototek.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. tototek.c - General ToToTEK flash card programmer routines for uCON64
  3. Copyright (c) 2004 dbjh
  4. Based on Delphi source code by ToToTEK Multi Media. Information in that source
  5. code has been used with permission. However, ToToTEK Multi Media explicitly
  6. stated that the information in that source code may be freely distributed.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <stdlib.h>
  23. #include "misc/bswap.h"
  24. #include "misc/misc.h"
  25. #include "misc/parallel.h"
  26. #include "misc/term.h"
  27. #include "backup/tototek.h"
  28. #ifdef USE_PARALLEL
  29. static void set_data_read (void);
  30. //static void set_data_write (void);
  31. static void set_ai (unsigned char ai);
  32. static void init_port (void);
  33. static void deinit_port (void);
  34. static void end_port (void);
  35. static void set_addr_read (int addr);
  36. static void set_addr_write (int addr);
  37. static unsigned char get_id_byte (unsigned char addr);
  38. static unsigned short port_8, port_9, port_a, port_b, port_c;
  39. void
  40. ttt_init_io (unsigned short port)
  41. {
  42. #if (defined __unix__ || defined __BEOS__) && !defined __MSDOS__
  43. init_conio ();
  44. #endif
  45. if (register_func (ttt_deinit_io) == -1)
  46. {
  47. fputs ("ERROR: Could not register function with register_func()\n", stderr);
  48. exit (1);
  49. }
  50. port_8 = port; // original code used 0x378 for port_8
  51. port_9 = port + 1;
  52. port_a = port + 2;
  53. port_b = port + 3;
  54. port_c = port + 4;
  55. parport_print_info ();
  56. init_port ();
  57. }
  58. void
  59. ttt_deinit_io (void)
  60. {
  61. #if (defined __unix__ || defined __BEOS__) && !defined __MSDOS__
  62. deinit_conio ();
  63. #endif
  64. end_port ();
  65. deinit_port ();
  66. }
  67. static void
  68. set_data_read (void) // original name: set_data_read
  69. {
  70. outportb (port_a, 0); // nastb=1,nib_sel=0,ndstb=1,nwrite=1
  71. }
  72. #if 0
  73. static void
  74. set_data_write (void) // original name: set_data_write
  75. {
  76. outportb (port_a, 1); // nastb=1,nib_sel=0,ndstb=1,nwrite=0
  77. }
  78. #endif
  79. static void
  80. set_ai (unsigned char ai) // original name: {epp_}set_ai
  81. {
  82. outportb (port_a, 1); // set_data_write ()
  83. outportb (port_b, ai);
  84. }
  85. void
  86. ttt_set_ai_data (unsigned char ai, unsigned char data) // original name: set_ai_data
  87. {
  88. set_ai (ai);
  89. outportb (port_a, 1); // set_data_write ()
  90. outportb (port_c, data);
  91. }
  92. static void
  93. init_port (void) // original name: init_port
  94. {
  95. #ifndef USE_PPDEV
  96. outportb (port_8 + 0x402, (inportb (port_8 + 0x402) & 0x1f) | 0x80);
  97. outportb (port_9, 1); // clear EPP time flag
  98. #endif
  99. ttt_set_ai_data (6, 0); // rst=0, wei=0(dis.), rdi=0(dis.)
  100. ttt_set_ai_data (6, 0x84); // rst=1, wei=0(dis.), rdi=0(dis.)
  101. }
  102. static void
  103. deinit_port (void) // original name: Close_end_port
  104. {
  105. outportb (port_a, 1);
  106. outportb (port_b, 0);
  107. outportb (port_a, 4); // port normal now
  108. }
  109. static void
  110. end_port (void) // original name: end_port
  111. {
  112. ttt_set_ai_data (6, 0); // rst=0, wei=0(dis.), rdi=0(dis.)
  113. outportb (port_a, 4); // set_normal ninit=1, nwrite=1
  114. }
  115. void
  116. ttt_rom_enable (void) // original name: romCS_on
  117. {
  118. ttt_set_ai_data (6, 0x84);
  119. }
  120. void
  121. ttt_rom_disable (void) // original name: romCS_off
  122. {
  123. ttt_set_ai_data (6, 0x80);
  124. }
  125. void
  126. ttt_ram_enable (void) // original name: ramCS_on
  127. {
  128. ttt_set_ai_data (6, 0x88);
  129. }
  130. void
  131. ttt_ram_disable (void) // original name: ramCS_off
  132. {
  133. ttt_set_ai_data (6, 0x80);
  134. }
  135. static void
  136. set_addr_write (int addr) // original name: set_Long_adrw
  137. {
  138. ttt_set_ai_data (0, (unsigned char) (addr & 0xff)); // a[7..0]
  139. addr >>= 8;
  140. ttt_set_ai_data (1, (unsigned char) (addr & 0xff)); // a[15..8]
  141. addr >>= 8;
  142. ttt_set_ai_data (2, (unsigned char) (addr & 0xff)); // a[23..16]
  143. set_ai (3);
  144. }
  145. static void
  146. set_addr_read (int addr) // original name: set_Long_adr
  147. {
  148. set_addr_write (addr);
  149. set_data_read (); // ninit=0, nwrite=1
  150. }
  151. void
  152. ttt_write_mem (int addr, unsigned char b) // original name: WriteMEMb
  153. {
  154. set_addr_write (addr);
  155. outportb (port_c, b);
  156. }
  157. static unsigned char
  158. get_id_byte (unsigned char addr) // original name: GETID
  159. {
  160. unsigned char byte;
  161. ttt_rom_enable ();
  162. ttt_set_ai_data (0, addr); // a[7..0] = 0
  163. set_ai (3);
  164. outportb (port_c, 0x90); // write_data ()
  165. set_ai (3);
  166. set_data_read (); // ninit=0, nwrite=1
  167. byte = inportb (port_c); // read_data ()
  168. ttt_rom_disable ();
  169. return byte;
  170. }
  171. unsigned short
  172. ttt_get_id (void) // original name: getIDword
  173. {
  174. unsigned short word;
  175. // eep_reset ();
  176. word = get_id_byte (0); // msg
  177. word <<= 8;
  178. word |= get_id_byte (2); // ID
  179. return word;
  180. }
  181. void
  182. ttt_read_rom_b (int addr, unsigned char *buf) // original name: read_buffb
  183. {
  184. int count;
  185. ttt_rom_enable ();
  186. for (count = 0; count < 0x100; count++)
  187. {
  188. set_addr_read (addr + count);
  189. buf[count] = inportb (port_c); // read_data ()
  190. }
  191. ttt_rom_disable ();
  192. }
  193. void
  194. ttt_read_rom_w (int addr, unsigned char *buf) // original name: read_buff
  195. {
  196. int count;
  197. set_addr_read (addr);
  198. for (count = 0; count < 0x80; count++)
  199. #ifdef WORDS_BIGENDIAN
  200. ((unsigned short *) buf)[count] = bswap_16 (inportw (port_c)); // read_dataw ()
  201. #else
  202. ((unsigned short *) buf)[count] = inportw (port_c); // read_dataw ()
  203. #endif
  204. }
  205. void
  206. ttt_read_ram_b (int addr, unsigned char *buf) // original name: read_rambuff
  207. {
  208. int count;
  209. ttt_ram_enable ();
  210. for (count = 0; count < 0x100; count++)
  211. {
  212. set_addr_read (addr + count);
  213. buf[count] = inportb (port_c); // read_data ()
  214. }
  215. ttt_ram_disable ();
  216. }
  217. void
  218. ttt_read_ram_w (int addr, unsigned char *buf) // original name: readpagerambuff
  219. {
  220. int count;
  221. set_addr_read (addr);
  222. for (count = 0; count < 0x80; count++)
  223. ((unsigned short *) buf)[count] = inportw (port_c);
  224. // read_dataw (); data is doubled for MD-PRO => no problems with endianess
  225. }
  226. // Sharp function
  227. void
  228. ttt_erase_block (int addr) // original name: EraseBLOCK
  229. {
  230. ttt_rom_enable ();
  231. ttt_write_mem (addr, 0x50);
  232. ttt_rom_disable ();
  233. ttt_rom_enable ();
  234. ttt_write_mem (addr, 0x20);
  235. outportb (port_c, 0xd0);
  236. ttt_rom_disable ();
  237. ttt_rom_enable ();
  238. set_ai (3);
  239. set_data_read (); // set read mode
  240. while (inportb (port_c) < 0x80)
  241. ;
  242. ttt_rom_disable ();
  243. }
  244. void
  245. ttt_write_byte_sharp (int addr, unsigned char b) // original name: writeEEPDataB
  246. {
  247. ttt_rom_enable ();
  248. ttt_write_mem (addr, 0x40);
  249. outportb (port_c, b);
  250. ttt_rom_disable ();
  251. }
  252. void
  253. ttt_write_byte_intel (int addr, unsigned char b) // original name: writeIntelEEPDataB
  254. { // MD-PRO function
  255. ttt_rom_enable ();
  256. ttt_write_mem (addr, 0x40);
  257. outportb (port_c, b);
  258. set_data_read ();
  259. while (inportb (port_c) < 0x80)
  260. ;
  261. ttt_rom_disable ();
  262. }
  263. void
  264. ttt_write_page_rom (int addr, unsigned char *buf) // original name: writeEEPDataPAGE
  265. {
  266. int i;
  267. // send command 0xe8
  268. ttt_rom_enable ();
  269. ttt_write_mem (addr, 0xe8);
  270. // read status
  271. set_ai (3);
  272. set_data_read ();
  273. while (inportb (port_c) < 0x80)
  274. ;
  275. set_ai (3);
  276. outportb (port_c, 0x1f);
  277. ttt_set_ai_data (6, 0x94);
  278. set_addr_write (addr);
  279. for (i = 0; i <= 0x1f; i++)
  280. outportb (port_c, buf[(addr + i) & 0x3fff]);
  281. ttt_set_ai_data (6, 0x84);
  282. set_ai (3);
  283. outportb (port_c, 0xd0);
  284. // read status
  285. set_ai (3);
  286. set_data_read ();
  287. while (inportb (port_c) < 0x80)
  288. ;
  289. ttt_rom_disable ();
  290. }
  291. void
  292. ttt_write_byte_ram (int addr, unsigned char b) // original name: writeRAMDataB
  293. {
  294. ttt_ram_enable ();
  295. ttt_write_mem (addr, b);
  296. ttt_ram_disable ();
  297. }
  298. void
  299. ttt_write_page_ram (int addr, unsigned char *buf) // original name: writeRAMDataPAGE
  300. {
  301. int i;
  302. ttt_ram_enable ();
  303. ttt_set_ai_data (6, 0x98);
  304. set_addr_write (addr);
  305. for (i = 0; i < 0x100; i++)
  306. outportb (port_c, buf[(addr + i) & 0x3fff]);
  307. ttt_ram_disable ();
  308. }
  309. void
  310. ttt_write_page_ram2 (int addr, unsigned char *buf) // original name: writeRAMDBDataPAGE
  311. { // MD-PRO function
  312. int i;
  313. ttt_ram_enable ();
  314. ttt_set_ai_data (6, 0x98);
  315. set_addr_write (addr * 2);
  316. for (i = 0; i < 0x80; i++)
  317. {
  318. outportb (port_c, buf[(addr + i) & 0x3fff]);
  319. outportb (port_c, buf[(addr + i) & 0x3fff]);
  320. }
  321. ttt_ram_disable ();
  322. }
  323. #if 0
  324. void
  325. readstatus (void)
  326. { // MD-PRO function
  327. // send command 0xe8
  328. ttt_rom_enable ();
  329. set_ai (3);
  330. set_data_read ();
  331. while (inportb (port_c) < 0x80)
  332. ;
  333. ttt_rom_disable ();
  334. }
  335. #endif
  336. #endif // USE_PARALLEL