via686.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * (C) Copyright 2002
  3. * Hyperion Entertainment, Hans-JoergF@hyperion-entertainment.com
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <pci.h>
  25. #include <ata.h>
  26. #include "memio.h"
  27. #include "articiaS.h"
  28. #include "via686.h"
  29. #include "i8259.h"
  30. #undef VIA_DEBUG
  31. #ifdef VIA_DEBUG
  32. #define PRINTF(fmt,args...) printf (fmt ,##args)
  33. #else
  34. #define PRINTF(fmt,args...)
  35. #endif
  36. /* Setup the ISA-to-PCI host bridge */
  37. void via_isa_init(pci_dev_t dev, struct pci_config_table *table)
  38. {
  39. char regval;
  40. if (PCI_FUNC(dev) == 0)
  41. {
  42. PRINTF("... PCI-to-ISA bridge, dev=0x%X\n", dev);
  43. /* Enable I/O Recovery time */
  44. pci_write_config_byte(dev, 0x40, 0x08);
  45. /* Enable ISA refresh */
  46. pci_write_config_byte(dev, 0x41, 0x41); /* was 01 */
  47. /* Enable ISA line buffer */
  48. pci_write_config_byte(dev, 0x45, 0x80);
  49. /* Gate INTR, and flush line buffer */
  50. pci_write_config_byte(dev, 0x46, 0x60);
  51. /* Enable EISA ports 4D0/4D1. Do we need this ? */
  52. pci_write_config_byte(dev, 0x47, 0xe6); /* was 20 */
  53. /* 512 K PCI Decode */
  54. pci_write_config_byte(dev, 0x48, 0x01);
  55. /* Wait for PGNT before grant to ISA Master/DMA */
  56. /* ports 0-FF to SDBus */
  57. /* IRQ 14 and 15 for ide 0/1 */
  58. pci_write_config_byte(dev, 0x4a, 0x04); /* Was c4 */
  59. /* Plug'n'Play */
  60. /* Parallel DRQ 3, Floppy DRQ 2 (default) */
  61. pci_write_config_byte(dev, 0x50, 0x0e);
  62. /* IRQ Routing for Floppy and Parallel port */
  63. /* IRQ 6 for floppy, IRQ 7 for parallel port */
  64. pci_write_config_byte(dev, 0x51, 0x76);
  65. /* IRQ Routing for serial ports (take IRQ 3 and 4) */
  66. pci_write_config_byte(dev, 0x52, 0x34);
  67. /* All IRQ's level triggered. */
  68. pci_write_config_byte(dev, 0x54, 0x00);
  69. /* PCI IRQ's all at IRQ 9 */
  70. pci_write_config_byte(dev, 0x55, 0x90);
  71. pci_write_config_byte(dev, 0x56, 0x99);
  72. pci_write_config_byte(dev, 0x57, 0x90);
  73. /* Enable Keyboard */
  74. pci_read_config_byte(dev, 0x5A, &regval);
  75. regval |= 0x01;
  76. pci_write_config_byte(dev, 0x5A, regval);
  77. pci_write_config_byte(dev, 0x80, 0);
  78. pci_write_config_byte(dev, 0x85, 0x01);
  79. /* pci_write_config_byte(dev, 0x77, 0x00); */
  80. }
  81. }
  82. /*
  83. * Initialize PNP irq routing
  84. */
  85. void via_init_irq_routing(uint8 irq_map[])
  86. {
  87. char *s;
  88. uint8 level_edge_bits = 0xf;
  89. /* Set irq routings */
  90. pci_write_cfg_byte(0, 7<<3, 0x55, irq_map[0]<<4);
  91. pci_write_cfg_byte(0, 7<<3, 0x56, irq_map[1] | irq_map[2]<<4);
  92. pci_write_cfg_byte(0, 7<<3, 0x57, irq_map[3]<<4);
  93. /*
  94. * Gather level/edge bits
  95. * Default is to assume level triggered
  96. */
  97. s = getenv("pci_irqa_select");
  98. if (s && strcmp(s, "level") == 0)
  99. level_edge_bits &= ~0x01;
  100. s = getenv("pci_irqb_select");
  101. if (s && strcmp(s, "level") == 0)
  102. level_edge_bits &= ~0x02;
  103. s = getenv("pci_irqc_select");
  104. if (s && strcmp(s, "level") == 0)
  105. level_edge_bits &= ~0x04;
  106. s = getenv("pci_irqd_select");
  107. if (s && strcmp(s, "level") == 0)
  108. level_edge_bits &= ~0x08;
  109. PRINTF("IRQ map\n");
  110. PRINTF("%d: %s\n", irq_map[0], level_edge_bits&0x1 ? "edge" : "level");
  111. PRINTF("%d: %s\n", irq_map[1], level_edge_bits&0x2 ? "edge" : "level");
  112. PRINTF("%d: %s\n", irq_map[2], level_edge_bits&0x4 ? "edge" : "level");
  113. PRINTF("%d: %s\n", irq_map[3], level_edge_bits&0x8 ? "edge" : "level");
  114. pci_write_cfg_byte(0, 7<<3, 0x54, level_edge_bits);
  115. PRINTF("%02x %02x %02x %02x\n", pci_read_cfg_byte(0, 7<<3, 0x54),
  116. pci_read_cfg_byte(0, 7<<3, 0x55), pci_read_cfg_byte(0, 7<<3, 0x56),
  117. pci_read_cfg_byte(0, 7<<3, 0x57));
  118. }
  119. /* Setup the IDE controller. This doesn't seem to work yet. I/O to an IDE controller port */
  120. /* always return the last character output on the serial port (!) */
  121. /* This function is called by the pnp-library when it encounters 0:7:1 */
  122. void via_cfgfunc_ide_init(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table)
  123. {
  124. PRINTF("... IDE controller, dev=0x%X\n", dev);
  125. /* Enable both IDE channels. */
  126. pci_write_config_byte(dev, 0x40, 0x03);
  127. /* udelay(10000); */
  128. /* udelay(10000); */
  129. /* Enable IO Space */
  130. pci_write_config_word(dev, 0x04, 0x03);
  131. /* Set to compatibility mode */
  132. pci_write_config_byte(dev, 0x09, 0x8A); /* WAS: 0x8f); */
  133. /* Set to legacy interrupt mode */
  134. pci_write_config_byte(dev, 0x3d, 0x00); /* WAS: 0x01); */
  135. }
  136. /* Set the base address of the floppy controller to 0x3F0 */
  137. void via_fdc_init(pci_dev_t dev)
  138. {
  139. unsigned char c;
  140. /* Enable Configuration mode */
  141. pci_read_config_byte(dev, 0x85, &c);
  142. c |= 0x02;
  143. pci_write_config_byte(dev, 0x85, c);
  144. /* Set floppy controller port to 0x3F0. */
  145. SIO_WRITE_CONFIG(0xE3, (0x3F<<2));
  146. /* Enable floppy controller */
  147. SIO_READ_CONFIG(0xE2, c);
  148. c |= 0x10;
  149. SIO_WRITE_CONFIG(0xE2, c);
  150. /* Switch of configuration mode */
  151. pci_read_config_byte(dev, 0x85, &c);
  152. c &= ~0x02;
  153. pci_write_config_byte(dev, 0x85, c);
  154. }
  155. /* Init function 0 of the via southbridge. Called by the pnp-library */
  156. void via_cfgfunc_via686(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table)
  157. {
  158. if (PCI_FUNC(dev) == 0)
  159. {
  160. /* FIXME: Try to generate a PCI reset */
  161. /* unsigned char c; */
  162. /* pci_read_config_byte(dev, 0x47, &c); */
  163. /* pci_write_config_byte(dev, 0x47, c | 0x01); */
  164. via_isa_init(dev, table);
  165. via_fdc_init(dev);
  166. }
  167. }
  168. __asm (" .globl via_calibrate_time_base \n"
  169. "via_calibrate_time_base: \n"
  170. " lis 9, 0xfe00 \n"
  171. " li 0, 0x00 \n"
  172. " mttbu 0 \n"
  173. " mttbl 0 \n"
  174. "ctb_loop: \n"
  175. " lbz 0, 0x61(9) \n"
  176. " eieio \n"
  177. " andi. 0, 0, 0x20 \n"
  178. " beq ctb_loop \n"
  179. "ctb_done: \n"
  180. " mftb 3 \n"
  181. " blr");
  182. extern unsigned long via_calibrate_time_base(void);
  183. void via_calibrate_bus_freq(void)
  184. {
  185. DECLARE_GLOBAL_DATA_PTR;
  186. unsigned long tb;
  187. /* This is 20 microseconds */
  188. #define CALIBRATE_TIME 28636
  189. /* Enable the timer (and disable speaker) */
  190. unsigned char c;
  191. c = in_byte(0x61);
  192. out_byte(0x61, ((c & ~0x02) | 0x01));
  193. /* Set timer 2 to low/high writing */
  194. out_byte(0x43, 0xb0);
  195. out_byte(0x42, CALIBRATE_TIME & 0xff);
  196. out_byte(0x42, CALIBRATE_TIME >>8);
  197. /* Read the time base */
  198. tb = via_calibrate_time_base();
  199. if (tb >= 700000)
  200. gd->bus_clk = 133333333;
  201. else
  202. gd->bus_clk = 100000000;
  203. }
  204. void ide_led(uchar led, uchar status)
  205. {
  206. /* unsigned char c = in_byte(0x92); */
  207. /* if (!status) */
  208. /* out_byte(0x92, c | 0xC0); */
  209. /* else */
  210. /* out_byte(0x92, c & ~0xC0); */
  211. }
  212. void via_init_afterscan(void)
  213. {
  214. /* Modify IDE controller setup */
  215. pci_write_cfg_byte(0, 7<<3|1, PCI_LATENCY_TIMER, 0x20);
  216. pci_write_cfg_byte(0, 7<<3|1, PCI_COMMAND, PCI_COMMAND_IO|PCI_COMMAND_MEMORY|PCI_COMMAND_MASTER);
  217. pci_write_cfg_byte(0, 7<<3|1, PCI_INTERRUPT_LINE, 0xff);
  218. pci_write_cfg_byte(0, 7<<3|1, 0x40, 0x0b); /* FIXME: Might depend on drives connected */
  219. pci_write_cfg_byte(0, 7<<3|1, 0x41, 0x42); /* FIXME: Might depend on drives connected */
  220. pci_write_cfg_byte(0, 7<<3|1, 0x43, 0x05);
  221. pci_write_cfg_byte(0, 7<<3|1, 0x44, 0x18);
  222. pci_write_cfg_byte(0, 7<<3|1, 0x45, 0x10);
  223. pci_write_cfg_byte(0, 7<<3|1, 0x4e, 0x22); /* FIXME: Not documented, but set in PC bios */
  224. pci_write_cfg_byte(0, 7<<3|1, 0x4f, 0x20); /* FIXME: Not documented */
  225. /* Modify some values in the USB controller */
  226. pci_write_cfg_byte(0, 7<<3|2, 0x05, 0x17);
  227. pci_write_cfg_byte(0, 7<<3|2, 0x06, 0x01);
  228. pci_write_cfg_byte(0, 7<<3|2, 0x41, 0x12);
  229. pci_write_cfg_byte(0, 7<<3|2, 0x42, 0x03);
  230. pci_write_cfg_byte(0, 7<<3|2, PCI_LATENCY_TIMER, 0x40);
  231. pci_write_cfg_byte(0, 7<<3|3, 0x05, 0x17);
  232. pci_write_cfg_byte(0, 7<<3|3, 0x06, 0x01);
  233. pci_write_cfg_byte(0, 7<<3|3, 0x41, 0x12);
  234. pci_write_cfg_byte(0, 7<<3|3, 0x42, 0x03);
  235. pci_write_cfg_byte(0, 7<<3|3, PCI_LATENCY_TIMER, 0x40);
  236. }