sc520.c 12 KB

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