ali512x.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * (C) Copyright 2002
  3. * Daniel Engström, Omicron Ceti AB <daniel@omicron.se>.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Based on sc520cdp.c from rolo 1.6:
  9. *----------------------------------------------------------------------
  10. * (C) Copyright 2000
  11. * Sysgo Real-Time Solutions GmbH
  12. * Klein-Winternheim, Germany
  13. *----------------------------------------------------------------------
  14. */
  15. #include <config.h>
  16. #include <common.h>
  17. #include <asm/io.h>
  18. #include <ali512x.h>
  19. /* ALI M5123 Logical device numbers:
  20. * 0 FDC
  21. * 1 unused?
  22. * 2 unused?
  23. * 3 lpt
  24. * 4 UART1
  25. * 5 UART2
  26. * 6 RTC
  27. * 7 mouse/kbd
  28. * 8 CIO
  29. */
  30. /*
  31. ************************************************************
  32. * Some access primitives for the ALi chip: *
  33. ************************************************************
  34. */
  35. static void ali_write(u8 index, u8 value)
  36. {
  37. /* write an arbirary register */
  38. outb(index, ALI_INDEX);
  39. outb(value, ALI_DATA);
  40. }
  41. #if 0
  42. static int ali_read(u8 index)
  43. {
  44. outb(index, ALI_INDEX);
  45. return inb(ALI_DATA);
  46. }
  47. #endif
  48. #define ALI_OPEN() \
  49. outb(0x51, ALI_INDEX); \
  50. outb(0x23, ALI_INDEX)
  51. #define ALI_CLOSE() \
  52. outb(0xbb, ALI_INDEX)
  53. /* Select a logical device */
  54. #define ALI_SELDEV(dev) \
  55. ali_write(0x07, dev)
  56. void ali512x_init(void)
  57. {
  58. ALI_OPEN();
  59. ali_write(0x02, 0x01); /* soft reset */
  60. ali_write(0x03, 0x03); /* disable access to CIOs */
  61. ali_write(0x22, 0x00); /* disable direct powerdown */
  62. ali_write(0x23, 0x00); /* disable auto powerdown */
  63. ali_write(0x24, 0x00); /* IR 8 is active hi, pin26 is PDIR */
  64. ALI_CLOSE();
  65. }
  66. void ali512x_set_fdc(int enabled, u16 io, u8 irq, u8 dma_channel)
  67. {
  68. ALI_OPEN();
  69. ALI_SELDEV(0);
  70. ali_write(0x30, enabled?1:0);
  71. if (enabled) {
  72. ali_write(0x60, io >> 8);
  73. ali_write(0x61, io & 0xff);
  74. ali_write(0x70, irq);
  75. ali_write(0x74, dma_channel);
  76. /* AT mode, no drive swap */
  77. ali_write(0xf0, 0x08);
  78. ali_write(0xf1, 0x00);
  79. ali_write(0xf2, 0xff);
  80. ali_write(0xf4, 0x00);
  81. }
  82. ALI_CLOSE();
  83. }
  84. void ali512x_set_pp(int enabled, u16 io, u8 irq, u8 dma_channel)
  85. {
  86. ALI_OPEN();
  87. ALI_SELDEV(3);
  88. ali_write(0x30, enabled?1:0);
  89. if (enabled) {
  90. ali_write(0x60, io >> 8);
  91. ali_write(0x61, io & 0xff);
  92. ali_write(0x70, irq);
  93. ali_write(0x74, dma_channel);
  94. /* mode: EPP 1.9, ECP FIFO threshold = 7, IRQ active low */
  95. ali_write(0xf0, 0xbc);
  96. /* 12 MHz, Burst DMA in ECP */
  97. ali_write(0xf1, 0x05);
  98. }
  99. ALI_CLOSE();
  100. }
  101. void ali512x_set_uart(int enabled, int index, u16 io, u8 irq)
  102. {
  103. ALI_OPEN();
  104. ALI_SELDEV(index?5:4);
  105. ali_write(0x30, enabled?1:0);
  106. if (enabled) {
  107. ali_write(0x60, io >> 8);
  108. ali_write(0x61, io & 0xff);
  109. ali_write(0x70, irq);
  110. ali_write(0xf0, 0x00);
  111. ali_write(0xf1, 0x00);
  112. /* huh? write 0xf2 twice - a typo in rolo
  113. * or some secret ali errata? Who knows?
  114. */
  115. if (index) {
  116. ali_write(0xf2, 0x00);
  117. }
  118. ali_write(0xf2, 0x0c);
  119. }
  120. ALI_CLOSE();
  121. }
  122. void ali512x_set_uart2_irda(int enabled)
  123. {
  124. ALI_OPEN();
  125. ALI_SELDEV(5);
  126. ali_write(0xf1, enabled?0x48:0x00); /* fullduplex IrDa */
  127. ALI_CLOSE();
  128. }
  129. void ali512x_set_rtc(int enabled, u16 io, u8 irq)
  130. {
  131. ALI_OPEN();
  132. ALI_SELDEV(6);
  133. ali_write(0x30, enabled?1:0);
  134. if (enabled) {
  135. ali_write(0x60, io >> 8);
  136. ali_write(0x61, io & 0xff);
  137. ali_write(0x70, irq);
  138. ali_write(0xf0, 0x00);
  139. }
  140. ALI_CLOSE();
  141. }
  142. void ali512x_set_kbc(int enabled, u8 kbc_irq, u8 mouse_irq)
  143. {
  144. ALI_OPEN();
  145. ALI_SELDEV(7);
  146. ali_write(0x30, enabled?1:0);
  147. if (enabled) {
  148. ali_write(0x70, kbc_irq);
  149. ali_write(0x72, mouse_irq);
  150. ali_write(0xf0, 0x00);
  151. }
  152. ALI_CLOSE();
  153. }
  154. /* Common I/O
  155. *
  156. * (This descripotsion is base on several incompete sources
  157. * since I have not been able to obtain any datasheet for the device
  158. * there may be some mis-understandings burried in here.
  159. * -- Daniel daniel@omicron.se)
  160. *
  161. * There are 22 CIO pins numbered
  162. * 10-17
  163. * 20-25
  164. * 30-37
  165. *
  166. * 20-24 are dedicated CIO pins, the other 17 are muliplexed with
  167. * other functions.
  168. *
  169. * Secondary
  170. * CIO Pin Function Decription
  171. * =======================================================
  172. * CIO10 IRQIN1 Interrupt input 1?
  173. * CIO11 IRQIN2 Interrupt input 2?
  174. * CIO12 IRRX IrDa Receive
  175. * CIO13 IRTX IrDa Transmit
  176. * CIO14 P21 KBC P21 fucntion
  177. * CIO15 P20 KBC P21 fucntion
  178. * CIO16 I2C_CLK I2C Clock
  179. * CIO17 I2C_DAT I2C Data
  180. *
  181. * CIO20 -
  182. * CIO21 -
  183. * CIO22 -
  184. * CIO23 -
  185. * CIO24 -
  186. * CIO25 LOCK Keylock
  187. *
  188. * CIO30 KBC_CLK Keybaord Clock
  189. * CIO31 CS0J General Chip Select decoder CS0J
  190. * CIO32 CS1J General Chip Select decoder CS1J
  191. * CIO33 ALT_KCLK Alternative Keyboard Clock
  192. * CIO34 ALT_KDAT Alternative Keyboard Data
  193. * CIO35 ALT_MCLK Alternative Mouse Clock
  194. * CIO36 ALT_MDAT Alternative Mouse Data
  195. * CIO37 ALT_KBC Alternative KBC select
  196. *
  197. * The CIO use an indirect address scheme.
  198. *
  199. * Reigster 3 in the SIO is used to select the index and data
  200. * port addresses where the CIO I/O registers show up.
  201. * The function selection registers are accessible under
  202. * function SIO 8.
  203. *
  204. * SIO reigster 3 (CIO Address Selection) bit definitions:
  205. * bit 7 CIO index and data registers enabled
  206. * bit 1-0 CIO indirect registers port address select
  207. * 0 index = 0xE0 data = 0xE1
  208. * 1 index = 0xE2 data = 0xE3
  209. * 2 index = 0xE4 data = 0xE5
  210. * 3 index = 0xEA data = 0xEB
  211. *
  212. * There are three CIO I/O register accessed via CIO index port and CIO data port
  213. * 0x01 CIO 10-17 data
  214. * 0x02 CIO 20-25 data (bits 7-6 unused)
  215. * 0x03 CIO 30-37 data
  216. *
  217. *
  218. * The pin function is accessed through normal
  219. * SIO registers, each register have the same format:
  220. *
  221. * Bit Function Value
  222. * 0 Input/output 1=input
  223. * 1 Polarity of signal 1=inverted
  224. * 2 Unused ??
  225. * 3 Function (normal or special) 1=special
  226. * 7-4 Unused
  227. *
  228. * SIO REG
  229. * 0xe0 CIO 10 Config
  230. * 0xe1 CIO 11 Config
  231. * 0xe2 CIO 12 Config
  232. * 0xe3 CIO 13 Config
  233. * 0xe4 CIO 14 Config
  234. * 0xe5 CIO 15 Config
  235. * 0xe6 CIO 16 Config
  236. * 0xe7 CIO 16 Config
  237. *
  238. * 0xe8 CIO 20 Config
  239. * 0xe9 CIO 21 Config
  240. * 0xea CIO 22 Config
  241. * 0xeb CIO 23 Config
  242. * 0xec CIO 24 Config
  243. * 0xed CIO 25 Config
  244. *
  245. * 0xf5 CIO 30 Config
  246. * 0xf6 CIO 31 Config
  247. * 0xf7 CIO 32 Config
  248. * 0xf8 CIO 33 Config
  249. * 0xf9 CIO 34 Config
  250. * 0xfa CIO 35 Config
  251. * 0xfb CIO 36 Config
  252. * 0xfc CIO 37 Config
  253. *
  254. */
  255. #define ALI_CIO_PORT_SEL 0x83
  256. #define ALI_CIO_INDEX 0xea
  257. #define ALI_CIO_DATA 0xeb
  258. void ali512x_set_cio(int enabled)
  259. {
  260. int i;
  261. ALI_OPEN();
  262. if (enabled) {
  263. ali_write(0x3, ALI_CIO_PORT_SEL); /* Enable CIO data register */
  264. } else {
  265. ali_write(0x3, ALI_CIO_PORT_SEL & ~0x80);
  266. }
  267. ALI_SELDEV(8);
  268. ali_write(0x30, enabled?1:0);
  269. /* set all pins to input to start with */
  270. for (i=0xe0;i<0xee;i++) {
  271. ali_write(i, 1);
  272. }
  273. for (i=0xf5;i<0xfe;i++) {
  274. ali_write(i, 1);
  275. }
  276. ALI_CLOSE();
  277. }
  278. void ali512x_cio_function(int pin, int special, int inv, int input)
  279. {
  280. u8 data;
  281. u8 addr;
  282. /* valid pins are 10-17, 20-25 and 30-37 */
  283. if (pin >= 10 && pin <= 17) {
  284. addr = 0xe0+(pin&7);
  285. } else if (pin >= 20 && pin <= 25) {
  286. addr = 0xe8+(pin&7);
  287. } else if (pin >= 30 && pin <= 37) {
  288. addr = 0xf5+(pin&7);
  289. } else {
  290. return;
  291. }
  292. ALI_OPEN();
  293. ALI_SELDEV(8);
  294. data=0xf4;
  295. if (special) {
  296. data |= 0x08;
  297. } else {
  298. if (inv) {
  299. data |= 0x02;
  300. }
  301. if (input) {
  302. data |= 0x01;
  303. }
  304. }
  305. ali_write(addr, data);
  306. ALI_CLOSE();
  307. }
  308. void ali512x_cio_out(int pin, int value)
  309. {
  310. u8 reg;
  311. u8 data;
  312. u8 bit;
  313. reg = pin/10;
  314. bit = 1 << (pin%10);
  315. outb(reg, ALI_CIO_INDEX); /* select I/O register */
  316. data = inb(ALI_CIO_DATA);
  317. if (value) {
  318. data |= bit;
  319. } else {
  320. data &= ~bit;
  321. }
  322. outb(data, ALI_CIO_DATA);
  323. }
  324. int ali512x_cio_in(int pin)
  325. {
  326. u8 reg;
  327. u8 data;
  328. u8 bit;
  329. /* valid pins are 10-17, 20-25 and 30-37 */
  330. reg = pin/10;
  331. bit = 1 << (pin%10);
  332. outb(reg, ALI_CIO_INDEX); /* select I/O register */
  333. data = inb(ALI_CIO_DATA);
  334. return data & bit;
  335. }