ops_bcm4706.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * BCM47XX NAND flash driver
  3. *
  4. * Copyright (C) 2012 Rafał Miłecki <zajec5@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/bcma/bcma.h>
  15. #include "bcm47xxnflash.h"
  16. /* Broadcom uses 1'000'000 but it seems to be too many. Tests on WNDR4500 has
  17. * shown 164 retries as maxiumum. */
  18. #define NFLASH_READY_RETRIES 1000
  19. #define NFLASH_SECTOR_SIZE 512
  20. /**************************************************
  21. * Various helpers
  22. **************************************************/
  23. static inline u8 bcm47xxnflash_ops_bcm4706_ns_to_cycle(u16 ns, u16 clock)
  24. {
  25. return ((ns * 1000 * clock) / 1000000) + 1;
  26. }
  27. static int bcm47xxnflash_ops_bcm4706_ctl_cmd(struct bcma_drv_cc *cc, u32 code)
  28. {
  29. int i = 0;
  30. bcma_cc_write32(cc, BCMA_CC_NFLASH_CTL, 0x80000000 | code);
  31. for (i = 0; i < NFLASH_READY_RETRIES; i++) {
  32. if (!(bcma_cc_read32(cc, BCMA_CC_NFLASH_CTL) & 0x80000000)) {
  33. i = 0;
  34. break;
  35. }
  36. }
  37. if (i) {
  38. pr_err("NFLASH control command not ready!\n");
  39. return -EBUSY;
  40. }
  41. return 0;
  42. }
  43. static int bcm47xxnflash_ops_bcm4706_poll(struct bcma_drv_cc *cc)
  44. {
  45. int i;
  46. for (i = 0; i < NFLASH_READY_RETRIES; i++) {
  47. if (bcma_cc_read32(cc, BCMA_CC_NFLASH_CTL) & 0x04000000) {
  48. if (bcma_cc_read32(cc, BCMA_CC_NFLASH_CTL) &
  49. BCMA_CC_NFLASH_CTL_ERR) {
  50. pr_err("Error on polling\n");
  51. return -EBUSY;
  52. } else {
  53. return 0;
  54. }
  55. }
  56. }
  57. pr_err("Polling timeout!\n");
  58. return -EBUSY;
  59. }
  60. /**************************************************
  61. * R/W
  62. **************************************************/
  63. static void bcm47xxnflash_ops_bcm4706_read(struct mtd_info *mtd, uint8_t *buf,
  64. int len)
  65. {
  66. struct nand_chip *nand_chip = (struct nand_chip *)mtd->priv;
  67. struct bcm47xxnflash *b47n = (struct bcm47xxnflash *)nand_chip->priv;
  68. u32 ctlcode;
  69. u32 *dest = (u32 *)buf;
  70. int i;
  71. int toread;
  72. BUG_ON(b47n->curr_page_addr & ~nand_chip->pagemask);
  73. /* Don't validate column using nand_chip->page_shift, it may be bigger
  74. * when accessing OOB */
  75. while (len) {
  76. /* We can read maximum of 0x200 bytes at once */
  77. toread = min(len, 0x200);
  78. /* Set page and column */
  79. bcma_cc_write32(b47n->cc, BCMA_CC_NFLASH_COL_ADDR,
  80. b47n->curr_column);
  81. bcma_cc_write32(b47n->cc, BCMA_CC_NFLASH_ROW_ADDR,
  82. b47n->curr_page_addr);
  83. /* Prepare to read */
  84. ctlcode = 0x40000000 | 0x00080000 | 0x00040000 | 0x00020000 |
  85. 0x00010000;
  86. ctlcode |= NAND_CMD_READSTART << 8;
  87. if (bcm47xxnflash_ops_bcm4706_ctl_cmd(b47n->cc, ctlcode))
  88. return;
  89. if (bcm47xxnflash_ops_bcm4706_poll(b47n->cc))
  90. return;
  91. /* Eventually read some data :) */
  92. for (i = 0; i < toread; i += 4, dest++) {
  93. ctlcode = 0x40000000 | 0x30000000 | 0x00100000;
  94. if (i == toread - 4) /* Last read goes without that */
  95. ctlcode &= ~0x40000000;
  96. if (bcm47xxnflash_ops_bcm4706_ctl_cmd(b47n->cc,
  97. ctlcode))
  98. return;
  99. *dest = bcma_cc_read32(b47n->cc, BCMA_CC_NFLASH_DATA);
  100. }
  101. b47n->curr_column += toread;
  102. len -= toread;
  103. }
  104. }
  105. /**************************************************
  106. * NAND chip ops
  107. **************************************************/
  108. /* Default nand_select_chip calls cmd_ctrl, which is not used in BCM4706 */
  109. static void bcm47xxnflash_ops_bcm4706_select_chip(struct mtd_info *mtd,
  110. int chip)
  111. {
  112. return;
  113. }
  114. /*
  115. * Default nand_command and nand_command_lp don't match BCM4706 hardware layout.
  116. * For example, reading chip id is performed in a non-standard way.
  117. * Setting column and page is also handled differently, we use a special
  118. * registers of ChipCommon core. Hacking cmd_ctrl to understand and convert
  119. * standard commands would be much more complicated.
  120. */
  121. static void bcm47xxnflash_ops_bcm4706_cmdfunc(struct mtd_info *mtd,
  122. unsigned command, int column,
  123. int page_addr)
  124. {
  125. struct nand_chip *nand_chip = (struct nand_chip *)mtd->priv;
  126. struct bcm47xxnflash *b47n = (struct bcm47xxnflash *)nand_chip->priv;
  127. u32 ctlcode;
  128. int i;
  129. if (column != -1)
  130. b47n->curr_column = column;
  131. if (page_addr != -1)
  132. b47n->curr_page_addr = page_addr;
  133. switch (command) {
  134. case NAND_CMD_RESET:
  135. pr_warn("Chip reset not implemented yet\n");
  136. break;
  137. case NAND_CMD_READID:
  138. ctlcode = 0x40000000 | 0x01000000 | 0x00080000 | 0x00010000;
  139. ctlcode |= NAND_CMD_READID;
  140. if (bcm47xxnflash_ops_bcm4706_ctl_cmd(b47n->cc, ctlcode)) {
  141. pr_err("READID error\n");
  142. break;
  143. }
  144. /*
  145. * Reading is specific, last one has to go without 0x40000000
  146. * bit. We don't know how many reads NAND subsystem is going
  147. * to perform, so cache everything.
  148. */
  149. for (i = 0; i < ARRAY_SIZE(b47n->id_data); i++) {
  150. ctlcode = 0x40000000 | 0x00100000;
  151. if (i == ARRAY_SIZE(b47n->id_data) - 1)
  152. ctlcode &= ~0x40000000;
  153. if (bcm47xxnflash_ops_bcm4706_ctl_cmd(b47n->cc,
  154. ctlcode)) {
  155. pr_err("READID error\n");
  156. break;
  157. }
  158. b47n->id_data[i] =
  159. bcma_cc_read32(b47n->cc, BCMA_CC_NFLASH_DATA)
  160. & 0xFF;
  161. }
  162. break;
  163. case NAND_CMD_READ0:
  164. break;
  165. case NAND_CMD_READOOB:
  166. if (page_addr != -1)
  167. b47n->curr_column += mtd->writesize;
  168. break;
  169. default:
  170. pr_err("Command 0x%X unsupported\n", command);
  171. break;
  172. }
  173. b47n->curr_command = command;
  174. }
  175. static u8 bcm47xxnflash_ops_bcm4706_read_byte(struct mtd_info *mtd)
  176. {
  177. struct nand_chip *nand_chip = (struct nand_chip *)mtd->priv;
  178. struct bcm47xxnflash *b47n = (struct bcm47xxnflash *)nand_chip->priv;
  179. u32 tmp = 0;
  180. switch (b47n->curr_command) {
  181. case NAND_CMD_READID:
  182. if (b47n->curr_column >= ARRAY_SIZE(b47n->id_data)) {
  183. pr_err("Requested invalid id_data: %d\n",
  184. b47n->curr_column);
  185. return 0;
  186. }
  187. return b47n->id_data[b47n->curr_column++];
  188. case NAND_CMD_READOOB:
  189. bcm47xxnflash_ops_bcm4706_read(mtd, (u8 *)&tmp, 4);
  190. return tmp & 0xFF;
  191. }
  192. pr_err("Invalid command for byte read: 0x%X\n", b47n->curr_command);
  193. return 0;
  194. }
  195. static void bcm47xxnflash_ops_bcm4706_read_buf(struct mtd_info *mtd,
  196. uint8_t *buf, int len)
  197. {
  198. struct nand_chip *nand_chip = (struct nand_chip *)mtd->priv;
  199. struct bcm47xxnflash *b47n = (struct bcm47xxnflash *)nand_chip->priv;
  200. switch (b47n->curr_command) {
  201. case NAND_CMD_READ0:
  202. case NAND_CMD_READOOB:
  203. bcm47xxnflash_ops_bcm4706_read(mtd, buf, len);
  204. return;
  205. }
  206. pr_err("Invalid command for buf read: 0x%X\n", b47n->curr_command);
  207. }
  208. /**************************************************
  209. * Init
  210. **************************************************/
  211. int bcm47xxnflash_ops_bcm4706_init(struct bcm47xxnflash *b47n)
  212. {
  213. int err;
  214. u32 freq;
  215. u16 clock;
  216. u8 w0, w1, w2, w3, w4;
  217. unsigned long chipsize; /* MiB */
  218. u8 tbits, col_bits, col_size, row_bits, row_bsize;
  219. u32 val;
  220. b47n->nand_chip.select_chip = bcm47xxnflash_ops_bcm4706_select_chip;
  221. b47n->nand_chip.cmdfunc = bcm47xxnflash_ops_bcm4706_cmdfunc;
  222. b47n->nand_chip.read_byte = bcm47xxnflash_ops_bcm4706_read_byte;
  223. b47n->nand_chip.read_buf = bcm47xxnflash_ops_bcm4706_read_buf;
  224. b47n->nand_chip.bbt_options = NAND_BBT_USE_FLASH;
  225. b47n->nand_chip.ecc.mode = NAND_ECC_NONE; /* TODO: implement ECC */
  226. /* Enable NAND flash access */
  227. bcma_cc_set32(b47n->cc, BCMA_CC_4706_FLASHSCFG,
  228. BCMA_CC_4706_FLASHSCFG_NF1);
  229. /* Configure wait counters */
  230. if (b47n->cc->status & BCMA_CC_CHIPST_4706_PKG_OPTION) {
  231. freq = 100000000;
  232. } else {
  233. freq = bcma_chipco_pll_read(b47n->cc, 4);
  234. freq = (freq * 0xFFF) >> 3;
  235. freq = (freq * 25000000) >> 3;
  236. }
  237. clock = freq / 1000000;
  238. w0 = bcm47xxnflash_ops_bcm4706_ns_to_cycle(15, clock);
  239. w1 = bcm47xxnflash_ops_bcm4706_ns_to_cycle(20, clock);
  240. w2 = bcm47xxnflash_ops_bcm4706_ns_to_cycle(10, clock);
  241. w3 = bcm47xxnflash_ops_bcm4706_ns_to_cycle(10, clock);
  242. w4 = bcm47xxnflash_ops_bcm4706_ns_to_cycle(100, clock);
  243. bcma_cc_write32(b47n->cc, BCMA_CC_NFLASH_WAITCNT0,
  244. (w4 << 24 | w3 << 18 | w2 << 12 | w1 << 6 | w0));
  245. /* Scan NAND */
  246. err = nand_scan(&b47n->mtd, 1);
  247. if (err) {
  248. pr_err("Could not scan NAND flash: %d\n", err);
  249. goto exit;
  250. }
  251. /* Configure FLASH */
  252. chipsize = b47n->nand_chip.chipsize >> 20;
  253. tbits = ffs(chipsize); /* find first bit set */
  254. if (!tbits || tbits != fls(chipsize)) {
  255. pr_err("Invalid flash size: 0x%lX\n", chipsize);
  256. err = -ENOTSUPP;
  257. goto exit;
  258. }
  259. tbits += 19; /* Broadcom increases *index* by 20, we increase *pos* */
  260. col_bits = b47n->nand_chip.page_shift + 1;
  261. col_size = (col_bits + 7) / 8;
  262. row_bits = tbits - col_bits + 1;
  263. row_bsize = (row_bits + 7) / 8;
  264. val = ((row_bsize - 1) << 6) | ((col_size - 1) << 4) | 2;
  265. bcma_cc_write32(b47n->cc, BCMA_CC_NFLASH_CONF, val);
  266. exit:
  267. if (err)
  268. bcma_cc_mask32(b47n->cc, BCMA_CC_4706_FLASHSCFG,
  269. ~BCMA_CC_4706_FLASHSCFG_NF1);
  270. return err;
  271. }