cpu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * (C) Copyright 2009
  3. * Marvell Semiconductor <www.marvell.com>
  4. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. * MA 02110-1301 USA
  23. */
  24. #include <common.h>
  25. #include <netdev.h>
  26. #include <asm/cache.h>
  27. #include <u-boot/md5.h>
  28. #include <asm/arch/kirkwood.h>
  29. #include <hush.h>
  30. #define BUFLEN 16
  31. void reset_cpu(unsigned long ignored)
  32. {
  33. struct kwcpu_registers *cpureg =
  34. (struct kwcpu_registers *)KW_CPU_REG_BASE;
  35. writel(readl(&cpureg->rstoutn_mask) | (1 << 2),
  36. &cpureg->rstoutn_mask);
  37. writel(readl(&cpureg->sys_soft_rst) | 1,
  38. &cpureg->sys_soft_rst);
  39. while (1) ;
  40. }
  41. /*
  42. * Generates Ramdom hex number reading some time varient system registers
  43. * and using md5 algorithm
  44. */
  45. unsigned char get_random_hex(void)
  46. {
  47. int i;
  48. u32 inbuf[BUFLEN];
  49. u8 outbuf[BUFLEN];
  50. /*
  51. * in case of 88F6281/88F6282/88F6192 A0,
  52. * Bit7 need to reset to generate random values in KW_REG_UNDOC_0x1470
  53. * Soc reg offsets KW_REG_UNDOC_0x1470 and KW_REG_UNDOC_0x1478 are
  54. * reserved regs and does not have names at this moment
  55. * (no errata available)
  56. */
  57. writel(readl(KW_REG_UNDOC_0x1478) & ~(1 << 7), KW_REG_UNDOC_0x1478);
  58. for (i = 0; i < BUFLEN; i++) {
  59. inbuf[i] = readl(KW_REG_UNDOC_0x1470);
  60. }
  61. md5((u8 *) inbuf, (BUFLEN * sizeof(u32)), outbuf);
  62. return outbuf[outbuf[7] % 0x0f];
  63. }
  64. /*
  65. * Window Size
  66. * Used with the Base register to set the address window size and location.
  67. * Must be programmed from LSB to MSB as sequence of ones followed by
  68. * sequence of zeros. The number of ones specifies the size of the window in
  69. * 64 KByte granularity (e.g., a value of 0x00FF specifies 256 = 16 MByte).
  70. * NOTE: A value of 0x0 specifies 64-KByte size.
  71. */
  72. unsigned int kw_winctrl_calcsize(unsigned int sizeval)
  73. {
  74. int i;
  75. unsigned int j = 0;
  76. u32 val = sizeval >> 1;
  77. for (i = 0; val >= 0x10000; i++) {
  78. j |= (1 << i);
  79. val = val >> 1;
  80. }
  81. return (0x0000ffff & j);
  82. }
  83. /*
  84. * kw_config_adr_windows - Configure address Windows
  85. *
  86. * There are 8 address windows supported by Kirkwood Soc to addess different
  87. * devices. Each window can be configured for size, BAR and remap addr
  88. * Below configuration is standard for most of the cases
  89. *
  90. * If remap function not used, remap_lo must be set as base
  91. *
  92. * Reference Documentation:
  93. * Mbus-L to Mbus Bridge Registers Configuration.
  94. * (Sec 25.1 and 25.3 of Datasheet)
  95. */
  96. int kw_config_adr_windows(void)
  97. {
  98. struct kwwin_registers *winregs =
  99. (struct kwwin_registers *)KW_CPU_WIN_BASE;
  100. /* Window 0: PCIE MEM address space */
  101. writel(KWCPU_WIN_CTRL_DATA(1024 * 1024 * 256, KWCPU_TARGET_PCIE,
  102. KWCPU_ATTR_PCIE_MEM, KWCPU_WIN_ENABLE), &winregs[0].ctrl);
  103. writel(KW_DEFADR_PCI_MEM, &winregs[0].base);
  104. writel(KW_DEFADR_PCI_MEM, &winregs[0].remap_lo);
  105. writel(0x0, &winregs[0].remap_hi);
  106. /* Window 1: PCIE IO address space */
  107. writel(KWCPU_WIN_CTRL_DATA(1024 * 64, KWCPU_TARGET_PCIE,
  108. KWCPU_ATTR_PCIE_IO, KWCPU_WIN_ENABLE), &winregs[1].ctrl);
  109. writel(KW_DEFADR_PCI_IO, &winregs[1].base);
  110. writel(KW_DEFADR_PCI_IO_REMAP, &winregs[1].remap_lo);
  111. writel(0x0, &winregs[1].remap_hi);
  112. /* Window 2: NAND Flash address space */
  113. writel(KWCPU_WIN_CTRL_DATA(1024 * 1024 * 128, KWCPU_TARGET_MEMORY,
  114. KWCPU_ATTR_NANDFLASH, KWCPU_WIN_ENABLE), &winregs[2].ctrl);
  115. writel(KW_DEFADR_NANDF, &winregs[2].base);
  116. writel(KW_DEFADR_NANDF, &winregs[2].remap_lo);
  117. writel(0x0, &winregs[2].remap_hi);
  118. /* Window 3: SPI Flash address space */
  119. writel(KWCPU_WIN_CTRL_DATA(1024 * 1024 * 128, KWCPU_TARGET_MEMORY,
  120. KWCPU_ATTR_SPIFLASH, KWCPU_WIN_ENABLE), &winregs[3].ctrl);
  121. writel(KW_DEFADR_SPIF, &winregs[3].base);
  122. writel(KW_DEFADR_SPIF, &winregs[3].remap_lo);
  123. writel(0x0, &winregs[3].remap_hi);
  124. /* Window 4: BOOT Memory address space */
  125. writel(KWCPU_WIN_CTRL_DATA(1024 * 1024 * 128, KWCPU_TARGET_MEMORY,
  126. KWCPU_ATTR_BOOTROM, KWCPU_WIN_ENABLE), &winregs[4].ctrl);
  127. writel(KW_DEFADR_BOOTROM, &winregs[4].base);
  128. /* Window 5: Security SRAM address space */
  129. writel(KWCPU_WIN_CTRL_DATA(1024 * 64, KWCPU_TARGET_SASRAM,
  130. KWCPU_ATTR_SASRAM, KWCPU_WIN_ENABLE), &winregs[5].ctrl);
  131. writel(KW_DEFADR_SASRAM, &winregs[5].base);
  132. /* Window 6-7: Disabled */
  133. writel(KWCPU_WIN_DISABLE, &winregs[6].ctrl);
  134. writel(KWCPU_WIN_DISABLE, &winregs[7].ctrl);
  135. return 0;
  136. }
  137. /*
  138. * kw_config_gpio - GPIO configuration
  139. */
  140. void kw_config_gpio(u32 gpp0_oe_val, u32 gpp1_oe_val, u32 gpp0_oe, u32 gpp1_oe)
  141. {
  142. struct kwgpio_registers *gpio0reg =
  143. (struct kwgpio_registers *)KW_GPIO0_BASE;
  144. struct kwgpio_registers *gpio1reg =
  145. (struct kwgpio_registers *)KW_GPIO1_BASE;
  146. /* Init GPIOS to default values as per board requirement */
  147. writel(gpp0_oe_val, &gpio0reg->dout);
  148. writel(gpp1_oe_val, &gpio1reg->dout);
  149. writel(gpp0_oe, &gpio0reg->oe);
  150. writel(gpp1_oe, &gpio1reg->oe);
  151. }
  152. /*
  153. * kw_config_mpp - Multi-Purpose Pins Functionality configuration
  154. *
  155. * Each MPP can be configured to different functionality through
  156. * MPP control register, ref (sec 6.1 of kirkwood h/w specification)
  157. *
  158. * There are maximum 64 Multi-Pourpose Pins on Kirkwood
  159. * Each MPP functionality can be configuration by a 4bit value
  160. * of MPP control reg, the value and associated functionality depends
  161. * upon used SoC varient
  162. */
  163. int kw_config_mpp(u32 mpp0_7, u32 mpp8_15, u32 mpp16_23, u32 mpp24_31,
  164. u32 mpp32_39, u32 mpp40_47, u32 mpp48_55)
  165. {
  166. u32 *mppreg = (u32 *) KW_MPP_BASE;
  167. /* program mpp registers */
  168. writel(mpp0_7, &mppreg[0]);
  169. writel(mpp8_15, &mppreg[1]);
  170. writel(mpp16_23, &mppreg[2]);
  171. writel(mpp24_31, &mppreg[3]);
  172. writel(mpp32_39, &mppreg[4]);
  173. writel(mpp40_47, &mppreg[5]);
  174. writel(mpp48_55, &mppreg[6]);
  175. return 0;
  176. }
  177. /*
  178. * SYSRSTn Duration Counter Support
  179. *
  180. * Kirkwood SoC implements a hardware-based SYSRSTn duration counter.
  181. * When SYSRSTn is asserted low, a SYSRSTn duration counter is running.
  182. * The SYSRSTn duration counter is useful for implementing a manufacturer
  183. * or factory reset. Upon a long reset assertion that is greater than a
  184. * pre-configured environment variable value for sysrstdelay,
  185. * The counter value is stored in the SYSRSTn Length Counter Register
  186. * The counter is based on the 25-MHz reference clock (40ns)
  187. * It is a 29-bit counter, yielding a maximum counting duration of
  188. * 2^29/25 MHz (21.4 seconds). When the counter reach its maximum value,
  189. * it remains at this value until counter reset is triggered by setting
  190. * bit 31 of KW_REG_SYSRST_CNT
  191. */
  192. static void kw_sysrst_action(void)
  193. {
  194. int ret;
  195. char *s = getenv("sysrstcmd");
  196. if (!s) {
  197. debug("Error.. %s failed, check sysrstcmd\n",
  198. __FUNCTION__);
  199. return;
  200. }
  201. debug("Starting %s process...\n", __FUNCTION__);
  202. #if !defined(CONFIG_SYS_HUSH_PARSER)
  203. ret = run_command (s, 0);
  204. #else
  205. ret = parse_string_outer(s, FLAG_PARSE_SEMICOLON
  206. | FLAG_EXIT_FROM_LOOP);
  207. #endif
  208. if (ret < 0)
  209. debug("Error.. %s failed\n", __FUNCTION__);
  210. else
  211. debug("%s process finished\n", __FUNCTION__);
  212. }
  213. static void kw_sysrst_check(void)
  214. {
  215. u32 sysrst_cnt, sysrst_dly;
  216. char *s;
  217. /*
  218. * no action if sysrstdelay environment variable is not defined
  219. */
  220. s = getenv("sysrstdelay");
  221. if (s == NULL)
  222. return;
  223. /* read sysrstdelay value */
  224. sysrst_dly = (u32) simple_strtoul(s, NULL, 10);
  225. /* read SysRst Length counter register (bits 28:0) */
  226. sysrst_cnt = (0x1fffffff & readl(KW_REG_SYSRST_CNT));
  227. debug("H/w Rst hold time: %d.%d secs\n",
  228. sysrst_cnt / SYSRST_CNT_1SEC_VAL,
  229. sysrst_cnt % SYSRST_CNT_1SEC_VAL);
  230. /* clear the counter for next valid read*/
  231. writel(1 << 31, KW_REG_SYSRST_CNT);
  232. /*
  233. * sysrst_action:
  234. * if H/w Reset key is pressed and hold for time
  235. * more than sysrst_dly in seconds
  236. */
  237. if (sysrst_cnt >= SYSRST_CNT_1SEC_VAL * sysrst_dly)
  238. kw_sysrst_action();
  239. }
  240. #if defined(CONFIG_DISPLAY_CPUINFO)
  241. int print_cpuinfo(void)
  242. {
  243. char *rev;
  244. u16 devid = (readl(KW_REG_PCIE_DEVID) >> 16) & 0xffff;
  245. u8 revid = readl(KW_REG_PCIE_REVID) & 0xff;
  246. if ((readl(KW_REG_DEVICE_ID) & 0x03) > 2) {
  247. printf("Error.. %s:Unsupported Kirkwood SoC 88F%04x\n", __FUNCTION__, devid);
  248. return -1;
  249. }
  250. switch (revid) {
  251. case 0:
  252. rev = "Z0";
  253. break;
  254. case 2:
  255. rev = "A0";
  256. break;
  257. case 3:
  258. rev = "A1";
  259. break;
  260. default:
  261. rev = "??";
  262. break;
  263. }
  264. printf("SoC: Kirkwood 88F%04x_%s\n", devid, rev);
  265. return 0;
  266. }
  267. #endif /* CONFIG_DISPLAY_CPUINFO */
  268. #ifdef CONFIG_ARCH_CPU_INIT
  269. int arch_cpu_init(void)
  270. {
  271. u32 reg;
  272. struct kwcpu_registers *cpureg =
  273. (struct kwcpu_registers *)KW_CPU_REG_BASE;
  274. /* Linux expects` the internal registers to be at 0xf1000000 */
  275. writel(KW_REGS_PHY_BASE, KW_OFFSET_REG);
  276. /* Enable and invalidate L2 cache in write through mode */
  277. writel(readl(&cpureg->l2_cfg) | 0x18, &cpureg->l2_cfg);
  278. invalidate_l2_cache();
  279. kw_config_adr_windows();
  280. #ifdef CONFIG_KIRKWOOD_RGMII_PAD_1V8
  281. /*
  282. * Configures the I/O voltage of the pads connected to Egigabit
  283. * Ethernet interface to 1.8V
  284. * By defult it is set to 3.3V
  285. */
  286. reg = readl(KW_REG_MPP_OUT_DRV_REG);
  287. reg |= (1 << 7);
  288. writel(reg, KW_REG_MPP_OUT_DRV_REG);
  289. #endif
  290. #ifdef CONFIG_KIRKWOOD_EGIGA_INIT
  291. /*
  292. * Set egiga port0/1 in normal functional mode
  293. * This is required becasue on kirkwood by default ports are in reset mode
  294. * OS egiga driver may not have provision to set them in normal mode
  295. * and if u-boot is build without network support, network may fail at OS level
  296. */
  297. reg = readl(KWGBE_PORT_SERIAL_CONTROL1_REG(0));
  298. reg &= ~(1 << 4); /* Clear PortReset Bit */
  299. writel(reg, (KWGBE_PORT_SERIAL_CONTROL1_REG(0)));
  300. reg = readl(KWGBE_PORT_SERIAL_CONTROL1_REG(1));
  301. reg &= ~(1 << 4); /* Clear PortReset Bit */
  302. writel(reg, (KWGBE_PORT_SERIAL_CONTROL1_REG(1)));
  303. #endif
  304. #ifdef CONFIG_KIRKWOOD_PCIE_INIT
  305. /*
  306. * Enable PCI Express Port0
  307. */
  308. reg = readl(&cpureg->ctrl_stat);
  309. reg |= (1 << 0); /* Set PEX0En Bit */
  310. writel(reg, &cpureg->ctrl_stat);
  311. #endif
  312. return 0;
  313. }
  314. #endif /* CONFIG_ARCH_CPU_INIT */
  315. /*
  316. * SOC specific misc init
  317. */
  318. #if defined(CONFIG_ARCH_MISC_INIT)
  319. int arch_misc_init(void)
  320. {
  321. volatile u32 temp;
  322. /*CPU streaming & write allocate */
  323. temp = readfr_extra_feature_reg();
  324. temp &= ~(1 << 28); /* disable wr alloc */
  325. writefr_extra_feature_reg(temp);
  326. temp = readfr_extra_feature_reg();
  327. temp &= ~(1 << 29); /* streaming disabled */
  328. writefr_extra_feature_reg(temp);
  329. /* L2Cache settings */
  330. temp = readfr_extra_feature_reg();
  331. /* Disable L2C pre fetch - Set bit 24 */
  332. temp |= (1 << 24);
  333. /* enable L2C - Set bit 22 */
  334. temp |= (1 << 22);
  335. writefr_extra_feature_reg(temp);
  336. icache_enable();
  337. /* Change reset vector to address 0x0 */
  338. temp = get_cr();
  339. set_cr(temp & ~CR_V);
  340. /* checks and execute resset to factory event */
  341. kw_sysrst_check();
  342. return 0;
  343. }
  344. #endif /* CONFIG_ARCH_MISC_INIT */
  345. #ifdef CONFIG_MVGBE
  346. int cpu_eth_init(bd_t *bis)
  347. {
  348. mvgbe_initialize(bis);
  349. return 0;
  350. }
  351. #endif