fsmc_nand.c 23 KB

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