ops_bcm4706.c 9.2 KB

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