fsmc_nand.c 22 KB

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