ops_bcm4706.c 13 KB

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