cpu.c 11 KB

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