fsmc_nand.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * drivers/mtd/nand/fsmc_nand.c
  3. *
  4. * ST Microelectronics
  5. * Flexible Static Memory Controller (FSMC)
  6. * Driver for NAND portions
  7. *
  8. * Copyright © 2010 ST Microelectronics
  9. * Vipin Kumar <vipin.kumar@st.com>
  10. * Ashish Priyadarshi
  11. *
  12. * Based on drivers/mtd/nand/nomadik_nand.c
  13. *
  14. * This file is licensed under the terms of the GNU General Public
  15. * License version 2. This program is licensed "as is" without any
  16. * warranty of any kind, whether express or implied.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/resource.h>
  23. #include <linux/sched.h>
  24. #include <linux/types.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/nand.h>
  27. #include <linux/mtd/nand_ecc.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/io.h>
  31. #include <linux/slab.h>
  32. #include <linux/mtd/fsmc.h>
  33. #include <linux/amba/bus.h>
  34. #include <mtd/mtd-abi.h>
  35. static struct nand_ecclayout fsmc_ecc1_layout = {
  36. .eccbytes = 24,
  37. .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52,
  38. 66, 67, 68, 82, 83, 84, 98, 99, 100, 114, 115, 116},
  39. .oobfree = {
  40. {.offset = 8, .length = 8},
  41. {.offset = 24, .length = 8},
  42. {.offset = 40, .length = 8},
  43. {.offset = 56, .length = 8},
  44. {.offset = 72, .length = 8},
  45. {.offset = 88, .length = 8},
  46. {.offset = 104, .length = 8},
  47. {.offset = 120, .length = 8}
  48. }
  49. };
  50. static struct nand_ecclayout fsmc_ecc4_lp_layout = {
  51. .eccbytes = 104,
  52. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  53. 9, 10, 11, 12, 13, 14,
  54. 18, 19, 20, 21, 22, 23, 24,
  55. 25, 26, 27, 28, 29, 30,
  56. 34, 35, 36, 37, 38, 39, 40,
  57. 41, 42, 43, 44, 45, 46,
  58. 50, 51, 52, 53, 54, 55, 56,
  59. 57, 58, 59, 60, 61, 62,
  60. 66, 67, 68, 69, 70, 71, 72,
  61. 73, 74, 75, 76, 77, 78,
  62. 82, 83, 84, 85, 86, 87, 88,
  63. 89, 90, 91, 92, 93, 94,
  64. 98, 99, 100, 101, 102, 103, 104,
  65. 105, 106, 107, 108, 109, 110,
  66. 114, 115, 116, 117, 118, 119, 120,
  67. 121, 122, 123, 124, 125, 126
  68. },
  69. .oobfree = {
  70. {.offset = 15, .length = 3},
  71. {.offset = 31, .length = 3},
  72. {.offset = 47, .length = 3},
  73. {.offset = 63, .length = 3},
  74. {.offset = 79, .length = 3},
  75. {.offset = 95, .length = 3},
  76. {.offset = 111, .length = 3},
  77. {.offset = 127, .length = 1}
  78. }
  79. };
  80. /*
  81. * ECC placement definitions in oobfree type format.
  82. * There are 13 bytes of ecc for every 512 byte block and it has to be read
  83. * consecutively and immediately after the 512 byte data block for hardware to
  84. * generate the error bit offsets in 512 byte data.
  85. * Managing the ecc bytes in the following way makes it easier for software to
  86. * read ecc bytes consecutive to data bytes. This way is similar to
  87. * oobfree structure maintained already in generic nand driver
  88. */
  89. static struct fsmc_eccplace fsmc_ecc4_lp_place = {
  90. .eccplace = {
  91. {.offset = 2, .length = 13},
  92. {.offset = 18, .length = 13},
  93. {.offset = 34, .length = 13},
  94. {.offset = 50, .length = 13},
  95. {.offset = 66, .length = 13},
  96. {.offset = 82, .length = 13},
  97. {.offset = 98, .length = 13},
  98. {.offset = 114, .length = 13}
  99. }
  100. };
  101. static struct nand_ecclayout fsmc_ecc4_sp_layout = {
  102. .eccbytes = 13,
  103. .eccpos = { 0, 1, 2, 3, 6, 7, 8,
  104. 9, 10, 11, 12, 13, 14
  105. },
  106. .oobfree = {
  107. {.offset = 15, .length = 1},
  108. }
  109. };
  110. static struct fsmc_eccplace fsmc_ecc4_sp_place = {
  111. .eccplace = {
  112. {.offset = 0, .length = 4},
  113. {.offset = 6, .length = 9}
  114. }
  115. };
  116. /*
  117. * Default partition tables to be used if the partition information not
  118. * provided through platform data.
  119. *
  120. * Default partition layout for small page(= 512 bytes) devices
  121. * Size for "Root file system" is updated in driver based on actual device size
  122. */
  123. static struct mtd_partition partition_info_16KB_blk[] = {
  124. {
  125. .name = "X-loader",
  126. .offset = 0,
  127. .size = 4*0x4000,
  128. },
  129. {
  130. .name = "U-Boot",
  131. .offset = 0x10000,
  132. .size = 20*0x4000,
  133. },
  134. {
  135. .name = "Kernel",
  136. .offset = 0x60000,
  137. .size = 256*0x4000,
  138. },
  139. {
  140. .name = "Root File System",
  141. .offset = 0x460000,
  142. .size = MTDPART_SIZ_FULL,
  143. },
  144. };
  145. /*
  146. * Default partition layout for large page(> 512 bytes) devices
  147. * Size for "Root file system" is updated in driver based on actual device size
  148. */
  149. static struct mtd_partition partition_info_128KB_blk[] = {
  150. {
  151. .name = "X-loader",
  152. .offset = 0,
  153. .size = 4*0x20000,
  154. },
  155. {
  156. .name = "U-Boot",
  157. .offset = 0x80000,
  158. .size = 12*0x20000,
  159. },
  160. {
  161. .name = "Kernel",
  162. .offset = 0x200000,
  163. .size = 48*0x20000,
  164. },
  165. {
  166. .name = "Root File System",
  167. .offset = 0x800000,
  168. .size = MTDPART_SIZ_FULL,
  169. },
  170. };
  171. /**
  172. * struct fsmc_nand_data - structure for FSMC NAND device state
  173. *
  174. * @pid: Part ID on the AMBA PrimeCell format
  175. * @mtd: MTD info for a NAND flash.
  176. * @nand: Chip related info for a NAND flash.
  177. *
  178. * @ecc_place: ECC placing locations in oobfree type format.
  179. * @bank: Bank number for probed device.
  180. * @clk: Clock structure for FSMC.
  181. *
  182. * @data_va: NAND port for Data.
  183. * @cmd_va: NAND port for Command.
  184. * @addr_va: NAND port for Address.
  185. * @regs_va: FSMC regs base address.
  186. */
  187. struct fsmc_nand_data {
  188. u32 pid;
  189. struct mtd_info mtd;
  190. struct nand_chip nand;
  191. struct fsmc_eccplace *ecc_place;
  192. unsigned int bank;
  193. struct clk *clk;
  194. struct resource *resregs;
  195. struct resource *rescmd;
  196. struct resource *resaddr;
  197. struct resource *resdata;
  198. void __iomem *data_va;
  199. void __iomem *cmd_va;
  200. void __iomem *addr_va;
  201. void __iomem *regs_va;
  202. void (*select_chip)(uint32_t bank, uint32_t busw);
  203. };
  204. /* Assert CS signal based on chipnr */
  205. static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
  206. {
  207. struct nand_chip *chip = mtd->priv;
  208. struct fsmc_nand_data *host;
  209. host = container_of(mtd, struct fsmc_nand_data, mtd);
  210. switch (chipnr) {
  211. case -1:
  212. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  213. break;
  214. case 0:
  215. case 1:
  216. case 2:
  217. case 3:
  218. if (host->select_chip)
  219. host->select_chip(chipnr,
  220. chip->options & NAND_BUSWIDTH_16);
  221. break;
  222. default:
  223. BUG();
  224. }
  225. }
  226. /*
  227. * fsmc_cmd_ctrl - For facilitaing Hardware access
  228. * This routine allows hardware specific access to control-lines(ALE,CLE)
  229. */
  230. static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  231. {
  232. struct nand_chip *this = mtd->priv;
  233. struct fsmc_nand_data *host = container_of(mtd,
  234. struct fsmc_nand_data, mtd);
  235. struct fsmc_regs *regs = host->regs_va;
  236. unsigned int bank = host->bank;
  237. if (ctrl & NAND_CTRL_CHANGE) {
  238. if (ctrl & NAND_CLE) {
  239. this->IO_ADDR_R = (void __iomem *)host->cmd_va;
  240. this->IO_ADDR_W = (void __iomem *)host->cmd_va;
  241. } else if (ctrl & NAND_ALE) {
  242. this->IO_ADDR_R = (void __iomem *)host->addr_va;
  243. this->IO_ADDR_W = (void __iomem *)host->addr_va;
  244. } else {
  245. this->IO_ADDR_R = (void __iomem *)host->data_va;
  246. this->IO_ADDR_W = (void __iomem *)host->data_va;
  247. }
  248. if (ctrl & NAND_NCE) {
  249. writel(readl(&regs->bank_regs[bank].pc) | FSMC_ENABLE,
  250. &regs->bank_regs[bank].pc);
  251. } else {
  252. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ENABLE,
  253. &regs->bank_regs[bank].pc);
  254. }
  255. }
  256. mb();
  257. if (cmd != NAND_CMD_NONE)
  258. writeb(cmd, this->IO_ADDR_W);
  259. }
  260. /*
  261. * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
  262. *
  263. * This routine initializes timing parameters related to NAND memory access in
  264. * FSMC registers
  265. */
  266. static void __init fsmc_nand_setup(struct fsmc_regs *regs, uint32_t bank,
  267. uint32_t busw)
  268. {
  269. uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
  270. if (busw)
  271. writel(value | FSMC_DEVWID_16, &regs->bank_regs[bank].pc);
  272. else
  273. writel(value | FSMC_DEVWID_8, &regs->bank_regs[bank].pc);
  274. writel(readl(&regs->bank_regs[bank].pc) | FSMC_TCLR_1 | FSMC_TAR_1,
  275. &regs->bank_regs[bank].pc);
  276. writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
  277. &regs->bank_regs[bank].comm);
  278. writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
  279. &regs->bank_regs[bank].attrib);
  280. }
  281. /*
  282. * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
  283. */
  284. static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
  285. {
  286. struct fsmc_nand_data *host = container_of(mtd,
  287. struct fsmc_nand_data, mtd);
  288. struct fsmc_regs *regs = host->regs_va;
  289. uint32_t bank = host->bank;
  290. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCPLEN_256,
  291. &regs->bank_regs[bank].pc);
  292. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCEN,
  293. &regs->bank_regs[bank].pc);
  294. writel(readl(&regs->bank_regs[bank].pc) | FSMC_ECCEN,
  295. &regs->bank_regs[bank].pc);
  296. }
  297. /*
  298. * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
  299. * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
  300. * max of 8-bits)
  301. */
  302. static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
  303. uint8_t *ecc)
  304. {
  305. struct fsmc_nand_data *host = container_of(mtd,
  306. struct fsmc_nand_data, mtd);
  307. struct fsmc_regs *regs = host->regs_va;
  308. uint32_t bank = host->bank;
  309. uint32_t ecc_tmp;
  310. unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
  311. do {
  312. if (readl(&regs->bank_regs[bank].sts) & FSMC_CODE_RDY)
  313. break;
  314. else
  315. cond_resched();
  316. } while (!time_after_eq(jiffies, deadline));
  317. ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
  318. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  319. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  320. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  321. ecc[3] = (uint8_t) (ecc_tmp >> 24);
  322. ecc_tmp = readl(&regs->bank_regs[bank].ecc2);
  323. ecc[4] = (uint8_t) (ecc_tmp >> 0);
  324. ecc[5] = (uint8_t) (ecc_tmp >> 8);
  325. ecc[6] = (uint8_t) (ecc_tmp >> 16);
  326. ecc[7] = (uint8_t) (ecc_tmp >> 24);
  327. ecc_tmp = readl(&regs->bank_regs[bank].ecc3);
  328. ecc[8] = (uint8_t) (ecc_tmp >> 0);
  329. ecc[9] = (uint8_t) (ecc_tmp >> 8);
  330. ecc[10] = (uint8_t) (ecc_tmp >> 16);
  331. ecc[11] = (uint8_t) (ecc_tmp >> 24);
  332. ecc_tmp = readl(&regs->bank_regs[bank].sts);
  333. ecc[12] = (uint8_t) (ecc_tmp >> 16);
  334. return 0;
  335. }
  336. /*
  337. * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
  338. * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
  339. * max of 1-bit)
  340. */
  341. static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
  342. uint8_t *ecc)
  343. {
  344. struct fsmc_nand_data *host = container_of(mtd,
  345. struct fsmc_nand_data, mtd);
  346. struct fsmc_regs *regs = host->regs_va;
  347. uint32_t bank = host->bank;
  348. uint32_t ecc_tmp;
  349. ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
  350. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  351. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  352. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  353. return 0;
  354. }
  355. /*
  356. * fsmc_read_page_hwecc
  357. * @mtd: mtd info structure
  358. * @chip: nand chip info structure
  359. * @buf: buffer to store read data
  360. * @page: page number to read
  361. *
  362. * This routine is needed for fsmc version 8 as reading from NAND chip has to be
  363. * performed in a strict sequence as follows:
  364. * data(512 byte) -> ecc(13 byte)
  365. * After this read, fsmc hardware generates and reports error data bits(up to a
  366. * max of 8 bits)
  367. */
  368. static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  369. uint8_t *buf, int page)
  370. {
  371. struct fsmc_nand_data *host = container_of(mtd,
  372. struct fsmc_nand_data, mtd);
  373. struct fsmc_eccplace *ecc_place = host->ecc_place;
  374. int i, j, s, stat, eccsize = chip->ecc.size;
  375. int eccbytes = chip->ecc.bytes;
  376. int eccsteps = chip->ecc.steps;
  377. uint8_t *p = buf;
  378. uint8_t *ecc_calc = chip->buffers->ecccalc;
  379. uint8_t *ecc_code = chip->buffers->ecccode;
  380. int off, len, group = 0;
  381. /*
  382. * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
  383. * end up reading 14 bytes (7 words) from oob. The local array is
  384. * to maintain word alignment
  385. */
  386. uint16_t ecc_oob[7];
  387. uint8_t *oob = (uint8_t *)&ecc_oob[0];
  388. for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
  389. chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
  390. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  391. chip->read_buf(mtd, p, eccsize);
  392. for (j = 0; j < eccbytes;) {
  393. off = ecc_place->eccplace[group].offset;
  394. len = ecc_place->eccplace[group].length;
  395. group++;
  396. /*
  397. * length is intentionally kept a higher multiple of 2
  398. * to read at least 13 bytes even in case of 16 bit NAND
  399. * devices
  400. */
  401. len = roundup(len, 2);
  402. chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
  403. chip->read_buf(mtd, oob + j, len);
  404. j += len;
  405. }
  406. memcpy(&ecc_code[i], oob, 13);
  407. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  408. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  409. if (stat < 0)
  410. mtd->ecc_stats.failed++;
  411. else
  412. mtd->ecc_stats.corrected += stat;
  413. }
  414. return 0;
  415. }
  416. /*
  417. * fsmc_correct_data
  418. * @mtd: mtd info structure
  419. * @dat: buffer of read data
  420. * @read_ecc: ecc read from device spare area
  421. * @calc_ecc: ecc calculated from read data
  422. *
  423. * calc_ecc is a 104 bit information containing maximum of 8 error
  424. * offset informations of 13 bits each in 512 bytes of read data.
  425. */
  426. static int fsmc_correct_data(struct mtd_info *mtd, uint8_t *dat,
  427. uint8_t *read_ecc, uint8_t *calc_ecc)
  428. {
  429. struct fsmc_nand_data *host = container_of(mtd,
  430. struct fsmc_nand_data, mtd);
  431. struct fsmc_regs *regs = host->regs_va;
  432. unsigned int bank = host->bank;
  433. uint16_t err_idx[8];
  434. uint64_t ecc_data[2];
  435. uint32_t num_err, i;
  436. /* The calculated ecc is actually the correction index in data */
  437. memcpy(ecc_data, calc_ecc, 13);
  438. /*
  439. * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
  440. * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
  441. *
  442. * calc_ecc is a 104 bit information containing maximum of 8 error
  443. * offset informations of 13 bits each. calc_ecc is copied into a
  444. * uint64_t array and error offset indexes are populated in err_idx
  445. * array
  446. */
  447. for (i = 0; i < 8; i++) {
  448. if (i == 4) {
  449. err_idx[4] = ((ecc_data[1] & 0x1) << 12) | ecc_data[0];
  450. ecc_data[1] >>= 1;
  451. continue;
  452. }
  453. err_idx[i] = (ecc_data[i/4] & 0x1FFF);
  454. ecc_data[i/4] >>= 13;
  455. }
  456. num_err = (readl(&regs->bank_regs[bank].sts) >> 10) & 0xF;
  457. if (num_err == 0xF)
  458. return -EBADMSG;
  459. i = 0;
  460. while (num_err--) {
  461. change_bit(0, (unsigned long *)&err_idx[i]);
  462. change_bit(1, (unsigned long *)&err_idx[i]);
  463. if (err_idx[i] <= 512 * 8) {
  464. change_bit(err_idx[i], (unsigned long *)dat);
  465. i++;
  466. }
  467. }
  468. return i;
  469. }
  470. /*
  471. * fsmc_nand_probe - Probe function
  472. * @pdev: platform device structure
  473. */
  474. static int __init fsmc_nand_probe(struct platform_device *pdev)
  475. {
  476. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  477. struct fsmc_nand_data *host;
  478. struct mtd_info *mtd;
  479. struct nand_chip *nand;
  480. struct fsmc_regs *regs;
  481. struct resource *res;
  482. int ret = 0;
  483. u32 pid;
  484. int i;
  485. if (!pdata) {
  486. dev_err(&pdev->dev, "platform data is NULL\n");
  487. return -EINVAL;
  488. }
  489. /* Allocate memory for the device structure (and zero it) */
  490. host = kzalloc(sizeof(*host), GFP_KERNEL);
  491. if (!host) {
  492. dev_err(&pdev->dev, "failed to allocate device structure\n");
  493. return -ENOMEM;
  494. }
  495. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
  496. if (!res) {
  497. ret = -EIO;
  498. goto err_probe1;
  499. }
  500. host->resdata = request_mem_region(res->start, resource_size(res),
  501. pdev->name);
  502. if (!host->resdata) {
  503. ret = -EIO;
  504. goto err_probe1;
  505. }
  506. host->data_va = ioremap(res->start, resource_size(res));
  507. if (!host->data_va) {
  508. ret = -EIO;
  509. goto err_probe1;
  510. }
  511. host->resaddr = request_mem_region(res->start + PLAT_NAND_ALE,
  512. resource_size(res), pdev->name);
  513. if (!host->resaddr) {
  514. ret = -EIO;
  515. goto err_probe1;
  516. }
  517. host->addr_va = ioremap(res->start + PLAT_NAND_ALE, resource_size(res));
  518. if (!host->addr_va) {
  519. ret = -EIO;
  520. goto err_probe1;
  521. }
  522. host->rescmd = request_mem_region(res->start + PLAT_NAND_CLE,
  523. resource_size(res), pdev->name);
  524. if (!host->rescmd) {
  525. ret = -EIO;
  526. goto err_probe1;
  527. }
  528. host->cmd_va = ioremap(res->start + PLAT_NAND_CLE, resource_size(res));
  529. if (!host->cmd_va) {
  530. ret = -EIO;
  531. goto err_probe1;
  532. }
  533. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
  534. if (!res) {
  535. ret = -EIO;
  536. goto err_probe1;
  537. }
  538. host->resregs = request_mem_region(res->start, resource_size(res),
  539. pdev->name);
  540. if (!host->resregs) {
  541. ret = -EIO;
  542. goto err_probe1;
  543. }
  544. host->regs_va = ioremap(res->start, resource_size(res));
  545. if (!host->regs_va) {
  546. ret = -EIO;
  547. goto err_probe1;
  548. }
  549. host->clk = clk_get(&pdev->dev, NULL);
  550. if (IS_ERR(host->clk)) {
  551. dev_err(&pdev->dev, "failed to fetch block clock\n");
  552. ret = PTR_ERR(host->clk);
  553. host->clk = NULL;
  554. goto err_probe1;
  555. }
  556. ret = clk_enable(host->clk);
  557. if (ret)
  558. goto err_probe1;
  559. /*
  560. * This device ID is actually a common AMBA ID as used on the
  561. * AMBA PrimeCell bus. However it is not a PrimeCell.
  562. */
  563. for (pid = 0, i = 0; i < 4; i++)
  564. pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
  565. host->pid = pid;
  566. dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
  567. "revision %02x, config %02x\n",
  568. AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
  569. AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
  570. host->bank = pdata->bank;
  571. host->select_chip = pdata->select_bank;
  572. regs = host->regs_va;
  573. /* Link all private pointers */
  574. mtd = &host->mtd;
  575. nand = &host->nand;
  576. mtd->priv = nand;
  577. nand->priv = host;
  578. host->mtd.owner = THIS_MODULE;
  579. nand->IO_ADDR_R = host->data_va;
  580. nand->IO_ADDR_W = host->data_va;
  581. nand->cmd_ctrl = fsmc_cmd_ctrl;
  582. nand->chip_delay = 30;
  583. nand->ecc.mode = NAND_ECC_HW;
  584. nand->ecc.hwctl = fsmc_enable_hwecc;
  585. nand->ecc.size = 512;
  586. nand->options = pdata->options;
  587. nand->select_chip = fsmc_select_chip;
  588. if (pdata->width == FSMC_NAND_BW16)
  589. nand->options |= NAND_BUSWIDTH_16;
  590. fsmc_nand_setup(regs, host->bank, nand->options & NAND_BUSWIDTH_16);
  591. if (AMBA_REV_BITS(host->pid) >= 8) {
  592. nand->ecc.read_page = fsmc_read_page_hwecc;
  593. nand->ecc.calculate = fsmc_read_hwecc_ecc4;
  594. nand->ecc.correct = fsmc_correct_data;
  595. nand->ecc.bytes = 13;
  596. } else {
  597. nand->ecc.calculate = fsmc_read_hwecc_ecc1;
  598. nand->ecc.correct = nand_correct_data;
  599. nand->ecc.bytes = 3;
  600. }
  601. /*
  602. * Scan to find existence of the device
  603. */
  604. if (nand_scan_ident(&host->mtd, 1, NULL)) {
  605. ret = -ENXIO;
  606. dev_err(&pdev->dev, "No NAND Device found!\n");
  607. goto err_probe;
  608. }
  609. if (AMBA_REV_BITS(host->pid) >= 8) {
  610. if (host->mtd.writesize == 512) {
  611. nand->ecc.layout = &fsmc_ecc4_sp_layout;
  612. host->ecc_place = &fsmc_ecc4_sp_place;
  613. } else {
  614. nand->ecc.layout = &fsmc_ecc4_lp_layout;
  615. host->ecc_place = &fsmc_ecc4_lp_place;
  616. }
  617. } else {
  618. nand->ecc.layout = &fsmc_ecc1_layout;
  619. }
  620. /* Second stage of scan to fill MTD data-structures */
  621. if (nand_scan_tail(&host->mtd)) {
  622. ret = -ENXIO;
  623. goto err_probe;
  624. }
  625. /*
  626. * The partition information can is accessed by (in the same precedence)
  627. *
  628. * command line through Bootloader,
  629. * platform data,
  630. * default partition information present in driver.
  631. */
  632. /*
  633. * Check for partition info passed
  634. */
  635. host->mtd.name = "nand";
  636. ret = mtd_device_parse_register(&host->mtd, NULL, 0,
  637. host->mtd.size <= 0x04000000 ?
  638. partition_info_16KB_blk :
  639. partition_info_128KB_blk,
  640. host->mtd.size <= 0x04000000 ?
  641. ARRAY_SIZE(partition_info_16KB_blk) :
  642. ARRAY_SIZE(partition_info_128KB_blk));
  643. if (ret)
  644. goto err_probe;
  645. platform_set_drvdata(pdev, host);
  646. dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
  647. return 0;
  648. err_probe:
  649. clk_disable(host->clk);
  650. err_probe1:
  651. if (host->clk)
  652. clk_put(host->clk);
  653. if (host->regs_va)
  654. iounmap(host->regs_va);
  655. if (host->resregs)
  656. release_mem_region(host->resregs->start,
  657. resource_size(host->resregs));
  658. if (host->cmd_va)
  659. iounmap(host->cmd_va);
  660. if (host->rescmd)
  661. release_mem_region(host->rescmd->start,
  662. resource_size(host->rescmd));
  663. if (host->addr_va)
  664. iounmap(host->addr_va);
  665. if (host->resaddr)
  666. release_mem_region(host->resaddr->start,
  667. resource_size(host->resaddr));
  668. if (host->data_va)
  669. iounmap(host->data_va);
  670. if (host->resdata)
  671. release_mem_region(host->resdata->start,
  672. resource_size(host->resdata));
  673. kfree(host);
  674. return ret;
  675. }
  676. /*
  677. * Clean up routine
  678. */
  679. static int fsmc_nand_remove(struct platform_device *pdev)
  680. {
  681. struct fsmc_nand_data *host = platform_get_drvdata(pdev);
  682. platform_set_drvdata(pdev, NULL);
  683. if (host) {
  684. nand_release(&host->mtd);
  685. clk_disable(host->clk);
  686. clk_put(host->clk);
  687. iounmap(host->regs_va);
  688. release_mem_region(host->resregs->start,
  689. resource_size(host->resregs));
  690. iounmap(host->cmd_va);
  691. release_mem_region(host->rescmd->start,
  692. resource_size(host->rescmd));
  693. iounmap(host->addr_va);
  694. release_mem_region(host->resaddr->start,
  695. resource_size(host->resaddr));
  696. iounmap(host->data_va);
  697. release_mem_region(host->resdata->start,
  698. resource_size(host->resdata));
  699. kfree(host);
  700. }
  701. return 0;
  702. }
  703. #ifdef CONFIG_PM
  704. static int fsmc_nand_suspend(struct device *dev)
  705. {
  706. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  707. if (host)
  708. clk_disable(host->clk);
  709. return 0;
  710. }
  711. static int fsmc_nand_resume(struct device *dev)
  712. {
  713. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  714. if (host)
  715. clk_enable(host->clk);
  716. return 0;
  717. }
  718. static const struct dev_pm_ops fsmc_nand_pm_ops = {
  719. .suspend = fsmc_nand_suspend,
  720. .resume = fsmc_nand_resume,
  721. };
  722. #endif
  723. static struct platform_driver fsmc_nand_driver = {
  724. .remove = fsmc_nand_remove,
  725. .driver = {
  726. .owner = THIS_MODULE,
  727. .name = "fsmc-nand",
  728. #ifdef CONFIG_PM
  729. .pm = &fsmc_nand_pm_ops,
  730. #endif
  731. },
  732. };
  733. static int __init fsmc_nand_init(void)
  734. {
  735. return platform_driver_probe(&fsmc_nand_driver,
  736. fsmc_nand_probe);
  737. }
  738. module_init(fsmc_nand_init);
  739. static void __exit fsmc_nand_exit(void)
  740. {
  741. platform_driver_unregister(&fsmc_nand_driver);
  742. }
  743. module_exit(fsmc_nand_exit);
  744. MODULE_LICENSE("GPL");
  745. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
  746. MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");