ops_bcm4706.c 11 KB

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