cam_enc_4xx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright (C) 2009 Texas Instruments Incorporated
  3. *
  4. * Copyright (C) 2011
  5. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <common.h>
  22. #include <linux/mtd/nand.h>
  23. #include <nand.h>
  24. #include <miiphy.h>
  25. #include <netdev.h>
  26. #include <asm/io.h>
  27. #include <asm/arch/hardware.h>
  28. #include <asm/arch/nand_defs.h>
  29. #include <asm/arch/davinci_misc.h>
  30. #ifdef CONFIG_DAVINCI_MMC
  31. #include <mmc.h>
  32. #include <asm/arch/sdmmc_defs.h>
  33. #endif
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #ifndef CONFIG_SPL_BUILD
  36. int dram_init(void)
  37. {
  38. /* dram_init must store complete ramsize in gd->ram_size */
  39. gd->ram_size = get_ram_size(
  40. (void *)CONFIG_SYS_SDRAM_BASE,
  41. CONFIG_MAX_RAM_BANK_SIZE);
  42. return 0;
  43. }
  44. void dram_init_banksize(void)
  45. {
  46. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  47. gd->bd->bi_dram[0].size = gd->ram_size;
  48. }
  49. static struct davinci_timer *timer =
  50. (struct davinci_timer *)DAVINCI_TIMER3_BASE;
  51. static unsigned long get_timer_val(void)
  52. {
  53. unsigned long now = readl(&timer->tim34);
  54. return now;
  55. }
  56. static void stop_timer(void)
  57. {
  58. writel(0x0, &timer->tcr);
  59. return;
  60. }
  61. int checkboard(void)
  62. {
  63. printf("Board: AIT CAM ENC 4XX\n");
  64. return 0;
  65. }
  66. int board_init(void)
  67. {
  68. gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x100;
  69. return 0;
  70. }
  71. #ifdef CONFIG_DRIVER_TI_EMAC
  72. int board_eth_init(bd_t *bis)
  73. {
  74. davinci_emac_initialize();
  75. return 0;
  76. }
  77. #endif
  78. #ifdef CONFIG_NAND_DAVINCI
  79. static int
  80. davinci_std_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  81. uint8_t *buf, int page)
  82. {
  83. struct nand_chip *this = mtd->priv;
  84. int i, eccsize = chip->ecc.size;
  85. int eccbytes = chip->ecc.bytes;
  86. int eccsteps = chip->ecc.steps;
  87. uint8_t *p = buf;
  88. uint8_t *oob = chip->oob_poi;
  89. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
  90. chip->read_buf(mtd, oob, mtd->oobsize);
  91. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page & this->pagemask);
  92. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  93. int stat;
  94. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  95. chip->read_buf(mtd, p, eccsize);
  96. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  97. if (chip->ecc.prepad)
  98. oob += chip->ecc.prepad;
  99. stat = chip->ecc.correct(mtd, p, oob, NULL);
  100. if (stat == -1)
  101. mtd->ecc_stats.failed++;
  102. else
  103. mtd->ecc_stats.corrected += stat;
  104. oob += eccbytes;
  105. if (chip->ecc.postpad)
  106. oob += chip->ecc.postpad;
  107. }
  108. /* Calculate remaining oob bytes */
  109. i = mtd->oobsize - (oob - chip->oob_poi);
  110. if (i)
  111. chip->read_buf(mtd, oob, i);
  112. return 0;
  113. }
  114. static void davinci_std_write_page_syndrome(struct mtd_info *mtd,
  115. struct nand_chip *chip, const uint8_t *buf)
  116. {
  117. unsigned char davinci_ecc_buf[NAND_MAX_OOBSIZE];
  118. struct nand_chip *this = mtd->priv;
  119. int i, eccsize = chip->ecc.size;
  120. int eccbytes = chip->ecc.bytes;
  121. int eccsteps = chip->ecc.steps;
  122. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  123. int offset = 0;
  124. const uint8_t *p = buf;
  125. uint8_t *oob = chip->oob_poi;
  126. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  127. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  128. chip->write_buf(mtd, p, eccsize);
  129. /* Calculate ECC without prepad */
  130. chip->ecc.calculate(mtd, p, oob + chip->ecc.prepad);
  131. if (chip->ecc.prepad) {
  132. offset = (chip->ecc.steps - eccsteps) * chunk;
  133. memcpy(&davinci_ecc_buf[offset], oob, chip->ecc.prepad);
  134. oob += chip->ecc.prepad;
  135. }
  136. offset = ((chip->ecc.steps - eccsteps) * chunk) +
  137. chip->ecc.prepad;
  138. memcpy(&davinci_ecc_buf[offset], oob, eccbytes);
  139. oob += eccbytes;
  140. if (chip->ecc.postpad) {
  141. offset = ((chip->ecc.steps - eccsteps) * chunk) +
  142. chip->ecc.prepad + eccbytes;
  143. memcpy(&davinci_ecc_buf[offset], oob,
  144. chip->ecc.postpad);
  145. oob += chip->ecc.postpad;
  146. }
  147. }
  148. /*
  149. * Write the sparebytes into the page once
  150. * all eccsteps have been covered
  151. */
  152. for (i = 0; i < mtd->oobsize; i++)
  153. writeb(davinci_ecc_buf[i], this->IO_ADDR_W);
  154. /* Calculate remaining oob bytes */
  155. i = mtd->oobsize - (oob - chip->oob_poi);
  156. if (i)
  157. chip->write_buf(mtd, oob, i);
  158. }
  159. static int davinci_std_write_oob_syndrome(struct mtd_info *mtd,
  160. struct nand_chip *chip, int page)
  161. {
  162. int pos, status = 0;
  163. const uint8_t *bufpoi = chip->oob_poi;
  164. pos = mtd->writesize;
  165. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  166. chip->write_buf(mtd, bufpoi, mtd->oobsize);
  167. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  168. status = chip->waitfunc(mtd, chip);
  169. return status & NAND_STATUS_FAIL ? -1 : 0;
  170. }
  171. static int davinci_std_read_oob_syndrome(struct mtd_info *mtd,
  172. struct nand_chip *chip, int page, int sndcmd)
  173. {
  174. struct nand_chip *this = mtd->priv;
  175. uint8_t *buf = chip->oob_poi;
  176. uint8_t *bufpoi = buf;
  177. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
  178. chip->read_buf(mtd, bufpoi, mtd->oobsize);
  179. return 1;
  180. }
  181. static void nand_dm365evm_select_chip(struct mtd_info *mtd, int chip)
  182. {
  183. struct nand_chip *this = mtd->priv;
  184. unsigned long wbase = (unsigned long) this->IO_ADDR_W;
  185. unsigned long rbase = (unsigned long) this->IO_ADDR_R;
  186. if (chip == 1) {
  187. __set_bit(14, &wbase);
  188. __set_bit(14, &rbase);
  189. } else {
  190. __clear_bit(14, &wbase);
  191. __clear_bit(14, &rbase);
  192. }
  193. this->IO_ADDR_W = (void *)wbase;
  194. this->IO_ADDR_R = (void *)rbase;
  195. }
  196. int board_nand_init(struct nand_chip *nand)
  197. {
  198. davinci_nand_init(nand);
  199. nand->select_chip = nand_dm365evm_select_chip;
  200. return 0;
  201. }
  202. struct nand_ecc_ctrl org_ecc;
  203. static int notsaved = 1;
  204. static int nand_switch_hw_func(int mode)
  205. {
  206. struct nand_chip *nand;
  207. struct mtd_info *mtd;
  208. if (nand_curr_device < 0 ||
  209. nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
  210. !nand_info[nand_curr_device].name) {
  211. printf("Error: Can't switch hw functions," \
  212. " no devices available\n");
  213. return -1;
  214. }
  215. mtd = &nand_info[nand_curr_device];
  216. nand = mtd->priv;
  217. if (mode == 0) {
  218. printf("switching to uboot hw functions.\n");
  219. memcpy(&nand->ecc, &org_ecc, sizeof(struct nand_ecc_ctrl));
  220. } else {
  221. /* RBL */
  222. printf("switching to RBL hw functions.\n");
  223. if (notsaved == 1) {
  224. memcpy(&org_ecc, &nand->ecc,
  225. sizeof(struct nand_ecc_ctrl));
  226. notsaved = 0;
  227. }
  228. nand->ecc.mode = NAND_ECC_HW_SYNDROME;
  229. nand->ecc.prepad = 6;
  230. nand->ecc.read_page = davinci_std_read_page_syndrome;
  231. nand->ecc.write_page = davinci_std_write_page_syndrome;
  232. nand->ecc.read_oob = davinci_std_read_oob_syndrome;
  233. nand->ecc.write_oob = davinci_std_write_oob_syndrome;
  234. }
  235. return mode;
  236. }
  237. static int hwmode;
  238. static int do_switch_ecc(cmd_tbl_t *cmdtp, int flag, int argc,
  239. char *const argv[])
  240. {
  241. if (argc != 2)
  242. goto usage;
  243. if (strncmp(argv[1], "rbl", 2) == 0)
  244. hwmode = nand_switch_hw_func(1);
  245. else if (strncmp(argv[1], "uboot", 2) == 0)
  246. hwmode = nand_switch_hw_func(0);
  247. else
  248. goto usage;
  249. return 0;
  250. usage:
  251. printf("Usage: nandrbl %s\n", cmdtp->usage);
  252. return 1;
  253. }
  254. U_BOOT_CMD(
  255. nandrbl, 2, 1, do_switch_ecc,
  256. "switch between rbl/uboot NAND ECC calculation algorithm",
  257. "[rbl/uboot] - Switch between rbl/uboot NAND ECC algorithm"
  258. );
  259. #endif /* #ifdef CONFIG_NAND_DAVINCI */
  260. #ifdef CONFIG_DAVINCI_MMC
  261. static struct davinci_mmc mmc_sd0 = {
  262. .reg_base = (struct davinci_mmc_regs *)DAVINCI_MMC_SD0_BASE,
  263. .input_clk = 121500000,
  264. .host_caps = MMC_MODE_4BIT,
  265. .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
  266. .version = MMC_CTLR_VERSION_2,
  267. };
  268. int board_mmc_init(bd_t *bis)
  269. {
  270. int err;
  271. /* Add slot-0 to mmc subsystem */
  272. err = davinci_mmc_init(bis, &mmc_sd0);
  273. return err;
  274. }
  275. #endif
  276. int board_late_init(void)
  277. {
  278. struct davinci_gpio *gpio = davinci_gpio_bank45;
  279. /* 24MHz InputClock / 15 prediv -> 1.6 MHz timer running */
  280. while (get_timer_val() < 0x186a00)
  281. ;
  282. /* 1 sec reached -> stop timer, clear all LED */
  283. stop_timer();
  284. clrbits_le32(&gpio->out_data, CONFIG_CAM_ENC_LED_MASK);
  285. return 0;
  286. }
  287. void reset_phy(void)
  288. {
  289. char *name = "GENERIC @ 0x00";
  290. /* reset the phy */
  291. miiphy_reset(name, 0x0);
  292. }
  293. #else /* #ifndef CONFIG_SPL_BUILD */
  294. static void cam_enc_4xx_set_all_led(void)
  295. {
  296. struct davinci_gpio *gpio = davinci_gpio_bank45;
  297. setbits_le32(&gpio->out_data, CONFIG_CAM_ENC_LED_MASK);
  298. }
  299. /*
  300. * TIMER 0 is used for tick
  301. */
  302. static struct davinci_timer *timer =
  303. (struct davinci_timer *)DAVINCI_TIMER3_BASE;
  304. #define TIMER_LOAD_VAL 0xffffffff
  305. #define TIM_CLK_DIV 16
  306. static int cam_enc_4xx_timer_init(void)
  307. {
  308. /* We are using timer34 in unchained 32-bit mode, full speed */
  309. writel(0x0, &timer->tcr);
  310. writel(0x0, &timer->tgcr);
  311. writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr);
  312. writel(0x0, &timer->tim34);
  313. writel(TIMER_LOAD_VAL, &timer->prd34);
  314. writel(2 << 22, &timer->tcr);
  315. return 0;
  316. }
  317. void board_gpio_init(void)
  318. {
  319. struct davinci_gpio *gpio;
  320. cam_enc_4xx_set_all_led();
  321. cam_enc_4xx_timer_init();
  322. gpio = davinci_gpio_bank01;
  323. clrbits_le32(&gpio->dir, ~0xfdfffffe);
  324. /* clear LED D14 = GPIO25 */
  325. clrbits_le32(&gpio->out_data, 0x02000000);
  326. gpio = davinci_gpio_bank23;
  327. clrbits_le32(&gpio->dir, ~0x5ff0afef);
  328. /* set GPIO61 to 1 -> intern UART0 as Console */
  329. setbits_le32(&gpio->out_data, 0x20000000);
  330. /*
  331. * PHY out of reset GIO 50 = 1
  332. * NAND WP off GIO 51 = 1
  333. */
  334. setbits_le32(&gpio->out_data, 0x000c0004);
  335. gpio = davinci_gpio_bank45;
  336. clrbits_le32(&gpio->dir, ~(0xdb2fffff) | CONFIG_CAM_ENC_LED_MASK);
  337. /*
  338. * clear LED:
  339. * D17 = GPIO86
  340. * D11 = GPIO87
  341. * GPIO88
  342. * GPIO89
  343. * D13 = GPIO90
  344. * GPIO91
  345. */
  346. clrbits_le32(&gpio->out_data, CONFIG_CAM_ENC_LED_MASK);
  347. gpio = davinci_gpio_bank67;
  348. clrbits_le32(&gpio->dir, ~0x000007ff);
  349. }
  350. /*
  351. * functions for the post memory test.
  352. */
  353. int arch_memory_test_prepare(u32 *vstart, u32 *size, phys_addr_t *phys_offset)
  354. {
  355. *vstart = CONFIG_SYS_SDRAM_BASE;
  356. *size = PHYS_SDRAM_1_SIZE;
  357. *phys_offset = 0;
  358. return 0;
  359. }
  360. void arch_memory_failure_handle(void)
  361. {
  362. cam_enc_4xx_set_all_led();
  363. puts("mem failure\n");
  364. while (1)
  365. ;
  366. }
  367. #endif