via686.c 8.5 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. DECLARE_GLOBAL_DATA_PTR;
  31. #undef VIA_DEBUG
  32. #ifdef VIA_DEBUG
  33. #define PRINTF(fmt,args...) printf (fmt ,##args)
  34. #else
  35. #define PRINTF(fmt,args...)
  36. #endif
  37. /* Setup the ISA-to-PCI host bridge */
  38. void via_isa_init(pci_dev_t dev, struct pci_config_table *table)
  39. {
  40. char regval;
  41. if (PCI_FUNC(dev) == 0)
  42. {
  43. PRINTF("... PCI-to-ISA bridge, dev=0x%X\n", dev);
  44. /* Enable I/O Recovery time */
  45. pci_write_config_byte(dev, 0x40, 0x08);
  46. /* Enable ISA refresh */
  47. pci_write_config_byte(dev, 0x41, 0x41); /* was 01 */
  48. /* Enable ISA line buffer */
  49. pci_write_config_byte(dev, 0x45, 0x80);
  50. /* Gate INTR, and flush line buffer */
  51. pci_write_config_byte(dev, 0x46, 0x60);
  52. /* Enable EISA ports 4D0/4D1. Do we need this ? */
  53. pci_write_config_byte(dev, 0x47, 0xe6); /* was 20 */
  54. /* 512 K PCI Decode */
  55. pci_write_config_byte(dev, 0x48, 0x01);
  56. /* Wait for PGNT before grant to ISA Master/DMA */
  57. /* ports 0-FF to SDBus */
  58. /* IRQ 14 and 15 for ide 0/1 */
  59. pci_write_config_byte(dev, 0x4a, 0x04); /* Was c4 */
  60. /* Plug'n'Play */
  61. /* Parallel DRQ 3, Floppy DRQ 2 (default) */
  62. pci_write_config_byte(dev, 0x50, 0x0e);
  63. /* IRQ Routing for Floppy and Parallel port */
  64. /* IRQ 6 for floppy, IRQ 7 for parallel port */
  65. pci_write_config_byte(dev, 0x51, 0x76);
  66. /* IRQ Routing for serial ports (take IRQ 3 and 4) */
  67. pci_write_config_byte(dev, 0x52, 0x34);
  68. /* All IRQ's level triggered. */
  69. pci_write_config_byte(dev, 0x54, 0x00);
  70. /* PCI IRQ's all at IRQ 9 */
  71. pci_write_config_byte(dev, 0x55, 0x90);
  72. pci_write_config_byte(dev, 0x56, 0x99);
  73. pci_write_config_byte(dev, 0x57, 0x90);
  74. /* Enable Keyboard */
  75. pci_read_config_byte(dev, 0x5A, &regval);
  76. regval |= 0x01;
  77. pci_write_config_byte(dev, 0x5A, regval);
  78. pci_write_config_byte(dev, 0x80, 0);
  79. pci_write_config_byte(dev, 0x85, 0x01);
  80. /* pci_write_config_byte(dev, 0x77, 0x00); */
  81. }
  82. }
  83. /*
  84. * Initialize PNP irq routing
  85. */
  86. void via_init_irq_routing(uint8 irq_map[])
  87. {
  88. char *s;
  89. uint8 level_edge_bits = 0xf;
  90. /* Set irq routings */
  91. pci_write_cfg_byte(0, 7<<3, 0x55, irq_map[0]<<4);
  92. pci_write_cfg_byte(0, 7<<3, 0x56, irq_map[1] | irq_map[2]<<4);
  93. pci_write_cfg_byte(0, 7<<3, 0x57, irq_map[3]<<4);
  94. /*
  95. * Gather level/edge bits
  96. * Default is to assume level triggered
  97. */
  98. s = getenv("pci_irqa_select");
  99. if (s && strcmp(s, "level") == 0)
  100. level_edge_bits &= ~0x01;
  101. s = getenv("pci_irqb_select");
  102. if (s && strcmp(s, "level") == 0)
  103. level_edge_bits &= ~0x02;
  104. s = getenv("pci_irqc_select");
  105. if (s && strcmp(s, "level") == 0)
  106. level_edge_bits &= ~0x04;
  107. s = getenv("pci_irqd_select");
  108. if (s && strcmp(s, "level") == 0)
  109. level_edge_bits &= ~0x08;
  110. PRINTF("IRQ map\n");
  111. PRINTF("%d: %s\n", irq_map[0], level_edge_bits&0x1 ? "edge" : "level");
  112. PRINTF("%d: %s\n", irq_map[1], level_edge_bits&0x2 ? "edge" : "level");
  113. PRINTF("%d: %s\n", irq_map[2], level_edge_bits&0x4 ? "edge" : "level");
  114. PRINTF("%d: %s\n", irq_map[3], level_edge_bits&0x8 ? "edge" : "level");
  115. pci_write_cfg_byte(0, 7<<3, 0x54, level_edge_bits);
  116. PRINTF("%02x %02x %02x %02x\n", pci_read_cfg_byte(0, 7<<3, 0x54),
  117. pci_read_cfg_byte(0, 7<<3, 0x55), pci_read_cfg_byte(0, 7<<3, 0x56),
  118. pci_read_cfg_byte(0, 7<<3, 0x57));
  119. }
  120. /* Setup the IDE controller. This doesn't seem to work yet. I/O to an IDE controller port */
  121. /* always return the last character output on the serial port (!) */
  122. /* This function is called by the pnp-library when it encounters 0:7:1 */
  123. void via_cfgfunc_ide_init(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table)
  124. {
  125. PRINTF("... IDE controller, dev=0x%X\n", dev);
  126. /* Enable both IDE channels. */
  127. pci_write_config_byte(dev, 0x40, 0x03);
  128. /* udelay(10000); */
  129. /* udelay(10000); */
  130. /* Enable IO Space */
  131. pci_write_config_word(dev, 0x04, 0x03);
  132. /* Set to compatibility mode */
  133. pci_write_config_byte(dev, 0x09, 0x8A); /* WAS: 0x8f); */
  134. /* Set to legacy interrupt mode */
  135. pci_write_config_byte(dev, 0x3d, 0x00); /* WAS: 0x01); */
  136. }
  137. /* Set the base address of the floppy controller to 0x3F0 */
  138. void via_fdc_init(pci_dev_t dev)
  139. {
  140. unsigned char c;
  141. /* Enable Configuration mode */
  142. pci_read_config_byte(dev, 0x85, &c);
  143. c |= 0x02;
  144. pci_write_config_byte(dev, 0x85, c);
  145. /* Set floppy controller port to 0x3F0. */
  146. SIO_WRITE_CONFIG(0xE3, (0x3F<<2));
  147. /* Enable floppy controller */
  148. SIO_READ_CONFIG(0xE2, c);
  149. c |= 0x10;
  150. SIO_WRITE_CONFIG(0xE2, c);
  151. /* Switch of configuration mode */
  152. pci_read_config_byte(dev, 0x85, &c);
  153. c &= ~0x02;
  154. pci_write_config_byte(dev, 0x85, c);
  155. }
  156. /* Init function 0 of the via southbridge. Called by the pnp-library */
  157. void via_cfgfunc_via686(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table)
  158. {
  159. if (PCI_FUNC(dev) == 0)
  160. {
  161. /* FIXME: Try to generate a PCI reset */
  162. /* unsigned char c; */
  163. /* pci_read_config_byte(dev, 0x47, &c); */
  164. /* pci_write_config_byte(dev, 0x47, c | 0x01); */
  165. via_isa_init(dev, table);
  166. via_fdc_init(dev);
  167. }
  168. }
  169. __asm (" .globl via_calibrate_time_base \n"
  170. "via_calibrate_time_base: \n"
  171. " lis 9, 0xfe00 \n"
  172. " li 0, 0x00 \n"
  173. " mttbu 0 \n"
  174. " mttbl 0 \n"
  175. "ctb_loop: \n"
  176. " lbz 0, 0x61(9) \n"
  177. " eieio \n"
  178. " andi. 0, 0, 0x20 \n"
  179. " beq ctb_loop \n"
  180. "ctb_done: \n"
  181. " mftb 3 \n"
  182. " blr");
  183. extern unsigned long via_calibrate_time_base(void);
  184. void via_calibrate_bus_freq (void)
  185. {
  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. }