cam_enc_4xx.c 9.8 KB

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