ath79_spinand.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /*
  2. * Copyright (c) 2015 The Linux Foundation. All rights reserved.
  3. *
  4. * Copyright (c) 2003-2013 Broadcom Corporation
  5. *
  6. * Copyright (c) 2009-2010 Micron Technology, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/delay.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <linux/mtd/nand.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/wait.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/sizes.h>
  28. /* cmd */
  29. #define ATH79_SPINAND_CMD_READ 0x13
  30. #define ATH79_SPINAND_CMD_READ_RDM 0x03
  31. #define ATH79_SPINAND_CMD_PROG_PAGE_LOAD 0x02
  32. #define ATH79_SPINAND_CMD_PROG_PAGE 0x84
  33. #define ATH79_SPINAND_CMD_PROG_PAGE_EXC 0x10
  34. #define ATH79_SPINAND_CMD_ERASE_BLK 0xd8
  35. #define ATH79_SPINAND_CMD_WR_ENABLE 0x06
  36. #define ATH79_SPINAND_CMD_WR_DISABLE 0x04
  37. #define ATH79_SPINAND_CMD_READ_ID 0x9f
  38. #define ATH79_SPINAND_CMD_RESET 0xff
  39. #define ATH79_SPINAND_CMD_READ_REG 0x0f
  40. #define ATH79_SPINAND_CMD_WRITE_REG 0x1f
  41. /* feature/ status reg */
  42. #define ATH79_SPINAND_REG_BLOCK_LOCK 0xa0
  43. #define ATH79_SPINAND_REG_OTP 0xb0
  44. #define ATH79_SPINAND_REG_STATUS 0xc0
  45. /* status */
  46. #define ATH79_SPINAND_STATUS_OIP_MASK 0x01
  47. #define ATH79_SPINAND_STATUS_READY 0
  48. #define ATH79_SPINAND_STATUS_BUSY BIT(1)
  49. #define ATH79_SPINAND_STATUS_E_FAIL_MASK 0x04
  50. #define ATH79_SPINAND_STATUS_E_FAIL BIT(2)
  51. #define ATH79_SPINAND_STATUS_P_FAIL_MASK 0x08
  52. #define ATH79_SPINAND_STATUS_P_FAIL BIT(3)
  53. /* ECC/OTP enable defines */
  54. #define ATH79_SPINAND_REG_ECC_MASK 0x10
  55. #define ATH79_SPINAND_REG_ECC_OFF 0
  56. #define ATH79_SPINAND_REG_ECC_ON BIT(4)
  57. #define ATH79_SPINAND_REG_OTP_EN BIT(6)
  58. #define ATH79_SPINAND_REG_OTP_PRT BIT(7)
  59. /* block lock */
  60. #define ATH79_SPINAND_BL_ALL_UNLOCKED 0
  61. #define ATH79_SPINAND_PAGE_TO_BLOCK(p) ((p) >> 6)
  62. #define ATH79_SPINAND_OFS_TO_PAGE(ofs) (((ofs) >> 17) << 6)
  63. #define ATH79_SPINAND_BUF_SIZE (2048 * 64)
  64. #define ATH79_SPINAND_BITS_PER_WORD(len) ((len) & 0x02 ? 16 : 32)
  65. struct ath79_spinand_priv {
  66. u8 mfr;
  67. u8 ecc_error;
  68. int ecc_size;
  69. int ecc_bytes;
  70. int ecc_strength;
  71. struct nand_ecclayout *ecc_layout;
  72. struct nand_bbt_descr *badblock_pattern;
  73. u8 (*ecc_status)(u8 status);
  74. void (*read_rdm_addr)(u32 offset, u8 *addr);
  75. int (*program_load)(struct spi_device *spi,
  76. u32 offset, u32 len, u8 *wbuf);
  77. int (*erase_block)(struct spi_device *spi, u32 page);
  78. int (*page_read_to_cache)(struct spi_device *spi, u32 page);
  79. int (*program_execute)(struct spi_device *spi, u32 page);
  80. };
  81. struct ath79_spinand_state {
  82. uint32_t col;
  83. uint32_t row;
  84. int buf_ptr;
  85. u8 *buf;
  86. };
  87. struct ath79_spinand_info {
  88. struct spi_device *spi;
  89. struct ath79_spinand_state *state;
  90. void *priv;
  91. };
  92. struct ath79_spinand_command {
  93. u8 cmd;
  94. u32 n_addr; /* Number of address */
  95. u8 addr[3]; /* Reg Offset */
  96. u32 n_dummy; /* Dummy use */
  97. u32 n_tx; /* Number of tx bytes */
  98. u8 *tx_buf; /* Tx buf */
  99. u32 n_rx; /* Number of rx bytes */
  100. u8 *rx_buf; /* Rx buf */
  101. };
  102. static u8 badblock_pattern[] = { 0xff, };
  103. static struct nand_bbt_descr ath79_badblock_pattern_default = {
  104. .options = 0,
  105. .offs = 0,
  106. .len = 1,
  107. .pattern = badblock_pattern,
  108. };
  109. static struct nand_ecclayout ath79_spinand_oob_128_gd = {
  110. .eccbytes = 64,
  111. .eccpos = {
  112. 64, 65, 66, 67, 68, 69, 70, 71,
  113. 72, 73, 74, 75, 76, 77, 78, 79,
  114. 80, 81, 82, 83, 84, 85, 86, 87,
  115. 88, 89, 90, 91, 92, 93, 94, 95,
  116. 96, 97, 98, 99, 100, 101, 102, 103,
  117. 104, 105, 106, 107, 108, 109, 110, 111,
  118. 112, 113, 114, 115, 116, 117, 118, 119,
  119. 120, 121, 122, 123, 124, 125, 126, 127},
  120. .oobfree = {
  121. {.offset = 16, .length = 48},
  122. }
  123. };
  124. /* ECC parity code stored in the additional hidden spare area */
  125. static struct nand_ecclayout ath79_spinand_oob_64_mx = {
  126. .eccbytes = 0,
  127. .eccpos = {},
  128. .oobfree = {
  129. {.offset = 4, .length = 4},
  130. {.offset = 20, .length = 4},
  131. {.offset = 36, .length = 4},
  132. {.offset = 52, .length = 4},
  133. }
  134. };
  135. static struct nand_ecclayout ath79_spinand_oob_64_win = {
  136. .eccbytes = 40,
  137. .eccpos = {
  138. 8, 9, 10, 11, 12, 13, 14, 15,
  139. 24, 25, 26, 27, 28, 29, 30, 31,
  140. 40, 41, 42, 43, 44, 45, 46, 47,
  141. 56, 57, 58, 59, 60, 61, 62, 63,
  142. 72, 73, 74, 75, 76, 77, 78, 79},
  143. .oobfree = {
  144. {.offset = 4, .length = 4},
  145. {.offset = 20, .length = 4},
  146. {.offset = 36, .length = 4},
  147. {.offset = 52, .length = 4},
  148. }
  149. };
  150. static inline struct ath79_spinand_state *mtd_to_state(struct mtd_info *mtd)
  151. {
  152. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  153. struct ath79_spinand_info *info = (struct ath79_spinand_info *)chip->priv;
  154. return info->state;
  155. }
  156. static inline struct ath79_spinand_priv *spi_to_priv(struct spi_device *spi)
  157. {
  158. struct mtd_info *mtd = dev_get_drvdata(&spi->dev);
  159. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  160. struct ath79_spinand_info *info = (struct ath79_spinand_info *)chip->priv;
  161. return info->priv;
  162. }
  163. static inline u8 ath79_spinand_ecc_status(struct spi_device *spi, u8 status)
  164. {
  165. struct ath79_spinand_priv *priv = spi_to_priv(spi);
  166. return priv->ecc_status(status);
  167. }
  168. static inline u8 ath79_spinand_ecc_error(struct spi_device *spi)
  169. {
  170. struct ath79_spinand_priv *priv = spi_to_priv(spi);
  171. return priv->ecc_error;
  172. }
  173. static inline void ath79_spinand_read_rdm_addr(struct spi_device *spi, u32 offset, u8 *addr)
  174. {
  175. struct ath79_spinand_priv *priv = spi_to_priv(spi);
  176. priv->read_rdm_addr(offset, addr);
  177. }
  178. static inline int ath79_spinand_program_load(struct spi_device *spi, u32 offset,
  179. u32 len, u8 *wbuf)
  180. {
  181. struct ath79_spinand_priv *priv = spi_to_priv(spi);
  182. return priv->program_load(spi, offset, len, wbuf);
  183. }
  184. static inline int ath79_spinand_erase_block_erase(struct spi_device *spi, u32 page)
  185. {
  186. struct ath79_spinand_priv *priv = spi_to_priv(spi);
  187. return priv->erase_block(spi, page);
  188. }
  189. static inline int ath79_spinand_read_page_to_cache(struct spi_device *spi, u32 page)
  190. {
  191. struct ath79_spinand_priv *priv = spi_to_priv(spi);
  192. return priv->page_read_to_cache(spi, page);
  193. }
  194. static inline int ath79_spinand_program_execute(struct spi_device *spi, u32 page)
  195. {
  196. struct ath79_spinand_priv *priv = spi_to_priv(spi);
  197. return priv->program_execute(spi, page);
  198. }
  199. static int ath79_spinand_cmd(struct spi_device *spi, struct ath79_spinand_command *cmd)
  200. {
  201. struct spi_message message;
  202. struct spi_transfer x[4];
  203. u8 dummy = 0xff;
  204. spi_message_init(&message);
  205. memset(x, 0, sizeof(x));
  206. x[0].len = 1;
  207. x[0].tx_buf = &cmd->cmd;
  208. spi_message_add_tail(&x[0], &message);
  209. if (cmd->n_addr) {
  210. x[1].len = cmd->n_addr;
  211. x[1].tx_buf = cmd->addr;
  212. spi_message_add_tail(&x[1], &message);
  213. }
  214. if (cmd->n_dummy) {
  215. x[2].len = cmd->n_dummy;
  216. x[2].tx_buf = &dummy;
  217. spi_message_add_tail(&x[2], &message);
  218. }
  219. if (cmd->n_tx) {
  220. x[3].len = cmd->n_tx;
  221. x[3].tx_buf = cmd->tx_buf;
  222. if (!(cmd->n_tx & 1))
  223. x[3].bits_per_word = ATH79_SPINAND_BITS_PER_WORD(cmd->n_tx);
  224. spi_message_add_tail(&x[3], &message);
  225. } else if (cmd->n_rx) {
  226. x[3].len = cmd->n_rx;
  227. x[3].rx_buf = cmd->rx_buf;
  228. if (!(cmd->n_rx & 1))
  229. x[3].bits_per_word = ATH79_SPINAND_BITS_PER_WORD(cmd->n_rx);
  230. spi_message_add_tail(&x[3], &message);
  231. }
  232. return spi_sync(spi, &message);
  233. }
  234. static int ath79_spinand_read_id(struct spi_device *spi_nand, u8 *id)
  235. {
  236. int retval;
  237. u8 nand_id[3];
  238. struct ath79_spinand_command cmd = {0};
  239. cmd.cmd = ATH79_SPINAND_CMD_READ_ID;
  240. cmd.n_rx = 3;
  241. cmd.rx_buf = nand_id;
  242. retval = ath79_spinand_cmd(spi_nand, &cmd);
  243. if (retval < 0) {
  244. dev_err(&spi_nand->dev, "error %d reading id\n", retval);
  245. return retval;
  246. }
  247. if (nand_id[0] == NAND_MFR_GIGADEVICE || nand_id[0] == NAND_MFR_HEYANGTEK) {
  248. id[0] = nand_id[0];
  249. id[1] = nand_id[1];
  250. } else { /* Macronix, Micron */
  251. id[0] = nand_id[1];
  252. id[1] = nand_id[2];
  253. }
  254. return retval;
  255. }
  256. static int ath79_spinand_read_status(struct spi_device *spi_nand, uint8_t *status)
  257. {
  258. struct ath79_spinand_command cmd = {0};
  259. int ret;
  260. cmd.cmd = ATH79_SPINAND_CMD_READ_REG;
  261. cmd.n_addr = 1;
  262. cmd.addr[0] = ATH79_SPINAND_REG_STATUS;
  263. cmd.n_rx = 1;
  264. cmd.rx_buf = status;
  265. ret = ath79_spinand_cmd(spi_nand, &cmd);
  266. if (ret < 0)
  267. dev_err(&spi_nand->dev, "err: %d read status register\n", ret);
  268. return ret;
  269. }
  270. #define ATH79_SPINAND_MAX_WAIT_JIFFIES (40 * HZ)
  271. static int __ath79_wait_till_ready(struct spi_device *spi_nand, u8 *status)
  272. {
  273. unsigned long deadline;
  274. deadline = jiffies + ATH79_SPINAND_MAX_WAIT_JIFFIES;
  275. do {
  276. if (ath79_spinand_read_status(spi_nand, status))
  277. return -1;
  278. else if ((*status & ATH79_SPINAND_STATUS_OIP_MASK) == ATH79_SPINAND_STATUS_READY)
  279. break;
  280. cond_resched();
  281. } while (!time_after_eq(jiffies, deadline));
  282. return 0;
  283. }
  284. static int ath79_wait_till_ready(struct spi_device *spi_nand)
  285. {
  286. u8 stat = 0;
  287. if (__ath79_wait_till_ready(spi_nand, &stat))
  288. return -1;
  289. if ((stat & ATH79_SPINAND_STATUS_OIP_MASK) == ATH79_SPINAND_STATUS_READY)
  290. return 0;
  291. return -1;
  292. }
  293. static int ath79_spinand_get_otp(struct spi_device *spi_nand, u8 *otp)
  294. {
  295. struct ath79_spinand_command cmd = {0};
  296. int retval;
  297. cmd.cmd = ATH79_SPINAND_CMD_READ_REG;
  298. cmd.n_addr = 1;
  299. cmd.addr[0] = ATH79_SPINAND_REG_OTP;
  300. cmd.n_rx = 1;
  301. cmd.rx_buf = otp;
  302. retval = ath79_spinand_cmd(spi_nand, &cmd);
  303. if (retval < 0)
  304. dev_err(&spi_nand->dev, "error %d get otp\n", retval);
  305. return retval;
  306. }
  307. static int ath79_spinand_set_otp(struct spi_device *spi_nand, u8 *otp)
  308. {
  309. int retval;
  310. struct ath79_spinand_command cmd = {0};
  311. cmd.cmd = ATH79_SPINAND_CMD_WRITE_REG,
  312. cmd.n_addr = 1,
  313. cmd.addr[0] = ATH79_SPINAND_REG_OTP,
  314. cmd.n_tx = 1,
  315. cmd.tx_buf = otp,
  316. retval = ath79_spinand_cmd(spi_nand, &cmd);
  317. if (retval < 0)
  318. dev_err(&spi_nand->dev, "error %d set otp\n", retval);
  319. return retval;
  320. }
  321. static int ath79_spinand_enable_ecc(struct spi_device *spi_nand)
  322. {
  323. u8 otp = 0;
  324. if (ath79_spinand_get_otp(spi_nand, &otp))
  325. return -1;
  326. if ((otp & ATH79_SPINAND_REG_ECC_MASK) == ATH79_SPINAND_REG_ECC_ON)
  327. return 0;
  328. otp |= ATH79_SPINAND_REG_ECC_ON;
  329. if (ath79_spinand_set_otp(spi_nand, &otp))
  330. return -1;
  331. return ath79_spinand_get_otp(spi_nand, &otp);
  332. }
  333. static int ath79_spinand_disable_ecc(struct spi_device *spi_nand)
  334. {
  335. u8 otp = 0;
  336. if (ath79_spinand_get_otp(spi_nand, &otp))
  337. return -1;
  338. if ((otp & ATH79_SPINAND_REG_ECC_MASK) == ATH79_SPINAND_REG_ECC_OFF)
  339. return 0;
  340. otp &= ~ATH79_SPINAND_REG_ECC_MASK;
  341. if (ath79_spinand_set_otp(spi_nand, &otp))
  342. return -1;
  343. return ath79_spinand_get_otp(spi_nand, &otp);
  344. }
  345. static int ath79_spinand_write_enable(struct spi_device *spi_nand)
  346. {
  347. struct ath79_spinand_command cmd = {0};
  348. cmd.cmd = ATH79_SPINAND_CMD_WR_ENABLE;
  349. return ath79_spinand_cmd(spi_nand, &cmd);
  350. }
  351. static int ath79_spinand_read_from_cache(struct spi_device *spi_nand,
  352. u32 offset, u32 len, u8 *rbuf)
  353. {
  354. struct ath79_spinand_command cmd = {0};
  355. ath79_spinand_read_rdm_addr(spi_nand, offset, cmd.addr);
  356. cmd.cmd = ATH79_SPINAND_CMD_READ_RDM;
  357. cmd.n_addr = 3;
  358. cmd.n_dummy = 0;
  359. cmd.n_rx = len;
  360. cmd.rx_buf = rbuf;
  361. return ath79_spinand_cmd(spi_nand, &cmd);
  362. }
  363. static int ath79_spinand_read_page(struct spi_device *spi_nand, u32 page_id,
  364. u32 offset, u32 len, u8 *rbuf)
  365. {
  366. int ret;
  367. u8 status = 0;
  368. ret = ath79_spinand_read_page_to_cache(spi_nand, page_id);
  369. if (ret < 0) {
  370. dev_err(&spi_nand->dev, "Read page to cache failed!\n");
  371. return ret;
  372. }
  373. if (__ath79_wait_till_ready(spi_nand, &status)) {
  374. dev_err(&spi_nand->dev, "WAIT timedout!\n");
  375. return -EBUSY;
  376. }
  377. if ((status & ATH79_SPINAND_STATUS_OIP_MASK) != ATH79_SPINAND_STATUS_READY)
  378. return -EBUSY;
  379. status = ath79_spinand_ecc_status(spi_nand, status);
  380. if (ath79_spinand_ecc_error(spi_nand) == status) {
  381. dev_err(&spi_nand->dev,
  382. "ecc error, page=%d\n", page_id);
  383. return -1;
  384. }
  385. ret = ath79_spinand_read_from_cache(spi_nand, offset, len, rbuf);
  386. if (ret < 0) {
  387. dev_err(&spi_nand->dev, "read from cache failed!!\n");
  388. return ret;
  389. }
  390. return ret;
  391. }
  392. static int ath79_spinand_program_data_to_cache(struct spi_device *spi_nand,
  393. u32 offset, u32 len, u8 *wbuf)
  394. {
  395. struct ath79_spinand_command cmd = {0};
  396. cmd.cmd = ATH79_SPINAND_CMD_PROG_PAGE_LOAD;
  397. cmd.n_addr = 2;
  398. cmd.addr[0] = (u8)(offset >> 8);
  399. cmd.addr[1] = (u8)(offset >> 0);
  400. cmd.n_tx = len;
  401. cmd.tx_buf = wbuf;
  402. return ath79_spinand_cmd(spi_nand, &cmd);
  403. }
  404. static int ath79_spinand_program_page(struct spi_device *spi_nand,
  405. u32 page_id, u32 offset, u32 len, u8 *buf, u32 cache_size)
  406. {
  407. int retval;
  408. u8 status = 0;
  409. if (unlikely(offset)) {
  410. uint8_t *wbuf;
  411. unsigned int i, j;
  412. wbuf = devm_kzalloc(&spi_nand->dev, cache_size, GFP_KERNEL);
  413. if (!wbuf) {
  414. dev_err(&spi_nand->dev, "No memory\n");
  415. return -ENOMEM;
  416. }
  417. if (ath79_spinand_read_page(spi_nand, page_id, 0, cache_size, wbuf)) {
  418. devm_kfree(&spi_nand->dev, wbuf);
  419. return -1;
  420. }
  421. for (i = offset, j = 0; i < len; i++, j++)
  422. wbuf[i] &= buf[j];
  423. retval = ath79_spinand_program_load(spi_nand, offset, len, wbuf);
  424. devm_kfree(&spi_nand->dev, wbuf);
  425. } else {
  426. retval = ath79_spinand_program_load(spi_nand, offset, len, buf);
  427. }
  428. if (retval < 0)
  429. return retval;
  430. retval = ath79_spinand_program_execute(spi_nand, page_id);
  431. if (retval < 0) {
  432. dev_err(&spi_nand->dev, "program execute failed\n");
  433. return retval;
  434. }
  435. if (__ath79_wait_till_ready(spi_nand, &status)) {
  436. dev_err(&spi_nand->dev, "wait timedout!!!\n");
  437. return -EBUSY;
  438. }
  439. if ((status & ATH79_SPINAND_STATUS_OIP_MASK) != ATH79_SPINAND_STATUS_READY)
  440. return -EBUSY;
  441. if ((status & ATH79_SPINAND_STATUS_P_FAIL_MASK) == ATH79_SPINAND_STATUS_P_FAIL) {
  442. dev_err(&spi_nand->dev,
  443. "program error, page %d\n", page_id);
  444. return -1;
  445. }
  446. return 0;
  447. }
  448. static int ath79_spinand_erase_block(struct mtd_info *mtd,
  449. struct spi_device *spi_nand, u32 page)
  450. {
  451. int retval;
  452. u8 status = 0;
  453. retval = ath79_spinand_write_enable(spi_nand);
  454. if (retval < 0) {
  455. dev_err(&spi_nand->dev, "write enable failed!\n");
  456. return retval;
  457. }
  458. if (ath79_wait_till_ready(spi_nand)) {
  459. dev_err(&spi_nand->dev, "wait timedout!\n");
  460. return -EBUSY;
  461. }
  462. retval = ath79_spinand_erase_block_erase(spi_nand, page);
  463. if (retval < 0) {
  464. dev_err(&spi_nand->dev, "erase block failed!\n");
  465. return retval;
  466. }
  467. if (__ath79_wait_till_ready(spi_nand, &status)) {
  468. dev_err(&spi_nand->dev, "wait timedout!\n");
  469. return -EBUSY;
  470. }
  471. if ((status & ATH79_SPINAND_STATUS_OIP_MASK) != ATH79_SPINAND_STATUS_READY)
  472. return -EBUSY;
  473. if ((status & ATH79_SPINAND_STATUS_E_FAIL_MASK) == ATH79_SPINAND_STATUS_E_FAIL) {
  474. dev_err(&spi_nand->dev,
  475. "erase error, block %d\n", ATH79_SPINAND_PAGE_TO_BLOCK(page));
  476. return -1;
  477. }
  478. return 0;
  479. }
  480. static int ath79_spinand_write_page_hwecc(struct mtd_info *mtd,
  481. struct nand_chip *chip,
  482. const uint8_t *buf,
  483. int oob_required)
  484. {
  485. chip->write_buf(mtd, buf, chip->ecc.size * chip->ecc.steps);
  486. return 0;
  487. }
  488. static int ath79_spinand_read_page_hwecc(struct mtd_info *mtd,
  489. struct nand_chip *chip,
  490. uint8_t *buf,
  491. int oob_required,
  492. int page)
  493. {
  494. u8 status;
  495. uint8_t *p = buf;
  496. struct ath79_spinand_info *info =
  497. (struct ath79_spinand_info *)chip->priv;
  498. struct spi_device *spi_nand = info->spi;
  499. chip->read_buf(mtd, p, chip->ecc.size * chip->ecc.steps);
  500. if (oob_required)
  501. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  502. if (__ath79_wait_till_ready(spi_nand, &status)) {
  503. dev_err(&spi_nand->dev, "wait timedout!\n");
  504. return -EBUSY;
  505. }
  506. if ((status & ATH79_SPINAND_STATUS_OIP_MASK) != ATH79_SPINAND_STATUS_READY)
  507. return -EBUSY;
  508. status = ath79_spinand_ecc_status(spi_nand, status);
  509. if (ath79_spinand_ecc_error(spi_nand) == status) {
  510. pr_info("%s: Internal ECC error\n", __func__);
  511. mtd->ecc_stats.failed++;
  512. } else if (status) {
  513. pr_debug("%s: Internal ECC error corrected\n", __func__);
  514. mtd->ecc_stats.corrected++;
  515. }
  516. return 0;
  517. }
  518. static void ath79_spinand_select_chip(struct mtd_info *mtd, int dev)
  519. {
  520. }
  521. /*
  522. * Mark bad block: the 1st page of the bad block set to zero, include
  523. * main area and spare area.
  524. * NOTE, irreversible and erase is not recommanded
  525. */
  526. static int ath79_spinand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  527. {
  528. u8 status;
  529. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  530. struct ath79_spinand_info *info = (struct ath79_spinand_info *)chip->priv;
  531. struct spi_device *spi_nand = info->spi;
  532. struct ath79_spinand_state *state = info->state;
  533. int page_len = mtd->writesize + mtd->oobsize;
  534. int retval, page_id = ATH79_SPINAND_OFS_TO_PAGE(ofs);;
  535. memset(state->buf, 0, page_len);
  536. retval = ath79_spinand_program_load(spi_nand, 0, page_len, state->buf);
  537. if (retval < 0)
  538. return retval;
  539. retval = ath79_spinand_program_execute(spi_nand, page_id);
  540. if (retval < 0) {
  541. dev_err(&spi_nand->dev, "program execute failed\n");
  542. return retval;
  543. }
  544. if (__ath79_wait_till_ready(spi_nand, &status)) {
  545. dev_err(&spi_nand->dev, "wait timedout!!!\n");
  546. return -EBUSY;
  547. }
  548. if ((status & ATH79_SPINAND_STATUS_OIP_MASK) != ATH79_SPINAND_STATUS_READY)
  549. return -EBUSY;
  550. if ((status & ATH79_SPINAND_STATUS_P_FAIL_MASK) == ATH79_SPINAND_STATUS_P_FAIL) {
  551. dev_err(&spi_nand->dev,
  552. "program error, page %d\n", page_id);
  553. return -1;
  554. }
  555. return 0;
  556. }
  557. static uint8_t ath79_spinand_read_byte(struct mtd_info *mtd)
  558. {
  559. struct ath79_spinand_state *state = mtd_to_state(mtd);
  560. return state->buf[state->buf_ptr++];
  561. }
  562. static int ath79_spinand_wait(struct mtd_info *mtd, struct nand_chip *chip)
  563. {
  564. struct ath79_spinand_info *info = (struct ath79_spinand_info *)chip->priv;
  565. unsigned long timeo = jiffies;
  566. u8 status;
  567. if (chip->state == FL_ERASING)
  568. timeo += (HZ * 400) / 1000;
  569. else
  570. timeo += (HZ * 20) / 1000;
  571. while (time_before(jiffies, timeo)) {
  572. if (ath79_spinand_read_status(info->spi, &status))
  573. return NAND_STATUS_FAIL;
  574. if ((status & ATH79_SPINAND_STATUS_OIP_MASK) == ATH79_SPINAND_STATUS_READY) {
  575. status = ath79_spinand_ecc_status(info->spi, status);
  576. return (ath79_spinand_ecc_error(info->spi) == status) ?
  577. NAND_STATUS_FAIL : NAND_STATUS_READY;
  578. }
  579. cond_resched();
  580. }
  581. return NAND_STATUS_FAIL;
  582. }
  583. static void ath79_spinand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  584. {
  585. struct ath79_spinand_state *state = mtd_to_state(mtd);
  586. BUG_ON(ATH79_SPINAND_BUF_SIZE - state->buf_ptr < len);
  587. memcpy(state->buf + state->buf_ptr, buf, len);
  588. state->buf_ptr += len;
  589. }
  590. static void ath79_spinand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  591. {
  592. struct ath79_spinand_state *state = mtd_to_state(mtd);
  593. memcpy(buf, state->buf + state->buf_ptr, len);
  594. state->buf_ptr += len;
  595. }
  596. static void ath79_spinand_reset(struct spi_device *spi_nand)
  597. {
  598. struct ath79_spinand_command cmd = {0};
  599. cmd.cmd = ATH79_SPINAND_CMD_RESET;
  600. if (ath79_spinand_cmd(spi_nand, &cmd) < 0)
  601. pr_info("ath79_spinand reset failed!\n");
  602. /* elapse 1ms before issuing any other command */
  603. udelay(1000);
  604. if (ath79_wait_till_ready(spi_nand))
  605. dev_err(&spi_nand->dev, "wait timedout!\n");
  606. }
  607. static void ath79_spinand_cmdfunc(struct mtd_info *mtd, unsigned int command,
  608. int column, int page)
  609. {
  610. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  611. struct ath79_spinand_info *info = (struct ath79_spinand_info *)chip->priv;
  612. struct ath79_spinand_state *state = info->state;
  613. switch (command) {
  614. case NAND_CMD_READ1:
  615. case NAND_CMD_READ0:
  616. state->buf_ptr = 0;
  617. ath79_spinand_read_page(info->spi, page, 0x0,
  618. mtd->writesize + mtd->oobsize,
  619. state->buf);
  620. break;
  621. case NAND_CMD_READOOB:
  622. state->buf_ptr = 0;
  623. ath79_spinand_read_page(info->spi, page, mtd->writesize,
  624. mtd->oobsize, state->buf);
  625. break;
  626. case NAND_CMD_RNDOUT:
  627. state->buf_ptr = column;
  628. break;
  629. case NAND_CMD_READID:
  630. state->buf_ptr = 0;
  631. ath79_spinand_read_id(info->spi, (u8 *)state->buf);
  632. break;
  633. case NAND_CMD_PARAM:
  634. state->buf_ptr = 0;
  635. break;
  636. /* ERASE1 stores the block and page address */
  637. case NAND_CMD_ERASE1:
  638. ath79_spinand_erase_block(mtd, info->spi, page);
  639. break;
  640. /* ERASE2 uses the block and page address from ERASE1 */
  641. case NAND_CMD_ERASE2:
  642. break;
  643. /* SEQIN sets up the addr buffer and all registers except the length */
  644. case NAND_CMD_SEQIN:
  645. state->col = column;
  646. state->row = page;
  647. state->buf_ptr = 0;
  648. break;
  649. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  650. case NAND_CMD_PAGEPROG:
  651. ath79_spinand_program_page(info->spi, state->row, state->col,
  652. state->buf_ptr, state->buf,
  653. mtd->writesize + mtd->oobsize);
  654. break;
  655. case NAND_CMD_STATUS:
  656. ath79_spinand_get_otp(info->spi, state->buf);
  657. if (!(state->buf[0] & ATH79_SPINAND_REG_OTP_PRT))
  658. state->buf[0] = ATH79_SPINAND_REG_OTP_PRT;
  659. state->buf_ptr = 0;
  660. break;
  661. /* RESET command */
  662. case NAND_CMD_RESET:
  663. if (ath79_wait_till_ready(info->spi))
  664. dev_err(&info->spi->dev, "WAIT timedout!!!\n");
  665. /* a minimum of 250us must elapse before issuing RESET cmd*/
  666. udelay(250);
  667. ath79_spinand_reset(info->spi);
  668. break;
  669. default:
  670. dev_err(&mtd->dev, "Unknown CMD: 0x%x\n", command);
  671. }
  672. }
  673. static int ath79_spinand_lock_block(struct spi_device *spi_nand, u8 lock)
  674. {
  675. struct ath79_spinand_command cmd = {0};
  676. int ret;
  677. cmd.cmd = ATH79_SPINAND_CMD_WRITE_REG;
  678. cmd.n_addr = 1;
  679. cmd.addr[0] = ATH79_SPINAND_REG_BLOCK_LOCK;
  680. cmd.n_tx = 1;
  681. cmd.tx_buf = &lock;
  682. ret = ath79_spinand_cmd(spi_nand, &cmd);
  683. if (ret < 0)
  684. dev_err(&spi_nand->dev, "error %d lock block\n", ret);
  685. return ret;
  686. }
  687. /*
  688. * ECCSR[2:0] ECC Status
  689. * -------------------------------------------
  690. * 000 no bit errors were detected
  691. * 001 bit errors(<3) corrected
  692. * 010 bit errors(=4) corrected
  693. * 011 bit errors(=5) corrected
  694. * 100 bit errors(=6) corrected
  695. * 101 bit errors(=7) corrected
  696. * 110 bit errors(=8) corrected
  697. * 111 uncorrectable
  698. */
  699. static inline u8 ath79_spinand_eccsr_gd(u8 status)
  700. {
  701. return status >> 4 & 0x7;
  702. }
  703. /*
  704. * ECCSR[1:0] ECC Status
  705. * -------------------------------------------
  706. * 00 no bit errors were detected
  707. * 01 bit errors(1~4) corrected
  708. * 10 uncorrectable
  709. * 11 reserved
  710. */
  711. static inline u8 ath79_spinand_eccsr_common(u8 status)
  712. {
  713. return status >> 4 & 0x3;
  714. }
  715. static inline void ath79_spinand_read_rdm_addr_gd(u32 offset, u8 *addr)
  716. {
  717. addr[0] = 0xff; /* dummy byte */
  718. addr[1] = (u8)(offset >> 8);
  719. addr[2] = (u8)(offset >> 0);
  720. }
  721. static inline void ath79_spinand_read_rdm_addr_common(u32 offset, u8 *addr)
  722. {
  723. addr[0] = (u8)(offset >> 8);
  724. addr[1] = (u8)(offset >> 0);
  725. addr[2] = 0xff; /* dummy byte */
  726. }
  727. static inline int ath79_spinand_program_load_gd(struct spi_device *spi, u32 offset,
  728. u32 len, u8 *wbuf)
  729. {
  730. int retval;
  731. retval = ath79_spinand_program_data_to_cache(spi, offset, len, wbuf);
  732. if (retval < 0) {
  733. dev_err(&spi->dev, "program data to cache failed\n");
  734. return retval;
  735. }
  736. retval = ath79_spinand_write_enable(spi);
  737. if (retval < 0) {
  738. dev_err(&spi->dev, "write enable failed!!\n");
  739. return retval;
  740. }
  741. if (ath79_wait_till_ready(spi)) {
  742. dev_err(&spi->dev, "wait timedout!!!\n");
  743. return -EBUSY;
  744. }
  745. return 0;
  746. }
  747. static inline int ath79_spinand_program_load_common(struct spi_device *spi, u32 offset,
  748. u32 len, u8 *wbuf)
  749. {
  750. int retval;
  751. retval = ath79_spinand_write_enable(spi);
  752. if (retval < 0) {
  753. dev_err(&spi->dev, "write enable failed!!\n");
  754. return retval;
  755. }
  756. retval = ath79_spinand_program_data_to_cache(spi, offset, len, wbuf);
  757. if (retval < 0) {
  758. dev_err(&spi->dev, "program data to cache failed\n");
  759. return retval;
  760. }
  761. if (ath79_wait_till_ready(spi)) {
  762. dev_err(&spi->dev, "wait timedout!!!\n");
  763. return -EBUSY;
  764. }
  765. return 0;
  766. }
  767. static inline int __ath79_spinand_execute_cmd_common(struct spi_device *spi,
  768. u32 page, u8 cmd_id)
  769. {
  770. struct ath79_spinand_command cmd = {0};
  771. cmd.cmd = cmd_id;
  772. cmd.n_addr = 3;
  773. cmd.addr[0] = (u8)(page >> 16);
  774. cmd.addr[1] = (u8)(page >> 8);
  775. cmd.addr[2] = (u8)(page >> 0);
  776. return ath79_spinand_cmd(spi, &cmd);
  777. }
  778. static inline int __ath79_spinand_execute_cmd_win(struct spi_device *spi,
  779. u32 page, u8 cmd_id)
  780. {
  781. struct ath79_spinand_command cmd = {0};
  782. cmd.cmd = cmd_id;
  783. cmd.n_addr = 3;
  784. cmd.addr[0] = 0;
  785. cmd.addr[1] = (u8)(page >> 8);
  786. cmd.addr[2] = (u8)(page >> 0);
  787. return ath79_spinand_cmd(spi, &cmd);
  788. }
  789. static inline int ath79_spinand_erase_block_erase_common(struct spi_device *spi_nand, u32 page)
  790. {
  791. return __ath79_spinand_execute_cmd_common(spi_nand, page, ATH79_SPINAND_CMD_ERASE_BLK);
  792. }
  793. static inline int ath79_spinand_erase_block_erase_win(struct spi_device *spi_nand, u32 page)
  794. {
  795. return __ath79_spinand_execute_cmd_win(spi_nand, page, ATH79_SPINAND_CMD_ERASE_BLK);
  796. }
  797. static inline int ath79_spinand_page_read_to_cache_common(struct spi_device *spi_nand, u32 page)
  798. {
  799. return __ath79_spinand_execute_cmd_common(spi_nand, page, ATH79_SPINAND_CMD_READ);
  800. }
  801. static inline int ath79_spinand_page_read_to_cache_win(struct spi_device *spi_nand, u32 page)
  802. {
  803. return __ath79_spinand_execute_cmd_win(spi_nand, page, ATH79_SPINAND_CMD_READ);
  804. }
  805. static inline int ath79_spinand_program_execute_common(struct spi_device *spi_nand, u32 page)
  806. {
  807. return __ath79_spinand_execute_cmd_common(spi_nand, page, ATH79_SPINAND_CMD_PROG_PAGE_EXC);
  808. }
  809. static inline int ath79_spinand_program_execute_win(struct spi_device *spi_nand, u32 page)
  810. {
  811. return __ath79_spinand_execute_cmd_win(spi_nand, page, ATH79_SPINAND_CMD_PROG_PAGE_EXC);
  812. }
  813. static struct ath79_spinand_priv ath79_spinand_ids[] = {
  814. { /* Giga Device */
  815. NAND_MFR_GIGADEVICE, /* manufacturer */
  816. 0x07, /* ecc error code */
  817. SZ_512, /* ecc size */
  818. 16, /* ecc bytes */
  819. 1, /* ecc strength */
  820. &ath79_spinand_oob_128_gd, /* ecc layout */
  821. &ath79_badblock_pattern_default, /* bad block pattern */
  822. ath79_spinand_eccsr_gd, /* get ecc status */
  823. ath79_spinand_read_rdm_addr_gd, /* wrap address for 03h command */
  824. ath79_spinand_program_load_gd, /* program load data to cache */
  825. ath79_spinand_erase_block_erase_common, /* erase block */
  826. ath79_spinand_page_read_to_cache_common,/* page read to cache */
  827. ath79_spinand_program_execute_common, /* program execute */
  828. },
  829. { /* Macronix */
  830. NAND_MFR_MACRONIX, /* manufacturer*/
  831. 0x02, /* ecc error code */
  832. SZ_512, /* ecc size */
  833. 7, /* ecc bytes */
  834. 1, /* ecc strength */
  835. &ath79_spinand_oob_64_mx, /* ecc layout */
  836. &ath79_badblock_pattern_default, /* bad block pattern */
  837. ath79_spinand_eccsr_common, /* get ecc status */
  838. ath79_spinand_read_rdm_addr_common, /* wrap address for 03h command */
  839. ath79_spinand_program_load_common, /* program load data to cache */
  840. ath79_spinand_erase_block_erase_common, /* erase block */
  841. ath79_spinand_page_read_to_cache_common,/* page read to cache */
  842. ath79_spinand_program_execute_common, /* program execute */
  843. },
  844. { /* Winbond */
  845. NAND_MFR_WINBOND, /* manufacturer*/
  846. 0x02, /* ecc error code */
  847. SZ_512, /* ecc size */
  848. 10, /* ecc bytes */
  849. 1, /* ecc strength */
  850. &ath79_spinand_oob_64_win, /* ecc layout */
  851. &ath79_badblock_pattern_default, /* bad block pattern */
  852. ath79_spinand_eccsr_common, /* get ecc status */
  853. ath79_spinand_read_rdm_addr_common, /* wrap address for 03h command */
  854. ath79_spinand_program_load_common, /* program load data to cache */
  855. ath79_spinand_erase_block_erase_win, /* erase block */
  856. ath79_spinand_page_read_to_cache_win, /* page read to cache */
  857. ath79_spinand_program_execute_win, /* program execute */
  858. },
  859. { /* HeYang Tek (the same as Giga Device) */
  860. NAND_MFR_HEYANGTEK, /* manufacturer */
  861. 0x07, /* ecc error code */
  862. SZ_512, /* ecc size */
  863. 16, /* ecc bytes */
  864. 1, /* ecc strength */
  865. &ath79_spinand_oob_128_gd, /* ecc layout */
  866. &ath79_badblock_pattern_default, /* bad block pattern */
  867. ath79_spinand_eccsr_gd, /* get ecc status */
  868. ath79_spinand_read_rdm_addr_gd, /* wrap address for 03h command */
  869. ath79_spinand_program_load_gd, /* program load data to cache */
  870. ath79_spinand_erase_block_erase_common, /* erase block */
  871. ath79_spinand_page_read_to_cache_common,/* page read to cache */
  872. ath79_spinand_program_execute_common, /* program execute */
  873. },
  874. };
  875. static void *ath79_spinand_priv_data_get(struct spi_device *spi_nand)
  876. {
  877. u8 id[3];
  878. int i;
  879. ath79_spinand_read_id(spi_nand, id);
  880. for (i = 0; i < ARRAY_SIZE(ath79_spinand_ids); i++)
  881. if (ath79_spinand_ids[i].mfr == id[0])
  882. return &ath79_spinand_ids[i];
  883. return NULL;
  884. }
  885. static int ath79_spinand_probe(struct spi_device *spi_nand)
  886. {
  887. struct mtd_info *mtd;
  888. struct nand_chip *chip;
  889. struct ath79_spinand_info *info;
  890. struct ath79_spinand_state *state;
  891. struct ath79_spinand_priv *priv_data;
  892. priv_data = ath79_spinand_priv_data_get(spi_nand);
  893. if (!priv_data)
  894. return -ENXIO;
  895. info = devm_kzalloc(&spi_nand->dev, sizeof(struct ath79_spinand_info),
  896. GFP_KERNEL);
  897. if (!info)
  898. return -ENOMEM;
  899. info->spi = spi_nand;
  900. ath79_spinand_lock_block(spi_nand, ATH79_SPINAND_BL_ALL_UNLOCKED);
  901. ath79_spinand_disable_ecc(spi_nand);
  902. state = devm_kzalloc(&spi_nand->dev, sizeof(struct ath79_spinand_state),
  903. GFP_KERNEL);
  904. if (!state)
  905. return -ENOMEM;
  906. info->state = state;
  907. info->priv = priv_data;
  908. state->buf_ptr = 0;
  909. state->buf = devm_kzalloc(&spi_nand->dev, ATH79_SPINAND_BUF_SIZE, GFP_KERNEL);
  910. if (!state->buf)
  911. return -ENOMEM;
  912. chip = devm_kzalloc(&spi_nand->dev, sizeof(struct nand_chip),
  913. GFP_KERNEL);
  914. if (!chip)
  915. return -ENOMEM;
  916. chip->ecc.mode = NAND_ECC_HW;
  917. chip->ecc.size = priv_data->ecc_size;
  918. chip->ecc.bytes = priv_data->ecc_bytes;
  919. chip->ecc.strength = priv_data->ecc_strength;
  920. chip->ecc.layout = priv_data->ecc_layout;
  921. chip->badblock_pattern = priv_data->badblock_pattern;
  922. chip->ecc.read_page = ath79_spinand_read_page_hwecc;
  923. chip->ecc.write_page = ath79_spinand_write_page_hwecc;
  924. chip->priv = info;
  925. chip->read_buf = ath79_spinand_read_buf;
  926. chip->write_buf = ath79_spinand_write_buf;
  927. chip->read_byte = ath79_spinand_read_byte;
  928. chip->cmdfunc = ath79_spinand_cmdfunc;
  929. chip->waitfunc = ath79_spinand_wait;
  930. chip->options = NAND_CACHEPRG | NAND_NO_SUBPAGE_WRITE;
  931. chip->select_chip = ath79_spinand_select_chip;
  932. chip->block_markbad = ath79_spinand_block_markbad;
  933. mtd = devm_kzalloc(&spi_nand->dev, sizeof(struct mtd_info), GFP_KERNEL);
  934. if (!mtd)
  935. return -ENOMEM;
  936. dev_set_drvdata(&spi_nand->dev, mtd);
  937. mtd->priv = chip;
  938. mtd->name = dev_name(&spi_nand->dev);
  939. mtd->owner = THIS_MODULE;
  940. if (nand_scan(mtd, 1))
  941. return -ENXIO;
  942. ath79_spinand_enable_ecc(spi_nand);
  943. return mtd_device_parse_register(mtd, NULL, NULL, NULL, 0);
  944. }
  945. static int ath79_spinand_remove(struct spi_device *spi)
  946. {
  947. mtd_device_unregister(dev_get_drvdata(&spi->dev));
  948. return 0;
  949. }
  950. static struct spi_driver ath79_spinand_driver = {
  951. .driver = {
  952. .name = "ath79-spinand",
  953. .bus = &spi_bus_type,
  954. .owner = THIS_MODULE,
  955. },
  956. .probe = ath79_spinand_probe,
  957. .remove = ath79_spinand_remove,
  958. };
  959. module_spi_driver(ath79_spinand_driver);
  960. MODULE_DESCRIPTION("SPI NAND driver for Giga Device/Macronix");
  961. MODULE_LICENSE("GPL v2");