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