sc520.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * (C) Copyright 2002
  3. * Daniel Engström, Omicron Ceti AB <daniel@omicron.se>.
  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. /* stuff specific for the sc520,
  24. * but idependent of implementation */
  25. #include <config.h>
  26. #ifdef CONFIG_SC520
  27. #include <common.h>
  28. #include <config.h>
  29. #include <pci.h>
  30. #ifdef CONFIG_SC520_SSI
  31. #include <ssi.h>
  32. #endif
  33. #include <asm/io.h>
  34. #include <asm/pci.h>
  35. #include <asm/ic/sc520.h>
  36. DECLARE_GLOBAL_DATA_PTR;
  37. /*
  38. * utility functions for boards based on the AMD sc520
  39. *
  40. * void write_mmcr_byte(u16 mmcr, u8 data)
  41. * void write_mmcr_word(u16 mmcr, u16 data)
  42. * void write_mmcr_long(u16 mmcr, u32 data)
  43. *
  44. * u8 read_mmcr_byte(u16 mmcr)
  45. * u16 read_mmcr_word(u16 mmcr)
  46. * u32 read_mmcr_long(u16 mmcr)
  47. *
  48. * void init_sc520(void)
  49. * unsigned long init_sc520_dram(void)
  50. * void pci_sc520_init(struct pci_controller *hose)
  51. *
  52. * void reset_timer(void)
  53. * ulong get_timer(ulong base)
  54. * void set_timer(ulong t)
  55. * void udelay(unsigned long usec)
  56. *
  57. */
  58. static u32 mmcr_base= 0xfffef000;
  59. void write_mmcr_byte(u16 mmcr, u8 data)
  60. {
  61. writeb(data, mmcr+mmcr_base);
  62. }
  63. void write_mmcr_word(u16 mmcr, u16 data)
  64. {
  65. writew(data, mmcr+mmcr_base);
  66. }
  67. void write_mmcr_long(u16 mmcr, u32 data)
  68. {
  69. writel(data, mmcr+mmcr_base);
  70. }
  71. u8 read_mmcr_byte(u16 mmcr)
  72. {
  73. return readb(mmcr+mmcr_base);
  74. }
  75. u16 read_mmcr_word(u16 mmcr)
  76. {
  77. return readw(mmcr+mmcr_base);
  78. }
  79. u32 read_mmcr_long(u16 mmcr)
  80. {
  81. return readl(mmcr+mmcr_base);
  82. }
  83. void init_sc520(void)
  84. {
  85. /* Set the UARTxCTL register at it's slower,
  86. * baud clock giving us a 1.8432 MHz reference
  87. */
  88. write_mmcr_byte(SC520_UART1CTL, 7);
  89. write_mmcr_byte(SC520_UART2CTL, 7);
  90. /* first set the timer pin mapping */
  91. write_mmcr_byte(SC520_CLKSEL, 0x72); /* no clock frequency selected, use 1.1892MHz */
  92. /* enable PCI bus arbitrer */
  93. write_mmcr_byte(SC520_SYSARBCTL,0x02); /* enable concurrent mode */
  94. write_mmcr_word(SC520_SYSARBMENB,0x1f); /* enable external grants */
  95. write_mmcr_word(SC520_HBCTL,0x04); /* enable posted-writes */
  96. if (CFG_SC520_HIGH_SPEED) {
  97. write_mmcr_byte(SC520_CPUCTL, 0x2); /* set it to 133 MHz and write back */
  98. gd->cpu_clk = 133000000;
  99. printf("## CPU Speed set to 133MHz\n");
  100. } else {
  101. write_mmcr_byte(SC520_CPUCTL, 1); /* set CPU to 100 MHz and write back cache */
  102. printf("## CPU Speed set to 100MHz\n");
  103. gd->cpu_clk = 100000000;
  104. }
  105. /* wait at least one millisecond */
  106. asm("movl $0x2000,%%ecx\n"
  107. "wait_loop: pushl %%ecx\n"
  108. "popl %%ecx\n"
  109. "loop wait_loop\n": : : "ecx");
  110. /* turn on the SDRAM write buffer */
  111. write_mmcr_byte(SC520_DBCTL, 0x11);
  112. /* turn on the cache and disable write through */
  113. asm("movl %%cr0, %%eax\n"
  114. "andl $0x9fffffff, %%eax\n"
  115. "movl %%eax, %%cr0\n" : : : "eax");
  116. }
  117. unsigned long init_sc520_dram(void)
  118. {
  119. bd_t *bd = gd->bd;
  120. u32 dram_present=0;
  121. u32 dram_ctrl;
  122. #ifdef CFG_SDRAM_DRCTMCTL
  123. /* these memory control registers are set up in the assember part,
  124. * in sc520_asm.S, during 'mem_init'. If we muck with them here,
  125. * after we are running a stack in RAM, we have troubles. Besides,
  126. * these refresh and delay values are better ? simply specified
  127. * outright in the include/configs/{cfg} file since the HW designer
  128. * simply dictates it.
  129. */
  130. #else
  131. int val;
  132. int cas_precharge_delay = CFG_SDRAM_PRECHARGE_DELAY;
  133. int refresh_rate = CFG_SDRAM_REFRESH_RATE;
  134. int ras_cas_delay = CFG_SDRAM_RAS_CAS_DELAY;
  135. /* set SDRAM speed here */
  136. refresh_rate/=78;
  137. if (refresh_rate<=1) {
  138. val = 0; /* 7.8us */
  139. } else if (refresh_rate==2) {
  140. val = 1; /* 15.6us */
  141. } else if (refresh_rate==3 || refresh_rate==4) {
  142. val = 2; /* 31.2us */
  143. } else {
  144. val = 3; /* 62.4us */
  145. }
  146. write_mmcr_byte(SC520_DRCCTL, (read_mmcr_byte(SC520_DRCCTL) & 0xcf) | (val<<4));
  147. val = read_mmcr_byte(SC520_DRCTMCTL);
  148. val &= 0xf0;
  149. if (cas_precharge_delay==3) {
  150. val |= 0x04; /* 3T */
  151. } else if (cas_precharge_delay==4) {
  152. val |= 0x08; /* 4T */
  153. } else if (cas_precharge_delay>4) {
  154. val |= 0x0c;
  155. }
  156. if (ras_cas_delay > 3) {
  157. val |= 2;
  158. } else {
  159. val |= 1;
  160. }
  161. write_mmcr_byte(SC520_DRCTMCTL, val);
  162. #endif
  163. /* We read-back the configuration of the dram
  164. * controller that the assembly code wrote */
  165. dram_ctrl = read_mmcr_long(SC520_DRCBENDADR);
  166. bd->bi_dram[0].start = 0;
  167. if (dram_ctrl & 0x80) {
  168. /* bank 0 enabled */
  169. dram_present = bd->bi_dram[1].start = (dram_ctrl & 0x7f) << 22;
  170. bd->bi_dram[0].size = bd->bi_dram[1].start;
  171. } else {
  172. bd->bi_dram[0].size = 0;
  173. bd->bi_dram[1].start = bd->bi_dram[0].start;
  174. }
  175. if (dram_ctrl & 0x8000) {
  176. /* bank 1 enabled */
  177. dram_present = bd->bi_dram[2].start = (dram_ctrl & 0x7f00) << 14;
  178. bd->bi_dram[1].size = bd->bi_dram[2].start - bd->bi_dram[1].start;
  179. } else {
  180. bd->bi_dram[1].size = 0;
  181. bd->bi_dram[2].start = bd->bi_dram[1].start;
  182. }
  183. if (dram_ctrl & 0x800000) {
  184. /* bank 2 enabled */
  185. dram_present = bd->bi_dram[3].start = (dram_ctrl & 0x7f0000) << 6;
  186. bd->bi_dram[2].size = bd->bi_dram[3].start - bd->bi_dram[2].start;
  187. } else {
  188. bd->bi_dram[2].size = 0;
  189. bd->bi_dram[3].start = bd->bi_dram[2].start;
  190. }
  191. if (dram_ctrl & 0x80000000) {
  192. /* bank 3 enabled */
  193. dram_present = (dram_ctrl & 0x7f000000) >> 2;
  194. bd->bi_dram[3].size = dram_present - bd->bi_dram[3].start;
  195. } else {
  196. bd->bi_dram[3].size = 0;
  197. }
  198. #if 0
  199. printf("Configured %d bytes of dram\n", dram_present);
  200. #endif
  201. gd->ram_size = dram_present;
  202. return dram_present;
  203. }
  204. #ifdef CONFIG_PCI
  205. static struct {
  206. u8 priority;
  207. u16 level_reg;
  208. u8 level_bit;
  209. } sc520_irq[] = {
  210. { SC520_IRQ0, SC520_MPICMODE, 0x01 },
  211. { SC520_IRQ1, SC520_MPICMODE, 0x02 },
  212. { SC520_IRQ2, SC520_SL1PICMODE, 0x02 },
  213. { SC520_IRQ3, SC520_MPICMODE, 0x08 },
  214. { SC520_IRQ4, SC520_MPICMODE, 0x10 },
  215. { SC520_IRQ5, SC520_MPICMODE, 0x20 },
  216. { SC520_IRQ6, SC520_MPICMODE, 0x40 },
  217. { SC520_IRQ7, SC520_MPICMODE, 0x80 },
  218. { SC520_IRQ8, SC520_SL1PICMODE, 0x01 },
  219. { SC520_IRQ9, SC520_SL1PICMODE, 0x02 },
  220. { SC520_IRQ10, SC520_SL1PICMODE, 0x04 },
  221. { SC520_IRQ11, SC520_SL1PICMODE, 0x08 },
  222. { SC520_IRQ12, SC520_SL1PICMODE, 0x10 },
  223. { SC520_IRQ13, SC520_SL1PICMODE, 0x20 },
  224. { SC520_IRQ14, SC520_SL1PICMODE, 0x40 },
  225. { SC520_IRQ15, SC520_SL1PICMODE, 0x80 }
  226. };
  227. /* The interrupt used for PCI INTA-INTD */
  228. int sc520_pci_ints[15] = {
  229. -1, -1, -1, -1, -1, -1, -1, -1,
  230. -1, -1, -1, -1, -1, -1, -1
  231. };
  232. /* utility function to configure a pci interrupt */
  233. int pci_sc520_set_irq(int pci_pin, int irq)
  234. {
  235. int i;
  236. # if 1
  237. printf("set_irq(): map INT%c to IRQ%d\n", pci_pin + 'A', irq);
  238. #endif
  239. if (irq < 0 || irq > 15) {
  240. return -1; /* illegal irq */
  241. }
  242. if (pci_pin < 0 || pci_pin > 15) {
  243. return -1; /* illegal pci int pin */
  244. }
  245. /* first disable any non-pci interrupt source that use
  246. * this level */
  247. for (i=SC520_GPTMR0MAP;i<=SC520_GP10IMAP;i++) {
  248. if (i>=SC520_PCIINTAMAP&&i<=SC520_PCIINTDMAP) {
  249. continue;
  250. }
  251. if (read_mmcr_byte(i) == sc520_irq[irq].priority) {
  252. write_mmcr_byte(i, SC520_IRQ_DISABLED);
  253. }
  254. }
  255. /* Set the trigger to level */
  256. write_mmcr_byte(sc520_irq[irq].level_reg,
  257. read_mmcr_byte(sc520_irq[irq].level_reg) | sc520_irq[irq].level_bit);
  258. if (pci_pin < 4) {
  259. /* PCI INTA-INTD */
  260. /* route the interrupt */
  261. write_mmcr_byte(SC520_PCIINTAMAP + pci_pin, sc520_irq[irq].priority);
  262. } else {
  263. /* GPIRQ0-GPIRQ10 used for additional PCI INTS */
  264. write_mmcr_byte(SC520_GP0IMAP + pci_pin - 4, sc520_irq[irq].priority);
  265. /* also set the polarity in this case */
  266. write_mmcr_word(SC520_INTPINPOL,
  267. read_mmcr_word(SC520_INTPINPOL) | (1 << (pci_pin-4)));
  268. }
  269. /* register the pin */
  270. sc520_pci_ints[pci_pin] = irq;
  271. return 0; /* OK */
  272. }
  273. void pci_sc520_init(struct pci_controller *hose)
  274. {
  275. hose->first_busno = 0;
  276. hose->last_busno = 0xff;
  277. /* System memory space */
  278. pci_set_region(hose->regions + 0,
  279. SC520_PCI_MEMORY_BUS,
  280. SC520_PCI_MEMORY_PHYS,
  281. SC520_PCI_MEMORY_SIZE,
  282. PCI_REGION_MEM | PCI_REGION_MEMORY);
  283. /* PCI memory space */
  284. pci_set_region(hose->regions + 1,
  285. SC520_PCI_MEM_BUS,
  286. SC520_PCI_MEM_PHYS,
  287. SC520_PCI_MEM_SIZE,
  288. PCI_REGION_MEM);
  289. /* ISA/PCI memory space */
  290. pci_set_region(hose->regions + 2,
  291. SC520_ISA_MEM_BUS,
  292. SC520_ISA_MEM_PHYS,
  293. SC520_ISA_MEM_SIZE,
  294. PCI_REGION_MEM);
  295. /* PCI I/O space */
  296. pci_set_region(hose->regions + 3,
  297. SC520_PCI_IO_BUS,
  298. SC520_PCI_IO_PHYS,
  299. SC520_PCI_IO_SIZE,
  300. PCI_REGION_IO);
  301. /* ISA/PCI I/O space */
  302. pci_set_region(hose->regions + 4,
  303. SC520_ISA_IO_BUS,
  304. SC520_ISA_IO_PHYS,
  305. SC520_ISA_IO_SIZE,
  306. PCI_REGION_IO);
  307. hose->region_count = 5;
  308. pci_setup_type1(hose,
  309. SC520_REG_ADDR,
  310. SC520_REG_DATA);
  311. pci_register_hose(hose);
  312. hose->last_busno = pci_hose_scan(hose);
  313. /* enable target memory acceses on host brige */
  314. pci_write_config_word(0, PCI_COMMAND,
  315. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  316. }
  317. #endif
  318. #ifdef CFG_TIMER_SC520
  319. void reset_timer(void)
  320. {
  321. write_mmcr_word(SC520_GPTMR0CNT, 0);
  322. write_mmcr_word(SC520_GPTMR0CTL, 0x6001);
  323. }
  324. ulong get_timer(ulong base)
  325. {
  326. /* fixme: 30 or 33 */
  327. return read_mmcr_word(SC520_GPTMR0CNT) / 33;
  328. }
  329. void set_timer(ulong t)
  330. {
  331. /* FixMe: use two cascade coupled timers */
  332. write_mmcr_word(SC520_GPTMR0CTL, 0x4001);
  333. write_mmcr_word(SC520_GPTMR0CNT, t*33);
  334. write_mmcr_word(SC520_GPTMR0CTL, 0x6001);
  335. }
  336. void udelay(unsigned long usec)
  337. {
  338. int m=0;
  339. long u;
  340. read_mmcr_word(SC520_SWTMRMILLI);
  341. read_mmcr_word(SC520_SWTMRMICRO);
  342. #if 0
  343. /* do not enable this line, udelay is used in the serial driver -> recursion */
  344. printf("udelay: %ld m.u %d.%d tm.tu %d.%d\n", usec, m, u, tm, tu);
  345. #endif
  346. while (1) {
  347. m += read_mmcr_word(SC520_SWTMRMILLI);
  348. u = read_mmcr_word(SC520_SWTMRMICRO) + (m * 1000);
  349. if (usec <= u) {
  350. break;
  351. }
  352. }
  353. }
  354. #endif
  355. int ssi_set_interface(int freq, int lsb_first, int inv_clock, int inv_phase)
  356. {
  357. u8 temp=0;
  358. if (freq >= 8192) {
  359. temp |= CTL_CLK_SEL_4;
  360. } else if (freq >= 4096) {
  361. temp |= CTL_CLK_SEL_8;
  362. } else if (freq >= 2048) {
  363. temp |= CTL_CLK_SEL_16;
  364. } else if (freq >= 1024) {
  365. temp |= CTL_CLK_SEL_32;
  366. } else if (freq >= 512) {
  367. temp |= CTL_CLK_SEL_64;
  368. } else if (freq >= 256) {
  369. temp |= CTL_CLK_SEL_128;
  370. } else if (freq >= 128) {
  371. temp |= CTL_CLK_SEL_256;
  372. } else {
  373. temp |= CTL_CLK_SEL_512;
  374. }
  375. if (!lsb_first) {
  376. temp |= MSBF_ENB;
  377. }
  378. if (inv_clock) {
  379. temp |= CLK_INV_ENB;
  380. }
  381. if (inv_phase) {
  382. temp |= PHS_INV_ENB;
  383. }
  384. write_mmcr_byte(SC520_SSICTL, temp);
  385. return 0;
  386. }
  387. u8 ssi_txrx_byte(u8 data)
  388. {
  389. write_mmcr_byte(SC520_SSIXMIT, data);
  390. while ((read_mmcr_byte(SC520_SSISTA)) & SSISTA_BSY);
  391. write_mmcr_byte(SC520_SSICMD, SSICMD_CMD_SEL_XMITRCV);
  392. while ((read_mmcr_byte(SC520_SSISTA)) & SSISTA_BSY);
  393. return read_mmcr_byte(SC520_SSIRCV);
  394. }
  395. void ssi_tx_byte(u8 data)
  396. {
  397. write_mmcr_byte(SC520_SSIXMIT, data);
  398. while ((read_mmcr_byte(SC520_SSISTA)) & SSISTA_BSY);
  399. write_mmcr_byte(SC520_SSICMD, SSICMD_CMD_SEL_XMIT);
  400. }
  401. u8 ssi_rx_byte(void)
  402. {
  403. while ((read_mmcr_byte(SC520_SSISTA)) & SSISTA_BSY);
  404. write_mmcr_byte(SC520_SSICMD, SSICMD_CMD_SEL_RCV);
  405. while ((read_mmcr_byte(SC520_SSISTA)) & SSISTA_BSY);
  406. return read_mmcr_byte(SC520_SSIRCV);
  407. }
  408. #endif /* CONFIG_SC520 */