fsmc_nand.c 22 KB

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